process_unix.c (18846B)
1 /* Copyright (c) 2003, Roger Dingledine 2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. 3 * Copyright (c) 2007-2021, The Tor Project, Inc. */ 4 /* See LICENSE for licensing information */ 5 6 /** 7 * \file process_unix.c 8 * \brief Module for working with Unix processes. 9 **/ 10 11 #define PROCESS_UNIX_PRIVATE 12 #include "lib/intmath/cmp.h" 13 #include "lib/buf/buffers.h" 14 #include "lib/net/buffers_net.h" 15 #include "lib/container/smartlist.h" 16 #include "lib/evloop/compat_libevent.h" 17 #include "lib/log/log.h" 18 #include "lib/log/util_bug.h" 19 #include "lib/process/process.h" 20 #include "lib/process/process_unix.h" 21 #include "lib/process/waitpid.h" 22 #include "lib/process/env.h" 23 24 #include <stdio.h> 25 26 #ifdef HAVE_STRING_H 27 #include <string.h> 28 #endif 29 30 #ifdef HAVE_ERRNO_H 31 #include <errno.h> 32 #endif 33 34 #ifdef HAVE_UNISTD_H 35 #include <unistd.h> 36 #endif 37 38 #ifdef HAVE_FCNTL_H 39 #include <fcntl.h> 40 #endif 41 42 #if defined(HAVE_SYS_PRCTL_H) && defined(__linux__) 43 #include <sys/prctl.h> 44 #endif 45 46 #if HAVE_SIGNAL_H 47 #include <signal.h> 48 #endif 49 50 #ifndef _WIN32 51 52 #include "lib/fdio/fdio.h" 53 54 /** Internal state for Unix handles. */ 55 struct process_unix_handle_t { 56 /** Unix File Descriptor. */ 57 int fd; 58 59 /** Have we reached end of file? */ 60 bool reached_eof; 61 62 /** Event structure for libevent. */ 63 struct event *event; 64 65 /** Are we writing? */ 66 bool is_writing; 67 }; 68 69 /** Internal state for our Unix process. */ 70 struct process_unix_t { 71 /** Standard in handle. */ 72 process_unix_handle_t stdin_handle; 73 74 /** Standard out handle. */ 75 process_unix_handle_t stdout_handle; 76 77 /** Standard error handle. */ 78 process_unix_handle_t stderr_handle; 79 80 /** The process identifier of our process. */ 81 pid_t pid; 82 83 /** Waitpid Callback structure. */ 84 waitpid_callback_t *waitpid; 85 }; 86 87 /** Returns a newly allocated <b>process_unix_t</b>. */ 88 process_unix_t * 89 process_unix_new(void) 90 { 91 process_unix_t *unix_process; 92 unix_process = tor_malloc_zero(sizeof(process_unix_t)); 93 94 unix_process->stdin_handle.fd = -1; 95 unix_process->stderr_handle.fd = -1; 96 unix_process->stdout_handle.fd = -1; 97 98 return unix_process; 99 } 100 101 /** Deallocates the given <b>unix_process</b>. */ 102 void 103 process_unix_free_(process_unix_t *unix_process) 104 { 105 if (! unix_process) 106 return; 107 108 /* Clean up our waitpid callback. */ 109 clear_waitpid_callback(unix_process->waitpid); 110 111 /* FIXME(ahf): Refactor waitpid code? */ 112 unix_process->waitpid = NULL; 113 114 /* Close all our file descriptors. */ 115 process_unix_close_file_descriptors(unix_process); 116 117 tor_event_free(unix_process->stdout_handle.event); 118 tor_event_free(unix_process->stderr_handle.event); 119 tor_event_free(unix_process->stdin_handle.event); 120 121 tor_free(unix_process); 122 } 123 124 /** Executes the given process as a child process of Tor. This function is 125 * responsible for setting up the child process and run it. This includes 126 * setting up pipes for interprocess communication, initialize the waitpid 127 * callbacks, and finally run fork() followed by execve(). Returns 128 * <b>PROCESS_STATUS_RUNNING</b> upon success. */ 129 process_status_t 130 process_unix_exec(process_t *process) 131 { 132 process_unix_t *unix_process; 133 pid_t pid; 134 int stdin_pipe[2]; 135 int stdout_pipe[2]; 136 int stderr_pipe[2]; 137 int retval; 138 139 unix_process = process_get_unix_process(process); 140 141 /* Create standard in pipe. */ 142 retval = tor_pipe_cloexec(stdin_pipe); 143 144 if (-1 == retval) { 145 log_warn(LD_PROCESS, 146 "Unable to create pipe for stdin " 147 "communication with process: %s", 148 strerror(errno)); 149 150 return PROCESS_STATUS_ERROR; 151 } 152 153 /* Create standard out pipe. */ 154 retval = tor_pipe_cloexec(stdout_pipe); 155 156 if (-1 == retval) { 157 log_warn(LD_PROCESS, 158 "Unable to create pipe for stdout " 159 "communication with process: %s", 160 strerror(errno)); 161 162 /** Cleanup standard in pipe. */ 163 close(stdin_pipe[0]); 164 close(stdin_pipe[1]); 165 166 return PROCESS_STATUS_ERROR; 167 } 168 169 /* Create standard error pipe. */ 170 retval = tor_pipe_cloexec(stderr_pipe); 171 172 if (-1 == retval) { 173 log_warn(LD_PROCESS, 174 "Unable to create pipe for stderr " 175 "communication with process: %s", 176 strerror(errno)); 177 178 /** Cleanup standard in pipe. */ 179 close(stdin_pipe[0]); 180 close(stdin_pipe[1]); 181 182 /** Cleanup standard out pipe. */ 183 close(stdout_pipe[0]); 184 close(stdout_pipe[1]); 185 186 return PROCESS_STATUS_ERROR; 187 } 188 189 pid = fork(); 190 191 if (0 == pid) { 192 /* This code is running in the child process context. */ 193 194 #if defined(HAVE_SYS_PRCTL_H) && defined(__linux__) 195 /* Attempt to have the kernel issue a SIGTERM if the parent 196 * goes away. Certain attributes of the binary being execve()ed 197 * will clear this during the execve() call, but it's better 198 * than nothing. 199 */ 200 prctl(PR_SET_PDEATHSIG, SIGTERM); 201 #endif /* defined(HAVE_SYS_PRCTL_H) && defined(__linux__) */ 202 203 /* Link process stdout to the write end of the pipe. */ 204 retval = dup2(stdout_pipe[1], STDOUT_FILENO); 205 if (-1 == retval) 206 goto error; 207 208 /* Link process stderr to the write end of the pipe. */ 209 retval = dup2(stderr_pipe[1], STDERR_FILENO); 210 if (-1 == retval) 211 goto error; 212 213 /* Link process stdin to the read end of the pipe */ 214 retval = dup2(stdin_pipe[0], STDIN_FILENO); 215 if (-1 == retval) 216 goto error; 217 218 /* Close our pipes now after they have been dup2()'ed. */ 219 close(stderr_pipe[0]); 220 close(stderr_pipe[1]); 221 close(stdout_pipe[0]); 222 close(stdout_pipe[1]); 223 close(stdin_pipe[0]); 224 close(stdin_pipe[1]); 225 226 /* Note that we don't close all FDs from here, which we used to do, because 227 * all our open are CLOEXEC. With a very large maximum number of FDs, the 228 * loop was taking a long time: #40990 */ 229 230 /* Create the argv value for our new process. */ 231 char **argv = process_get_argv(process); 232 233 /* Create the env value for our new process. */ 234 process_environment_t *env = process_get_environment(process); 235 236 /* Call the requested program. */ 237 execve(argv[0], argv, env->unixoid_environment_block); 238 239 /* If we made it here it is because execve failed :-( */ 240 tor_free(argv); 241 process_environment_free(env); 242 243 error: 244 fprintf(stderr, "Error from child process: %s", strerror(errno)); 245 _exit(1); 246 } 247 248 /* We are in the parent process. */ 249 if (-1 == pid) { 250 log_warn(LD_PROCESS, 251 "Failed to create child process: %s", strerror(errno)); 252 253 /** Cleanup standard in pipe. */ 254 close(stdin_pipe[0]); 255 close(stdin_pipe[1]); 256 257 /** Cleanup standard out pipe. */ 258 close(stdout_pipe[0]); 259 close(stdout_pipe[1]); 260 261 /** Cleanup standard error pipe. */ 262 close(stderr_pipe[0]); 263 close(stderr_pipe[1]); 264 265 return PROCESS_STATUS_ERROR; 266 } 267 268 /* Register our PID. */ 269 unix_process->pid = pid; 270 271 /* Setup waitpid callbacks. */ 272 unix_process->waitpid = set_waitpid_callback(pid, 273 process_unix_waitpid_callback, 274 process); 275 276 /* Handle standard out. */ 277 unix_process->stdout_handle.fd = stdout_pipe[0]; 278 retval = close(stdout_pipe[1]); 279 280 if (-1 == retval) { 281 log_warn(LD_PROCESS, "Failed to close write end of standard out pipe: %s", 282 strerror(errno)); 283 } 284 285 /* Handle standard error. */ 286 unix_process->stderr_handle.fd = stderr_pipe[0]; 287 retval = close(stderr_pipe[1]); 288 289 if (-1 == retval) { 290 log_warn(LD_PROCESS, 291 "Failed to close write end of standard error pipe: %s", 292 strerror(errno)); 293 } 294 295 /* Handle standard in. */ 296 unix_process->stdin_handle.fd = stdin_pipe[1]; 297 retval = close(stdin_pipe[0]); 298 299 if (-1 == retval) { 300 log_warn(LD_PROCESS, "Failed to close read end of standard in pipe: %s", 301 strerror(errno)); 302 } 303 304 /* Setup our handles. */ 305 process_unix_setup_handle(process, 306 &unix_process->stdout_handle, 307 EV_READ|EV_PERSIST, 308 stdout_read_callback); 309 310 process_unix_setup_handle(process, 311 &unix_process->stderr_handle, 312 EV_READ|EV_PERSIST, 313 stderr_read_callback); 314 315 process_unix_setup_handle(process, 316 &unix_process->stdin_handle, 317 EV_WRITE|EV_PERSIST, 318 stdin_write_callback); 319 320 /* Start reading from standard out and standard error. */ 321 process_unix_start_reading(&unix_process->stdout_handle); 322 process_unix_start_reading(&unix_process->stderr_handle); 323 324 return PROCESS_STATUS_RUNNING; 325 } 326 327 /** Terminate the given process. Returns true on success, otherwise false. */ 328 bool 329 process_unix_terminate(process_t *process) 330 { 331 tor_assert(process); 332 333 process_unix_t *unix_process = process_get_unix_process(process); 334 335 /* All running processes should have a waitpid. */ 336 if (BUG(unix_process->waitpid == NULL)) 337 return false; 338 339 bool success = true; 340 341 /* Send a SIGTERM to our child process. */ 342 int ret; 343 344 ret = kill(unix_process->pid, SIGTERM); 345 346 if (ret == -1) { 347 log_warn(LD_PROCESS, "Unable to terminate process: %s", 348 strerror(errno)); 349 success = false; 350 } 351 352 /* Close all our FD's. */ 353 if (! process_unix_close_file_descriptors(unix_process)) 354 success = false; 355 356 return success; 357 } 358 359 /** Returns the unique process identifier for the given <b>process</b>. */ 360 process_pid_t 361 process_unix_get_pid(process_t *process) 362 { 363 tor_assert(process); 364 365 process_unix_t *unix_process = process_get_unix_process(process); 366 return (process_pid_t)unix_process->pid; 367 } 368 369 /** Write the given <b>buffer</b> as input to the given <b>process</b>'s 370 * standard input. Returns the number of bytes written. */ 371 int 372 process_unix_write(process_t *process, buf_t *buffer) 373 { 374 tor_assert(process); 375 tor_assert(buffer); 376 377 process_unix_t *unix_process = process_get_unix_process(process); 378 379 size_t buffer_flush_len = buf_datalen(buffer); 380 const size_t max_to_write = MIN(PROCESS_MAX_WRITE, buffer_flush_len); 381 382 /* If we have data to write (when buffer_flush_len > 0) and we are not 383 * currently getting file descriptor events from the kernel, we tell the 384 * kernel to start notifying us about when we can write to our file 385 * descriptor and return. */ 386 if (buffer_flush_len > 0 && ! unix_process->stdin_handle.is_writing) { 387 process_unix_start_writing(&unix_process->stdin_handle); 388 return 0; 389 } 390 391 /* We don't have any data to write, but the kernel is currently notifying us 392 * about whether we are able to write or not. Tell the kernel to stop 393 * notifying us until we have data to write. */ 394 if (buffer_flush_len == 0 && unix_process->stdin_handle.is_writing) { 395 process_unix_stop_writing(&unix_process->stdin_handle); 396 return 0; 397 } 398 399 /* We have data to write and the kernel have told us to write it. */ 400 return buf_flush_to_pipe(buffer, 401 process_get_unix_process(process)->stdin_handle.fd, 402 max_to_write); 403 } 404 405 /** Read data from the given process's standard output and put it into 406 * <b>buffer</b>. Returns the number of bytes read. */ 407 int 408 process_unix_read_stdout(process_t *process, buf_t *buffer) 409 { 410 tor_assert(process); 411 tor_assert(buffer); 412 413 process_unix_t *unix_process = process_get_unix_process(process); 414 415 return process_unix_read_handle(process, 416 &unix_process->stdout_handle, 417 buffer); 418 } 419 420 /** Read data from the given process's standard error and put it into 421 * <b>buffer</b>. Returns the number of bytes read. */ 422 int 423 process_unix_read_stderr(process_t *process, buf_t *buffer) 424 { 425 tor_assert(process); 426 tor_assert(buffer); 427 428 process_unix_t *unix_process = process_get_unix_process(process); 429 430 return process_unix_read_handle(process, 431 &unix_process->stderr_handle, 432 buffer); 433 } 434 435 /** This function is called whenever libevent thinks we have data that could be 436 * read from the child process's standard output. We notify the Process 437 * subsystem, which is then responsible for calling back to us for doing the 438 * actual reading of the data. */ 439 STATIC void 440 stdout_read_callback(evutil_socket_t fd, short event, void *data) 441 { 442 (void)fd; 443 (void)event; 444 445 process_t *process = data; 446 tor_assert(process); 447 448 process_notify_event_stdout(process); 449 } 450 451 /** This function is called whenever libevent thinks we have data that could be 452 * read from the child process's standard error. We notify the Process 453 * subsystem, which is then responsible for calling back to us for doing the 454 * actual reading of the data. */ 455 STATIC void 456 stderr_read_callback(evutil_socket_t fd, short event, void *data) 457 { 458 (void)fd; 459 (void)event; 460 461 process_t *process = data; 462 tor_assert(process); 463 464 process_notify_event_stderr(process); 465 } 466 467 /** This function is called whenever libevent thinks we have data that could be 468 * written the child process's standard input. We notify the Process subsystem, 469 * which is then responsible for calling back to us for doing the actual write 470 * of the data. */ 471 STATIC void 472 stdin_write_callback(evutil_socket_t fd, short event, void *data) 473 { 474 (void)fd; 475 (void)event; 476 477 process_t *process = data; 478 tor_assert(process); 479 480 process_notify_event_stdin(process); 481 } 482 483 /** This function tells libevent that we are interested in receiving read 484 * events from the given <b>handle</b>. */ 485 STATIC void 486 process_unix_start_reading(process_unix_handle_t *handle) 487 { 488 tor_assert(handle); 489 490 if (event_add(handle->event, NULL)) 491 log_warn(LD_PROCESS, 492 "Unable to add libevent event for handle."); 493 } 494 495 /** This function tells libevent that we are no longer interested in receiving 496 * read events from the given <b>handle</b>. */ 497 STATIC void 498 process_unix_stop_reading(process_unix_handle_t *handle) 499 { 500 tor_assert(handle); 501 502 if (handle->event == NULL) 503 return; 504 505 if (event_del(handle->event)) 506 log_warn(LD_PROCESS, 507 "Unable to delete libevent event for handle."); 508 } 509 510 /** This function tells libevent that we are interested in receiving write 511 * events from the given <b>handle</b>. */ 512 STATIC void 513 process_unix_start_writing(process_unix_handle_t *handle) 514 { 515 tor_assert(handle); 516 517 if (event_add(handle->event, NULL)) 518 log_warn(LD_PROCESS, 519 "Unable to add libevent event for handle."); 520 521 handle->is_writing = true; 522 } 523 524 /** This function tells libevent that we are no longer interested in receiving 525 * write events from the given <b>handle</b>. */ 526 STATIC void 527 process_unix_stop_writing(process_unix_handle_t *handle) 528 { 529 tor_assert(handle); 530 531 if (handle->event == NULL) 532 return; 533 534 if (event_del(handle->event)) 535 log_warn(LD_PROCESS, 536 "Unable to delete libevent event for handle."); 537 538 handle->is_writing = false; 539 } 540 541 /** This function is called when the waitpid system have detected that our 542 * process have terminated. We disable the waitpid system and notify the 543 * Process subsystem that we have terminated. */ 544 STATIC void 545 process_unix_waitpid_callback(int status, void *data) 546 { 547 tor_assert(data); 548 549 process_t *process = data; 550 process_unix_t *unix_process = process_get_unix_process(process); 551 552 /* Remove our waitpid callback. */ 553 clear_waitpid_callback(unix_process->waitpid); 554 unix_process->waitpid = NULL; 555 556 /* Notify our process. */ 557 process_notify_event_exit(process, status); 558 559 /* Make sure you don't modify the process after we have called 560 * process_notify_event_exit() on it, to allow users to process_free() it in 561 * the exit callback. */ 562 } 563 564 /** This function sets the file descriptor in the <b>handle</b> as non-blocking 565 * and configures the libevent event structure based on the given <b>flags</b> 566 * to ensure that <b>callback</b> is called whenever we have events on the 567 * given <b>handle</b>. */ 568 STATIC void 569 process_unix_setup_handle(process_t *process, 570 process_unix_handle_t *handle, 571 short flags, 572 event_callback_fn callback) 573 { 574 tor_assert(process); 575 tor_assert(handle); 576 tor_assert(callback); 577 578 /* Put our file descriptor into non-blocking mode. */ 579 if (fcntl(handle->fd, F_SETFL, O_NONBLOCK) < 0) { 580 log_warn(LD_PROCESS, "Unable mark Unix handle as non-blocking: %s", 581 strerror(errno)); 582 } 583 584 /* Setup libevent event. */ 585 handle->event = tor_event_new(tor_libevent_get_base(), 586 handle->fd, 587 flags, 588 callback, 589 process); 590 } 591 592 /** This function reads data from the given <b>handle</b> and puts it into 593 * <b>buffer</b>. Returns the number of bytes read this way. */ 594 STATIC int 595 process_unix_read_handle(process_t *process, 596 process_unix_handle_t *handle, 597 buf_t *buffer) 598 { 599 tor_assert(process); 600 tor_assert(handle); 601 tor_assert(buffer); 602 603 int ret = 0; 604 int eof = 0; 605 int error = 0; 606 607 ret = buf_read_from_pipe(buffer, 608 handle->fd, 609 PROCESS_MAX_READ, 610 &eof, 611 &error); 612 613 if (error) 614 log_warn(LD_PROCESS, 615 "Unable to read data: %s", strerror(error)); 616 617 if (eof) { 618 handle->reached_eof = true; 619 process_unix_stop_reading(handle); 620 } 621 622 return ret; 623 } 624 625 /** Close the standard in, out, and error handles of the given 626 * <b>unix_process</b>. */ 627 STATIC bool 628 process_unix_close_file_descriptors(process_unix_t *unix_process) 629 { 630 tor_assert(unix_process); 631 632 int ret; 633 bool success = true; 634 635 /* Stop reading and writing before we close() our 636 * file descriptors. */ 637 if (! unix_process->stdout_handle.reached_eof) 638 process_unix_stop_reading(&unix_process->stdout_handle); 639 640 if (! unix_process->stderr_handle.reached_eof) 641 process_unix_stop_reading(&unix_process->stderr_handle); 642 643 if (unix_process->stdin_handle.is_writing) 644 process_unix_stop_writing(&unix_process->stdin_handle); 645 646 if (unix_process->stdin_handle.fd != -1) { 647 ret = close(unix_process->stdin_handle.fd); 648 if (ret == -1) { 649 log_warn(LD_PROCESS, "Unable to close standard in"); 650 success = false; 651 } 652 653 unix_process->stdin_handle.fd = -1; 654 } 655 656 if (unix_process->stdout_handle.fd != -1) { 657 ret = close(unix_process->stdout_handle.fd); 658 if (ret == -1) { 659 log_warn(LD_PROCESS, "Unable to close standard out"); 660 success = false; 661 } 662 663 unix_process->stdout_handle.fd = -1; 664 } 665 666 if (unix_process->stderr_handle.fd != -1) { 667 ret = close(unix_process->stderr_handle.fd); 668 if (ret == -1) { 669 log_warn(LD_PROCESS, "Unable to close standard error"); 670 success = false; 671 } 672 673 unix_process->stderr_handle.fd = -1; 674 } 675 676 return success; 677 } 678 679 #endif /* !defined(_WIN32) */