GNUnet 0.26.2-45-gb0324da85
 
Loading...
Searching...
No Matches
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_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)
 
#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.
 
static void parent_control_handler (void *cls)
 This handler is called when there are control data to be read on the pipe.
 
void GNUNET_OS_install_parent_control_handler (void *cls)
 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.
 
void GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
 Stop/kill a command.
 
static void cmd_read (void *cls)
 Read from the process and call the line processor.
 
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.
 

Variables

static struct GNUNET_SCHEDULER_Taskpch
 Handle for the parent_control_handler() Task.
 
static struct GNUNET_SCHEDULER_Taskspch
 Handle for the shutdown_pch() Task.
 

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__)

Definition at line 31 of file os_priority.c.

◆ LOG_STRERROR

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

Definition at line 33 of file os_priority.c.

59{
60 struct GNUNET_DISK_FileHandle *control_pipe = cls;
61
63 pch = NULL;
64 GNUNET_DISK_file_close (control_pipe);
65 control_pipe = NULL;
66}
67
68
74static void
75parent_control_handler (void *cls)
76{
77 struct GNUNET_DISK_FileHandle *control_pipe = cls;
78 char sig;
79 char *pipe_fd;
80 ssize_t ret;
81
82 pch = NULL;
83 ret = GNUNET_DISK_file_read (control_pipe, &sig, sizeof(sig));
84 if (sizeof(sig) != ret)
85 {
86 if (-1 == ret)
87 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
88 LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing control pipe\n");
89 GNUNET_DISK_file_close (control_pipe);
90 control_pipe = NULL;
92 spch = NULL;
93 return;
94 }
96 GNUNET_assert ((NULL == pipe_fd) || (strlen (pipe_fd) <= 0));
98 "Got control code %d from parent via pipe %s\n",
99 sig,
100 pipe_fd);
102 control_pipe,
104 control_pipe);
105 GNUNET_SIGNAL_raise ((int) sig);
106}
107
108
109void
111{
112 const char *env_buf;
113 char *env_buf_end;
114 struct GNUNET_DISK_FileHandle *control_pipe;
115 uint64_t pipe_fd;
116
117 (void) cls;
118 if (NULL != pch)
119 {
120 /* already done, we've been called twice... */
121 GNUNET_break (0);
122 return;
123 }
124 env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
125 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
126 {
128 "Not installing a handler because $%s is empty\n",
130 setenv (GNUNET_OS_CONTROL_PIPE, "", 1);
131 return;
132 }
133 errno = 0;
134 pipe_fd = strtoull (env_buf, &env_buf_end, 16);
135 if ((0 != errno) || (env_buf == env_buf_end))
136 {
137 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "strtoull", env_buf);
138 setenv (GNUNET_OS_CONTROL_PIPE, "", 1);
139 return;
140 }
141 if (pipe_fd >= FD_SETSIZE)
142 {
144 "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n",
145 env_buf);
146 setenv (GNUNET_OS_CONTROL_PIPE, "", 1);
147 return;
148 }
149
150 control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
151
152 if (NULL == control_pipe)
153 {
155 setenv (GNUNET_OS_CONTROL_PIPE, "", 1);
156 return;
157 }
159 "Adding parent control handler pipe `%s' to the scheduler\n",
160 env_buf);
162 control_pipe,
164 control_pipe);
166 setenv (GNUNET_OS_CONTROL_PIPE, "", 1);
167}
168
169
174{
178 struct GNUNET_Process *eip;
179
184
188 const struct GNUNET_DISK_FileHandle *r;
189
194
198 void *proc_cls;
199
203 char buf[1024];
204
209
214
218 size_t off;
219};
220
221
222void
224{
225 if (NULL != cmd->proc)
226 {
227 GNUNET_assert (NULL != cmd->rtask);
229 }
232 SIGKILL));
235 true,
236 NULL,
237 NULL));
240 GNUNET_free (cmd);
241}
242
243
249static void
250cmd_read (void *cls)
251{
252 struct GNUNET_OS_CommandHandle *cmd = cls;
253 const struct GNUNET_SCHEDULER_TaskContext *tc;
255 char *end;
256 ssize_t ret;
257
258 cmd->rtask = NULL;
260 if (GNUNET_YES !=
262 cmd->r))
263 {
264 /* timeout */
265 proc = cmd->proc;
266 cmd->proc = NULL;
267 proc (cmd->proc_cls, NULL);
268 return;
269 }
271 &cmd->buf[cmd->off],
272 sizeof(cmd->buf) - cmd->off);
273 if (ret <= 0)
274 {
275 if ((cmd->off > 0) && (cmd->off < sizeof(cmd->buf)))
276 {
277 cmd->buf[cmd->off] = '\0';
278 cmd->proc (cmd->proc_cls, cmd->buf);
279 }
280 proc = cmd->proc;
281 cmd->proc = NULL;
282 proc (cmd->proc_cls, NULL);
283 return;
284 }
285 end = memchr (&cmd->buf[cmd->off], '\n', ret);
286 cmd->off += ret;
287 while (NULL != end)
288 {
289 *end = '\0';
290 cmd->proc (cmd->proc_cls, cmd->buf);
291 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
292 cmd->off -= (end + 1 - cmd->buf);
293 end = memchr (cmd->buf, '\n', cmd->off);
294 }
295 cmd->rtask =
297 cmd->timeout),
298 cmd->r,
299 &cmd_read,
300 cmd);
301}
302
303
306 void *proc_cls,
308 const char *binary,
309 ...)
310{
312 struct GNUNET_Process *eip;
313 struct GNUNET_DISK_PipeHandle *opipe;
314 va_list ap;
315
317 if (NULL == opipe)
318 return NULL;
319 va_start (ap, binary);
320 /* redirect stdout, don't inherit stderr/stdin */
324 eip,
326 STDOUT_FILENO)));
327 if (GNUNET_OK !=
329 binary,
330 ap))
331 {
333 va_end (ap);
335 return NULL;
336 }
337 va_end (ap);
340 cmd = GNUNET_new (struct GNUNET_OS_CommandHandle);
342 cmd->eip = eip;
343 cmd->opipe = opipe;
344 cmd->proc = proc;
345 cmd->proc_cls = proc_cls;
346 cmd->r = GNUNET_DISK_pipe_handle (opipe,
349 cmd->r,
350 &cmd_read, cmd);
351 return cmd;
352}
353
354
355/* end of os_priority.c */
char * getenv()
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_TIME_Relative timeout
User defined timestamp for completing operations.
Definition gnunet-arm.c:118
static int end
Set if we are to shutdown all services (including ARM).
Definition gnunet-arm.c:33
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:1703
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(enum GNUNET_DISK_PipeFlags pf)
Creates an interprocess channel.
Definition disk.c:1524
enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition disk.c:1671
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition disk.c:1386
struct GNUNET_DISK_FileHandle * GNUNET_DISK_get_handle_from_int_fd(int fno)
Get a handle from a native integer FD.
Definition disk.c:1410
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:704
enum GNUNET_GenericReturnValue 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:1618
@ GNUNET_DISK_PF_BLOCKING_RW
Configure both pipe ends for blocking operations if set.
@ GNUNET_DISK_PIPE_END_WRITE
The writing-end of a pipe.
@ GNUNET_DISK_PIPE_END_READ
The reading-end of a pipe.
@ GNUNET_OK
@ GNUNET_YES
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
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:1126
void(* GNUNET_OS_LineProcessor)(void *cls, const char *line)
Type of a function to process a line of output.
void GNUNET_OS_install_parent_control_handler(void *cls)
Connects this process to its parent via pipe; essentially, the parent control handler will read signa...
enum GNUNET_GenericReturnValue GNUNET_process_run_command_va(struct GNUNET_Process *p, const char *filename,...)
Set the command and start a process.
Definition os_process.c:903
enum GNUNET_GenericReturnValue GNUNET_process_wait(struct GNUNET_Process *proc, bool blocking, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Wait for a process to terminate.
void GNUNET_process_destroy(struct GNUNET_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition os_process.c:363
void GNUNET_OS_command_stop(struct GNUNET_OS_CommandHandle *cmd)
Stop/kill a command.
struct GNUNET_OS_CommandHandle * GNUNET_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.
#define GNUNET_process_set_options(proc,...)
Set the requested options for the process.
#define GNUNET_process_option_inherit_wpipe(wpipe, child_fd)
Have child process inherit a pipe for writing.
enum GNUNET_GenericReturnValue GNUNET_process_kill(struct GNUNET_Process *proc, int sig)
Sends a signal to the process.
Definition os_process.c:307
struct GNUNET_Process * GNUNET_process_create(enum GNUNET_OS_InheritStdioFlags std_inheritance)
Create a process handle.
Definition os_process.c:462
@ GNUNET_OS_INHERIT_STD_NONE
No standard streams should be inherited.
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_SCHEDULER_TaskContext * GNUNET_SCHEDULER_get_task_context(void)
Obtain the reasoning why the current task was started.
Definition scheduler.c:764
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition scheduler.c:1345
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition scheduler.c:986
void GNUNET_SIGNAL_raise(const int sig)
Raise the given signal by calling the installed signal handlers.
Definition signal.c:98
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
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:406
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition time.c:316
#define GNUNET_OS_CONTROL_PIPE
Definition os_priority.c:40
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:76
static struct GNUNET_SCHEDULER_Task * pch
Handle for the parent_control_handler() Task.
Definition os_priority.c:45
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition os_priority.c:36
static void shutdown_pch(void *cls)
This handler is called on shutdown to remove the pch.
Definition os_priority.c:59
static struct GNUNET_SCHEDULER_Task * spch
Handle for the shutdown_pch() Task.
Definition os_priority.c:50
#define LOG(kind,...)
Definition os_priority.c:31
#define LOG_STRERROR(kind, syscall)
Definition os_priority.c:33
static void cmd_read(void *cls)
Read from the process and call the line processor.
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition scheduler.c:436
Handle used to access files (and pipes).
Handle used to manage a pipe.
Definition disk.c:69
Handle to a command.
struct GNUNET_Process * eip
Process handle.
struct GNUNET_DISK_PipeHandle * opipe
Handle to the output pipe.
size_t off
Current read offset in buf.
char buf[1024]
Buffer for the output.
struct GNUNET_SCHEDULER_Task * rtask
Task reading from pipe.
struct GNUNET_TIME_Absolute timeout
When to time out.
void * proc_cls
Closure for proc.
GNUNET_OS_LineProcessor proc
Function to call on each line of output.
const struct GNUNET_DISK_FileHandle * r
Read-end of output pipe.
Context information passed to each scheduler task.
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 ...
Entry in list of pending tasks.
Definition scheduler.c:141
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  syscall,
  filename 
)
Value:
GNUNET_log_from_strerror_file (kind, "util-os-priority", syscall, \
static char * filename
#define GNUNET_log_from_strerror_file(level, component, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...

Definition at line 36 of file os_priority.c.

◆ GNUNET_OS_CONTROL_PIPE

#define GNUNET_OS_CONTROL_PIPE   "GNUNET_OS_CONTROL_PIPE"

Definition at line 40 of file os_priority.c.

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 59 of file os_priority.c.

60{
61 struct GNUNET_DISK_FileHandle *control_pipe = cls;
62
64 pch = NULL;
65 GNUNET_DISK_file_close (control_pipe);
66 control_pipe = NULL;
67}

References GNUNET_DISK_file_close(), GNUNET_SCHEDULER_cancel(), and pch.

Referenced by GNUNET_OS_install_parent_control_handler().

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 76 of file os_priority.c.

77{
78 struct GNUNET_DISK_FileHandle *control_pipe = cls;
79 char sig;
80 char *pipe_fd;
81 ssize_t ret;
82
83 pch = NULL;
84 ret = GNUNET_DISK_file_read (control_pipe, &sig, sizeof(sig));
85 if (sizeof(sig) != ret)
86 {
87 if (-1 == ret)
88 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
89 LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing control pipe\n");
90 GNUNET_DISK_file_close (control_pipe);
91 control_pipe = NULL;
93 spch = NULL;
94 return;
95 }
97 GNUNET_assert ((NULL == pipe_fd) || (strlen (pipe_fd) <= 0));
99 "Got control code %d from parent via pipe %s\n",
100 sig,
101 pipe_fd);
103 control_pipe,
105 control_pipe);
106 GNUNET_SIGNAL_raise ((int) sig);
107}

