commit b0074ba45eae1294de10369c59d7150d9624316f
parent cf92097bc21188136c9e5581b6f4c8f33418faf1
Author: Joris Vink <joris@coders.se>
Date: Mon, 13 Aug 2018 09:53:49 +0200
Add fatalx().
If a worker process dies it automatically gets respawned by the
parent process, but sometimes you want the entire server to go down
if a worker encounters an error. This is what fatalx() does.
Calling fatalx() from a worker process will initiate a full shutdown
of the kore server you are running under.
Diffstat:
3 files changed, 42 insertions(+), 4 deletions(-)
diff --git a/include/kore/kore.h b/include/kore/kore.h
@@ -745,6 +745,7 @@ struct kore_validator *kore_validator_lookup(const char *);
#endif
void fatal(const char *, ...) __attribute__((noreturn));
+void fatalx(const char *, ...) __attribute__((noreturn));
void kore_debug_internal(char *, int, const char *, ...);
u_int16_t net_read16(u_int8_t *);
diff --git a/src/msg.c b/src/msg.c
@@ -34,6 +34,7 @@ static int msg_recv_packet(struct netbuf *);
static int msg_recv_data(struct netbuf *);
static void msg_disconnected_parent(struct connection *);
static void msg_disconnected_worker(struct connection *);
+static void msg_type_shutdown(struct kore_msg *, const void *);
#if !defined(KORE_NO_HTTP)
static void msg_type_accesslog(struct kore_msg *, const void *);
@@ -57,6 +58,8 @@ kore_msg_parent_init(void)
kore_msg_parent_add(kw);
}
+ kore_msg_register(KORE_MSG_SHUTDOWN, msg_type_shutdown);
+
#if !defined(KORE_NO_HTTP)
kore_msg_register(KORE_MSG_ACCESSLOG, msg_type_accesslog);
#endif
@@ -104,6 +107,7 @@ kore_msg_worker_init(void)
worker->msg[1]->state = CONN_STATE_ESTABLISHED;
worker->msg[1]->disconnect = msg_disconnected_parent;
worker->msg[1]->handle = kore_connection_handle;
+ worker->msg[1]->flags = CONN_WRITE_POSSIBLE;
TAILQ_INSERT_TAIL(&connections, worker->msg[1], list);
kore_platform_event_all(worker->msg[1]->fd, worker->msg[1]);
@@ -214,6 +218,15 @@ msg_disconnected_worker(struct connection *c)
c->hdlr_extra = NULL;
}
+static void
+msg_type_shutdown(struct kore_msg *msg, const void *data)
+{
+ kore_log(LOG_NOTICE,
+ "shutdown requested by worker %u, going down", msg->src);
+
+ (void)raise(SIGQUIT);
+}
+
#if !defined(KORE_NO_HTTP)
static void
msg_type_accesslog(struct kore_msg *msg, const void *data)
diff --git a/src/utils.c b/src/utils.c
@@ -45,6 +45,8 @@ static struct {
{ NULL, 0 },
};
+static void fatal_log(const char *, va_list);
+
static char b64table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#if defined(KORE_DEBUG)
@@ -607,13 +609,36 @@ kore_read_line(FILE *fp, char *in, size_t len)
void
fatal(const char *fmt, ...)
{
- va_list args;
+ va_list args;
+
+ va_start(args, fmt);
+ fatal_log(fmt, args);
+ va_end(args);
+
+ exit(1);
+}
+
+void
+fatalx(const char *fmt, ...)
+{
+ va_list args;
+
+ kore_msg_send(KORE_MSG_PARENT, KORE_MSG_SHUTDOWN, NULL, 0);
+
+ va_start(args, fmt);
+ fatal_log(fmt, args);
+ va_end(args);
+
+ exit(1);
+}
+
+static void
+fatal_log(const char *fmt, va_list args)
+{
char buf[2048];
extern const char *__progname;
- va_start(args, fmt);
(void)vsnprintf(buf, sizeof(buf), fmt, args);
- va_end(args);
if (!foreground)
kore_log(LOG_ERR, "%s", buf);
@@ -624,5 +649,4 @@ fatal(const char *fmt, ...)
#endif
printf("%s: %s\n", __progname, buf);
- exit(1);
}