LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2007, 02:19 PM   #1
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Rep: Reputation: 30
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.
 
Old 08-22-2007, 07:36 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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() */
 
Old 08-22-2007, 08:29 AM   #3
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by wjevans_7d1@yahoo.co View Post
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.
 
Old 08-23-2007, 12:29 AM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
No UTMPX entry, You must EXEC "login" for the lowest "shell" ooihc Solaris / OpenSolaris 7 03-12-2007 02:09 PM
Any way to get "Alice"; "Call of Duty" series and "Descent 3" to work? JBailey742 Linux - Games 13 06-23-2006 01:34 PM
"Function not implemented" error in call to "sem_open()" Krishnendu8 Linux - Newbie 1 06-07-2003 02:52 AM
"Function not imlemented" error in call to "sem_open()" Krishnendu8 Linux - Networking 0 06-07-2003 02:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration