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