Merged revisions 29196 via svnmerge from
[asterisk/asterisk.git] / res / res_monitor.c
index 9c154ef..23a9a98 100644 (file)
@@ -50,22 +50,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 AST_MUTEX_DEFINE_STATIC(monitorlock);
 
-#define LOCK_IF_NEEDED(lock, needed)                                                           \
-       do {                                                                                                                    \
-               if (needed) {                                                                                           \
-                       if (ast_mutex_lock(lock)) {                                                             \
-                               ast_log(LOG_WARNING, "Unable to lock channel\n");       \
-                               return -1;                                                                                      \
-                       }                                                                                                               \
-               }                                                                                                                       \
+#define LOCK_IF_NEEDED(lock, needed) do { \
+       if (needed) \
+               ast_channel_lock(lock); \
        } while(0)
 
-#define UNLOCK_IF_NEEDED(lock, needed)                 \
-       do {                                                                            \
-               if (needed) {                                                   \
-                       ast_mutex_unlock(lock);                         \
-               }                                                                               \
-       } while(0)                                                                      \
+#define UNLOCK_IF_NEEDED(lock, needed) do { \
+       if (needed) \
+               ast_channel_unlock(lock); \
+       } while (0)
 
 static unsigned long seq = 0;
 
@@ -119,13 +112,13 @@ static char *unpausemonitor_descrip = "UnpauseMonitor\n"
 
 static int ast_monitor_set_state(struct ast_channel *chan, int state)
 {
-       LOCK_IF_NEEDED(&chan->lock, 1);
+       LOCK_IF_NEEDED(chan, 1);
        if (!chan->monitor) {
-               UNLOCK_IF_NEEDED(&chan->lock, 1);
+               UNLOCK_IF_NEEDED(chan, 1);
                return -1;
        }
        chan->monitor->state = state;
-       UNLOCK_IF_NEEDED(&chan->lock, 1);
+       UNLOCK_IF_NEEDED(chan, 1);
        return 0;
 }
 
