Version 0.1.3 from FTP
[asterisk/asterisk.git] / asterisk.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Top level source file for asterisk
5  * 
6  * Copyright (C) 1999, Adtran Inc. and Linux Support Services, LLC
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/options.h>
18 #include <stdio.h>
19 #include <signal.h>
20 #include "asterisk.h"
21
22 int option_verbose=0;
23 int option_debug=0;
24 int option_nofork=0;
25 int option_quiet=0;
26
27 static void urg_handler(int num)
28 {
29         /* Called by soft_hangup to interrupt the select, read, or other
30            system call.  We don't actually need to do anything though.  */
31         if (option_debug)
32                 ast_log(LOG_DEBUG, "Urgent handler\n");
33         return;
34 }
35
36 static void quit_handler(int num)
37 {
38         /* Called on exit */
39         if (option_verbose)
40                 ast_verbose("Asterisk ending (%d).\n", num);
41         else if (option_debug)
42                 ast_log(LOG_DEBUG, "Asterisk ending (%d).\n", num);
43         exit(0);
44 }
45
46 int main(int argc, char *argv[])
47 {
48         char c;
49         /* Check if we're root */
50         if (geteuid()) {
51                 ast_log(LOG_ERROR, "Must be run as root\n");
52                 exit(1);
53         }
54         /* Check for options */
55         while((c=getopt(argc, argv, "dvq")) != EOF) {
56                 switch(c) {
57                 case 'd':
58                         option_debug++;
59                         option_nofork++;
60                         option_verbose++;
61                         break;
62                 case 'v':
63                         option_verbose++;
64                         break;
65                 case 'q':
66                         option_quiet++;
67                         break;
68                 case '?':
69                         exit(1);
70                 }
71         }
72         /* Print a welcome message if desired */
73         if (option_verbose) {
74                 ast_verbose( "Asterisk, Copyright (C) 1999 Adtran, Inc. and Linux Support Services, LLC\n");
75                 ast_verbose( "Written by Mark Spencer <markster@linux-support.net>\n");
76                 ast_verbose( "=========================================================================\n");
77         }
78         signal(SIGURG, urg_handler);
79         signal(SIGINT, quit_handler);
80         signal(SIGTERM, quit_handler);
81         signal(SIGHUP, quit_handler);
82         if (init_logger())
83                 exit(1);
84         if (load_pbx())
85                 exit(1);
86         if (load_modules())
87                 exit(1);
88         /* We might have the option of showing a console, but for now just
89            do nothing... */
90         if (option_verbose)
91                 ast_verbose( "Asterisk Ready.\n");
92         select(0,NULL,NULL,NULL,NULL);
93         return 0;
94 }