diff --git a/Makefile b/Makefile index 91ef47b..5f6c7a0 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,8 @@ OBJS= $(SRCS:.c=.o) CC=gcc CFLAGS=-O2 -g -DNDEBUG #WARN=-pedantic -Wall -Wnested-externs -Wpointer-arith -Werror -Wno-unused -WARN=-pedantic -Wall -W -Wnested-externs -Wpointer-arith -Wno-long-long +WARN=-pedantic -Wall -W -Wnested-externs -Wpointer-arith -Wno-long-long \ + -Wno-variadic-macros INCS= LIBS=-lpthread diff --git a/files/index.html b/files/index.html index 1638de6..74ecc96 100644 --- a/files/index.html +++ b/files/index.html @@ -28,6 +28,11 @@ +

+ diff --git a/files/init.html b/files/init.html index 49d77e0..6022c00 100644 --- a/files/init.html +++ b/files/init.html @@ -26,6 +26,11 @@ The system is still initialising.

Please wait... +

+ diff --git a/files/maint.html b/files/maint.html index 2b677ba..6f4dab0 100644 --- a/files/maint.html +++ b/files/maint.html @@ -26,6 +26,11 @@ +

+ diff --git a/files/noext.html b/files/noext.html index f2b6e91..3cdc0df 100644 --- a/files/noext.html +++ b/files/noext.html @@ -23,11 +23,17 @@
To install the full web interface and supporting packages a suitably formatted internal hard disk must be present and none was detected. +

This is unexpected - a HDR-Fox T2 should have an internal disk which is already formatted appropriately.

For help, please visit the Hummy.tv Forums +

+
diff --git a/files/noexthd.html b/files/noexthd.html index e6c5650..3a730f2 100644 --- a/files/noexthd.html +++ b/files/noexthd.html @@ -34,6 +34,11 @@ firmware, please see Converting a USB Flash Drive to EXT2 on a HD-Fox T2 on the hummy.tv Wiki for instructions on how to set this up. +

+ diff --git a/files/rma.html b/files/rma.html index 7ea043b..fefb6a2 100644 --- a/files/rma.html +++ b/files/rma.html @@ -25,10 +25,15 @@ still present in flash but components that were on the hard disk have been removed.

- Install a standard Humax firmware to complete the removal of all + Install a standard Humax firmware file to complete the removal of all traces of the custom firmware.

If you change your mind, you can clear RMA mode via the telnet menu. +

+ diff --git a/files/safe.html b/files/safe.html index 3d8bf7a..8e080ec 100644 --- a/files/safe.html +++ b/files/safe.html @@ -27,6 +27,11 @@ +

+ diff --git a/main.c b/main.c index 8a38868..a155a0b 100644 --- a/main.c +++ b/main.c @@ -20,7 +20,7 @@ #include #endif -#define VERSION "1.2" +#define VERSION "1.3" #define CRLF "\r\n" @@ -35,14 +35,14 @@ /* How to cast fd_set structures on this OS */ #define FD_CAST (fd_set *) -/******************************************************************* - * Variables which are only written to before any additional threads - * are created. They are safe without any locking. - */ - -/* The network socket on which we're listening */ +/* The network socket on which we're listening - only written to by main + * thread before other threads are created. */ int listen_sock = -1; +/* Flag indicating reload required. Only accessed using atomic ops. */ +int reload = 0; +/* Index page and its lock. */ char *xindex = "index.html"; +pthread_mutex_t xindex_lock = PTHREAD_MUTEX_INITIALIZER; /******************************************************************** * A fatal error has occured, log and exit. @@ -122,13 +122,13 @@ hexdump(char *s, unsigned long len) #ifdef DEBUGMAX #define debugmax(...) debug(__VA_ARGS__) #else -#define debugmax(...) 0 +#define debugmax(...) #endif #else /* !DEBUG */ -#define debug(...) 0 -#define debugmax(...) 0 +#define debug(...) +#define debugmax(...) #endif /* DEBUG */ @@ -376,9 +376,14 @@ handle_request(void *arg) *req = '\0'; if (!sscanf(line, "GET %80s HTTP", req)) + { debug("Could not extract req from first line."); + goto cleanup; + } else + { debug("Extracted req: %s", req); + } /* Skip additional request headers. */ do @@ -388,6 +393,8 @@ handle_request(void *arg) debugmax("Skipping request line: [%s]", line); } while (strlen(line)); + pthread_mutex_lock(&xindex_lock); + if (!strncmp(req, "/i/", 3)) send_image(fd, req + 3); else if (!strcmp(xindex, "index.html") && !strncmp(req, "/install", 8)) @@ -414,6 +421,8 @@ handle_request(void *arg) else send_file(fd, req + 1, "text/html", 0, 0, 0); + pthread_mutex_unlock(&xindex_lock); + cleanup: shutdown(fd, SHUT_RDWR); @@ -473,6 +482,64 @@ setup_socket(int port_number, int type, int family) return sockfd; } +void +sighup_handler(int sig __attribute__((unused))) +{ + /* Any thread can receive this signal so use atomic or to set + * the global reload variable. */ + __sync_fetch_and_or(&reload, 1); +} + +static void +setup_signals() +{ + struct sigaction sa; + + signal(SIGPIPE, SIG_IGN); + + memset(&sa, '\0', sizeof(sa)); + + sa.sa_handler = sighup_handler; + /*sa.sa_flags = SA_RESTART; */ + if (sigaction(SIGHUP, &sa, NULL) == -1) + perror("sigaction"); +} + +void +check_reload() +{ + char buf[0x100]; + int fd, n; + char *p; + + if (!__sync_bool_compare_and_swap(&reload, 1, 0)) + return; + + debug("Reload requested."); + + if ((fd = open("/tmp/.bootstrap", O_RDONLY)) == -1) + { + perror("open"); + return; + } + + if ((n = read(fd, buf, sizeof(buf) - 1)) > 0) + { + buf[n] = '\0'; + if ((p = strpbrk(buf, "\n\r "))) + *p = '\0'; + pthread_mutex_lock(&xindex_lock); + free(xindex); + xindex = strdup(buf); + pthread_mutex_unlock(&xindex_lock); + debug("Landing page set to '%s'", xindex); + } + else if (n == -1) + perror("read"); + close(fd); + unlink("/tmp/.bootstrap"); +} + int main(int argc, char **argv) { @@ -503,15 +570,18 @@ main(int argc, char **argv) #endif /* DEBUG */ if (argc > 1) - xindex = strdup(argv[1]); + xindex = argv[1]; + + xindex = strdup(xindex); + + debug("Installing signal handlers..."); + setup_signals(); chdir(ROOT); listen_sock = setup_socket(LISTEN_PORT, SOCK_STREAM, AF_INET); debug("Listening..."); - signal(SIGPIPE, SIG_IGN); - /* Main loop - wait for a connection on the socket then spawn a * child thread to perform the health check. */ @@ -533,6 +603,8 @@ main(int argc, char **argv) #endif delay.tv_usec = 0; + check_reload(); + if (select(listen_sock + 1, FD_CAST&readfds, FD_CAST NULL, FD_CAST&exfds, (struct timeval *)&delay) < 0) { @@ -546,7 +618,8 @@ main(int argc, char **argv) { /* New connection, accept and spawn child. */ struct sockaddr_in addr; - int length, newfd; + int newfd; + socklen_t length; pthread_t tid; length = sizeof(addr);