usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / rp-l2tp / main.c
blobdb7d7b6a335df2bf37f03e5971929b6b671bddef
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>
26 #include <syslog.h>
28 static void
29 usage(int argc, char *argv[], int exitcode)
31 fprintf(stderr, "\nl2tpd Version %s Copyright 2002 Roaring Penguin Software Inc.\n", VERSION);
32 fprintf(stderr, "http://www.roaringpenguin.com/\n\n");
33 fprintf(stderr, "Usage: %s [options]\n", argv[0]);
34 fprintf(stderr, "Options:\n");
35 fprintf(stderr, "-d level -- Set debugging to 'level'\n");
36 fprintf(stderr, "-f -- Do not fork\n");
37 fprintf(stderr, "-h -- Print usage\n");
38 fprintf(stderr, "\nThis program is licensed under the terms of\nthe GNU General Public License, Version 2.\n");
39 exit(exitcode);
42 static void
43 sighandler(int signum)
45 l2tp_cleanup();
46 exit(EXIT_FAILURE);
49 int
50 main(int argc, char *argv[])
52 EventSelector *es = Event_CreateSelector();
53 int i;
54 int opt;
55 int do_fork = 1;
56 int debugmask = 0;
58 while((opt = getopt(argc, argv, "d:fh")) != -1) {
59 switch(opt) {
60 case 'h':
61 usage(argc, argv, EXIT_SUCCESS);
62 break;
63 case 'f':
64 do_fork = 0;
65 break;
66 case 'd':
67 sscanf(optarg, "%d", &debugmask);
68 break;
69 default:
70 usage(argc, argv, EXIT_FAILURE);
74 openlog(argv[0], LOG_PID, LOG_DAEMON);
75 l2tp_random_init();
76 l2tp_tunnel_init(es);
77 l2tp_peer_init();
78 l2tp_debug_set_bitmask(debugmask);
80 if (l2tp_parse_config_file(es, "/tmp/l2tp.conf") < 0) { //2005-04-14 by kanki
81 l2tp_die();
84 if (!l2tp_network_init(es)) {
85 l2tp_die();
88 /* Daemonize */
89 if (do_fork) {
90 i = fork();
91 if (i < 0) {
92 perror("fork");
93 exit(EXIT_FAILURE);
94 } else if (i != 0) {
95 /* Parent */
96 exit(EXIT_SUCCESS);
99 setsid();
100 signal(SIGHUP, SIG_IGN);
101 i = fork();
102 if (i < 0) {
103 perror("fork");
104 exit(EXIT_FAILURE);
105 } else if (i != 0) {
106 exit(EXIT_SUCCESS);
109 chdir("/");
111 /* Point stdin/stdout/stderr to /dev/null */
112 for (i=0; i<3; i++) {
113 close(i);
115 i = open("/dev/console", O_RDWR); //2005-04-14 by kanki for debugging
116 if (i >= 0) {
117 dup2(i, 0);
118 dup2(i, 1);
119 dup2(i, 2);
120 if (i > 2) close(i);
124 signal(SIGTERM, sighandler);
125 signal(SIGINT, sighandler);
127 while(1) {
128 i = Event_HandleEvent(es);
129 if (i < 0) {
130 fprintf(stderr, "Event_HandleEvent returned %d\n", i);
131 l2tp_cleanup();
132 exit(EXIT_FAILURE);
135 return 0;