1/***********************************************************************
2*
3* main.c
4*
5* Main program of l2tp
6*
7* Copyright (C) 2002 by Roaring Penguin Software Inc.
8*
9* This software may be distributed under the terms of the GNU General
10* Public License, Version 2, or (at your option) any later version.
11*
12* LIC: GPL
13*
14***********************************************************************/
15
16static char const RCSID[] =
17"$Id: main.c.orig,v 1.1.1.1 2008/10/15 03:31:00 james26_jang Exp $";
18
19#include "l2tp.h"
20#include <stdio.h>
21#include <string.h>
22#include <getopt.h>
23#include <signal.h>
24#include <fcntl.h>
25#include <stdlib.h>
26
27static void
28usage(int argc, char *argv[], int exitcode)
29{
30    fprintf(stderr, "\nl2tpd Version %s Copyright 2002 Roaring Penguin Software Inc.\n", VERSION);
31    fprintf(stderr, "http://www.roaringpenguin.com/\n\n");
32    fprintf(stderr, "Usage: %s [options]\n", argv[0]);
33    fprintf(stderr, "Options:\n");
34    fprintf(stderr, "-d level               -- Set debugging to 'level'\n");
35    fprintf(stderr, "-f                     -- Do not fork\n");
36    fprintf(stderr, "-h                     -- Print usage\n");
37    fprintf(stderr, "\nThis program is licensed under the terms of\nthe GNU General Public License, Version 2.\n");
38    exit(exitcode);
39}
40
41int
42main(int argc, char *argv[])
43{
44    EventSelector *es = Event_CreateSelector();
45    int i;
46    int opt;
47    int do_fork = 1;
48    int debugmask = 0;
49
50    while((opt = getopt(argc, argv, "d:fh")) != -1) {
51	switch(opt) {
52	case 'h':
53	    usage(argc, argv, EXIT_SUCCESS);
54	    break;
55	case 'f':
56	    do_fork = 0;
57	    break;
58	case 'd':
59	    sscanf(optarg, "%d", &debugmask);
60	    break;
61	default:
62	    usage(argc, argv, EXIT_FAILURE);
63	}
64    }
65
66    l2tp_random_init();
67    l2tp_tunnel_init(es);
68    l2tp_peer_init();
69    l2tp_debug_set_bitmask(debugmask);
70
71    if (l2tp_parse_config_file(es, "/etc/l2tp/l2tp.conf") < 0) {
72	l2tp_die();
73    }
74
75    if (!l2tp_network_init(es)) {
76	l2tp_die();
77    }
78
79    /* Daemonize */
80    if (do_fork) {
81	i = fork();
82	if (i < 0) {
83	    perror("fork");
84	    exit(EXIT_FAILURE);
85	} else if (i != 0) {
86	    /* Parent */
87	    exit(EXIT_SUCCESS);
88	}
89
90	setsid();
91	signal(SIGHUP, SIG_IGN);
92	i = fork();
93	if (i < 0) {
94	    perror("fork");
95	    exit(EXIT_FAILURE);
96	} else if (i != 0) {
97	    exit(EXIT_SUCCESS);
98	}
99
100	chdir("/");
101
102	/* Point stdin/stdout/stderr to /dev/null */
103	for (i=0; i<3; i++) {
104	    close(i);
105	}
106	i = open("/dev/null", O_RDWR);
107	if (i >= 0) {
108	    dup2(i, 0);
109	    dup2(i, 1);
110	    dup2(i, 2);
111	    if (i > 2) close(i);
112	}
113    }
114
115    while(1) {
116	i = Event_HandleEvent(es);
117	if (i < 0) {
118	    fprintf(stderr, "Event_HandleEvent returned %d\n", i);
119	    l2tp_cleanup();
120	    exit(EXIT_FAILURE);
121	}
122    }
123    return 0;
124}
125