aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/client.c3
-rw-r--r--src/daemon.c48
-rw-r--r--src/parse_time.c46
-rw-r--r--src/satd-add.c7
-rw-r--r--src/satd-diminished.c11
-rw-r--r--src/satd-list.c23
-rw-r--r--src/satd-rm.c1
-rw-r--r--src/satd-run.c1
-rw-r--r--src/satd.c46
9 files changed, 71 insertions, 115 deletions
diff --git a/src/client.c b/src/client.c
index 6b57358..6da3b39 100644
--- a/src/client.c
+++ b/src/client.c
@@ -102,8 +102,7 @@ send_command(enum command cmd, size_t n, const char *restrict msg)
/* Send message. */
t (write(fd, &cmd_, sizeof(cmd_)) < (ssize_t)sizeof(cmd_));
while (n) {
- r = write(fd, msg, n);
- t (r <= 0);
+ t (r = write(fd, msg, n), r <= 0);
msg += (size_t)r;
n -= (size_t)r;
}
diff --git a/src/daemon.c b/src/daemon.c
index 88e1b93..6402473 100644
--- a/src/daemon.c
+++ b/src/daemon.c
@@ -104,33 +104,29 @@ int
readall(int fd, char **buf, size_t *n)
{
char *buffer = NULL;
- size_t ptr = 0;
- size_t size = 0;
- ssize_t got;
+ size_t ptr = 0, size = 0;
+ ssize_t got = 1;
char *new;
int saved_errno;
- for (;;) {
+ for (; got; ptr += (size_t)got) {
if (ptr == size) {
- new = realloc(buffer, size <<= 1);
- t (!new);
+ t (!(new = realloc(buffer, size <<= 1)));
buffer = new;
}
- got = read(fd, buffer + ptr, size - ptr);
- t (got < 0);
- if (got == 0)
- break;
- ptr += (size_t)got;
+ t (got = read(fd, buffer + ptr, size - ptr), got < 0);
}
new = realloc(buffer, ptr);
*buf = new ? new : buffer;
*n = ptr;
+ shutdown(SOCK_FILENO, SHUT_RD);
return 0;
fail:
saved_errno = errno;
free(buffer);
+ shutdown(SOCK_FILENO, SHUT_RD);
errno = saved_errno;
return -1;
}
@@ -153,20 +149,19 @@ char **
restore_array(char *buf, size_t len, size_t *n)
{
char **rc = malloc((len + 1) * sizeof(char*));
- char **new;
+ char **new = NULL;
size_t i, e = 0;
t (!rc);
while (i < len) {
- rc[++e] = buf + i;
+ rc[e++] = buf + i;
i += strlen(buf + i);
}
rc[e] = NULL;
new = realloc(rc, (e + 1) * sizeof(char*));
if (n)
*n = e;
- return new ? new : rc;
fail:
- return NULL;
+ return new ? new : rc;
}
@@ -184,12 +179,9 @@ sublist(char *const *list, size_t n)
{
char **rc = malloc((n + 1) * sizeof(char*));
t (!rc);
- rc[n] = NULL;
- while (n--)
- rc[n] = list[n];
- return rc;
+ for (rc[n] = NULL; n--; rc[n] = list[n]);
fail:
- return NULL;
+ return rc;
}
@@ -209,12 +201,11 @@ reopen(int fd, int oflag)
int r, saved_errno;
sprintf(path, "/dev/fd/%i", fd);
- r = open(path, oflag);
- if (r < 0)
+ if (r = open(path, oflag), r < 0)
return -1;
if (dup2(r, fd) == -1)
return saved_errno = errno, close(r), errno = saved_errno, -1;
- close(r);
+ close(fd);
return 0;
}
@@ -272,16 +263,14 @@ run_job_or_hook(struct job *job, const char *hook)
char **envp = NULL;
size_t argsn;
void *new;
- int status = 0;
- int saved_errno;
+ int status = 0, saved_errno;
t (!(args = restore_array(job->payload, job->n, &argsn)));
t (!(argv = sublist(args, (size_t)(job->argc))));
t (!(envp = sublist(args + job->argc, argsn - (size_t)(job->argc))));
if (hook) {
- new = realloc(argv, ((size_t)(job->argc) + 3) * sizeof(*argv));
- t (!new);
+ t (!(new = realloc(argv, ((size_t)(job->argc) + 3) * sizeof(*argv))));
argv = new;
memmove(argv + 2, argv, ((size_t)(job->argc) + 1) * sizeof(*argv));
argv[0] = getenv("SAT_HOOK_PATH");
@@ -297,7 +286,6 @@ run_job_or_hook(struct job *job, const char *hook)
environ = envp;
execve(*argv, argv, envp);
exit(1);
- break;
default:
t (waitpid(pid, &status, 0) != pid);
break;
@@ -342,12 +330,10 @@ remove_job(const char *jobno, int runjob)
t (flock(STATE_FILENO, LOCK_EX));
t (fstat(STATE_FILENO, &attr));
- n = (size_t)(attr.st_size);
- while (off < n) {
+ for (n = (size_t)(attr.st_size); off < n; off += sizeof(job) + job.n) {
t (preadn(STATE_FILENO, &job, sizeof(job), off) < (ssize_t)sizeof(job));
if (!jobno || (job.no == no))
goto found_it;
- off += sizeof(job) + job.n;
}
t (flock(STATE_FILENO, LOCK_UN));
return 0;
diff --git a/src/parse_time.c b/src/parse_time.c
index 18e5513..323e746 100644
--- a/src/parse_time.c
+++ b/src/parse_time.c
@@ -21,6 +21,7 @@
*/
#define _DEFAULT_SOURCE
#include "parse_time.h"
+#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -124,25 +125,22 @@ strtotime(const char *str, const char **end)
static int
parse_time_time(const char *str, struct timespec *ts, const char **end)
{
- time_t t;
-
+ time_t tm;
memset(ts, 0, sizeof(*ts));
ts->tv_sec = strtotime(str, end), str = *end;
- if (errno)
- return -1;
+ t (errno);
/* Must not be restricted to 23, beyond 24 is legal. */
MUL(ts->tv_sec, (time_t)(60 * 60));
if (*str++ != ':')
FAIL(EINVAL);
- t = strtotime(str, end), str = *end;
- if (errno)
- return -1;
- if (t >= 60)
+ tm = strtotime(str, end), str = *end;
+ t (errno);
+ if (tm >= 60)
FAIL(EINVAL);
- MUL(t, (time_t)60);
- ADD(ts->tv_sec, t);
+ MUL(tm, (time_t)60);
+ ADD(ts->tv_sec, tm);
switch (*str++) {
case 0: return 0;
@@ -150,13 +148,14 @@ parse_time_time(const char *str, struct timespec *ts, const char **end)
default: FAIL(EINVAL);
}
- t = strtotime(str, end), str = *end;
- if (errno)
- return -1;
+ tm = strtotime(str, end), str = *end;
+ t (errno);
/* Do not restrict to 59, or 60, there can be more than +1 leap second. */
- ADD(ts->tv_sec, t);
+ ADD(ts->tv_sec, tm);
return 0;
+fail:
+ return -1;
}
@@ -176,12 +175,8 @@ static int
parse_time_seconds(const char *str, struct timespec *ts, const char **end)
{
memset(ts, 0, sizeof(*ts));
-
ts->tv_sec = strtotime(str, end);
- if (errno)
- return -1;
-
- return 0;
+ return errno ? -1 : 0;
}
@@ -229,13 +224,11 @@ parse_time(const char *str, struct timespec *ts, clockid_t *clk)
/* HH:MM[:SS[.NNNNNNNNN]] or seconds? */
if (strchr(str, ':')) {
- if (parse_time_time(str, ts, &end))
- return -1;
+ t (parse_time_time(str, ts, &end));
adj = now.tv_sec - (now.tv_sec % ONE_DAY);
ADD(ts->tv_sec, adj); /* In case the HH is really large. */
} else {
- if (parse_time_seconds(str + plus, ts, &end))
- return -1;
+ t (parse_time_seconds(str + plus, ts, &end));
}
str = end;
@@ -269,9 +262,8 @@ parse_time(const char *str, struct timespec *ts, clockid_t *clk)
FAIL(EINVAL);
} else if (*clk == CLOCK_REALTIME) {
fprintf(stderr,
- "%s: warning: parsing as UTC, you can avoid "
- "this warning by adding a 'Z' at the end of "
- "the time argument.\n", argv0);
+ "%s: warning: parsing as UTC, you can avoid this warning "
+ "by adding a 'Z' at the end of the time argument.\n", argv0);
}
/* Adjust the day? */
@@ -288,5 +280,7 @@ parse_time(const char *str, struct timespec *ts, clockid_t *clk)
}
return 0;
+fail:
+ return -1;
}
diff --git a/src/satd-add.c b/src/satd-add.c
index c3f19b6..90d445c 100644
--- a/src/satd-add.c
+++ b/src/satd-add.c
@@ -47,7 +47,6 @@ main(int argc, char *argv[])
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n));
- shutdown(SOCK_FILENO, SHUT_RD);
t (n < sizeof(int) + sizeof(clockid_t) + sizeof(struct timespec));
n -= sizeof(int) + sizeof(clockid_t) + sizeof(struct timespec);
msg_argc = *(int *)(message + n);
@@ -63,11 +62,10 @@ main(int argc, char *argv[])
job->n = n;
memcpy(job->payload, message, n);
- /* Update state file. */
+ /* Update state file and run hook. */
t (flock(STATE_FILENO, LOCK_EX));
t (fstat(STATE_FILENO, &attr));
- r = preadn(STATE_FILENO, &(job->no), sizeof(job->no), 0);
- t (r < 0);
+ t (r = preadn(STATE_FILENO, &(job->no), sizeof(job->no), 0), r < 0);
if (r < (ssize_t)sizeof(job->no))
job->no = 0;
else
@@ -78,6 +76,7 @@ main(int argc, char *argv[])
n += sizeof(*job);
t (pwriten(STATE_FILENO, job, n, attr.st_size) < (ssize_t)n);
fsync(STATE_FILENO);
+ run_job_or_hook(job, "queued");
t (flock(STATE_FILENO, LOCK_UN));
DAEMON_CLEANUP_START;
diff --git a/src/satd-diminished.c b/src/satd-diminished.c
index 65f6d31..18f65d8 100644
--- a/src/satd-diminished.c
+++ b/src/satd-diminished.c
@@ -52,14 +52,10 @@ static volatile sig_atomic_t received_signo = 0;
static void sighandler(int signo)
{
int saved_errno = errno;
- switch (signo) {
- case SIGCHLD:
+ if (signo == SIGCHLD)
waitpid(-1, NULL, WNOHANG);
- break;
- default:
+ else
received_signo = (sig_atomic_t)signo;
- break;
- }
errno = saved_errno;
}
@@ -95,8 +91,7 @@ accept_again:
perror(argv[0]);
}
received_signo = 0;
- fd = accept(SOCK_FILENO, NULL, NULL);
- if (fd == -1) {
+ if (fd = accept(SOCK_FILENO, NULL, NULL), fd == -1) {
switch (errno) {
case ECONNABORTED:
case EINTR:
diff --git a/src/satd-list.c b/src/satd-list.c
index 4b45089..0a601dd 100644
--- a/src/satd-list.c
+++ b/src/satd-list.c
@@ -47,7 +47,7 @@ quote(const char *str)
size_t rn = 0; /* other */
size_t n, i = 0;
const unsigned char *s;
- char *rc;
+ char *rc = NULL;
for (s = (const unsigned char *)str; *s; s++) {
if (*s < ' ') in++;
@@ -70,9 +70,7 @@ quote(const char *str)
rc[i++] = '\\', rc[i++] = '\'', rc[i++] = '\'';
}
- rc[i++] = '\'';
- rc[i] = '\0';
- return rc;
+ break;
case 2:
n = 4 * in + rn + sn + 2 * bn + 2 * qn + 3;
t (!(rc = malloc((n + 1) * sizeof(char))));
@@ -85,16 +83,15 @@ quote(const char *str)
rc[i++] = "0123456789ABCDEF"[(*s >> 4) & 15];
rc[i++] = "0123456789ABCDEF"[(*s >> 0) & 15];
}
- else if (*s == '\\') rc[i++] = '\\', rc[i++] = (char)*s;
- else if (*s == '\'') rc[i++] = '\\', rc[i++] = (char)*s;
- else rc[i++] = (char)*s;
+ else if (strchr("\\'", *s)) rc[i++] = '\\', rc[i++] = (char)*s;
+ else rc[i++] = (char)*s;
}
- rc[i++] = '\'';
- rc[i] = '\0';
- return rc;
+ break;
}
+ rc[i++] = '\'';
+ rc[i] = '\0';
fail:
- return NULL;
+ return rc;
}
@@ -150,8 +147,7 @@ send_job_human(struct job *job)
char **argv = NULL;
char **envp = NULL;
size_t argsn;
- int rc = 0;
- int saved_errno;
+ int rc = 0, saved_errno;
/* Get remaining time. */
if (clock_gettime(job->clk, &rem))
@@ -253,7 +249,6 @@ main(int argc, char *argv[])
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n) || n);
- shutdown(SOCK_FILENO, SHUT_RD);
/* Perform action. */
t (!(jobs = get_jobs()));
diff --git a/src/satd-rm.c b/src/satd-rm.c
index 6fe0aac..435c302 100644
--- a/src/satd-rm.c
+++ b/src/satd-rm.c
@@ -43,7 +43,6 @@ main(int argc, char *argv[])
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n) || !n || message[n - 1]);
- shutdown(SOCK_FILENO, SHUT_RD);
msg_argv = restore_array(message, n, NULL);
t (!msg_argv);
diff --git a/src/satd-run.c b/src/satd-run.c
index 6e4eef1..00b642e 100644
--- a/src/satd-run.c
+++ b/src/satd-run.c
@@ -43,7 +43,6 @@ main(int argc, char *argv[])
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n) || (n && message[n - 1]));
- shutdown(SOCK_FILENO, SHUT_RD);
if (n) {
msg_argv = restore_array(message, n, NULL);
t (!msg_argv);
diff --git a/src/satd.c b/src/satd.c
index f4b5154..98ea6a7 100644
--- a/src/satd.c
+++ b/src/satd.c
@@ -135,15 +135,14 @@ hookpath(const char *env, const char *suffix)
if (!prefix || !*prefix)
goto try_next;
- path = malloc((strlen(prefix) + strlen(suffix) + 1) * sizeof(char));
- t (!path);
+ t (!(path = malloc((strlen(prefix) + strlen(suffix) + 1) * sizeof(char))));
stpcpy(stpcpy(path, prefix), suffix);
return path;
+try_next:
+ errno = 0;
fail:
return NULL;
-try_next:
- return errno = 0, NULL;
}
@@ -163,19 +162,16 @@ dup2_and_null(int old, int new)
int want, fd = -1;
int saved_errno;
- if (old != new) {
- t (dup2(old, new) == -1);
- close(old), want = old;
- if (want < 3) {
- fd = open("/dev/null", O_RDWR);
- t (fd == -1);
- if (fd != want) {
- t (dup2(fd, want) == -1);
- close(fd), fd = -1;
- }
- }
- }
+ if (old == new) goto done;
+ t (dup2(old, new) == -1);
+ close(old), want = old;
+ if (want >= 3) goto done;
+ t (fd = open("/dev/null", O_RDWR), fd == -1);
+ if (fd == want) goto done;
+ t (dup2(fd, want) == -1);
+ close(fd), fd = -1;
+done:
return new;
fail:
saved_errno = errno;
@@ -200,7 +196,7 @@ int
main(int argc, char *argv[])
{
struct sockaddr_un address;
- int sock = -1, state = -1, foreground = 0;
+ int sock = -1, state = -1, foreground = 0, do_not_free = 0;
char *path = NULL;
char *dir;
@@ -213,13 +209,9 @@ main(int argc, char *argv[])
/* Get hook-script pathname. */
if (!getenv("SAT_HOOK_PATH")) {
- int do_not_free = 0;
- path = hookpath("XDG_CONFIG_HOME", "/sat/hook");
- t (!path && errno);
- path = path ? path : hookpath("HOME", "/.config/sat/hook");
- t (!path && errno);
- path = path ? path : hookpath(NULL, "/.config/sat/hook");
- t (!path && errno);
+ t (path = hookpath("XDG_CONFIG_HOME", "/sat/hook"), !path && errno);
+ t (path = path ? path : hookpath("HOME", "/.config/sat/hook"), !path && errno);
+ t (path = path ? path : hookpath(NULL, "/.config/sat/hook"), !path && errno);
path = path ? path : (do_not_free = 1, "/etc/sat/hook");
t (setenv("SAT_HOOK_PATH", path, 1));
if (!do_not_free)
@@ -229,11 +221,9 @@ main(int argc, char *argv[])
/* Open/create state file. */
dir = getenv("XDG_RUNTIME_DIR"), dir = (dir ? dir : "/run");
- path = malloc(strlen(dir) * sizeof(char) + sizeof("/satd.state"));
- t (!path);
+ t (!(path = malloc(strlen(dir) * sizeof(char) + sizeof("/satd.state"))));
stpcpy(stpcpy(path, dir), "/satd.state");
- state = open(path, O_RDWR | O_CREAT /* but not O_EXCL */, S_IRWXU);
- t (state == -1);
+ t (state = open(path, O_RDWR | O_CREAT /* but not O_EXCL */, S_IRWXU), state == -1);
free(path), path = NULL;
/* The state fill shall be on fd STATE_FILENO. */