+ lttng_disconnect_sessiond();
+ return ret;
+}
+
+/*
+ * 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;
+}
+
+/*
+ * 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;
+}
+
+/*
+ * lttng_start_tracing
+ *
+ * Start tracing for all trace of the session.
+ */
+int lttng_start_tracing(char *session_name)
+{
+ strncpy(lsm.session_name, session_name, NAME_MAX);
+ return ask_sessiond(LTTNG_START_TRACE, NULL);
+}
+
+/*
+ * lttng_stop_tracing
+ *
+ * Stop tracing for all trace of the session.
+ */
+int lttng_stop_tracing(char *session_name)
+{
+ strncpy(lsm.session_name, session_name, NAME_MAX);
+ return ask_sessiond(LTTNG_STOP_TRACE, NULL);
+}
+
+/*
+ * BEGIN Kernel control API
+ */
+
+/*
+ * lttng_kernel_add_context
+ */
+int lttng_kernel_add_context(struct lttng_kernel_context *ctx,
+ char *event_name, char *channel_name)
+{
+ if (channel_name != NULL) {
+ strncpy(lsm.u.context.channel_name, channel_name, NAME_MAX);
+ }
+
+ 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_kernel_enable_event
+ */
+int lttng_kernel_enable_event(struct lttng_event *ev, char *channel_name)
+{
+ 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);
+ }
+
+ 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);
+ }
+