diff options
Diffstat (limited to 'src/multipart-replace-http-server.c')
-rw-r--r-- | src/multipart-replace-http-server.c | 53 |
1 files changed, 33 insertions, 20 deletions
diff --git a/src/multipart-replace-http-server.c b/src/multipart-replace-http-server.c index 0541ff9..bc188b8 100644 --- a/src/multipart-replace-http-server.c +++ b/src/multipart-replace-http-server.c @@ -29,6 +29,8 @@ #include <sys/un.h> #include <unistd.h> +#include <systemd/sd-daemon.h> + #include "util.h" #include "wg.h" #include "multipart-replace.h" @@ -44,23 +46,28 @@ struct httpfile *filev = NULL; struct reader_thread_args { struct multipart_replace_stream *stream; int fd; - const char *filename; - const char *boundary; + char *filename; + char *boundary; }; - +int exitcode = 0; bool running = true; int sock; struct wg wg; void stop(int sig) { - log("Caught %d", sig); - log("STOPPING=1"); + if (sig != 0) + log("Caught %d", sig); + sd_notify(0, "STOPPING=1"); running = false; close(sock); } -#define threaderror(stat, errnum, ...) do { error(0, errnum, __VA_ARGS__); stop(0); } while(0) +#define threaderror(stat, errnum, ...) do { \ + error(0, errnum, __VA_ARGS__); \ + exitcode = stat; \ + stop(0); \ + } while(0) void *reader_thread(void *args_anon) { struct reader_thread_args *args = args_anon; @@ -71,10 +78,12 @@ void *reader_thread(void *args_anon) { pthread_setname_np(pthread_self(), name); debug("starting thread: %s", name); - multipart_replace_reader(args->stream, args->fd, args->boundary); - sleep(5); - threaderror(EXIT_FAILURE, 0, "multipart_replace stream ended: %s", args->filename); - wg_sub(&wg, 1); + int ret = multipart_replace_reader(args->stream, args->fd, args->boundary); + threaderror(ret, 0, "multipart_replace stream ended: %s", args->filename); + free(args->filename); + free(args->boundary); + free(args); + wg_sub(&wg); return NULL; } @@ -83,9 +92,9 @@ void start_multipart_replace_reader(struct multipart_replace_stream *s, int fd, args->stream = s; args->fd = fd; args->filename = strdup(filename); - args->boundary = boundary; + args->boundary = strdup(boundary); pthread_t thread; - wg_add(&wg, 1); + wg_add(&wg); pthread_create(&thread, NULL, reader_thread, args); } @@ -132,13 +141,13 @@ void connection_handler(int fd) { "HTTP/1.1 405 Method Not Allowed\r\n" "Allow: GET\r\n" "\r\n"); - close(fd); + fclose(netstream); return; } char *version = &line_buf[4]; char *path = strsep(&version, " "); if (strcmp(version, "HTTP/1.1\r\n") != 0 && strcmp(version, "HTTP/1.0\r\n")) { - close(fd); + fclose(netstream); return; } path = strdupa(path); @@ -146,6 +155,7 @@ void connection_handler(int fd) { /* run through the rest of the headers */ while (strcmp(line_buf, "\r\n") != 0 && line_len >= 0) line_len = getline(&line_buf, &line_cap, netstream); + free(line_buf); if (strcmp(path, "/") == 0) { log("200 %s", path); @@ -167,7 +177,7 @@ void connection_handler(int fd) { " </ul>\n" "</body>\n" "</html>\n"); - close(fd); + fclose(netstream); return; } struct multipart_replace_stream *vidstream = file_get(path); @@ -187,7 +197,7 @@ void connection_handler(int fd) { "\r\n", boundary); multipart_replace_writer(vidstream, fd, boundary); - close(fd); + fclose(netstream); } void *connection_thread(void *arg_anon) { @@ -196,7 +206,7 @@ void *connection_thread(void *arg_anon) { log("Connection %d opened", fd); connection_handler(fd); log("Connection %d closed", fd); - wg_sub(&wg, 1); + wg_sub(&wg); return NULL; } @@ -323,7 +333,8 @@ void usage() { void cleanup() { fflush(stderr); - sleep(5); /* work around systemd bug dropping log messages */ + if (sd_booted() > 0) sleep(5); /* work around systemd bug dropping log messages */ + _exit(exitcode); } pthread_t main_thread; @@ -372,12 +383,14 @@ int main(int argc, char *argv[]) { if (conn < 0) continue; pthread_t thread; - wg_add(&wg, 1); + wg_add(&wg); pthread_create(&thread, NULL, connection_thread, (void*)(intptr_t)conn); } wg_wait(&wg); - for (size_t i = 0; i < filec; i++) + for (size_t i = 0; i < filec; i++) { destroy_multipart_replace_stream(filev[i].stream); + free(filev[i].stream); + } free(filev); return 0; } |