GNUnet  0.11.x
Data Structures | Macros | Functions | Variables
os_priority.c File Reference

Methods to set process priority. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "disk.h"
#include <unistr.h>
Include dependency graph for os_priority.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_OS_Process
 
struct  GNUNET_OS_CommandHandle
 Handle to a command. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-os-priority", __VA_ARGS__)
 
#define LOG_STRERROR(kind, syscall)   GNUNET_log_from_strerror (kind, "util-os-priority", syscall)
 
#define LOG_STRERROR_FILE(kind, syscall, filename)   GNUNET_log_from_strerror_file (kind, "util-os-priority", syscall, filename)
 
#define GNUNET_OS_CONTROL_PIPE   "GNUNET_OS_CONTROL_PIPE"
 

Functions

static void shutdown_pch (void *cls)
 This handler is called on shutdown to remove the pch. More...
 
static void parent_control_handler (void *cls)
 This handler is called when there are control data to be read on the pipe. More...
 
void GNUNET_OS_install_parent_control_handler (void *cls)
 Task that connects this process to its parent via pipe; essentially, the parent control handler will read signal numbers from the GNUNET_OS_CONTROL_PIPE (as given in an environment variable) and raise those signals. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_process_current ()
 Get process structure for current process. More...
 
int GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
 Sends a signal to the process. More...
 
pid_t GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc)
 Get the pid of the process in question. More...
 
void GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
 Cleans up process structure contents (OS-dependent) and deallocates it. More...
 
static void open_dev_null (int target_fd, int flags)
 Open '/dev/null' and make the result the given file descriptor. More...
 
static struct GNUNET_OS_Processstart_process (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 int *lsocks, const char *filename, char *const argv[])
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_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. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_va (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, va_list va)
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process (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,...)
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_v (enum GNUNET_OS_InheritStdioFlags std_inheritance, const int *lsocks, const char *filename, char *const argv[])
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_s (enum GNUNET_OS_InheritStdioFlags std_inheritance, const int *lsocks, const char *filename,...)
 Start a process. More...
 
static int process_status (struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code, int options)
 Retrieve the status of a process, waiting on it if dead. More...
 
int GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
 Retrieve the status of a process. More...
 
int GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
 Retrieve the status of a process, waiting on it if dead. More...
 
int GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
 Wait for a process to terminate. More...
 
void GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
 Stop/kill a command. More...
 
static void cmd_read (void *cls)
 Read from the process and call the line processor. More...
 
struct GNUNET_OS_CommandHandleGNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls, struct GNUNET_TIME_Relative timeout, const char *binary,...)
 Run the given command line and call the given function for each line of the output. More...
 

Variables

static struct GNUNET_OS_Process current_process
 Handle for 'this' process. More...
 
static struct GNUNET_SCHEDULER_Taskpch
 Handle for the parent_control_handler() Task. More...
 
static struct GNUNET_SCHEDULER_Taskspch
 Handle for the shutdown_pch() Task. More...
 

Detailed Description

Methods to set process priority.

Author
Nils Durner

Definition in file os_priority.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-os-priority", __VA_ARGS__)

◆ LOG_STRERROR

#define LOG_STRERROR (   kind,
  syscall 
)    GNUNET_log_from_strerror (kind, "util-os-priority", syscall)

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  syscall,
  filename 
)    GNUNET_log_from_strerror_file (kind, "util-os-priority", syscall, filename)

Definition at line 37 of file os_priority.c.

Referenced by GNUNET_OS_install_parent_control_handler(), and start_process().

◆ GNUNET_OS_CONTROL_PIPE

#define GNUNET_OS_CONTROL_PIPE   "GNUNET_OS_CONTROL_PIPE"

Function Documentation

◆ shutdown_pch()

static void shutdown_pch ( void *  cls)
static

This handler is called on shutdown to remove the pch.

Parameters
clsthe struct GNUNET_DISK_FileHandle of the control pipe

Definition at line 81 of file os_priority.c.

References GNUNET_OS_Process::control_pipe, GNUNET_DISK_file_close(), and GNUNET_SCHEDULER_cancel().

Referenced by GNUNET_OS_install_parent_control_handler().

