Liana Plugin: remove cast
[MonkeyD.git] / src / server.c
blob2dfad4d30ace92484a57eda7ccefc3376e4777ef
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #define _GNU_SOURCE
23 #include <stdio.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
27 #include <sys/time.h>
28 #include <sys/resource.h>
30 #include "monkey.h"
31 #include "config.h"
32 #include "scheduler.h"
33 #include "epoll.h"
34 #include "socket.h"
35 #include "plugin.h"
36 #include "utils.h"
38 /* Return the number of clients that can be attended
39 * at the same time per worker thread
41 int mk_server_worker_capacity(int nworkers)
43 int max, avl;
44 struct rlimit lim;
46 /* Limit by system */
47 getrlimit(RLIMIT_NOFILE, &lim);
48 max = lim.rlim_cur;
50 /* Minimum of fds needed by Monkey:
51 * --------------------------------
52 * 3 fds: stdin, stdout, stderr
53 * 1 fd for main socket server
54 * 1 fd for epoll array (per thread)
55 * 1 fd for worker logger when writing to FS
56 * 2 fd for worker logger pipe
59 avl = max - (3 + 1 + nworkers + 1 + 2);
61 /* The avl is divided by two as we need to consider
62 * a possible additional FD for each plugin working
63 * on the same request.
65 return ((avl / 2) / nworkers);
68 /* Here we launch the worker threads to attend clients */
69 void mk_server_launch_workers()
71 int i;
73 /* Look for plugins thread key data */
74 mk_plugin_preworker_calls();
76 /* Get each worker clients capacity based on FDs system limits */
77 config->worker_capacity = mk_server_worker_capacity(config->workers);
79 /* Launch workers */
80 for (i = 0; i < config->workers; i++) {
81 mk_sched_launch_thread(config->worker_capacity);
85 void mk_server_loop(int server_fd)
87 int remote_fd;
88 struct sockaddr_in sockaddr;
89 struct sched_list_node *sched = sched_list;
91 while (1) {
92 remote_fd = mk_socket_accept(server_fd, sockaddr);
94 if (remote_fd == -1) {
95 continue;
98 #ifdef TRACE
99 MK_TRACE("New connection arrived: FD %i", remote_fd);
100 #endif
102 /* Assign socket to worker thread */
103 mk_sched_add_client(sched, remote_fd);
105 if (sched->next) {
106 sched = sched->next;
108 else {
109 sched = sched_list;