1 /* runner.c - Run and watch the backend engines
2 * Copyright (C) 2009 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
33 #include "../common/exechelp.h"
34 #include "mountinfo.h"
36 /* The runner object. */
39 char *name; /* The name of this runner. */
40 unsigned int identifier; /* The runner identifier. */
42 int spawned; /* True if runner_spawn has been called. */
43 npth_t thread; /* The TID of the runner thread. */
44 runner_t next_running; /* Builds a list of all running threads. */
45 int canceled; /* Set if a cancel has already been send once. */
47 int cancel_flag; /* If set the thread should terminate itself. */
50 /* We use a reference counter to know when it is safe to remove the
51 object. Lacking an explicit ref function this counter will take
52 only these two values:
54 1 = Thread not running or only the thread is still running.
55 2 = Thread is running and someone is holding a reference. */
58 pid_t pid; /* PID of the backend's process (the engine). */
59 int in_fd; /* File descriptors to read from the engine. */
60 int out_fd; /* File descriptors to write to the engine. */
61 engine_handler_fnc_t handler; /* The handler functions. */
62 engine_handler_cleanup_fnc_t handler_cleanup;
63 void *handler_data; /* Private data of HANDLER and HANDLER_CLEANUP. */
65 /* Instead of IN_FD we use an estream. Note that the runner thread
66 may close the stream and set status_fp to NULL at any time. Thus
67 it won't be a good idea to use it while the runner thread is
73 /* The head of the list of all running threads. */
74 static runner_t running_threads;
79 /* Write NBYTES of BUF to file descriptor FD. */
81 writen (int fd, const void *buf, size_t nbytes)
83 size_t nleft = nbytes;
88 nwritten = npth_write (fd, buf, nleft);
97 buf = (const char*)buf + nwritten;
105 check_already_spawned (runner_t runner, const char *funcname)
109 log_error ("BUG: runner already spawned - ignoring call to %s\n",
118 /* Return the number of active threads. */
120 runner_get_threads (void)
125 for (r = running_threads; r; r = r->next_running)
131 /* The public release function. */
133 runner_release (runner_t runner)
140 if (!--runner->refcount)
143 err = mountinfo_del_mount (NULL, NULL, runner->identifier);
145 log_error ("failed to remove mount with rid %u from mtab: %s\n",
146 runner->identifier, gpg_strerror (err));
148 es_fclose (runner->status_fp);
149 if (runner->in_fd != -1)
150 close (runner->in_fd);
151 if (runner->out_fd != -1)
152 close (runner->out_fd);
154 /* Fixme: close the process. */
156 /* Tell the engine to release its data. */
157 if (runner->handler_cleanup)
158 runner->handler_cleanup (runner->handler_data);
160 if (runner->pid != (pid_t)(-1))
162 /* The process has not been cleaned up - do it now. */
163 gnupg_kill_process (runner->pid);
164 /* (Actually we should use the program name and not the
165 arbitrary NAME of the runner object. However it does not
166 matter because that information is only used for
168 gnupg_wait_process (runner->name, runner->pid, 1, NULL);
169 gnupg_release_process (runner->pid);
172 xfree (runner->name);
177 /* Create a new runner context. On success a new runner object is
178 stored at R_RUNNER. On failure NULL is stored at this address and
179 an error code returned. */
181 runner_new (runner_t *r_runner, const char *name)
183 static unsigned int namecounter; /* Global name counter. */
189 runner = xtrycalloc (1, sizeof *runner);
191 return gpg_error_from_syserror ();
193 /* Bump up the namecounter. In case we ever had an overflow we
194 check that this number is currently not in use. The algorithm is
195 a bit lame but should be sufficient because such an wrap is not
196 very likely: Assuming that we do a mount 10 times a second, then
197 we would overwrap on a 32 bit system after 13 years. */
201 for (r = running_threads; r; r = r->next_running)
202 if (r->identifier == namecounter)
207 runner->identifier = namecounter;
208 runner->name = namebuffer = xtryasprintf ("%s-%d", name, namecounter);
212 return gpg_error_from_syserror ();
214 runner->refcount = 1;
215 runner->pid = (pid_t)(-1);
224 /* Return the identifier of RUNNER. */
226 runner_get_rid (runner_t runner)
228 return runner->identifier;
232 /* Find a runner by its rid. Returns the runner object. The caller
233 must release the runner object. */
235 runner_find_by_rid (unsigned int rid)
239 for (r = running_threads; r; r = r->next_running)
240 if (r->identifier == rid)
249 /* A runner usually maintains two file descriptors to control the
250 backend engine. This function is used to set these file
251 descriptors. The function takes ownership of these file
252 descriptors. IN_FD will be used to read from engine and OUT_FD to
253 send data to the engine. */
255 runner_set_fds (runner_t runner, int in_fd, int out_fd)
257 if (check_already_spawned (runner, "runner_set_fds"))
260 if (runner->in_fd != -1)
261 close (runner->in_fd);
262 if (runner->out_fd != -1)
263 close (runner->out_fd);
264 runner->in_fd = in_fd;
265 runner->out_fd = out_fd;
269 /* Set the PID of the backend engine. After this call the engine is
270 owned by the runner object. */
272 runner_set_pid (runner_t runner, pid_t pid)
274 if (check_already_spawned (runner, "runner_set_fds"))
281 /* Register the engine handler fucntions HANDLER and HANDLER_CLEANUP
282 and its private HANDLER_DATA with RUNNER. */
284 runner_set_handler (runner_t runner,
285 engine_handler_fnc_t handler,
286 engine_handler_cleanup_fnc_t handler_cleanup,
289 if (check_already_spawned (runner, "runner_set_handler"))
292 runner->handler = handler;
293 runner->handler_cleanup = handler_cleanup;
294 runner->handler_data = handler_data;
298 /* The thread spawned by runner_spawn. */
300 runner_thread (void *arg)
302 runner_t runner = arg;
305 log_debug ("starting runner thread\n");
306 /* If a status_fp is available, the thread's main task is to read
307 from that stream and invoke the backend's handler function. This
308 is done on a line by line base and the line length is limited to
309 a reasonable value (about 1000 characters). Other work will
310 continue either due to an EOF of the stream or by demand of the
312 if (runner->status_fp)
317 estream_t fp = runner->status_fp;
321 while (!err && !runner->cancel_flag && (c=es_getc (fp)) != EOF)
324 if (pos >= sizeof buffer - 5 || c == '\n')
326 buffer[pos - (c == '\n')] = 0;
328 log_info ("%s%s: %s\n",
329 runner->name, cont_line? "(cont)":"", buffer);
330 /* We handle only complete lines and ignore any stuff we
331 possibly had to truncate. That is - at least for the
332 encfs engine - not an issue because our changes to
333 the tool make sure that only relatively short prompt
334 lines are of interest. */
335 if (!cont_line && runner->handler)
336 err = runner->handler (runner->handler_data,
339 cont_line = (c != '\n');
342 if (!err && runner->cancel_flag)
343 log_debug ("runner thread noticed cancel flag\n");
345 log_debug ("runner thread saw EOF\n");
350 log_info ("%s%s: %s\n",
351 runner->name, cont_line? "(cont)":"", buffer);
352 if (!cont_line && !err && runner->handler)
353 err = runner->handler (runner->handler_data,
356 if (!err && es_ferror (fp))
358 err = gpg_error_from_syserror ();
359 log_error ("error reading from %s: %s\n",
360 runner->name, gpg_strerror (err));
363 runner->status_fp = NULL;
365 log_debug ("runner thread closed status fp\n");
368 /* Now wait for the process to finish. */
369 if (!err && runner->pid != (pid_t)(-1))
373 log_debug ("runner thread waiting ...\n");
374 err = gnupg_wait_process (runner->name, runner->pid, 1, &exitcode);
375 gnupg_release_process (runner->pid);
376 runner->pid = (pid_t)(-1);
378 log_error ("running '%s' failed (exitcode=%d): %s\n",
379 runner->name, exitcode, gpg_strerror (err));
380 log_debug ("runner thread waiting finished\n");
383 /* Get rid of the runner object (note: it is refcounted). */
384 log_debug ("runner thread releasing runner ...\n");
388 for (r = running_threads, rprev = NULL; r; rprev = r, r = r->next_running)
392 running_threads = r->next_running;
394 rprev->next_running = r->next_running;
395 r->next_running = NULL;
399 runner_release (runner);
400 log_debug ("runner thread runner released\n");
406 /* Spawn a new thread to let RUNNER work as a coprocess. */
408 runner_spawn (runner_t runner)
415 if (check_already_spawned (runner, "runner_spawn"))
416 return gpg_error (GPG_ERR_BUG);
418 /* In case we have an input fd, open it as an estream so that the
419 Pth scheduling will work. The stdio functions don't work with
420 Pth because they don't call the pth counterparts of read and
421 write unless linker tricks are used. */
422 if (runner->in_fd != -1)
426 fp = es_fdopen (runner->in_fd, "r");
429 err = gpg_error_from_syserror ();
430 log_error ("can't fdopen pipe for reading: %s\n", gpg_strerror (err));
433 runner->status_fp = fp;
434 runner->in_fd = -1; /* Now owned by status_fp. */
437 npth_attr_init (&tattr);
438 npth_attr_setdetachstate (&tattr, NPTH_CREATE_DETACHED);
440 ret = npth_create (&thread, &tattr, runner_thread, runner);
443 err = gpg_error_from_errno (ret);
444 log_error ("error spawning runner thread: %s\n", gpg_strerror (err));
447 npth_setname_np (thread, runner->name);
449 /* The scheduler has not yet kicked in, thus we can safely set the
450 spawned flag and the tid. */
452 runner->thread = thread;
453 runner->next_running = running_threads;
454 running_threads = runner;
456 npth_attr_destroy (&tattr);
458 /* The runner thread is now runnable. */
464 /* Cancel a running thread. */
466 runner_cancel (runner_t runner)
468 /* Warning: runner_cancel_all has knowledge of this code. */
471 runner->canceled = 1; /* Mark that we canceled this one already. */
472 /* FIXME: This does only work if the thread emits status lines. We
473 need to change the thread to wait on an event. */
474 runner->cancel_flag = 1;
475 /* For now we use the brutal way and kill the process. */
476 gnupg_kill_process (runner->pid);
481 /* Cancel all runner threads. */
483 runner_cancel_all (void)
489 for (r = running_threads; r; r = r->next_running)
490 if (r->spawned && !r->canceled)
500 /* Send a line of data down to the engine. This line may not contain
501 a binary Nul or a LF character. This function is used by the
504 runner_send_line (runner_t runner, const void *data, size_t datalen)
508 if (!runner->spawned)
510 log_error ("BUG: runner for %s not spawned\n", runner->name);
511 err = gpg_error (GPG_ERR_INTERNAL);
513 else if (runner->out_fd == -1)
515 log_error ("no output file descriptor for runner %s\n", runner->name);
516 err = gpg_error (GPG_ERR_EBADF);
518 else if (data && datalen)
520 if (memchr (data, '\n', datalen))
522 log_error ("LF detected in response data\n");
523 err = gpg_error (GPG_ERR_BUG);
525 else if (memchr (data, 0, datalen))
527 log_error ("Nul detected in response data\n");
528 err = gpg_error (GPG_ERR_BUG);
530 else if (writen (runner->out_fd, data, datalen))
531 err = gpg_error_from_syserror ();
535 if (writen (runner->out_fd, "\n", 1))
536 err = gpg_error_from_syserror ();