Make root see all available pids v2
[ust.git] / libustctl / libustctl.c
index 9c4ced26482299b35b227b747c575dc899b91acb..356d5ef7235536a67b405f07431aed96018fbe69 100644 (file)
@@ -1,4 +1,5 @@
 /* Copyright (C) 2009  Pierre-Marc Fournier, Philippe Proulx-Barrette
+ * Copyright (C) 2011  Ericsson AB
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 #include "ust/ustctl.h"
 #include "usterr.h"
 
-static int do_cmd(const pid_t pid,
+static int do_cmd(int sock,
                  const struct ustcomm_header *req_header,
                  const char *req_data,
                  struct ustcomm_header *res_header,
                  char **res_data)
 {
-       int app_fd, result, saved_errno = 0;
+       int result, saved_errno = 0;
        char *recv_buf;
 
-       if (ustcomm_connect_app(pid, &app_fd)) {
-               ERR("could not connect to PID %u", (unsigned int) pid);
-               errno = ENOTCONN;
-               return -1;
-       }
-
        recv_buf = zmalloc(USTCOMM_BUFFER_SIZE);
        if (!recv_buf) {
                saved_errno = ENOMEM;
-               goto close_app_fd;
+               goto out;
        }
 
-       result = ustcomm_req(app_fd, req_header, req_data, res_header, recv_buf);
+       result = ustcomm_req(sock, req_header, req_data, res_header, recv_buf);
        if (result > 0) {
                saved_errno = -res_header->result;
                if (res_header->size == 0 || saved_errno > 0) {
@@ -71,11 +66,8 @@ static int do_cmd(const pid_t pid,
                free(recv_buf);
        }
 
-close_app_fd:
-       close(app_fd);
-
+out:
        errno = saved_errno;
-
        if (errno) {
                return -1;
        }
@@ -83,53 +75,146 @@ close_app_fd:
        return 0;
 }
 
-pid_t *ustctl_get_online_pids(void)
+int ustctl_connect_pid(pid_t pid)
 {
-       struct dirent *dirent;
-       DIR *dir;
-       unsigned int ret_size = 1 * sizeof(pid_t), i = 0;
+       int sock;
 
-       dir = opendir(SOCK_DIR);
-       if (!dir) {
-               return NULL;
+       if (ustcomm_connect_app(pid, &sock)) {
+               ERR("could not connect to PID %u", (unsigned int) pid);
+               errno = ENOTCONN;
+               return -1;
        }
 
-       pid_t *ret = (pid_t *) malloc(ret_size);
+       return sock;
+}
+
+static void get_pids_in_dir(DIR *dir, pid_t **pid_list,
+                           unsigned int *pid_list_size)
+{
+       struct dirent *dirent;
+       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();
+       }
 }
 
 /**
@@ -140,8 +225,8 @@ pid_t *ustctl_get_online_pids(void)
  * @param pid  Traced process ID
  * @return     0 if successful, or errors {USTCTL_ERR_GEN, USTCTL_ERR_ARG}
  */
