Clean up logging, log to stderr until syslogd has started

This patch removes internal logging macros and adds support for logging
to stderr until a syslogd is up and running, i.e. /dev/log appears.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2016-08-18 01:18:31 +02:00
1 parent 77561b6071
commit d670e7d3e8
7 files changed
+57 -41

No files matched your search

+3 -3
View File
@@ -125,7 +125,7 @@ void conf_parse_cond(svc_t *svc, char *cond)
ptr[i] = 0; ptr[i] = 0;
if (i >= sizeof(svc->cond)) { if (i >= sizeof(svc->cond)) {
FLOG_WARN("Too long event list in declaration of %s: %s", svc->cmd, ptr); logit(LOG_WARNING, "Too long event list in declaration of %s: %s", svc->cmd, ptr);
return; return;
} }
@@ -212,10 +212,10 @@ void conf_parse_rlimit(char *line)
return; return;
fail: fail:
FLOG_WARN("rlimit: Failed setting rlimit %s", name->name ? : "unknown"); logit(LOG_WARNING, "rlimit: Failed setting rlimit %s", name->name ? : "unknown");
return; return;
error: error:
FLOG_WARN("rlimit: parse error"); logit(LOG_WARNING, "rlimit: parse error");
} }
static void parse_static(char *line) static void parse_static(char *line)
+1 -1
View File
@@ -277,7 +277,7 @@ int getty(char *tty, char *baud, char *UNUSED(term))
if (baud) { if (baud) {
speed = do_parse_speed(baud); speed = do_parse_speed(baud);
if (speed == B0) { if (speed == B0) {
FLOG_ERROR("TTY %s: Invalid speed %s", tty, baud); logit(LOG_CRIT, "TTY %s: Invalid speed %s", tty, baud);
return 1; return 1;
} }
} }
+28
View File
@@ -173,6 +173,34 @@ int print_result(int fail)
return fail; return fail;
} }
/*
* Log to stderr until syslogd has started, then openlog() and continue
* logging as a regular daemon.
*
* TODO: Log to /dev/kmsg instead of stderr until syslogd has started
* Need to convert facility+prio => "<VAL> msg"
*/
void logit(int prio, const char *fmt, ...)
{
va_list ap;
static int active = 0;
va_start(ap, fmt);
if (!active && !fexist("/dev/log")) {
vfprintf(stderr, fmt, ap);
va_end(ap);
return;
}
if (!active) {
openlog("finit", LOG_PID, LOG_DAEMON);
active = 1;
}
vsyslog(prio, fmt, ap);
va_end(ap);
}
int getuser(char *username, char **home) int getuser(char *username, char **home)
{ {
#ifdef ENABLE_STATIC #ifdef ENABLE_STATIC
+3 -13
View File
@@ -30,19 +30,6 @@
#include <syslog.h> #include <syslog.h>
#include <sys/types.h> #include <sys/types.h>
#define DO_LOG(level, fmt, args...) \
do { \
openlog("finit", LOG_CONS | LOG_PID, LOG_DAEMON); \
syslog(level, fmt, ##args); \
closelog(); \
} while (0)
#define FLOG_DEBUG(fmt, args...) DO_LOG(LOG_DEBUG, fmt, ##args)
#define FLOG_INFO(fmt, args...) DO_LOG(LOG_INFO, fmt, ##args)
#define FLOG_WARN(fmt, args...) DO_LOG(LOG_WARNING, fmt, ##args)
#define FLOG_ERROR(fmt, args...) DO_LOG(LOG_CRIT, fmt, ##args)
#define FLOG_PERROR(fmt, args...) DO_LOG(LOG_CRIT, fmt ". Error %d: %s", ##args, errno, strerror(errno))
#define echo(fmt, args...) do { fprintf(stderr, fmt "\n", ##args); } while (0) #define echo(fmt, args...) do { fprintf(stderr, fmt "\n", ##args); } while (0)
#define _d(fmt, args...) do { if (debug) { fprintf(stderr, "finit:%s:%s() - " fmt "\n", __FILE__, __func__, ##args); } } while (0) #define _d(fmt, args...) do { if (debug) { fprintf(stderr, "finit:%s:%s() - " fmt "\n", __FILE__, __func__, ##args); } } while (0)
#define _e(fmt, args...) do { fprintf(stderr, "finit:%s:%s() - " fmt "\n", __FILE__, __func__, ##args); } while (0) #define _e(fmt, args...) do { fprintf(stderr, "finit:%s:%s() - " fmt "\n", __FILE__, __func__, ##args); } while (0)
@@ -61,6 +48,9 @@ void procname_set (char *name, char *args[]);
void print (int action, const char *fmt, ...); void print (int action, const char *fmt, ...);
void print_desc (char *action, char *desc); void print_desc (char *action, char *desc);
int print_result (int fail); int print_result (int fail);
void logit (int prio, const char *fmt, ...);
int start_process (char *cmd, char *args[], int console); int start_process (char *cmd, char *args[], int console);
void do_sleep (unsigned int sec); void do_sleep (unsigned int sec);
int getuser (char *username, char **home); int getuser (char *username, char **home);
+20 -22
View File
@@ -34,12 +34,12 @@
#include "private.h" #include "private.h"
#include "service.h" #include "service.h"
#define ENABLE_SOCKOPT(sd, level, opt) \ #define ENABLE_SOCKOPT(sd, level, opt) \
do { \ do { \
int val = 1; \ int val = 1; \
if (setsockopt(sd, level, opt, &val, sizeof(val)) < 0) \ if (setsockopt(sd, level, opt, &val, sizeof(val)) < 0) \
FLOG_PERROR("Failed setting %s on %s service.", \ logit(LOG_CRIT, "Failed enabling %s on %s service.", \
#opt, inetd->name); \ #opt, inetd->name); \
} while (0); } while (0);
/* Peek into SOCK_DGRAM socket to figure out where an inbound packet comes from. */ /* Peek into SOCK_DGRAM socket to figure out where an inbound packet comes from. */
@@ -114,7 +114,7 @@ static int get_stdin(svc_t *svc)
/* Open new client socket from server socket */ /* Open new client socket from server socket */
stdin = accept(stdin, NULL, NULL); stdin = accept(stdin, NULL, NULL);
if (stdin < 0) { if (stdin < 0) {
FLOG_PERROR("Failed accepting inetd service %d/tcp", svc->inetd.port); logit(LOG_CRIT, "Failed accepting inetd service %d/tcp", svc->inetd.port);
return -1; return -1;
} }
@@ -126,7 +126,7 @@ static int get_stdin(svc_t *svc)
} }
if (!inetd_is_allowed(&svc->inetd, ifname)) { if (!inetd_is_allowed(&svc->inetd, ifname)) {
FLOG_INFO("Service %s on %s:%d is not allowed.", svc->inetd.name, ifname, svc->inetd.port); logit(LOG_INFO, "Service %s on %s:%d is not allowed.", svc->inetd.name, ifname, svc->inetd.port);
if (svc->inetd.type == SOCK_STREAM) if (svc->inetd.type == SOCK_STREAM)
close(stdin); close(stdin);
@@ -145,7 +145,7 @@ static void socket_cb(uev_t *UNUSED(w), void *arg, int UNUSED(events))
_d("Got event on %s socket ...", svc->cmd); _d("Got event on %s socket ...", svc->cmd);
stdin = get_stdin(svc); stdin = get_stdin(svc);
if (stdin < 0) { if (stdin < 0) {
FLOG_ERROR("%s: Unable to accept incoming connection", svc->cmd); logit(LOG_CRIT, "%s: Unable to accept incoming connection", svc->cmd);
return; return;
} }
@@ -154,13 +154,13 @@ static void socket_cb(uev_t *UNUSED(w), void *arg, int UNUSED(events))
* passing it to the inetd service, that's what is expected. * passing it to the inetd service, that's what is expected.
*/ */
if (fcntl(stdin, F_SETFL, fcntl(stdin, F_GETFL, 0) & ~O_NONBLOCK) < 0) { if (fcntl(stdin, F_SETFL, fcntl(stdin, F_GETFL, 0) & ~O_NONBLOCK) < 0) {
FLOG_ERROR("Failed disabling non-blocking on %s socket", svc->cmd); logit(LOG_CRIT, "Failed disabling non-blocking on %s socket", svc->cmd);
return; return;
} }
task = svc_new(svc->cmd, svc->inetd.next_id++, SVC_TYPE_INETD_CONN); task = svc_new(svc->cmd, svc->inetd.next_id++, SVC_TYPE_INETD_CONN);
if (!task) { if (!task) {
FLOG_ERROR("%s: Unable to allocate service for inetd client", logit(LOG_CRIT, "%s: Unable to allocate service for inetd client",
svc->cmd); svc->cmd);
return; return;
} }
@@ -208,7 +208,7 @@ int inetd_check_loop(struct sockaddr *sa, socklen_t len, char *name)
if (((const struct sockaddr_in *)sa)->sin_port == i->port) { if (((const struct sockaddr_in *)sa)->sin_port == i->port) {
getnameinfo(sa, len, pname, sizeof(pname), NULL, 0, NI_NUMERICHOST); getnameinfo(sa, len, pname, sizeof(pname), NULL, 0, NI_NUMERICHOST);
FLOG_WARN("%s/%s:%s/%s loop request REFUSED from %s", i->name, "UDP", name, "UDP", pname); logit(LOG_WARNING, "%s/%s:%s/%s loop request REFUSED from %s", i->name, "UDP", name, "UDP", pname);
return 1; return 1;
} }
} }
@@ -226,14 +226,14 @@ static int spawn_socket(inetd_t *inetd)
struct sockaddr_in s; struct sockaddr_in s;
if (!inetd->type) { if (!inetd->type) {
FLOG_ERROR("Skipping invalid inetd service %s", inetd->name); logit(LOG_CRIT, "Skipping invalid inetd service %s", inetd->name);
return -EINVAL; return -EINVAL;
} }
_d("Spawning server socket for inetd %s, type %s ...", inetd->name, inetd->type == SOCK_STREAM ? "stream" : "dgram"); _d("Spawning server socket for inetd %s, type %s ...", inetd->name, inetd->type == SOCK_STREAM ? "stream" : "dgram");
sd = socket(AF_INET, inetd->type | SOCK_NONBLOCK | SOCK_CLOEXEC, inetd->proto); sd = socket(AF_INET, inetd->type | SOCK_NONBLOCK | SOCK_CLOEXEC, inetd->proto);
if (-1 == sd) { if (-1 == sd) {
FLOG_PERROR("Failed opening inetd socket type %d proto %d", inetd->type, inetd->proto); logit(LOG_CRIT, "Failed opening inetd socket type %d proto %d", inetd->type, inetd->proto);
return -errno; return -errno;
} }
@@ -247,8 +247,8 @@ static int spawn_socket(inetd_t *inetd)
s.sin_addr.s_addr = INADDR_ANY; s.sin_addr.s_addr = INADDR_ANY;
s.sin_port = htons(inetd->port); s.sin_port = htons(inetd->port);
if (bind(sd, (struct sockaddr *)&s, len) < 0) { if (bind(sd, (struct sockaddr *)&s, len) < 0) {
FLOG_PERROR("Failed binding to port %d, maybe another %s server is already running", logit(LOG_CRIT, "Failed binding to port %d, maybe another %s server is already running?",
inetd->port, inetd->name); inetd->port, inetd->name);
close(sd); close(sd);
return -errno; return -errno;
} }
@@ -256,7 +256,7 @@ static int spawn_socket(inetd_t *inetd)
if (inetd->port) { if (inetd->port) {
if (inetd->type == SOCK_STREAM) { if (inetd->type == SOCK_STREAM) {
if (-1 == listen(sd, 10)) { if (-1 == listen(sd, 10)) {
FLOG_PERROR("Failed listening to inetd service %s", inetd->name); logit(LOG_CRIT, "Failed listening to inetd service %s", inetd->name);
close(sd); close(sd);
return -errno; return -errno;
} }
@@ -267,7 +267,7 @@ static int spawn_socket(inetd_t *inetd)
} }
if (uev_io_init(ctx, &inetd->watcher, socket_cb, inetd->svc, sd, UEV_READ)) { if (uev_io_init(ctx, &inetd->watcher, socket_cb, inetd->svc, sd, UEV_READ)) {
FLOG_PERROR("Failed setting up inetd watcher for %s", inetd->name); logit(LOG_CRIT, "Failed setting up inetd watcher for %s", inetd->name);
close(sd); close(sd);
return -errno; return -errno;
} }
@@ -293,7 +293,7 @@ int inetd_start(inetd_t *inetd)
/* Restore O_NONBLOCK for socket */ /* Restore O_NONBLOCK for socket */
if (fcntl(sd, F_SETFL, fcntl(sd, F_GETFL, 0) | O_NONBLOCK)) { if (fcntl(sd, F_SETFL, fcntl(sd, F_GETFL, 0) | O_NONBLOCK)) {
FLOG_PERROR("Cannot safely (re)start %s inetd service", inetd->svc->cmd); logit(LOG_CRIT, "Cannot safely (re)start %s inetd service", inetd->svc->cmd);
return -errno; return -errno;
} }
@@ -646,9 +646,7 @@ int inetd_new(inetd_t *inetd, char *name, char *service, char *proto, int forkin
inetd->type = SOCK_DGRAM; inetd->type = SOCK_DGRAM;
if (inetd->type == SOCK_DGRAM && inetd->forking) { if (inetd->type == SOCK_DGRAM && inetd->forking) {
FLOG_WARN("%s: 'nowait' is not applicable on UDP services, ignoring", logit(LOG_WARNING, "%s: 'nowait' is not applicable on UDP services, ignoring", svc->cmd);
svc->cmd);
inetd->forking = 0; inetd->forking = 0;
} }
+1 -1
View File
@@ -34,7 +34,7 @@ static void lost_pid(void *arg)
if (!svc) if (!svc)
return; return;
FLOG_INFO("Lost PID %u, svc %s", pid, svc->cmd); logit(LOG_INFO, "Lost PID %u, svc %s", pid, svc->cmd);
} }
static plugin_t plugin = { static plugin_t plugin = {
+1 -1
View File
@@ -685,7 +685,7 @@ void service_monitor(pid_t lost)
snprintf(pidfile, sizeof(pidfile), "%s%s.pid", _PATH_VARRUN, basename(svc->cmd)); snprintf(pidfile, sizeof(pidfile), "%s%s.pid", _PATH_VARRUN, basename(svc->cmd));
if (remove(pidfile)) { if (remove(pidfile)) {
if (errno != ENOENT) if (errno != ENOENT)
FLOG_PERROR("Failed removing service %s pidfile %s", basename(svc->cmd), pidfile); logit(LOG_CRIT, "Failed removing service %s pidfile %s", basename(svc->cmd), pidfile);
} }
/* No longer running, update books. */ /* No longer running, update books. */