chiark / gitweb /
dirmngr: Drop useless housekeeping.
[gnupg2.git] / g13 / runner.c
1 /* runner.c - Run and watch the backend engines
2  * Copyright (C) 2009 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <assert.h>
27 #include <npth.h>
28
29 #include "g13.h"
30 #include "i18n.h"
31 #include "keyblob.h"
32 #include "runner.h"
33 #include "../common/exechelp.h"
34 #include "mountinfo.h"
35
36 /* The runner object.  */
37 struct runner_s
38 {
39   char *name;              /* The name of this runner.  */
40   unsigned int identifier; /* The runner identifier.  */
41
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.  */
46
47   int cancel_flag;  /* If set the thread should terminate itself.  */
48
49
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:
53
54      1 = Thread not running or only the thread is still running.
55      2 = Thread is running and someone is holding a reference.  */
56   int refcount;
57
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.  */
64
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
68      running.  */
69   estream_t status_fp;
70 };
71
72
73 /* The head of the list of all running threads.  */
74 static runner_t running_threads;
75
76
77
78 \f
79 /* Write NBYTES of BUF to file descriptor FD. */
80 static int
81 writen (int fd, const void *buf, size_t nbytes)
82 {
83   size_t nleft = nbytes;
84   int nwritten;
85
86   while (nleft > 0)
87     {
88       nwritten = npth_write (fd, buf, nleft);
89       if (nwritten < 0)
90         {
91           if (errno == EINTR)
92             nwritten = 0;
93           else
94             return -1;
95         }
96       nleft -= nwritten;
97       buf = (const char*)buf + nwritten;
98     }
99
100   return 0;
101 }
102
103
104 static int
105 check_already_spawned (runner_t runner, const char *funcname)
106 {
107   if (runner->spawned)
108     {
109       log_error ("BUG: runner already spawned - ignoring call to %s\n",
110                  funcname);
111       return 1;
112     }
113   else
114     return 0;
115 }
116
117
118 /* Return the number of active threads.  */
119 unsigned int
120 runner_get_threads (void)
121 {
122   unsigned int n = 0;
123   runner_t r;
124
125   for (r = running_threads; r; r = r->next_running)
126     n++;
127   return n;
128 }
129
130
131 /* The public release function. */
132 void
133 runner_release (runner_t runner)
134 {
135   gpg_error_t err;
136
137   if (!runner)
138     return;
139
140   if (!--runner->refcount)
141     return;
142
143   err = mountinfo_del_mount (NULL, NULL, runner->identifier);
144   if (err)
145     log_error ("failed to remove mount with rid %u from mtab: %s\n",
146                runner->identifier, gpg_strerror (err));
147
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);
153
154   /* Fixme: close the process. */
155
156   /* Tell the engine to release its data.  */
157   if (runner->handler_cleanup)
158     runner->handler_cleanup (runner->handler_data);
159
160   if (runner->pid != (pid_t)(-1))
161     {
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
167           diagnostics.)  */
168       gnupg_wait_process (runner->name, runner->pid, 1, NULL);
169       gnupg_release_process (runner->pid);
170     }
171
172   xfree (runner->name);
173   xfree (runner);
174 }
175
176
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.  */
180 gpg_error_t
181 runner_new (runner_t *r_runner, const char *name)
182 {
183   static unsigned int namecounter; /* Global name counter.  */
184   char *namebuffer;
185   runner_t runner, r;
186
187   *r_runner = NULL;
188
189   runner = xtrycalloc (1, sizeof *runner);
190   if (!runner)
191     return gpg_error_from_syserror ();
192
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.  */
198   do
199     {
200       namecounter++;
201       for (r = running_threads; r; r = r->next_running)
202         if (r->identifier == namecounter)
203           break;
204     }
205   while (r);
206
207   runner->identifier = namecounter;
208   runner->name = namebuffer = xtryasprintf ("%s-%d", name, namecounter);
209   if (!runner->name)
210     {
211       xfree (runner);
212       return gpg_error_from_syserror ();
213     }
214   runner->refcount = 1;
215   runner->pid = (pid_t)(-1);
216   runner->in_fd = -1;
217   runner->out_fd = -1;
218
219   *r_runner = runner;
220   return 0;
221 }
222
223
224 /* Return the identifier of RUNNER.  */
225 unsigned int
226 runner_get_rid (runner_t runner)
227 {
228   return runner->identifier;
229 }
230
231
232 /* Find a runner by its rid.  Returns the runner object.  The caller
233    must release the runner object.  */
234 runner_t
235 runner_find_by_rid (unsigned int rid)
236 {
237   runner_t r;
238
239   for (r = running_threads; r; r = r->next_running)
240     if (r->identifier == rid)
241       {
242         r->refcount++;
243         return r;
244       }
245   return NULL;
246 }
247
248
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. */
254 void
255 runner_set_fds (runner_t runner, int in_fd, int out_fd)
256 {
257   if (check_already_spawned (runner, "runner_set_fds"))
258     return;
259
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;
266 }
267
268
269 /* Set the PID of the backend engine.  After this call the engine is
270    owned by the runner object.  */
271 void
272 runner_set_pid (runner_t runner, pid_t pid)
273 {
274   if (check_already_spawned (runner, "runner_set_fds"))
275     return;
276
277   runner->pid = pid;
278 }
279
280
281 /* Register the engine handler fucntions HANDLER and HANDLER_CLEANUP
282    and its private HANDLER_DATA with RUNNER.  */
283 void
284 runner_set_handler (runner_t runner,
285                     engine_handler_fnc_t handler,
286                     engine_handler_cleanup_fnc_t handler_cleanup,
287                     void *handler_data)
288 {
289   if (check_already_spawned (runner, "runner_set_handler"))
290     return;
291
292   runner->handler = handler;
293   runner->handler_cleanup = handler_cleanup;
294   runner->handler_data = handler_data;
295 }
296
297
298 /* The thread spawned by runner_spawn.  */
299 static void *
300 runner_thread (void *arg)
301 {
302   runner_t runner = arg;
303   gpg_error_t err = 0;
304
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
311      engine.  */
312   if (runner->status_fp)
313     {
314       int c, cont_line;
315       unsigned int pos;
316       char buffer[1024];
317       estream_t fp = runner->status_fp;
318
319       pos = 0;
320       cont_line = 0;
321       while (!err && !runner->cancel_flag && (c=es_getc (fp)) != EOF)
322         {
323           buffer[pos++] = c;
324           if (pos >= sizeof buffer - 5 || c == '\n')
325             {
326               buffer[pos - (c == '\n')] = 0;
327               if (opt.verbose)
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,
337                                        runner, buffer);
338               pos = 0;
339               cont_line = (c != '\n');
340             }
341         }
342       if (!err && runner->cancel_flag)
343         log_debug ("runner thread noticed cancel flag\n");
344       else
345         log_debug ("runner thread saw EOF\n");
346       if (pos)
347         {
348           buffer[pos] = 0;
349           if (opt.verbose)
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,
354                                           runner, buffer);
355         }
356       if (!err && es_ferror (fp))
357         {
358           err = gpg_error_from_syserror ();
359           log_error ("error reading from %s: %s\n",
360                      runner->name, gpg_strerror (err));
361         }
362
363       runner->status_fp = NULL;
364       es_fclose (fp);
365       log_debug ("runner thread closed status fp\n");
366     }
367
368   /* Now wait for the process to finish.  */
369   if (!err && runner->pid != (pid_t)(-1))
370     {
371       int exitcode;
372
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);
377       if (err)
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");
381     }
382
383   /* Get rid of the runner object (note: it is refcounted).  */
384   log_debug ("runner thread releasing runner ...\n");
385   {
386     runner_t r, rprev;
387
388     for (r = running_threads, rprev = NULL; r; rprev = r, r = r->next_running)
389       if (r == runner)
390         {
391           if (!rprev)
392             running_threads = r->next_running;
393           else
394             rprev->next_running = r->next_running;
395           r->next_running = NULL;
396           break;
397         }
398   }
399   runner_release (runner);
400   log_debug ("runner thread runner released\n");
401
402   return NULL;
403 }
404
405
406 /* Spawn a new thread to let RUNNER work as a coprocess.  */
407 gpg_error_t
408 runner_spawn (runner_t runner)
409 {
410   gpg_error_t err;
411   npth_attr_t tattr;
412   npth_t thread;
413   int ret;
414
415   if (check_already_spawned (runner, "runner_spawn"))
416     return gpg_error (GPG_ERR_BUG);
417
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)
423     {
424       estream_t fp;
425
426       fp = es_fdopen (runner->in_fd, "r");
427       if (!fp)
428         {
429           err = gpg_error_from_syserror ();
430           log_error ("can't fdopen pipe for reading: %s\n", gpg_strerror (err));
431           return err;
432         }
433       runner->status_fp = fp;
434       runner->in_fd = -1;  /* Now owned by status_fp.  */
435     }
436
437   npth_attr_init (&tattr);
438   npth_attr_setdetachstate (&tattr, NPTH_CREATE_DETACHED);
439
440   ret = npth_create (&thread, &tattr, runner_thread, runner);
441   if (ret)
442     {
443       err = gpg_error_from_errno (ret);
444       log_error ("error spawning runner thread: %s\n", gpg_strerror (err));
445       return err;
446     }
447   npth_setname_np (thread, runner->name);
448
449   /* The scheduler has not yet kicked in, thus we can safely set the
450      spawned flag and the tid.  */
451   runner->spawned = 1;
452   runner->thread = thread;
453   runner->next_running = running_threads;
454   running_threads = runner;
455
456   npth_attr_destroy (&tattr);
457
458   /* The runner thread is now runnable.  */
459
460   return 0;
461 }
462
463
464 /* Cancel a running thread.  */
465 void
466 runner_cancel (runner_t runner)
467 {
468   /* Warning: runner_cancel_all has knowledge of this code.  */
469   if (runner->spawned)
470     {
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);
477     }
478 }
479
480
481 /* Cancel all runner threads.  */
482 void
483 runner_cancel_all (void)
484 {
485   runner_t r;
486
487   do
488     {
489       for (r = running_threads; r; r = r->next_running)
490         if (r->spawned && !r->canceled)
491           {
492             runner_cancel (r);
493             break;
494           }
495     }
496   while (r);
497 }
498
499
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
502    engine's handler.  */
503 gpg_error_t
504 runner_send_line (runner_t runner, const void *data, size_t datalen)
505 {
506   gpg_error_t err = 0;
507
508   if (!runner->spawned)
509     {
510       log_error ("BUG: runner for %s not spawned\n", runner->name);
511       err = gpg_error (GPG_ERR_INTERNAL);
512     }
513   else if (runner->out_fd == -1)
514     {
515       log_error ("no output file descriptor for runner %s\n", runner->name);
516       err = gpg_error (GPG_ERR_EBADF);
517     }
518   else if (data && datalen)
519     {
520       if (memchr (data, '\n', datalen))
521         {
522           log_error ("LF detected in response data\n");
523           err = gpg_error (GPG_ERR_BUG);
524         }
525       else if (memchr (data, 0, datalen))
526         {
527           log_error ("Nul detected in response data\n");
528           err = gpg_error (GPG_ERR_BUG);
529         }
530       else if (writen (runner->out_fd, data, datalen))
531         err = gpg_error_from_syserror ();
532     }
533
534   if (!err)
535     if (writen (runner->out_fd, "\n", 1))
536       err = gpg_error_from_syserror ();
537
538   return err;
539 }