82 {
83  struct GNUNET_DISK_FileHandle *control_pipe = cls;
84 
86  pch = NULL;
87  GNUNET_DISK_file_close (control_pipe);
88  control_pipe = NULL;
89 }
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1280
static struct GNUNET_SCHEDULER_Task * pch
Handle for the parent_control_handler() Task.
Definition: os_priority.c:67
Handle used to access files (and pipes).
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parent_control_handler()

static void parent_control_handler ( void *  cls)
static

This handler is called when there are control data to be read on the pipe.

Parameters
clsthe struct GNUNET_DISK_FileHandle of the control pipe

Definition at line 98 of file os_priority.c.

References GNUNET_OS_Process::control_pipe, getenv(), GNUNET_assert, GNUNET_DISK_file_close(), GNUNET_DISK_file_read(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_OS_CONTROL_PIPE, GNUNET_SCHEDULER_add_read_file(), GNUNET_SCHEDULER_cancel(), GNUNET_SIGNAL_raise(), GNUNET_TIME_UNIT_FOREVER_REL, LOG, LOG_STRERROR, and ret.

Referenced by GNUNET_OS_install_parent_control_handler().

99 {
100  struct GNUNET_DISK_FileHandle *control_pipe = cls;
101  char sig;
102  char *pipe_fd;
103  ssize_t ret;
104 
105  pch = NULL;
106  ret = GNUNET_DISK_file_read (control_pipe, &sig, sizeof(sig));
107  if (sizeof(sig) != ret)
108  {
109  if (-1 == ret)
110  LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
111  LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing control pipe\n");
112  GNUNET_DISK_file_close (control_pipe);
113  control_pipe = NULL;
115  spch = NULL;
116  return;
117  }
118  pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE);
119  GNUNET_assert ((NULL == pipe_fd) || (strlen (pipe_fd) <= 0));
121  "Got control code %d from parent via pipe %s\n",
122  sig,
123  pipe_fd);
125  control_pipe,
127  control_pipe);
128  GNUNET_SIGNAL_raise ((int) sig);
129 }
void GNUNET_SIGNAL_raise(const int sig)
Raise the given signal by calling the installed signal handlers.
Definition: signal.c:97
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1280
#define GNUNET_OS_CONTROL_PIPE
Definition: os_priority.c:40
#define LOG_STRERROR(kind, syscall)
Definition: os_priority.c:34
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
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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
static struct GNUNET_SCHEDULER_Task * spch
Handle for the shutdown_pch() Task.
Definition: os_priority.c:72
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
char * getenv()
static struct GNUNET_SCHEDULER_Task * pch
Handle for the parent_control_handler() Task.
Definition: os_priority.c:67
static void parent_control_handler(void *cls)
This handler is called when there are control data to be read on the pipe.
Definition: os_priority.c:98
Handle used to access files (and pipes).
#define LOG(kind,...)
Definition: os_priority.c:32
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
Here is the call graph for this function:
Here is the caller graph for this function:

◆ open_dev_null()

static void open_dev_null ( int  target_fd,
int  flags 
)
static

Open '/dev/null' and make the result the given file descriptor.

Parameters
target_fddesired FD to point to /dev/null
flagsopen flags (O_RDONLY, O_WRONLY)

Definition at line 304 of file os_priority.c.

References GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_log_strerror, and GNUNET_log_strerror_file.

Referenced by start_process().

305 {
306  int fd;
307 
308  fd = open ("/dev/null", flags);
309  if (-1 == fd)
310  {
311  GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", "/dev/null");
312  return;
313  }
314  if (fd == target_fd)
315  return;
316  if (-1 == dup2 (fd, target_fd))
317  {
319  (void) close (fd);
320  return;
321  }
322  GNUNET_break (0 == close (fd));
323 }
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
int fd
File handle on Unix-like systems.
Here is the caller graph for this function:

◆ start_process()

static struct GNUNET_OS_Process* start_process ( 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 int *  lsocks,
const char *  filename,
char *const  argv[] 
)
static

Start a process.

Parameters
std_inheritancea set of GNUNET_OS_INHERIT_STD_* flags controlling which std handles of the parent are inherited by the child. pipe_stdin and pipe_stdout take priority over std_inheritance (when they are non-NULL).
pipe_stdinpipe to use to send input to child process (or NULL)
pipe_stdoutpipe to use to get output from child process (or NULL)
pipe_stderrpipe to use for stderr for child process (or NULL)
lsocksarray of listen sockets to dup systemd-style (or NULL); must be NULL on platforms where dup is not supported
filenamename of the binary
argvNULL-terminated list of arguments to the process
Returns
process ID of the new process, -1 on error

