tor_api.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file tor_api.c
  8. **/
  9. #include "feature/api/tor_api.h"
  10. #include "feature/api/tor_api_internal.h"
  11. // Include this after the above headers, to insure that they don't
  12. // depend on anything else.
  13. #include "orconfig.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. // We don't want to use tor_malloc and tor_free here, since this needs
  18. // to run before anything is initialized at all, and ought to run when
  19. // we're not linked to anything at all.
  20. #define raw_malloc malloc
  21. #define raw_free free
  22. #define raw_realloc realloc
  23. #define raw_strdup strdup
  24. /**
  25. * Helper: Add a copy of <b>arg</b> to the owned arguments of <b>cfg</b>.
  26. * Return 0 on success, -1 on failure.
  27. */
  28. static int
  29. cfg_add_owned_arg(tor_main_configuration_t *cfg, const char *arg)
  30. {
  31. /* We aren't using amortized realloc here, because libc should do it for us,
  32. * and because this function is not critical-path. */
  33. char **new_argv = raw_realloc(cfg->argv_owned,
  34. sizeof(char*) * (cfg->argc_owned+1));
  35. if (new_argv == NULL)
  36. return -1;
  37. cfg->argv_owned = new_argv;
  38. if (NULL == (cfg->argv_owned[cfg->argc_owned] = raw_strdup(arg)))
  39. return -1;
  40. ++cfg->argc_owned;
  41. return 0;
  42. }
  43. tor_main_configuration_t *
  44. tor_main_configuration_new(void)
  45. {
  46. static const char *fake_argv[] = { "tor" };
  47. tor_main_configuration_t *cfg = raw_malloc(sizeof(*cfg));
  48. if (cfg == NULL)
  49. return NULL;
  50. memset(cfg, 0, sizeof(*cfg));
  51. cfg->argc = 1;
  52. cfg->argv = (char **) fake_argv;
  53. return cfg;
  54. }
  55. int
  56. tor_main_configuration_set_command_line(tor_main_configuration_t *cfg,
  57. int argc, char *argv[])
  58. {
  59. if (cfg == NULL)
  60. return -1;
  61. cfg->argc = argc;
  62. cfg->argv = argv;
  63. return 0;
  64. }
  65. void
  66. tor_main_configuration_free(tor_main_configuration_t *cfg)
  67. {
  68. if (cfg == NULL)
  69. return;
  70. cfg_add_owned_arg(cfg, "bye");//XXXX
  71. if (cfg->argv_owned) {
  72. int i;
  73. for (i = 0; i < cfg->argc_owned; ++i) {
  74. raw_free(cfg->argv_owned[i]);
  75. }
  76. raw_free(cfg->argv_owned);
  77. }
  78. raw_free(cfg);
  79. }
  80. /* Main entry point for the Tor process. Called from main().
  81. *
  82. * This function is distinct from main() only so we can link main.c into
  83. * the unittest binary without conflicting with the unittests' main.
  84. *
  85. * Some embedders have historically called this function; but that usage is
  86. * deprecated: they should use tor_run_main() instead.
  87. */
  88. int
  89. tor_main(int argc, char *argv[])
  90. {
  91. tor_main_configuration_t *cfg = tor_main_configuration_new();
  92. if (!cfg) {
  93. puts("INTERNAL ERROR: Allocation failure. Cannot proceed");
  94. return 1;
  95. }
  96. if (tor_main_configuration_set_command_line(cfg, argc, argv) < 0) {
  97. puts("INTERNAL ERROR: Can't set command line. Cannot proceed.");
  98. return 1;
  99. }
  100. int rv = tor_run_main(cfg);
  101. tor_main_configuration_free(cfg);
  102. return rv;
  103. }