Multi-process operation are not directly integrated

The multi-process model is presented as sample code.

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
main^2
Jianhui Zhao 2021-01-09 00:01:32 +08:00
parent 808bb3785b
commit c28eaa2bb7
12 changed files with 545 additions and 231 deletions

View File

@ -84,7 +84,7 @@ See which configuration are supported
# Run Example
Run
~/libuhttpd/build$ ./example/example
~/libuhttpd/build$ ./example/simple_server -v
Then use the command curl or browser to test

View File

@ -84,7 +84,7 @@
# 运行示例程序
运行
~/libuhttpd/build$ ./example/example -v
~/libuhttpd/build$ ./example/simple_server -v
然后使用命令curl或者浏览器进行测试

View File

@ -5,14 +5,23 @@ include_directories(
${CMAKE_BINARY_DIR}/src
${LIBEV_INCLUDE_DIR})
add_executable(example example.c)
set(LIBS ${LIBEV_LIBRARY})
if(BUILD_SHARED_LIBS)
target_link_libraries(example uhttpd ${LIBEV_LIBRARY})
list(APPEND LIBS uhttpd)
else()
target_link_libraries(example uhttpd_s ${LIBEV_LIBRARY})
list(APPEND LIBS uhttpd_s)
endif()
add_executable(simple_server simple_server.c handler.c)
target_link_libraries(simple_server ${LIBS})
add_executable(multi_process_server_reuseport multi_process_server_reuseport.c handler.c)
target_link_libraries(multi_process_server_reuseport ${LIBS})
add_executable(multi_process_server multi_process_server.c handler.c)
target_link_libraries(multi_process_server ${LIBS})
if(HAVE_DLOPEN)
add_library(test_plugin MODULE test_plugin.c)
set_target_properties(test_plugin PROPERTIES OUTPUT_NAME test_plugin PREFIX "")

114
example/handler.c 100644
View File

