Add support for logger -u /tmp/foo to log to alternate socket

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2019-10-29 10:43:14 +01:00
parent 08aa942ffb
commit ec9c92987f
3 changed files with 16 additions and 2 deletions

View File

@ -205,9 +205,10 @@ int main(int argc, char *argv[])
int rotate = 0; int rotate = 0;
off_t size = 200 * 1024; off_t size = 200 * 1024;
char *ident = NULL, *logfile = NULL; char *ident = NULL, *logfile = NULL;
char *sockpath = NULL;
char buf[512] = ""; char buf[512] = "";
while ((c = getopt(argc, argv, "?f:p:r:st:v")) != EOF) { while ((c = getopt(argc, argv, "?f:p:r:st:u:v")) != EOF) {
switch (c) { switch (c) {
case 'f': case 'f':
logfile = optarg; logfile = optarg;
@ -230,6 +231,10 @@ int main(int argc, char *argv[])
ident = optarg; ident = optarg;
break; break;
case 'u':
sockpath = optarg;
break;
case 'v': /* version */ case 'v': /* version */
fprintf(stderr, "%s\n", version_info); fprintf(stderr, "%s\n", version_info);
return 0; return 0;
@ -267,6 +272,10 @@ int main(int argc, char *argv[])
} }
log.log_file = fileno(fp); log.log_file = fileno(fp);
} else if (sockpath) {
if (access(sockpath, W_OK))
err(1, "Socket path %s", sockpath);
log.log_sockpath = sockpath;
} }
openlog_r(ident, log_opts, facility, &log); openlog_r(ident, log_opts, facility, &log);

View File

@ -483,7 +483,7 @@ static void
connectlog_r(struct syslog_data *data) connectlog_r(struct syslog_data *data)
{ {
/* AF_UNIX address of local logger */ /* AF_UNIX address of local logger */
static const struct sockaddr_un sun = { static struct sockaddr_un sun = {
.sun_family = AF_LOCAL, .sun_family = AF_LOCAL,
#ifdef HAVE_SA_LEN #ifdef HAVE_SA_LEN
.sun_len = sizeof(sun), .sun_len = sizeof(sun),
@ -491,6 +491,9 @@ connectlog_r(struct syslog_data *data)
.sun_path = _PATH_LOG, .sun_path = _PATH_LOG,
}; };
if (data->log_sockpath && !access(data->log_sockpath, W_OK))
strlcpy(sun.sun_path, data->log_sockpath, sizeof(sun.sun_path));
if (data->log_file == -1 || fcntl(data->log_file, F_GETFL, 0) == -1) { if (data->log_file == -1 || fcntl(data->log_file, F_GETFL, 0) == -1) {
data->log_file = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0); data->log_file = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (data->log_file == -1) if (data->log_file == -1)

View File

@ -195,6 +195,7 @@ struct syslog_data {
int log_opened; int log_opened;
int log_stat; int log_stat;
const char *log_tag; const char *log_tag;
const char *log_sockpath; /* Path to socket */
char log_hostname[256]; /* MAXHOSTNAMELEN */ char log_hostname[256]; /* MAXHOSTNAMELEN */
int log_fac; int log_fac;
int log_mask; int log_mask;
@ -207,6 +208,7 @@ struct syslog_data {
.log_opened = 0, \ .log_opened = 0, \
.log_stat = 0, \ .log_stat = 0, \
.log_tag = 0, \ .log_tag = 0, \
.log_sockpath = NULL, \
.log_hostname = { '\0' }, \ .log_hostname = { '\0' }, \
.log_fac = LOG_USER, \ .log_fac = LOG_USER, \
.log_mask = 0xff, \ .log_mask = 0xff, \