commit 98dfcc57058e294b995cadedd46db587f5b19aa5
parent d778bb09ac134db3cab68b0c8bc2087e877a43f2
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Wed, 14 Jul 2021 15:01:23 -0700
Refine logging function
Allow it's use without an aps struct and don't call exit(2) on FATAL
log.
Diffstat:
3 files changed, 32 insertions(+), 21 deletions(-)
diff --git a/aps/aps.c b/aps/aps.c
@@ -24,6 +24,7 @@
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "aps.h"
#include "command.h"
@@ -142,18 +143,16 @@ aps_update(struct aps *aps)
}
void
-aps_log(struct aps *aps, int mask, char *message, int fd)
+aps_log(struct aps *aps, int level, char *message, int fd)
{
- if (mask & aps->logmask)
- fprintf(stderr, "aps[%d]: (%d) %s\n", mask, fd, message);
-
- if (mask == FATAL || (fd == 0 && mask == ERROR)) {
- aps_close(aps);
- exit(1);
- }
-
- if (mask == ERROR) {
- if (aps_drop(aps, fd))
+ if (aps == NULL || level & aps->logmask)
+ fprintf(stderr, "aps.%d #%d: %s: %s.\n", level, fd, message,
+ strerror(errno));
+
+ if (aps != NULL) {
+ if (level == FATAL || (fd == 0 && level == ERROR))
+ aps->close = 1;
+ else if (level == ERROR && aps_drop(aps, fd))
aps_log(aps, FATAL, "unable to drop client", 0);
}
}
diff --git a/aps/main.c b/aps/main.c
@@ -11,8 +11,6 @@
#include <unistd.h>
#endif
-#include <aputil.h>
-
#include "aps.h"
#include "command.h"
#include "config.h"
@@ -21,6 +19,12 @@
static struct aps *aps;
void
+slog(char *s)
+{
+ aps_log(NULL, FATAL, s, 0);
+}
+
+void
sigclose(int sig)
{
aps->close = 1;
@@ -82,17 +86,25 @@ setup(char *path)
int
main(int argc, char *argv[])
{
+ int error;
+
#ifdef __OpenBSD__
- if (pledge("stdio rpath cpath proc exec unix", NULL))
- die("pledge");
+ if (pledge("stdio rpath cpath proc exec unix", NULL)) {
+ slog("pledge");
+ return 1;
+ }
#endif
aps = setup(NULL);
- if (aps == NULL)
- die("unable to setup server");
+ if (aps == NULL) {
+ slog("unable to setup server");
+ return 1;
+ }
+
+ error = run(aps);
+ if (error == 0)
+ error = aps->close;
- if (run(aps))
- aps_log(aps, FATAL, NULL, 0);
aps_close(aps);
- return 0;
+ return error;
}
diff --git a/aps/mkfile b/aps/mkfile
@@ -3,7 +3,7 @@
name = aps
src = aps.c buf.c command.c find.c item.c main.c match.c player.c queue.c response.c split.c
obj = ${src:%.c=%.o}
-lib = ap aputil
+lib = ap
mk = ../mk
cppflags = ${lib:%=-I../include/%}