Lines Matching +full:sig +full:- +full:dir

1 // SPDX-License-Identifier: GPL-2.0
3 #include <subcmd/parse-options.h>
39 * OK - session is up and running
40 * RECONFIG - session is pending for reconfiguration,
42 * KILL - session is pending to be killed
48 * - reads config file and setup session objects
51 * OK - no change needed
52 * RECONFIG - session needs to be changed
54 * KILL - session needs to be killed
58 * - scans session objects and does following actions
61 * OK - skip
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
110 static void sig_handler(int sig __maybe_unused) in sig_handler()
122 session->name = strdup(name); in daemon__add_session()
123 if (!session->name) { in daemon__add_session()
128 session->pid = -1; in daemon__add_session()
129 list_add_tail(&session->list, &config->sessions); in daemon__add_session()
137 list_for_each_entry(session, &daemon->sessions, list) { in daemon__find_session()
138 if (!strcmp(session->name, name)) in daemon__find_session()
147 const char *p = var + sizeof("session-") - 1; in get_session_name()
149 while (*p != '.' && *p != 0x0 && len--) in get_session_name()
153 return *p == '.' ? 0 : -EINVAL; in get_session_name()
161 if (get_session_name(var, name, sizeof(name) - 1)) in session_config()
162 return -EINVAL; in session_config()
166 return -EINVAL; in session_config()
176 return -ENOMEM; in session_config()
181 session->state = RECONFIG; in session_config()
182 } else if (session->state == KILL) { in session_config()
185 session->state = OK; in session_config()
191 if (session->run) in session_config()
192 same = !strcmp(session->run, value); in session_config()
195 if (session->run) { in session_config()
196 zfree(&session->run); in session_config()
200 session->run = strdup(value); in session_config()
201 if (!session->run) in session_config()
202 return -ENOMEM; in session_config()
208 session->state = RECONFIG; in session_config()
219 if (strstarts(var, "session-")) { in server_config()
221 } else if (!strcmp(var, "daemon.base") && !daemon->base_user) { in server_config()
222 if (daemon->base && strcmp(daemon->base, value)) { in server_config()
224 return -EINVAL; in server_config()
226 daemon->base = strdup(value); in server_config()
227 if (!daemon->base) in server_config()
228 return -ENOMEM; in server_config()
238 if (!strcmp(var, "daemon.base") && !daemon->base_user) { in client_config()
239 daemon->base = strdup(value); in client_config()
240 if (!daemon->base) in client_config()
241 return -ENOMEM; in client_config()
251 if (!daemon->base) { in check_base()
253 return -EINVAL; in check_base()
256 if (stat(daemon->base, &st)) { in check_base()
260 daemon->base); in check_base()
261 return -EACCES; in check_base()
264 daemon->base); in check_base()
265 return -EACCES; in check_base()
268 daemon->base, strerror(errno)); in check_base()
269 return -errno; in check_base()
275 daemon->base); in check_base()
276 return -EINVAL; in check_base()
284 struct perf_config_set *set = perf_config_set__load_file(daemon->config_real); in setup_client_config()
285 int err = -ENOMEM; in setup_client_config()
299 int err = -ENOMEM; in setup_server_config()
308 list_for_each_entry(session, &daemon->sessions, list) in setup_server_config()
309 session->state = KILL; in setup_server_config()
311 set = perf_config_set__load_file(daemon->config_real); in setup_server_config()
327 if (asprintf(&session->base, "%s/session-%s", in daemon_session__run()
328 daemon->base, session->name) < 0) { in daemon_session__run()
330 return -1; in daemon_session__run()
333 if (mkdir(session->base, 0755) && errno != EEXIST) { in daemon_session__run()
335 return -1; in daemon_session__run()
338 session->start = time(NULL); in daemon_session__run()
340 session->pid = fork(); in daemon_session__run()
341 if (session->pid < 0) in daemon_session__run()
342 return -1; in daemon_session__run()
343 if (session->pid > 0) { in daemon_session__run()
345 session->name, session->pid, session->run); in daemon_session__run()
349 if (chdir(session->base)) { in daemon_session__run()
351 return -1; in daemon_session__run()
357 return -1; in daemon_session__run()
366 return -1; in daemon_session__run()
375 return -1; in daemon_session__run()
380 return -1; in daemon_session__run()
383 scnprintf(buf, sizeof(buf), "%s record --control=fifo:%s,%s %s", in daemon_session__run()
384 daemon->perf, SESSION_CONTROL, SESSION_ACK, session->run); in daemon_session__run()
388 exit(-1); in daemon_session__run()
390 exit(execve(daemon->perf, argv, NULL)); in daemon_session__run()
391 return -1; in daemon_session__run()
408 err = read(daemon->signal_fd, &si, sizeof(struct signalfd_siginfo)); in handle_signalfd()
411 return -1; in handle_signalfd()
414 list_for_each_entry(session, &daemon->sessions, list) { in handle_signalfd()
415 if (session->pid == -1) in handle_signalfd()
418 pid = waitpid(session->pid, &status, WNOHANG); in handle_signalfd()
424 session->name, WEXITSTATUS(status)); in handle_signalfd()
427 session->name, WTERMSIG(status)); in handle_signalfd()
430 session->name, WSTOPSIG(status)); in handle_signalfd()
433 session->name, status); in handle_signalfd()
436 session->state = KILL; in handle_signalfd()
437 session->pid = -1; in handle_signalfd()
447 .fd = daemon->signal_fd, in daemon_session__wait()
461 return -1; in daemon_session__wait()
465 return -1; in daemon_session__wait()
466 } while (session->pid != -1); in daemon_session__wait()
475 list_for_each_entry(session, &daemon->sessions, list) { in daemon__has_alive_session()
476 if (session->pid != -1) in daemon__has_alive_session()
486 .fd = daemon->signal_fd, in daemon__wait()
500 return -1; in daemon__wait()
504 return -1; in daemon__wait()
516 int control, ack = -1, len; in daemon_session__control()
518 int ret = -1; in daemon_session__control()
523 session->base, SESSION_CONTROL); in daemon_session__control()
527 return -1; in daemon_session__control()
532 session->base, SESSION_ACK); in daemon_session__control()
537 return -1; in daemon_session__control()
574 if (ack != -1) in daemon_session__control()
589 return -1; in setup_server_socket()
595 return -1; in setup_server_socket()
598 scnprintf(path, sizeof(path), "%s/control", daemon->base); in setup_server_socket()
603 return -1; in setup_server_socket()
609 strlcpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); in setup_server_socket()
612 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { in setup_server_socket()
615 return -1; in setup_server_socket()
618 if (listen(fd, 1) == -1) { in setup_server_socket()
621 return -1; in setup_server_socket()
650 int sig; member
674 char csv_sep = cmd->list.csv_sep; in cmd_session_list()
683 csv_sep, daemon->base, in cmd_session_list()
685 csv_sep, daemon->base, SESSION_OUTPUT); in cmd_session_list()
689 csv_sep, daemon->base, "lock"); in cmd_session_list()
693 csv_sep, (curr - daemon->start) / 60); in cmd_session_list()
697 fprintf(out, "[%d:daemon] base: %s\n", getpid(), daemon->base); in cmd_session_list()
698 if (cmd->list.verbose) { in cmd_session_list()
700 daemon->base, SESSION_OUTPUT); in cmd_session_list()
702 daemon->base); in cmd_session_list()
704 (curr - daemon->start) / 60); in cmd_session_list()
708 list_for_each_entry(session, &daemon->sessions, list) { in cmd_session_list()
712 session->pid, in cmd_session_list()
714 csv_sep, session->name, in cmd_session_list()
716 csv_sep, session->run); in cmd_session_list()
719 /* session dir */ in cmd_session_list()
720 csv_sep, session->base, in cmd_session_list()
722 csv_sep, session->base, SESSION_OUTPUT); in cmd_session_list()
726 csv_sep, session->base, SESSION_CONTROL, in cmd_session_list()
728 csv_sep, session->base, SESSION_ACK); in cmd_session_list()
732 csv_sep, (curr - session->start) / 60); in cmd_session_list()
737 session->pid, session->name, session->run); in cmd_session_list()
738 if (!cmd->list.verbose) in cmd_session_list()
741 session->base); in cmd_session_list()
743 session->base, SESSION_OUTPUT); in cmd_session_list()
745 session->base, SESSION_CONTROL); in cmd_session_list()
747 session->base, SESSION_ACK); in cmd_session_list()
749 (curr - session->start) / 60); in cmd_session_list()
756 static int daemon_session__signal(struct daemon_session *session, int sig) in daemon_session__signal() argument
758 if (session->pid < 0) in daemon_session__signal()
759 return -1; in daemon_session__signal()
760 return kill(session->pid, sig); in daemon_session__signal()
768 all = !strcmp(cmd->signal.name, "all"); in cmd_session_kill()
770 list_for_each_entry(session, &daemon->sessions, list) { in cmd_session_kill()
771 if (all || !strcmp(cmd->signal.name, session->name)) { in cmd_session_kill()
772 daemon_session__signal(session, cmd->signal.sig); in cmd_session_kill()
774 cmd->signal.sig, session->name, session->pid); in cmd_session_kill()
791 all = !strcmp(cmd->ping.name, "all"); in cmd_session_ping()
793 list_for_each_entry(session, &daemon->sessions, list) { in cmd_session_ping()
794 if (all || !strcmp(cmd->ping.name, session->name)) { in cmd_session_ping()
797 fprintf(out, "%-4s %s\n", ping_str[state], session->name); in cmd_session_ping()
803 fprintf(out, "%-4s %s (not found)\n", in cmd_session_ping()
804 ping_str[PING_FAIL], cmd->ping.name); in cmd_session_ping()
811 int ret = -1, fd; in handle_server_socket()
818 return -1; in handle_server_socket()
865 if (fd == -1) { in setup_client_socket()
867 return -1; in setup_client_socket()
870 scnprintf(path, sizeof(path), "%s/control", daemon->base); in setup_client_socket()
875 return -1; in setup_client_socket()
880 strlcpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); in setup_client_socket()
882 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { in setup_client_socket()
885 return -1; in setup_client_socket()
909 session->name); in daemon_session__kill()
917 static void daemon__signal(struct daemon *daemon, int sig) in daemon__signal() argument
921 list_for_each_entry(session, &daemon->sessions, list) in daemon__signal()
922 daemon_session__signal(session, sig); in daemon__signal()
927 zfree(&session->base); in daemon_session__delete()
928 zfree(&session->name); in daemon_session__delete()
929 zfree(&session->run); in daemon_session__delete()
935 list_del(&session->list); in daemon_session__remove()
943 list_for_each_entry(session, &daemon->sessions, list) in daemon__stop()
975 list_for_each_entry_safe(session, h, &daemon->sessions, list) in daemon__exit()
978 zfree(&daemon->config_real); in daemon__exit()
979 zfree(&daemon->config_base); in daemon__exit()
980 zfree(&daemon->base); in daemon__exit()
987 list_for_each_entry_safe(session, n, &daemon->sessions, list) { in daemon__reconfig()
989 if (session->state == OK) in daemon__reconfig()
993 if (session->state == KILL) { in daemon__reconfig()
994 if (session->pid > 0) { in daemon__reconfig()
996 pr_info("reconfig: session '%s' killed\n", session->name); in daemon__reconfig()
1003 if (session->pid > 0) { in daemon__reconfig()
1005 pr_info("reconfig: session '%s' killed\n", session->name); in daemon__reconfig()
1008 return -1; in daemon__reconfig()
1010 session->state = OK; in daemon__reconfig()
1018 char *basen = strdup(daemon->config_real); in setup_config_changes()
1019 char *dirn = strdup(daemon->config_real); in setup_config_changes()
1020 char *base, *dir; in setup_config_changes() local
1021 int fd, wd = -1; in setup_config_changes()
1032 dir = dirname(dirn); in setup_config_changes()
1034 pr_debug("config file: %s, dir: %s\n", base, dir); in setup_config_changes()
1036 wd = inotify_add_watch(fd, dir, IN_CLOSE_WRITE); in setup_config_changes()
1038 daemon->config_base = strdup(base); in setup_config_changes()
1039 if (!daemon->config_base) { in setup_config_changes()
1041 wd = -1; in setup_config_changes()
1050 return wd < 0 ? -1 : fd; in setup_config_changes()
1064 if ((event->mask & IN_CLOSE_WRITE) && in process_inotify_event()
1065 !(event->mask & IN_ISDIR)) { in process_inotify_event()
1066 if (!strcmp(event->name, daemon->config_base)) in process_inotify_event()
1069 p += sizeof(*event) + event->len; in process_inotify_event()
1082 if (len == -1) { in handle_config_changes()
1085 return -1; in handle_config_changes()
1096 if (daemon->base_user) { in setup_config()
1097 daemon->base = strdup(daemon->base_user); in setup_config()
1098 if (!daemon->base) in setup_config()
1099 return -ENOMEM; in setup_config()
1102 if (daemon->config) { in setup_config()
1103 char *real = realpath(daemon->config, NULL); in setup_config()
1107 return -1; in setup_config()
1109 daemon->config_real = real; in setup_config()
1114 daemon->config_real = strdup(perf_etc_perfconfig()); in setup_config()
1116 daemon->config_real = strdup(perf_home_perfconfig()); in setup_config()
1118 return daemon->config_real ? 0 : -1; in setup_config()
1127 return -1; in lockf()
1148 scnprintf(path, sizeof(path), "%s/lock", daemon->base); in check_lock()
1152 return -1; in check_lock()
1157 pid, daemon->base); in check_lock()
1159 return -1; in check_lock()
1168 return -1; in check_lock()
1174 return -1; in check_lock()
1186 return -1; in go_background()
1192 return -1; in go_background()
1195 return -1; in go_background()
1199 if (chdir(daemon->base)) { in go_background()
1201 return -1; in go_background()
1207 return -1; in go_background()
1213 return -1; in go_background()
1221 daemon->out = fdopen(1, "w"); in go_background()
1222 if (!daemon->out) { in go_background()
1225 return -1; in go_background()
1228 setbuf(daemon->out, NULL); in go_background()
1239 if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) in setup_signalfd()
1240 return -1; in setup_signalfd()
1242 daemon->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); in setup_signalfd()
1243 return daemon->signal_fd; in setup_signalfd()
1255 int sock_fd = -1, conf_fd = -1, signal_fd = -1; in __cmd_start()
1264 daemon->start = time(NULL); in __cmd_start()
1268 return -1; in __cmd_start()
1272 return -1; in __cmd_start()
1275 return -1; in __cmd_start()
1288 debug_set_file(daemon->out); in __cmd_start()
1326 if (!err && fdarray__poll(&fda, -1)) { in __cmd_start()
1347 if (sock_fd != -1) in __cmd_start()
1349 if (conf_fd != -1) in __cmd_start()
1351 if (signal_fd != -1) in __cmd_start()
1355 fclose(daemon->out); in __cmd_start()
1361 int ret = -1, fd; in send_cmd()
1368 return -1; in send_cmd()
1372 return -1; in send_cmd()
1385 while ((nread = getline(&line, &len, in)) != -1) { in send_cmd()
1409 cmd.list.csv_sep = daemon->csv_sep ? *daemon->csv_sep : 0; in send_cmd_list()
1432 return -1; in __cmd_signal()
1437 cmd.signal.sig = SIGUSR2; in __cmd_signal()
1438 strncpy(cmd.signal.name, name, sizeof(cmd.signal.name) - 1); in __cmd_signal()
1458 return -1; in __cmd_stop()
1484 return -1; in __cmd_ping()
1509 OPT_STRING_OPTARG('x', "field-separator", &__daemon.csv_sep, in cmd_daemon()
1513 int ret = -1; in cmd_daemon()
1517 return -ENOMEM; in cmd_daemon()