I'm experiencing a strange issue with the C/C++ function getenv(). The function is used within a MySQL UDF on a Linux system (Linux Mint 16 Cinnamon 64bit). I've set the JAVA_HOME environment variable system-wide by editing the file /etc/environment and it is printed in the shell along with all other variables when using the env command, both as user and root. The code is simple and looks as follows:
char *javaHome;
javaHome = getenv("JAVA_HOME");
if (!javaHome) {
char *m = "Error getting JAVA_HOME variable";
strcpy(message, m);
return 1;
}
The UDF permanently returns the error message in the condition and hence the javaHome variable is always null. I've created another simple C++ program and dropped the cpp file into the MySQL plugin directory where the compiled UDF library resides (/usr/lib/mysql/plugin).
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
char *javaHome = getenv("JAVA_HOME");
cout << javaHome;
return 0;
}
The code returns the value of the variable,i.e.path to the JRE, as expected. The UDF code works fine on my Windows system, but in order to get it to work on Unix systems I had to adapt the code a little and replaced the GetEnvironmentVariableA() function with getenv(). I can't figure out what the problem is. Any pointers would be appreciated.
EDIT: I've tested the result of the "HOME" variable and the output was curiously /etc/mysql/ instead of /root/ as I would have expected. Do UDF's have their own environment or something like that?