X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fmain.c;h=37c7354ef0827f22fd23a1f53c2a71464fb18c1a;hb=72dd74910ee17be5d82c6b2286f454942bf19d24;hp=21654957aac170fc0f17c5b86496ecfdc7e94eae;hpb=97bc1426a470ac30ed29357e5c68a256a79fef0e;p=lttng-tools.git diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index 21654957a..37c7354ef 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -46,6 +46,7 @@ #include #include #include +#include #include #include "lttng-sessiond.h" @@ -76,7 +77,7 @@ static int tracing_group_name_override; static char *opt_pidfile; static int opt_sig_parent; static int opt_verbose_consumer; -static int opt_daemon; +static int opt_daemon, opt_background; static int opt_no_kernel; static int is_root; /* Set to 1 if the daemon is running as root */ static pid_t ppid; /* Parent PID for --sig-parent option */ @@ -139,6 +140,7 @@ static const struct option long_options[] = { { "consumerd64-path", 1, 0, 't' }, { "consumerd64-libdir", 1, 0, 'T' }, { "daemonize", 0, 0, 'd' }, + { "background", 0, 0, 'b' }, { "sig-parent", 0, 0, 'S' }, { "help", 0, 0, 'h' }, { "group", 1, 0, 'g' }, @@ -310,7 +312,7 @@ void lttng_sessiond_notify_ready(void) * Notify the parent of the fork() process that we are * ready. */ - if (opt_daemon) { + if (opt_daemon || opt_background) { kill(child_ppid, SIGUSR1); } } @@ -4038,6 +4040,7 @@ static void usage(void) fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n"); fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n"); fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); + fprintf(stderr, " -b, --background Start as a daemon, keeping console open.\n"); fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); fprintf(stderr, " -V, --version Show version number.\n"); fprintf(stderr, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n"); @@ -4076,6 +4079,9 @@ static int set_option(int opt, const char *arg, const char *optname) case 'd': opt_daemon = 1; break; + case 'b': + opt_background = 1; + break; case 'g': tracing_group_name = strdup(arg); break; @@ -4691,107 +4697,6 @@ error: return; } -/* - * Daemonize this process by forking and making the parent wait for the child - * to signal it indicating readiness. Once received, the parent successfully - * quits. - * - * The child process undergoes the same action that daemon(3) does meaning - * setsid, chdir, and dup /dev/null into 0, 1 and 2. - * - * Return 0 on success else -1 on error. - */ -static int daemonize(void) -{ - int ret; - pid_t pid; - - /* Get parent pid of this process. */ - child_ppid = getppid(); - - pid = fork(); - if (pid < 0) { - PERROR("fork"); - goto error; - } else if (pid == 0) { - int fd; - pid_t sid; - - /* Child */ - - /* - * Get the newly created parent pid so we can signal that process when - * we are ready to operate. - */ - child_ppid = getppid(); - - sid = setsid(); - if (sid < 0) { - PERROR("setsid"); - goto error; - } - - /* Try to change directory to /. If we can't well at least notify. */ - ret = chdir("/"); - if (ret < 0) { - PERROR("chdir"); - } - - fd = open(_PATH_DEVNULL, O_RDWR, 0); - if (fd < 0) { - PERROR("open %s", _PATH_DEVNULL); - /* Let 0, 1 and 2 open since we can't bind them to /dev/null. */ - } else { - (void) dup2(fd, STDIN_FILENO); - (void) dup2(fd, STDOUT_FILENO); - (void) dup2(fd, STDERR_FILENO); - if (fd > 2) { - ret = close(fd); - if (ret < 0) { - PERROR("close"); - } - } - } - goto end; - } else { - /* Parent */ - - /* - * Waiting for child to notify this parent that it can exit. Note that - * sleep() is interrupted before the 1 second delay as soon as the - * signal is received, so it will not cause visible delay for the - * user. - */ - while (!CMM_LOAD_SHARED(recv_child_signal)) { - int status; - pid_t ret; - - /* - * Check if child exists without blocking. If so, we have to stop - * this parent process and return an error. - */ - ret = waitpid(pid, &status, WNOHANG); - if (ret < 0 || (ret != 0 && WIFEXITED(status))) { - /* The child exited somehow or was not valid. */ - goto error; - } - sleep(1); - } - - /* - * From this point on, the parent can exit and the child is now an - * operationnal session daemon ready to serve clients and applications. - */ - exit(EXIT_SUCCESS); - } - -end: - return 0; - -error: - return -1; -} - /* * main */ @@ -4825,10 +4730,11 @@ int main(int argc, char **argv) } /* Daemonize */ - if (opt_daemon) { + if (opt_daemon || opt_background) { int i; - ret = daemonize(); + ret = lttng_daemonize(&child_ppid, &recv_child_signal, + !opt_background); if (ret < 0) { goto error; }