@@ -136,7 +129,7 @@ int ast_monitor_start(      struct ast_channel *chan, const char *format_spec,
        int res = 0;
        char tmp[256];
 
-       LOCK_IF_NEEDED(&chan->lock, need_lock);
+       LOCK_IF_NEEDED(chan, need_lock);
 
        if (!(chan->monitor)) {
                struct ast_channel_monitor *monitor;
@@ -151,7 +144,7 @@ int ast_monitor_start(      struct ast_channel *chan, const char *format_spec,
                }
 
                if (!(monitor = ast_calloc(1, sizeof(*monitor)))) {
-                       UNLOCK_IF_NEEDED(&chan->lock, need_lock);
+                       UNLOCK_IF_NEEDED(chan, need_lock);
                        return -1;
                }
 
@@ -179,17 +172,13 @@ int ast_monitor_start(    struct ast_channel *chan, const char *format_spec,
                        seq++;
                        ast_mutex_unlock(&monitorlock);
 
-                       if((channel_name = ast_strdupa(chan->name))) {
-                               while((p = strchr(channel_name, '/'))) {
-                                       *p = '-';
-                               }
-                               snprintf(monitor->filename_base, FILENAME_MAX, "%s/%d-%s",
-                                                ast_config_AST_MONITOR_DIR, (int)time(NULL),channel_name);
-                               monitor->filename_changed = 1;
-                       } else {
-                               ast_log(LOG_ERROR,"Failed to allocate Memory\n");
-                               return -1;
+                       channel_name = ast_strdupa(chan->name);
+                       while ((p = strchr(channel_name, '/'))) {
+                               *p = '-';
                        }
+                       snprintf(monitor->filename_base, FILENAME_MAX, "%s/%d-%s",
+                                        ast_config_AST_MONITOR_DIR, (int)time(NULL), channel_name);
+                       monitor->filename_changed = 1;
                }
 
                monitor->stop = ast_monitor_stop;
@@ -211,7 +200,7 @@ int ast_monitor_start(      struct ast_channel *chan, const char *format_spec,
                        ast_log(LOG_WARNING, "Could not create file %s\n",
                                                monitor->read_filename);
                        free(monitor);
-                       ast_mutex_unlock(&chan->lock);
+                       ast_channel_unlock(chan);
                        return -1;
                }
                if (ast_fileexists(monitor->write_filename, NULL, NULL) > 0) {
@@ -224,7 +213,7 @@ int ast_monitor_start(      struct ast_channel *chan, const char *format_spec,
                                                monitor->write_filename);
                        ast_closestream(monitor->read_stream);
                        free(monitor);
-                       ast_mutex_unlock(&chan->lock);
+                       ast_channel_unlock(chan);
                        return -1;
                }
                chan->monitor = monitor;
@@ -237,7 +226,7 @@ int ast_monitor_start(      struct ast_channel *chan, const char *format_spec,
                res = -1;
        }
 
-       UNLOCK_IF_NEEDED(&chan->lock, need_lock);
+       UNLOCK_IF_NEEDED(chan, need_lock);
 
        return res;
 }
@@ -247,7 +236,7 @@ int ast_monitor_stop(struct ast_channel *chan, int need_lock)
 {
        int delfiles = 0;
 
-       LOCK_IF_NEEDED(&chan->lock, need_lock);
+       LOCK_IF_NEEDED(chan, need_lock);
 
        if (chan->monitor) {
                char filename[ FILENAME_MAX ];
@@ -316,7 +305,7 @@ int ast_monitor_stop(struct ast_channel *chan, int need_lock)
                chan->monitor = NULL;
        }
 
-       UNLOCK_IF_NEEDED(&chan->lock, need_lock);
+       UNLOCK_IF_NEEDED(chan, need_lock);
 
        return 0;
 }
@@ -353,7 +342,7 @@ int ast_monitor_change_fname(struct ast_channel *chan, const char *fname_base, i
                return -1;
        }
 
-       LOCK_IF_NEEDED(&chan->lock, need_lock);
+       LOCK_IF_NEEDED(chan, need_lock);
 
        if (chan->monitor) {
                int directory = strchr(fname_base, '/') ? 1 : 0;
@@ -370,7 +359,7 @@ int ast_monitor_change_fname(struct ast_channel *chan, const char *fname_base, i
                ast_log(LOG_WARNING, "Cannot change monitor filename of channel %s to %s, monitoring not started\n", chan->name, fname_base);
        }
 
-       UNLOCK_IF_NEEDED(&chan->lock, need_lock);
+       UNLOCK_IF_NEEDED(chan, need_lock);
 
        return 0;
 }
@@ -423,15 +412,13 @@ static int start_monitor_exec(struct ast_channel *chan, void *data)
                   the following could give NULL results, but we check just to
                   be pedantic. Reconstructing with checks for 'm' option does not
                   work if we end up adding more options than 'm' in the future. */
-               delay = ast_strdupa((char*)data);
-               if (delay) {
-                       options = strrchr(delay, '|');
-                       if (options) {
-                               arg = strchr(options, 'b');
-                               if (arg) {
-                                       *arg = 'X';
-                                       pbx_builtin_setvar_helper(chan,"AUTO_MONITOR",delay);
-                               }
+               delay = ast_strdupa(data);
+               options = strrchr(delay, '|');
+               if (options) {
+                       arg = strchr(options, 'b');
+                       if (arg) {
+                               *arg = 'X';
+                               pbx_builtin_setvar_helper(chan,"AUTO_MONITOR",delay);
                        }
                }
                return 0;
@@ -492,7 +479,7 @@ static int start_monitor_action(struct mansession *s, struct message *m)
                /* No filename base specified, default to channel name as per CLI */            
                if (!(fname = ast_strdup(c->name))) {
                        astman_send_error(s, m, "Could not start monitoring channel");
-                       ast_mutex_unlock(&c->lock);
+                       ast_channel_unlock(c);
                        return 0;
                }
                /* Channels have the format technology/channel_name - have to replace that /  */
@@ -503,7 +490,7 @@ static int start_monitor_action(struct mansession *s, struct message *m)
        if (ast_monitor_start(c, format, fname, 1)) {
                if (ast_monitor_change_fname(c, fname, 1)) {
                        astman_send_error(s, m, "Could not start monitoring channel");
-                       ast_mutex_unlock(&c->lock);
+                       ast_channel_unlock(c);
                        return 0;
                }
        }
@@ -512,7 +499,7 @@ static int start_monitor_action(struct mansession *s, struct message *m)
                ast_monitor_setjoinfiles(c, 1);
        }
 
-       ast_mutex_unlock(&c->lock);
+       ast_channel_unlock(c);
        astman_send_ack(s, m, "Started monitoring channel");
        return 0;
 }
@@ -537,7 +524,7 @@ static int stop_monitor_action(struct mansession *s, struct message *m)
                return 0;
        }
        res = ast_monitor_stop(c, 1);
-       ast_mutex_unlock(&c->lock);
+       ast_channel_unlock(c);
        if (res) {
                astman_send_error(s, m, "Could not stop monitoring channel");
                return 0;
@@ -574,11 +561,11 @@ static int change_monitor_action(struct mansession *s, struct message *m)
        }
        if (ast_monitor_change_fname(c, fname, 1)) {
                astman_send_error(s, m, "Could not change monitored filename of channel");
-               ast_mutex_unlock(&c->lock);
+               ast_channel_unlock(c);
                return 0;
        }
-       ast_mutex_unlock(&c->lock);
-       astman_send_ack(s, m, "Stopped monitoring channel");
+       ast_channel_unlock(c);
+       astman_send_ack(s, m, "Changed monitor filename");
        return 0;
 }
 
@@ -617,7 +604,7 @@ static int do_pause_or_unpause(struct mansession *s, struct message *m, int acti
        else
                ast_monitor_unpause(c);
        
-       ast_mutex_unlock(&c->lock);
+       ast_channel_unlock(c);
        astman_send_ack(s, m, "Paused monitoring of the channel");
        return 0;       
 }
@@ -645,7 +632,7 @@ static int unpause_monitor_action(struct mansession *s, struct message *m)
 }
        
 
-int load_module(void)
+static int load_module(void *mod)
 {
        ast_register_application("Monitor", start_monitor_exec, monitor_synopsis, monitor_descrip);
        ast_register_application("StopMonitor", stop_monitor_exec, stopmonitor_synopsis, stopmonitor_descrip);
@@ -661,7 +648,7 @@ int load_module(void)
        return 0;
 }
 
-int unload_module(void)
+static int unload_module(void *mod)
 {
        ast_unregister_application("Monitor");
        ast_unregister_application("StopMonitor");
@@ -677,25 +664,14 @@ int unload_module(void)
        return 0;
 }
 
-char *description(void)
+static const char *description(void)
 {
        return "Call Monitoring Resource";
 }
 
-int usecount(void)
-{
-       /* Never allow monitor to be unloaded because it will
-          unresolve needed symbols in the channel */
-#if 0
-       int res;
-       STANDARD_USECOUNT(res);
-       return res;
-#else
-       return 1;
-#endif
-}
-
-char *key()
+static const char *key(void)
 {
        return ASTERISK_GPL_KEY;
 }
+
+STD_MOD(MOD_0 | NO_USECOUNT | NO_UNLOAD, NULL, NULL, NULL);    /* MOD_0 because it exports some symbols */