summaryrefslogtreecommitdiff
path: root/src/bin/elementary/quicklaunch.c
blob: 6370f6d6839459032161863fe11944a3c3235727 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#ifdef HAVE_ENVIRON
# define _GNU_SOURCE 1
#endif
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>

#include <Elementary.h>

#ifdef HAVE_ENVIRON
extern char **environ;
#endif

static double restart_time = 0.0;

#define LENGTH_OF_SOCKADDR_UN(s) (strlen((s)->sun_path) + (size_t)(((struct sockaddr_un *)NULL)->sun_path))

static struct sigaction old_sigint;
static struct sigaction old_sigterm;
static struct sigaction old_sigquit;
static struct sigaction old_sigalrm;
static struct sigaction old_sigusr1;
static struct sigaction old_sigusr2;
static struct sigaction old_sighup;
static struct sigaction old_sigchld;
static struct sigaction old_sigsegv;
static struct sigaction old_sigill;
static struct sigaction old_sigfpe;
static struct sigaction old_sigbus;
static struct sigaction old_sigabrt;
static int _log_dom = -1;

#define CRI(...) EINA_LOG_DOM_CRIT(_log_dom, __VA_ARGS__)
#define ERR(...) EINA_LOG_DOM_ERR(_log_dom, __VA_ARGS__)
#define WRN(...) EINA_LOG_DOM_WARN(_log_dom, __VA_ARGS__)
#define INF(...) EINA_LOG_DOM_INFO(_log_dom, __VA_ARGS__)
#define DBG(...) EINA_LOG_DOM_DBG(_log_dom, __VA_ARGS__)

static void
post_fork(void *data EINA_UNUSED)
{
   sigaction(SIGINT, &old_sigint, NULL);
   sigaction(SIGTERM, &old_sigterm, NULL);
   sigaction(SIGQUIT, &old_sigquit, NULL);
   sigaction(SIGALRM, &old_sigalrm, NULL);
   sigaction(SIGUSR1, &old_sigusr1, NULL);
   sigaction(SIGUSR2, &old_sigusr2, NULL);
   sigaction(SIGHUP, &old_sighup, NULL);
   sigaction(SIGCHLD, &old_sigchld, NULL);
   sigaction(SIGSEGV, &old_sigsegv, NULL);
   sigaction(SIGILL, &old_sigill, NULL);
   sigaction(SIGFPE, &old_sigfpe, NULL);
   sigaction(SIGBUS, &old_sigbus, NULL);
   sigaction(SIGABRT, &old_sigabrt, NULL);
   if ((_log_dom > -1) && (_log_dom != EINA_LOG_DOMAIN_GLOBAL))
     {
        eina_log_domain_unregister(_log_dom);
        _log_dom = -1;
     }
}

static void
child_handler(int x EINA_UNUSED, siginfo_t *info EINA_UNUSED, void *data EINA_UNUSED)
{
   int status;
   while (waitpid(-1, &status, WNOHANG) > 0);
}

static void
crash_handler(int x EINA_UNUSED, siginfo_t *info EINA_UNUSED, void *data EINA_UNUSED)
{
   double t;

   ERR("crash detected. restarting.");
   t = ecore_time_get();
   if ((t - restart_time) <= 2.0)
     {
        CRI("crash too fast - less than 2 seconds. abort restart");
        exit(-1);
     }
   ecore_app_restart();
}

