Update Copyright date in files
[MonkeyD.git] / src / monkey.c
blob618ea4f3be4bdcedfdf04465a7d1deeb67b109c5
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.
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 "logfile.h"
43 #include "signals.h"
44 #include "config.h"
45 #include "memory.h"
46 #include "clock.h"
47 #include "cache.h"
48 #include "worker.h"
49 #include "server.h"
50 #include "plugin.h"
51 #include "env.h"
53 #if defined(__DATE__) && defined(__TIME__)
54 static const char MONKEY_BUILT[] = __DATE__ " " __TIME__;
55 #else
56 static const char MONKEY_BUILT[] = "Unknown";
57 #endif
59 void mk_details()
61 printf("* Process ID is %i", getpid());
62 printf("\n* Server socket listening on Port %i", config->serverport);
63 printf("\n* %i threads, %i client connections per thread, total %i\n",
64 config->workers, config->worker_capacity,
65 config->workers * config->worker_capacity);
66 fflush(stdout);
69 void mk_version()
71 printf("Monkey HTTP Daemon %s\n", VERSION);
72 printf("Built : %s (%s %i.%i.%i)\n",
73 MONKEY_BUILT, CC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
74 printf("Home : http://www.monkey-project.com\n");
75 fflush(stdout);
78 void mk_help()
80 printf("Usage : monkey [-c directory] [-D] [-v] [-h]\n\n");
81 printf("Available options:\n");
82 printf(" -c directory\tspecify directory from configuration files\n");
83 printf(" -D\t\trun Monkey as daemon\n");
84 printf(" -v\t\tshow version number\n");
85 printf(" -h\t\tthis help\n\n");
86 exit(0);
89 /* MAIN */
90 int main(int argc, char **argv)
92 int opt;
93 int daemon = 0;
95 config = mk_mem_malloc(sizeof(struct server_config));
96 config->file_config = 0;
98 opterr = 0;
99 while ((opt = getopt(argc, argv, "DSvhc:")) != -1) {
100 switch (opt) {
101 case 'v':
102 mk_version();
103 exit(0);
104 break;
105 case 'h':
106 mk_help();
107 break;
108 case 'D':
109 daemon = 1;
110 break;
111 case 'c':
112 if (strlen(optarg) != 0) {
113 config->file_config = optarg;
114 break;
116 case '?':
117 printf("Monkey: Invalid option or option needs an argument.\n");
118 mk_help();
119 break;
123 if (!config->file_config) {
124 config->file_config = MONKEY_PATH_CONF;
127 mk_version();
128 mk_signal_init();
129 mk_config_start_configure();
130 mk_plugin_init();
132 server_fd = mk_socket_server(config->serverport, config->listen_addr);
134 /* Workers: logger and clock */
135 mk_worker_spawn((void *) mk_logger_worker_init);
136 mk_worker_spawn((void *) mk_clock_worker_init);
138 /* Running Monkey as daemon */
139 if (daemon) {
140 mk_utils_set_daemon();
143 /* Register PID of Monkey */
144 mk_logger_register_pid();
147 mk_mem_pointers_init();
149 /* Create thread keys */
150 pthread_key_create(&request_index, NULL);
151 pthread_key_create(&epoll_fd, NULL);
152 pthread_key_create(&timer, NULL);
153 pthread_key_create(&mk_cache_iov_log, NULL);
154 pthread_key_create(&mk_cache_iov_header, NULL);
155 pthread_key_create(&mk_cache_header_toc, NULL);
156 pthread_key_create(&mk_plugin_event_k, NULL);
158 /* Change process owner */
159 mk_user_set_uidgid();
161 mk_config_sanity_check();
163 /* Launch monkey http workers */
164 mk_server_launch_workers();
166 /* Print server details */
167 mk_details();
169 #ifdef TRACE
170 fprintf(stderr, "\n");
171 MK_TRACE("Monkey TRACE is enabled");
172 #endif
174 /* Plugins Stage 10 */
175 mk_plugin_stage_run(MK_PLUGIN_STAGE_10, 0, NULL, NULL, NULL);
177 /* Server loop, let's listen for incomming clients */
178 mk_server_loop(server_fd);
180 return 0;