aboutsummaryrefslogtreecommitdiffstats
path: root/src/daemon.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@member.fsf.org>2015-12-29 00:02:52 +0100
committerMattias Andrée <maandree@member.fsf.org>2015-12-29 00:03:49 +0100
commitd198ab627709910afdc4d9500468fab698ad6d60 (patch)
treefbb95119ec2ab2b48f35f55dace1e65cea728464 /src/daemon.c
parentthe spool does not persist between boots (diff)
downloadsat-d198ab627709910afdc4d9500468fab698ad6d60.tar.gz
sat-d198ab627709910afdc4d9500468fab698ad6d60.tar.bz2
sat-d198ab627709910afdc4d9500468fab698ad6d60.tar.xz
reopen state file, we will use flock on it, so all processes need their own open file descriptor for it
Signed-off-by: Mattias Andrée <maandree@member.fsf.org>
Diffstat (limited to 'src/daemon.c')
-rw-r--r--src/daemon.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/daemon.c b/src/daemon.c
index b47e4d2..75e0f36 100644
--- a/src/daemon.c
+++ b/src/daemon.c
@@ -20,10 +20,9 @@
* DEALINGS IN THE SOFTWARE.
*/
#include "daemon.h"
-#include <stdlib.h>
#include <unistd.h>
-#include <errno.h>
-#include <string.h>
+#include <stdio.h>
+#include <fcntl.h>
@@ -132,3 +131,29 @@ fail:
return NULL;
}
+
+/**
+ * Create a new open file descriptor for an already
+ * existing file descriptor.
+ *
+ * @param fd The file descriptor that shall be promoted
+ * to a new open file descriptor.
+ * @param oflag See open(3), `O_CREAT` is not allowed.
+ * @return 0 on success, -1 on error.
+ */
+int
+reopen(int fd, int oflag)
+{
+ char path[sizeof("/dev/fd/") + 3 * sizeof(int)];
+ int r, saved_errno;
+
+ sprintf(path, "/dev/fd/%i", fd);
+ r = open(fd, oflag);
+ if (r < 0)
+ return -1;
+ if (dup2(r, fd) == -1)
+ return saved_errno = errno, close(r), errno = saved_errno, -1;
+ close(r);
+ return 0;
+}
+