]> sourceware.org Git - systemtap.git/blame - staprun/mainloop.c
PR18848: Only compile monitor mode if json-c and ncurses developer
[systemtap.git] / staprun / mainloop.c
CommitLineData
a846e9cd
MH
1/* -*- linux-c -*-
2 *
3c02e16c 3 * mainloop - stapio main loop
a846e9cd
MH
4 *
5 * This file is part of systemtap, and is free software. You can
6 * redistribute it and/or modify it under the terms of the GNU General
7 * Public License (GPL); either version 2, or (at your option) any
8 * later version.
9 *
c9dd6e42 10 * Copyright (C) 2005-2010 Red Hat Inc.
a846e9cd
MH
11 */
12
13#include "staprun.h"
5eddf13b 14#include <sys/utsname.h>
e56e51c9 15#include <sys/ptrace.h>
a9e19b38 16#include <sys/select.h>
8358a79c 17#include <search.h>
a80aa597 18#include <wordexp.h>
11ea78d6 19#if HAVE_MONITOR_LIBS
fc8cab34 20#include <json-c/json.h>
443a2cbc 21#include <curses.h>
11ea78d6 22#endif
a80aa597 23
a846e9cd 24
d87442b6
FCE
25#define WORKAROUND_BZ467568 1 /* PR 6964; XXX: autoconf when able */
26
27
a846e9cd 28/* globals */
a846e9cd 29int ncpus;
b197bf0b 30static int use_old_transport = 0;
bf17a5d2 31static int pending_interrupts = 0;
276c6712 32static int target_pid_failed_p = 0;
bf17a5d2 33
a9e19b38
MW
34/* Setup by setup_main_signals, used by signal_thread to notify the
35 main thread of interruptable events. */
36static pthread_t main_thread;
ab70160b 37
25acff96 38static void *signal_thread(void *arg)
83c5b5fe 39{
e56e51c9 40 sigset_t *s = (sigset_t *) arg;
555ffd15 41 int signum = 0;
e56e51c9
FCE
42
43 while (1) {
44 if (sigwait(s, &signum) < 0) {
45 _perr("sigwait");
46 continue;
47 }
48 dbug(2, "sigproc %d (%s)\n", signum, strsignal(signum));
bf17a5d2
FCE
49 if (signum == SIGQUIT) {
50 pending_interrupts += 2;
51 break;
52 } else if (signum == SIGINT || signum == SIGHUP || signum == SIGTERM) {
53 pending_interrupts ++;
e56e51c9
FCE
54 break;
55 }
56 }
a9e19b38
MW
57 /* Notify main thread (interrupts select). */
58 pthread_kill (main_thread, SIGURG);
e56e51c9 59 return NULL;
83c5b5fe
MH
60}
61
a9e19b38
MW
62static void urg_proc(int signum)
63{
64 /* This handler is just notified from the signal_thread
65 whenever an interruptable condition is detected. The
66 handler itself doesn't do anything. But this will
67 result select to detect an EINTR event. */
68 dbug(2, "urg_proc %d (%s)\n", signum, strsignal(signum));
69}
70
3c335533
MH
71static void chld_proc(int signum)
72{
e56e51c9 73 int32_t rc, btype = STP_EXIT;
276c6712 74 int chld_stat = 0;
e56e51c9 75 dbug(2, "chld_proc %d (%s)\n", signum, strsignal(signum));
276c6712 76 pid_t pid = waitpid(-1, &chld_stat, WNOHANG);
45a8754f 77 if (pid != target_pid) {
e56e51c9 78 return;
276c6712
LB
79 }
80
45a8754f
JS
81 if (chld_stat) {
82 // our child exited with a non-zero status
83 if (WIFSIGNALED(chld_stat)) {
11bfd0cc 84 warn(_("Child process exited with signal %d (%s)\n"),
45a8754f 85 WTERMSIG(chld_stat), strsignal(WTERMSIG(chld_stat)));
276c6712
LB
86 target_pid_failed_p = 1;
87 }
45a8754f 88 if (WIFEXITED(chld_stat) && WEXITSTATUS(chld_stat)) {
11bfd0cc 89 warn(_("Child process exited with status %d\n"),
45a8754f
JS
90 WEXITSTATUS(chld_stat));
91 target_pid_failed_p = 1;
276c6712
LB
92 }
93 }
94
95 rc = write(control_channel, &btype, sizeof(btype)); // send STP_EXIT
5d8a0aea 96 (void) rc; /* XXX: notused */
3c335533
MH
97}
98
d87442b6 99#if WORKAROUND_BZ467568
ba9abf30
DS
100/* When a SIGUSR1 signal arrives, set this variable. */
101volatile sig_atomic_t usr1_interrupt = 0;
102
103static void signal_usr1(int signum)
d87442b6
FCE
104{
105 (void) signum;
ba9abf30 106 usr1_interrupt = 1;
d87442b6 107}
ba9abf30 108#endif /* WORKAROUND_BZ467568 */
d87442b6 109
25acff96 110static void setup_main_signals(void)
83c5b5fe 111{
e56e51c9
FCE
112 pthread_t tid;
113 struct sigaction sa;
114 sigset_t *s = malloc(sizeof(*s));
115 if (!s) {
116 _perr("malloc failed");
117 exit(1);
118 }
a9e19b38
MW
119
120 /* The main thread will only handle SIGCHLD and SIGURG.
121 SIGURG is send from the signal thread in case the interrupt
122 flag is set. This will then interrupt any select call. */
123 main_thread = pthread_self();
e56e51c9
FCE
124 sigfillset(s);
125 pthread_sigmask(SIG_SETMASK, s, NULL);
d87442b6 126
e56e51c9 127 memset(&sa, 0, sizeof(sa));
a9e19b38
MW
128 /* select will report EINTR even when SA_RESTART is set. */
129 sa.sa_flags = SA_RESTART;
e56e51c9 130 sigfillset(&sa.sa_mask);
a9e19b38
MW
131
132 /* Ignore all these events on the main thread. */
e56e51c9
FCE
133 sa.sa_handler = SIG_IGN;
134 sigaction(SIGINT, &sa, NULL);
135 sigaction(SIGTERM, &sa, NULL);
136 sigaction(SIGHUP, &sa, NULL);
137 sigaction(SIGQUIT, &sa, NULL);
138
a9e19b38 139 /* This is to notify when our child process (-c) ends. */
e56e51c9
FCE
140 sa.sa_handler = chld_proc;
141 sigaction(SIGCHLD, &sa, NULL);
142
11ea78d6 143#if HAVE_MONITOR_LIBS
4dd75b93
FL
144 if (monitor)
145 {
146 sa.sa_handler = monitor_winch;
147 sigaction(SIGWINCH, &sa, NULL);
148 }
11ea78d6 149#endif
da2c1bf6 150
a9e19b38
MW
151 /* This signal handler is notified from the signal_thread
152 whenever a interruptable event is detected. It will
153 result in an EINTR event for select or sleep. */
154 sa.sa_handler = urg_proc;
155 sigaction(SIGURG, &sa, NULL);
156
157 /* Everything else is handled on a special signal_thread. */
e56e51c9
FCE
158 sigemptyset(s);
159 sigaddset(s, SIGINT);
160 sigaddset(s, SIGTERM);
161 sigaddset(s, SIGHUP);
162 sigaddset(s, SIGQUIT);
163 pthread_sigmask(SIG_SETMASK, s, NULL);
164 if (pthread_create(&tid, NULL, signal_thread, s) < 0) {
737d9ce6 165 _perr(_("failed to create thread"));
e56e51c9
FCE
166 exit(1);
167 }
83c5b5fe
MH
168}
169
e56e51c9
FCE
170/*
171 * start_cmd forks the command given on the command line with the "-c"
172 * option. It will wait just at the cusp of the exec until we get the
173 * signal from the kernel to let it run. We do it this way because we
174 * must have the pid of the forked command so it can be set to the
175 * module and made available internally as _stp_target. PTRACE_DETACH
176 * is sent from stp_main_loop() below when it receives STP_START from
177 * the module.
a846e9cd
MH
178 */
179void start_cmd(void)
180{
e56e51c9
FCE
181 pid_t pid;
182 struct sigaction a;
ba9abf30
DS
183#if WORKAROUND_BZ467568
184 struct sigaction usr1_action, old_action;
185 sigset_t blockmask, oldmask;
186#endif /* WORKAROUND_BZ467568 */
e56e51c9
FCE
187
188 /* if we are execing a target cmd, ignore ^C in stapio */
189 /* and let the target cmd get it. */
11c6c509 190 memset(&a, 0, sizeof(a));
e56e51c9
FCE
191 sigemptyset(&a.sa_mask);
192 a.sa_flags = 0;
193 a.sa_handler = SIG_IGN;
194 sigaction(SIGINT, &a, NULL);
195
ba9abf30
DS
196#if WORKAROUND_BZ467568
197 /* Set up the mask of signals to temporarily block. */
198 sigemptyset (&blockmask);
199 sigaddset (&blockmask, SIGUSR1);
200
201 /* Establish the SIGUSR1 signal handler. */
11c6c509 202 memset(&usr1_action, 0, sizeof(usr1_action));
ba9abf30
DS
203 sigfillset (&usr1_action.sa_mask);
204 usr1_action.sa_flags = 0;
205 usr1_action.sa_handler = signal_usr1;
206 sigaction (SIGUSR1, &usr1_action, &old_action);
207
208 /* Block SIGUSR1 */
209 sigprocmask(SIG_BLOCK, &blockmask, &oldmask);
210#endif /* WORKAROUND_BZ467568 */
211
e56e51c9
FCE
212 if ((pid = fork()) < 0) {
213 _perr("fork");
214 exit(1);
215 } else if (pid == 0) {
216 /* We're in the target process. Let's start the execve of target_cmd, */
217 int rc;
a80aa597 218 wordexp_t words;
34f2e0b9 219 char *sh_c_argv[4] = { NULL, NULL, NULL, NULL };
e56e51c9
FCE
220
221 a.sa_handler = SIG_DFL;
222 sigaction(SIGINT, &a, NULL);
223
a80aa597
FCE
224 /* Formerly, we just execl'd(sh,-c,$target_cmd). But this does't
225 work well if target_cmd is a shell builtin. We really want to
226 probe a new child process, not a mishmash of shell-interpreted
227 stuff. */
927b945c 228 rc = wordexp (target_cmd, & words, WRDE_NOCMD|WRDE_UNDEF);
34f2e0b9 229 if (rc == WRDE_BADCHAR)
927b945c 230 {
34f2e0b9
FCE
231 /* The user must have used a shell metacharacter, thinking that
232 we use system(3) to evaluate 'stap -c CMD'. We could generate
aad1a79c 233 an error message ... but let's just do what the user meant.
34f2e0b9
FCE
234 rhbz 467652. */
235 sh_c_argv[0] = "sh";
236 sh_c_argv[1] = "-c";
237 sh_c_argv[2] = target_cmd;
238 sh_c_argv[3] = NULL;
239 }
240 else
241 {
aad1a79c 242 switch (rc)
34f2e0b9 243 {
aad1a79c 244 case 0:
34f2e0b9
FCE
245 break;
246 case WRDE_SYNTAX:
737d9ce6 247 _err (_("wordexp: syntax error (unmatched quotes?) in -c COMMAND\n"));
34f2e0b9
FCE
248 _exit(1);
249 default:
737d9ce6 250 _err (_("wordexp: parsing error (%d)\n"), rc);
34f2e0b9
FCE
251 _exit (1);
252 }
253 if (words.we_wordc < 1) { _err ("empty -c COMMAND"); _exit (1); }
927b945c 254 }
a80aa597 255
d87442b6
FCE
256/* PR 6964: when tracing all the user space process including the
257 child the signal will be messed due to uprobe module or utrace
258 bug. The kernel sometimes crashes. So as an alternative
259 approximation, we just wait here for a signal from the parent. */
e56e51c9 260
a80aa597 261 dbug(1, "blocking briefly\n");
d87442b6
FCE
262#if WORKAROUND_BZ467568
263 {
ba9abf30
DS
264 /* Wait for the SIGUSR1 */
265 while (!usr1_interrupt)
266 sigsuspend(&oldmask);
267
268 /* Restore the old SIGUSR1 signal handler. */
269 sigaction (SIGUSR1, &old_action, NULL);
270
271 /* Restore the original signal mask */
272 sigprocmask(SIG_SETMASK, &oldmask, NULL);
d87442b6 273 }
ba9abf30 274#else /* !WORKAROUND_BZ467568 */
d87442b6
FCE
275 rc = ptrace (PTRACE_TRACEME, 0, 0, 0);
276 if (rc < 0) perror ("ptrace me");
a80aa597 277 raise (SIGCONT); /* Harmless; just passes control to parent. */
ba9abf30 278#endif /* !WORKAROUND_BZ467568 */
a80aa597
FCE
279
280 dbug(1, "execing target_cmd %s\n", target_cmd);
281
282 /* Note that execvp() is not a direct system call; it does a $PATH
283 search in glibc. We would like to filter out these dummy syscalls
d87442b6
FCE
284 from the utrace events seen by scripts.
285
286 This filtering would be done for us for free, if we used ptrace
287 ... but see PR6964. XXX: Instead, we could open-code the
288 $PATH search here; put the pause() afterward; and run a direct
289 execve instead of execvp(). */
290
34f2e0b9
FCE
291 if (execvp ((sh_c_argv[0] == NULL ? words.we_wordv[0] : sh_c_argv[0]),
292 (sh_c_argv[0] == NULL ? words.we_wordv : sh_c_argv)) < 0)
e56e51c9 293 perror(target_cmd);
a80aa597
FCE
294
295 /* (There is no need to wordfree() words; they are or will be gone.) */
296
e56e51c9
FCE
297 _exit(1);
298 } else {
d87442b6
FCE
299 /* We're in the parent. The child will parse target_cmd and
300 execv() the result. It will be stopped thereabouts and send us
301 a SIGTRAP. Or rather, due to PR 6964, it will stop itself and wait for
302 us to release it. */
e56e51c9 303 target_pid = pid;
d87442b6 304#if WORKAROUND_BZ467568
ba9abf30
DS
305 /* Restore the old SIGUSR1 signal handler. */
306 sigaction (SIGUSR1, &old_action, NULL);
307
308 /* Restore the original signal mask */
309 sigprocmask(SIG_SETMASK, &oldmask, NULL);
310#else /* !WORKAROUND_BZ467568 */
a80aa597 311 int status;
e56e51c9 312 waitpid (target_pid, &status, 0);
a80aa597 313 dbug(1, "waited for target_cmd %s pid %d status %x\n", target_cmd, target_pid, (unsigned) status);
ba9abf30 314#endif /* !WORKAROUND_BZ467568 */
e56e51c9 315 }
a846e9cd
MH
316}
317
e56e51c9 318/**
a846e9cd
MH
319 * system_cmd() executes system commands in response
320 * to an STP_SYSTEM message from the module. These
321 * messages are sent by the system() systemtap function.
a846e9cd
MH
322 */
323void system_cmd(char *cmd)
324{
e56e51c9
FCE
325 pid_t pid;
326
327 dbug(2, "system %s\n", cmd);
328 if ((pid = fork()) < 0) {
329 _perr("fork");
330 } else if (pid == 0) {
aa17e6fd 331 if (execlp("sh", "sh", "-c", cmd, NULL) < 0)
e56e51c9
FCE
332 perr("%s", cmd);
333 _exit(1);
334 }
a846e9cd
MH
335}
336
5eddf13b
DS
337/* This is only used in the old relayfs code */
338static void read_buffer_info(void)
a846e9cd 339{
e56e51c9
FCE
340 char buf[PATH_MAX];
341 struct statfs st;
342 int fd, len, ret;
343
c5f7c84b
FCE
344 /* NB: we don't have to worry about PR14245 on old_transport aka
345 rhel4; no HAVE_OPENAT, and thus no -F fd option. */
e56e51c9
FCE
346 if (!use_old_transport)
347 return;
348
349 if (statfs("/sys/kernel/debug", &st) == 0 && (int)st.f_type == (int)DEBUGFS_MAGIC)
350 return;
351
352 if (sprintf_chk(buf, "/proc/systemtap/%s/bufsize", modname))
353 return;
354 fd = open(buf, O_RDONLY);
355 if (fd < 0)
356 return;
357
358 len = read(fd, buf, sizeof(buf));
359 if (len <= 0) {
737d9ce6 360 perr(_("Couldn't read bufsize"));
e56e51c9
FCE
361 close(fd);
362 return;
363 }
364 ret = sscanf(buf, "%u,%u", &n_subbufs, &subbuf_size);
365 if (ret != 2)
737d9ce6 366 perr(_("Couldn't read bufsize"));
e56e51c9
FCE
367
368 dbug(2, "n_subbufs= %u, size=%u\n", n_subbufs, subbuf_size);
369 close(fd);
370 return;
a846e9cd
MH
371}
372
a846e9cd 373/**
5eddf13b 374 * init_stapio - initialize the app
a846e9cd
MH
375 * @print_summary: boolean, print summary or not at end of run
376 *
377 * Returns 0 on success, negative otherwise.
378 */
5eddf13b 379int init_stapio(void)
a846e9cd 380{
e56e51c9
FCE
381 dbug(2, "init_stapio\n");
382
383 /* create control channel */
384 use_old_transport = init_ctl_channel(modname, 1);
385 if (use_old_transport < 0) {
737d9ce6 386 err(_("Failed to initialize control channel.\n"));
e56e51c9
FCE
387 return -1;
388 }
389 read_buffer_info();
390
391 if (attach_mod) {
392 dbug(2, "Attaching\n");
393 if (use_old_transport) {
394 if (init_oldrelayfs() < 0) {
395 close_ctl_channel();
396 return -1;
397 }
398 } else {
399 if (init_relayfs() < 0) {
400 close_ctl_channel();
401 return -1;
402 }
403 }
404 return 0;
405 }
406
407 /* fork target_cmd if requested. */
408 /* It will not actually exec until signalled. */
409 if (target_cmd)
410 start_cmd();
411
e9bb2221
AJ
412 if (target_namespaces_pid > 0)
413 dbug(2, "target_namespaces_pid=%d\n", target_namespaces_pid);
414
54892f28
MH
415 /* Run in background */
416 if (daemon_mode) {
417 pid_t pid;
418 int ret;
419 dbug(2, "daemonizing stapio\n");
420
421 /* daemonize */
422 ret = daemon(0, 1); /* don't close stdout at this time. */
423 if (ret) {
737d9ce6 424 err(_("Failed to daemonize stapio\n"));
54892f28
MH
425 return -1;
426 }
427
428 /* change error messages to syslog. */
429 switch_syslog("stapio");
430
431 /* show new pid */
432 pid = getpid();
433 fprintf(stdout, "%d\n", pid);
434 fflush(stdout);
435
436 /* redirect all outputs to /dev/null */
437 ret = open("/dev/null", O_RDWR);
438 if (ret < 0) {
737d9ce6 439 err(_("Failed to open /dev/null\n"));
54892f28
MH
440 return -1;
441 }
442 close(STDIN_FILENO);
443 close(STDOUT_FILENO);
444 close(STDERR_FILENO);
445 dup2(ret, STDOUT_FILENO);
446 dup2(ret, STDERR_FILENO);
447 close(ret);
448 }
449
e56e51c9 450 return 0;
a846e9cd
MH
451}
452
b197bf0b
MH
453/* cleanup_and_exit() closed channels, frees memory,
454 * removes the module (if necessary) and exits. */
249534c0 455void cleanup_and_exit(int detach, int rc)
a846e9cd 456{
e56e51c9 457 static int exiting = 0;
1477dcbc
FCE
458 const char *staprun;
459 pid_t pid;
460 int rstatus;
461 struct sigaction sa;
e56e51c9 462
11ea78d6 463#if HAVE_MONITOR_LIBS
443a2cbc
FL
464 if (monitor)
465 monitor_cleanup();
11ea78d6 466#endif
443a2cbc 467
e56e51c9
FCE
468 if (exiting)
469 return;
470 exiting = 1;
471
472 setup_main_signals();
473
474 dbug(1, "detach=%d\n", detach);
475
d87442b6
FCE
476 /* NB: We don't really need to wait for child processes. Any that
477 were started by the system() tapset function (system_cmd() above)
478 can run loose. Or, a target_cmd (stap -c CMD) may have already started and
479 stopped. */
480
481 /* OTOH, it may be still be running - but there's no need for
482 us to wait for it, considering that the script must have exited
483 for another reason. So, we no longer while(...wait()...); here.
484 XXX: we could consider killing it. */
e56e51c9
FCE
485
486 if (use_old_transport)
487 close_oldrelayfs(detach);
488 else
489 close_relayfs();
490
491 dbug(1, "closing control channel\n");
492 close_ctl_channel();
493
494 if (detach) {
11bfd0cc 495 eprintf(_("\nDisconnecting from systemtap module.\n" "To reconnect, type \"staprun -A %s\"\n"), modname);
1477dcbc
FCE
496 _exit(0);
497 }
5c854d7c 498 else if (rename_mod)
56c51cc8 499 dbug(2, "\nRenamed module to: %s\n", modname);
69aa1bdb 500
1477dcbc
FCE
501 /* At this point, we're committed to calling staprun -d MODULE to
502 * unload the thing and exit. */
503 /* Due to PR9788, we fork and exec the setuid staprun only in a child process. */
504
505 staprun = getenv ("SYSTEMTAP_STAPRUN") ?: BINDIR "/staprun";
506 dbug(2, "removing %s\n", modname);
c9dd6e42 507
1477dcbc
FCE
508 // So that waitpid() below will work correctly, we need to clear
509 // out our SIGCHLD handler.
11c6c509 510 memset(&sa, 0, sizeof(sa));
1477dcbc
FCE
511 sigemptyset(&sa.sa_mask);
512 sa.sa_flags = 0;
513 sa.sa_handler = SIG_DFL;
514 sigaction(SIGCHLD, &sa, NULL);
c9dd6e42 515
1477dcbc
FCE
516 pid = fork();
517 if (pid < 0) {
518 _perr("fork");
519 _exit(-1);
e56e51c9 520 }
c9dd6e42 521
1477dcbc
FCE
522 if (pid == 0) { /* child process */
523 /* Run the command. */
524 char *cmd;
579f0947 525 int rc = asprintf(&cmd, "%s %s %s -d -C %s '%s'", staprun,
1477dcbc
FCE
526 (verbose >= 1) ? "-v" : "",
527 (verbose >= 2) ? "-v" : "",
6d3599fc
JL
528 color_mode == color_always ? "always"
529 : color_mode == color_auto ? "auto" : "never",
1477dcbc
FCE
530 modname);
531 if (rc >= 1) {
aa17e6fd 532 execlp("sh", "sh", "-c", cmd, NULL);
1477dcbc
FCE
533 /* should not return */
534 perror(staprun);
535 _exit(-1);
536 } else {
537 perror("asprintf");
538 _exit(-1);
539 }
540 }
c9dd6e42 541
1477dcbc
FCE
542 /* parent process */
543 if (waitpid(pid, &rstatus, 0) < 0) {
544 _perr("waitpid");
545 _exit(-1);
546 }
c9dd6e42 547
1477dcbc 548 if (WIFEXITED(rstatus)) {
276c6712
LB
549 if(rc || target_pid_failed_p || rstatus) // if we have an error
550 _exit(1);
551 else
552 _exit(0); //success
1477dcbc 553 }
276c6712 554
1477dcbc 555 _exit(-1);
a846e9cd
MH
556}
557
1477dcbc 558
a846e9cd
MH
559/**
560 * stp_main_loop - loop forever reading data
561 */
a846e9cd
MH
562
563int stp_main_loop(void)
564{
e56e51c9 565 ssize_t nb;
e56e51c9 566 FILE *ofp = stdout;
c9dd6e42
RM
567 struct
568 {
569 uint32_t type;
570 union
571 {
572 char data[8192];
573 struct _stp_msg_start start;
574 struct _stp_msg_cmd cmd;
f9f56bab 575 struct _stp_msg_ns_pid nspid;
c9dd6e42
RM
576 } payload;
577 } recvbuf;
249534c0 578 int error_detected = 0;
a9e19b38 579 int select_supported;
bf17a5d2 580 int flags;
a9e19b38 581 int res;
933e53b0 582 int rc;
239f4e62 583 int maxfd;
a9e19b38 584 struct timeval tv;
11ea78d6 585#if HAVE_MONITOR_LIBS
10bb5efc 586 struct timespec ts;
11ea78d6 587#endif
6dfab43e 588 struct timespec *timeout = NULL;
a9e19b38
MW
589 fd_set fds;
590 sigset_t blockset, mainset;
591
e56e51c9 592
89651893 593 setvbuf(ofp, (char *)NULL, _IONBF, 0);
e56e51c9
FCE
594 setup_main_signals();
595 dbug(2, "in main loop\n");
596
933e53b0
DB
597 rc = send_request(STP_READY, NULL, 0);
598 if (rc != 0) {
599 perror ("Unable to send STP_READY");
600 cleanup_and_exit (1, rc);
601 }
e56e51c9 602
bf17a5d2
FCE
603 flags = fcntl(control_channel, F_GETFL);
604
a9e19b38
MW
605 /* Make select return immediately. We just check whether
606 there is an exception available on the control_channel,
607 which is how we know the module supports select. */
608 tv.tv_sec = 0;
609 tv.tv_usec = 0;
610 FD_ZERO(&fds);
611 FD_SET(control_channel, &fds);
612 res = select(control_channel + 1, NULL, NULL, &fds, &tv);
613 select_supported = (res == 1 && FD_ISSET(control_channel, &fds));
614 dbug(2, "select_supported: %d\n", select_supported);
615 if (select_supported) {
616 /* We block SIGURG to the main thread, except when we call
617 pselect(). This makes sure we won't miss any signals. All other
618 calls are non-blocking, so we defer till pselect() time, which
619 is when we are "sleeping". */
620 sigemptyset(&blockset);
621 sigaddset(&blockset, SIGURG);
622 pthread_sigmask(SIG_BLOCK, &blockset, &mainset);
623 }
624
11ea78d6 625#if HAVE_MONITOR_LIBS
10bb5efc
FL
626 /* In monitor mode, we must timeout pselect to poll the monitor
627 * interface. */
6dfab43e 628 if (monitor)
fc8cab34 629 {
443a2cbc 630 monitor_setup();
09ddc83c 631 ts.tv_sec = 0;
5ec604d2 632 ts.tv_nsec = 500*1000*1000;
fc8cab34
FL
633 timeout = &ts;
634 }
11ea78d6 635#endif
10bb5efc 636
e56e51c9 637 /* handle messages from control channel */
c9dd6e42 638 while (1) {
11ea78d6 639#if HAVE_MONITOR_LIBS
6dfab43e
FL
640 if (monitor)
641 {
443a2cbc
FL
642 monitor_input();
643 monitor_render();
6dfab43e 644 }
11ea78d6 645#endif
6dfab43e 646
c9dd6e42 647 if (pending_interrupts) {
bf17a5d2
FCE
648 int btype = STP_EXIT;
649 int rc = write(control_channel, &btype, sizeof(btype));
650 dbug(2, "signal-triggered %d exit rc %d\n", pending_interrupts, rc);
651 if (pending_interrupts >= 2) {
652 cleanup_and_exit (1, 0);
653 }
654 }
655
a9e19b38
MW
656
657 /* If the runtime does not implement select() on the command
658 filehandle, we have to poll periodically. The polling interval can
bf17a5d2
FCE
659 be relatively large, since we don't receive EAGAIN during the
660 time-sensitive startup period (packets go back-to-back). */
661
662 flags |= O_NONBLOCK;
663 fcntl(control_channel, F_SETFL, flags);
c9dd6e42 664 nb = read(control_channel, &recvbuf, sizeof(recvbuf));
bf17a5d2
FCE
665 flags &= ~O_NONBLOCK;
666 fcntl(control_channel, F_SETFL, flags);
667
c9dd6e42
RM
668 dbug(3, "nb=%ld\n", (long)nb);
669 if (nb < (ssize_t) sizeof(recvbuf.type)) {
670 if (nb >= 0 || (errno != EINTR && errno != EAGAIN)) {
737d9ce6 671 _perr(_("Unexpected EOF in read (nb=%ld)"), (long)nb);
e997043d
DS
672 cleanup_and_exit(0, 1);
673 }
a9e19b38
MW
674
675 if (!select_supported) {
676 dbug(4, "sleeping\n");
677 usleep (250*1000); /* sleep 250ms between polls */
678 } else {
679 FD_ZERO(&fds);
680 FD_SET(control_channel, &fds);
239f4e62 681 maxfd = control_channel;
11ea78d6 682#if HAVE_MONITOR_LIBS
239f4e62 683 if (monitor) {
7952c5a3 684 FD_SET(STDIN_FILENO, &fds);
239f4e62
FL
685 FD_SET(monitor_pfd[0], &fds);
686 if (monitor_pfd[0] > maxfd)
687 maxfd = monitor_pfd[0];
688 }
11ea78d6 689#endif
239f4e62 690 res = pselect(maxfd + 1, &fds, NULL, NULL, timeout, &mainset);
a9e19b38
MW
691 if (res < 0 && errno != EINTR)
692 {
693 _perr(_("Unexpected error in select"));
694 cleanup_and_exit(0, 1);
695 }
696 }
e56e51c9
FCE
697 continue;
698 }
699
c9dd6e42 700 nb -= sizeof(recvbuf.type);
0f5d597d 701 PROBE3(staprun, recv__ctlmsg, recvbuf.type, recvbuf.payload.data, nb);
e56e51c9 702
c9dd6e42 703 switch (recvbuf.type) {
9b23198d 704#if STP_TRANSPORT_VERSION == 1
e56e51c9 705 case STP_REALTIME_DATA:
c9dd6e42 706 if (write_realtime_data(recvbuf.payload.data, nb)) {
737d9ce6 707 _perr(_("write error (nb=%ld)"), (long)nb);
249534c0 708 cleanup_and_exit(0, 1);
e56e51c9 709 }
acd56c22 710 break;
a846e9cd 711#endif
e56e51c9 712 case STP_OOB_DATA:
06d3362b
DS
713 /* Note that "WARNING:" should not be translated, since it is
714 * part of the module cmd protocol. */
714f63a8 715 if (strncmp(recvbuf.payload.data, "WARNING: ", 9) == 0) {
8358a79c
FCE
716 if (suppress_warnings) break;
717 if (verbose) { /* don't eliminate duplicates */
714f63a8
JL
718 /* trim "WARNING: " */
719 warn("%.*s", (int) nb-9, recvbuf.payload.data+9);
8358a79c
FCE
720 break;
721 } else { /* eliminate duplicates */
722 static void *seen = 0;
723 static unsigned seen_count = 0;
724 char *dupstr = strndup (recvbuf.payload.data, (int) nb);
725 char *retval;
726
727 if (! dupstr) {
728 /* OOM, should not happen. */
714f63a8
JL
729 /* trim "WARNING: " */
730 warn("%.*s", (int) nb-9, recvbuf.payload.data+9);
8358a79c
FCE
731 break;
732 }
733
734 retval = tfind (dupstr, & seen, (int (*)(const void*, const void*))strcmp);
735 if (! retval) { /* new message */
714f63a8
JL
736 /* trim "WARNING: " */
737 warn("%.*s", strlen(dupstr)-9, dupstr+9);
8358a79c
FCE
738
739 /* We set a maximum for stored warning messages,
740 to prevent a misbehaving script/environment
741 from emitting countless _stp_warn()s, and
742 overflow staprun's memory. */
743#define MAX_STORED_WARNINGS 1024
744 if (seen_count++ == MAX_STORED_WARNINGS) {
737d9ce6 745 eprintf(_("WARNING deduplication table full\n"));
8358a79c
FCE
746 free (dupstr);
747 }
748 else if (seen_count > MAX_STORED_WARNINGS) {
749 /* Be quiet in the future, but stop counting to
750 preclude overflow. */
751 free (dupstr);
752 seen_count = MAX_STORED_WARNINGS+1;
753 }
754 else if (seen_count < MAX_STORED_WARNINGS) {
755 /* NB: don't free dupstr; it's going into the tree. */
756 retval = tsearch (dupstr, & seen,
757 (int (*)(const void*, const void*))strcmp);
758 if (retval == 0) {
759 /* OOM, should not happen */
760 /* Next time we should get the 'full' message. */
761 free (dupstr);
762 seen_count = MAX_STORED_WARNINGS;
763 }
764 }
765 } else { /* old message */
766 free (dupstr);
767 }
768 } /* duplicate elimination */
06d3362b
DS
769 /* Note that "ERROR:" should not be translated, since it is
770 * part of the module cmd protocol. */
714f63a8
JL
771 } else if (strncmp(recvbuf.payload.data, "ERROR: ", 7) == 0) {
772 /* trim "ERROR: " */
773 err("%.*s", (int) nb-7, recvbuf.payload.data+7);
8358a79c
FCE
774 error_detected = 1;
775 } else { /* neither warning nor error */
776 eprintf("%.*s", (int) nb, recvbuf.payload.data);
249534c0 777 }
e56e51c9
FCE
778 break;
779 case STP_EXIT:
780 {
781 /* module asks us to unload it and exit */
782 dbug(2, "got STP_EXIT\n");
249534c0 783 cleanup_and_exit(0, error_detected);
e56e51c9
FCE
784 break;
785 }
52aeb26b
JS
786 case STP_REQUEST_EXIT:
787 {
788 /* module asks us to start exiting, so send STP_EXIT */
789 dbug(2, "got STP_REQUEST_EXIT\n");
790 int32_t rc, btype = STP_EXIT;
791 rc = write(control_channel, &btype, sizeof(btype));
5d8a0aea 792 (void) rc; /* XXX: notused */
52aeb26b
JS
793 break;
794 }
e56e51c9
FCE
795 case STP_START:
796 {
c9dd6e42 797 struct _stp_msg_start *t = &recvbuf.payload.start;
9dfcbb9f 798 dbug(2, "systemtap_module_init() returned %d\n", t->res);
e56e51c9
FCE
799 if (t->res < 0) {
800 if (target_cmd)
801 kill(target_pid, SIGKILL);
249534c0 802 cleanup_and_exit(0, 1);
e56e51c9 803 } else if (target_cmd) {
a80aa597 804 dbug(1, "detaching pid %d\n", target_pid);
d87442b6
FCE
805#if WORKAROUND_BZ467568
806 /* Let's just send our pet signal to the child
807 process that should be waiting for us, mid-pause(). */
808 kill (target_pid, SIGUSR1);
809#else
810 /* Were it not for PR6964, we'd like to do it this way: */
e56e51c9
FCE
811 int rc = ptrace (PTRACE_DETACH, target_pid, 0, 0);
812 if (rc < 0)
813 {
737d9ce6 814 perror (_("ptrace detach"));
e56e51c9
FCE
815 if (target_cmd)
816 kill(target_pid, SIGKILL);
249534c0 817 cleanup_and_exit(0, 1);
e56e51c9 818 }
1b1d639e 819#endif
e56e51c9
FCE
820 }
821 break;
822 }
823 case STP_SYSTEM:
824 {
c9dd6e42 825 struct _stp_msg_cmd *c = &recvbuf.payload.cmd;
e56e51c9
FCE
826 dbug(2, "STP_SYSTEM: %s\n", c->cmd);
827 system_cmd(c->cmd);
828 break;
829 }
f9f56bab
AJ
830 case STP_NAMESPACES_PID:
831 {
832 struct _stp_msg_ns_pid *nspid = &recvbuf.payload.nspid;
833 dbug(2, "STP_NAMESPACES_PID: %d\n", nspid->target);
834 break;
835 }
e56e51c9
FCE
836 case STP_TRANSPORT:
837 {
838 struct _stp_msg_start ts;
f9f56bab 839 struct _stp_msg_ns_pid nspid;
e56e51c9
FCE
840 if (use_old_transport) {
841 if (init_oldrelayfs() < 0)
249534c0 842 cleanup_and_exit(0, 1);
e56e51c9
FCE
843 } else {
844 if (init_relayfs() < 0)
249534c0 845 cleanup_and_exit(0, 1);
e56e51c9 846 }
f9f56bab 847
d8155b15
AJ
848 if (target_namespaces_pid > 0) {
849 nspid.target = target_namespaces_pid;
850 rc = send_request(STP_NAMESPACES_PID, &nspid, sizeof(nspid));
851 if (rc != 0) {
852 perror ("Unable to send STP_NAMESPACES_PID");
853 cleanup_and_exit (1, rc);
854 }
855 }
856
e56e51c9 857 ts.target = target_pid;
933e53b0
DB
858 rc = send_request(STP_START, &ts, sizeof(ts));
859 if (rc != 0) {
860 perror ("Unable to send STP_START");
861 cleanup_and_exit (1, rc);
862 }
e56e51c9 863 if (load_only)
249534c0 864 cleanup_and_exit(1, 0);
e56e51c9
FCE
865 break;
866 }
867 default:
11bfd0cc 868 warn(_("Ignored message of type %d\n"), recvbuf.type);
e56e51c9
FCE
869 }
870 }
871 fclose(ofp);
872 return 0;
a846e9cd 873}
This page took 0.235624 seconds and 5 git commands to generate.