Definition at line 343 of file os_priority.c.

References GNUNET_OS_Process::control_pipe, GNUNET_array_append, GNUNET_array_grow, GNUNET_assert, GNUNET_break, GNUNET_DISK_file_close(), GNUNET_DISK_internal_file_handle_(), GNUNET_DISK_PF_NONE, GNUNET_DISK_pipe(), GNUNET_DISK_pipe_close(), GNUNET_DISK_pipe_detach_end(), GNUNET_DISK_PIPE_END_READ, GNUNET_DISK_PIPE_END_WRITE, GNUNET_DISK_pipe_handle(), GNUNET_ERROR_TYPE_ERROR, GNUNET_new, GNUNET_NO, GNUNET_OK, GNUNET_OS_check_helper_binary(), GNUNET_OS_CONTROL_PIPE, GNUNET_OS_INHERIT_STD_ERR, GNUNET_OS_INHERIT_STD_IN, GNUNET_OS_INHERIT_STD_OUT, GNUNET_OS_USE_PIPE_CONTROL, GNUNET_snprintf(), GNUNET_SYSERR, LOG_STRERROR, LOG_STRERROR_FILE, ls, open_dev_null(), GNUNET_OS_Process::pid, and ret.

Referenced by GNUNET_OS_start_process_v(), and GNUNET_OS_start_process_vap().

