main.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #if !defined(_WIN32) && !defined(_WIN64)
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #endif
  19. #include "zerotier_cgo.h"
  20. int main(int argc,char **argv)
  21. {
  22. // Fork into background if run with 'service -d'. This is best done prior
  23. // to launching the Go code, since Go likes to start thread pools and stuff
  24. // that don't play nice with fork. This is obviously unsupported on Windows.
  25. #if !defined(_WIN32) && !defined(_WIN64)
  26. for(int i=1;i<argc;) {
  27. if (strcmp(argv[i++], "service") == 0) {
  28. for(;i<argc;) {
  29. if (strcmp(argv[i++], "-d") == 0) {
  30. long p = (long)fork();
  31. if (p < 0) {
  32. fprintf(stderr,"FATAL: fork() failed!\n");
  33. return -1;
  34. } else if (p > 0) {
  35. return 0;
  36. }
  37. break;
  38. }
  39. }
  40. break;
  41. }
  42. }
  43. #endif
  44. ZeroTierMain();
  45. return 0;
  46. }