2 * Asterisk -- A telephony toolkit for Linux.
4 * Top level source file for asterisk
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
16 #include <asterisk/logger.h>
17 #include <asterisk/options.h>
18 #include <asterisk/cli.h>
23 #include <readline/readline.h>
24 #include <readline/history.h>
31 int option_highpriority=0;
33 #define HIGH_PRIORITY 1
34 #define HIGH_PRIORITY_SCHED SCHED_RR
36 static void urg_handler(int num)
38 /* Called by soft_hangup to interrupt the select, read, or other
39 system call. We don't actually need to do anything though. */
41 ast_log(LOG_DEBUG, "Urgent handler\n");
45 static int set_priority(int pri)
47 struct sched_param sched;
48 /* We set ourselves to a high priority, that we might pre-empt everything
49 else. If your PBX has heavy activity on it, this is a good thing. */
51 sched.sched_priority = HIGH_PRIORITY;
52 if (sched_setscheduler(0, HIGH_PRIORITY_SCHED, &sched)) {
53 ast_log(LOG_WARNING, "Unable to set high priority\n");
57 sched.sched_priority = 0;
58 if (sched_setscheduler(0, SCHED_OTHER, &sched)) {
59 ast_log(LOG_WARNING, "Unable to set normal priority\n");
66 static void quit_handler(int num)
68 static pthread_mutex_t quitlock = PTHREAD_MUTEX_INITIALIZER;
69 char filename[80] = "";
71 snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
73 pthread_mutex_lock(&quitlock);
76 ast_verbose("Asterisk ending (%d).\n", num);
77 else if (option_debug)
78 ast_log(LOG_DEBUG, "Asterisk ending (%d).\n", num);
80 write_history(filename);
84 static pthread_t consolethread = -1;
86 static void console_verboser(char *s, int pos, int replace, int complete)
88 /* Return to the beginning of the line */
90 fprintf(stdout, "\r");
91 fprintf(stdout, s + pos);
93 /* Wake up a select()ing console */
94 pthread_kill(consolethread, SIGURG);
97 static void consolehandler(char *s)
99 /* Called when readline data is available */
103 ast_cli_command(STDOUT_FILENO, s);
106 static char quit_help[] =
108 " Exits Asterisk.\n";
110 static int handle_quit(int fd, int argc, char *argv[])
113 return RESULT_SHOWUSAGE;
115 return RESULT_SUCCESS;
118 #define ASTERISK_PROMPT "*CLI> "
120 static struct ast_cli_entry quit = { { "quit", NULL }, handle_quit, "Exit Asterisk", quit_help };
122 static char *cli_generator(char *text, int state)
124 return ast_cli_generator(rl_line_buffer, text, state);
127 int main(int argc, char *argv[])
132 char filename[80] = "";
134 snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
135 /* Check if we're root */
137 ast_log(LOG_ERROR, "Must be run as root\n");
140 /* Check for options */
141 while((c=getopt(argc, argv, "dvqp")) != EOF) {
148 option_highpriority++;
160 /* Print a welcome message if desired */
161 if (option_verbose) {
162 ast_verbose( "Asterisk, Copyright (C) 1999 Mark Spencer\n");
163 ast_verbose( "Written by Mark Spencer <markster@linux-support.net>\n");
164 ast_verbose( "=========================================================================\n");
166 signal(SIGURG, urg_handler);
167 signal(SIGINT, quit_handler);
168 signal(SIGTERM, quit_handler);
169 signal(SIGHUP, quit_handler);
176 if (set_priority(option_highpriority))
178 /* We might have the option of showing a console, but for now just
181 /* Console stuff now... */
182 /* Register our quit function */
183 ast_cli_register(&quit);
184 consolethread = pthread_self();
185 ast_register_verbose(console_verboser);
187 ast_verbose( "Asterisk Ready.\n");
188 if (strlen(filename))
189 read_history(filename);
190 rl_callback_handler_install(ASTERISK_PROMPT, consolehandler);
191 rl_completion_entry_function = (Function *)cli_generator;
194 FD_SET(STDIN_FILENO, &rfds);
195 res = select(STDIN_FILENO + 1, &rfds, NULL, NULL, NULL);
197 rl_callback_read_char();
198 } else if (res < 1) {
199 rl_forced_update_display();