aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/daemon.c31
-rw-r--r--src/daemon.h11
-rw-r--r--src/satd-add.c2
-rw-r--r--src/satd-list.c2
-rw-r--r--src/satd-rm.c2
-rw-r--r--src/satd-run.c2
6 files changed, 47 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;
+}
+
diff --git a/src/daemon.h b/src/daemon.h
index 2c0d0ff..c642f66 100644
--- a/src/daemon.h
+++ b/src/daemon.h
@@ -150,3 +150,14 @@ char **restore_array(char *buf, size_t len, size_t *n);
*/
char **sublist(char *const *list, size_t n);
+/**
+ * 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);
+
diff --git a/src/satd-add.c b/src/satd-add.c
index e143c00..497e32d 100644
--- a/src/satd-add.c
+++ b/src/satd-add.c
@@ -41,6 +41,8 @@ main(int argc, char *argv[])
int msg_argc;
char **msg = NULL;
+ t (reopen(STATE_FILENO, O_RDRW));
+
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n));
shutdown(SOCK_FILENO, SHUT_RD);
diff --git a/src/satd-list.c b/src/satd-list.c
index 28a6e28..898c236 100644
--- a/src/satd-list.c
+++ b/src/satd-list.c
@@ -254,6 +254,8 @@ main(int argc, char *argv[])
struct job** jobs;
struct job** job;
+ t (reopen(STATE_FILENO, O_RDRW));
+
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n) || n);
shutdown(SOCK_FILENO, SHUT_RD);
diff --git a/src/satd-rm.c b/src/satd-rm.c
index 58b9eed..4feb93a 100644
--- a/src/satd-rm.c
+++ b/src/satd-rm.c
@@ -42,6 +42,8 @@ main(int argc, char *argv[])
char **arg;
int rc = 0;
+ t (reopen(STATE_FILENO, O_RDRW));
+
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n) || !n || message[n - 1]);
shutdown(SOCK_FILENO, SHUT_RD);
diff --git a/src/satd-run.c b/src/satd-run.c
index 49585c3..0ade981 100644
--- a/src/satd-run.c
+++ b/src/satd-run.c
@@ -42,6 +42,8 @@ main(int argc, char *argv[])
char **arg;
int rc = 0;
+ t (reopen(STATE_FILENO, O_RDRW));
+
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n) || (n && message[n - 1]));
shutdown(SOCK_FILENO, SHUT_RD);