aboutsummaryrefslogtreecommitdiff
path: root/src/lib/process/process.c
blob: 12c1f9a7727142fa3c8a3a44ee06073b4dd09590 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/* Copyright (c) 2003, Roger Dingledine
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
 * Copyright (c) 2007-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file process.c
 * \brief Module for working with other processes.
 **/

#define PROCESS_PRIVATE
#include "lib/buf/buffers.h"
#include "lib/net/buffers_net.h"
#include "lib/container/smartlist.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/process/process.h"
#include "lib/process/process_unix.h"
#include "lib/process/process_win32.h"
#include "lib/process/env.h"

#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif

/** A list of all <b>process_t</b> instances currently allocated. */
static smartlist_t *processes;

/**
 * Boolean.  If true, then Tor may call execve or CreateProcess via
 * tor_spawn_background.
 **/
static int may_spawn_background_process = 1;

/** Structure to represent a child process. */
struct process_t {
  /** Process status. */
  process_status_t status;

  /** Which protocol is the process using? */
  process_protocol_t protocol;

  /** Which function to call when we have data ready from stdout? */
  process_read_callback_t stdout_read_callback;

  /** Which function to call when we have data ready from stderr? */
  process_read_callback_t stderr_read_callback;

  /** Which function call when our process terminated? */
  process_exit_callback_t exit_callback;

  /** Our exit code when the process have terminated. */
  process_exit_code_t exit_code;

  /** Name of the command we want to execute (for example: /bin/ls). */
  char *command;

  /** The arguments used for the new process. The format here is one argument
   * per element of the smartlist_t. On Windows these arguments are combined
   * together using the <b>tor_join_win_cmdline</b> function. On Unix the
   * process name (argv[0]) and the trailing NULL is added automatically before
   * the process is executed. */
  smartlist_t *arguments;

  /** The environment used for the new process. */
  smartlist_t *environment;

  /** Buffer to store data from stdout when it is read. */
  buf_t *stdout_buffer;

  /** Buffer to store data from stderr when it is read. */
  buf_t *stderr_buffer;

  /** Buffer to store data to stdin before it is written. */
  buf_t *stdin_buffer;

  /** Do we need to store some custom data with the process? */
  void *data;

#ifndef _WIN32
  /** Our Unix process handle. */
  process_unix_t *unix_process;
#else
  /** Our Win32 process handle. */
  process_win32_t *win32_process;
#endif /* !defined(_WIN32) */
};

/** Convert a given process status in <b>status</b> to its string
 * representation. */
const char *
process_status_to_string(process_status_t status)
{
  switch (status) {
  case PROCESS_STATUS_NOT_RUNNING:
    return "not running";
  case PROCESS_STATUS_RUNNING:
    return "running";
  case PROCESS_STATUS_ERROR:
    return "error";
  }

  /* LCOV_EXCL_START */
  tor_assert_unreached();
  return NULL;
  /* LCOV_EXCL_STOP */
}

/** Convert a given process protocol in <b>protocol</b> to its string
 * representation. */
const char *
process_protocol_to_string(process_protocol_t protocol)
{
  switch (protocol) {
  case PROCESS_PROTOCOL_LINE:
    return "Line";
  case PROCESS_PROTOCOL_RAW:
    return "Raw";
  }

  /* LCOV_EXCL_START */
  tor_assert_unreached();
  return NULL;
  /* LCOV_EXCL_STOP */
}

/**
 * Turn off may_spawn_background_process, so that all future calls to
 * tor_spawn_background are guaranteed to fail.
 **/
void
tor_disable_spawning_background_processes(void)
{
  may_spawn_background_process = 0;
}

/** Initialize the Process subsystem.  This function initializes the Process
 * subsystem's global state. For cleaning up, <b>process_free_all()</b> should
 * be called. */
void
process_init(void)
{
  processes = smartlist_new();

#ifdef _WIN32
  process_win32_init();
#endif
}

/** Free up all resources that is handled by the Process subsystem. Note that
 * this call does not terminate already running processes. */
void
process_free_all(void)
{
#ifdef _WIN32
  process_win32_deinit();
#endif

  SMARTLIST_FOREACH(processes, process_t *, x, process_free(x));
  smartlist_free(processes);
}

/** Get a list of all processes.  This function returns a smartlist of
 * <b>process_t</b> containing all the currently allocated processes. */
