You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2010/01/05 08:28:18 UTC

svn commit: r895925 - in /commons/sandbox/runtime/trunk/src/main/native: include/ include/arch/windows/ os/unix/ os/win32/ shared/ test/

Author: mturk
Date: Tue Jan  5 07:28:17 2010
New Revision: 895925

URL: http://svn.apache.org/viewvc?rev=895925&view=rev
Log:
Add flags to the ACR_Signal

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_exec.h
    commons/sandbox/runtime/trunk/src/main/native/include/acr_signals.h
    commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/exec.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/signals.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/signals.c
    commons/sandbox/runtime/trunk/src/main/native/shared/sigaction.c
    commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_exec.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_exec.h?rev=895925&r1=895924&r2=895925&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_exec.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_exec.h Tue Jan  5 07:28:17 2010
@@ -49,6 +49,8 @@
     int                 exitval;
     /** Process exit reason  */
     int                 exitwhy;
+    /** Process directory */
+    acr_pchar_t        *currdir;
 } acr_exec_t;
 
 #define ACR_PROC_HAS_STDIN      0x0001
@@ -114,15 +116,16 @@
  * @return EINVAL if the executable object was created without
  *         ACR_PROC_HAS_STDIN flag.
  */
-ACR_DECLARE(int) ACR_ExecStdinSet(acr_exec_t *e,
+ACR_DECLARE(int) ACR_ExecStdinSet(acr_exec_t *exe,
                                    const void *data, size_t len);
 
 /**
- * Set execution timeout.
+ * Set process current working directory.
  * @param exe The executable object
- * @param timeout Timeout value.
+ * @param path Path to the existing working directory of the new process.
  */
-ACR_DECLARE(void) ACR_ExecTimeoutSet(acr_exec_t *exe, acr_time_t timeout);
+ACR_DECLARE(int) ACR_ExecDirSet(acr_exec_t *exe,
+                                const acr_pchar_t *path);
 
 /**
  * Free allocated executable object resources.

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_signals.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_signals.h?rev=895925&r1=895924&r2=895925&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_signals.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_signals.h Tue Jan  5 07:28:17 2010
@@ -63,20 +63,33 @@
  * Set the signal handler function for a given signal
  * @param signo The signal (eg... SIGHUP)
  * @param function The function to get called on signal.
+ * @param flags Signal handler flags.
  * @return previous signal handler function
  */
-ACR_DECLARE(acr_sigfunc_t *) ACR_Signal(int signo, acr_sigfunc_t *func);
+ACR_DECLARE(acr_sigfunc_t *) ACR_Signal(int signo, acr_sigfunc_t *func,
+                                        int flags);
+
+/**
+ * Set default signal handler function for a given signal
+ * @param signo The signal (eg... SIGHUP)
+ * @param flags Signal handler flags.
+ * @return previous signal handler function
+ * @remark Function sets the os default signal handler forcing SIG_DFL
+ *       for a given signal bypassing the original signal handler process
+ *       might have setup before signal initialization took place.
+ */
+ACR_DECLARE(acr_sigfunc_t *) ACR_SIG_DFL(int signo, int flags);
 
 /**
  * Block the signal
  * @param signum signal to block.
- */ 
+ */
 ACR_DECLARE(int) ACR_SignalBlock(int signum);
 
 /**
  * Unblock the signal
  * @param signum signal to unblock.
- */ 
+ */
 ACR_DECLARE(int) ACR_SignalUnblock(int signum);
 
 #ifdef __cplusplus

Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h?rev=895925&r1=895924&r2=895925&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h Tue Jan  5 07:28:17 2010
@@ -249,6 +249,13 @@
 };
 
 
