main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "launcher.h"
  47. using namespace ZeroTier;
  48. static Node *node = (Node *)0;
  49. static void printHelp(const char *cn,FILE *out)
  50. {
  51. 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);
  52. }
  53. #ifndef _WIN32
  54. static void sighandlerQuit(int sig)
  55. {
  56. Node *n = node;
  57. if (n)
  58. n->terminate();
  59. else exit(0);
  60. }
  61. #endif
  62. int main(int argc,char **argv)
  63. {
  64. #ifndef _WIN32
  65. signal(SIGHUP,SIG_IGN);
  66. signal(SIGPIPE,SIG_IGN);
  67. signal(SIGUSR1,SIG_IGN);
  68. signal(SIGUSR2,SIG_IGN);
  69. signal(SIGALRM,SIG_IGN);
  70. signal(SIGINT,&sighandlerQuit);
  71. signal(SIGTERM,&sighandlerQuit);
  72. signal(SIGQUIT,&sighandlerQuit);
  73. #endif
  74. if (argc < 2) {
  75. printHelp(argv[0],stderr);
  76. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  77. }
  78. const char *homeDir = (const char *)0;
  79. for(int i=1;i<argc;++i) {
  80. if (argv[i][0] == '-') {
  81. switch(argv[i][1]) {
  82. default:
  83. printHelp(argv[0],stderr);
  84. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  85. }
  86. } else {
  87. if (homeDir) {
  88. printHelp(argv[0],stderr);
  89. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  90. }
  91. homeDir = argv[i];
  92. break;
  93. }
  94. }
  95. if ((!homeDir)||(strlen(homeDir) <= 0)) {
  96. printHelp(argv[0],stderr);
  97. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  98. }
  99. #ifndef _WIN32
  100. mkdir(homeDir,0755); // will fail if it already exists
  101. #endif
  102. int exitCode = ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  103. node = new Node(homeDir);
  104. const char *termReason = (char *)0;
  105. switch(node->run()) {
  106. case Node::NODE_RESTART_FOR_RECONFIGURATION:
  107. exitCode = ZT_EXEC_RETURN_VALUE_PLEASE_RESTART;
  108. break;
  109. case Node::NODE_UNRECOVERABLE_ERROR:
  110. exitCode = ZT_EXEC_RETURN_VALUE_UNRECOVERABLE_ERROR;
  111. termReason = node->reasonForTermination();
  112. fprintf(stderr,"%s: abnormal termination: %s\n",argv[0],(termReason) ? termReason : "(unknown reason)");
  113. break;
  114. case Node::NODE_NEW_VERSION_AVAILABLE:
  115. exitCode = ZT_EXEC_RETURN_VALUE_TERMINATED_FOR_UPGRADE;
  116. break;
  117. default:
  118. break;
  119. }
  120. delete node;
  121. node = (Node *)0;
  122. return exitCode;
  123. }