const smartlist_t *
process_get_all_processes(void)
{
  return processes;
}

/** Allocate and initialize a new process.  This function returns a newly
 * allocated and initialized process data, which can be used to configure and
 * later run a subprocess of Tor. Use the various <b>process_set_*()</b>
 * methods to configure it and run the process using <b>process_exec()</b>. Use
 * <b>command</b> to specify the path to the command to run. You can either
 * specify an absolute path to the command or relative where Tor will use the
 * underlying operating system's functionality for finding the command to run.
 * */
process_t *
process_new(const char *command)
{
  tor_assert(command);

  process_t *process;
  process = tor_malloc_zero(sizeof(process_t));

  /* Set our command. */
  process->command = tor_strdup(command);

  /* By default we are not running. */
  process->status = PROCESS_STATUS_NOT_RUNNING;

  /* Prepare process environment. */
  process->arguments = smartlist_new();
  process->environment = smartlist_new();

  /* Prepare the buffers. */
  process->stdout_buffer = buf_new();
  process->stderr_buffer = buf_new();
  process->stdin_buffer = buf_new();

#ifndef _WIN32
  /* Prepare our Unix process handle. */
  process->unix_process = process_unix_new();
#else
  /* Prepare our Win32 process handle. */
  process->win32_process = process_win32_new();
#endif /* !defined(_WIN32) */

  smartlist_add(processes, process);

  return process;
}

/** Deallocate the given process in <b>process</b>. */
void
process_free_(process_t *process)
{
  if (! process)
    return;

  /* Cleanup parameters. */
  tor_free(process->command);

  /* Cleanup arguments and environment. */
  SMARTLIST_FOREACH(process->arguments, char *, x, tor_free(x));
  smartlist_free(process->arguments);

  SMARTLIST_FOREACH(process->environment, char *, x, tor_free(x));
  smartlist_free(process->environment);

  /* Cleanup the buffers. */
  buf_free(process->stdout_buffer);
  buf_free(process->stderr_buffer);
  buf_free(process->stdin_buffer);

#ifndef _WIN32
  /* Cleanup our Unix process handle. */
  process_unix_free(process->unix_process);
#else
  /* Cleanup our Win32 process handle. */
  process_win32_free(process->win32_process);
#endif /* !defined(_WIN32) */

  smartlist_remove(processes, process);

  tor_free(process);
}

/** Execute the given process.  This function executes the given process as a
 * subprocess of Tor. Returns <b>PROCESS_STATUS_RUNNING</b> upon success. */
process_status_t
process_exec(process_t *process)
{
  tor_assert(process);

  if (BUG(may_spawn_background_process == 0))
    return PROCESS_STATUS_ERROR;

  process_status_t status = PROCESS_STATUS_NOT_RUNNING;

  log_info(LD_PROCESS, "Starting new process: %s", process->command);

#ifndef _WIN32
  status = process_unix_exec(process);
#else
  status = process_win32_exec(process);
#endif

  /* Update our state. */
  process_set_status(process, status);

  if (status != PROCESS_STATUS_RUNNING) {
    log_warn(LD_PROCESS, "Failed to start process: %s",
             process_get_command(process));
  }

  return status;
}

/** Terminate the given process. Returns true on success,
 * otherwise false. */
bool
process_terminate(process_t *process)
{
  tor_assert(process);

  /* Terminating a non-running process isn't going to work. */
  if (process_get_status(process) != PROCESS_STATUS_RUNNING)
    return false;

  log_debug(LD_PROCESS, "Terminating process");

#ifndef _WIN32
  return process_unix_terminate(process);
#else
  return process_win32_terminate(process);
#endif
}

/** Returns the unique process identifier for the given <b>process</b>. */
process_pid_t
process_get_pid(process_t *process)
{
  tor_assert(process);

#ifndef _WIN32
  return process_unix_get_pid(process);
#else
  return process_win32_get_pid(process);
#endif
}

/** Set the callback function for output from the child process's standard out
 * handle.  This function sets the callback function which is called every time
 * the child process have written output to its standard out file handle.
 *
 * Use <b>process_set_protocol(process, PROCESS_PROTOCOL_LINE)</b> if you want
 * the callback to only contain complete "\n" or "\r\n" terminated lines. */
