comparison src/core/nginx.c @ 234:cd71b95716b4

nginx-0.0.1-2004-01-20-23:40:08 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 20 Jan 2004 20:40:08 +0000
parents 92db0aa1e83f
children 86e473b5641e
comparison
equal deleted inserted replaced
233:4eaafcd57be7 234:cd71b95716b4
4 #include <ngx_event.h> 4 #include <ngx_event.h>
5 #include <nginx.h> 5 #include <nginx.h>
6 6
7 7
8 typedef struct { 8 typedef struct {
9 ngx_str_t user;
10 int daemon; 9 int daemon;
11 int master; 10 int master;
11 uid_t user;
12 gid_t group;
12 ngx_str_t pid; 13 ngx_str_t pid;
13 ngx_str_t newpid; 14 ngx_str_t newpid;
14 } ngx_core_conf_t; 15 } ngx_core_conf_t;
15 16
16 17
25 static void ngx_master_exit(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx); 26 static void ngx_master_exit(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx);
26 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data); 27 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data);
27 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle, char **envp); 28 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle, char **envp);
28 static ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv); 29 static ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv);
29 static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle); 30 static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle);
31 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
30 32
31 33
32 static ngx_str_t core_name = ngx_string("core"); 34 static ngx_str_t core_name = ngx_string("core");
33 35
34 static ngx_command_t ngx_core_commands[] = { 36 static ngx_command_t ngx_core_commands[] = {
35 37
36 { ngx_string("user"), 38 { ngx_string("user"),
37 NGX_MAIN_CONF|NGX_CONF_TAKE1, 39 NGX_MAIN_CONF|NGX_CONF_TAKE12,
38 ngx_conf_set_core_str_slot, 40 ngx_set_user,
39 0, 41 0,
40 offsetof(ngx_core_conf_t, user), 42 0,
41 NULL }, 43 NULL },
42 44
43 { ngx_string("daemon"), 45 { ngx_string("daemon"),
44 NGX_MAIN_CONF|NGX_CONF_TAKE1, 46 NGX_MAIN_CONF|NGX_CONF_TAKE1,
45 ngx_conf_set_core_flag_slot, 47 ngx_conf_set_core_flag_slot,
66 ngx_core_module_init, /* init module */ 68 ngx_core_module_init, /* init module */
67 NULL /* init child */ 69 NULL /* init child */
68 }; 70 };
69 71
70 72
71 ngx_int_t ngx_max_module; 73 ngx_int_t ngx_max_module;
72 74 ngx_uint_t ngx_connection_counter;
73 75
74 /* STUB */ 76 ngx_int_t ngx_process;
75 uid_t user; 77 ngx_pid_t ngx_new_binary;
76 78
77 u_int ngx_connection_counter; 79 ngx_int_t ngx_inherited;
78 80 ngx_int_t ngx_signal;
79 ngx_int_t ngx_process; 81 ngx_int_t ngx_reap;
80 ngx_pid_t ngx_new_binary; 82 ngx_int_t ngx_terminate;
81 83 ngx_int_t ngx_quit;
82 ngx_int_t ngx_inherited; 84 ngx_int_t ngx_noaccept;
83 ngx_int_t ngx_signal; 85 ngx_int_t ngx_reconfigure;
84 ngx_int_t ngx_reap; 86 ngx_int_t ngx_reopen;
85 ngx_int_t ngx_terminate; 87 ngx_int_t ngx_change_binary;
86 ngx_int_t ngx_quit;
87 ngx_int_t ngx_noaccept;
88 ngx_int_t ngx_reconfigure;
89 ngx_int_t ngx_reopen;
90 ngx_int_t ngx_change_binary;
91 88
92 89
93 int main(int argc, char *const *argv, char **envp) 90 int main(int argc, char *const *argv, char **envp)
94 { 91 {
95 ngx_fd_t fd; 92 ngx_fd_t fd;
100 ngx_core_conf_t *ccf; 97 ngx_core_conf_t *ccf;
101 ngx_master_ctx_t ctx; 98 ngx_master_ctx_t ctx;
102 #if !(WIN32) 99 #if !(WIN32)
103 size_t len; 100 size_t len;
104 char pid[/* STUB */ 10]; 101 char pid[/* STUB */ 10];
105 struct passwd *pwd;
106 #endif 102 #endif
107 103
108 #if __FreeBSD__ 104 #if __FreeBSD__
109 ngx_debug_init(); 105 ngx_debug_init();
110 #endif 106 #endif
166 } 162 }
167 163
168 #endif 164 #endif
169 165
170 #else 166 #else
171
172 /* STUB */
173 if (ccf->user.len) {
174 pwd = getpwnam(ccf->user.data);
175 if (pwd == NULL) {
176 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
177 "getpwnam(%s) failed", ccf->user);
178 return 1;
179 }
180
181 user = pwd->pw_uid;
182 }
183 /* */
184 167
185 if (ccf->daemon != 0) { 168 if (ccf->daemon != 0) {
186 if (ngx_daemon(cycle->log) == NGX_ERROR) { 169 if (ngx_daemon(cycle->log) == NGX_ERROR) {
187 return 1; 170 return 1;
188 } 171 }
571 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data) 554 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
572 { 555 {
573 sigset_t set; 556 sigset_t set;
574 ngx_int_t i; 557 ngx_int_t i;
575 ngx_listening_t *ls; 558 ngx_listening_t *ls;
559 ngx_core_conf_t *ccf;
576 560
577 ngx_process = NGX_PROCESS_WORKER; 561 ngx_process = NGX_PROCESS_WORKER;
578 ngx_last_process = 0; 562 ngx_last_process = 0;
579 563
580 if (user) { 564 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
581 if (setuid(user) == -1) { 565
582 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 566 if (ccf->group != (gid_t) NGX_CONF_UNSET) {
583 "setuid() failed"); 567 if (setuid(ccf->group) == -1) {
568 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
569 "setgid(%d) failed", ccf->group);
584 /* fatal */ 570 /* fatal */
585 exit(1); 571 exit(2);
572 }
573 }
574
575 if (ccf->user != (uid_t) NGX_CONF_UNSET && geteuid() == 0) {
576 if (setuid(ccf->user) == -1) {
577 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
578 "setuid(%d) failed", ccf->user);
579 /* fatal */
580 exit(2);
586 } 581 }
587 } 582 }
588 583
589 sigemptyset(&set); 584 sigemptyset(&set);
590 585
753 } 748 }
754 /* set by pcalloc() 749 /* set by pcalloc()
755 * 750 *
756 * ccf->pid = NULL; 751 * ccf->pid = NULL;
757 */ 752 */
758 ccf->daemon = -1; 753 ccf->daemon = NGX_CONF_UNSET;
759 ccf->master = -1; 754 ccf->master = NGX_CONF_UNSET;
755 ccf->user = (uid_t) NGX_CONF_UNSET;
756 ccf->group = (gid_t) NGX_CONF_UNSET;
760 757
761 ((void **)(cycle->conf_ctx))[ngx_core_module.index] = ccf; 758 ((void **)(cycle->conf_ctx))[ngx_core_module.index] = ccf;
762 759
763 return NGX_OK; 760 return NGX_OK;
764 } 761 }
762
763
764 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
765 {
766 struct passwd *pwd;
767 struct group *grp;
768 ngx_str_t *value;
769 ngx_core_conf_t *ccf;
770
771 ccf = *(void **)conf;
772
773 if (ccf->user != (uid_t) NGX_CONF_UNSET) {
774 return "is duplicate";
775 }
776
777 value = (ngx_str_t *) cf->args->elts;
778
779 pwd = getpwnam(value[1].data);
780 if (pwd == NULL) {
781 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
782 "getpwnam(%s) failed", value[1].data);
783 return NGX_CONF_ERROR;
784 }
785
786 ccf->user = pwd->pw_uid;
787
788 if (cf->args->nelts == 2) {
789 return NGX_CONF_OK;
790 }
791
792 grp = getgrnam(value[2].data);
793 if (grp == NULL) {
794 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
795 "getgrnam(%s) failed", value[1].data);
796 return NGX_CONF_ERROR;
797 }
798
799 ccf->group = grp->gr_gid;
800
801 return NGX_CONF_OK;
802 }