Tomato 1.28
[tomato.git] / release / src / router / rp-l2tp / main.c
blob01e5d6875d1ed2194eab182ea56df017f50f6a08
1 /***********************************************************************
3 * main.c
5 * Main program of l2tp
7 * Copyright (C) 2002 by Roaring Penguin Software Inc.
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.
12 * LIC: GPL
14 ***********************************************************************/
16 static char const RCSID[] =
17 "$Id: main.c,v 1.1.48.1 2005/08/08 12:05:25 honor Exp $";
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>
27 static void
28 usage(int argc, char *argv[], int exitcode)
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);
41 int
42 main(int argc, char *argv[])
44 EventSelector *es = Event_CreateSelector();
45 int i;
46 int opt;
47 int do_fork = 1;
48 int debugmask = 0;
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);
66 l2tp_random_init();
67 l2tp_tunnel_init(es);
68 l2tp_peer_init();
69 l2tp_debug_set_bitmask(debugmask);
71 if (l2tp_parse_config_file(es, "/tmp/l2tp.conf") < 0) { //2005-04-14 by kanki
72 l2tp_die();
75 if (!l2tp_network_init(es)) {
76 l2tp_die();
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);
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);
100 chdir("/");
102 /* Point stdin/stdout/stderr to /dev/null */
103 for (i=0; i<3; i++) {
104 close(i);
106 i = open("/dev/console", O_RDWR); //2005-04-14 by kanki for debugging
107 if (i >= 0) {
108 dup2(i, 0);
109 dup2(i, 1);
110 dup2(i, 2);
111 if (i > 2) close(i);
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);
123 return 0;