Merge BSD stack size work (bug #2067)
[asterisk/asterisk.git] / channels / chan_vofr.c
index 37d8a1f..eefde38 100755 (executable)
@@ -12,7 +12,6 @@
  */
 
 #include <stdio.h>
-#include <pthread.h>
 #include <string.h>
 #include <asterisk/lock.h>
 #include <asterisk/channel.h>
@@ -20,6 +19,7 @@
 #include <asterisk/config.h>
 #include <asterisk/logger.h>
 #include <asterisk/module.h>
+#include <asterisk/utils.h>
 #include <asterisk/pbx.h>
 #include <asterisk/options.h>
 #include <sys/socket.h>
@@ -53,18 +53,18 @@ static char context[AST_MAX_EXTENSION] = "default";
 static char language[MAX_LANGUAGE] = "";
 
 static int usecnt =0;
-static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
+AST_MUTEX_DEFINE_STATIC(usecnt_lock);
 
 /* Protect the interface list (of vofr_pvt's) */
-static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
+AST_MUTEX_DEFINE_STATIC(iflock);
 
 /* Protect the monitoring thread, so only one process can kill or start it, and not
    when it's doing something critical. */
-static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
+AST_MUTEX_DEFINE_STATIC(monlock);
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
-static pthread_t monitor_thread = 0;
+static pthread_t monitor_thread = AST_PTHREADT_NULL;
 
 static int restart_monitor(void);
 
@@ -134,20 +134,20 @@ static char *dtypestr(int control)
 
 static char *vflagsstr(int flags)
 {
-       static char buf[80];
+       static char buf[80] = "";
        buf[0] = '\0';
        if (!flags)
                return "(None)";
        if (flags & VOFR_ROUTE_LOCAL)
-               strcat(buf, "Local ");
+               strncat(buf, "Local ", sizeof(buf) - strlen(buf) - 1);
        if (flags & VOFR_ROUTE_VOICE)
-               strcat(buf, "Voice ");
+               strncat(buf, "Voice ", sizeof(buf) - strlen(buf) - 1);
        if (flags & VOFR_ROUTE_DTE)
-               strcat(buf, "DTE ");
+               strncat(buf, "DTE ", sizeof(buf) - strlen(buf) - 1);
        else if (flags & VOFR_ROUTE_DTE1)
-               strcat(buf, "DTE1 ");
+               strncat(buf, "DTE1 ", sizeof(buf) - strlen(buf) - 1);
        else if (flags & VOFR_ROUTE_DTE2)       
-               strcat(buf, "DTE2 ");
+               strncat(buf, "DTE2 ", sizeof(buf) - strlen(buf) - 1);
        return buf;
 }
 
@@ -588,6 +588,8 @@ retry:
        fr->src = type;
        fr->offset = 0;
        fr->mallocd=0;
+       fr->delivery.tv_sec = 0;
+       fr->delivery.tv_usec = 0;
        
        /* Now, what we do depends on what we read */
        switch(vh->dtype) {
@@ -997,7 +999,7 @@ static void *do_monitor(void *data)
 static int restart_monitor(void)
 {
        /* If we're supposed to be stopped -- stay stopped */
-       if (monitor_thread == -2)
+       if (monitor_thread == AST_PTHREADT_STOP)
                return 0;
        if (ast_mutex_lock(&monlock)) {
                ast_log(LOG_WARNING, "Unable to lock monitor\n");
@@ -1008,12 +1010,12 @@ static int restart_monitor(void)
                ast_log(LOG_WARNING, "Cannot kill myself\n");
                return -1;
        }
-       if (monitor_thread) {
+       if (monitor_thread != AST_PTHREADT_NULL) {
                /* Wake up the thread */
                pthread_kill(monitor_thread, SIGURG);
        } else {
                /* Start a new monitor */
-               if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
+               if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
                        ast_mutex_unlock(&monlock);
                        ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
                        return -1;
@@ -1136,61 +1138,7 @@ static struct ast_channel *vofr_request(char *type, int format, void *data)
        return tmp;
 }
 
-int load_module()
-{
-       struct ast_config *cfg;
-       struct ast_variable *v;
-       struct vofr_pvt *tmp;
-       cfg = ast_load(config);
-
-       /* We *must* have a config file otherwise stop immediately */
-       if (!cfg) {
-               ast_log(LOG_ERROR, "Unable to load config %s\n", config);
-               return -1;
-       }
-       if (ast_mutex_lock(&iflock)) {
-               /* It's a little silly to lock it, but we mind as well just to be sure */
-               ast_log(LOG_ERROR, "Unable to lock interface list???\n");
-               return -1;
-       }
-       v = ast_variable_browse(cfg, "interfaces");
-       while(v) {
-               /* Create the interface list */
-               if (!strcasecmp(v->name, "user") ||
-                       !strcasecmp(v->name, "network")) {
-                               tmp = mkif(v->name, v->value);
-                               if (tmp) {
-                                       tmp->next = iflist;
-                                       iflist = tmp;
-                               } else {
-                                       ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
-                                       ast_destroy(cfg);
-                                       ast_mutex_unlock(&iflock);
-                                       unload_module();
-                                       return -1;
-                               }
-               } else if (!strcasecmp(v->name, "context")) {
-                       strncpy(context, v->value, sizeof(context)-1);
-               } else if (!strcasecmp(v->name, "language")) {
-                       strncpy(language, v->value, sizeof(language)-1);
-               }
-               v = v->next;
-       }
-       ast_mutex_unlock(&iflock);
-       /* Make sure we can register our AdtranVoFR channel type */
-       if (ast_channel_register(type, tdesc, AST_FORMAT_G723_1, vofr_request)) {
-               ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
-               ast_destroy(cfg);
-               unload_module();
-               return -1;
-       }
-       ast_destroy(cfg);
-       /* And start the monitor for the first time */
-       restart_monitor();
-       return 0;
-}
-
-int unload_module()
+static int __unload_module(void)
 {
        struct vofr_pvt *p, *pl;
        /* First, take us out of the channel loop */
@@ -1215,7 +1163,7 @@ int unload_module()
                        pthread_kill(monitor_thread, SIGURG);
                        pthread_join(monitor_thread, NULL);
                }
-               monitor_thread = -2;
+               monitor_thread = AST_PTHREADT_STOP;
                ast_mutex_unlock(&monlock);
        } else {
                ast_log(LOG_WARNING, "Unable to lock the monitor\n");
@@ -1244,6 +1192,65 @@ int unload_module()
        return 0;
 }
 
+int unload_module()
+{
+       return __unload_module();
+}
+
+int load_module()
+{
+       struct ast_config *cfg;
+       struct ast_variable *v;
+       struct vofr_pvt *tmp;
+       cfg = ast_load(config);
+
+       /* We *must* have a config file otherwise stop immediately */
+       if (!cfg) {
+               ast_log(LOG_ERROR, "Unable to load config %s\n", config);
+               return -1;
+       }
+       if (ast_mutex_lock(&iflock)) {
+               /* It's a little silly to lock it, but we mind as well just to be sure */
+               ast_log(LOG_ERROR, "Unable to lock interface list???\n");
+               return -1;
+       }
+       v = ast_variable_browse(cfg, "interfaces");
+       while(v) {
+               /* Create the interface list */
+               if (!strcasecmp(v->name, "user") ||
+                       !strcasecmp(v->name, "network")) {
+                               tmp = mkif(v->name, v->value);
+                               if (tmp) {
+                                       tmp->next = iflist;
+                                       iflist = tmp;
+                               } else {
+                                       ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
+                                       ast_destroy(cfg);
+                                       ast_mutex_unlock(&iflock);
+                                       __unload_module();
+                                       return -1;
+                               }
+               } else if (!strcasecmp(v->name, "context")) {
+                       strncpy(context, v->value, sizeof(context)-1);
+               } else if (!strcasecmp(v->name, "language")) {
+                       strncpy(language, v->value, sizeof(language)-1);
+               }
+               v = v->next;
+       }
+       ast_mutex_unlock(&iflock);
+       /* Make sure we can register our AdtranVoFR channel type */
+       if (ast_channel_register(type, tdesc, AST_FORMAT_G723_1, vofr_request)) {
+               ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
+               ast_destroy(cfg);
+               __unload_module();
+               return -1;
+       }
+       ast_destroy(cfg);
+       /* And start the monitor for the first time */
+       restart_monitor();
+       return 0;
+}
+
 int usecount()
 {
        int res;