add user-specified syslog priority setting

This commit is contained in:
Babak Farrokhi 2015-11-07 13:08:04 +03:30
parent 2bd53dde07
commit 6a037612d8

59
fsipd.c
View File

@ -46,6 +46,7 @@
#include <string.h> #include <string.h>
#include <sysexits.h> #include <sysexits.h>
#include <unistd.h> #include <unistd.h>
#define SYSLOG_NAMES
#include <syslog.h> #include <syslog.h>
#include <pthread.h> #include <pthread.h>
#include <pidutil.h> #include <pidutil.h>
@ -65,7 +66,7 @@
log_t *lfh; log_t *lfh;
struct pidfh *pfh; struct pidfh *pfh;
bool use_syslog = false; bool use_syslog = false;
int syslog_pri; int syslog_pri = -1;
struct sockaddr_in t_sa, u_sa; struct sockaddr_in t_sa, u_sa;
int t_sockfd, u_sockfd; int t_sockfd, u_sockfd;
@ -391,8 +392,8 @@ init_logger()
{ {
if (use_syslog) { if (use_syslog) {
/* initialize facility and level parameters */ /* initialize facility and level parameters */
/* todo: should be configurable from command line */ if (syslog_pri == -1) /* not specidied by user, use default */
syslog_pri = LOG_LOCAL0 | LOG_NOTICE; syslog_pri = LOG_USER | LOG_NOTICE | LOG_PID;
} else { } else {
/* open a log file in current directory */ /* open a log file in current directory */
/* todo: filename should be configurable from command line */ /* todo: filename should be configurable from command line */
@ -502,9 +503,48 @@ daemon_start()
void void
usage() usage()
{ {
printf("usage: fsipd [-h] [-s] \n"); printf("usage: fsipd [-h] [-s] [-p priority] \n");
printf("\t-h: this message\n"); printf("\t-h: this message\n");
printf("\t-s: use syslog (local0.notice) instead of local log file\n"); printf("\t-s: use syslog instead of local log file\n");
printf("\t-p: syslog priotiry (default: user.notice)\n");
}
static int
decode(char *name, const CODE * codetab)
{
const CODE *c;
if (isdigit(*name))
return (atoi(name));
for (c = codetab; c->c_name; c++)
if (!strcasecmp(name, c->c_name))
return (c->c_val);
return (-1);
}
static int
decodepri(char *s)
{
char *save;
int fac, lev;
for (save = s; *s && *s != '.'; ++s);
if (*s) {
*s = '\0';
fac = decode(save, facilitynames);
if (fac < 0)
errx(1, "unknown facility name: %s", save);
*s++ = '.';
} else {
fac = 0;
s = save;
}
lev = decode(s, prioritynames);
if (lev < 0)
errx(1, "unknown priority name: %s", save);
return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
} }
int int
@ -512,11 +552,18 @@ main(int argc, char *argv[])
{ {
int opt; int opt;
while ((opt = getopt(argc, argv, "hs")) != -1) { while ((opt = getopt(argc, argv, "hsp:")) != -1) {
switch (opt) { switch (opt) {
case 's': case 's':
use_syslog = true; use_syslog = true;
break; break;
case 'p':
if (use_syslog) {
syslog_pri = decodepri(optarg) | LOG_PID;
} else {
errx(EX_USAGE, "you need to specify \"-s\".");
}
break;
case 'h': case 'h':
usage(); usage();
exit(0); exit(0);