+int main(int argc, char **argv) {
+ program_name = argv[0];
+
+ if(!parse_options(argc, argv)) {
+ return 1;
+ }
+
+ if(show_version) {
+ printf("%s version %s\n", PACKAGE, VERSION);
+ printf("Copyright (C) 1998-2019 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");
+
+ return 0;
+ }
+
+ if(show_help) {
+ usage(false);
+ return 0;
+ }
+
+ make_names();
+
+ if(kill_tincd) {
+ return !kill_other(kill_tincd);
+ }
+
+ 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
+
+ init_configuration(&config_tree);
+
+#ifndef OPENSSL_NO_ENGINE
+ ENGINE_load_builtin_engines();
+ ENGINE_register_all_complete();
+#endif
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+ OpenSSL_add_all_algorithms();
+#endif
+
+ if(generate_keys) {
+ read_server_config();
+ return !keygen(generate_keys);
+ }
+
+ if(!read_server_config()) {
+ return 1;
+ }
+
+#ifdef HAVE_LZO
+
+ if(lzo_init() != LZO_E_OK) {
+ logger(LOG_ERR, "Error initializing LZO compressor!");
+ return 1;
+ }
+
+#endif
+
+#ifdef HAVE_MINGW
+
+ if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
+ logger(LOG_ERR, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
+ return 1;
+ }
+
+ if(!do_detach || !init_service()) {
+ return main2(argc, argv);
+ } else {
+ return 1;
+ }
+}
+
+int main2(int argc, char **argv) {
+ InitializeCriticalSection(&mutex);
+ EnterCriticalSection(&mutex);
+#endif
+ char *priority = NULL;
+
+ if(!detach()) {
+ return 1;
+ }
+
+#ifdef HAVE_MLOCKALL
+
+ /* Lock all pages into memory if requested.
+ * This has to be done after daemon()/fork() so it works for child.
+ * No need to do that in parent as it's very short-lived. */
+ if(do_mlock && mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
+ logger(LOG_ERR, "System call `%s' failed: %s", "mlockall",
+ strerror(errno));
+ return 1;
+ }
+
+#endif
+
+ /* Setup sockets and open device. */
+
+ if(!setup_network()) {
+ goto end;
+ }
+
+ /* Initiate all outgoing connections. */
+
+ try_outgoing_connections();
+
+ /* Change process priority */
+
+ if(get_config_string(lookup_config(config_tree, "ProcessPriority"), &priority)) {
+ if(!strcasecmp(priority, "Normal")) {
+ if(setpriority(NORMAL_PRIORITY_CLASS) != 0) {
+ logger(LOG_ERR, "System call `%s' failed: %s",
+ "setpriority", strerror(errno));
+ goto end;
+ }
+ } else if(!strcasecmp(priority, "Low")) {
+ if(setpriority(BELOW_NORMAL_PRIORITY_CLASS) != 0) {
+ logger(LOG_ERR, "System call `%s' failed: %s",
+ "setpriority", strerror(errno));
+ goto end;
+ }
+ } else if(!strcasecmp(priority, "High")) {
+ if(setpriority(HIGH_PRIORITY_CLASS) != 0) {
+ logger(LOG_ERR, "System call `%s' failed: %s",
+ "setpriority", strerror(errno));
+ goto end;
+ }
+ } else {
+ logger(LOG_ERR, "Invalid priority `%s`!", priority);
+ goto end;
+ }
+ }
+
+ /* drop privileges */
+ if(!drop_privs()) {
+ goto end;
+ }
+
+ /* Start main loop. It only exits when tinc is killed. */
+
+ status = main_loop();
+
+ /* Shutdown properly. */
+
+ ifdebug(CONNECTIONS)
+ devops.dump_stats();
+
+ close_network_connections();
+
+end:
+ logger(LOG_NOTICE, "Terminating");
+
+#ifndef HAVE_MINGW
+ remove_pid(pidfilename);
+#endif
+
+ free(priority);
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+ EVP_cleanup();
+ ERR_free_strings();
+#ifndef OPENSSL_NO_ENGINE
+ ENGINE_cleanup();
+#endif
+#endif
+
+ exit_configuration(&config_tree);
+ list_delete_list(cmdline_conf);
+ free_names();
+
+ return status;