1

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?

user46726
  • 47
  • 8
  • Try ‘extern char ** environ‘ (a null terminated array of pointers to null terminated strings just like argv). That should give you a list of all the environment variables so you can tell if it's the environment or getenv to blame. Example at http://stackoverflow.com/questions/3473692/list-environment-variables-with-c-in-unix – John5342 Mar 19 '14 at 19:35
  • Hi John, thanks for your comment. It turned out to be a permission problem. After restarting the server I executed the UDF again with root permission and it worked. Thus, nothing else than a common Unix issue. :) Regards, Chris – user46726 Mar 19 '14 at 20:37
  • What do you mean by "executed the UDF again with root permission"? As in, the root mysql user? Or the running the mysql server as root on the box? – Brian Leishman Sep 25 '20 at 19:16

0 Answers0