GNUnet  0.10.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 (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 SOCKTYPE *lsocks, const char *filename, char *const argv[])
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_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. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_va (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, va_list va)
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process (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,...)
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_v (int pipe_control, enum GNUNET_OS_InheritStdioFlags std_inheritance, const SOCKTYPE *lsocks, const char *filename, char *const argv[])
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_s (int pipe_control, unsigned int std_inheritance, const SOCKTYPE *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, waiting on it if dead. 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 80 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().

81 {
82  struct GNUNET_DISK_FileHandle *control_pipe = cls;
83 
85  pch = NULL;
86  GNUNET_DISK_file_close(control_pipe);
87  control_pipe = NULL;
88 }
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1339
static struct GNUNET_SCHEDULER_Task * pch
Handle for the parent_control_handler() Task.
Definition: os_priority.c:66
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:956
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 97 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().

98 {
99  struct GNUNET_DISK_FileHandle *control_pipe = cls;
100  char sig;
101  char *pipe_fd;
102  ssize_t ret;
103 
104  pch = NULL;
105  ret = GNUNET_DISK_file_read(control_pipe, &sig, sizeof(sig));
106  if (sizeof(sig) != ret)
107  {
108  if (-1 == ret)
109  LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
110  LOG(GNUNET_ERROR_TYPE_DEBUG, "Closing control pipe\n");
111  GNUNET_DISK_file_close(control_pipe);
112  control_pipe = NULL;
114  spch = NULL;
115  return;
116  }
117  pipe_fd = getenv(GNUNET_OS_CONTROL_PIPE);
118  GNUNET_assert((NULL == pipe_fd) || (strlen(pipe_fd) <= 0));
120  "Got control code %d from parent via pipe %s\n",
121  sig,
122  pipe_fd);
124  control_pipe,
126  control_pipe);
127  GNUNET_SIGNAL_raise((int)sig);
128 }
void GNUNET_SIGNAL_raise(const int sig)
Raise the given signal by calling the installed signal handlers.
Definition: signal.c:95
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1339
#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:729
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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
static int ret
Final status code.
Definition: gnunet-arm.c:89
static struct GNUNET_SCHEDULER_Task * spch
Handle for the shutdown_pch() Task.
Definition: os_priority.c:71
#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:66
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:97
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:956
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  {
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 ( 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 SOCKTYPE lsocks,
const char *  filename,
char *const  argv[] 
)
static

Start a process.

Parameters
pipe_controlshould a pipe be used to send signals to the child?
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 344 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_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_snprintf(), GNUNET_SYSERR, GNUNET_YES, 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().

352 {
353  pid_t ret;
354  char fds[16];
355  struct GNUNET_OS_Process *gnunet_proc;
356  struct GNUNET_DISK_FileHandle *childpipe_read;
357  struct GNUNET_DISK_FileHandle *childpipe_write;
358  int childpipe_read_fd;
359  int i;
360  int j;
361  int k;
362  int tgt;
363  int flags;
364  int *lscp;
365  unsigned int ls;
366  int fd_stdout_write;
367  int fd_stdout_read;
368  int fd_stderr_write;
369  int fd_stderr_read;
370  int fd_stdin_read;
371  int fd_stdin_write;
372 
373  if (GNUNET_SYSERR ==
375  return NULL; /* not executable */
376  if (GNUNET_YES == pipe_control)
377  {
378  struct GNUNET_DISK_PipeHandle *childpipe;
379  int dup_childpipe_read_fd = -1;
380 
382  if (NULL == childpipe)
383  return NULL;
384  childpipe_read =
386  childpipe_write =
388  GNUNET_DISK_pipe_close(childpipe);
389  if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
390  (GNUNET_OK != GNUNET_DISK_internal_file_handle_(childpipe_read,
391  &childpipe_read_fd,
392  sizeof(int))) ||
393  (-1 == (dup_childpipe_read_fd = dup(childpipe_read_fd))))
394  {
395  if (NULL != childpipe_read)
396  GNUNET_DISK_file_close(childpipe_read);
397  if (NULL != childpipe_write)
398  GNUNET_DISK_file_close(childpipe_write);
399  if (0 <= dup_childpipe_read_fd)
400  close(dup_childpipe_read_fd);
401  return NULL;
402  }
403  childpipe_read_fd = dup_childpipe_read_fd;
404  GNUNET_DISK_file_close(childpipe_read);
405  }
406  else
407  {
408  childpipe_write = NULL;
409  childpipe_read_fd = -1;
410  }
411  if (NULL != pipe_stdin)
412  {
414  GNUNET_OK ==
417  &fd_stdin_read,
418  sizeof(int)));
420  GNUNET_OK ==
423  &fd_stdin_write,
424  sizeof(int)));
425  }
426  if (NULL != pipe_stdout)
427  {
429  GNUNET_OK ==
432  &fd_stdout_write,
433  sizeof(int)));
435  GNUNET_OK ==
438  &fd_stdout_read,
439  sizeof(int)));
440  }
441  if (NULL != pipe_stderr)
442  {
444  GNUNET_OK ==
447  &fd_stderr_read,
448  sizeof(int)));
450  GNUNET_OK ==
453  &fd_stderr_write,
454  sizeof(int)));
455  }
456  lscp = NULL;
457  ls = 0;
458  if (NULL != lsocks)
459  {
460  i = 0;
461  while (-1 != (k = lsocks[i++]))
462  GNUNET_array_append(lscp, ls, k);
463  GNUNET_array_append(lscp, ls, -1);
464  }
465 #if DARWIN
466  /* see https://web.archive.org/web/20150924082249/gnunet.org/vfork */
467  ret = vfork();
468 #else
469  ret = fork();
470 #endif
471  if (-1 == ret)
472  {
473  int eno = errno;
475  GNUNET_array_grow(lscp, ls, 0);
476  if (NULL != childpipe_write)
477  GNUNET_DISK_file_close(childpipe_write);
478  if (0 <= childpipe_read_fd)
479  close(childpipe_read_fd);
480  errno = eno;
481  return NULL;
482  }
483  if (0 != ret)
484  {
485  unsetenv(GNUNET_OS_CONTROL_PIPE);
486  gnunet_proc = GNUNET_new(struct GNUNET_OS_Process);
487  gnunet_proc->pid = ret;
488  gnunet_proc->control_pipe = childpipe_write;
489  if (GNUNET_YES == pipe_control)
490  {
491  close(childpipe_read_fd);
492  }
493  GNUNET_array_grow(lscp, ls, 0);
494  return gnunet_proc;
495  }
496  if (0 <= childpipe_read_fd)
497  {
498  char fdbuf[100];
499 #ifndef DARWIN
500  /* due to vfork, we must NOT free memory on DARWIN! */
501  GNUNET_DISK_file_close(childpipe_write);
502 #endif
503  snprintf(fdbuf, 100, "%x", childpipe_read_fd);
504  setenv(GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
505  }
506  else
507  unsetenv(GNUNET_OS_CONTROL_PIPE);
508  if (NULL != pipe_stdin)
509  {
510  GNUNET_break(0 == close(fd_stdin_write));
511  if (-1 == dup2(fd_stdin_read, 0))
513  GNUNET_break(0 == close(fd_stdin_read));
514  }
515  else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
516  {
517  GNUNET_break(0 == close(0));
518  open_dev_null(0, O_RDONLY);
519  }
520  if (NULL != pipe_stdout)
521  {
522  GNUNET_break(0 == close(fd_stdout_read));
523  if (-1 == dup2(fd_stdout_write, 1))
525  GNUNET_break(0 == close(fd_stdout_write));
526  }
527  else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
528  {
529  GNUNET_break(0 == close(1));
530  open_dev_null(1, O_WRONLY);
531  }
532  if (NULL != pipe_stderr)
533  {
534  GNUNET_break(0 == close(fd_stderr_read));
535  if (-1 == dup2(fd_stderr_write, 2))
537  GNUNET_break(0 == close(fd_stderr_write));
538  }
539  else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
540  {
541  GNUNET_break(0 == close(2));
542  open_dev_null(2, O_WRONLY);
543  }
544  if (NULL != lscp)
545  {
546  /* read systemd documentation... */
547  i = 0;
548  tgt = 3;
549  while (-1 != lscp[i])
550  {
551  j = i + 1;
552  while (-1 != lscp[j])
553  {
554  if (lscp[j] == tgt)
555  {
556  /* dup away */
557  k = dup(lscp[j]);
558  GNUNET_assert(-1 != k);
559  GNUNET_assert(0 == close(lscp[j]));
560  lscp[j] = k;
561  break;
562  }
563  j++;
564  }
565  if (lscp[i] != tgt)
566  {
567  /* Bury any existing FD, no matter what; they should all be closed
568  * on exec anyway and the important onces have been dup'ed away */
569  (void)close(tgt);
570  GNUNET_assert(-1 != dup2(lscp[i], tgt));
571  }
572  /* unset close-on-exec flag */
573  flags = fcntl(tgt, F_GETFD);
574  GNUNET_assert(flags >= 0);
575  flags &= ~FD_CLOEXEC;
576  fflush(stderr);
577  (void)fcntl(tgt, F_SETFD, flags);
578  tgt++;
579  i++;
580  }
581  GNUNET_snprintf(fds, sizeof(fds), "%u", i);
582  setenv("LISTEN_FDS", fds, 1);
583  }
584 #ifndef DARWIN
585  /* due to vfork, we must NOT free memory on DARWIN! */
586  GNUNET_array_grow(lscp, ls, 0);
587 #endif
588  execvp(filename, argv);
590  _exit(1);
591 }
When this flag is set, the child process will inherit stderr of the parent.
Definition: gnunet_os_lib.h:90
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1339
#define GNUNET_OS_CONTROL_PIPE
Definition: os_priority.c:40
#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:47
#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:78
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
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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.
static int ret
Final status code.
Definition: gnunet-arm.c:89
#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 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:1673
When this flag is set, the child process will inherit stdout of the parent.
Definition: gnunet_os_lib.h:84
static char * filename
The writing-end of a pipe.
The reading-end of a pipe.
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
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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:1769
#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:1706
#define GNUNET_array_append(arr, size, element)
Append an element to a list (growing the list by one).
struct GNUNET_DISK_FileHandle * control_pipe
Pipe we use to signal the process.
Definition: os_priority.c:54
#define GNUNET_YES
Definition: gnunet_common.h:77
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 895 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().

