check for connect failure in syslog log opening
authorRich Felker <dalias@aerifal.cx>
Fri, 9 Jan 2015 05:09:54 +0000 (00:09 -0500)
committerRich Felker <dalias@aerifal.cx>
Mon, 30 Mar 2015 05:41:33 +0000 (01:41 -0400)
based on patch by Dima Krasner, with minor improvements for code size.
connect can fail if there is no listening syslogd, in which case a
useless socket was kept open, preventing subsequent syslog call from
attempting to connect again.

(cherry picked from commit c574321d75f035ff6d2c18dfb7e3f70db60ba7bd)

src/misc/syslog.c

index 57f1d75cbd12cf121715e62e41d93cf46d2c57ed..cc82508d2a604886abc1d36b9e8b87dfdf7c09ae 100644 (file)
@@ -45,8 +45,12 @@ void closelog(void)
 
 static void __openlog()
 {
-       log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
-       if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
+       int fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
+       if (fd < 0) return;
+       if (connect(fd, (void *)&log_addr, sizeof log_addr) < 0)
+               close(fd);
+       else
+               log_fd = fd;
 }
 
 void openlog(const char *ident, int opt, int facility)