Asterisk - The Open Source Telephony Project GIT-master-3dae2cf
asterisk.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2018, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19
20/* Doxygenified Copyright Header */
21/*!
22 * \mainpage Asterisk -- The Open Source Telephony Project
23 *
24 * \par Welcome
25 *
26 * This documentation created by the Doxygen project clearly explains the
27 * internals of the Asterisk software. This documentation contains basic
28 * examples, developer documentation, support information, and information
29 * for upgrading.
30 *
31 * \section community Community
32 * Asterisk is a big project and has a busy community. Look at the
33 * resources for questions and stick around to help answer questions.
34 * \li \ref asterisk_community_resources
35 *
36 * \par Developer Documentation for Asterisk
37 *
38 * This is the main developer documentation for Asterisk. It is
39 * generated by running "make progdocs" from the Asterisk source tree.
40 *
41 * In addition to the information available on the Asterisk source code,
42 * please see the appendices for information on coding guidelines,
43 * release management, commit policies, and more.
44 *
45 * \arg \ref AsteriskArchitecture
46 *
47 * \par Additional documentation
48 * \arg \ref Licensing
49 * \arg \ref DevDoc
50 * \arg \ref configuration_file
51 * \arg \ref channel_drivers
52 * \arg \ref applications
53 *
54 * \section copyright Copyright and Author
55 *
56 * Copyright (C) 1999 - 2021, Sangoma Technologies Corporation.
57 * Asterisk is a <a href="https://cdn.sangoma.com/wp-content/uploads/Sangoma-Trademark-Policy.pdf">registered trademark</a>
58 * of <a rel="nofollow" href="http://www.sangoma.com">Sangoma Technologies Corporation</a>.
59 *
60 * \author Mark Spencer <markster@digium.com>
61 *
62 * See http://www.asterisk.org for more information about
63 * the Asterisk project. Please do not directly contact
64 * any of the maintainers of this project for assistance;
65 * the project provides a web site, mailing lists, and IRC
66 * channels for your use.
67 *
68 */
69
70/*!
71 * \page asterisk_community_resources Asterisk Community Resources
72 * \par Websites
73 * \li https://www.asterisk.org Asterisk Homepage
74 * \li https://docs.asterisk.org Asterisk documentation
75 *
76 * \par Mailing Lists
77 * \par
78 * All lists: http://lists.digium.com/mailman/listinfo
79 * \li aadk-commits SVN commits to the AADK repository
80 * \li asterisk-addons-commits SVN commits to the Asterisk addons project
81 * \li asterisk-announce [no description available]
82 * \li asterisk-biz Commercial and Business-Oriented Asterisk Discussion
83 * \li Asterisk-BSD Asterisk on BSD discussion
84 * \li asterisk-bugs [no description available]
85 * \li asterisk-commits SVN commits to the Asterisk project
86 * \li asterisk-dev Asterisk Developers Mailing List
87 * \li asterisk-doc Discussions regarding The Asterisk Documentation Project
88 * \li asterisk-embedded Asterisk Embedded Development
89 * \li asterisk-gui Asterisk GUI project discussion
90 * \li asterisk-gui-commits SVN commits to the Asterisk-GUI project
91 * \li asterisk-ha-clustering Asterisk High Availability and Clustering List - Non-Commercial Discussion
92 * \li Asterisk-i18n Discussion of Asterisk internationalization
93 * \li asterisk-r2 [no description available]
94 * \li asterisk-scf-commits Commits to the Asterisk SCF project code repositories
95 * \li asterisk-scf-committee Asterisk SCF Steering Committee discussions
96 * \li asterisk-scf-dev Asterisk SCF Developers Mailing List
97 * \li asterisk-scf-wiki-changes Changes to the Asterisk SCF space on wiki.asterisk.org
98 * \li asterisk-security Asterisk Security Discussion
99 * \li asterisk-speech-rec Use of speech recognition in Asterisk
100 * \li asterisk-ss7 [no description available]
101 * \li asterisk-users Asterisk Users Mailing List - Non-Commercial Discussion
102 * \li asterisk-video Development discussion of video media support in Asterisk
103 * \li asterisk-wiki-changes Changes to the Asterisk space on wiki.asterisk.org
104 * \li asterisknow AsteriskNOW Discussion
105 * \li dahdi-commits SVN commits to the DAHDI project
106 * \li digium-announce Digium Product Announcements
107 * \li Dundi Distributed Universal Number Discovery
108 * \li libiax2-commits SVN commits to the libiax2 project
109 * \li libpri-commits SVN commits to the libpri project
110 * \li libss7-commits SVN commits to the libss7 project
111 * \li svn-commits SVN commits to the Digium repositories
112 * \li Test-results Results from automated testing
113 * \li thirdparty-commits SVN commits to the Digium third-party software repository
114 * \li zaptel-commits SVN commits to the Zaptel project
115 *
116 * \par Forums
117 * \li Forums are located at http://forums.asterisk.org/
118 *
119 * \par IRC
120 * \par
121 * Use https://libera.chat IRC server to connect with Asterisk
122 * developers and users in realtime.
123 *
124 * \li \verbatim #asterisk \endverbatim Asterisk Users Room
125 * \li \verbatim #asterisk-dev \endverbatim Asterisk Developers Room
126 *
127 * \par More
128 * \par
129 * If you would like to add a resource to this list please create an issue
130 * on the issue tracker with a patch.
131 */
132
133/*! \file
134 * \brief Top level source file for Asterisk - the Open Source PBX.
135 * Implementation of PBX core functions and CLI interface.
136 */
137
138/*! \li \ref asterisk.c uses the configuration file \ref asterisk.conf
139 * \addtogroup configuration_file
140 */
141
142/*! \page asterisk.conf asterisk.conf
143 * \verbinclude asterisk.conf.sample
144 */
145
146/*** MODULEINFO
147 <support_level>core</support_level>
148 ***/
149
150#include "asterisk.h"
151
152#include "asterisk/_private.h"
153
154#undef sched_setscheduler
155#undef setpriority
156#include <sys/time.h>
157#include <fcntl.h>
158#include <signal.h>
159#include <sched.h>
160#include <sys/un.h>
161#include <sys/wait.h>
162#include <ctype.h>
163#include <sys/resource.h>
164#include <grp.h>
165#include <pwd.h>
166#include <sys/stat.h>
167#if defined(HAVE_SYSINFO)
168#include <sys/sysinfo.h>
169#elif defined(HAVE_SYSCTL)
170#include <sys/param.h>
171#include <sys/sysctl.h>
172#include <sys/vmmeter.h>
173#if defined(__FreeBSD__) || defined(__DragonFly__)
174#include <vm/vm_param.h>
175#endif
176#if defined(HAVE_SWAPCTL)
177#include <sys/swap.h>
178#endif
179#endif
180#include <regex.h>
181#include <histedit.h>
182
183#if defined(SOLARIS)
184int daemon(int, int); /* defined in libresolv of all places */
185#include <sys/loadavg.h>
186#endif
187
188#ifdef linux
189#include <sys/prctl.h>
190#ifdef HAVE_CAP
191#include <sys/capability.h>
192#endif /* HAVE_CAP */
193#endif /* linux */
194
195/* we define here the variables so to better agree on the prototype */
196#include "asterisk/paths.h"
197#include "asterisk/network.h"
198#include "asterisk/cli.h"
199#include "asterisk/channel.h"
200#include "asterisk/translate.h"
201#include "asterisk/pickup.h"
202#include "asterisk/acl.h"
203#include "asterisk/ulaw.h"
204#include "asterisk/alaw.h"
205#include "asterisk/callerid.h"
206#include "asterisk/image.h"
207#include "asterisk/tdd.h"
208#include "asterisk/term.h"
209#include "asterisk/manager.h"
210#include "asterisk/cdr.h"
211#include "asterisk/pbx.h"
212#include "asterisk/app.h"
213#include "asterisk/mwi.h"
214#include "asterisk/lock.h"
215#include "asterisk/utils.h"
216#include "asterisk/file.h"
217#include "asterisk/io.h"
218#include "asterisk/config.h"
219#include "asterisk/ast_version.h"
220#include "asterisk/linkedlists.h"
221#include "asterisk/devicestate.h"
223#include "asterisk/module.h"
224#include "asterisk/buildinfo.h"
225#include "asterisk/xmldoc.h"
226#include "asterisk/poll-compat.h"
227#include "asterisk/test.h"
228#include "asterisk/rtp_engine.h"
229#include "asterisk/format.h"
230#include "asterisk/aoc.h"
231#include "asterisk/uuid.h"
232#include "asterisk/sorcery.h"
233#include "asterisk/bucket.h"
234#include "asterisk/stasis.h"
235#include "asterisk/json.h"
239#include "asterisk/endpoints.h"
240#include "asterisk/codec.h"
242#include "asterisk/media_cache.h"
243#include "asterisk/astdb.h"
244#include "asterisk/options.h"
245#include "asterisk/utf8.h"
246
247#include "../defaults.h"
248
249/*** DOCUMENTATION
250 <managerEvent language="en_US" name="FullyBooted">
251 <managerEventInstance class="EVENT_FLAG_SYSTEM">
252 <synopsis>Raised when all Asterisk initialization procedures have finished.</synopsis>
253 <syntax>
254 <parameter name="Status">
255 <para>Informational message</para>
256 </parameter>
257 <parameter name="Uptime">
258 <para>Seconds since start</para>
259 </parameter>
260 <parameter name="LastReload">
261 <para>Seconds since last reload</para>
262 </parameter>
263 </syntax>
264 </managerEventInstance>
265 </managerEvent>
266 <managerEvent language="en_US" name="Shutdown">
267 <managerEventInstance class="EVENT_FLAG_SYSTEM">
268 <synopsis>Raised when Asterisk is shutdown or restarted.</synopsis>
269 <syntax>
270 <parameter name="Shutdown">
271 <para>Whether the shutdown is proceeding cleanly (all channels
272 were hungup successfully) or uncleanly (channels will be
273 terminated)</para>
274 <enumlist>
275 <enum name="Uncleanly"/>
276 <enum name="Cleanly"/>
277 </enumlist>
278 </parameter>
279 <parameter name="Restart">
280 <para>Whether or not a restart will occur.</para>
281 <enumlist>
282 <enum name="True"/>
283 <enum name="False"/>
284 </enumlist>
285 </parameter>
286 </syntax>
287 </managerEventInstance>
288 </managerEvent>
289 ***/
290
291#ifndef AF_LOCAL
292#define AF_LOCAL AF_UNIX
293#define PF_LOCAL PF_UNIX
294#endif
295
296#define AST_MAX_CONNECTS 128
297#define NUM_MSGS 64
298
299/*! Displayed copyright tag */
300#define COPYRIGHT_TAG "Copyright (C) 1999 - 2022, Sangoma Technologies Corporation and others."
301
302/*! \brief Welcome message when starting a CLI interface */
303#define WELCOME_MESSAGE \
304 ast_verbose("Asterisk %s, " COPYRIGHT_TAG "\n" \
305 "Created by Mark Spencer <markster@digium.com>\n" \
306 "Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for details.\n" \
307 "This is free software, with components licensed under the GNU General Public\n" \
308 "License version 2 and other licenses; you are welcome to redistribute it under\n" \
309 "certain conditions. Type 'core show license' for details.\n" \
310 "=========================================================================\n", ast_get_version()) \
311
312static int ast_socket = -1; /*!< UNIX Socket for allowing remote control */
313static int ast_socket_is_sd = 0; /*!< Is socket activation responsible for ast_socket? */
314static int ast_consock = -1; /*!< UNIX Socket for controlling another asterisk */
316struct console {
317 int fd; /*!< File descriptor */
318 int p[2]; /*!< Pipe */
319 pthread_t t; /*!< Thread of handler */
320 int mute; /*!< Is the console muted for logs */
321 int uid; /*!< Remote user ID. */
322 int gid; /*!< Remote group ID. */
323 int levels[NUMLOGLEVELS]; /*!< Which log levels are enabled for the console */
324 /*! Verbosity level of this console. */
326};
327
329 void (*func)(void);
332};
333
335
336struct timeval ast_startuptime;
337struct timeval ast_lastreloadtime;
338
339static History *el_hist;
340static EditLine *el;
341static char *remotehostname;
342
344
345static int ast_el_add_history(const char *);
346static int ast_el_read_history(const char *);
347static int ast_el_write_history(const char *);
348
349static void ast_el_read_default_histfile(void);
350static void ast_el_write_default_histfile(void);
351
352static void asterisk_daemon(int isroot, const char *runuser, const char *rungroup);
353
354static char *_argv[256];
355
356typedef enum {
357 /*! Normal operation */
359 /*! Committed to shutting down. Final phase */
361 /*! Committed to shutting down. Initial phase */
363 /*!
364 * Valid values for quit_handler() niceness below.
365 * These shutdown/restart levels can be cancelled.
366 *
367 * Remote console exit right now
368 */
370 /*! core stop/restart now */
372 /*! core stop/restart gracefully */
374 /*! core stop/restart when convenient */
377
379
380/*! Prevent new channel allocation for shutdown. */
382
383static int restartnow;
385static pthread_t mon_sig_flags;
386static int canary_pid = 0;
387static char canary_filename[128];
389
390static char randompool[256];
391
392#ifdef HAVE_CAP
393static cap_t child_cap;
394#endif
395
396static int sig_alert_pipe[2] = { -1, -1 };
397static struct {
398 unsigned int need_reload:1;
399 unsigned int need_quit:1;
400 unsigned int need_quit_handler:1;
401 unsigned int need_el_end:1;
403
404#if !defined(LOW_MEMORY)
407 char *name;
408 pthread_t id;
409 int lwp;
410};
411
413
415{
416 struct thread_list_t *new = ast_calloc(1, sizeof(*new));
417
418 if (!new)
419 return;
420
422 new->id = pthread_self();
423 new->lwp = ast_get_tid();
424 new->name = name; /* steal the allocated memory for the thread name */
428}
429
431{
432 struct thread_list_t *x;
433
436 if ((void *) x->id == id) {
438 break;
439 }
440 }
443 if (x) {
444 ast_free(x->name);
445 ast_free(x);
446 }
447}
448
449/*! \brief Print the contents of a file */
450static int print_file(int fd, char *desc, const char *filename)
451{
452 FILE *f;
453 char c;
454 if (!(f = fopen(filename, "r"))) {
455 return -1;
456 }
457 ast_cli(fd, "%s", desc);
458 while ((c = fgetc(f)) != EOF) {
459 ast_cli(fd, "%c", c);
460 }
461 fclose(f);
462 /* no need for trailing new line, the file already has one */
463 return 0;
464}
465
466/*! \brief Give an overview of core settings */
467static char *handle_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
468{
469 char buf[BUFSIZ];
470 struct ast_tm tm;
471 char eid_str[128];
472 struct rlimit limits;
473 char pbx_uuid[AST_UUID_STR_LEN];
474#if defined(HAVE_EACCESS) || defined(HAVE_EUIDACCESS)
475 char dir[PATH_MAX];
476#endif
477
478 switch (cmd) {
479 case CLI_INIT:
480 e->command = "core show settings";
481 e->usage = "Usage: core show settings\n"
482 " Show core misc settings";
483 return NULL;
484 case CLI_GENERATE:
485 return NULL;
486 }
487
488 ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
489 ast_pbx_uuid_get(pbx_uuid, sizeof(pbx_uuid));
490
491 ast_cli(a->fd, "\nPBX Core settings\n");
492 ast_cli(a->fd, "-----------------\n");
493 ast_cli(a->fd, " Version: %s\n", ast_get_version());
494 ast_cli(a->fd, " ABI related Build Options: %s\n", S_OR(ast_get_build_opts(), "(none)"));
495 ast_cli(a->fd, " All Build Options: %s\n", S_OR(ast_get_build_opts_all(), "(none)"));
497 ast_cli(a->fd, " Maximum calls: %d (Current %d)\n", ast_option_maxcalls, ast_active_channels());
498 else
499 ast_cli(a->fd, " Maximum calls: Not set\n");
500
501 if (getrlimit(RLIMIT_NOFILE, &limits)) {
502 ast_cli(a->fd, " Maximum open file handles: Error because of %s\n", strerror(errno));
503 } else if (limits.rlim_cur == RLIM_INFINITY) {
504 ast_cli(a->fd, " Maximum open file handles: Unlimited\n");
505 } else if (limits.rlim_cur < ast_option_maxfiles) {
506 ast_cli(a->fd, " Maximum open file handles: %d (is) %d (requested)\n", (int) limits.rlim_cur, ast_option_maxfiles);
507 } else {
508 ast_cli(a->fd, " Maximum open file handles: %d\n", (int) limits.rlim_cur);
509 }
510
511 ast_cli(a->fd, " Root console verbosity: %d\n", option_verbose);
512 ast_cli(a->fd, " Current console verbosity: %d\n", ast_verb_console_get());
513 ast_cli(a->fd, " Debug level: %d\n", option_debug);
514 ast_cli(a->fd, " Trace level: %d\n", option_trace);
515 ast_cli(a->fd, " Dump core on crash: %s\n", ast_opt_dump_core ? "Yes" : "No");
516 print_file(a->fd, " Core dump file: ", "/proc/sys/kernel/core_pattern");
517 ast_cli(a->fd, " Maximum load average: %lf\n", ast_option_maxload);
518#if defined(HAVE_SYSINFO)
519 ast_cli(a->fd, " Minimum free memory: %ld MB\n", option_minmemfree);
520#endif
521 if (ast_localtime(&ast_startuptime, &tm, NULL)) {
522 ast_strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
523 ast_cli(a->fd, " Startup time: %s\n", buf);
524 }
526 ast_strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
527 ast_cli(a->fd, " Last reload time: %s\n", buf);
528 }
529 ast_cli(a->fd, " System: %s/%s built by %s on %s %s\n", ast_build_os, ast_build_kernel, ast_build_user, ast_build_machine, ast_build_date);
530 ast_cli(a->fd, " System name: %s\n", ast_config_AST_SYSTEM_NAME);
531 ast_cli(a->fd, " Entity ID: %s\n", eid_str);
532 ast_cli(a->fd, " PBX UUID: %s\n", pbx_uuid);
533 ast_cli(a->fd, " Default language: %s\n", ast_defaultlanguage);
534 ast_cli(a->fd, " Language prefix: %s\n", ast_language_is_prefix ? "Enabled" : "Disabled");
535 ast_cli(a->fd, " User name and group: %s/%s\n", ast_config_AST_RUN_USER, ast_config_AST_RUN_GROUP);
536#if defined(HAVE_EACCESS) || defined(HAVE_EUIDACCESS)
537#if defined(HAVE_EUIDACCESS) && !defined(HAVE_EACCESS)
538#define eaccess euidaccess
539#endif
540 if (!getcwd(dir, sizeof(dir))) {
541 if (eaccess(dir, R_OK | X_OK | F_OK)) {
542 ast_cli(a->fd, " Running directory: %s\n", "Unable to access");
543 } else {
544 ast_cli(a->fd, " Running directory: %s (%s)\n", dir, "Unable to access");
545 }
546 } else {
547 ast_cli(a->fd, " Running directory: %s\n", dir);
548 }
549#endif /* defined(HAVE_EACCESS) || defined(HAVE_EUIDACCESS) */
550 ast_cli(a->fd, " Executable includes: %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_EXEC_INCLUDES) ? "Enabled" : "Disabled");
551 ast_cli(a->fd, " Transcode via SLIN: %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_TRANSCODE_VIA_SLIN) ? "Enabled" : "Disabled");
552 ast_cli(a->fd, " Transmit silence during rec: %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_TRANSMIT_SILENCE) ? "Enabled" : "Disabled");
553 ast_cli(a->fd, " Generic PLC: %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_GENERIC_PLC) ? "Enabled" : "Disabled");
554 ast_cli(a->fd, " Generic PLC on equal codecs: %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_GENERIC_PLC_ON_EQUAL_CODECS) ? "Enabled" : "Disabled");
555 ast_cli(a->fd, " Hide Msg Chan AMI events: %s\n", ast_opt_hide_messaging_ami_events ? "Enabled" : "Disabled");
556 ast_cli(a->fd, " Sounds search custom dir: %s\n", ast_opt_sounds_search_custom ? "Enabled" : "Disabled");
557 ast_cli(a->fd, " Min DTMF duration:: %u\n", option_dtmfminduration);
558#if !defined(LOW_MEMORY)
559 ast_cli(a->fd, " Cache media frames: %s\n", ast_opt_cache_media_frames ? "Enabled" : "Disabled");
560#endif
561 ast_cli(a->fd, " RTP use dynamic payloads: %u\n", ast_option_rtpusedynamic);
562
564 ast_cli(a->fd, " RTP dynamic payload types: %u,%u-%u\n",
568 ast_cli(a->fd, " RTP dynamic payload types: %u-%u,%u-%u\n",
571 } else {
572 ast_cli(a->fd, " RTP dynamic payload types: %u-%u\n",
574 }
575
576 ast_cli(a->fd, "\n* Subsystems\n");
577 ast_cli(a->fd, " -------------\n");
578 ast_cli(a->fd, " Manager (AMI): %s\n", ast_manager_check_enabled() ? "Enabled" : "Disabled");
579 ast_cli(a->fd, " Web Manager (AMI/HTTP): %s\n", ast_webmanager_check_enabled() ? "Enabled" : "Disabled");
580 ast_cli(a->fd, " Call data records: %s\n", ast_cdr_is_enabled() ? "Enabled" : "Disabled");
581 ast_cli(a->fd, " Realtime Architecture (ARA): %s\n", ast_realtime_enabled() ? "Enabled" : "Disabled");
582
583 /*! \todo we could check musiconhold, voicemail, smdi, adsi, queues */
584
585 ast_cli(a->fd, "\n* Directories\n");
586 ast_cli(a->fd, " -------------\n");
587 ast_cli(a->fd, " Configuration file: %s\n", ast_config_AST_CONFIG_FILE);
588 ast_cli(a->fd, " Configuration directory: %s\n", ast_config_AST_CONFIG_DIR);
589 ast_cli(a->fd, " Module directory: %s\n", ast_config_AST_MODULE_DIR);
590 ast_cli(a->fd, " Spool directory: %s\n", ast_config_AST_SPOOL_DIR);
591 ast_cli(a->fd, " Log directory: %s\n", ast_config_AST_LOG_DIR);
592 ast_cli(a->fd, " Run/Sockets directory: %s\n", ast_config_AST_RUN_DIR);
593 ast_cli(a->fd, " PID file: %s\n", ast_config_AST_PID);
594 ast_cli(a->fd, " VarLib directory: %s\n", ast_config_AST_VAR_DIR);
595 ast_cli(a->fd, " Data directory: %s\n", ast_config_AST_DATA_DIR);
596 ast_cli(a->fd, " ASTDB: %s\n", ast_config_AST_DB);
597 ast_cli(a->fd, " IAX2 Keys directory: %s\n", ast_config_AST_KEY_DIR);
598 ast_cli(a->fd, " AGI Scripts directory: %s\n", ast_config_AST_AGI_DIR);
599 ast_cli(a->fd, " Cache directory: %s\n", ast_config_AST_CACHE_DIR);
600 ast_cli(a->fd, "\n\n");
601 return CLI_SUCCESS;
602}
603
604static char *handle_show_threads(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
605{
606 int count = 0;
607 struct thread_list_t *cur;
608 switch (cmd) {
609 case CLI_INIT:
610 e->command = "core show threads";
611 e->usage =
612 "Usage: core show threads\n"
613 " List threads currently active in the system.\n";
614 return NULL;
615 case CLI_GENERATE:
616 return NULL;
617 }
618
620 AST_RWLIST_TRAVERSE(&thread_list, cur, list) {
621 ast_cli(a->fd, "%p %d %s\n", (void *)cur->id, cur->lwp, cur->name);
622 count++;
623 }
625 ast_cli(a->fd, "%d threads listed.\n", count);
626 return CLI_SUCCESS;
627}
628
629#if defined (HAVE_SYSCTL) && defined(HAVE_SWAPCTL)
630/*
631 * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
632 * to be based on the new swapctl(2) system call.
633 */
634static int swapmode(int *used, int *total)
635{
636 struct swapent *swdev;
637 int nswap, rnswap, i;
638
639 nswap = swapctl(SWAP_NSWAP, 0, 0);
640 if (nswap == 0)
641 return 0;
642
643 swdev = ast_calloc(nswap, sizeof(*swdev));
644 if (swdev == NULL)
645 return 0;
646
647 rnswap = swapctl(SWAP_STATS, swdev, nswap);
648 if (rnswap == -1) {
649 ast_free(swdev);
650 return 0;
651 }
652
653 /* if rnswap != nswap, then what? */
654
655 /* Total things up */
656 *total = *used = 0;
657 for (i = 0; i < nswap; i++) {
658 if (swdev[i].se_flags & SWF_ENABLE) {
659 *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
660 *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
661 }
662 }
663 ast_free(swdev);
664 return 1;
665}
666#endif
667
668#if defined(HAVE_SYSINFO) || defined(HAVE_SYSCTL)
669/*! \brief Give an overview of system statistics */
670static char *handle_show_sysinfo(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
671{
672 uint64_t physmem, freeram;
673#if defined(HAVE_SYSINFO) || defined(HAVE_SWAPCTL)
674 int totalswap = 0;
675 uint64_t freeswap = 0;
676#endif
677 int nprocs = 0;
678 long uptime = 0;
679#if defined(HAVE_SYSINFO)
680 struct sysinfo sys_info;
681#elif defined(HAVE_SYSCTL)
682 static int pageshift;
683 struct vmtotal vmtotal;
684 struct timeval boottime;
685 time_t now;
686 int mib[2], pagesize;
687#if defined(HAVE_SWAPCTL)
688 int usedswap = 0;
689#endif
690 size_t len;
691#endif
692
693 switch (cmd) {
694 case CLI_INIT:
695 e->command = "core show sysinfo";
696 e->usage =
697 "Usage: core show sysinfo\n"
698 " List current system information.\n";
699 return NULL;
700 case CLI_GENERATE:
701 return NULL;
702 }
703
704#if defined(HAVE_SYSINFO)
705 sysinfo(&sys_info);
706 uptime = sys_info.uptime / 3600;
707 physmem = sys_info.totalram * sys_info.mem_unit;
708 freeram = (sys_info.freeram * sys_info.mem_unit) / 1024;
709 totalswap = (sys_info.totalswap * sys_info.mem_unit) / 1024;
710 freeswap = (sys_info.freeswap * sys_info.mem_unit) / 1024;
711 nprocs = sys_info.procs;
712#elif defined(HAVE_SYSCTL)
713 /* calculate the uptime by looking at boottime */
714 time(&now);
715 mib[0] = CTL_KERN;
716 mib[1] = KERN_BOOTTIME;
717 len = sizeof(boottime);
718 if (sysctl(mib, 2, &boottime, &len, NULL, 0) != -1) {
719 uptime = now - boottime.tv_sec;
720 }
721 uptime = uptime/3600;
722 /* grab total physical memory */
723 mib[0] = CTL_HW;
724#if defined(HW_PHYSMEM64)
725 mib[1] = HW_PHYSMEM64;
726#else
727 mib[1] = HW_PHYSMEM;
728#endif
729 len = sizeof(physmem);
730 sysctl(mib, 2, &physmem, &len, NULL, 0);
731
732 pagesize = getpagesize();
733 pageshift = 0;
734 while (pagesize > 1) {
735 pageshift++;
736 pagesize >>= 1;
737 }
738
739 /* we only need the amount of log(2)1024 for our conversion */
740 pageshift -= 10;
741
742 /* grab vm totals */
743 mib[0] = CTL_VM;
744 mib[1] = VM_METER;
745 len = sizeof(vmtotal);
746 sysctl(mib, 2, &vmtotal, &len, NULL, 0);
747 freeram = (vmtotal.t_free << pageshift);
748 /* generate swap usage and totals */
749#if defined(HAVE_SWAPCTL)
750 swapmode(&usedswap, &totalswap);
751 freeswap = (totalswap - usedswap);
752#endif
753 /* grab number of processes */
754#if defined(__OpenBSD__)
755 mib[0] = CTL_KERN;
756 mib[1] = KERN_NPROCS;
757 len = sizeof(nprocs);
758 sysctl(mib, 2, &nprocs, &len, NULL, 0);
759#endif
760#endif
761
762 ast_cli(a->fd, "\nSystem Statistics\n");
763 ast_cli(a->fd, "-----------------\n");
764 ast_cli(a->fd, " System Uptime: %ld hours\n", uptime);
765 ast_cli(a->fd, " Total RAM: %" PRIu64 " KiB\n", physmem / 1024);
766 ast_cli(a->fd, " Free RAM: %" PRIu64 " KiB\n", freeram);
767#if defined(HAVE_SYSINFO)
768 ast_cli(a->fd, " Buffer RAM: %" PRIu64 " KiB\n", ((uint64_t) sys_info.bufferram * sys_info.mem_unit) / 1024);
769#endif
770#if defined(HAVE_SYSINFO) || defined(HAVE_SWAPCTL)
771 ast_cli(a->fd, " Total Swap Space: %d KiB\n", totalswap);
772 ast_cli(a->fd, " Free Swap Space: %" PRIu64 " KiB\n\n", freeswap);
773#endif
774 ast_cli(a->fd, " Number of Processes: %d \n\n", nprocs);
775 return CLI_SUCCESS;
776}
777#endif
778
780 const char *name;
781 uint64_t scale; /* if non-zero, values are scaled by this */
782 int64_t mark;
783 int64_t value;
784 int64_t events;
785};
786
790 struct profile_entry e[0];
791};
792
793static struct profile_data *prof_data;
794#endif /* ! LOW_MEMORY */
795
796/*! \brief allocates a counter with a given name and scale.
797 * \return Returns the identifier of the counter.
798 */
799int ast_add_profile(const char *name, uint64_t scale)
800{
801#if !defined(LOW_MEMORY)
802 int l = sizeof(struct profile_data);
803 int n = 10; /* default entries */
804
805 if (prof_data == NULL) {
806 prof_data = ast_calloc(1, l + n*sizeof(struct profile_entry));
807 if (prof_data == NULL)
808 return -1;
809 prof_data->entries = 0;
810 prof_data->max_size = n;
811 }
813 void *p;
814 n = prof_data->max_size + 20;
815 p = ast_realloc(prof_data, l + n*sizeof(struct profile_entry));
816 if (p == NULL)
817 return -1;
818 prof_data = p;
819 prof_data->max_size = n;
820 }
821 n = prof_data->entries++;
823 prof_data->e[n].value = 0;
824 prof_data->e[n].events = 0;
825 prof_data->e[n].mark = 0;
826 prof_data->e[n].scale = scale;
827 return n;
828#else /* if defined(LOW_MEMORY) */
829 return 0;
830#endif
831}
832
833int64_t ast_profile(int i, int64_t delta)
834{
835#if !defined(LOW_MEMORY)
836 if (!prof_data || i < 0 || i > prof_data->entries) /* invalid index */
837 return 0;
838 if (prof_data->e[i].scale > 1)
839 delta /= prof_data->e[i].scale;
840 prof_data->e[i].value += delta;
841 prof_data->e[i].events++;
842 return prof_data->e[i].value;
843#else /* if defined(LOW_MEMORY) */
844 return 0;
845#endif
846}
847
848#if !defined(LOW_MEMORY)
849/* The RDTSC instruction was introduced on the Pentium processor and is not
850 * implemented on certain clones, like the Cyrix 586. Hence, the previous
851 * expectation of __i386__ was in error. */
852#if defined ( __i686__) && (defined(__FreeBSD__) || defined(__NetBSD__) || defined(linux))
853#if defined(__FreeBSD__)
854#include <machine/cpufunc.h>
855#elif defined(__NetBSD__) || defined(linux)
856static __inline uint64_t
857rdtsc(void)
858{
859 uint64_t rv;
860
861 __asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
862 return (rv);
863}
864#endif
865#else /* supply a dummy function on other platforms */
866static __inline uint64_t
867rdtsc(void)
868{
869 return 0;
870}
871#endif
872#endif /* ! LOW_MEMORY */
873
874int64_t ast_mark(int i, int startstop)
875{
876#if !defined(LOW_MEMORY)
877 if (!prof_data || i < 0 || i > prof_data->entries) /* invalid index */
878 return 0;
879 if (startstop == 1)
880 prof_data->e[i].mark = rdtsc();
881 else {
882 prof_data->e[i].mark = (rdtsc() - prof_data->e[i].mark);
883 if (prof_data->e[i].scale > 1)
884 prof_data->e[i].mark /= prof_data->e[i].scale;
885 prof_data->e[i].value += prof_data->e[i].mark;
886 prof_data->e[i].events++;
887 }
888 return prof_data->e[i].mark;
889#else /* if defined(LOW_MEMORY) */
890 return 0;
891#endif
892}
893
894#if !defined(LOW_MEMORY)
895#define DEFINE_PROFILE_MIN_MAX_VALUES min = 0; \
896 max = prof_data->entries;\
897 if (a->argc > 3) { /* specific entries */ \
898 if (isdigit(a->argv[3][0])) { \
899 min = atoi(a->argv[3]); \
900 if (a->argc == 5 && strcmp(a->argv[4], "-")) \
901 max = atoi(a->argv[4]); \
902 } else \
903 search = a->argv[3]; \
904 } \
905 if (max > prof_data->entries) \
906 max = prof_data->entries;
907
908static char *handle_show_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
909{
910 int i, min, max;
911 const char *search = NULL;
912 switch (cmd) {
913 case CLI_INIT:
914 e->command = "core show profile";
915 e->usage = "Usage: core show profile\n"
916 " show profile information";
917 return NULL;
918 case CLI_GENERATE:
919 return NULL;
920 }
921
922 if (prof_data == NULL)
923 return 0;
924
926 ast_cli(a->fd, "profile values (%d, allocated %d)\n-------------------\n",
928 ast_cli(a->fd, "%6s %8s %10s %12s %12s %s\n", "ID", "Scale", "Events",
929 "Value", "Average", "Name");
930 for (i = min; i < max; i++) {
931 struct profile_entry *entry = &prof_data->e[i];
932 if (!search || strstr(entry->name, search))
933 ast_cli(a->fd, "%6d: [%8ld] %10ld %12lld %12lld %s\n",
934 i,
935 (long)entry->scale,
936 (long)entry->events, (long long)entry->value,
937 (long long)(entry->events ? entry->value / entry->events : entry->value),
938 entry->name);
939 }
940 return CLI_SUCCESS;
941}
942
943static char *handle_clear_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
944{
945 int i, min, max;
946 const char *search = NULL;
947 switch (cmd) {
948 case CLI_INIT:
949 e->command = "core clear profile";
950 e->usage = "Usage: core clear profile\n"
951 " clear profile information";
952 return NULL;
953 case CLI_GENERATE:
954 return NULL;
955 }
956
957 if (prof_data == NULL)
958 return 0;
959
961 for (i= min; i < max; i++) {
962 if (!search || strstr(prof_data->e[i].name, search)) {
963 prof_data->e[i].value = 0;
964 prof_data->e[i].events = 0;
965 }
966 }
967 return CLI_SUCCESS;
968}
969#undef DEFINE_PROFILE_MIN_MAX_VALUES
970
971#endif /* ! LOW_MEMORY */
972
973int ast_pbx_uuid_get(char *pbx_uuid, int length)
974{
975 return ast_db_get("pbx", "UUID", pbx_uuid, length);
976}
977
978static void publish_fully_booted(void)
979{
980 struct ast_json *json_object;
981 int uptime = 0;
982 int lastreloaded = 0;
983 struct timeval tmp;
984 struct timeval curtime = ast_tvnow();
985
986 if (ast_startuptime.tv_sec) {
987 tmp = ast_tvsub(curtime, ast_startuptime);
988 uptime = (int) tmp.tv_sec;
989 }
990
991 if (ast_lastreloadtime.tv_sec) {
992 tmp = ast_tvsub(curtime, ast_lastreloadtime);
993 lastreloaded = (int) tmp.tv_sec;
994 }
995
996 json_object = ast_json_pack("{s: s, s: i, s: i}",
997 "Status", "Fully Booted",
998 "Uptime", uptime,
999 "LastReload", lastreloaded);
1000 ast_manager_publish_event("FullyBooted", EVENT_FLAG_SYSTEM, json_object);
1001 ast_json_unref(json_object);
1002}
1003
1004static void ast_run_atexits(int run_cleanups)
1005{
1006 struct ast_atexit *ae;
1007
1009 while ((ae = AST_LIST_REMOVE_HEAD(&atexits, list))) {
1010 if (ae->func && (!ae->is_cleanup || run_cleanups)) {
1011 ae->func();
1012 }
1013 ast_free(ae);
1014 }
1016}
1017
1018static void __ast_unregister_atexit(void (*func)(void))
1019{
1020 struct ast_atexit *ae;
1021
1023 if (ae->func == func) {
1025 ast_free(ae);
1026 break;
1027 }
1028 }
1030}
1031
1032static int register_atexit(void (*func)(void), int is_cleanup)
1033{
1034 struct ast_atexit *ae;
1035
1036 ae = ast_calloc(1, sizeof(*ae));
1037 if (!ae) {
1038 return -1;
1039 }
1040 ae->func = func;
1041 ae->is_cleanup = is_cleanup;
1042
1047
1048 return 0;
1049}
1050
1051int ast_register_atexit(void (*func)(void))
1052{
1053 return register_atexit(func, 0);
1054}
1055
1056int ast_register_cleanup(void (*func)(void))
1057{
1058 return register_atexit(func, 1);
1059}
1060
1061void ast_unregister_atexit(void (*func)(void))
1062{
1066}
1067
1068/* Sending commands from consoles back to the daemon requires a terminating NULL */
1069static int fdsend(int fd, const char *s)
1070{
1071 return write(fd, s, strlen(s) + 1);
1072}
1073
1074/* Sending messages from the daemon back to the display requires _excluding_ the terminating NULL */
1075static int fdprint(int fd, const char *s)
1076{
1077 return write(fd, s, strlen(s));
1078}
1079
1080/*! \brief NULL handler so we can collect the child exit status */
1081static void _null_sig_handler(int sig)
1082{
1083}
1084
1085static struct sigaction null_sig_handler = {
1086 .sa_handler = _null_sig_handler,
1087 .sa_flags = SA_RESTART,
1088};
1089
1090static struct sigaction ignore_sig_handler = {
1091 .sa_handler = SIG_IGN,
1092};
1093
1095/*! \brief Keep track of how many threads are currently trying to wait*() on
1096 * a child process
1097 */
1098static unsigned int safe_system_level = 0;
1099static struct sigaction safe_system_prev_handler;
1100
1102{
1103 unsigned int level;
1104
1106 level = safe_system_level++;
1107
1108 /* only replace the handler if it has not already been done */
1109 if (level == 0) {
1110 sigaction(SIGCHLD, &null_sig_handler, &safe_system_prev_handler);
1111 }
1112
1114}
1115
1117{
1118 unsigned int level;
1119
1121
1122 /* Wrapping around here is an error */
1124
1125 level = --safe_system_level;
1126
1127 /* only restore the handler if we are the last one */
1128 if (level == 0) {
1129 sigaction(SIGCHLD, &safe_system_prev_handler, NULL);
1130 }
1131
1133}
1134
1135/*! \brief fork and perform other preparations for spawning applications */
1136static pid_t safe_exec_prep(int dualfork)
1137{
1138 pid_t pid;
1139
1140#if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
1142
1143#ifdef HAVE_WORKING_FORK
1144 pid = fork();
1145#else
1146 pid = vfork();
1147#endif
1148
1149 if (pid == 0) {
1150#ifdef HAVE_CAP
1151 cap_set_proc(child_cap);
1152#endif
1153#ifdef HAVE_WORKING_FORK
1156 }
1157 /* Close file descriptors and launch system command */
1158 ast_close_fds_above_n(STDERR_FILENO);
1159#endif
1160 if (dualfork) {
1161#ifdef HAVE_WORKING_FORK
1162 pid = fork();
1163#else
1164 pid = vfork();
1165#endif
1166 if (pid < 0) {
1167 /* Second fork failed. */
1168 /* No logger available. */
1169 _exit(1);
1170 }
1171
1172 if (pid > 0) {
1173 /* This is the first fork, exit so the reaper finishes right away. */
1174 _exit(0);
1175 }
1176
1177 /* This is the second fork. The first fork will exit immediately so
1178 * Asterisk doesn't have to wait for completion.
1179 * ast_safe_system("cmd &") would run in the background, but the '&'
1180 * cannot be added with ast_safe_execvp, so we have to double fork.
1181 */
1182 }
1183 }
1184
1185 if (pid < 0) {
1186 ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
1187 }
1188#else
1189 ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(ENOTSUP));
1190 pid = -1;
1191#endif
1192
1193 return pid;
1194}
1195
1196/*! \brief wait for spawned application to complete and unreplace sigchld */
1197static int safe_exec_wait(pid_t pid)
1198{
1199 int res = -1;
1200
1201#if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
1202 if (pid > 0) {
1203 for (;;) {
1204 int status;
1205
1206 res = waitpid(pid, &status, 0);
1207 if (res > -1) {
1208 res = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
1209 break;
1210 }
1211 if (errno != EINTR) {
1212 break;
1213 }
1214 }
1215 }
1216
1218#endif
1219
1220 return res;
1221}
1222
1223int ast_safe_execvp(int dualfork, const char *file, char *const argv[])
1224{
1225 pid_t pid = safe_exec_prep(dualfork);
1226
1227 if (pid == 0) {
1228 execvp(file, argv);
1229 _exit(1);
1230 /* noreturn from _exit */
1231 }
1232
1233 return safe_exec_wait(pid);
1234}
1235
1236int ast_safe_system(const char *s)
1237{
1238 pid_t pid = safe_exec_prep(0);
1239
1240 if (pid == 0) {
1241 execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
1242 _exit(1);
1243 /* noreturn from _exit */
1244 }
1245
1246 return safe_exec_wait(pid);
1247}
1248
1249/*!
1250 * \brief enable or disable a logging level to a specified console
1251 */
1252void ast_console_toggle_loglevel(int fd, int level, int state)
1253{
1254 int x;
1255
1256 if (level >= NUMLOGLEVELS) {
1257 level = NUMLOGLEVELS - 1;
1258 }
1259
1260 for (x = 0;x < AST_MAX_CONNECTS; x++) {
1261 if (fd == consoles[x].fd) {
1262 /*
1263 * Since the logging occurs when levels are false, set to
1264 * flipped iinput because this function accepts 0 as off and 1 as on
1265 */
1266 consoles[x].levels[level] = state ? 0 : 1;
1267 return;
1268 }
1269 }
1270}
1271
1272/*!
1273 * \brief mute or unmute a console from logging
1274 */
1275void ast_console_toggle_mute(int fd, int silent)
1276{
1277 int x;
1278 for (x = 0;x < AST_MAX_CONNECTS; x++) {
1279 if (fd == consoles[x].fd) {
1280 if (consoles[x].mute) {
1281 consoles[x].mute = 0;
1282 if (!silent)
1283 ast_cli(fd, "Console is not muted anymore.\n");
1284 } else {
1285 consoles[x].mute = 1;
1286 if (!silent)
1287 ast_cli(fd, "Console is muted.\n");
1288 }
1289 return;
1290 }
1291 }
1292 ast_cli(fd, "Couldn't find remote console.\n");
1293}
1294
1295/*!
1296 * \brief log the string to all attached network console clients
1297 */
1298static void ast_network_puts_mutable(const char *string, int level, int sublevel)
1299{
1300 int x;
1301
1302 for (x = 0; x < AST_MAX_CONNECTS; ++x) {
1303 if (consoles[x].fd < 0
1304 || consoles[x].mute
1305 || consoles[x].levels[level]
1306 || (level == __LOG_VERBOSE && consoles[x].option_verbose < sublevel)) {
1307 continue;
1308 }
1309 fdprint(consoles[x].p[1], string);
1310 }
1311}
1312
1313/*!
1314 * \brief log the string to the root console, and all attached
1315 * network console clients
1316 */
1317void ast_console_puts_mutable(const char *string, int level)
1318{
1319 ast_console_puts_mutable_full(string, level, 0);
1320}
1321
1322static int console_print(const char *s);
1323
1324void ast_console_puts_mutable_full(const char *message, int level, int sublevel)
1325{
1326 /* Send to the root console */
1328
1329 /* Wake up a poll()ing console */
1331 pthread_kill(consolethread, SIGURG);
1332 }
1333
1334 /* Send to any network console clients */
1335 ast_network_puts_mutable(message, level, sublevel);
1336}
1337
1338/*!
1339 * \brief write the string to all attached console clients
1340 */
1341static void ast_network_puts(const char *string)
1342{
1343 int x;
1344
1345 for (x = 0; x < AST_MAX_CONNECTS; ++x) {
1346 if (consoles[x].fd < 0) {
1347 continue;
1348 }
1349 fdprint(consoles[x].p[1], string);
1350 }
1351}
1352
1353/*!
1354 * \brief write the string to the root console, and all attached
1355 * network console clients
1356 */
1357void ast_console_puts(const char *string)
1358{
1359 /* Send to the root console */
1360 fputs(string, stdout);
1361 fflush(stdout);
1362
1363 /* Send to any network console clients */
1364 ast_network_puts(string);
1365}
1366
1367static pthread_t lthread;
1368
1369/*!
1370 * \brief read() function supporting the reception of user credentials.
1371 *
1372 * \param fd Socket file descriptor.
1373 * \param buffer Receive buffer.
1374 * \param size 'buffer' size.
1375 * \param con Console structure to set received credentials
1376 * \retval -1 on error
1377 * \retval the number of bytes received on success.
1378 */
1379static int read_credentials(int fd, char *buffer, size_t size, struct console *con)
1380{
1381#if defined(SO_PEERCRED)
1382#ifdef HAVE_STRUCT_SOCKPEERCRED_UID
1383#define HAVE_STRUCT_UCRED_UID
1384 struct sockpeercred cred;
1385#else
1386 struct ucred cred;
1387#endif
1388 socklen_t len = sizeof(cred);
1389#endif
1390#if defined(HAVE_GETPEEREID)
1391 uid_t uid;
1392 gid_t gid;
1393#else
1394 int uid, gid;
1395#endif
1396 int result;
1397
1398 result = read(fd, buffer, size);
1399 if (result < 0) {
1400 return result;
1401 }
1402
1403#if defined(SO_PEERCRED) && (defined(HAVE_STRUCT_UCRED_UID) || defined(HAVE_STRUCT_UCRED_CR_UID))
1404 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len)) {
1405 return result;
1406 }
1407#if defined(HAVE_STRUCT_UCRED_UID)
1408 uid = cred.uid;
1409 gid = cred.gid;
1410#else /* defined(HAVE_STRUCT_UCRED_CR_UID) */
1411 uid = cred.cr_uid;
1412 gid = cred.cr_gid;
1413#endif /* defined(HAVE_STRUCT_UCRED_UID) */
1414
1415#elif defined(HAVE_GETPEEREID)
1416 if (getpeereid(fd, &uid, &gid)) {
1417 return result;
1418 }
1419#else
1420 return result;
1421#endif
1422 con->uid = uid;
1423 con->gid = gid;
1424
1425 return result;
1426}
1427
1428/* This is the thread running the remote console on the main process. */
1429static void *netconsole(void *vconsole)
1430{
1431 struct console *con = vconsole;
1432 char hostname[MAXHOSTNAMELEN] = "";
1433 char inbuf[512];
1434 char outbuf[512];
1435 const char * const end_buf = inbuf + sizeof(inbuf);
1436 char *start_read = inbuf;
1437 int res;
1438 struct pollfd fds[2];
1439
1440 if (gethostname(hostname, sizeof(hostname)-1))
1441 ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
1442 snprintf(outbuf, sizeof(outbuf), "%s/%ld/%s\n", hostname, (long)ast_mainpid, ast_get_version());
1443 fdprint(con->fd, outbuf);
1445 for (;;) {
1446 fds[0].fd = con->fd;
1447 fds[0].events = POLLIN;
1448 fds[0].revents = 0;
1449 fds[1].fd = con->p[0];
1450 fds[1].events = POLLIN;
1451 fds[1].revents = 0;
1452
1453 res = ast_poll(fds, 2, -1);
1454 if (res < 0) {
1455 if (errno != EINTR)
1456 ast_log(LOG_WARNING, "poll returned < 0: %s\n", strerror(errno));
1457 continue;
1458 }
1459 if (fds[0].revents) {
1460 int cmds_read, bytes_read;
1461 if ((bytes_read = read_credentials(con->fd, start_read, end_buf - start_read, con)) < 1) {
1462 break;
1463 }
1464 /* XXX This will only work if it is the first command, and I'm not sure fixing it is worth the effort. */
1465 if (strncmp(inbuf, "cli quit after ", 15) == 0) {
1466 ast_cli_command_multiple_full(con->uid, con->gid, con->fd, bytes_read - 15, inbuf + 15);
1467 break;
1468 }
1469 /* ast_cli_command_multiple_full will only process individual commands terminated by a
1470 * NULL and not trailing partial commands. */
1471 if (!(cmds_read = ast_cli_command_multiple_full(con->uid, con->gid, con->fd, bytes_read + start_read - inbuf, inbuf))) {
1472 /* No commands were read. We either have a short read on the first command
1473 * with space left, or a command that is too long */
1474 if (start_read + bytes_read < end_buf) {
1475 start_read += bytes_read;
1476 } else {
1477 ast_log(LOG_ERROR, "Command too long! Skipping\n");
1478 start_read = inbuf;
1479 }
1480 continue;
1481 }
1482 if (start_read[bytes_read - 1] == '\0') {
1483 /* The read ended on a command boundary, start reading again at the head of inbuf */
1484 start_read = inbuf;
1485 continue;
1486 }
1487 /* If we get this far, we have left over characters that have not been processed.
1488 * Advance to the character after the last command read by ast_cli_command_multiple_full.
1489 * We are guaranteed to have at least cmds_read NULLs */
1490 while (cmds_read-- && (start_read = strchr(start_read, '\0'))) {
1491 start_read++;
1492 }
1493 memmove(inbuf, start_read, end_buf - start_read);
1494 start_read = end_buf - start_read + inbuf;
1495 }
1496 if (fds[1].revents) {
1497 res = read_credentials(con->p[0], outbuf, sizeof(outbuf), con);
1498 if (res < 1) {
1499 ast_log(LOG_ERROR, "read returned %d\n", res);
1500 break;
1501 }
1502 res = write(con->fd, outbuf, res);
1503 if (res < 1)
1504 break;
1505 }
1506 }
1508 if (!ast_opt_hide_connect) {
1509 ast_verb(3, "Remote UNIX connection disconnected\n");
1510 }
1511 close(con->fd);
1512 close(con->p[0]);
1513 close(con->p[1]);
1514 con->fd = -1;
1515
1516 return NULL;
1517}
1518
1519static void *listener(void *unused)
1520{
1521 struct sockaddr_un sunaddr;
1522 int s;
1523 socklen_t len;
1524 int x;
1525 int poll_result;
1526 struct pollfd fds[1];
1527
1528 for (;;) {
1529 if (ast_socket < 0) {
1530 return NULL;
1531 }
1532 fds[0].fd = ast_socket;
1533 fds[0].events = POLLIN;
1534 poll_result = ast_poll(fds, 1, -1);
1535 pthread_testcancel();
1536 if (poll_result < 0) {
1537 if (errno != EINTR) {
1538 ast_log(LOG_WARNING, "poll returned error: %s\n", strerror(errno));
1539 }
1540 continue;
1541 }
1542 len = sizeof(sunaddr);
1543 s = accept(ast_socket, (struct sockaddr *)&sunaddr, &len);
1544 if (s < 0) {
1545 if (errno != EINTR)
1546 ast_log(LOG_WARNING, "Accept returned %d: %s\n", s, strerror(errno));
1547 } else {
1548#if defined(SO_PASSCRED)
1549 int sckopt = 1;
1550 /* turn on socket credentials passing. */
1551 if (setsockopt(s, SOL_SOCKET, SO_PASSCRED, &sckopt, sizeof(sckopt)) < 0) {
1552 ast_log(LOG_WARNING, "Unable to turn on socket credentials passing\n");
1553 close(s);
1554 } else
1555#endif
1556 {
1557 for (x = 0; x < AST_MAX_CONNECTS; x++) {
1558 if (consoles[x].fd >= 0) {
1559 continue;
1560 }
1561 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, consoles[x].p)) {
1562 ast_log(LOG_ERROR, "Unable to create pipe: %s\n", strerror(errno));
1563 fdprint(s, "Server failed to create pipe\n");
1564 close(s);
1565 break;
1566 }
1567 ast_fd_set_flags(consoles[x].p[1], O_NONBLOCK);
1568 consoles[x].mute = 1; /* Default is muted, we will un-mute if necessary */
1569 /* Default uid and gid to -2, so then in cli.c/cli_has_permissions() we will be able
1570 to know if the user didn't send the credentials. */
1571 consoles[x].uid = -2;
1572 consoles[x].gid = -2;
1573 /* Server default of remote console verbosity level is OFF. */
1574 consoles[x].option_verbose = 0;
1575 consoles[x].fd = s;
1577 consoles[x].fd = -1;
1578 ast_log(LOG_ERROR, "Unable to spawn thread to handle connection: %s\n", strerror(errno));
1579 close(consoles[x].p[0]);
1580 close(consoles[x].p[1]);
1581 fdprint(s, "Server failed to spawn thread\n");
1582 close(s);
1583 }
1584 break;
1585 }
1586 if (x >= AST_MAX_CONNECTS) {
1587 fdprint(s, "No more connections allowed\n");
1588 ast_log(LOG_WARNING, "No more connections allowed\n");
1589 close(s);
1590 } else if ((consoles[x].fd > -1) && (!ast_opt_hide_connect)) {
1591 ast_verb(3, "Remote UNIX connection\n");
1592 }
1593 }
1594 }
1595 }
1596 return NULL;
1597}
1598
1599static int ast_makesocket(void)
1600{
1601 struct sockaddr_un sunaddr;
1602 int res;
1603 int x;
1604 uid_t uid = -1;
1605 gid_t gid = -1;
1606
1607 for (x = 0; x < AST_MAX_CONNECTS; x++) {
1608 consoles[x].fd = -1;
1609 }
1610
1611 if (ast_socket_is_sd) {
1613
1614 goto start_lthread;
1615 }
1616
1617 unlink(ast_config_AST_SOCKET);
1618 ast_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
1619 if (ast_socket < 0) {
1620 ast_log(LOG_WARNING, "Unable to create control socket: %s\n", strerror(errno));
1621 return -1;
1622 }
1623 memset(&sunaddr, 0, sizeof(sunaddr));
1624 sunaddr.sun_family = AF_LOCAL;
1625 ast_copy_string(sunaddr.sun_path, ast_config_AST_SOCKET, sizeof(sunaddr.sun_path));
1626 res = bind(ast_socket, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
1627 if (res) {
1628 ast_log(LOG_WARNING, "Unable to bind socket to %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1629 close(ast_socket);
1630 ast_socket = -1;
1631 return -1;
1632 }
1633 res = listen(ast_socket, 2);
1634 if (res < 0) {
1635 ast_log(LOG_WARNING, "Unable to listen on socket %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1636 close(ast_socket);
1637 ast_socket = -1;
1638 return -1;
1639 }
1640
1641start_lthread:
1643 ast_log(LOG_WARNING, "Unable to create listener thread.\n");
1644 close(ast_socket);
1645 return -1;
1646 }
1647
1648 if (ast_socket_is_sd) {
1649 /* owner/group/permissions are set by systemd, we might not even have access
1650 * to socket file so leave it alone */
1651 return 0;
1652 }
1653
1655 struct passwd *pw;
1656 if ((pw = getpwnam(ast_config_AST_CTL_OWNER)) == NULL)
1657 ast_log(LOG_WARNING, "Unable to find uid of user %s\n", ast_config_AST_CTL_OWNER);
1658 else
1659 uid = pw->pw_uid;
1660 }
1661
1663 struct group *grp;
1664 if ((grp = getgrnam(ast_config_AST_CTL_GROUP)) == NULL)
1665 ast_log(LOG_WARNING, "Unable to find gid of group %s\n", ast_config_AST_CTL_GROUP);
1666 else
1667 gid = grp->gr_gid;
1668 }
1669
1670 if (chown(ast_config_AST_SOCKET, uid, gid) < 0)
1671 ast_log(LOG_WARNING, "Unable to change ownership of %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1672
1674 unsigned int p1;
1675 mode_t p;
1676 sscanf(ast_config_AST_CTL_PERMISSIONS, "%30o", &p1);
1677 p = p1;
1678 if ((chmod(ast_config_AST_SOCKET, p)) < 0)
1679 ast_log(LOG_WARNING, "Unable to change file permissions of %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1680 }
1681
1682 return 0;
1683}
1684
1685static int ast_tryconnect(void)
1686{
1687 struct sockaddr_un sunaddr;
1688 int res;
1689 ast_consock = socket(PF_LOCAL, SOCK_STREAM, 0);
1690 if (ast_consock < 0) {
1691 fprintf(stderr, "Unable to create socket: %s\n", strerror(errno));
1692 return 0;
1693 }
1694 memset(&sunaddr, 0, sizeof(sunaddr));
1695 sunaddr.sun_family = AF_LOCAL;
1696 ast_copy_string(sunaddr.sun_path, ast_config_AST_SOCKET, sizeof(sunaddr.sun_path));
1697 res = connect(ast_consock, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
1698 if (res) {
1699 close(ast_consock);
1700 ast_consock = -1;
1701 return 0;
1702 } else
1703 return 1;
1704}
1705
1706/*! \brief Urgent handler
1707 *
1708 * Called by soft_hangup to interrupt the poll, read, or other
1709 * system call. We don't actually need to do anything though.
1710 * Remember: Cannot EVER ast_log from within a signal handler
1711 */
1712static void _urg_handler(int num)
1713{
1714 return;
1715}
1716
1717static struct sigaction urg_handler = {
1718 .sa_handler = _urg_handler,
1719};
1720
1721static void _hup_handler(int num)
1722{
1723 int save_errno = errno;
1724
1725 if (restartnow) {
1726 if (el) {
1727 el_end(el);
1728 }
1729 execvp(_argv[0], _argv);
1730 }
1731
1732 printf("Received HUP signal -- Reloading configs\n");
1733 sig_flags.need_reload = 1;
1735 fprintf(stderr, "hup_handler: write() failed: %s\n", strerror(errno));
1736 }
1737 errno = save_errno;
1738}
1739
1740static struct sigaction hup_handler = {
1741 .sa_handler = _hup_handler,
1742 .sa_flags = SA_RESTART,
1743};
1744
1745static void _child_handler(int sig)
1746{
1747 /* Must not ever ast_log or ast_verbose within signal handler */
1748 int n, status, save_errno = errno;
1749
1750 /*
1751 * Reap all dead children -- not just one
1752 */
1753 for (n = 0; waitpid(-1, &status, WNOHANG) > 0; n++)
1754 ;
1755 if (n == 0 && option_debug)
1756 printf("Huh? Child handler, but nobody there?\n");
1757 errno = save_errno;
1758}
1759
1760static struct sigaction child_handler = {
1761 .sa_handler = _child_handler,
1762 .sa_flags = SA_RESTART,
1763};
1764
1765/*! \brief Set an X-term or screen title */
1766static void set_title(char *text)
1767{
1768 if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
1769 fprintf(stdout, "\033]2;%s\007", text);
1770}
1771
1772static void set_icon(char *text)
1773{
1774 if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
1775 fprintf(stdout, "\033]1;%s\007", text);
1776}
1777
1778/*! \brief Check whether we were set to high(er) priority. */
1779static int has_priority(void)
1780{
1781 /* Neither of these calls should fail with these arguments. */
1782#ifdef __linux__
1783 /* For SCHED_OTHER, SCHED_BATCH and SCHED_IDLE, this will return
1784 * 0. For the realtime priorities SCHED_RR and SCHED_FIFO, it
1785 * will return something >= 1. */
1786 return sched_getscheduler(0);
1787#else
1788 /* getpriority() can return a value in -20..19 (or even -INF..20)
1789 * where negative numbers are high priority. We don't bother
1790 * checking errno. If the query fails and it returns -1, we'll
1791 * assume that we're running at high prio; a safe assumption
1792 * that will enable the resource starvation monitor (canary)
1793 * just in case. */
1794 return (getpriority(PRIO_PROCESS, 0) < 0);
1795#endif
1796}
1797
1798/*! \brief Set priority on all known threads. */
1799static int set_priority_all(int pri)
1800{
1801#if !defined(__linux__)
1802 /* The non-linux version updates the entire process prio. */
1803 return ast_set_priority(pri);
1804#elif defined(LOW_MEMORY)
1805 ast_log(LOG_WARNING, "Unable to enumerate all threads to update priority\n");
1806 return ast_set_priority(pri);
1807#else
1808 struct thread_list_t *cur;
1809 struct sched_param sched;
1810 char const *policy_str;
1811 int policy;
1812
1813 memset(&sched, 0, sizeof(sched));
1814 if (pri) {
1815 policy = SCHED_RR;
1816 policy_str = "realtime";
1817 sched.sched_priority = 10;
1818 } else {
1819 policy = SCHED_OTHER;
1820 policy_str = "regular";
1821 sched.sched_priority = 0;
1822 }
1823 if (sched_setscheduler(getpid(), policy, &sched)) {
1824 ast_log(LOG_WARNING, "Unable to set %s thread priority on main thread\n", policy_str);
1825 return -1;
1826 }
1827 ast_verb(1, "Setting %s thread priority on all threads\n", policy_str);
1829 AST_RWLIST_TRAVERSE(&thread_list, cur, list) {
1830 /* Don't care about the return value. It should work. */
1831 sched_setscheduler(cur->lwp, policy, &sched);
1832 }
1834 return 0;
1835#endif
1836}
1837
1838/*! \brief We set ourselves to a high priority, that we might pre-empt
1839 * everything else. If your PBX has heavy activity on it, this is a
1840 * good thing.
1841 */
1843{
1844 struct sched_param sched;
1845 memset(&sched, 0, sizeof(sched));
1846#ifdef __linux__
1847 if (pri) {
1848 sched.sched_priority = 10;
1849 if (sched_setscheduler(0, SCHED_RR, &sched)) {
1850 return -1;
1851 }
1852 } else {
1853 sched.sched_priority = 0;
1854 /* According to the manpage, these parameters can never fail. */
1855 sched_setscheduler(0, SCHED_OTHER, &sched);
1856 }
1857#else
1858 if (pri) {
1859 if (setpriority(PRIO_PROCESS, 0, -10) == -1) {
1860 ast_log(LOG_WARNING, "Unable to set high priority\n");
1861 return -1;
1862 } else
1863 ast_verb(1, "Set to high priority\n");
1864 } else {
1865 /* According to the manpage, these parameters can never fail. */
1866 setpriority(PRIO_PROCESS, 0, 0);
1867 }
1868#endif
1869 return 0;
1870}
1871
1873{
1875}
1876
1878{
1879 return shutdown_pending;
1880}
1881
1883{
1884 int shutdown_aborted = 0;
1885
1887 if (shuttingdown >= SHUTDOWN_FAST) {
1889 shutdown_pending = 0;
1890 shutdown_aborted = 1;
1891 }
1893 return shutdown_aborted;
1894}
1895
1896/*!
1897 * \internal
1898 * \brief Initiate system shutdown -- prevents new channels from being allocated.
1899 */
1900static void ast_begin_shutdown(void)
1901{
1904 shutdown_pending = 1;
1905 }
1907}
1908
1909static int can_safely_quit(shutdown_nice_t niceness, int restart);
1910static void really_quit(int num, shutdown_nice_t niceness, int restart);
1911
1912static void quit_handler(int num, shutdown_nice_t niceness, int restart)
1913{
1914 if (can_safely_quit(niceness, restart)) {
1915 really_quit(num, niceness, restart);
1916 /* No one gets here. */
1917 }
1918 /* It wasn't our time. */
1919}
1920
1921#define SHUTDOWN_TIMEOUT 15 /* Seconds */
1922
1923/*!
1924 * \internal
1925 * \brief Wait for all channels to die, a timeout, or shutdown cancelled.
1926 * \since 13.3.0
1927 *
1928 * \param niceness Shutdown niceness in effect
1929 * \param seconds Number of seconds to wait or less than zero if indefinitely.
1930 *
1931 * \retval zero if waiting wasn't necessary. We were idle.
1932 * \retval non-zero if we had to wait.
1933 */
1934static int wait_for_channels_to_die(shutdown_nice_t niceness, int seconds)
1935{
1936 time_t start;
1937 time_t now;
1938 int waited = 0;
1939
1940 time(&start);
1941 for (;;) {
1942 if (!ast_undestroyed_channels() || shuttingdown != niceness) {
1943 break;
1944 }
1945 if (seconds < 0) {
1946 /* No timeout so just poll every second */
1947 sleep(1);
1948 } else {
1949 time(&now);
1950
1951 /* Wait up to the given seconds for all channels to go away */
1952 if (seconds < (now - start)) {
1953 break;
1954 }
1955
1956 /* Sleep 1/10 of a second */
1957 usleep(100000);
1958 }
1959 waited = 1;
1960 }
1961 return waited;
1962}
1963
1964static int can_safely_quit(shutdown_nice_t niceness, int restart)
1965{
1966 int waited = 0;
1967
1968 /* Check if someone else isn't already doing this. */
1970 if (shuttingdown != NOT_SHUTTING_DOWN && niceness >= shuttingdown) {
1971 /* Already in progress and other request was less nice. */
1973 ast_verbose("Ignoring asterisk %s request, already in progress.\n", restart ? "restart" : "shutdown");
1974 return 0;
1975 }
1976 shuttingdown = niceness;
1978
1979 /* Try to get as many CDRs as possible submitted to the backend engines
1980 * (if in batch mode). really_quit happens to call it again when running
1981 * the atexit handlers, otherwise this would be a bit early. */
1983
1984 /*
1985 * Shutdown the message queue for the technology agnostic message channel.
1986 * This has to occur before we pause shutdown pending ast_undestroyed_channels.
1987 *
1988 * XXX This is not reversed on shutdown cancel.
1989 */
1991
1992 if (niceness == SHUTDOWN_NORMAL) {
1993 /* Begin shutdown routine, hanging up active channels */
1995 if (ast_opt_console) {
1996 ast_verb(0, "Beginning asterisk %s....\n", restart ? "restart" : "shutdown");
1997 }
1999 waited |= wait_for_channels_to_die(niceness, SHUTDOWN_TIMEOUT);
2000 } else if (niceness >= SHUTDOWN_NICE) {
2001 if (niceness != SHUTDOWN_REALLY_NICE) {
2003 }
2004 if (ast_opt_console) {
2005 ast_verb(0, "Waiting for inactivity to perform %s...\n", restart ? "restart" : "halt");
2006 }
2007 waited |= wait_for_channels_to_die(niceness, -1);
2008 }
2009
2010 /* Re-acquire lock and check if someone changed the niceness, in which
2011 * case someone else has taken over the shutdown.
2012 */
2014 if (shuttingdown != niceness) {
2016 ast_verb(0, "Asterisk %s cancelled.\n", restart ? "restart" : "shutdown");
2017 }
2019 return 0;
2020 }
2021
2022 if (niceness >= SHUTDOWN_REALLY_NICE) {
2025
2026 /* No more Mr. Nice guy. We are committed to shutting down now. */
2030
2032 }
2035
2036 if (niceness >= SHUTDOWN_NORMAL && waited) {
2037 /*
2038 * We were not idle. Give things in progress a chance to
2039 * recognize the final shutdown phase.
2040 */
2041 sleep(1);
2042 }
2043 return 1;
2044}
2045
2046/*! Called when exiting is certain. */
2047static void really_quit(int num, shutdown_nice_t niceness, int restart)
2048{
2049 int active_channels;
2050 struct ast_json *json_object = NULL;
2051 int run_cleanups = niceness >= SHUTDOWN_NICE;
2052
2053 if (run_cleanups && modules_shutdown()) {
2054 ast_verb(0, "Some modules could not be unloaded, switching to fast shutdown\n");
2055 run_cleanups = 0;
2056 }
2057
2058 if (!restart && !ast_opt_remote) {
2059 ast_sd_notify("STOPPING=1");
2060 }
2063 if (consolethread == AST_PTHREADT_NULL || consolethread == pthread_self()) {
2064 /* Only end if we are the consolethread, otherwise there's a race with that thread. */
2065 if (el != NULL) {
2066 el_end(el);
2067 }
2068 if (el_hist != NULL) {
2069 history_end(el_hist);
2070 }
2071 } else if (!restart) {
2072 sig_flags.need_el_end = 1;
2073 pthread_kill(consolethread, SIGURG);
2074 }
2075 }
2076 active_channels = ast_active_channels();
2077 /* Don't publish messages if we're a remote console - we won't have all of the Stasis
2078 * topics or message types
2079 */
2080 if (!ast_opt_remote) {
2081 json_object = ast_json_pack("{s: s, s: s}",
2082 "Shutdown", active_channels ? "Uncleanly" : "Cleanly",
2083 "Restart", restart ? "True" : "False");
2084 ast_manager_publish_event("Shutdown", EVENT_FLAG_SYSTEM, json_object);
2085 ast_json_unref(json_object);
2086 json_object = NULL;
2087 }
2088 ast_verb(0, "Asterisk %s ending (%d).\n",
2089 active_channels ? "uncleanly" : "cleanly", num);
2090
2091 ast_verb(0, "Executing last minute cleanups\n");
2092 ast_run_atexits(run_cleanups);
2093
2094 ast_debug(1, "Asterisk ending (%d).\n", num);
2095 if (ast_socket > -1) {
2096 pthread_cancel(lthread);
2097 close(ast_socket);
2098 ast_socket = -1;
2099 if (!ast_socket_is_sd) {
2100 unlink(ast_config_AST_SOCKET);
2101 }
2102 pthread_kill(lthread, SIGURG);
2103 pthread_join(lthread, NULL);
2104 }
2105 if (ast_consock > -1)
2106 close(ast_consock);
2107 if (!ast_opt_remote)
2108 unlink(ast_config_AST_PID);
2110 printf("%s", term_quit());
2111 if (restart) {
2112 int i;
2113 ast_verb(0, "Preparing for Asterisk restart...\n");
2114 /* Mark all FD's for closing on exec */
2115 for (i = 3; i < 32768; i++) {
2116 fcntl(i, F_SETFD, FD_CLOEXEC);
2117 }
2118 ast_verb(0, "Asterisk is now restarting...\n");
2119 restartnow = 1;
2120
2121 /* close logger */
2122 close_logger();
2124
2125 /* If there is a consolethread running send it a SIGHUP
2126 so it can execvp, otherwise we can do it ourselves */
2127 if ((consolethread != AST_PTHREADT_NULL) && (consolethread != pthread_self())) {
2128 pthread_kill(consolethread, SIGHUP);
2129 /* Give the signal handler some time to complete */
2130 sleep(2);
2131 } else
2132 execvp(_argv[0], _argv);
2133
2134 } else {
2135 /* close logger */
2136 close_logger();
2138 }
2139
2140 exit(0);
2141}
2142
2143static void __quit_handler(int num)
2144{
2145 sig_flags.need_quit = 1;
2147 fprintf(stderr, "quit_handler: write() failed: %s\n", strerror(errno));
2148 }
2149 /* There is no need to restore the signal handler here, since the app
2150 * is going to exit */
2151}
2152
2153static void __remote_quit_handler(int num)
2154{
2155 sig_flags.need_quit = 1;
2156}
2157
2158static void set_header(char *outbuf, int maxout, char level)
2159{
2160 const char *cmp;
2161 char date[40];
2162
2163 switch (level) {
2164 case 0: cmp = NULL;
2165 break;
2166 case 1: cmp = VERBOSE_PREFIX_1;
2167 break;
2168 case 2: cmp = VERBOSE_PREFIX_2;
2169 break;
2170 case 3: cmp = VERBOSE_PREFIX_3;
2171 break;
2172 case 4: cmp = VERBOSE_PREFIX_4;
2173 break;
2174 case 5: cmp = VERBOSE_PREFIX_5;
2175 break;
2176 case 6: cmp = VERBOSE_PREFIX_6;
2177 break;
2178 case 7: cmp = VERBOSE_PREFIX_7;
2179 break;
2180 case 8: cmp = VERBOSE_PREFIX_8;
2181 break;
2182 case 9: cmp = VERBOSE_PREFIX_9;
2183 break;
2184 default: cmp = VERBOSE_PREFIX_10;
2185 break;
2186 }
2187
2188 if (ast_opt_timestamp) {
2189 struct ast_tm tm;
2190 struct timeval now = ast_tvnow();
2191 ast_localtime(&now, &tm, NULL);
2192 ast_strftime(date, sizeof(date), ast_logger_get_dateformat(), &tm);
2193 }
2194
2195 snprintf(outbuf, maxout, "%s%s%s%s%s%s",
2196 ast_opt_timestamp ? "[" : "",
2197 ast_opt_timestamp ? date : "",
2198 ast_opt_timestamp ? "] " : "",
2199 cmp ? ast_term_color(COLOR_GRAY, 0) : "",
2200 cmp ? cmp : "",
2201 cmp ? ast_term_reset() : "");
2202}
2203
2206};
2207
2208static int console_state_init(void *ptr)
2209{
2210 struct console_state_data *state = ptr;
2211 state->verbose_line_level = 0;
2212 return 0;
2213}
2214
2216
2217static int console_print(const char *s)
2218{
2219 struct console_state_data *state =
2221
2222 char prefix[80];
2223 const char *c;
2224 int num, res = 0;
2225 unsigned int newline;
2226
2227 do {
2228 if (VERBOSE_HASMAGIC(s)) {
2229
2230 /* always use the given line's level, otherwise
2231 we'll use the last line's level */
2232 state->verbose_line_level = VERBOSE_MAGIC2LEVEL(s);
2233
2234 /* move past magic */
2235 s++;
2236
2237 set_header(prefix, sizeof(prefix), state->verbose_line_level);
2238 } else {
2239 *prefix = '\0';
2240 }
2241 c = s;
2242
2243 /* for a given line separate on verbose magic, newline, and eol */
2244 if ((s = strchr(c, '\n'))) {
2245 ++s;
2246 newline = 1;
2247 } else {
2248 s = strchr(c, '\0');
2249 newline = 0;
2250 }
2251
2252 /* check if we should write this line after calculating begin/end
2253 so we process the case of a higher level line embedded within
2254 two lower level lines */
2255 if (state->verbose_line_level > option_verbose) {
2256 continue;
2257 }
2258
2259 if (!ast_strlen_zero(prefix)) {
2260 fputs(prefix, stdout);
2261 }
2262
2263 num = s - c;
2264 if (fwrite(c, sizeof(char), num, stdout) < num) {
2265 break;
2266 }
2267
2268 if (!res) {
2269 /* if at least some info has been written
2270 we'll want to return true */
2271 res = 1;
2272 }
2273 } while (*s);
2274
2275 if (newline) {
2276 /* if ending on a newline then reset last level to zero
2277 since what follows may be not be logging output */
2278 state->verbose_line_level = 0;
2279 }
2280
2281 if (res) {
2282 fflush(stdout);
2283 }
2284
2285 return res;
2286}
2287
2288static int ast_all_zeros(const char *s)
2289{
2290 while (*s) {
2291 if (*s > 32)
2292 return 0;
2293 s++;
2294 }
2295 return 1;
2296}
2297
2298/* This is the main console CLI command handler. Run by the main() thread. */
2299static void consolehandler(const char *s)
2300{
2301 printf("%s", term_end());
2302 fflush(stdout);
2303
2304 /* Called when readline data is available */
2305 if (!ast_all_zeros(s))
2307 /* The real handler for bang */
2308 if (s[0] == '!') {
2309 if (s[1])
2310 ast_safe_system(s+1);
2311 else
2312 ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
2313 } else
2314 ast_cli_command(STDOUT_FILENO, s);
2315}
2316
2317static int remoteconsolehandler(const char *s)
2318{
2319 int ret = 0;
2320
2321 /* Called when readline data is available */
2322 if (!ast_all_zeros(s))
2324
2325 while (isspace(*s)) {
2326 s++;
2327 }
2328
2329 /* The real handler for bang */
2330 if (s[0] == '!') {
2331 if (s[1])
2332 ast_safe_system(s+1);
2333 else
2334 ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
2335 ret = 1;
2336 } else if ((strncasecmp(s, "quit", 4) == 0 || strncasecmp(s, "exit", 4) == 0) &&
2337 (s[4] == '\0' || isspace(s[4]))) {
2339 ret = 1;
2340 }
2341
2342 return ret;
2343}
2344
2345static char *handle_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2346{
2347 switch (cmd) {
2348 case CLI_INIT:
2349 e->command = "core show version";
2350 e->usage =
2351 "Usage: core show version\n"
2352 " Shows Asterisk version information.\n";
2353 return NULL;
2354 case CLI_GENERATE:
2355 return NULL;
2356 }
2357
2358 if (a->argc != 3)
2359 return CLI_SHOWUSAGE;
2360 ast_cli(a->fd, "Asterisk %s built by %s @ %s on a %s running %s on %s\n",
2363 return CLI_SUCCESS;
2364}
2365
2366static char *handle_stop_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2367{
2368 switch (cmd) {
2369 case CLI_INIT:
2370 e->command = "core stop now";
2371 e->usage =
2372 "Usage: core stop now\n"
2373 " Shuts down a running Asterisk immediately, hanging up all active calls .\n";
2375 return NULL;
2376 case CLI_GENERATE:
2377 return NULL;
2378 }
2379
2380 if (a->argc != e->args)
2381 return CLI_SHOWUSAGE;
2382 quit_handler(0, SHUTDOWN_NORMAL, 0 /* not restart */);
2383 return CLI_SUCCESS;
2384}
2385
2386static char *handle_stop_gracefully(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2387{
2388 switch (cmd) {
2389 case CLI_INIT:
2390 e->command = "core stop gracefully";
2391 e->usage =
2392 "Usage: core stop gracefully\n"
2393 " Causes Asterisk to not accept new calls, and exit when all\n"
2394 " active calls have terminated normally.\n";
2396 return NULL;
2397 case CLI_GENERATE:
2398 return NULL;
2399 }
2400
2401 if (a->argc != e->args)
2402 return CLI_SHOWUSAGE;
2403 quit_handler(0, SHUTDOWN_NICE, 0 /* no restart */);
2404 return CLI_SUCCESS;
2405}
2406
2407static char *handle_stop_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2408{
2409 switch (cmd) {
2410 case CLI_INIT:
2411 e->command = "core stop when convenient";
2412 e->usage =
2413 "Usage: core stop when convenient\n"
2414 " Causes Asterisk to perform a shutdown when all active calls have ended.\n";
2416 return NULL;
2417 case CLI_GENERATE:
2418 return NULL;
2419 }
2420
2421 if (a->argc != e->args)
2422 return CLI_SHOWUSAGE;
2423 ast_cli(a->fd, "Waiting for inactivity to perform halt\n");
2424 quit_handler(0, SHUTDOWN_REALLY_NICE, 0 /* don't restart */);
2425 return CLI_SUCCESS;
2426}
2427
2428static char *handle_restart_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2429{
2430 switch (cmd) {
2431 case CLI_INIT:
2432 e->command = "core restart now";
2433 e->usage =
2434 "Usage: core restart now\n"
2435 " Causes Asterisk to hangup all calls and exec() itself performing a cold\n"
2436 " restart.\n";
2438 return NULL;
2439 case CLI_GENERATE:
2440 return NULL;
2441 }
2442
2443 if (a->argc != e->args)
2444 return CLI_SHOWUSAGE;
2445 quit_handler(0, SHUTDOWN_NORMAL, 1 /* restart */);
2446 return CLI_SUCCESS;
2447}
2448
2449static char *handle_restart_gracefully(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2450{
2451 switch (cmd) {
2452 case CLI_INIT:
2453 e->command = "core restart gracefully";
2454 e->usage =
2455 "Usage: core restart gracefully\n"
2456 " Causes Asterisk to stop accepting new calls and exec() itself performing a cold\n"
2457 " restart when all active calls have ended.\n";
2459 return NULL;
2460 case CLI_GENERATE:
2461 return NULL;
2462 }
2463
2464 if (a->argc != e->args)
2465 return CLI_SHOWUSAGE;
2466 quit_handler(0, SHUTDOWN_NICE, 1 /* restart */);
2467 return CLI_SUCCESS;
2468}
2469
2470static char *handle_restart_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2471{
2472 switch (cmd) {
2473 case CLI_INIT:
2474 e->command = "core restart when convenient";
2475 e->usage =
2476 "Usage: core restart when convenient\n"
2477 " Causes Asterisk to perform a cold restart when all active calls have ended.\n";
2479 return NULL;
2480 case CLI_GENERATE:
2481 return NULL;
2482 }
2483
2484 if (a->argc != e->args)
2485 return CLI_SHOWUSAGE;
2486 ast_cli(a->fd, "Waiting for inactivity to perform restart\n");
2487 quit_handler(0, SHUTDOWN_REALLY_NICE, 1 /* restart */);
2488 return CLI_SUCCESS;
2489}
2490
2491static char *handle_abort_shutdown(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2492{
2493 switch (cmd) {
2494 case CLI_INIT:
2495 e->command = "core abort shutdown";
2496 e->usage =
2497 "Usage: core abort shutdown\n"
2498 " Causes Asterisk to abort an executing shutdown or restart, and resume normal\n"
2499 " call operations.\n";
2501 return NULL;
2502 case CLI_GENERATE:
2503 return NULL;
2504 }
2505
2506 if (a->argc != e->args)
2507 return CLI_SHOWUSAGE;
2508
2510
2511 return CLI_SUCCESS;
2512}
2513
2514static char *handle_bang(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2515{
2516 switch (cmd) {
2517 case CLI_INIT:
2518 e->command = "!";
2519 e->usage =
2520 "Usage: !<command>\n"
2521 " Executes a given shell command\n";
2522 return NULL;
2523 case CLI_GENERATE:
2524 return NULL;
2525 }
2526
2527 return CLI_SUCCESS;
2528}
2529static const char warranty_lines[] = {
2530 "\n"
2531 " NO WARRANTY\n"
2532 "\n"
2533 "BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n"
2534 "FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN\n"
2535 "OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n"
2536 "PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n"
2537 "OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n"
2538 "MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS\n"
2539 "TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE\n"
2540 "PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n"
2541 "REPAIR OR CORRECTION.\n"
2542 "\n"
2543 "IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n"
2544 "WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n"
2545 "REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n"
2546 "INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n"
2547 "OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n"
2548 "TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n"
2549 "YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n"
2550 "PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n"
2551 "POSSIBILITY OF SUCH DAMAGES.\n"
2552};
2553
2554static char *show_warranty(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2555{
2556 switch (cmd) {
2557 case CLI_INIT:
2558 e->command = "core show warranty";
2559 e->usage =
2560 "Usage: core show warranty\n"
2561 " Shows the warranty (if any) for this copy of Asterisk.\n";
2562 return NULL;
2563 case CLI_GENERATE:
2564 return NULL;
2565 }
2566
2567 ast_cli(a->fd, "%s", warranty_lines);
2568
2569 return CLI_SUCCESS;
2570}
2571
2572static const char license_lines[] = {
2573 "\n"
2574 "This program is free software; you can redistribute it and/or modify\n"
2575 "it under the terms of the GNU General Public License version 2 as\n"
2576 "published by the Free Software Foundation.\n"
2577 "\n"
2578 "This program also contains components licensed under other licenses.\n"
2579 "They include:\n"
2580 "\n"
2581 "This program is distributed in the hope that it will be useful,\n"
2582 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
2583 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
2584 "GNU General Public License for more details.\n"
2585 "\n"
2586 "You should have received a copy of the GNU General Public License\n"
2587 "along with this program; if not, write to the Free Software\n"
2588 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
2589};
2590
2591static char *show_license(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2592{
2593 switch (cmd) {
2594 case CLI_INIT:
2595 e->command = "core show license";
2596 e->usage =
2597 "Usage: core show license\n"
2598 " Shows the license(s) for this copy of Asterisk.\n";
2599 return NULL;
2600 case CLI_GENERATE:
2601 return NULL;
2602 }
2603
2604 ast_cli(a->fd, "%s", license_lines);
2605
2606 return CLI_SUCCESS;
2607}
2608
2609#define ASTERISK_PROMPT "*CLI> "
2610
2611/*!
2612 * \brief Shutdown Asterisk CLI commands.
2613 *
2614 * \note These CLI commands cannot be unregistered at shutdown
2615 * because one of them is likely the reason for the shutdown.
2616 * The CLI generates a warning if a command is in-use when it is
2617 * unregistered.
2618 */
2620 AST_CLI_DEFINE(handle_stop_now, "Shut down Asterisk immediately"),
2621 AST_CLI_DEFINE(handle_stop_gracefully, "Gracefully shut down Asterisk"),
2622 AST_CLI_DEFINE(handle_stop_when_convenient, "Shut down Asterisk at empty call volume"),
2623 AST_CLI_DEFINE(handle_restart_now, "Restart Asterisk immediately"),
2624 AST_CLI_DEFINE(handle_restart_gracefully, "Restart Asterisk gracefully"),
2625 AST_CLI_DEFINE(handle_restart_when_convenient, "Restart Asterisk at empty call volume"),
2626};
2627
2628static struct ast_cli_entry cli_asterisk[] = {
2629 AST_CLI_DEFINE(handle_abort_shutdown, "Cancel a running shutdown"),
2630 AST_CLI_DEFINE(show_warranty, "Show the warranty (if any) for this copy of Asterisk"),
2631 AST_CLI_DEFINE(show_license, "Show the license(s) for this copy of Asterisk"),
2632 AST_CLI_DEFINE(handle_version, "Display version info"),
2633 AST_CLI_DEFINE(handle_bang, "Execute a shell command"),
2634#if !defined(LOW_MEMORY)
2635 AST_CLI_DEFINE(handle_show_threads, "Show running threads"),
2636#if defined(HAVE_SYSINFO) || defined(HAVE_SYSCTL)
2637 AST_CLI_DEFINE(handle_show_sysinfo, "Show System Information"),
2638#endif
2639 AST_CLI_DEFINE(handle_show_profile, "Display profiling info"),
2640 AST_CLI_DEFINE(handle_show_settings, "Show some core settings"),
2641 AST_CLI_DEFINE(handle_clear_profile, "Clear profiling info"),
2642#endif /* ! LOW_MEMORY */
2643};
2644
2646{
2647 char buf[80];
2648
2649 /*
2650 * Tell the server asterisk instance about the verbose level
2651 * initially desired.
2652 */
2653 if (option_verbose) {
2654 snprintf(buf, sizeof(buf), "core set verbose atleast %d silent", option_verbose);
2656 }
2657
2658 if (option_debug) {
2659 snprintf(buf, sizeof(buf), "core set debug atleast %d", option_debug);
2661 }
2662
2663 /* Leave verbose filtering to the server. */
2664 option_verbose = INT_MAX;
2665
2666 if (!ast_opt_mute) {
2667 fdsend(ast_consock, "logger mute silent");
2668 } else {
2669 printf("log and verbose output currently muted ('logger mute' to unmute)\n");
2670 }
2671}
2672
2673#ifdef HAVE_LIBEDIT_IS_UNICODE
2674#define CHAR_T_LIBEDIT wchar_t
2675#define CHAR_TO_LIBEDIT(c) btowc(c)
2676#else
2677#define CHAR_T_LIBEDIT char
2678#define CHAR_TO_LIBEDIT(c) c
2679#endif
2680
2681static int ast_el_read_char(EditLine *editline, CHAR_T_LIBEDIT *cp)
2682{
2683 int num_read = 0;
2684 int lastpos = 0;
2685 struct pollfd fds[2];
2686 int res;
2687 int max;
2688#define EL_BUF_SIZE 512
2689 char buf[EL_BUF_SIZE];
2690
2691 for (;;) {
2692 max = 1;
2693 fds[0].fd = ast_consock;
2694 fds[0].events = POLLIN;
2695 if (!ast_opt_exec) {
2696 fds[1].fd = STDIN_FILENO;
2697 fds[1].events = POLLIN;
2698 max++;
2699 }
2700 res = ast_poll(fds, max, -1);
2701 if (res < 0) {
2702 if (sig_flags.need_quit || sig_flags.need_quit_handler || sig_flags.need_el_end) {
2703 break;
2704 }
2705 if (errno == EINTR) {
2706 continue;
2707 }
2708 fprintf(stderr, "poll failed: %s\n", strerror(errno));
2709 break;
2710 }
2711
2712 if (!ast_opt_exec && fds[1].revents) {
2713 char c = '\0';
2714
2715 num_read = read(STDIN_FILENO, &c, 1);
2716 if (num_read < 1) {
2717 break;
2718 }
2719
2720 *cp = CHAR_TO_LIBEDIT(c);
2721
2722 return num_read;
2723 }
2724
2725 if (fds[0].revents) {
2726 res = read(ast_consock, buf, sizeof(buf) - 1);
2727 /* if the remote side disappears exit */
2728 if (res < 1) {
2729 fprintf(stderr, "\nDisconnected from Asterisk server\n");
2730 if (!ast_opt_reconnect) {
2732 } else {
2733 int tries;
2734 int reconnects_per_second = 20;
2735
2736 fprintf(stderr, "Attempting to reconnect for 30 seconds\n");
2737 for (tries = 0; tries < 30 * reconnects_per_second; tries++) {
2738 if (ast_tryconnect()) {
2739 fprintf(stderr, "Reconnect succeeded after %.3f seconds\n", 1.0 / reconnects_per_second * tries);
2740 printf("%s", term_quit());
2743 break;
2744 }
2745
2746 usleep(1000000 / reconnects_per_second);
2747 }
2748 if (tries >= 30 * reconnects_per_second) {
2749 fprintf(stderr, "Failed to reconnect for 30 seconds. Quitting.\n");
2751 }
2752 }
2753 continue;
2754 }
2755
2756 buf[res] = '\0';
2757
2758 /* Write over the CLI prompt */
2759 if (!ast_opt_exec && !lastpos) {
2760 if (write(STDOUT_FILENO, "\r␛[0K", 5) < 0) {
2761 }
2762 }
2763
2765
2766 if ((res < EL_BUF_SIZE - 1) && ((buf[res-1] == '\n') || (res >= 2 && buf[res-2] == '\n'))) {
2767 *cp = CHAR_TO_LIBEDIT(CC_REFRESH);
2768
2769 return 1;
2770 }
2771 lastpos = 1;
2772 }
2773 }
2774
2775 *cp = CHAR_TO_LIBEDIT('\0');
2776
2777 return 0;
2778}
2779
2780static struct ast_str *prompt = NULL;
2781
2782static char *cli_prompt(EditLine *editline)
2783{
2784 char tmp[100];
2785 char *pfmt;
2786 int color_used = 0;
2787 static int cli_prompt_changes = 0;
2788 struct passwd *pw;
2789 struct group *gr;
2790
2791 if (prompt == NULL) {
2792 prompt = ast_str_create(100);
2793 } else if (!cli_prompt_changes) {
2794 return ast_str_buffer(prompt);
2795 } else {
2797 }
2798
2799 if ((pfmt = getenv("ASTERISK_PROMPT"))) {
2800 char *t = pfmt;
2801 struct timeval ts = ast_tvnow();
2802 while (*t != '\0') {
2803 if (*t == '%') {
2804 char hostname[MAXHOSTNAMELEN] = "";
2805 int i, which;
2806 struct ast_tm tm = { 0, };
2807 int fgcolor = COLOR_WHITE, bgcolor = COLOR_BLACK;
2808
2809 t++;
2810 switch (*t) {
2811 case 'C': /* color */
2812 t++;
2813 if (sscanf(t, "%30d;%30d%n", &fgcolor, &bgcolor, &i) == 2) {
2814 ast_term_color_code(&prompt, fgcolor, bgcolor);
2815 t += i - 1;
2816 } else if (sscanf(t, "%30d%n", &fgcolor, &i) == 1) {
2817 ast_term_color_code(&prompt, fgcolor, 0);
2818 t += i - 1;
2819 }
2820
2821 /* If the color has been reset correctly, then there's no need to reset it later */
2822 color_used = ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) ? 0 : 1;
2823 break;
2824 case 'd': /* date */
2825 if (ast_localtime(&ts, &tm, NULL)) {
2826 ast_strftime(tmp, sizeof(tmp), "%Y-%m-%d", &tm);
2827 ast_str_append(&prompt, 0, "%s", tmp);
2828 cli_prompt_changes++;
2829 }
2830 break;
2831 case 'g': /* group */
2832 if ((gr = getgrgid(getgid()))) {
2833 ast_str_append(&prompt, 0, "%s", gr->gr_name);
2834 }
2835 break;
2836 case 'h': /* hostname */
2837 if (!gethostname(hostname, sizeof(hostname) - 1)) {
2838 ast_str_append(&prompt, 0, "%s", hostname);
2839 } else {
2840 ast_str_append(&prompt, 0, "%s", "localhost");
2841 }
2842 break;
2843 case 'H': /* short hostname */
2844 if (!gethostname(hostname, sizeof(hostname) - 1)) {
2845 char *dotptr;
2846 if ((dotptr = strchr(hostname, '.'))) {
2847 *dotptr = '\0';
2848 }
2849 ast_str_append(&prompt, 0, "%s", hostname);
2850 } else {
2851 ast_str_append(&prompt, 0, "%s", "localhost");
2852 }
2853 break;
2854#ifdef HAVE_GETLOADAVG
2855 case 'l': /* load avg */
2856 t++;
2857 if (sscanf(t, "%30d", &which) == 1 && which > 0 && which <= 3) {
2858 double list[3];
2859 getloadavg(list, 3);
2860 ast_str_append(&prompt, 0, "%.2f", list[which - 1]);
2861 cli_prompt_changes++;
2862 }
2863 break;
2864#endif
2865 case 's': /* Asterisk system name (from asterisk.conf) */
2867 break;
2868 case 't': /* time */
2869 if (ast_localtime(&ts, &tm, NULL)) {
2870 ast_strftime(tmp, sizeof(tmp), "%H:%M:%S", &tm);
2871 ast_str_append(&prompt, 0, "%s", tmp);
2872 cli_prompt_changes++;
2873 }
2874 break;
2875 case 'u': /* username */
2876 if ((pw = getpwuid(getuid()))) {
2877 ast_str_append(&prompt, 0, "%s", pw->pw_name);
2878 }
2879 break;
2880 case '#': /* process console or remote? */
2881 ast_str_append(&prompt, 0, "%c", ast_opt_remote ? '>' : '#');
2882 break;
2883 case '%': /* literal % */
2884 ast_str_append(&prompt, 0, "%c", '%');
2885 break;
2886 case '\0': /* % is last character - prevent bug */
2887 t--;
2888 break;
2889 }
2890 } else {
2891 ast_str_append(&prompt, 0, "%c", *t);
2892 }
2893 t++;
2894 }
2895 if (color_used) {
2896 /* Force colors back to normal at end */
2898 }
2899 } else {
2900 ast_str_set(&prompt, 0, "%s%s",
2903 }
2904
2905 return ast_str_buffer(prompt);
2906}
2907
2909{
2910 char *retstr;
2911 struct ast_vector_string *vec = ast_calloc(1, sizeof(*vec));
2912
2913 if (!vec) {
2914 return NULL;
2915 }
2916
2917 while ((retstr = strsep(&buf, " "))) {
2918 if (!strcmp(retstr, AST_CLI_COMPLETE_EOF)) {
2919 break;
2920 }
2921
2922 retstr = ast_strdup(retstr);
2923 if (!retstr || AST_VECTOR_APPEND(vec, retstr)) {
2924 ast_free(retstr);
2925 goto vector_cleanup;
2926 }
2927 }
2928
2929 if (!AST_VECTOR_SIZE(vec)) {
2930 goto vector_cleanup;
2931 }
2932
2933 return vec;
2934
2935vector_cleanup:
2938
2939 return NULL;
2940}
2941
2942static void ast_cli_display_match_list(struct ast_vector_string *matches, int max)
2943{
2944 int idx = 1;
2945 /* find out how many entries can be put on one line, with two spaces between strings */
2946 int limit = ast_get_termcols(STDOUT_FILENO) / (max + 2);
2947
2948 if (limit == 0) {
2949 limit = 1;
2950 }
2951
2952 for (;;) {
2953 int numoutputline;
2954
2955 for (numoutputline = 0; numoutputline < limit && idx < AST_VECTOR_SIZE(matches); idx++) {
2956 numoutputline++;
2957 fprintf(stdout, "%-*s ", max, AST_VECTOR_GET(matches, idx));
2958 }
2959
2960 if (!numoutputline) {
2961 break;
2962 }
2963
2964 fprintf(stdout, "\n");
2965 }
2966}
2967
2968
2969static char *cli_complete(EditLine *editline, int ch)
2970{
2971 int len = 0;
2972 char *ptr;
2973 struct ast_vector_string *matches;
2974 int retval = CC_ERROR;
2975 char savechr;
2976 int res;
2977
2978 LineInfo *lf = (LineInfo *)el_line(editline);
2979
2980 savechr = *(char *)lf->cursor;
2981 *(char *)lf->cursor = '\0';
2982 ptr = (char *)lf->cursor;
2983 if (ptr) {
2984 while (ptr > lf->buffer) {
2985 if (isspace(*ptr)) {
2986 ptr++;
2987 break;
2988 }
2989 ptr--;
2990 }
2991 }
2992
2993 len = lf->cursor - ptr;
2994
2995 if (ast_opt_remote) {
2996#define CMD_MATCHESARRAY "_COMMAND MATCHESARRAY \"%s\" \"%s\""
2997 char *mbuf;
2998 char *new_mbuf;
2999 int mlen = 0;
3000 int maxmbuf = ast_asprintf(&mbuf, CMD_MATCHESARRAY, lf->buffer, ptr);
3001
3002 if (maxmbuf == -1) {
3003 *((char *) lf->cursor) = savechr;
3004
3005 return (char *)(CC_ERROR);
3006 }
3007
3008 fdsend(ast_consock, mbuf);
3009 res = 0;
3010 mlen = 0;
3011 mbuf[0] = '\0';
3012
3013 while (!strstr(mbuf, AST_CLI_COMPLETE_EOF) && res != -1) {
3014 if (mlen + 1024 > maxmbuf) {
3015 /* Expand buffer to the next 1024 byte increment plus a NULL terminator. */
3016 maxmbuf = mlen + 1024;
3017 new_mbuf = ast_realloc(mbuf, maxmbuf + 1);
3018 if (!new_mbuf) {
3019 ast_free(mbuf);
3020 *((char *) lf->cursor) = savechr;
3021
3022 return (char *)(CC_ERROR);
3023 }
3024 mbuf = new_mbuf;
3025 }
3026 /* Only read 1024 bytes at a time */
3027 res = read(ast_consock, mbuf + mlen, 1024);
3028 if (res > 0) {
3029 if (!strncmp(mbuf, "Usage:", 6)) {
3030 /*
3031 * Abort on malformed tab completes
3032 * If help (tab complete) follows certain
3033 * special characters, the main Asterisk process
3034 * provides usage for the internal tab complete
3035 * helper command that the remote console processes
3036 * use.
3037 * If this happens, the AST_CLI_COMPLETE_EOF sentinel
3038 * value never gets sent. As a result, we'll just block
3039 * forever if we don't handle this case.
3040 * If we get command usage on a tab complete, then
3041 * we know this scenario just happened and we should
3042 * just silently ignore and do nothing.
3043 */
3044 break;
3045 }
3046 mlen += res;
3047 mbuf[mlen] = '\0';
3048 }
3049 }
3050 mbuf[mlen] = '\0';
3051
3052 matches = ast_el_strtoarr(mbuf);
3053 ast_free(mbuf);
3054 } else {
3055 matches = ast_cli_completion_vector((char *)lf->buffer, ptr);
3056 }
3057
3058 if (matches) {
3059 int i;
3060 int maxlen, match_len;
3061 const char *best_match = AST_VECTOR_GET(matches, 0);
3062
3063 if (!ast_strlen_zero(best_match)) {
3064 el_deletestr(editline, (int) len);
3065 el_insertstr(editline, best_match);
3066 retval = CC_REFRESH;
3067 }
3068
3069 if (AST_VECTOR_SIZE(matches) == 2) {
3070 /* Found an exact match */
3071 el_insertstr(editline, " ");
3072 retval = CC_REFRESH;
3073 } else {
3074 /* Must be more than one match */
3075 for (i = 1, maxlen = 0; i < AST_VECTOR_SIZE(matches); i++) {
3076 match_len = strlen(AST_VECTOR_GET(matches, i));
3077 if (match_len > maxlen) {
3078 maxlen = match_len;
3079 }
3080 }
3081
3082 fprintf(stdout, "\n");
3083 ast_cli_display_match_list(matches, maxlen);
3084 retval = CC_REDISPLAY;
3085 }
3087 AST_VECTOR_PTR_FREE(matches);
3088 }
3089
3090 *((char *) lf->cursor) = savechr;
3091
3092 return (char *)(long)retval;
3093}
3094
3095static int ast_el_initialize(void)
3096{
3097 HistEvent ev;
3098 char *editor, *editrc = getenv("EDITRC");
3099
3100 if (!(editor = getenv("AST_EDITMODE"))) {
3101 if (!(editor = getenv("AST_EDITOR"))) {
3102 editor = "emacs";
3103 }
3104 }
3105
3106 if (el != NULL)
3107 el_end(el);
3108 if (el_hist != NULL)
3109 history_end(el_hist);
3110
3111 el = el_init("asterisk", stdin, stdout, stderr);
3112 el_set(el, EL_PROMPT, cli_prompt);
3113
3114 el_set(el, EL_EDITMODE, 1);
3115 el_set(el, EL_EDITOR, editor);
3116 el_hist = history_init();
3117 if (!el || !el_hist)
3118 return -1;
3119
3120 /* setup history with 100 entries */
3121 history(el_hist, &ev, H_SETSIZE, 100);
3122
3123 el_set(el, EL_HIST, history, el_hist);
3124
3125 el_set(el, EL_ADDFN, "ed-complete", "Complete argument", cli_complete);
3126 /* Bind <tab> to command completion */
3127 el_set(el, EL_BIND, "^I", "ed-complete", NULL);
3128 /* Bind ? to command completion */
3129 el_set(el, EL_BIND, "?", "ed-complete", NULL);
3130 /* Bind ^D to redisplay */
3131 el_set(el, EL_BIND, "^D", "ed-redisplay", NULL);
3132 /* Bind Delete to delete char left */
3133 el_set(el, EL_BIND, "\\e[3~", "ed-delete-next-char", NULL);
3134 /* Bind Home and End to move to line start and end */
3135 el_set(el, EL_BIND, "\\e[1~", "ed-move-to-beg", NULL);
3136 el_set(el, EL_BIND, "\\e[4~", "ed-move-to-end", NULL);
3137 /* Bind C-left and C-right to move by word (not all terminals) */
3138 el_set(el, EL_BIND, "\\eOC", "vi-next-word", NULL);
3139 el_set(el, EL_BIND, "\\eOD", "vi-prev-word", NULL);
3140
3141 if (editrc) {
3142 el_source(el, editrc);
3143 }
3144
3145 return 0;
3146}
3147
3148#define MAX_HISTORY_COMMAND_LENGTH 256
3149
3150static int ast_el_add_history(const char *buf)
3151{
3152 HistEvent ev;
3153 char *stripped_buf;
3154
3155 if (el_hist == NULL || el == NULL) {
3157 }
3158 if (strlen(buf) > (MAX_HISTORY_COMMAND_LENGTH - 1)) {
3159 return 0;
3160 }
3161
3162 stripped_buf = ast_strip(ast_strdupa(buf));
3163
3164 /* HISTCONTROL=ignoredups */
3165 if (!history(el_hist, &ev, H_FIRST) && strcmp(ev.str, stripped_buf) == 0) {
3166 return 0;
3167 }
3168
3169 return history(el_hist, &ev, H_ENTER, stripped_buf);
3170}
3171
3172static int ast_el_write_history(const char *filename)
3173{
3174 HistEvent ev;
3175
3176 if (el_hist == NULL || el == NULL)
3178
3179 return (history(el_hist, &ev, H_SAVE, filename));
3180}
3181
3182static int ast_el_read_history(const char *filename)
3183{
3184 HistEvent ev;
3185
3186 if (el_hist == NULL || el == NULL) {
3188 }
3189
3190 if (access(filename, F_OK) == 0) {
3191 return history(el_hist, &ev, H_LOAD, filename);
3192 }
3193
3194 /* If the history file doesn't exist, failing to read it is unremarkable. */
3195 return 0;
3196}
3197
3198static void process_histfile(int (*readwrite)(const char *filename))
3199{
3200 struct passwd *pw = getpwuid(geteuid());
3201 int ret = 0;
3202 char *name = NULL;
3203
3204 if (!pw || ast_strlen_zero(pw->pw_dir)) {
3205 ast_log(LOG_ERROR, "Unable to determine home directory. History read/write disabled.\n");
3206 return;
3207 }
3208
3209 ret = ast_asprintf(&name, "%s/.asterisk_history", pw->pw_dir);
3210 if (ret <= 0) {
3211 ast_log(LOG_ERROR, "Unable to create history file name. History read/write disabled.\n");
3212 return;
3213 }
3214
3215 ret = readwrite(name);
3216 if (ret < 0) {
3217 ast_log(LOG_ERROR, "Unable to read or write history file '%s'\n", name);
3218 }
3219
3220 ast_free(name);
3221
3222 return;
3223}
3224
3226{
3228}
3229
3231{
3233}
3234
3235static void ast_remotecontrol(char *data)
3236{
3237 char buf[256] = "";
3238 int res;
3239 char *hostname;
3240 char *cpid;
3241 char *version;
3242 int pid;
3243 char *stringp = NULL;
3244
3245 char *ebuf;
3246 int num = 0;
3247
3248 ast_term_init();
3249 printf("%s", term_end());
3250 fflush(stdout);
3251
3252 memset(&sig_flags, 0, sizeof(sig_flags));
3253 signal(SIGINT, __remote_quit_handler);
3254 signal(SIGTERM, __remote_quit_handler);
3255 signal(SIGHUP, __remote_quit_handler);
3256
3257 if (read(ast_consock, buf, sizeof(buf) - 1) < 0) {
3258 ast_log(LOG_ERROR, "read() failed: %s\n", strerror(errno));
3259 return;
3260 }
3261 if (data) {
3262 char prefix[] = "cli quit after ";
3263 char *tmp = ast_alloca(strlen(data) + strlen(prefix) + 1);
3264 sprintf(tmp, "%s%s", prefix, data);
3265 if (write(ast_consock, tmp, strlen(tmp) + 1) < 0) {
3266 ast_log(LOG_ERROR, "write() failed: %s\n", strerror(errno));
3267 if (sig_flags.need_quit || sig_flags.need_quit_handler || sig_flags.need_el_end) {
3268 return;
3269 }
3270 }
3271 }
3272 stringp = buf;
3273 hostname = strsep(&stringp, "/");
3274 cpid = strsep(&stringp, "/");
3275 version = strsep(&stringp, "\n");
3276 if (!version)
3277 version = "<Version Unknown>";
3278 stringp = hostname;
3279 strsep(&stringp, ".");
3280 if (cpid)
3281 pid = atoi(cpid);
3282 else
3283 pid = -1;
3284 if (!data) {
3286 }
3287
3288 if (ast_opt_exec && data) { /* hack to print output then exit if asterisk -rx is used */
3289 int linefull = 1, prev_linefull = 1, prev_line_verbose = 0;
3290 struct pollfd fds;
3291 fds.fd = ast_consock;
3292 fds.events = POLLIN;
3293 fds.revents = 0;
3294
3295 while (ast_poll(&fds, 1, 60000) > 0) {
3296 char buffer[512] = "", *curline = buffer, *nextline;
3297 int not_written = 1;
3298
3299 if (sig_flags.need_quit || sig_flags.need_quit_handler || sig_flags.need_el_end) {
3300 break;
3301 }
3302
3303 if (read(ast_consock, buffer, sizeof(buffer) - 1) <= 0) {
3304 break;
3305 }
3306
3307 do {
3308 prev_linefull = linefull;
3309 if ((nextline = strchr(curline, '\n'))) {
3310 linefull = 1;
3311 nextline++;
3312 } else {
3313 linefull = 0;
3314 nextline = strchr(curline, '\0');
3315 }
3316
3317 /* Skip verbose lines */
3318 /* Prev line full? | Line is verbose | Last line verbose? | Print
3319 * TRUE | TRUE* | TRUE | FALSE
3320 * TRUE | TRUE* | FALSE | FALSE
3321 * TRUE | FALSE* | TRUE | TRUE
3322 * TRUE | FALSE* | FALSE | TRUE
3323 * FALSE | TRUE | TRUE* | FALSE
3324 * FALSE | TRUE | FALSE* | TRUE
3325 * FALSE | FALSE | TRUE* | FALSE
3326 * FALSE | FALSE | FALSE* | TRUE
3327 */
3328 if ((!prev_linefull && !prev_line_verbose) || (prev_linefull && *curline > 0)) {
3329 prev_line_verbose = 0;
3330 not_written = 0;
3331 if (write(STDOUT_FILENO, curline, nextline - curline) < 0) {
3332 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3333 }
3334 } else {
3335 prev_line_verbose = 1;
3336 }
3337 curline = nextline;
3338 } while (!ast_strlen_zero(curline));
3339
3340 /* No non-verbose output in 60 seconds. */
3341 if (not_written) {
3342 break;
3343 }
3344 }
3345 return;
3346 }
3347
3348 ast_verbose("Connected to Asterisk %s currently running on %s (pid = %d)\n", version, hostname, pid);
3350
3352 if (el_hist == NULL || el == NULL)
3355
3356 el_set(el, EL_GETCFN, ast_el_read_char);
3357
3358 for (;;) {
3359 ebuf = (char *)el_gets(el, &num);
3360
3361 if (sig_flags.need_quit || sig_flags.need_quit_handler || sig_flags.need_el_end) {
3362 break;
3363 }
3364
3365 if (!ebuf && write(1, "", 1) < 0)
3366 break;
3367
3368 if (!ast_strlen_zero(ebuf)) {
3369 if (ebuf[strlen(ebuf)-1] == '\n')
3370 ebuf[strlen(ebuf)-1] = '\0';
3371 if (!remoteconsolehandler(ebuf)) {
3372 res = write(ast_consock, ebuf, strlen(ebuf) + 1);
3373 if (res < 1) {
3374 ast_log(LOG_WARNING, "Unable to write: %s\n", strerror(errno));
3375 break;
3376 }
3377 }
3378 }
3379 }
3380 printf("\nDisconnected from Asterisk server\n");
3381}
3382
3383static int show_version(void)
3384{
3385 printf("Asterisk %s\n", ast_get_version());
3386 return 0;
3387}
3388
3389static int show_cli_help(void)
3390{
3391 printf("Asterisk %s, " COPYRIGHT_TAG "\n", ast_get_version());
3392 printf("Usage: asterisk [OPTIONS]\n");
3393 printf("Valid Options:\n");
3394 printf(" -V Display version number and exit\n");
3395 printf(" -C <configfile> Use an alternate configuration file\n");
3396 printf(" -G <group> Run as a group other than the caller\n");
3397 printf(" -U <user> Run as a user other than the caller\n");
3398 printf(" -c Provide console CLI\n");
3399 printf(" -d Increase debugging (multiple d's = more debugging)\n");
3400#if HAVE_WORKING_FORK
3401 printf(" -f Do not fork\n");
3402 printf(" -F Always fork\n");
3403#endif
3404 printf(" -g Dump core in case of a crash\n");
3405 printf(" -h This help screen\n");
3406 printf(" -i Initialize crypto keys at startup\n");
3407 printf(" -L <load> Limit the maximum load average before rejecting new calls\n");
3408 printf(" -M <value> Limit the maximum number of calls to the specified value\n");
3409 printf(" -m Mute debugging and console output on the console\n");
3410 printf(" -n Disable console colorization. Can be used only at startup.\n");
3411 printf(" -p Run as pseudo-realtime thread\n");
3412 printf(" -q Quiet mode (suppress output)\n");
3413 printf(" -r Connect to Asterisk on this machine\n");
3414 printf(" -R Same as -r, except attempt to reconnect if disconnected\n");
3415 printf(" -s <socket> Connect to Asterisk via socket <socket> (only valid with -r)\n");
3416 printf(" -t Record soundfiles in /var/tmp and move them where they\n");
3417 printf(" belong after they are done\n");
3418 printf(" -T Display the time in [Mmm dd hh:mm:ss] format for each line\n");
3419 printf(" of output to the CLI. Cannot be used with remote console mode.\n\n");
3420 printf(" -v Increase verbosity (multiple v's = more verbose)\n");
3421 printf(" -x <cmd> Execute command <cmd> (implies -r)\n");
3422 printf(" -X Enable use of #exec in asterisk.conf\n");
3423 printf(" -W Adjust terminal colors to compensate for a light background\n");
3424 printf("\n");
3425 return 0;
3426}
3427
3429{
3430 struct ast_config *cfg;
3431 struct ast_variable *v;
3432 struct ast_flags config_flags = { CONFIG_FLAG_NOCACHE | CONFIG_FLAG_NOREALTIME };
3433
3436
3437 cfg = ast_config_load2("pjproject.conf", "" /* core, can't reload */, config_flags);
3438 if (!cfg
3440 || cfg == CONFIG_STATUS_FILEINVALID) {
3441 /* We'll have to use defaults */
3442 return;
3443 }
3444
3445 for (v = ast_variable_browse(cfg, "startup"); v; v = v->next) {
3446 if (!strcasecmp(v->name, "log_level")) {
3447 if (sscanf(v->value, "%30d", &ast_option_pjproject_log_level) != 1) {
3449 } else if (ast_option_pjproject_log_level < 0) {
3453 }
3454 } else if (!strcasecmp(v->name, "cache_pools")) {
3456 }
3457 }
3458
3459 ast_config_destroy(cfg);
3460}
3461
3462static void *monitor_sig_flags(void *unused)
3463{
3464 for (;;) {
3465 struct pollfd p = { sig_alert_pipe[0], POLLIN, 0 };
3466
3467 ast_poll(&p, 1, -1);
3468 if (sig_flags.need_reload) {
3469 sig_flags.need_reload = 0;
3471 }
3472 if (sig_flags.need_quit) {
3473 sig_flags.need_quit = 0;
3474 if ((consolethread != AST_PTHREADT_NULL) && (consolethread != pthread_self())) {
3475 sig_flags.need_quit_handler = 1;
3476 pthread_kill(consolethread, SIGURG);
3477 } else {
3479 }
3480 }
3482 }
3483
3484 return NULL;
3485}
3486
3487static void *canary_thread(void *unused)
3488{
3489 struct stat canary_stat;
3490 struct timeval now;
3491
3492 /* Give the canary time to sing */
3493 sleep(120);
3494
3495 for (;;) {
3496 now = ast_tvnow();
3497 if (stat(canary_filename, &canary_stat) || now.tv_sec > canary_stat.st_mtime + 60) {
3499 "The canary is no more. He has ceased to be! "
3500 "He's expired and gone to meet his maker! "
3501 "He's a stiff! Bereft of life, he rests in peace. "
3502 "His metabolic processes are now history! He's off the twig! "
3503 "He's kicked the bucket. He's shuffled off his mortal coil, "
3504 "run down the curtain, and joined the bleeding choir invisible!! "
3505 "THIS is an EX-CANARY. (Reducing priority)\n");
3507 pthread_exit(NULL);
3508 }
3509
3510 /* Check the canary once a minute */
3511 sleep(60);
3512 }
3513}
3514
3515/* Used by libc's atexit(3) function */
3516static void canary_exit(void)
3517{
3518 if (canary_pid > 0) {
3519 int status;
3520 kill(canary_pid, SIGKILL);
3521 waitpid(canary_pid, &status, 0);
3522 }
3523}
3524
3525/* Execute CLI commands on startup. Run by main() thread. */
3526static void run_startup_commands(void)
3527{
3528 int fd;
3529 struct ast_config *cfg;
3530 struct ast_flags cfg_flags = { 0 };
3531 struct ast_variable *v;
3532
3533 if (!(cfg = ast_config_load2("cli.conf", "" /* core, can't reload */, cfg_flags)))
3534 return;
3536 return;
3537 }
3538
3539 fd = open("/dev/null", O_RDWR);
3540 if (fd < 0) {
3541 ast_config_destroy(cfg);
3542 return;
3543 }
3544
3545 for (v = ast_variable_browse(cfg, "startup_commands"); v; v = v->next) {
3546 if (ast_true(v->value))
3547 ast_cli_command(fd, v->name);
3548 }
3549
3550 close(fd);
3551 ast_config_destroy(cfg);
3552}
3553
3554static void env_init(void)
3555{
3556 setenv("AST_SYSTEMNAME", ast_config_AST_SYSTEM_NAME, 1);
3557 setenv("AST_BUILD_HOST", ast_build_hostname, 1);
3558 setenv("AST_BUILD_DATE", ast_build_date, 1);
3559 setenv("AST_BUILD_KERNEL", ast_build_kernel, 1);
3560 setenv("AST_BUILD_MACHINE", ast_build_machine, 1);
3561 setenv("AST_BUILD_OS", ast_build_os, 1);
3562 setenv("AST_BUILD_USER", ast_build_user, 1);
3563 setenv("AST_VERSION", ast_get_version(), 1);
3564}
3565
3566static void print_intro_message(const char *runuser, const char *rungroup)
3567{
3570 if (runuser) {
3571 ast_verbose("Running as user '%s'\n", runuser);
3572 }
3573 if (rungroup) {
3574 ast_verbose("Running under group '%s'\n", rungroup);
3575 }
3576 }
3577}
3578
3579static void main_atexit(void)
3580{
3582}
3583
3584int main(int argc, char *argv[])
3585{
3586 int c;
3587 int x;
3588 int isroot = 1, rundir_exists = 0;
3589 RAII_VAR(char *, runuser, NULL, ast_free);
3590 RAII_VAR(char *, rungroup, NULL, ast_free);
3591 RAII_VAR(char *, xarg, NULL, ast_free);
3592 struct rlimit l;
3593 static const char *getopt_settings = "BC:cde:FfG:ghIiL:M:mnpqRrs:TtU:VvWXx:";
3594
3595 /* Remember original args for restart */
3596 if (argc > ARRAY_LEN(_argv) - 1) {
3597 fprintf(stderr, "Truncating argument size to %d\n", (int)ARRAY_LEN(_argv) - 1);
3598 argc = ARRAY_LEN(_argv) - 1;
3599 }
3600 for (x = 0; x < argc; x++)
3601 _argv[x] = argv[x];
3602 _argv[x] = NULL;
3603
3604 if (geteuid() != 0)
3605 isroot = 0;
3606
3607 /* if the progname is rasterisk consider it a remote console */
3608 if (argv[0] && (strstr(argv[0], "rasterisk")) != NULL) {
3610 }
3611 ast_mainpid = getpid();
3612
3613 /* Process command-line options that affect asterisk.conf load. */
3614 while ((c = getopt(argc, argv, getopt_settings)) != -1) {
3615 switch (c) {
3616 case 'X':
3618 break;
3619 case 'C':
3620 set_asterisk_conf_path(optarg);
3621 break;
3622 case 'd':
3623 option_debug++;
3624 break;
3625 case 'h':
3626 show_cli_help();
3627 exit(0);
3628 case 'R':
3629 case 'r':
3630 case 'x':
3631 /* ast_opt_remote is checked during config load. This is only part of what
3632 * these options do, see the second loop for the rest of the actions. */
3634 break;
3635 case 'V':
3636 show_version();
3637 exit(0);
3638 case 'v':
3640 break;
3641 case '?':
3642 exit(1);
3643 }
3644 }
3645
3646 /* Initialize env so it is available if #exec is used in asterisk.conf. */
3647 env_init();
3648
3650
3651 /* Update env to include any systemname that was set. */
3652 env_init();
3653
3654 /*! \brief Check for options
3655 *
3656 * \todo Document these options
3657 */
3658 optind = 1;
3659 while ((c = getopt(argc, argv, getopt_settings)) != -1) {
3660 /*!\note Please keep the ordering here to alphabetical, capital letters
3661 * first. This will make it easier in the future to select unused
3662 * option flags for new features. */
3663 switch (c) {
3664 case 'B': /* Force black background */
3667 break;
3668 case 'X':
3669 /* The command-line -X option enables #exec for asterisk.conf only. */
3670 break;
3671 case 'C':
3672 /* already processed. */
3673 break;
3674 case 'c':
3676 break;
3677 case 'd':
3678 /* already processed. */
3679 break;
3680#if defined(HAVE_SYSINFO)
3681 case 'e':
3682 if ((sscanf(&optarg[1], "%30ld", &option_minmemfree) != 1) || (option_minmemfree < 0)) {
3684 }
3685 break;
3686#endif
3687#if HAVE_WORKING_FORK
3688 case 'F':
3690 break;
3691 case 'f':
3693 break;
3694#endif
3695 case 'G':
3696 rungroup = ast_strdup(optarg);
3697 break;
3698 case 'g':
3700 break;
3701 case 'h':
3702 /* already processed. */
3703 break;
3704 case 'I':
3705 fprintf(stderr,
3706 "NOTICE: The -I option is no longer needed.\n"
3707 " It will always be enabled if you have a timing module loaded.\n");
3708 break;
3709 case 'i':
3711 break;
3712 case 'L':
3713 if ((sscanf(optarg, "%30lf", &ast_option_maxload) != 1) || (ast_option_maxload < 0.0)) {
3714 ast_option_maxload = 0.0;
3715 }
3716 break;
3717 case 'M':
3718 if ((sscanf(optarg, "%30d", &ast_option_maxcalls) != 1) || (ast_option_maxcalls < 0)) {
3720 }
3721 break;
3722 case 'm':
3724 break;
3725 case 'n':
3727 break;
3728 case 'p':
3730 break;
3731 case 'q':
3733 break;
3734 case 'R':
3736 break;
3737 case 'r':
3739 break;
3740 case 's':
3741 if (ast_opt_remote) {
3742 set_socket_path(optarg);
3743 }
3744 break;
3745 case 'T':
3747 break;
3748 case 't':
3750 break;
3751 case 'U':
3752 runuser = ast_strdup(optarg);
3753 break;
3754 case 'V':
3755 case 'v':
3756 /* already processed. */
3757 break;
3758 case 'W': /* White background */
3761 break;
3762 case 'x':
3763 /* -r is implied by -x so set the flags -r sets as well. */
3765
3767 xarg = ast_strdup(optarg);
3768 break;
3769 case '?':
3770 /* already processed. */
3771 break;
3772 }
3773 }
3774
3775 if (ast_opt_remote) {
3776 int didwarn = 0;
3777 optind = 1;
3778
3779 /* Not all options can be used with remote console. Warn if they're used. */
3780 while ((c = getopt(argc, argv, getopt_settings)) != -1) {
3781 switch (c) {
3782 /* okay to run with remote console */
3783 case 'B': /* force black background */
3784 case 'C': /* set config path */
3785 case 'd': /* debug */
3786 case 'h': /* help */
3787 case 'I': /* obsolete timing option: warning already thrown if used */
3788 case 'L': /* max load */
3789 case 'M': /* max calls */
3790 case 'm': /* mute */
3791 /*! \note The q option is never used anywhere, only defined */
3792 case 'q': /* quiet */
3793 case 'R': /* reconnect */
3794 case 'r': /* remote */
3795 /*! \note Can ONLY be used with remote console */
3796 case 's': /* set socket path */
3797 case 'T': /* timestamp */
3798 case 'V': /* version */
3799 case 'v': /* verbose */
3800 case 'W': /* white background */
3801 case 'x': /* remote execute */
3802 case '?': /* ? */
3803 break;
3804 /* can only be run when Asterisk is starting */
3805 case 'X': /* enables #exec for asterisk.conf only. */
3806 case 'c': /* foreground console */
3807 case 'e': /* minimum memory free */
3808 case 'F': /* always fork */
3809 case 'f': /* no fork */
3810 case 'G': /* run group */
3811 case 'g': /* dump core */
3812 case 'i': /* init keys */
3813 case 'n': /* no color */
3814 case 'p': /* high priority */
3815 case 't': /* cache record files */
3816 case 'U': /* run user */
3817 fprintf(stderr, "'%c' option is not compatible with remote console mode and has no effect.\n", c);
3818 didwarn = 1;
3819 }
3820 }
3821 if (didwarn) {
3822 fprintf(stderr, "\n"); /* if any warnings print out, make them stand out */
3823 }
3824 }
3825
3826 /* For remote connections, change the name of the remote connection.
3827 * We do this for the benefit of init scripts (which need to know if/when
3828 * the main asterisk process has died yet). */
3829 if (ast_opt_remote) {
3830 strcpy(argv[0], "rasterisk");
3831 for (x = 1; x < argc; x++) {
3832 argv[x] = argv[0] + 10;
3833 }
3834 }
3835
3837 fprintf(stderr, "The 'languageprefix' option in asterisk.conf is deprecated; in a future release it will be removed, and your sound files will need to be organized in the 'new style' language layout.\n");
3838 }
3839
3841 fprintf(stderr, "'alwaysfork' is not compatible with console or remote console mode; ignored\n");
3843 }
3844
3845 if (ast_opt_dump_core) {
3846 memset(&l, 0, sizeof(l));
3847 l.rlim_cur = RLIM_INFINITY;
3848 l.rlim_max = RLIM_INFINITY;
3849 if (setrlimit(RLIMIT_CORE, &l)) {
3850 fprintf(stderr, "Unable to disable core size resource limit: %s\n", strerror(errno));
3851 }
3852 }
3853
3854 if (getrlimit(RLIMIT_NOFILE, &l)) {
3855 fprintf(stderr, "Unable to check file descriptor limit: %s\n", strerror(errno));
3856 }
3857
3858#if !defined(CONFIGURE_RAN_AS_ROOT)
3859 /* Check if select(2) will run with more file descriptors */
3860 do {
3861 int fd, fd2;
3862 ast_fdset readers;
3863 struct timeval tv = { 0, };
3864
3865 if (l.rlim_cur <= FD_SETSIZE) {
3866 /* The limit of select()able FDs is irrelevant, because we'll never
3867 * open one that high. */
3868 break;
3869 }
3870
3871 if (!(fd = open("/dev/null", O_RDONLY))) {
3872 fprintf(stderr, "Cannot open a file descriptor at boot? %s\n", strerror(errno));
3873 break; /* XXX Should we exit() here? XXX */
3874 }
3875
3876 fd2 = ((l.rlim_cur > sizeof(readers) * 8) ? sizeof(readers) * 8 : l.rlim_cur) - 1;
3877 if (dup2(fd, fd2) < 0) {
3878 fprintf(stderr, "Cannot open maximum file descriptor %d at boot? %s\n", fd2, strerror(errno));
3879 close(fd);
3880 break;
3881 }
3882
3883 FD_ZERO(&readers);
3884 FD_SET(fd2, &readers);
3885 if (ast_select(fd2 + 1, &readers, NULL, NULL, &tv) < 0) {
3886 fprintf(stderr, "Maximum select()able file descriptor is %d\n", FD_SETSIZE);
3887 }
3888 ast_FD_SETSIZE = l.rlim_cur > ast_FDMAX ? ast_FDMAX : l.rlim_cur;
3889 close(fd);
3890 close(fd2);
3891 } while (0);
3892#elif defined(HAVE_VARIABLE_FDSET)
3893 ast_FD_SETSIZE = l.rlim_cur > ast_FDMAX ? ast_FDMAX : l.rlim_cur;
3894#endif /* !defined(CONFIGURE_RAN_AS_ROOT) */
3895
3896 if ((!rungroup) && !ast_strlen_zero(ast_config_AST_RUN_GROUP))
3898 if ((!runuser) && !ast_strlen_zero(ast_config_AST_RUN_USER))
3900
3901 /* Must install this signal handler up here to ensure that if the canary
3902 * fails to execute that it doesn't kill the Asterisk process.
3903 */
3904 sigaction(SIGCHLD, &child_handler, NULL);
3905
3906 /* It's common on some platforms to clear /var/run at boot. Create the
3907 * socket file directory before we drop privileges. */
3908 if (mkdir(ast_config_AST_RUN_DIR, 0755)) {
3909 if (errno == EEXIST) {
3910 rundir_exists = 1;
3911 } else {
3912 fprintf(stderr, "Unable to create socket file directory. Remote consoles will not be able to connect! (%s)\n", strerror(x));
3913 }
3914 }
3915
3916#ifndef __CYGWIN__
3917
3918 if (isroot) {
3920 }
3921
3922 if (isroot && rungroup) {
3923 struct group *gr;
3924 gr = getgrnam(rungroup);
3925 if (!gr) {
3926 fprintf(stderr, "No such group '%s'!\n", rungroup);
3927 exit(1);
3928 }
3929 if (!rundir_exists && chown(ast_config_AST_RUN_DIR, -1, gr->gr_gid)) {
3930 fprintf(stderr, "Unable to chgrp run directory to %d (%s)\n", (int) gr->gr_gid, rungroup);
3931 }
3932 if (setgid(gr->gr_gid)) {
3933 fprintf(stderr, "Unable to setgid to %d (%s)\n", (int)gr->gr_gid, rungroup);
3934 exit(1);
3935 }
3936 if (setgroups(0, NULL)) {
3937 fprintf(stderr, "Unable to drop unneeded groups\n");
3938 exit(1);
3939 }
3940 }
3941
3942 if (runuser && !ast_test_flag(&ast_options, AST_OPT_FLAG_REMOTE)) {
3943#ifdef HAVE_CAP
3944 int has_cap = 1;
3945#endif /* HAVE_CAP */
3946 struct passwd *pw;
3947 pw = getpwnam(runuser);
3948 if (!pw) {
3949 fprintf(stderr, "No such user '%s'!\n", runuser);
3950 exit(1);
3951 }
3952 if (chown(ast_config_AST_RUN_DIR, pw->pw_uid, -1)) {
3953 fprintf(stderr, "Unable to chown run directory to %d (%s)\n", (int) pw->pw_uid, runuser);
3954 }
3955#ifdef HAVE_CAP
3956 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0)) {
3957 ast_log(LOG_WARNING, "Unable to keep capabilities.\n");
3958 has_cap = 0;
3959 }
3960#endif /* HAVE_CAP */
3961 if (!isroot && pw->pw_uid != geteuid()) {
3962 fprintf(stderr, "Asterisk started as nonroot, but runuser '%s' requested.\n", runuser);
3963 exit(1);
3964 }
3965 if (!rungroup) {
3966 if (setgid(pw->pw_gid)) {
3967 fprintf(stderr, "Unable to setgid to %d!\n", (int)pw->pw_gid);
3968 exit(1);
3969 }
3970 if (isroot && initgroups(pw->pw_name, pw->pw_gid)) {
3971 fprintf(stderr, "Unable to init groups for '%s'\n", runuser);
3972 exit(1);
3973 }
3974 }
3975 if (setuid(pw->pw_uid)) {
3976 fprintf(stderr, "Unable to setuid to %d (%s)\n", (int)pw->pw_uid, runuser);
3977 exit(1);
3978 }
3979#ifdef HAVE_CAP
3980 if (has_cap) {
3981 cap_t cap;
3982
3983 cap = cap_from_text("cap_net_admin=eip");
3984
3985 if (cap_set_proc(cap)) {
3986 fprintf(stderr, "Unable to install capabilities.\n");
3987 }
3988 if (cap_free(cap)) {
3989 fprintf(stderr, "Unable to drop capabilities.\n");
3990 }
3991 }
3992#endif /* HAVE_CAP */
3993 }
3994
3995#endif /* __CYGWIN__ */
3996
3997#ifdef linux
3998 if (geteuid() && ast_opt_dump_core) {
3999 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) < 0) {
4000 fprintf(stderr, "Unable to set the process for core dumps after changing to a non-root user. %s\n", strerror(errno));
4001 }
4002 }
4003#endif
4004
4005 {
4006#if defined(HAVE_EACCESS) || defined(HAVE_EUIDACCESS)
4007 char dir[PATH_MAX];
4008 if (!getcwd(dir, sizeof(dir)) || eaccess(dir, R_OK | X_OK | F_OK)) {
4009 fprintf(stderr, "Unable to access the running directory (%s). Changing to '/' for compatibility.\n", strerror(errno));
4010 /* If we cannot access the CWD, then we couldn't dump core anyway,
4011 * so chdir("/") won't break anything. */
4012 if (chdir("/")) {
4013 /* chdir(/) should never fail, so this ends up being a no-op */
4014 fprintf(stderr, "chdir(\"/\") failed?!! %s\n", strerror(errno));
4015 }
4016 } else
4017#endif /* defined(HAVE_EACCESS) || defined(HAVE_EUIDACCESS) */
4019 /* Backgrounding, but no cores, so chdir won't break anything. */
4020 if (chdir("/")) {
4021 fprintf(stderr, "Unable to chdir(\"/\") ?!! %s\n", strerror(errno));
4022 }
4023 }
4024 }
4025
4026 /* Initial value of the maximum active system verbosity level. */
4028
4029 if (ast_sd_get_fd_un(SOCK_STREAM, ast_config_AST_SOCKET) > 0) {
4030 ast_socket_is_sd = 1;
4031 }
4032
4033 /* DO NOT perform check for existing daemon if systemd has CLI socket activation */
4034 if (!ast_socket_is_sd && ast_tryconnect()) {
4035 /* One is already running */
4036 if (ast_opt_remote) {
4038 if (ast_opt_exec) {
4039 ast_remotecontrol(xarg);
4041 exit(0);
4042 }
4043 ast_term_init();
4044 printf("%s", term_end());
4045 fflush(stdout);
4046
4047 print_intro_message(runuser, rungroup);
4048 printf("%s", term_quit());
4051 exit(0);
4052 } else {
4053 fprintf(stderr, "Asterisk already running on %s. Use 'asterisk -r' to connect.\n", ast_config_AST_SOCKET);
4054 printf("%s", term_quit());
4055 exit(1);
4056 }
4057 } else if (ast_opt_remote || ast_opt_exec) {
4058 fprintf(stderr, "Unable to connect to remote asterisk (does %s exist?)\n", ast_config_AST_SOCKET);
4059 printf("%s", term_quit());
4060 exit(1);
4061 }
4062
4063#ifdef HAVE_CAP
4064 child_cap = cap_from_text("cap_net_admin-eip");
4065#endif
4066 /* Not a remote console? Start the daemon. */
4067 asterisk_daemon(isroot, runuser, rungroup);
4068#ifdef HAS_CAP
4069 cap_free(child_cap);
4070#endif
4071 return 0;
4072}
4073
4074static inline void check_init(int init_result, const char *name)
4075{
4076 if (init_result) {
4078 ast_log(LOG_ERROR, "%s initialization failed. ASTERISK EXITING!\n%s", name, term_quit());
4079 } else {
4080 fprintf(stderr, "%s initialization failed. ASTERISK EXITING!\n%s", name, term_quit());
4081 }
4082 ast_run_atexits(0);
4083 exit(init_result == -2 ? 2 : 1);
4084 }
4085}
4086
4087static void asterisk_daemon(int isroot, const char *runuser, const char *rungroup)
4088{
4089 FILE *f;
4090 sigset_t sigs;
4091 int num;
4092 char *buf;
4093 char pbx_uuid[AST_UUID_STR_LEN];
4094
4095 /* Set time as soon as possible */
4097
4098 /* This needs to remain as high up in the initial start up as possible.
4099 * daemon causes a fork to occur, which has all sorts of unintended
4100 * consequences for things that interact with threads. This call *must*
4101 * occur before anything in Asterisk spawns or manipulates thread related
4102 * primitives. */
4103#if HAVE_WORKING_FORK
4105#ifndef HAVE_SBIN_LAUNCHD
4106 if (daemon(1, 0) < 0) {
4107 fprintf(stderr, "daemon() failed: %s\n", strerror(errno));
4108 } else {
4109 ast_mainpid = getpid();
4110 }
4111#else
4112 fprintf(stderr, "Mac OS X detected. Use 'launchctl load /Library/LaunchDaemon/org.asterisk.asterisk.plist'.\n");
4113#endif
4114 }
4115#endif
4116
4117 /* At this point everything has been forked successfully,
4118 * we have determined that we aren't attempting to connect to
4119 * an Asterisk instance, and that there isn't one already running. */
4121
4123
4124 /* Check whether high prio was successfully set by us or some
4125 * other incantation. */
4126 if (has_priority()) {
4128 } else {
4130 }
4131
4132 /* Spawning of astcanary must happen AFTER the call to daemon(3) */
4134 snprintf(canary_filename, sizeof(canary_filename), "%s/alt.asterisk.canary.tweet.tweet.tweet", ast_config_AST_RUN_DIR);
4135
4136 /* Don't let the canary child kill Asterisk, if it dies immediately */
4137 sigaction(SIGPIPE, &ignore_sig_handler, NULL);
4138
4139 canary_pid = fork();
4140 if (canary_pid == 0) {
4141 char canary_binary[PATH_MAX], ppid[12];
4142
4143 /* Reset signal handler */
4144 signal(SIGCHLD, SIG_DFL);
4145 signal(SIGPIPE, SIG_DFL);
4146
4149 snprintf(ppid, sizeof(ppid), "%d", (int) ast_mainpid);
4150
4151 /* Use the astcanary binary that we installed */
4152 snprintf(canary_binary, sizeof(canary_binary), "%s/astcanary", ast_config_AST_SBIN_DIR);
4153 execl(canary_binary, "astcanary", canary_filename, ppid, (char *)NULL);
4154
4155 /* Should never happen */
4156 _exit(1);
4157 } else if (canary_pid > 0) {
4158 pthread_t dont_care;
4160 }
4161
4162 /* Kill the canary when we exit */
4164 }
4165
4166 /* Blindly write the PID file. */
4167 unlink(ast_config_AST_PID);
4168 f = fopen(ast_config_AST_PID, "w");
4169 if (f) {
4170 fprintf(f, "%ld\n", (long)ast_mainpid);
4171 fclose(f);
4172 } else {
4173 fprintf(stderr, "Unable to open pid file '%s': %s\n", ast_config_AST_PID, strerror(errno));
4174 }
4175
4176 /* Initialize the terminal. Since all processes have been forked,
4177 * we can now start using the standard log messages.
4178 */
4179 ast_term_init();
4180 printf("%s", term_end());
4181 fflush(stdout);
4182
4183 print_intro_message(runuser, rungroup);
4184
4186
4187 check_init(astobj2_init(), "AO2");
4188 check_init(ast_named_locks_init(), "Named Locks");
4189
4190 if (ast_opt_console) {
4191 if (el_hist == NULL || el == NULL)
4194 }
4195
4196#ifdef AST_XML_DOCS
4197 /* Load XML documentation. */
4199#endif
4200
4201 check_init(astdb_init(), "ASTdb");
4202
4203 ast_uuid_init();
4204
4205 if (ast_pbx_uuid_get(pbx_uuid, sizeof(pbx_uuid))) {
4206 ast_uuid_generate_str(pbx_uuid, sizeof(pbx_uuid));
4207 ast_db_put("pbx", "UUID", pbx_uuid);
4208 }
4209 ast_verb(0, "PBX UUID: %s\n", pbx_uuid);
4210
4211 check_init(ast_json_init(), "libjansson");
4212 ast_ulaw_init();
4213 ast_alaw_init();
4214 ast_utf8_init();
4215 tdd_init();
4216 callerid_init();
4218
4219 check_init(ast_utils_init(), "Utilities");
4220 check_init(ast_tps_init(), "Task Processor Core");
4221 check_init(ast_fd_init(), "File Descriptor Debugging");
4222 check_init(ast_pbx_init(), "ast_pbx_init");
4223 check_init(aco_init(), "Configuration Option Framework");
4224 check_init(stasis_init(), "Stasis");
4225#ifdef TEST_FRAMEWORK
4226 check_init(ast_test_init(), "Test Framework");
4227#endif
4228 check_init(ast_translate_init(), "Translator Core");
4229
4231
4232 check_init(ast_sorcery_init(), "Sorcery");
4233 check_init(ast_codec_init(), "Codecs");
4234 check_init(ast_format_init(), "Formats");
4235 check_init(ast_format_cache_init(), "Format Cache");
4236 check_init(ast_codec_builtin_init(), "Built-in Codecs");
4237 check_init(ast_bucket_init(), "Bucket API");
4238 check_init(ast_stasis_system_init(), "Stasis system-level information");
4239 check_init(ast_endpoint_stasis_init(), "Stasis Endpoint");
4240
4242 /* GCC 4.9 gives a bogus "right-hand operand of comma expression has
4243 * no effect" warning */
4244 (void) sigemptyset(&sigs);
4245 (void) sigaddset(&sigs, SIGHUP);
4246 (void) sigaddset(&sigs, SIGTERM);
4247 (void) sigaddset(&sigs, SIGINT);
4248 (void) sigaddset(&sigs, SIGPIPE);
4249 (void) sigaddset(&sigs, SIGWINCH);
4250 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
4251 sigaction(SIGURG, &urg_handler, NULL);
4252 signal(SIGINT, __quit_handler);
4253 signal(SIGTERM, __quit_handler);
4254 sigaction(SIGHUP, &hup_handler, NULL);
4255 sigaction(SIGPIPE, &ignore_sig_handler, NULL);
4256
4257 /* ensure that the random number generators are seeded with a different value every time
4258 Asterisk is started
4259 */
4260 srand((unsigned int) getpid() + (unsigned int) time(NULL));
4261 initstate((unsigned int) getpid() * 65536 + (unsigned int) time(NULL), randompool, sizeof(randompool));
4262
4264
4265 check_init(init_logger(), "Logger");
4266 check_init(ast_rtp_engine_init(), "RTP Engine");
4267
4269
4270 check_init(ast_timing_init(), "Timing");
4271 check_init(ast_ssl_init(), "SSL");
4273 check_init(ast_pj_init(), "Embedded PJProject");
4274 check_init(app_init(), "App Core");
4275 check_init(mwi_init(), "MWI Core");
4276 check_init(devstate_init(), "Device State Core");
4277 check_init(ast_msg_init(), "Messaging API");
4278 check_init(ast_channels_init(), "Channel");
4279 check_init(ast_endpoint_init(), "Endpoints");
4280 check_init(ast_pickup_init(), "Call Pickup");
4281 check_init(ast_bridging_init(), "Bridging");
4282 check_init(ast_parking_stasis_init(), "Parking Core");
4283 check_init(ast_device_state_engine_init(), "Device State Engine");
4284 check_init(ast_presence_state_engine_init(), "Presence State Engine");
4285 check_init(dns_core_init(), "DNS Resolver Core");
4286 check_init(ast_dns_system_resolver_init(), "Default DNS resolver");
4287 check_init(ast_security_stasis_init(), "Security Stasis Topic and Events");
4288 check_init(ast_image_init(), "Image");
4289 check_init(ast_file_init(), "Generic File Format Support");
4290 check_init(load_pbx(), "load_pbx");
4291 check_init(load_pbx_builtins(), "Builtin PBX Applications");
4292 check_init(load_pbx_functions_cli(), "PBX Functions Support");
4293 check_init(load_pbx_variables(), "PBX Variables Support");
4294 check_init(load_pbx_switch(), "PBX Switch Support");
4295 check_init(load_pbx_app(), "PBX Application Support");
4296 check_init(load_pbx_hangup_handler(), "PBX Hangup Handler Support");
4297 check_init(ast_local_init(), "Local Proxy Channel Driver");
4298 check_init(ast_refer_init(), "Refer API");
4299
4300 /* We should avoid most config loads before this point as they can't use realtime. */
4301 check_init(load_modules(), "Module");
4302
4303 /*
4304 * This has to load after the dynamic modules load, as items in the media
4305 * cache can't be constructed from items in the AstDB without their
4306 * bucket backends.
4307 */
4308 check_init(ast_media_cache_init(), "Media Cache");
4309
4310 /* loads the cli_permissions.conf file needed to implement cli restrictions. */
4312 ast_cli_channels_init(); /* Not always safe to access CLI commands until startup is complete. */
4313
4314 ast_stun_init();
4315
4317
4318 if (ast_opt_no_fork) {
4319 consolethread = pthread_self();
4320 }
4321
4323
4325
4328
4329 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
4330
4332
4336
4338 ast_sd_notify("READY=1");
4339
4340 ast_verb(0, COLORIZE_FMT "\n", COLORIZE(COLOR_BRGREEN, 0, "Asterisk Ready."));
4341
4343
4344 if (ast_opt_console) {
4345 /* Console stuff now... */
4346 /* Register our quit function */
4347 char title[296];
4348 char hostname[MAXHOSTNAMELEN] = "";
4349
4350 if (gethostname(hostname, sizeof(hostname) - 1)) {
4351 ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
4352 }
4353
4355
4356 set_icon("Asterisk");
4357 snprintf(title, sizeof(title), "Asterisk Console on '%s' (pid %ld)", hostname, (long)ast_mainpid);
4358 set_title(title);
4359
4360 el_set(el, EL_GETCFN, ast_el_read_char);
4361
4362 for (;;) {
4363 if (sig_flags.need_el_end) {
4364 el_end(el);
4365
4366 return;
4367 }
4368
4369 if (sig_flags.need_quit || sig_flags.need_quit_handler) {
4371 break;
4372 }
4373 buf = (char *) el_gets(el, &num);
4374
4375 if (!buf && write(1, "", 1) < 0)
4376 return; /* quit */
4377
4378 if (buf) {
4379 if (buf[strlen(buf)-1] == '\n')
4380 buf[strlen(buf)-1] = '\0';
4381
4383 }
4384 }
4385 }
4386
4387 /* Stall until a quit signal is given */
4389}
Prototypes for public functions only of internal interest,.
int load_pbx_variables(void)
void threadstorage_init(void)
Definition: threadstorage.c:35
void ast_process_pending_reloads(void)
Process reload requests received during startup.
Definition: loader.c:1566
int modules_shutdown(void)
Definition: loader.c:1172
int ast_bridging_init(void)
Initialize the bridging system.
Definition: bridge.c:5543
void dnsmgr_start_refresh(void)
Definition: dnsmgr.c:302
int ast_timing_init(void)
Definition: timing.c:289
int ast_device_state_engine_init(void)
Initialize the device state engine in separate thread.
Definition: devicestate.c:618
int ast_term_init(void)
Definition: term.c:165
int load_modules(void)
Definition: loader.c:2508
void ast_cli_channels_init(void)
Definition: main/cli.c:2252
void ast_msg_shutdown(void)
void logger_queue_start(void)
Start the ast_queue_log() logger.
Definition: logger.c:2186
int aco_init(void)
void load_asterisk_conf(void)
Definition: options.c:210
int ast_test_init(void)
Definition: test.c:1482
void close_logger(void)
Definition: logger.c:2245
void ast_stun_init(void)
Initialize the STUN system in Asterisk.
Definition: stun.c:576
void set_asterisk_conf_path(const char *path)
Definition: options.c:200
int astdb_init(void)
Definition: main/db.c:1256
int ast_pj_init(void)
Definition: libasteriskpj.c:45
int load_pbx_functions_cli(void)
int ast_tps_init(void)
void load_astmm_phase_2(void)
Initialize malloc debug phase 2.
Definition: astmm.c:1529
int ast_msg_init(void)
int load_pbx_hangup_handler(void)
int ast_local_init(void)
Initialize the local proxy channel.
Definition: core_local.c:1139
int load_pbx_builtins(void)
int load_pbx_switch(void)
Definition: pbx_switch.c:125
int ast_rtp_engine_init(void)
initializes the rtp engine arrays
Definition: rtp_engine.c:3840
int ast_xmldoc_load_documentation(void)
Load XML documentation. Provided by xmldoc.c.
Definition: xmldoc.c:3047
int ast_named_locks_init(void)
Definition: named_locks.c:52
int ast_channels_init(void)
Definition: channel.c:8029
void clean_time_zones(void)
Definition: localtime.c:1590
void load_astmm_phase_1(void)
Initialize malloc debug phase 1.
Definition: astmm.c:1525
void ast_builtins_init(void)
initialize the _full_cmd string in * each of the builtins.
Definition: main/cli.c:2245
void ast_autoservice_init(void)
Definition: autoservice.c:387
int init_logger(void)
Definition: logger.c:2202
int ast_endpoint_init(void)
Endpoint support initialization.
int dns_core_init(void)
Definition: dns_core.c:616
int load_pbx(void)
Definition: pbx.c:8421
int astobj2_init(void)
Definition: astobj2.c:1169
int load_pbx_app(void)
Definition: pbx_app.c:538
int ast_dns_system_resolver_init(void)
Initializes the resolver.
int ast_ssl_init(void)
int ast_cli_perms_init(int reload)
Definition: main/cli.c:2112
void set_socket_path(const char *path)
Definition: options.c:205
int ast_file_init(void)
Definition: file.c:2051
int ast_refer_init(void)
Definition: refer.c:529
int ast_parking_stasis_init(void)
initializes the rtp engine arrays
Definition: parking.c:53
Access Control of various sorts.
A-Law to Signed linear conversion.
void ast_alaw_init(void)
To init the alaw to slinear conversion stuff, this needs to be run.
Definition: alaw.c:152
void ast_alertpipe_close(int alert_pipe[2])
Close an alert pipe.
Definition: alertpipe.c:79
ssize_t ast_alertpipe_write(int alert_pipe[2])
Write an event to an alert pipe.
Definition: alertpipe.c:120
int ast_alertpipe_init(int alert_pipe[2])
Initialize an alert pipe.
Definition: alertpipe.c:38
ast_alert_status_t ast_alertpipe_read(int alert_pipe[2])
Read an event from an alert pipe.
Definition: alertpipe.c:102
Generic Advice of Charge encode and decode routines.
int ast_aoc_cli_init(void)
enable aoc cli options
Definition: aoc.c:2020
jack_status_t status
Definition: app_jack.c:146
char * text
Definition: app_queue.c:1639
enum queue_result id
Definition: app_queue.c:1638
if(!yyg->yy_init)
Definition: ast_expr2f.c:854
Asterisk version information.
const char * ast_get_build_opts(void)
const char * ast_get_version(void)
Retrieve the Asterisk version string.
const char * ast_get_build_opts_all(void)
Persistent data storage (akin to *doze registry)
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:341
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:427
static int ast_el_add_history(const char *)
Definition: asterisk.c:3150
static char * remotehostname
Definition: asterisk.c:341
static char * handle_stop_gracefully(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2386
static void __ast_unregister_atexit(void(*func)(void))
Definition: asterisk.c:1018
static char * cli_complete(EditLine *editline, int ch)
Definition: asterisk.c:2969
int64_t ast_mark(int i, int startstop)
Definition: asterisk.c:874
int main(int argc, char *argv[])
Definition: asterisk.c:3584
static int ast_socket_is_sd
Definition: asterisk.c:313
int ast_shutdown_final(void)
Definition: asterisk.c:1872
static void canary_exit(void)
Definition: asterisk.c:3516
static shutdown_nice_t shuttingdown
Definition: asterisk.c:378
static void check_init(int init_result, const char *name)
Definition: asterisk.c:4074
static void __remote_quit_handler(int num)
Definition: asterisk.c:2153
static void _urg_handler(int num)
Urgent handler.
Definition: asterisk.c:1712
static char * show_license(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2591
#define CHAR_T_LIBEDIT
Definition: asterisk.c:2674
static char * handle_show_threads(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:604
static void ast_run_atexits(int run_cleanups)
Definition: asterisk.c:1004
static int console_state_init(void *ptr)
Definition: asterisk.c:2208
static struct profile_data * prof_data
Definition: asterisk.c:793
static char * handle_stop_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2407
static int remoteconsolehandler(const char *s)
Definition: asterisk.c:2317
static struct ast_str * prompt
Definition: asterisk.c:2780
unsigned int need_reload
Definition: asterisk.c:398
static void publish_fully_booted(void)
Definition: asterisk.c:978
static int show_version(void)
Definition: asterisk.c:3383
static void main_atexit(void)
Definition: asterisk.c:3579
static int safe_exec_wait(pid_t pid)
wait for spawned application to complete and unreplace sigchld
Definition: asterisk.c:1197
int ast_add_profile(const char *name, uint64_t scale)
allocates a counter with a given name and scale.
Definition: asterisk.c:799
static void ast_cli_display_match_list(struct ast_vector_string *matches, int max)
Definition: asterisk.c:2942
static void _hup_handler(int num)
Definition: asterisk.c:1721
static char * handle_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2345
static pthread_t lthread
Definition: asterisk.c:1367
static unsigned int safe_system_level
Keep track of how many threads are currently trying to wait*() on a child process.
Definition: asterisk.c:1098
static void ast_network_puts_mutable(const char *string, int level, int sublevel)
log the string to all attached network console clients
Definition: asterisk.c:1298
static char randompool[256]
Definition: asterisk.c:390
static char * handle_show_sysinfo(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Give an overview of system statistics.
Definition: asterisk.c:670
#define WELCOME_MESSAGE
Welcome message when starting a CLI interface.
Definition: asterisk.c:303
int ast_safe_execvp(int dualfork, const char *file, char *const argv[])
Safely spawn an external program while closing file descriptors.
Definition: asterisk.c:1223
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: asterisk.c:1056
static void set_header(char *outbuf, int maxout, char level)
Definition: asterisk.c:2158
static int ast_el_read_char(EditLine *editline, CHAR_T_LIBEDIT *cp)
Definition: asterisk.c:2681
static char * handle_stop_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2366
static char canary_filename[128]
Definition: asterisk.c:387
#define CMD_MATCHESARRAY
static __inline uint64_t rdtsc(void)
Definition: asterisk.c:867
static struct sigaction child_handler
Definition: asterisk.c:1760
int ast_set_priority(int pri)
We set ourselves to a high priority, that we might pre-empt everything else. If your PBX has heavy ac...
Definition: asterisk.c:1842
static void asterisk_daemon(int isroot, const char *runuser, const char *rungroup)
Definition: asterisk.c:4087
void ast_console_toggle_loglevel(int fd, int level, int state)
enable or disable a logging level to a specified console
Definition: asterisk.c:1252
static char * handle_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Give an overview of core settings.
Definition: asterisk.c:467
static int has_priority(void)
Check whether we were set to high(er) priority.
Definition: asterisk.c:1779
#define PF_LOCAL
Definition: asterisk.c:293
unsigned int need_el_end
Definition: asterisk.c:401
void ast_console_toggle_mute(int fd, int silent)
mute or unmute a console from logging
Definition: asterisk.c:1275
static void send_rasterisk_connect_commands(void)
Definition: asterisk.c:2645
unsigned int need_quit
Definition: asterisk.c:399
#define ASTERISK_PROMPT
Definition: asterisk.c:2609
static struct sigaction safe_system_prev_handler
Definition: asterisk.c:1099
static char * handle_bang(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2514
static struct sigaction hup_handler
Definition: asterisk.c:1740
static pthread_t mon_sig_flags
Definition: asterisk.c:385
static int fdprint(int fd, const char *s)
Definition: asterisk.c:1075
static pthread_t consolethread
Definition: asterisk.c:384
static EditLine * el
Definition: asterisk.c:340
static void * netconsole(void *vconsole)
Definition: asterisk.c:1429
static int ast_el_initialize(void)
Definition: asterisk.c:3095
static int set_priority_all(int pri)
Set priority on all known threads.
Definition: asterisk.c:1799
static void ast_begin_shutdown(void)
Definition: asterisk.c:1900
static char * handle_restart_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2428
static char * handle_restart_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2470
static int ast_el_read_history(const char *)
Definition: asterisk.c:3182
static char * handle_abort_shutdown(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2491
int ast_shutting_down(void)
Definition: asterisk.c:1877
static void _child_handler(int sig)
Definition: asterisk.c:1745
void ast_console_puts_mutable(const char *string, int level)
log the string to the root console, and all attached network console clients
Definition: asterisk.c:1317
#define AST_MAX_CONNECTS
Definition: asterisk.c:296
int ast_pbx_uuid_get(char *pbx_uuid, int length)
Retrieve the PBX UUID.
Definition: asterisk.c:973
void ast_replace_sigchld(void)
Replace the SIGCHLD handler.
Definition: asterisk.c:1101
int ast_cancel_shutdown(void)
Cancel an existing shutdown and return to normal operation.
Definition: asterisk.c:1882
static void ast_network_puts(const char *string)
write the string to all attached console clients
Definition: asterisk.c:1341
static int read_credentials(int fd, char *buffer, size_t size, struct console *con)
read() function supporting the reception of user credentials.
Definition: asterisk.c:1379
#define CHAR_TO_LIBEDIT(c)
Definition: asterisk.c:2675
static int shutdown_pending
Definition: asterisk.c:381
static void _null_sig_handler(int sig)
NULL handler so we can collect the child exit status.
Definition: asterisk.c:1081
static void process_histfile(int(*readwrite)(const char *filename))
Definition: asterisk.c:3198
static struct @297 sig_flags
static int sig_alert_pipe[2]
Definition: asterisk.c:396
struct timeval ast_lastreloadtime
Definition: asterisk.c:337
static const char warranty_lines[]
Definition: asterisk.c:2529
void ast_console_puts(const char *string)
write the string to the root console, and all attached network console clients
Definition: asterisk.c:1357
void ast_unregister_atexit(void(*func)(void))
Unregister a function registered with ast_register_atexit().
Definition: asterisk.c:1061
static int console_print(const char *s)
Definition: asterisk.c:2217
static void quit_handler(int num, shutdown_nice_t niceness, int restart)
Definition: asterisk.c:1912
static History * el_hist
Definition: asterisk.c:339
static char * handle_restart_gracefully(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2449
static void * monitor_sig_flags(void *unused)
Definition: asterisk.c:3462
#define COPYRIGHT_TAG
Definition: asterisk.c:300
#define EL_BUF_SIZE
static char * show_warranty(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2554
#define MAX_HISTORY_COMMAND_LENGTH
Definition: asterisk.c:3148
void ast_register_thread(char *name)
Definition: asterisk.c:414
static void ast_remotecontrol(char *data)
Definition: asterisk.c:3235
static int restartnow
Definition: asterisk.c:383
static int print_file(int fd, char *desc, const char *filename)
Print the contents of a file.
Definition: asterisk.c:450
void ast_unregister_thread(void *id)
Definition: asterisk.c:430
static void consolehandler(const char *s)
Definition: asterisk.c:2299
static struct ast_cli_entry cli_asterisk_shutdown[]
Shutdown Asterisk CLI commands.
Definition: asterisk.c:2619
int ast_register_atexit(void(*func)(void))
Register a function to be executed before Asterisk exits.
Definition: asterisk.c:1051
shutdown_nice_t
Definition: asterisk.c:356
@ SHUTDOWN_FAST
Definition: asterisk.c:369
@ SHUTDOWN_NORMAL
Definition: asterisk.c:371
@ SHUTDOWN_NICE
Definition: asterisk.c:373
@ SHUTDOWN_REALLY_NICE
Definition: asterisk.c:375
@ SHUTTING_DOWN
Definition: asterisk.c:362
@ SHUTTING_DOWN_FINAL
Definition: asterisk.c:360
@ NOT_SHUTTING_DOWN
Definition: asterisk.c:358
static ast_mutex_t safe_system_lock
Definition: asterisk.c:1094
static pid_t safe_exec_prep(int dualfork)
fork and perform other preparations for spawning applications
Definition: asterisk.c:1136
int ast_safe_system(const char *s)
Safely spawn an OS shell command while closing file descriptors.
Definition: asterisk.c:1236
static int ast_all_zeros(const char *s)
Definition: asterisk.c:2288
#define SHUTDOWN_TIMEOUT
Definition: asterisk.c:1921
static int can_safely_quit(shutdown_nice_t niceness, int restart)
Definition: asterisk.c:1964
static char * cli_prompt(EditLine *editline)
Definition: asterisk.c:2782
static void set_title(char *text)
Set an X-term or screen title.
Definition: asterisk.c:1766
static char * _argv[256]
Definition: asterisk.c:354
static int register_atexit(void(*func)(void), int is_cleanup)
Definition: asterisk.c:1032
static char * handle_clear_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:943
static int wait_for_channels_to_die(shutdown_nice_t niceness, int seconds)
Definition: asterisk.c:1934
static struct ast_threadstorage console_state
Definition: asterisk.c:2215
static int ast_makesocket(void)
Definition: asterisk.c:1599
#define DEFINE_PROFILE_MIN_MAX_VALUES
Definition: asterisk.c:895
unsigned int need_quit_handler
Definition: asterisk.c:400
static int canary_pid
Definition: asterisk.c:386
static int ast_tryconnect(void)
Definition: asterisk.c:1685
static void * canary_thread(void *unused)
Definition: asterisk.c:3487
static void * listener(void *unused)
Definition: asterisk.c:1519
static struct ast_cli_entry cli_asterisk[]
Definition: asterisk.c:2628
static int fdsend(int fd, const char *s)
Definition: asterisk.c:1069
static int ast_el_write_history(const char *)
Definition: asterisk.c:3172
static void run_startup_commands(void)
Definition: asterisk.c:3526
static int show_cli_help(void)
Definition: asterisk.c:3389
static void ast_el_read_default_histfile(void)
Definition: asterisk.c:3225
static char * handle_show_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:908
static void env_init(void)
Definition: asterisk.c:3554
static int ast_consock
Definition: asterisk.c:314
struct timeval ast_startuptime
Definition: asterisk.c:336
static void read_pjproject_startup_options(void)
Definition: asterisk.c:3428
static const char license_lines[]
Definition: asterisk.c:2572
void ast_unreplace_sigchld(void)
Restore the SIGCHLD handler.
Definition: asterisk.c:1116
static void really_quit(int num, shutdown_nice_t niceness, int restart)
Definition: asterisk.c:2047
static int ast_socket
Definition: asterisk.c:312
pid_t ast_mainpid
Definition: asterisk.c:315
static void ast_el_write_default_histfile(void)
Definition: asterisk.c:3230
#define AF_LOCAL
Definition: asterisk.c:292
static int multi_thread_safe
Definition: asterisk.c:388
static struct ast_vector_string * ast_el_strtoarr(char *buf)
Definition: asterisk.c:2908
static void __quit_handler(int num)
Definition: asterisk.c:2143
void ast_console_puts_mutable_full(const char *message, int level, int sublevel)
log the string to the console, and all attached console clients
Definition: asterisk.c:1324
static void set_icon(char *text)
Definition: asterisk.c:1772
static void print_intro_message(const char *runuser, const char *rungroup)
Definition: asterisk.c:3566
static struct sigaction ignore_sig_handler
Definition: asterisk.c:1090
static struct sigaction null_sig_handler
Definition: asterisk.c:1085
struct console consoles[AST_MAX_CONNECTS]
Definition: asterisk.c:343
int64_t ast_profile(int i, int64_t delta)
Definition: asterisk.c:833
static struct sigaction urg_handler
Definition: asterisk.c:1717
Asterisk main include file. File version handling, generic pbx functions.
#define sched_setscheduler
Definition: asterisk.h:51
int ast_pbx_init(void)
Definition: pbx.c:8989
#define setpriority
Definition: asterisk.h:50
int ast_fd_init(void)
Definition: astfd.c:371
#define PATH_MAX
Definition: asterisk.h:40
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_free(a)
Definition: astmm.h:180
#define ast_realloc(p, len)
A wrapper for realloc()
Definition: astmm.h:226
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
void ast_free_ptr(void *ptr)
free() wrapper
Definition: astmm.c:1739
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:267
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
static int tmp()
Definition: bt_open.c:389
Bucket File API.
int ast_bucket_init(void)
Initialize bucket support.
Definition: bucket.c:954
const char * ast_build_os
Definition: buildinfo.c:32
const char * ast_build_machine
Definition: buildinfo.c:31
const char * ast_build_hostname
Definition: buildinfo.c:29
const char * ast_build_user
Definition: buildinfo.c:34
const char * ast_build_date
Definition: buildinfo.c:33
const char * ast_build_kernel
Definition: buildinfo.c:30
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
void callerid_init(void)
CallerID Initialization.
Definition: callerid.c:116
Call Detail Record API.
int ast_cdr_is_enabled(void)
Return TRUE if CDR subsystem is enabled.
Definition: cdr.c:2927
void ast_cdr_engine_term(void)
Definition: cdr.c:4682
static const char desc[]
Definition: cdr_radius.c:84
static PGresult * result
Definition: cel_pgsql.c:84
static char version[AST_MAX_EXTENSION]
Definition: chan_ooh323.c:391
General Asterisk PBX channel definitions.
void ast_softhangup_all(void)
Soft hangup all active channels.
Definition: channel.c:493
int ast_undestroyed_channels(void)
Definition: channel.c:504