@ -0,0 +1,114 @@
/*
* MIT License
*
* Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.com>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "uhttpd.h"
void default_handler(struct uh_connection *conn, int event)
{
if (event != UH_EV_COMPLETE)
return;
conn->serve_file(conn);
}
void echo_handler(struct uh_connection *conn, int event)
{
if (event == UH_EV_COMPLETE) {
struct uh_str path = conn->get_path(conn);
struct uh_str query = conn->get_query(conn);
struct uh_str ua = conn->get_header(conn, "User-Agent");
struct uh_str body = conn->get_body(conn);
conn->send_head(conn, HTTP_STATUS_OK, -1, NULL);
conn->chunk_printf(conn, "I'm Libuhttpd: %s\n", UHTTPD_VERSION_STRING);
conn->chunk_printf(conn, "Method: %s\n", conn->get_method_str(conn));
conn->chunk_printf(conn, "Path: %.*s\n", (int)path.len ,path.p);
conn->chunk_printf(conn, "Query: %.*s\n", (int)query.len, query.p);
conn->chunk_printf(conn, "User-Agent: %.*s\n", (int)ua.len, ua.p);
conn->chunk_printf(conn, "Body: %.*s\n", (int)body.len, body.p);
conn->chunk_end(conn);
conn->done(conn);
}
}
void upload_handler(struct uh_connection *conn, int event)
{
static int fd = -1;
if (event == UH_EV_HEAD_COMPLETE) {
struct uh_str str = conn->get_header(conn, "Content-Length");
int content_length;
char buf[128];
sprintf(buf, "%.*s\n", (int)str.len, str.p);
content_length = atoi(buf);
if (content_length > 1024 * 1024 * 1024) {
conn->error(conn, HTTP_STATUS_INTERNAL_SERVER_ERROR, "Too big");
return;
}
} if (event == UH_EV_BODY) {
struct uh_str body = conn->extract_body(conn);
if (fd < 0) {
fd = open("upload.bin", O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
conn->error(conn, HTTP_STATUS_INTERNAL_SERVER_ERROR, strerror(errno));
return;
}
}
if (write(fd, body.p, body.len) < 0) {
conn->error(conn, HTTP_STATUS_INTERNAL_SERVER_ERROR, strerror(errno));
close(fd);
return;
}
} else if (event == UH_EV_COMPLETE) {
struct stat st;
size_t size = 0;
conn->send_head(conn, HTTP_STATUS_OK, -1, NULL);
if (fd > 0) {
fstat(fd, &st);
close(fd);
fd = -1;
size = st.st_size;
}
conn->chunk_printf(conn, "Upload size: %zd\n", size);
conn->chunk_end(conn);
conn->done(conn);
}
}

34
example/handler.h 100644
View File

@ -0,0 +1,34 @@
/*
* MIT License
*
* Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.com>
*
* 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 _EXAMPLE_HANDLER_H
#define _EXAMPLE_HANDLER_H
#include "uhttpd.h"
void default_handler(struct uh_connection *conn, int event);
void echo_handler(struct uh_connection *conn, int event);
void upload_handler(struct uh_connection *conn, int event);
#endif

View File

@ -22,6 +22,8 @@
* SOFTWARE.
*/
#include <sys/sysinfo.h>
#include <sys/prctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -29,92 +31,23 @@
#include <fcntl.h>
#include <errno.h>
#include "uhttpd.h"
#include "handler.h"
static bool serve_file = false;
static void default_handler(struct uh_connection *conn, int event)
{
if (event != UH_EV_COMPLETE)
return;
if (!serve_file) {
struct uh_str path = conn->get_path(conn);
struct uh_str query = conn->get_query(conn);
struct uh_str ua = conn->get_header(conn, "User-Agent");
struct uh_str body = conn->get_body(conn);
conn->send_head(conn, HTTP_STATUS_OK, -1, NULL);
conn->chunk_printf(conn, "I'm Libuhttpd: %s\n", UHTTPD_VERSION_STRING);
conn->chunk_printf(conn, "Method: %s\n", conn->get_method_str(conn));
conn->chunk_printf(conn, "Path: %.*s\n", (int)path.len ,path.p);
conn->chunk_printf(conn, "Query: %.*s\n", (int)query.len, query.p);
conn->chunk_printf(conn, "User-Agent: %.*s\n", (int)ua.len, ua.p);
conn->chunk_printf(conn, "Body: %.*s\n", (int)body.len, body.p);
conn->chunk_end(conn);
conn->done(conn);
} else {
conn->serve_file(conn);
}
}
static void upload_handler(struct uh_connection *conn, int event)
{
static int fd = -1;
if (event == UH_EV_HEAD_COMPLETE) {
struct uh_str str = conn->get_header(conn, "Content-Length");
int content_length;
char buf[128];
sprintf(buf, "%.*s\n", (int)str.len, str.p);
content_length = atoi(buf);
if (content_length > 1024 * 1024 * 1024) {
conn->error(conn, HTTP_STATUS_INTERNAL_SERVER_ERROR, "Too big");
return;
}
} if (event == UH_EV_BODY) {
struct uh_str body = conn->extract_body(conn);
if (fd < 0) {
fd = open("upload.bin", O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
conn->error(conn, HTTP_STATUS_INTERNAL_SERVER_ERROR, strerror(errno));
return;
}
}
if (write(fd, body.p, body.len) < 0) {
conn->error(conn, HTTP_STATUS_INTERNAL_SERVER_ERROR, strerror(errno));
close(fd);
return;
}
} else if (event == UH_EV_COMPLETE) {
struct stat st;
size_t size = 0;
conn->send_head(conn, HTTP_STATUS_OK, -1, NULL);
if (fd > 0) {
fstat(fd, &st);
close(fd);
fd = -1;
size = st.st_size;
}
conn->chunk_printf(conn, "Upload size: %zd\n", size);
conn->chunk_end(conn);
conn->done(conn);
}
}
#define MAX_WORKER 10
static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
{
int i;
if (w->signum == SIGINT) {
pid_t *workers = w->data;
for (i = 0; i < MAX_WORKER; i++) {
if (workers[i] == 0)
break;
kill(workers[i], SIGKILL);
}
ev_break(loop, EVBREAK_ALL);
uh_log_info("Normal quit\n");
}
@ -129,7 +62,6 @@ static void usage(const char *prog)
" -a addr # Default addr is localhost\n"
" -p port # Default port is 8080\n"
" -s # SSl on\n"
" -f # Serve file\n"
" -P # plugin path\n"
" -w # worker process number, default is equal to available CPUs\n"
" -v # verbose\n", prog);
@ -147,9 +79,10 @@ int main(int argc, char **argv)
const char *docroot = ".";
const char *index_page = "index.html";
const char *addr = "localhost";
int nworker = -1;
pid_t workers[MAX_WORKER] = {};
int nworker = get_nprocs();
int port = 8080;
int opt;
int opt, i;
while ((opt = getopt(argc, argv, "h:i:a:p:sfP:w:v")) != -1) {
switch (opt) {
@ -168,9 +101,6 @@ int main(int argc, char **argv)
case 's':
ssl = true;
break;
case 'f':
serve_file = true;
break;
case 'P':
plugin_path = optarg;
break;
@ -189,6 +119,9 @@ int main(int argc, char **argv)
uh_log_info("libuhttpd version: %s\n", UHTTPD_VERSION_STRING);
if (nworker < 1)
return 0;
signal(SIGPIPE, SIG_IGN);
srv = uh_server_new(loop, addr, port);
@ -204,19 +137,31 @@ int main(int argc, char **argv)
srv->set_index_page(srv, index_page);
srv->set_default_handler(srv, default_handler);
srv->add_path_handler(srv, "/echo", echo_handler);
srv->add_path_handler(srv, "/upload", upload_handler);
if (plugin_path)
srv->load_plugin(srv, plugin_path);
/*
** -1 means automatically to available CPUs
** This function must be called after the Server has been initialized
*/
srv->start_worker(srv, nworker);
for (i = 0; i < nworker - 1; i++) {
pid_t pid = fork();
if (pid < 0) {
uh_log_info("fork: %s\n", strerror(errno));
break;
}
if (pid == 0) {
prctl(PR_SET_PDEATHSIG, SIGKILL);
ev_loop_fork(loop);
ev_run(loop, 0);
return 0;
}
workers[i] = pid;
}
ev_signal_init(&signal_watcher, signal_cb, SIGINT);
signal_watcher.data = workers;
ev_signal_start(loop, &signal_watcher);
ev_run(loop, 0);

View File

@ -0,0 +1,183 @@
/*
* MIT License
*
* Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.com>
*
* 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 <sys/sysinfo.h>
#include <sys/prctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "handler.h"
#define MAX_WORKER 10
static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
{
int i;
if (w->signum == SIGINT) {
pid_t *workers = w->data;
for (i = 0; i < MAX_WORKER; i++) {
if (workers[i] == 0)
break;
kill(workers[i], SIGKILL);
}
ev_break(loop, EVBREAK_ALL);
uh_log_info("Normal quit\n");
}
}
static void usage(const char *prog)
{
fprintf(stderr, "Usage: %s [option]\n"
" -h docroot # Document root, default is .\n"
" -i index_page # Index page, default is index.html\n"
" -a addr # Default addr is localhost\n"
" -p port # Default port is 8080\n"
" -s # SSl on\n"
" -P # plugin path\n"
" -w # worker process number, default is equal to available CPUs\n"
" -v # verbose\n", prog);
exit(1);
}
static void start_server(const char *addr, int port, const char *docroot, const char *index_page, const char *plugin, bool ssl)
{
struct ev_loop *loop = ev_loop_new(0);
struct uh_server *srv = NULL;
signal(SIGPIPE, SIG_IGN);
srv = uh_server_new(loop, addr, port);
if (!srv)
return;
#if UHTTPD_SSL_SUPPORT
if (ssl && srv->ssl_init(srv, "server-cert.pem", "server-key.pem") < 0)
return;
#endif
srv->set_docroot(srv, docroot);
srv->set_index_page(srv, index_page);
srv->set_default_handler(srv, default_handler);
srv->add_path_handler(srv, "/echo", echo_handler);
srv->add_path_handler(srv, "/upload", upload_handler);
if (plugin)
srv->load_plugin(srv, plugin);
ev_run(loop, 0);
}
int main(int argc, char **argv)
{
struct ev_loop *loop = EV_DEFAULT;
struct ev_signal signal_watcher;
const char *plugin_path = NULL;
bool verbose = false;
bool ssl = false;
const char *docroot = ".";
const char *index_page = "index.html";
const char *addr = "localhost";
pid_t workers[MAX_WORKER] = {};
int nworker = get_nprocs();
int port = 8080;
int opt, i;
while ((opt = getopt(argc, argv, "h:i:a:p:sP:w:v")) != -1) {
switch (opt) {
case 'h':
docroot = optarg;
break;
case 'i':
index_page = optarg;
break;
case 'a':
addr = optarg;
break;
case 'p':
port = atoi(optarg);
break;
case 's':
ssl = true;
break;
case 'P':
plugin_path = optarg;
break;
case 'w':
nworker = atoi(optarg);
break;
case 'v':
verbose = true;
break;
default: /* '?' */
usage(argv[0]);
}
}
if (!verbose)
uh_log_threshold(LOG_ERR);
uh_log_info("libuhttpd version: %s\n", UHTTPD_VERSION_STRING);
if (!support_so_reuseport()) {
uh_log_err("Not support SO_REUSEPORT\n");
return -1;
}
if (nworker < 1)
return 0;
for (i = 0; i < nworker; i++) {
pid_t pid = fork();
if (pid < 0) {
uh_log_info("fork: %s\n", strerror(errno));
break;
}
if (pid == 0) {
prctl(PR_SET_PDEATHSIG, SIGKILL);
start_server(addr, port, docroot, index_page, plugin_path, ssl);
return 0;
}
workers[i] = pid;
}
ev_signal_init(&signal_watcher, signal_cb, SIGINT);
signal_watcher.data = workers;
ev_signal_start(loop, &signal_watcher);
ev_run(loop, 0);
ev_loop_destroy(loop);
return 0;
}

