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
2016-08-18 01:18:31 +02:00
parent 77561b6071
commit d670e7d3e8
7 changed files with 57 additions and 41 deletions
+3 -3
View File
@@ -125,7 +125,7 @@ void conf_parse_cond(svc_t *svc, char *cond)
ptr[i] = 0;
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;
}
@@ -212,10 +212,10 @@ void conf_parse_rlimit(char *line)
return;
fail:
FLOG_WARN("rlimit: Failed setting rlimit %s", name->name ? : "unknown");
logit(LOG_WARNING, "rlimit: Failed setting rlimit %s", name->name ? : "unknown");
return;
error:
FLOG_WARN("rlimit: parse error");
logit(LOG_WARNING, "rlimit: parse error");
}
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) {
speed = do_parse_speed(baud);
if (speed == B0) {
FLOG_ERROR("TTY %s: Invalid speed %s", tty, baud);
logit(LOG_CRIT, "TTY %s: Invalid speed %s", tty, baud);
return 1;
}
}
+28
View File
@@ -173,6 +173,34 @@ int print_result(int 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)
{
#ifdef ENABLE_STATIC
+3 -13
View File
@@ -30,19 +30,6 @@
#include <syslog.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 _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)
@@ -61,6 +48,9 @@ void procname_set (char *name, char *args[]);
void print (int action, const char *fmt, ...);
void print_desc (char *action, char *desc);
int print_result (int fail);
void logit (int prio, const char *fmt, ...);
int start_process (char *cmd, char *args[], int console);
void do_sleep (unsigned int sec);
int getuser (char *username, char **home);
+20 -22
View File
@@ -34,12 +34,12 @@
#include "private.h"
#include "service.h"
#define ENABLE_SOCKOPT(sd, level, opt) \
do { \
int val = 1; \
if (setsockopt(sd, level, opt, &val, sizeof(val)) < 0) \
FLOG_PERROR("Failed setting %s on %s service.", \
#opt, inetd->name); \
#define ENABLE_SOCKOPT(sd, level, opt) \
do { \
int val = 1; \
if (setsockopt(sd, level, opt, &val, sizeof(val)) < 0) \
logit(LOG_CRIT, "Failed enabling %s on %s service.", \
#opt, inetd->name); \
} while (0);
/* 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 */
stdin = accept(stdin, NULL, NULL);
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;
}
@@ -126,7 +126,7 @@ static int get_stdin(svc_t *svc)
}
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)
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);
stdin = get_stdin(svc);
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;
}
@@ -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.
*/
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;
}
task = svc_new(svc->cmd, svc->inetd.next_id++, SVC_TYPE_INETD_CONN);
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);
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) {
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;
}
}
@@ -226,14 +226,14 @@ static int spawn_socket(inetd_t *inetd)
struct sockaddr_in s;
if (!inetd->type) {
FLOG_ERROR("Skipping invalid inetd service %s", inetd->name);
logit(LOG_CRIT, "Skipping invalid inetd service %s", inetd->name);
return -EINVAL;
}
_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);
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;
}
@@ -247,8 +247,8 @@ static int spawn_socket(inetd_t *inetd)
s.sin_addr.s_addr = INADDR_ANY;
s.sin_port = htons(inetd->port);
if (bind(sd, (struct sockaddr *)&s, len) < 0) {
FLOG_PERROR("Failed binding to port %d, maybe another %s server is already running",
inetd->port, inetd->name);
logit(LOG_CRIT, "Failed binding to port %d, maybe another %s server is already running?",
inetd->port, inetd->name);
close(sd);
return -errno;
}
@@ -256,7 +256,7 @@ static int spawn_socket(inetd_t *inetd)
if (inetd->port) {
if (inetd->type == SOCK_STREAM) {
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);
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)) {
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);
return -errno;
}
@@ -293,7 +293,7 @@ int inetd_start(inetd_t *inetd)
/* Restore O_NONBLOCK for socket */
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;
}
@@ -646,9 +646,7 @@ int inetd_new(inetd_t *inetd, char *name, char *service, char *proto, int forkin
inetd->type = SOCK_DGRAM;
if (inetd->type == SOCK_DGRAM && inetd->forking) {
FLOG_WARN("%s: 'nowait' is not applicable on UDP services, ignoring",
svc->cmd);
logit(LOG_WARNING, "%s: 'nowait' is not applicable on UDP services, ignoring", svc->cmd);
inetd->forking = 0;
}
+1 -1
View File
@@ -34,7 +34,7 @@ static void lost_pid(void *arg)
if (!svc)
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 = {
+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));
if (remove(pidfile)) {
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. */