| 1 |
/* -*- linux-c -*- |
| 2 |
* |
| 3 |
* mainloop - stapio main loop |
| 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 |
* |
| 10 |
* Copyright (C) 2005-2023 Red Hat Inc. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "staprun.h" |
| 14 |
#include <sys/utsname.h> |
| 15 |
#include <sys/ptrace.h> |
| 16 |
#include <sys/select.h> |
| 17 |
#include <sys/syscall.h> |
| 18 |
#include <search.h> |
| 19 |
#include <wordexp.h> |
| 20 |
|
| 21 |
|
| 22 |
#define WORKAROUND_BZ467568 1 /* PR 6964; XXX: autoconf when able; also in start_cmd.c */ |
| 23 |
|
| 24 |
|
| 25 |
/* globals */ |
| 26 |
int ncpus; |
| 27 |
static volatile sig_atomic_t pending_interrupts = 0; // tells stp_main_loop to trigger STP_EXIT message to kernel |
| 28 |
static int target_pid_failed_p = 0; |
| 29 |
|
| 30 |
|
| 31 |
static void set_blocking_std_fds(void) |
| 32 |
{ |
| 33 |
int fd; |
| 34 |
for (fd = 1; fd < 3; fd++) { |
| 35 |
/* NB: writing to stderr/stdout blockingly in signal handler is |
| 36 |
* dangerous since it may prevent the stap process from quitting |
| 37 |
* gracefully on receiving SIGTERM/etc signals when the stderr/stdout |
| 38 |
* write buffer is full. PR23891 */ |
| 39 |
int flags = fcntl(fd, F_GETFL); |
| 40 |
if (flags == -1) |
| 41 |
continue; |
| 42 |
|
| 43 |
if (!(flags & O_NONBLOCK)) |
| 44 |
continue; |
| 45 |
|
| 46 |
(void) fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
static void my_exit(int rc) |
| 51 |
{ |
| 52 |
/* to avoid leaving any side-effects on the stdout/stderr devices */ |
| 53 |
if (pending_interrupts > 2) |
| 54 |
set_blocking_std_fds(); |
| 55 |
|
| 56 |
_exit(rc); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static void interrupt_handler(int signum) |
| 62 |
{ |
| 63 |
if (signum == SIGQUIT) { |
| 64 |
load_only = 1; /* flag for stp_main_loop */ |
| 65 |
} |
| 66 |
pending_interrupts ++; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
static void chld_proc(int signum) |
| 71 |
{ |
| 72 |
int32_t rc, btype = STP_EXIT; |
| 73 |
int chld_stat = 0; |
| 74 |
dbug(2, "chld_proc %d (%s)\n", signum, strsignal(signum)); |
| 75 |
pid_t pid = waitpid(-1, &chld_stat, WNOHANG); |
| 76 |
if (pid != target_pid) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if (chld_stat) { |
| 81 |
// our child exited with a non-zero status |
| 82 |
if (WIFSIGNALED(chld_stat)) { |
| 83 |
warn(_("Child process exited with signal %d (%s)\n"), |
| 84 |
WTERMSIG(chld_stat), strsignal(WTERMSIG(chld_stat))); |
| 85 |
target_pid_failed_p = 1; |
| 86 |
} |
| 87 |
if (WIFEXITED(chld_stat) && WEXITSTATUS(chld_stat)) { |
| 88 |
warn(_("Child process exited with status %d\n"), |
| 89 |
WEXITSTATUS(chld_stat)); |
| 90 |
target_pid_failed_p = 1; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
rc = write(control_channel, &btype, sizeof(btype)); // send STP_EXIT |
| 95 |
(void) rc; /* XXX: notused */ |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
static void setup_main_signals(void) |
| 100 |
{ |
| 101 |
struct sigaction sa; |
| 102 |
sigset_t *s = malloc(sizeof(*s)); |
| 103 |
if (!s) { |
| 104 |
_perr("malloc failed"); |
| 105 |
exit(1); |
| 106 |
} |
| 107 |
|
| 108 |
memset(&sa, 0, sizeof(sa)); |
| 109 |
/* select will report EINTR even when SA_RESTART is set. */ |
| 110 |
sa.sa_flags = SA_RESTART; |
| 111 |
sigfillset(&sa.sa_mask); |
| 112 |
|
| 113 |
/* This is to notify when our child process (-c) ends. */ |
| 114 |
sa.sa_handler = chld_proc; |
| 115 |
sigaction(SIGCHLD, &sa, NULL); |
| 116 |
|
| 117 |
if (monitor) |
| 118 |
{ |
| 119 |
sa.sa_handler = monitor_winch; |
| 120 |
sigaction(SIGWINCH, &sa, NULL); |
| 121 |
} |
| 122 |
|
| 123 |
// listen to these signals via general interrupt handler in whatever thread |
| 124 |
memset(&sa, 0, sizeof(sa)); |
| 125 |
sa.sa_flags = SA_RESTART; |
| 126 |
sigfillset(&sa.sa_mask); |
| 127 |
|
| 128 |
sa.sa_handler = interrupt_handler; |
| 129 |
sigaction(SIGINT, &sa, NULL); |
| 130 |
sigaction(SIGTERM, &sa, NULL); |
| 131 |
sigaction(SIGHUP, &sa, NULL); |
| 132 |
sigaction(SIGQUIT, &sa, NULL); |
| 133 |
|
| 134 |
// Formerly, we had a signal catching thread. |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* system_cmd() executes system commands in response |
| 140 |
* to an STP_SYSTEM message from the module. These |
| 141 |
* messages are sent by the system() systemtap function. |
| 142 |
*/ |
| 143 |
void system_cmd(char *cmd) |
| 144 |
{ |
| 145 |
pid_t child_pid; |
| 146 |
|
| 147 |
/* |
| 148 |
* This needs some explanation. This function is going to fork, |
| 149 |
* creating a child process. That child will close fds, then fork |
| 150 |
* again to create a grandchild process, which execs the user's |
| 151 |
* command. The original child immediately exits after the 2nd fork |
| 152 |
* succeeds. The original parent will wait on the child to close the |
| 153 |
* fds and spawn the actual command. |
| 154 |
* |
| 155 |
* We're not waiting on the command to finish, we're waiting for the |
| 156 |
* child to close all fds (and then fork). This avoids a race if we |
| 157 |
* immediately get an exit after the system_cmd() and they fight |
| 158 |
* over who has the control channel and/or relay fds open. |
| 159 |
*/ |
| 160 |
|
| 161 |
dbug(2, "system %s\n", cmd); |
| 162 |
if ((child_pid = fork()) < 0) { /* fork failed */ |
| 163 |
_perr("fork"); |
| 164 |
return; |
| 165 |
} else if (child_pid > 0) { /* parent (stapio) */ |
| 166 |
dbug(2, "waiting on %lu\n", (unsigned long)child_pid); |
| 167 |
(void)waitpid(child_pid, NULL, 0); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
/* The child will close all fds (like the control channel and relay |
| 172 |
* fds), then fork/exec cmd, creating a grandchild. */ |
| 173 |
pid_t grandchild_pid; |
| 174 |
closefrom(3); |
| 175 |
|
| 176 |
if ((grandchild_pid = fork()) < 0) { /* fork failed */ |
| 177 |
_perr("fork"); |
| 178 |
_exit(1); |
| 179 |
} else if (grandchild_pid > 0) { /* child */ |
| 180 |
dbug(2, "created %lu\n", (unsigned long)grandchild_pid); |
| 181 |
_exit(0); |
| 182 |
} |
| 183 |
|
| 184 |
/* The grandchild will now actually run the command. */ |
| 185 |
if (execlp("sh", "sh", "-c", cmd, NULL) < 0) |
| 186 |
perr("%s", cmd); |
| 187 |
_exit(1); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/** |
| 192 |
* init_stapio - initialize the app |
| 193 |
* @print_summary: boolean, print summary or not at end of run |
| 194 |
* |
| 195 |
* Returns 0 on success, negative otherwise. |
| 196 |
*/ |
| 197 |
int init_stapio(void) |
| 198 |
{ |
| 199 |
dbug(2, "init_stapio\n"); |
| 200 |
|
| 201 |
/* create control channel */ |
| 202 |
int rc = init_ctl_channel(modname, 1); |
| 203 |
if (rc < 0) { |
| 204 |
err(_("Failed to initialize control channel.\n")); |
| 205 |
return -1; |
| 206 |
} |
| 207 |
|
| 208 |
if (attach_mod) { |
| 209 |
dbug(2, "Attaching\n"); |
| 210 |
if (init_relayfs() < 0) { |
| 211 |
close_ctl_channel(); |
| 212 |
return -1; |
| 213 |
} |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
/* fork target_cmd if requested. */ |
| 218 |
/* It will not actually exec until signalled. */ |
| 219 |
if (target_cmd) |
| 220 |
start_cmd(); |
| 221 |
|
| 222 |
if (target_namespaces_pid > 0) |
| 223 |
dbug(2, "target_namespaces_pid=%d\n", target_namespaces_pid); |
| 224 |
|
| 225 |
/* Run in background */ |
| 226 |
if (daemon_mode) { |
| 227 |
pid_t pid; |
| 228 |
int ret; |
| 229 |
dbug(2, "daemonizing stapio\n"); |
| 230 |
|
| 231 |
/* daemonize */ |
| 232 |
ret = daemon(0, 1); /* don't close stdout at this time. */ |
| 233 |
if (ret) { |
| 234 |
err(_("Failed to daemonize stapio\n")); |
| 235 |
return -1; |
| 236 |
} |
| 237 |
|
| 238 |
/* change error messages to syslog. */ |
| 239 |
switch_syslog("stapio"); |
| 240 |
|
| 241 |
/* show new pid */ |
| 242 |
pid = getpid(); |
| 243 |
fprintf(stdout, "%d\n", pid); |
| 244 |
fflush(stdout); |
| 245 |
|
| 246 |
/* redirect all outputs to /dev/null */ |
| 247 |
ret = open("/dev/null", O_RDWR); |
| 248 |
if (ret < 0) { |
| 249 |
err(_("Failed to open /dev/null\n")); |
| 250 |
return -1; |
| 251 |
} |
| 252 |
close(STDIN_FILENO); |
| 253 |
close(STDOUT_FILENO); |
| 254 |
close(STDERR_FILENO); |
| 255 |
dup2(ret, STDOUT_FILENO); |
| 256 |
dup2(ret, STDERR_FILENO); |
| 257 |
close(ret); |
| 258 |
} |
| 259 |
|
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
/* cleanup_and_exit() closed channels, frees memory, |
| 264 |
* removes the module (if necessary) and exits. */ |
| 265 |
void cleanup_and_exit(int detach, int rc) |
| 266 |
{ |
| 267 |
static int exiting = 0; |
| 268 |
const char *staprun; |
| 269 |
pid_t pid; |
| 270 |
int rstatus; |
| 271 |
struct sigaction sa; |
| 272 |
|
| 273 |
if (monitor) |
| 274 |
monitor_cleanup(); |
| 275 |
|
| 276 |
if (read_stdin) |
| 277 |
read_stdin_cleanup(); |
| 278 |
|
| 279 |
if (exiting) |
| 280 |
return; |
| 281 |
exiting = 1; |
| 282 |
|
| 283 |
setup_main_signals(); |
| 284 |
|
| 285 |
dbug(1, "detach=%d\n", detach); |
| 286 |
|
| 287 |
/* NB: We don't really need to wait for child processes. Any that |
| 288 |
were started by the system() tapset function (system_cmd() above) |
| 289 |
can run loose. Or, a target_cmd (stap -c CMD) may have already started and |
| 290 |
stopped. */ |
| 291 |
|
| 292 |
/* OTOH, it may be still be running - but there's no need for |
| 293 |
us to wait for it, considering that the script must have exited |
| 294 |
for another reason. So, we no longer while(...wait()...); here. |
| 295 |
*/ |
| 296 |
|
| 297 |
if (pending_interrupts > 2) |
| 298 |
kill_relayfs(); |
| 299 |
else |
| 300 |
close_relayfs(); |
| 301 |
|
| 302 |
dbug(1, "closing control channel\n"); |
| 303 |
close_ctl_channel(); |
| 304 |
|
| 305 |
if (detach) { |
| 306 |
eprintf(_("\nDisconnecting from systemtap module.\n" "To reconnect, type \"staprun -A %s\"\n"), modname); |
| 307 |
my_exit(0); |
| 308 |
} |
| 309 |
else if (rename_mod) |
| 310 |
dbug(2, "\nRenamed module to: %s\n", modname); |
| 311 |
|
| 312 |
/* At this point, we're committed to calling staprun -d MODULE to |
| 313 |
* unload the thing and exit. */ |
| 314 |
/* Due to PR9788, we fork and exec the setuid staprun only in a child process. */ |
| 315 |
|
| 316 |
staprun = getenv ("SYSTEMTAP_STAPRUN") ?: BINDIR "/staprun"; |
| 317 |
dbug(2, "removing %s\n", modname); |
| 318 |
|
| 319 |
// So that waitpid() below will work correctly, we need to clear |
| 320 |
// out our SIGCHLD handler. |
| 321 |
memset(&sa, 0, sizeof(sa)); |
| 322 |
sigemptyset(&sa.sa_mask); |
| 323 |
sa.sa_flags = 0; |
| 324 |
sa.sa_handler = SIG_DFL; |
| 325 |
sigaction(SIGCHLD, &sa, NULL); |
| 326 |
|
| 327 |
if (target_mnt_ns_fd > 0) { |
| 328 |
if (close(target_mnt_ns_fd) != 0) { |
| 329 |
perror("close(target_mnt_ns_fd) failed"); |
| 330 |
/* nonfatal */ |
| 331 |
} else |
| 332 |
target_mnt_ns_fd = -1; |
| 333 |
} |
| 334 |
|
| 335 |
dbug(2, "forking\n"); |
| 336 |
|
| 337 |
pid = fork(); |
| 338 |
if (pid < 0) { |
| 339 |
_perr("fork"); |
| 340 |
my_exit(-1); |
| 341 |
} |
| 342 |
|
| 343 |
if (pid == 0) { /* child process */ |
| 344 |
/* Run the command. */ |
| 345 |
char *cmd; |
| 346 |
int rc = asprintf(&cmd, "%s %s %s -d -C %s '%s'", staprun, |
| 347 |
(verbose >= 1) ? "-v" : "", |
| 348 |
(verbose >= 2) ? "-v" : "", |
| 349 |
color_mode == color_always ? "always" |
| 350 |
: color_mode == color_auto ? "auto" : "never", |
| 351 |
modname); |
| 352 |
|
| 353 |
if (orig_mnt_ns_fd > 0) { |
| 354 |
/* Make sure we switch back to the orignal mount namespace |
| 355 |
* just in case the kernel module switches to a different |
| 356 |
* one. |
| 357 |
* Also, this must be done in the new child process since |
| 358 |
* the parent process is multi-threaded. */ |
| 359 |
|
| 360 |
#ifdef __NR_setns |
| 361 |
dbug(2, "restoring the orignal mount namespace\n"); |
| 362 |
|
| 363 |
if (syscall(__NR_setns, orig_mnt_ns_fd, CLONE_NEWNS) == -1) { |
| 364 |
perror("setns(orig_mnt_ns_fd) failed"); |
| 365 |
/* nonfatal; just the best effort */ |
| 366 |
} |
| 367 |
#endif |
| 368 |
|
| 369 |
if (close(orig_mnt_ns_fd) != 0) { |
| 370 |
perror("close(orig_mnt_ns_fd) failed"); |
| 371 |
/* nonfatal */ |
| 372 |
} else |
| 373 |
orig_mnt_ns_fd = -1; |
| 374 |
} |
| 375 |
|
| 376 |
if (rc >= 1) { |
| 377 |
execlp("sh", "sh", "-c", cmd, NULL); |
| 378 |
/* should not return */ |
| 379 |
perror(staprun); |
| 380 |
my_exit(-1); |
| 381 |
} else { |
| 382 |
perror("asprintf"); |
| 383 |
my_exit(-1); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
if (orig_mnt_ns_fd > 0) { |
| 388 |
if (close(orig_mnt_ns_fd) != 0) { |
| 389 |
perror("close(orig_mnt_ns_fd) failed"); |
| 390 |
/* nonfatal */ |
| 391 |
} else |
| 392 |
orig_mnt_ns_fd = -1; |
| 393 |
} |
| 394 |
|
| 395 |
/* parent process */ |
| 396 |
if (waitpid(pid, &rstatus, 0) < 0) { |
| 397 |
_perr("waitpid"); |
| 398 |
my_exit(-1); |
| 399 |
} |
| 400 |
|
| 401 |
if (WIFEXITED(rstatus)) { |
| 402 |
if(rc || target_pid_failed_p || rstatus) // if we have an error |
| 403 |
my_exit(1); |
| 404 |
else |
| 405 |
my_exit(0); //success |
| 406 |
} |
| 407 |
|
| 408 |
my_exit(-1); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
/** |
| 413 |
* stp_main_loop - loop forever reading data |
| 414 |
*/ |
| 415 |
|
| 416 |
int stp_main_loop(void) |
| 417 |
{ |
| 418 |
ssize_t nb; |
| 419 |
FILE *ofp = stdout; |
| 420 |
struct |
| 421 |
{ |
| 422 |
uint32_t type; |
| 423 |
union |
| 424 |
{ |
| 425 |
char data[8192]; |
| 426 |
struct _stp_msg_start start; |
| 427 |
struct _stp_msg_cmd cmd; |
| 428 |
struct _stp_msg_ns_pid nspid; |
| 429 |
struct _stp_msg_mnt_ns_fds nsfds; |
| 430 |
} payload; |
| 431 |
} recvbuf; |
| 432 |
int error_detected = 0; |
| 433 |
int select_supported; |
| 434 |
int flags; |
| 435 |
int res; |
| 436 |
int rc; |
| 437 |
int maxfd; |
| 438 |
struct timeval tv; |
| 439 |
struct timespec ts; |
| 440 |
struct timespec *timeout = NULL; |
| 441 |
fd_set fds; |
| 442 |
|
| 443 |
|
| 444 |
setvbuf(ofp, (char *)NULL, _IONBF, 0); |
| 445 |
setup_main_signals(); |
| 446 |
dbug(2, "in main loop\n"); |
| 447 |
|
| 448 |
rc = send_request(STP_READY, NULL, 0); |
| 449 |
if (rc != 0) { |
| 450 |
perror ("Unable to send STP_READY"); |
| 451 |
cleanup_and_exit(0, rc); |
| 452 |
/* NOTREACHED */ |
| 453 |
} |
| 454 |
|
| 455 |
flags = fcntl(control_channel, F_GETFL); |
| 456 |
|
| 457 |
/* Make select return immediately. We just check whether |
| 458 |
there is an exception available on the control_channel, |
| 459 |
which is how we know the module supports select. */ |
| 460 |
tv.tv_sec = 0; |
| 461 |
tv.tv_usec = 0; |
| 462 |
FD_ZERO(&fds); |
| 463 |
FD_SET(control_channel, &fds); |
| 464 |
res = select(control_channel + 1, NULL, NULL, &fds, &tv); |
| 465 |
select_supported = (res == 1 && FD_ISSET(control_channel, &fds)); |
| 466 |
dbug(2, "select_supported: %d\n", select_supported); |
| 467 |
|
| 468 |
if (monitor) |
| 469 |
monitor_setup(); |
| 470 |
|
| 471 |
if (read_stdin) |
| 472 |
read_stdin_setup(); |
| 473 |
|
| 474 |
/* In monitor mode, we must timeout pselect to poll the monitor |
| 475 |
interface. In non-monitor mode, we must timeout pselect so that |
| 476 |
we can handle pending_interrupts. */ |
| 477 |
ts.tv_sec = 0; |
| 478 |
ts.tv_nsec = 500*1000*1000; |
| 479 |
timeout = &ts; |
| 480 |
|
| 481 |
/* handle messages from control channel */ |
| 482 |
while (1) { |
| 483 |
if (monitor) |
| 484 |
{ |
| 485 |
monitor_input(); |
| 486 |
monitor_render(); |
| 487 |
} |
| 488 |
|
| 489 |
if (pending_interrupts) { |
| 490 |
int btype = STP_EXIT; |
| 491 |
int rc; |
| 492 |
|
| 493 |
/* If 'load_only' is set, we don't want to send STP_EXIT, |
| 494 |
* which would cause any 'probe end' processing to be |
| 495 |
* done. Instead, we'll just detach by calling |
| 496 |
* cleanup_and_exit(). This should let the module continue to |
| 497 |
* run. */ |
| 498 |
if (load_only) |
| 499 |
{ |
| 500 |
cleanup_and_exit(load_only /* = detach */, 0); |
| 501 |
/* NOTREACHED */ |
| 502 |
} |
| 503 |
|
| 504 |
rc = write(control_channel, &btype, sizeof(btype)); |
| 505 |
dbug(2, "signal-triggered %d exit rc %d\n", pending_interrupts, rc); |
| 506 |
if (monitor || (pending_interrupts > 2)) |
| 507 |
{ /* user mashing on ^C multiple times */ |
| 508 |
cleanup_and_exit (load_only /* = detach */, 0); |
| 509 |
/* NOTREACHED */ |
| 510 |
} |
| 511 |
else |
| 512 |
{} /* await STP_EXIT reply message to kill staprun */ |
| 513 |
} |
| 514 |
|
| 515 |
/* If the runtime does not implement select() on the command |
| 516 |
filehandle, we have to poll periodically. The polling interval can |
| 517 |
be relatively large, since we don't receive EAGAIN during the |
| 518 |
time-sensitive startup period (packets go back-to-back). */ |
| 519 |
|
| 520 |
flags |= O_NONBLOCK; |
| 521 |
fcntl(control_channel, F_SETFL, flags); |
| 522 |
nb = read(control_channel, &recvbuf, sizeof(recvbuf)); |
| 523 |
flags &= ~O_NONBLOCK; |
| 524 |
fcntl(control_channel, F_SETFL, flags); |
| 525 |
|
| 526 |
dbug(3, "nb=%ld\n", (long)nb); |
| 527 |
if (nb < (ssize_t) sizeof(recvbuf.type)) { |
| 528 |
if (nb >= 0 || (errno != EINTR && errno != EAGAIN)) { |
| 529 |
_perr(_("Unexpected EOF in read (nb=%ld)"), (long)nb); |
| 530 |
cleanup_and_exit(0, 1); |
| 531 |
/* NOTREACHED */ |
| 532 |
} |
| 533 |
|
| 534 |
if (!select_supported) { |
| 535 |
dbug(4, "sleeping\n"); |
| 536 |
usleep (250*1000); /* sleep 250ms between polls */ |
| 537 |
} else { |
| 538 |
FD_ZERO(&fds); |
| 539 |
FD_SET(control_channel, &fds); |
| 540 |
maxfd = control_channel; |
| 541 |
// Immediately update screen on input |
| 542 |
if (monitor) |
| 543 |
FD_SET(STDIN_FILENO, &fds); |
| 544 |
res = pselect(maxfd + 1, &fds, NULL, NULL, timeout, NULL); |
| 545 |
if (res < 0 && errno != EINTR) |
| 546 |
{ |
| 547 |
_perr(_("Unexpected error in select")); |
| 548 |
cleanup_and_exit(0, 1); |
| 549 |
/* NOTREACHED */ |
| 550 |
} |
| 551 |
} |
| 552 |
continue; |
| 553 |
} |
| 554 |
|
| 555 |
nb -= sizeof(recvbuf.type); |
| 556 |
PROBE3(staprun, recv__ctlmsg, recvbuf.type, recvbuf.payload.data, nb); |
| 557 |
|
| 558 |
switch (recvbuf.type) { |
| 559 |
case STP_OOB_DATA: |
| 560 |
/* Note that "WARNING:" should not be translated, since it is |
| 561 |
* part of the module cmd protocol. */ |
| 562 |
if (strncmp(recvbuf.payload.data, "WARNING: ", 9) == 0) { |
| 563 |
if (suppress_warnings) break; |
| 564 |
if (verbose) { /* don't eliminate duplicates */ |
| 565 |
if (monitor) |
| 566 |
monitor_remember_output_line (recvbuf.payload.data, nb); |
| 567 |
else |
| 568 |
/* trim "WARNING: " */ |
| 569 |
warn("%.*s", (int) nb-9, recvbuf.payload.data+9); |
| 570 |
break; |
| 571 |
} else { /* eliminate duplicates */ |
| 572 |
static void *seen = 0; |
| 573 |
static unsigned seen_count = 0; |
| 574 |
char *dupstr = strndup (recvbuf.payload.data, (int) nb); |
| 575 |
char *retval; |
| 576 |
|
| 577 |
if (! dupstr) { |
| 578 |
/* OOM, should not happen. */ |
| 579 |
if (monitor) |
| 580 |
monitor_remember_output_line (recvbuf.payload.data, nb); |
| 581 |
else |
| 582 |
/* trim "WARNING: " */ |
| 583 |
warn("%.*s", (int) nb-9, recvbuf.payload.data+9); |
| 584 |
break; |
| 585 |
} |
| 586 |
|
| 587 |
retval = tfind (dupstr, & seen, (int (*)(const void*, const void*))strcmp); |
| 588 |
if (! retval) { /* new message */ |
| 589 |
if (monitor) |
| 590 |
monitor_remember_output_line (recvbuf.payload.data, nb); |
| 591 |
else |
| 592 |
/* trim "WARNING: " */ |
| 593 |
warn("%.*s", strlen(dupstr)-9, dupstr+9); |
| 594 |
|
| 595 |
/* We set a maximum for stored warning messages, |
| 596 |
to prevent a misbehaving script/environment |
| 597 |
from emitting countless _stp_warn()s, and |
| 598 |
overflow staprun's memory. */ |
| 599 |
#define MAX_STORED_WARNINGS 1024 |
| 600 |
if (seen_count++ == MAX_STORED_WARNINGS) { |
| 601 |
eprintf(_("WARNING deduplication table full\n")); |
| 602 |
free (dupstr); |
| 603 |
} |
| 604 |
else if (seen_count > MAX_STORED_WARNINGS) { |
| 605 |
/* Be quiet in the future, but stop counting to |
| 606 |
preclude overflow. */ |
| 607 |
free (dupstr); |
| 608 |
seen_count = MAX_STORED_WARNINGS+1; |
| 609 |
} |
| 610 |
else if (seen_count < MAX_STORED_WARNINGS) { |
| 611 |
/* NB: don't free dupstr; it's going into the tree. */ |
| 612 |
retval = tsearch (dupstr, & seen, |
| 613 |
(int (*)(const void*, const void*))strcmp); |
| 614 |
if (retval == 0) { |
| 615 |
/* OOM, should not happen */ |
| 616 |
/* Next time we should get the 'full' message. */ |
| 617 |
free (dupstr); |
| 618 |
seen_count = MAX_STORED_WARNINGS; |
| 619 |
} |
| 620 |
} |
| 621 |
} else { /* old message */ |
| 622 |
free (dupstr); |
| 623 |
} |
| 624 |
} /* duplicate elimination */ |
| 625 |
/* Note that "ERROR:" should not be translated, since it is |
| 626 |
* part of the module cmd protocol. */ |
| 627 |
} else if (strncmp(recvbuf.payload.data, "ERROR: ", 7) == 0) { |
| 628 |
if (monitor) |
| 629 |
monitor_remember_output_line (recvbuf.payload.data, nb); |
| 630 |
else |
| 631 |
/* trim "ERROR: " */ |
| 632 |
err("%.*s", (int) nb-7, recvbuf.payload.data+7); |
| 633 |
error_detected = 1; |
| 634 |
} else { /* neither warning nor error */ |
| 635 |
if (monitor) |
| 636 |
monitor_remember_output_line (recvbuf.payload.data, nb); |
| 637 |
else |
| 638 |
eprintf("%.*s", (int) nb, recvbuf.payload.data); |
| 639 |
} |
| 640 |
break; |
| 641 |
case STP_EXIT: |
| 642 |
{ |
| 643 |
/* module asks us to unload it and exit */ |
| 644 |
dbug(2, "got STP_EXIT\n"); |
| 645 |
if (monitor) |
| 646 |
monitor_exited(); |
| 647 |
else { |
| 648 |
cleanup_and_exit(0, error_detected); |
| 649 |
/* NOTREACHED */ |
| 650 |
} |
| 651 |
/* monitor mode exit handled elsewhere, later. */ |
| 652 |
break; |
| 653 |
} |
| 654 |
case STP_REQUEST_EXIT: |
| 655 |
{ |
| 656 |
/* module asks us to start exiting, so send STP_EXIT */ |
| 657 |
dbug(2, "got STP_REQUEST_EXIT\n"); |
| 658 |
int32_t rc, btype = STP_EXIT; |
| 659 |
rc = write(control_channel, &btype, sizeof(btype)); |
| 660 |
(void) rc; /* XXX: notused */ |
| 661 |
break; |
| 662 |
} |
| 663 |
case STP_START: |
| 664 |
{ |
| 665 |
struct _stp_msg_start *t = &recvbuf.payload.start; |
| 666 |
dbug(2, "systemtap_module_init() returned %d\n", t->res); |
| 667 |
if (t->res) { |
| 668 |
if (target_cmd) |
| 669 |
kill(target_pid, SIGKILL); |
| 670 |
cleanup_and_exit(0, 1); |
| 671 |
/* NOTREACHED */ |
| 672 |
} else if (target_cmd) { |
| 673 |
dbug(1, "detaching pid %d\n", target_pid); |
| 674 |
int rc = resume_cmd(); |
| 675 |
if (rc < 0) |
| 676 |
cleanup_and_exit(0, 1); |
| 677 |
} |
| 678 |
break; |
| 679 |
} |
| 680 |
case STP_SYSTEM: |
| 681 |
{ |
| 682 |
struct _stp_msg_cmd *c = &recvbuf.payload.cmd; |
| 683 |
dbug(2, "STP_SYSTEM: %s\n", c->cmd); |
| 684 |
system_cmd(c->cmd); |
| 685 |
break; |
| 686 |
} |
| 687 |
case STP_NAMESPACES_PID: |
| 688 |
{ |
| 689 |
struct _stp_msg_ns_pid *nspid = &recvbuf.payload.nspid; |
| 690 |
dbug(2, "STP_NAMESPACES_PID: %d\n", nspid->target); |
| 691 |
break; |
| 692 |
} |
| 693 |
case STP_MNT_NS_FDS: |
| 694 |
{ |
| 695 |
struct _stp_msg_mnt_ns_fds *nsfds = &recvbuf.payload.nsfds; |
| 696 |
dbug(2, "STP_MNT_NS_FDS: target=%d orig=%d\n", nsfds->target_fd, nsfds->orig_fd); |
| 697 |
break; |
| 698 |
} |
| 699 |
case STP_TRANSPORT: |
| 700 |
{ |
| 701 |
struct _stp_msg_start ts; |
| 702 |
struct _stp_msg_ns_pid nspid; |
| 703 |
struct _stp_msg_mnt_ns_fds nsfds; |
| 704 |
if (init_relayfs() < 0) { |
| 705 |
cleanup_and_exit(0, 1); |
| 706 |
/* NOTREACHED */ |
| 707 |
} |
| 708 |
|
| 709 |
if (target_namespaces_pid > 0) { |
| 710 |
char buf[sizeof("/proc/4194304/ns/mnt")]; |
| 711 |
int fd; |
| 712 |
|
| 713 |
nspid.target = target_namespaces_pid; |
| 714 |
|
| 715 |
rc = send_request(STP_NAMESPACES_PID, &nspid, sizeof(nspid)); |
| 716 |
if (rc != 0) { |
| 717 |
perror ("Unable to send STP_NAMESPACES_PID"); |
| 718 |
cleanup_and_exit (0, rc); |
| 719 |
/* NOTREACHED */ |
| 720 |
} |
| 721 |
|
| 722 |
if (snprintf_chk(buf, sizeof(buf), "/proc/%d/ns/mnt", target_namespaces_pid) != 0) { |
| 723 |
perror ("Unable to construct the /proc/PID/ns/mnt file path"); |
| 724 |
cleanup_and_exit (0, rc); |
| 725 |
/* NOTREACHED */ |
| 726 |
} |
| 727 |
|
| 728 |
fd = open(buf, O_RDONLY); |
| 729 |
if (fd == -1) { |
| 730 |
fprintf (stderr, "Failed to open file '%s': %s\n", buf, |
| 731 |
strerror(errno)); |
| 732 |
cleanup_and_exit (0, 1); |
| 733 |
/* NOTREACHED */ |
| 734 |
} |
| 735 |
|
| 736 |
nsfds.target_fd = fd; |
| 737 |
target_mnt_ns_fd = fd; |
| 738 |
|
| 739 |
fd = open("/proc/self/ns/mnt", O_RDONLY); |
| 740 |
if (fd == -1) { |
| 741 |
close(nsfds.target_fd); |
| 742 |
fprintf (stderr, "Failed to open file '%s': %s\n", buf, |
| 743 |
strerror(errno)); |
| 744 |
cleanup_and_exit (0, 1); |
| 745 |
/* NOTREACHED */ |
| 746 |
} |
| 747 |
|
| 748 |
nsfds.orig_fd = fd; |
| 749 |
orig_mnt_ns_fd = fd; |
| 750 |
|
| 751 |
/* We will keep the fds open until cleanup_and_exit() is called. */ |
| 752 |
|
| 753 |
rc = send_request(STP_MNT_NS_FDS, &nsfds, sizeof(nsfds)); |
| 754 |
if (rc != 0) { |
| 755 |
perror ("Unable to send STP_MNT_NS_FDS"); |
| 756 |
cleanup_and_exit (0, rc); |
| 757 |
/* NOTREACHED */ |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
ts.target = target_pid; |
| 762 |
rc = send_request(STP_START, &ts, sizeof(ts)); |
| 763 |
if (rc != 0) { |
| 764 |
perror ("Unable to send STP_START"); |
| 765 |
cleanup_and_exit(0, rc); |
| 766 |
/* NOTREACHED */ |
| 767 |
} |
| 768 |
if (load_only) { |
| 769 |
cleanup_and_exit(1, 0); |
| 770 |
/* NOTREACHED */ |
| 771 |
} |
| 772 |
break; |
| 773 |
} |
| 774 |
default: |
| 775 |
warn(_("Ignored message of type %d\n"), recvbuf.type); |
| 776 |
} |
| 777 |
} |
| 778 |
fclose(ofp); |
| 779 |
return 0; |
| 780 |
} |