-void
-setup_signals(void)
-{
- int i;
-
- for(i=0;i<32;i++)
- signal(i,sighuh);
-
- if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
- signal(SIGTERM, sigterm_handler);
- if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
- signal(SIGQUIT, sigquit_handler);
- if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
- signal(SIGSEGV, sigsegv_handler);
- if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
- signal(SIGHUP, sighup_handler);
- signal(SIGPIPE, SIG_IGN);
- if(signal(SIGINT, SIG_IGN) != SIG_ERR)
- signal(SIGINT, sigint_handler);
- signal(SIGUSR1, sigusr1_handler);
- signal(SIGUSR2, sigusr2_handler);
- signal(SIGCHLD, parent_exit);
+int main(int argc, char **argv) {
+ program_name = argv[0];
+
+ if(!parse_options(argc, argv)) {
+ return 1;
+ }
+
+ if(show_version) {
+ fprintf(stdout,
+ "%s version %s (built %s %s, protocol %d.%d)\n"
+ "Features:"
+#ifdef HAVE_OPENSSL
+ " openssl"
+#endif
+#ifdef HAVE_LIBGCRYPT
+ " libgcrypt"
+#endif
+#ifdef HAVE_LZO
+ " comp_lzo"
+#endif
+#ifdef HAVE_ZLIB
+ " comp_zlib"
+#endif
+#ifdef HAVE_LZ4
+ " comp_lz4"
+#endif
+#ifndef DISABLE_LEGACY
+ " legacy_protocol"
+#endif
+#ifdef ENABLE_JUMBOGRAMS
+ " jumbograms"
+#endif
+#ifdef ENABLE_TUNEMU
+ " tunemu"
+#endif
+#ifdef HAVE_MINIUPNPC
+ " miniupnpc"
+#endif
+#ifdef HAVE_SANDBOX
+ " sandbox"
+#endif
+#ifdef ENABLE_UML
+ " uml"
+#endif
+#ifdef ENABLE_VDE
+ " vde"
+#endif
+ "\n\n"
+ "Copyright (C) 1998-2021 Ivo Timmermans, Guus Sliepen and others.\n"
+ "See the AUTHORS file for a complete list.\n"
+ "\n"
+ "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
+ "and you are welcome to redistribute it under certain conditions;\n"
+ "see the file COPYING for details.\n",
+ PACKAGE, BUILD_VERSION, BUILD_DATE, BUILD_TIME, PROT_MAJOR, PROT_MINOR);
+ return 0;
+ }
+
+ if(show_help) {
+ usage(false);
+ return 0;
+ }
+
+ make_names(true);
+ atexit(cleanup);
+
+ if(chdir(confbase) == -1) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Could not change to configuration directory: %s", strerror(errno));
+ return 1;
+ }
+
+#ifdef HAVE_WINDOWS
+
+ if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
+ return 1;
+ }
+
+#else
+ // Check if we got an umbilical fd from the process that started us
+ char *umbstr = getenv("TINC_UMBILICAL");
+
+ if(umbstr) {
+ int colorize = 0;
+ sscanf(umbstr, "%d %d", &umbilical, &colorize);
+ umbilical_colorize = colorize;
+
+ if(fcntl(umbilical, F_GETFL) < 0) {
+ umbilical = 0;
+ }
+
+#ifdef FD_CLOEXEC
+
+ if(umbilical) {
+ fcntl(umbilical, F_SETFD, FD_CLOEXEC);
+ }
+
+#endif
+ }
+
+#endif
+
+ openlogger("tinc", use_logfile ? LOGMODE_FILE : LOGMODE_STDERR);
+
+ g_argv = argv;
+
+ if(getenv("LISTEN_PID") && atoi(getenv("LISTEN_PID")) == getpid()) {
+ do_detach = false;
+ }
+
+#ifdef HAVE_UNSETENV
+ unsetenv("LISTEN_PID");
+#endif
+
+ gettimeofday(&now, NULL);
+ random_init();
+ crypto_init();
+ prng_init();
+
+ if(!read_server_config(&config_tree)) {
+ return 1;
+ }
+
+ if(!read_sandbox_level()) {
+ return 1;
+ }
+
+ if(debug_level == DEBUG_NOTHING) {
+ int level = 0;
+
+ if(get_config_int(lookup_config(&config_tree, "LogLevel"), &level)) {
+ debug_level = level;
+ }
+ }
+
+#ifdef HAVE_LZO
+
+ if(lzo_init() != LZO_E_OK) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error initializing LZO compressor!");
+ return 1;
+ }
+
+#endif
+
+#ifdef HAVE_WINDOWS
+ io_add_event(&stop_io, stop_handler, NULL, WSACreateEvent());
+
+ if(stop_io.event == FALSE) {
+ abort();
+ }
+
+ int result;
+
+ if(!do_detach || !init_service()) {
+ SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
+ result = main2(argc, argv);
+ } else {
+ result = 1;
+ }
+
+ if(WSACloseEvent(stop_io.event) == FALSE) {
+ abort();
+ }
+
+ io_del(&stop_io);
+ return result;