Merge in ast_strftime branch, which changes timestamps to be accurate to the microsec...
[asterisk/asterisk.git] / main / asterisk.c
index 35c6745..56c5889 100644 (file)
@@ -61,6 +61,8 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#undef sched_setscheduler
+#undef setpriority
 #include <unistd.h>
 #include <stdlib.h>
 #include <sys/time.h>
@@ -78,6 +80,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include <grp.h>
 #include <pwd.h>
 #include <sys/stat.h>
+#if defined(HAVE_SYSINFO)
+#include <sys/sysinfo.h>
+#endif
 #ifdef linux
 #include <sys/prctl.h>
 #ifdef HAVE_CAP
@@ -121,6 +126,7 @@ int daemon(int, int);  /* defined in libresolv of all places */
 #include "asterisk/version.h"
 #include "asterisk/linkedlists.h"
 #include "asterisk/devicestate.h"
+#include "asterisk/module.h"
 
 #include "asterisk/doxyref.h"          /* Doxygen documentation */
 
@@ -136,7 +142,7 @@ int daemon(int, int);  /* defined in libresolv of all places */
 
 /*! \brief Welcome message when starting a CLI interface */
 #define WELCOME_MESSAGE \
-    ast_verbose("Asterisk " ASTERISK_VERSION ", Copyright (C) 1999 - 2006 Digium, Inc. and others.\n" \
+    ast_verbose("Asterisk " ASTERISK_VERSION ", Copyright (C) 1999 - 2007 Digium, Inc. and others.\n" \
                 "Created by Mark Spencer <markster@digium.com>\n" \
                 "Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for details.\n" \
                 "This is free software, with components licensed under the GNU General Public\n" \
@@ -161,6 +167,9 @@ int option_debug;                           /*!< Debug level */
 double option_maxload;                         /*!< Max load avg on system */
 int option_maxcalls;                           /*!< Max number of active calls */
 int option_maxfiles;                           /*!< Max number of open file handles (files, sockets) */
+#if defined(HAVE_SYSINFO)
+long option_minmemfree;                                /*!< Minimum amount of free system memory - stop accepting calls if free memory falls below this watermark */
+#endif
 
 /*! @} */
 
@@ -179,13 +188,13 @@ struct console {
 
 struct ast_atexit {
        void (*func)(void);
-       AST_LIST_ENTRY(ast_atexit) list;
+       AST_RWLIST_ENTRY(ast_atexit) list;
 };
 
-static AST_LIST_HEAD_STATIC(atexits, ast_atexit);
+static AST_RWLIST_HEAD_STATIC(atexits, ast_atexit);
 
-time_t ast_startuptime;
-time_t ast_lastreloadtime;
+struct timeval ast_startuptime;
+struct timeval ast_lastreloadtime;
 
 static History *el_hist;
 static EditLine *el;
@@ -243,12 +252,12 @@ static struct {
 
 #if !defined(LOW_MEMORY)
 struct file_version {
-       AST_LIST_ENTRY(file_version) list;
+       AST_RWLIST_ENTRY(file_version) list;
        const char *file;
        char *version;
 };
 
-static AST_LIST_HEAD_STATIC(file_versions, file_version);
+static AST_RWLIST_HEAD_STATIC(file_versions, file_version);
 
 void ast_register_file_version(const char *file, const char *version)
 {
@@ -266,35 +275,36 @@ void ast_register_file_version(const char *file, const char *version)
        new->file = file;
        new->version = (char *) new + sizeof(*new);
        memcpy(new->version, work, version_length);
-       AST_LIST_LOCK(&file_versions);
-       AST_LIST_INSERT_HEAD(&file_versions, new, list);
-       AST_LIST_UNLOCK(&file_versions);
+       AST_RWLIST_WRLOCK(&file_versions);
+       AST_RWLIST_INSERT_HEAD(&file_versions, new, list);
+       AST_RWLIST_UNLOCK(&file_versions);
 }
 
 void ast_unregister_file_version(const char *file)
 {
        struct file_version *find;
 
-       AST_LIST_LOCK(&file_versions);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&file_versions, find, list) {
+       AST_RWLIST_WRLOCK(&file_versions);
+       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&file_versions, find, list) {
                if (!strcasecmp(find->file, file)) {
-                       AST_LIST_REMOVE_CURRENT(&file_versions, list);
+                       AST_RWLIST_REMOVE_CURRENT(&file_versions, list);
                        break;
                }
        }
-       AST_LIST_TRAVERSE_SAFE_END;
-       AST_LIST_UNLOCK(&file_versions);
+       AST_RWLIST_TRAVERSE_SAFE_END;
+       AST_RWLIST_UNLOCK(&file_versions);
+
        if (find)
-               free(find);
+               ast_free(find);
 }
 
 struct thread_list_t {
-       AST_LIST_ENTRY(thread_list_t) list;
+       AST_RWLIST_ENTRY(thread_list_t) list;
        char *name;
        pthread_t id;
 };
 
-static AST_LIST_HEAD_STATIC(thread_list, thread_list_t);
+static AST_RWLIST_HEAD_STATIC(thread_list, thread_list_t);
 
 static const char show_threads_help[] =
 "Usage: core show threads\n"
@@ -308,27 +318,27 @@ void ast_register_thread(char *name)
                return;
        new->id = pthread_self();
        new->name = name; /* steal the allocated memory for the thread name */
-       AST_LIST_LOCK(&thread_list);
-       AST_LIST_INSERT_HEAD(&thread_list, new, list);
-       AST_LIST_UNLOCK(&thread_list);
+       AST_RWLIST_WRLOCK(&thread_list);
+       AST_RWLIST_INSERT_HEAD(&thread_list, new, list);
+       AST_RWLIST_UNLOCK(&thread_list);
 }
 
 void ast_unregister_thread(void *id)
 {
        struct thread_list_t *x;
 
-       AST_LIST_LOCK(&thread_list);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&thread_list, x, list) {
+       AST_RWLIST_WRLOCK(&thread_list);
+       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&thread_list, x, list) {
                if ((void *) x->id == id) {
-                       AST_LIST_REMOVE_CURRENT(&thread_list, list);
+                       AST_RWLIST_REMOVE_CURRENT(&thread_list, list);
                        break;
                }
        }
-       AST_LIST_TRAVERSE_SAFE_END;
-       AST_LIST_UNLOCK(&thread_list);
+       AST_RWLIST_TRAVERSE_SAFE_END;
+       AST_RWLIST_UNLOCK(&thread_list);
        if (x) {
-               free(x->name);
-               free(x);
+               ast_free(x->name);
+               ast_free(x);
        }
 }
 
@@ -336,7 +346,7 @@ void ast_unregister_thread(void *id)
 static int handle_show_settings(int fd, int argc, char *argv[])
 {
        char buf[BUFSIZ];
-       struct tm tm;
+       struct ast_tm tm;
 
        ast_cli(fd, "\nPBX Core settings\n");
        ast_cli(fd, "-----------------\n");
@@ -352,12 +362,15 @@ static int handle_show_settings(int fd, int argc, char *argv[])
        ast_cli(fd, "  Verbosity:                   %d\n", option_verbose);
        ast_cli(fd, "  Debug level:                 %d\n", option_debug);
        ast_cli(fd, "  Max load avg:                %lf\n", option_maxload);
-       if (localtime_r(&ast_startuptime, &tm)) {
-               strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
+#if defined(HAVE_SYSINFO)
+       ast_cli(fd, "  Min Free Memory:             %ld MB\n", option_minmemfree);
+#endif
+       if (ast_localtime(&ast_startuptime, &tm, NULL)) {
+               ast_strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
                ast_cli(fd, "  Startup time:                %s\n", buf);
        }
-       if (localtime_r(&ast_lastreloadtime, &tm)) {
-               strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
+       if (ast_localtime(&ast_lastreloadtime, &tm, NULL)) {
+               ast_strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
                ast_cli(fd, "  Last reload time:            %s\n", buf);
        }
        ast_cli(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);
@@ -395,16 +408,43 @@ static int handle_show_threads(int fd, int argc, char *argv[])
        int count = 0;
        struct thread_list_t *cur;
 
-       AST_LIST_LOCK(&thread_list);
-       AST_LIST_TRAVERSE(&thread_list, cur, list) {
+       AST_RWLIST_RDLOCK(&thread_list);
+       AST_RWLIST_TRAVERSE(&thread_list, cur, list) {
                ast_cli(fd, "%p %s\n", (void *)cur->id, cur->name);
                count++;
        }
-        AST_LIST_UNLOCK(&thread_list);
+        AST_RWLIST_UNLOCK(&thread_list);
        ast_cli(fd, "%d threads listed.\n", count);
        return 0;
 }
 
+#if defined(HAVE_SYSINFO)
+static const char show_sysinfo_help[] =
+"Usage: core show sysinfo\n"
+"       List current system information.\n";
+
+/*! \brief Give an overview of system statistics */
+static int handle_show_sysinfo(int fd, int argc, char *argv[])
+{
+       struct sysinfo sys_info;
+
+       if (sysinfo(&sys_info)) {
+               ast_cli(fd, "FAILED to retrieve system information\n\n");
+               return 0;
+       }
+       ast_cli(fd, "\nSystem Statistics\n");
+       ast_cli(fd, "-----------------\n");
+       ast_cli(fd, "  System Uptime:             %ld hours\n", sys_info.uptime/3600);
+       ast_cli(fd, "  Total RAM:                 %ld KiB\n", (sys_info.totalram / sys_info.mem_unit)/1024);
+       ast_cli(fd, "  Free RAM:                  %ld KiB\n", (sys_info.freeram / sys_info.mem_unit)/1024);
+       ast_cli(fd, "  Buffer RAM:                %ld KiB\n", (sys_info.bufferram / sys_info.mem_unit)/1024);
+       ast_cli(fd, "  Total Swap Space:          %ld KiB\n", (sys_info.totalswap / sys_info.mem_unit)/1024);
+       ast_cli(fd, "  Free Swap Space:           %ld KiB\n\n", (sys_info.freeswap / sys_info.mem_unit)/1024);
+       ast_cli(fd, "  Number of Processes:       %d \n\n", sys_info.procs);
+       return 0;
+}
+#endif
+
 struct profile_entry {
        const char *name;
        uint64_t        scale;  /* if non-zero, values are scaled by this */
@@ -583,8 +623,8 @@ static int handle_show_version_files(int fd, int argc, char *argv[])
 
        ast_cli(fd, FORMAT, "File", "Revision");
        ast_cli(fd, FORMAT, "----", "--------");
-       AST_LIST_LOCK(&file_versions);
-       AST_LIST_TRAVERSE(&file_versions, iterator, list) {
+       AST_RWLIST_RDLOCK(&file_versions);
+       AST_RWLIST_TRAVERSE(&file_versions, iterator, list) {
                if (havename && strcasecmp(iterator->file, argv[4]))
                        continue;
 
@@ -596,7 +636,7 @@ static int handle_show_version_files(int fd, int argc, char *argv[])
                if (havename)
                        break;
        }
-       AST_LIST_UNLOCK(&file_versions);
+       AST_RWLIST_UNLOCK(&file_versions);
        if (!havename) {
                ast_cli(fd, "%d files listed.\n", count_files);
        }
@@ -618,14 +658,14 @@ static char *complete_show_version_files(const char *line, const char *word, int
        if (pos != 3)
                return NULL;
 
-       AST_LIST_LOCK(&file_versions);
-       AST_LIST_TRAVERSE(&file_versions, find, list) {
+       AST_RWLIST_RDLOCK(&file_versions);
+       AST_RWLIST_TRAVERSE(&file_versions, find, list) {
                if (!strncasecmp(word, find->file, matchlen) && ++which > state) {
                        ret = ast_strdup(find->file);
                        break;
                }
        }
-       AST_LIST_UNLOCK(&file_versions);
+       AST_RWLIST_UNLOCK(&file_versions);
 
        return ret;
 }
@@ -636,28 +676,28 @@ int ast_register_atexit(void (*func)(void))
        int res = -1;
        struct ast_atexit *ae;
        ast_unregister_atexit(func);    
-       AST_LIST_LOCK(&atexits);
+       AST_RWLIST_WRLOCK(&atexits);
        if ((ae = ast_calloc(1, sizeof(*ae)))) {
-               AST_LIST_INSERT_HEAD(&atexits, ae, list);
+               AST_RWLIST_INSERT_HEAD(&atexits, ae, list);
                ae->func = func;
                res = 0;
        }
-       AST_LIST_UNLOCK(&atexits);
+       AST_RWLIST_UNLOCK(&atexits);
        return res;
 }
 
 void ast_unregister_atexit(void (*func)(void))
 {
        struct ast_atexit *ae;
-       AST_LIST_LOCK(&atexits);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&atexits, ae, list) {
+       AST_RWLIST_WRLOCK(&atexits);
+       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&atexits, ae, list) {
                if (ae->func == func) {
-                       AST_LIST_REMOVE_CURRENT(&atexits, list);
+                       AST_RWLIST_REMOVE_CURRENT(&atexits, list);
                        break;
                }
        }
-       AST_LIST_TRAVERSE_SAFE_END
-       AST_LIST_UNLOCK(&atexits);
+       AST_RWLIST_TRAVERSE_SAFE_END
+       AST_RWLIST_UNLOCK(&atexits);
 }
 
 static int fdprint(int fd, const char *s)
@@ -894,9 +934,6 @@ static void *listener(void *unused)
        int x;
        int flags;
        struct pollfd fds[1];
-       pthread_attr_t attr;
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        for (;;) {
                if (ast_socket < 0)
                        return NULL;
@@ -928,7 +965,7 @@ static void *listener(void *unused)
                                        fcntl(consoles[x].p[1], F_SETFL, flags | O_NONBLOCK);
                                        consoles[x].fd = s;
                                        consoles[x].mute = ast_opt_mute;
-                                       if (ast_pthread_create_background(&consoles[x].t, &attr, netconsole, &consoles[x])) {
+                                       if (ast_pthread_create_detached_background(&consoles[x].t, NULL, netconsole, &consoles[x])) {
                                                ast_log(LOG_ERROR, "Unable to spawn thread to handle connection: %s\n", strerror(errno));
                                                close(consoles[x].p[0]);
                                                close(consoles[x].p[1]);
@@ -1133,10 +1170,8 @@ int ast_set_priority(int pri)
                                ast_verbose("Set to realtime thread\n");
        } else {
                sched.sched_priority = 0;
-               if (sched_setscheduler(0, SCHED_OTHER, &sched)) {
-                       ast_log(LOG_WARNING, "Unable to set normal priority\n");
-                       return -1;
-               }
+               /* According to the manpage, these parameters can never fail. */
+               sched_setscheduler(0, SCHED_OTHER, &sched);
        }
 #else
        if (pri) {
@@ -1147,10 +1182,8 @@ int ast_set_priority(int pri)
                        if (option_verbose)
                                ast_verbose("Set to high priority\n");
        } else {
-               if (setpriority(PRIO_PROCESS, 0, 0) == -1) {
-                       ast_log(LOG_WARNING, "Unable to set normal priority\n");
-                       return -1;
-               }
+               /* According to the manpage, these parameters can never fail. */
+               setpriority(PRIO_PROCESS, 0, 0);
        }
 #endif
        return 0;
@@ -1159,12 +1192,12 @@ int ast_set_priority(int pri)
 static void ast_run_atexits(void)
 {
        struct ast_atexit *ae;
-       AST_LIST_LOCK(&atexits);
-       AST_LIST_TRAVERSE(&atexits, ae, list) {
+       AST_RWLIST_RDLOCK(&atexits);
+       AST_RWLIST_TRAVERSE(&atexits, ae, list) {
                if (ae->func) 
                        ae->func();
        }
-       AST_LIST_UNLOCK(&atexits);
+       AST_RWLIST_UNLOCK(&atexits);
 }
 
 static void quit_handler(int num, int nice, int safeshutdown, int restart)
@@ -1213,6 +1246,9 @@ static void quit_handler(int num, int nice, int safeshutdown, int restart)
                                ast_verbose("Asterisk %s cancelled.\n", restart ? "restart" : "shutdown");
                        return;
                }
+
+               if (nice)
+                       ast_module_shutdown();
        }
        if (ast_opt_console || ast_opt_remote) {
                if (getenv("HOME")) 
@@ -1230,8 +1266,7 @@ static void quit_handler(int num, int nice, int safeshutdown, int restart)
        /* Called on exit */
        if (option_verbose && ast_opt_console)
                ast_verbose("Asterisk %s ending (%d).\n", ast_active_channels() ? "uncleanly" : "cleanly", num);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "Asterisk ending (%d).\n", num);
+       ast_debug(1, "Asterisk ending (%d).\n", num);
        manager_event(EVENT_FLAG_SYSTEM, "Shutdown", "Shutdown: %s\r\nRestart: %s\r\n", ast_active_channels() ? "Uncleanly" : "Cleanly", restart ? "True" : "False");
        if (ast_socket > -1) {
                pthread_cancel(lthread);
@@ -1416,12 +1451,6 @@ static const char version_help[] =
 "Usage: core show version\n"
 "       Shows Asterisk version information.\n";
 
-#if defined(MARKO_BDAY)
-static const char markobday_help[] =
-"Usage: marko show birthday\n"
-"       Shows time until/since Mark Spencers 30th birthday.\n";
-#endif
-
 static int handle_version(int fd, int argc, char *argv[])
 {
        if (argc != 3)
@@ -1432,73 +1461,6 @@ static int handle_version(int fd, int argc, char *argv[])
        return RESULT_SUCCESS;
 }
 
-#if defined(MARKO_BDAY)
-static void print_markobdaystr(int fd, time_t timeval, const char *prefix)
-{
-       int x; /* the main part - years, weeks, etc. */
-       struct ast_str *out;
-
-#define SECOND (1)
-#define MINUTE (SECOND*60)
-#define HOUR (MINUTE*60)
-#define DAY (HOUR*24)
-#define WEEK (DAY*7)
-#define YEAR (DAY*365)
-#define NEEDCOMMA(x) ((x)? ",": "")    /* define if we need a comma */
-       if (timeval < 0)        /* invalid, nothing to show */
-               return;
-
-       out = ast_str_alloca(256);
-       if (timeval > YEAR) {
-               x = (timeval / YEAR);
-               timeval -= (x * YEAR);
-               ast_str_append(&out, 0, "%d year%s%s ", x, ESS(x),NEEDCOMMA(timeval));
-       }
-       if (timeval > WEEK) {
-               x = (timeval / WEEK);
-               timeval -= (x * WEEK);
-               ast_str_append(&out, 0, "%d week%s%s ", x, ESS(x),NEEDCOMMA(timeval));
-       }
-       if (timeval > DAY) {
-               x = (timeval / DAY);
-               timeval -= (x * DAY);
-               ast_str_append(&out, 0, "%d day%s%s ", x, ESS(x),NEEDCOMMA(timeval));
-       }
-       if (timeval > HOUR) {
-               x = (timeval / HOUR);
-               timeval -= (x * HOUR);
-               ast_str_append(&out, 0, "%d hour%s%s ", x, ESS(x),NEEDCOMMA(timeval));
-       }
-       if (timeval > MINUTE) {
-               x = (timeval / MINUTE);
-               timeval -= (x * MINUTE);
-               ast_str_append(&out, 0, "%d minute%s%s ", x, ESS(x),NEEDCOMMA(timeval));
-       }
-       x = timeval;
-       if (x > 0 || out->used == 0)    /* if there is nothing, print 0 seconds */
-               ast_str_append(&out, 0, "%d second%s ", x, ESS(x));
-       ast_cli(fd, "%s: %s\n", prefix, out->str);
-}
-
-static int handle_markobday(int fd, int argc, char *argv[])
-{
-       time_t markobdaystarttime = 1176008400; /* 2007-04-08 00:00:00 */
-       time_t markobdayendtime = 1176094799;   /* 2007-04-08 23:59:59 */
-       time_t curtime;
-
-       curtime = time(NULL);
-       if (markobdaystarttime && markobdayendtime) {
-               if (curtime >= markobdaystarttime && curtime <= markobdayendtime)
-                       ast_cli(fd, "Happy 30th birthday Marko!\n");
-               else if (curtime > markobdayendtime)
-                       print_markobdaystr(fd, curtime - markobdayendtime, "Time since Mark Spencers 30th birthday");
-               else
-                       print_markobdaystr(fd, markobdaystarttime - curtime, "Time until Mark Spencers 30th birthday");
-       }
-       return RESULT_SUCCESS;
-}
-#endif
-
 #if 0
 static int handle_quit(int fd, int argc, char *argv[])
 {
@@ -1674,12 +1636,6 @@ static struct ast_cli_entry cli_asterisk[] = {
        handle_version, "Display version info",
        version_help },
 
-#if defined(MARKO_BDAY)
-       { { "marko", "show", "birthday", NULL },
-       handle_markobday, "Display time until/since Mark Spencers 30th birthday",
-       markobday_help },
-#endif
-
        { { "!", NULL },
        handle_bang, "Execute a shell command",
        bang_help },
@@ -1693,6 +1649,12 @@ static struct ast_cli_entry cli_asterisk[] = {
        handle_show_threads, "Show running threads",
        show_threads_help },
 
+#if defined(HAVE_SYSINFO)
+       { { "core", "show", "sysinfo", NULL },
+       handle_show_sysinfo, "Show System Information",
+       show_sysinfo_help },
+#endif
+
        { { "core", "show", "profile", NULL },
        handle_show_profile, "Display profiling info",
        NULL },
@@ -1798,8 +1760,8 @@ static char *cli_prompt(EditLine *el)
                        if (*t == '%') {
                                char hostname[MAXHOSTNAMELEN]="";
                                int i;
-                               time_t ts;
-                               struct tm tm;
+                               struct timeval ts = ast_tvnow();
+                               struct ast_tm tm = { 0, };
 #ifdef linux
                                FILE *LOADAVG;
 #endif
@@ -1821,10 +1783,8 @@ static char *cli_prompt(EditLine *el)
                                        color_used = ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) ? 0 : 1;
                                        break;
                                case 'd': /* date */
-                                       memset(&tm, 0, sizeof(tm));
-                                       time(&ts);
-                                       if (localtime_r(&ts, &tm)) 
-                                               strftime(p, sizeof(prompt) - strlen(prompt), "%Y-%m-%d", &tm);
+                                       if (ast_localtime(&ts, &tm, NULL))
+                                               ast_strftime(p, sizeof(prompt) - strlen(prompt), "%Y-%m-%d", &tm);
                                        break;
                                case 'h': /* hostname */
                                        if (!gethostname(hostname, sizeof(hostname) - 1))
@@ -1878,10 +1838,8 @@ static char *cli_prompt(EditLine *el)
                                        strncat(p, ast_config_AST_SYSTEM_NAME, sizeof(prompt) - strlen(prompt) - 1);
                                        break;
                                case 't': /* time */
-                                       memset(&tm, 0, sizeof(tm));
-                                       time(&ts);
-                                       if (localtime_r(&ts, &tm))
-                                               strftime(p, sizeof(prompt) - strlen(prompt), "%H:%M:%S", &tm);
+                                       if (ast_localtime(&ts, &tm, NULL))
+                                               ast_strftime(p, sizeof(prompt) - strlen(prompt), "%H:%M:%S", &tm);
                                        break;
                                case '#': /* process console or remote? */
                                        if (!ast_opt_remote) 
@@ -1939,7 +1897,7 @@ static char **ast_el_strtoarr(char *buf)
                        }
                }
 
-               match_list[matches++] = strdup(retstr);
+               match_list[matches++] = ast_strdup(retstr);
        }
 
        if (!match_list)
@@ -1995,7 +1953,7 @@ static int ast_cli_display_match_list(char **matches, int len, int max)
                        /* Don't print dupes */
                        if ( (matches[idx+1] != NULL && strcmp(matches[idx], matches[idx+1]) == 0 ) ) {
                                i--;
-                               free(matches[idx]);
+                               ast_free(matches[idx]);
                                matches[idx] = NULL;
                                continue;
                        }
@@ -2003,7 +1961,7 @@ static int ast_cli_display_match_list(char **matches, int len, int max)
                        numoutput++;
                        numoutputline++;
                        fprintf(stdout, "%-*s  ", max, matches[idx]);
-                       free(matches[idx]);
+                       ast_free(matches[idx]);
                        matches[idx] = NULL;
                }
                if (numoutputline > 0)
@@ -2072,7 +2030,7 @@ static char *cli_complete(EditLine *el, int ch)
                        mbuf[mlen] = '\0';
 
                        matches = ast_el_strtoarr(mbuf);
-                       free(mbuf);
+                       ast_free(mbuf);
                } else
                        matches = (char **) NULL;
        } else {
@@ -2118,8 +2076,8 @@ static char *cli_complete(EditLine *el, int ch)
                        }
                }
                for (i = 0; matches[i]; i++)
-                       free(matches[i]);
-               free(matches);
+                       ast_free(matches[i]);
+               ast_free(matches);
        }
 
        return (char *)(long)retval;
