Commits

Bryan O'Sullivan  committed a836f69

Convert to new logging API.

  • Participants
  • Parent commits 532a569

Comments (0)

Files changed (6)

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <syslog.h>
 
 #include "netplug.h"
 
 	filename = "stdin";
 	fp = stdin;
     } else if ((fp = fopen(filename, "r")) == NULL) {
-	perror(filename);
+	do_log(LOG_ERR, "filename: %m");
 	return;
     }
 
 	}
 
 	if (save_pattern(l) == -1) {
-	    fprintf(stderr, "%s, line %d: bad pattern: %s\n",
-		    filename, line, l);
+	    do_log(LOG_ERR, "%s, line %d: bad pattern: %s",
+		   filename, line, l);
 	    exit(1);
 	}
     }
     
     if (ferror(fp)) {
-	fprintf(stderr, "Error reading %s: %s\n", filename, strerror(errno));
+	do_log(LOG_ERR, "Error reading %s: %s", filename, strerror(errno));
 	exit(1);
     }
 
 	    nmatch += try_probe(p->pat);
 	}
 	else if (m == 0) {
-	    fprintf(stderr, "Warning: Don't know how to probe for interfaces "
-		    "matching %s\n", p->pat);
+	    do_log(LOG_WARNING, "Don't know how to probe for interfaces "
+		   "matching %s", p->pat);
 	    continue;
 	}
 	else {
     }
 
     if (nmatch == 0) {
-	fprintf(stderr, "Warning: Could not probe for any interfaces\n");
+	do_log(LOG_WARNING, "Could not probe for any interfaces");
     }
 }
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <syslog.h>
 
 #include "netplug.h"
 
 	rta = RTA_NEXT(rta,len);
     }
     if (len) {
-	fprintf(stderr, "Badness! Deficit %d, rta_len=%d\n", len, rta->rta_len);
+	do_log(LOG_ERR, "Badness! Deficit %d, rta_len=%d", len, rta->rta_len);
 	abort();
     }
 }
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/wait.h>
+#include <syslog.h>
 #include <unistd.h>
 
 #include "netplug.h"
     pid_t pid;
 
     if ((pid = fork()) == -1) {
-	perror("fork");
+	do_log(LOG_ERR, "fork: %m");
 	exit(1);
     }
     else if (pid != 0) {
 	return pid;
     }
     
-    printf("%s %s %s\n", NP_SCRIPT, ifname, action);
-    fflush(stdout);
+    do_log(LOG_DEBUG, "%s %s %s", NP_SCRIPT, ifname, action);
     
     execl(NP_SCRIPT, NP_SCRIPT, ifname, action, NULL);
 
-    perror(NP_SCRIPT);
+    do_log(LOG_ERR, NP_SCRIPT ": %m");
     exit(1);
 }
 
     int status, ret;
 
     if ((ret = waitpid(pid, &status, 0)) == -1) {
-	perror("waitpid");
+	do_log(LOG_ERR, "waitpid: %m");
 	exit(1);
     }
     
     void *x = malloc(n);
 
     if (n > 0 && x == NULL) {
-	perror("malloc");
+	do_log(LOG_ERR, "malloc: %m");
 	exit(1);
     }
 
 #define _GNU_SOURCE
 #include <net/if.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <syslog.h>
 #include <unistd.h>
 
 #include "netplug.h"
 
 
+static int use_syslog;
+
+
+void
+do_log(int pri, const char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+
+    if (use_syslog) {
+	vsyslog(pri, fmt, ap);
+    } else {
+	FILE *fp;
+
+	switch (pri) {
+	case LOG_INFO:
+	case LOG_NOTICE:
+	case LOG_DEBUG:
+	    fp = stdout;
+	    break;
+	default:
+	    fp = stderr;
+	    break;
+	}
+	
+	switch (pri) {
+	case LOG_WARNING:
+	    fputs("Warning: ", fp);
+	    break;
+	case LOG_NOTICE:
+	    fputs("Notice: ", fp);
+	    break;
+	case LOG_ERR:
+	    fputs("Error: ", fp);
+	    break;
+	case LOG_DEBUG:
+	    break;
+	default:
+	    fprintf(fp, "Log type %d: ", pri);
+	    break;
+	}
+
+	vfprintf(fp, fmt, ap);
+	fputc('\n', fp);
+    }
+
+    va_end(ap);
+}
+
+
 #define flag_was_set(flag) \
 	(!(i->flags & (flag)) && (info->ifi_flags & (flag)))
 #define flag_was_unset(flag) \
     parse_rtattrs(attrs, IFLA_MAX, IFLA_RTA(info), len);
 
     if (attrs[IFLA_IFNAME] == NULL) {
-	fprintf(stderr, "No interface name\n");
+	do_log(LOG_ERR, "No interface name");
 	exit(1);
     }
     
 	goto done;
     }
     
-    printf("%s: flags 0x%08x -> 0x%08x\n", name, i->flags, info->ifi_flags);
+    do_log(LOG_INFO, "%s: flags 0x%08x -> 0x%08x", name, i->flags,
+	   info->ifi_flags);
 
     if (flag_was_set(IFF_RUNNING)) {
 	run_netplug_bg(name, "in");
     }
     if (flag_was_unset(IFF_UP)) {
 	if (try_probe(name) == 0) {
-	    fprintf(stderr, "Warning: Could not bring %s back up\n", name);
+	    do_log(LOG_WARNING, "Could not bring %s back up", name);
 	}
     }
 
 main(int argc, char *argv[])
 {
     int c;
+    int cfg_read = 0;
     int foreground = 0;
-    int cfg_read = 0;
     int probe = 1;
 
     while ((c = getopt(argc, argv, "FPc:hi:")) != EOF) {
     }
     
     if (getuid() != 0) {
-	fprintf(stderr, "Warning: This command will not work properly unless "
-		"run by root\n");
+	do_log(LOG_WARNING, "This command will not work properly unless "
+	       "run by root");
     }
     
     if (probe) {
     netlink_request_dump(fd);
     netlink_receive_dump(fd, if_info_save_interface, NULL);
 
-    if (!foreground && daemon(0, 0) == -1) {
-	perror("daemon");
-	exit(1);
+    if (!foreground) {
+	if (daemon(0, 0) == -1) {
+	    do_log(LOG_ERR, "daemon: %m");
+	    exit(1);
+	}
+	use_syslog = 1;
+	openlog("netplugd", LOG_PID, LOG_DAEMON);
     }
     
     netlink_listen(fd, handle_interface, NULL);
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <syslog.h>
 
 #include "netplug.h"
 
 
     if (sendto(fd, (void*) &req, sizeof(req), 0,
 	       (struct sockaddr *) &addr, sizeof(addr)) == -1) {
-	perror("Could not request interface dump");
+	do_log(LOG_ERR, "Could not request interface dump: %m");
 	exit(1);
     }
 }
 	if (status == -1) {
 	    if (errno == EINTR)
 		continue;
-	    perror("OVERRUN");
+	    do_log(LOG_ERR, "OVERRUN: %m");
 	    continue;
 	}
 	else if (status == 0) {
-	    fprintf(stderr, "Unexpected EOF on netlink\n");
+	    do_log(LOG_ERR, "Unexpected EOF on netlink");
 	    exit(1);
 	}
 
 	if (msg.msg_namelen != sizeof(addr)) {
-	    fprintf(stderr, "Unexpected sender address length\n");
+	    do_log(LOG_ERR, "Unexpected sender address length");
 	    exit(1);
 	}
 
 
 	    if (l < 0 || len > status) {
 		if (msg.msg_flags & MSG_TRUNC) {
-		    fprintf(stderr, "Truncated message\n");
+		    do_log(LOG_ERR, "Truncated message");
 		    exit(1);
 		}
-		fprintf(stderr, "Malformed netlink message\n");
+		do_log(LOG_ERR, "Malformed netlink message");
 		exit(1);
 	    }
 
 	    hdr = (struct nlmsghdr *) ((char *) hdr + NLMSG_ALIGN(len));
 	}
 	if (msg.msg_flags & MSG_TRUNC) {
-	    fprintf(stderr, "Message truncated\n");
+	    do_log(LOG_ERR, "Message truncated");
 	    continue;
 	}
 	if (status) {
-	    fprintf(stderr, "!!!Remnant of size %d\n", status);
+	    do_log(LOG_ERR, "!!!Remnant of size %d", status);
 	    exit(1);
 	}
     }
 	    if (errno == EINTR) {
 		continue;
 	    }