void
process_set_stdout_read_callback(process_t *process,
                                 process_read_callback_t callback)
{
  tor_assert(process);
  process->stdout_read_callback = callback;
}

/** Set the callback function for output from the child process's standard
 * error handle.  This function sets the callback function which is called
 * every time the child process have written output to its standard error file
 * handle.
 *
 * Use <b>process_set_protocol(process, PROCESS_PROTOCOL_LINE)</b> if you want
 * the callback to only contain complete "\n" or "\r\n" terminated lines. */
void
process_set_stderr_read_callback(process_t *process,
                                 process_read_callback_t callback)
{
  tor_assert(process);
  process->stderr_read_callback = callback;
}

/** Set the callback function for process exit notification.  The
 * <b>callback</b> function will be called every time your child process have
 * terminated. */
void
process_set_exit_callback(process_t *process,
                          process_exit_callback_t callback)
{
  tor_assert(process);
  process->exit_callback = callback;
}

/** Get the current command of the given process. */
const char *
process_get_command(const process_t *process)
{
  tor_assert(process);
  return process->command;
}

void
process_set_protocol(process_t *process, process_protocol_t protocol)
{
  tor_assert(process);
  process->protocol = protocol;
}

/** Get the currently used protocol of the given process. */
process_protocol_t
process_get_protocol(const process_t *process)
{
  tor_assert(process);
  return process->protocol;
}

/** Set opague pointer to data.  This function allows you to store a pointer to
 * your own data in the given process. Use <b>process_get_data()</b> in the
 * various callback functions to retrieve the data again.
 *
 * Note that the given process does NOT take ownership of the data and you are
 * responsible for freeing up any resources allocated by the given data.
 * */
void
process_set_data(process_t *process, void *data)
{
  tor_assert(process);
  process->data = data;
}

/** Get the opaque pointer to callback data from the given process.  This
 * function allows you get the data you stored with <b>process_set_data()</b>
 * in the different callback functions. */
void *
process_get_data(const process_t *process)
{
  tor_assert(process);
  return process->data;
}

/** Set the status of a given process. */
void
process_set_status(process_t *process, process_status_t status)
{
  tor_assert(process);
  process->status = status;
}

/** Get the status of the given process. */
process_status_t
process_get_status(const process_t *process)
{
  tor_assert(process);
  return process->status;
}

/** Append an argument to the list of arguments in the given process. */
void
process_append_argument(process_t *process, const char *argument)
{
  tor_assert(process);
  tor_assert(argument);

  smartlist_add(process->arguments, tor_strdup(argument));
}

/** Returns a list of arguments (excluding the command itself) from the
 * given process. */
const smartlist_t *
process_get_arguments(const process_t *process)
{
  tor_assert(process);
  return process->arguments;
}

/** Returns a newly allocated Unix style argument vector. Use <b>tor_free()</b>
 * to deallocate it after use. */
char **
process_get_argv(const process_t *process)
{
  tor_assert(process);

  /** Generate a Unix style process argument vector from our process's
   * arguments smartlist_t. */
  char **argv = NULL;

  char *filename = process->command;
  const smartlist_t *arguments = process->arguments;
  const size_t size = smartlist_len(arguments);

  /* Make space for the process filename as argv[0] and a trailing NULL. */
  argv = tor_malloc_zero(sizeof(char *) * (size + 2));

  /* Set our filename as first argument. */
  argv[0] = filename;

  /* Put in the rest of the values from arguments. */
  SMARTLIST_FOREACH_BEGIN(arguments, char *, arg_val) {
    tor_assert(arg_val != NULL);

    argv[arg_val_sl_idx + 1] = arg_val;
  } SMARTLIST_FOREACH_END(arg_val);

  return argv;
}

/** This function clears the internal environment and copies over every string
 * from <b>env</b> as the new environment. */
void
process_reset_environment(process_t *process, const smartlist_t *env)
{
  tor_assert(process);
  tor_assert(env);

  /* Cleanup old environment. */
  SMARTLIST_FOREACH(process->environment, char *, x, tor_free(x));
  smartlist_free(process->environment);
  process->environment = smartlist_new();

  SMARTLIST_FOREACH(env, char *, x,
                    smartlist_add(process->environment, tor_strdup(x)));
}

/** Set the given <b>key</b>/<b>value</b> pair as environment variable in the
 * given process. */
