Skip to content

Commit

Permalink
Always print error information on abnormal exit
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-fb-martin committed Apr 20, 2021
1 parent c353bf8 commit 3a035de
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
22 changes: 12 additions & 10 deletions hc_broadcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
7 changes: 4 additions & 3 deletions hc_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ 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);
for (i = 0; i < HC_CLOCK_DRIFT_DEPTH; ++i) hc_clock_drift_db[i] = 0;

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);
Expand Down
27 changes: 17 additions & 10 deletions hc_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion hc_nmea.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion hc_ntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion houseclock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 3a035de

Please sign in to comment.