From 68611eeab1d00febc4df3082a3b7cd5d7cc16cc1 Mon Sep 17 00:00:00 2001 From: Urban Wallasch Date: Wed, 21 Aug 2019 12:18:04 +0200 Subject: [PATCH] * Introduced debug make target, default is now to build w/o debug logging. * Let common signal handler catch SIGPIPE. * Improved error reporting in several places. * Changed shared memory file name. --- Makefile | 10 +++++++--- shmget.c | 2 +- telehttpd.c | 48 ++++++++++++++++++++++++++++++++++-------------- telemetry.h | 2 +- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index e190383..02beae3 100644 --- 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) diff --git a/shmget.c b/shmget.c index 46cfdd7..9895295 100644 --- 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 ) ) { diff --git a/telehttpd.c b/telehttpd.c index e060eaa..70a38f3 100644 --- a/telehttpd.c +++ b/telehttpd.c @@ -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 #include @@ -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); diff --git a/telemetry.h b/telemetry.h index ddebe0e..c856302 100644 --- a/telemetry.h +++ b/telemetry.h @@ -6,7 +6,7 @@ #include -#define TELE_SHM_NAME "/ets_teleshm3" +#define TELE_SHM_NAME "/ets2_teleshmem3" #define TELE_STRLEN 30 -- 2.30.2