void
process_set_environment(process_t *process,
                        const char *key,
                        const char *value)
{
  tor_assert(process);
  tor_assert(key);
  tor_assert(value);

  smartlist_add_asprintf(process->environment, "%s=%s", key, value);
}

/** Returns a newly allocated <b>process_environment_t</b> containing the
 * environment variables for the given process. */
process_environment_t *
process_get_environment(const process_t *process)
{
  tor_assert(process);
  return process_environment_make(process->environment);
}

#ifndef _WIN32
/** Get the internal handle for the Unix backend. */
process_unix_t *
process_get_unix_process(const process_t *process)
{
  tor_assert(process);
  tor_assert(process->unix_process);
  return process->unix_process;
}
#else /* defined(_WIN32) */
/** Get the internal handle for Windows backend. */
process_win32_t *
process_get_win32_process(const process_t *process)
{
  tor_assert(process);
  tor_assert(process->win32_process);
  return process->win32_process;
}
#endif /* !defined(_WIN32) */

/** Write <b>size</b> bytes of <b>data</b> to the given process's standard
 * input. */
void
process_write(process_t *process,
              const uint8_t *data, size_t size)
{
  tor_assert(process);
  tor_assert(data);

  buf_add(process->stdin_buffer, (char *)data, size);
  process_write_stdin(process, process->stdin_buffer);
}

/** As tor_vsnprintf(), but write the data to the given process's standard
 * input. */
void
process_vprintf(process_t *process,
                const char *format, va_list args)
{
  tor_assert(process);
  tor_assert(format);

  int size;
  char *data;

  size = tor_vasprintf(&data, format, args);
  tor_assert(data != NULL);
  process_write(process, (uint8_t *)data, size);
  tor_free(data);
}

/** As tor_snprintf(), but write the data to the given process's standard
 * input. */
void
process_printf(process_t *process,
               const char *format, ...)
{
  tor_assert(process);
  tor_assert(format);

  va_list ap;
  va_start(ap, format);
  process_vprintf(process, format, ap);
  va_end(ap);
}

/** This function is called by the Process backend when a given process have
 * data that is ready to be read from the child process's standard output
 * handle. */
void
process_notify_event_stdout(process_t *process)
{
  tor_assert(process);

  int ret;
  ret = process_read_stdout(process, process->stdout_buffer);

  if (ret > 0)
    process_read_data(process,
                      process->stdout_buffer,
                      process->stdout_read_callback);
}

/** This function is called by the Process backend when a given process have
 * data that is ready to be read from the child process's standard error
 * handle. */
void
process_notify_event_stderr(process_t *process)
{
  tor_assert(process);

  int ret;
  ret = process_read_stderr(process, process->stderr_buffer);

  if (ret > 0)
    process_read_data(process,
                      process->stderr_buffer,
                      process->stderr_read_callback);
}

/** This function is called by the Process backend when a given process is
 * allowed to begin writing data to the standard input of the child process. */
void
process_notify_event_stdin(process_t *process)
{
  tor_assert(process);

  process_write_stdin(process, process->stdin_buffer);
}

/** This function is called by the Process backend when a given process have
 * terminated. The exit status code is passed in <b>exit_code</b>. We mark the
 * process as no longer running and calls the <b>exit_callback</b> with
 * information about the process termination. The given <b>process</b> is
 * free'd iff the exit_callback returns true. */
void
process_notify_event_exit(process_t *process, process_exit_code_t exit_code)
{
  tor_assert(process);

  log_debug(LD_PROCESS,
            "Process terminated with exit code: %"PRIu64, exit_code);

  /* Update our state. */
  process_set_status(process, PROCESS_STATUS_NOT_RUNNING);
  process->exit_code = exit_code;

  /* Call our exit callback, if it exists. */
  bool free_process_handle = false;

  /* The exit callback will tell us if we should process_free() our handle. */
  if (process->exit_callback)
    free_process_handle = process->exit_callback(process, exit_code);

  if (free_process_handle)
    process_free(process);
}

/** This function is called whenever the Process backend have notified us that
 * there is data to be read from its standard out handle. Returns the number of
 * bytes that have been put into the given buffer. */
