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