src/script.c: pass error from launch_script
[vlock.git] / src / script.c
blob9d86aefdf7c0f652a091ccfc3cd3d1ad632b8834
1 /* script.c -- script routines for vlock, the VT locking program for linux
3 * This program is copyright (C) 2007 Frank Benkstein, and is free
4 * software which is freely distributable under the terms of the
5 * GNU General Public License version 2, included as the file COPYING in this
6 * distribution. It is NOT public domain software, and any
7 * redistribution not permitted by the GNU General Public License is
8 * expressly forbidden without prior written permission from
9 * the author.
13 /* Scripts are executables that are run as unprivileged child processes of
14 * vlock. They communicate with vlock through stdin and stdout.
16 * When dependencies are retrieved they are launched once for each dependency
17 * and should print the names of the plugins they depend on on stdout one per
18 * line. The dependency requested is given as a single command line argument.
20 * In hook mode the script is called once with "hooks" as a single command line
21 * argument. It should not exit until its stdin closes. The hook that should
22 * be executed is written to its stdin on a single line.
24 * Currently there is no way for a script to communicate errors or even success
25 * to vlock. If it exits it will linger as a zombie until the plugin is
26 * destroyed.
29 #if !defined(__FreeBSD__) && !defined(_GNU_SOURCE)
30 #define _GNU_SOURCE
31 #endif
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <sys/select.h>
39 #include <fcntl.h>
40 #include <signal.h>
41 #include <errno.h>
42 #include <sys/time.h>
43 #include <time.h>
45 #include "list.h"
46 #include "process.h"
47 #include "util.h"
49 #include "plugin.h"
51 static bool init_script(struct plugin *p);
52 static void destroy_script(struct plugin *p);
53 static bool call_script_hook(struct plugin *p, const char *hook_name);
55 struct plugin_type *script = &(struct plugin_type){
56 .init = init_script,
57 .destroy = destroy_script,
58 .call_hook = call_script_hook,
61 struct script_context
63 /* The pipe file descriptor that is connected to the script's stdin. */
64 int fd;
65 /* The PID of the script. */
66 pid_t pid;
69 /* Get the dependency from the script. */
70 static bool get_dependency(const char *path, const char *dependency_name,
71 struct list *dependency_list);
72 /* Launch the script creating a new script_context. No error detection aborts
73 * on fatal errors. */
74 static struct script_context *launch_script(const char *path);
76 bool init_script(struct plugin *p)
78 char *path;
80 if (asprintf(&path, "%s/%s", VLOCK_SCRIPT_DIR, p->name) < 0) {
81 errno = ENOMEM;
82 return false;
85 /* Test for access. This must be done manually because vlock most likely
86 * runs as a setuid executable and would otherwise override restrictions.
88 * Also there is currently no error detection in case exec() fails later.
90 if (access(path, X_OK) < 0) {
91 int errsv = errno;
92 free(path);
93 errno = errsv;
94 return false;
97 /* Get the dependency information. */
98 for (size_t i = 0; i < nr_dependencies; i++)
99 if (!get_dependency(path, dependency_names[i], p->dependencies[i]))
100 return false;
102 /* Launch the script. */
103 p->context = launch_script(path);
105 if (p->context == NULL) {
106 int errsv = errno;
107 free(path);
108 errno = errsv;
109 return false;
110 } else {
111 free(path);
112 return true;
116 static void destroy_script(struct plugin *p)
118 struct script_context *context = p->context;
120 if (context != NULL) {
121 /* Close the pipe. */
122 (void) close(context->fd);
124 /* Kill the child process. */
125 if (!wait_for_death(context->pid, 0, 500000L))
126 ensure_death(context->pid);
128 free(context);
132 /* Invoke the hook by writing it on a single line to the scripts stdin. */
133 static bool call_script_hook(struct plugin *s, const char *hook_name)
135 struct script_context *context = s->context;
136 char *data;
137 ssize_t data_length;
138 ssize_t length;
139 struct sigaction act, oldact;
141 /* Format the line. */
142 data_length = asprintf(&data, "%s\n", hook_name);
144 if (data_length < 0)
145 fatal_error("memory allocation failed");
147 /* When writing to a pipe when the read end is closed the kernel invariably
148 * sends SIGPIPE. Ignore it. */
149 (void) sigemptyset(&(act.sa_mask));
150 act.sa_flags = SA_RESTART;
151 act.sa_handler = SIG_IGN;
152 (void) sigaction(SIGPIPE, &act, &oldact);
154 /* Send hook name and a newline through the pipe. */
155 length = write(context->fd, data, data_length);
157 /* Restore the previous SIGPIPE handler. */
158 (void) sigaction(SIGPIPE, &oldact, NULL);
160 /* Scripts fail silently. */
161 return (length == data_length);
164 static struct script_context *launch_script(const char *path)
166 struct script_context *script = ensure_malloc(sizeof *script);
167 const char *argv[] = { path, "hooks", NULL };
168 struct child_process child = {
169 .path = path,
170 .argv = argv,
171 .stdin_fd = REDIRECT_PIPE,
172 .stdout_fd = REDIRECT_DEV_NULL,
173 .stderr_fd = REDIRECT_DEV_NULL,
174 .function = NULL,
177 if (!create_child(&child)) {
178 int errsv = errno;
179 free(script);
180 errno = errsv;
181 return NULL;
184 script->fd = child.stdin_fd;
185 script->pid = child.pid;
187 return script;
190 static char *read_dependency(const char *path, const char *dependency_name);
191 static bool parse_dependency(char *data, struct list *dependency_list);
193 /* Get the dependency from the script. */
194 static bool get_dependency(const char *path, const char *dependency_name,
195 struct list *dependency_list)
197 /* Read the dependency data. */
198 char *data;
199 errno = 0;
200 data = read_dependency(path, dependency_name);
202 if (data == NULL) {
203 return errno != 0;
204 } else {
205 /* Parse the dependency data. */
206 bool result = parse_dependency(data, dependency_list);
207 int errsv = errno;
208 free(data);
209 errno = errsv;
210 return result;
214 /* Read the dependency data by starting the script with the name of the
215 * dependency as a single command line argument. The script should then print
216 * the dependencies to its stdout one on per line. */
217 static char *read_dependency(const char *path, const char *dependency_name)
219 char *error = NULL;
220 int pipe_fds[2];
221 pid_t pid;
222 struct timeval timeout = (struct timeval){1, 0};
223 char *data = ensure_calloc(1, sizeof *data);
224 size_t data_length = 0;
226 /* Open a pipe for the script. */
227 if (pipe(pipe_fds) < 0)
228 fatal_error("pipe() failed");
230 pid = fork();
232 if (pid == 0) {
233 /* Child. */
234 int nullfd = open("/dev/null", O_RDWR);
236 if (nullfd < 0)
237 _exit(1);
239 /* Redirect stdio. */
240 (void) dup2(nullfd, STDIN_FILENO);
241 (void) dup2(pipe_fds[1], STDOUT_FILENO);
242 (void) dup2(nullfd, STDERR_FILENO);
244 /* Close all other file descriptors. */
245 close_all_fds();
247 /* Drop privileges. */
248 setgid(getgid());
249 setuid(getuid());
251 (void) execl(path, path, dependency_name, NULL);
253 _exit(1);
256 /* Close write end of the pipe. */
257 (void) close(pipe_fds[1]);
259 if (pid < 0) {
260 (void) close(pipe_fds[0]);
261 free(data);
262 fatal_error("fork() failed");
265 /* Read the dependency from the child. Reading fails if either the timeout
266 * elapses or more that LINE_MAX bytes are read. */
267 for (;;) {
268 struct timeval t = timeout;
269 struct timeval t1;
270 struct timeval t2;
271 char buffer[LINE_MAX];
272 ssize_t length;
274 fd_set read_fds;
276 FD_ZERO(&read_fds);
277 FD_SET(pipe_fds[0], &read_fds);
279 /* t1 is before select. */
280 (void) gettimeofday(&t1, NULL);
282 if (select(pipe_fds[0]+1, &read_fds, NULL, NULL, &t) != 1) {
283 timeout:
284 asprintf(&error, "timeout while reading dependency '%s' from '%s", dependency_name, path);
285 goto read_error;
288 /* t2 is after select. */
289 (void) gettimeofday(&t2, NULL);
291 /* Get the time that during select. */
292 timersub(&t2, &t1, &t2);
294 /* This is very unlikely. */
295 if (timercmp(&t2, &timeout, >))
296 goto timeout;
298 /* Reduce the timeout. */
299 timersub(&timeout, &t2, &timeout);
301 /* Read dependency data from the script. */
302 length = read(pipe_fds[0], buffer, sizeof buffer - 1);
304 /* Did the script close its stdin or exit? */
305 if (length <= 0)
306 break;
308 if (data_length+length+1 > LINE_MAX) {
309 asprintf(&error, "too much data while reading dependency '%s' from '%s'", dependency_name, path);
310 goto read_error;
313 /* Append the buffer to the data string. */
314 data = ensure_realloc(data, data_length+length);
315 strncpy(data+data_length, buffer, length);
316 data_length += length;
319 /* Terminate the data string. */
320 data[data_length] = '\0';
322 /* Close the read end of the pipe. */
323 (void) close(pipe_fds[0]);
324 /* Kill the script. */
325 if (!wait_for_death(pid, 0, 500000L))
326 ensure_death(pid);
328 return data;
330 read_error:
331 free(data);
332 (void) close(pipe_fds[0]);
333 if (!wait_for_death(pid, 0, 500000L))
334 ensure_death(pid);
335 fprintf(stderr, "%s\n", error);
336 free(error);
337 abort();
340 static bool parse_dependency(char *data, struct list *dependency_list)
342 for (char *s = data, *saveptr;; s = NULL) {
343 char *token = strtok_r(s, "\n", &saveptr);
345 if (token == NULL)
346 break;
348 list_append(dependency_list, ensure_not_null(strdup(token), "could not copy string"));
351 return true;