Issue 5852 (casper) Code clean up, adding documentation on new switches. Thanks!

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@16616 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.4
Olle Johansson 19 years ago
parent adf8c8deda
commit d80f3bc20e

@ -30,7 +30,7 @@
* *
* \section copyright Copyright and author * \section copyright Copyright and author
* *
* Copyright (C) 1999 - 2005, Digium, Inc. * Copyright (C) 1999 - 2006, Digium, Inc.
* Asterisk is a trade mark registered by Digium, Inc. * Asterisk is a trade mark registered by Digium, Inc.
* *
* \author Mark Spencer <markster@digium.com> * \author Mark Spencer <markster@digium.com>
@ -216,7 +216,7 @@ char ast_config_AST_CTL_PERMISSIONS[AST_CONFIG_MAX_PATH];
char ast_config_AST_CTL_OWNER[AST_CONFIG_MAX_PATH] = "\0"; char ast_config_AST_CTL_OWNER[AST_CONFIG_MAX_PATH] = "\0";
char ast_config_AST_CTL_GROUP[AST_CONFIG_MAX_PATH] = "\0"; char ast_config_AST_CTL_GROUP[AST_CONFIG_MAX_PATH] = "\0";
char ast_config_AST_CTL[AST_CONFIG_MAX_PATH] = "asterisk.ctl"; char ast_config_AST_CTL[AST_CONFIG_MAX_PATH] = "asterisk.ctl";
char ast_config_AST_SYSTEM_NAME[20]=""; char ast_config_AST_SYSTEM_NAME[20] = "";
static char *_argv[256]; static char *_argv[256];
static int shuttingdown = 0; static int shuttingdown = 0;
@ -462,7 +462,7 @@ int ast_safe_system(const char *s)
static void ast_network_puts(const char *string) static void ast_network_puts(const char *string)
{ {
int x; int x;
for (x=0;x<AST_MAX_CONNECTS; x++) { for (x=0; x < AST_MAX_CONNECTS; x++) {
if (consoles[x].fd > -1) if (consoles[x].fd > -1)
fdprint(consoles[x].p[1], string); fdprint(consoles[x].p[1], string);
} }
@ -503,7 +503,7 @@ static pthread_t lthread;
static void *netconsole(void *vconsole) static void *netconsole(void *vconsole)
{ {
struct console *con = vconsole; struct console *con = vconsole;
char hostname[MAXHOSTNAMELEN]=""; char hostname[MAXHOSTNAMELEN] = "";
char tmp[512]; char tmp[512];
int res; int res;
struct pollfd fds[2]; struct pollfd fds[2];
@ -566,11 +566,11 @@ static void *listener(void *unused)
pthread_attr_t attr; pthread_attr_t attr;
pthread_attr_init(&attr); pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
for(;;) { for (;;) {
if (ast_socket < 0) if (ast_socket < 0)
return NULL; return NULL;
fds[0].fd = ast_socket; fds[0].fd = ast_socket;
fds[0].events= POLLIN; fds[0].events = POLLIN;
s = poll(fds, 1, -1); s = poll(fds, 1, -1);
if (s < 0) { if (s < 0) {
if (errno != EINTR) if (errno != EINTR)
@ -583,7 +583,7 @@ static void *listener(void *unused)
if (errno != EINTR) if (errno != EINTR)
ast_log(LOG_WARNING, "Accept returned %d: %s\n", s, strerror(errno)); ast_log(LOG_WARNING, "Accept returned %d: %s\n", s, strerror(errno));
} else { } else {
for (x=0;x<AST_MAX_CONNECTS;x++) { for (x = 0; x < AST_MAX_CONNECTS; x++) {
if (consoles[x].fd < 0) { if (consoles[x].fd < 0) {
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, consoles[x].p)) { if (socketpair(AF_LOCAL, SOCK_STREAM, 0, consoles[x].p)) {
ast_log(LOG_ERROR, "Unable to create pipe: %s\n", strerror(errno)); ast_log(LOG_ERROR, "Unable to create pipe: %s\n", strerror(errno));
@ -829,7 +829,7 @@ static void quit_handler(int num, int nice, int safeshutdown, int restart)
if (option_verbose && ast_opt_console) if (option_verbose && ast_opt_console)
ast_verbose("Beginning asterisk %s....\n", restart ? "restart" : "shutdown"); ast_verbose("Beginning asterisk %s....\n", restart ? "restart" : "shutdown");
time(&s); time(&s);
for(;;) { for (;;) {
time(&e); time(&e);
/* Wait up to 15 seconds for all channels to go away */ /* Wait up to 15 seconds for all channels to go away */
if ((e - s) > 15) if ((e - s) > 15)
@ -846,7 +846,7 @@ static void quit_handler(int num, int nice, int safeshutdown, int restart)
ast_begin_shutdown(0); ast_begin_shutdown(0);
if (option_verbose && ast_opt_console) if (option_verbose && ast_opt_console)
ast_verbose("Waiting for inactivity to perform %s...\n", restart ? "restart" : "halt"); ast_verbose("Waiting for inactivity to perform %s...\n", restart ? "restart" : "halt");
for(;;) { for (;;) {
if (!ast_active_channels()) if (!ast_active_channels())
break; break;
if (!shuttingdown) if (!shuttingdown)
@ -895,7 +895,7 @@ static void quit_handler(int num, int nice, int safeshutdown, int restart)
if (option_verbose || ast_opt_console) if (option_verbose || ast_opt_console)
ast_verbose("Preparing for Asterisk restart...\n"); ast_verbose("Preparing for Asterisk restart...\n");
/* Mark all FD's for closing on exec */ /* Mark all FD's for closing on exec */
for (x=3;x<32768;x++) { for (x=3; x < 32768; x++) {
fcntl(x, F_SETFD, FD_CLOEXEC); fcntl(x, F_SETFD, FD_CLOEXEC);
} }
if (option_verbose || ast_opt_console) if (option_verbose || ast_opt_console)
@ -940,7 +940,7 @@ static const char *fix_header(char *outbuf, int maxout, const char *s, char *cmp
static void console_verboser(const char *s, int pos, int replace, int complete) static void console_verboser(const char *s, int pos, int replace, int complete)
{ {
char tmp[80]; char tmp[80];
const char *c=NULL; const char *c = NULL;
/* Return to the beginning of the line */ /* Return to the beginning of the line */
if (!pos) { if (!pos) {
fprintf(stdout, "\r"); fprintf(stdout, "\r");
@ -964,7 +964,7 @@ static void console_verboser(const char *s, int pos, int replace, int complete)
static int ast_all_zeros(char *s) static int ast_all_zeros(char *s)
{ {
while(*s) { while (*s) {
if (*s > 32) if (*s > 32)
return 0; return 0;
s++; s++;
@ -1234,8 +1234,8 @@ static struct ast_cli_entry core_cli[] = {
static int ast_el_read_char(EditLine *el, char *cp) static int ast_el_read_char(EditLine *el, char *cp)
{ {
int num_read=0; int num_read = 0;
int lastpos=0; int lastpos = 0;
struct pollfd fds[2]; struct pollfd fds[2];
int res; int res;
int max; int max;
@ -1276,7 +1276,7 @@ static int ast_el_read_char(EditLine *el, char *cp)
int tries; int tries;
int reconnects_per_second = 20; int reconnects_per_second = 20;
fprintf(stderr, "Attempting to reconnect for 30 seconds\n"); fprintf(stderr, "Attempting to reconnect for 30 seconds\n");
for (tries=0;tries<30 * reconnects_per_second;tries++) { for (tries=0; tries < 30 * reconnects_per_second; tries++) {
if (ast_tryconnect()) { if (ast_tryconnect()) {
fprintf(stderr, "Reconnect succeeded after %.3f seconds\n", 1.0 / reconnects_per_second * tries); fprintf(stderr, "Reconnect succeeded after %.3f seconds\n", 1.0 / reconnects_per_second * tries);
printf(term_quit()); printf(term_quit());
@ -1315,7 +1315,7 @@ static char *cli_prompt(EditLine *el)
{ {
static char prompt[200]; static char prompt[200];
char *pfmt; char *pfmt;
int color_used=0; int color_used = 0;
char term_code[20]; char term_code[20];
if ((pfmt = getenv("ASTERISK_PROMPT"))) { if ((pfmt = getenv("ASTERISK_PROMPT"))) {
@ -1367,7 +1367,7 @@ static char *cli_prompt(EditLine *el)
break; break;
case 'H': /* short hostname */ case 'H': /* short hostname */
if (!gethostname(hostname, sizeof(hostname) - 1)) { if (!gethostname(hostname, sizeof(hostname) - 1)) {
for (i=0;i<sizeof(hostname);i++) { for (i = 0; i < sizeof(hostname); i++) {
if (hostname[i] == '.') { if (hostname[i] == '.') {
hostname[i] = '\0'; hostname[i] = '\0';
break; break;
@ -1551,7 +1551,7 @@ static int ast_cli_display_match_list(char **matches, int len, int max)
static char *cli_complete(EditLine *el, int ch) static char *cli_complete(EditLine *el, int ch)
{ {
int len=0; int len = 0;
char *ptr; char *ptr;
int nummatches = 0; int nummatches = 0;
char **matches; char **matches;
@ -1750,7 +1750,7 @@ static void ast_remotecontrol(char * data)
char *version; char *version;
int pid; int pid;
char tmp[80]; char tmp[80];
char *stringp=NULL; char *stringp = NULL;
char *ebuf; char *ebuf;
int num = 0; int num = 0;
@ -1758,13 +1758,13 @@ static void ast_remotecontrol(char * data)
read(ast_consock, buf, sizeof(buf)); read(ast_consock, buf, sizeof(buf));
if (data) if (data)
write(ast_consock, data, strlen(data) + 1); write(ast_consock, data, strlen(data) + 1);
stringp=buf; stringp = buf;
hostname = strsep(&stringp, "/"); hostname = strsep(&stringp, "/");
cpid = strsep(&stringp, "/"); cpid = strsep(&stringp, "/");
version = strsep(&stringp, "\n"); version = strsep(&stringp, "\n");
if (!version) if (!version)
version = "<Version Unknown>"; version = "<Version Unknown>";
stringp=hostname; stringp = hostname;
strsep(&stringp, "."); strsep(&stringp, ".");
if (cpid) if (cpid)
pid = atoi(cpid); pid = atoi(cpid);
@ -1797,7 +1797,7 @@ static void ast_remotecontrol(char * data)
} }
return; return;
} }
for(;;) { for (;;) {
ebuf = (char *)el_gets(el, &num); ebuf = (char *)el_gets(el, &num);
if (!ast_strlen_zero(ebuf)) { if (!ast_strlen_zero(ebuf)) {
@ -1822,7 +1822,7 @@ static int show_version(void)
} }
static int show_cli_help(void) { static int show_cli_help(void) {
printf("Asterisk " ASTERISK_VERSION ", Copyright (C) 1999 - 2005, Digium, Inc. and others.\n"); printf("Asterisk " ASTERISK_VERSION ", Copyright (C) 1999 - 2006, Digium, Inc. and others.\n");
printf("Usage: asterisk [OPTIONS]\n"); printf("Usage: asterisk [OPTIONS]\n");
printf("Valid Options:\n"); printf("Valid Options:\n");
printf(" -V Display version number and exit\n"); printf(" -V Display version number and exit\n");
@ -1835,6 +1835,9 @@ static int show_cli_help(void) {
printf(" -g Dump core in case of a crash\n"); printf(" -g Dump core in case of a crash\n");
printf(" -h This help screen\n"); printf(" -h This help screen\n");
printf(" -i Initialize crypto keys at startup\n"); printf(" -i Initialize crypto keys at startup\n");
printf(" -I Enable internal timing if Zaptel timer is available\n");
printf(" -L <load> Limit the maximum load average before rejecting new calls\n");
printf(" -M <value> Limit the maximum number of calls to the specified value\n");
printf(" -n Disable console colorization\n"); printf(" -n Disable console colorization\n");
printf(" -p Run as pseudo-realtime thread\n"); printf(" -p Run as pseudo-realtime thread\n");
printf(" -q Quiet mode (suppress output)\n"); printf(" -q Quiet mode (suppress output)\n");
@ -2004,23 +2007,23 @@ int main(int argc, char *argv[])
{ {
int c; int c;
char filename[80] = ""; char filename[80] = "";
char hostname[MAXHOSTNAMELEN]=""; char hostname[MAXHOSTNAMELEN] = "";
char tmp[80]; char tmp[80];
char * xarg = NULL; char * xarg = NULL;
int x; int x;
FILE *f; FILE *f;
sigset_t sigs; sigset_t sigs;
int num; int num;
int is_child_of_nonroot=0; int is_child_of_nonroot = 0;
char *buf; char *buf;
char *runuser=NULL, *rungroup=NULL; char *runuser = NULL, *rungroup = NULL;
/* Remember original args for restart */ /* Remember original args for restart */
if (argc > sizeof(_argv) / sizeof(_argv[0]) - 1) { if (argc > sizeof(_argv) / sizeof(_argv[0]) - 1) {
fprintf(stderr, "Truncating argument size to %d\n", (int)(sizeof(_argv) / sizeof(_argv[0])) - 1); fprintf(stderr, "Truncating argument size to %d\n", (int)(sizeof(_argv) / sizeof(_argv[0])) - 1);
argc = sizeof(_argv) / sizeof(_argv[0]) - 1; argc = sizeof(_argv) / sizeof(_argv[0]) - 1;
} }
for (x=0;x<argc;x++) for (x=0; x<argc; x++)
_argv[x] = argv[x]; _argv[x] = argv[x];
_argv[x] = NULL; _argv[x] = NULL;
@ -2039,21 +2042,14 @@ int main(int argc, char *argv[])
tdd_init(); tdd_init();
/* When Asterisk restarts after it has dropped the root privileges, /* When Asterisk restarts after it has dropped the root privileges,
* it can't issue setuid(), setgid(), setgroups() or set_priority() * it can't issue setuid(), setgid(), setgroups() or set_priority()
* */ */
if (getenv("ASTERISK_ALREADY_NONROOT")) if (getenv("ASTERISK_ALREADY_NONROOT"))
is_child_of_nonroot=1; is_child_of_nonroot=1;
if (getenv("HOME")) if (getenv("HOME"))
snprintf(filename, sizeof(filename), "%s/.asterisk_history", 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 */ /* Check for options */
while((c=getopt(argc, argv, "tThfdvVqprRgciInx:U:G:C:L:M:")) != -1) { while ((c = getopt(argc, argv, "tThfdvVqprRgciInx:U:G:C:L:M:")) != -1) {
switch(c) { switch (c) {
case 'd': case 'd':
option_debug++; option_debug++;
ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK); ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK);
@ -2091,7 +2087,6 @@ int main(int argc, char *argv[])
case 'q': case 'q':
ast_set_flag(&ast_options, AST_OPT_FLAG_QUIET); ast_set_flag(&ast_options, AST_OPT_FLAG_QUIET);
break; break;
break;
case 't': case 't':
ast_set_flag(&ast_options, AST_OPT_FLAG_CACHE_RECORD_FILES); ast_set_flag(&ast_options, AST_OPT_FLAG_CACHE_RECORD_FILES);
break; break;
@ -2112,7 +2107,7 @@ int main(int argc, char *argv[])
case 'i': case 'i':
ast_set_flag(&ast_options, AST_OPT_FLAG_INIT_KEYS); ast_set_flag(&ast_options, AST_OPT_FLAG_INIT_KEYS);
break; break;
case'g': case 'g':
ast_set_flag(&ast_options, AST_OPT_FLAG_DUMP_CORE); ast_set_flag(&ast_options, AST_OPT_FLAG_DUMP_CORE);
break; break;
case 'h': case 'h':
@ -2160,6 +2155,7 @@ int main(int argc, char *argv[])
rungroup = ast_config_AST_RUN_GROUP; rungroup = ast_config_AST_RUN_GROUP;
if ((!runuser) && !ast_strlen_zero(ast_config_AST_RUN_USER)) if ((!runuser) && !ast_strlen_zero(ast_config_AST_RUN_USER))
runuser = ast_config_AST_RUN_USER; runuser = ast_config_AST_RUN_USER;
#ifndef __CYGWIN__ #ifndef __CYGWIN__
if (!is_child_of_nonroot) if (!is_child_of_nonroot)
@ -2205,7 +2201,7 @@ int main(int argc, char *argv[])
ast_log(LOG_WARNING, "Unable to setuid to %d (%s)\n", (int)pw->pw_uid, runuser); ast_log(LOG_WARNING, "Unable to setuid to %d (%s)\n", (int)pw->pw_uid, runuser);
exit(1); exit(1);
} }
setenv("ASTERISK_ALREADY_NONROOT","yes",1); setenv("ASTERISK_ALREADY_NONROOT", "yes", 1);
if (option_verbose) if (option_verbose)
ast_verbose("Running as user '%s'\n", runuser); ast_verbose("Running as user '%s'\n", runuser);
} }
@ -2230,7 +2226,6 @@ int main(int argc, char *argv[])
register_config_cli(); register_config_cli();
read_config_maps(); read_config_maps();
if (ast_opt_console) { if (ast_opt_console) {
if (el_hist == NULL || el == NULL) if (el_hist == NULL || el == NULL)
ast_el_initialize(); ast_el_initialize();
@ -2259,7 +2254,7 @@ int main(int argc, char *argv[])
exit(1); exit(1);
} }
} else if (ast_opt_remote || ast_opt_exec) { } else if (ast_opt_remote || ast_opt_exec) {
ast_log(LOG_ERROR, "Unable to connect to remote asterisk (does %s exist?)\n",ast_config_AST_SOCKET); ast_log(LOG_ERROR, "Unable to connect to remote asterisk (does %s exist?)\n", ast_config_AST_SOCKET);
printf(term_quit()); printf(term_quit());
exit(1); exit(1);
} }
@ -2273,7 +2268,7 @@ int main(int argc, char *argv[])
ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", ast_config_AST_PID, strerror(errno)); ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", ast_config_AST_PID, strerror(errno));
if (!option_verbose && !option_debug && !ast_opt_no_fork && !ast_opt_console) { if (!option_verbose && !option_debug && !ast_opt_no_fork && !ast_opt_console) {
daemon(0,0); daemon(0, 0);
/* Blindly re-write pid file since we are forking */ /* Blindly re-write pid file since we are forking */
unlink(ast_config_AST_PID); unlink(ast_config_AST_PID);
f = fopen(ast_config_AST_PID, "w"); f = fopen(ast_config_AST_PID, "w");
@ -2390,7 +2385,6 @@ int main(int argc, char *argv[])
ast_rtp_reload(); ast_rtp_reload();
#endif #endif
/* We might have the option of showing a console, but for now just /* We might have the option of showing a console, but for now just
do nothing... */ do nothing... */
if (ast_opt_console && !option_verbose) if (ast_opt_console && !option_verbose)

Loading…
Cancel
Save