Tizen 2.1 release
[platform/core/uifw/e17.git] / src / bin / e_start_main.c
1 #include "config.h"
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <dlfcn.h>
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include <sys/stat.h>
10 #include <sys/utsname.h>
11 #include <limits.h>
12 #include <fcntl.h>
13 #ifdef HAVE_ALLOCA_H
14 # include <alloca.h>
15 #endif
16 #include <Eina.h>
17
18 static void env_set(const char *var, const char *val);
19 EAPI int prefix_determine(char *argv0);
20
21 static void
22 env_set(const char *var, const char *val)
23 {
24    if (val)
25      {
26 #ifdef HAVE_SETENV
27         setenv(var, val, 1);
28 #else
29         char *buf;
30         size_t size = strlen(var) + 1 + strlen(val) + 1;
31
32         buf = alloca(size);
33         snprintf(buf, size, "%s=%s", var, val);
34         if (getenv(var)) putenv(buf);
35         else putenv(strdup(buf));
36 #endif
37      }
38    else
39      {
40 #ifdef HAVE_UNSETENV
41         unsetenv(var);
42 #else
43         if (getenv(var)) putenv(var);
44 #endif
45      }
46 }
47
48 /* local subsystem globals */
49 static Eina_Prefix *pfx = NULL;
50
51 /* externally accessible functions */
52 EAPI int
53 prefix_determine(char *argv0)
54 {
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);
59    if (!pfx) return 0;
60    return 1;
61 }
62
63 static void
64 precache(void)
65 {
66    FILE *f;
67    char *home;
68    char buf[4096], tbuf[256 * 1024];
69    struct stat st;
70    int l, fd, children = 0, cret;
71
72    home = getenv("HOME");
73    if (home) snprintf(buf, sizeof(buf), "%s/.e-precache", home);
74    else snprintf(buf, sizeof(buf), "/tmp/.e-precache");
75    f = fopen(buf, "r");
76    if (!f) return;
77    unlink(buf);
78    if (fork()) return;
79 //   while (fgets(buf, sizeof(buf), f));
80 //   rewind(f);
81    while (fgets(buf, sizeof(buf), f))
82      {
83         l = strlen(buf);
84         if (l > 0) buf[l - 1] = 0;
85         if (!fork())
86           {
87              if (buf[0] == 's') stat(buf + 2, &st);
88              else if (buf[0] == 'o')
89                {
90                   fd = open(buf + 2, O_RDONLY);
91                   if (fd >= 0)
92                     {
93                        while (read(fd, tbuf, 256 * 1024) > 0);
94                        close(fd);
95                     }
96                }
97              else if (buf[0] == 'd')
98                {
99                   fd = open(buf + 2, O_RDONLY);
100                   if (fd >= 0)
101                     {
102                        while (read(fd, tbuf, 256 * 1024) > 0);
103                        close(fd);
104                     }
105                }
106              exit(0);
107           }
108         children++;
109         if (children > 400)
110           {
111              wait(&cret);
112              children--;
113           }
114      }
115    fclose(f);
116    while (children > 0)
117      {
118         wait(&cret);
119         children--;
120      }
121    exit(0);
122 }
123
124 static int
125 find_valgrind(char *path, size_t path_len)
126 {
127    const char *env = getenv("PATH");
128
129    while (env)
130      {
131         const char *p = strchr(env, ':');
132         ssize_t p_len;
133
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;
141 next:
142         if (p) env = p + 1;
143         else break;
144      }
145    path[0] = '\0';
146    return 0;
147 }
148
149
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
154
155 static int
156 valgrind_append(char **dst, int valgrind_gdbserver, int valgrind_mode, int valgrind_tool, char *valgrind_path, const char *valgrind_log)
157 {
158    int i = 0;
159
160    if (valgrind_tool)
161      {
162         dst[i++] = valgrind_path;
163         switch (valgrind_tool)
164           {
165            case 1: dst[i++] = "--tool=massif"; break;
166            case 2: dst[i++] = "--tool=callgrind"; break;
167           }
168         return i;
169      }
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 */
175    if (valgrind_log)
176      {
177         static char logparam[PATH_MAX + sizeof("--log-file=")];
178
179         snprintf(logparam, sizeof(logparam), "--log-file=%s", valgrind_log);
180         dst[i++] = logparam;
181      }
182    if (valgrind_mode & 2) dst[i++] = "--trace-children=yes";
183    if (valgrind_mode & 4)
184      {
185         dst[i++] = "--leak-check=full";
186         dst[i++] = "--leak-resolution=high";
187         dst[i++] = "--track-fds=yes";
188      }
189    if (valgrind_mode & 8) dst[i++] = "--show-reachable=yes";
190    return i;
191 }
192
193 static void
194 copy_args(char **dst, char **src, size_t count)
195 {
196    for (; count > 0; count--, dst++, src++) *dst = *src;
197 }
198
199 static void
200 _env_path_prepend(const char *env, const char *path)
201 {
202    char *p, *p2, *s;
203    int len = 0, len2 = 0;
204    
205    p = getenv(env);
206    if (p) len = strlen(p);
207    p2 = (char *)path;
208    if (p2) len2 = strlen(p2);
209    if (p && p2)
210      {
211         // path already there at the start. dont prepend. :)
212         if ((!strcmp(p, p2)) ||
213             ((len > len2) &&
214                 (!strncmp(p, p2, len2)) &&
215                 (p[len2] == ':')))
216           return;
217      }
218    s = malloc(len + 1 + len2 + 1);
219    if (s)
220      {
221         s[0] = 0;
222         if (p2)
223           {
224              strcat(s, p2);
225              strcat(s, ":");
226           }
227         if (p) strcat(s, p);
228         env_set(env, s);
229         free(s);
230      }
231 }
232
233 static void
234 _env_path_append(const char *env, const char *path)
235 {
236    char *p, *p2, *s;
237    int len = 0, len2 = 0;
238    
239    p = getenv(env);
240    if (!p) return;
241    len = strlen(p);
242    p2 = (char *)path;
243    if (p2) len2 = strlen(p2);
244    if (p && p2)
245      {
246         // path already there at the end. dont append. :)
247         if ((!strcmp(p, p2)) ||
248             ((len > len2) &&
249                 (!strcmp((p + len - len2), p2)) &&
250                 (p[len - len2 - 1] == ':')))
251           return;
252      }
253    s = malloc(len + 1 + len2 + 1);
254    if (s)
255      {
256         s[0] = 0;
257         if (p) strcat(s, p);
258         if (p2)
259           {
260              strcat(s, ":");
261              strcat(s, p2);
262           }
263         env_set(env, s);
264         free(s);
265      }
266 }
267
268 int
269 main(int argc, char **argv)
270 {
271    int i, do_precache = 0, valgrind_mode = 0;
272    int valgrind_tool = 0;
273    int valgrind_gdbserver = 0;
274    char buf[16384], **args, *p;
275    char valgrind_path[PATH_MAX] = "";
276    const char *valgrind_log = NULL;
277    Eina_Bool really_know = EINA_FALSE;
278    
279    eina_init();
280    prefix_determine(argv[0]);
281
282    env_set("E_START", argv[0]);
283
284    for (i = 1; i < argc; i++)
285      {
286         if (!strcmp(argv[i], "-no-precache")) do_precache = 0;
287         else if (!strcmp(argv[i], "-valgrind-gdb")) valgrind_gdbserver = 1;
288         else if (!strncmp(argv[i], "-valgrind", sizeof("-valgrind") - 1))
289           {
290              const char *val = argv[i] + sizeof("-valgrind") - 1;
291
292              if (*val == '\0') valgrind_mode = 1;
293              else if (*val == '-')
294                {
295                   val++;
296                   if (!strncmp(val, "log-file=", sizeof("log-file=") - 1))
297                     {
298                        valgrind_log = val + sizeof("log-file=") - 1;
299                        if (*valgrind_log == '\0') valgrind_log = NULL;
300                     }
301                }
302              else if (*val == '=')
303                {
304                   val++;
305                   if (!strcmp(val, "all")) valgrind_mode = VALGRIND_MODE_ALL;
306                   else valgrind_mode = atoi(val);
307                }
308              else
309                 printf("Unknown valgrind option: %s\n", argv[i]);
310           }
311         else if (!strcmp(argv[i], "-massif")) valgrind_tool = 1;
312         else if (!strcmp(argv[i], "-callgrind")) valgrind_tool = 2;
313         else if ((!strcmp(argv[i], "-h")) ||
314                  (!strcmp(argv[i], "-help")) ||
315                  (!strcmp(argv[i], "--help")))
316           {
317              printf
318                 (
319                     "Options:\n"
320                     "\t-no-precache\n"
321                     "\t\tDisable pre-caching of files\n"
322                     "\t-valgrind[=MODE]\n"
323                     "\t\tRun enlightenment from inside valgrind, mode is OR of:\n"
324                     "\t\t   1 = plain valgrind to catch crashes (default)\n"
325                     "\t\t   2 = trace children (thumbnailer, efm slaves, ...)\n"
326                     "\t\t   4 = check leak\n"
327                     "\t\t   8 = show reachable after processes finish.\n"
328                     "\t\t all = all of above\n"
329                     "\t-massif\n"
330                     "\t\tRun enlightenment from inside massif valgrind tool.\n"
331                     "\t-callgrind\n"
332                     "\t\tRun enlightenment from inside callgrind valgrind tool.\n"
333                     "\t-valgrind-log-file=<FILENAME>\n"
334                     "\t\tSave valgrind log to file, see valgrind's --log-file for details.\n"
335                     "\n"
336                     "Please run:\n"
337                     "\tenlightenment %s\n"
338                     "for more options.\n",
339                     argv[i]);
340              exit(0);
341           }
342         else if (!strcmp(argv[i], "-i-really-know-what-i-am-doing-and-accept-full-responsibility-for-it"))
343           really_know = EINA_TRUE;
344      }
345
346    if (really_know)
347      {
348         _env_path_append("PATH", eina_prefix_bin_get(pfx));
349         _env_path_append("LD_LIBRARY_PATH", eina_prefix_lib_get(pfx));
350      }
351    else
352      {
353         _env_path_prepend("PATH", eina_prefix_bin_get(pfx));
354         _env_path_prepend("LD_LIBRARY_PATH", eina_prefix_lib_get(pfx));
355      }
356    
357    if (valgrind_mode || valgrind_tool)
358      {
359         if (!find_valgrind(valgrind_path, sizeof(valgrind_path)))
360           {
361              printf("E - valgrind required but no binary found! Ignoring request.\n");
362              valgrind_mode = 0;
363           }
364      }
365
366    printf("E - PID=%i, do_precache=%i, valgrind=%d", getpid(), do_precache, valgrind_mode);
367    if (valgrind_mode)
368      {
369         printf(" valgrind-command='%s'", valgrind_path);
370         if (valgrind_log) printf(" valgrind-log-file='%s'", valgrind_log);
371      }
372    putchar('\n');
373
374    if (do_precache)
375      {
376         void *lib, *func;
377
378         /* sanity checks - if precache might fail - check here first */
379         lib = dlopen("libeina.so", RTLD_GLOBAL | RTLD_LAZY);
380         if (!lib) dlopen("libeina.so.1", RTLD_GLOBAL | RTLD_LAZY);
381         if (!lib) goto done;
382         func = dlsym(lib, "eina_init");
383         if (!func) goto done;
384
385         lib = dlopen("libecore.so", RTLD_GLOBAL | RTLD_LAZY);
386         if (!lib) dlopen("libecore.so.1", RTLD_GLOBAL | RTLD_LAZY);
387         if (!lib) goto done;
388         func = dlsym(lib, "ecore_init");
389         if (!func) goto done;
390
391         lib = dlopen("libecore_file.so", RTLD_GLOBAL | RTLD_LAZY);
392         if (!lib) dlopen("libecore_file.so.1", RTLD_GLOBAL | RTLD_LAZY);
393         if (!lib) goto done;
394         func = dlsym(lib, "ecore_file_init");
395         if (!func) goto done;
396
397         lib = dlopen("libecore_x.so", RTLD_GLOBAL | RTLD_LAZY);
398         if (!lib) dlopen("libecore_x.so.1", RTLD_GLOBAL | RTLD_LAZY);
399         if (!lib) goto done;
400         func = dlsym(lib, "ecore_x_init");
401         if (!func) goto done;
402
403         lib = dlopen("libevas.so", RTLD_GLOBAL | RTLD_LAZY);
404         if (!lib) dlopen("libevas.so.1", RTLD_GLOBAL | RTLD_LAZY);
405         if (!lib) goto done;
406         func = dlsym(lib, "evas_init");
407         if (!func) goto done;
408
409         lib = dlopen("libedje.so", RTLD_GLOBAL | RTLD_LAZY);
410         if (!lib) dlopen("libedje.so.1", RTLD_GLOBAL | RTLD_LAZY);
411         if (!lib) goto done;
412         func = dlsym(lib, "edje_init");
413         if (!func) goto done;
414
415         lib = dlopen("libeet.so", RTLD_GLOBAL | RTLD_LAZY);
416         if (!lib) dlopen("libeet.so.0", RTLD_GLOBAL | RTLD_LAZY);
417         if (!lib) goto done;
418         func = dlsym(lib, "eet_init");
419         if (!func) goto done;
420
421         /* precache SHOULD work */
422         snprintf(buf, sizeof(buf), "%s/enlightenment/preload/e_precache.so",
423                  eina_prefix_lib_get(pfx));
424         env_set("LD_PRELOAD", buf);
425         printf("E - PRECACHE GOING NOW...\n");
426         fflush(stdout);
427         precache();
428      }
429 done:
430
431    /* mtrack memory tracker support */
432    p = getenv("HOME");
433    if (p)
434      {
435         FILE *f;
436
437         /* if you have ~/.e-mtrack, then the tracker will be enabled
438          * using the content of this file as the path to the mtrack.so
439          * shared object that is the mtrack preload */
440         snprintf(buf, sizeof(buf), "%s/.e-mtrack", p);
441         f = fopen(buf, "r");
442         if (f)
443           {
444              if (fgets(buf, sizeof(buf), f))
445                {
446                   int len = strlen(buf);
447                   if ((len > 1) && (buf[len - 1] == '\n'))
448                     {
449                        buf[len - 1] = 0;
450                        len--;
451                     }
452                   env_set("LD_PRELOAD", buf);
453                   env_set("MTRACK", "track");
454                   env_set("E_START_MTRACK", "track");
455                   snprintf(buf, sizeof(buf), "%s/.e-mtrack.log", p);
456                   env_set("MTRACK_TRACE_FILE", buf);
457                }
458              fclose(f);
459           }
460      }
461
462    /* try dbus-launch */
463    snprintf(buf, sizeof(buf), "%s/enlightenment", eina_prefix_bin_get(pfx));
464
465    args = alloca((argc + 2 + VALGRIND_MAX_ARGS) * sizeof(char *));
466    if ((!getenv("DBUS_SESSION_BUS_ADDRESS")) &&
467        (!getenv("DBUS_LAUNCHD_SESSION_BUS_SOCKET")))
468      {
469         args[0] = "dbus-launch";
470         args[1] = "--exit-with-session";
471
472         i = 2 + valgrind_append(args + 2, valgrind_gdbserver, valgrind_mode, valgrind_tool, valgrind_path, valgrind_log);
473         args[i++] = buf;
474         copy_args(args + i, argv + 1, argc - 1);
475         args[i + argc - 1] = NULL;
476         execvp("dbus-launch", args);
477      }
478
479    /* dbus-launch failed - run e direct */
480    i = valgrind_append(args, valgrind_gdbserver, valgrind_mode, valgrind_tool, valgrind_path, valgrind_log);
481    args[i++] = buf;
482    copy_args(args + i, argv + 1, argc - 1);
483    args[i + argc - 1] = NULL;
484    execv(args[0], args);
485
486    printf("FAILED TO RUN:\n");
487    printf("  %s\n", buf);
488    perror("execv");
489    return -1;
490 }