899 {
900  int status;
901  int ret;
902 
903  GNUNET_assert(0 != proc);
904  ret = waitpid(proc->pid, &status, options);
905  if (ret < 0)
906  {
908  return GNUNET_SYSERR;
909  }
910  if (0 == ret)
911  {
913  *code = 0;
914  return GNUNET_NO;
915  }
916  if (proc->pid != ret)
917  {
919  return GNUNET_SYSERR;
920  }
921  if (WIFEXITED(status))
922  {
924  *code = WEXITSTATUS(status);
925  }
926  else if (WIFSIGNALED(status))
927  {
929  *code = WTERMSIG(status);
930  }
931  else if (WIFSTOPPED(status))
932  {
934  *code = WSTOPSIG(status);
935  }
936 #ifdef WIFCONTINUED
937  else if (WIFCONTINUED(status))
938  {
940  *code = 0;
941  }
942 #endif
943  else
944  {
946  *code = 0;
947  }
948 
949  return GNUNET_OK;
950 }
#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:47
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static int ret
Final status code.
Definition: gnunet-arm.c:89
uint16_t status
See PRISM_STATUS_*-constants.
The process exited with a return code.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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 1096 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().

1097 {
1098  struct GNUNET_OS_CommandHandle *cmd = cls;
1099  const struct GNUNET_SCHEDULER_TaskContext *tc;
1101  char *end;
1102  ssize_t ret;
1103 
1104  cmd->rtask = NULL;
1107  {
1108  /* timeout */
1109  proc = cmd->proc;
1110  cmd->proc = NULL;
1111  proc(cmd->proc_cls, NULL);
1112  return;
1113  }
1114  ret = GNUNET_DISK_file_read(cmd->r,
1115  &cmd->buf[cmd->off],
1116  sizeof(cmd->buf) - cmd->off);
1117  if (ret <= 0)
1118  {
1119  if ((cmd->off > 0) && (cmd->off < sizeof(cmd->buf)))
1120  {
1121  cmd->buf[cmd->off] = '\0';
1122  cmd->proc(cmd->proc_cls, cmd->buf);
1123  }
1124  proc = cmd->proc;
1125  cmd->proc = NULL;
1126  proc(cmd->proc_cls, NULL);
1127  return;
1128  }
1129  end = memchr(&cmd->buf[cmd->off], '\n', ret);
1130  cmd->off += ret;
1131  while (NULL != end)
1132  {
1133  *end = '\0';
1134  cmd->proc(cmd->proc_cls, cmd->buf);
1135  memmove(cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1136  cmd->off -= (end + 1 - cmd->buf);
1137  end = memchr(cmd->buf, '\n', cmd->off);
1138  }
1139  cmd->rtask =
1141  cmd->timeout),
1142  cmd->r,
1143  &cmd_read,
1144  cmd);
1145 }
const struct GNUNET_SCHEDULER_TaskContext * GNUNET_SCHEDULER_get_task_context(void)
Obtain the reasoning why the current task was started.
Definition: scheduler.c:737
char buf[1024]
Buffer for the output.
Definition: os_priority.c:1048
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:729
Context information passed to each scheduler task.
const struct GNUNET_DISK_FileHandle * r
Read-end of output pipe.
Definition: os_priority.c:1033
size_t off
Current read offset in buf.
Definition: os_priority.c:1063
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:410
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
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 ...
static int ret
Final status code.
Definition: gnunet-arm.c:89
struct GNUNET_TIME_Absolute timeout
When to time out.
Definition: os_priority.c:1058
void * proc_cls
Closure for proc.
Definition: os_priority.c:1043
static void cmd_read(void *cls)
Read from the process and call the line processor.
Definition: os_priority.c:1096
struct GNUNET_SCHEDULER_Task * rtask
Task reading from pipe.
Definition: os_priority.c:1053
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:1196
Handle to a command.
Definition: os_priority.c:1019
GNUNET_OS_LineProcessor proc
Function to call on each line of output.
Definition: os_priority.c:1038
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
#define GNUNET_YES
Definition: gnunet_common.h:77
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 61 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 66 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 71 of file os_priority.c.