static void l2tp_init(void) { l2tp_log(LOG_INFO, "started"); #ifdef L2TP_DMALLOC /* dmalloc debug options are set in the environment. However, * certain options cause problems to this application. We * therefore ensure that the troublesome options are disabled, * regardless of the user's settings. The disabled options * are: alloc-blank, free-blank, force-linear. If these * options are enabled, it causes strange problems in the * generated RPC code. */ dmalloc_debug(dmalloc_debug_current() & 0xff5dffff); l2tp_dmalloc_mark = dmalloc_mark(); if (getenv("DMALLOC_OPTIONS") != NULL) { l2tp_log(LOG_WARNING, "DMALLOC debugging enabled"); } #endif l2tp_my_pid = getpid(); atexit(l2tp_cleanup); L2TP_DEBUG(L2TP_FUNC, "%s (%s %s): trace flags = %08lx", __FUNCTION__, __DATE__, __TIME__, l2tp_opt_trace_flags); usl_set_debug(l2tp_opt_debug, l2tp_system_log); usl_signal_terminate_hook = l2tp_die; usl_signal_init(); usl_fd_init(); usl_timer_init(); usl_pid_init(); l2tp_net_init(); l2tp_rand_fd = open("/dev/random", O_RDONLY); if (l2tp_rand_fd < 0) { fprintf(stderr, "No /dev/random device found. Exiting.\n"); exit(1); } usl_signal_notifier_add(l2tp_signal_handler, NULL); l2tp_avp_init(); l2tp_peer_init(); l2tp_api_init(); l2tp_xprt_init(); l2tp_tunnel_init(); l2tp_session_init(); l2tp_ppp_init(); }
int main(int argc, char *argv[]) { EventSelector *es = Event_CreateSelector(); int i; int opt; int do_fork = 1; int debugmask = 0; while((opt = getopt(argc, argv, "d:fh")) != -1) { switch(opt) { case 'h': usage(argc, argv, EXIT_SUCCESS); break; case 'f': do_fork = 0; break; case 'd': sscanf(optarg, "%d", &debugmask); break; default: usage(argc, argv, EXIT_FAILURE); } } l2tp_random_init(); l2tp_tunnel_init(es); l2tp_peer_init(); l2tp_debug_set_bitmask(debugmask); if (l2tp_parse_config_file(es, "/etc/l2tp/l2tp.conf") < 0) { l2tp_die(); } if (!l2tp_network_init(es)) { l2tp_die(); } /* Daemonize */ if (do_fork) { i = fork(); if (i < 0) { perror("fork"); exit(EXIT_FAILURE); } else if (i != 0) { /* Parent */ exit(EXIT_SUCCESS); } setsid(); signal(SIGHUP, SIG_IGN); i = fork(); if (i < 0) { perror("fork"); exit(EXIT_FAILURE); } else if (i != 0) { exit(EXIT_SUCCESS); } chdir("/"); /* Point stdin/stdout/stderr to /dev/null */ for (i=0; i<3; i++) { close(i); } i = open("/dev/null", O_RDWR); if (i >= 0) { dup2(i, 0); dup2(i, 1); dup2(i, 2); if (i > 2) close(i); } } while(1) { i = Event_HandleEvent(es); if (i < 0) { fprintf(stderr, "Event_HandleEvent returned %d\n", i); l2tp_cleanup(); exit(EXIT_FAILURE); } } return 0; }