the repository of RSA merge with private repository.
[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 #ifdef _F_SET_USER_GROUP_
275    int res;
276    char *user;
277    int user_id;
278    int group_id;
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;
284    
285    eina_init();
286    prefix_determine(argv[0]);
287
288    env_set("E_START", argv[0]);
289
290    for (i = 1; i < argc; i++)
291      {
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))
295           {
296              if (sscanf(argv[i+1], "%d", &user_id) <= 0) user_id = 0;
297              else i++;
298           }
299         else if (!strncmp(argv[i], "-group", sizeof("-group") - 1))
300           {
301              if (sscanf(argv[i+1], "%d", &group_id) <= 0) group_id = 0;
302              else i++;
303           }
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))
307           {
308              const char *val = argv[i] + sizeof("-valgrind") - 1;
309
310              if (*val == '\0') valgrind_mode = 1;
311              else if (*val == '-')
312                {
313                   val++;
314                   if (!strncmp(val, "log-file=", sizeof("log-file=") - 1))
315                     {
316                        valgrind_log = val + sizeof("log-file=") - 1;
317                        if (*valgrind_log == '\0') valgrind_log = NULL;
318                     }
319                }
320              else if (*val == '=')
321                {
322                   val++;
323                   if (!strcmp(val, "all")) valgrind_mode = VALGRIND_MODE_ALL;
324                   else valgrind_mode = atoi(val);
325                }
326              else
327                 printf("Unknown valgrind option: %s\n", argv[i]);
328           }
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")))
334           {
335              printf
336                 (
337                     "Options:\n"
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_ */
342                     "\t-no-precache\n"
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"
351                     "\t-massif\n"
352                     "\t\tRun enlightenment from inside massif valgrind tool.\n"
353                     "\t-callgrind\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"
357                     "\n"
358                     "Please run:\n"
359                     "\tenlightenment %s\n"
360                     "for more options.\n",
361                     argv[i]);
362              exit(0);
363           }
364         else if (!strcmp(argv[i], "-i-really-know-what-i-am-doing-and-accept-full-responsibility-for-it"))
365           really_know = EINA_TRUE;
366      }
367
368 #ifdef _F_SET_USER_GROUP_
369    if (group_id)
370      {
371         user = getenv("USER");
372
373         if (user)
374           {
375              res = initgroups(user, (gid_t)group_id);
376
377              if (res)
378                {
379                   printf("Failed to set groups !(errno=%d)\n", errno);
380                }
381           }
382      }
383
384    if ((group_id) && setgid((gid_t)group_id) && setegid((gid_t)group_id))
385      {
386         printf("Fail to set group to %d. errno=%d\n", group_id, errno);
387      }
388    if ((user_id) && setuid((uid_t)user_id) && seteuid((uid_t)user_id))
389      {
390         printf("Fail to set user to %d. errno=%d\n", user_id, errno);
391      }
392 #endif /* _F_SET_USER_GROUP_ */
393
394    if (really_know)
395      {
396         _env_path_append("PATH", eina_prefix_bin_get(pfx));
397         _env_path_append("LD_LIBRARY_PATH", eina_prefix_lib_get(pfx));
398      }
399    else
400      {
401         _env_path_prepend("PATH", eina_prefix_bin_get(pfx));
402         _env_path_prepend("LD_LIBRARY_PATH", eina_prefix_lib_get(pfx));
403      }
404    
405    if (valgrind_mode || valgrind_tool)
406      {
407         if (!find_valgrind(valgrind_path, sizeof(valgrind_path)))
408           {
409              printf("E - valgrind required but no binary found! Ignoring request.\n");
410              valgrind_mode = 0;
411           }
412      }
413
414    printf("E - PID=%i, do_precache=%i, valgrind=%d", getpid(), do_precache, valgrind_mode);
415    if (valgrind_mode)
416      {
417         printf(" valgrind-command='%s'", valgrind_path);
418         if (valgrind_log) printf(" valgrind-log-file='%s'", valgrind_log);
419      }
420    putchar('\n');
421
422    if (do_precache)
423      {
424         void *lib, *func;
425
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);
429         if (!lib) goto done;
430         func = dlsym(lib, "eina_init");
431         if (!func) goto done;
432
433         lib = dlopen("libecore.so", RTLD_GLOBAL | RTLD_LAZY);
434         if (!lib) dlopen("libecore.so.1", RTLD_GLOBAL | RTLD_LAZY);
435         if (!lib) goto done;
436         func = dlsym(lib, "ecore_init");
437         if (!func) goto done;
438
439         lib = dlopen("libecore_file.so", RTLD_GLOBAL | RTLD_LAZY);
440         if (!lib) dlopen("libecore_file.so.1", RTLD_GLOBAL | RTLD_LAZY);
441         if (!lib) goto done;
442         func = dlsym(lib, "ecore_file_init");
443         if (!func) goto done;
444
445         lib = dlopen("libecore_x.so", RTLD_GLOBAL | RTLD_LAZY);
446         if (!lib) dlopen("libecore_x.so.1", RTLD_GLOBAL | RTLD_LAZY);
447         if (!lib) goto done;
448         func = dlsym(lib, "ecore_x_init");
449         if (!func) goto done;
450
451         lib = dlopen("libevas.so", RTLD_GLOBAL | RTLD_LAZY);
452         if (!lib) dlopen("libevas.so.1", RTLD_GLOBAL | RTLD_LAZY);
453         if (!lib) goto done;
454         func = dlsym(lib, "evas_init");
455         if (!func) goto done;
456
457         lib = dlopen("libedje.so", RTLD_GLOBAL | RTLD_LAZY);
458         if (!lib) dlopen("libedje.so.1", RTLD_GLOBAL | RTLD_LAZY);
459         if (!lib) goto done;
460         func = dlsym(lib, "edje_init");
461         if (!func) goto done;
462
463         lib = dlopen("libeet.so", RTLD_GLOBAL | RTLD_LAZY);
464         if (!lib) dlopen("libeet.so.0", RTLD_GLOBAL | RTLD_LAZY);
465         if (!lib) goto done;
466         func = dlsym(lib, "eet_init");
467         if (!func) goto done;
468
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");
474         fflush(stdout);
475         precache();
476      }
477 done:
478
479    /* mtrack memory tracker support */
480    p = getenv("HOME");
481    if (p)
482      {
483         FILE *f;
484
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);
489         f = fopen(buf, "r");
490         if (f)
491           {
492              if (fgets(buf, sizeof(buf), f))
493                {
494                   int len = strlen(buf);
495                   if ((len > 1) && (buf[len - 1] == '\n'))
496                     {
497                        buf[len - 1] = 0;
498                        len--;
499                     }
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);
505                }
506              fclose(f);
507           }
508      }
509
510    /* try dbus-launch */
511    snprintf(buf, sizeof(buf), "%s/enlightenment", eina_prefix_bin_get(pfx));
512
513    args = alloca((argc + 2 + VALGRIND_MAX_ARGS) * sizeof(char *));
514    if ((!getenv("DBUS_SESSION_BUS_ADDRESS")) &&
515        (!getenv("DBUS_LAUNCHD_SESSION_BUS_SOCKET")))
516      {
517         args[0] = "dbus-launch";
518         args[1] = "--exit-with-session";
519
520         i = 2 + valgrind_append(args + 2, valgrind_gdbserver, valgrind_mode, valgrind_tool, valgrind_path, valgrind_log);
521         args[i++] = buf;
522         copy_args(args + i, argv + 1, argc - 1);
523         args[i + argc - 1] = NULL;
524         execvp("dbus-launch", args);
525      }
526
527    /* dbus-launch failed - run e direct */
528    i = valgrind_append(args, valgrind_gdbserver, valgrind_mode, valgrind_tool, valgrind_path, valgrind_log);
529    args[i++] = buf;
530    copy_args(args + i, argv + 1, argc - 1);
531    args[i + argc - 1] = NULL;
532    execv(args[0], args);
533
534    printf("FAILED TO RUN:\n");
535    printf("  %s\n", buf);
536    perror("execv");
537    return -1;
538 }