summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/app.c27
-rw-r--r--src/app.h2
-rw-r--r--src/main.c23
3 files changed, 38 insertions, 14 deletions
diff --git a/src/app.c b/src/app.c
index 0d3afda..3303472 100644
--- a/src/app.c
+++ b/src/app.c
@@ -72,22 +72,25 @@ static fcgy_front *front_create(fcgy_app *app, fcgy_front_type t, const char *ad
}
-/* TODO: Better error reporting */
-static int front_bind(fcgy_front *f) {
+static int front_bind(fcgy_front *f, char *err, size_t errlen) {
switch(f->type) {
case FCGY_FRONT_UNIX: {
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
int r = snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", f->addr);
- if(r < 0 || r >= (int)sizeof(addr.sun_path))
+ if(r < 0 || r >= (int)sizeof(addr.sun_path)) {
+ snprintf(err, errlen, "invalid socket path");
return -1;
+ }
f->fd = util_serversock(AF_UNIX, &addr, sizeof(addr), FCGY_CLOEXEC|FCGY_NONBLOCK);
- if(f->fd < 0)
+ if(f->fd < 0) {
+ snprintf(err, errlen, "can't bind socket: %s", strerror(errno));
return -1;
- break;
}
+ }
+ break;
case FCGY_FRONT_TCP:
case FCGY_FRONT_STDIO:
@@ -157,13 +160,15 @@ int app_config(fcgy_app *app, fcgy_config_name name, const char *val, char *err,
}
-/* TODO: Error reporting */
-int app_bind(fcgy_app *app) {
+int app_bind(fcgy_app *app, char *err, size_t errlen) {
fcgy_front *f;
- int n = 0;
- for(f=app->fronts; n == 0 && f; f=f->next)
- n = front_bind(f);
- return n;
+ char ebuff[256];
+ for(f=app->fronts; f; f=f->next)
+ if(front_bind(f, ebuff, sizeof(ebuff)) < 0) {
+ snprintf(err, errlen, "%s: %s", f->addr, ebuff);
+ return -1;
+ }
+ return 0;
}
diff --git a/src/app.h b/src/app.h
index 84a0018..f584efd 100644
--- a/src/app.h
+++ b/src/app.h
@@ -53,7 +53,7 @@ struct fcgy_app {
fcgy_app *app_create();
int app_config(fcgy_app *, fcgy_config_name, const char *, char *, size_t);
-int app_bind(fcgy_app *);
+int app_bind(fcgy_app *, char *, size_t);
void app_run(fcgy_app *);
void app_destroy(fcgy_app *);
diff --git a/src/main.c b/src/main.c
index 71c2d83..cdcec31 100644
--- a/src/main.c
+++ b/src/main.c
@@ -24,6 +24,7 @@
#include <yopt.h>
static fcgy_app *app; /* TODO: Needs to be a global array of apps */
+static ev_signal termsig, intsig;
static const yopt_opt_t cli_options[] = {
@@ -78,15 +79,33 @@ static int parse_args(int argc, char **argv) {
}
+static void shutdown_sig(EV_P_ ev_signal *w, int revents) {
+ ev_signal_stop(EV_DEFAULT_UC_ &termsig);
+ ev_signal_stop(EV_DEFAULT_UC_ &intsig);
+ app_destroy(app);
+}
+
+
int main(int argc, char **argv) {
app = app_create();
if(parse_args(argc, argv) < 0)
return 1;
ev_default_loop(0);
- app_bind(app);
+
+ char ebuff[256];
+ if(app_bind(app, ebuff, sizeof(ebuff)) < 0) {
+ fprintf(stderr, "fcgy: %s\n", ebuff);
+ return 1;
+ }
app_run(app);
+
+ ev_signal_init(&termsig, shutdown_sig, SIGTERM);
+ ev_signal_init(&intsig, shutdown_sig, SIGINT);
+ ev_signal_start(EV_DEFAULT_UC_ &termsig);
+ ev_signal_start(EV_DEFAULT_UC_ &intsig);
+
ev_run(EV_DEFAULT_UC_ 0);
- app_destroy(app);
+ fprintf(stderr, "Clean shutdown.\n");
return 0;
}