@@ -2241,10 +2199,6 @@ static void ast_remotecontrol(char * data)
        fdprint(ast_consock, tmp);
        snprintf(tmp, sizeof(tmp), "core set debug atleast %d", option_debug);
        fdprint(ast_consock, tmp);
-#if defined(MARKO_BDAY)
-       snprintf(tmp, sizeof(tmp), "marko show birthday");
-       fdprint(ast_consock, tmp);
-#endif
        if (ast_opt_mute) {
                snprintf(tmp, sizeof(tmp), "log and verbose output currently muted ('logger unmute' to unmute)");
                fdprint(ast_consock, tmp);
@@ -2274,6 +2228,9 @@ static void ast_remotecontrol(char * data)
        for (;;) {
                ebuf = (char *)el_gets(el, &num);
 
+               if (!ebuf && write(1, "", 1) < 0)
+                       break;
+
                if (!ast_strlen_zero(ebuf)) {
                        if (ebuf[strlen(ebuf)-1] == '\n')
                                ebuf[strlen(ebuf)-1] = '\0';
@@ -2296,7 +2253,7 @@ static int show_version(void)
 }
 
 static int show_cli_help(void) {
-       printf("Asterisk " ASTERISK_VERSION ", Copyright (C) 1999 - 2006, Digium, Inc. and others.\n");
+       printf("Asterisk " ASTERISK_VERSION ", Copyright (C) 1999 - 2007, Digium, Inc. and others.\n");
        printf("Usage: asterisk [OPTIONS]\n");
        printf("Valid Options:\n");
        printf("   -V              Display version number and exit\n");
@@ -2334,6 +2291,7 @@ static void ast_readconfig(void)
        struct ast_config *cfg;
        struct ast_variable *v;
        char *config = AST_CONFIG_FILE;
+       char hostname[MAXHOSTNAMELEN] = "";
 
        if (ast_opt_override_config) {
                cfg = ast_config_load(ast_config_AST_CONFIG_FILE);
@@ -2483,8 +2441,27 @@ static void ast_readconfig(void)
                        ast_copy_string(ast_config_AST_RUN_GROUP, v->value, sizeof(ast_config_AST_RUN_GROUP));
                } else if (!strcasecmp(v->name, "systemname")) {
                        ast_copy_string(ast_config_AST_SYSTEM_NAME, v->value, sizeof(ast_config_AST_SYSTEM_NAME));
+               } else if (!strcasecmp(v->name, "autosystemname")) {
+                       if (ast_true(v->value)) {
+                               if (!gethostname(hostname, sizeof(hostname) - 1))
+                                       ast_copy_string(ast_config_AST_SYSTEM_NAME, hostname, sizeof(ast_config_AST_SYSTEM_NAME));
+                               else {
+                                       if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)){
+                                               ast_copy_string(ast_config_AST_SYSTEM_NAME, "localhost", sizeof(ast_config_AST_SYSTEM_NAME));
+                                       }
+                                       ast_log(LOG_ERROR, "Cannot obtain hostname for this system.  Using '%s' instead.\n", ast_config_AST_SYSTEM_NAME);
+                               }
+                       }
                } else if (!strcasecmp(v->name, "languageprefix")) {
                        ast_language_is_prefix = ast_true(v->value);
+#if defined(HAVE_SYSINFO)
+               } else if (!strcasecmp(v->name, "minmemfree")) {
+                       /* specify the minimum amount of free memory to retain.  Asterisk should stop accepting new calls
+                        * if the amount of free memory falls below this watermark */
+                       if ((sscanf(v->value, "%ld", &option_minmemfree) != 1) || (option_minmemfree < 0)) {
+                               option_minmemfree = 0;
+                       }
+#endif
                }
        }
        ast_config_destroy(cfg);
