chiark / gitweb /
journal: optimize iteration by skipping exhausted files
[elogind.git] / src / shared / util.c
index 254b5637ae8c161acbcc581b929c8207db8c95a2..364f61885b00a1b3f9a076a993b982f74468ba7e 100644 (file)
@@ -2468,9 +2468,9 @@ int make_stdio(int fd) {
 
         assert(fd >= 0);
 
-        r = dup3(fd, STDIN_FILENO, 0);
-        s = dup3(fd, STDOUT_FILENO, 0);
-        t = dup3(fd, STDERR_FILENO, 0);
+        r = dup2(fd, STDIN_FILENO);
+        s = dup2(fd, STDOUT_FILENO);
+        t = dup2(fd, STDERR_FILENO);
 
         if (fd >= 3)
                 safe_close(fd);
@@ -2478,7 +2478,11 @@ int make_stdio(int fd) {
         if (r < 0 || s < 0 || t < 0)
                 return -errno;
 
-        /* We rely here that the new fd has O_CLOEXEC not set */
+        /* Explicitly unset O_CLOEXEC, since if fd was < 3, then
+         * dup2() was a NOP and the bit hence possibly set. */
+        fd_cloexec(STDIN_FILENO, false);
+        fd_cloexec(STDOUT_FILENO, false);
+        fd_cloexec(STDERR_FILENO, false);
 
         return 0;
 }
@@ -6977,6 +6981,14 @@ int tempfn_xxxxxx(const char *p, char **ret) {
         assert(p);
         assert(ret);
 
+        /*
+         * Turns this:
+         *         /foo/bar/waldo
+         *
+         * Into this:
+         *         /foo/bar/.waldoXXXXXX
+         */
+
         fn = basename(p);
         if (!filename_is_valid(fn))
                 return -EINVAL;
@@ -6987,7 +6999,7 @@ int tempfn_xxxxxx(const char *p, char **ret) {
 
         strcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), "."), fn), "XXXXXX");
 
-        *ret = t;
+        *ret = path_kill_slashes(t);
         return 0;
 }
 
@@ -7000,6 +7012,14 @@ int tempfn_random(const char *p, char **ret) {
         assert(p);
         assert(ret);
 
+        /*
+         * Turns this:
+         *         /foo/bar/waldo
+         *
+         * Into this:
+         *         /foo/bar/.waldobaa2a261115984a9
+         */
+
         fn = basename(p);
         if (!filename_is_valid(fn))
                 return -EINVAL;
@@ -7018,7 +7038,39 @@ int tempfn_random(const char *p, char **ret) {
 
         *x = 0;
 
-        *ret = t;
+        *ret = path_kill_slashes(t);
+        return 0;
+}
+
+int tempfn_random_child(const char *p, char **ret) {
+        char *t, *x;
+        uint64_t u;
+        unsigned i;
+
+        assert(p);
+        assert(ret);
+
+        /* Turns this:
+         *         /foo/bar/waldo
+         * Into this:
+         *         /foo/bar/waldo/.3c2b6219aa75d7d0
+         */
+
+        t = new(char, strlen(p) + 2 + 16 + 1);
+        if (!t)
+                return -ENOMEM;
+
+        x = stpcpy(stpcpy(t, p), "/.");
+
+        u = random_u64();
+        for (i = 0; i < 16; i++) {
+                *(x++) = hexchar(u & 0xF);
+                u >>= 4;
+        }
+
+        *x = 0;
+
+        *ret = path_kill_slashes(t);
         return 0;
 }