-	    perror("Netlink overrun");
+	    do_log(LOG_ERR, "Netlink overrun: %m");
 	    continue;
 	}
 	else if (status == 0) {
-	    fprintf(stderr, "Unexpected EOF on netlink\n");
+	    do_log(LOG_ERR, "Unexpected EOF on netlink");
 	    exit(1);
 	}
 
 	if (msg.msg_namelen != sizeof(addr)) {
-	    fprintf(stderr, "Unexpected sender address length\n");
+	    do_log(LOG_ERR, "Unexpected sender address length");
 	    exit(1);
 	}
 
 	    int err;
 
 	    if (hdr->nlmsg_seq != dump) {
-		fprintf(stderr, "Skipping junk\n");
+		do_log(LOG_ERR, "Skipping junk");
 		goto skip_it;
 	    }
 
 		struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA(hdr);
 		
 		if (hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
-		    fprintf(stderr, "Netlink message truncated\n");
+		    do_log(LOG_ERR, "Netlink message truncated");
 		} else {
 		    errno = -err->error;
-		    perror("Error from rtnetlink");
+		    do_log(LOG_ERR, "Error from rtnetlink: %m");
 		}
 		exit(1);
 	    }
 	    hdr = NLMSG_NEXT(hdr, status);
 	}
 	if (msg.msg_flags & MSG_TRUNC) {
-	    fprintf(stderr, "Message truncated\n");
+	    do_log(LOG_ERR, "Message truncated");
 	    continue;
 	}
 	if (status) {
-	    fprintf(stderr, "Dangling remnant of size %d!\n", status);
+	    do_log(LOG_ERR, "Dangling remnant of size %d!", status);
 	    exit(1);
 	}
     }
     int fd;
 
     if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) {
-	perror("Could not create netlink socket");
+	do_log(LOG_ERR, "Could not create netlink socket: %m");
 	exit(1);
     }
 
     addr.nl_groups = RTMGRP_LINK;
 
     if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
-	perror("Could not bind netlink socket");
+	do_log(LOG_ERR, "Could not bind netlink socket: %m");
 	exit(1);
     }
     
     int addr_len = sizeof(addr);
     
     if (getsockname(fd, (struct sockaddr *) &addr, &addr_len) == -1) {
-	perror("Could not get socket details");
+	do_log(LOG_ERR, "Could not get socket details: %m");
 	exit(1);
     }
 	
     if (addr_len != sizeof(addr)) {
-	fprintf(stderr, "Our netlink socket size does not match the kernel's!\n");
+	do_log(LOG_ERR, "Our netlink socket size does not match the kernel's!");
 	exit(1);
     }
 
     if (addr.nl_family != AF_NETLINK) {
-	fprintf(stderr, "The kernel has given us an insane address family!\n");
+	do_log(LOG_ERR, "The kernel has given us an insane address family!");
 	exit(1);
     }
 
 
 /* utilities */
 
+void do_log(int pri, const char *fmt, ...);
 pid_t run_netplug_bg(char *ifname, char *action);
 int run_netplug(char *ifname, char *action);
 void *xmalloc(size_t n);