fix: if tracing group does not exist, do not report a client error
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
index b7fc781610f9b214422d84bc5a9a34f97bb2b059..c5887fe65ead21a41f637aafa3adc10d60e0eb45 100644 (file)
@@ -74,6 +74,7 @@ static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src
                                memcpy(dst, src, sizeof(struct lttng_domain));
                                break;
                        default:
+                               memset(dst, 0, sizeof(struct lttng_domain));
                                dst->type = LTTNG_DOMAIN_KERNEL;
                                break;
                }
@@ -83,8 +84,8 @@ static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src
 /*
  * Send lttcomm_session_msg to the session daemon.
  *
- * On success, return 0
- * On error, return error code
+ * On success, returns the number of bytes sent (>=0)
+ * On error, returns -1
  */
 static int send_session_msg(struct lttcomm_session_msg *lsm)
 {
@@ -105,8 +106,8 @@ end:
 /*
  * Receive data from the sessiond socket.
  *
- * On success, return 0
- * On error, return recv() error code
+ * On success, returns the number of bytes received (>=0)
+ * On error, returns -1 (recvmsg() error) or -ENOTCONN
  */
 static int recv_data_sessiond(void *buf, size_t len)
 {
@@ -124,7 +125,7 @@ end:
 }
 
 /*
- *  Check if the specified group name exist.
+ *  Check if we are in the specified group.
  *
  *  If yes return 1, else return -1.
  */
@@ -137,11 +138,8 @@ static int check_tracing_group(const char *grp_name)
 
        /* Get GID of group 'tracing' */
        grp_tracing = getgrnam(grp_name);
-       if (grp_tracing == NULL) {
-               /* NULL means not found also. getgrnam(3) */
-               if (errno != 0) {
-                       perror("getgrnam");
-               }
+       if (!grp_tracing) {
+               /* If grp_tracing is NULL, the group does not exist. */
                goto end;
        }
 
@@ -155,11 +153,11 @@ static int check_tracing_group(const char *grp_name)
        /* Alloc group list of the right size */
        grp_list = malloc(grp_list_size * sizeof(gid_t));
        if (!grp_list) {
-               ret = -1;
+               perror("malloc");
                goto end;
        }
        grp_id = getgroups(grp_list_size, grp_list);
-       if (grp_id < -1) {
+       if (grp_id < 0) {
                perror("getgroups");
                goto free_list;
        }
@@ -209,8 +207,10 @@ static int try_connect_sessiond(const char *sock_path)
 }
 
 /*
- * Set sessiond socket path by putting it in the global sessiond_sock_path
- * variable.
+ * Set sessiond socket path by putting it in the global
+ * sessiond_sock_path variable.
+ * Returns 0 on success,
+ * -ENOMEM on failure (the sessiond socket path is somehow too long)
  */
 static int set_session_daemon_path(void)
 {
@@ -225,35 +225,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;
 }
 
@@ -268,7 +266,7 @@ static int connect_sessiond(void)
 
        ret = set_session_daemon_path();
        if (ret < 0) {
-               return ret;
+               return -1; /* set_session_daemon_path() returns -ENOMEM */
        }
 
        /* Connect to the sesssion daemon */
@@ -284,7 +282,8 @@ static int connect_sessiond(void)
 }
 
 /*
- *  Clean disconnect the session daemon.
+ *  Clean disconnect from the session daemon.
+ *  On success, return 0. On error, return -1.
  */
 static int disconnect_sessiond(void)
 {
@@ -302,7 +301,7 @@ static int disconnect_sessiond(void)
 /*
  * Ask the session daemon a specific command and put the data into buf.
  *
- * Return size of data (only payload, not header).
+ * Return size of data (only payload, not header) or a negative error code.
  */
 static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
 {
@@ -373,6 +372,7 @@ end:
 
 /*
  * Create lttng handle and return pointer.
+ * The returned pointer will be NULL in case of malloc() error.
  */
 struct lttng_handle *lttng_create_handle(const char *session_name,
                struct lttng_domain *domain)
@@ -408,6 +408,7 @@ void lttng_destroy_handle(struct lttng_handle *handle)
 
 /*
  * Register an outside consumer.
+ * Returns size of returned session payload data or a negative error code.
  */
 int lttng_register_consumer(struct lttng_handle *handle,
                const char *socket_path)
@@ -425,7 +426,8 @@ int lttng_register_consumer(struct lttng_handle *handle,
 }
 
 /*
- *  Start tracing for all trace of the session.
+ *  Start tracing for all traces of the session.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_start_tracing(const char *session_name)
 {
@@ -443,7 +445,8 @@ int lttng_start_tracing(const char *session_name)
 }
 
 /*
- *  Stop tracing for all trace of the session.
+ *  Stop tracing for all traces of the session.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_stop_tracing(const char *session_name)
 {
@@ -461,7 +464,12 @@ int lttng_stop_tracing(const char *session_name)
 }
 
 /*
- * Add context to event or/and channel.
+ * Add context to event and/or channel.
+ * If event_name is NULL, the context is applied to all events of the channel.
+ * If channel_name is NULL, a lookup of the event's channel is done.
+ * If both are NULL, the context is applied to all events of all channels.
+ *
+ * Returns the size of the returned payload data or a negative error code.
  */
 int lttng_add_context(struct lttng_handle *handle,
                struct lttng_event_context *ctx, const char *event_name,
@@ -474,6 +482,8 @@ int lttng_add_context(struct lttng_handle *handle,
                return -1;
        }
 
+       memset(&lsm, 0, sizeof(lsm));
+
        lsm.cmd_type = LTTNG_ADD_CONTEXT;
 
        /* Copy channel name */
@@ -494,7 +504,10 @@ int lttng_add_context(struct lttng_handle *handle,
 }
 
 /*
- * Enable event
+ *  Enable event(s) for a channel.
+ *  If no event name is specified, all events are enabled.
+ *  If no channel name is specified, the default 'channel0' is used.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_enable_event(struct lttng_handle *handle,
                struct lttng_event *ev, const char *channel_name)
@@ -505,6 +518,8 @@ int lttng_enable_event(struct lttng_handle *handle,
                return -1;
        }
 
+       memset(&lsm, 0, sizeof(lsm));
+
        /* If no channel name, we put the default name */
        if (channel_name == NULL) {
                copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
@@ -530,7 +545,10 @@ int lttng_enable_event(struct lttng_handle *handle,
 }
 
 /*
- * Disable event of a channel and domain.
+ *  Disable event(s) of a channel and domain.
+ *  If no event name is specified, all events are disabled.
+ *  If no channel name is specified, the default 'channel0' is used.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_disable_event(struct lttng_handle *handle, const char *name,
                const char *channel_name)
@@ -541,6 +559,8 @@ int lttng_disable_event(struct lttng_handle *handle, const char *name,
                return -1;
        }
 
+       memset(&lsm, 0, sizeof(lsm));
+
        if (channel_name) {
                copy_string(lsm.u.disable.channel_name, channel_name,
                                sizeof(lsm.u.disable.channel_name));
@@ -565,7 +585,8 @@ int lttng_disable_event(struct lttng_handle *handle, const char *name,
 }
 
 /*
- * Enable channel per domain
+ *  Enable channel per domain
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_enable_channel(struct lttng_handle *handle,
                struct lttng_channel *chan)
@@ -579,6 +600,8 @@ int lttng_enable_channel(struct lttng_handle *handle,
                return -1;
        }
 
+       memset(&lsm, 0, sizeof(lsm));
+
        memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
 
        lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
@@ -592,7 +615,8 @@ int lttng_enable_channel(struct lttng_handle *handle,
 }
 
 /*
- * All tracing will be stopped for registered events of the channel.
+ *  All tracing will be stopped for registered events of the channel.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
 {
@@ -603,6 +627,8 @@ int lttng_disable_channel(struct lttng_handle *handle, const char *name)
                return -1;
        }
 
+       memset(&lsm, 0, sizeof(lsm));
+
        lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
 
        copy_string(lsm.u.disable.channel_name, name,
@@ -617,10 +643,10 @@ int lttng_disable_channel(struct lttng_handle *handle, const char *name)
 }
 
 /*
- * List all available tracepoints of domain.
- *
- * Return the size (bytes) of the list and set the events array.
- * On error, return negative value.
+ *  Lists all available tracepoints of domain.
+ *  Sets the contents of the events array.
+ *  Returns the number of lttng_event entries in events;
+ *  on error, returns a negative value.
  */
 int lttng_list_tracepoints(struct lttng_handle *handle,
                struct lttng_event **events)
@@ -644,10 +670,12 @@ int lttng_list_tracepoints(struct lttng_handle *handle,
 }
 
 /*
- *  Return a human readable string of code
+ *  Returns a human readable string describing
+ *  the error code (a negative value).
  */
 const char *lttng_strerror(int code)
 {
+       /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
        if (code > -LTTCOMM_OK) {
                return "Ended with errors";
        }
@@ -656,7 +684,8 @@ const char *lttng_strerror(int code)
 }
 
 /*
- *  Create a brand new session using name.
+ *  Create a brand new session using name and path.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_create_session(const char *name, const char *path)
 {
@@ -671,6 +700,7 @@ int lttng_create_session(const char *name, const char *path)
 
 /*
  *  Destroy session using name.
+ *  Returns size of returned session payload data or a negative error code.
  */
 int lttng_destroy_session(const char *session_name)
 {
@@ -689,9 +719,9 @@ int lttng_destroy_session(const char *session_name)
 
 /*
  *  Ask the session daemon for all available sessions.
- *
- *  Return number of session.
- *  On error, return negative value.
+ *  Sets the contents of the sessions array.
+ *  Returns the number of lttng_session entries in sessions;
+ *  on error, returns a negative value.
  */
 int lttng_list_sessions(struct lttng_session **sessions)
 {
@@ -708,7 +738,10 @@ int lttng_list_sessions(struct lttng_session **sessions)
 }
 
 /*
- * List domain of a session.
+ *  Ask the session daemon for all available domains of a session.
+ *  Sets the contents of the domains array.
+ *  Returns the number of lttng_domain entries in domains;
+ *  on error, returns a negative value.
  */
 int lttng_list_domains(const char *session_name,
                struct lttng_domain **domains)
@@ -733,7 +766,10 @@ int lttng_list_domains(const char *session_name,
 }
 
 /*
- * List channels of a session
+ *  Ask the session daemon for all available channels of a session.
+ *  Sets the contents of the channels array.
+ *  Returns the number of lttng_channel entries in channels;
+ *  on error, returns a negative value.
  */
 int lttng_list_channels(struct lttng_handle *handle,
                struct lttng_channel **channels)
@@ -760,7 +796,10 @@ int lttng_list_channels(struct lttng_handle *handle,
 }
 
 /*
- * List events of a session channel.
+ *  Ask the session daemon for all available events of a session channel.
+ *  Sets the contents of the events array.
+ *  Returns the number of lttng_event entries in events;
+ *  on error, returns a negative value.
  */
 int lttng_list_events(struct lttng_handle *handle,
                const char *channel_name, struct lttng_event **events)
@@ -790,8 +829,9 @@ int lttng_list_events(struct lttng_handle *handle,
 }
 
 /*
- * Set tracing group variable with name. This function allocate memory pointed
- * by tracing_group.
+ * Sets the tracing_group variable with name.
+ * This function allocates memory pointed to by tracing_group.
+ * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
  */
 int lttng_set_tracing_group(const char *name)
 {
@@ -807,7 +847,7 @@ int lttng_set_tracing_group(const char *name)
 }
 
 /*
- *  lttng_calibrate
+ * Returns size of returned session payload data or a negative error code.
  */
 int lttng_calibrate(struct lttng_handle *handle,
                struct lttng_calibrate *calibrate)
@@ -829,10 +869,13 @@ int lttng_calibrate(struct lttng_handle *handle,
 
 /*
  * Set default channel attributes.
+ * If either or both of the arguments are null, attr content is zeroe'd.
  */
 void lttng_channel_set_default_attr(struct lttng_domain *domain,
                struct lttng_channel_attr *attr)
 {
+       memset(attr, 0, sizeof(struct lttng_channel_attr));
+
        /* Safety check */
        if (attr == NULL || domain == NULL) {
                return;
@@ -863,8 +906,7 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain,
                attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
                break;
        default:
-               /* Default behavior */
-               memset(attr, 0, sizeof(struct lttng_channel_attr));
+               /* Default behavior: leave set to 0. */
                break;
        }
 }
@@ -873,7 +915,7 @@ void lttng_channel_set_default_attr(struct lttng_domain *domain,
  * Check if session daemon is alive.
  *
  * Return 1 if alive or 0 if not.
- * On error return -1
+ * On error returns a negative value.
  */
 int lttng_session_daemon_alive(void)
 {
This page took 0.02876 seconds and 4 git commands to generate.