Skip to content

Commit

Permalink
mxq_reaper: Use printf-like arguments for die()
Browse files Browse the repository at this point in the history
  • Loading branch information
donald committed Feb 17, 2024
1 parent 5d0de79 commit 57cc235
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions mxq_reaper.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
#include <sys/wait.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>

static const char REAPER_PNAME[] = "mxqd reaper";

__attribute__((noreturn)) static void die(char *msg) {
perror(msg);
__attribute__ ((noreturn))
__attribute__ ((format (printf, 1, 2)))
static void die(const char *restrict fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
_exit(1);
}

Expand All @@ -36,18 +42,18 @@ int main(int argc, char **argv) {
struct timeval endtime;

if (prctl(PR_SET_NAME, REAPER_PNAME, NULL, NULL, NULL) == -1)
die("PR_SET_NAME");
die("PR_SET_NAME: %m\n");
user_pid = fork();
if (user_pid == 0) {
if (setreuid(uid, uid) == -1)
die("setreuid");
die("setreuid: %m\n");
execvp(user_argv[0], user_argv);
die(user_argv[0]);
die("%s: %m\n", user_argv[0]);
}
if (user_pid == -1)
die("fork");
die("fork: %m\n");
if (gettimeofday(&starttime, NULL) == -1)
die("gettimeofday");
die("gettimeofday: %m\n");
while (1) {
int status;
pid_t pid = wait(&status);
Expand All @@ -57,10 +63,10 @@ int main(int argc, char **argv) {
user_status = status;
}
if (gettimeofday(&endtime, NULL) == -1)
die("gettimeofday");
die("gettimeofday: %m\n");
timersub(&endtime, &starttime, &user_time);
if (getrusage(RUSAGE_CHILDREN, &user_rusage) == -1)
die("getrusage");
die("getrusage: %m\n");

if (user_time.tv_sec<30) {
int wait=30-user_time.tv_sec;
Expand All @@ -69,11 +75,11 @@ int main(int argc, char **argv) {

char *tmpfilename;
if (asprintf(&tmpfilename, "%s.tmp", spoolfilename) == -1)
die("");
die("%m\n");

FILE *out = fopen(tmpfilename,"w");
if (out == NULL)
die(tmpfilename);
die("%s: %m\n", tmpfilename);
fprintf(out,"1 %d %d %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld\n",
getpid(),
user_status,
Expand All @@ -99,6 +105,6 @@ int main(int argc, char **argv) {
fsync(fileno(out));
fclose(out);
if (rename(tmpfilename, spoolfilename) == -1)
die(spoolfilename);
die("%s: %m\n", spoolfilename);
return 0;
}

0 comments on commit 57cc235

Please sign in to comment.