Version 0.1.2 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, Mark Spencer
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 <asterisk/cli.h>
19 #include <stdio.h>
20 #include <signal.h>
21 #include <sched.h>
22 #include <pthread.h>
23 #include <readline/readline.h>
24 #include <readline/history.h>
25 #include "asterisk.h"
26
27 int option_verbose=0;
28 int option_debug=0;
29 int option_nofork=0;
30 int option_quiet=0;
31 int option_console=0;
32 int option_highpriority=0;
33 int fully_booted = 0;
34
35 #define HIGH_PRIORITY 1
36 #define HIGH_PRIORITY_SCHED SCHED_RR
37
38 static void urg_handler(int num)
39 {
40         /* Called by soft_hangup to interrupt the select, read, or other
41            system call.  We don't actually need to do anything though.  */
42         if (option_debug)
43                 ast_log(LOG_DEBUG, "Urgent handler\n");
44         signal(num, urg_handler);
45         return;
46 }
47
48 static int set_priority(int pri)
49 {
50         struct sched_param sched;
51         /* We set ourselves to a high priority, that we might pre-empt everything
52            else.  If your PBX has heavy activity on it, this is a good thing.  */
53         if (pri) {  
54                 sched.sched_priority = HIGH_PRIORITY;
55                 if (sched_setscheduler(0, HIGH_PRIORITY_SCHED, &sched)) {
56                         ast_log(LOG_WARNING, "Unable to set high priority\n");
57                         return -1;
58                 }
59         } else {
60                 sched.sched_priority = 0;
61                 if (sched_setscheduler(0, SCHED_OTHER, &sched)) {
62                         ast_log(LOG_WARNING, "Unable to set normal priority\n");
63                         return -1;
64                 }
65         }
66         return 0;
67 }
68
69 static void quit_handler(int num)
70 {
71         static pthread_mutex_t quitlock = PTHREAD_MUTEX_INITIALIZER;
72         char filename[80] = "";
73         if (getenv("HOME")) 
74                 snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
75         /* Quit only once */
76         pthread_mutex_lock(&quitlock);
77         /* Called on exit */
78         if (option_verbose)
79                 ast_verbose("Asterisk ending (%d).\n", num);
80         else if (option_debug)
81                 ast_log(LOG_DEBUG, "Asterisk ending (%d).\n", num);
82         if (strlen(filename))
83                 write_history(filename);
84         exit(0);
85 }
86
87 static pthread_t consolethread = -1;
88
89 static void console_verboser(char *s, int pos, int replace, int complete)
90 {
91         /* Return to the beginning of the line */
92         if (!pos)
93                 fprintf(stdout, "\r");
94         fprintf(stdout, s + pos);
95         fflush(stdout);
96         if (complete)
97         /* Wake up a select()ing console */
98                 pthread_kill(consolethread, SIGURG);
99 }
100
101 static void consolehandler(char *s)
102 {
103         /* Called when readline data is available */
104         if (s && strlen(s))
105                 add_history(s);
106         /* Give the console access to the shell */
107         if (s) {
108                 if (s[0] == '!') {
109                         if (s[1])
110                                 system(s+1);
111                         else
112                                 system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
113                 } else 
114                 ast_cli_command(STDOUT_FILENO, s);
115                 if (!strcasecmp(s, "help"))
116                         fprintf(stdout, "          !<command>   Executes a given shell command\n");
117         } else
118                 fprintf(stdout, "\nUse \"quit\" to exit\n");
119 }
120
121 static char quit_help[] = 
122 "Usage: quit\n"
123 "       Exits Asterisk.\n";
124
125 static int handle_quit(int fd, int argc, char *argv[])
126 {
127         if (argc != 1)
128                 return RESULT_SHOWUSAGE;
129         quit_handler(0);
130         return RESULT_SUCCESS;
131 }
132
133 #define ASTERISK_PROMPT "*CLI> "
134
135 static struct ast_cli_entry quit =      { { "quit", NULL }, handle_quit, "Exit Asterisk", quit_help };
136
137 static char *cli_generator(char *text, int state)
138 {
139         return ast_cli_generator(rl_line_buffer, text, state);
140 }
141
142 int main(int argc, char *argv[])
143 {
144         char c;
145         fd_set rfds;
146         int res;
147         char filename[80] = "";
148         if (getenv("HOME")) 
149                 snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
150         /* Check if we're root */
151         if (geteuid()) {
152                 ast_log(LOG_ERROR, "Must be run as root\n");
153                 exit(1);
154         }
155         /* Check for options */
156         while((c=getopt(argc, argv, "dvqpc")) != EOF) {
157                 switch(c) {
158                 case 'd':
159                         option_debug++;
160                         option_nofork++;
161                         break;
162                 case 'c':
163                         option_console++;
164                         option_nofork++;
165                 case 'p':
166                         option_highpriority++;
167                         break;
168                 case 'v':
169                         option_verbose++;
170                         option_nofork++;
171                         break;
172                 case 'q':
173                         option_quiet++;
174                         break;
175                 case '?':
176                         exit(1);
177                 }
178         }
179         ast_register_verbose(console_verboser);
180         /* Print a welcome message if desired */
181         if (option_verbose || option_console) {
182                 ast_verbose( "Asterisk, Copyright (C) 1999 Mark Spencer\n");
183                 ast_verbose( "Written by Mark Spencer <markster@linux-support.net>\n");
184                 ast_verbose( "=========================================================================\n");
185         }
186         if (option_console && !option_verbose) 
187                 ast_verbose("[ Booting...");
188         signal(SIGURG, urg_handler);
189         signal(SIGINT, quit_handler);
190         signal(SIGTERM, quit_handler);
191         signal(SIGHUP, quit_handler);
192         if (init_logger())
193                 exit(1);
194         if (load_pbx())
195                 exit(1);
196         if (load_modules())
197                 exit(1);
198         if (set_priority(option_highpriority))
199                 exit(1);
200         /* We might have the option of showing a console, but for now just
201            do nothing... */
202         if (option_console && !option_verbose)
203                 ast_verbose(" ]\n");
204         if (option_verbose || option_console)
205                 ast_verbose( "Asterisk Ready.\n");
206         fully_booted = 1;
207         if (option_console) {
208                 /* Console stuff now... */
209                 /* Register our quit function */
210             ast_cli_register(&quit);
211                 consolethread = pthread_self();
212                 if (strlen(filename))
213                         read_history(filename);
214                 rl_callback_handler_install(ASTERISK_PROMPT, consolehandler);
215                 rl_completion_entry_function = (Function *)cli_generator;
216                 for(;;) {
217                         FD_ZERO(&rfds);
218                         FD_SET(STDIN_FILENO, &rfds);
219                         res = select(STDIN_FILENO + 1, &rfds, NULL, NULL, NULL);
220                         if (res > 0) {
221                                 rl_callback_read_char();
222                         } else if (res < 1) {
223                                 rl_forced_update_display();
224                         }
225         
226                 }       
227         } else {
228                 /* Do nothing */
229                 select(0,NULL,NULL,NULL,NULL);
230         }
231         return 0;
232 }