tor_api.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file tor_api.c
  8. **/
  9. #ifdef _WIN32
  10. #include <winsock2.h>
  11. #include <windows.h>
  12. #endif
  13. #include "feature/api/tor_api.h"
  14. // Include this after the above headers, to insure that they don't
  15. // depend on anything else.
  16. #include "orconfig.h"
  17. #include "lib/cc/torint.h"
  18. #include "feature/api/tor_api_internal.h"
  19. #include "lib/cc/compat_compiler.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. // We don't want to use tor_malloc and tor_free here, since this needs
  24. // to run before anything is initialized at all, and ought to run when
  25. // we're not linked to anything at all.
  26. #define raw_malloc malloc
  27. #define raw_free free
  28. #define raw_realloc realloc
  29. #define raw_strdup strdup
  30. #ifdef _WIN32
  31. #include "lib/net/socketpair.h"
  32. #define raw_socketpair tor_ersatz_socketpair
  33. #define raw_closesocket closesocket
  34. #define snprintf _snprintf
  35. #else
  36. #define raw_socketpair socketpair
  37. #define raw_closesocket close
  38. #endif
  39. #ifdef HAVE_UNISTD_H
  40. #include <unistd.h>
  41. #endif
  42. /**
  43. * Helper: Add a copy of <b>arg</b> to the owned arguments of <b>cfg</b>.
  44. * Return 0 on success, -1 on failure.
  45. */
  46. static int
  47. cfg_add_owned_arg(tor_main_configuration_t *cfg, const char *arg)
  48. {
  49. /* We aren't using amortized realloc here, because libc should do it for us,
  50. * and because this function is not critical-path. */
  51. char **new_argv = raw_realloc(cfg->argv_owned,
  52. sizeof(char*) * (cfg->argc_owned+1));
  53. if (new_argv == NULL)
  54. return -1;
  55. cfg->argv_owned = new_argv;
  56. if (NULL == (cfg->argv_owned[cfg->argc_owned] = raw_strdup(arg)))
  57. return -1;
  58. ++cfg->argc_owned;
  59. return 0;
  60. }
  61. tor_main_configuration_t *
  62. tor_main_configuration_new(void)
  63. {
  64. static const char *fake_argv[] = { "tor" };
  65. tor_main_configuration_t *cfg = raw_malloc(sizeof(*cfg));
  66. if (cfg == NULL)
  67. return NULL;
  68. memset(cfg, 0, sizeof(*cfg));
  69. cfg->argc = 1;
  70. cfg->argv = (char **) fake_argv;
  71. cfg->owning_controller_socket = TOR_INVALID_SOCKET;
  72. return cfg;
  73. }
  74. int
  75. tor_main_configuration_set_command_line(tor_main_configuration_t *cfg,
  76. int argc, char *argv[])
  77. {
  78. if (cfg == NULL)
  79. return -1;
  80. cfg->argc = argc;
  81. cfg->argv = argv;
  82. return 0;
  83. }
  84. tor_control_socket_t
  85. tor_main_configuration_setup_control_socket(tor_main_configuration_t *cfg)
  86. {
  87. if (SOCKET_OK(cfg->owning_controller_socket))
  88. return INVALID_TOR_CONTROL_SOCKET;
  89. tor_socket_t fds[2];
  90. if (raw_socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
  91. return INVALID_TOR_CONTROL_SOCKET;
  92. }
  93. char buf[32];
  94. snprintf(buf, sizeof(buf), "%"PRIu64, (uint64_t)fds[1]);
  95. cfg_add_owned_arg(cfg, "__OwningControllerFD");
  96. cfg_add_owned_arg(cfg, buf);
  97. cfg->owning_controller_socket = fds[1];
  98. return fds[0];
  99. }
  100. void
  101. tor_main_configuration_free(tor_main_configuration_t *cfg)
  102. {
  103. if (cfg == NULL)
  104. return;
  105. if (cfg->argv_owned) {
  106. for (int i = 0; i < cfg->argc_owned; ++i) {
  107. raw_free(cfg->argv_owned[i]);
  108. }
  109. raw_free(cfg->argv_owned);
  110. }
  111. if (SOCKET_OK(cfg->owning_controller_socket)) {
  112. raw_closesocket(cfg->owning_controller_socket);
  113. }
  114. raw_free(cfg);
  115. }
  116. const char *
  117. tor_api_get_provider_version(void)
  118. {
  119. return "tor " VERSION;
  120. }
  121. /* Main entry point for the Tor process. Called from main().
  122. *
  123. * This function is distinct from main() only so we can link main.c into
  124. * the unittest binary without conflicting with the unittests' main.
  125. *
  126. * Some embedders have historically called this function; but that usage is
  127. * deprecated: they should use tor_run_main() instead.
  128. */
  129. int
  130. tor_main(int argc, char *argv[])
  131. {
  132. tor_main_configuration_t *cfg = tor_main_configuration_new();
  133. if (!cfg) {
  134. puts("INTERNAL ERROR: Allocation failure. Cannot proceed");
  135. return 1;
  136. }
  137. if (tor_main_configuration_set_command_line(cfg, argc, argv) < 0) {
  138. puts("INTERNAL ERROR: Can't set command line. Cannot proceed.");
  139. return 1;
  140. }
  141. int rv = tor_run_main(cfg);
  142. tor_main_configuration_free(cfg);
  143. return rv;
  144. }