neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

launch.c (4057B)


      1 /* swc: libswc/launch.c
      2  *
      3  * Copyright (c) 2013, 2014 Michael Forney
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a copy
      6  * of this software and associated documentation files (the "Software"), to deal
      7  * in the Software without restriction, including without limitation the rights
      8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9  * copies of the Software, and to permit persons to whom the Software is
     10  * furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice shall be included in
     13  * all copies or substantial portions of the Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     21  * SOFTWARE.
     22  */
     23 
     24 #include "launch.h"
     25 #include "event.h"
     26 #include "internal.h"
     27 #include "launch/protocol.h"
     28 #include "util.h"
     29 
     30 #include <fcntl.h>
     31 #include <sys/uio.h>
     32 #include <unistd.h>
     33 #include <wayland-server.h>
     34 
     35 static struct {
     36 	int socket;
     37 	struct wl_event_source *source;
     38 	uint32_t next_serial;
     39 } launch;
     40 
     41 static bool
     42 handle_event(struct swc_launch_event *event)
     43 {
     44 	switch (event->type) {
     45 	case SWC_LAUNCH_EVENT_ACTIVATE:
     46 		swc_activate();
     47 		break;
     48 	case SWC_LAUNCH_EVENT_DEACTIVATE:
     49 		swc_deactivate();
     50 		break;
     51 	default:
     52 		return false;
     53 	}
     54 
     55 	return true;
     56 }
     57 
     58 static int
     59 handle_data(int fd, uint32_t mask, void *data)
     60 {
     61 	struct swc_launch_event event;
     62 	struct iovec iov[1] = {
     63 	    {.iov_base = &event, .iov_len = sizeof(event)},
     64 	};
     65 
     66 	if (receive_fd(fd, NULL, iov, 1) != -1) {
     67 		handle_event(&event);
     68 	}
     69 	return 1;
     70 }
     71 
     72 bool
     73 launch_initialize(void)
     74 {
     75 	char *socket_string, *end;
     76 
     77 	if (!(socket_string = getenv(SWC_LAUNCH_SOCKET_ENV))) {
     78 		return false;
     79 	}
     80 
     81 	launch.socket = strtol(socket_string, &end, 10);
     82 	if (*end != '\0') {
     83 		return false;
     84 	}
     85 
     86 	unsetenv(SWC_LAUNCH_SOCKET_ENV);
     87 	if (fcntl(launch.socket, F_SETFD, FD_CLOEXEC) < 0) {
     88 		return false;
     89 	}
     90 
     91 	launch.source = wl_event_loop_add_fd(swc.event_loop, launch.socket,
     92 	                                     WL_EVENT_READABLE, &handle_data, NULL);
     93 	if (!launch.source) {
     94 		return false;
     95 	}
     96 
     97 	return true;
     98 }
     99 
    100 void
    101 launch_finalize(void)
    102 {
    103 	wl_event_source_remove(launch.source);
    104 	close(launch.socket);
    105 }
    106 
    107 static bool
    108 send_request(struct swc_launch_request *request, const void *data, size_t size,
    109              struct swc_launch_event *event, int out_fd, int *in_fd)
    110 {
    111 	struct iovec request_iov[2] = {
    112 	    {.iov_base = request, .iov_len = sizeof(*request)},
    113 	    {.iov_base = (void *)data, .iov_len = size},
    114 	};
    115 	struct iovec response_iov[1] = {
    116 	    {.iov_base = event, .iov_len = sizeof(*event)},
    117 	};
    118 
    119 	/* no launcher socket: the zeroed fd would be stdin */
    120 	if (!launch.source) {
    121 		return false;
    122 	}
    123 
    124 	request->serial = ++launch.next_serial;
    125 
    126 	if (send_fd(launch.socket, out_fd, request_iov, 1 + (size > 0)) == -1) {
    127 		return false;
    128 	}
    129 
    130 	while (receive_fd(launch.socket, in_fd, response_iov, 1) != -1) {
    131 		if (event->type == SWC_LAUNCH_EVENT_RESPONSE &&
    132 		    event->serial == request->serial) {
    133 			return true;
    134 		}
    135 		handle_event(event);
    136 	}
    137 
    138 	return false;
    139 }
    140 
    141 int
    142 launch_open_device(const char *path, int flags)
    143 {
    144 	struct swc_launch_request request;
    145 	struct swc_launch_event response;
    146 	int fd;
    147 
    148 	request.type = SWC_LAUNCH_REQUEST_OPEN_DEVICE;
    149 	request.flags = flags;
    150 
    151 	if (!send_request(&request, path, strlen(path) + 1, &response, -1, &fd)) {
    152 		return -1;
    153 	}
    154 
    155 	return fd;
    156 }
    157 
    158 bool
    159 launch_activate_vt(unsigned vt)
    160 {
    161 	struct swc_launch_request request;
    162 	struct swc_launch_event response;
    163 
    164 	request.type = SWC_LAUNCH_REQUEST_ACTIVATE_VT;
    165 	request.vt = vt;
    166 
    167 	if (!send_request(&request, NULL, 0, &response, -1, NULL)) {
    168 		return false;
    169 	}
    170 
    171 	return response.success;
    172 }