summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2015-12-12 15:12:33 +0100
committerYorhel <git@yorhel.nl>2015-12-12 15:36:39 +0100
commit7ca0d8c2cadf407ea36c8924e14a01d4e86bf55a (patch)
tree5da9cb50772310a47b6c3b9cd133a57f68fbfe94
parent18701a88de97acb966973c3c84eed2b96fdea1ed (diff)
Initial application config structure + UNIX socket binding
-rw-r--r--COPYING20
-rw-r--r--Makefile.am11
-rwxr-xr-xinit-from-git.sh4
-rw-r--r--src/app.c88
-rw-r--r--src/app.h52
-rw-r--r--src/fcgy.h62
-rw-r--r--src/main.c32
-rw-r--r--src/util.c73
-rw-r--r--src/util.h33
9 files changed, 369 insertions, 6 deletions
diff --git a/COPYING b/COPYING
new file mode 100644
index 0000000..d2768fb
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,20 @@
+Copyright (c) 2015-2016 Yoran Heling
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be included
+in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/Makefile.am b/Makefile.am
index 49e9a59..76e1866 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -32,16 +32,23 @@ EXTRA_DIST+=\
EXTRA_DIST+=deps/klib/khash.h
# ylib
-EXTRA_DIST+=deps/ylib/yuri.h
+EXTRA_DIST+=deps/ylib/yuri.h deps/ylib/vec.h
# fcgy
bin_PROGRAMS=fcgy
fcgy_LDADD=libdeps.a -lm -lpthread $(EV_LIBS)
fcgy_SOURCES=\
- src/main.c
+ src/app.c\
+ src/main.c\
+ src/util.c
nodist_fcgy_SOURCES=version.c
+noinst_HEADERS=\
+ src/app.h\
+ src/fcgy.h\
+ src/util.h
+
MOSTLYCLEANFILES+=version.c
if USE_GIT_VERSION
diff --git a/init-from-git.sh b/init-from-git.sh
index 6e3c3f1..fe41e59 100755
--- a/init-from-git.sh
+++ b/init-from-git.sh
@@ -24,11 +24,13 @@ ev() {
ylib() {
U=http://g.blicky.net/ylib.git/plain
+ UG=http://g.blicky.net/globster.git/plain/src/util
rm -rf ylib
mkdir -p ylib\
&& wget -q $U/yopt.h -O ylib/yopt.h\
&& wget -q $U/yuri.c -O ylib/yuri.c\
- && wget -q $U/yuri.h -O ylib/yuri.h
+ && wget -q $U/yuri.h -O ylib/yuri.h\
+ && wget -q $UG/vec.h -O ylib/vec.h
}
diff --git a/src/app.c b/src/app.c
new file mode 100644
index 0000000..8d3088b
--- /dev/null
+++ b/src/app.c
@@ -0,0 +1,88 @@
+/* Copyright (c) 2015-2016 Yoran Heling
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "fcgy.h"
+
+
+fcgy_app *app_create() {
+ fcgy_app *app = calloc(1, sizeof(fcgy_app));
+ return app;
+}
+
+
+void app_add_input_unix(fcgy_app *app, const char *addr) {
+ fcgy_input in = {0};
+ in.type = FCGY_INPUT_UNIX;
+ in.addr = strdup(addr);
+ in.fd = -1;
+ vec_insert_order(app->inputs, app->inputs.n, in);
+}
+
+
+static int app_bind_unix(fcgy_input *in) {
+ struct sockaddr_un addr;
+ addr.sun_family = AF_UNIX;
+ int r = snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", in->addr);
+ /* TODO: Better error reporting */
+ if(r < 0 || r >= (int)sizeof(addr.sun_path))
+ return -1;
+
+ in->fd = util_serversock(AF_UNIX, &addr, sizeof(addr), FCGY_CLOEXEC|FCGY_NONBLOCK);
+ if(in->fd < 0)
+ return -1;
+
+ return 0;
+}
+
+
+int app_bind(fcgy_app *app) {
+ size_t i;
+ for(i=0; i<app->inputs.n; i++) {
+ int n = -1;
+ switch(app->inputs.a[i].type) {
+ case FCGY_INPUT_UNIX:
+ n = app_bind_unix(app->inputs.a+i);
+ break;
+ default:
+ ;/* TODO: other types */
+ }
+ if(n < 0)
+ return n;
+ }
+ return 0;
+}
+
+
+void app_destroy(fcgy_app *app) {
+ size_t i;
+ for(i=0; i<app->inputs.n; i++) {
+ fcgy_input *in = app->inputs.a+i;
+ if(in->fd < 0)
+ continue;
+ close(in->fd);
+ in->fd = -1;
+ if(in->type == FCGY_INPUT_UNIX)
+ unlink(in->addr);
+ }
+ free(app->inputs.a);
+ free(app);
+}
diff --git a/src/app.h b/src/app.h
new file mode 100644
index 0000000..8f7ad94
--- /dev/null
+++ b/src/app.h
@@ -0,0 +1,52 @@
+/* Copyright (c) 2015-2016 Yoran Heling
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#ifndef FCGY_APP_H
+#define FCGY_APP_H
+
+typedef enum {
+ FCGY_INPUT_STDIO,
+ FCGY_INPUT_UNIX,
+ FCGY_INPUT_TCP
+} fcgy_input_type;
+
+
+typedef struct {
+ fcgy_input_type type;
+ char *addr;
+ uint16_t port;
+ int fd;
+} fcgy_input;
+
+
+typedef struct {
+ vec_t(fcgy_input) inputs;
+ /* TODO: uid / gid */
+ /* TODO: backends & options */
+} fcgy_app;
+
+fcgy_app *app_create();
+void app_add_input_unix(fcgy_app *, const char *);
+int app_bind(fcgy_app *);
+void app_destroy(fcgy_app *);
+
+#endif
diff --git a/src/fcgy.h b/src/fcgy.h
new file mode 100644
index 0000000..ddcc745
--- /dev/null
+++ b/src/fcgy.h
@@ -0,0 +1,62 @@
+/* Copyright (c) 2015-2016 Yoran Heling
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#ifndef FCGY_H
+#define FCGY_H
+
+#include "config.h"
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <math.h>
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/uio.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+
+#include <ev.h>
+
+#include <khash.h>
+#include <yuri.h>
+#include <vec.h>
+
+/* version.c */
+extern char *fcgy_version;
+
+#include "util.h"
+#include "app.h"
+
+#endif
diff --git a/src/main.c b/src/main.c
index 493bd59..f5dd5f3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,10 +1,36 @@
-#include <stdio.h>
+/* Copyright (c) 2015-2016 Yoran Heling
-#include "config.h"
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include <fcgy.h>
-extern char *fcgy_version;
int main(int argc, char **argv) {
printf("Version: %s\n", fcgy_version);
+ ev_default_loop(0);
+
+ fcgy_app *app = app_create();
+ app_add_input_unix(app, "/tmp/fcgy-test");
+ app_bind(app);
+ getchar();
+ app_destroy(app);
return 0;
}
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..b789822
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,73 @@
+/* Copyright (c) 2015-2016 Yoran Heling
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "fcgy.h"
+
+
+int util_socket(int d, int t, int p, uint32_t flags) {
+ int r = socket(d, t, p),
+ fd = r;
+
+ if(r >= 0 && flags & FCGY_NONBLOCK) {
+ r = fcntl(fd, F_GETFL);
+ if(r >= 0)
+ r = fcntl(fd, F_SETFL, r|O_NONBLOCK);
+ }
+
+ if(r >= 0 && flags & FCGY_CLOEXEC) {
+ r = fcntl(fd, F_GETFD);
+ if(r >= 0)
+ r = fcntl(fd, F_SETFD, r|FD_CLOEXEC);
+ }
+
+ if(r < 0) {
+ if(fd >= 0)
+ close(fd);
+ return r;
+ }
+ return fd;
+}
+
+
+int util_serversock(int domain, void *addr, socklen_t addrlen, uint32_t flags) {
+ int fd = util_socket(domain, SOCK_STREAM, 0, flags);
+ if(fd < 0)
+ return fd;
+
+ int r = 1;
+ if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &r, sizeof(int)) < 0) {
+ close(fd);
+ return -1;
+ }
+
+#ifdef IPV6_V6ONLY
+ /* Can fail, ignore error */
+ r = 1;
+ setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &r, sizeof(int));
+#endif
+
+ if(bind(fd, addr, addrlen) < 0 || listen(fd, 64) < 0) {
+ close(fd);
+ return -1;
+ }
+ return fd;
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644
index 0000000..091d1ff
--- /dev/null
+++ b/src/util.h
@@ -0,0 +1,33 @@
+/* Copyright (c) 2015-2016 Yoran Heling
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#ifndef FCGY_UTIL_H
+#define FCGY_UTIL_H
+
+#define FCGY_NONBLOCK 1
+#define FCGY_CLOEXEC 2
+
+int util_socket(int, int, int, uint32_t);
+int util_serversock(int, void *, socklen_t, uint32_t);
+
+#endif
+