Make root see all available pids v2
[ust.git] / libustctl / libustctl.c
index e9709c574e0bbe1e2b82a0b27e784288b5e8ec67..356d5ef7235536a67b405f07431aed96018fbe69 100644 (file)
@@ -88,53 +88,133 @@ int ustctl_connect_pid(pid_t pid)
        return sock;
 }
 
-pid_t *ustctl_get_online_pids(void)
+static void get_pids_in_dir(DIR *dir, pid_t **pid_list,
+                           unsigned int *pid_list_size)
 {
        struct dirent *dirent;
-       DIR *dir;
-       unsigned int ret_size = 1 * sizeof(pid_t), i = 0;
-
-       dir = opendir(SOCK_DIR);
-       if (!dir) {
-               return NULL;
-       }
-
-       pid_t *ret = (pid_t *) malloc(ret_size);
+       unsigned int read_pid;
 
        while ((dirent = readdir(dir))) {
                if (!strcmp(dirent->d_name, ".") ||
-                   !strcmp(dirent->d_name, "..")) {
+                   !strcmp(dirent->d_name, "..") ||
+                   !strcmp(dirent->d_name, "ust-consumer") ||
+                   dirent->d_type == DT_DIR) {
 
                        continue;
                }
 
-               if (dirent->d_type != DT_DIR &&
-                   !!strcmp(dirent->d_name, "ust-consumer")) {
-
-                       sscanf(dirent->d_name, "%u", (unsigned int *) &ret[i]);
-                       /* FIXME: Here we previously called pid_is_online, which
-                        * always returned 1, now I replaced it with just 1.
-                        * We need to figure out an intelligent way of solving
-                        * this, maybe connect-disconnect.
-                        */
-                       if (1) {
-                               ret_size += sizeof(pid_t);
-                               ret = (pid_t *) realloc(ret, ret_size);
-                               ++i;
-                       }
+               sscanf(dirent->d_name, "%u", &read_pid);
+
+               (*pid_list)[*pid_list_size - 1] = read_pid;
+               /* FIXME: Here we previously called pid_is_online, which
+                * always returned 1, now I replaced it with just 1.
+                * We need to figure out an intelligent way of solving
+                * this, maybe connect-disconnect.
+                */
+               if (1) {
+                       (*pid_list_size)++;
+                       *pid_list = realloc(*pid_list,
+                                           *pid_list_size * sizeof(pid_t));
                }
        }
 
-       ret[i] = 0; /* Array end */
+       (*pid_list)[*pid_list_size - 1] = 0; /* Array end */
+}
 
-       if (ret[0] == 0) {
-               /* No PID at all */
-               free(ret);
+static pid_t *get_pids_non_root(void)
+{
+       char *dir_name;
+       DIR *dir;
+       unsigned int pid_list_size = 1;
+       pid_t *pid_list = NULL;
+
+       dir_name = ustcomm_user_sock_dir();
+       if (!dir_name) {
                return NULL;
        }
 
+       dir = opendir(dir_name);
+       if (!dir) {
+               goto free_dir_name;
+       }
+
+       pid_list = malloc(pid_list_size * sizeof(pid_t));
+       if (!pid_list) {
+               goto close_dir;
+       }
+
+       get_pids_in_dir(dir, &pid_list, &pid_list_size);
+
+       if (pid_list[0] == 0) {
+               /* No PID at all */
+               free(pid_list);
+               pid_list = NULL;
+               goto close_dir;
+       }
+
+close_dir:
        closedir(dir);
-       return ret;
+
+free_dir_name:
+       free(dir_name);
+
+       return pid_list;
+}
+
+static pid_t *get_pids_root(void)
+{
+       char *dir_name;
+       DIR *tmp_dir, *dir;
+       unsigned int pid_list_size = 1;
+       pid_t *pid_list = NULL;
+       struct dirent *dirent;
+
+       tmp_dir = opendir(USER_TMP_DIR);
+       if (!tmp_dir) {
+               return NULL;
+       }
+
+       pid_list = malloc(pid_list_size * sizeof(pid_t));
+       if (!pid_list) {
+               goto close_tmp_dir;
+       }
+
+       while ((dirent = readdir(tmp_dir))) {
+               /* Compare the dir to check for the USER_SOCK_DIR_BASE prefix */
+               if (!strncmp(dirent->d_name, USER_SOCK_DIR_BASE,
+                            strlen(USER_SOCK_DIR_BASE))) {
+
+                       if (asprintf(&dir_name, USER_TMP_DIR "/%s", dirent->d_name) < 0) {
+                               goto close_tmp_dir;
+                       }
+
+                       dir = opendir(dir_name);
+
+                       free(dir_name);
+
+                       if (!dir) {
+                               continue;
+                       }
+
+                       get_pids_in_dir(dir, &pid_list, &pid_list_size);
+
+                       closedir(dir);
+               }
+       }
+
+close_tmp_dir:
+       closedir(tmp_dir);
+
+       return pid_list;
+}
+
+pid_t *ustctl_get_online_pids(void)
+{
+       if (geteuid()) {
+               return get_pids_non_root();
+       } else {
+               return get_pids_root();
+       }
 }
 
 /**
@@ -379,6 +459,12 @@ int ustctl_alloc_trace(int sock, const char *trace)
        return do_trace_cmd(sock, trace, ALLOC_TRACE);
 }
 
+
+int ustctl_force_switch(int sock, const char *trace)
+{
+       return do_trace_cmd(sock, trace, FORCE_SUBBUF_SWITCH);
+}
+
 /**
  * Stops an UST trace according to a PID.
  *
@@ -647,13 +733,3 @@ int ustctl_get_sock_path(int sock, char **sock_path)
 
        return 0;
 }
-
-int ustctl_force_switch(int sock, const char *trace)
-{
-       struct ustcomm_header req_header, res_header;
-
-       req_header.command = FORCE_SUBBUF_SWITCH;
-       req_header.size = 0;
-
-       return do_cmd(sock, &req_header, NULL, &res_header, NULL);
-}
This page took 0.024187 seconds and 4 git commands to generate.