350 {
351  pid_t ret;
352  char fds[16];
353  struct GNUNET_OS_Process *gnunet_proc;
354  struct GNUNET_DISK_FileHandle *childpipe_read;
355  struct GNUNET_DISK_FileHandle *childpipe_write;
356  int childpipe_read_fd;
357  int i;
358  int j;
359  int k;
360  int tgt;
361  int flags;
362  int *lscp;
363  unsigned int ls;
364  int fd_stdout_write;
365  int fd_stdout_read;
366  int fd_stderr_write;
367  int fd_stderr_read;
368  int fd_stdin_read;
369  int fd_stdin_write;
370 
371  if (GNUNET_SYSERR ==
373  return NULL; /* not executable */
374  if (0 != (std_inheritance & GNUNET_OS_USE_PIPE_CONTROL))
375  {
376  struct GNUNET_DISK_PipeHandle *childpipe;
377  int dup_childpipe_read_fd = -1;
378 
380  if (NULL == childpipe)
381  return NULL;
382  childpipe_read =
384  childpipe_write =
386  GNUNET_DISK_pipe_close (childpipe);
387  if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
388  (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
389  &childpipe_read_fd,
390  sizeof(int))) ||
391  (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
392  {
393  if (NULL != childpipe_read)
394  GNUNET_DISK_file_close (childpipe_read);
395  if (NULL != childpipe_write)
396  GNUNET_DISK_file_close (childpipe_write);
397  if (0 <= dup_childpipe_read_fd)
398  close (dup_childpipe_read_fd);
399  return NULL;
400  }
401  childpipe_read_fd = dup_childpipe_read_fd;
402  GNUNET_DISK_file_close (childpipe_read);
403  }
404  else
405  {
406  childpipe_write = NULL;
407  childpipe_read_fd = -1;
408  }
409  if (NULL != pipe_stdin)
410  {
411  GNUNET_assert (
412  GNUNET_OK ==
415  &fd_stdin_read,
416  sizeof(int)));
417  GNUNET_assert (
418  GNUNET_OK ==
421  &fd_stdin_write,
422  sizeof(int)));
423  }
424  if (NULL != pipe_stdout)
425  {
426  GNUNET_assert (
427  GNUNET_OK ==
430  &fd_stdout_write,
431  sizeof(int)));
432  GNUNET_assert (
433  GNUNET_OK ==
436  &fd_stdout_read,
437  sizeof(int)));
438  }
439  if (NULL != pipe_stderr)
440  {
441  GNUNET_assert (
442  GNUNET_OK ==
445  &fd_stderr_read,
446  sizeof(int)));
447  GNUNET_assert (
448  GNUNET_OK ==
451  &fd_stderr_write,
452  sizeof(int)));
453  }
454  lscp = NULL;
455  ls = 0;
456  if (NULL != lsocks)
457  {
458  i = 0;
459  while (-1 != (k = lsocks[i++]))
460  GNUNET_array_append (lscp, ls, k);
461  GNUNET_array_append (lscp, ls, -1);
462  }
463 #if DARWIN
464  /* see https://web.archive.org/web/20150924082249/gnunet.org/vfork */
465  ret = vfork ();
466 #else
467  ret = fork ();
468 #endif
469  if (-1 == ret)
470  {
471  int eno = errno;
473  GNUNET_array_grow (lscp, ls, 0);
474  if (NULL != childpipe_write)
475  GNUNET_DISK_file_close (childpipe_write);
476  if (0 <= childpipe_read_fd)
477  close (childpipe_read_fd);
478  errno = eno;
479  return NULL;
480  }
481  if (0 != ret)
482  {
483  unsetenv (GNUNET_OS_CONTROL_PIPE);
484  gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
485  gnunet_proc->pid = ret;
486  gnunet_proc->control_pipe = childpipe_write;
487  if (0 != (std_inheritance & GNUNET_OS_USE_PIPE_CONTROL))
488  {
489  close (childpipe_read_fd);
490  }
491  GNUNET_array_grow (lscp, ls, 0);
492  return gnunet_proc;
493  }
494  if (0 <= childpipe_read_fd)
495  {
496  char fdbuf[100];
497 #ifndef DARWIN
498  /* due to vfork, we must NOT free memory on DARWIN! */
499  GNUNET_DISK_file_close (childpipe_write);
500 #endif
501  snprintf (fdbuf, 100, "%x", childpipe_read_fd);
502  setenv (GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
503  }
504  else
505  unsetenv (GNUNET_OS_CONTROL_PIPE);
506  if (NULL != pipe_stdin)
507  {
508  GNUNET_break (0 == close (fd_stdin_write));
509  if (-1 == dup2 (fd_stdin_read, 0))
511  GNUNET_break (0 == close (fd_stdin_read));
512  }
513  else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
514  {
515  GNUNET_break (0 == close (0));
516  open_dev_null (0, O_RDONLY);
517  }
518  if (NULL != pipe_stdout)
519  {
520  GNUNET_break (0 == close (fd_stdout_read));
521  if (-1 == dup2 (fd_stdout_write, 1))
523  GNUNET_break (0 == close (fd_stdout_write));
524  }
525  else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
526  {
527  GNUNET_break (0 == close (1));
528  open_dev_null (1, O_WRONLY);
529  }
530  if (NULL != pipe_stderr)
531  {
532  GNUNET_break (0 == close (fd_stderr_read));
533  if (-1 == dup2 (fd_stderr_write, 2))
535  GNUNET_break (0 == close (fd_stderr_write));
536  }
537  else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
538  {
539  GNUNET_break (0 == close (2));
540  open_dev_null (2, O_WRONLY);
541  }
542  if (NULL != lscp)
543  {
544  /* read systemd documentation... */
545  i = 0;
546  tgt = 3;
547  while (-1 != lscp[i])
548  {
549  j = i + 1;
550  while (-1 != lscp[j])
551  {
552  if (lscp[j] == tgt)
553  {
554  /* dup away */
555  k = dup (lscp[j]);
556  GNUNET_assert (-1 != k);
557  GNUNET_assert (0 == close (lscp[j]));
558  lscp[j] = k;
559  break;
560  }
561  j++;
562  }
563  if (lscp[i] != tgt)
564  {
565  /* Bury any existing FD, no matter what; they should all be closed
566  * on exec anyway and the important onces have been dup'ed away */
567  (void) close (tgt);
568  GNUNET_assert (-1 != dup2 (lscp[i], tgt));
569  }
570  /* unset close-on-exec flag */
571  flags = fcntl (tgt, F_GETFD);
572  GNUNET_assert (flags >= 0);
573  flags &= ~FD_CLOEXEC;
574  fflush (stderr);
575  (void) fcntl (tgt, F_SETFD, flags);
576  tgt++;
577  i++;
578  }
579  GNUNET_snprintf (fds, sizeof(fds), "%u", i);
580  setenv ("LISTEN_FDS", fds, 1);
581  }
582 #ifndef DARWIN
583  /* due to vfork, we must NOT free memory on DARWIN! */
584  GNUNET_array_grow (lscp, ls, 0);
585 #endif
586  execvp (filename, argv);
588  _exit (1);
589 }
No special options, use non-blocking read/write operations.
When this flag is set, the child process will inherit stderr of the parent.
Definition: gnunet_os_lib.h:91
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1280
#define GNUNET_OS_CONTROL_PIPE
Definition: os_priority.c:40
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
#define LOG_STRERROR(kind, syscall)
Definition: os_priority.c:34
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
pid_t pid
PID of the process.
Definition: os_priority.c:48
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
When this flag is set, the child process will inherit stdin of the parent.
Definition: gnunet_os_lib.h:79
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static void open_dev_null(int target_fd, int flags)
Open &#39;/dev/null&#39; and make the result the given file descriptor.
Definition: os_priority.c:304
int GNUNET_OS_check_helper_binary(const char *binary, int check_suid, const char *params)
Check whether an executable exists and possibly if the suid bit is set on the file.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#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 GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_pipe_detach_end(struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd end)
Detaches one of the ends from the pipe.
Definition: disk.c:1601
When this flag is set, the child process will inherit stdout of the parent.
Definition: gnunet_os_lib.h:85
static char * filename
The writing-end of a pipe.
The reading-end of a pipe.
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(enum GNUNET_DISK_PipeFlags pf)
Creates an interprocess channel.
Definition: disk.c:1456
int GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh, void *dst, size_t dst_len)
Retrieve OS file handle.
Definition: disk.c:1697
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: os_priority.c:37
static struct GNUNET_NETWORK_Handle * ls
Listen socket for STUN processing.
Definition: gnunet-nat.c:85
int GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1634
struct GNUNET_DISK_FileHandle * control_pipe
Pipe we use to signal the process.
Definition: os_priority.c:55
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_status()

