main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <errno.h>
  32. #include <string>
  33. #include <stdexcept>
  34. #include <iostream>
  35. #ifdef _WIN32
  36. #include <Windows.h>
  37. #else
  38. #include <unistd.h>
  39. #include <pwd.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <signal.h>
  43. #endif
  44. #include "node/Node.hpp"
  45. #include "node/Utils.hpp"
  46. #include "node/Defaults.hpp"
  47. #include "launcher.h"
  48. using namespace ZeroTier;
  49. static Node *node = (Node *)0;
  50. static void printHelp(const char *cn,FILE *out)
  51. {
  52. fprintf(out,"ZeroTier One version %d.%d.%d\n(c)2012-2013 ZeroTier Networks LLC\nLicensed under the GNU General Public License v3\n\nUsage: %s <home directory>\n",Node::versionMajor(),Node::versionMinor(),Node::versionRevision(),cn);
  53. }
  54. #ifndef _WIN32
  55. static void sighandlerQuit(int sig)
  56. {
  57. Node *n = node;
  58. if (n)
  59. n->terminate();
  60. else exit(0);
  61. }
  62. static void sighandlerUsr(int sig)
  63. {
  64. }
  65. #endif
  66. int main(int argc,char **argv)
  67. {
  68. #ifndef _WIN32
  69. signal(SIGHUP,SIG_IGN);
  70. signal(SIGPIPE,SIG_IGN);
  71. signal(SIGUSR1,&sighandlerUsr);
  72. signal(SIGUSR2,&sighandlerUsr);
  73. signal(SIGALRM,SIG_IGN);
  74. signal(SIGINT,&sighandlerQuit);
  75. signal(SIGTERM,&sighandlerQuit);
  76. signal(SIGQUIT,&sighandlerQuit);
  77. #endif
  78. if (argc < 2) {
  79. printHelp(argv[0],stderr);
  80. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  81. }
  82. const char *homeDir = (const char *)0;
  83. for(int i=1;i<argc;++i) {
  84. if (argv[i][0] == '-') {
  85. switch(argv[i][1]) {
  86. default:
  87. printHelp(argv[0],stderr);
  88. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  89. }
  90. } else {
  91. if (homeDir) {
  92. printHelp(argv[0],stderr);
  93. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  94. }
  95. homeDir = argv[i];
  96. break;
  97. }
  98. }
  99. if ((!homeDir)||(strlen(homeDir) <= 0)) {
  100. printHelp(argv[0],stderr);
  101. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  102. }
  103. #ifndef _WIN32
  104. mkdir(homeDir,0755); // will fail if it already exists
  105. #endif
  106. int exitCode = ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  107. node = new Node(homeDir,ZT_DEFAULTS.configUrlPrefix.c_str(),ZT_DEFAULTS.configAuthority.c_str());
  108. switch(node->run()) {
  109. case Node::NODE_RESTART_FOR_RECONFIGURATION:
  110. exitCode = ZT_EXEC_RETURN_VALUE_PLEASE_RESTART;
  111. break;
  112. case Node::NODE_UNRECOVERABLE_ERROR:
  113. exitCode = ZT_EXEC_RETURN_VALUE_UNRECOVERABLE_ERROR;
  114. break;
  115. case Node::NODE_NEW_VERSION_AVAILABLE:
  116. exitCode = ZT_EXEC_RETURN_VALUE_TERMINATED_FOR_UPGRADE;
  117. break;
  118. default:
  119. break;
  120. }
  121. delete node;
  122. node = (Node *)0;
  123. return exitCode;
  124. }