1 // SPDX-License-Identifier: GPL-2.0
2 #include <internal/lib.h>
3 #include <subcmd/parse-options.h>
4 #include <api/fd/array.h>
6 #include <linux/zalloc.h>
7 #include <linux/string.h>
8 #include <linux/limits.h>
17 #include <sys/inotify.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
23 #include <sys/signalfd.h>
32 #define SESSION_OUTPUT "output"
33 #define SESSION_CONTROL "control"
34 #define SESSION_ACK "ack"
39 * OK - session is up and running
40 * RECONFIG - session is pending for reconfiguration,
41 * new values are already loaded in session object
42 * KILL - session is pending to be killed
44 * Session object life and its state is maintained by
45 * following functions:
48 * - reads config file and setup session objects
49 * with following states:
51 * OK - no change needed
52 * RECONFIG - session needs to be changed
53 * (run variable changed)
54 * KILL - session needs to be killed
55 * (session is no longer in config file)
58 * - scans session objects and does following actions
62 * RECONFIG - session is killed and re-run with new config
63 * KILL - session is killed
65 * - all sessions have OK state on the function exit
67 enum daemon_session_state {
73 struct daemon_session {
79 struct list_head list;
80 enum daemon_session_state state;
89 const char *base_user;
91 struct list_head sessions;
98 static struct daemon __daemon = {
99 .sessions = LIST_HEAD_INIT(__daemon.sessions),
102 static const char * const daemon_usage[] = {
103 "perf daemon start [<options>]",
104 "perf daemon [<options>]",
110 static void sig_handler(int sig __maybe_unused)
115 static struct daemon_session *daemon__add_session(struct daemon *config, char *name)
117 struct daemon_session *session = zalloc(sizeof(*session));
122 session->name = strdup(name);
123 if (!session->name) {
129 list_add_tail(&session->list, &config->sessions);
133 static struct daemon_session *daemon__find_session(struct daemon *daemon, char *name)
135 struct daemon_session *session;
137 list_for_each_entry(session, &daemon->sessions, list) {
138 if (!strcmp(session->name, name))
145 static int get_session_name(const char *var, char *session, int len)
147 const char *p = var + sizeof("session-") - 1;
149 while (*p != '.' && *p != 0x0 && len--)
153 return *p == '.' ? 0 : -EINVAL;
156 static int session_config(struct daemon *daemon, const char *var, const char *value)
158 struct daemon_session *session;
161 if (get_session_name(var, name, sizeof(name) - 1))
164 var = strchr(var, '.');
170 session = daemon__find_session(daemon, name);
173 /* New session is defined. */
174 session = daemon__add_session(daemon, name);
178 pr_debug("reconfig: found new session %s\n", name);
180 /* Trigger reconfig to start it. */
181 session->state = RECONFIG;
182 } else if (session->state == KILL) {
183 /* Current session is defined, no action needed. */
184 pr_debug("reconfig: found current session %s\n", name);
188 if (!strcmp(var, "run")) {
192 same = !strcmp(session->run, value);
197 pr_debug("reconfig: session %s is changed\n", name);
200 session->run = strdup(value);
205 * Either new or changed run value is defined,
206 * trigger reconfig for the session.
208 session->state = RECONFIG;
215 static int server_config(const char *var, const char *value, void *cb)
217 struct daemon *daemon = cb;
219 if (strstarts(var, "session-")) {
220 return session_config(daemon, var, value);
221 } else if (!strcmp(var, "daemon.base") && !daemon->base_user) {
222 if (daemon->base && strcmp(daemon->base, value)) {
223 pr_err("failed: can't redefine base, bailing out\n");
226 daemon->base = strdup(value);
234 static int client_config(const char *var, const char *value, void *cb)
236 struct daemon *daemon = cb;
238 if (!strcmp(var, "daemon.base") && !daemon->base_user) {
239 daemon->base = strdup(value);
247 static int check_base(struct daemon *daemon)
252 pr_err("failed: base not defined\n");
256 if (stat(daemon->base, &st)) {
259 pr_err("failed: permission denied for '%s' base\n",
263 pr_err("failed: base '%s' does not exists\n",
267 pr_err("failed: can't access base '%s': %s\n",
268 daemon->base, strerror(errno));
273 if ((st.st_mode & S_IFMT) != S_IFDIR) {
274 pr_err("failed: base '%s' is not directory\n",
282 static int setup_client_config(struct daemon *daemon)
284 struct perf_config_set *set = perf_config_set__load_file(daemon->config_real);
288 err = perf_config_set(set, client_config, daemon);
289 perf_config_set__delete(set);
292 return err ?: check_base(daemon);
295 static int setup_server_config(struct daemon *daemon)
297 struct perf_config_set *set;
298 struct daemon_session *session;
301 pr_debug("reconfig: started\n");
304 * Mark all sessions for kill, the server config
305 * will set following states, see explanation at
306 * enum daemon_session_state declaration.
308 list_for_each_entry(session, &daemon->sessions, list)
309 session->state = KILL;
311 set = perf_config_set__load_file(daemon->config_real);
313 err = perf_config_set(set, server_config, daemon);
314 perf_config_set__delete(set);
317 return err ?: check_base(daemon);
320 static int daemon_session__run(struct daemon_session *session,
321 struct daemon *daemon)
327 if (asprintf(&session->base, "%s/session-%s",
328 daemon->base, session->name) < 0) {
329 perror("failed: asprintf");
333 if (mkdir(session->base, 0755) && errno != EEXIST) {
334 perror("failed: mkdir");
338 session->start = time(NULL);
340 session->pid = fork();
341 if (session->pid < 0)
343 if (session->pid > 0) {
344 pr_info("reconfig: ruining session [%s:%d]: %s\n",
345 session->name, session->pid, session->run);
349 if (chdir(session->base)) {
350 perror("failed: chdir");
354 fd = open("/dev/null", O_RDONLY);
356 perror("failed: open /dev/null");
363 fd = open(SESSION_OUTPUT, O_RDWR|O_CREAT|O_TRUNC, 0644);
365 perror("failed: open session output");
373 if (mkfifo(SESSION_CONTROL, 0600) && errno != EEXIST) {
374 perror("failed: create control fifo");
378 if (mkfifo(SESSION_ACK, 0600) && errno != EEXIST) {
379 perror("failed: create ack fifo");
383 scnprintf(buf, sizeof(buf), "%s record --control=fifo:%s,%s %s",
384 daemon->perf, SESSION_CONTROL, SESSION_ACK, session->run);
386 argv = argv_split(buf, &argc);
390 exit(execve(daemon->perf, argv, NULL));
394 static pid_t handle_signalfd(struct daemon *daemon)
396 struct daemon_session *session;
397 struct signalfd_siginfo si;
403 * Take signal fd data as pure signal notification and check all
404 * the sessions state. The reason is that multiple signals can get
405 * coalesced in kernel and we can receive only single signal even
406 * if multiple SIGCHLD were generated.
408 err = read(daemon->signal_fd, &si, sizeof(struct signalfd_siginfo));
409 if (err != sizeof(struct signalfd_siginfo)) {
410 pr_err("failed to read signal fd\n");
414 list_for_each_entry(session, &daemon->sessions, list) {
415 if (session->pid == -1)
418 pid = waitpid(session->pid, &status, WNOHANG);
422 if (WIFEXITED(status)) {
423 pr_info("session '%s' exited, status=%d\n",
424 session->name, WEXITSTATUS(status));
425 } else if (WIFSIGNALED(status)) {
426 pr_info("session '%s' killed (signal %d)\n",
427 session->name, WTERMSIG(status));
428 } else if (WIFSTOPPED(status)) {
429 pr_info("session '%s' stopped (signal %d)\n",
430 session->name, WSTOPSIG(status));
432 pr_info("session '%s' Unexpected status (0x%x)\n",
433 session->name, status);
436 session->state = KILL;
443 static int daemon_session__wait(struct daemon_session *session, struct daemon *daemon,
446 struct pollfd pollfd = {
447 .fd = daemon->signal_fd,
455 int err = poll(&pollfd, 1, 1000);
458 handle_signalfd(daemon);
459 } else if (err < 0) {
460 perror("failed: poll\n");
464 if (start + secs < time(NULL))
466 } while (session->pid != -1);
471 static bool daemon__has_alive_session(struct daemon *daemon)
473 struct daemon_session *session;
475 list_for_each_entry(session, &daemon->sessions, list) {
476 if (session->pid != -1)
483 static int daemon__wait(struct daemon *daemon, int secs)
485 struct pollfd pollfd = {
486 .fd = daemon->signal_fd,
494 int err = poll(&pollfd, 1, 1000);
497 handle_signalfd(daemon);
498 } else if (err < 0) {
499 perror("failed: poll\n");
503 if (start + secs < time(NULL))
505 } while (daemon__has_alive_session(daemon));
510 static int daemon_session__control(struct daemon_session *session,
511 const char *msg, bool do_ack)
513 struct pollfd pollfd = { .events = POLLIN, };
514 char control_path[PATH_MAX];
515 char ack_path[PATH_MAX];
516 int control, ack = -1, len;
521 /* open the control file */
522 scnprintf(control_path, sizeof(control_path), "%s/%s",
523 session->base, SESSION_CONTROL);
525 control = open(control_path, O_WRONLY|O_NONBLOCK);
530 /* open the ack file */
531 scnprintf(ack_path, sizeof(ack_path), "%s/%s",
532 session->base, SESSION_ACK);
534 ack = open(ack_path, O_RDONLY, O_NONBLOCK);
541 /* write the command */
544 err = writen(control, msg, len);
546 pr_err("failed: write to control pipe: %d (%s)\n",
547 errno, control_path);
554 /* wait for an ack */
557 if (!poll(&pollfd, 1, 2000)) {
558 pr_err("failed: control ack timeout\n");
562 if (!(pollfd.revents & POLLIN)) {
563 pr_err("failed: did not received an ack\n");
567 err = read(ack, buf, sizeof(buf));
569 ret = strcmp(buf, "ack\n");
571 perror("failed: read ack %d\n");
581 static int setup_server_socket(struct daemon *daemon)
583 struct sockaddr_un addr;
585 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
588 fprintf(stderr, "socket: %s\n", strerror(errno));
592 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
593 perror("failed: fcntl FD_CLOEXEC");
598 scnprintf(path, sizeof(path), "%s/control", daemon->base);
600 if (strlen(path) + 1 >= sizeof(addr.sun_path)) {
601 pr_err("failed: control path too long '%s'\n", path);
606 memset(&addr, 0, sizeof(addr));
607 addr.sun_family = AF_UNIX;
609 strlcpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
612 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
613 perror("failed: bind");
618 if (listen(fd, 1) == -1) {
619 perror("failed: listen");
635 #define SESSION_MAX 64
651 char name[SESSION_MAX];
657 char name[SESSION_MAX];
667 static int daemon_session__ping(struct daemon_session *session)
669 return daemon_session__control(session, "ping", true) ? PING_FAIL : PING_OK;
672 static int cmd_session_list(struct daemon *daemon, union cmd *cmd, FILE *out)
674 char csv_sep = cmd->list.csv_sep;
675 struct daemon_session *session;
676 time_t curr = time(NULL);
679 fprintf(out, "%d%c%s%c%s%c%s/%s",
681 getpid(), csv_sep, "daemon",
683 csv_sep, daemon->base,
685 csv_sep, daemon->base, SESSION_OUTPUT);
687 fprintf(out, "%c%s/%s",
689 csv_sep, daemon->base, "lock");
691 fprintf(out, "%c%lu",
692 /* session up time */
693 csv_sep, (curr - daemon->start) / 60);
697 fprintf(out, "[%d:daemon] base: %s\n", getpid(), daemon->base);
698 if (cmd->list.verbose) {
699 fprintf(out, " output: %s/%s\n",
700 daemon->base, SESSION_OUTPUT);
701 fprintf(out, " lock: %s/lock\n",
703 fprintf(out, " up: %lu minutes\n",
704 (curr - daemon->start) / 60);
708 list_for_each_entry(session, &daemon->sessions, list) {
710 fprintf(out, "%d%c%s%c%s",
714 csv_sep, session->name,
716 csv_sep, session->run);
718 fprintf(out, "%c%s%c%s/%s",
720 csv_sep, session->base,
722 csv_sep, session->base, SESSION_OUTPUT);
724 fprintf(out, "%c%s/%s%c%s/%s",
725 /* session control */
726 csv_sep, session->base, SESSION_CONTROL,
728 csv_sep, session->base, SESSION_ACK);
730 fprintf(out, "%c%lu",
731 /* session up time */
732 csv_sep, (curr - session->start) / 60);
736 fprintf(out, "[%d:%s] perf record %s\n",
737 session->pid, session->name, session->run);
738 if (!cmd->list.verbose)
740 fprintf(out, " base: %s\n",
742 fprintf(out, " output: %s/%s\n",
743 session->base, SESSION_OUTPUT);
744 fprintf(out, " control: %s/%s\n",
745 session->base, SESSION_CONTROL);
746 fprintf(out, " ack: %s/%s\n",
747 session->base, SESSION_ACK);
748 fprintf(out, " up: %lu minutes\n",
749 (curr - session->start) / 60);
756 static int daemon_session__signal(struct daemon_session *session, int sig)
758 if (session->pid < 0)
760 return kill(session->pid, sig);
763 static int cmd_session_kill(struct daemon *daemon, union cmd *cmd, FILE *out)
765 struct daemon_session *session;
768 all = !strcmp(cmd->signal.name, "all");
770 list_for_each_entry(session, &daemon->sessions, list) {
771 if (all || !strcmp(cmd->signal.name, session->name)) {
772 daemon_session__signal(session, cmd->signal.sig);
773 fprintf(out, "signal %d sent to session '%s [%d]'\n",
774 cmd->signal.sig, session->name, session->pid);
781 static const char *ping_str[PING_MAX] = {
783 [PING_FAIL] = "FAIL",
786 static int cmd_session_ping(struct daemon *daemon, union cmd *cmd, FILE *out)
788 struct daemon_session *session;
789 bool all = false, found = false;
791 all = !strcmp(cmd->ping.name, "all");
793 list_for_each_entry(session, &daemon->sessions, list) {
794 if (all || !strcmp(cmd->ping.name, session->name)) {
795 int state = daemon_session__ping(session);
797 fprintf(out, "%-4s %s\n", ping_str[state], session->name);
802 if (!found && !all) {
803 fprintf(out, "%-4s %s (not found)\n",
804 ping_str[PING_FAIL], cmd->ping.name);
809 static int handle_server_socket(struct daemon *daemon, int sock_fd)
815 fd = accept(sock_fd, NULL, NULL);
817 perror("failed: accept");
821 if (sizeof(cmd) != readn(fd, &cmd, sizeof(cmd))) {
822 perror("failed: read");
826 out = fdopen(fd, "w");
828 perror("failed: fdopen");
834 ret = cmd_session_list(daemon, &cmd, out);
837 ret = cmd_session_kill(daemon, &cmd, out);
842 pr_debug("perf daemon is exciting\n");
845 ret = cmd_session_ping(daemon, &cmd, out);
853 /* If out is defined, then fd is closed via fclose. */
859 static int setup_client_socket(struct daemon *daemon)
861 struct sockaddr_un addr;
863 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
866 perror("failed: socket");
870 scnprintf(path, sizeof(path), "%s/control", daemon->base);
872 if (strlen(path) + 1 >= sizeof(addr.sun_path)) {
873 pr_err("failed: control path too long '%s'\n", path);
878 memset(&addr, 0, sizeof(addr));
879 addr.sun_family = AF_UNIX;
880 strlcpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
882 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
883 perror("failed: connect");
891 static void daemon_session__kill(struct daemon_session *session,
892 struct daemon *daemon)
899 daemon_session__control(session, "stop", false);
902 daemon_session__signal(session, SIGTERM);
905 daemon_session__signal(session, SIGKILL);
908 pr_err("failed to wait for session %s\n",
914 } while (daemon_session__wait(session, daemon, 10));
917 static void daemon__signal(struct daemon *daemon, int sig)
919 struct daemon_session *session;
921 list_for_each_entry(session, &daemon->sessions, list)
922 daemon_session__signal(session, sig);
925 static void daemon_session__delete(struct daemon_session *session)
933 static void daemon_session__remove(struct daemon_session *session)
935 list_del(&session->list);
936 daemon_session__delete(session);
939 static void daemon__stop(struct daemon *daemon)
941 struct daemon_session *session;
943 list_for_each_entry(session, &daemon->sessions, list)
944 daemon_session__control(session, "stop", false);
947 static void daemon__kill(struct daemon *daemon)
954 daemon__stop(daemon);
957 daemon__signal(daemon, SIGTERM);
960 daemon__signal(daemon, SIGKILL);
963 pr_err("failed to wait for sessions\n");
968 } while (daemon__wait(daemon, 10));
971 static void daemon__exit(struct daemon *daemon)
973 struct daemon_session *session, *h;
975 list_for_each_entry_safe(session, h, &daemon->sessions, list)
976 daemon_session__remove(session);
978 free(daemon->config_real);
979 free(daemon->config_base);
983 static int daemon__reconfig(struct daemon *daemon)
985 struct daemon_session *session, *n;
987 list_for_each_entry_safe(session, n, &daemon->sessions, list) {
989 if (session->state == OK)
992 /* Remove session. */
993 if (session->state == KILL) {
994 if (session->pid > 0) {
995 daemon_session__kill(session, daemon);
996 pr_info("reconfig: session '%s' killed\n", session->name);
998 daemon_session__remove(session);
1002 /* Reconfig session. */
1003 if (session->pid > 0) {
1004 daemon_session__kill(session, daemon);
1005 pr_info("reconfig: session '%s' killed\n", session->name);
1007 if (daemon_session__run(session, daemon))
1010 session->state = OK;
1016 static int setup_config_changes(struct daemon *daemon)
1018 char *basen = strdup(daemon->config_real);
1019 char *dirn = strdup(daemon->config_real);
1023 if (!dirn || !basen)
1026 fd = inotify_init1(IN_NONBLOCK|O_CLOEXEC);
1028 perror("failed: inotify_init");
1032 dir = dirname(dirn);
1033 base = basename(basen);
1034 pr_debug("config file: %s, dir: %s\n", base, dir);
1036 wd = inotify_add_watch(fd, dir, IN_CLOSE_WRITE);
1038 daemon->config_base = strdup(base);
1039 if (!daemon->config_base) {
1044 perror("failed: inotify_add_watch");
1050 return wd < 0 ? -1 : fd;
1053 static bool process_inotify_event(struct daemon *daemon, char *buf, ssize_t len)
1057 while (p < (buf + len)) {
1058 struct inotify_event *event = (struct inotify_event *) p;
1061 * We monitor config directory, check if our
1062 * config file was changes.
1064 if ((event->mask & IN_CLOSE_WRITE) &&
1065 !(event->mask & IN_ISDIR)) {
1066 if (!strcmp(event->name, daemon->config_base))
1069 p += sizeof(*event) + event->len;
1074 static int handle_config_changes(struct daemon *daemon, int conf_fd,
1075 bool *config_changed)
1080 while (!(*config_changed)) {
1081 len = read(conf_fd, buf, sizeof(buf));
1083 if (errno != EAGAIN) {
1084 perror("failed: read");
1089 *config_changed = process_inotify_event(daemon, buf, len);
1094 static int setup_config(struct daemon *daemon)
1096 if (daemon->base_user) {
1097 daemon->base = strdup(daemon->base_user);
1102 if (daemon->config) {
1103 char *real = realpath(daemon->config, NULL);
1106 perror("failed: realpath");
1109 daemon->config_real = real;
1113 if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK))
1114 daemon->config_real = strdup(perf_etc_perfconfig());
1115 else if (perf_config_global() && perf_home_perfconfig())
1116 daemon->config_real = strdup(perf_home_perfconfig());
1118 return daemon->config_real ? 0 : -1;
1124 #include <sys/file.h>
1126 static int lockf(int fd, int cmd, off_t len)
1128 if (cmd != F_TLOCK || len != 0)
1131 return flock(fd, LOCK_EX | LOCK_NB);
1136 * Each daemon tries to create and lock BASE/lock file,
1137 * if it's successful we are sure we're the only daemon
1138 * running over the BASE.
1140 * Once daemon is finished, file descriptor to lock file
1141 * is closed and lock is released.
1143 static int check_lock(struct daemon *daemon)
1145 char path[PATH_MAX];
1150 scnprintf(path, sizeof(path), "%s/lock", daemon->base);
1152 fd = open(path, O_RDWR|O_CREAT|O_CLOEXEC, 0640);
1156 if (lockf(fd, F_TLOCK, 0) < 0) {
1157 filename__read_int(path, &pid);
1158 fprintf(stderr, "failed: another perf daemon (pid %d) owns %s\n",
1164 scnprintf(buf, sizeof(buf), "%d", getpid());
1167 if (write(fd, buf, len) != len) {
1168 perror("failed: write");
1173 if (ftruncate(fd, len)) {
1174 perror("failed: ftruncate");
1182 static int go_background(struct daemon *daemon)
1196 if (check_lock(daemon))
1201 if (chdir(daemon->base)) {
1202 perror("failed: chdir");
1206 fd = open("output", O_RDWR|O_CREAT|O_TRUNC, 0644);
1208 perror("failed: open");
1212 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
1213 perror("failed: fcntl FD_CLOEXEC");
1223 daemon->out = fdopen(1, "w");
1230 setbuf(daemon->out, NULL);
1234 static int setup_signalfd(struct daemon *daemon)
1239 sigaddset(&mask, SIGCHLD);
1241 if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
1244 daemon->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
1245 return daemon->signal_fd;
1248 static int __cmd_start(struct daemon *daemon, struct option parent_options[],
1249 int argc, const char **argv)
1251 bool foreground = false;
1252 struct option start_options[] = {
1253 OPT_BOOLEAN('f', "foreground", &foreground, "stay on console"),
1254 OPT_PARENT(parent_options),
1257 int sock_fd = -1, conf_fd = -1, signal_fd = -1;
1258 int sock_pos, file_pos, signal_pos;
1262 argc = parse_options(argc, argv, start_options, daemon_usage, 0);
1264 usage_with_options(daemon_usage, start_options);
1266 daemon->start = time(NULL);
1268 if (setup_config(daemon)) {
1269 pr_err("failed: config not found\n");
1273 if (setup_server_config(daemon))
1276 if (foreground && check_lock(daemon))
1280 err = go_background(daemon);
1282 /* original process, exit normally */
1285 daemon__exit(daemon);
1290 debug_set_file(daemon->out);
1291 debug_set_display_time(true);
1293 pr_info("daemon started (pid %d)\n", getpid());
1295 fdarray__init(&fda, 3);
1297 sock_fd = setup_server_socket(daemon);
1301 conf_fd = setup_config_changes(daemon);
1305 signal_fd = setup_signalfd(daemon);
1309 sock_pos = fdarray__add(&fda, sock_fd, POLLIN|POLLERR|POLLHUP, 0);
1313 file_pos = fdarray__add(&fda, conf_fd, POLLIN|POLLERR|POLLHUP, 0);
1317 signal_pos = fdarray__add(&fda, signal_fd, POLLIN|POLLERR|POLLHUP, 0);
1321 signal(SIGINT, sig_handler);
1322 signal(SIGTERM, sig_handler);
1323 signal(SIGPIPE, SIG_IGN);
1325 while (!done && !err) {
1326 err = daemon__reconfig(daemon);
1328 if (!err && fdarray__poll(&fda, -1)) {
1329 bool reconfig = false;
1331 if (fda.entries[sock_pos].revents & POLLIN)
1332 err = handle_server_socket(daemon, sock_fd);
1333 if (fda.entries[file_pos].revents & POLLIN)
1334 err = handle_config_changes(daemon, conf_fd, &reconfig);
1335 if (fda.entries[signal_pos].revents & POLLIN)
1336 err = handle_signalfd(daemon) < 0;
1339 err = setup_server_config(daemon);
1344 fdarray__exit(&fda);
1346 daemon__kill(daemon);
1347 daemon__exit(daemon);
1353 if (signal_fd != -1)
1356 pr_info("daemon exited\n");
1357 fclose(daemon->out);
1361 static int send_cmd(struct daemon *daemon, union cmd *cmd)
1369 if (setup_client_config(daemon))
1372 fd = setup_client_socket(daemon);
1376 if (sizeof(*cmd) != writen(fd, cmd, sizeof(*cmd))) {
1377 perror("failed: write");
1381 in = fdopen(fd, "r");
1383 perror("failed: fdopen");
1387 while ((nread = getline(&line, &len, in)) != -1) {
1388 if (fwrite(line, nread, 1, stdout) != 1)
1398 /* If in is defined, then fd is closed via fclose. */
1404 static int send_cmd_list(struct daemon *daemon)
1406 union cmd cmd = { .cmd = CMD_LIST, };
1408 cmd.list.verbose = verbose;
1409 cmd.list.csv_sep = daemon->csv_sep ? *daemon->csv_sep : 0;
1411 return send_cmd(daemon, &cmd);
1414 static int __cmd_signal(struct daemon *daemon, struct option parent_options[],
1415 int argc, const char **argv)
1417 const char *name = "all";
1418 struct option start_options[] = {
1419 OPT_STRING(0, "session", &name, "session",
1420 "Sent signal to specific session"),
1421 OPT_PARENT(parent_options),
1426 argc = parse_options(argc, argv, start_options, daemon_usage, 0);
1428 usage_with_options(daemon_usage, start_options);
1430 if (setup_config(daemon)) {
1431 pr_err("failed: config not found\n");
1435 cmd.signal.cmd = CMD_SIGNAL,
1436 cmd.signal.sig = SIGUSR2;
1437 strncpy(cmd.signal.name, name, sizeof(cmd.signal.name) - 1);
1439 return send_cmd(daemon, &cmd);
1442 static int __cmd_stop(struct daemon *daemon, struct option parent_options[],
1443 int argc, const char **argv)
1445 struct option start_options[] = {
1446 OPT_PARENT(parent_options),
1449 union cmd cmd = { .cmd = CMD_STOP, };
1451 argc = parse_options(argc, argv, start_options, daemon_usage, 0);
1453 usage_with_options(daemon_usage, start_options);
1455 if (setup_config(daemon)) {
1456 pr_err("failed: config not found\n");
1460 return send_cmd(daemon, &cmd);
1463 static int __cmd_ping(struct daemon *daemon, struct option parent_options[],
1464 int argc, const char **argv)
1466 const char *name = "all";
1467 struct option ping_options[] = {
1468 OPT_STRING(0, "session", &name, "session",
1469 "Ping to specific session"),
1470 OPT_PARENT(parent_options),
1473 union cmd cmd = { .cmd = CMD_PING, };
1475 argc = parse_options(argc, argv, ping_options, daemon_usage, 0);
1477 usage_with_options(daemon_usage, ping_options);
1479 if (setup_config(daemon)) {
1480 pr_err("failed: config not found\n");
1484 scnprintf(cmd.ping.name, sizeof(cmd.ping.name), "%s", name);
1485 return send_cmd(daemon, &cmd);
1488 int cmd_daemon(int argc, const char **argv)
1490 struct option daemon_options[] = {
1491 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
1492 OPT_STRING(0, "config", &__daemon.config,
1493 "config file", "config file path"),
1494 OPT_STRING(0, "base", &__daemon.base_user,
1495 "directory", "base directory"),
1496 OPT_STRING_OPTARG('x', "field-separator", &__daemon.csv_sep,
1497 "field separator", "print counts with custom separator", ","),
1501 perf_exe(__daemon.perf, sizeof(__daemon.perf));
1502 __daemon.out = stdout;
1504 argc = parse_options(argc, argv, daemon_options, daemon_usage,
1505 PARSE_OPT_STOP_AT_NON_OPTION);
1508 if (!strcmp(argv[0], "start"))
1509 return __cmd_start(&__daemon, daemon_options, argc, argv);
1510 if (!strcmp(argv[0], "signal"))
1511 return __cmd_signal(&__daemon, daemon_options, argc, argv);
1512 else if (!strcmp(argv[0], "stop"))
1513 return __cmd_stop(&__daemon, daemon_options, argc, argv);
1514 else if (!strcmp(argv[0], "ping"))
1515 return __cmd_ping(&__daemon, daemon_options, argc, argv);
1517 pr_err("failed: unknown command '%s'\n", argv[0]);
1521 if (setup_config(&__daemon)) {
1522 pr_err("failed: config not found\n");
1526 return send_cmd_list(&__daemon);