Ticket #2086: pathDetection.2.diff

File pathDetection.2.diff, 1.5 KB (added by starslayer, 9 years ago)
  • source/lib/sysdep/os/linux/linux.cpp

     
    77 * distribute, sublicense, and/or sell copies of the Software, and to
    88 * permit persons to whom the Software is furnished to do so, subject to
    99 * the following conditions:
    10  * 
     10 *
    1111 * The above copyright notice and this permission notice shall be included
    1212 * in all copies or substantial portions of the Software.
    13  * 
     13 *
    1414 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    1515 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    1616 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     
    2727#define GNU_SOURCE
    2828#include "mocks/dlfcn.h"
    2929#include "mocks/unistd.h"
    30 
    3130#include <cstdio>
    3231
     32
     33static bool getPathFromProc(char* buffer, size_t length)
     34{
     35    int pos = readlink("/proc/self/exe", buffer, length-1);
     36    if (pos <= 0)
     37        return false;
     38
     39    buffer[pos] = '\0';
     40
     41    char* endOfPath = strrchr(buffer, '/');
     42    if (endOfPath == NULL)
     43        return false;
     44
     45    ++endOfPath;
     46    *endOfPath = '\0';
     47
     48    return true;
     49}
     50
    3351OsPath sys_ExecutablePathname()
    3452{
     53    // Try to find the Path with readlink
     54    char pathBuffer[PATH_MAX];
     55    if (getPathFromProc(pathBuffer, sizeof(pathBuffer)))
     56        return pathBuffer;
     57
    3558    // Find the executable's filename
    3659    Dl_info dl_info;
    3760    memset(&dl_info, 0, sizeof(dl_info));