问题
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