+/*
+ * get_cmdline_by_pid
+ *
+ * Get command line from /proc for a
+ * specific pid. Allocate cmdline so the
+ * user must free() that pointer.
+ *
+ * On success, return 1
+ * On error (not found), return 0
+ */
+static int get_cmdline_by_pid(pid_t pid, char **cmdline)
+{
+ int ret;
+ FILE *fp;
+ char path[24]; /* Can't go bigger than /proc/65535/cmdline */
+
+ snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
+ fp = fopen(path, "r");
+ if (fp == NULL) {
+ goto not_running;
+ }
+
+ /* Caller must free() *cmdline */
+ *cmdline = malloc(PATH_MAX);
+ ret = fread(*cmdline, 1, PATH_MAX, fp);
+ fclose(fp);
+
+ return 1;
+
+not_running:
+ return 0;
+}
+
+/*
+ * spawn_sessiond
+ *
+ * Spawn a session daemon by forking and execv.
+ */
+static int spawn_sessiond(char *pathname)
+{
+ int ret = 0;
+ pid_t pid;
+
+ MSG("Spawning session daemon");
+ pid = fork();
+ if (pid == 0) {
+ /* Spawn session daemon and tell
+ * it to signal us when ready.
+ */
+ ret = execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL);
+ if (ret < 0) {
+ if (errno == ENOENT) {
+ ERR("No session daemon found. Use --sessiond-path.");
+ } else {
+ perror("execlp");
+ }
+ kill(getppid(), SIGTERM);
+ exit(EXIT_FAILURE);
+ }
+ exit(EXIT_SUCCESS);
+ } else if (pid > 0) {
+ /* Wait for ltt-sessiond to start */
+ pause();
+ goto end;
+ } else {
+ perror("fork");
+ ret = -1;
+ goto end;
+ }
+
+end:
+ return ret;
+}
+