10 #include <sys/utsname.h>
18 static void env_set(const char *var, const char *val);
19 EAPI int prefix_determine(char *argv0);
22 env_set(const char *var, const char *val)
30 size_t size = strlen(var) + 1 + strlen(val) + 1;
33 snprintf(buf, size, "%s=%s", var, val);
34 if (getenv(var)) putenv(buf);
35 else putenv(strdup(buf));
43 if (getenv(var)) putenv(var);
48 /* local subsystem globals */
49 static Eina_Prefix *pfx = NULL;
51 /* externally accessible functions */
53 prefix_determine(char *argv0)
55 pfx = eina_prefix_new(argv0, prefix_determine,
56 "E", "enlightenment", "AUTHORS",
57 PACKAGE_BIN_DIR, PACKAGE_LIB_DIR,
58 PACKAGE_DATA_DIR, LOCALE_DIR);
68 char buf[4096], tbuf[256 * 1024];
70 int l, fd, children = 0, cret;
72 home = getenv("HOME");
73 if (home) snprintf(buf, sizeof(buf), "%s/.e-precache", home);
74 else snprintf(buf, sizeof(buf), "/tmp/.e-precache");
79 // while (fgets(buf, sizeof(buf), f));
81 while (fgets(buf, sizeof(buf), f))
84 if (l > 0) buf[l - 1] = 0;
87 if (buf[0] == 's') stat(buf + 2, &st);
88 else if (buf[0] == 'o')
90 fd = open(buf + 2, O_RDONLY);
93 while (read(fd, tbuf, 256 * 1024) > 0);
97 else if (buf[0] == 'd')
99 fd = open(buf + 2, O_RDONLY);
102 while (read(fd, tbuf, 256 * 1024) > 0);
125 find_valgrind(char *path, size_t path_len)
127 const char *env = getenv("PATH");
131 const char *p = strchr(env, ':');
134 if (p) p_len = p - env;
135 else p_len = strlen(env);
136 if (p_len <= 0) goto next;
137 else if (p_len + sizeof("/valgrind") >= path_len) goto next;
138 memcpy(path, env, p_len);
139 memcpy(path + p_len, "/valgrind", sizeof("/valgrind"));
140 if (access(path, X_OK | R_OK) == 0) return 1;
150 /* maximum number of arguments added above */
151 #define VALGRIND_MAX_ARGS 10
152 /* bitmask with all supported bits set */
153 #define VALGRIND_MODE_ALL 15
156 valgrind_append(char **dst, int valgrind_gdbserver, int valgrind_mode, int valgrind_tool, char *valgrind_path, const char *valgrind_log)
162 dst[i++] = valgrind_path;
163 switch (valgrind_tool)
165 case 1: dst[i++] = "--tool=massif"; break;
166 case 2: dst[i++] = "--tool=callgrind"; break;
170 if (valgrind_gdbserver) dst[i++] = "--db-attach=yes";
171 if (!valgrind_mode) return 0;
172 dst[i++] = valgrind_path;
173 dst[i++] = "--track-origins=yes";
174 dst[i++] = "--malloc-fill=13"; /* invalid pointer, make it crash */
177 static char logparam[PATH_MAX + sizeof("--log-file=")];
179 snprintf(logparam, sizeof(logparam), "--log-file=%s", valgrind_log);
182 if (valgrind_mode & 2) dst[i++] = "--trace-children=yes";
183 if (valgrind_mode & 4)
185 dst[i++] = "--leak-check=full";
186 dst[i++] = "--leak-resolution=high";
187 dst[i++] = "--track-fds=yes";
189 if (valgrind_mode & 8) dst[i++] = "--show-reachable=yes";
194 copy_args(char **dst, char **src, size_t count)
196 for (; count > 0; count--, dst++, src++) *dst = *src;
200 _env_path_prepend(const char *env, const char *path)
203 int len = 0, len2 = 0;
206 if (p) len = strlen(p);
208 if (p2) len2 = strlen(p2);
211 // path already there at the start. dont prepend. :)
212 if ((!strcmp(p, p2)) ||
214 (!strncmp(p, p2, len2)) &&
218 s = malloc(len + 1 + len2 + 1);
234 _env_path_append(const char *env, const char *path)
237 int len = 0, len2 = 0;
243 if (p2) len2 = strlen(p2);
246 // path already there at the end. dont append. :)
247 if ((!strcmp(p, p2)) ||
249 (!strcmp((p + len - len2), p2)) &&
250 (p[len - len2 - 1] == ':')))
253 s = malloc(len + 1 + len2 + 1);
269 main(int argc, char **argv)
271 int i, do_precache = 0, valgrind_mode = 0;
272 int valgrind_tool = 0;
273 int valgrind_gdbserver = 0;
274 #ifdef _F_SET_USER_GROUP_
279 #endif /* _F_SET_USER_GROUP_ */
280 char buf[16384], **args, *p;
281 char valgrind_path[PATH_MAX] = "";
282 const char *valgrind_log = NULL;
283 Eina_Bool really_know = EINA_FALSE;
286 prefix_determine(argv[0]);
288 env_set("E_START", argv[0]);
290 for (i = 1; i < argc; i++)
292 if (!strcmp(argv[i], "-no-precache")) do_precache = 0;
293 #ifdef _F_SET_USER_GROUP_
294 else if (!strncmp(argv[i], "-user", sizeof("-user") - 1))
296 if (sscanf(argv[i+1], "%d", &user_id) <= 0) user_id = 0;
299 else if (!strncmp(argv[i], "-group", sizeof("-group") - 1))
301 if (sscanf(argv[i+1], "%d", &group_id) <= 0) group_id = 0;
304 #endif /* _F_SET_USER_GROUP_ */
305 else if (!strcmp(argv[i], "-valgrind-gdb")) valgrind_gdbserver = 1;
306 else if (!strncmp(argv[i], "-valgrind", sizeof("-valgrind") - 1))
308 const char *val = argv[i] + sizeof("-valgrind") - 1;
310 if (*val == '\0') valgrind_mode = 1;
311 else if (*val == '-')
314 if (!strncmp(val, "log-file=", sizeof("log-file=") - 1))
316 valgrind_log = val + sizeof("log-file=") - 1;
317 if (*valgrind_log == '\0') valgrind_log = NULL;
320 else if (*val == '=')
323 if (!strcmp(val, "all")) valgrind_mode = VALGRIND_MODE_ALL;
324 else valgrind_mode = atoi(val);
327 printf("Unknown valgrind option: %s\n", argv[i]);
329 else if (!strcmp(argv[i], "-massif")) valgrind_tool = 1;
330 else if (!strcmp(argv[i], "-callgrind")) valgrind_tool = 2;
331 else if ((!strcmp(argv[i], "-h")) ||
332 (!strcmp(argv[i], "-help")) ||
333 (!strcmp(argv[i], "--help")))
338 #ifdef _F_SET_USER_GROUP_
339 "\t-user [user id in integer value]\n"
340 "\t-group [group id in integer value]\n"
341 #endif /* _F_SET_USER_GROUP_ */
343 "\t\tDisable pre-caching of files\n"
344 "\t-valgrind[=MODE]\n"
345 "\t\tRun enlightenment from inside valgrind, mode is OR of:\n"
346 "\t\t 1 = plain valgrind to catch crashes (default)\n"
347 "\t\t 2 = trace children (thumbnailer, efm slaves, ...)\n"
348 "\t\t 4 = check leak\n"
349 "\t\t 8 = show reachable after processes finish.\n"
350 "\t\t all = all of above\n"
352 "\t\tRun enlightenment from inside massif valgrind tool.\n"
354 "\t\tRun enlightenment from inside callgrind valgrind tool.\n"
355 "\t-valgrind-log-file=<FILENAME>\n"
356 "\t\tSave valgrind log to file, see valgrind's --log-file for details.\n"
359 "\tenlightenment %s\n"
360 "for more options.\n",
364 else if (!strcmp(argv[i], "-i-really-know-what-i-am-doing-and-accept-full-responsibility-for-it"))
365 really_know = EINA_TRUE;
368 #ifdef _F_SET_USER_GROUP_
371 user = getenv("USER");
375 res = initgroups(user, (gid_t)group_id);
379 printf("Failed to set groups !(errno=%d)\n", errno);
384 if ((group_id) && setgid((gid_t)group_id) && setegid((gid_t)group_id))
386 printf("Fail to set group to %d. errno=%d\n", group_id, errno);
388 if ((user_id) && setuid((uid_t)user_id) && seteuid((uid_t)user_id))
390 printf("Fail to set user to %d. errno=%d\n", user_id, errno);
392 #endif /* _F_SET_USER_GROUP_ */
396 _env_path_append("PATH", eina_prefix_bin_get(pfx));
397 _env_path_append("LD_LIBRARY_PATH", eina_prefix_lib_get(pfx));
401 _env_path_prepend("PATH", eina_prefix_bin_get(pfx));
402 _env_path_prepend("LD_LIBRARY_PATH", eina_prefix_lib_get(pfx));
405 if (valgrind_mode || valgrind_tool)
407 if (!find_valgrind(valgrind_path, sizeof(valgrind_path)))
409 printf("E - valgrind required but no binary found! Ignoring request.\n");
414 printf("E - PID=%i, do_precache=%i, valgrind=%d", getpid(), do_precache, valgrind_mode);
417 printf(" valgrind-command='%s'", valgrind_path);
418 if (valgrind_log) printf(" valgrind-log-file='%s'", valgrind_log);
426 /* sanity checks - if precache might fail - check here first */
427 lib = dlopen("libeina.so", RTLD_GLOBAL | RTLD_LAZY);
428 if (!lib) dlopen("libeina.so.1", RTLD_GLOBAL | RTLD_LAZY);
430 func = dlsym(lib, "eina_init");
431 if (!func) goto done;
433 lib = dlopen("libecore.so", RTLD_GLOBAL | RTLD_LAZY);
434 if (!lib) dlopen("libecore.so.1", RTLD_GLOBAL | RTLD_LAZY);
436 func = dlsym(lib, "ecore_init");
437 if (!func) goto done;
439 lib = dlopen("libecore_file.so", RTLD_GLOBAL | RTLD_LAZY);
440 if (!lib) dlopen("libecore_file.so.1", RTLD_GLOBAL | RTLD_LAZY);
442 func = dlsym(lib, "ecore_file_init");
443 if (!func) goto done;
445 lib = dlopen("libecore_x.so", RTLD_GLOBAL | RTLD_LAZY);
446 if (!lib) dlopen("libecore_x.so.1", RTLD_GLOBAL | RTLD_LAZY);
448 func = dlsym(lib, "ecore_x_init");
449 if (!func) goto done;
451 lib = dlopen("libevas.so", RTLD_GLOBAL | RTLD_LAZY);
452 if (!lib) dlopen("libevas.so.1", RTLD_GLOBAL | RTLD_LAZY);
454 func = dlsym(lib, "evas_init");
455 if (!func) goto done;
457 lib = dlopen("libedje.so", RTLD_GLOBAL | RTLD_LAZY);
458 if (!lib) dlopen("libedje.so.1", RTLD_GLOBAL | RTLD_LAZY);
460 func = dlsym(lib, "edje_init");
461 if (!func) goto done;
463 lib = dlopen("libeet.so", RTLD_GLOBAL | RTLD_LAZY);
464 if (!lib) dlopen("libeet.so.0", RTLD_GLOBAL | RTLD_LAZY);
466 func = dlsym(lib, "eet_init");
467 if (!func) goto done;
469 /* precache SHOULD work */
470 snprintf(buf, sizeof(buf), "%s/enlightenment/preload/e_precache.so",
471 eina_prefix_lib_get(pfx));
472 env_set("LD_PRELOAD", buf);
473 printf("E - PRECACHE GOING NOW...\n");
479 /* mtrack memory tracker support */
485 /* if you have ~/.e-mtrack, then the tracker will be enabled
486 * using the content of this file as the path to the mtrack.so
487 * shared object that is the mtrack preload */
488 snprintf(buf, sizeof(buf), "%s/.e-mtrack", p);
492 if (fgets(buf, sizeof(buf), f))
494 int len = strlen(buf);
495 if ((len > 1) && (buf[len - 1] == '\n'))
500 env_set("LD_PRELOAD", buf);
501 env_set("MTRACK", "track");
502 env_set("E_START_MTRACK", "track");
503 snprintf(buf, sizeof(buf), "%s/.e-mtrack.log", p);
504 env_set("MTRACK_TRACE_FILE", buf);
510 /* try dbus-launch */
511 snprintf(buf, sizeof(buf), "%s/enlightenment", eina_prefix_bin_get(pfx));
513 args = alloca((argc + 2 + VALGRIND_MAX_ARGS) * sizeof(char *));
514 if ((!getenv("DBUS_SESSION_BUS_ADDRESS")) &&
515 (!getenv("DBUS_LAUNCHD_SESSION_BUS_SOCKET")))
517 args[0] = "dbus-launch";
518 args[1] = "--exit-with-session";
520 i = 2 + valgrind_append(args + 2, valgrind_gdbserver, valgrind_mode, valgrind_tool, valgrind_path, valgrind_log);
522 copy_args(args + i, argv + 1, argc - 1);
523 args[i + argc - 1] = NULL;
524 execvp("dbus-launch", args);
527 /* dbus-launch failed - run e direct */
528 i = valgrind_append(args, valgrind_gdbserver, valgrind_mode, valgrind_tool, valgrind_path, valgrind_log);
530 copy_args(args + i, argv + 1, argc - 1);
531 args[i + argc - 1] = NULL;
532 execv(args[0], args);
534 printf("FAILED TO RUN:\n");
535 printf(" %s\n", buf);