- program_name = argv[0];
-
- setlocale (LC_ALL, "");
- bindtextdomain (PACKAGE, LOCALEDIR);
- textdomain (PACKAGE);
-
- environment = envp;
- parse_options(argc, argv, envp);
-
- if(show_version)
- {
- printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
- printf(_("Copyright (C) 1998-2001 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(0);
-
- if(geteuid())
- {
- fprintf(stderr, _("You must be root to run this program.\n"));
- return 1;
- }
-
-#ifdef HAVE_SOLARIS
- openlog("tinc", LOG_CONS, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */
-#else
- openlog("tinc", LOG_PERROR, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */
+ program_name = argv[0];
+
+ setlocale(LC_ALL, "");
+ bindtextdomain(PACKAGE, LOCALEDIR);
+ textdomain(PACKAGE);
+
+ if(!parse_options(argc, argv))
+ return 1;
+
+ make_names();
+
+ if(show_version) {
+ printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
+ VERSION, __DATE__, __TIME__, PROT_CURRENT);
+ printf(_("Copyright (C) 1998-2007 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);
+
+ /* Lock all pages into memory if requested */
+
+ if(do_mlock)
+#ifdef HAVE_MLOCKALL
+ if(mlockall(MCL_CURRENT | MCL_FUTURE)) {
+ logger(LOG_ERR, _("System call `%s' failed: %s"), "mlockall",
+ strerror(errno));
+#else
+ {
+ logger(LOG_ERR, _("mlockall() not supported on this platform!"));
+#endif
+ return -1;
+ }
+
+ g_argv = argv;
+
+ init_configuration(&config_tree);
+
+ /* Slllluuuuuuurrrrp! */
+
+ srand(time(NULL));
+ 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;
+
+ if(!event_init()) {
+ logger(LOG_ERR, _("Error initializing libevent!"));
+ return 1;
+ }
+
+ if(lzo_init() != LZO_E_OK) {
+ logger(LOG_ERR, _("Error initializing LZO compressor!"));
+ return 1;
+ }
+
+#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)
+{
+#endif
+
+ if(!detach())
+ return 1;
+
+
+ /* Setup sockets and open device. */
+
+ if(!setup_network_connections())
+ goto end;
+
+ /* Start main loop. It only exits when tinc is killed. */
+
+ status = main_loop();
+
+ /* Shutdown properly. */
+
+ close_network_connections();
+
+ ifdebug(CONNECTIONS)
+ dump_device_stats();
+
+end:
+ logger(LOG_NOTICE, _("Terminating"));
+
+#ifndef HAVE_MINGW
+ remove_pid(pidfilename);