linux - C segmentation fault using shared library -


have write program displaying list of logged users (their id , groups if -i or -g used) using shared library. functions in shared library working correctly, seems function pointer causes segmentation fault. since it's school assigment have use c , functions have return nothing, , take user name. i'm quite new programming in linux (obviously).

main code:

#include <unistd.h> #include <stdlib.h> #include <utmp.h> #include <dlfcn.h>  struct utmp *p;  int main(int argc, char* argv[]){   int opt, iflag = 0;  int gflag = 0; void *handle;    while((opt = getopt(argc, argv, "ig")) != -1) {     switch(opt)     {         case 'i':             iflag = 1;             break;         case 'g':             gflag = 1;             break;         default:         fprintf(stderr, "błąd \n");         exit(exit_failure);     } }     handle = dlopen("./zad2lib.so", rtld_lazy);  if (handle == null) {     fprintf(stderr, "unable open library: %s\n", dlerror());     iflag = 0;     gflag = 0;  } while ((p = getutent()) != null)    {     if (p->ut_type == user_process)     {         if (iflag == 1) {             dlerror();              void (*func)(char*) = (void(*)())dlsym(handle, "userid");             func(p->ut_user);          }              printf("%s ", p->ut_user);          if (gflag == 1) {             dlerror();               void (*func)(char*) = (void(*)())dlsym(handle, "usergroups");              func(p->ut_user);             }         }                    printf("\n");        }     dlclose(handle); } return 0; } 

.so file:

#include <sys/types.h> #include <grp.h> #include <pwd.h> #include <stdio.h> #include <stdlib.h>  void userid(char *name) {     struct passwd *pw = getpwnam(name);     printf("%d ", pw->pw_uid); }  void usergroups(char *name) {      struct passwd *pw = getpwnam(name);     int i;     int gidsize = 100;     struct group* g;     gid_t *grouplist = malloc(gidsize*sizeof(gid_t));     getgrouplist(name, pw->pw_gid, grouplist, &gidsize);     printf("[");     (i = 0; < gidsize; i++)   {         g = getgrgid(grouplist[i]);         printf(" %s", g->gr_name);      }     printf(" ]");     free(grouplist); } 


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -