Fix possible null pointer
[lttng-tools.git] / liblttngctl / liblttngctl.c
index a2934e63c9644e3e70ae3b7309880b4857e9b2bd..e05496a9449891f5f6174af5ed4de3c9ce1f5f0e 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "liblttsessiondcomm.h"
 #include "lttngerr.h"
+#include "lttng-share.h"
 
 /* Socket to session daemon for communication */
 static int sessiond_socket;
@@ -37,13 +38,6 @@ static char sessiond_sock_path[PATH_MAX];
 static struct lttcomm_session_msg lsm;
 static struct lttcomm_lttng_msg llm;
 
-/* Prototypes */
-static int check_tracing_group(const char *grp_name);
-static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf);
-static int recv_data_sessiond(void *buf, size_t len);
-static int send_data_sessiond(void);
-static int set_session_daemon_path(void);
-
 /* Variables */
 static char *tracing_group;
 static int connected;
@@ -89,9 +83,6 @@ static int recv_data_sessiond(void *buf, size_t len)
        }
 
        ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
-       if (ret < 0) {
-               goto end;
-       }
 
 end:
        return ret;
@@ -100,8 +91,7 @@ end:
 /*
  *  ask_sessiond
  *
- *  Ask the session daemon a specific command
- *  and put the data into buf.
+ *  Ask the session daemon a specific command and put the data into buf.
  *
  *  Return size of data (only payload, not header).
  */
@@ -136,7 +126,7 @@ static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf)
                goto end;
        }
 
-       size = llm.trace_name_offset + llm.data_size;
+       size = llm.data_size;
        if (size == 0) {
                goto end;
        }
@@ -146,6 +136,7 @@ static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf)
        /* Get payload data */
        ret = recv_data_sessiond(data, size);
        if (ret < 0) {
+               free(data);
                goto end;
        }
 
@@ -154,163 +145,255 @@ static int ask_sessiond(enum lttcomm_sessiond_command lct, void **buf)
 
 end:
        lttng_disconnect_sessiond();
-       memset(&lsm, 0, sizeof(lsm));
        return ret;
 }
 
 /*
- * BEGIN KERNEL CONTROL
- */
-
-/*
- *  lttng_kernel_enable_event
+ *  check_tracing_group
  *
- *  Enable an event in the kernel tracer.
+ *  Check if the specified group name exist.
+ *  If yes, 0, else -1
  */
-int lttng_kernel_enable_event(char *event_name)
+static int check_tracing_group(const char *grp_name)
 {
-       strncpy(lsm.u.event.event_name, event_name, NAME_MAX);
-       return ask_sessiond(KERNEL_ENABLE_EVENT, NULL);
+       struct group *grp_tracing;      /* no free(). See getgrnam(3) */
+       gid_t *grp_list;
+       int grp_list_size, grp_id, i;
+       int ret = -1;
+
+       /* 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");
+               }
+               goto end;
+       }
+
+       /* Get number of supplementary group IDs */
+       grp_list_size = getgroups(0, NULL);
+       if (grp_list_size < 0) {
+               perror("getgroups");
+               goto end;
+       }
+
+       /* Alloc group list of the right size */
+       grp_list = malloc(grp_list_size * sizeof(gid_t));
+       grp_id = getgroups(grp_list_size, grp_list);
+       if (grp_id < -1) {
+               perror("getgroups");
+               goto free_list;
+       }
+
+       for (i = 0; i < grp_list_size; i++) {
+               if (grp_list[i] == grp_tracing->gr_gid) {
+                       ret = 0;
+                       break;
+               }
+       }
+
+free_list:
+       free(grp_list);
+
+end:
+       return ret;
 }
 
 /*
- *  lttng_kernel_disable_event
+ *  set_session_daemon_path
  *
- *  Disable an event in the kernel tracer.
+ *  Set sessiond socket path by putting it in 
+ *  the global sessiond_sock_path variable.
  */
-int lttng_kernel_disable_event(char *event_name)
+static int set_session_daemon_path(void)
 {
-       strncpy(lsm.u.event.event_name, event_name, NAME_MAX);
-       return ask_sessiond(KERNEL_DISABLE_EVENT, NULL);
+       int ret;
+
+       /* Are we in the tracing group ? */
+       ret = check_tracing_group(tracing_group);
+       if (ret < 0 && getuid() != 0) {
+               if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK,
+                                       getenv("HOME")) < 0) {
+                       return -ENOMEM;
+               }
+       } else {
+               strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
+                               sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
+       }
+
+       return 0;
 }
 
 /*
- *  lttng_kernel_create_session
+ *  lttng_start_tracing
  *
- *  Create a session in the kernel tracer.
+ *  Start tracing for all trace of the session.
  */
-int lttng_kernel_create_session(void)
+int lttng_start_tracing(char *session_name)
 {
-       return ask_sessiond(KERNEL_CREATE_SESSION, NULL);
+       strncpy(lsm.session_name, session_name, NAME_MAX);
+       return ask_sessiond(LTTNG_START_TRACE, NULL);
 }
 
 /*
- *  lttng_kernel_create_channel
+ *  lttng_stop_tracing
  *
- *  Create a channel in the kernel tracer.
+ *  Stop tracing for all trace of the session.
  */
-int lttng_kernel_create_channel(int overwrite,
-               u64 subbuf_size, u64 num_subbuf,
-               unsigned int switch_timer_interval,
-               unsigned int read_timer_interval)
+int lttng_stop_tracing(char *session_name)
 {
-       /* Write setting to the session message */
-       lsm.u.create_channel.overwrite = overwrite;
-       lsm.u.create_channel.subbuf_size = subbuf_size;
-       lsm.u.create_channel.num_subbuf = num_subbuf;
-       lsm.u.create_channel.switch_timer_interval = switch_timer_interval;
-       lsm.u.create_channel.read_timer_interval = read_timer_interval;
-
-       return ask_sessiond(KERNEL_CREATE_CHANNEL, NULL);
+       strncpy(lsm.session_name, session_name, NAME_MAX);
+       return ask_sessiond(LTTNG_STOP_TRACE, NULL);
 }
 
 /*
- *  lttng_kernel_start_tracing
- *
- *  Start kernel tracing.
+ * BEGIN Kernel control API
  */
-int lttng_kernel_start_tracing(void)
-{
-       return ask_sessiond(KERNEL_START_TRACE, NULL);
-}
 
 /*
- *  lttng_kernel_stop_tracing
- *
- *  Stop kernel tracing.
+ *  lttng_kernel_add_context
  */
-int lttng_kernel_stop_tracing(void)
+int lttng_kernel_add_context(struct lttng_kernel_context *ctx,
+               char *event_name, char *channel_name)
 {
-       return ask_sessiond(KERNEL_STOP_TRACE, NULL);
-}
+       if (channel_name != NULL) {
+               strncpy(lsm.u.context.channel_name, channel_name, NAME_MAX);
+       }
 
-/*
- * END KERNEL CONTROL
- */
+       if (event_name != NULL) {
+               strncpy(lsm.u.context.event_name, event_name, NAME_MAX);
+       }
+
+       memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_kernel_context));
+       return ask_sessiond(LTTNG_KERNEL_ADD_CONTEXT, NULL);
+}
 
 /*
- *  lttng_get_readable_code
- *
- *  Return a human readable string of code
+ *  lttng_kernel_enable_event
  */
-const char *lttng_get_readable_code(int code)
+int lttng_kernel_enable_event(struct lttng_event *ev, char *channel_name)
 {
-       if (code > -LTTCOMM_OK) {
-               return "Ended with errors";
+       int ret;
+
+       if (channel_name == NULL) {
+               strncpy(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
+       } else {
+               strncpy(lsm.u.enable.channel_name, channel_name, NAME_MAX);
        }
 
-       return lttcomm_get_readable_code(code);
+       if (ev == NULL) {
+               ret = ask_sessiond(LTTNG_KERNEL_ENABLE_ALL_EVENT, NULL);
+       } else {
+               memcpy(&lsm.u.enable.event, ev, sizeof(struct lttng_event));
+               ret = ask_sessiond(LTTNG_KERNEL_ENABLE_EVENT, NULL);
+       }
+
+       return ret;
 }
 
 /*
- *  lttng_ust_start_trace
+ *  lttng_kernel_disable_event
  *
- *  Request a trace start for pid.
+ *  Disable an event in the kernel tracer.
  */
-int lttng_ust_start_trace(pid_t pid)
+int lttng_kernel_disable_event(char *name, char *channel_name)
 {
        int ret;
 
-       lsm.pid = pid;
-       ret = ask_sessiond(UST_START_TRACE, NULL);
+       if (channel_name == NULL) {
+               strncpy(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, NAME_MAX);
+       } else {
+               strncpy(lsm.u.disable.channel_name, channel_name, NAME_MAX);
+       }
+
+       if (name == NULL) {
+               ret = ask_sessiond(LTTNG_KERNEL_DISABLE_ALL_EVENT, NULL);
+       } else {
+               strncpy(lsm.u.disable.name, name, NAME_MAX);
+               ret = ask_sessiond(LTTNG_KERNEL_DISABLE_EVENT, NULL);
+       }
 
        return ret;
 }
 
 /*
- *  lttng_ust_stop_trace
+ *  lttng_kernel_enable_channel
  *
- *  Request a trace stop for pid.
+ *  Enable recording for a channel for the kernel tracer.
  */
-int lttng_ust_stop_trace(pid_t pid)
+int lttng_kernel_enable_channel(char *name)
 {
-       int ret;
+       strncpy(lsm.u.enable.channel_name, name, NAME_MAX);
+       return ask_sessiond(LTTNG_KERNEL_ENABLE_CHANNEL, NULL);
+}
 
-       lsm.pid = pid;
-       ret = ask_sessiond(UST_STOP_TRACE, NULL);
+/*
+ *  lttng_kernel_disable_channel
+ *
+ *  Disable recording for the channel for the kernel tracer.
+ */
+int lttng_kernel_disable_channel(char *name)
+{
+       strncpy(lsm.u.disable.channel_name, name, NAME_MAX);
+       return ask_sessiond(LTTNG_KERNEL_DISABLE_CHANNEL, NULL);
+}
 
-       return ret;
+/*
+ *  lttng_kernel_create_channel
+ *
+ *  Create a channel in the kernel tracer.
+ */
+int lttng_kernel_create_channel(struct lttng_channel *chan)
+{
+       memcpy(&lsm.u.channel.chan, chan, sizeof(struct lttng_channel));
+       return ask_sessiond(LTTNG_KERNEL_CREATE_CHANNEL, NULL);
 }
 
 /*
- *  lttng_ust_create_trace
+ *  lttng_list_events
+ *
+ *  List all available events in the kernel.
  *
- *  Request a trace creation for pid.
+ *  Return the size (bytes) of the list and set the event_list array.
+ *  On error, return negative value.
  */
-int lttng_ust_create_trace(pid_t pid)
+int lttng_kernel_list_events(char **event_list)
 {
-       int ret;
+       return ask_sessiond(LTTNG_KERNEL_LIST_EVENTS, (void **) event_list);
+}
 
-       lsm.pid = pid;
-       ret = ask_sessiond(UST_CREATE_TRACE, NULL);
+/*
+ * END Kernel control API
+ */
 
-       return ret;
+/*
+ *  lttng_get_readable_code
+ *
+ *  Return a human readable string of code
+ */
+const char *lttng_get_readable_code(int code)
+{
+       if (code > -LTTCOMM_OK) {
+               return "Ended with errors";
+       }
+
+       return lttcomm_get_readable_code(code);
 }
 
 /*
  *  lttng_ust_list_apps
  *
- *  Ask the session daemon for all UST traceable
- *  applications.
+ *  Ask the session daemon for all UST traceable applications.
  *
  *  Return the number of pids.
  *  On error, return negative value.
  */
-int lttng_ust_list_apps(pid_t **pids)
+int lttng_ust_list_traceable_apps(pid_t **pids)
 {
        int ret;
 
-       ret = ask_sessiond(UST_LIST_APPS, (void**) pids);
+       ret = ask_sessiond(LTTNG_LIST_TRACEABLE_APPS, (void**) pids);
        if (ret < 0) {
                return ret;
        }
@@ -321,16 +404,18 @@ int lttng_ust_list_apps(pid_t **pids)
 /*
  *  lttng_list_traces
  *
- *  Ask the session daemon for all traces (kernel and ust)
- *  for the session identified by uuid.
+ *  Ask the session daemon for all traces (kernel and ust) for the session
+ *  identified by name.
  *
  *  Return the number of traces.
+ *  On error, return negative value.
  */
-int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces)
+/*
+int lttng_list_traces(char *session_name, struct lttng_trace **traces)
 {
        int ret;
 
-       uuid_copy(lsm.session_uuid, *uuid);
+       strncpy(lsm.session_name, session_name, NAME_MAX);
 
        ret = ask_sessiond(LTTNG_LIST_TRACES, (void **) traces);
        if (ret < 0) {
@@ -339,29 +424,18 @@ int lttng_list_traces(uuid_t *uuid, struct lttng_trace **traces)
 
        return ret / sizeof(struct lttng_trace);
 }
+*/
 
 /*
  *  lttng_create_session
  *
- *  Create a brand new session using name. Allocate
- *  the session_id param pointing to the UUID.
+ *  Create a brand new session using name.
  */
-int lttng_create_session(char *name, uuid_t *session_id)
+int lttng_create_session(char *name, char *path)
 {
-       int ret;
-
-       strncpy(lsm.session_name, name, sizeof(lsm.session_name));
-       lsm.session_name[sizeof(lsm.session_name) - 1] = '\0';
-
-       ret = ask_sessiond(LTTNG_CREATE_SESSION, NULL);
-       if (ret < 0) {
-               goto end;
-       }
-
-       uuid_copy(*session_id, llm.session_uuid);
-
-end:
-       return ret;
+       strncpy(lsm.session_name, name, NAME_MAX);
+       strncpy(lsm.path, path, PATH_MAX);
+       return ask_sessiond(LTTNG_CREATE_SESSION, NULL);
 }
 
 /*
@@ -369,19 +443,10 @@ end:
  *
  *  Destroy session using name.
  */
-int lttng_destroy_session(uuid_t *uuid)
+int lttng_destroy_session(char *name)
 {
-       int ret;
-
-       uuid_copy(lsm.session_uuid, *uuid);
-
-       ret = ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
-       if (ret < 0) {
-               goto end;
-       }
-
-end:
-       return ret;
+       strncpy(lsm.session_name, name, NAME_MAX);
+       return ask_sessiond(LTTNG_DESTROY_SESSION, NULL);
 }
 
 /*
@@ -450,14 +515,9 @@ int lttng_disconnect_sessiond(void)
        return ret;
 }
 
-/*
- *  lttng_set_current_session_uuid
- *
- *  Set the session uuid for current lsm.
- */
-void lttng_set_current_session_uuid(uuid_t *uuid)
+void lttng_set_session_name(char *name)
 {
-       uuid_copy(lsm.session_uuid, *uuid);
+       strncpy(lsm.session_name, name, NAME_MAX);
 }
 
 /*
@@ -478,102 +538,29 @@ int lttng_set_tracing_group(const char *name)
 /*
  *  lttng_check_session_daemon
  *
- *  Return 0 if a sesssion daemon is available
- *  else return -1
+ *  Yes, return 1
+ *  No, return 0
+ *  Error, return negative value
  */
-int lttng_check_session_daemon(void)
+int lttng_session_daemon_alive(void)
 {
        int ret;
 
        ret = set_session_daemon_path();
        if (ret < 0) {
+               /* Error */
                return ret;
        }
 
        /* If socket exist, we consider the daemon started */
        ret = access(sessiond_sock_path, F_OK);
        if (ret < 0) {
-               return ret;
+               /* Not alive */
+               return 0;
        }
 
-       return 0;
-}
-
-/*
- *  set_session_daemon_path
- *
- *  Set sessiond socket path by putting it in 
- *  the global sessiond_sock_path variable.
- */
-static int set_session_daemon_path(void)
-{
-       int ret;
-
-       /* Are we in the tracing group ? */
-       ret = check_tracing_group(tracing_group);
-       if (ret < 0 && getuid() != 0) {
-               if (sprintf(sessiond_sock_path, DEFAULT_HOME_CLIENT_UNIX_SOCK,
-                                       getenv("HOME")) < 0) {
-                       return -ENOMEM;
-               }
-       } else {
-               strncpy(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
-                               sizeof(DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
-       }
-
-       return 0;
-}
-
-/*
- *  check_tracing_group
- *
- *  Check if the specified group name exist.
- *  If yes, 0, else -1
- */
-static int check_tracing_group(const char *grp_name)
-{
-       struct group *grp_tracing;      /* no free(). See getgrnam(3) */
-       gid_t *grp_list;
-       int grp_list_size, grp_id, i;
-       int ret = -1;
-
-       /* 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");
-               }
-               goto end;
-       }
-
-       /* Get number of supplementary group IDs */
-       grp_list_size = getgroups(0, NULL);
-       if (grp_list_size < 0) {
-               perror("getgroups");
-               goto end;
-       }
-
-       /* Alloc group list of the right size */
-       grp_list = malloc(grp_list_size * sizeof(gid_t));
-       grp_id = getgroups(grp_list_size, grp_list);
-       if (grp_id < -1) {
-               perror("getgroups");
-               goto free_list;
-       }
-
-       for (i = 0; i < grp_list_size; i++) {
-               if (grp_list[i] == grp_tracing->gr_gid) {
-                       ret = 0;
-                       break;
-               }
-       }
-
-free_list:
-       free(grp_list);
-
-end:
-       return ret;
+       /* Is alive */
+       return 1;
 }
 
 /*
@@ -582,5 +569,5 @@ end:
 static void __attribute__((constructor)) init()
 {
        /* Set default session group */
-       lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
+       lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
 }
This page took 0.030804 seconds and 4 git commands to generate.