1 // SPDX-License-Identifier: GPL-2.0
2 #include <internal/lib.h>
4 #include <subcmd/parse-options.h>
5 #include <api/fd/array.h>
7 #include <linux/zalloc.h>
8 #include <linux/string.h>
9 #include <linux/limits.h>
18 #include <sys/inotify.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
24 #include <sys/signalfd.h>
33 #define SESSION_OUTPUT "output"
34 #define SESSION_CONTROL "control"
35 #define SESSION_ACK "ack"
40 * OK - session is up and running
41 * RECONFIG - session is pending for reconfiguration,
42 * new values are already loaded in session object
43 * KILL - session is pending to be killed
45 * Session object life and its state is maintained by
46 * following functions:
49 * - reads config file and setup session objects
50 * with following states:
52 * OK - no change needed
53 * RECONFIG - session needs to be changed
54 * (run variable changed)
55 * KILL - session needs to be killed
56 * (session is no longer in config file)
59 * - scans session objects and does following actions
63 * RECONFIG - session is killed and re-run with new config
64 * KILL - session is killed
66 * - all sessions have OK state on the function exit
68 enum daemon_session_state
{
74 struct daemon_session
{
80 struct list_head list
;
81 enum daemon_session_state state
;
90 const char *base_user
;
92 struct list_head sessions
;
99 static struct daemon __daemon
= {
100 .sessions
= LIST_HEAD_INIT(__daemon
.sessions
),
103 static const char * const daemon_usage
[] = {
104 "perf daemon {start|signal|stop|ping} [<options>]",
105 "perf daemon [<options>]",
109 static volatile sig_atomic_t done
;
111 static void sig_handler(int sig __maybe_unused
)
116 static struct daemon_session
*daemon__add_session(struct daemon
*config
, char *name
)
118 struct daemon_session
*session
= zalloc(sizeof(*session
));
123 session
->name
= strdup(name
);
124 if (!session
->name
) {
130 list_add_tail(&session
->list
, &config
->sessions
);
134 static struct daemon_session
*daemon__find_session(struct daemon
*daemon
, char *name
)
136 struct daemon_session
*session
;
138 list_for_each_entry(session
, &daemon
->sessions
, list
) {
139 if (!strcmp(session
->name
, name
))
146 static int get_session_name(const char *var
, char *session
, int len
)
148 const char *p
= var
+ sizeof("session-") - 1;
150 while (*p
!= '.' && *p
!= 0x0 && len
--)
154 return *p
== '.' ? 0 : -EINVAL
;
157 static int session_config(struct daemon
*daemon
, const char *var
, const char *value
)
159 struct daemon_session
*session
;
162 if (get_session_name(var
, name
, sizeof(name
) - 1))
165 var
= strchr(var
, '.');
171 session
= daemon__find_session(daemon
, name
);
174 /* New session is defined. */
175 session
= daemon__add_session(daemon
, name
);
179 pr_debug("reconfig: found new session %s\n", name
);
181 /* Trigger reconfig to start it. */
182 session
->state
= RECONFIG
;
183 } else if (session
->state
== KILL
) {
184 /* Current session is defined, no action needed. */
185 pr_debug("reconfig: found current session %s\n", name
);
189 if (!strcmp(var
, "run")) {
193 same
= !strcmp(session
->run
, value
);
197 zfree(&session
->run
);
198 pr_debug("reconfig: session %s is changed\n", name
);
201 session
->run
= strdup(value
);
206 * Either new or changed run value is defined,
207 * trigger reconfig for the session.
209 session
->state
= RECONFIG
;
216 static int server_config(const char *var
, const char *value
, void *cb
)
218 struct daemon
*daemon
= cb
;
220 if (strstarts(var
, "session-")) {
221 return session_config(daemon
, var
, value
);
222 } else if (!strcmp(var
, "daemon.base") && !daemon
->base_user
) {
223 if (daemon
->base
&& strcmp(daemon
->base
, value
)) {
224 pr_err("failed: can't redefine base, bailing out\n");
227 daemon
->base
= strdup(value
);
235 static int client_config(const char *var
, const char *value
, void *cb
)
237 struct daemon
*daemon
= cb
;
239 if (!strcmp(var
, "daemon.base") && !daemon
->base_user
) {
240 daemon
->base
= strdup(value
);
248 static int check_base(struct daemon
*daemon
)
253 pr_err("failed: base not defined\n");
257 if (stat(daemon
->base
, &st
)) {
260 pr_err("failed: permission denied for '%s' base\n",
264 pr_err("failed: base '%s' does not exists\n",
268 pr_err("failed: can't access base '%s': %s\n",
269 daemon
->base
, strerror(errno
));
274 if ((st
.st_mode
& S_IFMT
) != S_IFDIR
) {
275 pr_err("failed: base '%s' is not directory\n",
283 static int setup_client_config(struct daemon
*daemon
)
285 struct perf_config_set
*set
= perf_config_set__load_file(daemon
->config_real
);
289 err
= perf_config_set(set
, client_config
, daemon
);
290 perf_config_set__delete(set
);
293 return err
?: check_base(daemon
);
296 static int setup_server_config(struct daemon
*daemon
)
298 struct perf_config_set
*set
;
299 struct daemon_session
*session
;
302 pr_debug("reconfig: started\n");
305 * Mark all sessions for kill, the server config
306 * will set following states, see explanation at
307 * enum daemon_session_state declaration.
309 list_for_each_entry(session
, &daemon
->sessions
, list
)
310 session
->state
= KILL
;
312 set
= perf_config_set__load_file(daemon
->config_real
);
314 err
= perf_config_set(set
, server_config
, daemon
);
315 perf_config_set__delete(set
);
318 return err
?: check_base(daemon
);
321 static int daemon_session__run(struct daemon_session
*session
,
322 struct daemon
*daemon
)
328 if (asprintf(&session
->base
, "%s/session-%s",
329 daemon
->base
, session
->name
) < 0) {
330 perror("failed: asprintf");
334 if (mkdir(session
->base
, 0755) && errno
!= EEXIST
) {
335 perror("failed: mkdir");
339 session
->start
= time(NULL
);
341 session
->pid
= fork();
342 if (session
->pid
< 0)
344 if (session
->pid
> 0) {
345 pr_info("reconfig: ruining session [%s:%d]: %s\n",
346 session
->name
, session
->pid
, session
->run
);
350 if (chdir(session
->base
)) {
351 perror("failed: chdir");
355 fd
= open("/dev/null", O_RDONLY
);
357 perror("failed: open /dev/null");
364 fd
= open(SESSION_OUTPUT
, O_RDWR
|O_CREAT
|O_TRUNC
, 0644);
366 perror("failed: open session output");
374 if (mkfifo(SESSION_CONTROL
, 0600) && errno
!= EEXIST
) {
375 perror("failed: create control fifo");
379 if (mkfifo(SESSION_ACK
, 0600) && errno
!= EEXIST
) {
380 perror("failed: create ack fifo");
384 scnprintf(buf
, sizeof(buf
), "%s record --control=fifo:%s,%s %s",
385 daemon
->perf
, SESSION_CONTROL
, SESSION_ACK
, session
->run
);
387 argv
= argv_split(buf
, &argc
);
391 exit(execve(daemon
->perf
, argv
, NULL
));
395 static pid_t
handle_signalfd(struct daemon
*daemon
)
397 struct daemon_session
*session
;
398 struct signalfd_siginfo si
;
404 * Take signal fd data as pure signal notification and check all
405 * the sessions state. The reason is that multiple signals can get
406 * coalesced in kernel and we can receive only single signal even
407 * if multiple SIGCHLD were generated.
409 err
= read(daemon
->signal_fd
, &si
, sizeof(struct signalfd_siginfo
));
410 if (err
!= sizeof(struct signalfd_siginfo
)) {
411 pr_err("failed to read signal fd\n");
415 list_for_each_entry(session
, &daemon
->sessions
, list
) {
416 if (session
->pid
== -1)
419 pid
= waitpid(session
->pid
, &status
, WNOHANG
);
423 if (WIFEXITED(status
)) {
424 pr_info("session '%s' exited, status=%d\n",
425 session
->name
, WEXITSTATUS(status
));
426 } else if (WIFSIGNALED(status
)) {
427 pr_info("session '%s' killed (signal %d)\n",
428 session
->name
, WTERMSIG(status
));
429 } else if (WIFSTOPPED(status
)) {
430 pr_info("session '%s' stopped (signal %d)\n",
431 session
->name
, WSTOPSIG(status
));
433 pr_info("session '%s' Unexpected status (0x%x)\n",
434 session
->name
, status
);
437 session
->state
= KILL
;
444 static int daemon_session__wait(struct daemon_session
*session
, struct daemon
*daemon
,
447 struct pollfd pollfd
= {
448 .fd
= daemon
->signal_fd
,
456 int err
= poll(&pollfd
, 1, 1000);
459 handle_signalfd(daemon
);
460 } else if (err
< 0) {
461 perror("failed: poll\n");
465 if (start
+ secs
< time(NULL
))
467 } while (session
->pid
!= -1);
472 static bool daemon__has_alive_session(struct daemon
*daemon
)
474 struct daemon_session
*session
;
476 list_for_each_entry(session
, &daemon
->sessions
, list
) {
477 if (session
->pid
!= -1)
484 static int daemon__wait(struct daemon
*daemon
, int secs
)
486 struct pollfd pollfd
= {
487 .fd
= daemon
->signal_fd
,
495 int err
= poll(&pollfd
, 1, 1000);
498 handle_signalfd(daemon
);
499 } else if (err
< 0) {
500 perror("failed: poll\n");
504 if (start
+ secs
< time(NULL
))
506 } while (daemon__has_alive_session(daemon
));
511 static int daemon_session__control(struct daemon_session
*session
,
512 const char *msg
, bool do_ack
)
514 struct pollfd pollfd
= { .events
= POLLIN
, };
515 char control_path
[PATH_MAX
];
516 char ack_path
[PATH_MAX
];
517 int control
, ack
= -1, len
;
522 /* open the control file */
523 scnprintf(control_path
, sizeof(control_path
), "%s/%s",
524 session
->base
, SESSION_CONTROL
);
526 control
= open(control_path
, O_WRONLY
|O_NONBLOCK
);
531 /* open the ack file */
532 scnprintf(ack_path
, sizeof(ack_path
), "%s/%s",
533 session
->base
, SESSION_ACK
);
535 ack
= open(ack_path
, O_RDONLY
, O_NONBLOCK
);
542 /* write the command */
545 err
= writen(control
, msg
, len
);
547 pr_err("failed: write to control pipe: %d (%s)\n",
548 errno
, control_path
);
555 /* wait for an ack */
558 if (!poll(&pollfd
, 1, 2000)) {
559 pr_err("failed: control ack timeout\n");
563 if (!(pollfd
.revents
& POLLIN
)) {
564 pr_err("failed: did not received an ack\n");
568 err
= read(ack
, buf
, sizeof(buf
));
570 ret
= strcmp(buf
, "ack\n");
572 perror("failed: read ack %d\n");
582 static int setup_server_socket(struct daemon
*daemon
)
584 struct sockaddr_un addr
;
586 int fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
589 fprintf(stderr
, "socket: %s\n", strerror(errno
));
593 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
594 perror("failed: fcntl FD_CLOEXEC");
599 scnprintf(path
, sizeof(path
), "%s/control", daemon
->base
);
601 if (strlen(path
) + 1 >= sizeof(addr
.sun_path
)) {
602 pr_err("failed: control path too long '%s'\n", path
);
607 memset(&addr
, 0, sizeof(addr
));
608 addr
.sun_family
= AF_UNIX
;
610 strlcpy(addr
.sun_path
, path
, sizeof(addr
.sun_path
) - 1);
613 if (bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1) {
614 perror("failed: bind");
619 if (listen(fd
, 1) == -1) {
620 perror("failed: listen");
636 #define SESSION_MAX 64
652 char name
[SESSION_MAX
];
658 char name
[SESSION_MAX
];
668 static int daemon_session__ping(struct daemon_session
*session
)
670 return daemon_session__control(session
, "ping", true) ? PING_FAIL
: PING_OK
;
673 static int cmd_session_list(struct daemon
*daemon
, union cmd
*cmd
, FILE *out
)
675 char csv_sep
= cmd
->list
.csv_sep
;
676 struct daemon_session
*session
;
677 time_t curr
= time(NULL
);
680 fprintf(out
, "%d%c%s%c%s%c%s/%s",
682 getpid(), csv_sep
, "daemon",
684 csv_sep
, daemon
->base
,
686 csv_sep
, daemon
->base
, SESSION_OUTPUT
);
688 fprintf(out
, "%c%s/%s",
690 csv_sep
, daemon
->base
, "lock");
692 fprintf(out
, "%c%" PRIu64
,
693 /* session up time */
694 csv_sep
, (uint64_t)((curr
- daemon
->start
) / 60));
698 fprintf(out
, "[%d:daemon] base: %s\n", getpid(), daemon
->base
);
699 if (cmd
->list
.verbose
) {
700 fprintf(out
, " output: %s/%s\n",
701 daemon
->base
, SESSION_OUTPUT
);
702 fprintf(out
, " lock: %s/lock\n",
704 fprintf(out
, " up: %" PRIu64
" minutes\n",
705 (uint64_t)((curr
- daemon
->start
) / 60));
709 list_for_each_entry(session
, &daemon
->sessions
, list
) {
711 fprintf(out
, "%d%c%s%c%s",
715 csv_sep
, session
->name
,
717 csv_sep
, session
->run
);
719 fprintf(out
, "%c%s%c%s/%s",
721 csv_sep
, session
->base
,
723 csv_sep
, session
->base
, SESSION_OUTPUT
);
725 fprintf(out
, "%c%s/%s%c%s/%s",
726 /* session control */
727 csv_sep
, session
->base
, SESSION_CONTROL
,
729 csv_sep
, session
->base
, SESSION_ACK
);
731 fprintf(out
, "%c%" PRIu64
,
732 /* session up time */
733 csv_sep
, (uint64_t)((curr
- session
->start
) / 60));
737 fprintf(out
, "[%d:%s] perf record %s\n",
738 session
->pid
, session
->name
, session
->run
);
739 if (!cmd
->list
.verbose
)
741 fprintf(out
, " base: %s\n",
743 fprintf(out
, " output: %s/%s\n",
744 session
->base
, SESSION_OUTPUT
);
745 fprintf(out
, " control: %s/%s\n",
746 session
->base
, SESSION_CONTROL
);
747 fprintf(out
, " ack: %s/%s\n",
748 session
->base
, SESSION_ACK
);
749 fprintf(out
, " up: %" PRIu64
" minutes\n",
750 (uint64_t)((curr
- session
->start
) / 60));
757 static int daemon_session__signal(struct daemon_session
*session
, int sig
)
759 if (session
->pid
< 0)
761 return kill(session
->pid
, sig
);
764 static int cmd_session_kill(struct daemon
*daemon
, union cmd
*cmd
, FILE *out
)
766 struct daemon_session
*session
;
769 all
= !strcmp(cmd
->signal
.name
, "all");
771 list_for_each_entry(session
, &daemon
->sessions
, list
) {
772 if (all
|| !strcmp(cmd
->signal
.name
, session
->name
)) {
773 daemon_session__signal(session
, cmd
->signal
.sig
);
774 fprintf(out
, "signal %d sent to session '%s [%d]'\n",
775 cmd
->signal
.sig
, session
->name
, session
->pid
);
782 static const char *ping_str
[PING_MAX
] = {
784 [PING_FAIL
] = "FAIL",
787 static int cmd_session_ping(struct daemon
*daemon
, union cmd
*cmd
, FILE *out
)
789 struct daemon_session
*session
;
790 bool all
= false, found
= false;
792 all
= !strcmp(cmd
->ping
.name
, "all");
794 list_for_each_entry(session
, &daemon
->sessions
, list
) {
795 if (all
|| !strcmp(cmd
->ping
.name
, session
->name
)) {
796 int state
= daemon_session__ping(session
);
798 fprintf(out
, "%-4s %s\n", ping_str
[state
], session
->name
);
803 if (!found
&& !all
) {
804 fprintf(out
, "%-4s %s (not found)\n",
805 ping_str
[PING_FAIL
], cmd
->ping
.name
);
810 static int handle_server_socket(struct daemon
*daemon
, int sock_fd
)
816 fd
= accept(sock_fd
, NULL
, NULL
);
818 perror("failed: accept");
822 if (sizeof(cmd
) != readn(fd
, &cmd
, sizeof(cmd
))) {
823 perror("failed: read");
827 out
= fdopen(fd
, "w");
829 perror("failed: fdopen");
835 ret
= cmd_session_list(daemon
, &cmd
, out
);
838 ret
= cmd_session_kill(daemon
, &cmd
, out
);
843 pr_debug("perf daemon is exciting\n");
846 ret
= cmd_session_ping(daemon
, &cmd
, out
);
854 /* If out is defined, then fd is closed via fclose. */
860 static int setup_client_socket(struct daemon
*daemon
)
862 struct sockaddr_un addr
;
864 int fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
867 perror("failed: socket");
871 scnprintf(path
, sizeof(path
), "%s/control", daemon
->base
);
873 if (strlen(path
) + 1 >= sizeof(addr
.sun_path
)) {
874 pr_err("failed: control path too long '%s'\n", path
);
879 memset(&addr
, 0, sizeof(addr
));
880 addr
.sun_family
= AF_UNIX
;
881 strlcpy(addr
.sun_path
, path
, sizeof(addr
.sun_path
) - 1);
883 if (connect(fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
884 perror("failed: connect");
892 static void daemon_session__kill(struct daemon_session
*session
,
893 struct daemon
*daemon
)
900 daemon_session__control(session
, "stop", false);
903 daemon_session__signal(session
, SIGTERM
);
906 daemon_session__signal(session
, SIGKILL
);
909 pr_err("failed to wait for session %s\n",
915 } while (daemon_session__wait(session
, daemon
, 10));
918 static void daemon__signal(struct daemon
*daemon
, int sig
)
920 struct daemon_session
*session
;
922 list_for_each_entry(session
, &daemon
->sessions
, list
)
923 daemon_session__signal(session
, sig
);
926 static void daemon_session__delete(struct daemon_session
*session
)
928 zfree(&session
->base
);
929 zfree(&session
->name
);
930 zfree(&session
->run
);
934 static void daemon_session__remove(struct daemon_session
*session
)
936 list_del(&session
->list
);
937 daemon_session__delete(session
);
940 static void daemon__stop(struct daemon
*daemon
)
942 struct daemon_session
*session
;
944 list_for_each_entry(session
, &daemon
->sessions
, list
)
945 daemon_session__control(session
, "stop", false);
948 static void daemon__kill(struct daemon
*daemon
)
955 daemon__stop(daemon
);
958 daemon__signal(daemon
, SIGTERM
);
961 daemon__signal(daemon
, SIGKILL
);
964 pr_err("failed to wait for sessions\n");
969 } while (daemon__wait(daemon
, 10));
972 static void daemon__exit(struct daemon
*daemon
)
974 struct daemon_session
*session
, *h
;
976 list_for_each_entry_safe(session
, h
, &daemon
->sessions
, list
)
977 daemon_session__remove(session
);
979 zfree(&daemon
->config_real
);
980 zfree(&daemon
->config_base
);
981 zfree(&daemon
->base
);
984 static int daemon__reconfig(struct daemon
*daemon
)
986 struct daemon_session
*session
, *n
;
988 list_for_each_entry_safe(session
, n
, &daemon
->sessions
, list
) {
990 if (session
->state
== OK
)
993 /* Remove session. */
994 if (session
->state
== KILL
) {
995 if (session
->pid
> 0) {
996 daemon_session__kill(session
, daemon
);
997 pr_info("reconfig: session '%s' killed\n", session
->name
);
999 daemon_session__remove(session
);
1003 /* Reconfig session. */
1004 if (session
->pid
> 0) {
1005 daemon_session__kill(session
, daemon
);
1006 pr_info("reconfig: session '%s' killed\n", session
->name
);
1008 if (daemon_session__run(session
, daemon
))
1011 session
->state
= OK
;
1017 static int setup_config_changes(struct daemon
*daemon
)
1019 char *basen
= strdup(daemon
->config_real
);
1020 char *dirn
= strdup(daemon
->config_real
);
1024 if (!dirn
|| !basen
)
1027 fd
= inotify_init1(IN_NONBLOCK
|O_CLOEXEC
);
1029 perror("failed: inotify_init");
1033 dir
= dirname(dirn
);
1034 base
= basename(basen
);
1035 pr_debug("config file: %s, dir: %s\n", base
, dir
);
1037 wd
= inotify_add_watch(fd
, dir
, IN_CLOSE_WRITE
);
1039 daemon
->config_base
= strdup(base
);
1040 if (!daemon
->config_base
) {
1045 perror("failed: inotify_add_watch");
1051 return wd
< 0 ? -1 : fd
;
1054 static bool process_inotify_event(struct daemon
*daemon
, char *buf
, ssize_t len
)
1058 while (p
< (buf
+ len
)) {
1059 struct inotify_event
*event
= (struct inotify_event
*) p
;
1062 * We monitor config directory, check if our
1063 * config file was changes.
1065 if ((event
->mask
& IN_CLOSE_WRITE
) &&
1066 !(event
->mask
& IN_ISDIR
)) {
1067 if (!strcmp(event
->name
, daemon
->config_base
))
1070 p
+= sizeof(*event
) + event
->len
;
1075 static int handle_config_changes(struct daemon
*daemon
, int conf_fd
,
1076 bool *config_changed
)
1081 while (!(*config_changed
)) {
1082 len
= read(conf_fd
, buf
, sizeof(buf
));
1084 if (errno
!= EAGAIN
) {
1085 perror("failed: read");
1090 *config_changed
= process_inotify_event(daemon
, buf
, len
);
1095 static int setup_config(struct daemon
*daemon
)
1097 if (daemon
->base_user
) {
1098 daemon
->base
= strdup(daemon
->base_user
);
1103 if (daemon
->config
) {
1104 char *real
= realpath(daemon
->config
, NULL
);
1107 perror("failed: realpath");
1110 daemon
->config_real
= real
;
1114 if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK
))
1115 daemon
->config_real
= strdup(perf_etc_perfconfig());
1116 else if (perf_config_global() && perf_home_perfconfig())
1117 daemon
->config_real
= strdup(perf_home_perfconfig());
1119 return daemon
->config_real
? 0 : -1;
1125 static int lockf(int fd
, int cmd
, off_t len
)
1127 if (cmd
!= F_TLOCK
|| len
!= 0)
1130 return flock(fd
, LOCK_EX
| LOCK_NB
);
1135 * Each daemon tries to create and lock BASE/lock file,
1136 * if it's successful we are sure we're the only daemon
1137 * running over the BASE.
1139 * Once daemon is finished, file descriptor to lock file
1140 * is closed and lock is released.
1142 static int check_lock(struct daemon
*daemon
)
1144 char path
[PATH_MAX
];
1149 scnprintf(path
, sizeof(path
), "%s/lock", daemon
->base
);
1151 fd
= open(path
, O_RDWR
|O_CREAT
|O_CLOEXEC
, 0640);
1155 if (lockf(fd
, F_TLOCK
, 0) < 0) {
1156 filename__read_int(path
, &pid
);
1157 fprintf(stderr
, "failed: another perf daemon (pid %d) owns %s\n",
1163 scnprintf(buf
, sizeof(buf
), "%d", getpid());
1166 if (write(fd
, buf
, len
) != len
) {
1167 perror("failed: write");
1172 if (ftruncate(fd
, len
)) {
1173 perror("failed: ftruncate");
1181 static int go_background(struct daemon
*daemon
)
1195 if (check_lock(daemon
))
1200 if (chdir(daemon
->base
)) {
1201 perror("failed: chdir");
1205 fd
= open("output", O_RDWR
|O_CREAT
|O_TRUNC
, 0644);
1207 perror("failed: open");
1211 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
1212 perror("failed: fcntl FD_CLOEXEC");
1222 daemon
->out
= fdopen(1, "w");
1229 setbuf(daemon
->out
, NULL
);
1233 static int setup_signalfd(struct daemon
*daemon
)
1238 sigaddset(&mask
, SIGCHLD
);
1240 if (sigprocmask(SIG_BLOCK
, &mask
, NULL
) == -1)
1243 daemon
->signal_fd
= signalfd(-1, &mask
, SFD_NONBLOCK
|SFD_CLOEXEC
);
1244 return daemon
->signal_fd
;
1247 static int __cmd_start(struct daemon
*daemon
, struct option parent_options
[],
1248 int argc
, const char **argv
)
1250 bool foreground
= false;
1251 struct option start_options
[] = {
1252 OPT_BOOLEAN('f', "foreground", &foreground
, "stay on console"),
1253 OPT_PARENT(parent_options
),
1256 int sock_fd
= -1, conf_fd
= -1, signal_fd
= -1;
1257 int sock_pos
, file_pos
, signal_pos
;
1261 argc
= parse_options(argc
, argv
, start_options
, daemon_usage
, 0);
1263 usage_with_options(daemon_usage
, start_options
);
1265 daemon
->start
= time(NULL
);
1267 if (setup_config(daemon
)) {
1268 pr_err("failed: config not found\n");
1272 if (setup_server_config(daemon
))
1275 if (foreground
&& check_lock(daemon
))
1279 err
= go_background(daemon
);
1281 /* original process, exit normally */
1284 daemon__exit(daemon
);
1289 debug_set_file(daemon
->out
);
1290 debug_set_display_time(true);
1292 pr_info("daemon started (pid %d)\n", getpid());
1294 fdarray__init(&fda
, 3);
1296 sock_fd
= setup_server_socket(daemon
);
1300 conf_fd
= setup_config_changes(daemon
);
1304 signal_fd
= setup_signalfd(daemon
);
1308 sock_pos
= fdarray__add(&fda
, sock_fd
, POLLIN
|POLLERR
|POLLHUP
, 0);
1312 file_pos
= fdarray__add(&fda
, conf_fd
, POLLIN
|POLLERR
|POLLHUP
, 0);
1316 signal_pos
= fdarray__add(&fda
, signal_fd
, POLLIN
|POLLERR
|POLLHUP
, 0);
1320 signal(SIGINT
, sig_handler
);
1321 signal(SIGTERM
, sig_handler
);
1322 signal(SIGPIPE
, SIG_IGN
);
1324 while (!done
&& !err
) {
1325 err
= daemon__reconfig(daemon
);
1327 if (!err
&& fdarray__poll(&fda
, -1)) {
1328 bool reconfig
= false;
1330 if (fda
.entries
[sock_pos
].revents
& POLLIN
)
1331 err
= handle_server_socket(daemon
, sock_fd
);
1332 if (fda
.entries
[file_pos
].revents
& POLLIN
)
1333 err
= handle_config_changes(daemon
, conf_fd
, &reconfig
);
1334 if (fda
.entries
[signal_pos
].revents
& POLLIN
)
1335 err
= handle_signalfd(daemon
) < 0;
1338 err
= setup_server_config(daemon
);
1343 fdarray__exit(&fda
);
1345 daemon__kill(daemon
);
1346 daemon__exit(daemon
);
1352 if (signal_fd
!= -1)
1355 pr_info("daemon exited\n");
1356 fclose(daemon
->out
);
1360 static int send_cmd(struct daemon
*daemon
, union cmd
*cmd
)
1368 if (setup_client_config(daemon
))
1371 fd
= setup_client_socket(daemon
);
1375 if (sizeof(*cmd
) != writen(fd
, cmd
, sizeof(*cmd
))) {
1376 perror("failed: write");
1380 in
= fdopen(fd
, "r");
1382 perror("failed: fdopen");
1386 while ((nread
= getline(&line
, &len
, in
)) != -1) {
1387 if (fwrite(line
, nread
, 1, stdout
) != 1)
1397 /* If in is defined, then fd is closed via fclose. */
1403 static int send_cmd_list(struct daemon
*daemon
)
1407 memset(&cmd
, 0, sizeof(cmd
));
1408 cmd
.list
.cmd
= CMD_LIST
;
1409 cmd
.list
.verbose
= verbose
;
1410 cmd
.list
.csv_sep
= daemon
->csv_sep
? *daemon
->csv_sep
: 0;
1412 return send_cmd(daemon
, &cmd
);
1415 static int __cmd_signal(struct daemon
*daemon
, struct option parent_options
[],
1416 int argc
, const char **argv
)
1418 const char *name
= "all";
1419 struct option start_options
[] = {
1420 OPT_STRING(0, "session", &name
, "session",
1421 "Sent signal to specific session"),
1422 OPT_PARENT(parent_options
),
1427 argc
= parse_options(argc
, argv
, start_options
, daemon_usage
, 0);
1429 usage_with_options(daemon_usage
, start_options
);
1431 if (setup_config(daemon
)) {
1432 pr_err("failed: config not found\n");
1436 memset(&cmd
, 0, sizeof(cmd
));
1437 cmd
.signal
.cmd
= CMD_SIGNAL
;
1438 cmd
.signal
.sig
= SIGUSR2
;
1439 strncpy(cmd
.signal
.name
, name
, sizeof(cmd
.signal
.name
) - 1);
1441 return send_cmd(daemon
, &cmd
);
1444 static int __cmd_stop(struct daemon
*daemon
, struct option parent_options
[],
1445 int argc
, const char **argv
)
1447 struct option start_options
[] = {
1448 OPT_PARENT(parent_options
),
1453 argc
= parse_options(argc
, argv
, start_options
, daemon_usage
, 0);
1455 usage_with_options(daemon_usage
, start_options
);
1457 if (setup_config(daemon
)) {
1458 pr_err("failed: config not found\n");
1462 memset(&cmd
, 0, sizeof(cmd
));
1464 return send_cmd(daemon
, &cmd
);
1467 static int __cmd_ping(struct daemon
*daemon
, struct option parent_options
[],
1468 int argc
, const char **argv
)
1470 const char *name
= "all";
1471 struct option ping_options
[] = {
1472 OPT_STRING(0, "session", &name
, "session",
1473 "Ping to specific session"),
1474 OPT_PARENT(parent_options
),
1479 argc
= parse_options(argc
, argv
, ping_options
, daemon_usage
, 0);
1481 usage_with_options(daemon_usage
, ping_options
);
1483 if (setup_config(daemon
)) {
1484 pr_err("failed: config not found\n");
1488 memset(&cmd
, 0, sizeof(cmd
));
1490 scnprintf(cmd
.ping
.name
, sizeof(cmd
.ping
.name
), "%s", name
);
1491 return send_cmd(daemon
, &cmd
);
1494 static char *alloc_perf_exe_path(void)
1496 char path
[PATH_MAX
];
1498 perf_exe(path
, sizeof(path
));
1499 return strdup(path
);
1502 int cmd_daemon(int argc
, const char **argv
)
1504 struct option daemon_options
[] = {
1505 OPT_INCR('v', "verbose", &verbose
, "be more verbose"),
1506 OPT_STRING(0, "config", &__daemon
.config
,
1507 "config file", "config file path"),
1508 OPT_STRING(0, "base", &__daemon
.base_user
,
1509 "directory", "base directory"),
1510 OPT_STRING_OPTARG('x', "field-separator", &__daemon
.csv_sep
,
1511 "field separator", "print counts with custom separator", ","),
1516 __daemon
.perf
= alloc_perf_exe_path();
1520 __daemon
.out
= stdout
;
1522 argc
= parse_options(argc
, argv
, daemon_options
, daemon_usage
,
1523 PARSE_OPT_STOP_AT_NON_OPTION
);
1526 if (!strcmp(argv
[0], "start"))
1527 ret
= __cmd_start(&__daemon
, daemon_options
, argc
, argv
);
1528 else if (!strcmp(argv
[0], "signal"))
1529 ret
= __cmd_signal(&__daemon
, daemon_options
, argc
, argv
);
1530 else if (!strcmp(argv
[0], "stop"))
1531 ret
= __cmd_stop(&__daemon
, daemon_options
, argc
, argv
);
1532 else if (!strcmp(argv
[0], "ping"))
1533 ret
= __cmd_ping(&__daemon
, daemon_options
, argc
, argv
);
1535 pr_err("failed: unknown command '%s'\n", argv
[0]);
1537 ret
= setup_config(&__daemon
);
1539 pr_err("failed: config not found\n");
1541 ret
= send_cmd_list(&__daemon
);
1543 zfree(&__daemon
.perf
);