* Introduced debug make target, default is now to build w/o debug logging.
authorUrban Wallasch <urban.wallasch@freenet.de>
Wed, 21 Aug 2019 10:18:04 +0000 (12:18 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Wed, 21 Aug 2019 10:18:04 +0000 (12:18 +0200)
* Let common signal handler catch SIGPIPE.
* Improved error reporting in several places.
* Changed shared memory file name.

Makefile
shmget.c
telehttpd.c
telemetry.h

index e19038338e8bd343b6501e9aa3011138b61619f5..02beae3600de782fecf3140fa40a23b67e2516d8 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -13,8 +13,8 @@ SDK_INCLUDES=\
   -Isdk/include/amtrucks/ \
   -Isdk/include/eurotrucks2
 
-CFLAGS=-Wall -Wextra -std=c99 -O2 -DDEBUG -I. -pthread
-CXXFLAGS=-Wall -O2 -DLOGGING -I. -pthread
+CFLAGS=-Wall -Wextra -std=c99 -O2 -I. -pthread
+CXXFLAGS=-Wall -O2 -I. -pthread
 LDFLAGS=-pthread -lrt
 
 UNAME:= $(shell uname -s)
@@ -30,10 +30,14 @@ PLUGIN_SRC := teleshmem.cpp shmget.c
 HTTPD_OBJ  := telehttpd.o shmget.o net.o fserv.o tele2json.o telehelper.o
 LOGGER_OBJ := telelogger.o shmget.o tele2json.o telehelper.o
 
-.PHONY: all clean
+.PHONY: all debug clean
 
 all: telehttpd telelogger teleshmem.so
 
+debug: CFLAGS += -DDEBUG
+debug: CXXFLAGS += -DLOGGING
+debug: all
+
 teleshmem.so: $(PLUGIN_SRC) $(COMMON_HDR) $(SDK_HEADERS) $(SELF)
        $(CXX) -o $@ $(CXXFLAGS) -fPIC --shared -Wl,$(LIB_NAME_OPTION),$@ $(SDK_INCLUDES) $(PLUGIN_SRC)
 
index 46cfdd7c12fdcbe88643a03d8cc732cb225c8021..989529576b7c93018272d16ef08cf617e0d301cc 100644 (file)
--- a/shmget.c
+++ b/shmget.c
@@ -23,7 +23,7 @@ static void *init_shm_( const char *name, size_t size, int oflag, int mode, int
     void *addr = NULL;
 
     if ( 0 > ( fd = shm_open( name, oflag, mode ) ) ) {
-        EPRINT( "shm_open: %s\n", strerror( errno ) );
+        EPRINT( "shm_open: %s: %s\n", name, strerror( errno ) );
         goto ERR_1;
     }
     if ( 0 != ftruncate( fd, size ) ) {
index e060eaa4112a9bcc34e307225dd3ab832588658e..70a38f3f130388d0e6d21a40759912c14bb7b60f 100644 (file)
@@ -1,5 +1,5 @@
-// Needed for getopt() and pthread_rwlock*
-#define _XOPEN_SOURCE 500
+// Needed for getopt(), pthread_rwlock*, and strsignal()
+#define _POSIX_C_SOURCE 200809L
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -48,12 +48,17 @@ static volatile struct telemetry_state_t *telemetry;
 static volatile sig_atomic_t force_quit;
 
 static void handle_signal(int sig) {
-    fprintf (stderr, "SIGNAL: %d\n", sig);
+    EPRINT( "SIGNAL: %d %s\n", sig, strsignal(sig) );
     switch (sig){
     case SIGTERM:
     case SIGINT:
         force_quit = 1;
         break;
+    case SIGPIPE:
+        // We don't want to get killed by writing to a stale socket!
+        break;
+    default:
+        break;
     }
 }
 
@@ -118,6 +123,18 @@ static size_t copy_field_val( char *buf, size_t bufsz, const char *needle, const
     return l;
 }
 
+static inline ssize_t sockwrite( int fd, const void *buf, size_t len ) {
+    ssize_t n;
+    n = write( fd, buf, len );
+    if ( 0 > n ) {
+        EPRINT( "write: %s\n", strerror(errno) );
+    }
+    else if ( len != (size_t)n ) {
+        EPRINT( "write: short write (%d of %d)\n", (int)n, (int)len );
+    }
+    return n;
+}
+
 static void respond( struct thread_data_t *td, const char *req ) {
     int n;
     char buf[4096]; // Must be large enough to satisfy tele2json()!
@@ -151,8 +168,8 @@ static void respond( struct thread_data_t *td, const char *req ) {
                 host,
                 origin
             );
-        if ( 0 < n && (size_t)n < sizeof buf && n == write( td->sock, buf, n ) )
-            write( td->sock, buf, tele2json( buf, sizeof buf, &td->tele ) );
+        if ( 0 < n && (size_t)n < sizeof buf && n == sockwrite( td->sock, buf, n ) )
+            sockwrite( td->sock, buf, tele2json( buf, sizeof buf, &td->tele ) );
         break;
     case r_index: {
         struct file_info_t fi;
@@ -165,7 +182,7 @@ static void respond( struct thread_data_t *td, const char *req ) {
                     "\r\n",
                     host
                 );
-            if ( 0 < n && (size_t)n < sizeof buf && n == write( td->sock, buf, n ) ) {
+            if ( 0 < n && (size_t)n < sizeof buf && n == sockwrite( td->sock, buf, n ) ) {
                 fserv_sendfile( td->sock, &fi );
                 fserv_close( &fi );
             }
@@ -186,7 +203,7 @@ static void respond( struct thread_data_t *td, const char *req ) {
                 host
             );
         if ( 0 < n && (size_t)n < sizeof buf )
-            write( td->sock, buf, n );
+            sockwrite( td->sock, buf, n );
         break;
     default:
         n = snprintf( buf, sizeof buf,
@@ -199,7 +216,7 @@ static void respond( struct thread_data_t *td, const char *req ) {
                 host
             );
         if ( 0 < n && (size_t)n < sizeof buf )
-            write( td->sock, buf, n );
+            sockwrite( td->sock, buf, n );
         break;
     }
 }
@@ -221,15 +238,16 @@ static int rcv_request(int sock, char *buf, size_t size, int timeout) {
             if ( 0 > bread ) {
                 switch (errno) {
                 case EAGAIN:
-                //case EWOULDBLOCK:
                 case EINTR:
                     break;
                 default:
+                    EPRINT( "read: %s\n", strerror(errno) );
                     res = -1;
                     break;
                 }
             }
             else if ( 0 == bread && 0 == errno ) {
+                EPRINT( "read: premature EOF\n" );
                 res = -1;
             }
             else {
@@ -278,14 +296,17 @@ static int serve_http(void) {
         }
         if ( 0 < as ) {
             if ( NULL != (td = malloc( sizeof *td )) ) {
+                int e;
                 td->sock = as;
-                if ( 0 != tele_cpy( &td->tele, telemetry ) )
+                if ( 0 != tele_cpy( &td->tele, telemetry ) ) {
                     DPRINT( "No telemetry, serving dummy data\n" );
+                }
                 pthread_attr_init( &attr );
                 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
-                if ( 0 != pthread_create( &tid, &attr, handle_conn, td ) ) {
-                    net_close( td->sock );
+                if ( 0 != ( e = pthread_create( &tid, &attr, handle_conn, td ) ) ) {
+                    EPRINT( "pthread_create: %s\n", strerror( e ) );
                     free( td );
+                    net_close( as );
                 }
             }
             else {
@@ -358,8 +379,7 @@ static int config( int argc, char *argv[] ) {
 int main(int argc, char *argv[]) {
     signal(SIGTERM, handle_signal);
     signal(SIGINT,  handle_signal);
-    // We don't want to get killed by some thread writing to a stale socket:
-    signal(SIGPIPE, SIG_IGN);
+    signal(SIGPIPE, handle_signal);
 
     if ( 0 != config( argc, argv ) )
         exit( EXIT_FAILURE);
index ddebe0e495cdc8d3b7b1d1a72c2573e2eb56c88e..c856302d824846e01545e06bc6b47b6d64c4efd2 100644 (file)
@@ -6,7 +6,7 @@
 
 #include <pthread.h>
 
-#define TELE_SHM_NAME   "/ets_teleshm3"
+#define TELE_SHM_NAME   "/ets2_teleshmem3"
 
 #define TELE_STRLEN     30