commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r812529 - /commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c
Date Tue, 08 Sep 2009 14:37:12 GMT
Author: mturk
Date: Tue Sep  8 14:37:12 2009
New Revision: 812529

URL: http://svn.apache.org/viewvc?rev=812529&view=rev
Log:
Ensure we cannot be interrupted during temp file create transaction

Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c?rev=812529&r1=812528&r2=812529&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c Tue Sep  8 14:37:12 2009
@@ -68,6 +68,8 @@
     int  fd;
     char name[PATH_MAX] = "";
     acr_file_t *fp;
+    sigset_t bset;
+    sigset_t oset;
 
     if (tmpath) {
         if (strlcpy(name, tmpath, TMP_PATH_MAX) >= TMP_PATH_MAX) {
@@ -85,25 +87,34 @@
         ACR_THROW_IO_IF_ERR(ACR_E2BIG);
         return -1;
     }
-
+    /* Block signals.
+     * Make sure file goes away at process exit.
+     */
+    sigfillset(&bset);
+    sigprocmask(SIG_BLOCK, &bset, &oset);
     fd = mkstemp(name);
-    if (fd < 0) {
-        ACR_THROW_IO_ERRNO();
-        return -1;
-    }
-    if (preserve) {
+    if (fd > 0) {
         /* Set close on exec flag */
         int flags;
+        if (preserve == 0)
+            unlink(name);
+        rc = 0;
         if ((flags = fcntl(fd, F_GETFD)) < 0) {
-            goto cleanup;
+            rc = errno;
         }
-        flags |= FD_CLOEXEC;
-        if (fcntl(fd, F_SETFD, flags) < 0) {
-            goto cleanup;
+        else {
+            flags |= FD_CLOEXEC;
+            if (fcntl(fd, F_SETFD, flags) < 0) {
+                rc = errno;
+            }
         }
     }
     else
-        unlink(name);
+        rc = errno;
+    sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
+    if (rc) {
+        goto finally;
+    }
     fp = ACR_Calloc(_E, THROW_NMARK, sizeof(acr_file_t));
     if (!fp) {
         goto cleanup;
@@ -115,8 +126,10 @@
 
 cleanup:
     rc = ACR_GET_OS_ERROR();
+finally:
     unlink(name);
-    close(fd);
+    if (fd > 0)
+        close(fd);
     ACR_THROW_IO_IF_ERR(rc);
     return -1;
 }



Mime
View raw message