Add syslog facility. - geomyidae - A small C-based gopherd.
git clone git://bitreich.org/geomyidae/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/geomyidae/
Log
Files
Refs
Tags
README
LICENSE
---
commit 147e063355b1560f40584b7ddb11f8831e350753
parent 1c649c8667e849d887d2946f7666fd60752a2570
Author: Christoph Lohmann <20h@r-36.net>
Date:   Wed, 17 Mar 2021 22:06:01 +0100

Add syslog facility.

Thanks to escapeinsert  for proposing this!

Diffstat:
  M geomyidae.8                         |       9 +++++++--
  M main.c                              |      46 +++++++++++++++++++++----------

2 files changed, 39 insertions(+), 16 deletions(-)
---
diff --git a/geomyidae.8 b/geomyidae.8
@@ -1,6 +1,6 @@
 .\" geomyidae.8 handcrafted in GNU groff -mdoc using nvi
 .\"
-.Dd February 23, 2020
+.Dd March 17, 2021
 .Dt GEOMYIDAE 8
 .Os
 .
@@ -17,6 +17,7 @@
 .Op Fl d
 .Op Fl e
 .Op Fl n
+.Op Fl s
 .Op Fl l Ar logfile
 .Op Fl v Ar loglevel
 .Op Fl b Ar base
@@ -79,7 +80,8 @@ values if set.  See
 .Ic OPTIONS
 below for specifics.  Launching geomyidae automatically is best done via a UNIX
 run-time (rc.d) script; several sample rc.d scripts are included in the geomyidae
-source archive.
+source archive. Logging in geomyidae can be done through either logfiles
+or syslog.
 .
 .Sh OPTIONS
 geomyidae options and default settings:
@@ -108,6 +110,9 @@ Disable execution of any CGI or DCGI script.
 .It Fl n
 Don't perform reverse lookups.
 .
+.It Fl s
+Log using syslog for logging.
+.
 .It Fl l Ar logfile
 Specify file where log output is written (no default).
 .
diff --git a/main.c b/main.c
@@ -26,6 +26,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #ifdef ENABLE_TLS
 #include 
@@ -46,12 +47,14 @@ enum {
 };
 
 int glfd = -1;
+int dosyslog = 0;
 int loglvl = 47;
-int *listfds = NULL;
-int nlistfds = 0;
 int revlookup = 1;
 char *logfile = NULL;
 
+int *listfds = NULL;
+int nlistfds = 0;
+
 char *argv0;
 char stdbase[] = "/var/gopher";
 char *stdport = "70";
@@ -104,15 +107,17 @@ logentry(char *host, char *port, char *qry, char *status)
         struct tm *ptr;
         char timstr[128], *ahost;
 
-        if (glfd >= 0) {
-                tim = time(0);
-                ptr = gmtime(&tim);
-
+        if (glfd >= 0 || dosyslog) {
                 ahost = revlookup ? reverselookup(host) : host;
-                strftime(timstr, sizeof(timstr), "%F %T %z", ptr);
-
-                dprintf(glfd, "[%s|%s|%s|%s] %s\n",
-                        timstr, ahost, port, status, qry);
+                if (dosyslog) {
+                        syslog("[%s|%s|%s] %s\n", ahost, port, status, qry);
+                } else {
+                        tim = time(0);
+                        ptr = gmtime(&tim);
+                        strftime(timstr, sizeof(timstr), "%F %T %z", ptr);
+                        dprintf(glfd, "[%s|%s|%s|%s] %s\n",
+                                timstr, ahost, port, status, qry);
+                }
                 if (revlookup)
                         free(ahost);
         }
@@ -284,7 +289,9 @@ sighandler(int sig)
         case SIGABRT:
         case SIGTERM:
         case SIGKILL:
-                if (logfile != NULL && glfd != -1) {
+                if (dosyslog) {
+                        closelog();
+                } else if (logfile != NULL && glfd != -1) {
                         close(glfd);
                         glfd = -1;
                 }
@@ -402,7 +409,7 @@ getlistenfd(struct addrinfo *hints, char *bindip, char *port, int *rlfdnum)
 void
 usage(void)
 {
-        dprintf(2, "usage: %s [-46cden] [-l logfile] "
+        dprintf(2, "usage: %s [-46cdens] [-l logfile] "
 #ifdef ENABLE_TLS
                    "[-t keyfile certfile] "
 #endif /* ENABLE_TLS */
@@ -490,6 +497,9 @@ main(int argc, char *argv[])
                 if (sport == NULL)
                         sport = port;
                 break;
+        case 's':
+                dosyslog = 1;
+                break;
 #ifdef ENABLE_TLS
         case 't':
                 dotls = 1;
@@ -590,7 +600,13 @@ main(int argc, char *argv[])
                 }
         }
 
-        if (logfile != NULL) {
+        if (dosyslog) {
+                if (!dofork) {
+                        openlog("geomyidae", LOG_CONS|LOG_PERROR, LOG_DAEMON|LOG_INFO);
+                } else {
+                        openlog("geomyidae", LOG_NDELAY|LOG_PID, LOG_DAEMON|LOG_INFO);
+                }
+        } else if (logfile != NULL) {
                 glfd = open(logfile, O_APPEND | O_WRONLY | O_CREAT, 0644);
                 if (glfd < 0) {
                         perror("log");
@@ -906,7 +922,9 @@ main(int argc, char *argv[])
                 close(sock);
         }
 
-        if (logfile != NULL && glfd != -1) {
+        if (dosyslog) {
+                closelog();
+        } else if (logfile != NULL && glfd != -1) {
                 close(glfd);
                 glfd = -1;
         }