static void
handle_run(int fd, unsigned long bytes)
{
   unsigned char *buf = NULL;
   int i;
   char **argv = NULL;
   char **envir = NULL;
   char *cwd;
   int argc, envnum;
   unsigned long off;

   if (bytes < 1)
     {
        CRI("no bytes to quicklaunch");
        return;
     }
   DBG("Starting building up process.");
   _elm_startup_time = ecore_time_unix_get();

   buf = alloca(bytes);
   if (read(fd, buf, bytes) != (int)bytes)
     {
        CRI("cannot read %i bytes of args and environment data", (int)bytes);
        close(fd);
        return;
     }
   close(fd);
   buf[bytes - 1] = 0;

   argc = ((unsigned long *)(buf))[0];
   envnum = ((unsigned long *)(buf))[1];

   if (argc <= 0)
     {
        CRI("no executable specified");
        return;
     }

   argv = alloca(argc * sizeof(char *));
   if (envnum > 0) envir = alloca(envnum * sizeof(char *));
   off = ((unsigned long *)(buf))[2 + argc + envnum] - sizeof(unsigned long);
   cwd = (char *)(buf + off);

   for (i = 0; i < argc; i++)
     {
        off = ((unsigned long *)(buf))[2 + i] - sizeof(unsigned long);
        argv[i] = (char *)(buf + off);
     }

#ifdef HAVE_ENVIRON
   if (envir)
     {
#ifdef HAVE_CLEARENV
        clearenv();
#else
        environ = NULL;
#endif
        for (i = 0; i < envnum; i++)
          {
             off = ((unsigned long *)(buf))[2 + argc + i] - sizeof(unsigned long);
             envir[i] = (char *)(buf + off);
             putenv(envir[i]);
          }
     }
#endif

   INF("Requested to run '%s' with %i arguments and %i environment.",
           argv[0], argc - 1, envnum);
   // Try new form before trying old form
   if (!efl_quicklaunch_prepare(argc, argv, cwd))
     {
        WRN("Failed to prepare with new EFL_MAIN macro, switching to legacy.");
        elm_quicklaunch_prepare(argc, argv, cwd);
     }

   elm_quicklaunch_fork(argc, argv, cwd, post_fork, NULL);
   elm_quicklaunch_cleanup();
}

