Little Palm fixes
[MonkeyD.git] / src / monkey.c
blob4833e868cc3a0e3ecec29529328efb85c55d573b
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 #include <stdio.h>
23 #include <sys/types.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <sys/wait.h>
29 #include <resolv.h>
31 #include "monkey.h"
32 #include "socket.h"
33 #include <sys/time.h>
35 #include <string.h>
37 #include "epoll.h"
38 #include "scheduler.h"
39 #include "user.h"
40 #include "info.h"
41 #include "utils.h"
42 #include "signals.h"
43 #include "config.h"
44 #include "memory.h"
45 #include "clock.h"
46 #include "cache.h"
47 #include "worker.h"
48 #include "server.h"
49 #include "plugin.h"
50 #include "env.h"
52 #if defined(__DATE__) && defined(__TIME__)
53 static const char MONKEY_BUILT[] = __DATE__ " " __TIME__;
54 #else
55 static const char MONKEY_BUILT[] = "Unknown";
56 #endif
58 void mk_details()
60 printf("* Process ID is %i", getpid());
61 printf("\n* Server socket listening on Port %i", config->serverport);
62 printf("\n* %i threads, %i client connections per thread, total %i\n",
63 config->workers, config->worker_capacity,
64 config->workers * config->worker_capacity);
65 fflush(stdout);
68 void mk_version()
70 printf("Monkey HTTP Daemon %s\n", VERSION);
71 printf("Built : %s (%s %i.%i.%i)\n",
72 MONKEY_BUILT, CC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
73 printf("Home : http://www.monkey-project.com\n");
74 fflush(stdout);
77 void mk_help()
79 printf("Usage : monkey [-c directory] [-D] [-v] [-h]\n\n");
80 printf("Available options:\n");
81 printf(" -c directory\tspecify configuration files directory\n");
82 printf(" -D\t\trun Monkey as daemon\n");
83 printf(" -v\t\tshow version number\n");
84 printf(" -h\t\tprint this help\n\n");
85 exit(0);
88 /* MAIN */
89 int main(int argc, char **argv)
91 int opt;
92 int daemon = 0;
94 config = mk_mem_malloc(sizeof(struct server_config));
95 config->file_config = 0;
97 opterr = 0;
98 while ((opt = getopt(argc, argv, "DSvhc:")) != -1) {
99 switch (opt) {
100 case 'v':
101 mk_version();
102 exit(0);
103 break;
104 case 'h':
105 mk_help();
106 break;
107 case 'D':
108 daemon = 1;
109 break;
110 case 'c':
111 if (strlen(optarg) != 0) {
112 config->file_config = optarg;
113 break;
115 case '?':
116 printf("Monkey: Invalid option or option needs an argument.\n");
117 mk_help();
118 break;
122 if (!config->file_config) {
123 config->file_config = MONKEY_PATH_CONF;
126 #ifdef TRACE
127 monkey_init_time = time(NULL);
128 MK_TRACE("Monkey TRACE is enabled");
129 envtrace = getenv("MONKEY_TRACE");
130 pthread_mutex_init(&mutex_trace, (pthread_mutexattr_t *) NULL);
131 #endif
133 mk_version();
134 mk_signal_init();
135 mk_config_start_configure();
136 mk_plugin_init();
138 server_fd = mk_socket_server(config->serverport, config->listen_addr);
140 /* Running Monkey as daemon */
141 if (daemon) {
142 mk_utils_set_daemon();
145 /* Workers: logger and clock */
146 mk_worker_spawn((void *) mk_clock_worker_init);
148 /* Register PID of Monkey */
149 mk_utils_register_pid();
151 /* Init mk pointers */
152 mk_mem_pointers_init();
154 /* Create thread keys */
155 pthread_key_create(&request_index, NULL);
156 pthread_key_create(&epoll_fd, NULL);
157 pthread_key_create(&mk_cache_iov_header, NULL);
158 pthread_key_create(&mk_cache_header_toc, NULL);
159 pthread_key_create(&mk_cache_header_lm, NULL);
160 pthread_key_create(&mk_cache_header_cl, NULL);
161 pthread_key_create(&mk_plugin_event_k, NULL);
163 /* Change process owner */
164 mk_user_set_uidgid();
166 /* Configuration sanity check */
167 mk_config_sanity_check();
169 /* Print server details */
170 mk_details();
172 /* Invoke Plugin PRCTX hooks */
173 mk_plugin_core_process();
175 /* Launch monkey http workers */
176 mk_server_launch_workers();
178 /* Server loop, let's listen for incomming clients */
179 mk_server_loop(server_fd);
181 return 0;