Skip to contentSkip to conversation

sanitize logging subsystem

PATCH In review Denis Vlasenko 3 participants 5 messages Mar 11, 2005 – Mar 19, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sanitize logging subsystem

It seems like there are several ways to log messages or display them on std{out,err} in nfs utils, more or less similar to each other. Let's try to untangle them.

I'll start from cleaning up xlog.c:

* There is no external API to log to arbitrary FILE*, we can drop code which supports this internally. Patch converts xlog.c to directly use stderr.
* Print date/time in ISO (YYYY-MM-DD HH:MM:SS) format.
* Add support for disabling logging to syslog.

Patch does not introduce behavioral changes except for making foregrounded mountd to really use stderr. There was a bug which prevented this.

Patch is tested. Please comment/apply.
--
vda

support/include/xlog.h +5 -1
diff -urpN nfs-utils-1.0.7.orig/support/include/xlog.h nfs-utils-1.0.7.logging1/support/include/xlog.h
@@ -7,12 +7,15 @@
 #ifndef XLOG_H
 #define XLOG_H
 
+/* These are logged always. L_FATAL also does exit(1) */
 #define L_FATAL		0x0100
 #define L_ERROR		0x0200
 #define L_WARNING	0x0400
 #define L_NOTICE	0x0800
 #define L_ALL		0xFF00
 
+/* These are logged if enabled with xlog_[s]config */
+/* NB: code does not expect ORing together D_ and L_ */
 #define D_GENERAL	0x0001		/* general debug info */
 #define D_CALL		0x0002
 #define D_AUTH		0x0004
@@ -31,7 +34,8 @@ struct xlog_debugfac {
 };
 
 void			xlog_open(char *progname);
-void			xlog_background(void);
+void			xlog_stderr(int on);
+void			xlog_syslog(int on);
 void			xlog_config(int fac, int on);
 void			xlog_sconfig(char *, int on);
 int			xlog_enabled(int fac);
support/nfs/xlog.c +45 -44

134 lines not on this page · raw message

utils/mountd/mountd.c +2 -2
diff -urpN nfs-utils-1.0.7.orig/utils/mountd/mountd.c nfs-utils-1.0.7.logging1/utils/mountd/mountd.c
@@ -531,7 +531,8 @@ main(int argc, char **argv)
 		}
 	}
 	/* Initialize logging. */
-/*	xlog_open("mountd"); */
+	if (!foreground) xlog_stderr(0);
+	xlog_open("mountd");
 
 	sa.sa_handler = SIG_IGN;
 	sa.sa_flags = 0;
@@ -589,7 +590,6 @@ main(int argc, char **argv)
 			if (fd > 2) (void) close(fd);
 		}
 		setsid();
-		xlog_background();
 	}
 
 	my_svc_run();
#1 lore raw
Quoted 12 lines · On Friday March 11, [email protected] wrote:

It seems like there are several ways to log messages or display them on std{out,err} in nfs utils, more or less similar to each other. Let's try to untangle them.

I'll start from cleaning up xlog.c:

* There is no external API to log to arbitrary FILE*, we can drop code which supports this internally. Patch converts xlog.c to directly use stderr.
* Print date/time in ISO (YYYY-MM-DD HH:MM:SS) format.
* Add support for disabling logging to syslog.

Quoted 5 lines

Patch does not introduce behavioral changes except for making foregrounded mountd to really use stderr. There was a bug which prevented this.

Patch is tested. Please comment/apply.

How well was it tested :-)

support/nfs/xlog.c xlog(int kind, const char *fmt, ...) · line 180

Quoted 5 lines
support/nfs/xlog.c +5 -0
+		fprintf(stderr, "%s[%d] %04d-%02d-%02d %02d:%02d:%02d %s\n",
+				log_name, log_pid,
+				tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
+				tm->tm_hour, tm->tm_min, tm->tm_sec,
+				buff);

tm_year is "number of years since 1900", which isn't what you want to print out. It needs a "+1900", which I have given it.

Otherwise the patch looks reasonably sane.
The only bit that bothers me is calling openlog in mountd where it wasn't called before.
mountd (on debian at least) currently prefixes all log lines with "rpc_mountd" as that is the program name. With this patch they will be prefixes with "mountd".
This is a change and people who filter their log file (like me) might
notice. But it probably isn't a big concern..

I've committed it to CVS.

