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
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
29 #if !defined(__FreeBSD__) && !defined(_GNU_SOURCE)
38 #include <sys/select.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
){
57 .destroy
= destroy_script
,
58 .call_hook
= call_script_hook
,
63 /* The path to the script. */
65 /* Was the script launched? */
67 /* Did the script die? */
69 /* The pipe file descriptor that is connected to the script's stdin. */
71 /* The PID of the script. */
75 /* Get the dependency from the script. */
76 static bool get_dependency(const char *path
, const char *dependency_name
,
77 struct list
*dependency_list
);
78 /* Launch the script creating a new script_context. */
79 static bool launch_script(struct script_context
*script
);
81 bool init_script(struct plugin
*p
)
84 struct script_context
*context
= malloc(sizeof *context
);
89 context
->dead
= false;
90 context
->launched
= false;
92 if (asprintf(&context
->path
, "%s/%s", VLOCK_SCRIPT_DIR
, p
->name
) < 0) {
98 /* Get the dependency information. Whether the script is executable or not
99 * is also detected here. */
100 for (size_t i
= 0; i
< nr_dependencies
; i
++)
101 if (!get_dependency(context
->path
, dependency_names
[i
], p
->dependencies
[i
]))
104 p
->context
= context
;
115 static void destroy_script(struct plugin
*p
)
117 struct script_context
*context
= p
->context
;
119 if (context
!= NULL
) {
122 /* Close the pipe. */
123 (void) close(context
->fd
);
125 /* Kill the child process. */
126 if (!wait_for_death(context
->pid
, 0, 500000L))
127 ensure_death(context
->pid
);
133 /* Invoke the hook by writing it on a single line to the scripts stdin. */
134 static bool call_script_hook(struct plugin
*s
, const char *hook_name
)
136 static const char newline
= '\n';
137 struct script_context
*context
= s
->context
;
138 ssize_t hook_name_length
= strlen(hook_name
);
140 struct sigaction act
;
141 struct sigaction oldact
;
143 if (!context
->launched
) {
145 context
->launched
= launch_script(context
);
147 if (!context
->launched
)
155 /* When writing to a pipe when the read end is closed the kernel invariably
156 * sends SIGPIPE. Ignore it. */
157 (void) sigemptyset(&(act
.sa_mask
));
158 act
.sa_flags
= SA_RESTART
;
159 act
.sa_handler
= SIG_IGN
;
160 (void) sigaction(SIGPIPE
, &act
, &oldact
);
162 /* Send hook name and a newline through the pipe. */
163 length
= write(context
->fd
, hook_name
, hook_name_length
);
166 length
+= write(context
->fd
, &newline
, sizeof newline
);
168 /* Restore the previous SIGPIPE handler. */
169 (void) sigaction(SIGPIPE
, &oldact
, NULL
);
171 /* If write fails the script is considered dead. */
172 context
->dead
= (length
!= hook_name_length
+ 1);
174 return !context
->dead
;
177 static bool launch_script(struct script_context
*script
)
180 const char *argv
[] = { script
->path
, "hooks", NULL
};
181 struct child_process child
= {
182 .path
= script
->path
,
184 .stdin_fd
= REDIRECT_PIPE
,
185 .stdout_fd
= REDIRECT_DEV_NULL
,
186 .stderr_fd
= REDIRECT_DEV_NULL
,
190 if (!create_child(&child
))
193 script
->fd
= child
.stdin_fd
;
194 script
->pid
= child
.pid
;
196 fd_flags
= fcntl(script
->fd
, F_GETFL
, &fd_flags
);
198 if (fd_flags
!= -1) {
199 fd_flags
|= O_NONBLOCK
;
200 (void) fcntl(script
->fd
, F_SETFL
, fd_flags
);
206 static char *read_dependency(const char *path
, const char *dependency_name
);
207 static bool parse_dependency(char *data
, struct list
*dependency_list
);
209 /* Get the dependency from the script. */
210 static bool get_dependency(const char *path
, const char *dependency_name
,
211 struct list
*dependency_list
)
213 /* Read the dependency data. */
216 data
= read_dependency(path
, dependency_name
);
221 /* Parse the dependency data. */
222 bool result
= parse_dependency(data
, dependency_list
);
230 /* Read the dependency data by starting the script with the name of the
231 * dependency as a single command line argument. The script should then print
232 * the dependencies to its stdout one on per line. */
233 static char *read_dependency(const char *path
, const char *dependency_name
)
235 const char *argv
[] = { path
, dependency_name
, NULL
};
236 struct child_process child
= {
239 .stdin_fd
= REDIRECT_DEV_NULL
,
240 .stdout_fd
= REDIRECT_PIPE
,
241 .stderr_fd
= REDIRECT_DEV_NULL
,
244 struct timeval timeout
= {1, 0};
245 char *data
= calloc(1, sizeof *data
);
246 size_t data_length
= 0;
251 if (!create_child(&child
)) {
258 /* Read the dependency from the child. Reading fails if either the timeout
259 * elapses or more that LINE_MAX bytes are read. */
261 struct timeval t
= timeout
;
264 char buffer
[LINE_MAX
];
270 FD_SET(child
.stdout_fd
, &read_fds
);
272 /* t1 is before select. */
273 (void) gettimeofday(&t1
, NULL
);
275 if (select(child
.stdout_fd
+1, &read_fds
, NULL
, NULL
, &t
) != 1) {
281 /* t2 is after select. */
282 (void) gettimeofday(&t2
, NULL
);
284 /* Get the time that during select. */
285 timersub(&t2
, &t1
, &t2
);
287 /* This is very unlikely. */
288 if (timercmp(&t2
, &timeout
, >))
291 /* Reduce the timeout. */
292 timersub(&timeout
, &t2
, &timeout
);
294 /* Read dependency data from the script. */
295 length
= read(child
.stdout_fd
, buffer
, sizeof buffer
- 1);
297 /* Did the script close its stdin or exit? */
301 if (data_length
+length
+1 > LINE_MAX
) {
306 /* Grow the data string. */
307 data
= realloc(data
, data_length
+length
);
312 /* Append the buffer to the data string. */
313 strncpy(data
+data_length
, buffer
, length
);
314 data_length
+= length
;
317 /* Terminate the data string. */
318 data
[data_length
] = '\0';
320 /* Close the read end of the pipe. */
321 (void) close(child
.stdout_fd
);
322 /* Kill the script. */
323 if (!wait_for_death(child
.pid
, 0, 500000L))
324 ensure_death(child
.pid
);
332 (void) close(child
.stdout_fd
);
333 if (!wait_for_death(child
.pid
, 0, 500000L))
334 ensure_death(child
.pid
);
340 static bool parse_dependency(char *data
, struct list
*dependency_list
)
342 for (char *saveptr
, *token
= strtok_r(data
, " \r\n", &saveptr
);
344 token
= strtok_r(NULL
, " \r\n", &saveptr
)) {
345 char *s
= strdup(token
);
347 if (s
== NULL
|| !list_append(dependency_list
, s
))