summaryrefslogtreecommitdiffstats
path: root/pttbbs/daemon/barebone/server.c
blob: df347abdb10546a474bf48d2d56f4facce2f3f6a (plain) (blame)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// $Id$
// Barebone TCP socket server daemon based on libevent 2.0

// Copyright (c) 2011, Chen-Yu Tsai <wens@csie.org>
// All rights reserved.

// This is a simple TCP/IP server daemon based on libevent 2.0.
// This program does not depend on anything other than libevent 2.0.
// Without additional code linked in, this program alone will behave as an
// echo server.
//
// You can supply your client_read_cb(), client_event_cb(), setup_client(), or
// setup_program() functions to modify the behavior of the server.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/util.h>
#include <event2/listener.h>

#include "server.h"

static const struct timeval timeout = {600, 0};
const struct timeval *common_timeout = &timeout;

int
split_args(char *line, char ***argp)
{
    int argc = 0;
    char *p, **argv;

    if ((argv = calloc(MAX_ARGS + 1, sizeof(char *))) == NULL)
    return -1;

    while ((p = strsep(&line, " \t\r\n")) != NULL) {
    argv[argc++] = p;

    if (argc == MAX_ARGS)
        break;
    }

    argv = realloc(argv, (argc + 1) * sizeof(char *));
    *argp = argv;

    return argc;
}

void __attribute__((weak))
client_read_cb(struct bufferevent *bev, void *ctx)
{
    bufferevent_write_buffer(bev, bufferevent_get_input(bev));
}

void __attribute__((weak))
client_event_cb(struct bufferevent *bev, short events, void *ctx)
{
    if (events & BEV_EVENT_ERROR)
    perror("Error from bufferevent");
    if (events & (BEV_EVENT_EOF | BEV_EVENT_TIMEOUT | BEV_EVENT_ERROR)) {
    bufferevent_free(bev);
    }
}

void __attribute__((weak))
setup_client(struct event_base *base, evutil_socket_t fd,
        struct sockaddr *address, int socklen)
{
    struct bufferevent *bev = bufferevent_socket_new(base, fd,
        BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
    bufferevent_setcb(bev, client_read_cb, NULL, client_event_cb, NULL);
    bufferevent_set_timeouts(bev, common_timeout, common_timeout);
    bufferevent_enable(bev, EV_READ|EV_WRITE);
}

static void
accept_conn_cb(struct evconnlistener *listener, evutil_socket_t fd,
        struct sockaddr *address, int socklen, void *ctx)
{
    struct event_base *base = evconnlistener_get_base(listener);
    return setup_client(base, fd, address, socklen);
}

int __attribute__((weak)) daemon(int nochdir, int noclose);
void __attribute__((weak)) setup_program();

int main(int argc, char *argv[])
{
    int ch, inetd = 0, run_as_daemon = 1;
    const char *iface_ip = "127.0.0.1:5150";
    struct event_base *base;
    struct evconnlistener *listener;

    while ((ch = getopt(argc, argv, "Dil:h")) != -1)
    switch (ch) {
        case 'D':
        run_as_daemon = 0;
        break;
        case 'i':
        inetd = 1;
        break;
        case 'l':
        iface_ip = optarg;
        break;
        case 'h':
        default:
        fprintf(stderr, "Usage: %s [-D] [-i] [-l interface_ip:port]\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (run_as_daemon && !inetd)
    if (daemon(1, 1) < 0) {
        perror("daemon");
        exit(EXIT_FAILURE);
    }

    base = event_base_new();
    assert(base);

    common_timeout = event_base_init_common_timeout(base, &timeout);

    if (!inetd) {
    struct sockaddr sa;
    int len = sizeof(sa);

    if (evutil_parse_sockaddr_port(iface_ip, &sa, &len) < 0)
        exit(EXIT_FAILURE);

    listener = evconnlistener_new_bind(base, accept_conn_cb, NULL,
        LEV_OPT_CLOSE_ON_FREE | LEV_OPT_CLOSE_ON_EXEC | LEV_OPT_REUSEABLE,
        100, &sa, len);

    if (!listener)
        exit(EXIT_FAILURE);
    } else {
    struct sockaddr sa;
    socklen_t len = sizeof(sa);
    getpeername(0, &sa, &len);
    setup_client(base, 0, &sa, len);
    }

    if (setup_program)
    setup_program();

    signal(SIGPIPE, SIG_IGN);

    event_base_dispatch(base);

    return 0;
}

#ifdef __linux__

int
daemon(int nochdir, int noclose)
{
    int fd;

    switch (fork()) {
    case -1:
        return -1;
    case 0:
        break;
    default:
        _exit(0);
    }

    if (setsid() == -1)
    return -1;

    if (!nochdir)
    chdir("/");

    if (!noclose && (fd = open("/dev/null", O_RDWR)) >= 0) {
    dup2(fd, 0);
    dup2(fd, 1);
    dup2(fd, 2);

    if (fd > 2)
        close(fd);
    }

    return 0;
}

#endif // __linux__