Make root see all available pids v2
authorNils Carlson <nils.carlson@ericsson.com>
Wed, 30 Mar 2011 14:55:38 +0000 (16:55 +0200)
committerNils Carlson <nils.carlson@ericsson.com>
Fri, 1 Apr 2011 07:33:46 +0000 (09:33 +0200)
Changes since v1:
* Fix a whitespace
* Make functions that should be static static

Allow root (geteuid() == 0) to see all pids. This way the super-user
can connect to any program. A step on the way of carefully outlining
what UST does and doesn't.

Signed-off-by: Nils Carlson <nils.carlson@ericsson.com>
libustcomm/ustcomm.h
libustctl/libustctl.c

index d16aec79fc9aa4b79cba8f2f72383665e58bd5c3..995295810a5fc80ee4f8fcd1cae87618ed821a03 100644 (file)
@@ -25,7 +25,9 @@
 #include <ust/kcompat/kcompat.h>
 
 #define SOCK_DIR "/tmp/ust-app-socks"
-#define USER_SOCK_DIR "/tmp/ust-socks-"
+#define USER_TMP_DIR "/tmp"
+#define USER_SOCK_DIR_BASE "ust-socks-"
+#define USER_SOCK_DIR USER_TMP_DIR "/" USER_SOCK_DIR_BASE
 
 struct ustcomm_sock {
        struct cds_list_head list;
index 5625f4346f51bdc92edcfdb37e99930ec781aa0b..356d5ef7235536a67b405f07431aed96018fbe69 100644 (file)
@@ -121,7 +121,7 @@ static void get_pids_in_dir(DIR *dir, pid_t **pid_list,
        (*pid_list)[*pid_list_size - 1] = 0; /* Array end */
 }
 
-pid_t *ustctl_get_online_pids(void)
+static pid_t *get_pids_non_root(void)
 {
        char *dir_name;
        DIR *dir;
@@ -139,6 +139,9 @@ pid_t *ustctl_get_online_pids(void)
        }
 
        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);
 
@@ -158,6 +161,62 @@ 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();
+       }
+}
+
 /**
  * Sets marker state (USTCTL_MS_ON or USTCTL_MS_OFF).
  *
This page took 0.027302 seconds and 4 git commands to generate.