如不做特殊说明,本博客所使用的 nginx 源码版本是 1.0.14,[] 中是代码所在的文件!

这一节我们分析ngx_worker_process_cycle(),该函数代码比较少,因为它通过调用函数实现功能的,先贴出代码:

[os/unix/ngx_process_cycle.c

 761 /* worker_process 所执行的程序,nginx 核心所在 */
 762 static void
 763 ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
 764 {
 765     ngx_uint_t         i;
 766     ngx_connection_t  *c;
 767 
 768     ngx_process = NGX_PROCESS_WORKER;
 769 
 770     /* worker 进程初始化 */
 771     ngx_worker_process_init(cycle, 1);
 772 
 773     /* 设置程序名称 */
 774     ngx_setproctitle("worker process");

  1. 768 行设置 ngx_process 的值为 NGX_PROCESS_WORKER, 该值会在信号回调函数中有用ngx_signal_handler()[ngx_process.c],该值表示的是进程模型的类型。

  2. 771 行调用 ngx_worker_process_init() 来先对 worker 进程进行一下初始化,该函数的源码如下:

 888 /* worker_process 初始化,根据配置来设置一些属性 */
 889 static void
 890 ngx_worker_process_init(ngx_cycle_t *cycle, ngx_uint_t priority)
 891 {
 892     sigset_t          set;
 893     ngx_int_t         n;
 894     ngx_uint_t        i;
 895     struct rlimit     rlmt;
 896     ngx_core_conf_t  *ccf;
 897     ngx_listening_t  *ls;
 898 
 899     if (ngx_set_environment(cycle, NULL) == NULL) {
 900         /* fatal */
 901         exit(2);
 902     }
 903 
 904     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 905 
 906     if (priority && ccf->priority != 0) {
 907         if (setpriority(PRIO_PROCESS, 0, ccf->priority) == -1) {
 908             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
 909                           "setpriority(%d) failed", ccf->priority);
 910         }
 911     }
 912 
 913     if (ccf->rlimit_nofile != NGX_CONF_UNSET) {
 914         rlmt.rlim_cur = (rlim_t) ccf->rlimit_nofile;
 915         rlmt.rlim_max = (rlim_t) ccf->rlimit_nofile;
 916 
 917         if (setrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
 918             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
 919                           "setrlimit(RLIMIT_NOFILE, %i) failed",
 920                           ccf->rlimit_nofile);
 921         }
 922     }
 923 
 924     if (ccf->rlimit_core != NGX_CONF_UNSET) {
 925         rlmt.rlim_cur = (rlim_t) ccf->rlimit_core;
 926         rlmt.rlim_max = (rlim_t) ccf->rlimit_core;
 927 
 928         if (setrlimit(RLIMIT_CORE, &rlmt) == -1) {
 929             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
 930                           "setrlimit(RLIMIT_CORE, %O) failed",
 931                           ccf->rlimit_core);
 932         }
 933     }
 934 
 935 #ifdef RLIMIT_SIGPENDING
 936     if (ccf->rlimit_sigpending != NGX_CONF_UNSET) {
 937         rlmt.rlim_cur = (rlim_t) ccf->rlimit_sigpending;
 938         rlmt.rlim_max = (rlim_t) ccf->rlimit_sigpending;
 939 
 940         if (setrlimit(RLIMIT_SIGPENDING, &rlmt) == -1) {
 941             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
 942                           "setrlimit(RLIMIT_SIGPENDING, %i) failed",
 943                           ccf->rlimit_sigpending);
 944         }
 945     }
 946 #endif
 947 
 948     if (geteuid() == 0) {
 949         if (setgid(ccf->group) == -1) {
 950             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
 951                           "setgid(%d) failed", ccf->group);
 952             /* fatal */
 953             exit(2);
 954         }
 955 
 956         if (initgroups(ccf->username, ccf->group) == -1) {
 957             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
 958                           "initgroups(%s, %d) failed",
 959                           ccf->username, ccf->group);
 960         }
 961 
 962         if (setuid(ccf->user) == -1) {
 963             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
 964                           "setuid(%d) failed", ccf->user);
 965             /* fatal */
 966             exit(2);
 967         }
 968     }
 969 
 970 #if (NGX_HAVE_SCHED_SETAFFINITY)
 971 
 972     if (cpu_affinity) {
 973         ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
 974                       "sched_setaffinity(0x%08Xl)", cpu_affinity);
 975 
 976         if (sched_setaffinity(0, sizeof(cpu_affinity),
 977                               (cpu_set_t *) &cpu_affinity)
 978             == -1)
 979         {
 980             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
 981                           "sched_setaffinity(0x%08Xl) failed", cpu_affinity);
 982         }
 983     }
 984 
 985 #endif
 986 
 987 #if (NGX_HAVE_PR_SET_DUMPABLE)
 988 
 989     /* allow coredump after setuid() in Linux 2.4.x */
 990 
 991     if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
 992         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
 993                       "prctl(PR_SET_DUMPABLE) failed");
 994     }
 995 
 996 #endif
 997 
 998     if (ccf->working_directory.len) {
 999         if (chdir((char *) ccf->working_directory.data) == -1) {
1000             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1001                           "chdir(\"%s\") failed", ccf->working_directory.data);
1002             /* fatal */
1003             exit(2);
1004         }
1005     }
1006 
1007     sigemptyset(&set);
1008 
1009     if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) {
1010         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1011                       "sigprocmask() failed");
1012     }
1013 
1014     /*
1015      * disable deleting previous events for the listening sockets because
1016      * in the worker processes there are no events at all at this point
1017      */
1018     ls = cycle->listening.elts;
1019     for (i = 0; i < cycle->listening.nelts; i++) {
1020         ls[i].previous = NULL;
1021     }
1022 
1023     for (i = 0; ngx_modules[i]; i++) {
1024         if (ngx_modules[i]->init_process) {
1025             /* 如果有模块设置了 init_process 函数 */
1026             if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
1027                 /* fatal */
1028                 exit(2);
1029             }
1030         }
1031     }
1032 
1033     for (n = 0; n < ngx_last_process; n++) {
1034 
1035         if (ngx_processes[n].pid == -1) {
1036             continue;
1037         }
1038 
1039         if (n == ngx_process_slot) {
1040             continue;
1041         }
1042 
1043         if (ngx_processes[n].channel[1] == -1) { // channel²»ŽæÔÚ£¬Ìø¹ý
1044             continue;
1045         }
1046 
1047         if (close(ngx_processes[n].channel[1]) == -1) {
1048             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1049                           "close() channel failed");
1050         }
1051     }
1052 
1053     if (close(ngx_processes[ngx_process_slot].channel[0]) == -1) {
1054         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1055                       "close() channel failed");
1056     }
1057 
1058 #if 0
1059     ngx_last_process = 0;
1060 #endif
1061     /* 貌似要加入读事件 */
1062     if (ngx_add_channel_event(cycle, ngx_channel, NGX_READ_EVENT,
1063                               ngx_channel_handler)
1064         == NGX_ERROR)
1065     {
1066         /* fatal */
1067         exit(2);
1068     }
1069 }
ngx_worker_process_init()

相关文章:

  • 2022-01-17
  • 2021-04-09
  • 2021-12-27
  • 2021-07-27
猜你喜欢
  • 2021-11-16
  • 2021-07-18
  • 2021-09-21
  • 2021-04-16
  • 2022-12-23
  • 2022-02-08
相关资源
相似解决方案