Solved

variable

Posted on 2011-03-10
6
235 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
Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
SetCurrentDirectory path limit 7 144
Retrieve PID of MicrosoftEdge.exe with GetForegroundWindow() 6 209
c++, dynamic object by json 1 60
designing in object programming 12 106
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 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