Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
Igor Sysoev | b262063 | 2003-01-10 06:09:20 +0000 | [diff] [blame] | 8 | #include <ngx_core.h> |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 9 | #include <ngx_event.h> |
Igor Sysoev | aa3436c | 2003-05-30 14:27:59 +0000 | [diff] [blame] | 10 | #include <nginx.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 11 | |
| 12 | |
Igor Sysoev | fc5a10a | 2004-03-09 19:47:07 +0000 | [diff] [blame] | 13 | static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle); |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 14 | static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv); |
| 15 | static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv); |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 16 | static void *ngx_core_module_create_conf(ngx_cycle_t *cycle); |
| 17 | static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf); |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 18 | static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 19 | static char *ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 20 | |
Igor Sysoev | 7f12508 | 2003-07-11 15:17:50 +0000 | [diff] [blame] | 21 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 22 | static ngx_conf_enum_t ngx_debug_points[] = { |
| 23 | { ngx_string("stop"), NGX_DEBUG_POINTS_STOP }, |
| 24 | { ngx_string("abort"), NGX_DEBUG_POINTS_ABORT }, |
| 25 | { ngx_null_string, 0 } |
| 26 | }; |
| 27 | |
| 28 | |
Igor Sysoev | 7f12508 | 2003-07-11 15:17:50 +0000 | [diff] [blame] | 29 | static ngx_command_t ngx_core_commands[] = { |
| 30 | |
Igor Sysoev | 2b58fbf | 2003-12-09 15:08:11 +0000 | [diff] [blame] | 31 | { ngx_string("daemon"), |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 32 | NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| 33 | ngx_conf_set_flag_slot, |
Igor Sysoev | 2b58fbf | 2003-12-09 15:08:11 +0000 | [diff] [blame] | 34 | 0, |
| 35 | offsetof(ngx_core_conf_t, daemon), |
| 36 | NULL }, |
Igor Sysoev | 7f12508 | 2003-07-11 15:17:50 +0000 | [diff] [blame] | 37 | |
Igor Sysoev | f067799 | 2004-01-08 17:08:10 +0000 | [diff] [blame] | 38 | { ngx_string("master_process"), |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 39 | NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| 40 | ngx_conf_set_flag_slot, |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 41 | 0, |
Igor Sysoev | f067799 | 2004-01-08 17:08:10 +0000 | [diff] [blame] | 42 | offsetof(ngx_core_conf_t, master), |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 43 | NULL }, |
| 44 | |
Igor Sysoev | a741f8d | 2004-03-30 20:31:58 +0000 | [diff] [blame] | 45 | { ngx_string("worker_processes"), |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 46 | NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| 47 | ngx_conf_set_num_slot, |
Igor Sysoev | a741f8d | 2004-03-30 20:31:58 +0000 | [diff] [blame] | 48 | 0, |
| 49 | offsetof(ngx_core_conf_t, worker_processes), |
| 50 | NULL }, |
| 51 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 52 | { ngx_string("debug_points"), |
| 53 | NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| 54 | ngx_conf_set_enum_slot, |
| 55 | 0, |
| 56 | offsetof(ngx_core_conf_t, debug_points), |
| 57 | &ngx_debug_points }, |
| 58 | |
Igor Sysoev | 32fcd5c | 2004-07-05 06:55:54 +0000 | [diff] [blame] | 59 | #if (NGX_THREADS) |
| 60 | |
| 61 | { ngx_string("worker_threads"), |
| 62 | NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| 63 | ngx_conf_set_num_slot, |
| 64 | 0, |
| 65 | offsetof(ngx_core_conf_t, worker_threads), |
| 66 | NULL }, |
| 67 | |
| 68 | { ngx_string("thread_stack_size"), |
| 69 | NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| 70 | ngx_conf_set_size_slot, |
| 71 | 0, |
| 72 | offsetof(ngx_core_conf_t, thread_stack_size), |
| 73 | NULL }, |
| 74 | |
| 75 | #endif |
| 76 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 77 | { ngx_string("user"), |
| 78 | NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12, |
| 79 | ngx_set_user, |
| 80 | 0, |
| 81 | 0, |
| 82 | NULL }, |
| 83 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 84 | { ngx_string("worker_priority"), |
| 85 | NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| 86 | ngx_set_priority, |
| 87 | 0, |
| 88 | 0, |
| 89 | NULL }, |
| 90 | |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 91 | { ngx_string("pid"), |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 92 | NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| 93 | ngx_conf_set_str_slot, |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 94 | 0, |
| 95 | offsetof(ngx_core_conf_t, pid), |
| 96 | NULL }, |
| 97 | |
Igor Sysoev | 2b58fbf | 2003-12-09 15:08:11 +0000 | [diff] [blame] | 98 | ngx_null_command |
Igor Sysoev | 7f12508 | 2003-07-11 15:17:50 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 102 | static ngx_core_module_t ngx_core_module_ctx = { |
| 103 | ngx_string("core"), |
| 104 | ngx_core_module_create_conf, |
| 105 | ngx_core_module_init_conf |
| 106 | }; |
| 107 | |
| 108 | |
Igor Sysoev | 7f12508 | 2003-07-11 15:17:50 +0000 | [diff] [blame] | 109 | ngx_module_t ngx_core_module = { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 110 | NGX_MODULE_V1, |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 111 | &ngx_core_module_ctx, /* module context */ |
Igor Sysoev | 7f12508 | 2003-07-11 15:17:50 +0000 | [diff] [blame] | 112 | ngx_core_commands, /* module directives */ |
| 113 | NGX_CORE_MODULE, /* module type */ |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 114 | NULL, /* init module */ |
Igor Sysoev | aad1b89 | 2004-10-03 20:02:06 +0000 | [diff] [blame] | 115 | NULL /* init process */ |
Igor Sysoev | 7f12508 | 2003-07-11 15:17:50 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | |
Igor Sysoev | 3f4685f | 2004-04-25 20:13:21 +0000 | [diff] [blame] | 119 | ngx_uint_t ngx_max_module; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 120 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 121 | |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 122 | int ngx_cdecl |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 123 | main(int argc, char *const *argv) |
Igor Sysoev | 6abfde6 | 2003-07-01 15:00:03 +0000 | [diff] [blame] | 124 | { |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 125 | ngx_int_t i; |
| 126 | ngx_log_t *log; |
| 127 | ngx_cycle_t *cycle, init_cycle; |
| 128 | ngx_core_conf_t *ccf; |
Igor Sysoev | 6abfde6 | 2003-07-01 15:00:03 +0000 | [diff] [blame] | 129 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 130 | #if (NGX_FREEBSD) |
Igor Sysoev | 62260f2 | 2003-12-05 17:07:27 +0000 | [diff] [blame] | 131 | ngx_debug_init(); |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
Igor Sysoev | bc5c287 | 2003-07-02 05:01:53 +0000 | [diff] [blame] | 134 | /* TODO */ ngx_max_sockets = -1; |
Igor Sysoev | 6abfde6 | 2003-07-01 15:00:03 +0000 | [diff] [blame] | 135 | |
Igor Sysoev | a8fa0a6 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 136 | ngx_time_init(); |
Igor Sysoev | f067799 | 2004-01-08 17:08:10 +0000 | [diff] [blame] | 137 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 138 | #if (NGX_PCRE) |
Igor Sysoev | a8fa0a6 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 139 | ngx_regex_init(); |
Igor Sysoev | e89c458 | 2003-12-19 08:15:11 +0000 | [diff] [blame] | 140 | #endif |
Igor Sysoev | 562e53e | 2003-11-13 06:14:05 +0000 | [diff] [blame] | 141 | |
Igor Sysoev | 25b36fe | 2004-02-03 16:43:54 +0000 | [diff] [blame] | 142 | ngx_pid = ngx_getpid(); |
Igor Sysoev | 6abfde6 | 2003-07-01 15:00:03 +0000 | [diff] [blame] | 143 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 144 | log = ngx_log_init(); |
| 145 | if (log == NULL) { |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 146 | return 1; |
| 147 | } |
Igor Sysoev | 55168f6 | 2004-09-28 20:09:22 +0000 | [diff] [blame] | 148 | |
Igor Sysoev | 1c3567e | 2004-07-15 16:35:51 +0000 | [diff] [blame] | 149 | #if (NGX_OPENSSL) |
| 150 | ngx_ssl_init(log); |
| 151 | #endif |
| 152 | |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 153 | /* init_cycle->log is required for signal handlers and ngx_getopt() */ |
Igor Sysoev | e89c458 | 2003-12-19 08:15:11 +0000 | [diff] [blame] | 154 | |
| 155 | ngx_memzero(&init_cycle, sizeof(ngx_cycle_t)); |
| 156 | init_cycle.log = log; |
| 157 | ngx_cycle = &init_cycle; |
| 158 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 159 | init_cycle.pool = ngx_create_pool(1024, log); |
| 160 | if (init_cycle.pool == NULL) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 161 | return 1; |
| 162 | } |
| 163 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 164 | if (ngx_save_argv(&init_cycle, argc, argv) == NGX_ERROR) { |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 165 | return 1; |
| 166 | } |
| 167 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 168 | if (ngx_getopt(&init_cycle, argc, ngx_argv) == NGX_ERROR) { |
Igor Sysoev | 6d2a14a | 2004-09-27 16:03:21 +0000 | [diff] [blame] | 169 | return 1; |
| 170 | } |
| 171 | |
Igor Sysoev | 8035fd2 | 2004-10-01 15:53:53 +0000 | [diff] [blame] | 172 | if (ngx_test_config) { |
| 173 | log->log_level = NGX_LOG_INFO; |
Igor Sysoev | 8035fd2 | 2004-10-01 15:53:53 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Igor Sysoev | 55168f6 | 2004-09-28 20:09:22 +0000 | [diff] [blame] | 176 | if (ngx_os_init(log) == NGX_ERROR) { |
| 177 | return 1; |
| 178 | } |
| 179 | |
Igor Sysoev | fc5a10a | 2004-03-09 19:47:07 +0000 | [diff] [blame] | 180 | if (ngx_add_inherited_sockets(&init_cycle) == NGX_ERROR) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 181 | return 1; |
| 182 | } |
| 183 | |
Igor Sysoev | 68df19d | 2004-04-15 15:34:36 +0000 | [diff] [blame] | 184 | ngx_max_module = 0; |
| 185 | for (i = 0; ngx_modules[i]; i++) { |
| 186 | ngx_modules[i]->index = ngx_max_module++; |
| 187 | } |
| 188 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 189 | cycle = ngx_init_cycle(&init_cycle); |
Igor Sysoev | bc5c287 | 2003-07-02 05:01:53 +0000 | [diff] [blame] | 190 | if (cycle == NULL) { |
Igor Sysoev | 9bfb434 | 2004-04-18 19:06:02 +0000 | [diff] [blame] | 191 | if (ngx_test_config) { |
| 192 | ngx_log_error(NGX_LOG_EMERG, log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 193 | "the configuration file \"%s\" test failed", |
Igor Sysoev | 9bfb434 | 2004-04-18 19:06:02 +0000 | [diff] [blame] | 194 | init_cycle.conf_file.data); |
| 195 | } |
| 196 | |
Igor Sysoev | 6abfde6 | 2003-07-01 15:00:03 +0000 | [diff] [blame] | 197 | return 1; |
| 198 | } |
| 199 | |
Igor Sysoev | 68df19d | 2004-04-15 15:34:36 +0000 | [diff] [blame] | 200 | if (ngx_test_config) { |
Igor Sysoev | 9bfb434 | 2004-04-18 19:06:02 +0000 | [diff] [blame] | 201 | ngx_log_error(NGX_LOG_INFO, log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 202 | "the configuration file \"%s\" was tested successfully", |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 203 | cycle->conf_file.data); |
Igor Sysoev | 68df19d | 2004-04-15 15:34:36 +0000 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
Igor Sysoev | aad1b89 | 2004-10-03 20:02:06 +0000 | [diff] [blame] | 207 | ngx_os_status(cycle->log); |
| 208 | |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 209 | ngx_cycle = cycle; |
Igor Sysoev | 340b03b | 2003-07-04 15:10:33 +0000 | [diff] [blame] | 210 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 211 | ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
| 212 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 213 | ngx_process = ccf->master ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 214 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 215 | #if (NGX_WIN32) |
Igor Sysoev | 3d58f8c | 2004-01-08 08:47:17 +0000 | [diff] [blame] | 216 | |
| 217 | #if 0 |
Igor Sysoev | e0207bb | 2004-06-23 15:18:17 +0000 | [diff] [blame] | 218 | |
| 219 | TODO: |
| 220 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 221 | if (ccf->run_as_service) { |
| 222 | if (ngx_service(cycle->log) == NGX_ERROR) { |
Igor Sysoev | 3d58f8c | 2004-01-08 08:47:17 +0000 | [diff] [blame] | 223 | return 1; |
| 224 | } |
| 225 | |
| 226 | return 0; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 227 | } |
Igor Sysoev | 3d58f8c | 2004-01-08 08:47:17 +0000 | [diff] [blame] | 228 | #endif |
| 229 | |
| 230 | #else |
Igor Sysoev | b8c367c | 2003-07-10 16:26:57 +0000 | [diff] [blame] | 231 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 232 | if (!ngx_inherited && ccf->daemon) { |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 233 | if (ngx_daemon(cycle->log) == NGX_ERROR) { |
Igor Sysoev | b8c367c | 2003-07-10 16:26:57 +0000 | [diff] [blame] | 234 | return 1; |
| 235 | } |
Igor Sysoev | e0207bb | 2004-06-23 15:18:17 +0000 | [diff] [blame] | 236 | |
| 237 | ngx_daemonized = 1; |
Igor Sysoev | b8c367c | 2003-07-10 16:26:57 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 240 | if (ngx_create_pidfile(cycle, NULL) == NGX_ERROR) { |
Igor Sysoev | dc867cd | 2003-12-14 20:10:27 +0000 | [diff] [blame] | 241 | return 1; |
| 242 | } |
| 243 | |
Igor Sysoev | b8c367c | 2003-07-10 16:26:57 +0000 | [diff] [blame] | 244 | #endif |
Igor Sysoev | 6abfde6 | 2003-07-01 15:00:03 +0000 | [diff] [blame] | 245 | |
Igor Sysoev | 630ad0c | 2004-04-16 05:14:16 +0000 | [diff] [blame] | 246 | if (ngx_process == NGX_PROCESS_MASTER) { |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 247 | ngx_master_process_cycle(cycle); |
Igor Sysoev | 630ad0c | 2004-04-16 05:14:16 +0000 | [diff] [blame] | 248 | |
| 249 | } else { |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 250 | ngx_single_process_cycle(cycle); |
Igor Sysoev | 630ad0c | 2004-04-16 05:14:16 +0000 | [diff] [blame] | 251 | } |
Igor Sysoev | 3d58f8c | 2004-01-08 08:47:17 +0000 | [diff] [blame] | 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 257 | static ngx_int_t |
| 258 | ngx_add_inherited_sockets(ngx_cycle_t *cycle) |
Igor Sysoev | a9030eb | 2004-01-06 16:49:34 +0000 | [diff] [blame] | 259 | { |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 260 | u_char *p, *v, *inherited; |
| 261 | ngx_int_t s; |
| 262 | ngx_listening_t *ls; |
Igor Sysoev | a9030eb | 2004-01-06 16:49:34 +0000 | [diff] [blame] | 263 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 264 | inherited = (u_char *) getenv(NGINX_VAR); |
Igor Sysoev | a9030eb | 2004-01-06 16:49:34 +0000 | [diff] [blame] | 265 | |
Igor Sysoev | fc5a10a | 2004-03-09 19:47:07 +0000 | [diff] [blame] | 266 | if (inherited == NULL) { |
| 267 | return NGX_OK; |
Igor Sysoev | a9030eb | 2004-01-06 16:49:34 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 270 | ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, |
Igor Sysoev | fc5a10a | 2004-03-09 19:47:07 +0000 | [diff] [blame] | 271 | "using inherited sockets from \"%s\"", inherited); |
| 272 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 273 | if (ngx_array_init(&cycle->listening, cycle->pool, 10, |
| 274 | sizeof(ngx_listening_t)) == NGX_ERROR) |
| 275 | { |
| 276 | return NGX_ERROR; |
| 277 | } |
Igor Sysoev | fc5a10a | 2004-03-09 19:47:07 +0000 | [diff] [blame] | 278 | |
| 279 | for (p = inherited, v = p; *p; p++) { |
| 280 | if (*p == ':' || *p == ';') { |
| 281 | s = ngx_atoi(v, p - v); |
| 282 | if (s == NGX_ERROR) { |
| 283 | ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
| 284 | "invalid socket number \"%s\" in " |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 285 | NGINX_VAR " environment variable, " |
Igor Sysoev | fc5a10a | 2004-03-09 19:47:07 +0000 | [diff] [blame] | 286 | "ignoring the rest of the variable", v); |
| 287 | break; |
| 288 | } |
| 289 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 290 | v = p + 1; |
Igor Sysoev | fc5a10a | 2004-03-09 19:47:07 +0000 | [diff] [blame] | 291 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 292 | ls = ngx_array_push(&cycle->listening); |
| 293 | if (ls == NULL) { |
Igor Sysoev | fc5a10a | 2004-03-09 19:47:07 +0000 | [diff] [blame] | 294 | return NGX_ERROR; |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 295 | } |
Igor Sysoev | fc5a10a | 2004-03-09 19:47:07 +0000 | [diff] [blame] | 296 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 297 | ls->fd = (ngx_socket_t) s; |
Igor Sysoev | fc5a10a | 2004-03-09 19:47:07 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | ngx_inherited = 1; |
| 302 | |
| 303 | return ngx_set_inherited_sockets(cycle); |
Igor Sysoev | a9030eb | 2004-01-06 16:49:34 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
Igor Sysoev | a536298 | 2004-03-04 07:04:55 +0000 | [diff] [blame] | 307 | ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv) |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 308 | { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 309 | char *env[3], *var; |
| 310 | u_char *p; |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 311 | ngx_uint_t i; |
Igor Sysoev | 80340f0 | 2004-01-13 21:33:59 +0000 | [diff] [blame] | 312 | ngx_pid_t pid; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 313 | ngx_exec_ctx_t ctx; |
| 314 | ngx_listening_t *ls; |
| 315 | |
| 316 | ctx.path = argv[0]; |
| 317 | ctx.name = "new binary process"; |
| 318 | ctx.argv = argv; |
| 319 | |
Igor Sysoev | fc5a10a | 2004-03-09 19:47:07 +0000 | [diff] [blame] | 320 | var = ngx_alloc(sizeof(NGINX_VAR) |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 321 | + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2, |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 322 | cycle->log); |
| 323 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 324 | p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR)); |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 325 | |
| 326 | ls = cycle->listening.elts; |
| 327 | for (i = 0; i < cycle->listening.nelts; i++) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 328 | p = ngx_sprintf(p, "%ud;", ls[i].fd); |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 331 | *p = '\0'; |
| 332 | |
Igor Sysoev | fc5a10a | 2004-03-09 19:47:07 +0000 | [diff] [blame] | 333 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "inherited: %s", var); |
| 334 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 335 | env[0] = var; |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 336 | |
| 337 | #if (NGX_SETPROCTITLE_USES_ENV) |
| 338 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 339 | /* allocate the spare 300 bytes for the new binary process title */ |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 340 | |
| 341 | env[1] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| 342 | "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| 343 | "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| 344 | "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| 345 | "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; |
| 346 | |
| 347 | env[2] = NULL; |
| 348 | |
| 349 | #else |
| 350 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 351 | env[1] = NULL; |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 352 | |
| 353 | #endif |
| 354 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 355 | ctx.envp = (char *const *) &env; |
| 356 | |
Igor Sysoev | 6a93045 | 2004-03-04 16:34:23 +0000 | [diff] [blame] | 357 | pid = ngx_execute(cycle, &ctx); |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 358 | |
| 359 | ngx_free(var); |
Igor Sysoev | 80340f0 | 2004-01-13 21:33:59 +0000 | [diff] [blame] | 360 | |
| 361 | return pid; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 365 | static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv) |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 366 | { |
| 367 | ngx_int_t i; |
| 368 | |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 369 | for (i = 1; i < argc; i++) { |
| 370 | if (argv[i][0] != '-') { |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 371 | ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 372 | "invalid option: \"%s\"", argv[i]); |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 373 | return NGX_ERROR; |
| 374 | } |
| 375 | |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 376 | switch (argv[i][1]) { |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 377 | |
Igor Sysoev | 68df19d | 2004-04-15 15:34:36 +0000 | [diff] [blame] | 378 | case 't': |
| 379 | ngx_test_config = 1; |
| 380 | break; |
| 381 | |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 382 | case 'c': |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 383 | if (argv[i + 1] == NULL) { |
Igor Sysoev | cccc552 | 2004-04-14 20:34:05 +0000 | [diff] [blame] | 384 | ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
| 385 | "the option: \"%s\" requires file name", |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 386 | argv[i]); |
Igor Sysoev | cccc552 | 2004-04-14 20:34:05 +0000 | [diff] [blame] | 387 | return NGX_ERROR; |
| 388 | } |
| 389 | |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 390 | cycle->conf_file.data = (u_char *) argv[++i]; |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 391 | cycle->conf_file.len = ngx_strlen(cycle->conf_file.data); |
| 392 | break; |
| 393 | |
| 394 | default: |
| 395 | ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 396 | "invalid option: \"%s\"", argv[i]); |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 397 | return NGX_ERROR; |
| 398 | } |
| 399 | } |
| 400 | |
Igor Sysoev | c7a2f68 | 2004-02-10 16:23:38 +0000 | [diff] [blame] | 401 | if (cycle->conf_file.data == NULL) { |
Igor Sysoev | 090849d | 2004-05-18 20:28:54 +0000 | [diff] [blame] | 402 | cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1; |
| 403 | cycle->conf_file.data = (u_char *) NGX_CONF_PATH; |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Igor Sysoev | 6d2a14a | 2004-09-27 16:03:21 +0000 | [diff] [blame] | 406 | if (ngx_conf_full_name(cycle, &cycle->conf_file) == NGX_ERROR) { |
| 407 | return NGX_ERROR; |
| 408 | } |
| 409 | |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 410 | return NGX_OK; |
| 411 | } |
| 412 | |
| 413 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 414 | static ngx_int_t |
| 415 | ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv) |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 416 | { |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 417 | #if (NGX_FREEBSD) |
| 418 | |
| 419 | ngx_os_argv = (char **) argv; |
| 420 | ngx_argc = argc; |
| 421 | ngx_argv = (char **) argv; |
| 422 | |
| 423 | #else |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 424 | size_t len; |
| 425 | ngx_int_t i; |
| 426 | |
| 427 | ngx_os_argv = (char **) argv; |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 428 | ngx_argc = argc; |
| 429 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 430 | ngx_argv = ngx_alloc((argc + 1) * sizeof(char *), cycle->log); |
| 431 | if (ngx_argv == NULL) { |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 432 | return NGX_ERROR; |
| 433 | } |
| 434 | |
| 435 | for (i = 0; i < argc; i++) { |
| 436 | len = ngx_strlen(argv[i]) + 1; |
| 437 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 438 | ngx_argv[i] = ngx_alloc(len, cycle->log); |
| 439 | if (ngx_argv[i] == NULL) { |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 440 | return NGX_ERROR; |
| 441 | } |
| 442 | |
Igor Sysoev | 4959ec4 | 2005-05-23 12:07:45 +0000 | [diff] [blame^] | 443 | (void) ngx_cpystrn((u_char *) ngx_argv[i], (u_char *) argv[i], len); |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | ngx_argv[i] = NULL; |
| 447 | |
| 448 | #endif |
| 449 | |
| 450 | return NGX_OK; |
| 451 | } |
| 452 | |
| 453 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 454 | static void * |
| 455 | ngx_core_module_create_conf(ngx_cycle_t *cycle) |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 456 | { |
| 457 | ngx_core_conf_t *ccf; |
| 458 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 459 | ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t)); |
| 460 | if (ccf == NULL) { |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 461 | return NULL; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 462 | } |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 463 | |
| 464 | /* |
| 465 | * set by pcalloc() |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 466 | * |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 467 | * ccf->pid = NULL; |
| 468 | * ccf->newpid = NULL; |
| 469 | * ccf->priority = 0; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 470 | */ |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 471 | |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 472 | ccf->daemon = NGX_CONF_UNSET; |
| 473 | ccf->master = NGX_CONF_UNSET; |
Igor Sysoev | a741f8d | 2004-03-30 20:31:58 +0000 | [diff] [blame] | 474 | ccf->worker_processes = NGX_CONF_UNSET; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 475 | ccf->debug_points = NGX_CONF_UNSET; |
Igor Sysoev | 4959ec4 | 2005-05-23 12:07:45 +0000 | [diff] [blame^] | 476 | ccf->user = (ngx_uid_t) NGX_CONF_UNSET_UINT; |
| 477 | ccf->group = (ngx_gid_t) NGX_CONF_UNSET_UINT; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 478 | #if (NGX_THREADS) |
| 479 | ccf->worker_threads = NGX_CONF_UNSET; |
| 480 | ccf->thread_stack_size = NGX_CONF_UNSET_SIZE; |
| 481 | #endif |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 482 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 483 | return ccf; |
| 484 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 485 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 486 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 487 | static char * |
| 488 | ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf) |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 489 | { |
| 490 | ngx_core_conf_t *ccf = conf; |
| 491 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 492 | #if !(NGX_WIN32) |
Igor Sysoev | 6d2a14a | 2004-09-27 16:03:21 +0000 | [diff] [blame] | 493 | struct passwd *pwd; |
| 494 | struct group *grp; |
| 495 | #endif |
| 496 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 497 | ngx_conf_init_value(ccf->daemon, 1); |
| 498 | ngx_conf_init_value(ccf->master, 1); |
| 499 | ngx_conf_init_value(ccf->worker_processes, 1); |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 500 | ngx_conf_init_value(ccf->debug_points, 0); |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 501 | |
Igor Sysoev | 32fcd5c | 2004-07-05 06:55:54 +0000 | [diff] [blame] | 502 | #if (NGX_THREADS) |
| 503 | ngx_conf_init_value(ccf->worker_threads, 0); |
| 504 | ngx_threads_n = ccf->worker_threads; |
| 505 | ngx_conf_init_size_value(ccf->thread_stack_size, 2 * 1024 * 1024); |
| 506 | #endif |
| 507 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 508 | #if !(NGX_WIN32) |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 509 | |
Igor Sysoev | 4959ec4 | 2005-05-23 12:07:45 +0000 | [diff] [blame^] | 510 | if (ccf->user == (uid_t) NGX_CONF_UNSET_UINT && geteuid() == 0) { |
Igor Sysoev | 6d2a14a | 2004-09-27 16:03:21 +0000 | [diff] [blame] | 511 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 512 | pwd = getpwnam(NGX_USER); |
Igor Sysoev | 6d2a14a | 2004-09-27 16:03:21 +0000 | [diff] [blame] | 513 | if (pwd == NULL) { |
| 514 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 515 | "getpwnam(\"" NGX_USER "\") failed"); |
Igor Sysoev | 6d2a14a | 2004-09-27 16:03:21 +0000 | [diff] [blame] | 516 | return NGX_CONF_ERROR; |
| 517 | } |
| 518 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 519 | ccf->username = NGX_USER; |
Igor Sysoev | 6d2a14a | 2004-09-27 16:03:21 +0000 | [diff] [blame] | 520 | ccf->user = pwd->pw_uid; |
| 521 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 522 | grp = getgrnam(NGX_GROUP); |
Igor Sysoev | 6d2a14a | 2004-09-27 16:03:21 +0000 | [diff] [blame] | 523 | if (grp == NULL) { |
| 524 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 525 | "getgrnam(\"" NGX_GROUP "\") failed"); |
Igor Sysoev | 6d2a14a | 2004-09-27 16:03:21 +0000 | [diff] [blame] | 526 | return NGX_CONF_ERROR; |
| 527 | } |
| 528 | |
| 529 | ccf->group = grp->gr_gid; |
| 530 | } |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 531 | |
| 532 | if (ccf->pid.len == 0) { |
Igor Sysoev | 090849d | 2004-05-18 20:28:54 +0000 | [diff] [blame] | 533 | ccf->pid.len = sizeof(NGX_PID_PATH) - 1; |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 534 | ccf->pid.data = (u_char *) NGX_PID_PATH; |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 535 | } |
Igor Sysoev | 6d2a14a | 2004-09-27 16:03:21 +0000 | [diff] [blame] | 536 | |
| 537 | if (ngx_conf_full_name(cycle, &ccf->pid) == NGX_ERROR) { |
| 538 | return NGX_CONF_ERROR; |
| 539 | } |
| 540 | |
| 541 | ccf->newpid.len = ccf->pid.len + sizeof(NGX_NEWPID_EXT); |
| 542 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 543 | ccf->newpid.data = ngx_palloc(cycle->pool, ccf->newpid.len); |
| 544 | if (ccf->newpid.data == NULL) { |
Igor Sysoev | 6d2a14a | 2004-09-27 16:03:21 +0000 | [diff] [blame] | 545 | return NGX_CONF_ERROR; |
| 546 | } |
| 547 | |
| 548 | ngx_memcpy(ngx_cpymem(ccf->newpid.data, ccf->pid.data, ccf->pid.len), |
| 549 | NGX_NEWPID_EXT, sizeof(NGX_NEWPID_EXT)); |
| 550 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 551 | #endif |
| 552 | |
| 553 | return NGX_CONF_OK; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 554 | } |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 555 | |
| 556 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 557 | static char * |
| 558 | ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 559 | { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 560 | #if (NGX_WIN32) |
Igor Sysoev | a536298 | 2004-03-04 07:04:55 +0000 | [diff] [blame] | 561 | |
| 562 | ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
| 563 | "\"user\" is not supported, ignored"); |
| 564 | |
| 565 | return NGX_CONF_OK; |
| 566 | |
| 567 | #else |
| 568 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 569 | ngx_core_conf_t *ccf = conf; |
| 570 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 571 | char *group; |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 572 | struct passwd *pwd; |
| 573 | struct group *grp; |
| 574 | ngx_str_t *value; |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 575 | |
Igor Sysoev | 4959ec4 | 2005-05-23 12:07:45 +0000 | [diff] [blame^] | 576 | if (ccf->user != (uid_t) NGX_CONF_UNSET_UINT) { |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 577 | return "is duplicate"; |
| 578 | } |
| 579 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 580 | if (geteuid() != 0) { |
| 581 | ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
| 582 | "the \"user\" directive makes sense only " |
| 583 | "if the master process runs " |
| 584 | "with super-user privileges, ignored"); |
| 585 | return NGX_CONF_OK; |
| 586 | } |
| 587 | |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 588 | value = (ngx_str_t *) cf->args->elts; |
| 589 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 590 | ccf->username = (char *) value[1].data; |
| 591 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 592 | pwd = getpwnam((const char *) value[1].data); |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 593 | if (pwd == NULL) { |
| 594 | ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno, |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 595 | "getpwnam(\"%s\") failed", value[1].data); |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 596 | return NGX_CONF_ERROR; |
| 597 | } |
| 598 | |
| 599 | ccf->user = pwd->pw_uid; |
| 600 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 601 | group = (char *) ((cf->args->nelts == 2) ? value[1].data : value[2].data); |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 602 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 603 | grp = getgrnam(group); |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 604 | if (grp == NULL) { |
| 605 | ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno, |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 606 | "getgrnam(\"%s\") failed", group); |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 607 | return NGX_CONF_ERROR; |
| 608 | } |
| 609 | |
| 610 | ccf->group = grp->gr_gid; |
| 611 | |
| 612 | return NGX_CONF_OK; |
Igor Sysoev | a536298 | 2004-03-04 07:04:55 +0000 | [diff] [blame] | 613 | |
| 614 | #endif |
Igor Sysoev | 03420a6 | 2004-01-20 20:40:08 +0000 | [diff] [blame] | 615 | } |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 616 | |
| 617 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 618 | static char * |
| 619 | ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 620 | { |
| 621 | ngx_core_conf_t *ccf = conf; |
| 622 | |
| 623 | ngx_str_t *value; |
| 624 | ngx_uint_t n, minus; |
| 625 | |
| 626 | if (ccf->priority != 0) { |
| 627 | return "is duplicate"; |
| 628 | } |
| 629 | |
| 630 | value = cf->args->elts; |
| 631 | |
| 632 | if (value[1].data[0] == '-') { |
| 633 | n = 1; |
| 634 | minus = 1; |
| 635 | |
| 636 | } else if (value[1].data[0] == '+') { |
| 637 | n = 1; |
| 638 | minus = 0; |
| 639 | |
| 640 | } else { |
| 641 | n = 0; |
| 642 | minus = 0; |
| 643 | } |
| 644 | |
| 645 | ccf->priority = ngx_atoi(&value[1].data[n], value[1].len - n); |
| 646 | if (ccf->priority == NGX_ERROR) { |
| 647 | return "invalid number"; |
| 648 | } |
| 649 | |
| 650 | if (minus) { |
| 651 | ccf->priority = -ccf->priority; |
| 652 | } |
| 653 | |
| 654 | return NGX_CONF_OK; |
| 655 | } |