1 #include <Elementary.h>
3 # include "elementary_config.h"
10 #include <sys/socket.h>
13 # define _GNU_SOURCE 1
22 extern char **environ;
25 static double restart_time = 0.0;
27 #define LENGTH_OF_SOCKADDR_UN(s) (strlen((s)->sun_path) + (size_t)(((struct sockaddr_un *)NULL)->sun_path))
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;
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__)
51 post_fork(void *data __UNUSED__)
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))
68 eina_log_domain_unregister(_log_dom);
74 child_handler(int x __UNUSED__, siginfo_t *info __UNUSED__, void *data __UNUSED__)
77 while (waitpid(-1, &status, WNOHANG) > 0);
81 crash_handler(int x __UNUSED__, siginfo_t *info __UNUSED__, void *data __UNUSED__)
85 ERR("crash detected. restarting.");
87 if ((t - restart_time) <= 2.0)
89 CRITICAL("crash too fast - less than 2 seconds. abort restart");
96 handle_run(int fd, unsigned long bytes)
98 unsigned char *buf = NULL;
107 if (read(fd, buf, bytes) != (int)bytes)
109 CRITICAL("cannot read %i bytes of args and environment data", (int)bytes);
115 argc = ((unsigned long *)(buf))[0];
116 envnum = ((unsigned long *)(buf))[1];
120 CRITICAL("no executable specified");
125 argv = alloca(argc * sizeof(char *));
126 if (envnum > 0) envir = alloca(envnum * sizeof(char *));
127 off = ((unsigned long *)(buf))[2 + argc + envnum] - sizeof(unsigned long);
128 cwd = (char *)(buf + off);
130 for (i = 0; i < argc; i++)
132 off = ((unsigned long *)(buf))[2 + i] - sizeof(unsigned long);
133 argv[i] = (char *)(buf + off);
144 for (i = 0; i < envnum; i++)
146 off = ((unsigned long *)(buf))[2 + argc + i] - sizeof(unsigned long);
147 envir[i] = (char *)(buf + off);
152 elm_quicklaunch_prepare(argc, argv);
153 elm_quicklaunch_fork(argc, argv, cwd, post_fork, NULL);
154 elm_quicklaunch_cleanup();
158 main(int argc, char **argv)
160 int sock, socket_unix_len;
162 struct sockaddr_un socket_unix;
165 struct sigaction action;
170 fprintf(stderr, "ERROR: failed to init eina.");
173 _log_dom = eina_log_domain_register
174 ("elementary_quicklaunch", EINA_COLOR_CYAN);
177 EINA_LOG_ERR("could not register elementary_quicklaunch log domain.");
178 _log_dom = EINA_LOG_DOMAIN_GLOBAL;
181 if (!(disp = getenv("DISPLAY"))) disp = "unknown";
182 snprintf(buf, sizeof(buf), "/tmp/elm-ql-%i", getuid());
183 if (stat(buf, &st) < 0) mkdir(buf, S_IRUSR | S_IWUSR | S_IXUSR);
184 snprintf(buf, sizeof(buf), "/tmp/elm-ql-%i/%s", getuid(), disp);
186 sock = socket(AF_UNIX, SOCK_STREAM, 0);
189 CRITICAL("cannot create socket for socket for '%s': %s",
190 buf, strerror(errno));
193 if (fcntl(sock, F_SETFD, FD_CLOEXEC) < 0)
195 CRITICAL("cannot set close on exec socket for '%s' (fd=%d): %s",
196 buf, sock, strerror(errno));
201 if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &lin, sizeof(struct linger)) < 0)
203 CRITICAL("cannot set linger for socket for '%s' (fd=%d): %s",
204 buf, sock, strerror(errno));
207 socket_unix.sun_family = AF_UNIX;
208 strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path));
209 socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix);
210 if (bind(sock, (struct sockaddr *)&socket_unix, socket_unix_len) < 0)
212 CRITICAL("cannot bind socket for '%s' (fd=%d): %s",
213 buf, sock, strerror(errno));
216 if (listen(sock, 4096) < 0)
218 CRITICAL("listen(sock=%d, 4096): %s", sock, strerror(errno));
221 elm_quicklaunch_mode_set(EINA_TRUE);
222 elm_quicklaunch_init(argc, argv);
223 restart_time = ecore_time_get();
225 memset(&action, 0, sizeof(struct sigaction));
226 action.sa_handler = SIG_DFL;
227 action.sa_sigaction = NULL;
228 action.sa_flags = SA_RESTART | SA_SIGINFO;
229 sigemptyset(&action.sa_mask);
230 sigaction(SIGINT, &action, &old_sigint);
232 action.sa_handler = SIG_DFL;
233 action.sa_sigaction = NULL;
234 action.sa_flags = SA_RESTART | SA_SIGINFO;
235 sigemptyset(&action.sa_mask);
236 sigaction(SIGTERM, &action, &old_sigterm);
238 action.sa_handler = SIG_DFL;
239 action.sa_sigaction = NULL;
240 action.sa_flags = SA_RESTART | SA_SIGINFO;
241 sigemptyset(&action.sa_mask);
242 sigaction(SIGQUIT, &action, &old_sigquit);
244 action.sa_handler = SIG_DFL;
245 action.sa_sigaction = NULL;
246 action.sa_flags = SA_RESTART | SA_SIGINFO;
247 sigemptyset(&action.sa_mask);
248 sigaction(SIGALRM, &action, &old_sigalrm);
250 action.sa_handler = SIG_DFL;
251 action.sa_sigaction = NULL;
252 action.sa_flags = SA_RESTART | SA_SIGINFO;
253 sigemptyset(&action.sa_mask);
254 sigaction(SIGUSR1, &action, &old_sigusr1);
256 action.sa_handler = SIG_DFL;
257 action.sa_sigaction = NULL;
258 action.sa_flags = SA_RESTART | SA_SIGINFO;
259 sigemptyset(&action.sa_mask);
260 sigaction(SIGUSR2, &action, &old_sigusr2);
262 action.sa_handler = SIG_DFL;
263 action.sa_sigaction = NULL;
264 action.sa_flags = SA_RESTART | SA_SIGINFO;
265 sigemptyset(&action.sa_mask);
266 sigaction(SIGHUP, &action, &old_sighup);
268 action.sa_handler = NULL;
269 action.sa_sigaction = child_handler;
270 action.sa_flags = SA_RESTART | SA_SIGINFO;
271 sigemptyset(&action.sa_mask);
272 sigaction(SIGCHLD, &action, &old_sigchld);
274 action.sa_handler = NULL;
275 action.sa_sigaction = crash_handler;
276 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
277 sigemptyset(&action.sa_mask);
278 sigaction(SIGSEGV, &action, &old_sigsegv);
280 action.sa_handler = NULL;
281 action.sa_sigaction = crash_handler;
282 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
283 sigemptyset(&action.sa_mask);
284 sigaction(SIGILL, &action, &old_sigill);
286 action.sa_handler = NULL;
287 action.sa_sigaction = crash_handler;
288 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
289 sigemptyset(&action.sa_mask);
290 sigaction(SIGFPE, &action, &old_sigfpe);
292 action.sa_handler = NULL;
293 action.sa_sigaction = crash_handler;
294 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
295 sigemptyset(&action.sa_mask);
296 sigaction(SIGBUS, &action, &old_sigbus);
298 action.sa_handler = NULL;
299 action.sa_sigaction = crash_handler;
300 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
301 sigemptyset(&action.sa_mask);
302 sigaction(SIGABRT, &action, &old_sigabrt);
307 struct sockaddr_un client;
310 len = sizeof(struct sockaddr_un);
311 fd = accept(sock, (struct sockaddr *)&client, &len);
312 elm_quicklaunch_sub_init(argc, argv);
313 // don't seed since we are doing this AFTER launch request
314 // elm_quicklaunch_seed();
320 num = read(fd, &bytes, sizeof(unsigned long));
321 if (num == sizeof(unsigned long))
322 handle_run(fd, bytes);
324 while (elm_quicklaunch_sub_shutdown() > 0);
326 elm_quicklaunch_shutdown();
328 if ((_log_dom > -1) && (_log_dom != EINA_LOG_DOMAIN_GLOBAL))
330 eina_log_domain_unregister(_log_dom);