References 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, parent_control_handler(), pch, ret, and spch.

Referenced by GNUNET_OS_install_parent_control_handler(), and parent_control_handler().

Here is the call graph for this function:
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 251 of file os_priority.c.

252{
253 struct GNUNET_OS_CommandHandle *cmd = cls;
254 const struct GNUNET_SCHEDULER_TaskContext *tc;
256 char *end;
257 ssize_t ret;
258
259 cmd->rtask = NULL;
261 if (GNUNET_YES !=
263 cmd->r))
264 {
265 /* timeout */
266 proc = cmd->proc;
267 cmd->proc = NULL;
268 proc (cmd->proc_cls, NULL);
269 return;
270 }
272 &cmd->buf[cmd->off],
273 sizeof(cmd->buf) - cmd->off);
274 if (ret <= 0)
275 {
276 if ((cmd->off > 0) && (cmd->off < sizeof(cmd->buf)))
277 {
278 cmd->buf[cmd->off] = '\0';
279 cmd->proc (cmd->proc_cls, cmd->buf);
280 }
281 proc = cmd->proc;
282 cmd->proc = NULL;
283 proc (cmd->proc_cls, NULL);
284 return;
285 }
286 end = memchr (&cmd->buf[cmd->off], '\n', ret);
287 cmd->off += ret;
288 while (NULL != end)
289 {
290 *end = '\0';
291 cmd->proc (cmd->proc_cls, cmd->buf);
292 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
293 cmd->off -= (end + 1 - cmd->buf);
294 end = memchr (cmd->buf, '\n', cmd->off);
295 }
296 cmd->rtask =
298 cmd->timeout),
299 cmd->r,
300 &cmd_read,
301 cmd);
302}

References GNUNET_OS_CommandHandle::buf, cmd_read(), 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 cmd_read(), and GNUNET_OS_command_run().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ pch

◆ spch

struct GNUNET_SCHEDULER_Task* spch
static

Handle for the shutdown_pch() Task.

Definition at line 50 of file os_priority.c.

Referenced by GNUNET_OS_install_parent_control_handler(), and parent_control_handler().