static int process_status ( struct GNUNET_OS_Process proc,
enum GNUNET_OS_ProcessStatusType type,
unsigned long *  code,
int  options 
)
static

Retrieve the status of a process, waiting on it if dead.

Nonblocking version.

Parameters
procprocess ID
typestatus type
codereturn code/signal number
optionsWNOHANG if non-blocking is desired
Returns
GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise

Definition at line 878 of file os_priority.c.

References GNUNET_assert, GNUNET_ERROR_TYPE_WARNING, GNUNET_NO, GNUNET_OK, GNUNET_OS_PROCESS_EXITED, GNUNET_OS_PROCESS_RUNNING, GNUNET_OS_PROCESS_SIGNALED, GNUNET_OS_PROCESS_UNKNOWN, GNUNET_SYSERR, LOG_STRERROR, GNUNET_OS_Process::pid, ret, and status.

Referenced by GNUNET_OS_process_status(), and GNUNET_OS_process_wait_status().

882 {
883  int status;
884  int ret;
885 
886  GNUNET_assert (0 != proc);
887  ret = waitpid (proc->pid, &status, options);
888  if (ret < 0)
889  {
891  return GNUNET_SYSERR;
892  }
893  if (0 == ret)
894  {
896  *code = 0;
897  return GNUNET_NO;
898  }
899  if (proc->pid != ret)
900  {
902  return GNUNET_SYSERR;
903  }
904  if (WIFEXITED (status))
905  {
907  *code = WEXITSTATUS (status);
908  }
909  else if (WIFSIGNALED (status))
910  {
912  *code = WTERMSIG (status);
913  }
914  else if (WIFSTOPPED (status))
915  {
917  *code = WSTOPSIG (status);
918  }
919 #ifdef WIFCONTINUED
920  else if (WIFCONTINUED (status))
921  {
923  *code = 0;
924  }
925 #endif
926  else
927  {
929  *code = 0;
930  }
931 
932  return GNUNET_OK;
933 }
#define LOG_STRERROR(kind, syscall)
Definition: os_priority.c:34
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
pid_t pid
PID of the process.
Definition: os_priority.c:48
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
uint16_t status
See PRISM_STATUS_*-constants.
The process exited with a return code.
The process was killed by a signal.
The process is still running.
The process is not known to the OS (or at least not one of our children).
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Here is the caller graph for this function:

◆ cmd_read()

static void cmd_read ( void *  cls)
static

Read from the process and call the line processor.

Parameters
clsthe struct GNUNET_OS_CommandHandle *

Definition at line 1080 of file os_priority.c.

