Unexpected behavior of get_environment_variable with gfortran - fortran

I am using a subroutine call to GET_ENVIRONMENT_VARIABLE to read the computer hostname in a Fortran program. I cannot read this variable, though it is okay if I read other variables, as $USER. In my system (Debian Jessie, gfortran 4.9):
$ echo $HOSTNAME
deckard
$ echo $USER
curro
I have prepared this short program:
program hello
!
implicit none
integer :: ivar = 0, len, stat
character(LEN=256) :: host, user
call GET_ENVIRONMENT_VARIABLE('HOSTNAME', host, len, stat)
if (stat == 0) then
print*, "Hostname read: ", host
else
print*, "Hostname read failed: stat = ", stat
endif
call GET_ENVIRONMENT_VARIABLE('USER', user, len, stat)
if (stat == 0) print*, "Username read: ", user
print *, "This is user ", trim(user), " in node ", trim(host), "."
!
end program hello
If I run this simple program (compiled with or without the -std=f2003) the output is:
$ ./a.out
Hostname read failed: stat = 1
Username read: curro
This is user curro in node .
Therefore the error is stat = 1.
I know that gfortran has the intrinsic HOSTNM but for compatibility with other compilers I would prefer GET_ENVIRONMENT_VARIABLE. Any idea why is this happening?

I will suppose that your shell is bash but the end result should not change too much if the actual shell is another one.
In bash, you have access to environment variables and to regular bash variables. To make a variable accessible to child process (that is, programs that are launched by bash such as gfortran or your program 'a.out') it has to be part of the environment. By default, only variables from the posix set of environment variables are passed along (altough some may be omitted).
Examples of posix variables that are accessible both from the bash command line and from your fortran program (or any other program for that matter): HOME, USER, PWD or PATH.
You can verify that they are indeed accessible by checking what the env program knows:
env | grep HOME
env | grep USER
while
env | grep HOSTNAME
returns nothing.
So you need to export the variable HOSTNAME to place it in the list of known environment variable. bash happens to know about hostname because it defines a set of variables for the convenience of the user (see https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-HOSTNAME ). Another example of convenience variables is BASH_VERSION that is specific to bash itself.
export HOSTNAME
env | grep HOSTNAME
but the users of your program (or yourself but on a different computer) might not have access to it as an environment variable so this solution might not be of use to you. The "canonical" way of knowing the hostname without relying on the environment variable, on linux, is to execute the program "hostname".

Have you exported the HOSTNAME variable?
Exporting means that it is available to the child processes as well. That might very well be the difference between the HOSTNAME and USER environment variables that you experience.

Related

Show environment variable LD_LIBRARY_PATH

I have simple console application that prints environment variables:
int main(int argc, char **argv, char **envp)
{
printf("Scanning for LD_LIB_PATH\n");
for (char **env = envp; *env != 0; env++)
{
char *thisEnv = *env;
std::string s= *env;
if (s.find("LD_LIBRARY_PATH")!=std::string::npos)
{
printf("!!!!!!! %s\n", thisEnv);
}
printf("%s\n", thisEnv);
}
}
Before run executable I run script that sets LD_LIBRARY_PATH
export LD_LIBRARY_PATH=~/aaa/bbb/Debug:~/ccc/ddd/Debug
echo "searching:"
export | grep LD_LIBRARY
echo "done"
Script run fine with output:
exporting
searching:
declare -x LD_LIBRARY_PATH="/home/vicp/aaa/bbb/Debug:/home/vico/ccc/ddd/Debug"
done
I run executable and it finds many variables but no environment variable LD_LIB_PATH. Why?
UPD
As recommended I script . ./script.sh
Then double check with command:
export |grep LD_LIBRARY_PATH
Got output:
declare -x LD_LIBRARY_PATH="/home/vicp/aaa/bbb/Debug:/home/vico/ccc/ddd/Debug"
But still don't see LD_LIBRARY_PATH in my programm.
Depending on how you run the script, the env variable will only be added to the environment of the subshell running the script.
If you run the script like this:
$ ./script.sh
This will spawn a new shell wherein the script is run. The parent shell, i.e. the one you started the script from, will not be affected by what the script does. Changes to the environment will therefore not be visible (changing the working directory or similar will also not work).
If the script is intended to modify the environment of the current shell, the script needs to be sourced using the . or source commands:
$ . ./script.sh
$ source ./script.sh
From help source in bash:
Execute commands from a file in the current shell.
Then there seems to be a problem with the code:
As a commenter stated previousy, there are a lot of exclamation points in the success case printf. I presume these were meant to highlight the found variable name. Since they are outside of the opening quote of the format string, they are interpreted as logical not operators.
The result is that the format string literal (a pointer) is negated, resulting in false due to the number of operators. This usually maps to 0, which would mean the format string becomes a null pointer. What happens when handing a null pointer to printf as the format string depends, as far as I can tell, on the implementation. If it doesn't crash, it most certainly will not print anything however.
So the code may actually work, but there is no visible output due to that bug. To fix it, move the exclamation points into the format string, i.e. after the opening quote.
Look at the line printf(!!!!!!!"%s\n", thisEnv);
Change to printf("!!!! %s\n", thisEnv);
It has nothing to do with your C/C++ application.
try the following:
$ ./script.sh
$ echo $LD_LIBRARY_PATH
And you'll see that LD_LIBRARY_PATH is not set
When you launch your script, bash creates a new process with its environment inherited from the original bash process. In that newly created process, you set the process environment variable LD_LIBRARY_PATH=xxxx
When finalized your script.sh exits, and its environment dies with it.
Meaning the LD_LIBRARY_PATH is not set in your original shell.
As mentioned here above you need to run your script in the current shell
Either with . or with source.
I tested with your C/C++ and it works

Permissions issue calling bash script from c++ code that apache is running

The goal of this code is to create a stack trace whenever a sigterm/sigint/sigsegv/etc is caught. In order to not rely on memory management inside of the C++ code in the case of a sigsegv, I have decided to write a bash script that will receive the PID and memory addresses in the trace array.
The Sig events are being caught.
Below is where I generate the call to the bash script
trace_size = backtrace(trace, 16);
trace[1] = (void *)ctx->rsi;
messages = backtrace_symbols(trace, trace_size);
char syscom[356] = {0};
sprintf(syscom,"bash_crash_supp.sh %d", getpid());
for (i=1; i<(trace_size-1) && i < 10; ++i)
{
sprintf(syscom,"%s %p",syscom,trace[i]);
}
Below is where my issue arises. The command in syscom is generating correctly. I can stop the code before the following popen, run the command in a terminal, and it functions correctly.
However running the script directly from the c++ code does not seem to work.
setuid(0);
FILE *bashCommand = popen(syscom,"r");
char buf[256] = {0};
while(fgets(buf,sizeof(buf),bashCommand) != 0) {
LogMessage(LOG_WARNING, "%s\n", buf);
}
pclose(bashCommand);
exit(sig);
The purpose of the bash script is to get the offset from /proc/pid/maps, and then use that to run addr2line to get the file name/line number.
strResult=$(sudo cat /proc/"$1"/maps | grep "target_file" | grep -m 1 '[0-9a-fA-F]')
offset=$( cut -d '-' -f 1 <<< "$strResult");
However offset is getting 0 when I run from the c++ code, but when I run the exact same command (that is stored in syscom in the c++ code) in a terminal, I get the expected output.
I have been trying to fix this for a while. Permissions are most likely the issue, but I've tried to work around these with every way I know of/have seen via google. The user trying to run the script (currently running the c++ code) is apache.
The fix does not need to worry about the security of the box. If something as simple as "chmod 777 /proc -r" worked, that would have been the solution (sadly the OS doesn't let me mess do such commands with /proc).
Things I've already tried:
Adding `` around the command that's stored in syscom from the c++ code
chown the script to apache
chmod 4755 on the bash_crash_supp.sh script, allowing it to always fire as root.
I have added apache to sudoers (visudo), allowing them to run sudo without using a password
I have added a sub file to sudoers.d (just in case) that does the same as above
I have looked into objdump, however it does not give me either the offset or the file/line num for an addr (from what I can see)
I have setuid(0) in the c++ code to set the current user to root
Command generated in C++
bash_crash_supp.sh 25817 0x7f4bfe600ec8 0x7f4bf28f7400 0x7f4bf28f83c6 0x7f4bf2904f02 0x7f4bfdf0fbb0 0x7f4bfdf1346e 0x7f4bfdf1eb30 0x7f4bfdf1b9a8 0x7f4bfdf176b8
Params in bash:
25817 0x7f4bfe600ec8 0x7f4bf28f7400 0x7f4bf28f83c6 0x7f4bf2904f02 0x7f4bfdf0fbb0 0x7f4bfdf1346e 0x7f4bfdf1eb30 0x7f4bfdf1b9a8 0x7f4bfdf176b8
Can anyone think of any other ways to solve this?
Long story short, almost all Unix-based systems ignore setuid on any interpreted script (anything with a shebang #!) as a security precaution.
You may use setuid on actual executables, but not the shell scripts themselves. If you're willing to take a massive security risk, you can make a wrapper executable to run the shell script and give the executable setuid.
For more information, see this question on the Unix StackExchange: https://unix.stackexchange.com/a/2910

What environment variables are presented to a Fortran program for GET_ENVIRONMENT_VARIABLE

I would like to use the value of the $HOSTNAME environment variable in my Fortran code.
My attempt to do so is:
CHARACTER(LEN=100) :: hostname
INTEGER :: status_value = 0
CALL GET_ENVIRONMENT_VARIABLE("hostname",hostname, STATUS=status_value)
IF (status_value == 2) THEN
WRITE(nout,*) 'WARNING: Processor does not support environment variables - hostname is unknown.'
hostname = 'Unknown'
ELSE IF (status_value == -1) THEN
WRITE(nout,*) 'WARNING: Hostname is too long for character variable - hostname is truncated.'
ELSE IF (status_value == 1) THEN
WRITE(nout,*) 'WARNING: $HOSTNAME environment variable does not exist - hostname is unknown.'
hostname = 'Unknown'
But my result is that I always get the result that $HOSTNAME does not exist.
My interpretation of the status value is based on this:
http://gcc.gnu.org/onlinedocs/gfortran/GET_005fENVIRONMENT_005fVARIABLE.html
Interestingly, the example at the above page uses the 'HOME' environment variable. This environment variable works for me.
But I see no reason why $HOSTNAME should not:
MacBook-Pro:1N45 emiller$ echo $HOSTNAME # My shell prompt
MacBook-Pro.local
What is going on? What environment does my Fortran program see at runtime?
For what it's worth, I am using iFort 12.
This actually works fine, but on most platforms, these environment variable names are case sensitive. I get your results on my linux box with gfortran or ifort, but if I change your "hostname" string to "HOSTNAME" I get the expected results.

How can I get the user's locale when running as root?

I am running as root, but have the user's uid (e.g. 504). How can I work out the user's locale (in my case en_GB)? The following does not work:
setuid(user_uid);
fprintf(stderr,
CFStringGetCStringPtr(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
kCFStringEncodingMacRoman);
setuid(0);
This outputs en_US for me.
This information is contained in GlobalPreferences.plist, so running:
$ defaults read /Library/Preferences/.GlobalPreferences AppleLocale
gives the desired result.
You can't, because it doesn't exist. The locale is controled by
environment variables, and can change dynamically, and from window to
window and even from application to application (i.e. if the user
started a program with:
env LC_LANG=fr_FR program_name ...
.) Under Unix, you might be able to get user's default locale by doing
something like:
FILE* in = popen( "su -c 'env | grep ^LC_ ; env | grep ^LANG' - user", "r" );
, then reading and parsing the input, but I don't think that there's
anything simpler.

Using getenv function in Linux

I have this following simple program:
int main()
{
char* v = getenv("TEST_VAR");
cout << "v = " << (v==NULL ? "NULL" : v) << endl;
return 0;
}
These lines are added to .bashrc file:
TEST_VAR="2"
export TEST_VAR
Now, when I run this program from the terminal window (Ubuntu 10.04), it prints v = 2. If I run the program by another way: using launcher or from Eclipse, it prints NULL. I think this is because TEST_VAR is defined only inside bash shell. How can I create persistent Linux environment variable, which is accessible in any case?
On my system (Fedora 13) you can make system wide environment variables by adding them under /etc/profile.d/.
So for example if you add this to a file in /etc/profile.d/my_system_wide.sh
SYSTEM_WIDE="system wide"
export SYSTEM_WIDE
and then open a another terminal it should source it regardless of who the user is opening the terminal
echo $SYSTEM_WIDE
system_wide
Add that to .bash_profile (found in your home directory). You will need to log out and log back in for it to take effect.
Also, since you are using bash, you can combine the export and set in a single statement:
export TEST_VAR="2"
Sorry if I'm being naive but isn't .bash_profile useful only if you are running bash as your default shell ?
I 'sometimes' use Linux and mostly use ksh. I have .profile so may be you should check for .*profile and export the variable there.
Good luck :)
There is no such thing as a system-wide environment variable on Linux. Every process has its own environment. Now by default, every process inherits its environment from its parent, so you can get something like a system-wide environment by ensuring that a var is set in an ancestor of every process of interest. Then, as long as no other process changes that var, every process of interest will have it set.
The other answers here give various methods of setting variables early. For example, .bash_profile sets it in every login process a user runs, which is the ultimate parent of every process they run after login.
/etc/profile is read by every bash login by every user.