package upload
[framework/uifw/elementary.git] / src / bin / quicklaunch.c
1 #include <Elementary.h>
2 #ifdef HAVE_CONFIG_H
3 # include "elementary_config.h"
4 #endif
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <sys/un.h>
12 #ifdef HAVE_ENVIRON
13 # define _GNU_SOURCE 1
14 #endif
15 #include <unistd.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <signal.h>
19 #include <sys/wait.h>
20
21 #ifdef HAVE_ENVIRON
22 extern char **environ;
23 #endif
24
25 static double restart_time = 0.0;
26
27 #define LENGTH_OF_SOCKADDR_UN(s) (strlen((s)->sun_path) + (size_t)(((struct sockaddr_un *)NULL)->sun_path))
28
29 static struct sigaction old_sigint;
30 static struct sigaction old_sigterm;
31 static struct sigaction old_sigquit;
32 static struct sigaction old_sigalrm;
33 static struct sigaction old_sigusr1;
34 static struct sigaction old_sigusr2;
35 static struct sigaction old_sighup;
36 static struct sigaction old_sigchld;
37 static struct sigaction old_sigsegv;
38 static struct sigaction old_sigill;
39 static struct sigaction old_sigfpe;
40 static struct sigaction old_sigbus;
41 static struct sigaction old_sigabrt;
42 static int _log_dom = -1;
43
44 #define CRITICAL(...) EINA_LOG_DOM_CRIT(_log_dom, __VA_ARGS__)
45 #define ERR(...) EINA_LOG_DOM_ERR(_log_dom, __VA_ARGS__)
46 #define WRN(...) EINA_LOG_DOM_WARN(_log_dom, __VA_ARGS__)
47 #define INF(...) EINA_LOG_DOM_INFO(_log_dom, __VA_ARGS__)
48 #define DBG(...) EINA_LOG_DOM_DBG(_log_dom, __VA_ARGS__)
49
50 static void
51 post_fork(void *data __UNUSED__)
52 {
53    sigaction(SIGINT, &old_sigint, NULL);
54    sigaction(SIGTERM, &old_sigterm, NULL);
55    sigaction(SIGQUIT, &old_sigquit, NULL);
56    sigaction(SIGALRM, &old_sigalrm, NULL);
57    sigaction(SIGUSR1, &old_sigusr1, NULL);
58    sigaction(SIGUSR2, &old_sigusr2, NULL);
59    sigaction(SIGHUP, &old_sighup, NULL);
60    sigaction(SIGCHLD, &old_sigchld, NULL);
61    sigaction(SIGSEGV, &old_sigsegv, NULL);
62    sigaction(SIGILL, &old_sigill, NULL);
63    sigaction(SIGFPE, &old_sigfpe, NULL);
64    sigaction(SIGBUS, &old_sigbus, NULL);
65    sigaction(SIGABRT, &old_sigabrt, NULL);
66    if ((_log_dom > -1) && (_log_dom != EINA_LOG_DOMAIN_GLOBAL))
67      {
68         eina_log_domain_unregister(_log_dom);
69         _log_dom = -1;
70      }
71 }
72
73 static void
74 child_handler(int x __UNUSED__, siginfo_t *info __UNUSED__, void *data __UNUSED__)
75 {
76    int status;
77    while (waitpid(-1, &status, WNOHANG) > 0);
78 }
79
80 static void
81 crash_handler(int x __UNUSED__, siginfo_t *info __UNUSED__, void *data __UNUSED__)
82 {
83    double t;
84
85    ERR("crash detected. restarting.");
86    t = ecore_time_get();
87    if ((t - restart_time) <= 2.0)
88      {
89         CRITICAL("crash too fast - less than 2 seconds. abort restart");
90         exit(-1);
91      }
92    ecore_app_restart();
93 }
94
95 static void
96 handle_run(int fd, unsigned long bytes)
97 {
98    unsigned char *buf = NULL;
99    int i;
100    char **argv = NULL;
101    char **envir = NULL;
102    char *cwd;
103    int argc, envnum;
104    unsigned long off;
105
106    buf = alloca(bytes);
107    if (read(fd, buf, bytes) != (int)bytes)
108      {
109         CRITICAL("cannot read %i bytes of args and environment data", (int)bytes);
110         close(fd);
111         return;
112      }
113    close(fd);
114    
115    argc = ((unsigned long *)(buf))[0];
116    envnum = ((unsigned long *)(buf))[1];
117
118    if (argc <= 0)
119      {
120         CRITICAL("no executable specified");
121         return;
122      }
123    
124    argv = alloca(argc * sizeof(char *));
125    if (envnum > 0) envir = alloca(envnum * sizeof(char *));
126    off = ((unsigned long *)(buf))[2 + argc + envnum] - sizeof(unsigned long);
127    cwd = (char *)(buf + off);
128
129    for (i = 0; i < argc; i++)
130      {
131         off = ((unsigned long *)(buf))[2 + i] - sizeof(unsigned long);
132         argv[i] = (char *)(buf + off);
133      }
134
135 #ifdef HAVE_ENVIRON
136    if (envir)
137      {
138 #ifdef HAVE_CLEARENV
139         clearenv();
140 #else
141         environ = NULL;
142 #endif
143         for (i = 0; i < envnum; i++)
144           {
145              off = ((unsigned long *)(buf))[2 + argc + i] - sizeof(unsigned long);
146              envir[i] = (char *)(buf + off);
147              putenv(envir[i]);
148           }
149      }
150 #endif
151    elm_quicklaunch_prepare(argc, argv);
152    elm_quicklaunch_fork(argc, argv, cwd, post_fork, NULL);
153    elm_quicklaunch_cleanup();
154 }
155
156 int
157 main(int argc, char **argv)
158 {
159    int sock, socket_unix_len;
160    struct stat st;
161    struct sockaddr_un socket_unix;
162    struct linger lin;
163    char buf[PATH_MAX];
164    struct sigaction action;
165    const char *disp;
166
167    if (!eina_init())
168      {
169         fprintf(stderr, "ERROR: failed to init eina.");
170         exit(-1);
171      }
172    _log_dom = eina_log_domain_register
173      ("elementary_quicklaunch", EINA_COLOR_CYAN);
174    if (_log_dom < 0)
175      {
176         EINA_LOG_ERR("could not register elementary_quicklaunch log domain.");
177         _log_dom = EINA_LOG_DOMAIN_GLOBAL;
178      }
179
180    if (!(disp = getenv("DISPLAY"))) disp = "unknown";
181    snprintf(buf, sizeof(buf), "/tmp/elm-ql-%i", getuid());
182    if (stat(buf, &st) < 0) mkdir(buf, S_IRUSR | S_IWUSR | S_IXUSR);
183    snprintf(buf, sizeof(buf), "/tmp/elm-ql-%i/%s", getuid(), disp);
184    unlink(buf);
185    sock = socket(AF_UNIX, SOCK_STREAM, 0);
186    if (sock < 0)
187      {
188         CRITICAL("cannot create socket for socket for '%s': %s",
189                  buf, strerror(errno));
190         exit(-1);
191      }
192    if (fcntl(sock, F_SETFD, FD_CLOEXEC) < 0)
193      {
194         CRITICAL("cannot set close on exec socket for '%s' (fd=%d): %s",
195                  buf, sock, strerror(errno));
196         exit(-1);
197      }
198    lin.l_onoff = 1;
199    lin.l_linger = 0;
200    if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &lin, sizeof(struct linger)) < 0)
201      {
202         CRITICAL("cannot set linger for socket for '%s' (fd=%d): %s",
203                  buf, sock, strerror(errno));
204         exit(-1);
205      }
206    socket_unix.sun_family = AF_UNIX;
207    strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path));
208    socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix);
209    if (bind(sock, (struct sockaddr *)&socket_unix, socket_unix_len) < 0)
210      {
211         CRITICAL("cannot bind socket for '%s' (fd=%d): %s",
212                  buf, sock, strerror(errno));
213         exit(-1);
214      }
215    if (listen(sock, 4096) < 0)
216      {
217         CRITICAL("listen(sock=%d, 4096): %s", sock, strerror(errno));
218         exit(-1);
219      }
220    elm_quicklaunch_mode_set(EINA_TRUE);
221    elm_quicklaunch_init(argc, argv);
222    restart_time = ecore_time_get();
223
224    memset(&action, 0, sizeof(struct sigaction));
225    action.sa_handler = SIG_DFL;
226    action.sa_sigaction = NULL;
227    action.sa_flags = SA_RESTART | SA_SIGINFO;
228    sigemptyset(&action.sa_mask);
229    sigaction(SIGINT, &action, &old_sigint);
230
231    action.sa_handler = SIG_DFL;
232    action.sa_sigaction = NULL;
233    action.sa_flags = SA_RESTART | SA_SIGINFO;
234    sigemptyset(&action.sa_mask);
235    sigaction(SIGTERM, &action, &old_sigterm);
236
237    action.sa_handler = SIG_DFL;
238    action.sa_sigaction = NULL;
239    action.sa_flags = SA_RESTART | SA_SIGINFO;
240    sigemptyset(&action.sa_mask);
241    sigaction(SIGQUIT, &action, &old_sigquit);
242
243    action.sa_handler = SIG_DFL;
244    action.sa_sigaction = NULL;
245    action.sa_flags = SA_RESTART | SA_SIGINFO;
246    sigemptyset(&action.sa_mask);
247    sigaction(SIGALRM, &action, &old_sigalrm);
248
249    action.sa_handler = SIG_DFL;
250    action.sa_sigaction = NULL;
251    action.sa_flags = SA_RESTART | SA_SIGINFO;
252    sigemptyset(&action.sa_mask);
253    sigaction(SIGUSR1, &action, &old_sigusr1);
254
255    action.sa_handler = SIG_DFL;
256    action.sa_sigaction = NULL;
257    action.sa_flags = SA_RESTART | SA_SIGINFO;
258    sigemptyset(&action.sa_mask);
259    sigaction(SIGUSR2, &action, &old_sigusr2);
260
261    action.sa_handler = SIG_DFL;
262    action.sa_sigaction = NULL;
263    action.sa_flags = SA_RESTART | SA_SIGINFO;
264    sigemptyset(&action.sa_mask);
265    sigaction(SIGHUP, &action, &old_sighup);
266
267    action.sa_handler = NULL;
268    action.sa_sigaction = child_handler;
269    action.sa_flags = SA_RESTART | SA_SIGINFO;
270    sigemptyset(&action.sa_mask);
271    sigaction(SIGCHLD, &action, &old_sigchld);
272
273    action.sa_handler = NULL;
274    action.sa_sigaction = crash_handler;
275    action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
276    sigemptyset(&action.sa_mask);
277    sigaction(SIGSEGV, &action, &old_sigsegv);
278
279    action.sa_handler = NULL;
280    action.sa_sigaction = crash_handler;
281    action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
282    sigemptyset(&action.sa_mask);
283    sigaction(SIGILL, &action, &old_sigill);
284
285    action.sa_handler = NULL;
286    action.sa_sigaction = crash_handler;
287    action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
288    sigemptyset(&action.sa_mask);
289    sigaction(SIGFPE, &action, &old_sigfpe);
290
291    action.sa_handler = NULL;
292    action.sa_sigaction = crash_handler;
293    action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
294    sigemptyset(&action.sa_mask);
295    sigaction(SIGBUS, &action, &old_sigbus);
296
297    action.sa_handler = NULL;
298    action.sa_sigaction = crash_handler;
299    action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
300    sigemptyset(&action.sa_mask);
301    sigaction(SIGABRT, &action, &old_sigabrt);
302
303    for (;;)
304      {
305         int fd;
306         struct sockaddr_un client;
307         socklen_t len;
308
309         len = sizeof(struct sockaddr_un);
310         fd = accept(sock, (struct sockaddr *)&client, &len);
311         elm_quicklaunch_sub_init(argc, argv);
312 // don't seed since we are doing this AFTER launch request        
313 //      elm_quicklaunch_seed();
314         if (fd >= 0)
315           {
316              unsigned long bytes;
317              int num;
318
319              num = read(fd, &bytes, sizeof(unsigned long));
320              if (num == sizeof(unsigned long))
321                handle_run(fd, bytes);
322           }
323         while (elm_quicklaunch_sub_shutdown() > 0);
324      }
325    elm_quicklaunch_shutdown();
326
327    if ((_log_dom > -1) && (_log_dom != EINA_LOG_DOMAIN_GLOBAL))
328      {
329         eina_log_domain_unregister(_log_dom);
330         _log_dom = -1;
331      }
332    eina_shutdown();
333
334    return 0;
335 }