References GNUNET_OS_CommandHandle::buf, end, GNUNET_DISK_file_read(), GNUNET_NETWORK_fdset_handle_isset(), GNUNET_SCHEDULER_add_read_file(), GNUNET_SCHEDULER_get_task_context(), GNUNET_TIME_absolute_get_remaining(), GNUNET_YES, GNUNET_OS_CommandHandle::off, GNUNET_OS_CommandHandle::proc, GNUNET_OS_CommandHandle::proc_cls, GNUNET_OS_CommandHandle::r, GNUNET_SCHEDULER_TaskContext::read_ready, ret, GNUNET_OS_CommandHandle::rtask, tc, and GNUNET_OS_CommandHandle::timeout.

Referenced by GNUNET_OS_command_run().

1081 {
1082  struct GNUNET_OS_CommandHandle *cmd = cls;
1083  const struct GNUNET_SCHEDULER_TaskContext *tc;
1085  char *end;
1086  ssize_t ret;
1087 
1088  cmd->rtask = NULL;
1091  {
1092  /* timeout */
1093  proc = cmd->proc;
1094  cmd->proc = NULL;
1095  proc (cmd->proc_cls, NULL);
1096  return;
1097  }
1098  ret = GNUNET_DISK_file_read (cmd->r,
1099  &cmd->buf[cmd->off],
1100  sizeof(cmd->buf) - cmd->off);
1101  if (ret <= 0)
1102  {
1103  if ((cmd->off > 0) && (cmd->off < sizeof(cmd->buf)))
1104  {
1105  cmd->buf[cmd->off] = '\0';
1106  cmd->proc (cmd->proc_cls, cmd->buf);
1107  }
1108  proc = cmd->proc;
1109  cmd->proc = NULL;
1110  proc (cmd->proc_cls, NULL);
1111  return;
1112  }
1113  end = memchr (&cmd->buf[cmd->off], '\n', ret);
1114  cmd->off += ret;
1115  while (NULL != end)
1116  {
1117  *end = '\0';
1118  cmd->proc (cmd->proc_cls, cmd->buf);
1119  memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1120  cmd->off -= (end + 1 - cmd->buf);
1121  end = memchr (cmd->buf, '\n', cmd->off);
1122  }
1123  cmd->rtask =
1125  cmd->timeout),
1126  cmd->r,
1127  &cmd_read,
1128  cmd);
1129 }
const struct GNUNET_SCHEDULER_TaskContext * GNUNET_SCHEDULER_get_task_context(void)
Obtain the reasoning why the current task was started.
Definition: scheduler.c:752
char buf[1024]
Buffer for the output.
Definition: os_priority.c:1032
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
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
Context information passed to each scheduler task.
const struct GNUNET_DISK_FileHandle * r
Read-end of output pipe.
Definition: os_priority.c:1017
size_t off
Current read offset in buf.
Definition: os_priority.c:1047
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:423
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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
const struct GNUNET_NETWORK_FDSet * read_ready
Set of file descriptors ready for reading; note that additional bits may be set that were not in the ...
struct GNUNET_TIME_Absolute timeout
When to time out.
Definition: os_priority.c:1042
void * proc_cls
Closure for proc.
Definition: os_priority.c:1027
static void cmd_read(void *cls)
Read from the process and call the line processor.
Definition: os_priority.c:1080
struct GNUNET_SCHEDULER_Task * rtask
Task reading from pipe.
Definition: os_priority.c:1037
int GNUNET_NETWORK_fdset_handle_isset(const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
Check if a file handle is part of an fd set.
Definition: network.c:1208
Handle to a command.
Definition: os_priority.c:1002
GNUNET_OS_LineProcessor proc
Function to call on each line of output.
Definition: os_priority.c:1022
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:331
void(* GNUNET_OS_LineProcessor)(void *cls, const char *line)
Type of a function to process a line of output.
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ current_process

struct GNUNET_OS_Process current_process
static

Handle for 'this' process.

Definition at line 62 of file os_priority.c.

Referenced by GNUNET_OS_process_current().

◆ pch

struct GNUNET_SCHEDULER_Task* pch
static

Handle for the parent_control_handler() Task.

Definition at line 67 of file os_priority.c.

Referenced by extract_filename().

◆ spch

struct GNUNET_SCHEDULER_Task* spch
static

Handle for the shutdown_pch() Task.

Definition at line 72 of file os_priority.c.