Bug 794240: Disable timerthread + close sockets + cleanup tmp-files on shutdown + file permissions. r=jesup
authorJan-Ivar Bruaroey <jib@mozilla.com>
Fri, 14 Dec 2012 16:15:21 -0500
changeset 125541 5c073841902c03fc02f3ec80a033286db62ed078
parent 125529 ba17dfaa86e991c6e5a7ec1ec6d8a5c77031bcd4
child 125542 7c4c1bbf4d889f58bdb14ffd14c62bfef8c783c1
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjesup
bugs794240
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 794240: Disable timerthread + close sockets + cleanup tmp-files on shutdown + file permissions. r=jesup
media/webrtc/signaling/signaling.gyp
media/webrtc/signaling/src/sipcc/core/common/init.c
media/webrtc/signaling/src/sipcc/core/sipstack/sip_platform_task.c
media/webrtc/signaling/src/sipcc/cpr/darwin/cpr_darwin_init.c
media/webrtc/signaling/src/sipcc/cpr/darwin/cpr_darwin_socket.c
media/webrtc/signaling/src/sipcc/cpr/darwin/cpr_darwin_timers_using_select.c
media/webrtc/signaling/src/sipcc/cpr/linux/cpr_linux_init.c
media/webrtc/signaling/src/sipcc/cpr/linux/cpr_linux_socket.c
media/webrtc/signaling/src/sipcc/cpr/win32/cpr_win_socket.c
--- a/media/webrtc/signaling/signaling.gyp
+++ b/media/webrtc/signaling/signaling.gyp
@@ -577,16 +577,19 @@
 
       ],
 
       #
       # DEFINES
       #
       
       'defines' : [
+      # CPR timers are needed by SIP, but are disabled for now
+      # to avoid the extra timer thread and stale cleanup code
+      #    'CPR_TIMERS_ENABLED',
       ],
 
       'cflags_mozilla': [
         '$(NSPR_CFLAGS)',
       ],
 
       #
       # OS SPECIFIC
--- a/media/webrtc/signaling/src/sipcc/core/common/init.c
+++ b/media/webrtc/signaling/src/sipcc/core/common/init.c
@@ -211,17 +211,17 @@ ccInit ()
 
     platInit();
 
     strlib_init();
 
     /*
      * below should move to cprPreInit. keep it here until then
      */
-#ifdef _WIN32
+#if defined(_WIN32) && defined(CPR_TIMERS_ENABLED)
     cprTimerSystemInit();
 #endif
 
     /* Initialize threads, queues etc. */
     (void) thread_init();
 
     platform_initialized = TRUE;
 
--- a/media/webrtc/signaling/src/sipcc/core/sipstack/sip_platform_task.c
+++ b/media/webrtc/signaling/src/sipcc/core/sipstack/sip_platform_task.c
@@ -32,27 +32,26 @@
 /* The maximum number of messages parsed from the message queue at one time */
 #define MAX_SIP_MESSAGES 8
 
 /* The maximum number of connections allowed */
 #define MAX_SIP_CONNECTIONS (64 - 2)
 
 /* SIP Message queue waiting thread and the main thread IPC names */
 #ifdef __ANDROID__
-#define SIP_MSG_IPC_PATH  "/data/data/com.cisco.telephony.provider/"
+#define SIP_IPC_TEMP_PATH "/data/data/com.cisco.telephony.provider/SIP-%d"
 #else
-#define SIP_MSG_IPC_PATH  "/tmp/"
+#define SIP_IPC_TEMP_PATH "/tmp/SIP-%d"
 #endif
-#define SIP_MSG_SERV_NAME "SIP-Main-%d"
-#define SIP_MSG_CLNT_NAME "SIP-MsgQ-%d"
+#define SIP_MSG_SERV_NAME "Main"
+#define SIP_MSG_CLNT_NAME "MsgQ"
 
 #define SIP_PAUSE_WAIT_IPC_LISTEN_READY_TIME   50  /* 50ms. */
 #define SIP_MAX_WAIT_FOR_IPC_LISTEN_READY    1200  /* 50 * 1200 = 1 minutes */
 
-
 /*---------------------------------------------------------
  *
  * Local Variables
  *
  */
 fd_set read_fds;
 fd_set write_fds;
 static cpr_socket_t listen_socket = INVALID_SOCKET;