+typedef  int        rlim_t;
+
+struct rlimit {
+    rlim_t rlim_cur;  /* Soft limit */
+    rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */
+};
+
 #if defined(ACR_ENABLE_TEST)
 ACR_DECLARE(pid_t) getppid(void);
 #else

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/exec.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/exec.c?rev=895925&r1=895924&r2=895925&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/exec.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/exec.c Tue Jan  5 07:28:17 2010
@@ -112,6 +112,12 @@
     int pipes[8] = { -1, -1, -1, -1,  -1, -1, -1, -1 };
     _info_c info = { -1,  0,  0,  0 };
 
+    /* By default process terminates when writting to a
+     * pipe with no readers.
+     * Ignore SIGPIPE
+     */
+    ACR_Signal(SIGPIPE, SIG_IGN, SA_RESTART);
+
     /* Create pipes */
     if (ep->flags & ACR_PROC_HAS_STDIN && ep->data.iov_len) {
         if ((rc = pipepair(&pipes[PIPE_STDINP], ACR_PIPE_READ_BLOCK)))
@@ -125,6 +131,9 @@
         if ((rc = pipepair(&pipes[PIPE_STDERR], ACR_PIPE_WRITE_BLOCK)))
             goto cleanup;
     }
+    /* Create signaling pipe that is used both for reporting the
+     * failed execv and syncing with execv.
+     */
     if ((rc = pipepair(&pipes[PIPE_SIGNAL], ACR_PIPE_FULL_BLOCK)))
         goto cleanup;
 
@@ -152,10 +161,19 @@
         /* Make sure the signaling pipe is closed on exec.
          * This forces parent to wait until actual
          * exec is performed or until the error is
-         * written to the signal pipe
+         * written to the signal pipe.
          */
         acr_cloexec(pipes[PIPE_SIGNAL_WRS]);
 
+        if (ep->currdir && chdir(ep->currdir) == -1) {
+            /* Failed changing the current directoty */
+            rc = ACR_GET_OS_ERROR();
+            goto child_cleanup;
+        }
+        /* Set the real os SIGCHLD child handler.
+         */
+        ACR_SIG_DFL(SIGCHLD, SA_RESTART);
+
         close(STDIN_FILENO);
         if (pipes[PIPE_STDINP_RDS] == -1) {
             /* Redirect stdin to /dev/null
@@ -237,7 +255,7 @@
         /* Handshake with the child process.
          * If the read is sucessful it means that
          * either init or execv inside child failed.
-         */        
+         */
         rd = r_read(pipes[PIPE_SIGNAL_RDS], &info, sizeof(info));
         i_close(&pipes[PIPE_SIGNAL_RDS]);
         if (rd == sizeof(info)) {
@@ -246,6 +264,10 @@
              */
             running = 0;
         }
+        else {
+            /* Guard againts partial reads */
+            info.err = 0;
+        }
         while (running) {
             npipes = 0;
             if (pipes[PIPE_STDINP_WRS] != -1) {
@@ -390,6 +412,7 @@
             break;
         }
     }
+    ACR_Signal(SIGPIPE, SIG_DFL, 0);
 
     return ep->exitwhy;
 cleanup:
@@ -397,6 +420,7 @@
     ep->exitval = rc;
     for (i = 0; i < 8; i++)
         s_close(pipes[i]);
+    ACR_Signal(SIGPIPE, SIG_DFL, 0);
     return ep->exitwhy;
 }
 
@@ -451,16 +475,24 @@
     return ACR_EINVAL;
 }
 