int
main(int argc, char **argv)
{
   int sock, socket_unix_len;
   struct stat st;
   struct sockaddr_un socket_unix;
   struct linger lin;
   char buf[PATH_MAX];
   struct sigaction action;
   const char *domain;
   int ret = 0;

   if (!eina_init())
     {
        fprintf(stderr, "ERROR: failed to init eina.");
        exit(-1);
     }
   _log_dom = eina_log_domain_register
      ("elementary_quicklaunch", EINA_COLOR_CYAN);
   if (_log_dom < 0)
     {
        EINA_LOG_ERR("could not register elementary_quicklaunch log domain.");
        _log_dom = EINA_LOG_DOMAIN_GLOBAL;
     }

   if (!(domain = getenv("ELM_QUICKLAUNCH_DOMAIN")))
     {
        domain = getenv("WAYLAND_DISPLAY");
        if (!domain) domain = getenv("DISPLAY");
        if (!domain) domain = "unknown";
     }
   snprintf(buf, sizeof(buf), "/tmp/elm-ql-%i", getuid());
   if (stat(buf, &st) < 0)
     {
        ret = mkdir(buf, S_IRUSR | S_IWUSR | S_IXUSR);
        if (ret < 0)
          {
             CRI("cannot create directory '%s'", buf);
             exit(-1);
          }
     }
   snprintf(buf, sizeof(buf), "/tmp/elm-ql-%i/%s", getuid(), domain);
   unlink(buf);
   sock = socket(AF_UNIX, SOCK_STREAM, 0);
   if (sock < 0)
     {
        CRI("cannot create socket for socket for '%s': %s",
                 buf, strerror(errno));
        exit(-1);
     }
   if (!eina_file_close_on_exec(sock, EINA_TRUE))
     {
        CRI("cannot set close on exec socket for '%s' (fd=%d): %s",
                 buf, sock, strerror(errno));
        exit(-1);
     }
   lin.l_onoff = 1;
   lin.l_linger = 0;
   if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &lin, sizeof(struct linger)) < 0)
     {
        CRI("cannot set linger for socket for '%s' (fd=%d): %s",
                 buf, sock, strerror(errno));
        exit(-1);
     }
   socket_unix.sun_family = AF_UNIX;
   strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path));
   socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix);
   if (bind(sock, (struct sockaddr *)&socket_unix, socket_unix_len) < 0)
     {
        CRI("cannot bind socket for '%s' (fd=%d): %s",
                 buf, sock, strerror(errno));
        exit(-1);
     }
   if (listen(sock, 4096) < 0)
     {
        CRI("listen(sock=%d, 4096): %s", sock, strerror(errno));
        exit(-1);
     }
   elm_quicklaunch_mode_set(EINA_TRUE);
   elm_quicklaunch_init(argc, argv);
   restart_time = ecore_time_get();

   memset(&action, 0, sizeof(struct sigaction));
   action.sa_handler = SIG_DFL;
   action.sa_sigaction = NULL;
   action.sa_flags = SA_RESTART | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGINT, &action, &old_sigint);

   action.sa_handler = SIG_DFL;
   action.sa_sigaction = NULL;
   action.sa_flags = SA_RESTART | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGTERM, &action, &old_sigterm);

   action.sa_handler = SIG_DFL;
   action.sa_sigaction = NULL;
   action.sa_flags = SA_RESTART | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGQUIT, &action, &old_sigquit);

   action.sa_handler = SIG_DFL;
   action.sa_sigaction = NULL;
   action.sa_flags = SA_RESTART | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGALRM, &action, &old_sigalrm);

   action.sa_handler = SIG_DFL;
   action.sa_sigaction = NULL;
   action.sa_flags = SA_RESTART | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGUSR1, &action, &old_sigusr1);

   action.sa_handler = SIG_DFL;
   action.sa_sigaction = NULL;
   action.sa_flags = SA_RESTART | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGUSR2, &action, &old_sigusr2);

   action.sa_handler = SIG_DFL;
   action.sa_sigaction = NULL;
   action.sa_flags = SA_RESTART | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGHUP, &action, &old_sighup);

   action.sa_handler = NULL;
   action.sa_sigaction = child_handler;
   action.sa_flags = SA_RESTART | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGCHLD, &action, &old_sigchld);

   action.sa_handler = NULL;
   action.sa_sigaction = crash_handler;
   action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGSEGV, &action, &old_sigsegv);

   action.sa_handler = NULL;
   action.sa_sigaction = crash_handler;
   action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGILL, &action, &old_sigill);

   action.sa_handler = NULL;
   action.sa_sigaction = crash_handler;
   action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGFPE, &action, &old_sigfpe);

   action.sa_handler = NULL;
   action.sa_sigaction = crash_handler;
   action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGBUS, &action, &old_sigbus);

   action.sa_handler = NULL;
   action.sa_sigaction = crash_handler;
   action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
   sigemptyset(&action.sa_mask);
   sigaction(SIGABRT, &action, &old_sigabrt);

   for (;;)
     {
        int fd;
        struct sockaddr_un client;
        socklen_t len;

        len = sizeof(struct sockaddr_un);
        fd = accept(sock, (struct sockaddr *)&client, &len);

        DBG("Accepting connection.");
        elm_quicklaunch_sub_init(argc, argv);
        // don't seed since we are doing this AFTER launch request
        // elm_quicklaunch_seed();
        if (fd >= 0)
          {
             unsigned long bytes;
             int num;

             num = read(fd, &bytes, sizeof(unsigned long));
             if (num == sizeof(unsigned long))
               handle_run(fd, bytes);
          }
        while (elm_quicklaunch_sub_shutdown() > 0);
     }
   elm_quicklaunch_shutdown();

   if ((_log_dom > -1) && (_log_dom != EINA_LOG_DOMAIN_GLOBAL))
     {
        eina_log_domain_unregister(_log_dom);
        _log_dom = -1;
     }
   eina_shutdown();

   return 0;
}