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