-ACR_DECLARE(void) ACR_ExecTimeoutSet(acr_exec_t *e, acr_time_t timeout)
+ACR_DECLARE(int) ACR_ExecDirSet(acr_exec_t *e,
+                                const char *path)
 {
-    if (e)
-        e->timeout = timeout;
+    if (e) {
+        x_free(e->currdir);
+        if (path) {
+            if (!(e->currdir = x_strdup(path)))
+                return ACR_ENOMEM;
+        }
+        return 0;
+    }
+    return ACR_EINVAL;
 }
 
 ACR_DECLARE(void) ACR_ExecFree(acr_exec_t *e)
 {
     if (e) {
-
+        x_free(e->currdir);
         acr_sbuf_delete(&e->sout);
         acr_sbuf_delete(&e->serr);
 

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/signals.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/signals.c?rev=895925&r1=895924&r2=895925&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/signals.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/signals.c Tue Jan  5 07:28:17 2010
@@ -154,16 +154,18 @@
     }
     for (i = 0; i < ACR_NUMSIG; i++) {
         struct sigaction act;
-        act.sa_handler = SIG_DFL;
+
+        memset(&act, 0, sizeof(sigaction));
         sigemptyset(&act.sa_mask);
-        act.sa_flags = 0;
+        act.sa_handler = SIG_DFL;
+        act.sa_flags   = 0;
 #ifdef SA_INTERRUPT             /* SunOS */
         act.sa_flags |= SA_INTERRUPT;
 #endif
         if (sigaction(i, &act, _signalset[i]) == 0) {
             /* Restore the original saved sigaction.
-             * ###: Perhaps there is a smarter for getting the
-             * original signal handlers.
+             * ###: Is there a smarter way for getting the
+             * original signal handlers?
              */
             sigaction(i, _signalset[i], &act);
         }
@@ -180,9 +182,11 @@
 
         if (pthread_key_create(&seh_ctxt_key, NULL))
             return ACR_GET_OS_ERROR();
-        act.sa_handler = seh_sig_handler;
+
+        memset(&act, 0, sizeof(sigaction));
         sigemptyset(&act.sa_mask);
-        act.sa_flags = 0;
+        act.sa_handler = seh_sig_handler;
+        act.sa_flags   = 0;
 #ifdef SA_INTERRUPT
         act.sa_flags |= SA_INTERRUPT;
 #endif
@@ -236,21 +240,32 @@
 }
 #endif /* DARWIN */
 
-ACR_DECLARE(acr_sigfunc_t *) ACR_Signal(int signo, acr_sigfunc_t *func)
+ACR_DECLARE(acr_sigfunc_t *) ACR_Signal(int signo, acr_sigfunc_t *func,
+                                        int flags)
 {
-    int rc;
+    int rc = -1;
     struct sigaction act, oact;
 
-    if (signo < 1 || signo > ACR_NUMSIG) {
+    if (signo < 1 || signo > ACR_NUMSIG || func == SIG_ERR) {
         errno = EINVAL;
         return SIG_ERR;
     }
-    act.sa_handler = func;
+    memset(&act, 0, sizeof(sigaction));
+
     sigemptyset(&act.sa_mask);
-    act.sa_flags = 0;
+    act.sa_handler = func;
+    act.sa_flags   = flags;
 #ifdef SA_INTERRUPT             /* SunOS */
     act.sa_flags |= SA_INTERRUPT;
 #endif
+    if ((signo == SIGCHLD) && (func == SIG_IGN)) {
+#ifdef SA_NOCLDSTOP
+        act.sa_flags |= SA_NOCLDSTOP;
+#endif
+#ifdef SA_NOCLDWAIT
+        act.sa_flags |= SA_NOCLDWAIT;
+#endif
+    }
 #if defined(__NetBSD__) || defined(DARWIN)
     /* ignoring SIGCHLD or leaving the default disposition doesn't avoid zombies,
      * and there is no SA_NOCLDWAIT flag, so catch the signal and reap status in
@@ -264,13 +279,38 @@
         if (_signalset[signo]->sa_handler != SIG_ERR)
             rc = sigaction(signo, _signalset[signo], &oact);
         else
-            rc = EACCES;
+            errno = EACCES;
     }
     else
         rc = sigaction(signo, &act, &oact);
     if (rc < 0)
         return SIG_ERR;
-    return oact.sa_handler;
+    else
+        return oact.sa_handler;
+}
+
+ACR_DECLARE(acr_sigfunc_t *) ACR_SIG_DFL(int signo, int flags)
+{
+    int rc;
+    struct sigaction act, oact;
+
+    if (signo < 1 || signo > ACR_NUMSIG) {
+        errno = EINVAL;
+        return SIG_ERR;
+    }
+    memset(&act, 0, sizeof(sigaction));
+
+    sigemptyset(&act.sa_mask);
+    act.sa_handler = SIG_DFL;
+    act.sa_flags   = flags;
+#ifdef SA_INTERRUPT             /* SunOS */
+    act.sa_flags |= SA_INTERRUPT;
+#endif
+    rc = sigaction(signo, &act, &oact);
+    if (rc < 0)
+        return SIG_ERR;
+    else
+        return oact.sa_handler;
 }
 
 ACR_DECLARE(int) ACR_SignalBlock(int signum)

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c?rev=895925&r1=895924&r2=895925&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c Tue Jan  5 07:28:17 2010
@@ -217,7 +217,7 @@
 #ifdef O_NONBLOCK
     /* Use non-blocking I/O
      */
-    int mode, forg;
+    long mode, forg;
     if ((mode = fcntl(fd, F_GETFL, 0)) == -1) {
         return ACR_GET_OS_ERROR();
     }
@@ -246,7 +246,7 @@
 #ifdef FD_CLOEXEC
     /* Close on exec
      */
-    int mode, forg;
+    long mode, forg;
     if ((mode = fcntl(fd, F_GETFD, 0)) == -1) {
         return ACR_GET_OS_ERROR();
     }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/signals.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/signals.c?rev=895925&r1=895924&r2=895925&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/signals.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/signals.c Tue Jan  5 07:28:17 2010
@@ -817,11 +817,12 @@
     }
 }
 
