diff options
author | markster <markster@f38db490-d61c-443f-a65b-d21fe96a405b> | 1999-12-19 22:38:55 +0000 |
---|---|---|
committer | markster <markster@f38db490-d61c-443f-a65b-d21fe96a405b> | 1999-12-19 22:38:55 +0000 |
commit | a4430bca536242afea56468f9e9cb669c5bfa77d (patch) | |
tree | d04c949294343ad1033dca9dc4c81812481e4e1b /asterisk.c | |
parent | 8a317c03a2bf05b95e939bff76e48e06e93a1777 (diff) |
Version 0.1.1 from FTP
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@142 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'asterisk.c')
-rwxr-xr-x | asterisk.c | 120 |
1 files changed, 115 insertions, 5 deletions
diff --git a/asterisk.c b/asterisk.c index 1bc7be174..a79474077 100755 --- a/asterisk.c +++ b/asterisk.c @@ -3,7 +3,7 @@ * * Top level source file for asterisk * - * Copyright (C) 1999, Adtran Inc. and Linux Support Services, LLC + * Copyright (C) 1999, Mark Spencer * * Mark Spencer <markster@linux-support.net> * @@ -15,14 +15,23 @@ #include <stdlib.h> #include <asterisk/logger.h> #include <asterisk/options.h> +#include <asterisk/cli.h> #include <stdio.h> #include <signal.h> +#include <sched.h> +#include <pthread.h> +#include <readline/readline.h> +#include <readline/history.h> #include "asterisk.h" int option_verbose=0; int option_debug=0; int option_nofork=0; int option_quiet=0; +int option_highpriority=0; + +#define HIGH_PRIORITY 1 +#define HIGH_PRIORITY_SCHED SCHED_RR static void urg_handler(int num) { @@ -33,31 +42,110 @@ static void urg_handler(int num) return; } +static int set_priority(int pri) +{ + struct sched_param sched; + /* We set ourselves to a high priority, that we might pre-empt everything + else. If your PBX has heavy activity on it, this is a good thing. */ + if (pri) { + sched.sched_priority = HIGH_PRIORITY; + if (sched_setscheduler(0, HIGH_PRIORITY_SCHED, &sched)) { + ast_log(LOG_WARNING, "Unable to set high priority\n"); + return -1; + } + } else { + sched.sched_priority = 0; + if (sched_setscheduler(0, SCHED_OTHER, &sched)) { + ast_log(LOG_WARNING, "Unable to set normal priority\n"); + return -1; + } + } + return 0; +} + static void quit_handler(int num) { + static pthread_mutex_t quitlock = PTHREAD_MUTEX_INITIALIZER; + char filename[80] = ""; + if (getenv("HOME")) + snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME")); + /* Quit only once */ + pthread_mutex_lock(&quitlock); /* Called on exit */ if (option_verbose) ast_verbose("Asterisk ending (%d).\n", num); else if (option_debug) ast_log(LOG_DEBUG, "Asterisk ending (%d).\n", num); + if (strlen(filename)) + write_history(filename); exit(0); } +static pthread_t consolethread = -1; + +static void console_verboser(char *s, int pos, int replace, int complete) +{ + /* Return to the beginning of the line */ + if (!pos) + fprintf(stdout, "\r"); + fprintf(stdout, s + pos); + if (complete) + /* Wake up a select()ing console */ + pthread_kill(consolethread, SIGURG); +} + +static void consolehandler(char *s) +{ + /* Called when readline data is available */ + if (s && strlen(s)) + add_history(s); + if (s) + ast_cli_command(STDOUT_FILENO, s); +} + +static char quit_help[] = +"Usage: quit\n" +" Exits Asterisk.\n"; + +static int handle_quit(int fd, int argc, char *argv[]) +{ + if (argc != 1) + return RESULT_SHOWUSAGE; + quit_handler(0); + return RESULT_SUCCESS; +} + +#define ASTERISK_PROMPT "*CLI> " + +static struct ast_cli_entry quit = { { "quit", NULL }, handle_quit, "Exit Asterisk", quit_help }; + +static char *cli_generator(char *text, int state) +{ + return ast_cli_generator(rl_line_buffer, text, state); +} + int main(int argc, char *argv[]) { char c; + fd_set rfds; + int res; + char filename[80] = ""; + if (getenv("HOME")) + snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME")); /* Check if we're root */ if (geteuid()) { ast_log(LOG_ERROR, "Must be run as root\n"); exit(1); } /* Check for options */ - while((c=getopt(argc, argv, "dvq")) != EOF) { + while((c=getopt(argc, argv, "dvqp")) != EOF) { switch(c) { case 'd': option_debug++; option_nofork++; - option_verbose++; + break; + case 'p': + option_highpriority++; break; case 'v': option_verbose++; @@ -71,7 +159,7 @@ int main(int argc, char *argv[]) } /* Print a welcome message if desired */ if (option_verbose) { - ast_verbose( "Asterisk, Copyright (C) 1999 Adtran, Inc. and Linux Support Services, LLC\n"); + ast_verbose( "Asterisk, Copyright (C) 1999 Mark Spencer\n"); ast_verbose( "Written by Mark Spencer <markster@linux-support.net>\n"); ast_verbose( "=========================================================================\n"); } @@ -85,10 +173,32 @@ int main(int argc, char *argv[]) exit(1); if (load_modules()) exit(1); + if (set_priority(option_highpriority)) + exit(1); /* We might have the option of showing a console, but for now just do nothing... */ + + /* Console stuff now... */ + /* Register our quit function */ + ast_cli_register(&quit); + consolethread = pthread_self(); + ast_register_verbose(console_verboser); if (option_verbose) ast_verbose( "Asterisk Ready.\n"); - select(0,NULL,NULL,NULL,NULL); + if (strlen(filename)) + read_history(filename); + rl_callback_handler_install(ASTERISK_PROMPT, consolehandler); + rl_completion_entry_function = (Function *)cli_generator; + for(;;) { + FD_ZERO(&rfds); + FD_SET(STDIN_FILENO, &rfds); + res = select(STDIN_FILENO + 1, &rfds, NULL, NULL, NULL); + if (res > 0) { + rl_callback_read_char(); + } else if (res < 1) { + rl_forced_update_display(); + } + + } return 0; } |