MOCK_IMPL(STATIC int, process_read_stdout, (process_t *process, buf_t *buffer))
{
  tor_assert(process);
  tor_assert(buffer);

#ifndef _WIN32
  return process_unix_read_stdout(process, buffer);
#else
  return process_win32_read_stdout(process, buffer);
#endif
}

/** This function is called whenever the Process backend have notified us that
 * there is data to be read from its standard error handle. Returns the number
 * of bytes that have been put into the given buffer. */
MOCK_IMPL(STATIC int, process_read_stderr, (process_t *process, buf_t *buffer))
{
  tor_assert(process);
  tor_assert(buffer);

#ifndef _WIN32
  return process_unix_read_stderr(process, buffer);
#else
  return process_win32_read_stderr(process, buffer);
#endif
}

/** This function calls the backend function for the given process whenever
 * there is data to be written to the backends' file handles. */
MOCK_IMPL(STATIC void, process_write_stdin,
          (process_t *process, buf_t *buffer))
{
  tor_assert(process);
  tor_assert(buffer);

#ifndef _WIN32
  process_unix_write(process, buffer);
#else
  process_win32_write(process, buffer);
#endif
}

/** This function calls the protocol handlers based on the value of
 * <b>process_get_protocol(process)</b>. Currently we call
 * <b>process_read_buffer()</b> for <b>PROCESS_PROTOCOL_RAW</b> and
 * <b>process_read_lines()</b> for <b>PROCESS_PROTOCOL_LINE</b>. */
STATIC void
process_read_data(process_t *process,
                  buf_t *buffer,
                  process_read_callback_t callback)
{
  tor_assert(process);
  tor_assert(buffer);

  switch (process_get_protocol(process)) {
  case PROCESS_PROTOCOL_RAW:
    process_read_buffer(process, buffer, callback);
    break;
  case PROCESS_PROTOCOL_LINE:
    process_read_lines(process, buffer, callback);
    break;
  default:
    /* LCOV_EXCL_START */
    tor_assert_unreached();
    return;
    /* LCOV_EXCL_STOP */
  }
}

/** This function takes the content of the given <b>buffer</b> and passes it to
 * the given <b>callback</b> function, but ensures that an additional zero byte
 * is added to the end of the data such that the given callback implementation
 * can threat the content as a ASCIIZ string. */
STATIC void
process_read_buffer(process_t *process,
                    buf_t *buffer,
                    process_read_callback_t callback)
{
  tor_assert(process);
  tor_assert(buffer);

  const size_t size = buf_datalen(buffer);

  /* We allocate an extra byte for the zero byte in the end. */
  char *data = tor_malloc_zero(size + 1);

  buf_get_bytes(buffer, data, size);
  log_debug(LD_PROCESS, "Read data from process");

  if (callback)
    callback(process, data, size);

  tor_free(data);
}

/** This function tries to extract complete lines from the given <b>buffer</b>
 * and calls the given <b>callback</b> function whenever it has a complete
 * line. Before calling <b>callback</b> we remove the trailing "\n" or "\r\n"
 * from the line. If we are unable to extract a complete line we leave the data
 * in the buffer for next call. */
STATIC void
process_read_lines(process_t *process,
                   buf_t *buffer,
                   process_read_callback_t callback)
{
  tor_assert(process);
  tor_assert(buffer);

  const size_t size = buf_datalen(buffer) + 1;
  size_t line_size = 0;
  char *data = tor_malloc_zero(size);
  int ret;

  while (true) {
    line_size = size;
    ret = buf_get_line(buffer, data, &line_size);

    /* A complete line should always be smaller than the size of our
     * buffer. */
    tor_assert(ret != -1);

    /* Remove \n from the end of the line. */
    if (line_size >= 1 && data[line_size - 1] == '\n') {
      data[line_size - 1] = '\0';
      --line_size;
    }

    /* Remove \r from the end of the line. */
    if (line_size >= 1 && data[line_size - 1] == '\r') {
      data[line_size - 1] = '\0';
      --line_size;
    }

    if (ret == 1) {
      log_debug(LD_PROCESS, "Read line from process: \"%s\"", data);

      if (callback)
        callback(process, data, line_size);

      /* We have read a whole line, let's see if there is more lines to read.
       * */
      continue;
    }

    /* No complete line for us to read. We are done for now. */
    tor_assert_nonfatal(ret == 0);
    break;
  }

  tor_free(data);
}