View File

@ -0,0 +1,135 @@
/*
* MIT License
*
* Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.com>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "handler.h"
static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents)
{
if (w->signum == SIGINT) {
ev_break(loop, EVBREAK_ALL);
uh_log_info("Normal quit\n");
}
}
static void usage(const char *prog)
{
fprintf(stderr, "Usage: %s [option]\n"
" -h docroot # Document root, default is .\n"
" -i index_page # Index page, default is index.html\n"
" -a addr # Default addr is localhost\n"
" -p port # Default port is 8080\n"
" -s # SSl on\n"
" -P # plugin path\n"
" -v # verbose\n", prog);
exit(1);
}
int main(int argc, char **argv)
{
struct ev_loop *loop = EV_DEFAULT;
struct ev_signal signal_watcher;
struct uh_server *srv = NULL;
const char *plugin_path = NULL;
bool verbose = false;
bool ssl = false;
const char *docroot = ".";
const char *index_page = "index.html";
const char *addr = "localhost";
int port = 8080;
int opt;
while ((opt = getopt(argc, argv, "h:i:a:p:sP:v")) != -1) {
switch (opt) {
case 'h':
docroot = optarg;
break;
case 'i':
index_page = optarg;
break;
case 'a':
addr = optarg;
break;
case 'p':
port = atoi(optarg);
break;
case 's':
ssl = true;
break;
case 'P':
plugin_path = optarg;
break;
case 'v':
verbose = true;
break;
default: /* '?' */
usage(argv[0]);
}
}
if (!verbose)
uh_log_threshold(LOG_ERR);
uh_log_info("libuhttpd version: %s\n", UHTTPD_VERSION_STRING);
signal(SIGPIPE, SIG_IGN);
srv = uh_server_new(loop, addr, port);
if (!srv)
return -1;
#if UHTTPD_SSL_SUPPORT
if (ssl && srv->ssl_init(srv, "server-cert.pem", "server-key.pem") < 0)
goto err;
#endif
srv->set_docroot(srv, docroot);
srv->set_index_page(srv, index_page);
srv->set_default_handler(srv, default_handler);
srv->add_path_handler(srv, "/echo", echo_handler);
srv->add_path_handler(srv, "/upload", upload_handler);
if (plugin_path)
srv->load_plugin(srv, plugin_path);
ev_signal_init(&signal_watcher, signal_cb, SIGINT);
ev_signal_start(loop, &signal_watcher);
ev_run(loop, 0);
err:
srv->free(srv);
free(srv);
ev_loop_destroy(loop);
return 0;
}

