GNUnet  0.10.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 
41 
46 
50  const struct GNUNET_MessageHeader *msg;
51 
56 
61 
65  void *cont_cls;
66 
70  unsigned int wpos;
71 };
72 
73 
82 
87 
92 
97 
102 
107 
112 
116  void *cb_cls;
117 
122 
127 
131  char *binary_name;
132 
136  char **binary_argv;
137 
142 
147 
152 
157 
161  unsigned int retry_back_off;
162 };
163 
164 
174 int
176 {
178  int ret;
179 
180  while (NULL != (sh = h->sh_head))
181  {
183  if (NULL != sh->cont)
184  sh->cont(sh->cont_cls, GNUNET_NO);
185  GNUNET_free(sh);
186  }
187  if (NULL != h->restart_task)
188  {
190  h->restart_task = NULL;
191  }
192  if (NULL != h->read_task)
193  {
195  h->read_task = NULL;
196  }
197  if (NULL == h->helper_proc)
198  return GNUNET_SYSERR;
199  if (GNUNET_YES == soft_kill)
200  {
201  /* soft-kill only possible with pipes */
202  GNUNET_assert(NULL != h->helper_in);
204  h->helper_in = NULL;
205  h->fh_to_helper = NULL;
206  return ret;
207  }
209  return GNUNET_SYSERR;
210  return GNUNET_OK;
211 }
212 
213 
222 int
224 {
226  int ret;
227 
228  ret = GNUNET_SYSERR;
229  if (NULL != h->helper_proc)
230  {
233  h->helper_proc = NULL;
234  }
235  if (NULL != h->read_task)
236  {
238  h->read_task = NULL;
239  }
240  if (NULL != h->write_task)
241  {
243  h->write_task = NULL;
244  }
245  if (NULL != h->helper_in)
246  {
248  h->helper_in = NULL;
249  h->fh_to_helper = NULL;
250  }
251  if (NULL != h->helper_out)
252  {
254  h->helper_out = NULL;
255  h->fh_from_helper = NULL;
256  }
257  while (NULL != (sh = h->sh_head))
258  {
260  if (NULL != sh->cont)
261  sh->cont(sh->cont_cls, GNUNET_NO);
262  GNUNET_free(sh);
263  }
264  /* purge MST buffer */
265  if (NULL != h->mst)
266  (void)GNUNET_MST_from_buffer(h->mst, NULL, 0, GNUNET_YES, GNUNET_NO);
267  return ret;
268 }
269 
270 
278 static void
279 stop_helper(struct GNUNET_HELPER_Handle *h, int soft_kill)
280 {
281  if (NULL != h->restart_task)
282  {
284  h->restart_task = NULL;
285  }
286  else
287  {
288  GNUNET_break(GNUNET_OK == GNUNET_HELPER_kill(h, soft_kill));
290  }
291 }
292 
293 
299 static void
300 restart_task(void *cls);
301 
302 
308 static void
309 helper_read(void *cls)
310 {
311  struct GNUNET_HELPER_Handle *h = cls;
313  ssize_t t;
314 
315  h->read_task = NULL;
316  t = GNUNET_DISK_file_read(h->fh_from_helper, &buf, sizeof(buf));
317  if (t < 0)
318  {
319  /* On read-error, restart the helper */
321  _("Error reading from `%s': %s\n"),
322  h->binary_name,
323  strerror(errno));
324  if (NULL != h->exp_cb)
325  {
326  h->exp_cb(h->cb_cls);
328  return;
329  }
331  /* Restart the helper */
334  h->retry_back_off),
335  &restart_task,
336  h);
337  return;
338  }
339  if (0 == t)
340  {
341  /* this happens if the helper is shut down via a
342  signal, so it is not a "hard" error */
344  "Got 0 bytes from helper `%s' (EOF)\n",
345  h->binary_name);
346  if (NULL != h->exp_cb)
347  {
348  h->exp_cb(h->cb_cls);
350  return;
351  }
353  /* Restart the helper */
356  h->retry_back_off),
357  &restart_task,
358  h);
359  return;
360  }
362  "Got %u bytes from helper `%s'\n",
363  (unsigned int)t,
364  h->binary_name);
366  h->fh_from_helper,
367  &helper_read,
368  h);
369  if (GNUNET_SYSERR ==
371  {
373  _("Failed to parse inbound message from helper `%s'\n"),
374  h->binary_name);
375  if (NULL != h->exp_cb)
376  {
377  h->exp_cb(h->cb_cls);
379  return;
380  }
382  /* Restart the helper */
385  h->retry_back_off),
386  &restart_task,
387  h);
388  return;
389  }
390 }
391 
392 
398 static void
400 {
401  h->helper_in =
403  h->helper_out =
405  if ((h->helper_in == NULL) || (h->helper_out == NULL))
406  {
407  /* out of file descriptors? try again later... */
411  h->retry_back_off),
412  &restart_task,
413  h);
414  return;
415  }
417  "Starting HELPER process `%s'\n",
418  h->binary_name);
419  h->fh_from_helper =
421  h->fh_to_helper =
425  h->helper_in,
426  h->helper_out,
427  NULL,
428  h->binary_name,
429  h->binary_argv);
430  if (NULL == h->helper_proc)
431  {
432  /* failed to start process? try again later... */
436  h->retry_back_off),
437  &restart_task,
438  h);
439  return;
440  }
443  if (NULL != h->mst)
445  h->fh_from_helper,
446  &helper_read,
447  h);
448 }
449 
450 
456 static void
457 restart_task(void *cls)
458 {
459  struct GNUNET_HELPER_Handle *h = cls;
460 
461  h->restart_task = NULL;
462  h->retry_back_off++;
464  "Restarting helper with back-off %u\n",
465  h->retry_back_off);
466  start_helper(h);
467 }
468 
469 
486 struct GNUNET_HELPER_Handle *
488  const char *binary_name,
489  char *const binary_argv[],
492  void *cb_cls)
493 {
494  struct GNUNET_HELPER_Handle *h;
495  unsigned int c;
496 
497  h = GNUNET_new(struct GNUNET_HELPER_Handle);
499  /* Lookup in libexec path only if we are starting gnunet helpers */
500  if (NULL != strstr(binary_name, "gnunet"))
502  else
503  h->binary_name = GNUNET_strdup(binary_name);
504  for (c = 0; NULL != binary_argv[c]; c++)
505  ;
506  h->binary_argv = GNUNET_malloc(sizeof(char *) * (c + 1));
507  for (c = 0; NULL != binary_argv[c]; c++)
508  h->binary_argv[c] = GNUNET_strdup(binary_argv[c]);
509  h->binary_argv[c] = NULL;
510  h->cb_cls = cb_cls;
511  if (NULL != cb)
512  h->mst = GNUNET_MST_create(cb, h->cb_cls);
513  h->exp_cb = exp_cb;
514  h->retry_back_off = 0;
515  start_helper(h);
516  return h;
517 }
518 
519 
525 void
527 {
528  unsigned int c;
530 
531  if (NULL != h->write_task)
532  {
534  h->write_task = NULL;
535  }
536  GNUNET_assert(NULL == h->read_task);
537  GNUNET_assert(NULL == h->restart_task);
538  while (NULL != (sh = h->sh_head))
539  {
541  if (NULL != sh->cont)
542  sh->cont(sh->cont_cls, GNUNET_SYSERR);
543  GNUNET_free(sh);
544  }
545  if (NULL != h->mst)
548  for (c = 0; h->binary_argv[c] != NULL; c++)
549  GNUNET_free(h->binary_argv[c]);
551  GNUNET_free(h);
552 }
553 
554 
562 void
564 {
565  h->exp_cb = NULL;
566  stop_helper(h, soft_kill);
568 }
569 
570 
576 static void
577 helper_write(void *cls)
578 {
579  struct GNUNET_HELPER_Handle *h = cls;
581  const char *buf;
582  ssize_t t;
583 
584  h->write_task = NULL;
585  if (NULL == (sh = h->sh_head))
586  {
587  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Helper write had no work!\n");
588  return; /* how did this happen? */
589  }
590  buf = (const char *)sh->msg;
592  &buf[sh->wpos],
593  ntohs(sh->msg->size) - sh->wpos);
594  if (-1 == t)
595  {
596  /* On write-error, restart the helper */
598  _("Error writing to `%s': %s\n"),
599  h->binary_name,
600  strerror(errno));
601  if (NULL != h->exp_cb)
602  {
603  h->exp_cb(h->cb_cls);
605  return;
606  }
608  "Stopping and restarting helper task!\n");
610  /* Restart the helper */
613  h->retry_back_off),
614  &restart_task,
615  h);
616  return;
617  }
619  "Transmitted %u bytes to %s\n",
620  (unsigned int)t,
621  h->binary_name);
622  sh->wpos += t;
623  if (sh->wpos == ntohs(sh->msg->size))
624  {
626  if (NULL != sh->cont)
627  sh->cont(sh->cont_cls, GNUNET_YES);
628  GNUNET_free(sh);
629  }
630  if (NULL != h->sh_head)
631  h->write_task =
633  h->fh_to_helper,
634  &helper_write,
635  h);
636 }
637 
638 
654  const struct GNUNET_MessageHeader *msg,
655  int can_drop,
657  void *cont_cls)
658 {
660  uint16_t mlen;
661 
662  if (NULL == h->fh_to_helper)
663  return NULL;
664  if ((GNUNET_YES == can_drop) && (NULL != h->sh_head))
665  return NULL;
666  mlen = ntohs(msg->size);
667  sh = GNUNET_malloc(sizeof(struct GNUNET_HELPER_SendHandle) + mlen);
668  sh->msg = (const struct GNUNET_MessageHeader *)&sh[1];
669  GNUNET_memcpy(&sh[1], msg, mlen);
670  sh->h = h;
671  sh->cont = cont;
672  sh->cont_cls = cont_cls;
674  if (NULL == h->write_task)
675  h->write_task =
677  h->fh_to_helper,
678  &helper_write,
679  h);
680 
681  return sh;
682 }
683 
691 void
693 {
694  struct GNUNET_HELPER_Handle *h = sh->h;
695 
696  sh->cont = NULL;
697  sh->cont_cls = NULL;
698  if (0 == sh->wpos)
699  {
701  GNUNET_free(sh);
702  if (NULL == h->sh_head)
703  {
705  h->write_task = NULL;
706  }
707  }
708 }
709 
710 
711 /* 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:175
const struct GNUNET_DISK_FileHandle * fh_to_helper
FileHandle to send data to the helper.
Definition: helper.c:96
When this flag is set, the child process will inherit stderr of the parent.
Definition: gnunet_os_lib.h:90
static void restart_task(void *cls)
Restart the helper process.
Definition: helper.c:457
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:286
struct GNUNET_SCHEDULER_Task * restart_task
Restart task.
Definition: helper.c:151
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:729
The handle to a helper process.
Definition: helper.c:77
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:487
unsigned int wpos
Current write position.
Definition: helper.c:70
struct GNUNET_DISK_PipeHandle * helper_out
PipeHandle to send data to the helper.
Definition: helper.c:86
#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:45
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:563
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.
#define GNUNET_NO
Definition: gnunet_common.h:78
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:1647
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:1615
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#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:126
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...
static int ret
Final status code.
Definition: gnunet-arm.c:89
#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:156
#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:1744
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
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:141
struct GNUNET_SCHEDULER_Task * write_task
Task to read from the helper.
Definition: helper.c:146
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:1237
void GNUNET_HELPER_send_cancel(struct GNUNET_HELPER_SendHandle *sh)
Cancel a GNUNET_HELPER_send operation.
Definition: helper.c:692
struct GNUNET_HELPER_Handle * h
The handle to a helper process.
Definition: helper.c:55
void GNUNET_MST_destroy(struct GNUNET_MessageStreamTokenizer *mst)
Destroys a tokenizer.
Definition: mst.c:410
unsigned int retry_back_off
Count start attempts to increase linear back off.
Definition: helper.c:161
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:817
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:81
struct GNUNET_OS_Process * GNUNET_OS_start_process_vap(int pipe_control, 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:607
static char buf[2048]
struct GNUNET_MessageStreamTokenizer * GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
Definition: mst.c:84
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:440
#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:113
struct GNUNET_HELPER_SendHandle * sh_head
First message queued for transmission to helper.
Definition: helper.c:121
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(int blocking_read, int blocking_write, int inherit_read, int inherit_write)
Creates an interprocess channel.
Definition: disk.c:1518
char * binary_name
Binary to run.
Definition: helper.c:131
GNUNET_HELPER_ExceptionCallback exp_cb
The exception callback.
Definition: helper.c:111
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
char ** binary_argv
NULL-terminated list of command-line arguments.
Definition: helper.c:136
#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:116
struct GNUNET_MessageStreamTokenizer * mst
The Message-Tokenizer that tokenizes the messages comming from the helper.
Definition: helper.c:106
#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:399
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:279
int GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1706
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:1635
#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:526
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:1000
struct GNUNET_OS_Process * helper_proc
The process id of the helper.
Definition: helper.c:101
struct GNUNET_HELPER_SendHandle * next
This is an entry in a DLL.
Definition: helper.c:40
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:653
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:131
int GNUNET_HELPER_wait(struct GNUNET_HELPER_Handle *h)
Reap the helper process.
Definition: helper.c:223
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.
#define GNUNET_YES
Definition: gnunet_common.h:77
static void helper_read(void *cls)
Read from the helper-process.
Definition: helper.c:309
#define GNUNET_TERM_SIG
The termination signal.
Definition: platform.h:242
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:91
#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:50
GNUNET_HELPER_Continuation cont
Function to call upon completion.
Definition: helper.c:60
static void helper_write(void *cls)
Write to the helper-process.
Definition: helper.c:577
void * cont_cls
Closure to &#39;cont&#39;.
Definition: helper.c:65
#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:224
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956