Thanks,
NeilBrown

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#2 lore raw

PATCH 1/1 sanitize logging subsystem #2

Quoted 6 lines
Quoted 5 lines

Patch does not introduce behavioral changes except for making foregrounded mountd to really use stderr. There was a bug which prevented this.

Patch is tested. Please comment/apply.

Quoted 7 lines

How well was it tested :-)

Review

Quoted 5 lines
(quoted diff) +5 -0
+		fprintf(stderr, "%s[%d] %04d-%02d-%02d %02d:%02d:%02d %s\n",
+				log_name, log_pid,
+				tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
+				tm->tm_hour, tm->tm_min, tm->tm_sec,
+				buff);

Good spotting.
Totally overlooked because this thing normally is NOT compiled in. It sits in #ifdef VERBOSE_PRINTF. VERBOSE_PRINTF is #undef'ed.

Quoted 12 lines

tm_year is "number of years since 1900", which isn't what you want to print out. It needs a "+1900", which I have given it.

Otherwise the patch looks reasonably sane.
The only bit that bothers me is calling openlog in mountd where it wasn't called before.
mountd (on debian at least) currently prefixes all log lines with "rpc_mountd" as that is the program name. With this patch they will be prefixes with "mountd".
This is a change and people who filter their log file (like me) might
notice. But it probably isn't a big concern..

Quoted 1 line

I've committed it to CVS.

Your concern is indeed valid:

        /* Initialize logging. */
        if (!foreground) xlog_stderr(0);
        xlog_open("mountd");
...
        if (!foreground) {
                int fd = sysconf (_SC_OPEN_MAX);
                while (--fd > 2)
                        (void) close(fd);
        }

This could close a fd to syslog socket!
If it indeed happens, just move xlog_open below fd closing loop. However, you may opt to just omit xlog_open(). I will test this scenario.

Meanwhile, here are two patches to be applied on top of first one:

nfs-utils-1.0.7_log12.diff:

* Remove unneded '\n' from xlog messages. They were discarded by xlog anyway.
* Remove inconsistently used trailing periods and the like.

nfs-utils-1.0.7_log23.diff:

* Convert statd to use xlog. Kill log.[ch] from statd/ subdir.
* Change verbose logging in xlog.c to match one used by statd.
NB: by default stderr logging no longer includes timestamp and PID. #define VERBOSE_PRINTF in xlog.h to revert to previous behaviour. This will make mountd and statd logging output formats uniform.
* Remove trailing '\n' and periods from messages. Convert %m to %s+strerror(errno)

Comments?
--
vda

support/include/ha-callout.h +1 -1
diff -urpN nfs-utils-1.0.7.logging1/support/include/ha-callout.h nfs-utils-1.0.7.logging2/support/include/ha-callout.h
@@ -56,7 +56,7 @@ ha_callout(char *event, char *arg1, char
 #ifdef dprintf
 	dprintf(N_DEBUG, "ha callout returned %d\n", WEXITSTATUS(ret));
 #else
-	xlog(D_GENERAL, "ha callout returned %d\n", WEXITSTATUS(ret));
+	xlog(D_GENERAL, "ha callout returned %d", WEXITSTATUS(ret));
 #endif
 }
 
