add basic syslog support

This commit is contained in:
Babak Farrokhi 2015-11-07 12:23:42 +03:30
parent c2f04b5a43
commit 2bd53dde07

42
fsipd.c
View File

@ -65,6 +65,7 @@
log_t *lfh; log_t *lfh;
struct pidfh *pfh; struct pidfh *pfh;
bool use_syslog = false; bool use_syslog = false;
int syslog_pri;
struct sockaddr_in t_sa, u_sa; struct sockaddr_in t_sa, u_sa;
int t_sockfd, u_sockfd; int t_sockfd, u_sockfd;
@ -109,6 +110,7 @@ void
daemon_shutdown() daemon_shutdown()
{ {
pidfile_remove(pfh); pidfile_remove(pfh);
if (!use_syslog)
log_close(lfh); log_close(lfh);
} }
@ -121,7 +123,9 @@ signal_handler(int sig)
switch (sig) { switch (sig) {
case SIGHUP: case SIGHUP:
log_reopen(&lfh); /* necessary for log file rotation */ if (!use_syslog)
log_reopen(&lfh); /* necessary for log file
* rotation */
break; break;
case SIGINT: case SIGINT:
case SIGTERM: case SIGTERM:
@ -163,23 +167,34 @@ process_request(int af, struct sockaddr *src, int proto, char *str)
chomp(str); chomp(str);
/* todo: add optional syslog support */
#ifdef PF_INET6 #ifdef PF_INET6
switch (af) { switch (af) {
case AF_INET6: case AF_INET6:
s_in6 = (struct sockaddr_in6 *)src; s_in6 = (struct sockaddr_in6 *)src;
inet_ntop(af, &s_in6->sin6_addr, addr_str, sizeof(addr_str)); inet_ntop(af, &s_in6->sin6_addr, addr_str, sizeof(addr_str));
port = ntohs(s_in6->sin6_port); port = ntohs(s_in6->sin6_port);
if (use_syslog) {
syslog(syslog_pri, "From: %s:%d (%s6) - Message: \"%s\"", addr_str, port, pname, str);
} else {
log_printf(lfh, "%ld,%s6,%s,%d,\"%s\"", time(NULL), pname, addr_str, port, str); log_printf(lfh, "%ld,%s6,%s,%d,\"%s\"", time(NULL), pname, addr_str, port, str);
}
break; break;
case AF_INET: case AF_INET:
s_in = (struct sockaddr_in *)src; s_in = (struct sockaddr_in *)src;
if (use_syslog) {
syslog(syslog_pri, "From: %s:%d (%s4) - Message: \"%s\"", inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), pname, str);
} else {
log_printf(lfh, "%ld,%s4,%s,%d,\"%s\"", time(NULL), pname, inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), str); log_printf(lfh, "%ld,%s4,%s,%d,\"%s\"", time(NULL), pname, inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), str);
}
break; break;
} }
#else #else
s_in = (struct sockaddr_in *)src; s_in = (struct sockaddr_in *)src;
if (use_syslog) {
syslog(syslog_pri, "From: %s:%d (%s4) - Message: \"%s\"", inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), pname, str);
} else {
log_printf(lfh, "%ld,%s4,%s,%d,\"%s\"", time(NULL), pname, inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), str); log_printf(lfh, "%ld,%s4,%s,%d,\"%s\"", time(NULL), pname, inet_ntoa(s_in->sin_addr), ntohs(s_in->sin_port), str);
}
#endif #endif
} }
@ -371,6 +386,21 @@ udp6_handler(void *args)
#endif /* PF_INET6 */ #endif /* PF_INET6 */
void
init_logger()
{
if (use_syslog) {
/* initialize facility and level parameters */
/* todo: should be configurable from command line */
syslog_pri = LOG_LOCAL0 | LOG_NOTICE;
} else {
/* open a log file in current directory */
/* todo: filename should be configurable from command line */
if ((lfh = log_open("fsipd.log", 0644)) == NULL)
err(EXIT_FAILURE, "Cannot open log file");
}
}
/* /*
* Daemonize and persist pid * Daemonize and persist pid
*/ */
@ -393,10 +423,8 @@ daemon_start()
} }
err(EXIT_FAILURE, "Cannot open or create pidfile"); err(EXIT_FAILURE, "Cannot open or create pidfile");
} }
/* open a log file in current directory */ init_logger();
if ((lfh = log_open("fsipd.log", 0644)) == NULL) {
err(EXIT_FAILURE, "Cannot open log file");
}
/* Initialize TCP46 and UDP46 sockets */ /* Initialize TCP46 and UDP46 sockets */
if (init_tcp() == EXIT_FAILURE) if (init_tcp() == EXIT_FAILURE)
return (EXIT_FAILURE); return (EXIT_FAILURE);
@ -476,7 +504,7 @@ usage()
{ {
printf("usage: fsipd [-h] [-s] \n"); printf("usage: fsipd [-h] [-s] \n");
printf("\t-h: this message\n"); printf("\t-h: this message\n");
printf("\t-s: use syslog instead of local log file\n"); printf("\t-s: use syslog (local0.notice) instead of local log file\n");
} }
int int