aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/daemon.h31
-rw-r--r--src/satd-add.c20
-rw-r--r--src/satd-list.c20
-rw-r--r--src/satd-rm.c20
-rw-r--r--src/satd-run.c20
5 files changed, 43 insertions, 68 deletions
diff --git a/src/daemon.h b/src/daemon.h
index 562ec52..e2a3f40 100644
--- a/src/daemon.h
+++ b/src/daemon.h
@@ -80,6 +80,37 @@
/**
+ * Macro to put directly after the variable definitions in `main`.
+ */
+#define DAEMON_PROLOGUE \
+ int rc = 0; \
+ assert(argc == 3); \
+ t (reopen(STATE_FILENO, O_RDWR)) \
+
+/**
+ * Macro to put before the cleanup code in `main`.
+ */
+#define DAEMON_CLEANUP_START \
+done: \
+ shutdown(SOCK_FILENO, SHUT_WR); \
+ close(SOCK_FILENO); \
+ close(STATE_FILENO)
+
+/**
+ * Macro to put after the cleanup code in `main`.
+ */
+#define DAEMON_CLEANUP_END \
+ return rc; \
+fail: \
+ if (send_string(SOCK_FILENO, STDERR_FILENO, argv[0], ": ", strerror(errno), "\n", NULL)) \
+ perror(argv[0]); \
+ rc = 1; \
+ goto done; \
+ (void) argc
+
+
+
+/**
* A queued job.
*/
struct job {
diff --git a/src/satd-add.c b/src/satd-add.c
index 5a5e004..c3f19b6 100644
--- a/src/satd-add.c
+++ b/src/satd-add.c
@@ -41,12 +41,9 @@ main(int argc, char *argv[])
ssize_t r;
char *message = NULL;
int msg_argc;
- int rc = 0;
struct job *job = NULL;
struct stat attr;
-
- assert(argc == 3);
- t (reopen(STATE_FILENO, O_RDWR));
+ DAEMON_PROLOGUE;
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n));
@@ -83,20 +80,9 @@ main(int argc, char *argv[])
fsync(STATE_FILENO);
t (flock(STATE_FILENO, LOCK_UN));
-done:
- /* Cleanup. */
- shutdown(SOCK_FILENO, SHUT_WR);
- close(SOCK_FILENO);
- close(STATE_FILENO);
+ DAEMON_CLEANUP_START;
free(message);
free(job);
- return rc;
-fail:
- if (send_string(SOCK_FILENO, STDERR_FILENO, argv[0], ": ", strerror(errno), "\n", NULL))
- perror(argv[0]);
- rc = 1;
- goto done;
-
- (void) argc;
+ DAEMON_CLEANUP_END;
}
diff --git a/src/satd-list.c b/src/satd-list.c
index 29175c9..ed7c2bd 100644
--- a/src/satd-list.c
+++ b/src/satd-list.c
@@ -249,10 +249,7 @@ main(int argc, char *argv[])
char *message = NULL;
struct job** jobs;
struct job** job;
- int rc = 0;
-
- assert(argc == 3);
- t (reopen(STATE_FILENO, O_RDWR));
+ DAEMON_PROLOGUE;
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n) || n);
@@ -263,22 +260,11 @@ main(int argc, char *argv[])
for (job = jobs; *job; job++)
t (send_job_human(*job));
-done:
- /* Cleanup. */
- shutdown(SOCK_FILENO, SHUT_WR);
- close(SOCK_FILENO);
- close(STATE_FILENO);
+ DAEMON_CLEANUP_START;
for (job = jobs; *job; job++)
free(*job);
free(jobs);
free(message);
- return rc;
-fail:
- if (send_string(SOCK_FILENO, STDERR_FILENO, argv[0], ": ", strerror(errno), "\n", NULL))
- perror(argv[0]);
- rc = 1;
- goto done;
-
- (void) argc;
+ DAEMON_CLEANUP_END;
}
diff --git a/src/satd-rm.c b/src/satd-rm.c
index f245a1f..6fe0aac 100644
--- a/src/satd-rm.c
+++ b/src/satd-rm.c
@@ -39,10 +39,7 @@ main(int argc, char *argv[])
char *message = NULL;
char **msg_argv = NULL;
char **arg;
- int rc = 0;
-
- assert(argc == 3);
- t (reopen(STATE_FILENO, O_RDWR));
+ DAEMON_PROLOGUE;
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n) || !n || message[n - 1]);
@@ -54,20 +51,9 @@ main(int argc, char *argv[])
for (arg = msg_argv; *arg; arg++)
t (remove_job(*arg, 0) && errno);
-done:
- /* Cleanup. */
- shutdown(SOCK_FILENO, SHUT_WR);
- close(SOCK_FILENO);
- close(STATE_FILENO);
+ DAEMON_CLEANUP_START;
free(msg_argv);
free(message);
- return rc;
-fail:
- if (send_string(SOCK_FILENO, STDERR_FILENO, argv[0], ": ", strerror(errno), "\n", NULL))
- perror(argv[0]);
- rc = 1;
- goto done;
-
- (void) argc;
+ DAEMON_CLEANUP_END;
}
diff --git a/src/satd-run.c b/src/satd-run.c
index 1b92558..6e4eef1 100644
--- a/src/satd-run.c
+++ b/src/satd-run.c
@@ -39,10 +39,7 @@ main(int argc, char *argv[])
char *message = NULL;
char **msg_argv = NULL;
char **arg;
- int rc = 0;
-
- assert(argc == 3);
- t (reopen(STATE_FILENO, O_RDWR));
+ DAEMON_PROLOGUE;
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n) || (n && message[n - 1]));
@@ -62,20 +59,9 @@ main(int argc, char *argv[])
t (errno);
}
-done:
- /* Cleanup. */
- shutdown(SOCK_FILENO, SHUT_WR);
- close(SOCK_FILENO);
- close(STATE_FILENO);
+ DAEMON_CLEANUP_START;
free(msg_argv);
free(message);
- return rc;
-fail:
- if (send_string(SOCK_FILENO, STDERR_FILENO, argv[0], ": ", strerror(errno), "\n", NULL))
- perror(argv[0]);
- rc = 1;
- goto done;
-
- (void) argc;
+ DAEMON_CLEANUP_END;
}