View File

@ -142,6 +142,7 @@ install(
FILES
uhttpd.h
log.h
utils.h
buffer/buffer.h
http-parser/http_parser.h
${CMAKE_CURRENT_BINARY_DIR}/config.h

View File

@ -31,8 +31,6 @@
#ifdef HAVE_DLOPEN
#include <dlfcn.h>
#endif
#include <sys/sysinfo.h>
#include <sys/prctl.h>
#include "uhttpd_internal.h"
#include "connection.h"
@ -125,110 +123,6 @@ static void uh_accept_cb(struct ev_loop *loop, struct ev_io *w, int revents)
srv->conns = conn;
}
static void uh_start_accept(struct uh_server_internal *srv)
{
ev_io_init(&srv->ior, uh_accept_cb, srv->sock, EV_READ);
ev_io_start(srv->loop, &srv->ior);
}
static void uh_stop_accept(struct uh_server_internal *srv)
{
ev_io_stop(srv->loop, &srv->ior);
}
static void uh_worker_exit(struct ev_loop *loop, struct ev_child *w, int revents)
{
struct worker *wk = container_of(w, struct worker, w);
uh_log_info("worker %d exit\n", wk->i);
free(wk);
}
static int uh_socket(int family)
{
int on = 1;
int sock;
sock = socket(family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (sock < 0) {
uh_log_err("socket: %s\n", strerror(errno));
return -1;
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) < 0) {
uh_log_err("setsockopt: %s\n", strerror(errno));
close(sock);
return -1;
}
setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(int));
return sock;
}
static int uh_listen(int sock, struct sockaddr *addr, socklen_t addrlen)
{
if (bind(sock, addr, addrlen) < 0) {
uh_log_err("bind: %s\n", strerror(errno));
return -1;
}
listen(sock, SOMAXCONN);
return 0;
}
static void uh_start_worker(struct uh_server *srv, int n)
{
struct uh_server_internal *srvi = (struct uh_server_internal *)srv;
pid_t pids[20];
int i;
if (n < 0)
n = get_nprocs();
if (n < 2)
return;
uh_stop_accept(srvi);
if (srvi->reuseport)
close(srvi->sock);
for (i = 0; i < n; i++) {
pids[i] = fork();
switch (pids[i]) {
case -1:
uh_log_err("fork: %s\n", strerror(errno));
return;
case 0:
prctl(PR_SET_PDEATHSIG, SIGKILL);
ev_loop_fork(srvi->loop);
if (srvi->reuseport) {
srvi->sock = uh_socket(srvi->addr.sa.sa_family);
uh_listen(srvi->sock, &srvi->addr.sa, srvi->addrlen);
}
uh_start_accept(srvi);
uh_log_info("worker %d started\n", i);
ev_run(srvi->loop, 0);
return;
}
}
while (i-- > 0) {
struct worker *w = calloc(1, sizeof(struct worker));
w->i = i;
ev_child_init(&w->w, uh_worker_exit, pids[i], 0);
ev_child_start(srvi->loop, &w->w);
}
}
struct uh_server *uh_server_new(struct ev_loop *loop, const char *host, int port)
{
struct uh_server *srv;
@ -389,6 +283,7 @@ int uh_server_init(struct uh_server *srv, struct ev_loop *loop, const char *host
char addr_str[INET6_ADDRSTRLEN];
socklen_t addrlen;
int sock = -1;
int on = 1;
if (!host || *host == '\0') {
addr.sin.sin_family = AF_INET;
@ -428,34 +323,41 @@ int uh_server_init(struct uh_server *srv, struct ev_loop *loop, const char *host
inet_ntop(AF_INET6, &addr.sin6.sin6_addr, addr_str, sizeof(addr_str));
}
memset(srvi, 0, sizeof(struct uh_server_internal));
srvi->loop = loop ? loop : EV_DEFAULT;
srvi->reuseport = support_so_reuseport();
sock = uh_socket(addr.sa.sa_family);
if (sock < 0)
sock = socket(addr.sa.sa_family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (sock < 0) {
uh_log_err("socket: %s\n", strerror(errno));
return -1;
}
if (uh_listen(sock, &addr.sa, addrlen) < 0)
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) < 0) {
uh_log_err("setsockopt: %s\n", strerror(errno));
goto err;
}
srvi->sock = sock;
setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(int));
uh_start_accept(srvi);
if (bind(sock, &addr.sa, addrlen) < 0) {
uh_log_err("bind: %s\n", strerror(errno));
goto err;
}
srvi->addrlen = addrlen;
memcpy(&srvi->addr, &addr, sizeof(addr));
if (listen(sock, SOMAXCONN) < 0) {
uh_log_err("bind: %s\n", strerror(errno));
goto err;
}
if (uh_log_get_threshold() == LOG_DEBUG) {
saddr2str(&addr.sa, addr_str, sizeof(addr_str), &port);
uh_log_debug("Listen on: %s %d\n", addr_str, port);
}
memset(srvi, 0, sizeof(struct uh_server_internal));
srvi->loop = loop ? loop : EV_DEFAULT;
srvi->sock = sock;
srv->get_loop = uh_get_loop;
srv->free = uh_server_free;
srv->start_worker = uh_start_worker;
#if UHTTPD_SSL_SUPPORT
srv->ssl_init = uh_server_ssl_init;
@ -469,6 +371,9 @@ int uh_server_init(struct uh_server *srv, struct ev_loop *loop, const char *host
srv->set_docroot = uh_set_docroot;
srv->set_index_page = uh_set_index_page;
ev_io_init(&srvi->ior, uh_accept_cb, sock, EV_READ);
ev_io_start(srvi->loop, &srvi->ior);
return 0;
err:

View File

@ -32,6 +32,7 @@
#include "http_parser.h"
#include "config.h"
#include "utils.h"
#include "log.h"
struct uh_str {
@ -84,12 +85,6 @@ typedef void (*uh_path_handler_prototype)(struct uh_connection *conn, int event)
struct uh_server {
struct ev_loop *(*get_loop)(struct uh_server *srv);
void (*free)(struct uh_server *srv);
/*
** Start n worker processes to process the requests
** Must be called after the Server has been initialized
** If n is -1, automatically to available CPUs
*/
void (*start_worker)(struct uh_server *srv, int n);
#if UHTTPD_SSL_SUPPORT
int (*ssl_init)(struct uh_server *srv, const char *cert, const char *key);
#endif

View File

@ -34,13 +34,6 @@ struct uh_connection_internal;
struct uh_server_internal {
struct uh_server com;
int sock;
bool reuseport;
union {
struct sockaddr sa;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
} addr;
socklen_t addrlen;
char *docroot;
char *index_page;
struct ev_loop *loop;