| 1 |
// stapsh - systemtap remote shell |
| 2 |
// Copyright (C) 2011 Red Hat Inc. |
| 3 |
// |
| 4 |
// This file is part of systemtap, and is free software. You can |
| 5 |
// redistribute it and/or modify it under the terms of the GNU General |
| 6 |
// Public License (GPL); either version 2, or (at your option) any |
| 7 |
// later version. |
| 8 |
// |
| 9 |
|
| 10 |
// stapsh implements a minimal protocol for a remote stap client to transfer a |
| 11 |
// systemtap module to a temporary location and invoke staprun on it. It is |
| 12 |
// not meant to be invoked directly by the user. Commands are simply |
| 13 |
// whitespace-delimited strings, terminated by newlines. |
| 14 |
// |
| 15 |
// command: stap VERSION |
| 16 |
// reply: stapsh VERSION MACHINE RELEASE |
| 17 |
// desc: This is the initial handshake. The VERSION exchange is intended |
| 18 |
// to facilitate compatibility checks, in case the protocol needs to |
| 19 |
// change. MACHINE and RELEASE are reported as given by uname. |
| 20 |
// |
| 21 |
// command: option OPTIONNAME |
| 22 |
// reply: OK / error message |
| 23 |
// desc: Enables the option OPTIONNAME. Introduced in v2.4. Currently |
| 24 |
// supported options are: |
| 25 |
// |
| 26 |
// data: Causes data output from staprun to be prefixed by a line |
| 27 |
// with the format "data stream size" where stream is either |
| 28 |
// "stdout" or "stderr" and size is the size of the data. It also |
| 29 |
// causes stapsh to send a "quit" message right before it leaves. |
| 30 |
// This is necessary in conditions where stap cannot detect the |
| 31 |
// state of stapsh from its pipes, such as when using a |
| 32 |
// virtio-serial port. |
| 33 |
// |
| 34 |
// verbose: Increases verbosity of debug statements. |
| 35 |
// |
| 36 |
// command: file SIZE NAME |
| 37 |
// DATA |
| 38 |
// reply: OK / error message |
| 39 |
// desc: Create a file of SIZE bytes, called NAME. The NAME is a basename |
| 40 |
// only, and limited to roughly "[a-z0-9][a-z0-9._]*". The DATA is |
| 41 |
// read as raw bytes following the command's newline. |
| 42 |
// |
| 43 |
// command: run ARG1 ARG2 ... |
| 44 |
// reply: OK / error message |
| 45 |
// desc: Start staprun with the given quoted-printable arguments. When |
| 46 |
// the child exits, stapsh will clean up and then exit with the same |
| 47 |
// return code. Note that whitespace has significance in stapsh |
| 48 |
// command parsing, all tabs, spaces, and newlines must be escaped |
| 49 |
// in the arguments. Any embedded NIL (=00) will truncate the |
| 50 |
// argument in the actual command invocation. |
| 51 |
// |
| 52 |
// command: quit |
| 53 |
// reply: (none) |
| 54 |
// desc: Signal the child process to quit, then cleanup and exit. |
| 55 |
// |
| 56 |
// If stapsh reaches EOF on its standard input, it will send SIGHUP to the |
| 57 |
// child process, wait for completion, then cleanup and exit normally. |
| 58 |
// Since v2.4, the program can also be run in listening mode, compatible with |
| 59 |
// QEMU's virtio-serial feature. In this mode, stapsh will listen for commands |
| 60 |
// on the port. Behaviour is otherwise the same. |
| 61 |
|
| 62 |
|
| 63 |
#include "../config.h" |
| 64 |
|
| 65 |
#include <ctype.h> |
| 66 |
#include <errno.h> |
| 67 |
#include <fcntl.h> |
| 68 |
#include <spawn.h> |
| 69 |
#include <stdio.h> |
| 70 |
#include <stdarg.h> |
| 71 |
#include <stdlib.h> |
| 72 |
#include <string.h> |
| 73 |
#include <unistd.h> |
| 74 |
|
| 75 |
#include <sys/stat.h> |
| 76 |
#include <sys/types.h> |
| 77 |
#include <sys/utsname.h> |
| 78 |
#include <sys/wait.h> |
| 79 |
|
| 80 |
#include <fcntl.h> |
| 81 |
#include <poll.h> |
| 82 |
|
| 83 |
#define STAPSH_TOK_DELIM " \t\r\n" |
| 84 |
#define STAPSH_MAX_FILE_SIZE 32000000 // XXX should be cumulative? |
| 85 |
#define STAPSH_MAX_ARGS 256 |
| 86 |
|
| 87 |
|
| 88 |
struct stapsh_handler { |
| 89 |
const char* name; |
| 90 |
int (*fn)(void); |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
static int do_hello(void); |
| 95 |
static int do_option(void); |
| 96 |
static int do_file(void); |
| 97 |
static int do_run(void); |
| 98 |
static int do_quit(void); |
| 99 |
|
| 100 |
static const int signals[] = { |
| 101 |
SIGHUP, SIGPIPE, SIGINT, SIGTERM, SIGCHLD |
| 102 |
}; |
| 103 |
|
| 104 |
static const struct stapsh_handler commands[] = { |
| 105 |
{ "stap", do_hello }, |
| 106 |
{ "option", do_option }, |
| 107 |
{ "file", do_file }, |
| 108 |
{ "run", do_run }, |
| 109 |
{ "quit", do_quit }, |
| 110 |
}; |
| 111 |
static const unsigned ncommands = sizeof(commands) / sizeof(*commands); |
| 112 |
|
| 113 |
static char tmpdir[FILENAME_MAX] = ""; |
| 114 |
|
| 115 |
static pid_t staprun_pid = -1; |
| 116 |
static int fd_staprun_out = -1; |
| 117 |
static int fd_staprun_err = -1; |
| 118 |
|
| 119 |
struct pollfd pfds[3]; |
| 120 |
#define PFD_STAP_OUT 0 |
| 121 |
#define PFD_STAPRUN_OUT 1 |
| 122 |
#define PFD_STAPRUN_ERR 2 |
| 123 |
|
| 124 |
static unsigned prefix_data = 0; |
| 125 |
static unsigned verbose = 0; |
| 126 |
static unsigned dyninst = 0; |
| 127 |
static unsigned bpf = 0; |
| 128 |
|
| 129 |
struct stapsh_option { |
| 130 |
const char* name; |
| 131 |
unsigned* var; |
| 132 |
}; |
| 133 |
|
| 134 |
static const struct stapsh_option options[] = { |
| 135 |
{ "verbose", &verbose }, |
| 136 |
{ "data", &prefix_data }, |
| 137 |
{ "dyninst", &dyninst }, |
| 138 |
{ "bpf", &bpf }, |
| 139 |
}; |
| 140 |
static const unsigned noptions = sizeof(options) / sizeof(*options); |
| 141 |
|
| 142 |
// if != NULL, then we're in listening mode |
| 143 |
static char *listening_port = NULL; |
| 144 |
static int listening_port_fd = -1; |
| 145 |
|
| 146 |
static struct utsname uts; |
| 147 |
|
| 148 |
static FILE *stapsh_in, *stapsh_out, *stapsh_err; |
| 149 |
|
| 150 |
static int sigio_enabled = 0; |
| 151 |
static int sigio_port_status = 0; |
| 152 |
|
| 153 |
// Returns nonzero if the host is connected. Used by any function/macro which |
| 154 |
// writes to the port. This is necessary because if the host is not connected, |
| 155 |
// a write() to the port will block. |
| 156 |
static int host_connected() |
| 157 |
{ |
| 158 |
if (listening_port == NULL || stapsh_out == stdout) |
| 159 |
return 1; |
| 160 |
if (sigio_enabled) |
| 161 |
return sigio_port_status; |
| 162 |
// Note that POLLHUP is useless there, it's just so that events != 0 |
| 163 |
struct pollfd p = { listening_port_fd, POLLHUP, 0 }; |
| 164 |
if (poll(&p, 1, 0) < 0) // if we can't poll, we don't know the real |
| 165 |
return 0; // status, so be safe and say we're not connected |
| 166 |
else // rc >= 0 // POLLHUP occurred (or we timed out == connected) |
| 167 |
return !(p.revents & POLLHUP); |
| 168 |
} |
| 169 |
|
| 170 |
#define dbug(level, format, args...) do { \ |
| 171 |
if (verbose >= level && host_connected()) \ |
| 172 |
fprintf (stapsh_err, "stapsh:%s:%d " format, \ |
| 173 |
__FUNCTION__, __LINE__, ## args); \ |
| 174 |
} while (0) |
| 175 |
|
| 176 |
#define vdbug(level, format, args) do { \ |
| 177 |
if (verbose >= level && host_connected()) { \ |
| 178 |
fprintf (stapsh_err, "stapsh:%s:%d ", __FUNCTION__, __LINE__); \ |
| 179 |
vfprintf (stapsh_err, format, args); \ |
| 180 |
} } while (0) |
| 181 |
|
| 182 |
#define die(format, args...) do { \ |
| 183 |
if (host_connected()) { \ |
| 184 |
fprintf (stapsh_err, "stapsh:%s:%d " format, \ |
| 185 |
__FUNCTION__, __LINE__, ## args); \ |
| 186 |
fprintf (stapsh_err, ": %s (%d)\n", strerror(errno), errno); } \ |
| 187 |
cleanup(2); } while (0) |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
// Send a reply back to the client on stdout |
| 192 |
static int __attribute__ ((format (printf, 1, 2))) |
| 193 |
reply(const char* format, ...) |
| 194 |
{ |
| 195 |
if (!host_connected()) |
| 196 |
return 1; |
| 197 |
va_list args, dbug_args; |
| 198 |
va_start (args, format); |
| 199 |
va_copy (dbug_args, args); |
| 200 |
vdbug (1, format, dbug_args); |
| 201 |
int ret = vfprintf (stapsh_out, format, args); |
| 202 |
fflush (stapsh_out); |
| 203 |
va_end (dbug_args); |
| 204 |
va_end (args); |
| 205 |
return ret; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
static void __attribute__ ((noreturn)) |
| 210 |
cleanup(int status) |
| 211 |
{ |
| 212 |
// Mask signals, so if called from non-signal context, we |
| 213 |
// won't get a reentry (especially for SIGCHLD). |
| 214 |
unsigned i; |
| 215 |
sigset_t mask; |
| 216 |
sigemptyset (&mask); |
| 217 |
for (i = 0; i < sizeof(signals) / sizeof(*signals); ++i) |
| 218 |
sigaddset (&mask, signals[i]); |
| 219 |
sigprocmask(SIG_BLOCK, &mask, 0); |
| 220 |
|
| 221 |
if (staprun_pid > 0) |
| 222 |
{ |
| 223 |
int rc, ret; |
| 224 |
kill(staprun_pid, SIGHUP); |
| 225 |
ret = waitpid(staprun_pid, &rc, 0); |
| 226 |
if (status == 0) |
| 227 |
{ |
| 228 |
if (ret == staprun_pid) |
| 229 |
status = WIFEXITED(rc) ? WEXITSTATUS(rc) : 128 + WTERMSIG(rc); |
| 230 |
else |
| 231 |
status = 2; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
if (tmpdir[0]) |
| 236 |
{ |
| 237 |
pid_t pid = 0; |
| 238 |
const char* argv[] = {"rm", "-rf", "--", tmpdir, NULL}; |
| 239 |
if (chdir("/")) {} // ignore failure, rm will probably work anyway |
| 240 |
if (!posix_spawnp(&pid, argv[0], NULL, NULL, |
| 241 |
(char* const*)argv, environ)) |
| 242 |
waitpid(pid, NULL, 0); |
| 243 |
} |
| 244 |
|
| 245 |
// Announce we're quitting if prefixing is on. Schemes that use the "data" |
| 246 |
// option (e.g. unix and libvirt) are not able to detect when stapsh exits and |
| 247 |
// thus need this announcement to close the connection. |
| 248 |
if (prefix_data) |
| 249 |
reply("quit\n"); |
| 250 |
|
| 251 |
if (listening_port) |
| 252 |
{ |
| 253 |
fclose(stapsh_in); |
| 254 |
fclose(stapsh_out); |
| 255 |
} |
| 256 |
|
| 257 |
exit(status); |
| 258 |
} |
| 259 |
|
| 260 |
static void |
| 261 |
handle_signal(int sig) |
| 262 |
{ |
| 263 |
dbug(1, "received signal %d: %s\n", sig, strsignal(sig)); |
| 264 |
cleanup(0); |
| 265 |
} |
| 266 |
|
| 267 |
static void |
| 268 |
sigio_handler(int sig) |
| 269 |
{ |
| 270 |
// sanity checks |
| 271 |
if (sig != SIGIO || listening_port_fd == -1) |
| 272 |
return; |
| 273 |
// Note that POLLHUP is useless there, it's just so that events != 0 |
| 274 |
struct pollfd pfd = { listening_port_fd, POLLHUP, 0 }; |
| 275 |
if (poll(&pfd, 1, 0) >= 0) |
| 276 |
// POLLHUP occurred (or we timed out == connected) |
| 277 |
sigio_port_status = !(pfd.revents & POLLHUP); |
| 278 |
// else (i.e. poll() failed), keep the same status |
| 279 |
} |
| 280 |
|
| 281 |
static int |
| 282 |
can_sigio(void) |
| 283 |
{ |
| 284 |
return strverscmp("2.6.37", uts.release) <= 0; |
| 285 |
} |
| 286 |
|
| 287 |
static void |
| 288 |
setup_signals (void) |
| 289 |
{ |
| 290 |
unsigned i; |
| 291 |
struct sigaction sa; |
| 292 |
memset(&sa, 0, sizeof(sa)); |
| 293 |
sa.sa_handler = handle_signal; |
| 294 |
sigemptyset (&sa.sa_mask); |
| 295 |
for (i = 0; i < sizeof(signals) / sizeof(*signals); ++i) |
| 296 |
sigaddset (&sa.sa_mask, signals[i]); |
| 297 |
for (i = 0; i < sizeof(signals) / sizeof(*signals); ++i) |
| 298 |
sigaction (signals[i], &sa, NULL); |
| 299 |
|
| 300 |
// catch SIGIO if kernel supports it |
| 301 |
if (listening_port && can_sigio()) |
| 302 |
{ |
| 303 |
memset(&sa, 0, sizeof(sa)); |
| 304 |
sa.sa_handler = sigio_handler; |
| 305 |
sigemptyset (&sa.sa_mask); |
| 306 |
sigaddset (&sa.sa_mask, SIGIO); |
| 307 |
sigaction (SIGIO, &sa, NULL); |
| 308 |
sigio_enabled = 1; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
static void __attribute__ ((noreturn)) |
| 313 |
usage (char *prog, int status) |
| 314 |
{ |
| 315 |
fprintf (stapsh_err, "%s [-v] [-l PORT]\n", prog); |
| 316 |
exit (status); |
| 317 |
} |
| 318 |
|
| 319 |
static void |
| 320 |
parse_args(int argc, char* const argv[]) |
| 321 |
{ |
| 322 |
int c; |
| 323 |
while ((c = getopt (argc, argv, "vl:")) != -1) |
| 324 |
switch (c) |
| 325 |
{ |
| 326 |
case 'v': |
| 327 |
++verbose; |
| 328 |
break; |
| 329 |
case 'l': |
| 330 |
listening_port = optarg; |
| 331 |
break; |
| 332 |
case '?': |
| 333 |
default: |
| 334 |
usage (argv[0], 2); |
| 335 |
} |
| 336 |
if (optind < argc) |
| 337 |
{ |
| 338 |
fprintf (stapsh_err, "%s: invalid extraneous arguments\n", argv[0]); |
| 339 |
usage (argv[0], 2); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
// Decode a quoted-printable string in-place |
| 345 |
static int |
| 346 |
qpdecode(char* s) |
| 347 |
{ |
| 348 |
char* o = s; |
| 349 |
while (*s) |
| 350 |
if (*s != '=') |
| 351 |
*o++ = *s++; |
| 352 |
else |
| 353 |
{ |
| 354 |
if (s[1] == '\r' || s[1] == '\n') |
| 355 |
s += 2; |
| 356 |
else if (s[1] == '\r' && s[2] == '\n') |
| 357 |
s += 3; |
| 358 |
else if (!s[1] || !s[2]) |
| 359 |
{ |
| 360 |
dbug(2, "truncated quoted-printable escape \"%s\"\n", s); |
| 361 |
return 1; |
| 362 |
} |
| 363 |
else |
| 364 |
{ |
| 365 |
errno = 0; |
| 366 |
char *end = 0, hex[] = { s[1], s[2], 0 }; |
| 367 |
unsigned char c = strtol(hex, &end, 16); |
| 368 |
if (errno || end != hex + 2) |
| 369 |
{ |
| 370 |
dbug(2, "invalid quoted-printable escape \"=%s\"\n", hex); |
| 371 |
return 1; |
| 372 |
} |
| 373 |
*o++ = c; |
| 374 |
s += 3; |
| 375 |
} |
| 376 |
} |
| 377 |
*o = '\0'; |
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
static int |
| 383 |
do_hello() |
| 384 |
{ |
| 385 |
if (staprun_pid > 0) |
| 386 |
return 1; |
| 387 |
|
| 388 |
// XXX check caller's version compatibility |
| 389 |
|
| 390 |
reply ("stapsh %s %s %s\n", VERSION, uts.machine, uts.release); |
| 391 |
return 0; |
| 392 |
} |
| 393 |
|
| 394 |
static int |
| 395 |
do_option() |
| 396 |
{ |
| 397 |
char *opt = strtok(NULL, STAPSH_TOK_DELIM); |
| 398 |
if (opt == NULL) |
| 399 |
return reply("ERROR: Could not parse option\n"); |
| 400 |
|
| 401 |
// find and affect option |
| 402 |
unsigned i; |
| 403 |
for (i = 0; i < noptions; ++i) |
| 404 |
if (strcmp(opt, options[i].name) == 0) |
| 405 |
{ |
| 406 |
dbug(2, "turning on option %s\n", opt); |
| 407 |
(*options[i].var)++; |
| 408 |
reply("OK\n"); |
| 409 |
return 0; |
| 410 |
} |
| 411 |
return reply("ERROR: Invalid option\n"); |
| 412 |
} |
| 413 |
|
| 414 |
static int |
| 415 |
do_file() |
| 416 |
{ |
| 417 |
if (staprun_pid > 0) |
| 418 |
return 1; |
| 419 |
|
| 420 |
int ret = 0; |
| 421 |
int size = -1; |
| 422 |
const char* arg = strtok(NULL, STAPSH_TOK_DELIM); |
| 423 |
if (arg) |
| 424 |
size = atoi(arg); |
| 425 |
if (size <= 0 || size > STAPSH_MAX_FILE_SIZE) |
| 426 |
return reply ("ERROR: Bad file size %d\n", size); |
| 427 |
|
| 428 |
const char* name = strtok(NULL, STAPSH_TOK_DELIM); |
| 429 |
if (!name) |
| 430 |
return reply ("ERROR: Missing file name\n"); |
| 431 |
for (arg = name; *arg; ++arg) |
| 432 |
{ |
| 433 |
if (dyninst && *arg != 's') |
| 434 |
dyninst--; |
| 435 |
if (bpf && *arg != 'b') |
| 436 |
bpf--; |
| 437 |
if (*arg == '.') |
| 438 |
{ |
| 439 |
dyninst += 2; |
| 440 |
bpf += 2; |
| 441 |
} |
| 442 |
if (!isalnum(*arg) && |
| 443 |
!(arg > name && (*arg == '.' || *arg == '_'))) |
| 444 |
return reply ("ERROR: Bad character '%c' in file name\n", *arg); |
| 445 |
} |
| 446 |
|
| 447 |
FILE* f = fopen(name, "w"); |
| 448 |
if (!f) |
| 449 |
return reply ("ERROR: Can't open file \"%s\" for writing\n", name); |
| 450 |
while (size > 0 && ret == 0) |
| 451 |
{ |
| 452 |
char buf[1024]; |
| 453 |
size_t r = sizeof(buf); |
| 454 |
if ((size_t)size < sizeof(buf)) |
| 455 |
r = size; |
| 456 |
r = fread(buf, 1, r, stapsh_in); |
| 457 |
if (!r && feof(stapsh_in)) |
| 458 |
ret = reply ("ERROR: Reached EOF while reading file data\n"); |
| 459 |
else if (!r) |
| 460 |
ret = reply ("ERROR: Unable to read file data\n"); |
| 461 |
else |
| 462 |
{ |
| 463 |
size -= r; |
| 464 |
|
| 465 |
const char* bufp = buf; |
| 466 |
while (bufp < buf + r && ret == 0) |
| 467 |
{ |
| 468 |
size_t w = (buf + r) - bufp; |
| 469 |
w = fwrite(bufp, 1, w, f); |
| 470 |
if (!w) |
| 471 |
ret = reply ("ERROR: Unable to write file data\n"); |
| 472 |
else |
| 473 |
bufp += w; |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
fclose(f); |
| 478 |
|
| 479 |
if (ret == 0) |
| 480 |
reply ("OK\n"); |
| 481 |
return ret; |
| 482 |
} |
| 483 |
|
| 484 |
// From util.cxx |
| 485 |
static int |
| 486 |
pipe_child_fd(posix_spawn_file_actions_t* fa, int pipefd[2], int childfd) |
| 487 |
{ |
| 488 |
if (pipe(pipefd)) |
| 489 |
return -1; |
| 490 |
|
| 491 |
int err = 0; |
| 492 |
int dir = childfd ? 1 : 0; |
| 493 |
if (!fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) && |
| 494 |
!fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) && |
| 495 |
!(err = posix_spawn_file_actions_adddup2(fa, pipefd[dir], childfd))) |
| 496 |
return 0; |
| 497 |
|
| 498 |
int olderrno = errno; |
| 499 |
close(pipefd[0]); |
| 500 |
close(pipefd[1]); |
| 501 |
return err ?: olderrno; |
| 502 |
} |
| 503 |
|
| 504 |
// based on stap_spawn_piped from util.cxx |
| 505 |
static pid_t |
| 506 |
spawn_staprun_piped(char** args) |
| 507 |
{ |
| 508 |
pid_t pid = -1; |
| 509 |
int outfd[2], errfd[2]; |
| 510 |
posix_spawn_file_actions_t fa; |
| 511 |
|
| 512 |
int err; |
| 513 |
if ((err = posix_spawn_file_actions_init(&fa)) != 0) |
| 514 |
{ |
| 515 |
reply("ERROR: Can't initialize posix_spawn actions: %s\n", strerror(err)); |
| 516 |
return -1; |
| 517 |
} |
| 518 |
if ((err = pipe_child_fd(&fa, outfd, 1)) != 0) |
| 519 |
{ |
| 520 |
reply("ERROR: Can't create pipe for stdout: %s\n", |
| 521 |
err > 0 ? strerror(err) : "pipe_child_fd"); |
| 522 |
goto cleanup_fa; |
| 523 |
} |
| 524 |
if ((err = pipe_child_fd(&fa, errfd, 2)) != 0) |
| 525 |
{ |
| 526 |
reply("ERROR: Can't create pipe for stderr: %s\n", |
| 527 |
err > 0 ? strerror(err) : "pipe_child_fd"); |
| 528 |
goto cleanup_out; |
| 529 |
} |
| 530 |
|
| 531 |
posix_spawn(&pid, args[0], &fa, NULL, args, environ); |
| 532 |
|
| 533 |
if (pid > 0) |
| 534 |
fd_staprun_err = errfd[0]; |
| 535 |
else |
| 536 |
close(errfd[0]); |
| 537 |
close(errfd[1]); |
| 538 |
|
| 539 |
cleanup_out: |
| 540 |
|
| 541 |
if (pid > 0) |
| 542 |
fd_staprun_out = outfd[0]; |
| 543 |
else |
| 544 |
close(outfd[0]); |
| 545 |
close(outfd[1]); |
| 546 |
|
| 547 |
cleanup_fa: |
| 548 |
|
| 549 |
posix_spawn_file_actions_destroy(&fa); |
| 550 |
return pid; |
| 551 |
} |
| 552 |
|
| 553 |
static pid_t |
| 554 |
spawn_staprun(char** args) |
| 555 |
{ |
| 556 |
pid_t pid = -1; |
| 557 |
posix_spawn_file_actions_t fa; |
| 558 |
|
| 559 |
int err; |
| 560 |
if ((err = posix_spawn_file_actions_init(&fa)) != 0) |
| 561 |
{ |
| 562 |
reply ("ERROR: Can't initialize posix_spawn actions: %s\n", strerror(err)); |
| 563 |
return -1; |
| 564 |
} |
| 565 |
|
| 566 |
// no stdin for staprun |
| 567 |
if ((err = posix_spawn_file_actions_addopen(&fa, 0, "/dev/null", O_RDONLY, 0)) != 0) |
| 568 |
{ |
| 569 |
reply("ERROR: Can't set posix_spawn actions: %s\n", strerror(err)); |
| 570 |
posix_spawn_file_actions_destroy(&fa); |
| 571 |
return -1; |
| 572 |
} |
| 573 |
|
| 574 |
if ((err = posix_spawn(&pid, args[0], &fa, NULL, args, environ)) != 0) |
| 575 |
{ |
| 576 |
reply("ERROR: Can't launch stap backend: %s\n", strerror(err)); |
| 577 |
posix_spawn_file_actions_destroy(&fa); |
| 578 |
return -1; |
| 579 |
} |
| 580 |
|
| 581 |
posix_spawn_file_actions_destroy(&fa); |
| 582 |
return pid; |
| 583 |
} |
| 584 |
|
| 585 |
static int |
| 586 |
do_run() |
| 587 |
{ |
| 588 |
if (staprun_pid > 0) |
| 589 |
return 1; |
| 590 |
|
| 591 |
char staprun[] = BINDIR "/staprun"; |
| 592 |
if (dyninst) |
| 593 |
strcpy(staprun, BINDIR "/stapdyn"); |
| 594 |
if (bpf) |
| 595 |
strcpy(staprun, BINDIR "/stapbpf"); |
| 596 |
char* args[STAPSH_MAX_ARGS + 1] = { staprun, 0 }; |
| 597 |
unsigned nargs = 1; |
| 598 |
|
| 599 |
char* arg; |
| 600 |
while ((arg = strtok(NULL, STAPSH_TOK_DELIM))) |
| 601 |
{ |
| 602 |
// check if we have space left |
| 603 |
// note that we have to keep at least one 0 at the end for posix_spawn |
| 604 |
if (nargs + 1 > STAPSH_MAX_ARGS) |
| 605 |
return reply ("ERROR: Too many arguments\n"); |
| 606 |
if (qpdecode(arg) != 0) |
| 607 |
return reply ("ERROR: Invalid encoding in argument \"%s\"\n", arg); |
| 608 |
args[nargs++] = arg; |
| 609 |
} |
| 610 |
|
| 611 |
dbug(1, "invoking %s\n", args[0]); |
| 612 |
|
| 613 |
// Explicitly check execute permissions here, because posix_spawn will only |
| 614 |
// report that failure through a process exit code. |
| 615 |
if (access(staprun, X_OK) != 0) |
| 616 |
return reply ("ERROR: Can't execute %s (%s)\n", staprun, strerror(errno)); |
| 617 |
|
| 618 |
// We pipe staprun under two conditions: |
| 619 |
// 1. The "data" option is on: we need to prefix all the output from staprun with data headers |
| 620 |
// 2. We're in listening mode: staprun needs to use the same port for |
| 621 |
// writing, but would fail to open it since stapsh is already using it |
| 622 |
pid_t pid; |
| 623 |
if (prefix_data || listening_port != NULL) |
| 624 |
pid = spawn_staprun_piped(args); |
| 625 |
else |
| 626 |
pid = spawn_staprun(args); |
| 627 |
|
| 628 |
if (pid <= 0) |
| 629 |
{ |
| 630 |
staprun_pid = fd_staprun_out = fd_staprun_err = -1; |
| 631 |
return reply ("ERROR: Failed to spawn staprun\n"); |
| 632 |
} |
| 633 |
|
| 634 |
if (prefix_data || listening_port != NULL) |
| 635 |
{ |
| 636 |
// make staprun fds non-blocking and prep polling array |
| 637 |
int flags; |
| 638 |
if ((flags = fcntl(fd_staprun_out, F_GETFL)) == -1 |
| 639 |
|| fcntl(fd_staprun_out, F_SETFL, flags | O_NONBLOCK) == -1 |
| 640 |
||(flags = fcntl(fd_staprun_err, F_GETFL)) == -1 |
| 641 |
|| fcntl(fd_staprun_err, F_SETFL, flags | O_NONBLOCK) == -1) |
| 642 |
return reply ("ERROR: Call to fcntl() failed"); |
| 643 |
|
| 644 |
pfds[PFD_STAPRUN_OUT].fd = fd_staprun_out; |
| 645 |
pfds[PFD_STAPRUN_OUT].events = POLLIN; |
| 646 |
pfds[PFD_STAPRUN_ERR].fd = fd_staprun_err; |
| 647 |
pfds[PFD_STAPRUN_ERR].events = POLLIN; |
| 648 |
} |
| 649 |
|
| 650 |
staprun_pid = pid; |
| 651 |
reply ("OK\n"); |
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
static int |
| 656 |
do_quit() |
| 657 |
{ |
| 658 |
cleanup(0); |
| 659 |
} |
| 660 |
|
| 661 |
static void |
| 662 |
process_command(void) |
| 663 |
{ |
| 664 |
static int no_cmd_yet = 1; |
| 665 |
char command[4096]; |
| 666 |
|
| 667 |
// This could technically block if only a partial command is received, but |
| 668 |
// stap commands are short and always end in \n. Even if we do block it's not |
| 669 |
// so bad a thing. |
| 670 |
if (fgets(command, sizeof(command), stapsh_in) == NULL) |
| 671 |
{ |
| 672 |
if (feof(stapsh_in)) // no more stap commands coming |
| 673 |
pfds[PFD_STAP_OUT].events = 0; |
| 674 |
return; |
| 675 |
} |
| 676 |
|
| 677 |
dbug(1, "command: %s", command); |
| 678 |
const char* arg = strtok(command, STAPSH_TOK_DELIM) ?: "(null)"; |
| 679 |
|
| 680 |
// If this is the first command and we're in listening mode, the only |
| 681 |
// acceptable command is "stap". This is necessary because there can be |
| 682 |
// data leftover in the buffer from a previous session |
| 683 |
if (no_cmd_yet && listening_port != NULL && strcmp(arg, "stap") != 0) |
| 684 |
return; |
| 685 |
no_cmd_yet = 0; |
| 686 |
|
| 687 |
unsigned i; |
| 688 |
for (i = 0; i < ncommands; ++i) |
| 689 |
if (strcmp(arg, commands[i].name) == 0) |
| 690 |
{ |
| 691 |
int rc = commands[i].fn(); |
| 692 |
if (rc) |
| 693 |
dbug(2, "failed command %s, rc=%d\n", arg, rc); |
| 694 |
break; |
| 695 |
} |
| 696 |
|
| 697 |
if (i >= ncommands) |
| 698 |
dbug(2, "invalid command %s\n", arg); |
| 699 |
} |
| 700 |
|
| 701 |
static void |
| 702 |
prefix_staprun(int i, FILE *out, const char *stream) |
| 703 |
{ |
| 704 |
char buf[4096]; |
| 705 |
ssize_t n = read(pfds[i].fd, buf, sizeof buf); |
| 706 |
if (n > 0) |
| 707 |
{ |
| 708 |
// actually check if we need to prefix data (we could also be piping for |
| 709 |
// other reasons, e.g. listening_mode != NULL) |
| 710 |
if (prefix_data) |
| 711 |
fprintf(out, "data %s %zd\n", stream, n); |
| 712 |
if (fwrite(buf, n, 1, out) != 1) |
| 713 |
dbug(2, "failed fwrite\n"); // appease older gccs (don't ignore fwrite rc) |
| 714 |
fflush(out); |
| 715 |
} |
| 716 |
else if (n == 0) // eof |
| 717 |
pfds[i].events = 0; |
| 718 |
} |
| 719 |
|
| 720 |
int |
| 721 |
main(int argc, char* const argv[]) |
| 722 |
{ |
| 723 |
// set those right away so that *print* statements don't fail |
| 724 |
stapsh_in = stdin; |
| 725 |
stapsh_out = stdout; |
| 726 |
stapsh_err = stderr; |
| 727 |
|
| 728 |
parse_args(argc, argv); |
| 729 |
|
| 730 |
// we're gonna need this for setup_signals so let's do it now |
| 731 |
if (uname(&uts)) |
| 732 |
die("Error calling uname"); |
| 733 |
|
| 734 |
setup_signals(); |
| 735 |
|
| 736 |
if (listening_port != NULL) |
| 737 |
{ |
| 738 |
listening_port_fd = open(listening_port, O_RDWR); |
| 739 |
if (listening_port_fd == -1) |
| 740 |
// no one might be watching but might as well print |
| 741 |
die("Error calling open()"); |
| 742 |
|
| 743 |
if (can_sigio()) |
| 744 |
{ |
| 745 |
// make ourselves the recipient of SIGIO signals for this port |
| 746 |
if (fcntl(listening_port_fd, F_SETOWN, getpid()) < 0) |
| 747 |
die("Error calling fcntl F_SETOWN"); |
| 748 |
|
| 749 |
// add O_ASYNC flag to enable SIGIO notifications on this port |
| 750 |
int flags = fcntl(listening_port_fd, F_GETFL); |
| 751 |
if (flags == -1) |
| 752 |
die("Error calling fcntl F_GETFL"); |
| 753 |
if (fcntl(listening_port_fd, F_SETFL, flags | O_ASYNC) == -1) |
| 754 |
die("Error calling fcntl F_SETFL"); |
| 755 |
} |
| 756 |
|
| 757 |
// We need to have different FILE* for each direction, otherwise |
| 758 |
// we'll have trouble down the line due to stdio's buffering |
| 759 |
// (doing "r+" on sockets is very wonky) |
| 760 |
stapsh_in = fdopen(listening_port_fd, "r"); |
| 761 |
stapsh_out = fdopen(listening_port_fd, "w"); |
| 762 |
stapsh_err = stapsh_out; |
| 763 |
if (!stapsh_in || !stapsh_out) |
| 764 |
die("Could not open serial port"); |
| 765 |
} |
| 766 |
|
| 767 |
umask(0077); |
| 768 |
snprintf(tmpdir, sizeof(tmpdir), "%s/stapsh.XXXXXX", |
| 769 |
getenv("TMPDIR") ?: "/tmp"); |
| 770 |
if (!mkdtemp(tmpdir)) |
| 771 |
die ("Can't make a temporary working directory"); |
| 772 |
if (chdir(tmpdir)) |
| 773 |
die ("Can't change to temporary working directory \"%s\"", tmpdir); |
| 774 |
|
| 775 |
// Prep pfds. For now we're only interested in commands from stap, and we |
| 776 |
// don't poll staprun until it is started. |
| 777 |
pfds[PFD_STAP_OUT].fd = fileno(stapsh_in); |
| 778 |
pfds[PFD_STAP_OUT].events = POLLIN; |
| 779 |
pfds[PFD_STAPRUN_OUT].events = 0; |
| 780 |
pfds[PFD_STAPRUN_ERR].events = 0; |
| 781 |
|
| 782 |
// Wait until a host connects before entering the command loop or poll() will |
| 783 |
// return with POLLHUP right away. Note that sleep() will be interrupted upon |
| 784 |
// SIGIO. |
| 785 |
if (listening_port != NULL) |
| 786 |
while (!host_connected()) |
| 787 |
sleep(2); // Once we support only platforms with guaranteed SIGIO support, |
| 788 |
// we could replace this with a pause(). |
| 789 |
|
| 790 |
// keep polling as long as we're listening for stap commands |
| 791 |
while (pfds[PFD_STAP_OUT].events) |
| 792 |
{ |
| 793 |
if (poll(pfds, 3, -1) < 0) |
| 794 |
{ |
| 795 |
if (errno == EINTR) |
| 796 |
continue; // go back to poll() |
| 797 |
else |
| 798 |
die ("poll() failed with critical error"); |
| 799 |
} |
| 800 |
if (pfds[PFD_STAP_OUT].revents & POLLHUP) |
| 801 |
break; |
| 802 |
if (pfds[PFD_STAP_OUT].revents & POLLIN) |
| 803 |
process_command(); |
| 804 |
if (pfds[PFD_STAPRUN_OUT].revents & POLLIN) |
| 805 |
prefix_staprun(PFD_STAPRUN_OUT, stapsh_out, "stdout"); |
| 806 |
if (pfds[PFD_STAPRUN_ERR].revents & POLLIN) |
| 807 |
prefix_staprun(PFD_STAPRUN_ERR, stapsh_err, "stderr"); |
| 808 |
} |
| 809 |
|
| 810 |
cleanup(0); |
| 811 |
} |
| 812 |
|
| 813 |
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |