Use getauxval for obtaining the executable path.

It appears that reading /proc/self/exe actually can return the path
of the shared library from which the reading is made.
This commit is contained in:
Johan Wikman
2016-02-12 13:47:26 +02:00
parent d1ba050bfd
commit 0aaba6bc5f

View File

@ -22,6 +22,8 @@
* *
*/ */
#include <sys/auxv.h>
#include <elf.h>
#include <log_manager.h> #include <log_manager.h>
#include <modules.h> #include <modules.h>
#include <query_classifier.h> #include <query_classifier.h>
@ -86,37 +88,32 @@ static void end_and_unload_classifier(QUERY_CLASSIFIER* classifier, const char*
static bool resolve_pp_path(char* path, int size) static bool resolve_pp_path(char* path, int size)
{ {
bool success = false; bool success = false;
ssize_t sz = readlink("/proc/self/exe", path, size); const char* exe_path = (const char*) getauxval(AT_EXECFN);
size_t len = strlen(exe_path);
if (sz >= 0) if (len < size)
{ {
if (sz == size) strcpy(path, exe_path);
{ char* s = path + len;
MXS_ERROR("The full path of the current executable does not fit in a "
"buffer of %d bytes.", size);
}
else
{
char* s = path + sz;
// Find the last '/'. // Find the last '/'.
while ((*s != '/') && (s != path)) while ((*s != '/') && (s != path))
{ {
--s; --s;
--sz; --len;
} }
if (*s == '/') if (*s == '/')
{ {
++s; ++s;
++sz; ++len;
} }
*s = 0; *s = 0;
int required_size = sz + sizeof(MAXPP) + 1; int required_size = len + sizeof(MAXPP) + 1;
if (required_size + 1 <= size) if (required_size <= size)
{ {
strcat(path, MAXPP); strcat(path, MAXPP);
@ -129,10 +126,10 @@ static bool resolve_pp_path(char* path, int size)
"not fit into a buffer of %d bytes. ", size); "not fit into a buffer of %d bytes. ", size);
} }
} }
}
else else
{ {
MXS_ERROR("Could not establish location of current process."); MXS_ERROR("The full path of the current executable does not fit in a "
"buffer of %d bytes.", size);
} }
return success; return success;