1 #include <Elementary.h>
7 #include <sys/socket.h>
15 static double restart_time = 0.0;
17 #define LENGTH_OF_SOCKADDR_UN(s) (strlen((s)->sun_path) + (size_t)(((struct sockaddr_un *)NULL)->sun_path))
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;
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__)
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))
58 eina_log_domain_unregister(_log_dom);
64 child_handler(int x, siginfo_t *info, void *data)
69 while ((pid = waitpid(-1, &status, WNOHANG)) > 0);
73 crash_handler(int x, siginfo_t *info, void *data)
77 ERR("crash detected. restarting.");
79 if ((t - restart_time) <= 2.0)
81 CRITICAL("crash too fast - less than 2 seconds. abort restart");
88 handle_run(int fd, unsigned long bytes)
90 unsigned char *buf = NULL;
97 if ((num = read(fd, buf, bytes)) < 0)
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();
113 main(int argc, char **argv)
115 int sock, socket_unix_len;
117 struct sockaddr_un socket_unix;
120 struct sigaction action;
124 fprintf(stderr, "ERROR: failed to init eina.");
127 _log_dom = eina_log_domain_register
128 ("elementary_quicklaunch", EINA_COLOR_CYAN);
131 EINA_LOG_ERR("could not register elementary_quicklaunch log domain.");
132 _log_dom = EINA_LOG_DOMAIN_GLOBAL;
135 if (!getenv("DISPLAY"))
137 CRITICAL("DISPLAY env var not set");
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"));
144 sock = socket(AF_UNIX, SOCK_STREAM, 0);
147 CRITICAL("cannot create socket for socket for '%s': %s",
148 buf, strerror(errno));
151 if (fcntl(sock, F_SETFD, FD_CLOEXEC) < 0)
153 CRITICAL("cannot set close on exec socket for '%s' (fd=%d): %s",
154 buf, sock, strerror(errno));
159 if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &lin, sizeof(struct linger)) < 0)
161 CRITICAL("cannot set linger for socket for '%s' (fd=%d): %s",
162 buf, sock, strerror(errno));
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)
170 CRITICAL("cannot bind socket for '%s' (fd=%d): %s",
171 buf, sock, strerror(errno));
174 if (listen(sock, 4096) < 0)
176 CRITICAL("listen(sock=%d, 4096): %s", sock, strerror(errno));
179 elm_quicklaunch_init(argc, argv);
180 restart_time = ecore_time_get();
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
264 struct sockaddr_un client;
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);
276 num = read(fd, &bytes, sizeof(unsigned long));
277 if (num == sizeof(unsigned long))
279 ecore_app_args_set(argc, (const char **)argv);
280 handle_run(fd, bytes);
283 elm_quicklaunch_sub_shutdown();
285 elm_quicklaunch_shutdown();
287 if ((_log_dom > -1) && (_log_dom != EINA_LOG_DOMAIN_GLOBAL))
289 eina_log_domain_unregister(_log_dom);