Rewrites lttng-ctl's set_session_daemon_path
authorThibault, Daniel <Daniel.Thibault@drdc-rddc.gc.ca>
Mon, 30 Jan 2012 21:26:34 +0000 (16:26 -0500)
committerDavid Goulet <dgoulet@efficios.com>
Mon, 30 Jan 2012 21:32:32 +0000 (16:32 -0500)
This fifth patch rewrites lttng-ctl's set_session_daemon_path() to avoid
duplicating snippets of code. It also fixes the snprintf return value
test so the code works with both GNU C < 2.1 and >= 2.1. With GNU C <
2.1, snprintf returns -1 if the target buffer is too small; with GNU C
>= 2.1, snprintf returns the required size (excluding the closing null)
under the same conditions.

Signed-off-by: Daniel U. Thibault <daniel.thibault@drdc-rddc.gc.ca>
Signed-off-by: David Goulet <dgoulet@efficios.com>
src/lib/lttng-ctl/lttng-ctl.c

index c62dcfa1d4b335748f5c192fe8b712120a71decb..dfaa47f7d7d3efb49aa109b9d05dc4dfc53af857 100644 (file)
@@ -227,35 +227,33 @@ static int set_session_daemon_path(void)
                in_tgroup = check_tracing_group(tracing_group);
        }
 
-       if (uid == 0) {
-               /* Root */
-               copy_string(sessiond_sock_path,
-                               DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
-                               sizeof(sessiond_sock_path));
-       } else if (in_tgroup) {
-               /* Tracing group */
-               copy_string(sessiond_sock_path,
-                               DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
+       if ((uid == 0) || in_tgroup) {
+               copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
                                sizeof(sessiond_sock_path));
+       }
 
-               ret = try_connect_sessiond(sessiond_sock_path);
-               if (ret < 0) {
-                       /* Global session daemon not available */
-                       if (snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
-                                               DEFAULT_HOME_CLIENT_UNIX_SOCK,
-                                               getenv("HOME")) < 0) {
-                               return -ENOMEM;
+       if (uid != 0) {
+               if (in_tgroup) {
+                       /* Tracing group */
+                       ret = try_connect_sessiond(sessiond_sock_path);
+                       if (ret >= 0) {
+                               goto end;
                        }
+                       /* Global session daemon not available... */
                }
-       } else {
-               /* Not in tracing group and not root, default */
-               if (snprintf(sessiond_sock_path, PATH_MAX,
-                                       DEFAULT_HOME_CLIENT_UNIX_SOCK,
-                                       getenv("HOME")) < 0) {
+               /* ...or not in tracing group (and not root), default */
+
+               /*
+                * With GNU C <  2.1, snprintf returns -1 if the target buffer is too small;
+                * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
+                */
+               ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
+                               DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME"));
+               if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
                        return -ENOMEM;
                }
        }
-
+end:
        return 0;
 }
 
This page took 0.028178 seconds and 4 git commands to generate.