summaryrefslogtreecommitdiff
path: root/src/app.c
blob: ac3d217c0117381634080ea0fa10edb33fb8af5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* 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"

/* Time to wait before retrying a failed accept() */
#define LISTEN_BACKOFF_TIME 3.


static void front_timer_cb(EV_P_ ev_timer *w, int revents) {
    fcgy_front *f = w->data;
    ev_io_start(EV_A_ f->listener);
}


static void front_accept_cb(EV_P_ ev_io *w, int revents) {
    fcgy_front *f = w->data;

    int fd = accept(w->fd, NULL, NULL);
    if(fd < 0 && (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN))
        return;

    if(fd < 0) {
        fprintf(stderr, "Error accept()'ing: %s.\n", strerror(errno));
        ev_io_stop(EV_A_ w);
        ev_timer_set(f->backoff, LISTEN_BACKOFF_TIME, 0.);
        ev_timer_start(EV_A_ f->backoff);
        return;
    }

    fconn_fastcgi_create(f->app, fd, fd);
    fprintf(stderr, "HI!\n");
}


static fcgy_front *front_create(fcgy_app *app, fcgy_front_type t, const char *addr, uint16_t port) {
    fcgy_front *f = calloc(1, sizeof(fcgy_front));
    f->app = app;
    f->type = t;
    f->port = port;
    f->fd = -1;
    f->addr = strdup(addr);
    ev_init(f->listener, front_accept_cb);
    ev_init(f->backoff, front_timer_cb);
    f->listener->data = f;
    f->backoff->data = f;
    return f;
}


/* TODO: Better error reporting */
static int front_bind(fcgy_front *f) {
    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))
            return -1;

        f->fd = util_serversock(AF_UNIX, &addr, sizeof(addr), FCGY_CLOEXEC|FCGY_NONBLOCK);
        if(f->fd < 0)
            return -1;
        break;
        }

    case FCGY_FRONT_TCP:
    case FCGY_FRONT_STDIO:
        break; /* TODO */

    }
    return 0;
}


static void front_start(fcgy_front *f) {
    switch(f->type) {
    case FCGY_FRONT_UNIX:
    case FCGY_FRONT_TCP:
        ev_io_set(f->listener, f->fd, EV_READ);
        ev_io_start(EV_DEFAULT_UC_ f->listener);
        break;

    case FCGY_FRONT_STDIO:
        /* TODO: Create connection struct */
        break;
    }
}


static void front_stop(fcgy_front *f) {
    if(f->fd < 0)
        return;

    switch(f->type) {
    case FCGY_FRONT_UNIX:
        unlink(f->addr);
    case FCGY_FRONT_TCP:
        ev_io_stop(EV_DEFAULT_UC_ f->listener);
        ev_timer_stop(EV_DEFAULT_UC_ f->backoff);
        break;

    case FCGY_FRONT_STDIO:
        /* TODO: Nothing? */
        break;
    }

    close(f->fd);
    f->fd = -1;
}


fcgy_app *app_create() {
    fcgy_app *app = calloc(1, sizeof(fcgy_app));
    return app;
}


/* Returns 0 on success, -1 on error.
 * Writes a string to *err on error. */
int app_config(fcgy_app *app, fcgy_config_name name, const char *val, char *err, size_t errlen) {
    fcgy_front *f;
    switch(name) {
    case FCGY_CONFIG_APP_UNIX:
        f = front_create(app, FCGY_FRONT_UNIX, val, 0);
        hlist_prepend(app->fronts, f);
        return 0;
    default:
        assert(0 && "app_config() called on invalid option");
    }
    return -1;
}


/* TODO: Error reporting */
int app_bind(fcgy_app *app) {
    fcgy_front *f;
    int n = 0;
    for(f=app->fronts; n == 0 && f; f=f->next)
        n = front_bind(f);
    return n;
}


void app_run(fcgy_app *app) {
    fcgy_front *f;
    for(f=app->fronts; f; f=f->next)
        front_start(f);
}


void app_destroy(fcgy_app *app) {
    while(app->fronts) {
        fcgy_front *n = app->fronts;
        hlist_remove(app->fronts, n);
        front_stop(n);
        free(n);
    }
    while(app->fconns)
        fconn_fastcgi_destroy(app->fconns);
    free(app);
}