Make app socket directories per-user v2
authorNils Carlson <nils.carlson@ericsson.com>
Tue, 29 Mar 2011 08:27:46 +0000 (10:27 +0200)
committerNils Carlson <nils.carlson@ericsson.com>
Wed, 30 Mar 2011 15:00:15 +0000 (17:00 +0200)
Changes since v1:
* Document memory allocation

Make a separate app socket directories for each user, providing
some basic security and also the possibility of consistent cleanup.

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

index 33c7280743973aa281993280596a8708d428561a..ae92b7e17577f279d30456622842037d371455ca 100644 (file)
@@ -1221,37 +1221,41 @@ static void auto_probe_connect(struct marker *m)
 
 static struct ustcomm_sock * init_app_socket(int epoll_fd)
 {
-       char *name;
+       char *dir_name, *sock_name;
        int result;
-       struct ustcomm_sock *sock;
+       struct ustcomm_sock *sock = NULL;
 
-       result = asprintf(&name, "%s/%d", SOCK_DIR, (int)getpid());
+       dir_name = ustcomm_user_sock_dir();
+       if (!dir_name)
+               return NULL;
+
+       result = asprintf(&sock_name, "%s/%d", dir_name, (int)getpid());
        if (result < 0) {
                ERR("string overflow allocating socket name, "
                    "UST thread bailing");
-               return NULL;
+               goto free_dir_name;
        }
 
-       result = ensure_dir_exists(SOCK_DIR);
+       result = ensure_dir_exists(dir_name);
        if (result == -1) {
                ERR("Unable to create socket directory %s, UST thread bailing",
-                   SOCK_DIR);
-               goto free_name;
+                   dir_name);
+               goto free_sock_name;
        }
 
-       sock = ustcomm_init_named_socket(name, epoll_fd);
+       sock = ustcomm_init_named_socket(sock_name, epoll_fd);
        if (!sock) {
                ERR("Error initializing named socket (%s). Check that directory"
-                   "exists and that it is writable. UST thread bailing", name);
-               goto free_name;
+                   "exists and that it is writable. UST thread bailing", sock_name);
+               goto free_sock_name;
        }
 
-       free(name);
-       return sock;
+free_sock_name:
+       free(sock_name);
+free_dir_name:
+       free(dir_name);
 
-free_name:
-       free(name);
-       return NULL;
+       return sock;
 }
 
 static void __attribute__((constructor)) init()
index 43f4289a58662cca985fcbc68d26b2f7c49bd7ee..dce1e521c5b5c83714d439a00edf07ccb73253dc 100644 (file)
@@ -533,6 +533,21 @@ close_sock:
        return -1;
 }
 
+/* Returns the current users socket directory, must be freed */
+char *ustcomm_user_sock_dir(void)
+{
+       int result;
+       char *sock_dir = NULL;
+
+       result = asprintf(&sock_dir, "%s%s", USER_SOCK_DIR,
+                         cuserid(NULL));
+       if (result < 0) {
+               ERR("string overflow allocating directory name");
+               return NULL;
+       }
+
+       return sock_dir;
+}
 
 /* Open a connection to a traceable app.
  *
@@ -545,21 +560,30 @@ int ustcomm_connect_app(pid_t pid, int *app_fd)
 {
        int result;
        int retval = 0;
-       char *name;
+       char *dir_name, *sock_name;
+
+       dir_name = ustcomm_user_sock_dir();
+       if (!dir_name)
+               return -ENOMEM;
 
-       result = asprintf(&name, "%s/%d", SOCK_DIR, pid);
+       result = asprintf(&sock_name, "%s/%d", dir_name, pid);
        if (result < 0) {
                ERR("failed to allocate socket name");
-               return -1;
+               retval = -1;
+               goto free_dir_name;
        }
 
-       result = ustcomm_connect_path(name, app_fd);
+       result = ustcomm_connect_path(sock_name, app_fd);
        if (result < 0) {
                ERR("failed to connect to app");
                retval = -1;
+               goto free_sock_name;
        }
 
-       free(name);
+free_sock_name:
+       free(sock_name);
+free_dir_name:
+       free(dir_name);
 
        return retval;
 }
index 0ec04fc6e5edfd83458e021ecf835c0b5f429eaa..db3811961b2b8481aeed2704cb1333fd95dab0fd 100644 (file)
@@ -25,6 +25,7 @@
 #include <ust/kcompat/kcompat.h>
 
 #define SOCK_DIR "/tmp/ust-app-socks"
+#define USER_SOCK_DIR "/tmp/ust-socks-"
 
 struct ustcomm_sock {
        struct cds_list_head list;
@@ -156,6 +157,9 @@ extern int ustcomm_req(int sock,
                       char *res_data);
 
 extern int ustcomm_request_consumer(pid_t pid, const char *channel);
+
+/* Returns the current users socket directory, must be freed */
+extern char *ustcomm_user_sock_dir(void);
 extern int ustcomm_connect_app(pid_t pid, int *app_fd);
 extern int ustcomm_connect_path(const char *path, int *connection_fd);
 
This page took 0.027794 seconds and 4 git commands to generate.