-int ustctl_set_marker_state(const char *trace, const char *channel,
-                           const char *marker, int state, pid_t pid)
+int ustctl_set_marker_state(int sock, const char *trace, const char *channel,
+                           const char *marker, int state)
 {
        struct ustcomm_header req_header, res_header;
        struct ustcomm_marker_info marker_inf;
@@ -159,7 +244,7 @@ int ustctl_set_marker_state(const char *trace, const char *channel,
 
        req_header.command = state ? ENABLE_MARKER : DISABLE_MARKER;
 
-       return do_cmd(pid, &req_header, (char *)&marker_inf,
+       return do_cmd(sock, &req_header, (char *)&marker_inf,
                      &res_header, NULL);
 }
 
@@ -170,8 +255,8 @@ int ustctl_set_marker_state(const char *trace, const char *channel,
  * @param pid          Traced process ID
  * @return             0 if successful, or error
  */
-int ustctl_set_subbuf_size(const char *trace, const char *channel,
-                          unsigned int subbuf_size, pid_t pid)
+int ustctl_set_subbuf_size(int sock, const char *trace, const char *channel,
+                          unsigned int subbuf_size)
 {
        struct ustcomm_header req_header, res_header;
        struct ustcomm_channel_info ch_inf;
@@ -189,7 +274,7 @@ int ustctl_set_subbuf_size(const char *trace, const char *channel,
        req_header.command = SET_SUBBUF_SIZE;
        ch_inf.subbuf_size = subbuf_size;
 
-       return do_cmd(pid, &req_header, (char *)&ch_inf,
+       return do_cmd(sock, &req_header, (char *)&ch_inf,
                      &res_header, NULL);
 }
 
@@ -200,8 +285,8 @@ int ustctl_set_subbuf_size(const char *trace, const char *channel,
  * @param pid          Traced process ID
  * @return             0 if successful, or error
  */
-int ustctl_set_subbuf_num(const char *trace, const char *channel,
-                         unsigned int num, pid_t pid)
+int ustctl_set_subbuf_num(int sock, const char *trace, const char *channel,
+                         unsigned int num)
 {
        struct ustcomm_header req_header, res_header;
        struct ustcomm_channel_info ch_inf;
@@ -219,13 +304,14 @@ int ustctl_set_subbuf_num(const char *trace, const char *channel,
        req_header.command = SET_SUBBUF_NUM;
        ch_inf.subbuf_num = num;
 
-       return do_cmd(pid, &req_header, (char *)&ch_inf,
+       return do_cmd(sock, &req_header, (char *)&ch_inf,
                      &res_header, NULL);
 
 }
 
-static int ustctl_get_subbuf_num_size(const char *trace, const char *channel,
-                                     pid_t pid, int *num, int *size)
+
+static int ustctl_get_subbuf_num_size(int sock, const char *trace, const char *channel,
+                                     int *num, int *size)
 {
        struct ustcomm_header req_header, res_header;
        struct ustcomm_channel_info ch_inf, *ch_inf_res;
@@ -243,7 +329,7 @@ static int ustctl_get_subbuf_num_size(const char *trace, const char *channel,
 
        req_header.command = GET_SUBBUF_NUM_SIZE;
 
-       result = do_cmd(pid, &req_header, (char *)&ch_inf,
+       result = do_cmd(sock, &req_header, (char *)&ch_inf,
                        &res_header, (char **)&ch_inf_res);
        if (result < 0) {
                return -1;
@@ -264,11 +350,11 @@ static int ustctl_get_subbuf_num_size(const char *trace, const char *channel,
  * @param pid          Traced process ID
  * @return             subbuf cnf if successful, or error
  */
-int ustctl_get_subbuf_num(const char *trace, const char *channel, pid_t pid)
+int ustctl_get_subbuf_num(int sock, const char *trace, const char *channel)
 {
        int num, size, result;
 
-       result = ustctl_get_subbuf_num_size(trace, channel, pid,
+       result = ustctl_get_subbuf_num_size(sock, trace, channel,
                                            &num, &size);
        if (result < 0) {
                errno = -result;
@@ -285,11 +371,11 @@ int ustctl_get_subbuf_num(const char *trace, const char *channel, pid_t pid)
  * @param pid          Traced process ID
  * @return             subbuf size if successful, or error
  */
-int ustctl_get_subbuf_size(const char *trace, const char *channel, pid_t pid)
+int ustctl_get_subbuf_size(int sock, const char *trace, const char *channel)
 {
        int num, size, result;
 
-       result = ustctl_get_subbuf_num_size(trace, channel, pid,
+       result = ustctl_get_subbuf_num_size(sock, trace, channel,
                                            &num, &size);
        if (result < 0) {
                errno = -result;
@@ -299,8 +385,7 @@ int ustctl_get_subbuf_size(const char *trace, const char *channel, pid_t pid)
        return size;
 }
 
-
-static int do_trace_cmd(const char *trace, pid_t pid, int command)
+static int do_trace_cmd(int sock, const char *trace, int command)
 {
        struct ustcomm_header req_header, res_header;
        struct ustcomm_single_field trace_inf;
@@ -316,7 +401,7 @@ static int do_trace_cmd(const char *trace, pid_t pid, int command)
 
        req_header.command = command;
 
-       return do_cmd(pid, &req_header, (char *)&trace_inf, &res_header, NULL);
+       return do_cmd(sock, &req_header, (char *)&trace_inf, &res_header, NULL);
 }
 
 /**
@@ -325,9 +410,9 @@ static int do_trace_cmd(const char *trace, pid_t pid, int command)
  * @param pid  Traced process ID
  * @return     0 if successful, or error USTCTL_ERR_GEN
  */
-int ustctl_destroy_trace(const char *trace, pid_t pid)
+int ustctl_destroy_trace(int sock, const char *trace)
 {
-       return do_trace_cmd(trace, pid, DESTROY_TRACE);
+       return do_trace_cmd(sock, trace, DESTROY_TRACE);
 }
 
 /**
@@ -336,9 +421,9 @@ int ustctl_destroy_trace(const char *trace, pid_t pid)
  * @param pid  Traced process ID
  * @return     0 if successful, or error USTCTL_ERR_GEN
  */
-int ustctl_setup_and_start(const char *trace, pid_t pid)
+int ustctl_setup_and_start(int sock, const char *trace)
 {
-       return do_trace_cmd(trace, pid, START);
+       return do_trace_cmd(sock, trace, START);
 }
 
 /**
@@ -347,9 +432,9 @@ int ustctl_setup_and_start(const char *trace, pid_t pid)
  * @param pid  Traced process ID
  * @return     0 if successful, or error USTCTL_ERR_GEN
  */
-int ustctl_create_trace(const char *trace, pid_t pid)
+int ustctl_create_trace(int sock, const char *trace)
 {
-       return do_trace_cmd(trace, pid, CREATE_TRACE);
+       return do_trace_cmd(sock, trace, CREATE_TRACE);
 }
 
 /**
@@ -358,9 +443,9 @@ int ustctl_create_trace(const char *trace, pid_t pid)
  * @param pid  Traced process ID
  * @return     0 if successful, or error USTCTL_ERR_GEN
  */
-int ustctl_start_trace(const char *trace, pid_t pid)
+int ustctl_start_trace(int sock, const char *trace)
 {
-       return do_trace_cmd(trace, pid, START_TRACE);
+       return do_trace_cmd(sock, trace, START_TRACE);
 }
 
 /**
@@ -369,9 +454,15 @@ int ustctl_start_trace(const char *trace, pid_t pid)
  * @param pid  Traced process ID
  * @return     0 if successful, or error USTCTL_ERR_GEN
  */
-int ustctl_alloc_trace(const char *trace, pid_t pid)
+int ustctl_alloc_trace(int sock, const char *trace)
 {
-       return do_trace_cmd(trace, pid, ALLOC_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);
 }
 
 /**
@@ -380,9 +471,9 @@ int ustctl_alloc_trace(const char *trace, pid_t pid)
  * @param pid  Traced process ID
  * @return     0 if successful, or error USTCTL_ERR_GEN
  */
-int ustctl_stop_trace(const char *trace, pid_t pid)
+int ustctl_stop_trace(int sock, const char *trace)
 {
-       return do_trace_cmd(trace, pid, STOP_TRACE);
+       return do_trace_cmd(sock, trace, STOP_TRACE);
 }
 
 /**
@@ -437,11 +528,11 @@ int ustctl_free_cmsf(struct marker_status *cmsf)
  * @param pid  Targeted PID
  * @return     0 if successful, or -1 on error
  */
-int ustctl_get_cmsf(struct marker_status **cmsf, const pid_t pid)
+int ustctl_get_cmsf(int sock, struct marker_status **cmsf)
 {
        struct ustcomm_header req_header, res_header;
        char *big_str = NULL;
-       int result, app_fd;
+       int result;
        struct marker_status *tmp_cmsf = NULL;
        unsigned int i = 0, cmsf_ind = 0;
 
@@ -449,28 +540,21 @@ int ustctl_get_cmsf(struct marker_status **cmsf, const pid_t pid)
                return -1;
        }
 
-       if (ustcomm_connect_app(pid, &app_fd)) {
-               ERR("could not connect to PID %u", (unsigned int) pid);
-               return -1;
-       }
-
        req_header.command = LIST_MARKERS;
        req_header.size = 0;
 
-       result = ustcomm_send(app_fd, &req_header, NULL);
+       result = ustcomm_send(sock, &req_header, NULL);
        if (result <= 0) {
-               PERROR("error while requesting markers list for process %d", pid);
+               PERROR("error while requesting markers list");
                return -1;
        }
 
-       result = ustcomm_recv_alloc(app_fd, &res_header, &big_str);
+       result = ustcomm_recv_alloc(sock, &res_header, &big_str);
        if (result <= 0) {
                ERR("error while receiving markers list");
                return -1;
        }
 
-       close(app_fd);
-
        tmp_cmsf = (struct marker_status *) zmalloc(sizeof(struct marker_status) *
                                                    (ustctl_count_nl(big_str) + 1));
        if (tmp_cmsf == NULL) {
@@ -506,7 +590,6 @@ int ustctl_get_cmsf(struct marker_status **cmsf, const pid_t pid)
        return 0;
 }
 
-
 /**
  * Frees a TES array.
  *
@@ -537,12 +620,11 @@ int ustctl_free_tes(struct trace_event_status *tes)
  * @param pid  Targeted PID
  * @return     0 if successful, or -1 on error
  */
-int ustctl_get_tes(struct trace_event_status **tes,
-                  const pid_t pid)
+int ustctl_get_tes(int sock, struct trace_event_status **tes)
 {
        struct ustcomm_header req_header, res_header;
        char *big_str = NULL;
-       int result, app_fd;
+       int result;
        struct trace_event_status *tmp_tes = NULL;
        unsigned int i = 0, tes_ind = 0;
 
@@ -550,28 +632,21 @@ int ustctl_get_tes(struct trace_event_status **tes,
                return -1;
        }
 
-       if (ustcomm_connect_app(pid, &app_fd)) {
-               ERR("could not connect to PID %u", (unsigned int) pid);
-               return -1;
-       }
-
        req_header.command = LIST_TRACE_EVENTS;
        req_header.size = 0;
 
-       result = ustcomm_send(app_fd, &req_header, NULL);
+       result = ustcomm_send(sock, &req_header, NULL);
        if (result != 1) {
                ERR("error while requesting trace_event list");
                return -1;
        }
 
-       result = ustcomm_recv_alloc(app_fd, &res_header, &big_str);
+       result = ustcomm_recv_alloc(sock, &res_header, &big_str);
        if (result != 1) {
                ERR("error while receiving markers list");
                return -1;
        }
 
-       close(app_fd);
-
        tmp_tes = (struct trace_event_status *)
                zmalloc(sizeof(struct trace_event_status) *
                        (ustctl_count_nl(big_str) + 1));
@@ -599,13 +674,13 @@ int ustctl_get_tes(struct trace_event_status **tes,
 }
 
 /**
- * Set socket path
+ * Set sock path
  *
- * @param sock_path    Socket path
+ * @param sock_path    Sock path
  * @param pid          Traced process ID
  * @return             0 if successful, or error
  */
-int ustctl_set_sock_path(const char *sock_path, pid_t pid)
+int ustctl_set_sock_path(int sock, const char *sock_path)
 {
        int result;
        struct ustcomm_header req_header, res_header;
@@ -621,18 +696,18 @@ int ustctl_set_sock_path(const char *sock_path, pid_t pid)
 
        req_header.command = SET_SOCK_PATH;
 
-       return do_cmd(pid, &req_header, (char *)&sock_path_msg,
+       return do_cmd(sock, &req_header, (char *)&sock_path_msg,
                      &res_header, NULL);
 }
 
 /**
- * Get socket path
+ * Get sock path
  *
- * @param sock_path    Pointer to where the socket path will be returned
+ * @param sock_path    Pointer to where the sock path will be returned
  * @param pid          Traced process ID
  * @return             0 if successful, or error
  */
-int ustctl_get_sock_path(char **sock_path, pid_t pid)
+int ustctl_get_sock_path(int sock, char **sock_path)
 {
        int result;
        struct ustcomm_header req_header, res_header;
@@ -641,7 +716,7 @@ int ustctl_get_sock_path(char **sock_path, pid_t pid)
        req_header.command = GET_SOCK_PATH;
        req_header.size = 0;
 
-       result = do_cmd(pid, &req_header, NULL, &res_header,
+       result = do_cmd(sock, &req_header, NULL, &res_header,
                        (char **)&sock_path_msg);
        if (result < 0) {
                return -1;
@@ -658,14 +733,3 @@ int ustctl_get_sock_path(char **sock_path, pid_t pid)
 
        return 0;
 }
-
-int ustctl_force_switch(pid_t pid)
-{
-       struct ustcomm_header req_header, res_header;
-
-       req_header.command = FORCE_SUBBUF_SWITCH;
-       req_header.size = 0;
-
-       return do_cmd(pid, &req_header, NULL, &res_header, NULL);
-}
-
This page took 0.030543 seconds and 4 git commands to generate.