X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=ltt-sessiond%2Fmain.c;h=44b0c84ba0bedf98f301c237fbf57e1994660a79;hp=69d5f6b7f1b10feb51463f86aba65f9b1386cbda;hb=7d29a2477524f7ee2ee46a94e538e6141f5ecc0e;hpb=04ea676f7fb2a6b3d5470737b498418ab37d5e0a diff --git a/ltt-sessiond/main.c b/ltt-sessiond/main.c index 69d5f6b7f..44b0c84ba 100644 --- a/ltt-sessiond/main.c +++ b/ltt-sessiond/main.c @@ -49,7 +49,6 @@ #include "session.h" #include "traceable-app.h" #include "lttng-kconsumerd.h" -#include "libustctl.h" #include "utils.h" /* @@ -90,6 +89,12 @@ static int kconsumerd_cmd_sock; static int kernel_tracer_fd; static int kernel_poll_pipe[2]; +/* + * Quit pipe for all threads. This permits a single cancellation point + * for all threads when receiving an event on the pipe. + */ +static int thread_quit_pipe[2]; + /* Pthread, Mutexes and Semaphores */ static pthread_t kconsumerd_thread; static pthread_t apps_thread; @@ -110,6 +115,25 @@ static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid */ static struct ltt_session_list *session_list_ptr; +/* + * Init quit pipe. + * + * Return -1 on error or 0 if all pipes are created. + */ +static int init_thread_quit_pipe(void) +{ + int ret; + + ret = pipe2(thread_quit_pipe, O_CLOEXEC); + if (ret < 0) { + perror("thread quit pipe"); + goto error; + } + +error: + return ret; +} + /* * teardown_kernel_session * @@ -127,9 +151,7 @@ static void teardown_kernel_session(struct ltt_session *session) } /* - * cleanup - * - * Cleanup the daemon on exit + * Cleanup the daemon */ static void cleanup() { @@ -140,23 +162,15 @@ static void cleanup() DBG("Cleaning up"); /* */ - MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0); - MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0); + MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm" + "Matthew, BEET driven development works!%c[%dm", + 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); /* */ /* Stopping all threads */ DBG("Terminating all threads"); - pthread_cancel(client_thread); - pthread_cancel(apps_thread); - pthread_cancel(kernel_thread); - if (kconsumerd_pid != 0) { - pthread_cancel(kconsumerd_thread); - } - - DBG("Unlinking all unix socket"); - unlink(client_unix_sock_path); - unlink(apps_unix_sock_path); - unlink(kconsumerd_err_unix_sock_path); + close(thread_quit_pipe[0]); + close(thread_quit_pipe[1]); DBG("Removing %s directory", LTTNG_RUNDIR); ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR); @@ -171,19 +185,22 @@ static void cleanup() } DBG("Cleaning up all session"); - /* Cleanup ALL session */ - cds_list_for_each_entry(sess, &session_list_ptr->head, list) { - teardown_kernel_session(sess); - // TODO complete session cleanup (including UST) - } /* Destroy session list mutex */ - pthread_mutex_destroy(&session_list_ptr->lock); + if (session_list_ptr != NULL) { + pthread_mutex_destroy(&session_list_ptr->lock); + + /* Cleanup ALL session */ + cds_list_for_each_entry(sess, &session_list_ptr->head, list) { + teardown_kernel_session(sess); + // TODO complete session cleanup (including UST) + } + } + + pthread_mutex_destroy(&kconsumerd_pid_mutex); DBG("Closing kernel fd"); close(kernel_tracer_fd); - close(kernel_poll_pipe[0]); - close(kernel_poll_pipe[1]); } /* @@ -208,18 +225,18 @@ static int send_unix_sock(int sock, void *buf, size_t len) * * Free memory of a command context structure. */ -static void clean_command_ctx(struct command_ctx *cmd_ctx) +static void clean_command_ctx(struct command_ctx **cmd_ctx) { - DBG("Clean command context structure %p", cmd_ctx); - if (cmd_ctx) { - if (cmd_ctx->llm) { - free(cmd_ctx->llm); + DBG("Clean command context structure"); + if (*cmd_ctx) { + if ((*cmd_ctx)->llm) { + free((*cmd_ctx)->llm); } - if (cmd_ctx->lsm) { - free(cmd_ctx->lsm); + if ((*cmd_ctx)->lsm) { + free((*cmd_ctx)->lsm); } - free(cmd_ctx); - cmd_ctx = NULL; + free(*cmd_ctx); + *cmd_ctx = NULL; } } @@ -332,6 +349,7 @@ error: return ret; } +#ifdef DISABLED /* * ust_connect_app * @@ -362,6 +380,7 @@ static int ust_connect_app(pid_t pid) return sock; } +#endif /* DISABLED */ /* * notify_apps @@ -439,14 +458,18 @@ error: static int update_kernel_pollfd(void) { int i = 0; - unsigned int nb_fd = 1; + /* + * The wakup pipe and the quit pipe are needed so the number of fds starts + * at 2 for those pipes. + */ + unsigned int nb_fd = 2; struct ltt_session *session; struct ltt_kernel_channel *channel; DBG("Updating kernel_pollfd"); /* Get the number of channel of all kernel session */ - pthread_mutex_lock(&session_list_ptr->lock); + lock_session_list(); cds_list_for_each_entry(session, &session_list_ptr->head, list) { lock_session(session); if (session->kernel_session == NULL) { @@ -483,16 +506,19 @@ static int update_kernel_pollfd(void) } unlock_session(session); } - pthread_mutex_unlock(&session_list_ptr->lock); + unlock_session_list(); /* Adding wake up pipe */ - kernel_pollfd[nb_fd - 1].fd = kernel_poll_pipe[0]; - kernel_pollfd[nb_fd - 1].events = POLLIN; + kernel_pollfd[nb_fd - 2].fd = kernel_poll_pipe[0]; + kernel_pollfd[nb_fd - 2].events = POLLIN; + + /* Adding the quit pipe */ + kernel_pollfd[nb_fd - 1].fd = thread_quit_pipe[0]; return nb_fd; error: - pthread_mutex_unlock(&session_list_ptr->lock); + unlock_session_list(); return -1; } @@ -512,7 +538,7 @@ static int update_kernel_stream(int fd) DBG("Updating kernel streams for channel fd %d", fd); - pthread_mutex_lock(&session_list_ptr->lock); + lock_session_list(); cds_list_for_each_entry(session, &session_list_ptr->head, list) { lock_session(session); if (session->kernel_session == NULL) { @@ -543,10 +569,10 @@ static int update_kernel_stream(int fd) } end: + unlock_session_list(); if (session) { unlock_session(session); } - pthread_mutex_unlock(&session_list_ptr->lock); return ret; } @@ -587,13 +613,18 @@ static void *thread_manage_kernel(void *data) continue; } + /* Thread quit pipe has been closed. Killing thread. */ + if (kernel_pollfd[nb_fd - 1].revents == POLLNVAL) { + goto error; + } + DBG("Kernel poll event triggered"); /* * Check if the wake up pipe was triggered. If so, the kernel_pollfd * must be updated. */ - switch (kernel_pollfd[nb_fd - 1].revents) { + switch (kernel_pollfd[nb_fd - 2].revents) { case POLLIN: ret = read(kernel_poll_pipe[0], &tmp, 1); update_poll_flag = 1; @@ -625,6 +656,9 @@ error: if (kernel_pollfd) { free(kernel_pollfd); } + + close(kernel_poll_pipe[0]); + close(kernel_poll_pipe[1]); return NULL; } @@ -636,8 +670,9 @@ error: */ static void *thread_manage_kconsumerd(void *data) { - int sock, ret; + int sock = 0, ret; enum lttcomm_return_code code; + struct pollfd pollfd[2]; DBG("[thread] Manage kconsumerd started"); @@ -646,6 +681,28 @@ static void *thread_manage_kconsumerd(void *data) goto error; } + /* First fd is always the quit pipe */ + pollfd[0].fd = thread_quit_pipe[0]; + + /* Apps socket */ + pollfd[1].fd = kconsumerd_err_sock; + pollfd[1].events = POLLIN; + + /* Inifinite blocking call, waiting for transmission */ + ret = poll(pollfd, 2, -1); + if (ret < 0) { + perror("poll kconsumerd thread"); + goto error; + } + + /* Thread quit pipe has been closed. Killing thread. */ + if (pollfd[0].revents == POLLNVAL) { + goto error; + } else if (pollfd[1].revents == POLLERR) { + ERR("Kconsumerd err socket poll error"); + goto error; + } + sock = lttcomm_accept_unix_sock(kconsumerd_err_sock); if (sock < 0) { goto error; @@ -683,8 +740,21 @@ static void *thread_manage_kconsumerd(void *data) ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code)); error: - kconsumerd_pid = 0; DBG("Kconsumerd thread dying"); + if (kconsumerd_err_sock) { + close(kconsumerd_err_sock); + } + if (kconsumerd_cmd_sock) { + close(kconsumerd_cmd_sock); + } + if (sock) { + close(sock); + } + + unlink(kconsumerd_err_unix_sock_path); + unlink(kconsumerd_cmd_unix_sock_path); + + kconsumerd_pid = 0; return NULL; } @@ -695,7 +765,8 @@ error: */ static void *thread_manage_apps(void *data) { - int sock, ret; + int sock = 0, ret; + struct pollfd pollfd[2]; /* TODO: Something more elegant is needed but fine for now */ /* FIXME: change all types to either uint8_t, uint32_t, uint64_t @@ -714,18 +785,41 @@ static void *thread_manage_apps(void *data) goto error; } + /* First fd is always the quit pipe */ + pollfd[0].fd = thread_quit_pipe[0]; + + /* Apps socket */ + pollfd[1].fd = apps_sock; + pollfd[1].events = POLLIN; + /* Notify all applications to register */ notify_apps(default_global_apps_pipe); while (1) { DBG("Accepting application registration"); - /* Blocking call, waiting for transmission */ + + /* Inifinite blocking call, waiting for transmission */ + ret = poll(pollfd, 2, -1); + if (ret < 0) { + perror("poll apps thread"); + goto error; + } + + /* Thread quit pipe has been closed. Killing thread. */ + if (pollfd[0].revents == POLLNVAL) { + goto error; + } else if (pollfd[1].revents == POLLERR) { + ERR("Apps socket poll error"); + goto error; + } + sock = lttcomm_accept_unix_sock(apps_sock); if (sock < 0) { goto error; } - /* Basic recv here to handle the very simple data + /* + * Basic recv here to handle the very simple data * that the libust send to register (reg_msg). */ ret = recv(sock, ®_msg, sizeof(reg_msg), 0); @@ -751,7 +845,15 @@ static void *thread_manage_apps(void *data) } error: + DBG("Apps thread dying"); + if (apps_sock) { + close(apps_sock); + } + if (sock) { + close(sock); + } + unlink(apps_unix_sock_path); return NULL; } @@ -1088,7 +1190,6 @@ error: static int create_kernel_session(struct ltt_session *session) { int ret; - struct lttng_channel *chan; DBG("Creating kernel session"); @@ -1098,12 +1199,6 @@ static int create_kernel_session(struct ltt_session *session) goto error; } - chan = init_default_channel(); - if (chan == NULL) { - ret = LTTCOMM_FATAL; - goto error; - } - ret = mkdir_recursive(session->path, S_IRWXU | S_IRWXG ); if (ret < 0) { if (ret != EEXIST) { @@ -1112,20 +1207,34 @@ static int create_kernel_session(struct ltt_session *session) } } - DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME); - - ret = kernel_create_channel(session->kernel_session, chan, session->path); - if (ret < 0) { - ret = LTTCOMM_KERN_CHAN_FAIL; - goto error; - } - - ret = notify_kernel_pollfd(); - error: return ret; } +/* + * Using the session list, filled a lttng_session array to send back to the + * client for session listing. + * + * The session list lock MUST be acquired before calling this function. Use + * lock_session_list() and unlock_session_list(). + */ +static void list_lttng_sessions(struct lttng_session *sessions) +{ + int i = 0; + struct ltt_session *session; + + DBG("Getting all available session"); + /* + * Iterate over session list and append data after the control struct in + * the buffer. + */ + cds_list_for_each_entry(session, &session_list_ptr->head, list) { + strncpy(sessions[i].path, session->path, PATH_MAX); + strncpy(sessions[i].name, session->name, NAME_MAX); + i++; + } +} + /* * process_client_msg * @@ -1172,7 +1281,6 @@ static int process_client_msg(struct command_ctx *cmd_ctx) */ switch (cmd_ctx->lsm->cmd_type) { case LTTNG_KERNEL_ADD_CONTEXT: - case LTTNG_KERNEL_CREATE_CHANNEL: case LTTNG_KERNEL_DISABLE_ALL_EVENT: case LTTNG_KERNEL_DISABLE_CHANNEL: case LTTNG_KERNEL_DISABLE_EVENT: @@ -1209,6 +1317,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx) } } +#ifdef DISABLED /* Connect to ust apps if available pid */ if (cmd_ctx->lsm->pid > 0) { /* Connect to app using ustctl API */ @@ -1218,6 +1327,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx) goto error; } } +#endif /* DISABLED */ /* Process by command type */ switch (cmd_ctx->lsm->cmd_type) { @@ -1226,6 +1336,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx) int found = 0, no_event = 0; struct ltt_kernel_channel *chan; struct ltt_kernel_event *event; + struct lttng_kernel_context ctx; /* Setup lttng message with no payload */ ret = setup_lttng_msg(cmd_ctx, 0); @@ -1238,22 +1349,28 @@ static int process_client_msg(struct command_ctx *cmd_ctx) no_event = 1; } + /* Create Kernel context */ + ctx.ctx = cmd_ctx->lsm->u.context.ctx.ctx; + ctx.u.perf_counter.type = cmd_ctx->lsm->u.context.ctx.u.perf_counter.type; + ctx.u.perf_counter.config = cmd_ctx->lsm->u.context.ctx.u.perf_counter.config; + strncpy(ctx.u.perf_counter.name, + cmd_ctx->lsm->u.context.ctx.u.perf_counter.name, + sizeof(ctx.u.perf_counter.name)); + if (strlen(cmd_ctx->lsm->u.context.channel_name) == 0) { /* Go over all channels */ DBG("Adding context to all channels"); cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { if (no_event) { - ret = kernel_add_channel_context(chan, - &cmd_ctx->lsm->u.context.ctx); + ret = kernel_add_channel_context(chan, &ctx); if (ret < 0) { continue; } } else { event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan); if (event != NULL) { - ret = kernel_add_event_context(event, - &cmd_ctx->lsm->u.context.ctx); + ret = kernel_add_event_context(event, &ctx); if (ret < 0) { ret = LTTCOMM_KERN_CONTEXT_FAIL; goto error; @@ -1272,8 +1389,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx) } if (no_event) { - ret = kernel_add_channel_context(chan, - &cmd_ctx->lsm->u.context.ctx); + ret = kernel_add_channel_context(chan, &ctx); if (ret < 0) { ret = LTTCOMM_KERN_CONTEXT_FAIL; goto error; @@ -1281,8 +1397,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx) } else { event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan); if (event != NULL) { - ret = kernel_add_event_context(event, - &cmd_ctx->lsm->u.context.ctx); + ret = kernel_add_event_context(event, &ctx); if (ret < 0) { ret = LTTCOMM_KERN_CONTEXT_FAIL; goto error; @@ -1299,33 +1414,6 @@ static int process_client_msg(struct command_ctx *cmd_ctx) ret = LTTCOMM_OK; break; } - case LTTNG_KERNEL_CREATE_CHANNEL: - { - /* Setup lttng message with no payload */ - ret = setup_lttng_msg(cmd_ctx, 0); - if (ret < 0) { - goto setup_error; - } - - /* Kernel tracer */ - DBG("Creating kernel channel"); - - ret = kernel_create_channel(cmd_ctx->session->kernel_session, - &cmd_ctx->lsm->u.channel.chan, cmd_ctx->session->path); - if (ret < 0) { - ret = LTTCOMM_KERN_CHAN_FAIL; - goto error; - } - - ret = notify_kernel_pollfd(); - if (ret < 0) { - ret = LTTCOMM_FATAL; - goto error; - } - - ret = LTTCOMM_OK; - break; - } case LTTNG_KERNEL_DISABLE_CHANNEL: { struct ltt_kernel_channel *chan; @@ -1436,8 +1524,22 @@ static int process_client_msg(struct command_ctx *cmd_ctx) chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name, cmd_ctx->session->kernel_session); if (chan == NULL) { - ret = LTTCOMM_KERN_CHAN_NOT_FOUND; - goto error; + /* Channel not found, creating it */ + DBG("Creating kernel channel"); + + ret = kernel_create_channel(cmd_ctx->session->kernel_session, + &cmd_ctx->lsm->u.channel.chan, cmd_ctx->session->path); + if (ret < 0) { + ret = LTTCOMM_KERN_CHAN_FAIL; + goto error; + } + + /* Notify kernel thread that there is a new channel */ + ret = notify_kernel_pollfd(); + if (ret < 0) { + ret = LTTCOMM_FATAL; + goto error; + } } else if (chan->enabled == 0) { ret = kernel_enable_channel(chan); if (ret < 0) { @@ -1454,8 +1556,10 @@ static int process_client_msg(struct command_ctx *cmd_ctx) } case LTTNG_KERNEL_ENABLE_EVENT: { - struct ltt_kernel_channel *chan; + char *channel_name; + struct ltt_kernel_channel *kchan; struct ltt_kernel_event *ev; + struct lttng_channel *chan; /* Setup lttng message with no payload */ ret = setup_lttng_msg(cmd_ctx, 0); @@ -1463,22 +1567,42 @@ static int process_client_msg(struct command_ctx *cmd_ctx) goto setup_error; } - chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name, - cmd_ctx->session->kernel_session); - if (chan == NULL) { - ret = LTTCOMM_KERN_CHAN_NOT_FOUND; - goto error; - } + channel_name = cmd_ctx->lsm->u.enable.channel_name; - ev = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, chan); + do { + kchan = get_kernel_channel_by_name(channel_name, + cmd_ctx->session->kernel_session); + if (kchan == NULL) { + DBG("Creating default channel"); + + chan = init_default_channel(); + if (chan == NULL) { + ret = LTTCOMM_FATAL; + goto error; + } + + ret = kernel_create_channel(cmd_ctx->session->kernel_session, + chan, cmd_ctx->session->path); + if (ret < 0) { + ret = LTTCOMM_KERN_CHAN_FAIL; + goto error; + } + } + } while (kchan == NULL); + + ev = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan); if (ev == NULL) { DBG("Creating kernel event %s for channel %s.", - cmd_ctx->lsm->u.enable.event.name, chan->channel->name); - ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, chan); + cmd_ctx->lsm->u.enable.event.name, channel_name); + ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, kchan); } else { DBG("Enabling kernel event %s for channel %s.", - cmd_ctx->lsm->u.enable.event.name, chan->channel->name); + cmd_ctx->lsm->u.enable.event.name, channel_name); ret = kernel_enable_event(ev); + if (ret == -EEXIST) { + ret = LTTCOMM_KERN_EVENT_EXIST; + goto error; + } } if (ret < 0) { @@ -1493,10 +1617,11 @@ static int process_client_msg(struct command_ctx *cmd_ctx) case LTTNG_KERNEL_ENABLE_ALL_EVENT: { int pos, size; - char *event_list, *event, *ptr; - struct ltt_kernel_channel *chan; + char *event_list, *event, *ptr, *channel_name; + struct ltt_kernel_channel *kchan; struct ltt_kernel_event *ev; struct lttng_event ev_attr; + struct lttng_channel *chan; /* Setup lttng message with no payload */ ret = setup_lttng_msg(cmd_ctx, 0); @@ -1506,17 +1631,33 @@ static int process_client_msg(struct command_ctx *cmd_ctx) DBG("Enabling all kernel event"); - chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name, - cmd_ctx->session->kernel_session); - if (chan == NULL) { - ret = LTTCOMM_KERN_CHAN_NOT_FOUND; - goto error; - } + channel_name = cmd_ctx->lsm->u.enable.channel_name; + + do { + kchan = get_kernel_channel_by_name(channel_name, + cmd_ctx->session->kernel_session); + if (kchan == NULL) { + DBG("Creating default channel"); + + chan = init_default_channel(); + if (chan == NULL) { + ret = LTTCOMM_FATAL; + goto error; + } + + ret = kernel_create_channel(cmd_ctx->session->kernel_session, + &cmd_ctx->lsm->u.channel.chan, cmd_ctx->session->path); + if (ret < 0) { + ret = LTTCOMM_KERN_CHAN_FAIL; + goto error; + } + } + } while (kchan == NULL); /* For each event in the kernel session */ - cds_list_for_each_entry(ev, &chan->events_list.head, list) { + cds_list_for_each_entry(ev, &kchan->events_list.head, list) { DBG("Enabling kernel event %s for channel %s.", - ev->event->name, chan->channel->name); + ev->event->name, channel_name); ret = kernel_enable_event(ev); if (ret < 0) { continue; @@ -1531,13 +1672,13 @@ static int process_client_msg(struct command_ctx *cmd_ctx) ptr = event_list; while ((size = sscanf(ptr, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) { - ev = get_kernel_event_by_name(event, chan); + ev = get_kernel_event_by_name(event, kchan); if (ev == NULL) { strncpy(ev_attr.name, event, LTTNG_SYM_NAME_LEN); /* Default event type for enable all */ - ev_attr.type = LTTNG_EVENT_TRACEPOINTS; + ev_attr.type = LTTNG_EVENT_TRACEPOINT; /* Enable each single tracepoint event */ - ret = kernel_create_event(&ev_attr, chan); + ret = kernel_create_event(&ev_attr, kchan); if (ret < 0) { /* Ignore error here and continue */ } @@ -1837,20 +1978,23 @@ static int process_client_msg(struct command_ctx *cmd_ctx) */ case LTTNG_LIST_SESSIONS: { - unsigned int session_count; + lock_session_list(); - session_count = get_session_count(); - if (session_count == 0) { + if (session_list_ptr->count == 0) { ret = LTTCOMM_NO_SESSION; goto error; } - ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * session_count); + ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * + session_list_ptr->count); if (ret < 0) { goto setup_error; } - get_lttng_session((struct lttng_session *)(cmd_ctx->llm->payload)); + /* Filled the session array */ + list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload)); + + unlock_session_list(); ret = LTTCOMM_OK; break; @@ -1900,8 +2044,9 @@ setup_error: */ static void *thread_manage_clients(void *data) { - int sock, ret; - struct command_ctx *cmd_ctx; + int sock = 0, ret; + struct command_ctx *cmd_ctx = NULL; + struct pollfd pollfd[2]; DBG("[thread] Manage client started"); @@ -1910,6 +2055,13 @@ static void *thread_manage_clients(void *data) goto error; } + /* First fd is always the quit pipe */ + pollfd[0].fd = thread_quit_pipe[0]; + + /* Apps socket */ + pollfd[1].fd = client_sock; + pollfd[1].events = POLLIN; + /* Notify parent pid that we are ready * to accept command for client side. */ @@ -1918,8 +2070,23 @@ static void *thread_manage_clients(void *data) } while (1) { - /* Blocking call, waiting for transmission */ DBG("Accepting client command ..."); + + /* Inifinite blocking call, waiting for transmission */ + ret = poll(pollfd, 2, -1); + if (ret < 0) { + perror("poll client thread"); + goto error; + } + + /* Thread quit pipe has been closed. Killing thread. */ + if (pollfd[0].revents == POLLNVAL) { + goto error; + } else if (pollfd[1].revents == POLLERR) { + ERR("Client socket poll error"); + goto error; + } + sock = lttcomm_accept_unix_sock(client_sock); if (sock < 0) { goto error; @@ -1957,7 +2124,7 @@ static void *thread_manage_clients(void *data) /* TODO: Inform client somehow of the fatal error. At this point, * ret < 0 means that a malloc failed (ENOMEM). */ /* Error detected but still accept command */ - clean_command_ctx(cmd_ctx); + clean_command_ctx(&cmd_ctx); continue; } @@ -1968,13 +2135,24 @@ static void *thread_manage_clients(void *data) ERR("Failed to send data back to client"); } - clean_command_ctx(cmd_ctx); + clean_command_ctx(&cmd_ctx); /* End of transmission */ close(sock); } error: + DBG("Client thread dying"); + if (client_sock) { + close(client_sock); + } + if (sock) { + close(sock); + } + + unlink(client_unix_sock_path); + + clean_command_ctx(&cmd_ctx); return NULL; } @@ -2360,6 +2538,7 @@ static void set_ulimit(void) int ret; struct rlimit lim; + /* The kernel does not allowed an infinite limit for open files */ lim.rlim_cur = 65535; lim.rlim_max = 65535; @@ -2378,10 +2557,15 @@ int main(int argc, char **argv) void *status; const char *home_path; + /* Create thread quit pipe */ + if (init_thread_quit_pipe() < 0) { + goto exit; + } + /* Parse arguments */ progname = argv[0]; if ((ret = parse_args(argc, argv) < 0)) { - goto error; + goto exit; } /* Daemonize */ @@ -2389,7 +2573,7 @@ int main(int argc, char **argv) ret = daemon(0, 0); if (ret < 0) { perror("daemon"); - goto error; + goto exit; } } @@ -2399,7 +2583,7 @@ int main(int argc, char **argv) if (is_root) { ret = create_lttng_rundir(); if (ret < 0) { - goto error; + goto exit; } if (strlen(apps_unix_sock_path) == 0) { @@ -2411,23 +2595,12 @@ int main(int argc, char **argv) snprintf(client_unix_sock_path, PATH_MAX, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); } - - ret = set_kconsumerd_sockets(); - if (ret < 0) { - goto error; - } - - /* Setup kernel tracer */ - init_kernel_tracer(); - - /* Set ulimit for open files */ - set_ulimit(); } else { home_path = get_home_dir(); if (home_path == NULL) { - ERR("Can't get HOME directory for sockets creation.\n \ - Please specify --socket PATH."); - goto error; + /* TODO: Add --socket PATH option */ + ERR("Can't get HOME directory for sockets creation."); + goto exit; } if (strlen(apps_unix_sock_path) == 0) { @@ -2445,15 +2618,39 @@ int main(int argc, char **argv) DBG("Client socket path %s", client_unix_sock_path); DBG("Application socket path %s", apps_unix_sock_path); - /* See if daemon already exist. If any of the two - * socket needed by the daemon are present, this test fails + /* + * See if daemon already exist. If any of the two socket needed by the + * daemon are present, this test fails. However, if the daemon is killed + * with a SIGKILL, those unix socket must be unlinked by hand. */ if ((ret = check_existing_daemon()) == 0) { ERR("Already running daemon.\n"); - /* We do not goto error because we must not - * cleanup() because a daemon is already running. + /* + * We do not goto error because we must not cleanup() because a daemon + * is already running. */ - exit(EXIT_FAILURE); + goto exit; + } + + /* After this point, we can safely call cleanup() so goto error is used */ + + /* + * These actions must be executed as root. We do that *after* setting up + * the sockets path because we MUST make the check for another daemon using + * those paths *before* trying to set the kernel consumer sockets and init + * kernel tracer. + */ + if (is_root) { + ret = set_kconsumerd_sockets(); + if (ret < 0) { + goto error; + } + + /* Setup kernel tracer */ + init_kernel_tracer(); + + /* Set ulimit for open files */ + set_ulimit(); } if (set_signal_handler() < 0) { @@ -2480,7 +2677,10 @@ int main(int argc, char **argv) goto error; } - /* Get session list pointer */ + /* + * Get session list pointer. This pointer MUST NOT be free(). + * This list is statically declared in session.c + */ session_list_ptr = get_session_list(); while (1) { @@ -2517,5 +2717,7 @@ int main(int argc, char **argv) error: cleanup(); + +exit: exit(EXIT_FAILURE); }