utils: Add convenience function for setting fd flags
[asterisk/asterisk.git] / main / asterisk.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2016, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19
20 /* Doxygenified Copyright Header */
21 /*!
22  * \mainpage Asterisk -- The Open Source Telephony Project
23  *
24  * \par Welcome
25  *
26  * This documentation created by the Doxygen project clearly explains the
27  * internals of the Asterisk software. This documentation contains basic
28  * examples, developer documentation, support information, and information
29  * for upgrading.
30  *
31  * \section community Community
32  * Asterisk is a big project and has a busy community. Look at the
33  * resources for questions and stick around to help answer questions.
34  * \li \ref asterisk_community_resources
35  *
36  * \par Developer Documentation for Asterisk
37  *
38  * This is the main developer documentation for Asterisk. It is
39  * generated by running "make progdocs" from the Asterisk source tree.
40  *
41  * In addition to the information available on the Asterisk source code,
42  * please see the appendices for information on coding guidelines,
43  * release management, commit policies, and more.
44  *
45  * \arg \ref AsteriskArchitecture
46  *
47  * \par Additional documentation
48  * \arg \ref Licensing
49  * \arg \ref DevDoc
50  * \arg \ref configuration_file
51  * \arg \ref channel_drivers
52  * \arg \ref applications
53  *
54  * \section copyright Copyright and Author
55  *
56  * Copyright (C) 1999 - 2016, Digium, Inc.
57  * Asterisk is a <a href="http://www.digium.com/en/company/view-policy.php?id=Trademark-Policy">registered trademark</a>
58  * of <a rel="nofollow" href="http://www.digium.com">Digium, Inc</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 http://www.asterisk.org Asterisk Homepage
74  * \li http://wiki.asterisk.org Asterisk Wiki
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 http://www.freenode.net 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__)
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)
184 int 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/features.h"
203 #include "asterisk/acl.h"
204 #include "asterisk/ulaw.h"
205 #include "asterisk/alaw.h"
206 #include "asterisk/callerid.h"
207 #include "asterisk/image.h"
208 #include "asterisk/tdd.h"
209 #include "asterisk/term.h"
210 #include "asterisk/manager.h"
211 #include "asterisk/cdr.h"
212 #include "asterisk/cel.h"
213 #include "asterisk/pbx.h"
214 #include "asterisk/enum.h"
215 #include "asterisk/http.h"
216 #include "asterisk/udptl.h"
217 #include "asterisk/app.h"
218 #include "asterisk/lock.h"
219 #include "asterisk/utils.h"
220 #include "asterisk/file.h"
221 #include "asterisk/io.h"
222 #include "editline/histedit.h"
223 #include "asterisk/config.h"
224 #include "asterisk/ast_version.h"
225 #include "asterisk/linkedlists.h"
226 #include "asterisk/devicestate.h"
227 #include "asterisk/presencestate.h"
228 #include "asterisk/module.h"
229 #include "asterisk/dsp.h"
230 #include "asterisk/buildinfo.h"
231 #include "asterisk/xmldoc.h"
232 #include "asterisk/poll-compat.h"
233 #include "asterisk/ccss.h"
234 #include "asterisk/test.h"
235 #include "asterisk/rtp_engine.h"
236 #include "asterisk/format.h"
237 #include "asterisk/aoc.h"
238 #include "asterisk/uuid.h"
239 #include "asterisk/sorcery.h"
240 #include "asterisk/bucket.h"
241 #include "asterisk/stasis.h"
242 #include "asterisk/json.h"
243 #include "asterisk/stasis_endpoints.h"
244 #include "asterisk/stasis_system.h"
245 #include "asterisk/security_events.h"
246 #include "asterisk/endpoints.h"
247 #include "asterisk/codec.h"
248 #include "asterisk/format_cache.h"
249 #include "asterisk/media_cache.h"
250 #include "asterisk/astdb.h"
251 #include "asterisk/options.h"
252
253 #include "../defaults.h"
254
255 /*** DOCUMENTATION
256         <managerEvent language="en_US" name="FullyBooted">
257                 <managerEventInstance class="EVENT_FLAG_SYSTEM">
258                         <synopsis>Raised when all Asterisk initialization procedures have finished.</synopsis>
259                         <syntax>
260                                 <parameter name="Status">
261                                         <para>Informational message</para>
262                                 </parameter>
263                                 <parameter name="Uptime">
264                                         <para>Seconds since start</para>
265                                 </parameter>
266                                 <parameter name="LastReload">
267                                         <para>Seconds since last reload</para>
268                                 </parameter>
269                         </syntax>
270                 </managerEventInstance>
271         </managerEvent>
272         <managerEvent language="en_US" name="Shutdown">
273                 <managerEventInstance class="EVENT_FLAG_SYSTEM">
274                         <synopsis>Raised when Asterisk is shutdown or restarted.</synopsis>
275                         <syntax>
276                                 <parameter name="Shutdown">
277                                         <para>Whether the shutdown is proceeding cleanly (all channels
278                                         were hungup successfully) or uncleanly (channels will be
279                                         terminated)</para>
280                                         <enumlist>
281                                                 <enum name="Uncleanly"/>
282                                                 <enum name="Cleanly"/>
283                                         </enumlist>
284                                 </parameter>
285                                 <parameter name="Restart">
286                                         <para>Whether or not a restart will occur.</para>
287                                         <enumlist>
288                                                 <enum name="True"/>
289                                                 <enum name="False"/>
290                                         </enumlist>
291                                 </parameter>
292                         </syntax>
293                 </managerEventInstance>
294         </managerEvent>
295  ***/
296
297 #ifndef AF_LOCAL
298 #define AF_LOCAL AF_UNIX
299 #define PF_LOCAL PF_UNIX
300 #endif
301
302 #define AST_MAX_CONNECTS 128
303 #define NUM_MSGS 64
304
305 /*! Default minimum DTMF digit length - 80ms */
306 #define AST_MIN_DTMF_DURATION 80
307
308
309 /*! \brief Welcome message when starting a CLI interface */
310 #define WELCOME_MESSAGE \
311     ast_verbose("Asterisk %s, Copyright (C) 1999 - 2016, Digium, Inc. and others.\n" \
312                 "Created by Mark Spencer <markster@digium.com>\n" \
313                 "Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for details.\n" \
314                 "This is free software, with components licensed under the GNU General Public\n" \
315                 "License version 2 and other licenses; you are welcome to redistribute it under\n" \
316                 "certain conditions. Type 'core show license' for details.\n" \
317                 "=========================================================================\n", ast_get_version()) \
318
319 /*! \defgroup main_options Main Configuration Options
320  * \brief Main configuration options from asterisk.conf or OS command line on starting Asterisk.
321  * \arg \ref Config_ast "asterisk.conf"
322  * \note Some of them can be changed in the CLI
323  */
324 /*! @{ */
325
326 struct ast_flags ast_options = { AST_DEFAULT_OPTIONS };
327
328 /*! Maximum active system verbosity level. */
329 int ast_verb_sys_level;
330
331 int option_verbose;                             /*!< Verbosity level */
332 int option_debug;                               /*!< Debug level */
333 int ast_pjproject_max_log_level = -1;/* Default to -1 to know if we have read the level from pjproject yet. */
334 int ast_option_pjproject_log_level;
335 double ast_option_maxload;                      /*!< Max load avg on system */
336 int ast_option_maxcalls;                        /*!< Max number of active calls */
337 int ast_option_maxfiles;                        /*!< Max number of open file handles (files, sockets) */
338 unsigned int option_dtmfminduration;            /*!< Minimum duration of DTMF. */
339 #if defined(HAVE_SYSINFO)
340 long option_minmemfree;                         /*!< Minimum amount of free system memory - stop accepting calls if free memory falls below this watermark */
341 #endif
342 int ast_option_rtpusedynamic;
343 unsigned int ast_option_rtpptdynamic;
344
345 /*! @} */
346
347 struct ast_eid ast_eid_default;
348
349 /* XXX tmpdir is a subdir of the spool directory, and no way to remap it */
350 char record_cache_dir[AST_CACHE_DIR_LEN] = DEFAULT_TMP_DIR;
351
352 static int ast_socket = -1;             /*!< UNIX Socket for allowing remote control */
353 static int ast_socket_is_sd = 0; /*!< Is socket activation responsible for ast_socket? */
354 static int ast_consock = -1;            /*!< UNIX Socket for controlling another asterisk */
355 pid_t ast_mainpid;
356 struct console {
357         int fd;                         /*!< File descriptor */
358         int p[2];                       /*!< Pipe */
359         pthread_t t;                    /*!< Thread of handler */
360         int mute;                       /*!< Is the console muted for logs */
361         int uid;                        /*!< Remote user ID. */
362         int gid;                        /*!< Remote group ID. */
363         int levels[NUMLOGLEVELS];       /*!< Which log levels are enabled for the console */
364         /*! Verbosity level of this console. */
365         int option_verbose;
366 };
367
368 struct ast_atexit {
369         void (*func)(void);
370         int is_cleanup;
371         AST_LIST_ENTRY(ast_atexit) list;
372 };
373
374 static AST_LIST_HEAD_STATIC(atexits, ast_atexit);
375
376 struct timeval ast_startuptime;
377 struct timeval ast_lastreloadtime;
378
379 static History *el_hist;
380 static EditLine *el;
381 static char *remotehostname;
382
383 struct console consoles[AST_MAX_CONNECTS];
384
385 char ast_defaultlanguage[MAX_LANGUAGE] = DEFAULT_LANGUAGE;
386
387 static int ast_el_add_history(const char *);
388 static int ast_el_read_history(const char *);
389 static int ast_el_write_history(const char *);
390
391 static void ast_el_read_default_histfile(void);
392 static void ast_el_write_default_histfile(void);
393
394 static void asterisk_daemon(int isroot, const char *runuser, const char *rungroup);
395
396 #define DEFAULT_MONITOR_DIR DEFAULT_SPOOL_DIR "/monitor"
397 #define DEFAULT_RECORDING_DIR DEFAULT_SPOOL_DIR "/recording"
398
399 struct _cfg_paths {
400         char config_dir[PATH_MAX];
401         char module_dir[PATH_MAX];
402         char spool_dir[PATH_MAX];
403         char monitor_dir[PATH_MAX];
404         char recording_dir[PATH_MAX];
405         char var_dir[PATH_MAX];
406         char data_dir[PATH_MAX];
407         char log_dir[PATH_MAX];
408         char agi_dir[PATH_MAX];
409         char run_dir[PATH_MAX];
410         char key_dir[PATH_MAX];
411
412         char config_file[PATH_MAX];
413         char db_path[PATH_MAX];
414         char sbin_dir[PATH_MAX];
415         char pid_path[PATH_MAX];
416         char socket_path[PATH_MAX];
417         char run_user[PATH_MAX];
418         char run_group[PATH_MAX];
419         char system_name[128];
420 };
421
422 static struct _cfg_paths cfg_paths;
423
424 const char *ast_config_AST_CONFIG_DIR   = cfg_paths.config_dir;
425 const char *ast_config_AST_CONFIG_FILE  = cfg_paths.config_file;
426 const char *ast_config_AST_MODULE_DIR   = cfg_paths.module_dir;
427 const char *ast_config_AST_SPOOL_DIR    = cfg_paths.spool_dir;
428 const char *ast_config_AST_MONITOR_DIR  = cfg_paths.monitor_dir;
429 const char *ast_config_AST_RECORDING_DIR        = cfg_paths.recording_dir;
430 const char *ast_config_AST_VAR_DIR      = cfg_paths.var_dir;
431 const char *ast_config_AST_DATA_DIR     = cfg_paths.data_dir;
432 const char *ast_config_AST_LOG_DIR      = cfg_paths.log_dir;
433 const char *ast_config_AST_AGI_DIR      = cfg_paths.agi_dir;
434 const char *ast_config_AST_KEY_DIR      = cfg_paths.key_dir;
435 const char *ast_config_AST_RUN_DIR      = cfg_paths.run_dir;
436 const char *ast_config_AST_SBIN_DIR = cfg_paths.sbin_dir;
437
438 const char *ast_config_AST_DB           = cfg_paths.db_path;
439 const char *ast_config_AST_PID          = cfg_paths.pid_path;
440 const char *ast_config_AST_SOCKET       = cfg_paths.socket_path;
441 const char *ast_config_AST_RUN_USER     = cfg_paths.run_user;
442 const char *ast_config_AST_RUN_GROUP    = cfg_paths.run_group;
443 const char *ast_config_AST_SYSTEM_NAME  = cfg_paths.system_name;
444
445 static char ast_config_AST_CTL_PERMISSIONS[PATH_MAX];
446 static char ast_config_AST_CTL_OWNER[PATH_MAX] = "\0";
447 static char ast_config_AST_CTL_GROUP[PATH_MAX] = "\0";
448 static char ast_config_AST_CTL[PATH_MAX] = "asterisk.ctl";
449
450 extern unsigned int ast_FD_SETSIZE;
451
452 static char *_argv[256];
453
454 typedef enum {
455         /*! Normal operation */
456         NOT_SHUTTING_DOWN,
457         /*! Committed to shutting down.  Final phase */
458         SHUTTING_DOWN_FINAL,
459         /*! Committed to shutting down.  Initial phase */
460         SHUTTING_DOWN,
461         /*!
462          * Valid values for quit_handler() niceness below.
463          * These shutdown/restart levels can be cancelled.
464          *
465          * Remote console exit right now
466          */
467         SHUTDOWN_FAST,
468         /*! core stop/restart now */
469         SHUTDOWN_NORMAL,
470         /*! core stop/restart gracefully */
471         SHUTDOWN_NICE,
472         /*! core stop/restart when convenient */
473         SHUTDOWN_REALLY_NICE
474 } shutdown_nice_t;
475
476 static shutdown_nice_t shuttingdown = NOT_SHUTTING_DOWN;
477
478 /*! Prevent new channel allocation for shutdown. */
479 static int shutdown_pending;
480
481 static int restartnow;
482 static pthread_t consolethread = AST_PTHREADT_NULL;
483 static pthread_t mon_sig_flags;
484 static int canary_pid = 0;
485 static char canary_filename[128];
486 static int multi_thread_safe;
487
488 static char randompool[256];
489
490 static int sig_alert_pipe[2] = { -1, -1 };
491 static struct {
492          unsigned int need_reload:1;
493          unsigned int need_quit:1;
494          unsigned int need_quit_handler:1;
495 } sig_flags;
496
497 #if !defined(LOW_MEMORY)
498 struct thread_list_t {
499         AST_RWLIST_ENTRY(thread_list_t) list;
500         char *name;
501         pthread_t id;
502         int lwp;
503 };
504
505 static AST_RWLIST_HEAD_STATIC(thread_list, thread_list_t);
506
507 void ast_register_thread(char *name)
508 {
509         struct thread_list_t *new = ast_calloc(1, sizeof(*new));
510
511         if (!new)
512                 return;
513
514         ast_assert(multi_thread_safe);
515         new->id = pthread_self();
516         new->lwp = ast_get_tid();
517         new->name = name; /* steal the allocated memory for the thread name */
518         AST_RWLIST_WRLOCK(&thread_list);
519         AST_RWLIST_INSERT_HEAD(&thread_list, new, list);
520         AST_RWLIST_UNLOCK(&thread_list);
521 }
522
523 void ast_unregister_thread(void *id)
524 {
525         struct thread_list_t *x;
526
527         AST_RWLIST_WRLOCK(&thread_list);
528         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&thread_list, x, list) {
529                 if ((void *) x->id == id) {
530                         AST_RWLIST_REMOVE_CURRENT(list);
531                         break;
532                 }
533         }
534         AST_RWLIST_TRAVERSE_SAFE_END;
535         AST_RWLIST_UNLOCK(&thread_list);
536         if (x) {
537                 ast_free(x->name);
538                 ast_free(x);
539         }
540 }
541
542 /*! \brief Give an overview of core settings */
543 static char *handle_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
544 {
545         char buf[BUFSIZ];
546         struct ast_tm tm;
547         char eid_str[128];
548         struct rlimit limits;
549         char pbx_uuid[AST_UUID_STR_LEN];
550
551         switch (cmd) {
552         case CLI_INIT:
553                 e->command = "core show settings";
554                 e->usage = "Usage: core show settings\n"
555                            "       Show core misc settings";
556                 return NULL;
557         case CLI_GENERATE:
558                 return NULL;
559         }
560
561         ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
562         ast_pbx_uuid_get(pbx_uuid, sizeof(pbx_uuid));
563
564         ast_cli(a->fd, "\nPBX Core settings\n");
565         ast_cli(a->fd, "-----------------\n");
566         ast_cli(a->fd, "  Version:                     %s\n", ast_get_version());
567         ast_cli(a->fd, "  Build Options:               %s\n", S_OR(ast_get_build_opts(), "(none)"));
568         if (ast_option_maxcalls)
569                 ast_cli(a->fd, "  Maximum calls:               %d (Current %d)\n", ast_option_maxcalls, ast_active_channels());
570         else
571                 ast_cli(a->fd, "  Maximum calls:               Not set\n");
572
573         if (getrlimit(RLIMIT_NOFILE, &limits)) {
574                 ast_cli(a->fd, "  Maximum open file handles:   Error because of %s\n", strerror(errno));
575         } else if (limits.rlim_cur == RLIM_INFINITY) {
576                 ast_cli(a->fd, "  Maximum open file handles:   Unlimited\n");
577         } else if (limits.rlim_cur < ast_option_maxfiles) {
578                 ast_cli(a->fd, "  Maximum open file handles:   %d (is) %d (requested)\n", (int) limits.rlim_cur, ast_option_maxfiles);
579         } else {
580                 ast_cli(a->fd, "  Maximum open file handles:   %d\n", (int) limits.rlim_cur);
581         }
582
583         ast_cli(a->fd, "  Root console verbosity:      %d\n", option_verbose);
584         ast_cli(a->fd, "  Current console verbosity:   %d\n", ast_verb_console_get());
585         ast_cli(a->fd, "  Debug level:                 %d\n", option_debug);
586         ast_cli(a->fd, "  Maximum load average:        %lf\n", ast_option_maxload);
587 #if defined(HAVE_SYSINFO)
588         ast_cli(a->fd, "  Minimum free memory:         %ld MB\n", option_minmemfree);
589 #endif
590         if (ast_localtime(&ast_startuptime, &tm, NULL)) {
591                 ast_strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
592                 ast_cli(a->fd, "  Startup time:                %s\n", buf);
593         }
594         if (ast_localtime(&ast_lastreloadtime, &tm, NULL)) {
595                 ast_strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
596                 ast_cli(a->fd, "  Last reload time:            %s\n", buf);
597         }
598         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);
599         ast_cli(a->fd, "  System name:                 %s\n", ast_config_AST_SYSTEM_NAME);
600         ast_cli(a->fd, "  Entity ID:                   %s\n", eid_str);
601         ast_cli(a->fd, "  PBX UUID:                    %s\n", pbx_uuid);
602         ast_cli(a->fd, "  Default language:            %s\n", ast_defaultlanguage);
603         ast_cli(a->fd, "  Language prefix:             %s\n", ast_language_is_prefix ? "Enabled" : "Disabled");
604         ast_cli(a->fd, "  User name and group:         %s/%s\n", ast_config_AST_RUN_USER, ast_config_AST_RUN_GROUP);
605         ast_cli(a->fd, "  Executable includes:         %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_EXEC_INCLUDES) ? "Enabled" : "Disabled");
606         ast_cli(a->fd, "  Transcode via SLIN:          %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_TRANSCODE_VIA_SLIN) ? "Enabled" : "Disabled");
607         ast_cli(a->fd, "  Transmit silence during rec: %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_TRANSMIT_SILENCE) ? "Enabled" : "Disabled");
608         ast_cli(a->fd, "  Generic PLC:                 %s\n", ast_test_flag(&ast_options, AST_OPT_FLAG_GENERIC_PLC) ? "Enabled" : "Disabled");
609         ast_cli(a->fd, "  Min DTMF duration::          %u\n", option_dtmfminduration);
610 #if !defined(LOW_MEMORY)
611         ast_cli(a->fd, "  Cache media frames:          %s\n", ast_opt_cache_media_frames ? "Enabled" : "Disabled");
612 #endif
613         ast_cli(a->fd, "  RTP use dynamic payloads:    %u\n", ast_option_rtpusedynamic);
614
615         if (ast_option_rtpptdynamic == AST_RTP_PT_LAST_REASSIGN) {
616                 ast_cli(a->fd, "  RTP dynamic payload types:   %u,%u-%u\n",
617                         ast_option_rtpptdynamic,
618                         AST_RTP_PT_FIRST_DYNAMIC, AST_RTP_MAX_PT - 1);
619         } else if (ast_option_rtpptdynamic < AST_RTP_PT_LAST_REASSIGN) {
620                 ast_cli(a->fd, "  RTP dynamic payload types:   %u-%u,%u-%u\n",
621                         ast_option_rtpptdynamic, AST_RTP_PT_LAST_REASSIGN,
622                         AST_RTP_PT_FIRST_DYNAMIC, AST_RTP_MAX_PT - 1);
623         } else {
624                 ast_cli(a->fd, "  RTP dynamic payload types:   %u-%u\n",
625                         AST_RTP_PT_FIRST_DYNAMIC, AST_RTP_MAX_PT - 1);
626         }
627
628         ast_cli(a->fd, "\n* Subsystems\n");
629         ast_cli(a->fd, "  -------------\n");
630         ast_cli(a->fd, "  Manager (AMI):               %s\n", check_manager_enabled() ? "Enabled" : "Disabled");
631         ast_cli(a->fd, "  Web Manager (AMI/HTTP):      %s\n", check_webmanager_enabled() ? "Enabled" : "Disabled");
632         ast_cli(a->fd, "  Call data records:           %s\n", ast_cdr_is_enabled() ? "Enabled" : "Disabled");
633         ast_cli(a->fd, "  Realtime Architecture (ARA): %s\n", ast_realtime_enabled() ? "Enabled" : "Disabled");
634
635         /*! \todo we could check musiconhold, voicemail, smdi, adsi, queues  */
636
637         ast_cli(a->fd, "\n* Directories\n");
638         ast_cli(a->fd, "  -------------\n");
639         ast_cli(a->fd, "  Configuration file:          %s\n", ast_config_AST_CONFIG_FILE);
640         ast_cli(a->fd, "  Configuration directory:     %s\n", ast_config_AST_CONFIG_DIR);
641         ast_cli(a->fd, "  Module directory:            %s\n", ast_config_AST_MODULE_DIR);
642         ast_cli(a->fd, "  Spool directory:             %s\n", ast_config_AST_SPOOL_DIR);
643         ast_cli(a->fd, "  Log directory:               %s\n", ast_config_AST_LOG_DIR);
644         ast_cli(a->fd, "  Run/Sockets directory:       %s\n", ast_config_AST_RUN_DIR);
645         ast_cli(a->fd, "  PID file:                    %s\n", ast_config_AST_PID);
646         ast_cli(a->fd, "  VarLib directory:            %s\n", ast_config_AST_VAR_DIR);
647         ast_cli(a->fd, "  Data directory:              %s\n", ast_config_AST_DATA_DIR);
648         ast_cli(a->fd, "  ASTDB:                       %s\n", ast_config_AST_DB);
649         ast_cli(a->fd, "  IAX2 Keys directory:         %s\n", ast_config_AST_KEY_DIR);
650         ast_cli(a->fd, "  AGI Scripts directory:       %s\n", ast_config_AST_AGI_DIR);
651         ast_cli(a->fd, "\n\n");
652         return CLI_SUCCESS;
653 }
654
655 static char *handle_show_threads(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
656 {
657         int count = 0;
658         struct thread_list_t *cur;
659         switch (cmd) {
660         case CLI_INIT:
661                 e->command = "core show threads";
662                 e->usage =
663                         "Usage: core show threads\n"
664                         "       List threads currently active in the system.\n";
665                 return NULL;
666         case CLI_GENERATE:
667                 return NULL;
668         }
669
670         AST_RWLIST_RDLOCK(&thread_list);
671         AST_RWLIST_TRAVERSE(&thread_list, cur, list) {
672                 ast_cli(a->fd, "%p %d %s\n", (void *)cur->id, cur->lwp, cur->name);
673                 count++;
674         }
675         AST_RWLIST_UNLOCK(&thread_list);
676         ast_cli(a->fd, "%d threads listed.\n", count);
677         return CLI_SUCCESS;
678 }
679
680 #if defined (HAVE_SYSCTL) && defined(HAVE_SWAPCTL)
681 /*
682  * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
683  * to be based on the new swapctl(2) system call.
684  */
685 static int swapmode(int *used, int *total)
686 {
687         struct swapent *swdev;
688         int nswap, rnswap, i;
689
690         nswap = swapctl(SWAP_NSWAP, 0, 0);
691         if (nswap == 0)
692                 return 0;
693
694         swdev = ast_calloc(nswap, sizeof(*swdev));
695         if (swdev == NULL)
696                 return 0;
697
698         rnswap = swapctl(SWAP_STATS, swdev, nswap);
699         if (rnswap == -1) {
700                 ast_free(swdev);
701                 return 0;
702         }
703
704         /* if rnswap != nswap, then what? */
705
706         /* Total things up */
707         *total = *used = 0;
708         for (i = 0; i < nswap; i++) {
709                 if (swdev[i].se_flags & SWF_ENABLE) {
710                         *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
711                         *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
712                 }
713         }
714         ast_free(swdev);
715         return 1;
716 }
717 #elif defined(HAVE_SYSCTL) && !defined(HAVE_SYSINFO)
718 static int swapmode(int *used, int *total)
719 {
720         *used = *total = 0;
721         return 1;
722 }
723 #endif
724
725 #if defined(HAVE_SYSINFO) || defined(HAVE_SYSCTL)
726 /*! \brief Give an overview of system statistics */
727 static char *handle_show_sysinfo(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
728 {
729         uint64_t physmem, freeram;
730         uint64_t freeswap = 0;
731         int nprocs = 0;
732         long uptime = 0;
733         int totalswap = 0;
734 #if defined(HAVE_SYSINFO)
735         struct sysinfo sys_info;
736         sysinfo(&sys_info);
737         uptime = sys_info.uptime / 3600;
738         physmem = sys_info.totalram * sys_info.mem_unit;
739         freeram = (sys_info.freeram * sys_info.mem_unit) / 1024;
740         totalswap = (sys_info.totalswap * sys_info.mem_unit) / 1024;
741         freeswap = (sys_info.freeswap * sys_info.mem_unit) / 1024;
742         nprocs = sys_info.procs;
743 #elif defined(HAVE_SYSCTL)
744         static int pageshift;
745         struct vmtotal vmtotal;
746         struct timeval  boottime;
747         time_t  now;
748         int mib[2], pagesize, usedswap = 0;
749         size_t len;
750         /* calculate the uptime by looking at boottime */
751         time(&now);
752         mib[0] = CTL_KERN;
753         mib[1] = KERN_BOOTTIME;
754         len = sizeof(boottime);
755         if (sysctl(mib, 2, &boottime, &len, NULL, 0) != -1) {
756                 uptime = now - boottime.tv_sec;
757         }
758         uptime = uptime/3600;
759         /* grab total physical memory  */
760         mib[0] = CTL_HW;
761 #if defined(HW_PHYSMEM64)
762         mib[1] = HW_PHYSMEM64;
763 #else
764         mib[1] = HW_PHYSMEM;
765 #endif
766         len = sizeof(physmem);
767         sysctl(mib, 2, &physmem, &len, NULL, 0);
768
769         pagesize = getpagesize();
770         pageshift = 0;
771         while (pagesize > 1) {
772                 pageshift++;
773                 pagesize >>= 1;
774         }
775
776         /* we only need the amount of log(2)1024 for our conversion */
777         pageshift -= 10;
778
779         /* grab vm totals */
780         mib[0] = CTL_VM;
781         mib[1] = VM_METER;
782         len = sizeof(vmtotal);
783         sysctl(mib, 2, &vmtotal, &len, NULL, 0);
784         freeram = (vmtotal.t_free << pageshift);
785         /* generate swap usage and totals */
786         swapmode(&usedswap, &totalswap);
787         freeswap = (totalswap - usedswap);
788         /* grab number of processes */
789 #if defined(__OpenBSD__)
790         mib[0] = CTL_KERN;
791         mib[1] = KERN_NPROCS;
792         len = sizeof(nprocs);
793         sysctl(mib, 2, &nprocs, &len, NULL, 0);
794 #endif
795 #endif
796
797         switch (cmd) {
798         case CLI_INIT:
799                 e->command = "core show sysinfo";
800                 e->usage =
801                         "Usage: core show sysinfo\n"
802                         "       List current system information.\n";
803                 return NULL;
804         case CLI_GENERATE:
805                 return NULL;
806         }
807
808         ast_cli(a->fd, "\nSystem Statistics\n");
809         ast_cli(a->fd, "-----------------\n");
810         ast_cli(a->fd, "  System Uptime:             %ld hours\n", uptime);
811         ast_cli(a->fd, "  Total RAM:                 %" PRIu64 " KiB\n", physmem / 1024);
812         ast_cli(a->fd, "  Free RAM:                  %" PRIu64 " KiB\n", freeram);
813 #if defined(HAVE_SYSINFO)
814         ast_cli(a->fd, "  Buffer RAM:                %" PRIu64 " KiB\n", ((uint64_t) sys_info.bufferram * sys_info.mem_unit) / 1024);
815 #endif
816 #if defined(HAVE_SWAPCTL) || defined(HAVE_SYSINFO)
817         ast_cli(a->fd, "  Total Swap Space:          %d KiB\n", totalswap);
818         ast_cli(a->fd, "  Free Swap Space:           %" PRIu64 " KiB\n\n", freeswap);
819 #endif
820         ast_cli(a->fd, "  Number of Processes:       %d \n\n", nprocs);
821         return CLI_SUCCESS;
822 }
823 #endif
824
825 struct profile_entry {
826         const char *name;
827         uint64_t        scale;  /* if non-zero, values are scaled by this */
828         int64_t mark;
829         int64_t value;
830         int64_t events;
831 };
832
833 struct profile_data {
834         int entries;
835         int max_size;
836         struct profile_entry e[0];
837 };
838
839 static struct profile_data *prof_data;
840 #endif /* ! LOW_MEMORY */
841
842 /*! \brief allocates a counter with a given name and scale.
843  * \return Returns the identifier of the counter.
844  */
845 int ast_add_profile(const char *name, uint64_t scale)
846 {
847 #if !defined(LOW_MEMORY)
848         int l = sizeof(struct profile_data);
849         int n = 10;     /* default entries */
850
851         if (prof_data == NULL) {
852                 prof_data = ast_calloc(1, l + n*sizeof(struct profile_entry));
853                 if (prof_data == NULL)
854                         return -1;
855                 prof_data->entries = 0;
856                 prof_data->max_size = n;
857         }
858         if (prof_data->entries >= prof_data->max_size) {
859                 void *p;
860                 n = prof_data->max_size + 20;
861                 p = ast_realloc(prof_data, l + n*sizeof(struct profile_entry));
862                 if (p == NULL)
863                         return -1;
864                 prof_data = p;
865                 prof_data->max_size = n;
866         }
867         n = prof_data->entries++;
868         prof_data->e[n].name = ast_strdup(name);
869         prof_data->e[n].value = 0;
870         prof_data->e[n].events = 0;
871         prof_data->e[n].mark = 0;
872         prof_data->e[n].scale = scale;
873         return n;
874 #else /* if defined(LOW_MEMORY) */
875         return 0;
876 #endif
877 }
878
879 int64_t ast_profile(int i, int64_t delta)
880 {
881 #if !defined(LOW_MEMORY)
882         if (!prof_data || i < 0 || i > prof_data->entries)      /* invalid index */
883                 return 0;
884         if (prof_data->e[i].scale > 1)
885                 delta /= prof_data->e[i].scale;
886         prof_data->e[i].value += delta;
887         prof_data->e[i].events++;
888         return prof_data->e[i].value;
889 #else /* if defined(LOW_MEMORY) */
890         return 0;
891 #endif
892 }
893
894 #if !defined(LOW_MEMORY)
895 /* The RDTSC instruction was introduced on the Pentium processor and is not
896  * implemented on certain clones, like the Cyrix 586. Hence, the previous
897  * expectation of __i386__ was in error. */
898 #if defined ( __i686__) && (defined(__FreeBSD__) || defined(linux))
899 #if defined(__FreeBSD__)
900 #include <machine/cpufunc.h>
901 #elif defined(linux)
902 static __inline uint64_t
903 rdtsc(void)
904 {
905         uint64_t rv;
906
907         __asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
908         return (rv);
909 }
910 #endif
911 #else   /* supply a dummy function on other platforms */
912 static __inline uint64_t
913 rdtsc(void)
914 {
915         return 0;
916 }
917 #endif
918 #endif /* ! LOW_MEMORY */
919
920 int64_t ast_mark(int i, int startstop)
921 {
922 #if !defined(LOW_MEMORY)
923         if (!prof_data || i < 0 || i > prof_data->entries) /* invalid index */
924                 return 0;
925         if (startstop == 1)
926                 prof_data->e[i].mark = rdtsc();
927         else {
928                 prof_data->e[i].mark = (rdtsc() - prof_data->e[i].mark);
929                 if (prof_data->e[i].scale > 1)
930                         prof_data->e[i].mark /= prof_data->e[i].scale;
931                 prof_data->e[i].value += prof_data->e[i].mark;
932                 prof_data->e[i].events++;
933         }
934         return prof_data->e[i].mark;
935 #else /* if defined(LOW_MEMORY) */
936         return 0;
937 #endif
938 }
939
940 #if !defined(LOW_MEMORY)
941 #define DEFINE_PROFILE_MIN_MAX_VALUES min = 0; \
942         max = prof_data->entries;\
943         if  (a->argc > 3) { /* specific entries */ \
944                 if (isdigit(a->argv[3][0])) { \
945                         min = atoi(a->argv[3]); \
946                         if (a->argc == 5 && strcmp(a->argv[4], "-")) \
947                                 max = atoi(a->argv[4]); \
948                 } else \
949                         search = a->argv[3]; \
950         } \
951         if (max > prof_data->entries) \
952                 max = prof_data->entries;
953
954 static char *handle_show_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
955 {
956         int i, min, max;
957         const char *search = NULL;
958         switch (cmd) {
959         case CLI_INIT:
960                 e->command = "core show profile";
961                 e->usage = "Usage: core show profile\n"
962                            "       show profile information";
963                 return NULL;
964         case CLI_GENERATE:
965                 return NULL;
966         }
967
968         if (prof_data == NULL)
969                 return 0;
970
971         DEFINE_PROFILE_MIN_MAX_VALUES;
972         ast_cli(a->fd, "profile values (%d, allocated %d)\n-------------------\n",
973                 prof_data->entries, prof_data->max_size);
974         ast_cli(a->fd, "%6s   %8s  %10s %12s %12s  %s\n", "ID", "Scale", "Events",
975                         "Value", "Average", "Name");
976         for (i = min; i < max; i++) {
977                 struct profile_entry *entry = &prof_data->e[i];
978                 if (!search || strstr(entry->name, search))
979                     ast_cli(a->fd, "%6d: [%8ld] %10ld %12lld %12lld  %s\n",
980                         i,
981                         (long)entry->scale,
982                         (long)entry->events, (long long)entry->value,
983                         (long long)(entry->events ? entry->value / entry->events : entry->value),
984                         entry->name);
985         }
986         return CLI_SUCCESS;
987 }
988
989 static char *handle_clear_profile(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
990 {
991         int i, min, max;
992         const char *search = NULL;
993         switch (cmd) {
994         case CLI_INIT:
995                 e->command = "core clear profile";
996                 e->usage = "Usage: core clear profile\n"
997                            "       clear profile information";
998                 return NULL;
999         case CLI_GENERATE:
1000                 return NULL;
1001         }
1002
1003         if (prof_data == NULL)
1004                 return 0;
1005
1006         DEFINE_PROFILE_MIN_MAX_VALUES;
1007         for (i= min; i < max; i++) {
1008                 if (!search || strstr(prof_data->e[i].name, search)) {
1009                         prof_data->e[i].value = 0;
1010                         prof_data->e[i].events = 0;
1011                 }
1012         }
1013         return CLI_SUCCESS;
1014 }
1015 #undef DEFINE_PROFILE_MIN_MAX_VALUES
1016
1017 #endif /* ! LOW_MEMORY */
1018
1019 int ast_pbx_uuid_get(char *pbx_uuid, int length)
1020 {
1021         return ast_db_get("pbx", "UUID", pbx_uuid, length);
1022 }
1023
1024 static void publish_fully_booted(void)
1025 {
1026         RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
1027         int uptime = 0;
1028         int lastreloaded = 0;
1029         struct timeval tmp;
1030         struct timeval curtime = ast_tvnow();
1031
1032         if (ast_startuptime.tv_sec) {
1033                 tmp = ast_tvsub(curtime, ast_startuptime);
1034                 uptime = (int) tmp.tv_sec;
1035         }
1036
1037         if (ast_lastreloadtime.tv_sec) {
1038                 tmp = ast_tvsub(curtime, ast_lastreloadtime);
1039                 lastreloaded = (int) tmp.tv_sec;
1040         }
1041
1042         json_object = ast_json_pack("{s: s, s: i, s: i}",
1043                         "Status", "Fully Booted",
1044                         "Uptime", uptime,
1045                         "LastReload", lastreloaded);
1046         ast_manager_publish_event("FullyBooted", EVENT_FLAG_SYSTEM, json_object);
1047 }
1048
1049 static void ast_run_atexits(int run_cleanups)
1050 {
1051         struct ast_atexit *ae;
1052
1053         AST_LIST_LOCK(&atexits);
1054         while ((ae = AST_LIST_REMOVE_HEAD(&atexits, list))) {
1055                 if (ae->func && (!ae->is_cleanup || run_cleanups)) {
1056                         ae->func();
1057                 }
1058                 ast_free(ae);
1059         }
1060         AST_LIST_UNLOCK(&atexits);
1061 }
1062
1063 static void __ast_unregister_atexit(void (*func)(void))
1064 {
1065         struct ast_atexit *ae;
1066
1067         AST_LIST_TRAVERSE_SAFE_BEGIN(&atexits, ae, list) {
1068                 if (ae->func == func) {
1069                         AST_LIST_REMOVE_CURRENT(list);
1070                         ast_free(ae);
1071                         break;
1072                 }
1073         }
1074         AST_LIST_TRAVERSE_SAFE_END;
1075 }
1076
1077 static int register_atexit(void (*func)(void), int is_cleanup)
1078 {
1079         struct ast_atexit *ae;
1080
1081         ae = ast_calloc(1, sizeof(*ae));
1082         if (!ae) {
1083                 return -1;
1084         }
1085         ae->func = func;
1086         ae->is_cleanup = is_cleanup;
1087
1088         AST_LIST_LOCK(&atexits);
1089         __ast_unregister_atexit(func);
1090         AST_LIST_INSERT_HEAD(&atexits, ae, list);
1091         AST_LIST_UNLOCK(&atexits);
1092
1093         return 0;
1094 }
1095
1096 int ast_register_atexit(void (*func)(void))
1097 {
1098         return register_atexit(func, 0);
1099 }
1100
1101 int ast_register_cleanup(void (*func)(void))
1102 {
1103         return register_atexit(func, 1);
1104 }
1105
1106 void ast_unregister_atexit(void (*func)(void))
1107 {
1108         AST_LIST_LOCK(&atexits);
1109         __ast_unregister_atexit(func);
1110         AST_LIST_UNLOCK(&atexits);
1111 }
1112
1113 /* Sending commands from consoles back to the daemon requires a terminating NULL */
1114 static int fdsend(int fd, const char *s)
1115 {
1116         return write(fd, s, strlen(s) + 1);
1117 }
1118
1119 /* Sending messages from the daemon back to the display requires _excluding_ the terminating NULL */
1120 static int fdprint(int fd, const char *s)
1121 {
1122         return write(fd, s, strlen(s));
1123 }
1124
1125 /*! \brief NULL handler so we can collect the child exit status */
1126 static void _null_sig_handler(int sig)
1127 {
1128 }
1129
1130 static struct sigaction null_sig_handler = {
1131         .sa_handler = _null_sig_handler,
1132         .sa_flags = SA_RESTART,
1133 };
1134
1135 static struct sigaction ignore_sig_handler = {
1136         .sa_handler = SIG_IGN,
1137 };
1138
1139 AST_MUTEX_DEFINE_STATIC(safe_system_lock);
1140 /*! \brief Keep track of how many threads are currently trying to wait*() on
1141  *  a child process
1142  */
1143 static unsigned int safe_system_level = 0;
1144 static struct sigaction safe_system_prev_handler;
1145
1146 void ast_replace_sigchld(void)
1147 {
1148         unsigned int level;
1149
1150         ast_mutex_lock(&safe_system_lock);
1151         level = safe_system_level++;
1152
1153         /* only replace the handler if it has not already been done */
1154         if (level == 0) {
1155                 sigaction(SIGCHLD, &null_sig_handler, &safe_system_prev_handler);
1156         }
1157
1158         ast_mutex_unlock(&safe_system_lock);
1159 }
1160
1161 void ast_unreplace_sigchld(void)
1162 {
1163         unsigned int level;
1164
1165         ast_mutex_lock(&safe_system_lock);
1166         level = --safe_system_level;
1167
1168         /* only restore the handler if we are the last one */
1169         if (level == 0) {
1170                 sigaction(SIGCHLD, &safe_system_prev_handler, NULL);
1171         }
1172
1173         ast_mutex_unlock(&safe_system_lock);
1174 }
1175
1176 /*! \brief fork and perform other preparations for spawning applications */
1177 static pid_t safe_exec_prep(int dualfork)
1178 {
1179         pid_t pid;
1180
1181 #if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
1182         ast_replace_sigchld();
1183
1184 #ifdef HAVE_WORKING_FORK
1185         pid = fork();
1186 #else
1187         pid = vfork();
1188 #endif
1189
1190         if (pid == 0) {
1191 #ifdef HAVE_CAP
1192                 cap_t cap = cap_from_text("cap_net_admin-eip");
1193
1194                 if (cap_set_proc(cap)) {
1195                         /* Careful with order! Logging cannot happen after we close FDs */
1196                         ast_log(LOG_WARNING, "Unable to remove capabilities.\n");
1197                 }
1198                 cap_free(cap);
1199 #endif
1200 #ifdef HAVE_WORKING_FORK
1201                 if (ast_opt_high_priority) {
1202                         ast_set_priority(0);
1203                 }
1204                 /* Close file descriptors and launch system command */
1205                 ast_close_fds_above_n(STDERR_FILENO);
1206 #endif
1207                 if (dualfork) {
1208 #ifdef HAVE_WORKING_FORK
1209                         pid = fork();
1210 #else
1211                         pid = vfork();
1212 #endif
1213                         if (pid < 0) {
1214                                 /* Second fork failed. */
1215                                 /* No logger available. */
1216                                 _exit(1);
1217                         }
1218
1219                         if (pid > 0) {
1220                                 /* This is the first fork, exit so the reaper finishes right away. */
1221                                 _exit(0);
1222                         }
1223
1224                         /* This is the second fork.  The first fork will exit immediately so
1225                          * Asterisk doesn't have to wait for completion.
1226                          * ast_safe_system("cmd &") would run in the background, but the '&'
1227                          * cannot be added with ast_safe_execvp, so we have to double fork.
1228                          */
1229                 }
1230         }
1231
1232         if (pid < 0) {
1233                 ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
1234         }
1235 #else
1236         ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(ENOTSUP));
1237         pid = -1;
1238 #endif
1239
1240         return pid;
1241 }
1242
1243 /*! \brief wait for spawned application to complete and unreplace sigchld */
1244 static int safe_exec_wait(pid_t pid)
1245 {
1246         int res = -1;
1247
1248 #if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
1249         if (pid > 0) {
1250                 for (;;) {
1251                         int status;
1252
1253                         res = waitpid(pid, &status, 0);
1254                         if (res > -1) {
1255                                 res = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
1256                                 break;
1257                         }
1258                         if (errno != EINTR) {
1259                                 break;
1260                         }
1261                 }
1262         }
1263
1264         ast_unreplace_sigchld();
1265 #endif
1266
1267         return res;
1268 }
1269
1270 int ast_safe_execvp(int dualfork, const char *file, char *const argv[])
1271 {
1272         pid_t pid = safe_exec_prep(dualfork);
1273
1274         if (pid == 0) {
1275                 execvp(file, argv);
1276                 _exit(1);
1277                 /* noreturn from _exit */
1278         }
1279
1280         return safe_exec_wait(pid);
1281 }
1282
1283 int ast_safe_system(const char *s)
1284 {
1285         pid_t pid = safe_exec_prep(0);
1286
1287         if (pid == 0) {
1288                 execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
1289                 _exit(1);
1290                 /* noreturn from _exit */
1291         }
1292
1293         return safe_exec_wait(pid);
1294 }
1295
1296 /*!
1297  * \brief enable or disable a logging level to a specified console
1298  */
1299 void ast_console_toggle_loglevel(int fd, int level, int state)
1300 {
1301         int x;
1302
1303         if (level >= NUMLOGLEVELS) {
1304                 level = NUMLOGLEVELS - 1;
1305         }
1306
1307         for (x = 0;x < AST_MAX_CONNECTS; x++) {
1308                 if (fd == consoles[x].fd) {
1309                         /*
1310                          * Since the logging occurs when levels are false, set to
1311                          * flipped iinput because this function accepts 0 as off and 1 as on
1312                          */
1313                         consoles[x].levels[level] = state ? 0 : 1;
1314                         return;
1315                 }
1316         }
1317 }
1318
1319 /*!
1320  * \brief mute or unmute a console from logging
1321  */
1322 void ast_console_toggle_mute(int fd, int silent)
1323 {
1324         int x;
1325         for (x = 0;x < AST_MAX_CONNECTS; x++) {
1326                 if (fd == consoles[x].fd) {
1327                         if (consoles[x].mute) {
1328                                 consoles[x].mute = 0;
1329                                 if (!silent)
1330                                         ast_cli(fd, "Console is not muted anymore.\n");
1331                         } else {
1332                                 consoles[x].mute = 1;
1333                                 if (!silent)
1334                                         ast_cli(fd, "Console is muted.\n");
1335                         }
1336                         return;
1337                 }
1338         }
1339         ast_cli(fd, "Couldn't find remote console.\n");
1340 }
1341
1342 /*!
1343  * \brief log the string to all attached network console clients
1344  */
1345 static void ast_network_puts_mutable(const char *string, int level, int sublevel)
1346 {
1347         int x;
1348
1349         for (x = 0; x < AST_MAX_CONNECTS; ++x) {
1350                 if (consoles[x].fd < 0
1351                         || consoles[x].mute
1352                         || consoles[x].levels[level]
1353                         || (level == __LOG_VERBOSE && consoles[x].option_verbose < sublevel)) {
1354                         continue;
1355                 }
1356                 fdprint(consoles[x].p[1], string);
1357         }
1358 }
1359
1360 /*!
1361  * \brief log the string to the root console, and all attached
1362  * network console clients
1363  */
1364 void ast_console_puts_mutable(const char *string, int level)
1365 {
1366         ast_console_puts_mutable_full(string, level, 0);
1367 }
1368
1369 static int console_print(const char *s);
1370
1371 void ast_console_puts_mutable_full(const char *message, int level, int sublevel)
1372 {
1373         /* Send to the root console */
1374         console_print(message);
1375
1376         /* Wake up a poll()ing console */
1377         if (ast_opt_console && consolethread != AST_PTHREADT_NULL) {
1378                 pthread_kill(consolethread, SIGURG);
1379         }
1380
1381         /* Send to any network console clients */
1382         ast_network_puts_mutable(message, level, sublevel);
1383 }
1384
1385 /*!
1386  * \brief write the string to all attached console clients
1387  */
1388 static void ast_network_puts(const char *string)
1389 {
1390         int x;
1391
1392         for (x = 0; x < AST_MAX_CONNECTS; ++x) {
1393                 if (consoles[x].fd < 0) {
1394                         continue;
1395                 }
1396                 fdprint(consoles[x].p[1], string);
1397         }
1398 }
1399
1400 /*!
1401  * \brief write the string to the root console, and all attached
1402  * network console clients
1403  */
1404 void ast_console_puts(const char *string)
1405 {
1406         /* Send to the root console */
1407         fputs(string, stdout);
1408         fflush(stdout);
1409
1410         /* Send to any network console clients */
1411         ast_network_puts(string);
1412 }
1413
1414 static pthread_t lthread;
1415
1416 /*!
1417  * \brief read() function supporting the reception of user credentials.
1418  *
1419  * \param fd Socket file descriptor.
1420  * \param buffer Receive buffer.
1421  * \param size 'buffer' size.
1422  * \param con Console structure to set received credentials
1423  * \retval -1 on error
1424  * \retval the number of bytes received on success.
1425  */
1426 static int read_credentials(int fd, char *buffer, size_t size, struct console *con)
1427 {
1428 #if defined(SO_PEERCRED)
1429 #ifdef HAVE_STRUCT_SOCKPEERCRED_UID
1430 #define HAVE_STRUCT_UCRED_UID
1431         struct sockpeercred cred;
1432 #else
1433         struct ucred cred;
1434 #endif
1435         socklen_t len = sizeof(cred);
1436 #endif
1437 #if defined(HAVE_GETPEEREID)
1438         uid_t uid;
1439         gid_t gid;
1440 #else
1441         int uid, gid;
1442 #endif
1443         int result;
1444
1445         result = read(fd, buffer, size);
1446         if (result < 0) {
1447                 return result;
1448         }
1449
1450 #if defined(SO_PEERCRED) && (defined(HAVE_STRUCT_UCRED_UID) || defined(HAVE_STRUCT_UCRED_CR_UID))
1451         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len)) {
1452                 return result;
1453         }
1454 #if defined(HAVE_STRUCT_UCRED_UID)
1455         uid = cred.uid;
1456         gid = cred.gid;
1457 #else /* defined(HAVE_STRUCT_UCRED_CR_UID) */
1458         uid = cred.cr_uid;
1459         gid = cred.cr_gid;
1460 #endif /* defined(HAVE_STRUCT_UCRED_UID) */
1461
1462 #elif defined(HAVE_GETPEEREID)
1463         if (getpeereid(fd, &uid, &gid)) {
1464                 return result;
1465         }
1466 #else
1467         return result;
1468 #endif
1469         con->uid = uid;
1470         con->gid = gid;
1471
1472         return result;
1473 }
1474
1475 /* This is the thread running the remote console on the main process. */
1476 static void *netconsole(void *vconsole)
1477 {
1478         struct console *con = vconsole;
1479         char hostname[MAXHOSTNAMELEN] = "";
1480         char inbuf[512];
1481         char outbuf[512];
1482         const char * const end_buf = inbuf + sizeof(inbuf);
1483         char *start_read = inbuf;
1484         int res;
1485         struct pollfd fds[2];
1486
1487         if (gethostname(hostname, sizeof(hostname)-1))
1488                 ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
1489         snprintf(outbuf, sizeof(outbuf), "%s/%ld/%s\n", hostname, (long)ast_mainpid, ast_get_version());
1490         fdprint(con->fd, outbuf);
1491         ast_verb_console_register(&con->option_verbose);
1492         for (;;) {
1493                 fds[0].fd = con->fd;
1494                 fds[0].events = POLLIN;
1495                 fds[0].revents = 0;
1496                 fds[1].fd = con->p[0];
1497                 fds[1].events = POLLIN;
1498                 fds[1].revents = 0;
1499
1500                 res = ast_poll(fds, 2, -1);
1501                 if (res < 0) {
1502                         if (errno != EINTR)
1503                                 ast_log(LOG_WARNING, "poll returned < 0: %s\n", strerror(errno));
1504                         continue;
1505                 }
1506                 if (fds[0].revents) {
1507                         int cmds_read, bytes_read;
1508                         if ((bytes_read = read_credentials(con->fd, start_read, end_buf - start_read, con)) < 1) {
1509                                 break;
1510                         }
1511                         /* XXX This will only work if it is the first command, and I'm not sure fixing it is worth the effort. */
1512                         if (strncmp(inbuf, "cli quit after ", 15) == 0) {
1513                                 ast_cli_command_multiple_full(con->uid, con->gid, con->fd, bytes_read - 15, inbuf + 15);
1514                                 break;
1515                         }
1516                         /* ast_cli_command_multiple_full will only process individual commands terminated by a
1517                          * NULL and not trailing partial commands. */
1518                         if (!(cmds_read = ast_cli_command_multiple_full(con->uid, con->gid, con->fd, bytes_read + start_read - inbuf, inbuf))) {
1519                                 /* No commands were read. We either have a short read on the first command
1520                                  * with space left, or a command that is too long */
1521                                 if (start_read + bytes_read < end_buf) {
1522                                         start_read += bytes_read;
1523                                 } else {
1524                                         ast_log(LOG_ERROR, "Command too long! Skipping\n");
1525                                         start_read = inbuf;
1526                                 }
1527                                 continue;
1528                         }
1529                         if (start_read[bytes_read - 1] == '\0') {
1530                                 /* The read ended on a command boundary, start reading again at the head of inbuf */
1531                                 start_read = inbuf;
1532                                 continue;
1533                         }
1534                         /* If we get this far, we have left over characters that have not been processed.
1535                          * Advance to the character after the last command read by ast_cli_command_multiple_full.
1536                          * We are guaranteed to have at least cmds_read NULLs */
1537                         while (cmds_read-- && (start_read = strchr(start_read, '\0'))) {
1538                                 start_read++;
1539                         }
1540                         memmove(inbuf, start_read, end_buf - start_read);
1541                         start_read = end_buf - start_read + inbuf;
1542                 }
1543                 if (fds[1].revents) {
1544                         res = read_credentials(con->p[0], outbuf, sizeof(outbuf), con);
1545                         if (res < 1) {
1546                                 ast_log(LOG_ERROR, "read returned %d\n", res);
1547                                 break;
1548                         }
1549                         res = write(con->fd, outbuf, res);
1550                         if (res < 1)
1551                                 break;
1552                 }
1553         }
1554         ast_verb_console_unregister();
1555         if (!ast_opt_hide_connect) {
1556                 ast_verb(3, "Remote UNIX connection disconnected\n");
1557         }
1558         close(con->fd);
1559         close(con->p[0]);
1560         close(con->p[1]);
1561         con->fd = -1;
1562
1563         return NULL;
1564 }
1565
1566 static void *listener(void *unused)
1567 {
1568         struct sockaddr_un sunaddr;
1569         int s;
1570         socklen_t len;
1571         int x;
1572         struct pollfd fds[1];
1573         for (;;) {
1574                 if (ast_socket < 0)
1575                         return NULL;
1576                 fds[0].fd = ast_socket;
1577                 fds[0].events = POLLIN;
1578                 s = ast_poll(fds, 1, -1);
1579                 pthread_testcancel();
1580                 if (s < 0) {
1581                         if (errno != EINTR)
1582                                 ast_log(LOG_WARNING, "poll returned error: %s\n", strerror(errno));
1583                         continue;
1584                 }
1585                 len = sizeof(sunaddr);
1586                 s = accept(ast_socket, (struct sockaddr *)&sunaddr, &len);
1587                 if (s < 0) {
1588                         if (errno != EINTR)
1589                                 ast_log(LOG_WARNING, "Accept returned %d: %s\n", s, strerror(errno));
1590                 } else {
1591 #if defined(SO_PASSCRED)
1592                         int sckopt = 1;
1593                         /* turn on socket credentials passing. */
1594                         if (setsockopt(s, SOL_SOCKET, SO_PASSCRED, &sckopt, sizeof(sckopt)) < 0) {
1595                                 ast_log(LOG_WARNING, "Unable to turn on socket credentials passing\n");
1596                         } else
1597 #endif
1598                         {
1599                                 for (x = 0; x < AST_MAX_CONNECTS; x++) {
1600                                         if (consoles[x].fd >= 0) {
1601                                                 continue;
1602                                         }
1603                                         if (socketpair(AF_LOCAL, SOCK_STREAM, 0, consoles[x].p)) {
1604                                                 ast_log(LOG_ERROR, "Unable to create pipe: %s\n", strerror(errno));
1605                                                 fdprint(s, "Server failed to create pipe\n");
1606                                                 close(s);
1607                                                 break;
1608                                         }
1609                                         ast_fd_set_flags(consoles[x].p[1], O_NONBLOCK);
1610                                         consoles[x].mute = 1; /* Default is muted, we will un-mute if necessary */
1611                                         /* Default uid and gid to -2, so then in cli.c/cli_has_permissions() we will be able
1612                                            to know if the user didn't send the credentials. */
1613                                         consoles[x].uid = -2;
1614                                         consoles[x].gid = -2;
1615                                         /* Server default of remote console verbosity level is OFF. */
1616                                         consoles[x].option_verbose = 0;
1617                                         consoles[x].fd = s;
1618                                         if (ast_pthread_create_detached_background(&consoles[x].t, NULL, netconsole, &consoles[x])) {
1619                                                 consoles[x].fd = -1;
1620                                                 ast_log(LOG_ERROR, "Unable to spawn thread to handle connection: %s\n", strerror(errno));
1621                                                 close(consoles[x].p[0]);
1622                                                 close(consoles[x].p[1]);
1623                                                 fdprint(s, "Server failed to spawn thread\n");
1624                                                 close(s);
1625                                         }
1626                                         break;
1627                                 }
1628                                 if (x >= AST_MAX_CONNECTS) {
1629                                         fdprint(s, "No more connections allowed\n");
1630                                         ast_log(LOG_WARNING, "No more connections allowed\n");
1631                                         close(s);
1632                                 } else if ((consoles[x].fd > -1) && (!ast_opt_hide_connect)) {
1633                                         ast_verb(3, "Remote UNIX connection\n");
1634                                 }
1635                         }
1636                 }
1637         }
1638         return NULL;
1639 }
1640
1641 static int ast_makesocket(void)
1642 {
1643         struct sockaddr_un sunaddr;
1644         int res;
1645         int x;
1646         uid_t uid = -1;
1647         gid_t gid = -1;
1648
1649         for (x = 0; x < AST_MAX_CONNECTS; x++) {
1650                 consoles[x].fd = -1;
1651         }
1652
1653         if (ast_socket_is_sd) {
1654                 ast_socket = ast_sd_get_fd_un(SOCK_STREAM, ast_config_AST_SOCKET);
1655
1656                 goto start_lthread;
1657         }
1658
1659         unlink(ast_config_AST_SOCKET);
1660         ast_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
1661         if (ast_socket < 0) {
1662                 ast_log(LOG_WARNING, "Unable to create control socket: %s\n", strerror(errno));
1663                 return -1;
1664         }
1665         memset(&sunaddr, 0, sizeof(sunaddr));
1666         sunaddr.sun_family = AF_LOCAL;
1667         ast_copy_string(sunaddr.sun_path, ast_config_AST_SOCKET, sizeof(sunaddr.sun_path));
1668         res = bind(ast_socket, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
1669         if (res) {
1670                 ast_log(LOG_WARNING, "Unable to bind socket to %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1671                 close(ast_socket);
1672                 ast_socket = -1;
1673                 return -1;
1674         }
1675         res = listen(ast_socket, 2);
1676         if (res < 0) {
1677                 ast_log(LOG_WARNING, "Unable to listen on socket %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1678                 close(ast_socket);
1679                 ast_socket = -1;
1680                 return -1;
1681         }
1682
1683 start_lthread:
1684         if (ast_pthread_create_background(&lthread, NULL, listener, NULL)) {
1685                 ast_log(LOG_WARNING, "Unable to create listener thread.\n");
1686                 close(ast_socket);
1687                 return -1;
1688         }
1689
1690         if (ast_socket_is_sd) {
1691                 /* owner/group/permissions are set by systemd, we might not even have access
1692                  * to socket file so leave it alone */
1693                 return 0;
1694         }
1695
1696         if (!ast_strlen_zero(ast_config_AST_CTL_OWNER)) {
1697                 struct passwd *pw;
1698                 if ((pw = getpwnam(ast_config_AST_CTL_OWNER)) == NULL)
1699                         ast_log(LOG_WARNING, "Unable to find uid of user %s\n", ast_config_AST_CTL_OWNER);
1700                 else
1701                         uid = pw->pw_uid;
1702         }
1703
1704         if (!ast_strlen_zero(ast_config_AST_CTL_GROUP)) {
1705                 struct group *grp;
1706                 if ((grp = getgrnam(ast_config_AST_CTL_GROUP)) == NULL)
1707                         ast_log(LOG_WARNING, "Unable to find gid of group %s\n", ast_config_AST_CTL_GROUP);
1708                 else
1709                         gid = grp->gr_gid;
1710         }
1711
1712         if (chown(ast_config_AST_SOCKET, uid, gid) < 0)
1713                 ast_log(LOG_WARNING, "Unable to change ownership of %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1714
1715         if (!ast_strlen_zero(ast_config_AST_CTL_PERMISSIONS)) {
1716                 unsigned int p1;
1717                 mode_t p;
1718                 sscanf(ast_config_AST_CTL_PERMISSIONS, "%30o", &p1);
1719                 p = p1;
1720                 if ((chmod(ast_config_AST_SOCKET, p)) < 0)
1721                         ast_log(LOG_WARNING, "Unable to change file permissions of %s: %s\n", ast_config_AST_SOCKET, strerror(errno));
1722         }
1723
1724         return 0;
1725 }
1726
1727 static int ast_tryconnect(void)
1728 {
1729         struct sockaddr_un sunaddr;
1730         int res;
1731         ast_consock = socket(PF_LOCAL, SOCK_STREAM, 0);
1732         if (ast_consock < 0) {
1733                 fprintf(stderr, "Unable to create socket: %s\n", strerror(errno));
1734                 return 0;
1735         }
1736         memset(&sunaddr, 0, sizeof(sunaddr));
1737         sunaddr.sun_family = AF_LOCAL;
1738         ast_copy_string(sunaddr.sun_path, ast_config_AST_SOCKET, sizeof(sunaddr.sun_path));
1739         res = connect(ast_consock, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
1740         if (res) {
1741                 close(ast_consock);
1742                 ast_consock = -1;
1743                 return 0;
1744         } else
1745                 return 1;
1746 }
1747
1748 /*! \brief Urgent handler
1749  *
1750  * Called by soft_hangup to interrupt the poll, read, or other
1751  * system call.  We don't actually need to do anything though.
1752  * Remember: Cannot EVER ast_log from within a signal handler
1753  */
1754 static void _urg_handler(int num)
1755 {
1756         return;
1757 }
1758
1759 static struct sigaction urg_handler = {
1760         .sa_handler = _urg_handler,
1761 };
1762
1763 static void _hup_handler(int num)
1764 {
1765         int save_errno = errno;
1766         printf("Received HUP signal -- Reloading configs\n");
1767         if (restartnow)
1768                 execvp(_argv[0], _argv);
1769         sig_flags.need_reload = 1;
1770         if (ast_alertpipe_write(sig_alert_pipe)) {
1771                 fprintf(stderr, "hup_handler: write() failed: %s\n", strerror(errno));
1772         }
1773         errno = save_errno;
1774 }
1775
1776 static struct sigaction hup_handler = {
1777         .sa_handler = _hup_handler,
1778         .sa_flags = SA_RESTART,
1779 };
1780
1781 static void _child_handler(int sig)
1782 {
1783         /* Must not ever ast_log or ast_verbose within signal handler */
1784         int n, status, save_errno = errno;
1785
1786         /*
1787          * Reap all dead children -- not just one
1788          */
1789         for (n = 0; waitpid(-1, &status, WNOHANG) > 0; n++)
1790                 ;
1791         if (n == 0 && option_debug)
1792                 printf("Huh?  Child handler, but nobody there?\n");
1793         errno = save_errno;
1794 }
1795
1796 static struct sigaction child_handler = {
1797         .sa_handler = _child_handler,
1798         .sa_flags = SA_RESTART,
1799 };
1800
1801 /*! \brief Set maximum open files */
1802 static void set_ulimit(int value)
1803 {
1804         struct rlimit l = {0, 0};
1805
1806         if (value <= 0) {
1807                 ast_log(LOG_WARNING, "Unable to change max files open to invalid value %i\n",value);
1808                 return;
1809         }
1810
1811         l.rlim_cur = value;
1812         l.rlim_max = value;
1813
1814         if (setrlimit(RLIMIT_NOFILE, &l)) {
1815                 ast_log(LOG_WARNING, "Unable to disable core size resource limit: %s\n",strerror(errno));
1816                 return;
1817         }
1818
1819         ast_log(LOG_NOTICE, "Setting max files open to %d\n",value);
1820
1821         return;
1822 }
1823
1824 /*! \brief Set an X-term or screen title */
1825 static void set_title(char *text)
1826 {
1827         if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
1828                 fprintf(stdout, "\033]2;%s\007", text);
1829 }
1830
1831 static void set_icon(char *text)
1832 {
1833         if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
1834                 fprintf(stdout, "\033]1;%s\007", text);
1835 }
1836
1837 /*! \brief Check whether we were set to high(er) priority. */
1838 static int has_priority(void)
1839 {
1840         /* Neither of these calls should fail with these arguments. */
1841 #ifdef __linux__
1842         /* For SCHED_OTHER, SCHED_BATCH and SCHED_IDLE, this will return
1843          * 0. For the realtime priorities SCHED_RR and SCHED_FIFO, it
1844          * will return something >= 1. */
1845         return sched_getscheduler(0);
1846 #else
1847         /* getpriority() can return a value in -20..19 (or even -INF..20)
1848          * where negative numbers are high priority. We don't bother
1849          * checking errno. If the query fails and it returns -1, we'll
1850          * assume that we're running at high prio; a safe assumption
1851          * that will enable the resource starvation monitor (canary)
1852          * just in case. */
1853         return (getpriority(PRIO_PROCESS, 0) < 0);
1854 #endif
1855 }
1856
1857 /*! \brief Set priority on all known threads. */
1858 static int set_priority_all(int pri)
1859 {
1860 #if !defined(__linux__)
1861         /* The non-linux version updates the entire process prio. */
1862         return ast_set_priority(pri);
1863 #elif defined(LOW_MEMORY)
1864         ast_log(LOG_WARNING, "Unable to enumerate all threads to update priority\n");
1865         return ast_set_priority(pri);
1866 #else
1867         struct thread_list_t *cur;
1868         struct sched_param sched;
1869         char const *policy_str;
1870         int policy;
1871
1872         memset(&sched, 0, sizeof(sched));
1873         if (pri) {
1874                 policy = SCHED_RR;
1875                 policy_str = "realtime";
1876                 sched.sched_priority = 10;
1877         } else {
1878                 policy = SCHED_OTHER;
1879                 policy_str = "regular";
1880                 sched.sched_priority = 0;
1881         }
1882         if (sched_setscheduler(getpid(), policy, &sched)) {
1883                 ast_log(LOG_WARNING, "Unable to set %s thread priority on main thread\n", policy_str);
1884                 return -1;
1885         }
1886         ast_verb(1, "Setting %s thread priority on all threads\n", policy_str);
1887         AST_RWLIST_RDLOCK(&thread_list);
1888         AST_RWLIST_TRAVERSE(&thread_list, cur, list) {
1889                 /* Don't care about the return value. It should work. */
1890                 sched_setscheduler(cur->lwp, policy, &sched);
1891         }
1892         AST_RWLIST_UNLOCK(&thread_list);
1893         return 0;
1894 #endif
1895 }
1896
1897 /*! \brief We set ourselves to a high priority, that we might pre-empt
1898  * everything else.  If your PBX has heavy activity on it, this is a
1899  * good thing.
1900  */
1901 int ast_set_priority(int pri)
1902 {
1903         struct sched_param sched;
1904         memset(&sched, 0, sizeof(sched));
1905 #ifdef __linux__
1906         if (pri) {
1907                 sched.sched_priority = 10;
1908                 if (sched_setscheduler(0, SCHED_RR, &sched)) {
1909                         ast_log(LOG_WARNING, "Unable to set high priority\n");
1910                         return -1;
1911                 } else
1912                         ast_verb(1, "Set to realtime thread\n");
1913         } else {
1914                 sched.sched_priority = 0;
1915                 /* According to the manpage, these parameters can never fail. */
1916                 sched_setscheduler(0, SCHED_OTHER, &sched);
1917         }
1918 #else
1919         if (pri) {
1920                 if (setpriority(PRIO_PROCESS, 0, -10) == -1) {
1921                         ast_log(LOG_WARNING, "Unable to set high priority\n");
1922                         return -1;
1923                 } else
1924                         ast_verb(1, "Set to high priority\n");
1925         } else {
1926                 /* According to the manpage, these parameters can never fail. */
1927                 setpriority(PRIO_PROCESS, 0, 0);
1928         }
1929 #endif
1930         return 0;
1931 }
1932
1933 int ast_shutdown_final(void)
1934 {
1935         return shuttingdown == SHUTTING_DOWN_FINAL;
1936 }
1937
1938 int ast_shutting_down(void)
1939 {
1940         return shutdown_pending;
1941 }
1942
1943 int ast_cancel_shutdown(void)
1944 {
1945         int shutdown_aborted = 0;
1946
1947         ast_mutex_lock(&safe_system_lock);
1948         if (shuttingdown >= SHUTDOWN_FAST) {
1949                 shuttingdown = NOT_SHUTTING_DOWN;
1950                 shutdown_pending = 0;
1951                 shutdown_aborted = 1;
1952         }
1953         ast_mutex_unlock(&safe_system_lock);
1954         return shutdown_aborted;
1955 }
1956
1957 /*!
1958  * \internal
1959  * \brief Initiate system shutdown -- prevents new channels from being allocated.
1960  */
1961 static void ast_begin_shutdown(void)
1962 {
1963         ast_mutex_lock(&safe_system_lock);
1964         if (shuttingdown != NOT_SHUTTING_DOWN) {
1965                 shutdown_pending = 1;
1966         }
1967         ast_mutex_unlock(&safe_system_lock);
1968 }
1969
1970 static int can_safely_quit(shutdown_nice_t niceness, int restart);
1971 static void really_quit(int num, shutdown_nice_t niceness, int restart);
1972
1973 static void quit_handler(int num, shutdown_nice_t niceness, int restart)
1974 {
1975         if (can_safely_quit(niceness, restart)) {
1976                 really_quit(num, niceness, restart);
1977                 /* No one gets here. */
1978         }
1979         /* It wasn't our time. */
1980 }
1981
1982 #define SHUTDOWN_TIMEOUT        15      /* Seconds */
1983
1984 /*!
1985  * \internal
1986  * \brief Wait for all channels to die, a timeout, or shutdown cancelled.
1987  * \since 13.3.0
1988  *
1989  * \param niceness Shutdown niceness in effect
1990  * \param seconds Number of seconds to wait or less than zero if indefinitely.
1991  *
1992  * \retval zero if waiting wasn't necessary.  We were idle.
1993  * \retval non-zero if we had to wait.
1994  */
1995 static int wait_for_channels_to_die(shutdown_nice_t niceness, int seconds)
1996 {
1997         time_t start;
1998         time_t now;
1999         int waited = 0;
2000
2001         time(&start);
2002         for (;;) {
2003                 if (!ast_undestroyed_channels() || shuttingdown != niceness) {
2004                         break;
2005                 }
2006                 if (seconds < 0) {
2007                         /* No timeout so just poll every second */
2008                         sleep(1);
2009                 } else {
2010                         time(&now);
2011
2012                         /* Wait up to the given seconds for all channels to go away */
2013                         if (seconds < (now - start)) {
2014                                 break;
2015                         }
2016
2017                         /* Sleep 1/10 of a second */
2018                         usleep(100000);
2019                 }
2020                 waited = 1;
2021         }
2022         return waited;
2023 }
2024
2025 static int can_safely_quit(shutdown_nice_t niceness, int restart)
2026 {
2027         int waited = 0;
2028
2029         /* Check if someone else isn't already doing this. */
2030         ast_mutex_lock(&safe_system_lock);
2031         if (shuttingdown != NOT_SHUTTING_DOWN && niceness >= shuttingdown) {
2032                 /* Already in progress and other request was less nice. */
2033                 ast_mutex_unlock(&safe_system_lock);
2034                 ast_verbose("Ignoring asterisk %s request, already in progress.\n", restart ? "restart" : "shutdown");
2035                 return 0;
2036         }
2037         shuttingdown = niceness;
2038         ast_mutex_unlock(&safe_system_lock);
2039
2040         /* Try to get as many CDRs as possible submitted to the backend engines
2041          * (if in batch mode). really_quit happens to call it again when running
2042          * the atexit handlers, otherwise this would be a bit early. */
2043         ast_cdr_engine_term();
2044
2045         /*
2046          * Shutdown the message queue for the technology agnostic message channel.
2047          * This has to occur before we pause shutdown pending ast_undestroyed_channels.
2048          *
2049          * XXX This is not reversed on shutdown cancel.
2050          */
2051         ast_msg_shutdown();
2052
2053         if (niceness == SHUTDOWN_NORMAL) {
2054                 /* Begin shutdown routine, hanging up active channels */
2055                 ast_begin_shutdown();
2056                 if (ast_opt_console) {
2057                         ast_verb(0, "Beginning asterisk %s....\n", restart ? "restart" : "shutdown");
2058                 }
2059                 ast_softhangup_all();
2060                 waited |= wait_for_channels_to_die(niceness, SHUTDOWN_TIMEOUT);
2061         } else if (niceness >= SHUTDOWN_NICE) {
2062                 if (niceness != SHUTDOWN_REALLY_NICE) {
2063                         ast_begin_shutdown();
2064                 }
2065                 if (ast_opt_console) {
2066                         ast_verb(0, "Waiting for inactivity to perform %s...\n", restart ? "restart" : "halt");
2067                 }
2068                 waited |= wait_for_channels_to_die(niceness, -1);
2069         }
2070
2071         /* Re-acquire lock and check if someone changed the niceness, in which
2072          * case someone else has taken over the shutdown.
2073          */
2074         ast_mutex_lock(&safe_system_lock);
2075         if (shuttingdown != niceness) {
2076                 if (shuttingdown == NOT_SHUTTING_DOWN && ast_opt_console) {
2077                         ast_verb(0, "Asterisk %s cancelled.\n", restart ? "restart" : "shutdown");
2078                 }
2079                 ast_mutex_unlock(&safe_system_lock);
2080                 return 0;
2081         }
2082
2083         if (niceness >= SHUTDOWN_REALLY_NICE) {
2084                 shuttingdown = SHUTTING_DOWN;
2085                 ast_mutex_unlock(&safe_system_lock);
2086
2087                 /* No more Mr. Nice guy.  We are committed to shutting down now. */
2088                 ast_begin_shutdown();
2089                 ast_softhangup_all();
2090                 waited |= wait_for_channels_to_die(SHUTTING_DOWN, SHUTDOWN_TIMEOUT);
2091
2092                 ast_mutex_lock(&safe_system_lock);
2093         }
2094         shuttingdown = SHUTTING_DOWN_FINAL;
2095         ast_mutex_unlock(&safe_system_lock);
2096
2097         if (niceness >= SHUTDOWN_NORMAL && waited) {
2098                 /*
2099                  * We were not idle.  Give things in progress a chance to
2100                  * recognize the final shutdown phase.
2101                  */
2102                 sleep(1);
2103         }
2104         return 1;
2105 }
2106
2107 /*! Called when exiting is certain. */
2108 static void really_quit(int num, shutdown_nice_t niceness, int restart)
2109 {
2110         int active_channels;
2111         struct ast_json *json_object = NULL;
2112         int run_cleanups = niceness >= SHUTDOWN_NICE;
2113
2114         if (run_cleanups && modules_shutdown()) {
2115                 ast_verb(0, "Some modules could not be unloaded, switching to fast shutdown\n");
2116                 run_cleanups = 0;
2117         }
2118
2119         if (!restart) {
2120                 ast_sd_notify("STOPPING=1");
2121         }
2122         if (ast_opt_console || (ast_opt_remote && !ast_opt_exec)) {
2123                 ast_el_write_default_histfile();
2124                 if (consolethread == AST_PTHREADT_NULL || consolethread == pthread_self()) {
2125                         /* Only end if we are the consolethread, otherwise there's a race with that thread. */
2126                         if (el != NULL) {
2127                                 el_end(el);
2128                         }
2129                         if (el_hist != NULL) {
2130                                 history_end(el_hist);
2131                         }
2132                 } else if (mon_sig_flags == pthread_self()) {
2133                         if (consolethread != AST_PTHREADT_NULL) {
2134                                 pthread_kill(consolethread, SIGURG);
2135                         }
2136                 }
2137         }
2138         active_channels = ast_active_channels();
2139         /* Don't publish messages if we're a remote console - we won't have all of the Stasis
2140          * topics or message types
2141          */
2142         if (!ast_opt_remote) {
2143                 json_object = ast_json_pack("{s: s, s: s}",
2144                                 "Shutdown", active_channels ? "Uncleanly" : "Cleanly",
2145                                 "Restart", restart ? "True" : "False");
2146                 ast_manager_publish_event("Shutdown", EVENT_FLAG_SYSTEM, json_object);
2147                 ast_json_unref(json_object);
2148                 json_object = NULL;
2149         }
2150         ast_verb(0, "Asterisk %s ending (%d).\n",
2151                 active_channels ? "uncleanly" : "cleanly", num);
2152
2153         ast_verb(0, "Executing last minute cleanups\n");
2154         ast_run_atexits(run_cleanups);
2155
2156         ast_debug(1, "Asterisk ending (%d).\n", num);
2157         if (ast_socket > -1) {
2158                 pthread_cancel(lthread);
2159                 close(ast_socket);
2160                 ast_socket = -1;
2161                 if (!ast_socket_is_sd) {
2162                         unlink(ast_config_AST_SOCKET);
2163                 }
2164                 pthread_kill(lthread, SIGURG);
2165                 pthread_join(lthread, NULL);
2166         }
2167         if (ast_consock > -1)
2168                 close(ast_consock);
2169         if (!ast_opt_remote)
2170                 unlink(ast_config_AST_PID);
2171         ast_alertpipe_close(sig_alert_pipe);
2172         printf("%s", term_quit());
2173         if (restart) {
2174                 int i;
2175                 ast_verb(0, "Preparing for Asterisk restart...\n");
2176                 /* Mark all FD's for closing on exec */
2177                 for (i = 3; i < 32768; i++) {
2178                         fcntl(i, F_SETFD, FD_CLOEXEC);
2179                 }
2180                 ast_verb(0, "Asterisk is now restarting...\n");
2181                 restartnow = 1;
2182
2183                 /* close logger */
2184                 close_logger();
2185                 clean_time_zones();
2186
2187                 /* If there is a consolethread running send it a SIGHUP
2188                    so it can execvp, otherwise we can do it ourselves */
2189                 if ((consolethread != AST_PTHREADT_NULL) && (consolethread != pthread_self())) {
2190                         pthread_kill(consolethread, SIGHUP);
2191                         /* Give the signal handler some time to complete */
2192                         sleep(2);
2193                 } else
2194                         execvp(_argv[0], _argv);
2195
2196         } else {
2197                 /* close logger */
2198                 close_logger();
2199                 clean_time_zones();
2200         }
2201
2202         exit(0);
2203 }
2204
2205 static void __quit_handler(int num)
2206 {
2207         sig_flags.need_quit = 1;
2208         if (ast_alertpipe_write(sig_alert_pipe)) {
2209                 fprintf(stderr, "quit_handler: write() failed: %s\n", strerror(errno));
2210         }
2211         /* There is no need to restore the signal handler here, since the app
2212          * is going to exit */
2213 }
2214
2215 static void __remote_quit_handler(int num)
2216 {
2217         sig_flags.need_quit = 1;
2218 }
2219
2220 static void set_header(char *outbuf, int maxout, char level)
2221 {
2222         const char *cmp;
2223         char date[40];
2224
2225         switch (level) {
2226         case 0: cmp = NULL;
2227                 break;
2228         case 1: cmp = VERBOSE_PREFIX_1;
2229                 break;
2230         case 2: cmp = VERBOSE_PREFIX_2;
2231                 break;
2232         case 3: cmp = VERBOSE_PREFIX_3;
2233                 break;
2234         default: cmp = VERBOSE_PREFIX_4;
2235                 break;
2236         }
2237
2238         if (ast_opt_timestamp) {
2239                 struct ast_tm tm;
2240                 struct timeval now = ast_tvnow();
2241                 ast_localtime(&now, &tm, NULL);
2242                 ast_strftime(date, sizeof(date), ast_logger_get_dateformat(), &tm);
2243         }
2244
2245         snprintf(outbuf, maxout, "%s%s%s%s%s%s",
2246                 ast_opt_timestamp ? "[" : "",
2247                 ast_opt_timestamp ? date : "",
2248                 ast_opt_timestamp ? "] " : "",
2249                 cmp ? ast_term_color(COLOR_GRAY, 0) : "",
2250                 cmp ? cmp : "",
2251                 cmp ? ast_term_reset() : "");
2252 }
2253
2254 struct console_state_data {
2255         char verbose_line_level;
2256 };
2257
2258 static int console_state_init(void *ptr)
2259 {
2260         struct console_state_data *state = ptr;
2261         state->verbose_line_level = 0;
2262         return 0;
2263 }
2264
2265 AST_THREADSTORAGE_CUSTOM(console_state, console_state_init, ast_free_ptr);
2266
2267 static int console_print(const char *s)
2268 {
2269         struct console_state_data *state =
2270                 ast_threadstorage_get(&console_state, sizeof(*state));
2271
2272         char prefix[80];
2273         const char *c;
2274         int num, res = 0;
2275         unsigned int newline;
2276
2277         do {
2278                 if (VERBOSE_HASMAGIC(s)) {
2279
2280                         /* always use the given line's level, otherwise
2281                            we'll use the last line's level */
2282                         state->verbose_line_level = VERBOSE_MAGIC2LEVEL(s);
2283
2284                         /* move past magic */
2285                         s++;
2286
2287                         set_header(prefix, sizeof(prefix), state->verbose_line_level);
2288                 } else {
2289                         *prefix = '\0';
2290                 }
2291                 c = s;
2292
2293                 /* for a given line separate on verbose magic, newline, and eol */
2294                 if ((s = strchr(c, '\n'))) {
2295                         ++s;
2296                         newline = 1;
2297                 } else {
2298                         s = strchr(c, '\0');
2299                         newline = 0;
2300                 }
2301
2302                 /* check if we should write this line after calculating begin/end
2303                    so we process the case of a higher level line embedded within
2304                    two lower level lines */
2305                 if (state->verbose_line_level > option_verbose) {
2306                         continue;
2307                 }
2308
2309                 if (!ast_strlen_zero(prefix)) {
2310                         fputs(prefix, stdout);
2311                 }
2312
2313                 num = s - c;
2314                 if (fwrite(c, sizeof(char), num, stdout) < num) {
2315                         break;
2316                 }
2317
2318                 if (!res) {
2319                         /* if at least some info has been written
2320                            we'll want to return true */
2321                         res = 1;
2322                 }
2323         } while (*s);
2324
2325         if (newline) {
2326                 /* if ending on a newline then reset last level to zero
2327                     since what follows may be not be logging output */
2328                 state->verbose_line_level = 0;
2329         }
2330
2331         if (res) {
2332                 fflush(stdout);
2333         }
2334
2335         return res;
2336 }
2337
2338 static int ast_all_zeros(const char *s)
2339 {
2340         while (*s) {
2341                 if (*s > 32)
2342                         return 0;
2343                 s++;
2344         }
2345         return 1;
2346 }
2347
2348 /* This is the main console CLI command handler.  Run by the main() thread. */
2349 static void consolehandler(const char *s)
2350 {
2351         printf("%s", term_end());
2352         fflush(stdout);
2353
2354         /* Called when readline data is available */
2355         if (!ast_all_zeros(s))
2356                 ast_el_add_history(s);
2357         /* The real handler for bang */
2358         if (s[0] == '!') {
2359                 if (s[1])
2360                         ast_safe_system(s+1);
2361                 else
2362                         ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
2363         } else
2364                 ast_cli_command(STDOUT_FILENO, s);
2365 }
2366
2367 static int remoteconsolehandler(const char *s)
2368 {
2369         int ret = 0;
2370
2371         /* Called when readline data is available */
2372         if (!ast_all_zeros(s))
2373                 ast_el_add_history(s);
2374
2375         while (isspace(*s)) {
2376                 s++;
2377         }
2378
2379         /* The real handler for bang */
2380         if (s[0] == '!') {
2381                 if (s[1])
2382                         ast_safe_system(s+1);
2383                 else
2384                         ast_safe_system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
2385                 ret = 1;
2386         } else if ((strncasecmp(s, "quit", 4) == 0 || strncasecmp(s, "exit", 4) == 0) &&
2387             (s[4] == '\0' || isspace(s[4]))) {
2388                 quit_handler(0, SHUTDOWN_FAST, 0);
2389                 ret = 1;
2390         } else if (s[0]) {
2391                 char *shrunk = ast_strdupa(s);
2392                 char *cur;
2393                 char *prev;
2394
2395                 /*
2396                  * Remove duplicate spaces from shrunk for matching purposes.
2397                  *
2398                  * shrunk has at least one character in it to start with or we
2399                  * couldn't get here.
2400                  */
2401                 for (prev = shrunk, cur = shrunk + 1; *cur; ++cur) {
2402                         if (*prev == ' ' && *cur == ' ') {
2403                                 /* Skip repeated space delimiter. */
2404                                 continue;
2405                         }
2406                         *++prev = *cur;
2407                 }
2408                 *++prev = '\0';
2409
2410                 if (strncasecmp(shrunk, "core set verbose ", 17) == 0) {
2411                         /*
2412                          * We need to still set the rasterisk option_verbose in case we are
2413                          * talking to an earlier version which doesn't prefilter verbose
2414                          * levels.  This is really a compromise as we should always take
2415                          * whatever the server sends.
2416                          */
2417
2418                         if (!strncasecmp(shrunk + 17, "off", 3)) {
2419                                 ast_verb_console_set(0);
2420                         } else {
2421                                 int verbose_new;
2422                                 int atleast;
2423
2424                                 atleast = 8;
2425                                 if (strncasecmp(shrunk + 17, "atleast ", atleast)) {
2426                                         atleast = 0;
2427                                 }
2428
2429                                 if (sscanf(shrunk + 17 + atleast, "%30d", &verbose_new) == 1) {
2430                                         if (!atleast || ast_verb_console_get() < verbose_new) {
2431                                                 ast_verb_console_set(verbose_new);
2432                                         }
2433                                 }
2434                         }
2435                 }
2436         }
2437
2438         return ret;
2439 }
2440
2441 static char *handle_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2442 {
2443         switch (cmd) {
2444         case CLI_INIT:
2445                 e->command = "core show version";
2446                 e->usage =
2447                         "Usage: core show version\n"
2448                         "       Shows Asterisk version information.\n";
2449                 return NULL;
2450         case CLI_GENERATE:
2451                 return NULL;
2452         }
2453
2454         if (a->argc != 3)
2455                 return CLI_SHOWUSAGE;
2456         ast_cli(a->fd, "Asterisk %s built by %s @ %s on a %s running %s on %s\n",
2457                 ast_get_version(), ast_build_user, ast_build_hostname,
2458                 ast_build_machine, ast_build_os, ast_build_date);
2459         return CLI_SUCCESS;
2460 }
2461
2462 static char *handle_stop_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2463 {
2464         switch (cmd) {
2465         case CLI_INIT:
2466                 e->command = "core stop now";
2467                 e->usage =
2468                         "Usage: core stop now\n"
2469                         "       Shuts down a running Asterisk immediately, hanging up all active calls .\n";
2470                 ast_cli_allow_at_shutdown(e);
2471                 return NULL;
2472         case CLI_GENERATE:
2473                 return NULL;
2474         }
2475
2476         if (a->argc != e->args)
2477                 return CLI_SHOWUSAGE;
2478         quit_handler(0, SHUTDOWN_NORMAL, 0 /* not restart */);
2479         return CLI_SUCCESS;
2480 }
2481
2482 static char *handle_stop_gracefully(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2483 {
2484         switch (cmd) {
2485         case CLI_INIT:
2486                 e->command = "core stop gracefully";
2487                 e->usage =
2488                         "Usage: core stop gracefully\n"
2489                         "       Causes Asterisk to not accept new calls, and exit when all\n"
2490                         "       active calls have terminated normally.\n";
2491                 ast_cli_allow_at_shutdown(e);
2492                 return NULL;
2493         case CLI_GENERATE:
2494                 return NULL;
2495         }
2496
2497         if (a->argc != e->args)
2498                 return CLI_SHOWUSAGE;
2499         quit_handler(0, SHUTDOWN_NICE, 0 /* no restart */);
2500         return CLI_SUCCESS;
2501 }
2502
2503 static char *handle_stop_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2504 {
2505         switch (cmd) {
2506         case CLI_INIT:
2507                 e->command = "core stop when convenient";
2508                 e->usage =
2509                         "Usage: core stop when convenient\n"
2510                         "       Causes Asterisk to perform a shutdown when all active calls have ended.\n";
2511                 ast_cli_allow_at_shutdown(e);
2512                 return NULL;
2513         case CLI_GENERATE:
2514                 return NULL;
2515         }
2516
2517         if (a->argc != e->args)
2518                 return CLI_SHOWUSAGE;
2519         ast_cli(a->fd, "Waiting for inactivity to perform halt\n");
2520         quit_handler(0, SHUTDOWN_REALLY_NICE, 0 /* don't restart */);
2521         return CLI_SUCCESS;
2522 }
2523
2524 static char *handle_restart_now(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2525 {
2526         switch (cmd) {
2527         case CLI_INIT:
2528                 e->command = "core restart now";
2529                 e->usage =
2530                         "Usage: core restart now\n"
2531                         "       Causes Asterisk to hangup all calls and exec() itself performing a cold\n"
2532                         "       restart.\n";
2533                 ast_cli_allow_at_shutdown(e);
2534                 return NULL;
2535         case CLI_GENERATE:
2536                 return NULL;
2537         }
2538
2539         if (a->argc != e->args)
2540                 return CLI_SHOWUSAGE;
2541         quit_handler(0, SHUTDOWN_NORMAL, 1 /* restart */);
2542         return CLI_SUCCESS;
2543 }
2544
2545 static char *handle_restart_gracefully(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2546 {
2547         switch (cmd) {
2548         case CLI_INIT:
2549                 e->command = "core restart gracefully";
2550                 e->usage =
2551                         "Usage: core restart gracefully\n"
2552                         "       Causes Asterisk to stop accepting new calls and exec() itself performing a cold\n"
2553                         "       restart when all active calls have ended.\n";
2554                 ast_cli_allow_at_shutdown(e);
2555                 return NULL;
2556         case CLI_GENERATE:
2557                 return NULL;
2558         }
2559
2560         if (a->argc != e->args)
2561                 return CLI_SHOWUSAGE;
2562         quit_handler(0, SHUTDOWN_NICE, 1 /* restart */);
2563         return CLI_SUCCESS;
2564 }
2565
2566 static char *handle_restart_when_convenient(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2567 {
2568         switch (cmd) {
2569         case CLI_INIT:
2570                 e->command = "core restart when convenient";
2571                 e->usage =
2572                         "Usage: core restart when convenient\n"
2573                         "       Causes Asterisk to perform a cold restart when all active calls have ended.\n";
2574                 ast_cli_allow_at_shutdown(e);
2575                 return NULL;
2576         case CLI_GENERATE:
2577                 return NULL;
2578         }
2579
2580         if (a->argc != e->args)
2581                 return CLI_SHOWUSAGE;
2582         ast_cli(a->fd, "Waiting for inactivity to perform restart\n");
2583         quit_handler(0, SHUTDOWN_REALLY_NICE, 1 /* restart */);
2584         return CLI_SUCCESS;
2585 }
2586
2587 static char *handle_abort_shutdown(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2588 {
2589         switch (cmd) {
2590         case CLI_INIT:
2591                 e->command = "core abort shutdown";
2592                 e->usage =
2593                         "Usage: core abort shutdown\n"
2594                         "       Causes Asterisk to abort an executing shutdown or restart, and resume normal\n"
2595                         "       call operations.\n";
2596                 ast_cli_allow_at_shutdown(e);
2597                 return NULL;
2598         case CLI_GENERATE:
2599                 return NULL;
2600         }
2601
2602         if (a->argc != e->args)
2603                 return CLI_SHOWUSAGE;
2604
2605         ast_cancel_shutdown();
2606
2607         return CLI_SUCCESS;
2608 }
2609
2610 static char *handle_bang(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2611 {
2612         switch (cmd) {
2613         case CLI_INIT:
2614                 e->command = "!";
2615                 e->usage =
2616                         "Usage: !<command>\n"
2617                         "       Executes a given shell command\n";
2618                 return NULL;
2619         case CLI_GENERATE:
2620                 return NULL;
2621         }
2622
2623         return CLI_SUCCESS;
2624 }
2625 static const char warranty_lines[] = {
2626         "\n"
2627         "                           NO WARRANTY\n"
2628         "\n"
2629         "BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n"
2630         "FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN\n"
2631         "OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n"
2632         "PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n"
2633         "OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n"
2634         "MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS\n"
2635         "TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE\n"
2636         "PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n"
2637         "REPAIR OR CORRECTION.\n"
2638         "\n"
2639         "IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n"
2640         "WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n"
2641         "REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n"
2642         "INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n"
2643         "OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n"
2644         "TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n"
2645         "YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n"
2646         "PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n"
2647         "POSSIBILITY OF SUCH DAMAGES.\n"
2648 };
2649
2650 static char *show_warranty(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2651 {
2652         switch (cmd) {
2653         case CLI_INIT:
2654                 e->command = "core show warranty";
2655                 e->usage =
2656                         "Usage: core show warranty\n"
2657                         "       Shows the warranty (if any) for this copy of Asterisk.\n";
2658                 return NULL;
2659         case CLI_GENERATE:
2660                 return NULL;
2661         }
2662
2663         ast_cli(a->fd, "%s", warranty_lines);
2664
2665         return CLI_SUCCESS;
2666 }
2667
2668 static const char license_lines[] = {
2669         "\n"
2670         "This program is free software; you can redistribute it and/or modify\n"
2671         "it under the terms of the GNU General Public License version 2 as\n"
2672         "published by the Free Software Foundation.\n"
2673         "\n"
2674         "This program also contains components licensed under other licenses.\n"
2675         "They include:\n"
2676         "\n"
2677         "This program is distributed in the hope that it will be useful,\n"
2678         "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
2679         "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
2680         "GNU General Public License for more details.\n"
2681         "\n"
2682         "You should have received a copy of the GNU General Public License\n"
2683         "along with this program; if not, write to the Free Software\n"
2684         "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n"
2685 };
2686
2687 static char *show_license(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2688 {
2689         switch (cmd) {
2690         case CLI_INIT:
2691                 e->command = "core show license";
2692                 e->usage =
2693                         "Usage: core show license\n"
2694                         "       Shows the license(s) for this copy of Asterisk.\n";
2695                 return NULL;
2696         case CLI_GENERATE:
2697                 return NULL;
2698         }
2699
2700         ast_cli(a->fd, "%s", license_lines);
2701
2702         return CLI_SUCCESS;
2703 }
2704
2705 #define ASTERISK_PROMPT "*CLI> "
2706
2707 /*!
2708  * \brief Shutdown Asterisk CLI commands.
2709  *
2710  * \note These CLI commands cannot be unregistered at shutdown
2711  * because one of them is likely the reason for the shutdown.
2712  * The CLI generates a warning if a command is in-use when it is
2713  * unregistered.
2714  */
2715 static struct ast_cli_entry cli_asterisk_shutdown[] = {
2716         AST_CLI_DEFINE(handle_stop_now, "Shut down Asterisk immediately"),
2717         AST_CLI_DEFINE(handle_stop_gracefully, "Gracefully shut down Asterisk"),
2718         AST_CLI_DEFINE(handle_stop_when_convenient, "Shut down Asterisk at empty call volume"),
2719         AST_CLI_DEFINE(handle_restart_now, "Restart Asterisk immediately"),
2720         AST_CLI_DEFINE(handle_restart_gracefully, "Restart Asterisk gracefully"),
2721         AST_CLI_DEFINE(handle_restart_when_convenient, "Restart Asterisk at empty call volume"),
2722 };
2723
2724 static struct ast_cli_entry cli_asterisk[] = {
2725         AST_CLI_DEFINE(handle_abort_shutdown, "Cancel a running shutdown"),
2726         AST_CLI_DEFINE(show_warranty, "Show the warranty (if any) for this copy of Asterisk"),
2727         AST_CLI_DEFINE(show_license, "Show the license(s) for this copy of Asterisk"),
2728         AST_CLI_DEFINE(handle_version, "Display version info"),
2729         AST_CLI_DEFINE(handle_bang, "Execute a shell command"),
2730 #if !defined(LOW_MEMORY)
2731         AST_CLI_DEFINE(handle_show_threads, "Show running threads"),
2732 #if defined(HAVE_SYSINFO) || defined(HAVE_SYSCTL)
2733         AST_CLI_DEFINE(handle_show_sysinfo, "Show System Information"),
2734 #endif
2735         AST_CLI_DEFINE(handle_show_profile, "Display profiling info"),
2736         AST_CLI_DEFINE(handle_show_settings, "Show some core settings"),
2737         AST_CLI_DEFINE(handle_clear_profile, "Clear profiling info"),
2738 #endif /* ! LOW_MEMORY */
2739 };
2740
2741 static void send_rasterisk_connect_commands(void)
2742 {
2743         char buf[80];
2744
2745         /*
2746          * Tell the server asterisk instance about the verbose level
2747          * initially desired.
2748          */
2749         if (option_verbose) {
2750                 snprintf(buf, sizeof(buf), "core set verbose atleast %d silent", option_verbose);
2751                 fdsend(ast_consock, buf);
2752         }
2753
2754         if (option_debug) {
2755                 snprintf(buf, sizeof(buf), "core set debug atleast %d", option_debug);
2756                 fdsend(ast_consock, buf);
2757         }
2758
2759         if (!ast_opt_mute) {
2760                 fdsend(ast_consock, "logger mute silent");
2761         } else {
2762                 printf("log and verbose output currently muted ('logger mute' to unmute)\n");
2763         }
2764 }
2765
2766 #ifdef HAVE_LIBEDIT_IS_UNICODE
2767 static int ast_el_read_char(EditLine *editline, wchar_t *cp)
2768 #else
2769 static int ast_el_read_char(EditLine *editline, char *cp)
2770 #endif
2771 {
2772         int num_read = 0;
2773         int lastpos = 0;
2774         struct pollfd fds[2];
2775         int res;
2776         int max;
2777 #define EL_BUF_SIZE 512
2778         char buf[EL_BUF_SIZE];
2779
2780         for (;;) {
2781                 max = 1;
2782                 fds[0].fd = ast_consock;
2783                 fds[0].events = POLLIN;
2784                 if (!ast_opt_exec) {
2785                         fds[1].fd = STDIN_FILENO;
2786                         fds[1].events = POLLIN;
2787                         max++;
2788                 }
2789                 res = ast_poll(fds, max, -1);
2790                 if (res < 0) {
2791                         if (sig_flags.need_quit || sig_flags.need_quit_handler)
2792                                 break;
2793                         if (errno == EINTR)
2794                                 continue;
2795                         fprintf(stderr, "poll failed: %s\n", strerror(errno));
2796                         break;
2797                 }
2798
2799                 if (!ast_opt_exec && fds[1].revents) {
2800                         char c = '\0';
2801                         num_read = read(STDIN_FILENO, &c, 1);
2802                         if (num_read < 1) {
2803                                 break;
2804                         } else {
2805 #ifdef  HAVE_LIBEDIT_IS_UNICODE
2806                                 *cp = btowc(c);
2807 #else
2808                                 *cp = c;
2809 #endif
2810                                 return (num_read);
2811                         }
2812                 }
2813                 if (fds[0].revents) {
2814                         res = read(ast_consock, buf, sizeof(buf) - 1);
2815                         /* if the remote side disappears exit */
2816                         if (res < 1) {
2817                                 fprintf(stderr, "\nDisconnected from Asterisk server\n");
2818                                 if (!ast_opt_reconnect) {
2819                                         quit_handler(0, SHUTDOWN_FAST, 0);
2820                                 } else {
2821                                         int tries;
2822                                         int reconnects_per_second = 20;
2823                                         fprintf(stderr, "Attempting to reconnect for 30 seconds\n");
2824                                         for (tries = 0; tries < 30 * reconnects_per_second; tries++) {
2825                                                 if (ast_tryconnect()) {
2826                                                         fprintf(stderr, "Reconnect succeeded after %.3f seconds\n", 1.0 / reconnects_per_second * tries);
2827                                                         printf("%s", term_quit());
2828                                                         WELCOME_MESSAGE;
2829                                                         send_rasterisk_connect_commands();
2830                                                         break;
2831                                                 } else
2832                                                         usleep(1000000 / reconnects_per_second);
2833                                         }
2834                                         if (tries >= 30 * reconnects_per_second) {
2835                                                 fprintf(stderr, "Failed to reconnect for 30 seconds.  Quitting.\n");
2836                                                 quit_handler(0, SHUTDOWN_FAST, 0);
2837                                         }
2838                                 }
2839                                 continue;
2840                         }
2841
2842                         buf[res] = '\0';
2843
2844                         /* Write over the CLI prompt */
2845                         if (!ast_opt_exec && !lastpos) {
2846                                 if (write(STDOUT_FILENO, "\r\e[0K", 5) < 0) {
2847                                 }
2848                         }
2849
2850                         console_print(buf);
2851
2852                         if ((res < EL_BUF_SIZE - 1) && ((buf[res-1] == '\n') || (res >= 2 && buf[res-2] == '\n'))) {
2853 #ifdef  HAVE_LIBEDIT_IS_UNICODE
2854                                 *cp = btowc(CC_REFRESH);
2855 #else
2856                                 *cp = CC_REFRESH;
2857 #endif
2858                                 return(1);
2859                         } else {
2860                                 lastpos = 1;
2861                         }
2862                 }
2863         }
2864
2865 #ifdef  HAVE_LIBEDIT_IS_UNICODE
2866         *cp = btowc('\0');
2867 #else
2868         *cp = '\0';
2869 #endif
2870
2871         return (0);
2872 }
2873
2874 static struct ast_str *prompt = NULL;
2875
2876 static char *cli_prompt(EditLine *editline)
2877 {
2878         char tmp[100];
2879         char *pfmt;
2880         int color_used = 0;
2881         static int cli_prompt_changes = 0;
2882         struct passwd *pw;
2883         struct group *gr;
2884
2885         if (prompt == NULL) {
2886                 prompt = ast_str_create(100);
2887         } else if (!cli_prompt_changes) {
2888                 return ast_str_buffer(prompt);
2889         } else {
2890                 ast_str_reset(prompt);
2891         }
2892
2893         if ((pfmt = getenv("ASTERISK_PROMPT"))) {
2894                 char *t = pfmt;
2895                 struct timeval ts = ast_tvnow();
2896                 while (*t != '\0') {
2897                         if (*t == '%') {
2898                                 char hostname[MAXHOSTNAMELEN] = "";
2899                                 int i, which;
2900                                 struct ast_tm tm = { 0, };
2901                                 int fgcolor = COLOR_WHITE, bgcolor = COLOR_BLACK;
2902
2903                                 t++;
2904                                 switch (*t) {
2905                                 case 'C': /* color */
2906                                         t++;
2907                                         if (sscanf(t, "%30d;%30d%n", &fgcolor, &bgcolor, &i) == 2) {
2908                                                 ast_term_color_code(&prompt, fgcolor, bgcolor);
2909                                                 t += i - 1;
2910                                         } else if (sscanf(t, "%30d%n", &fgcolor, &i) == 1) {
2911                                                 ast_term_color_code(&prompt, fgcolor, 0);
2912                                                 t += i - 1;
2913                                         }
2914
2915                                         /* If the color has been reset correctly, then there's no need to reset it later */
2916                                         color_used = ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) ? 0 : 1;
2917                                         break;
2918                                 case 'd': /* date */
2919                                         if (ast_localtime(&ts, &tm, NULL)) {
2920                                                 ast_strftime(tmp, sizeof(tmp), "%Y-%m-%d", &tm);
2921                                                 ast_str_append(&prompt, 0, "%s", tmp);
2922                                                 cli_prompt_changes++;
2923                                         }
2924                                         break;
2925                                 case 'g': /* group */
2926                                         if ((gr = getgrgid(getgid()))) {
2927                                                 ast_str_append(&prompt, 0, "%s", gr->gr_name);
2928                                         }
2929                                         break;
2930                                 case 'h': /* hostname */
2931                                         if (!gethostname(hostname, sizeof(hostname) - 1)) {
2932                                                 ast_str_append(&prompt, 0, "%s", hostname);
2933                                         } else {
2934                                                 ast_str_append(&prompt, 0, "%s", "localhost");
2935                                         }
2936                                         break;
2937                                 case 'H': /* short hostname */
2938                                         if (!gethostname(hostname, sizeof(hostname) - 1)) {
2939                                                 char *dotptr;
2940                                                 if ((dotptr = strchr(hostname, '.'))) {
2941                                                         *dotptr = '\0';
2942                                                 }
2943                                                 ast_str_append(&prompt, 0, "%s", hostname);
2944                                         } else {
2945                                                 ast_str_append(&prompt, 0, "%s", "localhost");
2946                                         }
2947                                         break;
2948 #ifdef HAVE_GETLOADAVG
2949                                 case 'l': /* load avg */
2950                                         t++;
2951                                         if (sscanf(t, "%30d", &which) == 1 && which > 0 && which <= 3) {
2952                                                 double list[3];
2953                                                 getloadavg(list, 3);
2954                                                 ast_str_append(&prompt, 0, "%.2f", list[which - 1]);
2955                                                 cli_prompt_changes++;
2956                                         }
2957                                         break;
2958 #endif
2959                                 case 's': /* Asterisk system name (from asterisk.conf) */
2960                                         ast_str_append(&prompt, 0, "%s", ast_config_AST_SYSTEM_NAME);
2961                                         break;
2962                                 case 't': /* time */
2963                                         if (ast_localtime(&ts, &tm, NULL)) {
2964                                                 ast_strftime(tmp, sizeof(tmp), "%H:%M:%S", &tm);
2965                                                 ast_str_append(&prompt, 0, "%s", tmp);
2966                                                 cli_prompt_changes++;
2967                                         }
2968                                         break;
2969                                 case 'u': /* username */
2970                                         if ((pw = getpwuid(getuid()))) {
2971                                                 ast_str_append(&prompt, 0, "%s", pw->pw_name);
2972                                         }
2973                                         break;
2974                                 case '#': /* process console or remote? */
2975                                         ast_str_append(&prompt, 0, "%c", ast_opt_remote ? '>' : '#');
2976                                         break;
2977                                 case '%': /* literal % */
2978                                         ast_str_append(&prompt, 0, "%c", '%');
2979                                         break;
2980                                 case '\0': /* % is last character - prevent bug */
2981                                         t--;
2982                                         break;
2983                                 }
2984                         } else {
2985                                 ast_str_append(&prompt, 0, "%c", *t);
2986                         }
2987                         t++;
2988                 }
2989                 if (color_used) {
2990                         /* Force colors back to normal at end */
2991                         ast_term_color_code(&prompt, 0, 0);
2992                 }
2993         } else {
2994                 ast_str_set(&prompt, 0, "%s%s",
2995                         remotehostname ? remotehostname : "",
2996                         ASTERISK_PROMPT);
2997         }
2998
2999         return ast_str_buffer(prompt);
3000 }
3001
3002 static struct ast_vector_string *ast_el_strtoarr(char *buf)
3003 {
3004         char *retstr;
3005         struct ast_vector_string *vec = ast_calloc(1, sizeof(*vec));
3006
3007         if (!vec) {
3008                 return NULL;
3009         }
3010
3011         while ((retstr = strsep(&buf, " "))) {
3012                 if (!strcmp(retstr, AST_CLI_COMPLETE_EOF)) {
3013                         break;
3014                 }
3015
3016                 retstr = ast_strdup(retstr);
3017                 if (!retstr || AST_VECTOR_APPEND(vec, retstr)) {
3018                         ast_free(retstr);
3019                         goto vector_cleanup;
3020                 }
3021         }
3022
3023         if (!AST_VECTOR_SIZE(vec)) {
3024                 goto vector_cleanup;
3025         }
3026
3027         return vec;
3028
3029 vector_cleanup:
3030         AST_VECTOR_CALLBACK_VOID(vec, ast_free);
3031         AST_VECTOR_PTR_FREE(vec);
3032
3033         return NULL;
3034 }
3035
3036 static void ast_cli_display_match_list(struct ast_vector_string *matches, int max)
3037 {
3038         int idx = 1;
3039         /* find out how many entries can be put on one line, with two spaces between strings */
3040         int limit = ast_get_termcols(STDOUT_FILENO) / (max + 2);
3041
3042         if (limit == 0) {
3043                 limit = 1;
3044         }
3045
3046         for (;;) {
3047                 int numoutputline;
3048
3049                 for (numoutputline = 0; numoutputline < limit && idx < AST_VECTOR_SIZE(matches); idx++) {
3050                         numoutputline++;
3051                         fprintf(stdout, "%-*s  ", max, AST_VECTOR_GET(matches, idx));
3052                 }
3053
3054                 if (!numoutputline) {
3055                         break;
3056                 }
3057
3058                 fprintf(stdout, "\n");
3059         }
3060 }
3061
3062
3063 static char *cli_complete(EditLine *editline, int ch)
3064 {
3065         int len = 0;
3066         char *ptr;
3067         struct ast_vector_string *matches;
3068         int retval = CC_ERROR;
3069         char savechr;
3070         int res;
3071
3072         LineInfo *lf = (LineInfo *)el_line(editline);
3073
3074         savechr = *(char *)lf->cursor;
3075         *(char *)lf->cursor = '\0';
3076         ptr = (char *)lf->cursor;
3077         if (ptr) {
3078                 while (ptr > lf->buffer) {
3079                         if (isspace(*ptr)) {
3080                                 ptr++;
3081                                 break;
3082                         }
3083                         ptr--;
3084                 }
3085         }
3086
3087         len = lf->cursor - ptr;
3088
3089         if (ast_opt_remote) {
3090 #define CMD_MATCHESARRAY "_COMMAND MATCHESARRAY \"%s\" \"%s\""
3091                 char *mbuf;
3092                 char *new_mbuf;
3093                 int mlen = 0, maxmbuf = 2048;
3094
3095                 /* Start with a 2048 byte buffer */
3096                 mbuf = ast_malloc(maxmbuf);
3097
3098                 /* This will run snprintf twice at most. */
3099                 while (mbuf && (mlen = snprintf(mbuf, maxmbuf, CMD_MATCHESARRAY, lf->buffer, ptr)) > maxmbuf) {
3100                         /* Return value does not include space for NULL terminator. */
3101                         maxmbuf = mlen + 1;
3102                         ast_free(mbuf);
3103                         mbuf = ast_malloc(maxmbuf);
3104                 }
3105
3106                 if (!mbuf) {
3107                         *((char *) lf->cursor) = savechr;
3108
3109                         return (char *)(CC_ERROR);
3110                 }
3111
3112                 fdsend(ast_consock, mbuf);
3113                 res = 0;
3114                 mlen = 0;
3115                 mbuf[0] = '\0';
3116
3117                 while (!strstr(mbuf, AST_CLI_COMPLETE_EOF) && res != -1) {
3118                         if (mlen + 1024 > maxmbuf) {
3119                                 /* Expand buffer to the next 1024 byte increment. */
3120                                 maxmbuf = mlen + 1024;
3121                                 new_mbuf = ast_realloc(mbuf, maxmbuf);
3122                                 if (!new_mbuf) {
3123                                         ast_free(mbuf);
3124                                         *((char *) lf->cursor) = savechr;
3125
3126                                         return (char *)(CC_ERROR);
3127                                 }
3128                                 mbuf = new_mbuf;
3129                         }
3130                         /* Only read 1024 bytes at a time */
3131                         res = read(ast_consock, mbuf + mlen, 1024);
3132                         if (res > 0) {
3133                                 mlen += res;
3134                         }
3135                 }
3136                 mbuf[mlen] = '\0';
3137
3138                 matches = ast_el_strtoarr(mbuf);
3139                 ast_free(mbuf);
3140         } else {
3141                 matches = ast_cli_completion_vector((char *)lf->buffer, ptr);
3142         }
3143
3144         if (matches) {
3145                 int i;
3146                 int maxlen, match_len;
3147                 const char *best_match = AST_VECTOR_GET(matches, 0);
3148
3149                 if (!ast_strlen_zero(best_match)) {
3150                         el_deletestr(editline, (int) len);
3151                         el_insertstr(editline, best_match);
3152                         retval = CC_REFRESH;
3153                 }
3154
3155                 if (AST_VECTOR_SIZE(matches) == 2) {
3156                         /* Found an exact match */
3157                         el_insertstr(editline, " ");
3158                         retval = CC_REFRESH;
3159                 } else {
3160                         /* Must be more than one match */
3161                         for (i = 1, maxlen = 0; i < AST_VECTOR_SIZE(matches); i++) {
3162                                 match_len = strlen(AST_VECTOR_GET(matches, i));
3163                                 if (match_len > maxlen) {
3164                                         maxlen = match_len;
3165                                 }
3166                         }
3167
3168                         fprintf(stdout, "\n");
3169                         ast_cli_display_match_list(matches, maxlen);
3170                         retval = CC_REDISPLAY;
3171                 }
3172                 AST_VECTOR_CALLBACK_VOID(matches, ast_free);
3173                 AST_VECTOR_PTR_FREE(matches);
3174         }
3175
3176         *((char *) lf->cursor) = savechr;
3177
3178         return (char *)(long)retval;
3179 }
3180
3181 static int ast_el_initialize(void)
3182 {
3183         HistEvent ev;
3184         char *editor, *editrc = getenv("EDITRC");
3185
3186         if (!(editor = getenv("AST_EDITMODE"))) {
3187                 if (!(editor = getenv("AST_EDITOR"))) {
3188                         editor = "emacs";
3189                 }
3190         }
3191
3192         if (el != NULL)
3193                 el_end(el);
3194         if (el_hist != NULL)
3195                 history_end(el_hist);
3196
3197         el = el_init("asterisk", stdin, stdout, stderr);
3198         el_set(el, EL_PROMPT, cli_prompt);
3199
3200         el_set(el, EL_EDITMODE, 1);
3201         el_set(el, EL_EDITOR, editor);
3202         el_hist = history_init();
3203         if (!el || !el_hist)
3204                 return -1;
3205
3206         /* setup history with 100 entries */
3207         history(el_hist, &ev, H_SETSIZE, 100);
3208
3209         el_set(el, EL_HIST, history, el_hist);
3210
3211         el_set(el, EL_ADDFN, "ed-complete", "Complete argument", cli_complete);
3212         /* Bind <tab> to command completion */
3213         el_set(el, EL_BIND, "^I", "ed-complete", NULL);
3214         /* Bind ? to command completion */
3215         el_set(el, EL_BIND, "?", "ed-complete", NULL);
3216         /* Bind ^D to redisplay */
3217         el_set(el, EL_BIND, "^D", "ed-redisplay", NULL);
3218         /* Bind Delete to delete char left */
3219         el_set(el, EL_BIND, "\\e[3~", "ed-delete-next-char", NULL);
3220         /* Bind Home and End to move to line start and end */
3221         el_set(el, EL_BIND, "\\e[1~", "ed-move-to-beg", NULL);
3222         el_set(el, EL_BIND, "\\e[4~", "ed-move-to-end", NULL);
3223         /* Bind C-left and C-right to move by word (not all terminals) */
3224         el_set(el, EL_BIND, "\\eOC", "vi-next-word", NULL);
3225         el_set(el, EL_BIND, "\\eOD", "vi-prev-word", NULL);
3226
3227         if (editrc) {
3228                 el_source(el, editrc);
3229         }
3230
3231         return 0;
3232 }
3233
3234 #define MAX_HISTORY_COMMAND_LENGTH 256
3235
3236 static int ast_el_add_history(const char *buf)
3237 {
3238         HistEvent ev;