-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathnginx.c
executable file
·113 lines (90 loc) · 2.23 KB
/
nginx.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
#include <string.h>//for strcmp()
#include "ngx_config.h"
#include "ngx_cycle.h"
#include "ngx_log.h"
#include "ngx_process.h"
#include "ngx_process_cycle.h"
#include "ngx_os.h"
#include "ngx_conf_file.h"
extern unsigned int ngx_process;
static int ngx_get_options(int argc, const char *argv[]);
//下面的变量在解析命令行选项流程中赋值
static char *ngx_prefix; //存储命令行 -p 参数
static char *ngx_signal; //存储命令行 -s 参数
int
main(int argc, const char *argv[])
{
ngx_core_conf_t *ccf;
ngx_get_options(argc, argv);
//初始化日志
ngx_log_init(ngx_prefix);
if (ngx_signal) {
return ngx_signal_process(ngx_signal);
}
//获取配置信息,这里是一个伪实现
ccf = ngx_get_conf();
//根据配置文件,确定使用何种进程模式,默认是单进程模式
if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
ngx_process = NGX_PROCESS_MASTER;
}
ngx_init_signals();
//daemonized
if (ccf->daemon) {
ngx_daemon();
}
ngx_create_pidfile(NGX_PID_PATH);
if (ngx_process == NGX_PROCESS_SINGLE) {
ngx_single_process_cycle();
} else {
ngx_master_process_cycle();
}
return 0;
}
static int
ngx_get_options(int argc, const char *argv[])
{
int i;
char *p;
/* nginx -s stop
* argv[0] = "nginx"
* argv[1] = "-s"
* argv[2] = "stop"
*/
for (i = 1; i < argc; i++) {
/* p point "[-]s" */
p = argv[i];
if (*p != '-') {
ngx_log_stderr("invalid option: \"%s\"", argv[i]);
return -1;
}
/* p point "-[s]" */
p++;
while (*p) {
switch (*p++) {
case 's':
if (*p) {
/* support "nginx -sstop" */
ngx_signal = p;
} else if (argv[++i]) {
/* nginx -s stop*/
ngx_signal = argv[i];
} else {
ngx_log_stderr("option \"-s\" requires parameter");
return -1;
}
/* validate ngx_signal value */
if (strcmp(ngx_signal, "stop") == 0) {
goto next;
}
ngx_log_stderr("invalid option: \"-s %s\"", ngx_signal);
return -1;
default:
ngx_log_stderr("invalid option: \"%c\"", *(p - 1));
return -1;
}
}
next:
continue;
}
return 0;
}