Asterisk - The Open Source Telephony Project GIT-master-7e7a603
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, "\n\n");
600 return CLI_SUCCESS;
601}
602
603static char *handle_show_threads(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
604{
605 int count = 0;
606 struct thread_list_t *cur;
607 switch (cmd) {
608 case CLI_INIT:
609 e->command = "core show threads";
610 e->usage =
611 "Usage: core show threads\n"
612 " List threads currently active in the system.\n";
613 return NULL;
614 case CLI_GENERATE:
615 return NULL;
616 }
617
619 AST_RWLIST_TRAVERSE(&thread_list, cur, list) {
620 ast_cli(a->fd, "%p %d %s\n", (void *)cur->id, cur->lwp, cur->name);
621 count++;
622 }
624 ast_cli(a->fd, "%d threads listed.\n", count);
625 return CLI_SUCCESS;
626}
627
628#if defined (HAVE_SYSCTL) && defined(HAVE_SWAPCTL)
629/*
630 * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
631 * to be based on the new swapctl(2) system call.
632 */
633static int swapmode(int *used, int *total)
634{
635 struct swapent *swdev;
636 int nswap, rnswap, i;
637
638 nswap = swapctl(SWAP_NSWAP, 0, 0);
639 if (nswap == 0)
640 return 0;
641
642 swdev = ast_calloc(nswap, sizeof(*swdev));
643 if (swdev == NULL)
644 return 0;
645
646 rnswap = swapctl(SWAP_STATS, swdev, nswap);
647 if (rnswap == -1) {
648 ast_free(swdev);
649 return 0;
650 }
651
652 /* if rnswap != nswap, then what? */
653
654 /* Total things up */
655 *total = *used = 0;
656 for (i = 0; i < nswap; i++) {
657 if (swdev[i].se_flags & SWF_ENABLE) {
658 *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
659 *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
660 }
661 }
662 ast_free(swdev);
663 return 1;
664}
665#endif
666
667#if defined(HAVE_SYSINFO) || defined(HAVE_SYSCTL)
668/*! \brief Give an overview of system statistics */
669static char *handle_show_sysinfo(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
670{
671 uint64_t physmem, freeram;
672#if defined(HAVE_SYSINFO) || defined(HAVE_SWAPCTL)
673 int totalswap = 0;
674 uint64_t freeswap = 0;
675#endif
676 int nprocs = 0;
677 long uptime = 0;
678#if defined(HAVE_SYSINFO)
679 struct sysinfo sys_info;
680#elif defined(HAVE_SYSCTL)
681 static int pageshift;
682 struct vmtotal vmtotal;
683 struct timeval boottime;
684 time_t now;
685 int mib[2], pagesize;
686#if defined(HAVE_SWAPCTL)
687 int usedswap = 0;
688#endif
689 size_t len;
690#endif
691
692 switch (cmd) {
693 case CLI_INIT:
694 e->command = "core show sysinfo";
695 e->usage =
696 "Usage: core show sysinfo\n"
697 " List current system information.\n";
698 return NULL;
699 case CLI_GENERATE:
700 return NULL;
701 }
702
703#if defined(HAVE_SYSINFO)
704 sysinfo(&sys_info);
705 uptime = sys_info.uptime / 3600;
706 physmem = sys_info.totalram * sys_info.mem_unit;
707 freeram = (sys_info.freeram * sys_info.mem_unit) / 1024;
708 totalswap = (sys_info.totalswap * sys_info.mem_unit) / 1024;
709 freeswap = (sys_info.freeswap * sys_info.mem_unit) / 1024;
710 nprocs = sys_info.procs;
711#elif defined(HAVE_SYSCTL)
712 /* calculate the uptime by looking at boottime */
713 time(&now);
714 mib[0] = CTL_KERN;
715 mib[1] = KERN_BOOTTIME;
716 len = sizeof(boottime);
717 if (sysctl(mib, 2, &boottime, &len, NULL, 0) != -1) {
718 uptime = now - boottime.tv_sec;
719 }
720 uptime = uptime/3600;
721 /* grab total physical memory */
722 mib[0] = CTL_HW;
723#if defined(HW_PHYSMEM64)
724 mib[1] = HW_PHYSMEM64;
725#else
726 mib[1] = HW_PHYSMEM;
727#endif
728 len = sizeof(physmem);
729 sysctl(mib, 2, &physmem, &len, NULL, 0);
730
731 pagesize = getpagesize();
732 pageshift = 0;
733 while (pagesize > 1) {
734 pageshift++;
735 pagesize >>= 1;
736 }
737
738 /* we only need the amount of log(2)1024 for our conversion */
739 pageshift -= 10;
740
741 /* grab vm totals */
742 mib[0] = CTL_VM;
743 mib[1] = VM_METER;
744 len = sizeof(vmtotal);
745 sysctl(mib, 2, &vmtotal, &len, NULL, 0);
746 freeram = (vmtotal.t_free << pageshift);
747 /* generate swap usage and totals */
748#if defined(HAVE_SWAPCTL)
749 swapmode(&usedswap, &totalswap);
750 freeswap = (totalswap - usedswap);
751#endif
752 /* grab number of processes */
753#if defined(__OpenBSD__)
754 mib[0] = CTL_KERN;
755 mib[1] = KERN_NPROCS;
756 len = sizeof(nprocs);
757 sysctl(mib, 2, &nprocs, &len, NULL, 0);
758#endif
759#endif
760
761 ast_cli(a->fd, "\nSystem Statistics\n");
762 ast_cli(a->fd, "-----------------\n");
763 ast_cli(a->fd, " System Uptime: %ld hours\n", uptime);
764 ast_cli(a->fd, " Total RAM: %" PRIu64 " KiB\n", physmem / 1024);
765 ast_cli(a->fd, " Free RAM: %" PRIu64 " KiB\n", freeram);
766#if defined(HAVE_SYSINFO)
767 ast_cli(a->fd, " Buffer RAM: %" PRIu64 " KiB\n", ((uint64_t) sys_info.bufferram * sys_info.mem_unit) / 1024);
768#endif
769#if defined(HAVE_SYSINFO) || defined(HAVE_SWAPCTL)
770 ast_cli(a->fd, " Total Swap Space: %d KiB\n", totalswap);
771 ast_cli(a->fd, " Free Swap Space: %" PRIu64 " KiB\n\n", freeswap);
772#endif
773 ast_cli(a->fd, " Number of Processes: %d \n\n", nprocs);
774 return CLI_SUCCESS;
775}
776#endif
777
779 const char *name;
780 uint64_t scale; /* if non-zero, values are scaled by this */
781 int64_t mark;
782 int64_t value;
783 int64_t events;
784};
785
789 struct profile_entry e[0];
790};
791
792static struct profile_data *prof_data;
793#endif /* ! LOW_MEMORY */
794
795/*! \brief allocates a counter with a given name and scale.
796 * \return Returns the identifier of the counter.
797 */
798int ast_add_profile(const char *name, uint64_t scale)
799{
800#if !defined(LOW_MEMORY)
801 int l = sizeof(struct profile_data);
802 int n = 10; /* default entries */
803
804 if (prof_data == NULL) {
805 prof_data = ast_calloc(1, l + n*sizeof(struct profile_entry));
806 if (prof_data == NULL)
807 return -1;
808 prof_data->entries = 0;
809 prof_data->max_size = n;
810 }
812 void *p;
813 n = prof_data->max_size + 20;
814 p = ast_realloc(prof_data, l + n*sizeof(struct profile_entry));
815 if (p == NULL)
816 return -1;
817 prof_data = p;
818 prof_data->max_size = n;
819 }
820 n = prof_data->entries++;
822 prof_data->e[n].value = 0;
823 prof_data->e[n].events = 0;
824 prof_data->e[n].mark = 0;
825 prof_data->e[n].scale = scale;
826 return n;
827#else /* if defined(LOW_MEMORY) */
828 return 0;
829#endif
830}
831
832int64_t ast_profile(int i, int64_t delta)
833{
834#if !defined(LOW_MEMORY)
835 if (!prof_data || i < 0 || i > prof_data->entries) /* invalid index */
836 return 0;
837 if (prof_data->e[i].scale > 1)
838 delta /= prof_data->e[i].scale;
839 prof_data->e[i].value += delta;
840 prof_data->e[i].events++;
841 return prof_data->e[i].value;
842#else /* if defined(LOW_MEMORY) */
843 return 0;
844#endif
845}
846
847#if !defined(LOW_MEMORY)
848/* The RDTSC instruction was introduced on the Pentium processor and is not
849 * implemented on certain clones, like the Cyrix 586. Hence, the previous
850 * expectation of __i386__ was in error. */
851#if defined ( __i686__) && (defined(__FreeBSD__) || defined(__NetBSD__) || defined(linux))
852#if defined(__FreeBSD__)
853#include <machine/cpufunc.h>
854#elif defined(__NetBSD__) || defined(linux)
855static __inline uint64_t
856rdtsc(void)
857{
858 uint64_t rv;
859
860 __asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
861 return (rv);
862}
863#endif
864#else /* supply a dummy function on other platforms */
865static __inline uint64_t
866rdtsc(void)
867{
868 return 0;
869}
870#endif
871#endif /* ! LOW_MEMORY */
872
873int64_t ast_mark(int i, int startstop)
874{
875#if !defined(LOW_MEMORY)
876 if (!prof_data || i < 0 || i > prof_data->entries) /* invalid index */
877 return 0;
878 if (startstop == 1)
879 prof_data->e[i].mark = rdtsc();
880 else {
881 prof_data->e[i].mark = (rdtsc() - prof_data->e[i].mark);
882 if (prof_data->e[i].scale > 1)
883 prof_data->e[i].mark /= prof_data->e[i].scale;
884 prof_data->e[i].value += prof_data->e[i].mark;
885 prof_data->e[i].events++;
886 }
887 return prof_data->e[i].mark;
888#else /* if defined(LOW_MEMORY) */
889 return 0;
890#endif
891}
892
893#if !defined(LOW_MEMORY)
894#define DEFINE_PROFILE_MIN_MAX_VALUES min = 0; \
895 max = prof_data->entries;\
896 if (a->argc > 3) { /* specific entries */ \
897 if (isdigit(a->argv[3][0])) { \
898 min = atoi(a->argv[3]); \
899 if (a->argc == 5 && strcmp(a->argv[4], "-")) \
900 max = atoi(a->argv[4]); \
901 } else \
902 search = a->argv[3]; \
903 } \
904 if (max > prof_data->entries) \
905 max = prof_data->entries;
906
907static char *handle_show_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
908{
909 int i, min, max;
910 const char *search = NULL;
911 switch (cmd) {
912 case CLI_INIT:
913 e->command = "core show profile";
914 e->usage = "Usage: core show profile\n"
915 " show profile information";
916 return NULL;
917 case CLI_GENERATE:
918 return NULL;
919 }
920
921 if (prof_data == NULL)
922 return 0;
923
925 ast_cli(a->fd, "profile values (%d, allocated %d)\n-------------------\n",
927 ast_cli(a->fd, "%6s %8s %10s %12s %12s %s\n", "ID", "Scale", "Events",
928 "Value", "Average", "Name");
929 for (i = min; i < max; i++) {
930 struct profile_entry *entry = &prof_data->e[i];
931 if (!search || strstr(entry->name, search))
932 ast_cli(a->fd, "%6d: [%8ld] %10ld %12lld %12lld %s\n",
933 i,
934 (long)entry->scale,
935 (long)entry->events, (long long)entry->value,
936 (long long)(entry->events ? entry->value / entry->events : entry->value),
937 entry->name);
938 }
939 return CLI_SUCCESS;
940}
941
942static char *handle_clear_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
943{
944 int i, min, max;
945 const char *search = NULL;
946 switch (cmd) {
947 case CLI_INIT:
948 e->command = "core clear profile";
949 e->usage = "Usage: core clear profile\n"
950 " clear profile information";
951 return NULL;
952 case CLI_GENERATE:
953 return NULL;
954 }
955
956 if (prof_data == NULL)
957 return 0;
958
960 for (i= min; i < max; i++) {
961 if (!search || strstr(prof_data->e[i].name, search)) {
962 prof_data->e[i].value = 0;
963 prof_data->e[i].events = 0;
964 }
965 }
966 return CLI_SUCCESS;
967}
968#undef DEFINE_PROFILE_MIN_MAX_VALUES
969
970#endif /* ! LOW_MEMORY */
971
972int ast_pbx_uuid_get(char *pbx_uuid, int length)
973{
974 return ast_db_get("pbx", "UUID", pbx_uuid, length);
975}
976
977static void publish_fully_booted(void)
978{
979 struct ast_json *json_object;
980 int uptime = 0;
981 int lastreloaded = 0;
982 struct timeval tmp;
983 struct timeval curtime = ast_tvnow();
984
985 if (ast_startuptime.tv_sec) {
986 tmp = ast_tvsub(curtime, ast_startuptime);
987 uptime = (int) tmp.tv_sec;
988 }
989
990 if (ast_lastreloadtime.tv_sec) {
991 tmp = ast_tvsub(curtime, ast_lastreloadtime);
992 lastreloaded = (int) tmp.tv_sec;
993 }
994
995 json_object = ast_json_pack("{s: s, s: i, s: i}",
996 "Status", "Fully Booted",
997 "Uptime", uptime,
998 "LastReload", lastreloaded);
999 ast_manager_publish_event("FullyBooted", EVENT_FLAG_SYSTEM, json_object);
1000 ast_json_unref(json_object);
1001}
1002
1003static void ast_run_atexits(int run_cleanups)
1004{
1005 struct ast_atexit *ae;
1006
1008 while ((ae = AST_LIST_REMOVE_HEAD(&atexits, list))) {
1009 if (ae->func && (!ae->is_cleanup || run_cleanups)) {
1010 ae->func();
1011 }
1012 ast_free(ae);
1013 }
1015}
1016
1017static void __ast_unregister_atexit(void (*func)(void))
1018{
1019 struct ast_atexit *ae;
1020
1022 if (ae->func == func) {
1024 ast_free(ae);
1025 break;
1026 }
1027 }
1029}
1030
1031static int register_atexit(void (*func)(void), int is_cleanup)
1032{
1033 struct ast_atexit *ae;
1034
1035 ae = ast_calloc(1, sizeof(*ae));
1036 if (!ae) {
1037 return -1;
1038 }
1039 ae->func = func;
1040 ae->is_cleanup = is_cleanup;
1041
1046
1047 return 0;
1048}
1049
1050int ast_register_atexit(void (*func)(void))
1051{
1052 return register_atexit(func, 0);
1053}
1054
1055int ast_register_cleanup(void (*func)(void))
1056{
1057 return register_atexit(func, 1);
1058}
1059
1060void ast_unregister_atexit(void (*func)(void))
1061{
1065}
1066
1067/* Sending commands from consoles back to the daemon requires a terminating NULL */
1068static int fdsend(int fd, const char *s)
1069{
1070 return write(fd, s, strlen(s) + 1);
1071}
1072
1073/* Sending messages from the daemon back to the display requires _excluding_ the terminating NULL */
1074static int fdprint(int fd, const char *s)
1075{
1076 return write(fd, s, strlen(s));
1077}
1078
1079/*! \brief NULL handler so we can collect the child exit status */
1080static void _null_sig_handler(int sig)
1081{
1082}
1083
1084static struct sigaction null_sig_handler = {
1085 .sa_handler = _null_sig_handler,
1086 .sa_flags = SA_RESTART,
1087};
1088
1089static struct sigaction ignore_sig_handler = {
1090 .sa_handler = SIG_IGN,
1091};
1092
1094/*! \brief Keep track of how many threads are currently trying to wait*() on
1095 * a child process
1096 */
1097static unsigned int safe_system_level = 0;
1098static struct sigaction safe_system_prev_handler;
1099
1101{
1102 unsigned int level;
1103
1105 level = safe_system_level++;
1106
1107 /* only replace the handler if it has not already been done */
1108 if (level == 0) {
1109 sigaction(SIGCHLD, &null_sig_handler, &safe_system_prev_handler);
1110 }
1111
1113}
1114
1116{
1117 unsigned int level;
1118
1120 level = --safe_system_level;
1121
1122 /* only restore the handler if we are the last one */
1123 if (level == 0) {
1124 sigaction(SIGCHLD, &safe_system_prev_handler, NULL);
1125 }
1126
1128}
1129
1130/*! \brief fork and perform other preparations for spawning applications */
1131static pid_t safe_exec_prep(int dualfork)
1132{
1133 pid_t pid;
1134
1135#if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
1137
1138#ifdef HAVE_WORKING_FORK
1139 pid = fork();
1140#else
1141 pid = vfork();
1142#endif
1143
1144 if (pid == 0) {
1145#ifdef HAVE_CAP
1146 cap_set_proc(child_cap);
1147#endif
1148#ifdef HAVE_WORKING_FORK
1151 }
1152 /* Close file descriptors and launch system command */
1153 ast_close_fds_above_n(STDERR_FILENO);
1154#endif
1155 if (dualfork) {
1156#ifdef HAVE_WORKING_FORK
1157 pid = fork();
1158#else
1159 pid = vfork();
1160#endif
1161 if (pid < 0) {
1162 /* Second fork failed. */
1163 /* No logger available. */
1164 _exit(1);
1165 }
1166
1167 if (pid > 0) {
1168 /* This is the first fork, exit so the reaper finishes right away. */
1169 _exit(0);
1170 }
1171
1172 /* This is the second fork. The first fork will exit immediately so
1173 * Asterisk doesn't have to wait for completion.
1174 * ast_safe_system("cmd &") would run in the background, but the '&'
1175 * cannot be added with ast_safe_execvp, so we have to double fork.
1176 */
1177 }
1178 }
1179
1180 if (pid < 0) {
1181 ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
1182 }
1183#else
1184 ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(ENOTSUP));
1185 pid = -1;
1186#endif
1187
1188 return pid;
1189}
1190
1191/*! \brief wait for spawned application to complete and unreplace sigchld */
1192static int safe_exec_wait(pid_t pid)
1193{
1194 int res = -1;
1195
1196#if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
1197 if (pid > 0) {
1198 for (;;) {
1199 int status;
1200
1201 res = waitpid(pid, &status, 0);
1202 if (res > -1) {
1203 res = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
1204 break;
1205 }
1206 if (errno != EINTR) {
1207 break;
1208 }
1209 }
1210 }
1211
1213#endif
1214
1215 return res;
1216}
1217
1218int ast_safe_execvp(int dualfork, const char *file, char *const argv[])
1219{
1220 pid_t pid = safe_exec_prep(dualfork);
1221
1222 if (pid == 0) {
1223 execvp(file, argv);
1224 _exit(1);
1225 /* noreturn from _exit */
1226 }
1227
1228 return safe_exec_wait(pid);
1229}
1230
1231int ast_safe_system(const char *s)
1232{
1233 pid_t pid = safe_exec_prep(0);
1234
1235 if (pid == 0) {
1236 execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
1237 _exit(1);
1238 /* noreturn from _exit */
1239 }
1240
1241 return safe_exec_wait(pid);
1242}
1243
1244/*!
1245 * \brief enable or disable a logging level to a specified console
1246 */
1247void ast_console_toggle_loglevel(int fd, int level, int state)
1248{
1249 int x;
1250
1251 if (level >= NUMLOGLEVELS) {
1252 level = NUMLOGLEVELS - 1;
1253 }
1254
1255 for (x = 0;x < AST_MAX_CONNECTS; x++) {
1256 if (fd == consoles[x].fd) {
1257 /*
1258 * Since the logging occurs when levels are false, set to
1259 * flipped iinput because this function accepts 0 as off and 1 as on
1260 */
1261 consoles[x].levels[level] = state ? 0 : 1;
1262 return;
1263 }
1264 }
1265}
1266
1267/*!
1268 * \brief mute or unmute a console from logging
1269 */
1270void ast_console_toggle_mute(int fd, int silent)
1271{
1272 int x;
1273 for (x = 0;x < AST_MAX_CONNECTS; x++) {
1274 if (fd == consoles[x].fd) {
1275 if (consoles[x].mute) {
1276 consoles[x].mute = 0;
1277 if (!silent)
1278 ast_cli(fd, "Console is not muted anymore.\n");
1279 } else {
1280 consoles[x].mute = 1;
1281 if (!silent)
1282 ast_cli(fd, "Console is muted.\n");
1283 }
1284 return;
1285 }
1286 }
1287 ast_cli(fd, "Couldn't find remote console.\n");
1288}
1289
1290/*!
1291 * \brief log the string to all attached network console clients
1292 */
1293static void ast_network_puts_mutable(const char *string, int level, int sublevel)
1294{
1295 int x;
1296
1297 for (x = 0; x < AST_MAX_CONNECTS; ++x) {
1298 if (consoles[x].fd < 0
1299 || consoles[x].mute
1300 || consoles[x].levels[level]
1301 || (level == __LOG_VERBOSE && consoles[x].option_verbose < sublevel)) {
1302 continue;
1303 }
1304 fdprint(consoles[x].p[1], string);
1305 }
1306}
1307
1308/*!
1309 * \brief log the string to the root console, and all attached
1310 * network console clients
1311 */
1312void ast_console_puts_mutable(const char *string, int level)
1313{
1314 ast_console_puts_mutable_full(string, level, 0);
1315}
1316
1317static int console_print(const char *s);
1318
1319void ast_console_puts_mutable_full(const char *message, int level, int sublevel)
1320{
1321 /* Send to the root console */
1323
1324 /* Wake up a poll()ing console */
1326 pthread_kill(consolethread, SIGURG);
1327 }
1328
1329 /* Send to any network console clients */
1330 ast_network_puts_mutable(message, level, sublevel);
1331}
1332
1333/*!
1334 * \brief write the string to all attached console clients
1335 */
1336static void ast_network_puts(const char *string)
1337{
1338 int x;
1339
1340 for (x = 0; x < AST_MAX_CONNECTS; ++x) {
1341 if (consoles[x].fd < 0) {
1342 continue;
1343 }
1344 fdprint(consoles[x].p[1], string);
1345 }
1346}
1347
1348/*!
1349 * \brief write the string to the root console, and all attached
1350 * network console clients
1351 */
1352void ast_console_puts(const char *string)
1353{
1354 /* Send to the root console */
1355 fputs(string, stdout);
1356 fflush(stdout);
1357
1358 /* Send to any network console clients */
1359 ast_network_puts(string);
1360}
1361
1362static pthread_t lthread;
1363
1364/*!
1365 * \brief read() function supporting the reception of user credentials.
1366 *
1367 * \param fd Socket file descriptor.
1368 * \param buffer Receive buffer.
1369 * \param size 'buffer' size.
1370 * \param con Console structure to set received credentials
1371 * \retval -1 on error
1372 * \retval the number of bytes received on success.
1373 */
1374static int read_credentials(int fd, char *buffer, size_t size, struct console *con)
1375{
1376#if defined(SO_PEERCRED)
1377#ifdef HAVE_STRUCT_SOCKPEERCRED_UID
1378#define HAVE_STRUCT_UCRED_UID
1379 struct sockpeercred cred;
1380#else
1381 struct ucred cred;
1382#endif
1383 socklen_t len = sizeof(cred);
1384#endif
1385#if defined(HAVE_GETPEEREID)
1386 uid_t uid;
1387 gid_t gid;
1388#else
1389 int uid, gid;
1390#endif
1391 int result;
1392
1393 result = read(fd, buffer, size);
1394 if (result < 0) {
1395 return result;
1396 }
1397
1398#if defined(SO_PEERCRED) && (defined(HAVE_STRUCT_UCRED_UID) || defined(HAVE_STRUCT_UCRED_CR_UID))
1399 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len)) {
1400 return result;
1401 }
1402#if defined(HAVE_STRUCT_UCRED_UID)
1403 uid = cred.uid;
1404 gid = cred.gid;
1405#else /* defined(HAVE_STRUCT_UCRED_CR_UID) */
1406 uid = cred.cr_uid;
1407 gid = cred.cr_gid;
1408#endif /* defined(HAVE_STRUCT_UCRED_UID) */
1409
1410#elif defined(HAVE_GETPEEREID)
1411 if (getpeereid(fd, &uid, &gid)) {
1412 return result;
1413 }
1414#else
1415 return result;
1416#endif
1417 con->uid = uid;
1418 con->gid = gid;
1419
1420 return result;
1421}
1422
1423/* This is the thread running the remote console on the main process. */
1424static void *netconsole(void *vconsole)
1425{
1426 struct console *con = vconsole;
1427 char hostname[MAXHOSTNAMELEN] = "";
1428 char inbuf[512];
1429 char outbuf[512];
1430 const char * const end_buf = inbuf + sizeof(inbuf);
1431 char *start_read = inbuf;
1432 int res;
1433 struct pollfd fds[2];
1434
1435 if (gethostname(hostname, sizeof(hostname)-1))
1436 ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
1437 snprintf(outbuf, sizeof(outbuf), "%s/%ld/%s\n", hostname, (long)ast_mainpid, ast_get_version());
1438 fdprint(con->fd, outbuf);
1440 for (;;) {
1441 fds[0].fd = con->fd;
1442 fds[0].events = POLLIN;
1443 fds[0].revents = 0;
1444 fds[1].fd = con->p[0];
1445 fds[1].events = POLLIN;
1446 fds[1].revents = 0;
1447
1448 res = ast_poll(fds, 2, -1);
1449 if (res < 0) {
1450 if (errno != EINTR)
1451 ast_log(LOG_WARNING, "poll returned < 0: %s\n", strerror(errno));
1452 continue;
1453 }
1454 if (fds[0].revents) {
1455 int cmds_read, bytes_read;
1456 if ((bytes_read = read_credentials(con->fd, start_read, end_buf - start_read, con)) < 1) {
1457 break;
1458 }
1459 /* XXX This will only work if it is the first command, and I'm not sure fixing it is worth the effort. */
1460 if (strncmp(inbuf, "cli quit after ", 15) == 0) {
1461 ast_cli_command_multiple_full(con->uid, con->gid, con->fd, bytes_read - 15, inbuf + 15);
1462 break;
1463 }
1464 /* ast_cli_command_multiple_full will only process individual commands terminated by a
1465 * NULL and not trailing partial commands. */
1466 if (!(cmds_read = ast_cli_command_multiple_full(con->uid, con->gid, con->fd, bytes_read + start_read - inbuf, inbuf))) {
1467 /* No commands were read. We either have a short read on the first command
1468 * with space left, or a command that is too long */
1469 if (start_read + bytes_read < end_buf) {
1470 start_read += bytes_read;
1471 } else {
1472 ast_log(LOG_ERROR, "Command too long! Skipping\n");
1473 start_read = inbuf;
1474 }
1475 continue;
1476 }
1477 if (start_read[bytes_read - 1] == '\0') {
1478 /* The read ended on a command boundary, start reading again at the head of inbuf */
1479 start_read = inbuf;
1480 continue;
1481 }
1482 /* If we get this far, we have left over characters that have not been processed.
1483 * Advance to the character after the last command read by ast_cli_command_multiple_full.
1484 * We are guaranteed to have at least cmds_read NULLs */
1485 while (cmds_read-- && (start_read = strchr(start_read, '\0'))) {
1486 start_read++;
1487 }
1488 memmove(inbuf, start_read, end_buf - start_read);
1489 start_read = end_buf - start_read + inbuf;
1490 }
1491 if (fds[1].revents) {
1492 res = read_credentials(con->p[0], outbuf, sizeof(outbuf), con);
1493 if (res < 1) {
1494 ast_log(LOG_ERROR, "read returned %d\n", res);
1495 break;
1496 }
1497 res = write(con->fd, outbuf, res);
1498 if (res < 1)
1499 break;
1500 }
1501 }
1503 if (!ast_opt_hide_connect) {
1504 ast_verb(3, "Remote UNIX connection disconnected\n");
1505 }
1506 close(con->fd);
1507 close(con->p[0]);
1508 close(con->p[1]);
1509 con->fd = -1;
1510
1511 return NULL;
1512}
1513
1514static void *listener(void *unused)
1515{
1516 struct sockaddr_un sunaddr;
1517 int s;
1518 socklen_t len;
1519 int x;
1520 int poll_result;
1521 struct pollfd fds[1];
1522
1523 for (;;) {
1524 if (ast_socket < 0) {
1525 return NULL;
1526 }
1527 fds[0].fd = ast_socket;
1528 fds[0].events = POLLIN;
1529 poll_result = ast_poll(fds, 1, -1);
1530 pthread_testcancel();
1531 if (poll_result < 0) {
1532 if (errno != EINTR) {
1533 ast_log(LOG_WARNING, "poll returned error: %s\n", strerror(errno));
1534 }
1535 continue;
1536 }
1537 len = sizeof(sunaddr);
1538 s = accept(ast_socket, (struct sockaddr *)&sunaddr, &len);
1539 if (s < 0) {
1540 if (errno != EINTR)
1541 ast_log(LOG_WARNING, "Accept returned %d: %s\n", s, strerror(errno));
1542 } else {
1543#if defined(SO_PASSCRED)
1544 int sckopt = 1;
1545 /* turn on socket credentials passing. */
1546 if (setsockopt(s, SOL_SOCKET, SO_PASSCRED, &sckopt, sizeof(sckopt)) < 0) {
1547 ast_log(LOG_WARNING, "Unable to turn on socket credentials passing\n");
1548 close(s);
1549 } else
1550#endif
1551 {
1552 for (x = 0; x < AST_MAX_CONNECTS; x++) {
1553 if (consoles[x].fd >= 0) {
1554 continue;
1555 }
1556 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, consoles[x].p)) {
1557 ast_log(LOG_ERROR, "Unable to create pipe: %s\n", strerror(errno));
1558 fdprint(s, "Server failed to create pipe\n");
1559 close(s);
1560 break;
1561 }
1562 ast_fd_set_flags(consoles[x].p[1], O_NONBLOCK);
1563 consoles[x].mute = 1; /* Default is muted, we will un-mute if necessary */
1564 /* Default uid and gid to -2, so then in cli.c/cli_has_permissions() we will be able
1565 to know if the user didn't send the credentials. */
1566 consoles[x].uid = -2;
1567 consoles[x].gid = -2;
1568 /* Server default of remote console verbosity level is OFF. */
1569 consoles[x].option_verbose = 0;
1570 consoles[x].fd = s;
1572 consoles[x].fd = -1;
1573 ast_log(LOG_ERROR, "Unable to spawn thread to handle connection: %s\n", strerror(errno));
1574 close(consoles[x].p[0]);
1575 close(consoles[x].p[1]);
1576 fdprint(s, "Server failed to spawn thread\n");
1577 close(s);
1578 }
1579 break;
1580 }
1581 if (x >= AST_MAX_CONNECTS) {
1582 fdprint(s, "No more connections allowed\n");
1583 ast_log(LOG_WARNING, "No more connections allowed\n");
1584 close(s);
1585 } else if ((consoles[x].fd > -1) && (!ast_opt_hide_connect)) {
1586 ast_verb(3, "Remote UNIX connection\n");
1587 }
1588 }
1589 }
1590 }
1591 return NULL;
1592}
1593
1594static int ast_makesocket(void)
1595{
1596 struct sockaddr_un sunaddr;
1597 int res;
1598 int x;
1599 uid_t uid = -1;
1600 gid_t gid = -1;
1601
1602 for (x = 0; x < AST_MAX_CONNECTS; x++) {
1603 consoles[x].fd = -1;
1604 }
1605
1606 if (ast_socket_is_sd) {
1608
1609 goto start_lthread;
1610 }
1611
1612 unlink(ast_config_AST_SOCKET);
1613 ast_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
1614 if (ast_socket < 0) {
1615 ast_log(LOG_WARNING, "Unable to create control socket: %s\n", strerror(errno));
1616 return -1;
1617 }
1618 memset(&sunaddr, 0, sizeof(sunaddr));
1619 sunaddr.sun_family = AF_LOCAL;
1620 ast_copy_string(sunaddr.sun_path, ast_config_AST_SOCKET, sizeof(sunaddr.sun_path));
1621 res = bind(ast_socket, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
1622 if (res) {
1623 ast_log(LOG_WARNING, "Unable to bind socket to %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1624 close(ast_socket);
1625 ast_socket = -1;
1626 return -1;
1627 }
1628 res = listen(ast_socket, 2);
1629 if (res < 0) {
1630 ast_log(LOG_WARNING, "Unable to listen on socket %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1631 close(ast_socket);
1632 ast_socket = -1;
1633 return -1;
1634 }
1635
1636start_lthread:
1638 ast_log(LOG_WARNING, "Unable to create listener thread.\n");
1639 close(ast_socket);
1640 return -1;
1641 }
1642
1643 if (ast_socket_is_sd) {
1644 /* owner/group/permissions are set by systemd, we might not even have access
1645 * to socket file so leave it alone */
1646 return 0;
1647 }
1648
1650 struct passwd *pw;
1651 if ((pw = getpwnam(ast_config_AST_CTL_OWNER)) == NULL)
1652 ast_log(LOG_WARNING, "Unable to find uid of user %s\n", ast_config_AST_CTL_OWNER);
1653 else
1654 uid = pw->pw_uid;
1655 }
1656
1658 struct group *grp;
1659 if ((grp = getgrnam(ast_config_AST_CTL_GROUP)) == NULL)
1660 ast_log(LOG_WARNING, "Unable to find gid of group %s\n", ast_config_AST_CTL_GROUP);
1661 else
1662 gid = grp->gr_gid;
1663 }
1664
1665 if (chown(ast_config_AST_SOCKET, uid, gid) < 0)
1666 ast_log(LOG_WARNING, "Unable to change ownership of %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1667
1669 unsigned int p1;
1670 mode_t p;
1671 sscanf(ast_config_AST_CTL_PERMISSIONS, "%30o", &p1);
1672 p = p1;
1673 if ((chmod(ast_config_AST_SOCKET, p)) < 0)
1674 ast_log(LOG_WARNING, "Unable to change file permissions of %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1675 }
1676
1677 return 0;
1678}
1679
1680static int ast_tryconnect(void)
1681{
1682 struct sockaddr_un sunaddr;
1683 int res;
1684 ast_consock = socket(PF_LOCAL, SOCK_STREAM, 0);
1685 if (ast_consock < 0) {
1686 fprintf(stderr, "Unable to create socket: %s\n", strerror(errno));
1687 return 0;
1688 }
1689 memset(&sunaddr, 0, sizeof(sunaddr));
1690 sunaddr.sun_family = AF_LOCAL;
1691 ast_copy_string(sunaddr.sun_path, ast_config_AST_SOCKET, sizeof(sunaddr.sun_path));
1692 res = connect(ast_consock, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
1693 if (res) {
1694 close(ast_consock);
1695 ast_consock = -1;
1696 return 0;
1697 } else
1698 return 1;
1699}
1700
1701/*! \brief Urgent handler
1702 *
1703 * Called by soft_hangup to interrupt the poll, read, or other
1704 * system call. We don't actually need to do anything though.
1705 * Remember: Cannot EVER ast_log from within a signal handler
1706 */
1707static void _urg_handler(int num)
1708{
1709 return;
1710}
1711
1712static struct sigaction urg_handler = {
1713 .sa_handler = _urg_handler,
1714};
1715
1716static void _hup_handler(int num)
1717{
1718 int save_errno = errno;
1719
1720 if (restartnow) {
1721 if (el) {
1722 el_end(el);
1723 }
1724 execvp(_argv[0], _argv);
1725 }
1726
1727 printf("Received HUP signal -- Reloading configs\n");
1728 sig_flags.need_reload = 1;
1730 fprintf(stderr, "hup_handler: write() failed: %s\n", strerror(errno));
1731 }
1732 errno = save_errno;
1733}
1734
1735static struct sigaction hup_handler = {
1736 .sa_handler = _hup_handler,
1737 .sa_flags = SA_RESTART,
1738};
1739
1740static void _child_handler(int sig)
1741{
1742 /* Must not ever ast_log or ast_verbose within signal handler */
1743 int n, status, save_errno = errno;
1744
1745 /*
1746 * Reap all dead children -- not just one
1747 */
1748 for (n = 0; waitpid(-1, &status, WNOHANG) > 0; n++)
1749 ;
1750 if (n == 0 && option_debug)
1751 printf("Huh? Child handler, but nobody there?\n");
1752 errno = save_errno;
1753}
1754
1755static struct sigaction child_handler = {
1756 .sa_handler = _child_handler,
1757 .sa_flags = SA_RESTART,
1758};
1759
1760/*! \brief Set an X-term or screen title */
1761static void set_title(char *text)
1762{
1763 if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
1764 fprintf(stdout, "\033]2;%s\007", text);
1765}
1766
1767static void set_icon(char *text)
1768{
1769 if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
1770 fprintf(stdout, "\033]1;%s\007", text);
1771}
1772
1773/*! \brief Check whether we were set to high(er) priority. */
1774static int has_priority(void)
1775{
1776 /* Neither of these calls should fail with these arguments. */
1777#ifdef __linux__
1778 /* For SCHED_OTHER, SCHED_BATCH and SCHED_IDLE, this will return
1779 * 0. For the realtime priorities SCHED_RR and SCHED_FIFO, it
1780 * will return something >= 1. */
1781 return sched_getscheduler(0);
1782#else
1783 /* getpriority() can return a value in -20..19 (or even -INF..20)
1784 * where negative numbers are high priority. We don't bother
1785 * checking errno. If the query fails and it returns -1, we'll
1786 * assume that we're running at high prio; a safe assumption
1787 * that will enable the resource starvation monitor (canary)
1788 * just in case. */
1789 return (getpriority(PRIO_PROCESS, 0) < 0);
1790#endif
1791}
1792
1793/*! \brief Set priority on all known threads. */
1794static int set_priority_all(int pri)
1795{
1796#if !defined(__linux__)
1797 /* The non-linux version updates the entire process prio. */
1798 return ast_set_priority(pri);
1799#elif defined(LOW_MEMORY)
1800 ast_log(LOG_WARNING, "Unable to enumerate all threads to update priority\n");
1801 return ast_set_priority(pri);
1802#else
1803 struct thread_list_t *cur;
1804 struct sched_param sched;
1805 char const *policy_str;
1806 int policy;
1807
1808 memset(&sched, 0, sizeof(sched));
1809 if (pri) {
1810 policy = SCHED_RR;
1811 policy_str = "realtime";
1812 sched.sched_priority = 10;
1813 } else {
1814 policy = SCHED_OTHER;
1815 policy_str = "regular";
1816 sched.sched_priority = 0;
1817 }
1818 if (sched_setscheduler(getpid(), policy, &sched)) {
1819 ast_log(LOG_WARNING, "Unable to set %s thread priority on main thread\n", policy_str);
1820 return -1;
1821 }
1822 ast_verb(1, "Setting %s thread priority on all threads\n", policy_str);
1824 AST_RWLIST_TRAVERSE(&thread_list, cur, list) {
1825 /* Don't care about the return value. It should work. */
1826 sched_setscheduler(cur->lwp, policy, &sched);
1827 }
1829 return 0;
1830#endif
1831}
1832
1833/*! \brief We set ourselves to a high priority, that we might pre-empt
1834 * everything else. If your PBX has heavy activity on it, this is a
1835 * good thing.
1836 */
1838{
1839 struct sched_param sched;
1840 memset(&sched, 0, sizeof(sched));
1841#ifdef __linux__
1842 if (pri) {
1843 sched.sched_priority = 10;
1844 if (sched_setscheduler(0, SCHED_RR, &sched)) {
1845 return -1;
1846 }
1847 } else {
1848 sched.sched_priority = 0;
1849 /* According to the manpage, these parameters can never fail. */
1850 sched_setscheduler(0, SCHED_OTHER, &sched);
1851 }
1852#else
1853 if (pri) {
1854 if (setpriority(PRIO_PROCESS, 0, -10) == -1) {
1855 ast_log(LOG_WARNING, "Unable to set high priority\n");
1856 return -1;
1857 } else
1858 ast_verb(1, "Set to high priority\n");
1859 } else {
1860 /* According to the manpage, these parameters can never fail. */
1861 setpriority(PRIO_PROCESS, 0, 0);
1862 }
1863#endif
1864 return 0;
1865}
1866
1868{
1870}
1871
1873{
1874 return shutdown_pending;
1875}
1876
1878{
1879 int shutdown_aborted = 0;
1880
1882 if (shuttingdown >= SHUTDOWN_FAST) {
1884 shutdown_pending = 0;
1885 shutdown_aborted = 1;
1886 }
1888 return shutdown_aborted;
1889}
1890
1891/*!
1892 * \internal
1893 * \brief Initiate system shutdown -- prevents new channels from being allocated.
1894 */
1895static void ast_begin_shutdown(void)
1896{
1899 shutdown_pending = 1;
1900 }
1902}
1903
1904static int can_safely_quit(shutdown_nice_t niceness, int restart);
1905static void really_quit(int num, shutdown_nice_t niceness, int restart);
1906
1907static void quit_handler(int num, shutdown_nice_t niceness, int restart)
1908{
1909 if (can_safely_quit(niceness, restart)) {
1910 really_quit(num, niceness, restart);
1911 /* No one gets here. */
1912 }
1913 /* It wasn't our time. */
1914}
1915
1916#define SHUTDOWN_TIMEOUT 15 /* Seconds */
1917
1918/*!
1919 * \internal
1920 * \brief Wait for all channels to die, a timeout, or shutdown cancelled.
1921 * \since 13.3.0
1922 *
1923 * \param niceness Shutdown niceness in effect
1924 * \param seconds Number of seconds to wait or less than zero if indefinitely.
1925 *
1926 * \retval zero if waiting wasn't necessary. We were idle.
1927 * \retval non-zero if we had to wait.
1928 */
1929static int wait_for_channels_to_die(shutdown_nice_t niceness, int seconds)
1930{
1931 time_t start;
1932 time_t now;
1933 int waited = 0;
1934
1935 time(&start);
1936 for (;;) {
1937 if (!ast_undestroyed_channels() || shuttingdown != niceness) {
1938 break;
1939 }
1940 if (seconds < 0) {
1941 /* No timeout so just poll every second */
1942 sleep(1);
1943 } else {
1944 time(&now);
1945
1946 /* Wait up to the given seconds for all channels to go away */
1947 if (seconds < (now - start)) {
1948 break;
1949 }
1950
1951 /* Sleep 1/10 of a second */
1952 usleep(100000);
1953 }
1954 waited = 1;
1955 }
1956 return waited;
1957}
1958
1959static int can_safely_quit(shutdown_nice_t niceness, int restart)
1960{
1961 int waited = 0;
1962
1963 /* Check if someone else isn't already doing this. */
1965 if (shuttingdown != NOT_SHUTTING_DOWN && niceness >= shuttingdown) {
1966 /* Already in progress and other request was less nice. */
1968 ast_verbose("Ignoring asterisk %s request, already in progress.\n", restart ? "restart" : "shutdown");
1969 return 0;
1970 }
1971 shuttingdown = niceness;
1973
1974 /* Try to get as many CDRs as possible submitted to the backend engines
1975 * (if in batch mode). really_quit happens to call it again when running
1976 * the atexit handlers, otherwise this would be a bit early. */
1978
1979 /*
1980 * Shutdown the message queue for the technology agnostic message channel.
1981 * This has to occur before we pause shutdown pending ast_undestroyed_channels.
1982 *
1983 * XXX This is not reversed on shutdown cancel.
1984 */
1986
1987 if (niceness == SHUTDOWN_NORMAL) {
1988 /* Begin shutdown routine, hanging up active channels */
1990 if (ast_opt_console) {
1991 ast_verb(0, "Beginning asterisk %s....\n", restart ? "restart" : "shutdown");
1992 }
1994 waited |= wait_for_channels_to_die(niceness, SHUTDOWN_TIMEOUT);
1995 } else if (niceness >= SHUTDOWN_NICE) {
1996 if (niceness != SHUTDOWN_REALLY_NICE) {
1998 }
1999 if (ast_opt_console) {
2000 ast_verb(0, "Waiting for inactivity to perform %s...\n", restart ? "restart" : "halt");
2001 }
2002 waited |= wait_for_channels_to_die(niceness, -1);
2003 }
2004
2005 /* Re-acquire lock and check if someone changed the niceness, in which
2006 * case someone else has taken over the shutdown.
2007 */
2009 if (shuttingdown != niceness) {
2011 ast_verb(0, "Asterisk %s cancelled.\n", restart ? "restart" : "shutdown");
2012 }
2014 return 0;
2015 }
2016
2017 if (niceness >= SHUTDOWN_REALLY_NICE) {
2020
2021 /* No more Mr. Nice guy. We are committed to shutting down now. */
2025
2027 }
2030
2031 if (niceness >= SHUTDOWN_NORMAL && waited) {
2032 /*
2033 * We were not idle. Give things in progress a chance to
2034 * recognize the final shutdown phase.
2035 */
2036 sleep(1);
2037 }
2038 return 1;
2039}
2040
2041/*! Called when exiting is certain. */
2042static void really_quit(int num, shutdown_nice_t niceness, int restart)
2043{
2044 int active_channels;
2045 struct ast_json *json_object = NULL;
2046 int run_cleanups = niceness >= SHUTDOWN_NICE;
2047
2048 if (run_cleanups && modules_shutdown()) {
2049 ast_verb(0, "Some modules could not be unloaded, switching to fast shutdown\n");
2050 run_cleanups = 0;
2051 }
2052
2053 if (!restart) {
2054 ast_sd_notify("STOPPING=1");
2055 }
2058 if (consolethread == AST_PTHREADT_NULL || consolethread == pthread_self()) {
2059 /* Only end if we are the consolethread, otherwise there's a race with that thread. */
2060 if (el != NULL) {
2061 el_end(el);
2062 }
2063 if (el_hist != NULL) {
2064 history_end(el_hist);
2065 }
2066 } else if (!restart) {
2067 sig_flags.need_el_end = 1;
2068 pthread_kill(consolethread, SIGURG);
2069 }
2070 }
2071 active_channels = ast_active_channels();
2072 /* Don't publish messages if we're a remote console - we won't have all of the Stasis
2073 * topics or message types
2074 */
2075 if (!ast_opt_remote) {
2076 json_object = ast_json_pack("{s: s, s: s}",
2077 "Shutdown", active_channels ? "Uncleanly" : "Cleanly",
2078 "Restart", restart ? "True" : "False");
2079 ast_manager_publish_event("Shutdown", EVENT_FLAG_SYSTEM, json_object);
2080 ast_json_unref(json_object);
2081 json_object = NULL;
2082 }
2083 ast_verb(0, "Asterisk %s ending (%d).\n",
2084 active_channels ? "uncleanly" : "cleanly", num);
2085
2086 ast_verb(0, "Executing last minute cleanups\n");
2087 ast_run_atexits(run_cleanups);
2088
2089 ast_debug(1, "Asterisk ending (%d).\n", num);
2090 if (ast_socket > -1) {
2091 pthread_cancel(lthread);
2092 close(ast_socket);
2093 ast_socket = -1;
2094 if (!ast_socket_is_sd) {
2095 unlink(ast_config_AST_SOCKET);
2096 }
2097 pthread_kill(lthread, SIGURG);
2098 pthread_join(lthread, NULL);
2099 }
2100 if (ast_consock > -1)
2101 close(ast_consock);
2102 if (!ast_opt_remote)
2103 unlink(ast_config_AST_PID);
2105 printf("%s", term_quit());
2106 if (restart) {
2107 int i;
2108 ast_verb(0, "Preparing for Asterisk restart...\n");
2109 /* Mark all FD's for closing on exec */
2110 for (i = 3; i < 32768; i++) {
2111 fcntl(i, F_SETFD, FD_CLOEXEC);
2112 }
2113 ast_verb(0, "Asterisk is now restarting...\n");
2114 restartnow = 1;
2115
2116 /* close logger */
2117 close_logger();
2119
2120 /* If there is a consolethread running send it a SIGHUP
2121 so it can execvp, otherwise we can do it ourselves */
2122 if ((consolethread != AST_PTHREADT_NULL) && (consolethread != pthread_self())) {
2123 pthread_kill(consolethread, SIGHUP);
2124 /* Give the signal handler some time to complete */
2125 sleep(2);
2126 } else
2127 execvp(_argv[0], _argv);
2128
2129 } else {
2130 /* close logger */
2131 close_logger();
2133 }
2134
2135 exit(0);
2136}
2137
2138static void __quit_handler(int num)
2139{
2140 sig_flags.need_quit = 1;
2142 fprintf(stderr, "quit_handler: write() failed: %s\n", strerror(errno));
2143 }
2144 /* There is no need to restore the signal handler here, since the app
2145 * is going to exit */
2146}
2147
2148static void __remote_quit_handler(int num)
2149{
2150 sig_flags.need_quit = 1;
2151}
2152
2153static void set_header(char *outbuf, int maxout, char level)
2154{
2155 const char *cmp;
2156 char date[40];
2157
2158 switch (level) {
2159 case 0: cmp = NULL;
2160 break;
2161 case 1: cmp = VERBOSE_PREFIX_1;
2162 break;
2163 case 2: cmp = VERBOSE_PREFIX_2;
2164 break;
2165 case 3: cmp = VERBOSE_PREFIX_3;
2166 break;
2167 default: cmp = VERBOSE_PREFIX_4;
2168 break;
2169 }
2170
2171 if (ast_opt_timestamp) {
2172 struct ast_tm tm;
2173 struct timeval now = ast_tvnow();
2174 ast_localtime(&now, &tm, NULL);
2175 ast_strftime(date, sizeof(date), ast_logger_get_dateformat(), &tm);
2176 }
2177
2178 snprintf(outbuf, maxout, "%s%s%s%s%s%s",
2179 ast_opt_timestamp ? "[" : "",
2180 ast_opt_timestamp ? date : "",
2181 ast_opt_timestamp ? "] " : "",
2182 cmp ? ast_term_color(COLOR_GRAY, 0) : "",
2183 cmp ? cmp : "",
2184 cmp ? ast_term_reset() : "");
2185}
2186
2189};
2190
2191static int console_state_init(void *ptr)
2192{
2193 struct console_state_data *state = ptr;
2194 state->verbose_line_level = 0;
2195 return 0;
2196}
2197
2199
2200static int console_print(const char *s)
2201{
2202 struct console_state_data *state =
2204
2205 char prefix[80];
2206 const char *c;
2207 int num, res = 0;
2208 unsigned int newline;
2209
2210 do {
2211 if (VERBOSE_HASMAGIC(s)) {
2212
2213 /* always use the given line's level, otherwise
2214 we'll use the last line's level */
2215 state->verbose_line_level = VERBOSE_MAGIC2LEVEL(s);
2216
2217 /* move past magic */
2218 s++;
2219
2220 set_header(prefix, sizeof(prefix), state->verbose_line_level);
2221 } else {
2222 *prefix = '\0';
2223 }
2224 c = s;
2225
2226 /* for a given line separate on verbose magic, newline, and eol */
2227 if ((s = strchr(c, '\n'))) {
2228 ++s;
2229 newline = 1;
2230 } else {
2231 s = strchr(c, '\0');
2232 newline = 0;
2233 }
2234
2235 /* check if we should write this line after calculating begin/end
2236 so we process the case of a higher level line embedded within
2237 two lower level lines */
2238 if (state->verbose_line_level > option_verbose) {
2239 continue;
2240 }
2241
2242 if (!ast_strlen_zero(prefix)) {
2243 fputs(prefix, stdout);
2244 }
2245
2246 num = s - c;
2247 if (fwrite(c, sizeof(char), num, stdout) < num) {
2248 break;
2249 }
2250
2251 if (!res) {
2252 /* if at least some info has been written
2253 we'll want to return true */
2254 res = 1;
2255 }
2256 } while (*s);
2257
2258 if (newline) {
2259 /* if ending on a newline then reset last level to zero
2260 since what follows may be not be logging output */
2261 state->verbose_line_level = 0;
2262 }
2263
2264 if (res) {
2265 fflush(stdout);
2266 }
2267
2268 return res;
2269}
2270
2271static int ast_all_zeros(const char *s)
2272{
2273 while (*s) {
2274 if (*s > 32)
2275 return 0;
2276 s++;
2277 }
2278 return 1;
2279}
2280
2281/* This is the main console CLI command handler. Run by the main() thread. */
2282static void consolehandler(const char *s)
2283{
2284 printf("%s", term_end());
2285 fflush(stdout);
2286
2287 /* Called when readline data is available */
2288 if (!ast_all_zeros(s))
2290 /* The real handler for bang */
2291 if (s[0] == '!') {
2292 if (s[1])
2293 ast_safe_system(s+1);
2294 else
2295 ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
2296 } else
2297 ast_cli_command(STDOUT_FILENO, s);
2298}
2299
2300static int remoteconsolehandler(const char *s)
2301{
2302 int ret = 0;
2303
2304 /* Called when readline data is available */
2305 if (!ast_all_zeros(s))
2307
2308 while (isspace(*s)) {
2309 s++;
2310 }
2311
2312 /* The real handler for bang */
2313 if (s[0] == '!') {
2314 if (s[1])
2315 ast_safe_system(s+1);
2316 else
2317 ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
2318 ret = 1;
2319 } else if ((strncasecmp(s, "quit", 4) == 0 || strncasecmp(s, "exit", 4) == 0) &&
2320 (s[4] == '\0' || isspace(s[4]))) {
2322 ret = 1;
2323 }
2324
2325 return ret;
2326}
2327
2328static char *handle_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2329{
2330 switch (cmd) {
2331 case CLI_INIT:
2332 e->command = "core show version";
2333 e->usage =
2334 "Usage: core show version\n"
2335 " Shows Asterisk version information.\n";
2336 return NULL;
2337 case CLI_GENERATE:
2338 return NULL;
2339 }
2340
2341 if (a->argc != 3)
2342 return CLI_SHOWUSAGE;
2343 ast_cli(a->fd, "Asterisk %s built by %s @ %s on a %s running %s on %s\n",
2346 return CLI_SUCCESS;
2347}
2348
2349static char *handle_stop_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2350{
2351 switch (cmd) {
2352 case CLI_INIT:
2353 e->command = "core stop now";
2354 e->usage =
2355 "Usage: core stop now\n"
2356 " Shuts down a running Asterisk immediately, hanging up all active calls .\n";
2358 return NULL;
2359 case CLI_GENERATE:
2360 return NULL;
2361 }
2362
2363 if (a->argc != e->args)
2364 return CLI_SHOWUSAGE;
2365 quit_handler(0, SHUTDOWN_NORMAL, 0 /* not restart */);
2366 return CLI_SUCCESS;
2367}
2368
2369static char *handle_stop_gracefully(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2370{
2371 switch (cmd) {
2372 case CLI_INIT:
2373 e->command = "core stop gracefully";
2374 e->usage =
2375 "Usage: core stop gracefully\n"
2376 " Causes Asterisk to not accept new calls, and exit when all\n"
2377 " active calls have terminated normally.\n";
2379 return NULL;
2380 case CLI_GENERATE:
2381 return NULL;
2382 }
2383
2384 if (a->argc != e->args)
2385 return CLI_SHOWUSAGE;
2386 quit_handler(0, SHUTDOWN_NICE, 0 /* no restart */);
2387 return CLI_SUCCESS;
2388}
2389
2390static char *handle_stop_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2391{
2392 switch (cmd) {
2393 case CLI_INIT:
2394 e->command = "core stop when convenient";
2395 e->usage =
2396 "Usage: core stop when convenient\n"
2397 " Causes Asterisk to perform a shutdown when all active calls have ended.\n";
2399 return NULL;
2400 case CLI_GENERATE:
2401 return NULL;
2402 }
2403
2404 if (a->argc != e->args)
2405 return CLI_SHOWUSAGE;
2406 ast_cli(a->fd, "Waiting for inactivity to perform halt\n");
2407 quit_handler(0, SHUTDOWN_REALLY_NICE, 0 /* don't restart */);
2408 return CLI_SUCCESS;
2409}
2410
2411static char *handle_restart_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2412{
2413 switch (cmd) {
2414 case CLI_INIT:
2415 e->command = "core restart now";
2416 e->usage =
2417 "Usage: core restart now\n"
2418 " Causes Asterisk to hangup all calls and exec() itself performing a cold\n"
2419 " restart.\n";
2421 return NULL;
2422 case CLI_GENERATE:
2423 return NULL;
2424 }
2425
2426 if (a->argc != e->args)
2427 return CLI_SHOWUSAGE;
2428 quit_handler(0, SHUTDOWN_NORMAL, 1 /* restart */);
2429 return CLI_SUCCESS;
2430}
2431
2432static char *handle_restart_gracefully(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2433{
2434 switch (cmd) {
2435 case CLI_INIT:
2436 e->command = "core restart gracefully";
2437 e->usage =
2438 "Usage: core restart gracefully\n"
2439 " Causes Asterisk to stop accepting new calls and exec() itself performing a cold\n"
2440 " restart when all active calls have ended.\n";
2442 return NULL;
2443 case CLI_GENERATE:
2444 return NULL;
2445 }
2446
2447 if (a->argc != e->args)
2448 return CLI_SHOWUSAGE;
2449 quit_handler(0, SHUTDOWN_NICE, 1 /* restart */);
2450 return CLI_SUCCESS;
2451}
2452
2453static char *handle_restart_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2454{
2455 switch (cmd) {
2456 case CLI_INIT:
2457 e->command = "core restart when convenient";
2458 e->usage =
2459 "Usage: core restart when convenient\n"
2460 " Causes Asterisk to perform a cold restart when all active calls have ended.\n";
2462 return NULL;
2463 case CLI_GENERATE:
2464 return NULL;
2465 }
2466
2467 if (a->argc != e->args)
2468 return CLI_SHOWUSAGE;
2469 ast_cli(a->fd, "Waiting for inactivity to perform restart\n");
2470 quit_handler(0, SHUTDOWN_REALLY_NICE, 1 /* restart */);
2471 return CLI_SUCCESS;
2472}
2473
2474static char *handle_abort_shutdown(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2475{
2476 switch (cmd) {
2477 case CLI_INIT:
2478 e->command = "core abort shutdown";
2479 e->usage =
2480 "Usage: core abort shutdown\n"
2481 " Causes Asterisk to abort an executing shutdown or restart, and resume normal\n"
2482 " call operations.\n";
2484 return NULL;
2485 case CLI_GENERATE:
2486 return NULL;
2487 }
2488
2489 if (a->argc != e->args)
2490 return CLI_SHOWUSAGE;
2491
2493
2494 return CLI_SUCCESS;
2495}
2496
2497static char *handle_bang(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2498{
2499 switch (cmd) {
2500 case CLI_INIT:
2501 e->command = "!";
2502 e->usage =
2503 "Usage: !<command>\n"
2504 " Executes a given shell command\n";
2505 return NULL;
2506 case CLI_GENERATE:
2507 return NULL;
2508 }
2509
2510 return CLI_SUCCESS;
2511}
2512static const char warranty_lines[] = {
2513 "\n"
2514 " NO WARRANTY\n"
2515 "\n"
2516 "BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n"
2517 "FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN\n"
2518 "OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n"
2519 "PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n"
2520 "OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n"
2521 "MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS\n"
2522 "TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE\n"
2523 "PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n"
2524 "REPAIR OR CORRECTION.\n"
2525 "\n"
2526 "IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n"
2527 "WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n"
2528 "REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n"
2529 "INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n"
2530 "OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n"
2531 "TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n"
2532 "YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n"
2533 "PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n"
2534 "POSSIBILITY OF SUCH DAMAGES.\n"
2535};
2536
2537static char *show_warranty(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2538{
2539 switch (cmd) {
2540 case CLI_INIT:
2541 e->command = "core show warranty";
2542 e->usage =
2543 "Usage: core show warranty\n"
2544 " Shows the warranty (if any) for this copy of Asterisk.\n";
2545 return NULL;
2546 case CLI_GENERATE:
2547 return NULL;
2548 }
2549
2550 ast_cli(a->fd, "%s", warranty_lines);
2551
2552 return CLI_SUCCESS;
2553}
2554
2555static const char license_lines[] = {
2556 "\n"
2557 "This program is free software; you can redistribute it and/or modify\n"
2558 "it under the terms of the GNU General Public License version 2 as\n"
2559 "published by the Free Software Foundation.\n"
2560 "\n"
2561 "This program also contains components licensed under other licenses.\n"
2562 "They include:\n"
2563 "\n"
2564 "This program is distributed in the hope that it will be useful,\n"
2565 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
2566 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
2567 "GNU General Public License for more details.\n"
2568 "\n"
2569 "You should have received a copy of the GNU General Public License\n"
2570 "along with this program; if not, write to the Free Software\n"
2571 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
2572};
2573
2574static char *show_license(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2575{
2576 switch (cmd) {
2577 case CLI_INIT:
2578 e->command = "core show license";
2579 e->usage =
2580 "Usage: core show license\n"
2581 " Shows the license(s) for this copy of Asterisk.\n";
2582 return NULL;
2583 case CLI_GENERATE:
2584 return NULL;
2585 }
2586
2587 ast_cli(a->fd, "%s", license_lines);
2588
2589 return CLI_SUCCESS;
2590}
2591
2592#define ASTERISK_PROMPT "*CLI> "
2593
2594/*!
2595 * \brief Shutdown Asterisk CLI commands.
2596 *
2597 * \note These CLI commands cannot be unregistered at shutdown
2598 * because one of them is likely the reason for the shutdown.
2599 * The CLI generates a warning if a command is in-use when it is
2600 * unregistered.
2601 */
2603 AST_CLI_DEFINE(handle_stop_now, "Shut down Asterisk immediately"),
2604 AST_CLI_DEFINE(handle_stop_gracefully, "Gracefully shut down Asterisk"),
2605 AST_CLI_DEFINE(handle_stop_when_convenient, "Shut down Asterisk at empty call volume"),
2606 AST_CLI_DEFINE(handle_restart_now, "Restart Asterisk immediately"),
2607 AST_CLI_DEFINE(handle_restart_gracefully, "Restart Asterisk gracefully"),
2608 AST_CLI_DEFINE(handle_restart_when_convenient, "Restart Asterisk at empty call volume"),
2609};
2610
2611static struct ast_cli_entry cli_asterisk[] = {
2612 AST_CLI_DEFINE(handle_abort_shutdown, "Cancel a running shutdown"),
2613 AST_CLI_DEFINE(show_warranty, "Show the warranty (if any) for this copy of Asterisk"),
2614 AST_CLI_DEFINE(show_license, "Show the license(s) for this copy of Asterisk"),
2615 AST_CLI_DEFINE(handle_version, "Display version info"),
2616 AST_CLI_DEFINE(handle_bang, "Execute a shell command"),
2617#if !defined(LOW_MEMORY)
2618 AST_CLI_DEFINE(handle_show_threads, "Show running threads"),
2619#if defined(HAVE_SYSINFO) || defined(HAVE_SYSCTL)
2620 AST_CLI_DEFINE(handle_show_sysinfo, "Show System Information"),
2621#endif
2622 AST_CLI_DEFINE(handle_show_profile, "Display profiling info"),
2623 AST_CLI_DEFINE(handle_show_settings, "Show some core settings"),
2624 AST_CLI_DEFINE(handle_clear_profile, "Clear profiling info"),
2625#endif /* ! LOW_MEMORY */
2626};
2627
2629{
2630 char buf[80];
2631
2632 /*
2633 * Tell the server asterisk instance about the verbose level
2634 * initially desired.
2635 */
2636 if (option_verbose) {
2637 snprintf(buf, sizeof(buf), "core set verbose atleast %d silent", option_verbose);
2639 }
2640
2641 if (option_debug) {
2642 snprintf(buf, sizeof(buf), "core set debug atleast %d", option_debug);
2644 }
2645
2646 /* Leave verbose filtering to the server. */
2647 option_verbose = INT_MAX;
2648
2649 if (!ast_opt_mute) {
2650 fdsend(ast_consock, "logger mute silent");
2651 } else {
2652 printf("log and verbose output currently muted ('logger mute' to unmute)\n");
2653 }
2654}
2655
2656#ifdef HAVE_LIBEDIT_IS_UNICODE
2657#define CHAR_T_LIBEDIT wchar_t
2658#define CHAR_TO_LIBEDIT(c) btowc(c)
2659#else
2660#define CHAR_T_LIBEDIT char
2661#define CHAR_TO_LIBEDIT(c) c
2662#endif
2663
2664static int ast_el_read_char(EditLine *editline, CHAR_T_LIBEDIT *cp)
2665{
2666 int num_read = 0;
2667 int lastpos = 0;
2668 struct pollfd fds[2];
2669 int res;
2670 int max;
2671#define EL_BUF_SIZE 512
2672 char buf[EL_BUF_SIZE];
2673
2674 for (;;) {
2675 max = 1;
2676 fds[0].fd = ast_consock;
2677 fds[0].events = POLLIN;
2678 if (!ast_opt_exec) {
2679 fds[1].fd = STDIN_FILENO;
2680 fds[1].events = POLLIN;
2681 max++;
2682 }
2683 res = ast_poll(fds, max, -1);
2684 if (res < 0) {
2685 if (sig_flags.need_quit || sig_flags.need_quit_handler || sig_flags.need_el_end) {
2686 break;
2687 }
2688 if (errno == EINTR) {
2689 continue;
2690 }
2691 fprintf(stderr, "poll failed: %s\n", strerror(errno));
2692 break;
2693 }
2694
2695 if (!ast_opt_exec && fds[1].revents) {
2696 char c = '\0';
2697
2698 num_read = read(STDIN_FILENO, &c, 1);
2699 if (num_read < 1) {
2700 break;
2701 }
2702
2703 *cp = CHAR_TO_LIBEDIT(c);
2704
2705 return num_read;
2706 }
2707
2708 if (fds[0].revents) {
2709 res = read(ast_consock, buf, sizeof(buf) - 1);
2710 /* if the remote side disappears exit */
2711 if (res < 1) {
2712 fprintf(stderr, "\nDisconnected from Asterisk server\n");
2713 if (!ast_opt_reconnect) {
2715 } else {
2716 int tries;
2717 int reconnects_per_second = 20;
2718
2719 fprintf(stderr, "Attempting to reconnect for 30 seconds\n");
2720 for (tries = 0; tries < 30 * reconnects_per_second; tries++) {
2721 if (ast_tryconnect()) {
2722 fprintf(stderr, "Reconnect succeeded after %.3f seconds\n", 1.0 / reconnects_per_second * tries);
2723 printf("%s", term_quit());
2726 break;
2727 }
2728
2729 usleep(1000000 / reconnects_per_second);
2730 }
2731 if (tries >= 30 * reconnects_per_second) {
2732 fprintf(stderr, "Failed to reconnect for 30 seconds. Quitting.\n");
2734 }
2735 }
2736 continue;
2737 }
2738
2739 buf[res] = '\0';
2740
2741 /* Write over the CLI prompt */
2742 if (!ast_opt_exec && !lastpos) {
2743 if (write(STDOUT_FILENO, "\r␛[0K", 5) < 0) {
2744 }
2745 }
2746
2748
2749 if ((res < EL_BUF_SIZE - 1) && ((buf[res-1] == '\n') || (res >= 2 && buf[res-2] == '\n'))) {
2750 *cp = CHAR_TO_LIBEDIT(CC_REFRESH);
2751
2752 return 1;
2753 }
2754 lastpos = 1;
2755 }
2756 }
2757
2758 *cp = CHAR_TO_LIBEDIT('\0');
2759
2760 return 0;
2761}
2762
2763static struct ast_str *prompt = NULL;
2764
2765static char *cli_prompt(EditLine *editline)
2766{
2767 char tmp[100];
2768 char *pfmt;
2769 int color_used = 0;
2770 static int cli_prompt_changes = 0;
2771 struct passwd *pw;
2772 struct group *gr;
2773
2774 if (prompt == NULL) {
2775 prompt = ast_str_create(100);
2776 } else if (!cli_prompt_changes) {
2777 return ast_str_buffer(prompt);
2778 } else {
2780 }
2781
2782 if ((pfmt = getenv("ASTERISK_PROMPT"))) {
2783 char *t = pfmt;
2784 struct timeval ts = ast_tvnow();
2785 while (*t != '\0') {
2786 if (*t == '%') {
2787 char hostname[MAXHOSTNAMELEN] = "";
2788 int i, which;
2789 struct ast_tm tm = { 0, };
2790 int fgcolor = COLOR_WHITE, bgcolor = COLOR_BLACK;
2791
2792 t++;
2793 switch (*t) {
2794 case 'C': /* color */
2795 t++;
2796 if (sscanf(t, "%30d;%30d%n", &fgcolor, &bgcolor, &i) == 2) {
2797 ast_term_color_code(&prompt, fgcolor, bgcolor);
2798 t += i - 1;
2799 } else if (sscanf(t, "%30d%n", &fgcolor, &i) == 1) {
2800 ast_term_color_code(&prompt, fgcolor, 0);
2801 t += i - 1;
2802 }
2803
2804 /* If the color has been reset correctly, then there's no need to reset it later */
2805 color_used = ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) ? 0 : 1;
2806 break;
2807 case 'd': /* date */
2808 if (ast_localtime(&ts, &tm, NULL)) {
2809 ast_strftime(tmp, sizeof(tmp), "%Y-%m-%d", &tm);
2810 ast_str_append(&prompt, 0, "%s", tmp);
2811 cli_prompt_changes++;
2812 }
2813 break;
2814 case 'g': /* group */
2815 if ((gr = getgrgid(getgid()))) {
2816 ast_str_append(&prompt, 0, "%s", gr->gr_name);
2817 }
2818 break;
2819 case 'h': /* hostname */
2820 if (!gethostname(hostname, sizeof(hostname) - 1)) {
2821 ast_str_append(&prompt, 0, "%s", hostname);
2822 } else {
2823 ast_str_append(&prompt, 0, "%s", "localhost");
2824 }
2825 break;
2826 case 'H': /* short hostname */
2827 if (!gethostname(hostname, sizeof(hostname) - 1)) {
2828 char *dotptr;
2829 if ((dotptr = strchr(hostname, '.'))) {
2830 *dotptr = '\0';
2831 }
2832 ast_str_append(&prompt, 0, "%s", hostname);
2833 } else {
2834 ast_str_append(&prompt, 0, "%s", "localhost");
2835 }
2836 break;
2837#ifdef HAVE_GETLOADAVG
2838 case 'l': /* load avg */
2839 t++;
2840 if (sscanf(t, "%30d", &which) == 1 && which > 0 && which <= 3) {
2841 double list[3];
2842 getloadavg(list, 3);
2843 ast_str_append(&prompt, 0, "%.2f", list[which - 1]);
2844 cli_prompt_changes++;
2845 }
2846 break;
2847#endif
2848 case 's': /* Asterisk system name (from asterisk.conf) */
2850 break;
2851 case 't': /* time */
2852 if (ast_localtime(&ts, &tm, NULL)) {
2853 ast_strftime(tmp, sizeof(tmp), "%H:%M:%S", &tm);
2854 ast_str_append(&prompt, 0, "%s", tmp);
2855 cli_prompt_changes++;
2856 }
2857 break;
2858 case 'u': /* username */
2859 if ((pw = getpwuid(getuid()))) {
2860 ast_str_append(&prompt, 0, "%s", pw->pw_name);
2861 }
2862 break;
2863 case '#': /* process console or remote? */
2864 ast_str_append(&prompt, 0, "%c", ast_opt_remote ? '>' : '#');
2865 break;
2866 case '%': /* literal % */
2867 ast_str_append(&prompt, 0, "%c", '%');
2868 break;
2869 case '\0': /* % is last character - prevent bug */
2870 t--;
2871 break;
2872 }
2873 } else {
2874 ast_str_append(&prompt, 0, "%c", *t);
2875 }
2876 t++;
2877 }
2878 if (color_used) {
2879 /* Force colors back to normal at end */
2881 }
2882 } else {
2883 ast_str_set(&prompt, 0, "%s%s",
2886 }
2887
2888 return ast_str_buffer(prompt);
2889}
2890
2892{
2893 char *retstr;
2894 struct ast_vector_string *vec = ast_calloc(1, sizeof(*vec));
2895
2896 if (!vec) {
2897 return NULL;
2898 }
2899
2900 while ((retstr = strsep(&buf, " "))) {
2901 if (!strcmp(retstr, AST_CLI_COMPLETE_EOF)) {
2902 break;
2903 }
2904
2905 retstr = ast_strdup(retstr);
2906 if (!retstr || AST_VECTOR_APPEND(vec, retstr)) {
2907 ast_free(retstr);
2908 goto vector_cleanup;
2909 }
2910 }
2911
2912 if (!AST_VECTOR_SIZE(vec)) {
2913 goto vector_cleanup;
2914 }
2915
2916 return vec;
2917
2918vector_cleanup:
2921
2922 return NULL;
2923}
2924
2925static void ast_cli_display_match_list(struct ast_vector_string *matches, int max)
2926{
2927 int idx = 1;
2928 /* find out how many entries can be put on one line, with two spaces between strings */
2929 int limit = ast_get_termcols(STDOUT_FILENO) / (max + 2);
2930
2931 if (limit == 0) {
2932 limit = 1;
2933 }
2934
2935 for (;;) {
2936 int numoutputline;
2937
2938 for (numoutputline = 0; numoutputline < limit && idx < AST_VECTOR_SIZE(matches); idx++) {
2939 numoutputline++;
2940 fprintf(stdout, "%-*s ", max, AST_VECTOR_GET(matches, idx));
2941 }
2942
2943 if (!numoutputline) {
2944 break;
2945 }
2946
2947 fprintf(stdout, "\n");
2948 }
2949}
2950
2951
2952static char *cli_complete(EditLine *editline, int ch)
2953{
2954 int len = 0;
2955 char *ptr;
2956 struct ast_vector_string *matches;
2957 int retval = CC_ERROR;
2958 char savechr;
2959 int res;
2960
2961 LineInfo *lf = (LineInfo *)el_line(editline);
2962
2963 savechr = *(char *)lf->cursor;
2964 *(char *)lf->cursor = '\0';
2965 ptr = (char *)lf->cursor;
2966 if (ptr) {
2967 while (ptr > lf->buffer) {
2968 if (isspace(*ptr)) {
2969 ptr++;
2970 break;
2971 }
2972 ptr--;
2973 }
2974 }
2975
2976 len = lf->cursor - ptr;
2977
2978 if (ast_opt_remote) {
2979#define CMD_MATCHESARRAY "_COMMAND MATCHESARRAY \"%s\" \"%s\""
2980 char *mbuf;
2981 char *new_mbuf;
2982 int mlen = 0;
2983 int maxmbuf = ast_asprintf(&mbuf, CMD_MATCHESARRAY, lf->buffer, ptr);
2984
2985 if (maxmbuf == -1) {
2986 *((char *) lf->cursor) = savechr;
2987
2988 return (char *)(CC_ERROR);
2989 }
2990
2991 fdsend(ast_consock, mbuf);
2992 res = 0;
2993 mlen = 0;
2994 mbuf[0] = '\0';
2995
2996 while (!strstr(mbuf, AST_CLI_COMPLETE_EOF) && res != -1) {
2997 if (mlen + 1024 > maxmbuf) {
2998 /* Expand buffer to the next 1024 byte increment plus a NULL terminator. */
2999 maxmbuf = mlen + 1024;
3000 new_mbuf = ast_realloc(mbuf, maxmbuf + 1);
3001 if (!new_mbuf) {
3002 ast_free(mbuf);
3003 *((char *) lf->cursor) = savechr;
3004
3005 return (char *)(CC_ERROR);
3006 }
3007 mbuf = new_mbuf;
3008 }
3009 /* Only read 1024 bytes at a time */
3010 res = read(ast_consock, mbuf + mlen, 1024);
3011 if (res > 0) {
3012 if (!strncmp(mbuf, "Usage:", 6)) {
3013 /*
3014 * Abort on malformed tab completes
3015 * If help (tab complete) follows certain
3016 * special characters, the main Asterisk process
3017 * provides usage for the internal tab complete
3018 * helper command that the remote console processes
3019 * use.
3020 * If this happens, the AST_CLI_COMPLETE_EOF sentinel
3021 * value never gets sent. As a result, we'll just block
3022 * forever if we don't handle this case.
3023 * If we get command usage on a tab complete, then
3024 * we know this scenario just happened and we should
3025 * just silently ignore and do nothing.
3026 */
3027 break;
3028 }
3029 mlen += res;
3030 mbuf[mlen] = '\0';
3031 }
3032 }
3033 mbuf[mlen] = '\0';
3034
3035 matches = ast_el_strtoarr(mbuf);
3036 ast_free(mbuf);
3037 } else {
3038 matches = ast_cli_completion_vector((char *)lf->buffer, ptr);
3039 }
3040
3041 if (matches) {
3042 int i;
3043 int maxlen, match_len;
3044 const char *best_match = AST_VECTOR_GET(matches, 0);
3045
3046 if (!ast_strlen_zero(best_match)) {
3047 el_deletestr(editline, (int) len);
3048 el_insertstr(editline, best_match);
3049 retval = CC_REFRESH;
3050 }
3051
3052 if (AST_VECTOR_SIZE(matches) == 2) {
3053 /* Found an exact match */
3054 el_insertstr(editline, " ");
3055 retval = CC_REFRESH;
3056 } else {
3057 /* Must be more than one match */
3058 for (i = 1, maxlen = 0; i < AST_VECTOR_SIZE(matches); i++) {
3059 match_len = strlen(AST_VECTOR_GET(matches, i));
3060 if (match_len > maxlen) {
3061 maxlen = match_len;
3062 }
3063 }
3064
3065 fprintf(stdout, "\n");
3066 ast_cli_display_match_list(matches, maxlen);
3067 retval = CC_REDISPLAY;
3068 }
3070 AST_VECTOR_PTR_FREE(matches);
3071 }
3072
3073 *((char *) lf->cursor) = savechr;
3074
3075 return (char *)(long)retval;
3076}
3077
3078static int ast_el_initialize(void)
3079{
3080 HistEvent ev;
3081 char *editor, *editrc = getenv("EDITRC");
3082
3083 if (!(editor = getenv("AST_EDITMODE"))) {
3084 if (!(editor = getenv("AST_EDITOR"))) {
3085 editor = "emacs";
3086 }
3087 }
3088
3089 if (el != NULL)
3090 el_end(el);
3091 if (el_hist != NULL)
3092 history_end(el_hist);
3093
3094 el = el_init("asterisk", stdin, stdout, stderr);
3095 el_set(el, EL_PROMPT, cli_prompt);
3096
3097 el_set(el, EL_EDITMODE, 1);
3098 el_set(el, EL_EDITOR, editor);
3099 el_hist = history_init();
3100 if (!el || !el_hist)
3101 return -1;
3102
3103 /* setup history with 100 entries */
3104 history(el_hist, &ev, H_SETSIZE, 100);
3105
3106 el_set(el, EL_HIST, history, el_hist);
3107
3108 el_set(el, EL_ADDFN, "ed-complete", "Complete argument", cli_complete);
3109 /* Bind <tab> to command completion */
3110 el_set(el, EL_BIND, "^I", "ed-complete", NULL);
3111 /* Bind ? to command completion */
3112 el_set(el, EL_BIND, "?", "ed-complete", NULL);
3113 /* Bind ^D to redisplay */
3114 el_set(el, EL_BIND, "^D", "ed-redisplay", NULL);
3115 /* Bind Delete to delete char left */
3116 el_set(el, EL_BIND, "\\e[3~", "ed-delete-next-char", NULL);
3117 /* Bind Home and End to move to line start and end */
3118 el_set(el, EL_BIND, "\\e[1~", "ed-move-to-beg", NULL);
3119 el_set(el, EL_BIND, "\\e[4~", "ed-move-to-end", NULL);
3120 /* Bind C-left and C-right to move by word (not all terminals) */
3121 el_set(el, EL_BIND, "\\eOC", "vi-next-word", NULL);
3122 el_set(el, EL_BIND, "\\eOD", "vi-prev-word", NULL);
3123
3124 if (editrc) {
3125 el_source(el, editrc);
3126 }
3127
3128 return 0;
3129}
3130
3131#define MAX_HISTORY_COMMAND_LENGTH 256
3132
3133static int ast_el_add_history(const char *buf)
3134{
3135 HistEvent ev;
3136 char *stripped_buf;
3137
3138 if (el_hist == NULL || el == NULL) {
3140 }
3141 if (strlen(buf) > (MAX_HISTORY_COMMAND_LENGTH - 1)) {
3142 return 0;
3143 }
3144
3145 stripped_buf = ast_strip(ast_strdupa(buf));
3146
3147 /* HISTCONTROL=ignoredups */
3148 if (!history(el_hist, &ev, H_FIRST) && strcmp(ev.str, stripped_buf) == 0) {
3149 return 0;
3150 }
3151
3152 return history(el_hist, &ev, H_ENTER, stripped_buf);
3153}
3154
3155static int ast_el_write_history(const char *filename)
3156{
3157 HistEvent ev;
3158
3159 if (el_hist == NULL || el == NULL)
3161
3162 return (history(el_hist, &ev, H_SAVE, filename));
3163}
3164
3165static int ast_el_read_history(const char *filename)
3166{
3167 HistEvent ev;
3168
3169 if (el_hist == NULL || el == NULL) {
3171 }
3172
3173 return history(el_hist, &ev, H_LOAD, filename);
3174}
3175
3176static void process_histfile(int (*readwrite)(const char *filename))
3177{
3178 struct passwd *pw = getpwuid(geteuid());
3179 int ret = 0;
3180 char *name = NULL;
3181
3182 if (!pw || ast_strlen_zero(pw->pw_dir)) {
3183 ast_log(LOG_ERROR, "Unable to determine home directory. History read/write disabled.\n");
3184 return;
3185 }
3186
3187 ret = ast_asprintf(&name, "%s/.asterisk_history", pw->pw_dir);
3188 if (ret <= 0) {
3189 ast_log(LOG_ERROR, "Unable to create history file name. History read/write disabled.\n");
3190 return;
3191 }
3192
3193 ret = readwrite(name);
3194 if (ret < 0) {
3195 ast_log(LOG_ERROR, "Unable to read or write history file '%s'\n", name);
3196 }
3197
3198 ast_free(name);
3199
3200 return;
3201}
3202
3204{
3206}
3207
3209{
3211}
3212
3213static void ast_remotecontrol(char *data)
3214{
3215 char buf[256] = "";
3216 int res;
3217 char *hostname;
3218 char *cpid;
3219 char *version;
3220 int pid;
3221 char *stringp = NULL;
3222
3223 char *ebuf;
3224 int num = 0;
3225
3226 ast_term_init();
3227 printf("%s", term_end());
3228 fflush(stdout);
3229
3230 memset(&sig_flags, 0, sizeof(sig_flags));
3231 signal(SIGINT, __remote_quit_handler);
3232 signal(SIGTERM, __remote_quit_handler);
3233 signal(SIGHUP, __remote_quit_handler);
3234
3235 if (read(ast_consock, buf, sizeof(buf) - 1) < 0) {
3236 ast_log(LOG_ERROR, "read() failed: %s\n", strerror(errno));
3237 return;
3238 }
3239 if (data) {
3240 char prefix[] = "cli quit after ";
3241 char *tmp = ast_alloca(strlen(data) + strlen(prefix) + 1);
3242 sprintf(tmp, "%s%s", prefix, data);
3243 if (write(ast_consock, tmp, strlen(tmp) + 1) < 0) {
3244 ast_log(LOG_ERROR, "write() failed: %s\n", strerror(errno));
3245 if (sig_flags.need_quit || sig_flags.need_quit_handler || sig_flags.need_el_end) {
3246 return;
3247 }
3248 }
3249 }
3250 stringp = buf;
3251 hostname = strsep(&stringp, "/");
3252 cpid = strsep(&stringp, "/");
3253 version = strsep(&stringp, "\n");
3254 if (!version)
3255 version = "<Version Unknown>";
3256 stringp = hostname;
3257 strsep(&stringp, ".");
3258 if (cpid)
3259 pid = atoi(cpid);
3260 else
3261 pid = -1;
3262 if (!data) {
3264 }
3265
3266 if (ast_opt_exec && data) { /* hack to print output then exit if asterisk -rx is used */
3267 int linefull = 1, prev_linefull = 1, prev_line_verbose = 0;
3268 struct pollfd fds;
3269 fds.fd = ast_consock;
3270 fds.events = POLLIN;
3271 fds.revents = 0;
3272
3273 while (ast_poll(&fds, 1, 60000) > 0) {
3274 char buffer[512] = "", *curline = buffer, *nextline;
3275 int not_written = 1;
3276
3277 if (sig_flags.need_quit || sig_flags.need_quit_handler || sig_flags.need_el_end) {
3278 break;
3279 }
3280
3281 if (read(ast_consock, buffer, sizeof(buffer) - 1) <= 0) {
3282 break;
3283 }
3284
3285 do {
3286 prev_linefull = linefull;
3287 if ((nextline = strchr(curline, '\n'))) {
3288 linefull = 1;
3289 nextline++;
3290 } else {
3291 linefull = 0;
3292 nextline = strchr(curline, '\0');
3293 }
3294
3295 /* Skip verbose lines */
3296 /* Prev line full? | Line is verbose | Last line verbose? | Print
3297 * TRUE | TRUE* | TRUE | FALSE
3298 * TRUE | TRUE* | FALSE | FALSE
3299 * TRUE | FALSE* | TRUE | TRUE
3300 * TRUE | FALSE* | FALSE | TRUE
3301 * FALSE | TRUE | TRUE* | FALSE
3302 * FALSE | TRUE | FALSE* | TRUE
3303 * FALSE | FALSE | TRUE* | FALSE
3304 * FALSE | FALSE | FALSE* | TRUE
3305 */
3306 if ((!prev_linefull && !prev_line_verbose) || (prev_linefull && *curline > 0)) {
3307 prev_line_verbose = 0;
3308 not_written = 0;
3309 if (write(STDOUT_FILENO, curline, nextline - curline) < 0) {
3310 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3311 }
3312 } else {
3313 prev_line_verbose = 1;
3314 }
3315 curline = nextline;
3316 } while (!ast_strlen_zero(curline));
3317
3318 /* No non-verbose output in 60 seconds. */
3319 if (not_written) {
3320 break;
3321 }
3322 }
3323 return;
3324 }
3325
3326 ast_verbose("Connected to Asterisk %s currently running on %s (pid = %d)\n", version, hostname, pid);
3328
3330 if (el_hist == NULL || el == NULL)
3333
3334 el_set(el, EL_GETCFN, ast_el_read_char);
3335
3336 for (;;) {
3337 ebuf = (char *)el_gets(el, &num);
3338
3339 if (sig_flags.need_quit || sig_flags.need_quit_handler || sig_flags.need_el_end) {
3340 break;
3341 }
3342
3343 if (!ebuf && write(1, "", 1) < 0)
3344 break;
3345
3346 if (!ast_strlen_zero(ebuf)) {
3347 if (ebuf[strlen(ebuf)-1] == '\n')
3348 ebuf[strlen(ebuf)-1] = '\0';
3349 if (!remoteconsolehandler(ebuf)) {
3350 res = write(ast_consock, ebuf, strlen(ebuf) + 1);
3351 if (res < 1) {
3352 ast_log(LOG_WARNING, "Unable to write: %s\n", strerror(errno));
3353 break;
3354 }
3355 }
3356 }
3357 }
3358 printf("\nDisconnected from Asterisk server\n");
3359}
3360
3361static int show_version(void)
3362{
3363 printf("Asterisk %s\n", ast_get_version());
3364 return 0;
3365}
3366
3367static int show_cli_help(void)
3368{
3369 printf("Asterisk %s, " COPYRIGHT_TAG "\n", ast_get_version());
3370 printf("Usage: asterisk [OPTIONS]\n");
3371 printf("Valid Options:\n");
3372 printf(" -V Display version number and exit\n");
3373 printf(" -C <configfile> Use an alternate configuration file\n");
3374 printf(" -G <group> Run as a group other than the caller\n");
3375 printf(" -U <user> Run as a user other than the caller\n");
3376 printf(" -c Provide console CLI\n");
3377 printf(" -d Increase debugging (multiple d's = more debugging)\n");
3378#if HAVE_WORKING_FORK
3379 printf(" -f Do not fork\n");
3380 printf(" -F Always fork\n");
3381#endif
3382 printf(" -g Dump core in case of a crash\n");
3383 printf(" -h This help screen\n");
3384 printf(" -i Initialize crypto keys at startup\n");
3385 printf(" -L <load> Limit the maximum load average before rejecting new calls\n");
3386 printf(" -M <value> Limit the maximum number of calls to the specified value\n");
3387 printf(" -m Mute debugging and console output on the console\n");
3388 printf(" -n Disable console colorization. Can be used only at startup.\n");
3389 printf(" -p Run as pseudo-realtime thread\n");
3390 printf(" -q Quiet mode (suppress output)\n");
3391 printf(" -r Connect to Asterisk on this machine\n");
3392 printf(" -R Same as -r, except attempt to reconnect if disconnected\n");
3393 printf(" -s <socket> Connect to Asterisk via socket <socket> (only valid with -r)\n");
3394 printf(" -t Record soundfiles in /var/tmp and move them where they\n");
3395 printf(" belong after they are done\n");
3396 printf(" -T Display the time in [Mmm dd hh:mm:ss] format for each line\n");
3397 printf(" of output to the CLI. Cannot be used with remote console mode.\n\n");
3398 printf(" -v Increase verbosity (multiple v's = more verbose)\n");
3399 printf(" -x <cmd> Execute command <cmd> (implies -r)\n");
3400 printf(" -X Enable use of #exec in asterisk.conf\n");
3401 printf(" -W Adjust terminal colors to compensate for a light background\n");
3402 printf("\n");
3403 return 0;
3404}
3405
3407{
3408 struct ast_config *cfg;
3409 struct ast_variable *v;
3410 struct ast_flags config_flags = { CONFIG_FLAG_NOCACHE | CONFIG_FLAG_NOREALTIME };
3411
3414
3415 cfg = ast_config_load2("pjproject.conf", "" /* core, can't reload */, config_flags);
3416 if (!cfg
3418 || cfg == CONFIG_STATUS_FILEINVALID) {
3419 /* We'll have to use defaults */
3420 return;
3421 }
3422
3423 for (v = ast_variable_browse(cfg, "startup"); v; v = v->next) {
3424 if (!strcasecmp(v->name, "log_level")) {
3425 if (sscanf(v->value, "%30d", &ast_option_pjproject_log_level) != 1) {
3427 } else if (ast_option_pjproject_log_level < 0) {
3431 }
3432 } else if (!strcasecmp(v->name, "cache_pools")) {
3434 }
3435 }
3436
3437 ast_config_destroy(cfg);
3438}
3439
3440static void *monitor_sig_flags(void *unused)
3441{
3442 for (;;) {
3443 struct pollfd p = { sig_alert_pipe[0], POLLIN, 0 };
3444
3445 ast_poll(&p, 1, -1);
3446 if (sig_flags.need_reload) {
3447 sig_flags.need_reload = 0;
3449 }
3450 if (sig_flags.need_quit) {
3451 sig_flags.need_quit = 0;
3452 if ((consolethread != AST_PTHREADT_NULL) && (consolethread != pthread_self())) {
3453 sig_flags.need_quit_handler = 1;
3454 pthread_kill(consolethread, SIGURG);
3455 } else {
3457 }
3458 }
3460 }
3461
3462 return NULL;
3463}
3464
3465static void *canary_thread(void *unused)
3466{
3467 struct stat canary_stat;
3468 struct timeval now;
3469
3470 /* Give the canary time to sing */
3471 sleep(120);
3472
3473 for (;;) {
3474 now = ast_tvnow();
3475 if (stat(canary_filename, &canary_stat) || now.tv_sec > canary_stat.st_mtime + 60) {
3477 "The canary is no more. He has ceased to be! "
3478 "He's expired and gone to meet his maker! "
3479 "He's a stiff! Bereft of life, he rests in peace. "
3480 "His metabolic processes are now history! He's off the twig! "
3481 "He's kicked the bucket. He's shuffled off his mortal coil, "
3482 "run down the curtain, and joined the bleeding choir invisible!! "
3483 "THIS is an EX-CANARY. (Reducing priority)\n");
3485 pthread_exit(NULL);
3486 }
3487
3488 /* Check the canary once a minute */
3489 sleep(60);
3490 }
3491}
3492
3493/* Used by libc's atexit(3) function */
3494static void canary_exit(void)
3495{
3496 if (canary_pid > 0) {
3497 int status;
3498 kill(canary_pid, SIGKILL);
3499 waitpid(canary_pid, &status, 0);
3500 }
3501}
3502
3503/* Execute CLI commands on startup. Run by main() thread. */
3504static void run_startup_commands(void)
3505{
3506 int fd;
3507 struct ast_config *cfg;
3508 struct ast_flags cfg_flags = { 0 };
3509 struct ast_variable *v;
3510
3511 if (!(cfg = ast_config_load2("cli.conf", "" /* core, can't reload */, cfg_flags)))
3512 return;
3514 return;
3515 }
3516
3517 fd = open("/dev/null", O_RDWR);
3518 if (fd < 0) {
3519 ast_config_destroy(cfg);
3520 return;
3521 }
3522
3523 for (v = ast_variable_browse(cfg, "startup_commands"); v; v = v->next) {
3524 if (ast_true(v->value))
3525 ast_cli_command(fd, v->name);
3526 }
3527
3528 close(fd);
3529 ast_config_destroy(cfg);
3530}
3531
3532static void env_init(void)
3533{
3534 setenv("AST_SYSTEMNAME", ast_config_AST_SYSTEM_NAME, 1);
3535 setenv("AST_BUILD_HOST", ast_build_hostname, 1);
3536 setenv("AST_BUILD_DATE", ast_build_date, 1);
3537 setenv("AST_BUILD_KERNEL", ast_build_kernel, 1);
3538 setenv("AST_BUILD_MACHINE", ast_build_machine, 1);
3539 setenv("AST_BUILD_OS", ast_build_os, 1);
3540 setenv("AST_BUILD_USER", ast_build_user, 1);
3541 setenv("AST_VERSION", ast_get_version(), 1);
3542}
3543
3544static void print_intro_message(const char *runuser, const char *rungroup)
3545{
3548 if (runuser) {
3549 ast_verbose("Running as user '%s'\n", runuser);
3550 }
3551 if (rungroup) {
3552 ast_verbose("Running under group '%s'\n", rungroup);
3553 }
3554 }
3555}
3556
3557static void main_atexit(void)
3558{
3560}
3561
3562int main(int argc, char *argv[])
3563{
3564 int c;
3565 int x;
3566 int isroot = 1, rundir_exists = 0;
3567 RAII_VAR(char *, runuser, NULL, ast_free);
3568 RAII_VAR(char *, rungroup, NULL, ast_free);
3569 RAII_VAR(char *, xarg, NULL, ast_free);
3570 struct rlimit l;
3571 static const char *getopt_settings = "BC:cde:FfG:ghIiL:M:mnpqRrs:TtU:VvWXx:";
3572
3573 /* Remember original args for restart */
3574 if (argc > ARRAY_LEN(_argv) - 1) {
3575 fprintf(stderr, "Truncating argument size to %d\n", (int)ARRAY_LEN(_argv) - 1);
3576 argc = ARRAY_LEN(_argv) - 1;
3577 }
3578 for (x = 0; x < argc; x++)
3579 _argv[x] = argv[x];
3580 _argv[x] = NULL;
3581
3582 if (geteuid() != 0)
3583 isroot = 0;
3584
3585 /* if the progname is rasterisk consider it a remote console */
3586 if (argv[0] && (strstr(argv[0], "rasterisk")) != NULL) {
3588 }
3589 ast_mainpid = getpid();
3590
3591 /* Process command-line options that affect asterisk.conf load. */
3592 while ((c = getopt(argc, argv, getopt_settings)) != -1) {
3593 switch (c) {
3594 case 'X':
3596 break;
3597 case 'C':
3598 set_asterisk_conf_path(optarg);
3599 break;
3600 case 'd':
3601 option_debug++;
3602 break;
3603 case 'h':
3604 show_cli_help();
3605 exit(0);
3606 case 'R':
3607 case 'r':
3608 case 'x':
3609 /* ast_opt_remote is checked during config load. This is only part of what
3610 * these options do, see the second loop for the rest of the actions. */
3612 break;
3613 case 'V':
3614 show_version();
3615 exit(0);
3616 case 'v':
3618 break;
3619 case '?':
3620 exit(1);
3621 }
3622 }
3623
3624 /* Initialize env so it is available if #exec is used in asterisk.conf. */
3625 env_init();
3626
3628
3629 /* Update env to include any systemname that was set. */
3630 env_init();
3631
3632 /*! \brief Check for options
3633 *
3634 * \todo Document these options
3635 */
3636 optind = 1;
3637 while ((c = getopt(argc, argv, getopt_settings)) != -1) {
3638 /*!\note Please keep the ordering here to alphabetical, capital letters
3639 * first. This will make it easier in the future to select unused
3640 * option flags for new features. */
3641 switch (c) {
3642 case 'B': /* Force black background */
3645 break;
3646 case 'X':
3647 /* The command-line -X option enables #exec for asterisk.conf only. */
3648 break;
3649 case 'C':
3650 /* already processed. */
3651 break;
3652 case 'c':
3654 break;
3655 case 'd':
3656 /* already processed. */
3657 break;
3658#if defined(HAVE_SYSINFO)
3659 case 'e':
3660 if ((sscanf(&optarg[1], "%30ld", &option_minmemfree) != 1) || (option_minmemfree < 0)) {
3662 }
3663 break;
3664#endif
3665#if HAVE_WORKING_FORK
3666 case 'F':
3668 break;
3669 case 'f':
3671 break;
3672#endif
3673 case 'G':
3674 rungroup = ast_strdup(optarg);
3675 break;
3676 case 'g':
3678 break;
3679 case 'h':
3680 /* already processed. */
3681 break;
3682 case 'I':
3683 fprintf(stderr,
3684 "NOTICE: The -I option is no longer needed.\n"
3685 " It will always be enabled if you have a timing module loaded.\n");
3686 break;
3687 case 'i':
3689 break;
3690 case 'L':
3691 if ((sscanf(optarg, "%30lf", &ast_option_maxload) != 1) || (ast_option_maxload < 0.0)) {
3692 ast_option_maxload = 0.0;
3693 }
3694 break;
3695 case 'M':
3696 if ((sscanf(optarg, "%30d", &ast_option_maxcalls) != 1) || (ast_option_maxcalls < 0)) {
3698 }
3699 break;
3700 case 'm':
3702 break;
3703 case 'n':
3705 break;
3706 case 'p':
3708 break;
3709 case 'q':
3711 break;
3712 case 'R':
3714 break;
3715 case 'r':
3717 break;
3718 case 's':
3719 if (ast_opt_remote) {
3720 set_socket_path(optarg);
3721 }
3722 break;
3723 case 'T':
3725 break;
3726 case 't':
3728 break;
3729 case 'U':
3730 runuser = ast_strdup(optarg);
3731 break;
3732 case 'V':
3733 case 'v':
3734 /* already processed. */
3735 break;
3736 case 'W': /* White background */
3739 break;
3740 case 'x':
3741 /* -r is implied by -x so set the flags -r sets as well. */
3743
3745 xarg = ast_strdup(optarg);
3746 break;
3747 case '?':
3748 /* already processed. */
3749 break;
3750 }
3751 }
3752
3753 if (ast_opt_remote) {
3754 int didwarn = 0;
3755 optind = 1;
3756
3757 /* Not all options can be used with remote console. Warn if they're used. */
3758 while ((c = getopt(argc, argv, getopt_settings)) != -1) {
3759 switch (c) {
3760 /* okay to run with remote console */
3761 case 'B': /* force black background */
3762 case 'C': /* set config path */
3763 case 'd': /* debug */
3764 case 'h': /* help */
3765 case 'I': /* obsolete timing option: warning already thrown if used */
3766 case 'L': /* max load */
3767 case 'M': /* max calls */
3768 case 'm': /* mute */
3769 /*! \note The q option is never used anywhere, only defined */
3770 case 'q': /* quiet */
3771 case 'R': /* reconnect */
3772 case 'r': /* remote */
3773 /*! \note Can ONLY be used with remote console */
3774 case 's': /* set socket path */
3775 case 'T': /* timestamp */
3776 case 'V': /* version */
3777 case 'v': /* verbose */
3778 case 'W': /* white background */
3779 case 'x': /* remote execute */
3780 case '?': /* ? */
3781 break;
3782 /* can only be run when Asterisk is starting */
3783 case 'X': /* enables #exec for asterisk.conf only. */
3784 case 'c': /* foreground console */
3785 case 'e': /* minimum memory free */
3786 case 'F': /* always fork */
3787 case 'f': /* no fork */
3788 case 'G': /* run group */
3789 case 'g': /* dump core */
3790 case 'i': /* init keys */
3791 case 'n': /* no color */
3792 case 'p': /* high priority */
3793 case 't': /* cache record files */
3794 case 'U': /* run user */
3795 fprintf(stderr, "'%c' option is not compatible with remote console mode and has no effect.\n", c);
3796 didwarn = 1;
3797 }
3798 }
3799 if (didwarn) {
3800 fprintf(stderr, "\n"); /* if any warnings print out, make them stand out */
3801 }
3802 }
3803
3804 /* For remote connections, change the name of the remote connection.
3805 * We do this for the benefit of init scripts (which need to know if/when
3806 * the main asterisk process has died yet). */
3807 if (ast_opt_remote) {
3808 strcpy(argv[0], "rasterisk");
3809 for (x = 1; x < argc; x++) {
3810 argv[x] = argv[0] + 10;
3811 }
3812 }
3813
3815 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");
3816 }
3817
3819 fprintf(stderr, "'alwaysfork' is not compatible with console or remote console mode; ignored\n");
3821 }
3822
3823 if (ast_opt_dump_core) {
3824 memset(&l, 0, sizeof(l));
3825 l.rlim_cur = RLIM_INFINITY;
3826 l.rlim_max = RLIM_INFINITY;
3827 if (setrlimit(RLIMIT_CORE, &l)) {
3828 fprintf(stderr, "Unable to disable core size resource limit: %s\n", strerror(errno));
3829 }
3830 }
3831
3832 if (getrlimit(RLIMIT_NOFILE, &l)) {
3833 fprintf(stderr, "Unable to check file descriptor limit: %s\n", strerror(errno));
3834 }
3835
3836#if !defined(CONFIGURE_RAN_AS_ROOT)
3837 /* Check if select(2) will run with more file descriptors */
3838 do {
3839 int fd, fd2;
3840 ast_fdset readers;
3841 struct timeval tv = { 0, };
3842
3843 if (l.rlim_cur <= FD_SETSIZE) {
3844 /* The limit of select()able FDs is irrelevant, because we'll never
3845 * open one that high. */
3846 break;
3847 }
3848
3849 if (!(fd = open("/dev/null", O_RDONLY))) {
3850 fprintf(stderr, "Cannot open a file descriptor at boot? %s\n", strerror(errno));
3851 break; /* XXX Should we exit() here? XXX */
3852 }
3853
3854 fd2 = ((l.rlim_cur > sizeof(readers) * 8) ? sizeof(readers) * 8 : l.rlim_cur) - 1;
3855 if (dup2(fd, fd2) < 0) {
3856 fprintf(stderr, "Cannot open maximum file descriptor %d at boot? %s\n", fd2, strerror(errno));
3857 close(fd);
3858 break;
3859 }
3860
3861 FD_ZERO(&readers);
3862 FD_SET(fd2, &readers);
3863 if (ast_select(fd2 + 1, &readers, NULL, NULL, &tv) < 0) {
3864 fprintf(stderr, "Maximum select()able file descriptor is %d\n", FD_SETSIZE);
3865 }
3866 ast_FD_SETSIZE = l.rlim_cur > ast_FDMAX ? ast_FDMAX : l.rlim_cur;
3867 close(fd);
3868 close(fd2);
3869 } while (0);
3870#elif defined(HAVE_VARIABLE_FDSET)
3871 ast_FD_SETSIZE = l.rlim_cur > ast_FDMAX ? ast_FDMAX : l.rlim_cur;
3872#endif /* !defined(CONFIGURE_RAN_AS_ROOT) */
3873
3874 if ((!rungroup) && !ast_strlen_zero(ast_config_AST_RUN_GROUP))
3876 if ((!runuser) && !ast_strlen_zero(ast_config_AST_RUN_USER))
3878
3879 /* Must install this signal handler up here to ensure that if the canary
3880 * fails to execute that it doesn't kill the Asterisk process.
3881 */
3882 sigaction(SIGCHLD, &child_handler, NULL);
3883
3884 /* It's common on some platforms to clear /var/run at boot. Create the
3885 * socket file directory before we drop privileges. */
3886 if (mkdir(ast_config_AST_RUN_DIR, 0755)) {
3887 if (errno == EEXIST) {
3888 rundir_exists = 1;
3889 } else {
3890 fprintf(stderr, "Unable to create socket file directory. Remote consoles will not be able to connect! (%s)\n", strerror(x));
3891 }
3892 }
3893
3894#ifndef __CYGWIN__
3895
3896 if (isroot) {
3898 }
3899
3900 if (isroot && rungroup) {
3901 struct group *gr;
3902 gr = getgrnam(rungroup);
3903 if (!gr) {
3904 fprintf(stderr, "No such group '%s'!\n", rungroup);
3905 exit(1);
3906 }
3907 if (!rundir_exists && chown(ast_config_AST_RUN_DIR, -1, gr->gr_gid)) {
3908 fprintf(stderr, "Unable to chgrp run directory to %d (%s)\n", (int) gr->gr_gid, rungroup);
3909 }
3910 if (setgid(gr->gr_gid)) {
3911 fprintf(stderr, "Unable to setgid to %d (%s)\n", (int)gr->gr_gid, rungroup);
3912 exit(1);
3913 }
3914 if (setgroups(0, NULL)) {
3915 fprintf(stderr, "Unable to drop unneeded groups\n");
3916 exit(1);
3917 }
3918 }
3919
3920 if (runuser && !ast_test_flag(&ast_options, AST_OPT_FLAG_REMOTE)) {
3921#ifdef HAVE_CAP
3922 int has_cap = 1;
3923#endif /* HAVE_CAP */
3924 struct passwd *pw;
3925 pw = getpwnam(runuser);
3926 if (!pw) {
3927 fprintf(stderr, "No such user '%s'!\n", runuser);
3928 exit(1);
3929 }
3930 if (chown(ast_config_AST_RUN_DIR, pw->pw_uid, -1)) {
3931 fprintf(stderr, "Unable to chown run directory to %d (%s)\n", (int) pw->pw_uid, runuser);
3932 }
3933#ifdef HAVE_CAP
3934 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0)) {
3935 ast_log(LOG_WARNING, "Unable to keep capabilities.\n");
3936 has_cap = 0;
3937 }
3938#endif /* HAVE_CAP */
3939 if (!isroot && pw->pw_uid != geteuid()) {
3940 fprintf(stderr, "Asterisk started as nonroot, but runuser '%s' requested.\n", runuser);
3941 exit(1);
3942 }
3943 if (!rungroup) {
3944 if (setgid(pw->pw_gid)) {
3945 fprintf(stderr, "Unable to setgid to %d!\n", (int)pw->pw_gid);
3946 exit(1);
3947 }
3948 if (isroot && initgroups(pw->pw_name, pw->pw_gid)) {
3949 fprintf(stderr, "Unable to init groups for '%s'\n", runuser);
3950 exit(1);
3951 }
3952 }
3953 if (setuid(pw->pw_uid)) {
3954 fprintf(stderr, "Unable to setuid to %d (%s)\n", (int)pw->pw_uid, runuser);
3955 exit(1);
3956 }
3957#ifdef HAVE_CAP
3958 if (has_cap) {
3959 cap_t cap;
3960
3961 cap = cap_from_text("cap_net_admin=eip");
3962
3963 if (cap_set_proc(cap)) {
3964 fprintf(stderr, "Unable to install capabilities.\n");
3965 }
3966 if (cap_free(cap)) {
3967 fprintf(stderr, "Unable to drop capabilities.\n");
3968 }
3969 }
3970#endif /* HAVE_CAP */
3971 }
3972
3973#endif /* __CYGWIN__ */
3974
3975#ifdef linux
3976 if (geteuid() && ast_opt_dump_core) {
3977 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) < 0) {
3978 fprintf(stderr, "Unable to set the process for core dumps after changing to a non-root user. %s\n", strerror(errno));
3979 }
3980 }
3981#endif
3982
3983 {
3984#if defined(HAVE_EACCESS) || defined(HAVE_EUIDACCESS)
3985 char dir[PATH_MAX];
3986 if (!getcwd(dir, sizeof(dir)) || eaccess(dir, R_OK | X_OK | F_OK)) {
3987 fprintf(stderr, "Unable to access the running directory (%s). Changing to '/' for compatibility.\n", strerror(errno));
3988 /* If we cannot access the CWD, then we couldn't dump core anyway,
3989 * so chdir("/") won't break anything. */
3990 if (chdir("/")) {
3991 /* chdir(/) should never fail, so this ends up being a no-op */
3992 fprintf(stderr, "chdir(\"/\") failed?!! %s\n", strerror(errno));
3993 }
3994 } else
3995#endif /* defined(HAVE_EACCESS) || defined(HAVE_EUIDACCESS) */
3997 /* Backgrounding, but no cores, so chdir won't break anything. */
3998 if (chdir("/")) {
3999 fprintf(stderr, "Unable to chdir(\"/\") ?!! %s\n", strerror(errno));
4000 }
4001 }
4002 }
4003
4004 /* Initial value of the maximum active system verbosity level. */
4006
4007 if (ast_sd_get_fd_un(SOCK_STREAM, ast_config_AST_SOCKET) > 0) {
4008 ast_socket_is_sd = 1;
4009 }
4010
4011 /* DO NOT perform check for existing daemon if systemd has CLI socket activation */
4012 if (!ast_socket_is_sd && ast_tryconnect()) {
4013 /* One is already running */
4014 if (ast_opt_remote) {
4016 if (ast_opt_exec) {
4017 ast_remotecontrol(xarg);
4019 exit(0);
4020 }
4021 ast_term_init();
4022 printf("%s", term_end());
4023 fflush(stdout);
4024
4025 print_intro_message(runuser, rungroup);
4026 printf("%s", term_quit());
4029 exit(0);
4030 } else {
4031 fprintf(stderr, "Asterisk already running on %s. Use 'asterisk -r' to connect.\n", ast_config_AST_SOCKET);
4032 printf("%s", term_quit());
4033 exit(1);
4034 }
4035 } else if (ast_opt_remote || ast_opt_exec) {
4036 fprintf(stderr, "Unable to connect to remote asterisk (does %s exist?)\n", ast_config_AST_SOCKET);
4037 printf("%s", term_quit());
4038 exit(1);
4039 }
4040
4041#ifdef HAVE_CAP
4042 child_cap = cap_from_text("cap_net_admin-eip");
4043#endif
4044 /* Not a remote console? Start the daemon. */
4045 asterisk_daemon(isroot, runuser, rungroup);
4046#ifdef HAS_CAP
4047 cap_free(child_cap);
4048#endif
4049 return 0;
4050}
4051
4052static inline void check_init(int init_result, const char *name)
4053{
4054 if (init_result) {
4056 ast_log(LOG_ERROR, "%s initialization failed. ASTERISK EXITING!\n%s", name, term_quit());
4057 } else {
4058 fprintf(stderr, "%s initialization failed. ASTERISK EXITING!\n%s", name, term_quit());
4059 }
4060 ast_run_atexits(0);
4061 exit(init_result == -2 ? 2 : 1);
4062 }
4063}
4064
4065static void asterisk_daemon(int isroot, const char *runuser, const char *rungroup)
4066{
4067 FILE *f;
4068 sigset_t sigs;
4069 int num;
4070 char *buf;
4071 char pbx_uuid[AST_UUID_STR_LEN];
4072
4073 /* Set time as soon as possible */
4075
4076 /* This needs to remain as high up in the initial start up as possible.
4077 * daemon causes a fork to occur, which has all sorts of unintended
4078 * consequences for things that interact with threads. This call *must*
4079 * occur before anything in Asterisk spawns or manipulates thread related
4080 * primitives. */
4081#if HAVE_WORKING_FORK
4083#ifndef HAVE_SBIN_LAUNCHD
4084 if (daemon(1, 0) < 0) {
4085 fprintf(stderr, "daemon() failed: %s\n", strerror(errno));
4086 } else {
4087 ast_mainpid = getpid();
4088 }
4089#else
4090 fprintf(stderr, "Mac OS X detected. Use 'launchctl load /Library/LaunchDaemon/org.asterisk.asterisk.plist'.\n");
4091#endif
4092 }
4093#endif
4094
4095 /* At this point everything has been forked successfully,
4096 * we have determined that we aren't attempting to connect to
4097 * an Asterisk instance, and that there isn't one already running. */
4099
4101
4102 /* Check whether high prio was successfully set by us or some
4103 * other incantation. */
4104 if (has_priority()) {
4106 } else {
4108 }
4109
4110 /* Spawning of astcanary must happen AFTER the call to daemon(3) */
4112 snprintf(canary_filename, sizeof(canary_filename), "%s/alt.asterisk.canary.tweet.tweet.tweet", ast_config_AST_RUN_DIR);
4113
4114 /* Don't let the canary child kill Asterisk, if it dies immediately */
4115 sigaction(SIGPIPE, &ignore_sig_handler, NULL);
4116
4117 canary_pid = fork();
4118 if (canary_pid == 0) {
4119 char canary_binary[PATH_MAX], ppid[12];
4120
4121 /* Reset signal handler */
4122 signal(SIGCHLD, SIG_DFL);
4123 signal(SIGPIPE, SIG_DFL);
4124
4127 snprintf(ppid, sizeof(ppid), "%d", (int) ast_mainpid);
4128
4129 /* Use the astcanary binary that we installed */
4130 snprintf(canary_binary, sizeof(canary_binary), "%s/astcanary", ast_config_AST_SBIN_DIR);
4131 execl(canary_binary, "astcanary", canary_filename, ppid, (char *)NULL);
4132
4133 /* Should never happen */
4134 _exit(1);
4135 } else if (canary_pid > 0) {
4136 pthread_t dont_care;
4138 }
4139
4140 /* Kill the canary when we exit */
4142 }
4143
4144 /* Blindly write the PID file. */
4145 unlink(ast_config_AST_PID);
4146 f = fopen(ast_config_AST_PID, "w");
4147 if (f) {
4148 fprintf(f, "%ld\n", (long)ast_mainpid);
4149 fclose(f);
4150 } else {
4151 fprintf(stderr, "Unable to open pid file '%s': %s\n", ast_config_AST_PID, strerror(errno));
4152 }
4153
4154 /* Initialize the terminal. Since all processes have been forked,
4155 * we can now start using the standard log messages.
4156 */
4157 ast_term_init();
4158 printf("%s", term_end());
4159 fflush(stdout);
4160
4161 print_intro_message(runuser, rungroup);
4162
4164
4165 check_init(astobj2_init(), "AO2");
4166 check_init(ast_named_locks_init(), "Named Locks");
4167
4168 if (ast_opt_console) {
4169 if (el_hist == NULL || el == NULL)
4172 }
4173
4174#ifdef AST_XML_DOCS
4175 /* Load XML documentation. */
4177#endif
4178
4179 check_init(astdb_init(), "ASTdb");
4180
4181 ast_uuid_init();
4182
4183 if (ast_pbx_uuid_get(pbx_uuid, sizeof(pbx_uuid))) {
4184 ast_uuid_generate_str(pbx_uuid, sizeof(pbx_uuid));
4185 ast_db_put("pbx", "UUID", pbx_uuid);
4186 }
4187 ast_verb(0, "PBX UUID: %s\n", pbx_uuid);
4188
4189 check_init(ast_json_init(), "libjansson");
4190 ast_ulaw_init();
4191 ast_alaw_init();
4192 ast_utf8_init();
4193 tdd_init();
4194 callerid_init();
4196
4197 check_init(ast_utils_init(), "Utilities");
4198 check_init(ast_tps_init(), "Task Processor Core");
4199 check_init(ast_fd_init(), "File Descriptor Debugging");
4200 check_init(ast_pbx_init(), "ast_pbx_init");
4201 check_init(aco_init(), "Configuration Option Framework");
4202 check_init(stasis_init(), "Stasis");
4203#ifdef TEST_FRAMEWORK
4204 check_init(ast_test_init(), "Test Framework");
4205#endif
4206 check_init(ast_translate_init(), "Translator Core");
4207
4209
4210 check_init(ast_sorcery_init(), "Sorcery");
4211 check_init(ast_codec_init(), "Codecs");
4212 check_init(ast_format_init(), "Formats");
4213 check_init(ast_format_cache_init(), "Format Cache");
4214 check_init(ast_codec_builtin_init(), "Built-in Codecs");
4215 check_init(ast_bucket_init(), "Bucket API");
4216 check_init(ast_stasis_system_init(), "Stasis system-level information");
4217 check_init(ast_endpoint_stasis_init(), "Stasis Endpoint");
4218
4220 /* GCC 4.9 gives a bogus "right-hand operand of comma expression has
4221 * no effect" warning */
4222 (void) sigemptyset(&sigs);
4223 (void) sigaddset(&sigs, SIGHUP);
4224 (void) sigaddset(&sigs, SIGTERM);
4225 (void) sigaddset(&sigs, SIGINT);
4226 (void) sigaddset(&sigs, SIGPIPE);
4227 (void) sigaddset(&sigs, SIGWINCH);
4228 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
4229 sigaction(SIGURG, &urg_handler, NULL);
4230 signal(SIGINT, __quit_handler);
4231 signal(SIGTERM, __quit_handler);
4232 sigaction(SIGHUP, &hup_handler, NULL);
4233 sigaction(SIGPIPE, &ignore_sig_handler, NULL);
4234
4235 /* ensure that the random number generators are seeded with a different value every time
4236 Asterisk is started
4237 */
4238 srand((unsigned int) getpid() + (unsigned int) time(NULL));
4239 initstate((unsigned int) getpid() * 65536 + (unsigned int) time(NULL), randompool, sizeof(randompool));
4240
4242
4243 check_init(init_logger(), "Logger");
4244 check_init(ast_rtp_engine_init(), "RTP Engine");
4245
4247
4248 check_init(ast_timing_init(), "Timing");
4249 check_init(ast_ssl_init(), "SSL");
4251 check_init(ast_pj_init(), "Embedded PJProject");
4252 check_init(app_init(), "App Core");
4253 check_init(mwi_init(), "MWI Core");
4254 check_init(devstate_init(), "Device State Core");
4255 check_init(ast_msg_init(), "Messaging API");
4256 check_init(ast_channels_init(), "Channel");
4257 check_init(ast_endpoint_init(), "Endpoints");
4258 check_init(ast_pickup_init(), "Call Pickup");
4259 check_init(ast_bridging_init(), "Bridging");
4260 check_init(ast_parking_stasis_init(), "Parking Core");
4261 check_init(ast_device_state_engine_init(), "Device State Engine");
4262 check_init(ast_presence_state_engine_init(), "Presence State Engine");
4263 check_init(dns_core_init(), "DNS Resolver Core");
4264 check_init(ast_dns_system_resolver_init(), "Default DNS resolver");
4265 check_init(ast_security_stasis_init(), "Security Stasis Topic and Events");
4266 check_init(ast_image_init(), "Image");
4267 check_init(ast_file_init(), "Generic File Format Support");
4268 check_init(load_pbx(), "load_pbx");
4269 check_init(load_pbx_builtins(), "Builtin PBX Applications");
4270 check_init(load_pbx_functions_cli(), "PBX Functions Support");
4271 check_init(load_pbx_variables(), "PBX Variables Support");
4272 check_init(load_pbx_switch(), "PBX Switch Support");
4273 check_init(load_pbx_app(), "PBX Application Support");
4274 check_init(load_pbx_hangup_handler(), "PBX Hangup Handler Support");
4275 check_init(ast_local_init(), "Local Proxy Channel Driver");
4276 check_init(ast_refer_init(), "Refer API");
4277
4278 /* We should avoid most config loads before this point as they can't use realtime. */
4279 check_init(load_modules(), "Module");
4280
4281 /*
4282 * This has to load after the dynamic modules load, as items in the media
4283 * cache can't be constructed from items in the AstDB without their
4284 * bucket backends.
4285 */
4286 check_init(ast_media_cache_init(), "Media Cache");
4287
4288 /* loads the cli_permissions.conf file needed to implement cli restrictions. */
4290 ast_cli_channels_init(); /* Not always safe to access CLI commands until startup is complete. */
4291
4292 ast_stun_init();
4293
4295
4296 if (ast_opt_no_fork) {
4297 consolethread = pthread_self();
4298 }
4299
4301
4303
4306
4307 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
4308
4310
4314
4316 ast_sd_notify("READY=1");
4317
4318 ast_verb(0, COLORIZE_FMT "\n", COLORIZE(COLOR_BRGREEN, 0, "Asterisk Ready."));
4319
4321
4322 if (ast_opt_console) {
4323 /* Console stuff now... */
4324 /* Register our quit function */
4325 char title[256];
4326 char hostname[MAXHOSTNAMELEN] = "";
4327
4328 if (gethostname(hostname, sizeof(hostname) - 1)) {
4329 ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
4330 }
4331
4333
4334 set_icon("Asterisk");
4335 snprintf(title, sizeof(title), "Asterisk Console on '%s' (pid %ld)", hostname, (long)ast_mainpid);
4336 set_title(title);
4337
4338 el_set(el, EL_GETCFN, ast_el_read_char);
4339
4340 for (;;) {
4341 if (sig_flags.need_el_end) {
4342 el_end(el);
4343
4344 return;
4345 }
4346
4347 if (sig_flags.need_quit || sig_flags.need_quit_handler) {
4349 break;
4350 }
4351 buf = (char *) el_gets(el, &num);
4352
4353 if (!buf && write(1, "", 1) < 0)
4354 return; /* quit */
4355
4356 if (buf) {
4357 if (buf[strlen(buf)-1] == '\n')
4358 buf[strlen(buf)-1] = '\0';
4359
4361 }
4362 }
4363 }
4364
4365 /* Stall until a quit signal is given */
4367}
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:1412
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:2354
void ast_cli_channels_init(void)
Definition: main/cli.c:2240
void ast_msg_shutdown(void)
void logger_queue_start(void)
Start the ast_queue_log() logger.
Definition: logger.c:2174
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:2233
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:1228
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:3677
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:8007
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:2233
void ast_autoservice_init(void)
Definition: autoservice.c:380
int init_logger(void)
Definition: logger.c:2190
int ast_endpoint_init(void)
Endpoint support initialization.
int dns_core_init(void)
Definition: dns_core.c:614
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:2100
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:342
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:3133
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:2369
static void __ast_unregister_atexit(void(*func)(void))
Definition: asterisk.c:1017
static char * cli_complete(EditLine *editline, int ch)
Definition: asterisk.c:2952
int64_t ast_mark(int i, int startstop)
Definition: asterisk.c:873
int main(int argc, char *argv[])
Definition: asterisk.c:3562
static int ast_socket_is_sd
Definition: asterisk.c:313
int ast_shutdown_final(void)
Definition: asterisk.c:1867
static void canary_exit(void)
Definition: asterisk.c:3494
static shutdown_nice_t shuttingdown
Definition: asterisk.c:378
static void check_init(int init_result, const char *name)
Definition: asterisk.c:4052
static void __remote_quit_handler(int num)
Definition: asterisk.c:2148
static void _urg_handler(int num)
Urgent handler.
Definition: asterisk.c:1707
static char * show_license(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2574
#define CHAR_T_LIBEDIT
Definition: asterisk.c:2657
static char * handle_show_threads(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:603
static void ast_run_atexits(int run_cleanups)
Definition: asterisk.c:1003
static int console_state_init(void *ptr)
Definition: asterisk.c:2191
static struct profile_data * prof_data
Definition: asterisk.c:792
static char * handle_stop_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2390
static int remoteconsolehandler(const char *s)
Definition: asterisk.c:2300
static struct ast_str * prompt
Definition: asterisk.c:2763
unsigned int need_reload
Definition: asterisk.c:398
static void publish_fully_booted(void)
Definition: asterisk.c:977
static int show_version(void)
Definition: asterisk.c:3361
static void main_atexit(void)
Definition: asterisk.c:3557
static int safe_exec_wait(pid_t pid)
wait for spawned application to complete and unreplace sigchld
Definition: asterisk.c:1192
int ast_add_profile(const char *name, uint64_t scale)
allocates a counter with a given name and scale.
Definition: asterisk.c:798
static void ast_cli_display_match_list(struct ast_vector_string *matches, int max)
Definition: asterisk.c:2925
static void _hup_handler(int num)
Definition: asterisk.c:1716
static char * handle_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2328
static pthread_t lthread
Definition: asterisk.c:1362
static unsigned int safe_system_level
Keep track of how many threads are currently trying to wait*() on a child process.
Definition: asterisk.c:1097
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:1293
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:669
#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:1218
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: asterisk.c:1055
static void set_header(char *outbuf, int maxout, char level)
Definition: asterisk.c:2153
static int ast_el_read_char(EditLine *editline, CHAR_T_LIBEDIT *cp)
Definition: asterisk.c:2664
static char * handle_stop_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2349
static char canary_filename[128]
Definition: asterisk.c:387
#define CMD_MATCHESARRAY
static __inline uint64_t rdtsc(void)
Definition: asterisk.c:866
static struct sigaction child_handler
Definition: asterisk.c:1755
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:1837
static void asterisk_daemon(int isroot, const char *runuser, const char *rungroup)
Definition: asterisk.c:4065
void ast_console_toggle_loglevel(int fd, int level, int state)
enable or disable a logging level to a specified console
Definition: asterisk.c:1247
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:1774
#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:1270
static void send_rasterisk_connect_commands(void)
Definition: asterisk.c:2628
unsigned int need_quit
Definition: asterisk.c:399
#define ASTERISK_PROMPT
Definition: asterisk.c:2592
static struct sigaction safe_system_prev_handler
Definition: asterisk.c:1098
static char * handle_bang(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2497
static struct sigaction hup_handler
Definition: asterisk.c:1735
static pthread_t mon_sig_flags
Definition: asterisk.c:385
static int fdprint(int fd, const char *s)
Definition: asterisk.c:1074
static pthread_t consolethread
Definition: asterisk.c:384
static EditLine * el
Definition: asterisk.c:340
static void * netconsole(void *vconsole)
Definition: asterisk.c:1424
static int ast_el_initialize(void)
Definition: asterisk.c:3078
static int set_priority_all(int pri)
Set priority on all known threads.
Definition: asterisk.c:1794
static void ast_begin_shutdown(void)
Definition: asterisk.c:1895
static char * handle_restart_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2411
static char * handle_restart_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2453
static int ast_el_read_history(const char *)
Definition: asterisk.c:3165
static char * handle_abort_shutdown(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:2474
int ast_shutting_down(void)
Definition: asterisk.c:1872
static void _child_handler(int sig)
Definition: asterisk.c:1740
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:1312
#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:972
void ast_replace_sigchld(void)
Replace the SIGCHLD handler.
Definition: asterisk.c:1100
int ast_cancel_shutdown(void)
Cancel an existing shutdown and return to normal operation.
Definition: asterisk.c:1877
static void ast_network_puts(const char *string)
write the string to all attached console clients
Definition: asterisk.c:1336
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:1374
#define CHAR_TO_LIBEDIT(c)
Definition: asterisk.c:2658
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:1080
static void process_histfile(int(*readwrite)(const char *filename))
Definition: asterisk.c:3176
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:2512
void ast_console_puts(const char *string)
write the string to the root console, and all attached network console clients
Definition: asterisk.c:1352
void ast_unregister_atexit(void(*func)(void))
Unregister a function registered with ast_register_atexit().
Definition: asterisk.c:1060
static int console_print(const char *s)
Definition: asterisk.c:2200
static void quit_handler(int num, shutdown_nice_t niceness, int restart)
Definition: asterisk.c:1907
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:2432
static void * monitor_sig_flags(void *unused)
Definition: asterisk.c:3440
#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:2537
#define MAX_HISTORY_COMMAND_LENGTH
Definition: asterisk.c:3131
void ast_register_thread(char *name)
Definition: asterisk.c:414
static void ast_remotecontrol(char *data)
Definition: asterisk.c:3213
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:2282
static struct ast_cli_entry cli_asterisk_shutdown[]
Shutdown Asterisk CLI commands.
Definition: asterisk.c:2602
int ast_register_atexit(void(*func)(void))
Register a function to be executed before Asterisk exits.
Definition: asterisk.c:1050
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:1093
static pid_t safe_exec_prep(int dualfork)
fork and perform other preparations for spawning applications
Definition: asterisk.c:1131
int ast_safe_system(const char *s)
Safely spawn an OS shell command while closing file descriptors.
Definition: asterisk.c:1231
static int ast_all_zeros(const char *s)
Definition: asterisk.c:2271
#define SHUTDOWN_TIMEOUT
Definition: asterisk.c:1916
static int can_safely_quit(shutdown_nice_t niceness, int restart)
Definition: asterisk.c:1959
static char * cli_prompt(EditLine *editline)
Definition: asterisk.c:2765
static void set_title(char *text)
Set an X-term or screen title.
Definition: asterisk.c:1761
static char * _argv[256]
Definition: asterisk.c:354
static int register_atexit(void(*func)(void), int is_cleanup)
Definition: asterisk.c:1031
static char * handle_clear_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:942
static int wait_for_channels_to_die(shutdown_nice_t niceness, int seconds)
Definition: asterisk.c:1929
static struct ast_threadstorage console_state
Definition: asterisk.c:2198
static int ast_makesocket(void)
Definition: asterisk.c:1594
#define DEFINE_PROFILE_MIN_MAX_VALUES
Definition: asterisk.c:894
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:1680
static void * canary_thread(void *unused)
Definition: asterisk.c:3465
static void * listener(void *unused)
Definition: asterisk.c:1514
static struct ast_cli_entry cli_asterisk[]
Definition: asterisk.c:2611
static int fdsend(int fd, const char *s)
Definition: asterisk.c:1068
static int ast_el_write_history(const char *)
Definition: asterisk.c:3155
static void run_startup_commands(void)
Definition: asterisk.c:3504
static int show_cli_help(void)
Definition: asterisk.c:3367
static void ast_el_read_default_histfile(void)
Definition: asterisk.c:3203
static char * handle_show_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: asterisk.c:907
static void env_init(void)
Definition: asterisk.c:3532
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:3406
static const char license_lines[]
Definition: asterisk.c:2555
void ast_unreplace_sigchld(void)
Restore the SIGCHLD handler.
Definition: asterisk.c:1115
static void really_quit(int num, shutdown_nice_t niceness, int restart)
Definition: asterisk.c:2042
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:3208
#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:2891
static void __quit_handler(int num)
Definition: asterisk.c:2138
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:1319
static void set_icon(char *text)
Definition: asterisk.c:1767
static void print_intro_message(const char *runuser, const char *rungroup)
Definition: asterisk.c:3544
static struct sigaction ignore_sig_handler
Definition: asterisk.c:1089
static struct sigaction null_sig_handler
Definition: asterisk.c:1084
struct console consoles[AST_MAX_CONNECTS]
Definition: asterisk.c:343
int64_t ast_profile(int i, int64_t delta)
Definition: asterisk.c:832
static struct sigaction urg_handler
Definition: asterisk.c:1712
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:370
#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:2923
void ast_cdr_engine_term(void)
Definition: cdr.c:4665
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
int ast_active_channels(void)
returns number of active/allocated channels
Definition: channel.c:499
Standard Command Line Interface.
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define AST_CLI_COMPLETE_EOF
Definition: cli.h:52
#define CLI_SUCCESS
Definition: cli.h:44
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
int ast_cli_allow_at_shutdown(struct ast_cli_entry *e)
Allow a CLI command to be executed while Asterisk is shutting down.
Definition: main/cli.c:3056
int ast_cli_command_multiple_full(int uid, int gid, int fd, size_t size, const char *s)
Executes multiple CLI commands Interpret strings separated by NULL and execute each one,...
Definition: main/cli.c:3034
ast_cli_command
calling arguments for new-style handlers.
Definition: cli.h:151
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
struct ast_vector_string * ast_cli_completion_vector(const char *text, const char *word)
Generates a vector of strings for CLI completion.
Definition: main/cli.c:2761
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
Codec API.
int ast_codec_init(void)
Initialize codec support within the core.
Definition: codec.c:250
int ast_codec_builtin_init(void)
Initialize built-in codecs within the core.
Device state management.
int devstate_init(void)
Initialize the device state core.
Definition: devicestate.c:896
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
Endpoint abstractions.
void ast_verbose(const char *fmt,...)
Definition: extconf.c:2206
#define min(a, b)
Definition: f2c.h:197
#define max(a, b)
Definition: f2c.h:198
Generic File Format Support. Should be included by clients of the file handling routines....
Media Format API.
int ast_format_init(void)
Initialize media format support.
Definition: format.c:77
Media Format Cache API.
int ast_format_cache_init(void)
Initialize format cache support within the core.
Definition: format_cache.c:364
static const char name[]
Definition: format_mp3.c:68
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int ast_manager_check_enabled(void)
Check if AMI is enabled.
Definition: manager.c:2101
int ast_webmanager_check_enabled(void)
Check if AMI/HTTP is enabled.
Definition: manager.c:2106
void ast_manager_publish_event(const char *type, int class_type, struct ast_json *obj)
Publish an event to AMI.
Definition: manager.c:2055
void ast_init_logger_for_socket_console(void)
load logger.conf configuration for console socket connections
Definition: logger.c:701
int ast_option_rtpusedynamic
Definition: options.c:88
int ast_option_maxfiles
Definition: options.c:81
int option_debug
Definition: options.c:69
int ast_verb_sys_level
Definition: options.c:64
int ast_option_maxcalls
Definition: options.c:79
int ast_option_pjproject_log_level
Definition: options.c:74
double ast_option_maxload
Definition: options.c:77
unsigned int ast_option_rtpptdynamic
Definition: options.c:89
int option_verbose
Definition: options.c:67
struct ast_flags ast_options
Definition: options.c:61
unsigned int option_dtmfminduration
Definition: options.c:83
int ast_option_pjproject_cache_pools
Definition: options.c:75
long option_minmemfree
Definition: options.c:86
int option_trace
Definition: options.c:71
@ AST_OPT_FLAG_HIGH_PRIORITY
Definition: options.h:48
@ AST_OPT_FLAG_GENERIC_PLC
Definition: options.h:100
@ AST_OPT_FLAG_TRANSCODE_VIA_SLIN
Definition: options.h:60
@ AST_OPT_FLAG_EXEC_INCLUDES
Definition: options.h:40
@ AST_OPT_FLAG_NO_COLOR
Definition: options.h:56
@ AST_OPT_FLAG_NO_FORK
Definition: options.h:42
@ AST_OPT_FLAG_TRANSMIT_SILENCE
Definition: options.h:74
@ AST_OPT_FLAG_ALWAYS_FORK
Definition: options.h:82
@ AST_OPT_FLAG_QUIET
Definition: options.h:44
@ AST_OPT_FLAG_GENERIC_PLC_ON_EQUAL_CODECS
Definition: options.h:102
@ AST_OPT_FLAG_MUTE
Definition: options.h:84
@ AST_OPT_FLAG_TIMESTAMP
Definition: options.h:68
@ AST_OPT_FLAG_EXEC
Definition: options.h:54
@ AST_OPT_FLAG_CACHE_RECORD_FILES
Definition: options.h:66
@ AST_OPT_FLAG_FULLY_BOOTED
Definition: options.h:58
@ AST_OPT_FLAG_INIT_KEYS
Definition: options.h:50
@ AST_OPT_FLAG_CONSOLE
Definition: options.h:46
@ AST_OPT_FLAG_FORCE_BLACK_BACKGROUND
Definition: options.h:94
@ AST_OPT_FLAG_LIGHT_BACKGROUND
Definition: options.h:90
@ AST_OPT_FLAG_REMOTE
Definition: options.h:52
@ AST_OPT_FLAG_DUMP_CORE
Definition: options.h:64
@ AST_OPT_FLAG_RECONNECT
Definition: options.h:72
static ENTRY retval
Definition: hsearch.c:50
static char prefix[MAX_PREFIX]
Definition: http.c:144
General Asterisk channel definitions for image handling.
int ast_image_init(void)
Initialize image stuff Initializes all the various image stuff. Basically just registers the cli stuf...
Definition: image.c:212
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
int app_init(void)
Initialize the application core.
Definition: main/app.c:3355
void ast_close_fds_above_n(int n)
Common routine for child processes, to close all fds prior to exec(2)
Definition: main/app.c:3192
int setenv(const char *name, const char *value, int overwrite)
int getloadavg(double *list, int nelem)
char * strsep(char **str, const char *delims)
Configuration File Parser.
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
Definition: main/config.c:3321
#define CONFIG_STATUS_FILEMISSING
int ast_realtime_enabled(void)
Check if there's any realtime engines loaded.
Definition: main/config.c:3544
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1215
@ CONFIG_FLAG_NOCACHE
@ CONFIG_FLAG_NOREALTIME
int register_config_cli(void)
Exposed initialization method for core process.
Definition: main/config.c:4236
#define VERBOSE_PREFIX_3
void ast_verb_console_unregister(void)
Unregister this thread's console verbosity level.
Definition: logger.c:2650
void ast_verb_console_register(int *level)
Register this thread's console verbosity level pointer.
Definition: logger.c:2634
#define NUMLOGLEVELS
#define ast_debug(level,...)
Log a DEBUG message.
int ast_is_logger_initialized(void)
Test if logger is initialized.
Definition: logger.c:2163
#define VERBOSE_PREFIX_2
#define VERBOSE_PREFIX_4
#define __LOG_VERBOSE
#define VERBOSE_PREFIX_1
#define LOG_ERROR
#define ast_verb(level,...)
#define LOG_WARNING
int ast_verb_console_get(void)
Get this thread's console verbosity level.
Definition: logger.c:2661
const char * ast_logger_get_dateformat(void)
Get the logger configured date format.
Definition: logger.c:2915
#define VERBOSE_MAGIC2LEVEL(x)
#define VERBOSE_HASMAGIC(x)
I/O Management (derived from Cheops-NG)
int ast_get_termcols(int fd)
Columns of Terminal.
Definition: io.c:373
int ast_sd_notify(const char *state)
a wrapper for sd_notify(): notify systemd of any state changes.
Definition: io.c:392
int ast_sd_get_fd_un(int type, const char *path)
Find a listening AF_LOCAL file descriptor provided by socket activation.
Definition: io.c:454
Asterisk JSON abstraction layer.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
int ast_json_init(void)
Initialize the JSON library.
Definition: json.c:726
A set of macros to manage forward-linked lists.
#define AST_RWLIST_REMOVE_CURRENT
Definition: linkedlists.h:570
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define AST_LIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
Definition: linkedlists.h:291
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:545
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized.
Definition: linkedlists.h:333
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:410
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:617
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:40
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:494
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:711
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:718
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:140
#define AST_RWLIST_ENTRY
Definition: linkedlists.h:415
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
Asterisk locking-related definitions:
#define AST_PTHREADT_NULL
Definition: lock.h:66
#define ast_mutex_unlock(a)
Definition: lock.h:190
#define ast_mutex_lock(a)
Definition: lock.h:189
#define AST_MUTEX_DEFINE_STATIC(mutex)
Definition: lock.h:520
static char * levels[NUMLOGLEVELS]
Logging channels used in the Asterisk logging system.
Definition: logger.c:214
static char hostname[MAXHOSTNAMELEN]
Definition: logger.c:119
#define WEXITSTATUS(status)
#define WIFEXITED(status)
int errno
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
#define EVENT_FLAG_SYSTEM
Definition: manager.h:75
An in-memory media cache.
int ast_media_cache_init(void)
Initialize the media cache.
Definition: media_cache.c:671
Asterisk module definitions.
enum ast_module_reload_result ast_module_reload(const char *name)
Reload asterisk modules.
Definition: loader.c:1567
Asterisk MWI API.
int mwi_init(void)
Initialize the mwi core.
Definition: mwi.c:507
Wrapper for network related headers, masking differences between various operating systems....
#define MAXHOSTNAMELEN
Definition: network.h:69
Options provided by main asterisk program.
#define ast_opt_dump_core
Definition: options.h:119
#define ast_opt_remote
Definition: options.h:114
int ast_language_is_prefix
The following variable controls the layout of localized sound files. If 0, use the historical layout ...
Definition: file.c:67
#define MAX_PJ_LOG_MAX_LEVEL
Definition: options.h:141
#define ast_opt_exec
Definition: options.h:115
#define ast_opt_reconnect
Definition: options.h:123
#define DEFAULT_PJPROJECT_CACHE_POOLS
Definition: options.h:178
#define ast_opt_console
Definition: options.h:111
#define ast_opt_hide_connect
Definition: options.h:132
#define ast_opt_mute
Definition: options.h:127
#define ast_opt_high_priority
Definition: options.h:112
#define ast_opt_timestamp
Definition: options.h:122
#define ast_opt_always_fork
Definition: options.h:126
#define ast_opt_no_fork
Definition: options.h:109
#define ast_opt_hide_messaging_ami_events
Definition: options.h:137
#define ast_opt_sounds_search_custom
Definition: options.h:138
#define DEFAULT_PJ_LOG_MAX_LEVEL
Definition: options.h:149
#define ast_opt_cache_media_frames
Definition: options.h:121
char ast_defaultlanguage[]
Definition: options.c:98
Asterisk file paths, configured in asterisk.conf.
const char * ast_config_AST_KEY_DIR
Definition: options.c:161
const char * ast_config_AST_RUN_GROUP
Definition: options.c:169
const char * ast_config_AST_SOCKET
Definition: options.c:167
const char * ast_config_AST_RUN_USER
Definition: options.c:168
const char * ast_config_AST_CTL_PERMISSIONS
Definition: options.c:172
const char * ast_config_AST_CTL_GROUP
Definition: options.c:174
const char * ast_config_AST_MODULE_DIR
Definition: options.c:153
const char * ast_config_AST_PID
Definition: options.c:166
const char * ast_config_AST_RUN_DIR
Definition: options.c:162
const char * ast_config_AST_DATA_DIR
Definition: options.c:158
const char * ast_config_AST_CONFIG_DIR
Definition: options.c:151
const char * ast_config_AST_SPOOL_DIR
Definition: options.c:154
const char * ast_config_AST_AGI_DIR
Definition: options.c:160
const char * ast_config_AST_VAR_DIR
Definition: options.c:157
const char * ast_config_AST_SYSTEM_NAME
Definition: options.c:170
const char * ast_config_AST_SBIN_DIR
Definition: options.c:163
const char * ast_config_AST_CONFIG_FILE
Definition: options.c:152
const char * ast_config_AST_LOG_DIR
Definition: options.c:159
const char * ast_config_AST_CTL_OWNER
Definition: options.c:173
const char * ast_config_AST_DB
Definition: options.c:165
Core PBX routines and definitions.
Call Pickup API.
int ast_pickup_init(void)
Initialize pickup.
Definition: pickup.c:399
#define ast_poll(a, b, c)
Definition: poll-compat.h:88
Presence state management.
int ast_presence_state_engine_init(void)
static int total
Definition: res_adsi.c:970
#define NULL
Definition: resample.c:96
Pluggable RTP Architecture.
#define AST_RTP_PT_LAST_REASSIGN
Definition: rtp_engine.h:95
#define AST_RTP_MAX_PT
Definition: rtp_engine.h:83
#define AST_RTP_PT_FIRST_DYNAMIC
Definition: rtp_engine.h:92
Scheduler Routines (derived from cheops)
Security Event Reporting API.
int ast_security_stasis_init(void)
initializes stasis topic/event types for ast_security_topic and ast_security_event_type
#define FD_SET(fd, fds)
Definition: select.h:58
#define ast_FDMAX
Definition: select.h:41
unsigned int ast_FD_SETSIZE
Definition: poll.c:86
static int ast_select(int nfds, ast_fdset *rfds, ast_fdset *wfds, ast_fdset *efds, struct timeval *tvp)
Waits for activity on a group of channels.
Definition: select.h:79
#define FD_ZERO(a)
Definition: select.h:49
Sorcery Data Access Layer API.
int ast_sorcery_init(void)
Initialize the sorcery API.
Definition: sorcery.c:387
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
int stasis_init(void)
Initialize the Stasis subsystem.
Definition: stasis.c:3061
Endpoint abstractions.
int ast_endpoint_stasis_init(void)
Initialization function for endpoint stasis support.
int ast_stasis_system_init(void)
Initialize the stasis system topic and message types.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is "false"....
Definition: utils.c:2216
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:693
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:223
int is_cleanup
Definition: asterisk.c:330
void(* func)(void)
Definition: asterisk.c:329
struct ast_atexit::@298 list
descriptor for a cli entry.
Definition: cli.h:171
int args
This gets set in ast_cli_register()
Definition: cli.h:185
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
Structure used to handle boolean flags.
Definition: utils.h:199
Abstract JSON element (object, array, string, int, ...).
Support for dynamic strings.
Definition: strings.h:623
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
String vector definitions.
Definition: vector.h:55
int mute
Definition: asterisk.c:320
int uid
Definition: asterisk.c:321
int gid
Definition: asterisk.c:322
int p[2]
Definition: asterisk.c:318
int fd
Definition: asterisk.c:317
int option_verbose
Definition: asterisk.c:325
pthread_t t
Definition: asterisk.c:319
int levels[NUMLOGLEVELS]
Definition: asterisk.c:323
Definition: search.h:40
int max_size
Definition: asterisk.c:788
struct profile_entry e[0]
Definition: asterisk.c:789
Definition: asterisk.c:778
int64_t events
Definition: asterisk.c:783
int64_t mark
Definition: asterisk.c:781
const char * name
Definition: asterisk.c:779
int64_t value
Definition: asterisk.c:782
uint64_t scale
Definition: asterisk.c:780
Definition: sched.c:76
TTY/TDD Generation support.
void tdd_init(void)
Definition: tdd.c:94
Handy terminal functions for vt* terms.
#define COLOR_BRGREEN
Definition: term.h:55
const char * term_quit(void)
Definition: term.c:412
#define COLOR_GRAY
Definition: term.h:51
int ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor)
Append a color sequence to an ast_str.
Definition: term.c:296
const char * ast_term_reset(void)
Returns the terminal reset code.
Definition: term.c:357
#define COLOR_WHITE
Definition: term.h:64
const char * ast_term_color(int fgcolor, int bgcolor)
Return a color sequence string.
Definition: term.c:341
const char * term_end(void)
Definition: term.c:407
#define COLOR_BLACK
Definition: term.h:50
#define COLORIZE(fg, bg, str)
Definition: term.h:72
#define COLORIZE_FMT
Shortcut macros for coloring a set of text.
Definition: term.h:71
Test Framework API.
static struct test_val a
static struct test_val c
#define AST_THREADSTORAGE_CUSTOM(a, b, c)
Define a thread storage variable, with custom initialization and cleanup.
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition: extconf.c:2297
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
Support for translation of data formats. translate.c.
int ast_translate_init(void)
Initialize the translation matrix and index to format conversion table.
Definition: translate.c:1675
u-Law to Signed linear conversion
void ast_ulaw_init(void)
Set up mu-law conversion table.
Definition: ulaw.c:173
UTF-8 information and validation functions.
int ast_utf8_init(void)
Register UTF-8 tests.
Definition: utf8.c:919
static int inbuf(struct baseio *bio, FILE *fi)
utility used by inchar(), for base_encode()
Definition: utils.c:590
Utility functions.
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
#define ast_assert(a)
Definition: utils.h:739
#define ast_pthread_create_background(a, b, c, d)
Definition: utils.h:592
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
Definition: utils.c:2839
int ast_get_tid(void)
Get current thread ID.
Definition: utils.c:2752
#define ast_clear_flag(p, flag)
Definition: utils.h:77
int ast_utils_init(void)
Definition: utils.c:2617
#define ast_pthread_create_detached(a, b, c, d)
Definition: utils.h:588
#define ast_fd_set_flags(fd, flags)
Set flags on the given file descriptor.
Definition: utils.h:1039
#define ast_pthread_create_detached_background(a, b, c, d)
Definition: utils.h:597
#define ast_set_flag(p, flag)
Definition: utils.h:70
#define ARRAY_LEN(a)
Definition: utils.h:666
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
Universally unique identifier support.
#define AST_UUID_STR_LEN
Definition: uuid.h:27
void ast_uuid_init(void)
Initialize the UUID system.
Definition: uuid.c:192
char * ast_uuid_generate_str(char *buf, size_t size)
Generate a UUID string.
Definition: uuid.c:141
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
#define AST_VECTOR_PTR_FREE(vec)
Deallocates this vector pointer.
Definition: vector.h:189
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
Definition: vector.h:862
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680
Asterisk XML Documentation API.