summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-03-22 23:26:29 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-03-22 23:26:29 -0400
commitd3a6398406f1a3f222c7ca00d4e4608267148456 (patch)
treef6ae31c550d45d9e95a4a5df69d1a44aea118adf
parente90c4a77b7783468048bc4bc49eb0423ae676a78 (diff)
stuff
-rw-r--r--Makefile20
-rw-r--r--freenect-server.c6
-rw-r--r--multipart-replace-http-server.c29
-rw-r--r--multipart-replace.c61
4 files changed, 77 insertions, 39 deletions
diff --git a/Makefile b/Makefile
index f9df186..05f71c9 100644
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,25 @@
# Copyright 2016 Luke Shumaker
-CFLAGS += -g -std=c99 -Wall -Werror -Wextra -pedantic
-CPPFLAGS += -std=c99 -Wall -Werror -Wextra -pedantic
+# The source is C99 written for GNU
+CFLAGS += -std=c99
+CPPFLAGS += -std=c99
CPPFLAGS += -D_GNU_SOURCE
+# Be hyper-strict
+CFLAGS += -Wall -Werror -Wextra -pedantic
+CPPFLAGS += -Wall -Werror -Wextra -pedantic
+
+# Debugging
+CFLAGS += -g -fvar-tracking-assignments
+CPPFLAGS += -g -fvar-tracking-assignments
+
+# Enable the stack protector
+CFLAGS += -fstack-protector
+
+# Enable FORTIFY_SOURCE
+CFLAGS += -O2
+CPPFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2
+
all: freenect-server multipart-replace-http-server
.PHONY: all
diff --git a/freenect-server.c b/freenect-server.c
index 558b837..c5ed99a 100644
--- a/freenect-server.c
+++ b/freenect-server.c
@@ -240,8 +240,10 @@ int main(int argc, char *argv[]) {
dup2(2, 1);
usage();
return EXIT_FAILURE;
- }
-
+ }
+
+ jpeg_init();
+
res = freenect_init(&ctx, 0);
if (res) {
error(EXIT_FAILURE, 0, "freenect_init: %s", libusb_strerror(res));
diff --git a/multipart-replace-http-server.c b/multipart-replace-http-server.c
index d4f0cd3..29b0ff5 100644
--- a/multipart-replace-http-server.c
+++ b/multipart-replace-http-server.c
@@ -27,21 +27,30 @@ struct httpfile *filev = NULL;
struct reader_thread_args {
struct multipart_replace_stream *stream;
int fd;
+ const char *filename;
const char *boundary;
};
void *reader_thread(void *args_anon) {
- pthread_setname_np(pthread_self(), "reader");
struct reader_thread_args *args = args_anon;
+
+ char *name = alloca(strlen("read ")+strlen(args->filename)+1);
+ strcpy(name, "read ");
+ strcpy(&name[strlen("read ")], args->filename);
+ pthread_setname_np(pthread_self(), name);
+ debug("starting thread: %s", name);
+
multipart_replace_reader(args->stream, args->fd, args->boundary);
- error(EXIT_FAILURE, 0, "multipart_replace stream ended");
+ sleep(5);
+ error(EXIT_FAILURE, 0, "multipart_replace stream ended: %s", args->filename);
return NULL;
}
-void start_multipart_replace_reader(struct multipart_replace_stream *s, int fd, const char *boundary) {
+void start_multipart_replace_reader(struct multipart_replace_stream *s, int fd, const char *filename, const char *boundary) {
struct reader_thread_args *args = xrealloc(NULL, sizeof(struct reader_thread_args));
args->stream = s;
args->fd = fd;
+ args->filename = filename;
args->boundary = boundary;
pthread_t thread;
pthread_create(&thread, NULL, reader_thread, args);
@@ -63,7 +72,7 @@ void file_add(const char *filename) {
strncpy(filev[filec-1].name, shortname, sizeof(filev[filec-1].name));
log("added file #%zd: %s", filec-1, filev[filec-1].name);
filev[filec-1].stream = stream;
- start_multipart_replace_reader(stream, fd, "ffserver" /* FIXME */);
+ start_multipart_replace_reader(stream, fd, filev[filec-1].name, "ffserver" /* FIXME */);
}
struct multipart_replace_stream *file_get(const char *filename) {
@@ -282,6 +291,16 @@ void cleanup() {
sleep(5); /* work around systemd bug dropping log messages */
}
+pthread_t main_thread;
+
+void progname() {
+ char threadname[16];
+ if (pthread_self() != main_thread && pthread_getname_np(pthread_self(), threadname, sizeof(threadname)) == 0)
+ fprintf(stderr, "%s:%s: ", program_invocation_name, threadname);
+ else
+ fprintf(stderr, "%s: ", program_invocation_name);
+}
+
int main(int argc, char *argv[]) {
if (argc >=2 && strcmp(argv[1], "-h") == 0) {
usage();
@@ -293,6 +312,8 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
+ main_thread = pthread_self();
+ error_print_progname = progname;
atexit(cleanup);
int sock = sockstream_listen(argv[1], argv[2]);
diff --git a/multipart-replace.c b/multipart-replace.c
index 78732e6..a10ba4d 100644
--- a/multipart-replace.c
+++ b/multipart-replace.c
@@ -10,6 +10,8 @@
#include "multipart-replace.h"
#include "util.h"
+#define error(stat, errnum, ...) do { error(0, errnum, __VA_ARGS__); return; } while(0)
+
static
char *boundary_line(const char *old) {
size_t len = strlen(old);
@@ -47,28 +49,33 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const
ssize_t line_len = 0;
ssize_t content_length = -1;
+ bool first = true;
while (1) {
content_length = -1;
/* scan for the first non-empty line */
do {
line_len = getline(&line_buf, &line_cap, stream);
- if (line_len < 0) {
- error(0, ferror(stream), "source hung up");
- return;
- }
+ if (line_len < 0)
+ error(EXIT_FAILURE, ferror(stream), "source hung up");
} while (strcmp(line_buf, "\r\n") == 0);
/* make sure it matches the boundary separator */
- if (strcmp(line_buf, boundary) != 0) {
- error(0, 0, "line does not match boundary: \"%s\"", line_buf);
- return;
+ if (first) {
+ while (strcmp(line_buf, boundary) != 0) {
+ line_len = getline(&line_buf, &line_cap, stream);
+ if (line_len < 0)
+ error(EXIT_FAILURE, ferror(stream), "source hung up");
+ }
+ first = false;
+ } else {
+ if (strcmp(line_buf, boundary) != 0)
+ error(EXIT_FAILURE, 0, "line does not match boundary: \"%s\"", line_buf);
}
/* read the frame header (MIME headers) */
+ s->back->len = 0;
do {
line_len = getline(&line_buf, &line_cap, stream);
- if (line_len < 0) {
- error(0, ferror(stream), "source hung up");
- return;
- }
+ if (line_len < 0)
+ error(EXIT_FAILURE, ferror(stream), "source hung up");
/* append the line to the frame contents */
if ((ssize_t)s->back->cap < s->back->len + line_len)
s->back->buf = xrealloc(s->back->buf, s->back->cap = s->back->len + line_len);
@@ -80,17 +87,13 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const
}
} while (strcmp(line_buf, "\r\n") != 0);
/* make sure that it included a Content-length header */
- if (content_length < 0) {
- error(0, 0, "did not get frame length");
- return;
- }
+ if (content_length < 0)
+ error(EXIT_FAILURE, 0, "did not get frame length");
/* read the frame contents */
if ((ssize_t)s->back->cap < s->back->len + content_length)
s->back->buf = xrealloc(s->back->buf, s->back->cap = s->back->len + content_length);
- if (fread(&s->back->buf[s->back->len], content_length, 1, stream) != 1) {
- error(0, ferror(stream), "fread(%zd)", s->back->len);
- return;
- }
+ if (fread(&s->back->buf[s->back->len], content_length, 1, stream) != 1)
+ error(EXIT_FALURE, ferror(stream), "fread(%zd)", s->back->len);
s->back->len += content_length;
/* swap the frames */
@@ -100,6 +103,8 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const
s->back = tmp;
s->framecount++;
pthread_rwlock_unlock(&s->frontlock);
+
+ //debug("got frame on %d", fd);
}
}
@@ -128,19 +133,13 @@ void multipart_replace_writer(struct multipart_replace_stream *s, int fd, const
pthread_rwlock_unlock(&s->frontlock);
/* send the frame to the client */
- if (fwrite(boundary, boundary_len, 1, stream) < 1) {
- error(0, ferror(stream), "fwrite(boundary)");
- return;
- }
- if (fwrite(myframe.buf, myframe.len, 1, stream) < 1) {
- error(0, ferror(stream), "fwrite(frame.buf)");
- return;
- }
+ if (fwrite(boundary, boundary_len, 1, stream) < 1)
+ error(EXIT_FAILURE, ferror(stream), "fwrite(boundary)");
+ if (fwrite(myframe.buf, myframe.len, 1, stream) < 1)
+ error(EXIT_FAILURE, ferror(stream), "fwrite(frame.buf)");
/* send a blank line for pleasantness */
- if (fwrite("\r\n", 2, 1, stream) < 1) {
- error(0, ferror(stream), "fwrite(\"\\r\\n\")");
- return;
- }
+ if (fwrite("\r\n", 2, 1, stream) < 1)
+ error(EXIT_FAILURE, ferror(stream), "fwrite(\"\\r\\n\")");
fflush(stream);
}
}