mirror of https://github.com/omar-polo/gmid.git
switch to the more usual log.c
This commit is contained in:
parent
58fae4ea90
commit
eae52ad493
5
Makefile
5
Makefile
|
@ -23,6 +23,7 @@ GMID_SRCS = config.c \
|
|||
fcgi.c \
|
||||
gmid.c \
|
||||
iri.c \
|
||||
log.c \
|
||||
logger.c \
|
||||
mime.c \
|
||||
proxy.c \
|
||||
|
@ -40,6 +41,7 @@ GE_SRCS = config.c \
|
|||
fcgi.c \
|
||||
ge.c \
|
||||
iri.c \
|
||||
log.c \
|
||||
logger.c \
|
||||
mime.c \
|
||||
proxy.c \
|
||||
|
@ -58,6 +60,7 @@ GG_SRCS = gg.c \
|
|||
GG_OBJS = ${GG_SRCS:.c=.o} ${COBJS}
|
||||
|
||||
SRCS = gmid.h \
|
||||
log.h \
|
||||
logger.h \
|
||||
parse.y \
|
||||
${GMID_SRCS} \
|
||||
|
@ -158,6 +161,8 @@ DISTFILES = .cirrus.yml \
|
|||
gmid.conf.5 \
|
||||
gmid.h \
|
||||
iri.c \
|
||||
log.c \
|
||||
log.h \
|
||||
logger.c \
|
||||
mime.c \
|
||||
parse.y \
|
||||
|
|
14
fcgi.c
14
fcgi.c
|
@ -20,7 +20,7 @@
|
|||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "logger.h"
|
||||
#include "log.h"
|
||||
|
||||
/*
|
||||
* Sometimes it can be useful to inspect the fastcgi traffic as
|
||||
|
@ -262,8 +262,7 @@ fcgi_read(struct bufferevent *bev, void *d)
|
|||
memcpy(&hdr, EVBUFFER_DATA(src), sizeof(hdr));
|
||||
|
||||
if (recid(&hdr) != 1) {
|
||||
log_err(NULL,
|
||||
"got invalid client id %d from fcgi backend",
|
||||
log_warnx("got invalid client id %d from fcgi backend",
|
||||
recid(&hdr));
|
||||
goto err;
|
||||
}
|
||||
|
@ -283,8 +282,8 @@ fcgi_read(struct bufferevent *bev, void *d)
|
|||
switch (hdr.type) {
|
||||
case FCGI_END_REQUEST:
|
||||
if (len != sizeof(end)) {
|
||||
log_err(NULL,
|
||||
"got invalid end request record size");
|
||||
log_warnx("got invalid end request"
|
||||
" record size");
|
||||
goto err;
|
||||
}
|
||||
bufferevent_read(bev, &end, sizeof(end));
|
||||
|
@ -305,7 +304,7 @@ fcgi_read(struct bufferevent *bev, void *d)
|
|||
break;
|
||||
|
||||
default:
|
||||
log_err(NULL, "got invalid fcgi record (type=%d)",
|
||||
log_warnx("got invalid fcgi record (type=%d)",
|
||||
hdr.type);
|
||||
goto err;
|
||||
}
|
||||
|
@ -332,8 +331,7 @@ fcgi_error(struct bufferevent *bev, short err, void *d)
|
|||
struct client *c = d;
|
||||
|
||||
if (!(err & (EVBUFFER_ERROR|EVBUFFER_EOF)))
|
||||
log_warn(NULL, "unknown event error (%x): %s",
|
||||
err, strerror(errno));
|
||||
log_warn("unknown event error (%x)", err);
|
||||
|
||||
c->type = REQUEST_DONE;
|
||||
if (c->code != 0)
|
||||
|
|
7
ge.c
7
ge.c
|
@ -26,9 +26,11 @@
|
|||
#include <libgen.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <syslog.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "logger.h"
|
||||
#include "log.h"
|
||||
|
||||
struct imsgbuf ibuf, logibuf;
|
||||
struct conf conf;
|
||||
|
@ -184,7 +186,7 @@ serve(const char *host, int port, const char *dir)
|
|||
fatal("%s", cause);
|
||||
freeaddrinfo(res0);
|
||||
|
||||
log_notice(NULL, "serving %s on port %d", dir, port);
|
||||
log_info("serving %s on port %d", dir, port);
|
||||
return server_main(NULL, sock, -1);
|
||||
}
|
||||
|
||||
|
@ -209,6 +211,8 @@ main(int argc, char **argv)
|
|||
|
||||
setlocale(LC_CTYPE, "");
|
||||
|
||||
log_init(1, LOG_DAEMON);
|
||||
log_setverbose(0);
|
||||
logger_init();
|
||||
config_init();
|
||||
|
||||
|
@ -251,7 +255,6 @@ main(int argc, char **argv)
|
|||
certs_dir = data_dir();
|
||||
|
||||
/* set up the implicit vhost and location */
|
||||
|
||||
host = xcalloc(1, sizeof(*host));
|
||||
TAILQ_INSERT_HEAD(&hosts, host, vhosts);
|
||||
|
||||
|
|
11
gmid.c
11
gmid.c
|
@ -28,8 +28,10 @@
|
|||
#include <pwd.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#include "logger.h"
|
||||
#include "log.h"
|
||||
|
||||
static const char *opts = "c:D:fhnP:Vv";
|
||||
|
||||
|
@ -154,8 +156,7 @@ drop_priv(void)
|
|||
}
|
||||
|
||||
if (getuid() == 0)
|
||||
log_warn(NULL,
|
||||
"not a good idea to run a network daemon as root");
|
||||
log_warnx("not a good idea to run a network daemon as root");
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -253,6 +254,8 @@ main(int argc, char **argv)
|
|||
|
||||
setlocale(LC_CTYPE, "");
|
||||
|
||||
/* log to stderr until daemonized */
|
||||
log_init(1, LOG_DAEMON);
|
||||
logger_init();
|
||||
config_init();
|
||||
|
||||
|
@ -308,10 +311,12 @@ main(int argc, char **argv)
|
|||
/* log to syslog */
|
||||
imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, -1, NULL, 0);
|
||||
imsg_flush(&logibuf);
|
||||
log_init(0, LOG_DAEMON);
|
||||
|
||||
if (daemon(1, 1) == -1)
|
||||
fatal("daemon");
|
||||
}
|
||||
log_setverbose(conf.verbose);
|
||||
|
||||
sock4 = make_socket(conf.port, AF_INET);
|
||||
sock6 = -1;
|
||||
|
@ -338,7 +343,7 @@ main(int argc, char **argv)
|
|||
if (!wait_signal())
|
||||
break;
|
||||
|
||||
log_info(NULL, "reloading configuration %s", config_path);
|
||||
log_info("reloading configuration %s", config_path);
|
||||
|
||||
/* close the servers */
|
||||
for (i = 0; i < conf.prefork; ++i) {
|
||||
|
|
|
@ -0,0 +1,197 @@
|
|||
/* $OpenBSD: log.c,v 1.1 2018/07/10 16:39:54 florian Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <syslog.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
static int debug;
|
||||
static int verbose;
|
||||
static const char *log_procname;
|
||||
|
||||
void
|
||||
log_init(int n_debug, int facility)
|
||||
{
|
||||
debug = n_debug;
|
||||
verbose = n_debug;
|
||||
log_procinit(getprogname());
|
||||
|
||||
if (!debug)
|
||||
openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
|
||||
|
||||
tzset();
|
||||
}
|
||||
|
||||
void
|
||||
log_procinit(const char *procname)
|
||||
{
|
||||
if (procname != NULL)
|
||||
log_procname = procname;
|
||||
}
|
||||
|
||||
void
|
||||
log_setverbose(int v)
|
||||
{
|
||||
verbose = v;
|
||||
}
|
||||
|
||||
int
|
||||
log_getverbose(void)
|
||||
{
|
||||
return (verbose);
|
||||
}
|
||||
|
||||
void
|
||||
logit(int pri, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vlog(pri, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
vlog(int pri, const char *fmt, va_list ap)
|
||||
{
|
||||
char *nfmt;
|
||||
int saved_errno = errno;
|
||||
|
||||
if (debug) {
|
||||
/* best effort in out of mem situations */
|
||||
if (asprintf(&nfmt, "%s\n", fmt) == -1) {
|
||||
vfprintf(stderr, fmt, ap);
|
||||
fprintf(stderr, "\n");
|
||||
} else {
|
||||
vfprintf(stderr, nfmt, ap);
|
||||
free(nfmt);
|
||||
}
|
||||
fflush(stderr);
|
||||
} else
|
||||
vsyslog(pri, fmt, ap);
|
||||
|
||||
errno = saved_errno;
|
||||
}
|
||||
|
||||
void
|
||||
log_warn(const char *emsg, ...)
|
||||
{
|
||||
char *nfmt;
|
||||
va_list ap;
|
||||
int saved_errno = errno;
|
||||
|
||||
/* best effort to even work in out of memory situations */
|
||||
if (emsg == NULL)
|
||||
logit(LOG_ERR, "%s", strerror(saved_errno));
|
||||
else {
|
||||
va_start(ap, emsg);
|
||||
|
||||
if (asprintf(&nfmt, "%s: %s", emsg,
|
||||
strerror(saved_errno)) == -1) {
|
||||
/* we tried it... */
|
||||
vlog(LOG_ERR, emsg, ap);
|
||||
logit(LOG_ERR, "%s", strerror(saved_errno));
|
||||
} else {
|
||||
vlog(LOG_ERR, nfmt, ap);
|
||||
free(nfmt);
|
||||
}
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
errno = saved_errno;
|
||||
}
|
||||
|
||||
void
|
||||
log_warnx(const char *emsg, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, emsg);
|
||||
vlog(LOG_ERR, emsg, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
log_info(const char *emsg, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, emsg);
|
||||
vlog(LOG_INFO, emsg, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
log_debug(const char *emsg, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
if (verbose) {
|
||||
va_start(ap, emsg);
|
||||
vlog(LOG_DEBUG, emsg, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
vfatalc(int code, const char *emsg, va_list ap)
|
||||
{
|
||||
static char s[BUFSIZ];
|
||||
const char *sep;
|
||||
|
||||
if (emsg != NULL) {
|
||||
(void)vsnprintf(s, sizeof(s), emsg, ap);
|
||||
sep = ": ";
|
||||
} else {
|
||||
s[0] = '\0';
|
||||
sep = "";
|
||||
}
|
||||
if (code)
|
||||
logit(LOG_CRIT, "fatal in %s: %s%s%s",
|
||||
log_procname, s, sep, strerror(code));
|
||||
else
|
||||
logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
|
||||
}
|
||||
|
||||
void
|
||||
fatal(const char *emsg, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, emsg);
|
||||
vfatalc(errno, emsg, ap);
|
||||
va_end(ap);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void
|
||||
fatalx(const char *emsg, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, emsg);
|
||||
vfatalc(0, emsg, ap);
|
||||
va_end(ap);
|
||||
exit(1);
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
/* $OpenBSD: log.h,v 1.2 2021/12/13 18:28:40 deraadt Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef LOG_H
|
||||
#define LOG_H
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
void log_init(int, int);
|
||||
void log_procinit(const char *);
|
||||
void log_setverbose(int);
|
||||
int log_getverbose(void);
|
||||
void log_warn(const char *, ...)
|
||||
__attribute__((__format__ (printf, 1, 2)));
|
||||
void log_warnx(const char *, ...)
|
||||
__attribute__((__format__ (printf, 1, 2)));
|
||||
void log_info(const char *, ...)
|
||||
__attribute__((__format__ (printf, 1, 2)));
|
||||
void log_debug(const char *, ...)
|
||||
__attribute__((__format__ (printf, 1, 2)));
|
||||
void logit(int, const char *, ...)
|
||||
__attribute__((__format__ (printf, 2, 3)));
|
||||
void vlog(int, const char *, va_list)
|
||||
__attribute__((__format__ (printf, 2, 0)));
|
||||
__dead void fatal(const char *, ...)
|
||||
__attribute__((__format__ (printf, 1, 2)));
|
||||
__dead void fatalx(const char *, ...)
|
||||
__attribute__((__format__ (printf, 1, 2)));
|
||||
|
||||
#endif /* LOG_H */
|
218
logger.c
218
logger.c
|
@ -31,6 +31,7 @@
|
|||
#include <time.h>
|
||||
|
||||
#include "logger.h"
|
||||
#include "log.h"
|
||||
|
||||
static struct event imsgev;
|
||||
|
||||
|
@ -48,186 +49,6 @@ static imsg_handlerfn *handlers[] = {
|
|||
[IMSG_LOG_TYPE] = handle_imsg_log_type,
|
||||
};
|
||||
|
||||
static inline void
|
||||
print_date(FILE *f)
|
||||
{
|
||||
struct tm tminfo;
|
||||
time_t t;
|
||||
char buf[20];
|
||||
|
||||
time(&t);
|
||||
strftime(buf, sizeof(buf), "%F %T",
|
||||
localtime_r(&t, &tminfo));
|
||||
fprintf(f, "[%s] ", buf);
|
||||
}
|
||||
|
||||
static inline int
|
||||
should_log(int priority)
|
||||
{
|
||||
switch (priority) {
|
||||
case LOG_ERR:
|
||||
return 1;
|
||||
case LOG_WARNING:
|
||||
return 1;
|
||||
case LOG_NOTICE:
|
||||
return conf.verbose >= 1;
|
||||
case LOG_INFO:
|
||||
return conf.verbose >= 2;
|
||||
case LOG_DEBUG:
|
||||
return conf.verbose >= 3;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void
|
||||
send_log(int type, int priority, const char *msg, size_t len)
|
||||
{
|
||||
imsg_compose(&logibuf, type, priority, 0, -1, msg, len);
|
||||
imsg_flush(&logibuf);
|
||||
}
|
||||
|
||||
static __dead void
|
||||
fatal_impl(int use_err, const char *fmt, va_list ap)
|
||||
{
|
||||
struct pollfd pfd;
|
||||
char *str, *tmp;
|
||||
int r, t, err;
|
||||
|
||||
err = errno;
|
||||
|
||||
if ((r = vasprintf(&str, fmt, ap)) != -1) {
|
||||
if (use_err &&
|
||||
(t = asprintf(&tmp, "%s: %s", str, strerror(err))) !=
|
||||
-1) {
|
||||
free(str);
|
||||
str = tmp;
|
||||
r = t;
|
||||
}
|
||||
} else
|
||||
str = NULL, r = 0;
|
||||
|
||||
send_log(IMSG_LOG, LOG_CRIT, str, r + 1);
|
||||
free(str);
|
||||
|
||||
/* wait for the logger process to shut down */
|
||||
memset(&pfd, 0, sizeof(pfd));
|
||||
pfd.fd = logibuf.fd;
|
||||
pfd.events = POLLIN;
|
||||
poll(&pfd, 1, 1000);
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void __dead
|
||||
fatal(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
fatal_impl(1, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void __dead
|
||||
fatalx(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
fatal_impl(0, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
static inline void
|
||||
vlog(int priority, struct client *c,
|
||||
const char *fmt, va_list ap)
|
||||
{
|
||||
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
|
||||
char *fmted, *s;
|
||||
size_t len;
|
||||
int ec;
|
||||
|
||||
if (!should_log(priority))
|
||||
return;
|
||||
|
||||
if (c != NULL) {
|
||||
len = sizeof(c->addr);
|
||||
ec = getnameinfo((struct sockaddr*)&c->addr, len,
|
||||
hbuf, sizeof(hbuf),
|
||||
sbuf, sizeof(sbuf),
|
||||
NI_NUMERICHOST | NI_NUMERICSERV);
|
||||
if (ec != 0)
|
||||
fatal("getnameinfo: %s", gai_strerror(ec));
|
||||
}
|
||||
|
||||
if (vasprintf(&fmted, fmt, ap) == -1)
|
||||
fatal("vasprintf: %s", strerror(errno));
|
||||
|
||||
if (c == NULL)
|
||||
ec = asprintf(&s, "internal: %s", fmted);
|
||||
else
|
||||
ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
|
||||
|
||||
if (ec < 0)
|
||||
fatal("asprintf: %s", strerror(errno));
|
||||
|
||||
send_log(IMSG_LOG, priority, s, ec+1);
|
||||
|
||||
free(fmted);
|
||||
free(s);
|
||||
}
|
||||
|
||||
void
|
||||
log_err(struct client *c, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vlog(LOG_ERR, c, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
log_warn(struct client *c, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vlog(LOG_WARNING, c, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
log_notice(struct client *c, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vlog(LOG_NOTICE, c, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
log_info(struct client *c, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vlog(LOG_INFO, c, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
log_debug(struct client *c, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vlog(LOG_DEBUG, c, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
log_request(struct client *c, char *meta, size_t l)
|
||||
{
|
||||
|
@ -277,33 +98,15 @@ log_request(struct client *c, char *meta, size_t l)
|
|||
(int)(t-meta), meta);
|
||||
if (ec < 0)
|
||||
err(1, "asprintf");
|
||||
send_log(IMSG_LOG_REQUEST, LOG_NOTICE, fmted, ec+1);
|
||||
|
||||
imsg_compose(&logibuf, IMSG_LOG_REQUEST, 0, 0, -1, fmted, ec + 1);
|
||||
imsg_flush(&logibuf);
|
||||
|
||||
free(fmted);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
do_log(int type, int priority, const char *msg)
|
||||
{
|
||||
int quit = 0;
|
||||
|
||||
if (priority == LOG_CRIT) {
|
||||
quit = 1;
|
||||
priority = LOG_ERR;
|
||||
}
|
||||
|
||||
if (log != NULL) {
|
||||
if (type != IMSG_LOG_REQUEST)
|
||||
print_date(log);
|
||||
fprintf(log, "%s\n", msg);
|
||||
} else
|
||||
syslog(LOG_DAEMON | priority, "%s", msg);
|
||||
|
||||
if (quit)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static void
|
||||
handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
|
||||
{
|
||||
|
@ -313,13 +116,15 @@ handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
|
|||
static void
|
||||
handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
|
||||
{
|
||||
int priority;
|
||||
char *msg;
|
||||
|
||||
msg = imsg->data;
|
||||
msg[datalen-1] = '\0';
|
||||
priority = imsg->hdr.peerid;
|
||||
do_log(imsg->hdr.type, priority, msg);
|
||||
|
||||
if (log != NULL)
|
||||
fprintf(log, "%s\n", msg);
|
||||
else
|
||||
syslog(LOG_DAEMON | LOG_NOTICE, "%s", msg);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -352,9 +157,6 @@ logger_main(int fd, struct imsgbuf *ibuf)
|
|||
{
|
||||
log = stderr;
|
||||
|
||||
openlog(getprogname(), LOG_NDELAY, LOG_DAEMON);
|
||||
tzset();
|
||||
|
||||
event_init();
|
||||
|
||||
event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
|
||||
|
|
16
logger.h
16
logger.h
|
@ -14,21 +14,5 @@
|
|||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
void fatal(const char *, ...)
|
||||
__attribute__((format (printf, 1, 2)))
|
||||
__attribute__((__noreturn__));
|
||||
void fatalx(const char *, ...)
|
||||
__attribute__((format (printf, 1, 2)))
|
||||
__attribute__((__noreturn__));
|
||||
|
||||
struct client;
|
||||
|
||||
#define LOG_ATTR_FMT __attribute__((format (printf, 2, 3)))
|
||||
void log_err(struct client *, const char *, ...) LOG_ATTR_FMT;
|
||||
void log_warn(struct client *, const char *, ...) LOG_ATTR_FMT;
|
||||
void log_warnx(struct client *, const char *, ...) LOG_ATTR_FMT;
|
||||
void log_notice(struct client *, const char *, ...) LOG_ATTR_FMT;
|
||||
void log_info(struct client *, const char *, ...) LOG_ATTR_FMT;
|
||||
void log_debug(struct client *, const char *, ...) LOG_ATTR_FMT;
|
||||
void log_request(struct client *, char *, size_t);
|
||||
int logger_main(int, struct imsgbuf *);
|
||||
|
|
2
mime.c
2
mime.c
|
@ -20,7 +20,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "logger.h"
|
||||
#include "log.h"
|
||||
|
||||
void
|
||||
init_mime(struct mime *mime)
|
||||
|
|
3
parse.y
3
parse.y
|
@ -886,8 +886,7 @@ pushfile(const char *name, int secret)
|
|||
nfile = xcalloc(1, sizeof(*nfile));
|
||||
nfile->name = xstrdup(name);
|
||||
if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
|
||||
log_warn(NULL, "can't open %s: %s", nfile->name,
|
||||
strerror(errno));
|
||||
log_warn("can't open %s", nfile->name);
|
||||
free(nfile->name);
|
||||
free(nfile);
|
||||
return NULL;
|
||||
|
|
10
proxy.c
10
proxy.c
|
@ -20,7 +20,7 @@
|
|||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "logger.h"
|
||||
#include "log.h"
|
||||
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
|
||||
|
@ -152,7 +152,7 @@ proxy_read(struct bufferevent *bev, void *d)
|
|||
if (hdr == NULL) {
|
||||
/* max reply + \r\n */
|
||||
if (EVBUFFER_LENGTH(src) > 1029) {
|
||||
log_warn(c, "upstream server is trying to "
|
||||
log_warnx("upstream server is trying to "
|
||||
"send a header that's too long.");
|
||||
proxy_error(bev, EVBUFFER_READ, c);
|
||||
}
|
||||
|
@ -166,7 +166,7 @@ proxy_read(struct bufferevent *bev, void *d)
|
|||
!isdigit((unsigned char)hdr[1]) ||
|
||||
!isspace((unsigned char)hdr[2])) {
|
||||
free(hdr);
|
||||
log_warn(c, "upstream server is trying to send a "
|
||||
log_warnx("upstream server is trying to send a "
|
||||
"header that's too long.");
|
||||
proxy_error(bev, EVBUFFER_READ, c);
|
||||
return;
|
||||
|
@ -176,7 +176,7 @@ proxy_read(struct bufferevent *bev, void *d)
|
|||
code = (hdr[0] - '0') * 10 + (hdr[1] - '0');
|
||||
|
||||
if (code < 10 || code >= 70) {
|
||||
log_warn(c, "upstream server is trying to send an "
|
||||
log_warnx("upstream server is trying to send an "
|
||||
"invalid reply code: %d", code);
|
||||
proxy_error(bev, EVBUFFER_READ, c);
|
||||
return;
|
||||
|
@ -284,7 +284,7 @@ proxy_handshake(int fd, short event, void *d)
|
|||
event_add(&c->proxyev, &handshake_timeout);
|
||||
return;
|
||||
case -1:
|
||||
log_warn(c, "handshake with proxy failed: %s",
|
||||
log_warnx("handshake with proxy failed: %s",
|
||||
tls_error(c->proxyctx));
|
||||
start_reply(c, PROXY_ERROR, "handshake failed");
|
||||
return;
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
*/
|
||||
|
||||
#include "gmid.h"
|
||||
#include "logger.h"
|
||||
#include "log.h"
|
||||
|
||||
#if defined(__OpenBSD__)
|
||||
|
||||
|
|
88
server.c
88
server.c
|
@ -29,6 +29,7 @@
|
|||
#include <string.h>
|
||||
|
||||
#include "logger.h"
|
||||
#include "log.h"
|
||||
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
|
||||
|
@ -321,16 +322,16 @@ check_path(struct client *c, const char *path, int *fd)
|
|||
p = ".";
|
||||
|
||||
dirfd = vhost_dirfd(c->host, path, &c->loc);
|
||||
log_debug(c, "check_path: strip=%d path=%s original=%s",
|
||||
log_debug("check_path: strip=%d path=%s original=%s",
|
||||
strip, p, path);
|
||||
if (*fd == -1 && (*fd = openat(dirfd, p, O_RDONLY)) == -1) {
|
||||
if (errno == EACCES)
|
||||
log_info(c, "can't open %s: %s", p, strerror(errno));
|
||||
log_info("can't open %s: %s", p, strerror(errno));
|
||||
return FILE_MISSING;
|
||||
}
|
||||
|
||||
if (fstat(*fd, &sb) == -1) {
|
||||
log_notice(c, "failed stat for %s: %s", path, strerror(errno));
|
||||
log_warn("fstat %s", path);
|
||||
return FILE_MISSING;
|
||||
}
|
||||
|
||||
|
@ -414,12 +415,12 @@ handle_handshake(int fd, short ev, void *d)
|
|||
#endif
|
||||
|
||||
if ((servname = tls_conn_servername(c->ctx)) == NULL) {
|
||||
log_debug(c, "handshake: missing SNI");
|
||||
log_debug("handshake: missing SNI");
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
|
||||
log_info(c, "puny_decode: %s", parse_err);
|
||||
log_info("puny_decode: %s", parse_err);
|
||||
goto err;
|
||||
}
|
||||
|
||||
|
@ -433,7 +434,7 @@ handle_handshake(int fd, short ev, void *d)
|
|||
}
|
||||
|
||||
found:
|
||||
log_debug(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
|
||||
log_debug("handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
|
||||
servname != NULL ? servname : "(null)",
|
||||
c->domain,
|
||||
h != NULL ? h->domain : "(null)");
|
||||
|
@ -580,7 +581,8 @@ static int
|
|||
proxy_socket(struct client *c, const char *host, const char *port)
|
||||
{
|
||||
struct addrinfo hints, *res, *res0;
|
||||
int r, sock;
|
||||
int r, sock, save_errno;
|
||||
const char *cause = NULL;
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
|
@ -589,7 +591,7 @@ proxy_socket(struct client *c, const char *host, const char *port)
|
|||
/* XXX: asr_run? :> */
|
||||
r = getaddrinfo(host, port, &hints, &res0);
|
||||
if (r != 0) {
|
||||
log_warn(c, "getaddrinfo(\"%s\", \"%s\"): %s",
|
||||
log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
|
||||
host, port, gai_strerror(r));
|
||||
return -1;
|
||||
}
|
||||
|
@ -597,11 +599,16 @@ proxy_socket(struct client *c, const char *host, const char *port)
|
|||
for (res = res0; res; res = res->ai_next) {
|
||||
sock = socket(res->ai_family, res->ai_socktype,
|
||||
res->ai_protocol);
|
||||
if (sock == -1)
|
||||
if (sock == -1) {
|
||||
cause = "socket";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
|
||||
cause = "connect";
|
||||
save_errno = errno;
|
||||
close(sock);
|
||||
errno = save_errno;
|
||||
sock = -1;
|
||||
continue;
|
||||
}
|
||||
|
@ -609,10 +616,10 @@ proxy_socket(struct client *c, const char *host, const char *port)
|
|||
break;
|
||||
}
|
||||
|
||||
freeaddrinfo(res0);
|
||||
|
||||
if (sock == -1)
|
||||
log_warn(c, "can't connect to %s:%s", host, port);
|
||||
log_warn("can't connect to %s:%s: %s", host, port, cause);
|
||||
|
||||
freeaddrinfo(res0);
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
@ -631,7 +638,7 @@ apply_reverse_proxy(struct client *c)
|
|||
if (p->reqca != NULL && check_matching_certificate(p->reqca, c))
|
||||
return 1;
|
||||
|
||||
log_debug(c, "opening proxy connection for %s:%s",
|
||||
log_debug("opening proxy connection for %s:%s",
|
||||
p->host, p->port);
|
||||
|
||||
if ((c->pfd = proxy_socket(c, p->host, p->port)) == -1) {
|
||||
|
@ -653,7 +660,7 @@ fcgi_open_sock(struct fcgi *f)
|
|||
int fd;
|
||||
|
||||
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
||||
log_err(NULL, "socket: %s", strerror(errno));
|
||||
log_warn("socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -662,8 +669,7 @@ fcgi_open_sock(struct fcgi *f)
|
|||
strlcpy(addr.sun_path, f->path, sizeof(addr.sun_path));
|
||||
|
||||
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
|
||||
log_warn(NULL, "failed to connect to %s: %s", f->path,
|
||||
strerror(errno));
|
||||
log_warn("failed to connect to %s", f->path);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
@ -674,8 +680,9 @@ fcgi_open_sock(struct fcgi *f)
|
|||
static int
|
||||
fcgi_open_conn(struct fcgi *f)
|
||||
{
|
||||
struct addrinfo hints, *servinfo, *p;
|
||||
int r, sock;
|
||||
struct addrinfo hints, *servinfo, *p;
|
||||
int r, sock, save_errno;
|
||||
const char *cause = NULL;
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
|
@ -683,24 +690,30 @@ fcgi_open_conn(struct fcgi *f)
|
|||
hints.ai_flags = AI_ADDRCONFIG;
|
||||
|
||||
if ((r = getaddrinfo(f->path, f->port, &hints, &servinfo)) != 0) {
|
||||
log_warn(NULL, "getaddrinfo %s:%s: %s", f->path, f->port,
|
||||
log_warnx("getaddrinfo %s:%s: %s", f->path, f->port,
|
||||
gai_strerror(r));
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (p = servinfo; p != NULL; p = p->ai_next) {
|
||||
sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
|
||||
if (sock == -1)
|
||||
if (sock == -1) {
|
||||
cause = "socket";
|
||||
continue;
|
||||
}
|
||||
if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) {
|
||||
cause = "connect";
|
||||
save_errno = errno;
|
||||
close(sock);
|
||||
errno = save_errno;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (p == NULL) {
|
||||
log_warn(NULL, "couldn't connect to %s:%s", f->path, f->port);
|
||||
log_warn("couldn't connect to %s:%s: %s", f->path, f->port,
|
||||
cause);
|
||||
sock = -1;
|
||||
}
|
||||
|
||||
|
@ -720,7 +733,7 @@ apply_fastcgi(struct client *c)
|
|||
|
||||
f = &fcgi[id];
|
||||
|
||||
log_debug(c, "opening fastcgi connection for (%s,%s)",
|
||||
log_debug("opening fastcgi connection for (%s,%s)",
|
||||
f->path, f->port);
|
||||
|
||||
if (*f->port == '\0')
|
||||
|
@ -766,7 +779,7 @@ open_dir(struct client *c)
|
|||
int dirfd, root;
|
||||
char *before_file;
|
||||
|
||||
log_debug(c, "in open_dir");
|
||||
log_debug("in open_dir");
|
||||
|
||||
root = !strcmp(c->iri.path, "/") || *c->iri.path == '\0';
|
||||
|
||||
|
@ -818,8 +831,8 @@ open_dir(struct client *c)
|
|||
root ? select_non_dotdot : select_non_dot,
|
||||
alphasort);
|
||||
if (c->dirlen == -1) {
|
||||
log_err(c, "scandir_fd(%d) (vhost:%s) %s: %s",
|
||||
c->pfd, c->host->domain, c->iri.path, strerror(errno));
|
||||
log_warn("scandir_fd(%d) (vhost:%s) %s",
|
||||
c->pfd, c->host->domain, c->iri.path);
|
||||
start_reply(c, TEMP_FAILURE, "internal server error");
|
||||
return;
|
||||
}
|
||||
|
@ -959,7 +972,7 @@ retry:
|
|||
event_add(&bufev->ev_write, NULL);
|
||||
return;
|
||||
err:
|
||||
log_err(client, "tls error: %s", tls_error(client->ctx));
|
||||
log_warnx("tls error: %s", tls_error(client->ctx));
|
||||
(*bufev->errorcb)(bufev, what, bufev->cbarg);
|
||||
}
|
||||
|
||||
|
@ -984,7 +997,7 @@ client_read(struct bufferevent *bev, void *d)
|
|||
|
||||
/* max url len + \r\n */
|
||||
if (EVBUFFER_LENGTH(src) > 1024 + 2) {
|
||||
log_err(c, "too much data received");
|
||||
log_debug("too much data received");
|
||||
start_reply(c, BAD_REQUEST, "bad request");
|
||||
return;
|
||||
}
|
||||
|
@ -997,14 +1010,14 @@ client_read(struct bufferevent *bev, void *d)
|
|||
}
|
||||
c->reqlen = strlen(c->req);
|
||||
if (c->reqlen > 1024+2) {
|
||||
log_err(c, "URL too long");
|
||||
log_debug("URL too long");
|
||||
start_reply(c, BAD_REQUEST, "bad request");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!parse_iri(c->req, &c->iri, &parse_err) ||
|
||||
!puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
|
||||
log_err(c, "IRI parse error: %s", parse_err);
|
||||
log_debug("IRI parse error: %s", parse_err);
|
||||
start_reply(c, BAD_REQUEST, "bad request");
|
||||
return;
|
||||
}
|
||||
|
@ -1046,7 +1059,7 @@ client_write(struct bufferevent *bev, void *d)
|
|||
|
||||
case REQUEST_FILE:
|
||||
if ((r = read(c->pfd, buf, sizeof(buf))) == -1) {
|
||||
log_warn(c, "read: %s", strerror(errno));
|
||||
log_warn("read");
|
||||
client_error(bev, EVBUFFER_ERROR, c);
|
||||
return;
|
||||
} else if (r == 0) {
|
||||
|
@ -1100,8 +1113,7 @@ client_error(struct bufferevent *bev, short error, void *d)
|
|||
c->type = REQUEST_DONE;
|
||||
|
||||
if (error & EVBUFFER_TIMEOUT) {
|
||||
log_warn(c, "timeout reached, "
|
||||
"forcefully closing the connection");
|
||||
log_debug("timeout; forcefully closing the connection");
|
||||
if (c->code == 0)
|
||||
start_reply(c, BAD_REQUEST, "timeout");
|
||||
else
|
||||
|
@ -1114,7 +1126,7 @@ client_error(struct bufferevent *bev, short error, void *d)
|
|||
return;
|
||||
}
|
||||
|
||||
log_err(c, "unknown bufferevent error %x", error);
|
||||
log_warnx("unknown bufferevent error 0x%x", error);
|
||||
client_close(c);
|
||||
}
|
||||
|
||||
|
@ -1160,13 +1172,13 @@ start_reply(struct client *c, int code, const char *meta)
|
|||
return;
|
||||
|
||||
err:
|
||||
log_err(c, "evbuffer_add_printf error: no memory");
|
||||
log_warnx("evbuffer_add_printf error: no memory");
|
||||
evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
|
||||
client_close(c);
|
||||
return;
|
||||
|
||||
overflow:
|
||||
log_warn(c, "reply header overflow");
|
||||
log_warnx("reply header overflow");
|
||||
evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
|
||||
start_reply(c, TEMP_FAILURE, "internal error");
|
||||
}
|
||||
|
@ -1296,7 +1308,7 @@ do_accept(int sock, short et, void *d)
|
|||
c->addr = addr;
|
||||
|
||||
if (tls_accept_socket(ctx, &c->ctx, fd) == -1) {
|
||||
log_warn(c, "failed to accept socket: %s", tls_error(c->ctx));
|
||||
log_warnx("failed to accept socket: %s", tls_error(c->ctx));
|
||||
close(c->fd);
|
||||
free(c);
|
||||
return;
|
||||
|
@ -1364,7 +1376,7 @@ handle_dispatch_imsg(int fd, short ev, void *d)
|
|||
static void
|
||||
handle_siginfo(int fd, short ev, void *d)
|
||||
{
|
||||
log_info(NULL, "%d connected clients", connected_clients);
|
||||
log_info("%d connected clients", connected_clients);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -1441,7 +1453,7 @@ setup_tls(void)
|
|||
|
||||
h = TAILQ_FIRST(&hosts);
|
||||
|
||||
log_warn(NULL, "loading %s, %s, %s", h->cert, h->key, h->ocsp);
|
||||
log_info("loading %s, %s, %s", h->cert, h->key, h->ocsp);
|
||||
|
||||
/* we need to set something, then we can add how many key we want */
|
||||
if (tls_config_set_keypair_file(tlsconf, h->cert, h->key))
|
||||
|
|
7
utils.c
7
utils.c
|
@ -24,7 +24,7 @@
|
|||
#include <openssl/x509_vfy.h>
|
||||
#include <openssl/x509v3.h>
|
||||
|
||||
#include "logger.h"
|
||||
#include "log.h"
|
||||
|
||||
int
|
||||
starts_with(const char *str, const char *prefix)
|
||||
|
@ -119,8 +119,7 @@ gen_certificate(const char *hostname, const char *certpath, const char *keypath)
|
|||
FILE *f;
|
||||
const unsigned char *host = (const unsigned char*)hostname;
|
||||
|
||||
log_notice(NULL,
|
||||
"generating new certificate for %s (it could take a while)",
|
||||
log_info("generating new certificate for %s (it could take a while)",
|
||||
host);
|
||||
|
||||
if ((pkey = EVP_PKEY_new()) == NULL)
|
||||
|
@ -174,7 +173,7 @@ gen_certificate(const char *hostname, const char *certpath, const char *keypath)
|
|||
X509_free(x509);
|
||||
RSA_free(rsa);
|
||||
|
||||
log_notice(NULL, "certificate successfully generated");
|
||||
log_info("certificate successfully generated");
|
||||
}
|
||||
|
||||
X509_STORE *
|
||||
|
|
Loading…
Reference in New Issue