10 #include <sys/utsname.h>
17 static void env_set(const char *var, const char *val);
18 static int prefix_determine(char *argv0);
21 env_set(const char *var, const char *val)
30 buf = alloca(strlen(var) + 1 + strlen(val) + 1);
31 snprintf(buf, sizeof(buf), "%s=%s", var, val);
43 if (getenv(var)) putenv(var);
48 /* local subsystem functions */
49 static int _prefix_fallbacks(void);
50 static int _prefix_try_proc(void);
51 static int _prefix_try_argv(char *argv0);
53 /* local subsystem globals */
54 static char *_exe_path = NULL;
55 static char *_prefix_path = NULL;
57 /* externally accessible functions */
59 prefix_determine(char *argv0)
63 if (!_prefix_try_proc())
65 if (!_prefix_try_argv(argv0))
71 /* _exe_path is now a full absolute path TO this exe - figure out rest */
73 * exe = /blah/whatever/bin/exe
75 * prefix = /blah/whatever
77 p = strrchr(_exe_path, '/');
81 while (p >= _exe_path)
85 _prefix_path = malloc(p - _exe_path + 1);
88 strncpy(_prefix_path, _exe_path, p - _exe_path);
89 _prefix_path[p - _exe_path] = 0;
110 _prefix_fallbacks(void)
114 _prefix_path = strdup(PACKAGE_BIN_DIR);
115 p = strrchr(_prefix_path, '/');
117 printf("WARNING: Enlightenment could not determine its installed prefix\n"
118 " and is falling back on the compiled in default:\n"
119 " %s\n", _prefix_path);
124 _prefix_try_proc(void)
130 func = (void *)_prefix_try_proc;
131 f = fopen("/proc/self/maps", "r");
133 while (fgets(buf, sizeof(buf), f))
136 char *p, mode[5] = "";
137 unsigned long ptr1 = 0, ptr2 = 0;
140 if (buf[len - 1] == '\n')
145 if (sscanf(buf, "%lx-%lx %4s", &ptr1, &ptr2, mode) == 3)
147 if (!strcmp(mode, "r-xp"))
149 if (((void *)ptr1 <= func) && (func < (void *)ptr2))
151 p = strchr(buf, '/');
156 if (!strcmp(buf + len - 10, " (deleted)"))
159 _exe_path = strdup(p);
174 _prefix_try_argv(char *argv0)
176 char *path, *p, *cp, *s;
179 char buf[PATH_MAX], buf2[PATH_MAX], buf3[PATH_MAX];
181 char buf[4096], buf2[4096], buf3[4096];
184 /* 1. is argv0 abs path? */
187 _exe_path = strdup(argv0);
188 if (access(_exe_path, X_OK) == 0) return 1;
193 /* 2. relative path */
194 if (strchr(argv0, '/'))
196 if (getcwd(buf3, sizeof(buf3)))
198 snprintf(buf2, sizeof(buf2), "%s/%s", buf3, argv0);
199 if (realpath(buf2, buf))
201 _exe_path = strdup(buf);
202 if (access(_exe_path, X_OK) == 0) return 1;
208 /* 3. argv0 no path - look in PATH */
209 path = getenv("PATH");
213 lenexe = strlen(argv0);
214 while ((p = strchr(cp, ':')))
217 s = malloc(len + 1 + lenexe + 1);
222 strcpy(s + len + 1, argv0);
223 if (realpath(s, buf))
225 if (access(buf, X_OK) == 0)
227 _exe_path = strdup(buf);
237 s = malloc(len + 1 + lenexe + 1);
242 strcpy(s + len + 1, argv0);
243 if (realpath(s, buf))
245 if (access(buf, X_OK) == 0)
247 _exe_path = strdup(buf);
254 /* 4. big problems. arg[0] != executable - weird execution */
263 char buf[4096], tbuf[256 * 1024];
265 int l, fd, children = 0, cret;
267 home = getenv("HOME");
268 if (home) snprintf(buf, sizeof(buf), "%s/.e-precache", home);
269 else snprintf(buf, sizeof(buf), "/tmp/.e-precache");
274 // while (fgets(buf, sizeof(buf), f));
276 while (fgets(buf, sizeof(buf), f))
279 if (l > 0) buf[l - 1] = 0;
284 else if (buf[0] == 'o')
286 fd = open(buf + 2, O_RDONLY);
289 while (read(fd, tbuf, 256 * 1024) > 0);
293 else if (buf[0] == 'd')
295 fd = open(buf + 2, O_RDONLY);
298 while (read(fd, tbuf, 256 * 1024) > 0);
321 find_valgrind(char *path, size_t path_len)
323 const char *env = getenv("PATH");
327 const char *p = strchr(env, ':');
337 else if (p_len + sizeof("/valgrind") >= path_len)
340 memcpy(path, env, p_len);
341 memcpy(path + p_len, "/valgrind", sizeof("/valgrind"));
342 if (access(path, X_OK | R_OK) == 0)
356 /* maximum number of arguments added above */
357 #define VALGRIND_MAX_ARGS 9
358 /* bitmask with all supported bits set */
359 #define VALGRIND_MODE_ALL 15
362 valgrind_append(char **dst, int valgrind_mode, int valgrind_tool, char *valgrind_path, const char *valgrind_log)
368 dst[i++] = valgrind_path;
370 switch (valgrind_tool)
372 case 1: dst[i++] = "--tool=massif"; break;
373 case 2: dst[i++] = "--tool=callgrind"; break;
381 dst[i++] = valgrind_path;
382 dst[i++] = "--track-origins=yes";
383 dst[i++] = "--malloc-fill=13"; /* invalid pointer, make it crash */
387 static char logparam[PATH_MAX + sizeof("--log-file=")];
389 snprintf(logparam, sizeof(logparam), "--log-file=%s", valgrind_log);
393 if (valgrind_mode & 2)
394 dst[i++] = "--trace-children=yes";
396 if (valgrind_mode & 4)
398 dst[i++] = "--leak-check=full";
399 dst[i++] = "--leak-resolution=high";
400 dst[i++] = "--track-fds=yes";
403 if (valgrind_mode & 8)
404 dst[i++] = "--show-reachable=yes";
410 copy_args(char **dst, char **src, size_t count)
412 for (; count > 0; count--, dst++, src++)
417 main(int argc, char **argv)
419 int i, do_precache = 0, valgrind_mode = 0;
420 int valgrind_tool = 0;
421 char buf[16384], **args, *p;
422 char valgrind_path[PATH_MAX] = "";
423 const char *valgrind_log = NULL;
425 prefix_determine(argv[0]);
427 env_set("E_START", argv[0]);
430 if (p) snprintf(buf, sizeof(buf), "%s/bin:%s", _prefix_path, p);
431 else snprintf(buf, sizeof(buf), "%s/bin", _prefix_path);
432 env_set("PATH", buf);
434 p = getenv("LD_LIBRARY_PATH");
435 if (p) snprintf(buf, sizeof(buf), "%s/lib:%s", _prefix_path, p);
436 else snprintf(buf, sizeof(buf), "%s/lib", _prefix_path);
437 env_set("LD_LIBRARY_PATH", buf);
439 for (i = 1; i < argc; i++)
441 if (!strcmp(argv[i], "-no-precache")) do_precache = 0;
442 else if (!strncmp(argv[i], "-valgrind", sizeof("-valgrind") - 1))
444 const char *val = argv[i] + sizeof("-valgrind") - 1;
448 else if (*val == '-')
451 if (!strncmp(val, "log-file=", sizeof("log-file=") - 1))
453 valgrind_log = val + sizeof("log-file=") - 1;
454 if (*valgrind_log == '\0')
458 else if (*val == '=')
461 if (!strcmp(val, "all"))
462 valgrind_mode = VALGRIND_MODE_ALL;
464 valgrind_mode = atoi(val);
467 printf("Unknown valgrind option: %s\n", argv[i]);
469 else if (!strcmp(argv[i], "-massif"))
473 else if (!strcmp(argv[i], "-callgrind"))
477 else if ((!strcmp(argv[i], "-h")) ||
478 (!strcmp(argv[i], "-help")) ||
479 (!strcmp(argv[i], "--help")))
484 "\t\tDisable pre-caching of files\n"
485 "\t-valgrind[=MODE]\n"
486 "\t\tRun enlightenment from inside valgrind, mode is OR of:\n"
487 "\t\t 1 = plain valgrind to catch crashes (default)\n"
488 "\t\t 2 = trace children (thumbnailer, efm slaves, ...)\n"
489 "\t\t 4 = check leak\n"
490 "\t\t 8 = show reachable after processes finish.\n"
491 "\t\t all = all of above\n"
493 "\t\tRun enlightenment from inside massif valgrind tool.\n"
495 "\t\tRun enlightenment from inside callgrind valgrind tool.\n"
496 "\t-valgrind-log-file=<FILENAME>\n"
497 "\t\tSave valgrind log to file, see valgrind's --log-file for details.\n"
500 "\tenlightenment %s\n"
501 "for more options.\n",
507 if (valgrind_mode || valgrind_tool)
509 if (!find_valgrind(valgrind_path, sizeof(valgrind_path)))
511 printf("E - valgrind required but no binary found! Ignoring request.\n");
516 printf("E - PID=%i, do_precache=%i, valgrind=%d", getpid(), do_precache, valgrind_mode);
519 printf(" valgrind-command='%s'", valgrind_path);
521 printf(" valgrind-log-file='%s'", valgrind_log);
529 /* sanity checks - if precache might fail - check here first */
530 lib = dlopen("libevas.so", RTLD_GLOBAL | RTLD_LAZY);
531 if (!lib) dlopen("libevas.so.1", RTLD_GLOBAL | RTLD_LAZY);
533 func = dlsym(lib, "evas_init");
534 if (!func) goto done;
535 lib = dlopen("libecore_file.so", RTLD_GLOBAL | RTLD_LAZY);
536 if (!lib) dlopen("libecore_file.so.1", RTLD_GLOBAL | RTLD_LAZY);
538 func = dlsym(lib, "ecore_file_init");
539 if (!func) goto done;
540 lib = dlopen("libeet.so", RTLD_GLOBAL | RTLD_LAZY);
541 if (!lib) dlopen("libeet.so.0", RTLD_GLOBAL | RTLD_LAZY);
543 func = dlsym(lib, "eet_init");
544 if (!func) goto done;
545 /* precache SHOULD work */
546 snprintf(buf, sizeof(buf), "%s/lib/enlightenment/preload/e_precache.so", _prefix_path);
547 env_set("LD_PRELOAD", buf);
548 printf("E - PRECACHE GOING NOW...\n");
554 /* try dbus-launch */
555 snprintf(buf, sizeof(buf), "%s/bin/enlightenment", _prefix_path);
557 args = alloca((argc + 2 + VALGRIND_MAX_ARGS) * sizeof(char *));
558 if ((!getenv("DBUS_SESSION_BUS_ADDRESS")) &&
559 (!getenv("DBUS_LAUNCHD_SESSION_BUS_SOCKET")))
561 args[0] = "dbus-launch";
562 args[1] = "--exit-with-session";
564 i = 2 + valgrind_append(args + 2, valgrind_mode, valgrind_tool, valgrind_path, valgrind_log);
566 copy_args(args + i, argv + 1, argc - 1);
567 args[i + argc - 1] = NULL;
568 execvp("dbus-launch", args);
571 /* dbus-launch failed - run e direct */
572 i = valgrind_append(args, valgrind_mode, valgrind_tool, valgrind_path, valgrind_log);
574 copy_args(args + i, argv + 1, argc - 1);
575 args[i + argc - 1] = NULL;
576 execv(args[0], args);
578 printf("FAILED TO RUN:\n");
579 printf(" %s\n", buf);