LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   getenv("LOGNAME") is incorrect when a child create a shell which call parent (https://www.linuxquestions.org/questions/programming-9/getenv-logname-is-incorrect-when-a-child-create-a-shell-which-call-parent-578744/)

powah 08-21-2007 02:19 PM

getenv("LOGNAME") is incorrect when a child create a shell which call parent
 
The command line interface parent process (CLI) fork and its child create a shell which then create a new process for CLI (we call it CLI2).
Inside CLI2, the getenv("LOGNAME") value is incorrect. Is it because getenv is not reentrant? getenv_r is not supported on the linux I used.
How to ensure inside CLI2 the environment value of getenv("LOGNAME") value of CLI is right?
This is an old program which I maintained.

wjevans_7d1@yahoo.co 08-22-2007 07:36 AM

What output do you get when you run this program?

Code:

#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int    argc,
        char **argv
        )
{
  int  child_process;
  int  status;

  char *env_logname;
  char *env_stophere;

  char  answer[4];

  env_stophere=getenv("STOPHERE");

  if(env_stophere)
  {
    printf("In child  process, ");
  }
  else
  {
    printf("In parent process, ");
  }

  env_logname=getenv("LOGNAME");

  if(env_logname)
  {
    printf("LOGNAME is %s.\n",
          env_logname
          );
  }
  else
  {
    printf("LOGNAME is undefined.\n");
  }

  env_stophere=getenv("STOPHERE");

  if(env_stophere)
  {
    return 0;
  }

  for(;;)
  {
    printf("About to spawn a new process.  Should I continue?  ");

    fgets(answer,
          sizeof(answer),
          stdin
        );

    if(answer[0]=='n')
    {
      return 0;
    }

    if(answer[0]=='y')
    {
      break;
    }
  }

  if(putenv("STOPHERE=1")<0)
  {
    perror("putenv(\"STOPHERE=1\")");

    return 1;
  }

  child_process=fork();

  if(child_process<0)
  {
    perror("fork()");

    exit(1);
  }

  if(child_process==0)
  {
    if(execl("/bin/bash",
            "/bin/bash",
            "-c",
            argv[0],
            NULL
            )
      <0
      )
    {
      perror("execl()");

      return 1;
    }

    return 0;
  }

  wait(&status);

  return 0;

} /* main() */


powah 08-22-2007 08:29 AM

Quote:

Originally Posted by wjevans_7d1@yahoo.co (Post 2866808)
What output do you get when you run this program?

Code:

#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int    argc,
        char **argv
        )
{
  int  child_process;
  int  status;

  char *env_logname;
  char *env_stophere;

  char  answer[4];

  env_stophere=getenv("STOPHERE");

  if(env_stophere)
  {
    printf("In child  process, ");
  }
  else
  {
    printf("In parent process, ");
  }

  env_logname=getenv("LOGNAME");

  if(env_logname)
  {
    printf("LOGNAME is %s.\n",
          env_logname
          );
  }
  else
  {
    printf("LOGNAME is undefined.\n");
  }

  env_stophere=getenv("STOPHERE");

  if(env_stophere)
  {
    return 0;
  }

  for(;;)
  {
    printf("About to spawn a new process.  Should I continue?  ");

    fgets(answer,
          sizeof(answer),
          stdin
        );

    if(answer[0]=='n')
    {
      return 0;
    }

    if(answer[0]=='y')
    {
      break;
    }
  }

  if(putenv("STOPHERE=1")<0)
  {
    perror("putenv(\"STOPHERE=1\")");

    return 1;
  }

  child_process=fork();

  if(child_process<0)
  {
    perror("fork()");

    exit(1);
  }

  if(child_process==0)
  {
    if(execl("/bin/bash",
            "/bin/bash",
            "-c",
            argv[0],
            NULL
            )
      <0
      )
    {
      perror("execl()");

      return 1;
    }

    return 0;
  }

  wait(&status);

  return 0;

} /* main() */


Output is:
$ a.out
In parent process, LOGNAME is powah.
About to spawn a new process. Should I continue? y
In child process, LOGNAME is powah.

wjevans_7d1@yahoo.co 08-23-2007 12:29 AM

Ok. So my sample program does exactly what you say your program does, and my sample program shows that environment variable LOGNAME remains in the child process.

But your program doesn't behave that way.

So your job is to figure out why my program works in this regard and yours doesn't.

Good luck.


All times are GMT -5. The time now is 11:32 AM.