blob: 6d6103c0e76e80c2c6d86b7f3e67367514ac757d [file] [log] [blame]
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00001
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00002#include <ngx_config.h>
Igor Sysoevb2620632003-01-10 06:09:20 +00003#include <ngx_core.h>
Igor Sysoev239baac2003-06-11 15:28:34 +00004#include <ngx_event.h>
Igor Sysoevaa3436c2003-05-30 14:27:59 +00005#include <nginx.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00006
7
Igor Sysoevfc5a10a2004-03-09 19:47:07 +00008static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
Igor Sysoeve9b2cb12004-02-09 20:47:18 +00009static ngx_int_t ngx_getopt(ngx_master_ctx_t *ctx, ngx_cycle_t *cycle);
Igor Sysoev43f13192004-04-12 16:38:09 +000010static void *ngx_core_module_create_conf(ngx_cycle_t *cycle);
11static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
Igor Sysoev03420a62004-01-20 20:40:08 +000012static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
Igor Sysoev2b542382002-08-20 14:48:28 +000013
Igor Sysoev7f125082003-07-11 15:17:50 +000014
Igor Sysoev7f125082003-07-11 15:17:50 +000015static ngx_command_t ngx_core_commands[] = {
16
Igor Sysoev2b58fbf2003-12-09 15:08:11 +000017 { ngx_string("daemon"),
Igor Sysoev43f13192004-04-12 16:38:09 +000018 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
19 ngx_conf_set_flag_slot,
Igor Sysoev2b58fbf2003-12-09 15:08:11 +000020 0,
21 offsetof(ngx_core_conf_t, daemon),
22 NULL },
Igor Sysoev7f125082003-07-11 15:17:50 +000023
Igor Sysoevf0677992004-01-08 17:08:10 +000024 { ngx_string("master_process"),
Igor Sysoev43f13192004-04-12 16:38:09 +000025 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
26 ngx_conf_set_flag_slot,
Igor Sysoev3c3ca172004-01-05 20:55:48 +000027 0,
Igor Sysoevf0677992004-01-08 17:08:10 +000028 offsetof(ngx_core_conf_t, master),
Igor Sysoev3c3ca172004-01-05 20:55:48 +000029 NULL },
30
Igor Sysoeva741f8d2004-03-30 20:31:58 +000031 { ngx_string("worker_processes"),
Igor Sysoev43f13192004-04-12 16:38:09 +000032 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
33 ngx_conf_set_num_slot,
Igor Sysoeva741f8d2004-03-30 20:31:58 +000034 0,
35 offsetof(ngx_core_conf_t, worker_processes),
36 NULL },
37
Igor Sysoev32fcd5c2004-07-05 06:55:54 +000038#if (NGX_THREADS)
39
40 { ngx_string("worker_threads"),
41 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
42 ngx_conf_set_num_slot,
43 0,
44 offsetof(ngx_core_conf_t, worker_threads),
45 NULL },
46
47 { ngx_string("thread_stack_size"),
48 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
49 ngx_conf_set_size_slot,
50 0,
51 offsetof(ngx_core_conf_t, thread_stack_size),
52 NULL },
53
54#endif
55
Igor Sysoev43f13192004-04-12 16:38:09 +000056 { ngx_string("user"),
57 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12,
58 ngx_set_user,
59 0,
60 0,
61 NULL },
62
Igor Sysoeve9b2cb12004-02-09 20:47:18 +000063 { ngx_string("pid"),
Igor Sysoev43f13192004-04-12 16:38:09 +000064 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
65 ngx_conf_set_str_slot,
Igor Sysoeve9b2cb12004-02-09 20:47:18 +000066 0,
67 offsetof(ngx_core_conf_t, pid),
68 NULL },
69
Igor Sysoev2b58fbf2003-12-09 15:08:11 +000070 ngx_null_command
Igor Sysoev7f125082003-07-11 15:17:50 +000071};
72
73
Igor Sysoev43f13192004-04-12 16:38:09 +000074static ngx_core_module_t ngx_core_module_ctx = {
75 ngx_string("core"),
76 ngx_core_module_create_conf,
77 ngx_core_module_init_conf
78};
79
80
Igor Sysoev7f125082003-07-11 15:17:50 +000081ngx_module_t ngx_core_module = {
82 NGX_MODULE,
Igor Sysoev43f13192004-04-12 16:38:09 +000083 &ngx_core_module_ctx, /* module context */
Igor Sysoev7f125082003-07-11 15:17:50 +000084 ngx_core_commands, /* module directives */
85 NGX_CORE_MODULE, /* module type */
Igor Sysoev43f13192004-04-12 16:38:09 +000086 NULL, /* init module */
Igor Sysoev7f125082003-07-11 15:17:50 +000087 NULL /* init child */
88};
89
90
Igor Sysoev3f4685f2004-04-25 20:13:21 +000091ngx_uint_t ngx_max_module;
Igor Sysoeva9830112003-05-19 16:39:14 +000092
Igor Sysoev3c3ca172004-01-05 20:55:48 +000093
Igor Sysoevfc5a10a2004-03-09 19:47:07 +000094int main(int argc, char *const *argv)
Igor Sysoev6abfde62003-07-01 15:00:03 +000095{
Igor Sysoev3c3ca172004-01-05 20:55:48 +000096 ngx_int_t i;
Igor Sysoev3c3ca172004-01-05 20:55:48 +000097 ngx_log_t *log;
98 ngx_cycle_t *cycle, init_cycle;
Igor Sysoev3c3ca172004-01-05 20:55:48 +000099 ngx_core_conf_t *ccf;
Igor Sysoevf0677992004-01-08 17:08:10 +0000100 ngx_master_ctx_t ctx;
Igor Sysoev6abfde62003-07-01 15:00:03 +0000101
Igor Sysoev732a2712004-04-21 18:54:33 +0000102#if defined __FreeBSD__
Igor Sysoev62260f22003-12-05 17:07:27 +0000103 ngx_debug_init();
Igor Sysoev9d639522003-07-07 06:11:50 +0000104#endif
105
Igor Sysoevbc5c2872003-07-02 05:01:53 +0000106 /* TODO */ ngx_max_sockets = -1;
Igor Sysoev6abfde62003-07-01 15:00:03 +0000107
Igor Sysoeva8fa0a62003-11-25 20:44:56 +0000108 ngx_time_init();
Igor Sysoevf0677992004-01-08 17:08:10 +0000109
Igor Sysoeve89c4582003-12-19 08:15:11 +0000110#if (HAVE_PCRE)
Igor Sysoeva8fa0a62003-11-25 20:44:56 +0000111 ngx_regex_init();
Igor Sysoeve89c4582003-12-19 08:15:11 +0000112#endif
Igor Sysoev562e53e2003-11-13 06:14:05 +0000113
Igor Sysoev6abfde62003-07-01 15:00:03 +0000114 log = ngx_log_init_errlog();
Igor Sysoev25b36fe2004-02-03 16:43:54 +0000115 ngx_pid = ngx_getpid();
Igor Sysoev6abfde62003-07-01 15:00:03 +0000116
Igor Sysoev1c3567e2004-07-15 16:35:51 +0000117#if (NGX_OPENSSL)
118 ngx_ssl_init(log);
119#endif
120
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000121 /* init_cycle->log is required for signal handlers and ngx_getopt() */
Igor Sysoeve89c4582003-12-19 08:15:11 +0000122
123 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
124 init_cycle.log = log;
125 ngx_cycle = &init_cycle;
126
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000127 ngx_memzero(&ctx, sizeof(ngx_master_ctx_t));
128 ctx.argc = argc;
129 ctx.argv = argv;
130
131 if (ngx_getopt(&ctx, &init_cycle) == NGX_ERROR) {
132 return 1;
133 }
134
Igor Sysoev6abfde62003-07-01 15:00:03 +0000135 if (ngx_os_init(log) == NGX_ERROR) {
136 return 1;
137 }
138
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000139 if (!(init_cycle.pool = ngx_create_pool(1024, log))) {
140 return 1;
141 }
142
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000143 if (ngx_add_inherited_sockets(&init_cycle) == NGX_ERROR) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000144 return 1;
145 }
146
Igor Sysoev68df19d2004-04-15 15:34:36 +0000147 ngx_max_module = 0;
148 for (i = 0; ngx_modules[i]; i++) {
149 ngx_modules[i]->index = ngx_max_module++;
150 }
151
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000152 cycle = ngx_init_cycle(&init_cycle);
Igor Sysoevbc5c2872003-07-02 05:01:53 +0000153 if (cycle == NULL) {
Igor Sysoev9bfb4342004-04-18 19:06:02 +0000154 if (ngx_test_config) {
155 ngx_log_error(NGX_LOG_EMERG, log, 0,
156 "the configuration file %s test failed",
157 init_cycle.conf_file.data);
158 }
159
Igor Sysoev6abfde62003-07-01 15:00:03 +0000160 return 1;
161 }
162
Igor Sysoev68df19d2004-04-15 15:34:36 +0000163 if (ngx_test_config) {
Igor Sysoev9bfb4342004-04-18 19:06:02 +0000164 ngx_log_error(NGX_LOG_INFO, log, 0,
165 "the configuration file %s was tested successfully",
166 init_cycle.conf_file.data);
Igor Sysoev68df19d2004-04-15 15:34:36 +0000167 return 0;
168 }
169
Igor Sysoev9d639522003-07-07 06:11:50 +0000170 ngx_cycle = cycle;
Igor Sysoev340b03b2003-07-04 15:10:33 +0000171
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000172 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
173
Igor Sysoev43f13192004-04-12 16:38:09 +0000174 ngx_process = ccf->master ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000175
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000176#if (WIN32)
177
178#if 0
Igor Sysoeve0207bb2004-06-23 15:18:17 +0000179
180 TODO:
181
Igor Sysoev43f13192004-04-12 16:38:09 +0000182 if (ccf->run_as_service) {
183 if (ngx_service(cycle->log) == NGX_ERROR) {
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000184 return 1;
185 }
186
187 return 0;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000188 }
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000189#endif
190
191#else
Igor Sysoevb8c367c2003-07-10 16:26:57 +0000192
Igor Sysoev43f13192004-04-12 16:38:09 +0000193 if (!ngx_inherited && ccf->daemon) {
Igor Sysoev160d7742003-11-19 16:26:41 +0000194 if (ngx_daemon(cycle->log) == NGX_ERROR) {
Igor Sysoevb8c367c2003-07-10 16:26:57 +0000195 return 1;
196 }
Igor Sysoeve0207bb2004-06-23 15:18:17 +0000197
198 ngx_daemonized = 1;
Igor Sysoevb8c367c2003-07-10 16:26:57 +0000199 }
200
Igor Sysoev43f13192004-04-12 16:38:09 +0000201 if (ngx_create_pidfile(cycle, NULL) == NGX_ERROR) {
Igor Sysoevdc867cd2003-12-14 20:10:27 +0000202 return 1;
203 }
204
Igor Sysoevb8c367c2003-07-10 16:26:57 +0000205#endif
Igor Sysoev6abfde62003-07-01 15:00:03 +0000206
Igor Sysoev630ad0c2004-04-16 05:14:16 +0000207 if (ngx_process == NGX_PROCESS_MASTER) {
208 ngx_master_process_cycle(cycle, &ctx);
209
210 } else {
211 ngx_single_process_cycle(cycle, &ctx);
212 }
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000213
214 return 0;
215}
216
217
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000218static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle)
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000219{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000220 u_char *p, *v, *inherited;
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000221 ngx_socket_t s;
222 ngx_listening_t *ls;
223
Igor Sysoev10a543a2004-03-16 07:10:12 +0000224 inherited = (u_char *) getenv(NGINX_VAR);
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000225
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000226 if (inherited == NULL) {
227 return NGX_OK;
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000228 }
229
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000230 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
231 "using inherited sockets from \"%s\"", inherited);
232
233 ngx_init_array(cycle->listening, cycle->pool,
234 10, sizeof(ngx_listening_t), NGX_ERROR);
235
236 for (p = inherited, v = p; *p; p++) {
237 if (*p == ':' || *p == ';') {
238 s = ngx_atoi(v, p - v);
239 if (s == NGX_ERROR) {
240 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
241 "invalid socket number \"%s\" in "
242 NGINX_VAR " enviroment variable, "
243 "ignoring the rest of the variable", v);
244 break;
245 }
246
Igor Sysoev10a543a2004-03-16 07:10:12 +0000247 v = p + 1;
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000248
Igor Sysoev10a543a2004-03-16 07:10:12 +0000249 if (!(ls = ngx_push_array(&cycle->listening))) {
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000250 return NGX_ERROR;
Igor Sysoev10a543a2004-03-16 07:10:12 +0000251 }
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000252
253 ls->fd = s;
254 }
255 }
256
257 ngx_inherited = 1;
258
259 return ngx_set_inherited_sockets(cycle);
Igor Sysoeva9030eb2004-01-06 16:49:34 +0000260}
261
262
Igor Sysoeva5362982004-03-04 07:04:55 +0000263ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000264{
265 char *env[2], *var, *p;
Igor Sysoev10a543a2004-03-16 07:10:12 +0000266 ngx_uint_t i;
Igor Sysoev80340f02004-01-13 21:33:59 +0000267 ngx_pid_t pid;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000268 ngx_exec_ctx_t ctx;
269 ngx_listening_t *ls;
270
271 ctx.path = argv[0];
272 ctx.name = "new binary process";
273 ctx.argv = argv;
274
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000275 var = ngx_alloc(sizeof(NGINX_VAR)
276 + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000277 cycle->log);
278
Igor Sysoev10a543a2004-03-16 07:10:12 +0000279 p = (char *) ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR));
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000280
281 ls = cycle->listening.elts;
282 for (i = 0; i < cycle->listening.nelts; i++) {
283 p += ngx_snprintf(p, NGX_INT32_LEN + 2, "%u;", ls[i].fd);
284 }
285
Igor Sysoevfc5a10a2004-03-09 19:47:07 +0000286 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "inherited: %s", var);
287
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000288 env[0] = var;
289 env[1] = NULL;
290 ctx.envp = (char *const *) &env;
291
Igor Sysoev6a930452004-03-04 16:34:23 +0000292 pid = ngx_execute(cycle, &ctx);
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000293
294 ngx_free(var);
Igor Sysoev80340f02004-01-13 21:33:59 +0000295
296 return pid;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000297}
298
299
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000300static ngx_int_t ngx_getopt(ngx_master_ctx_t *ctx, ngx_cycle_t *cycle)
301{
302 ngx_int_t i;
303
304 for (i = 1; i < ctx->argc; i++) {
305 if (ctx->argv[i][0] != '-') {
306 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
307 "invalid option: \"%s\"", ctx->argv[i]);
308 return NGX_ERROR;
309 }
310
311 switch (ctx->argv[i][1]) {
312
Igor Sysoev68df19d2004-04-15 15:34:36 +0000313 case 't':
314 ngx_test_config = 1;
315 break;
316
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000317 case 'c':
Igor Sysoevcccc5522004-04-14 20:34:05 +0000318 if (ctx->argv[i + 1] == NULL) {
319 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
320 "the option: \"%s\" requires file name",
321 ctx->argv[i]);
322 return NGX_ERROR;
323 }
324
Igor Sysoev10a543a2004-03-16 07:10:12 +0000325 cycle->conf_file.data = (u_char *) ctx->argv[++i];
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000326 cycle->conf_file.len = ngx_strlen(cycle->conf_file.data);
327 break;
328
329 default:
330 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
331 "invalid option: \"%s\"", ctx->argv[i]);
332 return NGX_ERROR;
333 }
334 }
335
Igor Sysoevc7a2f682004-02-10 16:23:38 +0000336 if (cycle->conf_file.data == NULL) {
Igor Sysoev090849d2004-05-18 20:28:54 +0000337 cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1;
338 cycle->conf_file.data = (u_char *) NGX_CONF_PATH;
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000339 }
340
341 return NGX_OK;
342}
343
344
Igor Sysoev43f13192004-04-12 16:38:09 +0000345static void *ngx_core_module_create_conf(ngx_cycle_t *cycle)
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000346{
347 ngx_core_conf_t *ccf;
348
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000349 if (!(ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t)))) {
Igor Sysoev43f13192004-04-12 16:38:09 +0000350 return NULL;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000351 }
352 /* set by pcalloc()
353 *
354 * ccf->pid = NULL;
Igor Sysoevc7a2f682004-02-10 16:23:38 +0000355 * ccf->newpid = NULL;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000356 */
Igor Sysoev03420a62004-01-20 20:40:08 +0000357 ccf->daemon = NGX_CONF_UNSET;
358 ccf->master = NGX_CONF_UNSET;
Igor Sysoeva741f8d2004-03-30 20:31:58 +0000359 ccf->worker_processes = NGX_CONF_UNSET;
Igor Sysoev32fcd5c2004-07-05 06:55:54 +0000360#if (NGX_THREADS)
361 ccf->worker_threads = NGX_CONF_UNSET;
362 ccf->thread_stack_size = NGX_CONF_UNSET;
363#endif
Igor Sysoeva5362982004-03-04 07:04:55 +0000364 ccf->user = (ngx_uid_t) NGX_CONF_UNSET;
365 ccf->group = (ngx_gid_t) NGX_CONF_UNSET;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000366
Igor Sysoev43f13192004-04-12 16:38:09 +0000367 return ccf;
368}
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000369
Igor Sysoev43f13192004-04-12 16:38:09 +0000370
371static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
372{
373 ngx_core_conf_t *ccf = conf;
374
375 ngx_conf_init_value(ccf->daemon, 1);
376 ngx_conf_init_value(ccf->master, 1);
377 ngx_conf_init_value(ccf->worker_processes, 1);
378
Igor Sysoev32fcd5c2004-07-05 06:55:54 +0000379#if (NGX_THREADS)
380 ngx_conf_init_value(ccf->worker_threads, 0);
381 ngx_threads_n = ccf->worker_threads;
382 ngx_conf_init_size_value(ccf->thread_stack_size, 2 * 1024 * 1024);
383#endif
384
Igor Sysoev43f13192004-04-12 16:38:09 +0000385#if !(WIN32)
386
387 /* TODO: default "nobody" user */
388
389 if (ccf->pid.len == 0) {
Igor Sysoev090849d2004-05-18 20:28:54 +0000390 ccf->pid.len = sizeof(NGX_PID_PATH) - 1;
391 ccf->pid.data = NGX_PID_PATH;
392 ccf->newpid.len = sizeof(NGX_PID_PATH NGX_NEWPID_EXT) - 1;
393 ccf->newpid.data = NGX_PID_PATH NGX_NEWPID_EXT;
Igor Sysoev43f13192004-04-12 16:38:09 +0000394
395 } else {
Igor Sysoev090849d2004-05-18 20:28:54 +0000396 ccf->newpid.len = ccf->pid.len + sizeof(NGX_NEWPID_EXT);
Igor Sysoev43f13192004-04-12 16:38:09 +0000397
398 if (!(ccf->newpid.data = ngx_palloc(cycle->pool, ccf->newpid.len))) {
399 return NGX_CONF_ERROR;
400 }
401
402 ngx_memcpy(ngx_cpymem(ccf->newpid.data, ccf->pid.data, ccf->pid.len),
Igor Sysoev090849d2004-05-18 20:28:54 +0000403 NGX_NEWPID_EXT, sizeof(NGX_NEWPID_EXT));
Igor Sysoev43f13192004-04-12 16:38:09 +0000404 }
405#endif
406
407 return NGX_CONF_OK;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000408}
Igor Sysoev03420a62004-01-20 20:40:08 +0000409
410
411static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
412{
Igor Sysoeva5362982004-03-04 07:04:55 +0000413#if (WIN32)
414
415 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
416 "\"user\" is not supported, ignored");
417
418 return NGX_CONF_OK;
419
420#else
421
Igor Sysoev43f13192004-04-12 16:38:09 +0000422 ngx_core_conf_t *ccf = conf;
423
Igor Sysoev03420a62004-01-20 20:40:08 +0000424 struct passwd *pwd;
425 struct group *grp;
426 ngx_str_t *value;
Igor Sysoev03420a62004-01-20 20:40:08 +0000427
428 if (ccf->user != (uid_t) NGX_CONF_UNSET) {
429 return "is duplicate";
430 }
431
432 value = (ngx_str_t *) cf->args->elts;
433
Igor Sysoev10a543a2004-03-16 07:10:12 +0000434 pwd = getpwnam((const char *) value[1].data);
Igor Sysoev03420a62004-01-20 20:40:08 +0000435 if (pwd == NULL) {
436 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
437 "getpwnam(%s) failed", value[1].data);
438 return NGX_CONF_ERROR;
439 }
440
441 ccf->user = pwd->pw_uid;
442
443 if (cf->args->nelts == 2) {
444 return NGX_CONF_OK;
445 }
446
Igor Sysoev10a543a2004-03-16 07:10:12 +0000447 grp = getgrnam((const char *) value[2].data);
Igor Sysoev03420a62004-01-20 20:40:08 +0000448 if (grp == NULL) {
449 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
450 "getgrnam(%s) failed", value[1].data);
451 return NGX_CONF_ERROR;
452 }
453
454 ccf->group = grp->gr_gid;
455
456 return NGX_CONF_OK;
Igor Sysoeva5362982004-03-04 07:04:55 +0000457
458#endif
Igor Sysoev03420a62004-01-20 20:40:08 +0000459}