Fix various compiler warnings (bug #322)
[asterisk/asterisk.git] / res / res_musiconhold.c
index d99d8bd..5ca4f5f 100755 (executable)
@@ -27,6 +27,7 @@
 #include <errno.h>
 #include <unistd.h>
 #include <string.h>
+#include <signal.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/time.h>
@@ -39,7 +40,6 @@
 #endif
 #include <unistd.h>
 #include <sys/ioctl.h>
-#include <sys/wait.h>
 
 #include <pthread.h>
 
@@ -91,19 +91,12 @@ struct mohdata {
 
 static struct mohclass *mohclasses;
 
-static pthread_mutex_t moh_lock = AST_MUTEX_INITIALIZER;
+static ast_mutex_t moh_lock = AST_MUTEX_INITIALIZER;
 
+#define LOCAL_MPG_123 "/usr/local/bin/mpg123"
 #define MPG_123 "/usr/bin/mpg123"
 #define MAX_MP3S 256
 
-static void child_handler(int sig)
-{
-       int status;
-       if (wait4(-1,&status, WNOHANG, NULL)<1) 
-               if (option_debug)       
-                       ast_log(LOG_DEBUG, "Huh?  Child handler, but nobody there?\n");
-}
-
 static int spawn_mp3(struct mohclass *class)
 {
        int fds[2];
@@ -120,7 +113,7 @@ static int spawn_mp3(struct mohclass *class)
                ast_log(LOG_WARNING, "%s is not a valid directory\n", class->dir);
                return -1;
        }
-       argv[0] = MPG_123;
+       argv[0] = "mpg123";
        argv[1] = "-q";
        argv[2] = "-s";
        argv[3] = "--mono";
@@ -189,7 +182,12 @@ static int spawn_mp3(struct mohclass *class)
                        close(x);
                /* Child */
                chdir(class->dir);
+               /* Default install is /usr/local/bin */
+               execv(LOCAL_MPG_123, argv);
+               /* Many places have it in /usr/bin */
                execv(MPG_123, argv);
+               /* Check PATH as a last-ditch effort */
+               execvp("mpg123", argv);
                ast_log(LOG_WARNING, "Exec failed: %s\n", strerror(errno));
                exit(1);
        } else {
@@ -219,7 +217,6 @@ static void *monmp3thread(void *data)
        tv.tv_usec = 0;
        error_sec = 0;
        error_usec = 0;
-       signal(SIGCHLD, child_handler);
        for(;/* ever */;) {
                /* Spawn mp3 player if it's not there */
                if (class->srcfd < 0)  {
@@ -283,7 +280,7 @@ static void *monmp3thread(void *data)
                                ast_log(LOG_DEBUG, "Read %d bytes of audio while expecting %d\n", res2, res * 2);
                        continue;
                }
-               ast_pthread_mutex_lock(&moh_lock);
+               ast_mutex_lock(&moh_lock);
                moh = class->members;
                while(moh) {
                        /* Write data */
@@ -292,7 +289,7 @@ static void *monmp3thread(void *data)
                                    ast_log(LOG_DEBUG, "Only wrote %d of %d bytes to pipe\n", res, res2);
                        moh = moh->next;
                }
-               pthread_mutex_unlock(&moh_lock);
+               ast_mutex_unlock(&moh_lock);
        }
        return NULL;
 }
@@ -373,7 +370,7 @@ static void moh_release(struct ast_channel *chan, void *data)
 {
        struct mohdata *moh = data, *prev, *cur;
        int oldwfmt;
-       ast_pthread_mutex_lock(&moh_lock);
+       ast_mutex_lock(&moh_lock);
        /* Unlink */
        prev = NULL;
        cur = moh->parent->members;
@@ -388,14 +385,14 @@ static void moh_release(struct ast_channel *chan, void *data)
                prev = cur;
                cur = cur->next;
        }
-       ast_pthread_mutex_unlock(&moh_lock);
+       ast_mutex_unlock(&moh_lock);
        close(moh->pipe[0]);
        close(moh->pipe[1]);
        oldwfmt = moh->origwfmt;
        free(moh);
        if (chan) {
                if (oldwfmt && ast_set_write_format(chan, oldwfmt)) 
-                       ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %d\n", chan->name, oldwfmt);
+                       ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %s\n", chan->name, ast_getformatname(oldwfmt));
                if (option_verbose > 2)
                        ast_verbose(VERBOSE_PREFIX_3 "Stopped music on hold on %s\n", chan->name);
        }
@@ -405,7 +402,7 @@ static void *moh_alloc(struct ast_channel *chan, void *params)
 {
        struct mohdata *res;
        struct mohclass *class;
-       ast_pthread_mutex_lock(&moh_lock);
+       ast_mutex_lock(&moh_lock);
        class = get_mohbyname(params);
        if (class)
                res = mohalloc(class);
@@ -414,7 +411,7 @@ static void *moh_alloc(struct ast_channel *chan, void *params)
                        ast_log(LOG_WARNING, "No class: %s\n", (char *)params);
                res = NULL;
        }
-       ast_pthread_mutex_unlock(&moh_lock);
+       ast_mutex_unlock(&moh_lock);
        if (res) {
                res->origwfmt = chan->writeformat;
                if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
@@ -477,10 +474,12 @@ static struct ast_generator mohgen =
 static int moh_register(char *classname, char *mode, char *param, char *miscargs)
 {
        struct mohclass *moh;
+#ifdef ZAPATA_MOH
        int x;
-       ast_pthread_mutex_lock(&moh_lock);
+#endif
+       ast_mutex_lock(&moh_lock);
        moh = get_mohbyname(classname);
-       ast_pthread_mutex_unlock(&moh_lock);
+       ast_mutex_unlock(&moh_lock);
        if (moh) {
                ast_log(LOG_WARNING, "Music on Hold '%s' already exists\n", classname);
                return -1;
@@ -523,10 +522,10 @@ static int moh_register(char *classname, char *mode, char *param, char *miscargs
                free(moh);
                return -1;
        }
-       ast_pthread_mutex_lock(&moh_lock);
+       ast_mutex_lock(&moh_lock);
        moh->next = mohclasses;
        mohclasses = moh;
-       ast_pthread_mutex_unlock(&moh_lock);
+       ast_mutex_unlock(&moh_lock);
        return 0;
 }
 
@@ -574,18 +573,27 @@ static void load_moh_classes(void)
 static void ast_moh_destroy(void)
 {
        struct mohclass *moh;
+       char buff[8192];
+       int bytes, tbytes=0, stime = 0;
        if (option_verbose > 1)
                ast_verbose(VERBOSE_PREFIX_2 "Destroying any remaining musiconhold processes\n");
-       ast_pthread_mutex_lock(&moh_lock);
+       ast_mutex_lock(&moh_lock);
        moh = mohclasses;
        while(moh) {
                if (moh->pid) {
-                       kill(moh->pid, SIGKILL);
+                       ast_log(LOG_DEBUG, "killing %d!\n", moh->pid);
+                       stime = time(NULL);
+                       kill(moh->pid, SIGABRT);
+                       while ((bytes = read(moh->srcfd, buff, 8192)) && time(NULL) < stime + 5) {
+                               tbytes = tbytes + bytes;
+                       }
+                       ast_log(LOG_DEBUG, "mpg123 pid %d and child died after %d bytes read\n", moh->pid, tbytes);
+                       close(moh->srcfd);
                        moh->pid = 0;
                        }
                moh = moh->next;
        }
-       ast_pthread_mutex_unlock(&moh_lock);
+       ast_mutex_unlock(&moh_lock);
 }
 
 int load_module(void)