From fc88512b5e985fc26ba4fb92b6b0bb19723524a3 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 28 Dec 2015 17:12:34 +0100 Subject: support recovering from a crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/satd-diminished.c | 17 +++++++++++++++++ src/satd.c | 2 ++ 2 files changed, 19 insertions(+) diff --git a/src/satd-diminished.c b/src/satd-diminished.c index 7ab6478..7d18a27 100644 --- a/src/satd-diminished.c +++ b/src/satd-diminished.c @@ -37,6 +37,11 @@ */ #define STATE_FILENO 4 +/** + * The file descriptor for connection to the current client. + */ +#define CONN_FILENO 5 + /** * Command: queue a job. @@ -77,6 +82,13 @@ main(int argc, char *argv[]) pid_t pid; char type; const char *image; + struct stat _attr; + + /* Pick-up where we left off. */ + if (!fstat(CONN_FILENO, _attr)) { + fd = CONN_FILENO; + goto fork_again; + } accept_again: fd = accept(SOCK_FILENO, NULL, NULL); @@ -91,6 +103,11 @@ accept_again: goto fail; } } + if (fd != CONN_FILENO) { + if (dup2(fd, CONN_FILENO) == -1) + goto fail; + close(fd), fd = CONN_FILENO; + } fork_again: if (recv(fd, &type, (size_t)1, MSG_PEEK /* Just peek in case we fail! */) <= 0) { perror(argv[0]); diff --git a/src/satd.c b/src/satd.c index f69af3b..40e04ac 100644 --- a/src/satd.c +++ b/src/satd.c @@ -263,6 +263,8 @@ main(int argc, char *argv[]) /* Daemonise. */ t (foreground ? 0 : daemonise("satd", DAEMONISE_KEEP_FDS, sock, -1)); + if (foreground) + close(5); /* Required to be closed by the next image. */ /* Change to a process image without all this initialisation text. */ execl(LIBEXEC "/" PACKAGE "/satd-diminished", argv0, -- cgit v1.2.3-70-g09d2