示例#1
0
文件: process.c 项目: codoranro/tinc
static RETSIGTYPE fatal_signal_handler(int a) {
	struct sigaction act;
	logger(LOG_ERR, "Got fatal signal %d (%s)", a, strsignal(a));

	if(do_detach) {
		logger(LOG_NOTICE, "Trying to re-execute in 5 seconds...");

		act.sa_handler = fatal_signal_square;
		act.sa_mask = emptysigset;
		act.sa_flags = 0;
		sigaction(SIGSEGV, &act, NULL);

		close_network_connections();
		sleep(5);
		remove_pid(pidfilename);
		execvp(g_argv[0], g_argv);
	} else {
		logger(LOG_NOTICE, "Not restarting.");
		exit(1);
	}
}
示例#2
0
文件: tincd.c 项目: Rumko/tinc
int main2(int argc, char **argv) {
	InitializeCriticalSection(&mutex);
	EnterCriticalSection(&mutex);
#endif

	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 */

        char *priority = 0;

        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)
		dump_device_stats();

	close_network_connections();

end:
	logger(LOG_NOTICE, "Terminating");

#ifndef HAVE_MINGW
	remove_pid(pidfilename);
#endif

	EVP_cleanup();
	ENGINE_cleanup();
	CRYPTO_cleanup_all_ex_data();
	ERR_remove_state(0);
	ERR_free_strings();

	exit_configuration(&config_tree);
	free_names();

	return status;
}
示例#3
0
文件: tincd.c 项目: xentec/tinc
int main2(int argc, char **argv) {
#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(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "mlockall",
               strerror(errno));
        return 1;
    }
#endif

    /* Setup sockets and open device. */

    if(!setup_network())
        goto end;

    /* Change process priority */

    if(get_config_string(lookup_config(config_tree, "ProcessPriority"), &priority)) {
        if(!strcasecmp(priority, "Normal")) {
            if (setpriority(NORMAL_PRIORITY_CLASS) != 0) {
                logger(DEBUG_ALWAYS, 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(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "setpriority", strerror(errno));
                goto end;
            }
        } else if(!strcasecmp(priority, "High")) {
            if (setpriority(HIGH_PRIORITY_CLASS) != 0) {
                logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "setpriority", strerror(errno));
                goto end;
            }
        } else {
            logger(DEBUG_ALWAYS, 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. */

    logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");

    if(umbilical) { // snip!
        write(umbilical, "", 1);
        close(umbilical);
        umbilical = 0;
    }

    try_outgoing_connections();

    status = main_loop();

    /* Shutdown properly. */

end:
    close_network_connections();

    logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");

    free(priority);

    crypto_exit();

    exit_configuration(&config_tree);
    free(cmdline_conf);
    free_names();

    return status;
}