comparison src/core/nginx.c @ 210:00cafae0bdf1

nginx-0.0.1-2003-12-14-23:10:27 import
author Igor Sysoev <igor@sysoev.ru>
date Sun, 14 Dec 2003 20:10:27 +0000
parents e1c815be05ae
children 679f60139863
comparison
equal deleted inserted replaced
209:e1c815be05ae 210:00cafae0bdf1
14 static int ngx_open_listening_sockets(ngx_cycle_t *cycle, ngx_log_t *log); 14 static int ngx_open_listening_sockets(ngx_cycle_t *cycle, ngx_log_t *log);
15 static void ngx_clean_old_cycles(ngx_event_t *ev); 15 static void ngx_clean_old_cycles(ngx_event_t *ev);
16 16
17 17
18 typedef struct { 18 typedef struct {
19 int daemon; 19 int daemon;
20 ngx_str_t pid;
20 } ngx_core_conf_t; 21 } ngx_core_conf_t;
21 22
22 23
23 static ngx_str_t core_name = ngx_string("core"); 24 static ngx_str_t core_name = ngx_string("core");
24 25
59 static ngx_connection_t dumb; 60 static ngx_connection_t dumb;
60 61
61 u_int ngx_connection_counter; 62 u_int ngx_connection_counter;
62 63
63 64
65 int done;
64 int restart; 66 int restart;
65 int rotate; 67 int rotate;
66 68
67 69
68 int main(int argc, char *const *argv) 70 int main(int argc, char *const *argv)
71 ngx_fd_t fd; 73 ngx_fd_t fd;
72 ngx_log_t *log; 74 ngx_log_t *log;
73 ngx_cycle_t *cycle; 75 ngx_cycle_t *cycle;
74 ngx_open_file_t *file; 76 ngx_open_file_t *file;
75 #if !(WIN32) 77 #if !(WIN32)
78 size_t len;
79 char pid[/* STUB */ 10];
80 ngx_file_t pidfile;
76 ngx_core_conf_t *ccf; 81 ngx_core_conf_t *ccf;
77 #endif 82 #endif
78 83
79 #if __FreeBSD__ 84 #if __FreeBSD__
80 ngx_debug_init(); 85 ngx_debug_init();
115 120
116 if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) { 121 if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
117 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, 122 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
118 "dup2(STDERR) failed"); 123 "dup2(STDERR) failed");
119 return 1; 124 return 1;
125 }
126
127 if (ccf->pid.len == 0) {
128 ccf->pid.len = sizeof(NGINX_PID) - 1;
129 ccf->pid.data = NGINX_PID;
130 }
131
132 len = ngx_snprintf(pid, /* STUB */ 10, PID_T_FMT, ngx_getpid());
133 ngx_memzero(&pidfile, sizeof(ngx_file_t));
134 pidfile.name = ccf->pid;
135
136 pidfile.fd = ngx_open_file(pidfile.name.data, NGX_FILE_RDWR,
137 NGX_FILE_CREATE_OR_OPEN);
138
139 if (pidfile.fd == NGX_INVALID_FILE) {
140 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
141 ngx_open_file_n " \"%s\" failed", pidfile.name.data);
142 return 1;
143 }
144
145 if (ngx_write_file(&pidfile, pid, len, 0) == NGX_ERROR) {
146 return 1;
147 }
148
149 if (ngx_close_file(pidfile.fd) == NGX_FILE_ERROR) {
150 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
151 ngx_close_file_n " \"%s\" failed", pidfile.name.data);
120 } 152 }
121 153
122 #endif 154 #endif
123 155
124 /* life cycle */ 156 /* life cycle */
160 for ( ;; ) { 192 for ( ;; ) {
161 ngx_log_debug(cycle->log, "worker cycle"); 193 ngx_log_debug(cycle->log, "worker cycle");
162 194
163 ngx_process_events(cycle->log); 195 ngx_process_events(cycle->log);
164 196
197 if (done) {
198 if (ngx_delete_file(pidfile.name.data) == NGX_FILE_ERROR) {
199 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
200 ngx_delete_file_n " \"%s\" failed",
201 pidfile.name.data);
202 }
203
204 ngx_log_error(NGX_LOG_INFO,
205 cycle->log, 0, "exiting");
206 exit(0);
207 }
208
165 if (rotate) { 209 if (rotate) {
166 ngx_log_debug(cycle->log, "rotate"); 210 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "rotating logs");
167 211
168 file = cycle->open_files.elts; 212 file = cycle->open_files.elts;
169 for (i = 0; i < cycle->open_files.nelts; i++) { 213 for (i = 0; i < cycle->open_files.nelts; i++) {
170 if (file[i].name.data == NULL) { 214 if (file[i].name.data == NULL) {
171 continue; 215 continue;
311 355
312 if (!(ccf = ngx_pcalloc(pool, sizeof(ngx_core_conf_t)))) { 356 if (!(ccf = ngx_pcalloc(pool, sizeof(ngx_core_conf_t)))) {
313 ngx_destroy_pool(pool); 357 ngx_destroy_pool(pool);
314 return NULL; 358 return NULL;
315 } 359 }
360 /* set by pcalloc()
361 *
362 * ccf->pid = NULL;
363 */
316 ccf->daemon = -1; 364 ccf->daemon = -1;
317 ((void **)(cycle->conf_ctx))[ngx_core_module.index] = ccf; 365 ((void **)(cycle->conf_ctx))[ngx_core_module.index] = ccf;
318 366
319 367
320 ngx_memzero(&conf, sizeof(ngx_conf_t)); 368 ngx_memzero(&conf, sizeof(ngx_conf_t));