-ACR_DECLARE(acr_sigfunc_t *) ACR_Signal(int signo, acr_sigfunc_t *func)
+ACR_DECLARE(acr_sigfunc_t *) ACR_Signal(int signo, acr_sigfunc_t *func,
+                                        int flags)
 {
     acr_sigfunc_t *orghandler;
 
-    if (signo <= 0 || signo >= _NSIG) {
+    if (signo <= 0 || signo >= _NSIG || func == SIG_ERR) {
         SetLastError(ERROR_INVALID_PARAMETER);
         return SIG_ERR;
     }
@@ -838,6 +839,11 @@
     return orghandler;
 }
 
+ACR_DECLARE(acr_sigfunc_t *) ACR_SIG_DFL(int signo, int flags)
+{
+    return ACR_Signal(signo, SIG_DFL, flags);
+}
+
 ACR_DECLARE(int) ACR_SignalSetKey(const wchar_t *key)
 {
     acr_sha1_ctx_t sha;

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/sigaction.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/sigaction.c?rev=895925&r1=895924&r2=895925&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/sigaction.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/sigaction.c Tue Jan  5 07:28:17 2010
@@ -318,8 +318,9 @@
             handler = SIG_ERR;
         break;
     }
-    if (ACR_Signal(signum, handler) == SIG_ERR)
+    if (ACR_Signal(signum, handler, 0) == SIG_ERR)
         return ACR_GET_OS_ERROR();
     else
         return 0;
 }
+

Modified: commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c?rev=895925&r1=895924&r2=895925&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c Tue Jan  5 07:28:17 2010
@@ -575,7 +575,7 @@
         return ACR_EINVAL;
     }
     exe = ACR_ExecNew(ACR_PROC_HAS_STDOUT);
-    ACR_ExecTimeoutSet(exe, ACR_USEC_PER_SEC * 2);
+    exe->timeout = ACR_USEC_PER_SEC * 2;
     rc = ACR_ExecShellCmd(exe, argv[0], NULL);
 
     fprintf(stdout, "[STDOUT]:\n%s", ACR_ExecStream(exe, 1));