From 3a035debc8695cc227cb8806d42de42e66e2833d Mon Sep 17 00:00:00 2001 From: Pascal Martin Date: Tue, 20 Apr 2021 08:32:13 -0700 Subject: [PATCH] Always print error information on abnormal exit --- hc_broadcast.c | 22 ++++++++++++---------- hc_clock.c | 7 ++++--- hc_http.c | 27 +++++++++++++++++---------- hc_nmea.c | 3 ++- hc_ntp.c | 3 ++- houseclock.c | 5 ++++- 6 files changed, 41 insertions(+), 26 deletions(-) diff --git a/hc_broadcast.c b/hc_broadcast.c index 026399b..d059854 100644 --- a/hc_broadcast.c +++ b/hc_broadcast.c @@ -105,8 +105,8 @@ static int hc_broadcast_socket (int ipv4, int port) { s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (s < 0) { - fprintf (stderr, "cannot open socket for port %d: %s\n", - port, strerror(errno)); + fprintf (stderr, "[%s %d] cannot open socket for port %d: %s\n", + __FILE__, __LINE__, port, strerror(errno)); exit (1); } @@ -115,8 +115,8 @@ static int hc_broadcast_socket (int ipv4, int port) { value = 1; if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value)) < 0) { - fprintf (stderr, "cannot enable broadcast for port %d: %s\n", - port, strerror(errno)); + fprintf (stderr, "[%s %d] cannot enable broadcast for port %d: %s\n", + __FILE__, __LINE__, port, strerror(errno)); exit (1); } @@ -127,7 +127,8 @@ static int hc_broadcast_socket (int ipv4, int port) { if (bind(s, (struct sockaddr *)&netaddress, sizeof(netaddress)) < 0) { fprintf (stderr, - "cannot bind to %s: %s\n", + "[%s %d] cannot bind to %s: %s\n", + __FILE__, __LINE__, hc_broadcast_format(&netaddress), strerror(errno)); exit (1); } @@ -206,7 +207,8 @@ int hc_broadcast_open (const char *service) { endservent(); if (serverport <= 0) { - fprintf (stderr, "invalid service name %s\n", service); + fprintf (stderr, "[%s %d] invalid service name %s\n", + __FILE__, __LINE__, service); exit (1); } @@ -217,15 +219,15 @@ int hc_broadcast_open (const char *service) { value = 1024 * 1024; if (setsockopt(udpserver, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0) { - fprintf (stderr, "cannot set receive buffer to %d for service %s: %s\n", - value, service, strerror(errno)); + fprintf (stderr, "[%s %d] cannot set receive buffer to %d for service %s: %s\n", + __FILE__, __LINE__, value, service, strerror(errno)); exit (1); } value = 1024 * 1024; if (setsockopt(udpserver, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0) { - fprintf (stderr, "cannot set send buffer to %d for service %s: %s\n", - value, service, strerror(errno)); + fprintf (stderr, "[%s %d] cannot set send buffer to %d for service %s: %s\n", + __FILE__, __LINE__, value, service, strerror(errno)); exit (1); } diff --git a/hc_clock.c b/hc_clock.c index c0c5491..ca1be05 100644 --- a/hc_clock.c +++ b/hc_clock.c @@ -114,7 +114,8 @@ void hc_clock_initialize (int argc, const char **argv) { i = hc_db_new (HC_CLOCK_DRIFT, sizeof(int), HC_CLOCK_DRIFT_DEPTH); if (i != 0) { - fprintf (stderr, "cannot create %s: %s\n", HC_CLOCK_DRIFT, strerror(i)); + fprintf (stderr, "[%s %d] cannot create %s: %s\n", + __FILE__, __LINE__, HC_CLOCK_DRIFT, strerror(i)); exit (1); } hc_clock_drift_db = (int *) hc_db_get (HC_CLOCK_DRIFT); @@ -122,8 +123,8 @@ void hc_clock_initialize (int argc, const char **argv) { i = hc_db_new (HC_CLOCK_STATUS, sizeof(hc_clock_status), 1); if (i != 0) { - fprintf (stderr, - "cannot create %s: %s\n", HC_CLOCK_STATUS, strerror(i)); + fprintf (stderr, "[%s %d] cannot create %s: %s\n", + __FILE__, __LINE__, HC_CLOCK_STATUS, strerror(i)); exit (1); } hc_clock_status_db = (hc_clock_status *)hc_db_get (HC_CLOCK_STATUS); diff --git a/hc_http.c b/hc_http.c index e315d27..90b73be 100644 --- a/hc_http.c +++ b/hc_http.c @@ -72,7 +72,11 @@ static void hc_background (int fd, int mode) { } if (now >= LastParentCheck + 3) { - if (kill (parent, 0) < 0) exit(0); + if (kill (parent, 0) < 0) { + fprintf (stderr, "[%s %d] Parent disappeared, exit now\n", + __FILE__, __LINE__); + exit(1); + } LastParentCheck = now; } @@ -104,8 +108,8 @@ static int hc_http_attach_clock (void) { if (clock_db == 0) return 0; if (hc_db_get_count (HC_CLOCK_STATUS) != 1 || hc_db_get_size (HC_CLOCK_STATUS) != sizeof(hc_clock_status)) { - fprintf (stderr, "wrong data structure for table %s\n", - HC_CLOCK_STATUS); + fprintf (stderr, "[%s %d] wrong data structure for table %s\n", + __FILE__, __LINE__, HC_CLOCK_STATUS); exit (1); } } @@ -119,8 +123,8 @@ static int hc_http_attach_nmea (void) { if (nmea_db == 0) return 0; if (hc_db_get_count (HC_NMEA_STATUS) != 1 || hc_db_get_size (HC_NMEA_STATUS) != sizeof(hc_nmea_status)) { - fprintf (stderr, "wrong data structure for table %s\n", - HC_NMEA_STATUS); + fprintf (stderr, "[%s %d] wrong data structure for table %s\n", + __FILE__, __LINE__, HC_NMEA_STATUS); exit (1); } } @@ -134,8 +138,8 @@ static int hc_http_attach_ntp (void) { if (ntp_db == 0) return 0; if (hc_db_get_count (HC_NTP_STATUS) != 1 || hc_db_get_size (HC_NTP_STATUS) != sizeof(hc_ntp_status)) { - fprintf (stderr, "wrong data structure for table %s\n", - HC_NTP_STATUS); + fprintf (stderr, "[%s %d] wrong data structure for table %s\n", + __FILE__, __LINE__, HC_NTP_STATUS); exit (1); } } @@ -332,8 +336,8 @@ static const char *hc_http_clockdrift (const char *method, const char *uri, if (drift_db == 0) return ""; drift_count = hc_db_get_count (HC_CLOCK_DRIFT); if (hc_db_get_size (HC_CLOCK_DRIFT) != sizeof(int)) { - fprintf (stderr, "wrong data structure for table %s\n", - HC_CLOCK_DRIFT); + fprintf (stderr, "[%s %d] wrong data structure for table %s\n", + __FILE__, __LINE__, HC_CLOCK_DRIFT); exit (1); } } @@ -462,7 +466,10 @@ void hc_http (int argc, const char **argv) { echttp_default ("-http-service=dynamic"); - if (echttp_open (argc, argv) <= 0) exit(1); + if (echttp_open (argc, argv) <= 0) { + fprintf (stderr, "[%s %d] echttp_open() failed\n", __FILE__, __LINE__); + exit(1); + } if (echttp_dynamic_port()) { houseportal_initialize (argc, argv); use_houseportal = 1; diff --git a/hc_nmea.c b/hc_nmea.c index dc5d899..55ac175 100644 --- a/hc_nmea.c +++ b/hc_nmea.c @@ -217,7 +217,8 @@ void hc_nmea_initialize (int argc, const char **argv) { i = hc_db_new (HC_NMEA_STATUS, sizeof(hc_nmea_status), 1); if (i != 0) { fprintf (stderr, - "cannot create %s: %s\n", HC_NMEA_STATUS, strerror(i)); + "[%s %d] cannot create %s: %s\n", + __FILE__, __LINE__, HC_NMEA_STATUS, strerror(i)); exit(1); } hc_nmea_status_db = (hc_nmea_status *) hc_db_get (HC_NMEA_STATUS); diff --git a/hc_ntp.c b/hc_ntp.c index bf6c031..3ac43df 100644 --- a/hc_ntp.c +++ b/hc_ntp.c @@ -155,7 +155,8 @@ int hc_ntp_initialize (int argc, const char **argv) { i = hc_db_new (HC_NTP_STATUS, sizeof(hc_ntp_status), 1); if (i != 0) { - fprintf (stderr, "cannot create %s: %s\n", HC_NTP_STATUS, strerror(i)); + fprintf (stderr, "[%s %d] cannot create %s: %s\n", + __FILE__, __LINE__, HC_NTP_STATUS, strerror(i)); exit (1); } hc_ntp_status_db = (hc_ntp_status *) hc_db_get (HC_NTP_STATUS); diff --git a/houseclock.c b/houseclock.c index 2fc4c4b..5588df3 100644 --- a/houseclock.c +++ b/houseclock.c @@ -131,7 +131,8 @@ int main (int argc, const char **argv) { hc_http (argc, argv); } if (httpid < 0) { - fprintf (stderr, "Cannot fork: %s\n", strerror (errno)); + fprintf (stderr, "[%s %d] Cannot fork: %s\n", + __FILE__, __LINE__, strerror (errno)); exit (1); } @@ -198,6 +199,8 @@ int main (int argc, const char **argv) { int wstatus; if (waitpid (httpid, &wstatus, WNOHANG) == httpid) { + fprintf (stderr, "[%s %d] the HTTP server died, exit now\n", + __FILE__, __LINE__); exit(1); } }