Solved

variable

Posted on 2011-03-10
6
231 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
  • 3
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
parnasso earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
can i set that env variable in bash script ?
export VAR=<value>

how to read the value in bash script?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:learningunix
Comment Utility
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
Comment Utility
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
Comment Utility
thx
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now