aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2016-07-11 14:58:50 +0200
committerMattias Andrée <maandree@kth.se>2016-07-11 14:58:50 +0200
commit0fd708b8f1ffd099d092c02af28c79101ca58524 (patch)
tree2c06414416b4a7bf7e42bcce0f6dbedabbe57d64 /src/util.c
parentSignal the spawner when the service is initialised enough (diff)
downloadcoopgammad-0fd708b8f1ffd099d092c02af28c79101ca58524.tar.gz
coopgammad-0fd708b8f1ffd099d092c02af28c79101ca58524.tar.bz2
coopgammad-0fd708b8f1ffd099d092c02af28c79101ca58524.tar.xz
Place in background unless -f
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--src/util.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index cb8c155..715aa6b 100644
--- a/src/util.c
+++ b/src/util.c
@@ -96,3 +96,40 @@ void* nread(int fd, size_t* n)
return NULL;
}
+
+/**
+ * Duplicate a file descriptor an make sure
+ * the new file descriptor's index as a
+ * specified minimum value
+ *
+ * @param fd The file descriptor
+ * @param atleast The least acceptable new file descriptor
+ * @return The new file descriptor, -1 on error
+ */
+int dup2atleast(int fd, int atleast)
+{
+ int* stack = malloc((size_t)(atleast + 1) * sizeof(int));
+ size_t stack_ptr = 0;
+ int new = -1, saved_errno;
+
+ if (stack == NULL)
+ goto fail;
+
+ for (;;)
+ {
+ new = dup(fd);
+ if (new < 0)
+ goto fail;
+ if (new >= atleast)
+ break;
+ }
+
+ fail:
+ saved_errno = errno;
+ while (stack_ptr--)
+ close(stack[stack_ptr]);
+ free(stack);
+ errno = saved_errno;
+ return new;
+}
+