support/nfs/exports.c +5 -5
diff -urpN nfs-utils-1.0.7.logging1/support/nfs/exports.c nfs-utils-1.0.7.logging2/support/nfs/exports.c
@@ -384,7 +384,7 @@ parseopts(char *cp, struct exportent *ep
 			char *oe;
 			ep->e_anonuid = strtol(opt+8, &oe, 10);
 			if (opt[8]=='\0' || *oe != '\0') {
-				xlog(L_ERROR, "%s: %d: bad anonuid \"%s\"\n",
+				xlog(L_ERROR, "%s: %d: bad anonuid \"%s\"",
 				     flname, flline, opt);	
 bad_option:
 				free(opt);
@@ -395,7 +395,7 @@ bad_option:
 			char *oe;
 			ep->e_anongid = strtol(opt+8, &oe, 10);
 			if (opt[8]=='\0' || *oe != '\0') {
-				xlog(L_ERROR, "%s: %d: bad anongid \"%s\"\n",
+				xlog(L_ERROR, "%s: %d: bad anongid \"%s\"",
 				     flname, flline, opt);	
 				goto bad_option;
 			}
@@ -411,7 +411,7 @@ bad_option:
 			char *oe;
 			ep->e_fsid = strtoul(opt+5, &oe, 0);
 			if (opt[5]=='\0' || *oe != '\0') {
-				xlog(L_ERROR, "%s: %d: bad fsid \"%s\"\n",
+				xlog(L_ERROR, "%s: %d: bad fsid \"%s\"",
 				     flname, flline, opt);	
 				goto bad_option;
 			}
@@ -426,7 +426,7 @@ bad_option:
 			else
 				ep->e_mountpoint = strdup("");
 		} else {
-			xlog(L_ERROR, "%s:%d: unknown keyword \"%s\"\n",
+			xlog(L_ERROR, "%s:%d: unknown keyword \"%s\"",
 					flname, flline, opt);
 			ep->e_flags |= NFSEXP_ALLSQUASH | NFSEXP_READONLY;
 			goto bad_option;
@@ -445,7 +445,7 @@ out:
 	if (warn && !had_sync_opt)
 		xlog(L_WARNING, "%s [%d]: No 'sync' or 'async' option specified for export \"%s:%s\".\n"
 				"  Assuming default behaviour ('sync').\n"
-		     		"  NOTE: this default has changed from previous versions\n",
+		     		"  NOTE: this default has changed from previous versions",
 
 				flname, flline,
 				ep->e_hostname, ep->e_path);
support/nfs/keytab.c +1 -1
diff -urpN nfs-utils-1.0.7.logging1/support/nfs/keytab.c nfs-utils-1.0.7.logging2/support/nfs/keytab.c
@@ -38,7 +38,7 @@ fsetnfskeyent(char *fname, char *type)
 	FILE	*fp;
 
 	if ((fp = fopen(fname, type)) == NULL)
-		xlog(L_ERROR, "can't open %s for %sing\n",
+		xlog(L_ERROR, "can't open %s for %sing",
 				fname, type[0] == 'r'? "read" : "writ");
 	return fp;
 #else
support/nfs/rpcmisc.c +7 -7
diff -urpN nfs-utils-1.0.7.logging1/support/nfs/rpcmisc.c nfs-utils-1.0.7.logging2/support/nfs/rpcmisc.c
@@ -75,7 +75,7 @@ rpc_init(char *name, int prog, int vers,
 			if (defport == 0)
 				sock = RPC_ANYSOCK;
 			else if ((sock = makesock(defport, IPPROTO_UDP)) < 0) {
-				xlog(L_FATAL, "%s: cannot make a UDP socket\n",
+				xlog(L_FATAL, "%s: cannot make a UDP socket",
 						name);
 			}
 		}
@@ -105,7 +105,7 @@ rpc_init(char *name, int prog, int vers,
 			if (defport == 0)
 				sock = RPC_ANYSOCK;
 			else if ((sock = makesock(defport, IPPROTO_TCP)) < 0) {
-				xlog(L_FATAL, "%s: cannot make a TCP socket\n",
+				xlog(L_FATAL, "%s: cannot make a TCP socket",
 						name);
 			}
 		}
@@ -161,7 +161,7 @@ int makesock(int port, int proto)
 	sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
 	s = socket(AF_INET, sock_type, proto);
 	if (s < 0) {
-		xlog(L_FATAL, "Could not make a socket: %s\n",
+		xlog(L_FATAL, "Could not make a socket: %s",
 					strerror(errno));
 		return (-1);
 	}
@@ -172,7 +172,7 @@ int makesock(int port, int proto)
 
 	val = 1;
 	if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
-		xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
+		xlog(L_ERROR, "setsockopt failed: %s", strerror(errno));
 
 #if 0
 	/* I was told it didn't work with gigabit ethernet.
@@ -185,13 +185,13 @@ int makesock(int port, int proto)
 		sblen = rblen = socksz + 1024;
 		if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
 		    setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
-			xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
+			xlog(L_ERROR, "setsockopt failed: %s", strerror(errno));
 	}
 #endif				/* SO_SNDBUF */
 #endif
 
 	if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
-		xlog(L_FATAL, "Could not bind name to socket: %s\n",
+		xlog(L_FATAL, "Could not bind name to socket: %s",
 					strerror(errno));
 		return (-1);
 	}
@@ -253,5 +253,5 @@ rpc_logcall(struct svc_req *rqstp, char 
 	default:
 		sprintf(sp, "CRED %d", rqstp->rq_cred.oa_flavor);
 	}
-	xlog(D_CALL, "%s [%s]\n\t%s\n", xname, buff, arg);
+	xlog(D_CALL, "%s [%s]\n\t%s", xname, buff, arg);
 }
support/nfs/xlog.c +1 -1
diff -urpN nfs-utils-1.0.7.logging1/support/nfs/xlog.c nfs-utils-1.0.7.logging2/support/nfs/xlog.c
@@ -114,7 +114,7 @@ xlog_sconfig(char *kind, int on)
 	while (tbl->df_name != NULL && strcasecmp(tbl->df_name, kind)) 
 		tbl++;
 	if (!tbl->df_name) {
-		xlog (L_WARNING, "Invalid debug facility: %s\n", kind);
+		xlog(L_WARNING, "Invalid debug facility: %s", kind);
 		return;
 	}
 	xlog_config(tbl->df_fac, on);
utils/mountd/mountd.c +9 -9
diff -urpN nfs-utils-1.0.7.logging1/utils/mountd/mountd.c nfs-utils-1.0.7.logging2/utils/mountd/mountd.c
@@ -65,13 +65,13 @@ static int nfs_version = -1;
 static void 
 killer (int sig)
 {
-  if (nfs_version & 0x1)
-    pmap_unset (MOUNTPROG, MOUNTVERS);
-  if (nfs_version & (0x1 << 1))
-    pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
-  if (nfs_version & (0x1 << 2))
-    pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
-  xlog (L_FATAL, "Caught signal %d, un-registering and exiting.", sig);
+	if (nfs_version & 0x1)
+		pmap_unset (MOUNTPROG, MOUNTVERS);
+	if (nfs_version & (0x1 << 1))
+		pmap_unset (MOUNTPROG, MOUNTVERS_POSIX);
+	if (nfs_version & (0x1 << 2))
+		pmap_unset (MOUNTPROG, MOUNTVERS_NFSV3);
+	xlog (L_FATAL, "Caught signal %d, un-registering and exiting", sig);
 }
 
 bool_t
@@ -577,7 +577,7 @@ main(int argc, char **argv)
 		if ((c = fork()) > 0)
 			exit(0);
 		if (c < 0) {
-			xlog(L_FATAL, "mountd: cannot fork: %s\n",
+			xlog(L_FATAL, "mountd: cannot fork: %s",
 						strerror(errno));
 		}
 		/* Now we remove ourselves from the foreground.
@@ -594,7 +594,7 @@ main(int argc, char **argv)
 
 	my_svc_run();
 
-	xlog(L_ERROR, "Ack! Gack! svc_run returned!\n");
+	xlog(L_ERROR, "Ack! Gack! svc_run returned!");
 	exit(1);
 }
 
support/nfs/xlog.c +1 -1
diff -urpN nfs-utils-1.0.7.logging2/support/nfs/xlog.c nfs-utils-1.0.7.logging3/support/nfs/xlog.c
@@ -173,7 +173,7 @@ xlog(int kind, const char *fmt, ...)
 
 		time(&now);
 		tm = localtime(&now);
-		fprintf(stderr, "%s[%d] %04d-%02d-%02d %02d:%02d:%02d %s\n",
+		fprintf(stderr, "%04d-%02d-%02d %02d:%02d:%02d %s[%d]: %s\n",
 				log_name, log_pid,
 				tm->tm_year+1900, tm->tm_mon + 1, tm->tm_mday,
 				tm->tm_hour, tm->tm_min, tm->tm_sec,
utils/statd/Makefile +1 -1
diff -urpN nfs-utils-1.0.7.logging2/utils/statd/Makefile nfs-utils-1.0.7.logging3/utils/statd/Makefile
@@ -19,7 +19,7 @@ CCOPTS	= $(DEBUG) $(SIMUL)
 LIBS	= -lexport -lnfs -lmisc $(LIBWRAP) $(LIBNSL)
 
 SRCS	= $(RPCSRCS) $(SIMSRCS) \
-	  callback.c notlist.c log.c misc.c monitor.c notify.c simu.c \
+	  callback.c notlist.c misc.c monitor.c notify.c simu.c \
 	  stat.c statd.c state.c svc_run.c rmtcall.c
 HDRS	= $(RPCHDRS) $(SIMHDRS)
 
utils/statd/callback.c +3 -2
diff -urpN nfs-utils-1.0.7.logging2/utils/statd/callback.c nfs-utils-1.0.7.logging3/utils/statd/callback.c
@@ -9,6 +9,7 @@
 #include "config.h"
 #include "statd.h"
 #include "notlist.h"
+#include "xlog.h"
 
 /* Callback notify list. */
 /* notify_list *cbnl = NULL; ... never used */
@@ -26,14 +27,14 @@ sm_notify_1_svc(struct stat_chge *argp, 
 	notify_list    *lp, *call;
 	static char    *result = NULL;
 
-	dprintf(N_DEBUG, "Received SM_NOTIFY from %s, state: %d",
+	xlog(D_GENERAL, "Received SM_NOTIFY from %s, state: %d",
 				argp->mon_name, argp->state);
 
 	/* quick check - don't bother if we're not monitoring anyone */
 	/* LH - this was != MULL, meaning that if anyone _was_ in our RTNL,
 	 * we'd never pass this point. */
 	if (!(lp = rtnl)) {
-		note(N_WARNING, "SM_NOTIFY from %s while not monitoring any hosts.",
+		xlog(L_WARNING, "SM_NOTIFY from %s while not monitoring any hosts",
 				argp->mon_name, argp->state);
 		return ((void *) &result);
 	}
utils/statd/log.c +0 -91

91 lines not on this page · raw message

utils/statd/log.h +0 -42

42 lines not on this page · raw message

utils/statd/misc.c +6 -5
diff -urpN nfs-utils-1.0.7.logging2/utils/statd/misc.c nfs-utils-1.0.7.logging3/utils/statd/misc.c
@@ -14,6 +14,7 @@
 #include <unistd.h>
 #include "statd.h"
 #include "notlist.h"
+#include "xlog.h"
 
 /*
  * Error-checking malloc() wrapper.
@@ -28,7 +29,7 @@ xmalloc (size_t size)
 
   if (!(ptr = malloc (size)))
     /* SHIT!  SHIT!  SHIT! */
-    die ("malloc failed");
+    xlog(L_FATAL, "malloc failed");
 
   return (ptr);
 }
@@ -44,7 +45,7 @@ xstrdup (const char *string)
 
   /* Will only fail if underlying malloc() fails (ENOMEM). */
   if (!(result = strdup (string)))
-    die ("strdup failed");
+    xlog(L_FATAL, "strdup failed");
 
   return (result);
 }
@@ -64,10 +65,10 @@ xunlink (char *path, char *host, short i
 
   if (!check || !nlist_gethost(rtnl, host, 0)) {
     if (unlink (tozap) == -1)
-      note (N_ERROR, "unlink (%s): %s", tozap, strerror (errno));
+      xlog (L_ERROR, "unlink (%s): %s", tozap, strerror (errno));
     else
-      dprintf (N_DEBUG, "Unlinked %s", tozap);
+      xlog (D_GENERAL, "Unlinked %s", tozap);
   }
   else
-    dprintf (N_DEBUG, "Not unlinking %s--host still monitored.", tozap);
+    xlog (D_GENERAL, "Not unlinking %s - host still monitored", tozap);
 }
utils/statd/monitor.c +24 -22

157 lines not on this page · raw message

utils/statd/notify.c +3 -2
diff -urpN nfs-utils-1.0.7.logging2/utils/statd/notify.c nfs-utils-1.0.7.logging3/utils/statd/notify.c
@@ -17,6 +17,7 @@
 #include <netdb.h>
 #include <string.h>
 #include <unistd.h>
+#include "xlog.h"
 #include "misc.h"
 #include "statd.h"
 #include "notlist.h"
@@ -54,10 +55,10 @@ notify_hosts(void)
 		 || matchhostname(de->d_name, "localhost")) {
 			char *fname;
 			fname=xmalloc(strlen(SM_BAK_DIR)+sizeof(de->d_name)+2);
-			dprintf(N_DEBUG, "We're on our own notify list?!?");
+			xlog(D_GENERAL, "We're on our own notify list?!?");
 			sprintf(fname, "%s/%s",  SM_BAK_DIR, de->d_name);
 			if (unlink(fname)) 
-				note(N_ERROR, "unlink(%s): %s", 
+				xlog(L_ERROR, "unlink(%s): %s", 
 					fname, strerror(errno));
 			free(fname);
 			continue;
utils/statd/rmtcall.c +30 -29

189 lines not on this page · raw message

utils/statd/simu.c +2 -1
diff -urpN nfs-utils-1.0.7.logging2/utils/statd/simu.c nfs-utils-1.0.7.logging3/utils/statd/simu.c
@@ -4,6 +4,7 @@
  * NSM for Linux.
  */
 
+#include "xlog.h"
 #include "config.h"
 #include "statd.h"
 #include "notlist.h"
@@ -19,7 +20,7 @@ sm_simu_crash_1_svc (void *argp, struct 
 {
   static char *result = NULL;
 
-  note (N_WARNING, "*** SIMULATING CRASH! ***");
+  xlog (L_WARNING, "*** SIMULATING CRASH! ***");
   my_svc_exit ();
 
   if (rtnl)
utils/statd/simulate.c +22 -23

139 lines not on this page · raw message

utils/statd/stat.c +4 -3
diff -urpN nfs-utils-1.0.7.logging2/utils/statd/stat.c nfs-utils-1.0.7.logging3/utils/statd/stat.c
@@ -5,6 +5,7 @@
  * NSM for Linux.
  */
 
+#include "xlog.h"
 #include "config.h"
 #include <netdb.h>
 #include "statd.h"
@@ -25,12 +26,12 @@ sm_stat_1_svc (struct sm_name *argp, str
   static sm_stat_res result;
 
   if (gethostbyname (argp->mon_name) == NULL) {
-    note (N_WARNING, "gethostbyname error for %s", argp->mon_name);
+    xlog (L_WARNING, "gethostbyname error for %s", argp->mon_name);
     result.res_stat = STAT_FAIL;
-    dprintf (N_DEBUG, "STAT_FAIL for %s", argp->mon_name);
+    xlog (D_GENERAL, "STAT_FAIL for %s", argp->mon_name);
   } else {
     result.res_stat = STAT_SUCC;
-    dprintf (N_DEBUG, "STAT_SUCC for %s", argp->mon_name);
+    xlog (D_GENERAL, "STAT_SUCC for %s", argp->mon_name);
   }
   result.state = MY_STATE;
   return(&result);
utils/statd/statd.c +31 -30

136 lines not on this page · raw message

utils/statd/statd.h +0 -8
diff -urpN nfs-utils-1.0.7.logging2/utils/statd/statd.h nfs-utils-1.0.7.logging3/utils/statd/statd.h
@@ -8,7 +8,6 @@
 #include "config.h"
 #include "sm_inter.h"
 #include "system.h"
-#include "log.h"
 
 /*
  * Paths and filenames.
@@ -78,12 +77,5 @@ extern int run_mode;
 /* LH - notify_only mode would be for notifying hosts on an IP alias
  * that just came back up, for ex, when failing over a HA service to
  * another host.... */
-
-/*
- * Program name and version pointers -- See statd.c for the reasoning
- * as to why they're global.
- */
-extern char *name_p;		/* program basename */
-extern char *version_p;		/* program version */
 
 extern int		re_notify; /* time to re-read notify list */
utils/statd/state.c +20 -19
diff -urpN nfs-utils-1.0.7.logging2/utils/statd/state.c nfs-utils-1.0.7.logging3/utils/statd/state.c
@@ -14,6 +14,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include "xlog.h"
 #include "statd.h"
 
 
@@ -28,43 +29,43 @@ change_state (void)
   extern short int restart;
   
   if ((fd = open (SM_STAT_PATH, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) == -1)
-    die ("open (%s): %s", SM_STAT_PATH, strerror (errno));
+    xlog(L_FATAL, "open (%s): %s", SM_STAT_PATH, strerror (errno));
 
   if ((size = read (fd, &MY_STATE, sizeof MY_STATE)) == -1)
-    die ("read (%s): %s", SM_STAT_PATH, strerror (errno));
+    xlog(L_FATAL, "read (%s): %s", SM_STAT_PATH, strerror (errno));
 
   if (size != 0 && size != sizeof MY_STATE) {
-    note (N_ERROR, "Error in status file format...correcting.");
+    xlog (L_ERROR, "Error in status file format...correcting");
 
     if (close (fd) == -1)
-      die ("close (%s): %s", SM_STAT_PATH, strerror (errno));
+      xlog(L_FATAL, "close (%s): %s", SM_STAT_PATH, strerror (errno));
 
     if ((fd = creat (SM_STAT_PATH, S_IRUSR | S_IWUSR)) == -1)
-      die ("creat (%s): %s", SM_STAT_PATH, strerror (errno));
+      xlog(L_FATAL, "creat (%s): %s", SM_STAT_PATH, strerror (errno));
   }
-  note (N_DEBUG, "New state: %u", (++MY_STATE % 2) ? MY_STATE : ++MY_STATE);
+  xlog (D_GENERAL, "New state: %u", (++MY_STATE % 2) ? MY_STATE : ++MY_STATE);
 
   if (lseek (fd, 0, SEEK_SET) == -1)
-    die ("lseek (%s): %s", SM_STAT_PATH, strerror (errno));
+    xlog(L_FATAL, "lseek (%s): %s", SM_STAT_PATH, strerror (errno));
 
   if (write (fd, &MY_STATE, sizeof MY_STATE) != sizeof MY_STATE)
-    die ("write (%s): %s", SM_STAT_PATH, strerror (errno));
+    xlog(L_FATAL, "write (%s): %s", SM_STAT_PATH, strerror (errno));
 
   if (fsync (fd) == -1)
-    note (N_ERROR, "fsync (%s): %s", SM_STAT_PATH, strerror (errno));
+    xlog (L_ERROR, "fsync (%s): %s", SM_STAT_PATH, strerror (errno));
 
   if (close (fd) == -1)
-    note (N_ERROR, "close (%s): %s", SM_STAT_PATH, strerror (errno));
+    xlog (L_ERROR, "close (%s): %s", SM_STAT_PATH, strerror (errno));
 
   if (MY_NAME == NULL) {
     char fullhost[SM_MAXSTRLEN + 1];
     struct hostent *hostinfo;
 
     if (gethostname (fullhost, SM_MAXSTRLEN) == -1)
-      die ("gethostname: %s", strerror (errno));
+      xlog(L_FATAL, "gethostname: %s", strerror (errno));
 
     if ((hostinfo = gethostbyname (fullhost)) == NULL)
-      note (N_ERROR, "gethostbyname error for %s", fullhost);
+      xlog (L_ERROR, "gethostbyname error for %s", fullhost);
     else {
       strncpy (fullhost, hostinfo->h_name, sizeof (fullhost) - 1);
       fullhost[sizeof (fullhost) - 1] = '\0';
@@ -88,23 +89,23 @@ shuffle_dirs (void)
   int len1, len2, len;
   
   if (stat (SM_DIR, &st) == -1 && errno != ENOENT)
-    die ("stat (%s): %s", SM_DIR, strerror (errno));
+    xlog(L_FATAL, "stat (%s): %s", SM_DIR, strerror (errno));
 
   if (!S_ISDIR (st.st_mode))
     if (mkdir (SM_DIR, S_IRWXU) == -1)
-      die ("mkdir (%s): %s", SM_DIR, strerror (errno));
+      xlog(L_FATAL, "mkdir (%s): %s", SM_DIR, strerror (errno));
 
   memset (&st, 0, sizeof st);
 
   if (stat (SM_BAK_DIR, &st) == -1 && errno != ENOENT)
-    die ("stat (%s): %s", SM_BAK_DIR, strerror (errno));
+    xlog(L_FATAL, "stat (%s): %s", SM_BAK_DIR, strerror (errno));
 
   if (!S_ISDIR (st.st_mode))
     if (mkdir (SM_BAK_DIR, S_IRWXU) == -1)
-      die ("mkdir (%s): %s", SM_BAK_DIR, strerror (errno));
+      xlog(L_FATAL, "mkdir (%s): %s", SM_BAK_DIR, strerror (errno));
 
   if (!(nld = opendir (SM_DIR)))
-    die ("opendir (%s): %s", SM_DIR, strerror (errno));
+    xlog(L_FATAL, "opendir (%s): %s", SM_DIR, strerror (errno));
 
   len1=strlen(SM_DIR);
   len2=strlen(SM_BAK_DIR);
@@ -117,10 +118,10 @@ shuffle_dirs (void)
     sprintf (src, "%s/%s", SM_DIR, de->d_name);
     sprintf (dst, "%s/%s", SM_BAK_DIR, de->d_name);
     if (rename (src, dst) == -1)
-      die ("rename (%s to %s): %s", SM_DIR, SM_BAK_DIR, strerror (errno));
+      xlog(L_FATAL, "rename (%s to %s): %s", SM_DIR, SM_BAK_DIR, strerror (errno));
     free(src);
     free(dst);
   }
   if (closedir (nld) == -1)
-    note (N_ERROR, "closedir (%s): %s", SM_DIR, strerror (errno));
+    xlog (L_ERROR, "closedir (%s): %s", SM_DIR, strerror (errno));
 }
utils/statd/svc_run.c +5 -4
diff -urpN nfs-utils-1.0.7.logging2/utils/statd/svc_run.c nfs-utils-1.0.7.logging3/utils/statd/svc_run.c
@@ -50,6 +50,7 @@
 #include "config.h"
 #include <errno.h>
 #include <time.h>
+#include "xlog.h"
 #include "statd.h"
 #include "notlist.h"
 
@@ -89,7 +90,7 @@ my_svc_run(void)
 			return;
 		if (re_notify) {
 			change_state();
-			dprintf(N_DEBUG, "Notifying...(new state %d)",
+			xlog(D_GENERAL, "Notifying...(new state %d)",
 								MY_STATE);
 			notify_hosts();
 			re_notify = 0;
@@ -106,14 +107,14 @@ my_svc_run(void)
 
 			tv.tv_sec  = NL_WHEN(notify) - now;
 			tv.tv_usec = 0;
-			dprintf(N_DEBUG, "Waiting for reply... (timeo %d)",
+			xlog(D_GENERAL, "Waiting for reply... (timeo %d)",
 							tv.tv_sec);
 			selret = select(FD_SETSIZE, &readfds,
 				(void *) 0, (void *) 0, &tv);
 		} else if (run_mode & MODE_NOTIFY_ONLY)
 			return;
 		else {
-			dprintf(N_DEBUG, "Waiting for client connections.");
+			xlog(D_GENERAL, "Waiting for client connections");
 			selret = select(FD_SETSIZE, &readfds,
 				(void *) 0, (void *) 0, (struct timeval *) 0);
 		}
@@ -123,7 +124,7 @@ my_svc_run(void)
 			if (errno == EINTR || errno == ECONNREFUSED
 			 || errno == ENETUNREACH || errno == EHOSTUNREACH)
 				continue;
-			note(N_ERROR, "my_svc_run() - select: %m");
+			xlog(L_ERROR, "my_svc_run() - select: %s", strerror(errno));
 			return;
 
 		case 0:
#3 lore raw

Re: [PATCH] sanitize logging subsystem #2

Quoted 23 lines · Denis Vlasenko
Quoted 10 lines

Otherwise the patch looks reasonably sane.
The only bit that bothers me is calling openlog in mountd where it wasn't called before.
mountd (on debian at least) currently prefixes all log lines with "rpc_mountd" as that is the program name. With this patch they will be prefixes with "mountd".
This is a change and people who filter their log file (like me) might
notice. But it probably isn't a big concern..

I've committed it to CVS.

Your concern is indeed valid:

        /* Initialize logging. */
        if (!foreground) xlog_stderr(0);
        xlog_open("mountd");
...
        if (!foreground) {
                int fd = sysconf (_SC_OPEN_MAX);
                while (--fd > 2)
                        (void) close(fd);
        }
Quoted 4 lines

This could close a fd to syslog socket!
If it indeed happens, just move xlog_open below fd closing loop. However, you may opt to just omit xlog_open(). I will test this scenario.

glibc behaviour:

openlog() (inside xlog_open()) does not open fds per se. syslog() will create and connect fd to /dev/log on first call. Moreover, if syslog() gets -EBADF (i.e. somebody has closed fd) it will recreate socket and reconnect to /dev/log.

IOW: glibc is pretty resilient to us closing syslog fd.

I will add a comment on this and submit a patch
after your feedback on previous ones.

Cheers,
--
vda

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#4 lore raw
In reply to Denis Vlasenko
Quoted 3 lines · Denis Vlasenko

Patch does not introduce behavioral changes except for making foregrounded mountd to really use stderr. There was a bug which prevented this.

hmm... I've always found that if a daemon lives
in background, that the best place to debug it...

steved.

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs