vaynedu / nginx-1.16.0

学习nginx架构设计与实现,翻译nginx的源码,写nginx的测试代码, 在issue中记录nginx的精妙设计及其常见问题https://github.com/vaynedu/nginx-1.16.0/issues 。 myexercise内存池、哈希表、链表、md5、crc测试代码,mymodule中有hello自定义模块代码。通过nginx将自己整个知识体系连接起来
https://github.com/vaynedu/nginx-1.16.0/issues
BSD 2-Clause "Simplified" License
12 stars 2 forks source link

妹子反馈: nginx的worker进程获取不到环境变量getenv("PATH")为空? #46

Open vaynedu opened 5 years ago

vaynedu commented 5 years ago

这里我肯定怀疑,肯定不相信 worker进程 被 master进程 fork出来,肯定也继承环境变量,测试代码:实践往往是最好的证明

#include <sys/types.h>
#include <sys/socket.h>

#include <stdlib.h>
#include <stdio.h>

int main()
{
    pid_t  pid;

    pid = fork();

    if (0 == pid)
    {
        pid = getpid();
        printf("[%d]-child process start, %s\n", pid,getenv("HOME"));

    }
    else if (0 < pid)
    {
        pid = getpid();
        printf("[%d]-parent process continue, %s\n", pid, getenv("HOME"));

    }
    else
    {
        printf("%s", "fork failed");
    }

    return 0;
}

而在nginx代码中,首先猜测肯定是某个地方修改了环境变量,不然不可能为空

static void
ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
{
   ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "11111111111111111111111111[%d]-child process start, %s\n", getpid(),getenv("PATH"));

    ngx_worker_process_init(cycle, worker);

    ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "22222222222222222222222222[%d]-child process start \n", getpid(),getenv("PATH"));

}

日中打印结果
2019/10/12 15:53:43 [notice] 24279#0: 11111111111111111111111111[24279]-child process start, /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/bin/vim:/root/bin:/usr/local/bi    n/vim:/usr/lib/golang/bin:/data/go/bin:/usr/local/bin/vim:/usr/lib/go/bin:/data/go/bin:/usr/local/bin/vim:/usr/local/go/bin:/data/go/bin

2019/10/12 15:53:43 [notice] 24279#0: 22222222222222222222222222[24279]-child process start

nginx中获取不到PATH变量。 这个和fork、不fork没有任何关系。 我一直想说只是在nginx下,getenv获取不到环境变量。因为在ngx_set_environment重新设置环境变量了

vaynedu commented 5 years ago

exec函数:环境变量(使用了execle、execve函数则不继承环境变量)