how to set environment variable when execle executes bash?

二次信任 提交于 2020-01-07 02:43:18

问题


Here I tried to spawn bash using execle() and wanted to set TMOUT environment for the new bash. It worked well. I could see TMOUT environment variable from the new bash shell.

<example1>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
    char *env[] = {"TMOUT=60", NULL};

    execle("/bin/bash", "bash", NULL, env);
    return 0;
}

But if I do the same thing like example2 to use sudo, I cannot see TMOUT environment variable from the new shell.

<example2>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
    char *env[] = {"TMOUT=60", NULL};

    execle("/bin/sudo", "sudo", "/bin/bash", NULL, NULL);
    return 0;
}

I tried to find some example like this from here. I could find some information about example1 above. But I could not find good information about example2.

I also tried the following using putenv().

<example3>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
    putenv("TMOUT=60");
    execle("/bin/sudo", "sudo", "/bin/bash", NULL, NULL);
    return 0;
}

But this example3 also didn't work. Can you please give me some idea how to set the environment when I use sudo like this?

来源:https://stackoverflow.com/questions/43243305/how-to-set-environment-variable-when-execle-executes-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!