GNUnet  0.11.x
helper.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2011, 2012 Christian Grothoff
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_mst_lib.h"
31 
32 
37 {
42 
47 
51  const struct GNUNET_MessageHeader *msg;
52 
57 
62 
66  void *cont_cls;
67 
71  unsigned int wpos;
72 };
73 
74 
79 {
84 
89 
94 
99 
104 
109 
114 
118  void *cb_cls;
119 
124 
129 
133  char *binary_name;
134 
138  char **binary_argv;
139 
144 
149 
154 
159 
163  unsigned int retry_back_off;
164 };
165 
166 
176 int
177 GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill)
178 {
180  int ret;
181 
182  while (NULL != (sh = h->sh_head))
183  {
185  if (NULL != sh->cont)
186  sh->cont (sh->cont_cls, GNUNET_NO);
187  GNUNET_free (sh);
188  }
189  if (NULL != h->restart_task)
190  {
192  h->restart_task = NULL;
193  }
194  if (NULL != h->read_task)
195  {
197  h->read_task = NULL;
198  }
199  if (NULL == h->helper_proc)
200  return GNUNET_SYSERR;
201  if (GNUNET_YES == soft_kill)
202  {
203  /* soft-kill only possible with pipes */
204  GNUNET_assert (NULL != h->helper_in);
206  h->helper_in = NULL;
207  h->fh_to_helper = NULL;
208  return ret;
209  }
211  return GNUNET_SYSERR;
212  return GNUNET_OK;
213 }
214 
215 
224 int
226 {
228  int ret;
229 
230  ret = GNUNET_SYSERR;
231  if (NULL != h->helper_proc)
232  {
235  h->helper_proc = NULL;
236  }
237  if (NULL != h->read_task)
238  {
240  h->read_task = NULL;
241  }
242  if (NULL != h->write_task)
243  {
245  h->write_task = NULL;
246  }
247  if (NULL != h->helper_in)
248  {
250  h->helper_in = NULL;
251  h->fh_to_helper = NULL;
252  }
253  if (NULL != h->helper_out)
254  {
256  h->helper_out = NULL;
257  h->fh_from_helper = NULL;
258  }
259  while (NULL != (sh = h->sh_head))
260  {
262  if (NULL != sh->cont)
263  sh->cont (sh->cont_cls, GNUNET_NO);
264  GNUNET_free (sh);
265  }
266  /* purge MST buffer */
267  if (NULL != h->mst)
268  (void) GNUNET_MST_from_buffer (h->mst, NULL, 0, GNUNET_YES, GNUNET_NO);
269  return ret;
270 }
271 
272 
280 static void
281 stop_helper (struct GNUNET_HELPER_Handle *h, int soft_kill)
282 {
283  if (NULL != h->restart_task)
284  {
286  h->restart_task = NULL;
287  }
288  else
289  {
290  GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill));
292  }
293 }
294 
295 
301 static void
302 restart_task (void *cls);
303 
304 
310 static void
311 helper_read (void *cls)
312 {
313  struct GNUNET_HELPER_Handle *h = cls;
315  ssize_t t;
316 
317  h->read_task = NULL;
318  t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof(buf));
319  if (t < 0)
320  {
321  /* On read-error, restart the helper */
323  _ ("Error reading from `%s': %s\n"),
324  h->binary_name,
325  strerror (errno));
326  if (NULL != h->exp_cb)
327  {
328  h->exp_cb (h->cb_cls);
330  return;
331  }
332  stop_helper (h, GNUNET_NO);
333  /* Restart the helper */
336  h->retry_back_off),
337  &restart_task,
338  h);
339  return;
340  }
341  if (0 == t)
342  {
343  /* this happens if the helper is shut down via a
344  signal, so it is not a "hard" error */
346  "Got 0 bytes from helper `%s' (EOF)\n",
347  h->binary_name);
348  if (NULL != h->exp_cb)
349  {
350  h->exp_cb (h->cb_cls);
352  return;
353  }
354  stop_helper (h, GNUNET_NO);
355  /* Restart the helper */
358  h->retry_back_off),
359  &restart_task,
360  h);
361  return;
362  }
364  "Got %u bytes from helper `%s'\n",
365  (unsigned int) t,
366  h->binary_name);
368  h->fh_from_helper,
369  &helper_read,
370  h);
371  if (GNUNET_SYSERR ==
373  {
375  _ ("Failed to parse inbound message from helper `%s'\n"),
376  h->binary_name);
377  if (NULL != h->exp_cb)
378  {
379  h->exp_cb (h->cb_cls);
381  return;
382  }
383  stop_helper (h, GNUNET_NO);
384  /* Restart the helper */
387  h->retry_back_off),
388  &restart_task,
389  h);
390  return;
391  }
392 }
393 
394 
400 static void
402 {
403  h->helper_in =
405  h->helper_out =
407  if ((h->helper_in == NULL) || (h->helper_out == NULL))
408  {
409  /* out of file descriptors? try again later... */
410  stop_helper (h, GNUNET_NO);
413  h->retry_back_off),
414  &restart_task,
415  h);
416  return;
417  }
419  "Starting HELPER process `%s'\n",
420  h->binary_name);
421  h->fh_from_helper =
423  h->fh_to_helper =
429  h->helper_in,
430  h->helper_out,
431  NULL,
432  h->binary_name,
433  h->binary_argv);
434  if (NULL == h->helper_proc)
435  {
436  /* failed to start process? try again later... */
437  stop_helper (h, GNUNET_NO);
440  h->retry_back_off),
441  &restart_task,
442  h);
443  return;
444  }
447  if (NULL != h->mst)
449  h->fh_from_helper,
450  &helper_read,
451  h);
452 }
453 
454 
460 static void
461 restart_task (void *cls)
462 {
463  struct GNUNET_HELPER_Handle *h = cls;
464 
465  h->restart_task = NULL;
466  h->retry_back_off++;
468  "Restarting helper with back-off %u\n",
469  h->retry_back_off);
470  start_helper (h);
471 }
472 
473 
490 struct GNUNET_HELPER_Handle *
492  const char *binary_name,
493  char *const binary_argv[],
496  void *cb_cls)
497 {
498  struct GNUNET_HELPER_Handle *h;
499  unsigned int c;
500 
501  h = GNUNET_new (struct GNUNET_HELPER_Handle);
503  /* Lookup in libexec path only if we are starting gnunet helpers */
504  if (NULL != strstr (binary_name, "gnunet"))
506  else
507  h->binary_name = GNUNET_strdup (binary_name);
508  for (c = 0; NULL != binary_argv[c]; c++)
509  ;
510  h->binary_argv = GNUNET_malloc (sizeof(char *) * (c + 1));
511  for (c = 0; NULL != binary_argv[c]; c++)
512  h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
513  h->binary_argv[c] = NULL;
514  h->cb_cls = cb_cls;
515  if (NULL != cb)
516  h->mst = GNUNET_MST_create (cb, h->cb_cls);
517  h->exp_cb = exp_cb;
518  h->retry_back_off = 0;
519  start_helper (h);
520  return h;
521 }
522 
523 
529 void
531 {
532  unsigned int c;
534 
535  if (NULL != h->write_task)
536  {
538  h->write_task = NULL;
539  }
540  GNUNET_assert (NULL == h->read_task);
541  GNUNET_assert (NULL == h->restart_task);
542  while (NULL != (sh = h->sh_head))
543  {
545  if (NULL != sh->cont)
546  sh->cont (sh->cont_cls, GNUNET_SYSERR);
547  GNUNET_free (sh);
548  }
549  if (NULL != h->mst)
550  GNUNET_MST_destroy (h->mst);
552  for (c = 0; h->binary_argv[c] != NULL; c++)
553  GNUNET_free (h->binary_argv[c]);
555  GNUNET_free (h);
556 }
557 
558 
566 void
567 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h, int soft_kill)
568 {
569  h->exp_cb = NULL;
570  stop_helper (h, soft_kill);
572 }
573 
574 
580 static void
581 helper_write (void *cls)
582 {
583  struct GNUNET_HELPER_Handle *h = cls;
585  const char *buf;
586  ssize_t t;
587 
588  h->write_task = NULL;
589  if (NULL == (sh = h->sh_head))
590  {
591  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Helper write had no work!\n");
592  return; /* how did this happen? */
593  }
594  buf = (const char *) sh->msg;
596  &buf[sh->wpos],
597  ntohs (sh->msg->size) - sh->wpos);
598  if (-1 == t)
599  {
600  /* On write-error, restart the helper */
602  _ ("Error writing to `%s': %s\n"),
603  h->binary_name,
604  strerror (errno));
605  if (NULL != h->exp_cb)
606  {
607  h->exp_cb (h->cb_cls);
609  return;
610  }
612  "Stopping and restarting helper task!\n");
613  stop_helper (h, GNUNET_NO);
614  /* Restart the helper */
617  h->retry_back_off),
618  &restart_task,
619  h);
620  return;
621  }
623  "Transmitted %u bytes to %s\n",
624  (unsigned int) t,
625  h->binary_name);
626  sh->wpos += t;
627  if (sh->wpos == ntohs (sh->msg->size))
628  {
630  if (NULL != sh->cont)
631  sh->cont (sh->cont_cls, GNUNET_YES);
632  GNUNET_free (sh);
633  }
634  if (NULL != h->sh_head)
635  h->write_task =
637  h->fh_to_helper,
638  &helper_write,
639  h);
640 }
641 
642 
658  const struct GNUNET_MessageHeader *msg,
659  int can_drop,
661  void *cont_cls)
662 {
664  uint16_t mlen;
665 
666  if (NULL == h->fh_to_helper)
667  return NULL;
668  if ((GNUNET_YES == can_drop) && (NULL != h->sh_head))
669  return NULL;
670  mlen = ntohs (msg->size);
671  sh = GNUNET_malloc (sizeof(struct GNUNET_HELPER_SendHandle) + mlen);
672  sh->msg = (const struct GNUNET_MessageHeader *) &sh[1];
673  GNUNET_memcpy (&sh[1], msg, mlen);
674  sh->h = h;
675  sh->cont = cont;
676  sh->cont_cls = cont_cls;
678  if (NULL == h->write_task)
679  h->write_task =
681  h->fh_to_helper,
682  &helper_write,
683  h);
684 
685  return sh;
686 }
687 
688 
696 void
698 {
699  struct GNUNET_HELPER_Handle *h = sh->h;
700 
701  sh->cont = NULL;
702  sh->cont_cls = NULL;
703  if (0 == sh->wpos)
704  {
706  GNUNET_free (sh);
707  if (NULL == h->sh_head)
708  {
710  h->write_task = NULL;
711  }
712  }
713 }
714 
715 
716 /* end of helper.c */
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
int GNUNET_HELPER_kill(struct GNUNET_HELPER_Handle *h, int soft_kill)
Sends termination signal to the helper process.
Definition: helper.c:177
const struct GNUNET_DISK_FileHandle * fh_to_helper
FileHandle to send data to the helper.
Definition: helper.c:98
When this flag is set, the child process will inherit stderr of the parent.
Definition: gnunet_os_lib.h:91
struct GNUNET_OS_Process * GNUNET_OS_start_process_vap(enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename, char *const argv[])
Start a process.
Definition: os_priority.c:604
static void restart_task(void *cls)
Restart the helper process.
Definition: helper.c:461
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:287
struct GNUNET_SCHEDULER_Task * restart_task
Restart task.
Definition: helper.c:153
void(* GNUNET_HELPER_ExceptionCallback)(void *cls)
Callback that will be called when the helper process dies.
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:732
The handle to a helper process.
Definition: helper.c:78
struct GNUNET_HELPER_Handle * GNUNET_HELPER_start(int with_control_pipe, const char *binary_name, char *const binary_argv[], GNUNET_MessageTokenizerCallback cb, GNUNET_HELPER_ExceptionCallback exp_cb, void *cb_cls)
Starts a helper and begins reading from it.
Definition: helper.c:491
unsigned int wpos
Current write position.
Definition: helper.c:71
struct GNUNET_DISK_PipeHandle * helper_out
PipeHandle to send data to the helper.
Definition: helper.c:88
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_HELPER_SendHandle * prev
This is an entry in a DLL.
Definition: helper.c:46
void(* GNUNET_HELPER_Continuation)(void *cls, int result)
Continuation function.
#define GNUNET_TIME_UNIT_SECONDS
One second.
void GNUNET_HELPER_stop(struct GNUNET_HELPER_Handle *h, int soft_kill)
Kills the helper, closes the pipe and frees the handle.
Definition: helper.c:567
Entry in the queue of messages we need to transmit to the helper.
Definition: helper.c:36
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_write_file(struct GNUNET_TIME_Relative delay, const struct GNUNET_DISK_FileHandle *wfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1700
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_file(struct GNUNET_TIME_Relative delay, const struct GNUNET_DISK_FileHandle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1667
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_HELPER_SendHandle * sh_tail
Last message queued for transmission to helper.
Definition: helper.c:128
static struct GNUNET_SCHEDULER_Task * t
Main task.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
int with_control_pipe
Does the helper support the use of a control pipe for signalling?
Definition: helper.c:158
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
const struct GNUNET_DISK_FileHandle * GNUNET_DISK_pipe_handle(const struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd n)
Get the handle to a particular pipe end.
Definition: disk.c:1672
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
int(* GNUNET_MessageTokenizerCallback)(void *cls, const struct GNUNET_MessageHeader *message)
Functions with this signature are called whenever a complete message is received by the tokenizer...
struct GNUNET_SCHEDULER_Task * read_task
Task to read from the helper.
Definition: helper.c:143
struct GNUNET_SCHEDULER_Task * write_task
Task to read from the helper.
Definition: helper.c:148
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1269
void GNUNET_HELPER_send_cancel(struct GNUNET_HELPER_SendHandle *sh)
Cancel a GNUNET_HELPER_send operation.
Definition: helper.c:697
struct GNUNET_HELPER_Handle * h
The handle to a helper process.
Definition: helper.c:56
void GNUNET_MST_destroy(struct GNUNET_MessageStreamTokenizer *mst)
Destroys a tokenizer.
Definition: mst.c:411
unsigned int retry_back_off
Count start attempts to increase linear back off.
Definition: helper.c:163
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:820
static struct SolverHandle * sh
Handle to a message stream tokenizer.
Definition: mst.c:43
struct GNUNET_DISK_PipeHandle * helper_in
PipeHandle to receive data from the helper.
Definition: helper.c:83
static char buf[2048]
struct GNUNET_MessageStreamTokenizer * GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
Definition: mst.c:85
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition: time.c:442
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
The writing-end of a pipe.
The reading-end of a pipe.
int GNUNET_MST_from_buffer(struct GNUNET_MessageStreamTokenizer *mst, const char *buf, size_t size, int purge, int one_shot)
Add incoming data to the receive buffer and call the callback for all complete messages.
Definition: mst.c:114
struct GNUNET_HELPER_SendHandle * sh_head
First message queued for transmission to helper.
Definition: helper.c:123
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(enum GNUNET_DISK_PipeFlags pf)
Creates an interprocess channel.
Definition: disk.c:1456
char * binary_name
Binary to run.
Definition: helper.c:133
GNUNET_HELPER_ExceptionCallback exp_cb
The exception callback.
Definition: helper.c:113
char ** binary_argv
NULL-terminated list of command-line arguments.
Definition: helper.c:138
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
void * cb_cls
The closure for callbacks.
Definition: helper.c:118
struct GNUNET_MessageStreamTokenizer * mst
The Message-Tokenizer that tokenizes the messages coming from the helper.
Definition: helper.c:108
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
static void start_helper(struct GNUNET_HELPER_Handle *h)
Start the helper process.
Definition: helper.c:401
static void stop_helper(struct GNUNET_HELPER_Handle *h, int soft_kill)
Stop the helper process, we&#39;re closing down or had an error.
Definition: helper.c:281
int GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1634
int GNUNET_DISK_pipe_close_end(struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd end)
Closes one half of an interprocess channel.
Definition: disk.c:1562
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to &#39;struct&#39;s...
void GNUNET_HELPER_destroy(struct GNUNET_HELPER_Handle *h)
Free&#39;s the resources occupied by the helper handle.
Definition: helper.c:530
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:983
struct GNUNET_OS_Process * helper_proc
The process id of the helper.
Definition: helper.c:103
struct GNUNET_HELPER_SendHandle * next
This is an entry in a DLL.
Definition: helper.c:41
struct GNUNET_HELPER_SendHandle * GNUNET_HELPER_send(struct GNUNET_HELPER_Handle *h, const struct GNUNET_MessageHeader *msg, int can_drop, GNUNET_HELPER_Continuation cont, void *cont_cls)
Send an message to the helper.
Definition: helper.c:657
#define GNUNET_log(kind,...)
Configure both pipe ends for blocking operations if set.
Entry in list of pending tasks.
Definition: scheduler.c:134
int GNUNET_HELPER_wait(struct GNUNET_HELPER_Handle *h)
Reap the helper process.
Definition: helper.c:225
char * GNUNET_OS_get_libexec_binary_path(const char *progname)
Given the name of a gnunet-helper, gnunet-service or gnunet-daemon binary, try to prefix it with the ...
Header for all communications.
static void helper_read(void *cls)
Read from the helper-process.
Definition: helper.c:311
#define GNUNET_TERM_SIG
The termination signal.
Definition: platform.h:246
Should a pipe be used to send signals to the child?
Handle used to access files (and pipes).
Handle used to manage a pipe.
Definition: disk.c:68
const struct GNUNET_DISK_FileHandle * fh_from_helper
FileHandle to receive data from the helper.
Definition: helper.c:93
#define GNUNET_malloc(size)
Wrapper around malloc.
const struct GNUNET_MessageHeader * msg
Message to transmit (allocated at the end of this struct)
Definition: helper.c:51
GNUNET_HELPER_Continuation cont
Function to call upon completion.
Definition: helper.c:61
static void helper_write(void *cls)
Write to the helper-process.
Definition: helper.c:581
void * cont_cls
Closure to &#39;cont&#39;.
Definition: helper.c:66
#define GNUNET_free(ptr)
Wrapper around free.
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:225
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972