@@ -2545,6 +2522,7 @@ int main(int argc, char *argv[])
        ast_alaw_init();
        callerid_init();
        ast_builtins_init();
+       ast_event_init();
        ast_utils_init();
        tdd_init();
        /* When Asterisk restarts after it has dropped the root privileges,
@@ -2555,8 +2533,15 @@ int main(int argc, char *argv[])
        if (getenv("HOME")) 
                snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
        /* Check for options */
-       while ((c = getopt(argc, argv, "mtThfFdvVqprRgciInx:U:G:C:L:M:")) != -1) {
+       while ((c = getopt(argc, argv, "mtThfFdvVqprRgciInx:U:G:C:L:M:e:")) != -1) {
                switch (c) {
+#if defined(HAVE_SYSINFO)
+               case 'e':
+                       if ((sscanf(&optarg[1], "%ld", &option_minmemfree) != 1) || (option_minmemfree < 0)) {
+                               option_minmemfree = 0;
+                       }
+                       break;
+#endif
 #if HAVE_WORKING_FORK
                case 'F':
                        ast_set_flag(&ast_options, AST_OPT_FLAG_ALWAYS_FORK);
@@ -2865,7 +2850,7 @@ int main(int argc, char *argv[])
 
        threadstorage_init();
 
-       if (load_modules(1)) {          /* Load modules */
+       if (load_modules(1)) {          /* Load modules, pre-load only */
                printf(term_quit());
                exit(1);
        }
@@ -2954,20 +2939,16 @@ int main(int argc, char *argv[])
        __ast_mm_init();
 #endif 
 
-       time(&ast_startuptime);
+       ast_startuptime = ast_tvnow();
        ast_cli_register_multiple(cli_asterisk, sizeof(cli_asterisk) / sizeof(struct ast_cli_entry));
 
        if (ast_opt_console) {
                /* Console stuff now... */
                /* Register our quit function */
                char title[256];
-               pthread_attr_t attr;
                pthread_t dont_care;
 
-               pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-               ast_pthread_create(&dont_care, &attr, monitor_sig_flags, NULL);
-               pthread_attr_destroy(&attr);
+               ast_pthread_create_detached(&dont_care, NULL, monitor_sig_flags, NULL);
 
                set_icon("Asterisk");
                snprintf(title, sizeof(title), "Asterisk Console on '%s' (pid %ld)", hostname, (long)ast_mainpid);
@@ -2975,6 +2956,10 @@ int main(int argc, char *argv[])
 
                for (;;) {
                        buf = (char *)el_gets(el, &num);
+
+                       if (!buf && write(1, "", 1) < 0)
+                               goto lostterm;
+
                        if (buf) {
                                if (buf[strlen(buf)-1] == '\n')
                                        buf[strlen(buf)-1] = '\0';
@@ -2997,5 +2982,6 @@ int main(int argc, char *argv[])
 
        monitor_sig_flags(NULL);
 
+lostterm:
        return 0;
 }