+#ifdef HAVE_MINGW
+# define setpriority(level) !SetPriorityClass(GetCurrentProcess(), (level))
+#else
+# define NORMAL_PRIORITY_CLASS 0
+# define BELOW_NORMAL_PRIORITY_CLASS 10
+# define HIGH_PRIORITY_CLASS -10
+# define setpriority(level) (setpriority(PRIO_PROCESS, 0, (level)))
+#endif
+
+int main(int argc, char **argv) {
+ program_name = argv[0];
+
+ if(!parse_options(argc, argv))
+ return 1;
+
+ make_names();
+
+ if(show_version) {
+ printf("%s version %s\n", PACKAGE, VERSION);
+ printf("Copyright (C) 1998-2017 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;
+ }
+
+ 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);
+
+ /* Slllluuuuuuurrrrp! */
+
+ RAND_load_file("/dev/urandom", 1024);
+
+ ENGINE_load_builtin_engines();
+ ENGINE_register_all_complete();
+
+ OpenSSL_add_all_algorithms();
+
+ 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);
+
+ EVP_cleanup();
+ ENGINE_cleanup();
+ CRYPTO_cleanup_all_ex_data();
+#ifdef HAVE_ERR_REMOVE_STATE
+ // OpenSSL claims this function was deprecated in 1.0.0,
+ // but valgrind's leak detector shows you still need to call it to make sure OpenSSL cleans up properly.
+ ERR_remove_state(0);
+#endif
+ ERR_free_strings();
+
+ exit_configuration(&config_tree);
+ list_free(cmdline_conf);
+ free_names();
+
+ return status;
+}