Skip to content

Commit

Permalink
Added basic syslog support (everything is LOG_INFO) and console detac…
Browse files Browse the repository at this point in the history
…hing.
  • Loading branch information
darkk committed Feb 9, 2008
1 parent fab9a38 commit 2a58763
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 12 deletions.
64 changes: 59 additions & 5 deletions base.c
Expand Up @@ -7,6 +7,7 @@
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <stdlib.h>
#include "config.h"
#if defined USE_IPTABLES
# include <limits.h>
Expand All @@ -33,6 +34,8 @@ typedef struct base_instance_t {
char *group;
char *redirector_name;
redirector_subsys *redirector;
char *log_name;
bool daemon;
} base_instance;

static base_instance instance = {
Expand Down Expand Up @@ -212,6 +215,8 @@ static parser_entry base_entries[] =
{ .key = "user", .type = pt_pchar, .addr = &instance.user },
{ .key = "group", .type = pt_pchar, .addr = &instance.group },
{ .key = "redirector", .type = pt_pchar, .addr = &instance.redirector_name },
{ .key = "log", .type = pt_pchar, .addr = &instance.log_name },
{ .key = "daemon", .type = pt_bool, .addr = &instance.daemon },
{ }
};

Expand Down Expand Up @@ -269,6 +274,7 @@ static int base_init()
{
uid_t uid;
gid_t gid;
int devnull = -1;

if (!instance.configured) {
log_error("there is no configured instance of `base`, check config file");
Expand Down Expand Up @@ -296,13 +302,34 @@ static int base_init()
gid = gr->gr_gid;
}

if (log_preopen(
instance.log_name ? instance.log_name : instance.daemon ? "syslog:daemon" : "stderr"
) < 0 ) {
goto fail;
}

if (instance.daemon) {
devnull = open("/dev/null", O_RDWR);
if (devnull == -1) {
log_errno("open(\"/dev/null\", O_RDWR");
goto fail;
}
}

if (instance.chroot) {
if (chroot(instance.chroot) < 0) {
log_errno("chroot(%s)", instance.chroot);
goto fail;
}
}

if (instance.daemon || instance.chroot) {
if (chdir("/") < 0) {
log_errno("chdir(\"/\")");
goto fail;
}
}

if (instance.group) {
if (setgid(gid) < 0) {
log_errno("setgid(%i)", gid);
Expand All @@ -317,14 +344,41 @@ static int base_init()
}
}

#if 0
if (daemon(0, 0) < 0) {
log_errno("daemon()");
goto fail;
if (instance.daemon) {
switch (fork()) {
case -1: // error
log_errno("fork()");
goto fail;
case 0: // child
break;
default: // parent, pid is returned
exit(EXIT_SUCCESS);
}
}

log_open(); // child has nothing to do with TTY

if (instance.daemon) {
if (setsid() < 0) {
log_errno("setsid()");
goto fail;
}

int fds[] = { STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO };
int *pfd;
FOREACH(pfd, fds)
if (dup2(devnull, *pfd) < 0) {
log_errno("dup2(devnull, %i)", *pfd);
goto fail;
}

close(devnull);
devnull = -1;
}
#endif
return 0;
fail:
if (devnull != -1)
close(devnull);
if (instance.redirector->fini)
instance.redirector->fini();
return -1;
Expand Down
81 changes: 74 additions & 7 deletions log.c
Expand Up @@ -6,10 +6,83 @@
#include <sys/time.h>
#include <sys/types.h>
#include <event.h>
#include "utils.h"
#include "log.h"

static const char *lowmem = "<Can't print error, not enough memory>";

typedef void (*log_func)(const char *file, int line, const char *func, const char *message, const char *appendix);

static void stderr_msg(const char *file, int line, const char *func, const char *message, const char *appendix)
{
struct timeval tv = { };
gettimeofday(&tv, 0);

if (appendix)
fprintf(stderr, "%lu.%6.6lu %s:%u %s(...) %s: %s\n", tv.tv_sec, tv.tv_usec, file, line, func, message, appendix);
else
fprintf(stderr, "%lu.%6.6lu %s:%u %s(...) %s\n", tv.tv_sec, tv.tv_usec, file, line, func, message);
}

static void syslog_msg(const char *file, int line, const char *func, const char *message, const char *appendix)
{
if (appendix)
syslog(LOG_INFO, "%s: %s\n", message, appendix);
else
syslog(LOG_INFO, "%s\n", message);
}

static log_func log_msg = stderr_msg;
static log_func log_msg_next = NULL;

int log_preopen(const char *dst)
{
const char *syslog_prefix = "syslog:";
if (strcmp(dst, "stderr") == 0) {
log_msg_next = stderr_msg;
}
else if (strncmp(dst, syslog_prefix, strlen(syslog_prefix)) == 0) {
const char *facility_name = dst + strlen(syslog_prefix);
int facility = -1;
struct {
char *name; int value;
} *ptpl, tpl[] = {
{ "daemon", LOG_DAEMON },
{ "local0", LOG_LOCAL0 },
{ "local1", LOG_LOCAL1 },
{ "local2", LOG_LOCAL2 },
{ "local3", LOG_LOCAL3 },
{ "local4", LOG_LOCAL4 },
{ "local5", LOG_LOCAL5 },
{ "local6", LOG_LOCAL6 },
{ "local7", LOG_LOCAL7 },
};
FOREACH(ptpl, tpl)
if (strcmp(facility_name, ptpl->name) == 0) {
facility = ptpl->value;
break;
}
if (facility == -1) {
log_error("log_preopen(%s, ...): unknown syslog facility");
return -1;
}

openlog("redsocks", LOG_NDELAY | LOG_PID, facility);
log_msg_next = syslog_msg;
}
else {
log_error("log_preopen(%s, ...): unknown destination", dst);
return -1;
}
return 0;
}

void log_open()
{
log_msg = log_msg_next;
log_msg_next = NULL;
}

void _log_vwrite(const char *file, int line, const char *func, int do_errno, const char *fmt, va_list ap)
{
int saved_errno = errno;
Expand All @@ -23,13 +96,7 @@ void _log_vwrite(const char *file, int line, const char *func, int do_errno, con
else
message = lowmem;

struct timeval tv = { };
gettimeofday(&tv, 0);

if (do_errno)
fprintf(stderr, "%lu.%6.6lu %s:%u %s(...) %s: %s\n", tv.tv_sec, tv.tv_usec, file, line, func, message, strerror(saved_errno));
else
fprintf(stderr, "%lu.%6.6lu %s:%u %s(...) %s\n", tv.tv_sec, tv.tv_usec, file, line, func, message);
log_msg(file, line, func, message, do_errno ? strerror(saved_errno) : NULL);

if (buff)
evbuffer_free(buff);
Expand Down
5 changes: 5 additions & 0 deletions log.h
Expand Up @@ -2,10 +2,15 @@
#define LOG_H_WED_JAN_24_18_21_27_2007
/* $Id$ */
#include <stdarg.h>
#include <stdbool.h>
#include <syslog.h>

#define log_errno(msg...) _log_write(__FILE__, __LINE__, __func__, 1, ## msg)
#define log_error(msg...) _log_write(__FILE__, __LINE__, __func__, 0, ## msg)

int log_preopen(const char *dst);
void log_open();

void _log_vwrite(const char *file, int line, const char *func, int do_errno, const char *fmt, va_list ap);
void _log_write(const char *file, int line, const char *func, int do_errno, const char *fmt, ...);

Expand Down

0 comments on commit 2a58763

Please sign in to comment.