diff --git a/configfile.c b/configfile.c index 67f5a5e..28eacf7 100644 --- a/configfile.c +++ b/configfile.c @@ -85,13 +85,13 @@ int config_parse(const char *config) { FILE *fz = fopen(config, "r"); if (fz == NULL) { - log_print(LOG_ERROR, "config_parse(): %s", config); + log_print(LOG_ERROR, "%s(): failed to open config '%s'", __FUNCTION__, config); return -1; } char *line = malloc(BUFSIZE); if (line == NULL) { - log_print(LOG_ERROR, "config_parse(): out of memory"); + log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__); fclose(fz); return -1; } @@ -108,7 +108,7 @@ int config_parse(const char *config) char *tok = strtok(line +1, " ]\n"); if (tok == NULL || (section = config_add_section(tok)) == NULL) { - log_print(LOG_WARN, "config_parse(): invalid section in row %d", linenum); + log_print(LOG_WARN, "%s(): invalid section in row %d", __FUNCTION__, linenum); free(line); fclose(fz); return -1; @@ -116,7 +116,7 @@ int config_parse(const char *config) continue; } else if (section == NULL) { - log_print(LOG_WARN, "config_parse(): missing section in row %d", linenum); + log_print(LOG_WARN, "%s(): missing section in row %d", __FUNCTION__, linenum); free(line); fclose(fz); return -1; @@ -127,7 +127,7 @@ int config_parse(const char *config) char *tok2; while ((tok2 = strtok_r(NULL, " \n", &tmp))) { if (config_add_tupel(section, tok, tok2) != 0) - log_print(LOG_WARN, "config_parse(): invalid row %d", linenum); + log_print(LOG_WARN, "%s(): invalid row %d", __FUNCTION__, linenum); } } } diff --git a/event.c b/event.c index ffc4769..0dbf270 100644 --- a/event.c +++ b/event.c @@ -60,13 +60,13 @@ struct event_fd * event_add_fd( { /* check valid filediskriptor */ if (fd < 0 || fd > FD_SETSIZE) { - log_print(LOG_ERROR, "event_add_fd(): invalid fd"); + log_print(LOG_ERROR, "%s(): invalid fd", __FUNCTION__); return NULL; } /* check valid type (read/write) */ if (!(type & FD_TYPES)) { - log_print(LOG_ERROR, "event_add_fd(): invalid type"); + log_print(LOG_ERROR, "%s(): invalid type", __FUNCTION__); return NULL; } @@ -74,7 +74,7 @@ struct event_fd * event_add_fd( if (entry == NULL) { entry = malloc(sizeof(struct event_fd)); if (entry == NULL) { - log_print(LOG_ERROR, "event_add_fd(): out of memory"); + log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__); return NULL; } @@ -177,7 +177,7 @@ struct event_timeout * event_add_timeout( struct event_timeout *entry; entry = malloc(sizeof(struct event_timeout)); if (entry == NULL) { - log_print(LOG_ERROR, "event_add_timeout(): out of memory"); + log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__); return NULL; } @@ -283,19 +283,19 @@ int event_loop(int (*pre_select_cb)(int *maxfd, void *readfds, void *writefds, s else retval = select(maxfd, &readfds, &writefds, NULL, &timeout); + /* exit loop if callback returns true */ + if (post_select_cb != NULL && post_select_cb(retval, (void *)&readfds, (void *)&writefds, privdata) != 0) + break; + if (retval < 0 && errno == EINTR) { errno = 0; continue; } else if (retval < 0) { - log_print(LOG_ERROR, "event_loop(): select():"); + log_print(LOG_ERROR, "%s(): select():", __FUNCTION__); continue; } - /* exit loop if callback returns true */ - if (post_select_cb != NULL && post_select_cb(retval, (void *)&readfds, (void *)&writefds, privdata) != 0) - break; - /* timeout */ if (retval == 0) continue; diff --git a/lcd.c b/lcd.c index cb5085f..588df33 100644 --- a/lcd.c +++ b/lcd.c @@ -33,6 +33,7 @@ #include "logging.h" #define _LCD_DEBUG 1 +#define _LCD_DUMMY 1 #define LCD_RESET_TIMEOUT_US 250000 /* 250ms */ #define LCD_RESET_RETRY_TIMEOUT 10 /* 10s */ @@ -59,7 +60,9 @@ enum lcdstate { struct lcddev { int fd; +#if (_LCD_DUMMY) struct event_fd *fakedevice_event; +#endif /* (_LCD_DUMMY) */ struct termios oldtio; enum lcdstate state; @@ -102,14 +105,17 @@ void lcd_close(struct lcddev *dev) dev->read_event = NULL; } - if (dev->fakedevice_event == NULL) { - tcsetattr(dev->fd, TCSANOW, &dev->oldtio); - - } else { +#if (_LCD_DUMMY) + if (dev->fakedevice_event != NULL) { int fd = event_get_fd(dev->fakedevice_event); event_remove_fd(dev->fakedevice_event); dev->fakedevice_event = NULL; close(fd); + + } else +#endif /* (_LCD_DUMMY) */ + { + tcsetattr(dev->fd, TCSANOW, &dev->oldtio); } close(dev->fd); @@ -120,7 +126,7 @@ static int lcd_open(struct lcddev *dev, const char *device) { dev->fd = open(device, O_RDWR); if (dev->fd < 0) { - log_print(LOG_ERROR, "failed to open '%s'", device); + log_print(LOG_ERROR, "%s(): failed to open '%s'", __FUNCTION__, device); return -1; } @@ -138,7 +144,7 @@ static int lcd_open(struct lcddev *dev, const char *device) int err = tcsetattr(dev->fd, TCSAFLUSH, &newtio); if (err < 0) { - log_print(LOG_ERROR, "failed to set termios"); + log_print(LOG_ERROR, "%s(): failed to set termios", __FUNCTION__); close(dev->fd); return -1; } @@ -146,6 +152,7 @@ static int lcd_open(struct lcddev *dev, const char *device) return 0; } +#if (_LCD_DUMMY) static int lcd_fakedevice_reply(int fd, void *privdata) { struct lcddev *dev = (struct lcddev *)privdata; @@ -169,8 +176,22 @@ static int lcd_fakedevice_open(struct lcddev *dev) { int fd[2]; - if (socketpair(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0 , fd) < 0) { - log_print(LOG_ERROR, "socketpair() failed"); + if (socketpair(AF_LOCAL, SOCK_STREAM, 0 , fd) < 0) { + log_print(LOG_ERROR, "%s(): socketpair() failed", __FUNCTION__); + return -1; + } + + if (fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0) { + log_print(LOG_ERROR, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__); + close(fd[0]); + close(fd[1]); + return -1; + } + + if (fcntl(fd[1], F_SETFD, FD_CLOEXEC) < 0) { + log_print(LOG_ERROR, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__); + close(fd[0]); + close(fd[1]); return -1; } @@ -178,19 +199,22 @@ static int lcd_fakedevice_open(struct lcddev *dev) dev->fakedevice_event = event_add_readfd(NULL, fd[1], lcd_fakedevice_reply, dev); return 0; } +#endif /* (_LCD_DUMMY) */ #if (_LCD_DEBUG > 1) static void lcd_dump(const char *prefix, int len, int size, const char *buf) { int i; + int pos = 0; + char buf[256]; - fprintf(stderr, "%s:[%d/%d]: ", prefix, len, size); - for (i = 0; i < len; i++) - fprintf(stderr, "0x%X ", (unsigned char)buf[i]); + for (i = 0; i < len; i++) { + pos += snprintf(buf, sizeof(buf) - pos, "0x%X ", (unsigned char)buf[i]); + } - fprintf(stderr, "\n"); + log_print(LOG_DEBUG, "%s:[%d/%d]: %s", prefix, len, size, buf); } -#endif +#endif /* (_LCD_DEBUG > 1) */ static int lcd_read(struct lcddev *dev, const char *buf, int len) { @@ -203,8 +227,8 @@ static int lcd_read(struct lcddev *dev, const char *buf, int len) cnt += retval; } #if (_LCD_DEBUG > 1) - lcd_dump("lcd_read", cnt, len, buf); -#endif + lcd_dump(__FUNCTION__, cnt, len, buf); +#endif /* (_LCD_DEBUG > 1) */ return (cnt != 0) ? cnt : retval; } @@ -213,8 +237,8 @@ static int lcd_write(struct lcddev *dev, char *buf, int len) int retval = write(dev->fd, buf, len); #if (_LCD_DEBUG > 1) - lcd_dump("lcd_write", retval, len, buf); -#endif + lcd_dump(__FUNCTION__, retval, len, buf); +#endif /* (_LCD_DEBUG > 1) */ return retval; } @@ -243,8 +267,8 @@ static int lcd_reset_timeout_cb(void *privdata) void lcd_reset(struct lcddev *dev) { #if (_LCD_DEBUG > 0) - fprintf(stderr, "lcd_reset()\n"); -#endif + log_print(LOG_DEBUG, "%s()", __FUNCTION__); +#endif /* (_LCD_DEBUG > 0) */ char cmd[] = A125_CMD_RESET; lcd_write(dev, cmd, sizeof(cmd)); @@ -263,8 +287,8 @@ static int lcd_backlight(struct lcddev *dev, int mode) return -1; #if (_LCD_DEBUG > 0) - fprintf(stderr, "lcd_backlight(%d)\n", mode); -#endif + log_print(LOG_DEBUG, "%s(%d)", __FUNCTION__, mode); +#endif /* (_LCD_DEBUG > 0) */ if (dev->backlight_state != mode) { char cmd[] = A125_CMD_BACKLIGHT; @@ -358,8 +382,8 @@ static int lcd_setline(struct lcddev *dev, int line, int len, const char *buf) cmd[20] = '\0'; #if (_LCD_DEBUG > 0) - fprintf(stderr, "lcd_setline(%d, '%-16s')\n", line, cmd +4); -#endif + log_print(LOG_DEBUG, "%s(%d, '%-16s')", __FUNCTION__, line, cmd +4); +#endif /* (_LCD_DEBUG > 0) */ lcd_write(dev, cmd, 20); return 0; @@ -436,16 +460,19 @@ struct lcddev * lcd_init(const char *devicename, { struct lcddev *dev = malloc(sizeof(struct lcddev)); if (dev == NULL) { - log_print(LOG_ERROR, "lcd_init(): out of memory"); + log_print(LOG_ERROR, "%s(): out of memory", __FUNCTION__); return NULL; } memset(dev, 0, sizeof(struct lcddev)); int retval; +#if (_LCD_DUMMY) if (strncmp(devicename, "dummy", 5) == 0) { retval = lcd_fakedevice_open(dev); - } else { + } else +#endif /* (_LCD_DUMMY) */ + { retval = lcd_open(dev, devicename); } diff --git a/logging.c b/logging.c index ddc1128..172a535 100644 --- a/logging.c +++ b/logging.c @@ -29,7 +29,7 @@ #define BUFSIZE 8192 -static FILE *log_fd = NULL; +static FILE *log_file = NULL; static int log_prio = LOG_EVERYTIME; static char *buffer = NULL; @@ -44,12 +44,12 @@ int log_print(int prio, const char *fmt, ...) if (buffer == NULL) { buffer = malloc(BUFSIZE); if (buffer == NULL) { - fprintf(stderr, "log_print(): out of memory\n"); + fprintf(stderr, "%s(): out of memory\n", __FUNCTION__); return -1; } } - if (log_fd != NULL) { + if (log_file != NULL) { time_t tzgr; time(&tzgr); @@ -62,7 +62,7 @@ int log_print(int prio, const char *fmt, ...) if (len < 0 || len >= BUFSIZE) { errno = 0; - return log_print(LOG_ERROR, "log_print: arguments too long"); + return log_print(LOG_ERROR, "%s: arguments too long", __FUNCTION__); } if (errno) { @@ -70,8 +70,8 @@ int log_print(int prio, const char *fmt, ...) errno = 0; } - retval = fprintf((log_fd ? log_fd : stderr), "%s\n", buffer); - fflush(log_fd); + retval = fprintf((log_file ? log_file : stderr), "%s\n", buffer); + fflush(log_file); return retval; } @@ -82,25 +82,26 @@ void log_close(void) buffer = NULL; } - if (log_fd) { - fclose(log_fd); - log_fd = NULL; + if (log_file) { + fclose(log_file); + log_file = NULL; } } int log_init(const char *logfile) { - if (log_fd != NULL) + if (log_file != NULL) log_close(); - log_fd = fopen(logfile, "a"); - if (log_fd == NULL) { - fprintf(stderr, "log_init(): can not open logfile"); + log_file = fopen(logfile, "a"); + if (log_file == NULL) { + fprintf(stderr, "%s(): can not open logfile", __FUNCTION__); return -1; } - if (fcntl(fileno(log_fd), F_SETFD, FD_CLOEXEC) < 0) { - fprintf(stderr, "log_init(): fcntl(FD_CLOEXEC)"); + if (fcntl(fileno(log_file), F_SETFD, FD_CLOEXEC) < 0) { + fprintf(stderr, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__); + fclose(log_file); return -1; } diff --git a/signals.c b/signals.c index 4ae7176..c962b03 100644 --- a/signals.c +++ b/signals.c @@ -68,7 +68,7 @@ int signal_remove_callback(int signum, int type) } if (sigaction(signum, &sig_action, NULL) < 0) { - log_print(LOG_WARN, "signal_remove_callback(): sigaction()"); + log_print(LOG_WARN, "%s(): sigaction(%d)", __FUNCTION__, signum); return -1; } @@ -81,7 +81,7 @@ int signal_add_callback(int signum, void (*callback)(void *privdata), void *priv { struct signal_entry *entry = malloc(sizeof(struct signal_entry)); if (entry == NULL) { - log_print(LOG_WARN, "signal_add_callback(): out of memory"); + log_print(LOG_WARN, "%s(): out of memory", __FUNCTION__); return -1; } @@ -96,7 +96,7 @@ int signal_add_callback(int signum, void (*callback)(void *privdata), void *priv }; if (sigaction(signum, &sig_action, NULL) < 0) { - log_print(LOG_WARN, "signal_add_callback(): sigaction()"); + log_print(LOG_WARN, "%s(): sigaction(%d)", __FUNCTION__, signum); list_del(&entry->list); free(entry); return -1; @@ -110,7 +110,7 @@ static int sig_event(int fd, void *privdata) unsigned char signum; int len = read(fd, &signum, 1); if (len <= 0) { - log_print(LOG_WARN, "sig_event(): read()"); + log_print(LOG_WARN, "%s(): read()", __FUNCTION__); return -1; } @@ -132,17 +132,17 @@ static int sig_event(int fd, void *privdata) int signal_init(void) { if (pipe(sig_pipe) < 0) { - log_print(LOG_ERROR, "signal_init(): pipe()"); + log_print(LOG_ERROR, "%s(): pipe()", __FUNCTION__); return -1; } if (fcntl(sig_pipe[0], F_SETFD, FD_CLOEXEC) < 0) { - log_print(LOG_WARN, "signal_init(): fcntl(FD_CLOEXEC)"); + log_print(LOG_WARN, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__); return -1; } if (fcntl(sig_pipe[1], F_SETFD, FD_CLOEXEC) < 0) { - log_print(LOG_WARN, "signal_init(): fcntl(FD_CLOEXEC)"); + log_print(LOG_WARN, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__); return -1; }