@@ -69,19 +68,16 @@ sip_connection_t sip_conn;
 typedef struct sip_int_msg_t_ {
     void            *msg;
     phn_syshdr_t    *syshdr;
 } sip_int_msg_t;
 
 /* Internal message queue (array) */
 static sip_int_msg_t sip_int_msgq_buf[MAX_SIP_MESSAGES] = {{0,0},{0,0}};
 
-/* Main thread and message queue waiting thread IPC names */
-static const char *sip_IPC_serv_name = SIP_MSG_IPC_PATH SIP_MSG_SERV_NAME;
-static const char *sip_IPC_clnt_name = SIP_MSG_IPC_PATH SIP_MSG_CLNT_NAME;
 static cpr_sockaddr_un_t sip_serv_sock_addr;
 static cpr_sockaddr_un_t sip_clnt_sock_addr;
 
 
 /*---------------------------------------------------------
  *
  * Global Variables
  *
@@ -158,17 +154,17 @@ static cpr_socket_t sip_create_IPC_sock 
                           " errno=%d\n", fname, cpr_errno);
         return (INVALID_SOCKET);
     }
 
     /* Bind to the local socket */
     cpr_set_sockun_addr(&addr, name, getpid());
 
     /* make sure file doesn't already exist */
-    unlink( (char *)addr.sun_path);
+    unlink(addr.sun_path);
 
     /* do the bind */
     if (cprBind(sock, (cpr_sockaddr_t *)&addr,
                 cpr_sun_len(addr)) == CPR_FAILURE) {
         (void) sipSocketClose(sock, FALSE);
         CCSIP_DEBUG_ERROR(SIP_F_PREFIX"cprBind() failed"
                           " errno=%d\n", fname, cpr_errno);
         return (INVALID_SOCKET);
@@ -235,30 +231,26 @@ void sip_platform_task_msgqwait (void *a
                               " socket ready, exiting\n", fname);
             return;
         }
     }
 
     /*
      * Adjust relative priority of SIP thread.
      */
-#ifndef WIN32
     (void) cprAdjustRelativeThreadPriority(SIP_THREAD_RELATIVE_PRIORITY);
-#else
-    /* Use default priority */
-    (void) cprAdjustRelativeThreadPriority(0);
-#endif
 
     /*
      * The main thread is ready. set global client socket address
      * so that the server can send back response.
      */
-    cpr_set_sockun_addr(&sip_clnt_sock_addr, sip_IPC_clnt_name, getpid());
+    cpr_set_sockun_addr(&sip_clnt_sock_addr,
+                        SIP_IPC_TEMP_PATH "/" SIP_MSG_CLNT_NAME, getpid());
 
-    sip_ipc_clnt_socket = sip_create_IPC_sock(sip_IPC_clnt_name);
+    sip_ipc_clnt_socket = sip_create_IPC_sock(sip_clnt_sock_addr.sun_path);
 
     if (sip_ipc_clnt_socket == INVALID_SOCKET) {
         CCSIP_DEBUG_ERROR(SIP_F_PREFIX"sip_create_IPC_sock() failed,"
                           "  exiting\n", fname);
         return;
     }
 
     while (quit_thread == FALSE) {
@@ -318,16 +310,18 @@ void sip_platform_task_msgqwait (void *a
             			sizeof(response), 0, NULL, NULL) < 0) {
             		CCSIP_DEBUG_ERROR(SIP_F_PREFIX"read IPC failed:"
             				" errno=%d\n", fname, cpr_errno);
             	}
             	num_messages = 0;
             }
         }
     }
