aboutsummaryrefslogtreecommitdiff
path: root/src/or/tor_api.c
blob: 4260cc88f4d3528601de27af733e79a184a8717e (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
/* Copyright (c) 2001 Matej Pfajfar.
 * Copyright (c) 2001-2004, Roger Dingledine.
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
 * Copyright (c) 2007-2017, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file tor_api.c
 **/

#include "tor_api.h"
#include "tor_api_internal.h"

// Include this after the above headers, to insure that they don't
// depend on anything else.
#include "orconfig.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// We don't want to use tor_malloc and tor_free here, since this needs
// to run before anything is initialized at all, and ought to run when
// we're not linked to anything at all.

#define raw_malloc malloc
#define raw_free free

tor_main_configuration_t *
tor_main_configuration_new(void)
{
  static const char *fake_argv[] = { "tor" };
  tor_main_configuration_t *cfg = raw_malloc(sizeof(*cfg));
  if (cfg == NULL)
    return NULL;

  memset(cfg, 0, sizeof(*cfg));

  cfg->argc = 1;
  cfg->argv = (char **) fake_argv;

  return cfg;
}

int
tor_main_configuration_set_command_line(tor_main_configuration_t *cfg,
                                        int argc, char *argv[])
{
  if (cfg == NULL)
    return -1;
  cfg->argc = argc;
  cfg->argv = argv;
  return 0;
}

void
tor_main_configuration_free(tor_main_configuration_t *cfg)
{
  if (cfg == NULL)
    return;
  raw_free(cfg);
}

/* Main entry point for the Tor process.  Called from main().
 *
 * This function is distinct from main() only so we can link main.c into
 * the unittest binary without conflicting with the unittests' main.
 *
 * Some embedders have historically called this function; but that usage is
 * deprecated: they should use tor_run_main() instead.
 */
int
tor_main(int argc, char *argv[])
{
  tor_main_configuration_t *cfg = tor_main_configuration_new();
  if (!cfg) {
    puts("INTERNAL ERROR: Allocation failure. Cannot proceed");
    return 1;
  }
  if (tor_main_configuration_set_command_line(cfg, argc, argv) < 0) {
    puts("INTERNAL ERROR: Can't set command line. Cannot proceed.");
    return 1;
  }
  int rv = tor_run_main(cfg);
  tor_main_configuration_free(cfg);
  return rv;
}