efl/eina_prefix: cleanup and review logs.

there shouldn't be behavior changes at this point, just minor cleanups
and simplifications, particularly to the log.



SVN revision: 82464
This commit is contained in:
Gustavo Sverzut Barbieri 2013-01-09 15:21:40 +00:00
parent 954a831e89
commit 2f49e495ee
1 changed files with 189 additions and 119 deletions

View File

@ -56,6 +56,10 @@
#include "eina_private.h" #include "eina_private.h"
#include "eina_alloca.h" #include "eina_alloca.h"
#include "eina_log.h" #include "eina_log.h"
#include "eina_str.h"
/* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
#include "eina_safety_checks.h"
#include "eina_prefix.h" #include "eina_prefix.h"
#ifdef _WIN32 #ifdef _WIN32
@ -120,6 +124,73 @@ struct _Eina_Prefix
static int _eina_prefix_log_dom = -1; static int _eina_prefix_log_dom = -1;
static int
_path_join(char *buf, int bufsize, const char *base, const char *extra)
{
return eina_str_join(buf, bufsize, DSEP_C, base, extra);
}
static int
_path_join_multiple(char *buf, int bufsize, ...)
{
va_list ap;
int used = 0;
EINA_SAFETY_ON_NULL_RETURN_VAL(buf, -1);
EINA_SAFETY_ON_TRUE_RETURN_VAL(bufsize < 1, -1);
va_start(ap, bufsize);
while (used < bufsize - 1)
{
const char *comp = va_arg(ap, const char *);
int complen, seplen;
if (!comp) break;
seplen = (used == 0) ? 0 : 1;
complen = strlen(comp);
if (seplen + complen >= bufsize -1)
{
va_end(ap);
buf[0] = '\0';
return -1;
}
if (used > 0)
buf[used] = DSEP_C;
memcpy(buf + used + seplen, comp, complen);
used += complen + seplen;
}
va_end(ap);
buf[used] = '\0';
return used;
}
static void
_path_sep_fix(char *buf)
{
#ifdef _WIN32
for (; *buf != '\0'; buf++)
{
if (*buf == '/')
*buf = DSEP_C;
}
#else
(void)buf;
#endif
}
static Eina_Bool
_path_absolute_check(const char *path)
{
#ifdef _WIN32
return evil_path_is_absolute(path);
#else
return (path[0] == DSEP_C);
#endif
}
static int static int
_fallback(Eina_Prefix *pfx, const char *pkg_bin, const char *pkg_lib, _fallback(Eina_Prefix *pfx, const char *pkg_bin, const char *pkg_lib,
const char *pkg_data, const char *pkg_locale, const char *envprefix) const char *pkg_data, const char *pkg_locale, const char *envprefix)
@ -163,10 +234,13 @@ _try_proc(Eina_Prefix *pfx, void *symbol)
FILE *f; FILE *f;
char buf[4096]; char buf[4096];
DBG("Try /proc/self/maps");
f = fopen("/proc/self/maps", "rb"); f = fopen("/proc/self/maps", "rb");
if (!f) return 0; if (!f)
DBG("Exists /proc/self/maps"); {
WRN("Couldn't read /proc/self/maps to lookup symbol=%p", symbol);
return 0;
}
DBG("Check /proc/self/maps for symbol=%p", symbol);
while (fgets(buf, sizeof(buf), f)) while (fgets(buf, sizeof(buf), f))
{ {
int len; int len;
@ -185,28 +259,30 @@ _try_proc(Eina_Prefix *pfx, void *symbol)
{ {
if (((void *)ptr1 <= symbol) && (symbol < (void *)ptr2)) if (((void *)ptr1 <= symbol) && (symbol < (void *)ptr2))
{ {
DBG("Found in /proc/self/maps: %s", buf);
p = strchr(buf, '/'); p = strchr(buf, '/');
if (p) if (p)
{ {
DBG("Found in /proc/self/maps: found last /");
if (len > 10) if (len > 10)
{ {
if (!strcmp(buf + len - 10, " (deleted)")) if (!strcmp(buf + len - 10, " (deleted)"))
buf[len - 10] = 0; buf[len - 10] = 0;
} }
STRDUP_REP(pfx->exe_path, p); STRDUP_REP(pfx->exe_path, p);
INF("Found in /proc/self/maps: guess exe path is %s", pfx->exe_path); INF("Found %p in /proc/self/maps: %s (%s)", symbol, pfx->exe_path, buf);
fclose(f); fclose(f);
return 1; return 1;
} }
else break; else
{
DBG("Found %p in /proc/self/maps but not a file (%s)", symbol, buf);
break;
}
} }
} }
} }
} }
fclose(f); fclose(f);
WRN("Failed in /proc/self/maps"); WRN("Couldn't find symbol %p in a file in /proc/self/maps", symbol);
return 0; return 0;
} }
#endif #endif
@ -214,108 +290,119 @@ _try_proc(Eina_Prefix *pfx, void *symbol)
static int static int
_try_argv(Eina_Prefix *pfx, const char *argv0) _try_argv(Eina_Prefix *pfx, const char *argv0)
{ {
char *path, *p, *cp, *s; char *path, *p, *cp;
int len, lenexe; int len, lenexe;
char buf[PATH_MAX], buf2[PATH_MAX], buf3[PATH_MAX]; char buf[PATH_MAX], buf2[PATH_MAX];
DBG("Try argv0 = %s", argv0);
/* 1. is argv0 abs path? */ /* 1. is argv0 abs path? */
#ifdef _WIN32 if (_path_absolute_check(argv0))
if (evil_path_is_absolute(argv0))
#else
if (argv0[0] == DSEP_C)
#endif
{ {
DBG("Match arvg0 is full path: %s", argv0); if (access(argv0, X_OK) == 0)
STRDUP_REP(pfx->exe_path, argv0);
if (access(pfx->exe_path, X_OK) == 0)
{ {
INF("Executable argv0 = %s", argv0); INF("Executable argv0 is full path = %s", argv0);
STRDUP_REP(pfx->exe_path, argv0);
return 1; return 1;
} }
IF_FREE_NULL(pfx->exe_path); WRN("Non executable argv0: %s", argv0);
DBG("Non existent argv0: %s", argv0); return 0;
return 0;
} }
/* 2. relative path */ /* 2. relative path */
if (strchr(argv0, DSEP_C)) if (strchr(argv0, DSEP_C))
{ {
DBG("Relative path argv0: %s", argv0); if (getcwd(buf2, sizeof(buf2)))
if (getcwd(buf3, sizeof(buf3))) {
{ char joined[PATH_MAX];
snprintf(buf2, sizeof(buf2), "%s" DSEP_S "%s", buf3, argv0); _path_join(joined, sizeof(joined), buf2, argv0);
DBG("Relative to CWD: %s", buf2); if (realpath(joined, buf))
if (realpath(buf2, buf)) {
{ if (access(buf, X_OK) == 0)
DBG("Realpath is: %s", buf);
STRDUP_REP(pfx->exe_path, buf);
if (access(pfx->exe_path, X_OK) == 0)
{ {
INF("Path %s is executable", pfx->exe_path); INF("Executable relative argv0=%s, cwd=%s, realpath=%s",
argv0, buf2, buf);
STRDUP_REP(pfx->exe_path, buf);
return 1; return 1;
} }
DBG("Fail check for executable: %s", pfx->exe_path); WRN("Non executable relative argv0=%s, cwd=%s, realpath=%s",
IF_FREE_NULL(pfx->exe_path); argv0, buf2, buf);
} }
} else
WRN("No realpath for argv0=%s, cwd=%s", argv0, buf2);
}
else
WRN("Couldn't get current directory to lookup argv0=%s", argv0);
} }
/* 3. argv0 no path - look in PATH */ /* 3. argv0 no path - look in PATH */
DBG("Look for argv0=%s in $PATH", argv0);
path = getenv("PATH"); path = getenv("PATH");
if (!path) return 0; if (!path)
{
DBG("No env PATH to lookup argv0=%s", argv0);
return 0;
}
p = path; p = path;
cp = p; cp = p;
lenexe = strlen(argv0); lenexe = strlen(argv0);
while ((p = strchr(cp, PSEP_C))) while ((p = strchr(cp, PSEP_C)))
{ {
len = p - cp; len = p - cp;
s = malloc(len + 1 + lenexe + 1); if ((len == 0) || (len + lenexe + 2 >= (int)sizeof(buf2)))
if (s) {
{ cp = p + 1;
strncpy(s, cp, len); continue;
s[len] = DSEP_C; }
strcpy(s + len + 1, argv0);
DBG("Try path: %s", s); strncpy(buf2, cp, len);
if (realpath(s, buf)) buf2[len] = DSEP_C;
{ strcpy(buf2 + len + 1, argv0);
DBG("Realpath is: %s", buf); if (realpath(buf2, buf))
if (access(buf, X_OK) == 0) {
{ if (access(buf, X_OK) == 0)
STRDUP_REP(pfx->exe_path, buf); {
INF("Path %s is executable", pfx->exe_path); STRDUP_REP(pfx->exe_path, buf);
free(s); INF("Path %s is executable", pfx->exe_path);
return 1; return 1;
} }
} else
free(s); DBG("Path not executable %s", buf);
} }
else
DBG("No realpath for argv0=%s in %.*s", argv0, len, cp);
cp = p + 1; cp = p + 1;
} }
/* 4. big problems. arg[0] != executable - weird execution */ /* 4. big problems. arg[0] != executable - weird execution */
WRN("Couldn't find argv0=%s in current directory or env PATH=%s",
argv0, path);
return 0; return 0;
} }
static int static int
_get_env_var(char **var, const char *env, const char *prefix, const char *dir) _get_env_var(char **var, const char *envprefix, const char *envsuffix, const char *prefix, const char *dir)
{ {
char buf[PATH_MAX]; char env[1024];
const char *s = getenv(env); const char *s;
DBG("Try env var %s", env); snprintf(env, sizeof(env), "%s_%s_DIR", envprefix, envsuffix);
s = getenv(env);
if (s) if (s)
{ {
INF("Have env %s = %s", env, s); INF("Have prefix env %s = %s", env, s);
STRDUP_REP(*var, s); STRDUP_REP(*var, s);
return 1; return 1;
} }
else if (prefix) else if (prefix)
{ {
snprintf(buf, sizeof(buf), "%s" DSEP_S "%s", prefix, dir); char buf[PATH_MAX];
INF("Have prefix %s = %s", prefix, buf); _path_join(buf, sizeof(buf), prefix, dir);
INF("Have %s_PREFIX = %s, use %s = %s", envprefix, prefix, env, buf);
STRDUP_REP(*var, buf); STRDUP_REP(*var, buf);
return 1; return 1;
} }
return 0; else
{
DBG("No env %s_PREFIX or %s for dir '%s'", envprefix, env, dir);
return 0;
}
} }
static int static int
@ -327,19 +414,16 @@ _get_env_vars(Eina_Prefix *pfx,
const char *localedir) const char *localedir)
{ {
char env[1024]; char env[1024];
const char *s; const char *prefix;
int ret = 0; int ret = 0;
snprintf(env, sizeof(env), "%s_PREFIX", envprefix); snprintf(env, sizeof(env), "%s_PREFIX", envprefix);
if ((s = getenv(env))) STRDUP_REP(pfx->prefix_path, s); if ((prefix = getenv(env))) STRDUP_REP(pfx->prefix_path, prefix);
snprintf(env, sizeof(env), "%s_BIN_DIR", envprefix);
ret += _get_env_var(&pfx->prefix_path_bin, env, s, bindir); ret += _get_env_var(&pfx->prefix_path_bin, envprefix, "BIN", prefix, bindir);
snprintf(env, sizeof(env), "%s_LIB_DIR", envprefix); ret += _get_env_var(&pfx->prefix_path_lib, envprefix, "LIB", prefix, libdir);
ret += _get_env_var(&pfx->prefix_path_lib, env, s, libdir); ret += _get_env_var(&pfx->prefix_path_data, envprefix, "DATA", prefix, datadir);
snprintf(env, sizeof(env), "%s_DATA_DIR", envprefix); ret += _get_env_var(&pfx->prefix_path_locale, envprefix, "LOCALE", prefix, localedir);
ret += _get_env_var(&pfx->prefix_path_data, env, s, datadir);
snprintf(env, sizeof(env), "%s_LOCALE_DIR", envprefix);
ret += _get_env_var(&pfx->prefix_path_locale, env, s, localedir);
return ret; return ret;
} }
@ -380,6 +464,8 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix,
DBG("EINA PREFIX: argv0=%s, symbol=%p, magicsharefile=%s, envprefix=%s", DBG("EINA PREFIX: argv0=%s, symbol=%p, magicsharefile=%s, envprefix=%s",
argv0, symbol, magicsharefile, envprefix); argv0, symbol, magicsharefile, envprefix);
DBG("EINA PREFIX: share=%s, bin=%s, lib=%s, data=%s, locale=%s",
sharedir, pkg_bin, pkg_lib, pkg_data, pkg_locale);
pfx = calloc(1, sizeof(Eina_Prefix)); pfx = calloc(1, sizeof(Eina_Prefix));
if (!pfx) return NULL; if (!pfx) return NULL;
@ -388,16 +474,10 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix,
{ {
int len; int len;
len = snprintf(buf, sizeof(buf), "%s" DSEP_S "%s", datadir, sharedir); len = _path_join(buf, sizeof(buf), datadir, sharedir);
if (len > 0) if (len > 0)
{ {
#ifdef _WIN32 _path_sep_fix(buf + strlen(datadir) + strlen(DSEP_S));
/* on win32 convert / to \ for path here */
for (p = buf + strlen(datadir) + strlen(DSEP_S); *p; p++)
{
if (*p == '/') *p = DSEP_C;
}
#endif
tmp = alloca(len + 1); tmp = alloca(len + 1);
strcpy(tmp, buf); strcpy(tmp, buf);
datadir = tmp; datadir = tmp;
@ -407,13 +487,7 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix,
{ {
magic = alloca(strlen(magicsharefile)); magic = alloca(strlen(magicsharefile));
strcpy(magic, magicsharefile); strcpy(magic, magicsharefile);
#ifdef _WIN32 _path_sep_fix(magic);
/* on win32 convert / to \ for path here */
for (p = magic; *p; p++)
{
if (*p == '/') *p = DSEP_C;
}
#endif
} }
/* look at compile-time package bin/lib/datadir etc. and figure out the /* look at compile-time package bin/lib/datadir etc. and figure out the
@ -479,10 +553,9 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix,
libdir = pkg_lib_p; libdir = pkg_lib_p;
datadir = pkg_data_p; datadir = pkg_data_p;
localedir = pkg_locale_p; localedir = pkg_locale_p;
DBG("Prefix relative bindir = %s", bindir); DBG("Prefix common=%.*s, bin=%s, lib=%s, data=%s, locale=%s",
DBG("Prefix relative libdir = %s", libdir); (int)(pkg_bin_p - pkg_bin), pkg_bin,
DBG("Prefix relative datadir = %s", datadir); bindir, libdir, datadir, localedir);
DBG("Prefix relative localedir = %s", localedir);
} }
/* 3. some galoot thought it awesome not to give us a common prefix at compile time /* 3. some galoot thought it awesome not to give us a common prefix at compile time
* so fall back to the compile time directories. we are no longer relocatable */ * so fall back to the compile time directories. we are no longer relocatable */
@ -506,29 +579,28 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix,
} }
#ifdef HAVE_DLADDR #ifdef HAVE_DLADDR
DBG("Try dladdr on %p", symbol);
if (symbol) if (symbol)
{ {
Dl_info info_dl; Dl_info info_dl;
if (dladdr(symbol, &info_dl)) if (dladdr(symbol, &info_dl))
{ {
DBG("Dlinfo worked");
if (info_dl.dli_fname) if (info_dl.dli_fname)
{ {
DBG("Dlinfo dli_fname = %s", info_dl.dli_fname); if (_path_absolute_check(info_dl.dli_fname))
# ifdef _WIN32
if (evil_path_is_absolute(info_dl.dli_fname))
# else
if (info_dl.dli_fname[0] == DSEP_C)
# endif
{ {
INF("Dlsym gave full path = %s", info_dl.dli_fname); INF("dladdr for symbol=%p: %s", symbol, info_dl.dli_fname);
STRDUP_REP(pfx->exe_path, info_dl.dli_fname); STRDUP_REP(pfx->exe_path, info_dl.dli_fname);
from_lib = EINA_TRUE; from_lib = EINA_TRUE;
} }
else
WRN("dladdr for symbol=%p: %s is relative", symbol, info_dl.dli_fname);
} }
else
WRN("no dladdr filename for symbol=%p", symbol);
} }
else
WRN("no dladdr for symbol=%p", symbol);
} }
#endif #endif
/* no env var - examine process and possible argv0 */ /* no env var - examine process and possible argv0 */
@ -540,6 +612,7 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix,
#endif #endif
if (!_try_argv(pfx, argv0)) if (!_try_argv(pfx, argv0))
{ {
WRN("Fallback - couldn't resolve based on argv0=%s", argv0);
_fallback(pfx, pkg_bin, pkg_lib, pkg_data, pkg_locale, _fallback(pfx, pkg_bin, pkg_lib, pkg_data, pkg_locale,
envprefix); envprefix);
return pfx; return pfx;
@ -551,7 +624,7 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix,
} }
if (!pfx->exe_path) if (!pfx->exe_path)
{ {
WRN("Fallback - nothing found"); WRN("Fallback - no variables, symbol or argv0 could be used.");
_fallback(pfx, pkg_bin, pkg_lib, pkg_data, pkg_locale, envprefix); _fallback(pfx, pkg_bin, pkg_lib, pkg_data, pkg_locale, envprefix);
return pfx; return pfx;
} }
@ -591,24 +664,21 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix,
DBG("Have prefix = %s", pfx->prefix_path); DBG("Have prefix = %s", pfx->prefix_path);
/* bin */ /* bin */
snprintf(buf, sizeof(buf), "%s" DSEP_S "%s", _path_join(buf, sizeof(buf), pfx->prefix_path, bindir);
pfx->prefix_path, bindir);
STRDUP_REP(pfx->prefix_path_bin, buf); STRDUP_REP(pfx->prefix_path_bin, buf);
DBG("Have bin = %s", pfx->prefix_path_bin); DBG("Have bin = %s", pfx->prefix_path_bin);
if ((!from_bin) && (stat(buf, &st) == 0)) if ((!from_bin) && (stat(buf, &st) == 0))
checks_passed++; checks_passed++;
/* lib */ /* lib */
snprintf(buf, sizeof(buf), "%s" DSEP_S "%s", _path_join(buf, sizeof(buf), pfx->prefix_path, libdir);
pfx->prefix_path, libdir);
STRDUP_REP(pfx->prefix_path_lib, buf); STRDUP_REP(pfx->prefix_path_lib, buf);
DBG("Have lib = %s", pfx->prefix_path_lib); DBG("Have lib = %s", pfx->prefix_path_lib);
if ((!from_lib) && (stat(buf, &st) == 0)) if ((!from_lib) && (stat(buf, &st) == 0))
checks_passed++; checks_passed++;
/* locale */ /* locale */
snprintf(buf, sizeof(buf), "%s" DSEP_S "%s", _path_join(buf, sizeof(buf), pfx->prefix_path, localedir);
pfx->prefix_path, localedir);
STRDUP_REP(pfx->prefix_path_locale, buf); STRDUP_REP(pfx->prefix_path_locale, buf);
DBG("Have locale = %s", pfx->prefix_path_locale); DBG("Have locale = %s", pfx->prefix_path_locale);
if (stat(buf, &st) == 0) if (stat(buf, &st) == 0)
@ -620,9 +690,10 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix,
else else
{ {
DBG("Magic = %s", magic); DBG("Magic = %s", magic);
snprintf(buf, sizeof(buf), _path_join_multiple(buf, sizeof(buf),
"%s" DSEP_S "%s" DSEP_S "%s", pfx->prefix_path,
pfx->prefix_path, datadir, magic); datadir,
magic, NULL);
DBG("Check in %s", buf); DBG("Check in %s", buf);
if (stat(buf, &st) == 0) if (stat(buf, &st) == 0)
@ -638,8 +709,7 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix,
if (((!magic) && (checks_passed > 0)) || if (((!magic) && (checks_passed > 0)) ||
((magic) && (magic_found))) ((magic) && (magic_found)))
{ {
snprintf(buf, sizeof(buf), "%s" DSEP_S "%s", _path_join(buf, sizeof(buf), pfx->prefix_path, datadir);
pfx->prefix_path, datadir);
STRDUP_REP(pfx->prefix_path_data, buf); STRDUP_REP(pfx->prefix_path_data, buf);
} }
else else