Commit Diff


commit - b290a4cca440a1deb97931a806942fdaa1724892
commit + 6e168f4f9e15f2009039cd6881a7ca08a5e8fc8b
blob - 9ba7cff4808b0fbb7e3fb100ba00cd9630cef7cf
blob + 935708084fb50162f60e79391b3b2d0374ef060e
--- gotd/libexec/got-notify-http/got-notify-http.c
+++ gotd/libexec/got-notify-http/got-notify-http.c
@@ -27,8 +27,10 @@
 #include <netdb.h>
 #include <poll.h>
 #include <stdio.h>
+#include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
+#include <syslog.h>
 #include <unistd.h>
 
 #include "got_opentemp.h"
@@ -36,6 +38,7 @@
 
 #include "bufio.h"
 #include "utf8d.h"
+#include "log.h"
 
 #define USERAGENT	 "got-notify-http/" GOT_VERSION_STR
 
@@ -838,6 +841,8 @@ main(int argc, char **argv)
 		err(1, "pledge");
 #endif
 
+	log_init(0, LOG_DAEMON);
+
 	while ((ch = getopt(argc, argv, "ch:p:r:")) != -1) {
 		switch (ch) {
 		case 'c':
@@ -869,16 +874,16 @@ main(int argc, char **argv)
 	password = getenv("GOT_NOTIFY_HTTP_PASS");
 	if ((username != NULL && password == NULL) ||
 	    (username == NULL && password != NULL))
-		errx(1, "username or password are not specified");
+		fatalx("username or password are not specified");
 	if (username && *password == '\0')
-		errx(1, "password can't be empty");
+		fatalx("password can't be empty");
 
 	/* used by the regression test suite */
 	timeoutstr = getenv("GOT_NOTIFY_TIMEOUT");
 	if (timeoutstr) {
 		http_timeout = strtonum(timeoutstr, 0, 600, &errstr);
 		if (errstr != NULL)
-			errx(1, "timeout in seconds is %s: %s",
+			fatalx("timeout in seconds is %s: %s",
 			    errstr, timeoutstr);
 	}
 
@@ -887,15 +892,15 @@ main(int argc, char **argv)
 
 	tmpfp = got_opentemp();
 	if (tmpfp == NULL)
-		err(1, "opentemp");
+		fatal("opentemp");
 
 	jsonify(tmpfp, repo);
 
 	paylen = ftello(tmpfp);
 	if (paylen == -1)
-		err(1, "ftello");
+		fatal("ftello");
 	if (fseeko(tmpfp, 0, SEEK_SET) == -1)
-		err(1, "fseeko");
+		fatal("fseeko");
 
 #ifndef PROFILE
 	/* drop tmppath */
@@ -907,15 +912,15 @@ main(int argc, char **argv)
 	pfd.fd = dial(host, port);
 
 	if ((flags = fcntl(pfd.fd, F_GETFL)) == -1)
-		err(1, "fcntl(F_GETFL)");
+		fatal("fcntl(F_GETFL)");
 	if (fcntl(pfd.fd, F_SETFL, flags | O_NONBLOCK) == -1)
-		err(1, "fcntl(F_SETFL)");
+		fatal("fcntl(F_SETFL)");
 
 	if (bufio_init(&bio) == -1)
-		err(1, "bufio_init");
+		fatal("bufio_init");
 	bufio_set_fd(&bio, pfd.fd);
 	if (tls && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1)
-		err(1, "bufio_starttls");
+		fatal("bufio_starttls");
 
 #ifndef PROFILE
 	/* drop rpath dns inet */
@@ -938,19 +943,19 @@ main(int argc, char **argv)
 	    nonstd ? ":" : "", nonstd ? port : "",
 	    (long long)paylen, USERAGENT);
 	if (ret == -1)
-		err(1, "bufio_compose_fmt");
+		fatal("bufio_compose_fmt");
 
 	if (username) {
 		auth = basic_auth(username, password);
 		ret = bufio_compose_fmt(&bio, "Authorization: basic %s\r\n",
 		    auth);
 		if (ret == -1)
-			err(1, "bufio_compose_fmt");
+			fatal("bufio_compose_fmt");
 		free(auth);
 	}
 
 	if (bufio_compose(&bio, "\r\n", 2) == -1)
-		err(1, "bufio_compose");
+		fatal("bufio_compose");
 
 	while (!done) {
 		struct timespec	 elapsed, start, stop;
@@ -960,23 +965,23 @@ main(int argc, char **argv)
 		clock_gettime(CLOCK_MONOTONIC, &start);
 		ret = ppoll(&pfd, 1, &timeout, NULL);
 		if (ret == -1)
-			err(1, "poll");
+			fatal("poll");
 		clock_gettime(CLOCK_MONOTONIC, &stop);
 		timespecsub(&stop, &start, &elapsed);
 		timespecsub(&timeout, &elapsed, &timeout);
 		if (ret == 0 || timeout.tv_sec <= 0)
-			errx(1, "timeout");
+			fatalx("timeout");
 
 		if (bio.wbuf.len > 0 && (pfd.revents & POLLOUT)) {
 			if (bufio_write(&bio) == -1 && errno != EAGAIN)
-				errx(1, "bufio_write: %s", bufio_io_err(&bio));
+				fatalx("bufio_write: %s", bufio_io_err(&bio));
 		}
 		if (pfd.revents & POLLIN) {
 			r = bufio_read(&bio);
 			if (r == -1 && errno != EAGAIN)
-				errx(1, "bufio_read: %s", bufio_io_err(&bio));
+				fatalx("bufio_read: %s", bufio_io_err(&bio));
 			if (r == 0)
-				errx(1, "unexpected EOF");
+				fatalx("unexpected EOF");
 
 			for (;;) {
 				line = buf_getdelim(&bio.rbuf, "\r\n", &len);
@@ -996,22 +1001,22 @@ main(int argc, char **argv)
 				}
 				spc = strchr(line, ' ');
 				if (spc == NULL)
-					errx(1, "bad reply");
+					fatalx("bad HTTP response from server");
 				*spc++ = '\0';
 				if (strcasecmp(line, "HTTP/1.1") != 0)
-					errx(1, "unexpected protocol: %s",
+					log_warnx("unexpected protocol: %s",
 					    line);
 				line = spc;
 
 				spc = strchr(line, ' ');
 				if (spc == NULL)
-					errx(1, "bad reply");
+					fatalx("bad HTTP response from server");
 				*spc++ = '\0';
 
 				response_code = strtonum(line, 100, 599,
 				    &errstr);
 				if (errstr != NULL)
-					errx(1, "response code is %s: %s",
+					log_warnx("response code is %s: %s",
 					    errstr, line);
 
 				buf_drain(&bio.rbuf, len);
@@ -1024,16 +1029,16 @@ main(int argc, char **argv)
 			len = fread(buf, 1, sizeof(buf), tmpfp);
 			if (len == 0) {
 				if (ferror(tmpfp))
-					err(1, "fread");
+					fatal("fread");
 				continue;
 			}
 
 			if (bufio_compose(&bio, buf, len) == -1)
-				err(1, "buf_compose");
+				fatal("buf_compose");
 		}
 	}
 
 	if (response_code >= 200 && response_code < 300)
 		return 0;
-	errx(1, "request failed with code %d", response_code);
+	fatal("request failed with code %d", response_code);
 }