+    cprCloseSocket(sip_ipc_clnt_socket);
+    unlink(sip_clnt_sock_addr.sun_path); // removes tmp file
 }
 
 /**
  *  sip_process_int_msg - process internal IPC message from the
  *  the message queue waiting thread.
  *
  *  @param - none.
  *
@@ -365,16 +359,31 @@ static void sip_process_int_msg (void)
     }
 
     /* process messages */
     int_msg = &sip_int_msgq_buf[0];
     while (num_messages) {
         msg    = int_msg->msg;
         syshdr = int_msg->syshdr;
         if (msg != NULL && syshdr != NULL) {
+            if (syshdr->Cmd == THREAD_UNLOAD) {
+                /*
+                 * Cleanup here, as SIPTaskProcessListEvent wont return.
+                 * - Remove last tmp file and tmp dir.
+                 */
+                cprCloseSocket(sip_ipc_serv_socket);
+                unlink(sip_serv_sock_addr.sun_path);
+
+                char stmpdir[sizeof(sip_serv_sock_addr.sun_path)];
+                PR_snprintf(stmpdir, sizeof(stmpdir), SIP_IPC_TEMP_PATH, getpid());
+                if (rmdir(stmpdir) != 0) {
+                    CCSIP_DEBUG_ERROR(SIP_F_PREFIX"failed to remove temp dir\n",
+                                      fname);
+                }
+            }
             SIPTaskProcessListEvent(syshdr->Cmd, msg, syshdr->Usr.UsrPtr,
                 syshdr->Len);
             cprReleaseSysHeader(syshdr);
 
             int_msg->msg    = NULL;
             int_msg->syshdr = NULL;
         }
 
@@ -430,33 +439,38 @@ sip_platform_task_loop (void *arg)
     if (platThreadInit("SIPStack Task") != 0) {
         CCSIP_DEBUG_ERROR(SIP_F_PREFIX"failed to attach thread to JVM\n", fname);
         return;
     }
 
     /*
      * Adjust relative priority of SIP thread.
      */
-#ifndef WIN32
     (void) cprAdjustRelativeThreadPriority(SIP_THREAD_RELATIVE_PRIORITY);
-#else
-    /* Use default priority */
-    (void) cprAdjustRelativeThreadPriority(0);
-#endif
 
     /*
      * Setup IPC socket addresses for main thread (server)
      */
-    cpr_set_sockun_addr(&sip_serv_sock_addr, sip_IPC_serv_name, getpid());
+    {
+      char stmpdir[sizeof(sip_serv_sock_addr.sun_path)];
+      PR_snprintf(stmpdir, sizeof(stmpdir), SIP_IPC_TEMP_PATH, getpid());
+
+      if (mkdir(stmpdir, 0700) != 0) {
+          CCSIP_DEBUG_ERROR(SIP_F_PREFIX"failed to create temp dir\n", fname);
+          return;
+      }
+    }
+    cpr_set_sockun_addr(&sip_serv_sock_addr,
+                        SIP_IPC_TEMP_PATH "/" SIP_MSG_SERV_NAME, getpid());
 
     /*
      * Create IPC between the message queue thread and this main
      * thread.
      */
-    sip_ipc_serv_socket = sip_create_IPC_sock(sip_IPC_serv_name);
+    sip_ipc_serv_socket = sip_create_IPC_sock(sip_serv_sock_addr.sun_path);
 
     if (sip_ipc_serv_socket == INVALID_SOCKET) {
         CCSIP_DEBUG_ERROR(SIP_F_PREFIX"sip_create_IPC_sock() failed:"
                           " errno=%d\n", fname, cpr_errno);
         return;
     }
 
     /*
@@ -475,16 +489,17 @@ sip_platform_task_loop (void *arg)
     /*
      * Let the message queue waiting thread know that the main
      * thread is ready.
      */
     main_thread_ready = TRUE;
 
     /*
      * Main Event Loop
+     * - Forever-loop exits in sip_process_int_msg()::THREAD_UNLOAD
      */
     while (TRUE) {
         /*
          * Wait on events or timeout
          */
         sip_read_fds = read_fds;
 
         // start off by init to zero
--- a/media/webrtc/signaling/src/sipcc/cpr/darwin/cpr_darwin_init.c
+++ b/media/webrtc/signaling/src/sipcc/cpr/darwin/cpr_darwin_init.c
@@ -166,24 +166,23 @@ cprPreInit (void)
     /*
      * Create message queue list mutex
      */
     returnCode = pthread_mutex_init(&msgQueueListMutex, NULL);
     if (returnCode != 0) {
         CPR_ERROR("%s: MsgQueue Mutex init failure %d\n", fname, returnCode);
         return CPR_FAILURE;
     }
-
+#ifdef CPR_TIMERS_ENABLED
     returnCode = cpr_timer_pre_init();
     if (returnCode != 0) {
         CPR_ERROR("%s: timer pre init failed %d\n", fname, returnCode);
         return CPR_FAILURE;
     }
-
-
+#endif
     return CPR_SUCCESS;
 }
 
 
 /**
  * cprPostInit
  *
  * @brief The cprPostInit function IS called from pSIPCC @b after all the components are initialized.
--- a/media/webrtc/signaling/src/sipcc/cpr/darwin/cpr_darwin_socket.c
+++ b/media/webrtc/signaling/src/sipcc/cpr/darwin/cpr_darwin_socket.c
@@ -760,26 +760,26 @@ cpr_inet_pton (int af, const char *src, 
 }
 
 
 /**
  *  Utility function that sets up the socket address
  *
  *  @param[in] addr - socket fd to bind with the IPC address.
  *  @param[in] name - pointer to the name of socket to bind to.
- *
+ *  @param[in] pid  - process id (only used if name contains %d)
  *
  *  @pre  (name != NULL)
  */
 void cpr_set_sockun_addr (cpr_sockaddr_un_t *addr, const char *name, pid_t pid)
 {
     /* Bind to the local socket */
     memset(addr, 0, sizeof(cpr_sockaddr_un_t));
     addr->sun_family = AF_UNIX;
-    snprintf((char *) addr->sun_path, sizeof(addr->sun_path), "%s_%d", name, pid);
+    snprintf(addr->sun_path, sizeof(addr->sun_path), name, pid);
 }
 
 /* int
  * inet_pton4(src, dst, pton)
  *	when last arg is 0: inet_aton(). with hexadecimal, octal and shorthand.
  *	when last arg is 1: inet_pton(). decimal dotted-quad only.
  * return:
  *	1 if `src' is a valid input, else 0.
--- a/media/webrtc/signaling/src/sipcc/cpr/darwin/cpr_darwin_timers_using_select.c
+++ b/media/webrtc/signaling/src/sipcc/cpr/darwin/cpr_darwin_timers_using_select.c
@@ -47,16 +47,17 @@
  * @brief The module related to Timer abstraction for the pSIPCC
  * @addtogroup TimerAPIs The Timer APIs
  * @ingroup Timers
  * @brief APIs expected by pSIPCC for using Timers
  *
  */
 
 #include "cpr.h"
+#include "cpr_assert.h"
 #include "cpr_socket.h"
 #include "cpr_stdlib.h"
 #include "cpr_stdio.h"
 #include "cpr_threads.h"
 #include "cpr_timers.h"
 #include "cpr_string.h"
 #include "phntask.h"
 #include <errno.h>
@@ -221,24 +222,20 @@ cprSleep (uint32_t duration)
  * @param[in] cprTimerPtr  - timer pointer
  * @param[in] duration     - timer duration in msec.
  * @param[in] data         - opaque data
  * @return  - CPR_SUCCESS or CPR_FAILURE
  */
 static cprRC_t addTimerToList (cpr_timer_t *cprTimerPtr, uint32_t duration, void *data)
 {
     // TODO(ekr@rtfm.com): Put this back in when you figure out why it causes crashes
-    return CPR_SUCCESS;
-
-#if 0
-    static const char fname[] = "addTimerToList";
+#ifdef CPR_TIMERS_ENABLED
     timer_ipc_t tmr_cmd = {0};
     timer_ipc_t tmr_rsp={0};
 
-
     API_ENTER();
 
     CPR_INFO("%s: cprTimerptr=0x%x dur=%d user_data=%p\n",
              fname, cprTimerPtr, duration, data);
     tmr_cmd.msg_type = TMR_CMD_ADD;
     tmr_cmd.u.cmd.timer_ptr = cprTimerPtr;
     tmr_cmd.u.cmd.user_data_ptr = data;
     tmr_cmd.u.cmd.duration = duration;
@@ -249,33 +246,37 @@ static cprRC_t addTimerToList (cpr_timer
         if (sendto(client_sock, &tmr_cmd, sizeof(timer_ipc_t), 0,
                    (struct sockaddr *)&tmr_serv_addr, sizeof(tmr_serv_addr)) < 0) {
             CPR_ERROR("Failed to tx IPC msg to timer service, errno = %s %s\n",
                    strerror(errno), fname);
             API_RETURN(CPR_FAILURE);
         }
 
     } else {
-        CPR_ERROR("can not make IPC connection, client_sock is invalid %s\n", fname);
+        CPR_ERROR("can not make IPC connection, client_sock is invalid %s\n", __FUNCTION__);
         API_RETURN(CPR_FAILURE);
     }
 
     /*
      * wait for the timer service to excute the request
      * so that we get result of operation
      */
 
     if (recvfrom(client_sock, &tmr_rsp, sizeof(timer_ipc_t),0, NULL, NULL) < 0) {
         //CPR_INFO("error in recving the result error=%s\n", strerror(errno));
         API_RETURN(CPR_FAILURE);
     } else {
         //CPR_INFO("received response from the timer result=%d\n", tmr_rsp.u.result);
         API_RETURN(tmr_rsp.u.result);
     }
+#else
+    cprAssert(FALSE, CPR_FAILURE);
+    CPR_ERROR("CPR Timers are disabled! %s\n", __FUNCTION__);
 #endif
+    return CPR_SUCCESS;
 }
 
 
 /**
  * addTimer
  *
  * Add a timer to the timer linked list.
  * This function is only called by CPR functions and is not visible to external
--- a/media/webrtc/signaling/src/sipcc/cpr/linux/cpr_linux_init.c
+++ b/media/webrtc/signaling/src/sipcc/cpr/linux/cpr_linux_init.c
@@ -170,24 +170,23 @@ cprPreInit (void)
     /*
      * Create message queue list mutex
      */
     returnCode = pthread_mutex_init(&msgQueueListMutex, NULL);
     if (returnCode != 0) {
         CPR_ERROR("%s: MsgQueue Mutex init failure %d\n", fname, returnCode);
         return CPR_FAILURE;
     }
-
+#if CPR_TIMERS_ENABLED
     returnCode = cpr_timer_pre_init();
     if (returnCode != 0) {
         CPR_ERROR("%s: timer pre init failed %d\n", fname, returnCode);
         return CPR_FAILURE;
     }
-
-
+#endif
     return CPR_SUCCESS;
 }
 
 
 /**
  * cprPostInit
  *
  * @brief The cprPostInit function IS called from pSIPCC @b after all the components are initialized.
--- a/media/webrtc/signaling/src/sipcc/cpr/linux/cpr_linux_socket.c
+++ b/media/webrtc/signaling/src/sipcc/cpr/linux/cpr_linux_socket.c
@@ -735,26 +735,26 @@ cpr_inet_pton (int af, const char *src, 
 
 
 /**
  *  Utility function that sets up the socket address, using
  *  the name and the pid to guarantee uniqueness
  *
  *  @param[in] addr - socket fd to bind with the IPC address.
  *  @param[in] name - pointer to the name of socket to bind to.
- *
+ *  @param[in] pid  - process id (only used if name contains %d)
  *
  *  @pre  (name != NULL)
  */
 void cpr_set_sockun_addr (cpr_sockaddr_un_t *addr, const char *name, pid_t pid)
 {
     /* Bind to the local socket */
     memset(addr, 0, sizeof(cpr_sockaddr_un_t));
     addr->sun_family = AF_LOCAL;
-    snprintf((char *) addr->sun_path, sizeof(addr->sun_path), "%s_%d", name, pid);
+    snprintf(addr->sun_path, sizeof(addr->sun_path), name, pid);
 }
 
 /* int
  * inet_pton4(src, dst, pton)
  *	when last arg is 0: inet_aton(). with hexadecimal, octal and shorthand.
  *	when last arg is 1: inet_pton(). decimal dotted-quad only.
  * return:
  *	1 if `src' is a valid input, else 0.
--- a/media/webrtc/signaling/src/sipcc/cpr/win32/cpr_win_socket.c
+++ b/media/webrtc/signaling/src/sipcc/cpr/win32/cpr_win_socket.c
@@ -1079,10 +1079,10 @@ cpr_inet_pton (int af, const char *src, 
 }
 
 void cpr_set_sockun_addr(cpr_sockaddr_un_t *addr, const char *name, pid_t pid)
 {
   /* COPIED FROM OTHER PLATFORM AS A PLACE HOLDER */
   /* Bind to the local socket */
   memset(addr, 0, sizeof(cpr_sockaddr_un_t));
   addr->sun_family = AF_INET;
-  snprintf((char *) addr->sun_path, sizeof(addr->sun_path), "%s_%d", name, pid);
+  snprintf(addr->sun_path, sizeof(addr->sun_path), name, pid);
 }