tor  0.4.2.1-alpha-dev
process.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-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
11 #define PROCESS_PRIVATE
12 #include "lib/buf/buffers.h"
13 #include "lib/net/buffers_net.h"
15 #include "lib/log/log.h"
16 #include "lib/log/util_bug.h"
17 #include "lib/process/process.h"
20 #include "lib/process/env.h"
21 
22 #ifdef HAVE_STDDEF_H
23 #include <stddef.h>
24 #endif
25 
28 
34 
36 struct process_t {
39 
42 
44  process_read_callback_t stdout_read_callback;
45 
47  process_read_callback_t stderr_read_callback;
48 
50  process_exit_callback_t exit_callback;
51 
53  process_exit_code_t exit_code;
54 
56  char *command;
57 
64 
67 
69  buf_t *stdout_buffer;
70 
72  buf_t *stderr_buffer;
73 
75  buf_t *stdin_buffer;
76 
78  void *data;
79 
80 #ifndef _WIN32
81 
83 #else
84 
85  process_win32_t *win32_process;
86 #endif /* !defined(_WIN32) */
87 };
88 
91 const char *
93 {
94  switch (status) {
96  return "not running";
98  return "running";
100  return "error";
101  }
102 
103  /* LCOV_EXCL_START */
104  tor_assert_unreached();
105  return NULL;
106  /* LCOV_EXCL_STOP */
107 }
108 
111 const char *
113 {
114  switch (protocol) {
116  return "Line";
118  return "Raw";
119  }
120 
121  /* LCOV_EXCL_START */
122  tor_assert_unreached();
123  return NULL;
124  /* LCOV_EXCL_STOP */
125 }
126 
131 void
133 {
135 }
136 
140 void
142 {
143  processes = smartlist_new();
144 
145 #ifdef _WIN32
146  process_win32_init();
147 #endif
148 }
149 
152 void
154 {
155 #ifdef _WIN32
156  process_win32_deinit();
157 #endif
158 
159  SMARTLIST_FOREACH(processes, process_t *, x, process_free(x));
160  smartlist_free(processes);
161 }
162 
165 const smartlist_t *
167 {
168  return processes;
169 }
170 
179 process_t *
180 process_new(const char *command)
181 {
182  tor_assert(command);
183 
184  process_t *process;
185  process = tor_malloc_zero(sizeof(process_t));
186 
187  /* Set our command. */
188  process->command = tor_strdup(command);
189 
190  /* By default we are not running. */
192 
193  /* Prepare process environment. */
194  process->arguments = smartlist_new();
195  process->environment = smartlist_new();
196 
197  /* Prepare the buffers. */
198  process->stdout_buffer = buf_new();
199  process->stderr_buffer = buf_new();
200  process->stdin_buffer = buf_new();
201 
202 #ifndef _WIN32
203  /* Prepare our Unix process handle. */
204  process->unix_process = process_unix_new();
205 #else
206  /* Prepare our Win32 process handle. */
207  process->win32_process = process_win32_new();
208 #endif /* !defined(_WIN32) */
209 
210  smartlist_add(processes, process);
211 
212  return process;
213 }
214 
216 void
218 {
219  if (! process)
220  return;
221 
222  /* Cleanup parameters. */
223  tor_free(process->command);
224 
225  /* Cleanup arguments and environment. */
226  SMARTLIST_FOREACH(process->arguments, char *, x, tor_free(x));
227  smartlist_free(process->arguments);
228 
229  SMARTLIST_FOREACH(process->environment, char *, x, tor_free(x));
230  smartlist_free(process->environment);
231 
232  /* Cleanup the buffers. */
233  buf_free(process->stdout_buffer);
234  buf_free(process->stderr_buffer);
235  buf_free(process->stdin_buffer);
236 
237 #ifndef _WIN32
238  /* Cleanup our Unix process handle. */
239  process_unix_free(process->unix_process);
240 #else
241  /* Cleanup our Win32 process handle. */
242  process_win32_free(process->win32_process);
243 #endif /* !defined(_WIN32) */
244 
245  smartlist_remove(processes, process);
246 
247  tor_free(process);
248 }
249 
254 {
255  tor_assert(process);
256 
257  if (BUG(may_spawn_background_process == 0))
258  return PROCESS_STATUS_ERROR;
259 
261 
262  log_info(LD_PROCESS, "Starting new process: %s", process->command);
263 
264 #ifndef _WIN32
265  status = process_unix_exec(process);
266 #else
267  status = process_win32_exec(process);
268 #endif
269 
270  /* Update our state. */
271  process_set_status(process, status);
272 
273  if (status != PROCESS_STATUS_RUNNING) {
274  log_warn(LD_PROCESS, "Failed to start process: %s",
275  process_get_command(process));
276  }
277 
278  return status;
279 }
280 
283 bool
285 {
286  tor_assert(process);
287 
288  /* Terminating a non-running process isn't going to work. */
290  return false;
291 
292  log_debug(LD_PROCESS, "Terminating process");
293 
294 #ifndef _WIN32
295  return process_unix_terminate(process);
296 #else
297  return process_win32_terminate(process);
298 #endif
299 }
300 
302 process_pid_t
304 {
305  tor_assert(process);
306 
307 #ifndef _WIN32
308  return process_unix_get_pid(process);
309 #else
310  return process_win32_get_pid(process);
311 #endif
312 }
313 
320 void
322  process_read_callback_t callback)
323 {
324  tor_assert(process);
325  process->stdout_read_callback = callback;
326 }
327 
335 void
337  process_read_callback_t callback)
338 {
339  tor_assert(process);
340  process->stderr_read_callback = callback;
341 }
342 
346 void
348  process_exit_callback_t callback)
349 {
350  tor_assert(process);
351  process->exit_callback = callback;
352 }
353 
355 const char *
357 {
358  tor_assert(process);
359  return process->command;
360 }
361 
362 void
363 process_set_protocol(process_t *process, process_protocol_t protocol)
364 {
365  tor_assert(process);
366  process->protocol = protocol;
367 }
368 
372 {
373  tor_assert(process);
374  return process->protocol;
375 }
376 
384 void
385 process_set_data(process_t *process, void *data)
386 {
387  tor_assert(process);
388  process->data = data;
389 }
390 
394 void *
396 {
397  tor_assert(process);
398  return process->data;
399 }
400 
402 void
404 {
405  tor_assert(process);
406  process->status = status;
407 }
408 
412 {
413  tor_assert(process);
414  return process->status;
415 }
416 
418 void
419 process_append_argument(process_t *process, const char *argument)
420 {
421  tor_assert(process);
422  tor_assert(argument);
423 
424  smartlist_add(process->arguments, tor_strdup(argument));
425 }
426 
429 const smartlist_t *
431 {
432  tor_assert(process);
433  return process->arguments;
434 }
435 
438 char **
440 {
441  tor_assert(process);
442 
445  char **argv = NULL;
446 
447  char *filename = process->command;
448  const smartlist_t *arguments = process->arguments;
449  const size_t size = smartlist_len(arguments);
450 
451  /* Make space for the process filename as argv[0] and a trailing NULL. */
452  argv = tor_malloc_zero(sizeof(char *) * (size + 2));
453 
454  /* Set our filename as first argument. */
455  argv[0] = filename;
456 
457  /* Put in the rest of the values from arguments. */
458  SMARTLIST_FOREACH_BEGIN(arguments, char *, arg_val) {
459  tor_assert(arg_val != NULL);
460 
461  argv[arg_val_sl_idx + 1] = arg_val;
462  } SMARTLIST_FOREACH_END(arg_val);
463 
464  return argv;
465 }
466 
469 void
471 {
472  tor_assert(process);
473  tor_assert(env);
474 
475  /* Cleanup old environment. */
476  SMARTLIST_FOREACH(process->environment, char *, x, tor_free(x));
477  smartlist_free(process->environment);
478  process->environment = smartlist_new();
479 
480  SMARTLIST_FOREACH(env, char *, x,
481  smartlist_add(process->environment, tor_strdup(x)));
482 }
483 
486 void
488  const char *key,
489  const char *value)
490 {
491  tor_assert(process);
492  tor_assert(key);
493  tor_assert(value);
494 
495  smartlist_add_asprintf(process->environment, "%s=%s", key, value);
496 }
497 
502 {
503  tor_assert(process);
504  return process_environment_make(process->environment);
505 }
506 
507 #ifndef _WIN32
508 
511 {
512  tor_assert(process);
513  tor_assert(process->unix_process);
514  return process->unix_process;
515 }
516 #else /* !(!defined(_WIN32)) */
517 
518 process_win32_t *
519 process_get_win32_process(const process_t *process)
520 {
521  tor_assert(process);
522  tor_assert(process->win32_process);
523  return process->win32_process;
524 }
525 #endif /* !defined(_WIN32) */
526 
529 void
531  const uint8_t *data, size_t size)
532 {
533  tor_assert(process);
534  tor_assert(data);
535 
536  buf_add(process->stdin_buffer, (char *)data, size);
537  process_write_stdin(process, process->stdin_buffer);
538 }
539 
542 void
544  const char *format, va_list args)
545 {
546  tor_assert(process);
547  tor_assert(format);
548 
549  int size;
550  char *data;
551 
552  size = tor_vasprintf(&data, format, args);
553  process_write(process, (uint8_t *)data, size);
554  tor_free(data);
555 }
556 
559 void
561  const char *format, ...)
562 {
563  tor_assert(process);
564  tor_assert(format);
565 
566  va_list ap;
567  va_start(ap, format);
568  process_vprintf(process, format, ap);
569  va_end(ap);
570 }
571 
575 void
577 {
578  tor_assert(process);
579 
580  int ret;
581  ret = process_read_stdout(process, process->stdout_buffer);
582 
583  if (ret > 0)
584  process_read_data(process,
585  process->stdout_buffer,
586  process->stdout_read_callback);
587 }
588 
592 void
594 {
595  tor_assert(process);
596 
597  int ret;
598  ret = process_read_stderr(process, process->stderr_buffer);
599 
600  if (ret > 0)
601  process_read_data(process,
602  process->stderr_buffer,
603  process->stderr_read_callback);
604 }
605 
608 void
610 {
611  tor_assert(process);
612 
613  process_write_stdin(process, process->stdin_buffer);
614 }
615 
621 void
622 process_notify_event_exit(process_t *process, process_exit_code_t exit_code)
623 {
624  tor_assert(process);
625 
626  log_debug(LD_PROCESS,
627  "Process terminated with exit code: %"PRIu64, exit_code);
628 
629  /* Update our state. */
631  process->exit_code = exit_code;
632 
633  /* Call our exit callback, if it exists. */
634  bool free_process_handle = false;
635 
636  /* The exit callback will tell us if we should process_free() our handle. */
637  if (process->exit_callback)
638  free_process_handle = process->exit_callback(process, exit_code);
639 
640  if (free_process_handle)
641  process_free(process);
642 }
643 
647 MOCK_IMPL(STATIC int, process_read_stdout, (process_t *process, buf_t *buffer))
648 {
649  tor_assert(process);
650  tor_assert(buffer);
651 
652 #ifndef _WIN32
653  return process_unix_read_stdout(process, buffer);
654 #else
655  return process_win32_read_stdout(process, buffer);
656 #endif
657 }
658 
662 MOCK_IMPL(STATIC int, process_read_stderr, (process_t *process, buf_t *buffer))
663 {
664  tor_assert(process);
665  tor_assert(buffer);
666 
667 #ifndef _WIN32
668  return process_unix_read_stderr(process, buffer);
669 #else
670  return process_win32_read_stderr(process, buffer);
671 #endif
672 }
673 
676 MOCK_IMPL(STATIC void, process_write_stdin,
677  (process_t *process, buf_t *buffer))
678 {
679  tor_assert(process);
680  tor_assert(buffer);
681 
682 #ifndef _WIN32
683  process_unix_write(process, buffer);
684 #else
685  process_win32_write(process, buffer);
686 #endif
687 }
688 
693 STATIC void
695  buf_t *buffer,
696  process_read_callback_t callback)
697 {
698  tor_assert(process);
699  tor_assert(buffer);
700 
701  switch (process_get_protocol(process)) {
703  process_read_buffer(process, buffer, callback);
704  break;
706  process_read_lines(process, buffer, callback);
707  break;
708  default:
709  /* LCOV_EXCL_START */
710  tor_assert_unreached();
711  return;
712  /* LCOV_EXCL_STOP */
713  }
714 }
715 
720 STATIC void
722  buf_t *buffer,
723  process_read_callback_t callback)
724 {
725  tor_assert(process);
726  tor_assert(buffer);
727 
728  const size_t size = buf_datalen(buffer);
729 
730  /* We allocate an extra byte for the zero byte in the end. */
731  char *data = tor_malloc_zero(size + 1);
732 
733  buf_get_bytes(buffer, data, size);
734  log_debug(LD_PROCESS, "Read data from process");
735 
736  if (callback)
737  callback(process, data, size);
738 
739  tor_free(data);
740 }
741 
747 STATIC void
749  buf_t *buffer,
750  process_read_callback_t callback)
751 {
752  tor_assert(process);
753  tor_assert(buffer);
754 
755  const size_t size = buf_datalen(buffer) + 1;
756  size_t line_size = 0;
757  char *data = tor_malloc_zero(size);
758  int ret;
759 
760  while (true) {
761  line_size = size;
762  ret = buf_get_line(buffer, data, &line_size);
763 
764  /* A complete line should always be smaller than the size of our
765  * buffer. */
766  tor_assert(ret != -1);
767 
768  /* Remove \n from the end of the line. */
769  if (line_size >= 1 && data[line_size - 1] == '\n') {
770  data[line_size - 1] = '\0';
771  --line_size;
772  }
773 
774  /* Remove \r from the end of the line. */
775  if (line_size >= 1 && data[line_size - 1] == '\r') {
776  data[line_size - 1] = '\0';
777  --line_size;
778  }
779 
780  if (ret == 1) {
781  log_debug(LD_PROCESS, "Read line from process: \"%s\"", data);
782 
783  if (callback)
784  callback(process, data, line_size);
785 
786  /* We have read a whole line, let's see if there is more lines to read.
787  * */
788  continue;
789  }
790 
791  /* No complete line for us to read. We are done for now. */
792  tor_assert_nonfatal(ret == 0);
793  break;
794  }
795 
796  tor_free(data);
797 }
buf_t * stdin_buffer
Definition: process.c:75
void process_free_all(void)
Definition: process.c:153
void process_set_exit_callback(process_t *process, process_exit_callback_t callback)
Definition: process.c:347
void process_reset_environment(process_t *process, const smartlist_t *env)
Definition: process.c:470
Header for process_unix.c.
process_protocol_t protocol
Definition: process.c:41
int process_unix_write(process_t *process, buf_t *buffer)
Definition: process_unix.c:398
process_unix_t * process_unix_new(void)
Definition: process_unix.c:90
MOCK_IMPL(STATIC void, process_write_stdin,(process_t *process, buf_t *buffer))
Definition: process.c:676
void process_free_(process_t *process)
Definition: process.c:217
Header for smartlist.c.
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
process_unix_t * unix_process
Definition: process.c:82
const smartlist_t * process_get_arguments(const process_t *process)
Definition: process.c:430
const smartlist_t * process_get_all_processes(void)
Definition: process.c:166
process_exit_code_t exit_code
Definition: process.c:53
int tor_vasprintf(char **strp, const char *fmt, va_list args)
Definition: printf.c:96
void smartlist_add(smartlist_t *sl, void *element)
STATIC void process_read_data(process_t *process, buf_t *buffer, process_read_callback_t callback)
Definition: process.c:694
void process_set_stdout_read_callback(process_t *process, process_read_callback_t callback)
Definition: process.c:321
process_environment_t * process_environment_make(struct smartlist_t *env_vars)
Definition: env.c:102
#define tor_free(p)
Definition: malloc.h:52
process_read_callback_t stdout_read_callback
Definition: process.c:44
Header for env.c.
smartlist_t * arguments
Definition: process.c:63
process_protocol_t process_get_protocol(const process_t *process)
Definition: process.c:371
buf_t * buf_new(void)
Definition: buffers.c:363
static smartlist_t * processes
Definition: process.c:27
void process_notify_event_stderr(process_t *process)
Definition: process.c:593
process_pid_t process_get_pid(process_t *process)
Definition: process.c:303
void process_set_status(process_t *process, process_status_t status)
Definition: process.c:403
void process_notify_event_stdin(process_t *process)
Definition: process.c:609
process_status_t status
Definition: process.c:38
int buf_get_line(buf_t *buf, char *data_out, size_t *data_len)
Definition: buffers.c:864
tor_assert(buffer)
const char * process_status_to_string(process_status_t status)
Definition: process.c:92
process_exit_callback_t exit_callback
Definition: process.c:50
bool process_unix_terminate(process_t *process)
Definition: process_unix.c:355
process_status_t process_exec(process_t *process)
Definition: process.c:253
int buf_get_bytes(buf_t *buf, char *string, size_t string_len)
Definition: buffers.c:634
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
void process_write(process_t *process, const uint8_t *data, size_t size)
Definition: process.c:530
char ** process_get_argv(const process_t *process)
Definition: process.c:439
void process_notify_event_stdout(process_t *process)
Definition: process.c:576
void tor_disable_spawning_background_processes(void)
Definition: process.c:132
Header for process_win32.c.
const char * process_protocol_to_string(process_protocol_t protocol)
Definition: process.c:112
#define LD_PROCESS
Definition: log.h:113
int buf_add(buf_t *buf, const char *string, size_t string_len)
Definition: buffers.c:525
void smartlist_remove(smartlist_t *sl, const void *element)
Header file for buffers_net.c.
bool process_terminate(process_t *process)
Definition: process.c:284
Header for process.c.
void * data
Definition: process.c:78
void process_set_data(process_t *process, void *data)
Definition: process.c:385
void process_append_argument(process_t *process, const char *argument)
Definition: process.c:419
process_status_t
Definition: process.h:24
buf_t * stdout_buffer
Definition: process.c:69
buf_t * stderr_buffer
Definition: process.c:72
smartlist_t * environment
Definition: process.c:66
void process_set_stderr_read_callback(process_t *process, process_read_callback_t callback)
Definition: process.c:336
void process_init(void)
Definition: process.c:141
process_status_t process_get_status(const process_t *process)
Definition: process.c:411
process_unix_t * process_get_unix_process(const process_t *process)
Definition: process.c:510
#define SMARTLIST_FOREACH(sl, type, var, cmd)
process_t * process_new(const char *command)
Definition: process.c:180
void process_vprintf(process_t *process, const char *format, va_list args)
Definition: process.c:543
process_status_t process_unix_exec(process_t *process)
Definition: process_unix.c:131
process_environment_t * process_get_environment(const process_t *process)
Definition: process.c:501
char * command
Definition: process.c:56
void * process_get_data(const process_t *process)
Definition: process.c:395
void process_set_environment(process_t *process, const char *key, const char *value)
Definition: process.c:487
void process_printf(process_t *process, const char *format,...)
Definition: process.c:560
Header file for buffers.c.
Headers for log.c.
process_protocol_t
Definition: process.h:37
void process_notify_event_exit(process_t *process, process_exit_code_t exit_code)
Definition: process.c:622
static int may_spawn_background_process
Definition: process.c:33
Macros to manage assertions, fatal and non-fatal.
STATIC void process_read_buffer(process_t *process, buf_t *buffer, process_read_callback_t callback)
Definition: process.c:721
STATIC void process_read_lines(process_t *process, buf_t *buffer, process_read_callback_t callback)
Definition: process.c:748
process_read_callback_t stderr_read_callback
Definition: process.c:47
const char * process_get_command(const process_t *process)
Definition: process.c:356
process_pid_t process_unix_get_pid(process_t *process)
Definition: process_unix.c:387