Fix GetExecutablePath on Linux

It was using lstat to get the size of /proc/self/exe but
it always returns 0, so we just use a big buffer on the
stack instead.

BUG=angleproject:892

Change-Id: I6d88efeb4ec5de7a78cb3668e3d78520203ad1d5
Reviewed-on: https://chromium-review.googlesource.com/269990
Reviewed-by: Kenneth Russell <kbr@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Tested-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Corentin Wallez
2015-05-07 13:55:14 -04:00
parent 0e201cb7df
commit 2cf30bd09c

View File

@@ -7,36 +7,25 @@
// Linux_path_utils.cpp: Implementation of OS-specific path functions for Linux
#include "path_utils.h"
#include <array>
#include <sys/stat.h>
#include <unistd.h>
#include <array>
std::string GetExecutablePath()
{
struct stat sb;
if (lstat("/proc/self/exe", &sb) == -1)
// We cannot use lstat to get the size of /proc/self/exe as it always returns 0
// so we just use a big buffer and hope the path fits in it.
char path[4096];
ssize_t result = readlink("/proc/self/exe", path, sizeof(path) - 1);
if (result < 0 || result >= sizeof(path) - 1)
{
return "";
}
char *path = static_cast<char*>(malloc(sb.st_size + 1));
if (!path)
{
return "";
}
ssize_t result = readlink("/proc/self/exe", path, sb.st_size);
if (result != sb.st_size)
{
free(path);
return "";
}
path[sb.st_size] = '\0';
std::string strPath(path);
free(path);
return strPath;
path[result] = '\0';
return path;
}
std::string GetExecutableDirectory()