weston: migrate x11 to head-based output API
[platform/upstream/weston.git] / compositor / xwayland.c
1 /*
2  * Copyright © 2011 Intel Corporation
3  * Copyright © 2016 Giulio Camuffo
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26
27 #include "config.h"
28
29 #include <signal.h>
30 #include <sys/socket.h>
31
32 #include "compositor.h"
33 #include "compositor/weston.h"
34 #include "xwayland/xwayland-api.h"
35 #include "shared/helpers.h"
36
37 struct wet_xwayland {
38         struct weston_compositor *compositor;
39         const struct weston_xwayland_api *api;
40         struct weston_xwayland *xwayland;
41         struct wl_event_source *sigusr1_source;
42         struct wl_client *client;
43         int wm_fd;
44         struct weston_process process;
45 };
46
47 static int
48 handle_sigusr1(int signal_number, void *data)
49 {
50         struct wet_xwayland *wxw = data;
51
52         /* We'd be safer if we actually had the struct
53          * signalfd_siginfo from the signalfd data and could verify
54          * this came from Xwayland.*/
55         wxw->api->xserver_loaded(wxw->xwayland, wxw->client, wxw->wm_fd);
56         wl_event_source_remove(wxw->sigusr1_source);
57
58         return 1;
59 }
60
61 static pid_t
62 spawn_xserver(void *user_data, const char *display, int abstract_fd, int unix_fd)
63 {
64         struct wet_xwayland *wxw = user_data;
65         pid_t pid;
66         char s[8], abstract_fd_str[8], unix_fd_str[8], wm_fd_str[8];
67         int sv[2], wm[2], fd;
68         char *xserver = NULL;
69         struct weston_config *config = wet_get_config(wxw->compositor);
70         struct weston_config_section *section;
71
72         if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
73                 weston_log("wl connection socketpair failed\n");
74                 return 1;
75         }
76
77         if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, wm) < 0) {
78                 weston_log("X wm connection socketpair failed\n");
79                 return 1;
80         }
81
82         pid = fork();
83         switch (pid) {
84         case 0:
85                 /* SOCK_CLOEXEC closes both ends, so we need to unset
86                  * the flag on the client fd. */
87                 fd = dup(sv[1]);
88                 if (fd < 0)
89                         goto fail;
90                 snprintf(s, sizeof s, "%d", fd);
91                 setenv("WAYLAND_SOCKET", s, 1);
92
93                 fd = dup(abstract_fd);
94                 if (fd < 0)
95                         goto fail;
96                 snprintf(abstract_fd_str, sizeof abstract_fd_str, "%d", fd);
97                 fd = dup(unix_fd);
98                 if (fd < 0)
99                         goto fail;
100                 snprintf(unix_fd_str, sizeof unix_fd_str, "%d", fd);
101                 fd = dup(wm[1]);
102                 if (fd < 0)
103                         goto fail;
104                 snprintf(wm_fd_str, sizeof wm_fd_str, "%d", fd);
105
106                 section = weston_config_get_section(config,
107                                                     "xwayland", NULL, NULL);
108                 weston_config_section_get_string(section, "path",
109                                                  &xserver, XSERVER_PATH);
110
111                 /* Ignore SIGUSR1 in the child, which will make the X
112                  * server send SIGUSR1 to the parent (weston) when
113                  * it's done with initialization.  During
114                  * initialization the X server will round trip and
115                  * block on the wayland compositor, so avoid making
116                  * blocking requests (like xcb_connect_to_fd) until
117                  * it's done with that. */
118                 signal(SIGUSR1, SIG_IGN);
119
120                 if (execl(xserver,
121                           xserver,
122                           display,
123                           "-rootless",
124                           "-listen", abstract_fd_str,
125                           "-listen", unix_fd_str,
126                           "-wm", wm_fd_str,
127                           "-terminate",
128                           NULL) < 0)
129                         weston_log("exec of '%s %s -rootless "
130                                    "-listen %s -listen %s -wm %s "
131                                    "-terminate' failed: %m\n",
132                                    xserver, display,
133                                    abstract_fd_str, unix_fd_str, wm_fd_str);
134         fail:
135                 _exit(EXIT_FAILURE);
136
137         default:
138                 close(sv[1]);
139                 wxw->client = wl_client_create(wxw->compositor->wl_display, sv[0]);
140
141                 close(wm[1]);
142                 wxw->wm_fd = wm[0];
143
144                 wxw->process.pid = pid;
145                 weston_watch_process(&wxw->process);
146                 break;
147
148         case -1:
149                 weston_log("Failed to fork to spawn xserver process\n");
150                 break;
151         }
152
153         return pid;
154 }
155
156 static void
157 xserver_cleanup(struct weston_process *process, int status)
158 {
159         struct wet_xwayland *wxw =
160                 container_of(process, struct wet_xwayland, process);
161         struct wl_event_loop *loop =
162                 wl_display_get_event_loop(wxw->compositor->wl_display);
163
164         wxw->api->xserver_exited(wxw->xwayland, status);
165         wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1,
166                                                        handle_sigusr1, wxw);
167         wxw->client = NULL;
168 }
169
170 int
171 wet_load_xwayland(struct weston_compositor *comp)
172 {
173         const struct weston_xwayland_api *api;
174         struct weston_xwayland *xwayland;
175         struct wet_xwayland *wxw;
176         struct wl_event_loop *loop;
177
178         if (weston_compositor_load_xwayland(comp) < 0)
179                 return -1;
180
181         api = weston_xwayland_get_api(comp);
182         if (!api) {
183                 weston_log("Failed to get the xwayland module API.\n");
184                 return -1;
185         }
186
187         xwayland = api->get(comp);
188         if (!xwayland) {
189                 weston_log("Failed to get the xwayland object.\n");
190                 return -1;
191         }
192
193         wxw = zalloc(sizeof *wxw);
194         if (!wxw)
195                 return -1;
196
197         wxw->compositor = comp;
198         wxw->api = api;
199         wxw->xwayland = xwayland;
200         wxw->process.cleanup = xserver_cleanup;
201         if (api->listen(xwayland, wxw, spawn_xserver) < 0)
202                 return -1;
203
204         loop = wl_display_get_event_loop(comp->wl_display);
205         wxw->sigusr1_source = wl_event_loop_add_signal(loop, SIGUSR1,
206                                                        handle_sigusr1, wxw);
207
208         return 0;
209 }