waffle

user and group backend daemon
git clone git://git.2f30.org/waffle.git
Log | Files | Refs | LICENSE

commit e6cd53daf70d8eb853c37afb28741d64f4e4933e
parent 3ac84a94abb4c09224d899139228a790d18a9f1f
Author: sin <sin@2f30.org>
Date:   Sat Mar  7 21:17:04 +0000

Add daemonization code

Diffstat:
Makefile | 1+
req.c | 4----
util.c | 30++++++++++++++++++++++++++++++
waffle.c | 34+++++++++++++++++++++++++---------
waffle.h | 8++++++--
5 files changed, 62 insertions(+), 15 deletions(-)
diff --git a/Makefile b/Makefile @@ -10,6 +10,7 @@ OBJ =\ backend.o\ dummy.o\ req.o\ + util.o\ waffle.o BIN = waffle diff --git a/req.c b/req.c @@ -186,9 +186,5 @@ process_req(int clifd, struct nscdreq *req, char *key) else return replygr(clifd, NULL); } - - if (debug) - printf("Unhandled request type %ld\n", - (long)req->type); return -1; } diff --git a/util.c b/util.c @@ -0,0 +1,30 @@ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <syslog.h> +#include "waffle.h" + +void +logwarn(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + if (daemonize == 1) + vsyslog(LOG_WARNING, fmt, ap); + else + vfprintf(stderr, fmt, ap); + va_end(ap); +} + +void +logerr(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + if (daemonize == 1) + vsyslog(LOG_ERR, fmt, ap); + else + vfprintf(stderr, fmt, ap); + va_end(ap); + exit(1); +} diff --git a/waffle.c b/waffle.c @@ -3,19 +3,20 @@ #include <sys/socket.h> #include <sys/types.h> #include <sys/un.h> -#include <err.h> #include <errno.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <syslog.h> #include <unistd.h> #include "arg.h" #include "waffle.h" #define SOCKPATH "/var/run/nscd/socket" +#define PIDFILE "/var/run/waffle.pid" -int debug = 0; +int daemonize = 1; char *argv0; static fd_set master; static int fdmax; @@ -29,7 +30,7 @@ serv_listen(const char *name) fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd < 0) - err(1, "socket"); + logerr("socket: %s\n", strerror(errno)); unlink(name); @@ -40,11 +41,11 @@ serv_listen(const char *name) len = sizeof(sun); r = bind(fd, (struct sockaddr *)&sun, len); if (r < 0) - err(1, "bind %s", name); + logerr("bind %s: %s\n", name, strerror(errno)); r = listen(fd, 5); if (r < 0) - err(1, "listen"); + logerr("listen: %s\n", strerror(errno)); return fd; } @@ -59,7 +60,7 @@ serv_accept(int listenfd) len = sizeof(sun); clifd = accept(listenfd, (struct sockaddr *)&sun, &len); if (clifd < 0) - err(1, "accept"); + logerr("accept: %s\n", strerror(errno)); return clifd; } @@ -97,6 +98,7 @@ usage(void) int main(int argc, char *argv[]) { + FILE *fp; sigset_t mask; struct signalfd_siginfo si; fd_set rfds; @@ -106,7 +108,7 @@ main(int argc, char *argv[]) ARGBEGIN { case 'd': - debug = 1; + daemonize = 0; break; default: usage(); @@ -118,6 +120,16 @@ main(int argc, char *argv[]) sigaddset(&mask, SIGINT); sigprocmask(SIG_BLOCK, &mask, NULL); + if (daemonize) { + openlog(argv[0], LOG_CONS | LOG_PID, LOG_DAEMON); + if (daemon(0, 0) < 0) + logerr("daemon: %s\n", strerror(errno)); + if ((fp = fopen(PIDFILE, "w"))) { + fprintf(fp, "%d\n", getpid()); + fclose(fp); + } + } + listenfd = serv_listen(SOCKPATH); FD_ZERO(&master); FD_ZERO(&rfds); @@ -126,7 +138,7 @@ main(int argc, char *argv[]) sfd = signalfd(-1, &mask, 0); if (sfd < 0) - err(1, "signalfd"); + logerr("signalfd: %s\n", strerror(errno)); FD_SET(sfd, &master); if (sfd > fdmax) fdmax = sfd; @@ -142,7 +154,7 @@ main(int argc, char *argv[]) if (n < 0) { if (errno == EINTR) continue; - err(1, "select"); + logerr("select: %s\n", strerror(errno)); } for (i = 0; i <= fdmax; i++) { if (!FD_ISSET(i, &rfds)) @@ -168,5 +180,9 @@ main(int argc, char *argv[]) } out: backends_term(); + if (daemonize) { + unlink(PIDFILE); + closelog(); + } exit(0); } diff --git a/waffle.h b/waffle.h @@ -14,8 +14,8 @@ struct backend_ops { int (*grbygid)(gid_t, struct group *); }; -/* nscd.c */ -extern int debug; +/* waffle.c */ +extern int daemonize; /* dummy.c */ extern struct backend_ops dummy_ops; @@ -30,3 +30,7 @@ int backends_pwbyname(const char *, struct passwd *); int backends_pwbyuid(const char *, struct passwd *); int backends_grbyname(const char *, struct group *); int backends_grbygid(const char *, struct group *); + +/* util.c */ +void logwarn(const char *, ...); +void logerr(const char *, ...);