forked from buaazp/zimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
266 lines (243 loc) · 7.47 KB
/
main.c
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* zimg - high performance image storage and processing system.
* http://zimg.buaa.us
*
* Copyright (c) 2013, Peter Zhao <[email protected]>.
* All rights reserved.
*
* Use and distribution licensed under the BSD license.
* See the LICENSE file for full text.
*
*/
/**
* @file main.c
* @brief Entrance of zimg.
* @author 招牌疯子 [email protected]
* @version 1.0
* @date 2013-07-19
*/
#include <evhtp.h>
#include <wand/MagickWand.h>
#include <inttypes.h>
#include <unistd.h>
#include <signal.h>
#include "zcommon.h"
#include "zhttpd.h"
#include "zutil.h"
#include "zlog.h"
#include "zcache.h"
extern struct setting settings;
evbase_t *evbase;
static void settings_init(void);
static void sighandler(int signal);
int main(int argc, char **argv);
void kill_server(void);
/**
* @brief settings_init Init the setting with default values.
*/
static void settings_init(void)
{
strcpy(settings.root_path, "./www/index.html");
strcpy(settings.img_path, "./img");
strcpy(settings.log_name, "./log/zimg.log");
settings.port = 4869;
settings.backlog = 1024;
settings.num_threads = 4; /* N workers */
settings.log = false;
settings.cache_on = false;
strcpy(settings.cache_ip, "127.0.0.1");
settings.cache_port = 11211;
settings.max_keepalives = 1;
}
/**
* @brief sighandler Signal process function.
*
* @param signal System signals.
*/
static void sighandler(int signal)
{
LOG_PRINT(LOG_INFO, "Received signal %d: %s. Shutting down.", signal, strsignal(signal));
kill_server();
}
/**
* @brief main The entrance of zimg.
*
* @param argc Count of args.
* @param argv Arg list.
*
* @return It returns a int to system.
*/
int main(int argc, char **argv)
{
int c;
_init_path = getcwd(NULL, 0);
LOG_PRINT(LOG_INFO, "Get init-path: %s", _init_path);
/* Set signal handlers */
sigset_t sigset;
sigemptyset(&sigset);
struct sigaction siginfo = {
.sa_handler = sighandler,
.sa_mask = sigset,
.sa_flags = SA_RESTART,
};
sigaction(SIGINT, &siginfo, NULL);
sigaction(SIGTERM, &siginfo, NULL);
settings_init();
while (-1 != (c = getopt(argc, argv,
"p:"
"t:"
"l"
"c"
"M:"
"m:"
"b:"
"h"
"k:"
)))
{
switch(c)
{
case 'p':
settings.port = atoi(optarg);
break;
case 't':
settings.num_threads = atoi(optarg);
if (settings.num_threads <= 0) {
fprintf(stderr, "Number of threads must be greater than 0\n");
return 1;
}
/* There're other problems when you get above 64 threads.
* In the future we should portably detect # of cores for the
* default.
*/
if (settings.num_threads > 64) {
fprintf(stderr, "WARNING: Setting a high number of worker"
"threads is not recommended.\n"
" Set this value to the number of cores in"
" your machine or less.\n");
}
break;
case 'l':
settings.log = true;
break;
case 'c':
settings.cache_on = true;
break;
case 'M':
strcpy(settings.cache_ip, optarg);
break;
case 'm':
settings.cache_port = atoi(optarg);
break;
case 'b':
settings.backlog = atoi(optarg);
break;
case 'k':
settings.max_keepalives = atoll(optarg);
break;
case 'h':
printf("Usage: ./zimg -p port -t thread_num -M memcached_ip -m memcached_port -l[og] -c[ache] -b backlog_num -k max_keepalives -h[elp]\n");
exit(1);
default:
fprintf(stderr, "Illegal argument \"%c\"\n", c);
return 1;
}
}
//init the Path zimg need to use.
LOG_PRINT(LOG_INFO, "Begin to Init the Path zimg Using...");
// if(is_dir(settings.root_path) != 1)
// {
// if(mk_dir(settings.root_path) != 1)
// {
// LOG_PRINT(LOG_ERROR, "root_path[%s] Create Failed!", settings.root_path);
// return -1;
// }
// }
//
//
//start log module... ./log/zimg.log
if(settings.log)
{
const char *log_path = "./log";
if(is_dir(log_path) != 1)
{
if(mk_dir(log_path) != 1)
{
LOG_PRINT(LOG_ERROR, "log_path[%s] Create Failed!", log_path);
return -1;
}
}
log_init();
}
if(is_dir(settings.img_path) != 1)
{
if(mk_dir(settings.img_path) != 1)
{
LOG_PRINT(LOG_ERROR, "img_path[%s] Create Failed!", settings.img_path);
return -1;
}
}
LOG_PRINT(LOG_INFO,"Paths Init Finished.");
//init memcached connection...
if(settings.cache_on == true)
{
LOG_PRINT(LOG_INFO, "Begin to Init Memcached Connection...");
memcached_st *memc;
memc= memcached_create(NULL);
char mserver[32];
sprintf(mserver, "%s:%d", settings.cache_ip, settings.cache_port);
memcached_server_st *servers = memcached_servers_parse(mserver);
memcached_server_push(memc, servers);
memcached_server_list_free(servers);
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 0);
//使用NO-BLOCK,防止memcache倒掉时挂死
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
LOG_PRINT(LOG_INFO, "Memcached Connection Init Finished.");
if(set_cache("zimg", "1") == -1)
{
LOG_PRINT(LOG_WARNING, "Memcached[%s] Connect Failed!", mserver);
settings.cache_on = false;
}
else
{
LOG_PRINT(LOG_INFO, "memcached connection to: %s", mserver);
settings.cache_on = true;
}
memcached_free(memc);
}
else
LOG_PRINT(LOG_INFO, "Don't use memcached as cache.");
//init magickwand
MagickWandGenesis();
//begin to start httpd...
LOG_PRINT(LOG_INFO, "Begin to Start Httpd Server...");
evbase = event_base_new();
evhtp_t * htp = evhtp_new(evbase, NULL);
evhtp_set_cb(htp, "/dump", dump_request_cb, NULL);
evhtp_set_cb(htp, "/upload", post_request_cb, NULL);
//evhtp_set_gencb(htp, echo_cb, NULL);
evhtp_set_gencb(htp, send_document_cb, NULL);
#ifndef EVHTP_DISABLE_EVTHR
evhtp_use_threads(htp, NULL, settings.num_threads, NULL);
#endif
evhtp_set_max_keepalive_requests(htp, settings.max_keepalives);
evhtp_bind_socket(htp, "0.0.0.0", settings.port, settings.backlog);
event_base_loop(evbase, 0);
evhtp_unbind_socket(htp);
evhtp_free(htp);
event_base_free(evbase);
MagickWandTerminus();
fprintf(stdout, "\nByebye!\n");
return 0;
}
/**
* @brief kill_server Kill threads and exit the event_base_loop.
*/
void kill_server(void)
{
LOG_PRINT(LOG_INFO, "Stopping socket listener event loop.");
if (event_base_loopexit(evbase, NULL)) {
LOG_PRINT(LOG_ERROR, "Error shutting down server");
}
LOG_PRINT(LOG_INFO, "Stopping workers.\n");
}