Solved

variable

Posted on 2011-03-10
6
236 Views
Last Modified: 2012-05-11
i am calling getenv() in my C++ code

If I call C++ executable directly then getenv() it reads the value correctly

However if I call the executable from within /etc/init.d/myDaemon  
"myDaemon "  then calls C++ executable, in this case getenv() does not read the value.
I have to manually set the env variable in /etc/init.d/my.exe in order my C++ executable to read it.

not sure why it behaves this way

0
Comment
Question by:learningunix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
parnasso earned 250 total points
ID: 35097647
The reason is, with all likelyhood, the environment variable you are trying to read belongs to a certain user, different than the one that is executing your daemon. Therefore your deamon can not access via getenv() to the value you want.

Maybe changing your deamon to start as a different user or setting that environment variable to the user that its starting the deamon.
0
 
LVL 86

Expert Comment

by:jkr
ID: 35097668
How are you starting the executable that calls 'getenv()'? if you are using 'system()', this call will start a new shell and tyour changes to the environment are null and void, meaning the process won't find the environment variable you have set before:
NAME
     system -- pass a command to the shell

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <stdlib.h>

     int
     system(const char *string);

DESCRIPTION
     The system() function hands the argument string to the command inter-
     preter sh(1).  The calling process waits for the shell to finish execut-
     ing the command, ignoring SIGINT and SIGQUIT, and blocking SIGCHLD.

     If string is a NULL pointer, system() will return non-zero if the command
     interpreter sh(1) is available, and zero if it is not.

Open in new window


You should rather use one of the 'exec*()' functions, i.e.
void run_progrem(const char* pCmd) {

  /* Fork a child */

  if ((pid = fork()) == 0) {

      /* If execl() returns at all, there was an error. */

      if (execlp(pCmd, pCmd, NULL)) { /* Bye bye! */
        perror("execl");
        exit(128);
      }
    }
}

Open in new window

0
 

Author Comment

by:learningunix
ID: 35097827
can i set that env variable in bash script ?
export VAR=<value>

how to read the value in bash script?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:learningunix
ID: 35098008
In the myDaemon bash script if I do
echo $VAR

it did not print anything. meaning the bash script is somehow not  able to read the env variable
0
 
LVL 86

Expert Comment

by:jkr
ID: 35098030
You can do that in a bash script, sure - but this variable will only be valid in the same "shell", as soon as you start another one that is unrelated to the one you use 'export', the change will be lost.
0
 

Author Closing Comment

by:learningunix
ID: 35099715
thx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question