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