2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/os_priority.c
23 * @brief Methods to set process priority
28 #include "gnunet_common.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet_scheduler_lib.h"
31 #include "gnunet_strings_lib.h"
32 #include "gnunet_crypto_lib.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
38 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
40 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
42 #define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE"
45 struct GNUNET_OS_Process
60 * Pipe we use to signal the process.
61 * NULL if unused, or if process was deemed uncontrollable.
63 struct GNUNET_DISK_FileHandle *control_pipe;
68 * Handle for 'this' process.
70 static struct GNUNET_OS_Process current_process;
74 * This handler is called when there are control data to be read on the pipe
76 * @param cls the 'struct GNUNET_DISK_FileHandle' of the control pipe
77 * @param tc scheduler context
80 parent_control_handler (void *cls,
81 const struct GNUNET_SCHEDULER_TaskContext *tc)
83 struct GNUNET_DISK_FileHandle *control_pipe = cls;
88 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' invoked because of %d\n", __FUNCTION__,
90 if (0 != (tc->reason &
91 (GNUNET_SCHEDULER_REASON_SHUTDOWN | GNUNET_SCHEDULER_REASON_TIMEOUT)))
93 GNUNET_DISK_file_close (control_pipe);
97 ret = GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig));
98 if (sizeof (sig) != ret)
101 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
102 LOG (GNUNET_ERROR_TYPE_WARNING, "Closing control pipe\n");
103 GNUNET_DISK_file_close (control_pipe);
107 pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE);
108 GNUNET_assert ( (NULL == pipe_fd) || (strlen (pipe_fd) <= 0) );
109 LOG (GNUNET_ERROR_TYPE_DEBUG,
110 "Got control code %d from parent via pipe %s\n", sig, pipe_fd);
111 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
112 control_pipe, &parent_control_handler,
119 * Task that connects this process to its parent via pipe;
120 * essentially, the parent control handler will read signal numbers
121 * from the 'GNUNET_OS_CONTROL_PIPE' (as given in an environment
122 * variable) and raise those signals.
124 * @param cls closure (unused)
125 * @param tc scheduler context (unused)
128 GNUNET_OS_install_parent_control_handler (void *cls,
130 GNUNET_SCHEDULER_TaskContext *tc)
134 struct GNUNET_DISK_FileHandle *control_pipe;
137 env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
138 if ( (NULL == env_buf) || (strlen (env_buf) <= 0) )
140 LOG (GNUNET_ERROR_TYPE_DEBUG,
141 "Not installing a handler because $%s is empty\n",
142 GNUNET_OS_CONTROL_PIPE);
143 putenv (GNUNET_OS_CONTROL_PIPE "=");
147 pipe_fd = strtoull (env_buf, &env_buf_end, 16);
148 if ((0 != errno) || (env_buf == env_buf_end))
150 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "strtoull", env_buf);
151 putenv (GNUNET_OS_CONTROL_PIPE "=");
154 #if !defined (WINDOWS)
155 if (pipe_fd >= FD_SETSIZE)
157 if ((FILE_TYPE_UNKNOWN == GetFileType ((HANDLE) (uintptr_t) pipe_fd))
158 && (0 != GetLastError ()))
161 LOG (GNUNET_ERROR_TYPE_ERROR,
162 "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n", env_buf);
163 putenv (GNUNET_OS_CONTROL_PIPE "=");
167 control_pipe = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) (uintptr_t) pipe_fd);
169 control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
171 if (NULL == control_pipe)
173 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", env_buf);
174 putenv (GNUNET_OS_CONTROL_PIPE "=");
177 LOG (GNUNET_ERROR_TYPE_DEBUG,
178 "Adding parent control handler pipe `%s' to the scheduler\n", env_buf);
179 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, control_pipe,
180 &parent_control_handler, control_pipe);
181 putenv (GNUNET_OS_CONTROL_PIPE "=");
186 * Get process structure for current process
188 * The pointer it returns points to static memory location and must not be
191 * @return pointer to the process sturcutre for this process
193 struct GNUNET_OS_Process *
194 GNUNET_OS_process_current ()
197 current_process.pid = GetCurrentProcessId ();
198 current_process.handle = GetCurrentProcess ();
200 current_process.pid = 0;
202 return ¤t_process;
207 * Sends a signal to the process
209 * @param proc pointer to process structure
211 * @return 0 on success, -1 on error
214 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
220 if (NULL != proc->control_pipe)
222 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending signal %d to pid: %u via pipe\n", sig, proc->pid);
223 ret = GNUNET_DISK_file_write (proc->control_pipe, &csig, sizeof (csig));
224 if (sizeof (csig) == ret)
227 /* pipe failed or non-existent, try other methods */
230 #if !defined (WINDOWS)
236 #if defined(WINDOWS) && !defined(__CYGWIN__)
239 int must_kill = GNUNET_YES;
240 if (0 != GetExitCodeProcess (proc->handle, &exitcode))
241 must_kill = (exitcode == STILL_ACTIVE) ? GNUNET_YES : GNUNET_NO;
242 if (GNUNET_YES == must_kill)
243 if (0 == SafeTerminateProcess (proc->handle, 0, 0))
245 DWORD error_code = GetLastError ();
246 if ((error_code != WAIT_TIMEOUT) && (error_code != ERROR_PROCESS_ABORTED))
248 LOG ((error_code == ERROR_ACCESS_DENIED) ?
249 GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
250 "SafeTermiateProcess failed with code %lu\n", error_code);
251 /* The problem here is that a process that is already dying
252 * might cause SafeTerminateProcess to fail with
253 * ERROR_ACCESS_DENIED, but the process WILL die eventually.
254 * If we really had a permissions problem, hanging up (which
255 * is what will happen in process_wait() in that case) is
258 if (ERROR_ACCESS_DENIED == error_code)
264 SetErrnoFromWinError (error_code);
272 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending signal %d to pid: %u via system call\n", sig, proc->pid);
273 return PLIBC_KILL (proc->pid, sig);
276 #if defined (WINDOWS)
280 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending signal %d to pid: %u via system call\n", sig, proc->pid);
281 return PLIBC_KILL (proc->pid, sig);
287 * Get the pid of the process in question
289 * @param proc the process to get the pid of
291 * @return the current process id
294 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process * proc)
301 * Cleans up process structure contents (OS-dependent) and deallocates it
303 * @param proc pointer to process structure
306 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
308 if (NULL != proc->control_pipe)
309 GNUNET_DISK_file_close (proc->control_pipe);
310 #if defined (WINDOWS)
311 if (proc->handle != NULL)
312 CloseHandle (proc->handle);
318 #include "gnunet_signal_lib.h"
320 extern GNUNET_SIGNAL_Handler w32_sigchld_handler;
323 * Make seaspider happy.
325 #define DWORD_WINAPI DWORD WINAPI
328 * @brief Waits for a process to terminate and invokes the SIGCHLD handler
329 * @param proc pointer to process structure
332 child_wait_thread (void *arg)
334 struct GNUNET_OS_Process *proc = (struct GNUNET_OS_Process *) arg;
336 WaitForSingleObject (proc->handle, INFINITE);
338 if (w32_sigchld_handler)
339 w32_sigchld_handler ();
347 * Set process priority
349 * @param proc pointer to process structure
350 * @param prio priority value
351 * @return GNUNET_OK on success, GNUNET_SYSERR on error
354 GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
355 enum GNUNET_SCHEDULER_Priority prio)
359 GNUNET_assert (prio < GNUNET_SCHEDULER_PRIORITY_COUNT);
360 if (GNUNET_SCHEDULER_PRIORITY_KEEP == prio)
363 /* convert to MINGW/Unix values */
366 case GNUNET_SCHEDULER_PRIORITY_UI:
367 case GNUNET_SCHEDULER_PRIORITY_URGENT:
369 rprio = HIGH_PRIORITY_CLASS;
375 case GNUNET_SCHEDULER_PRIORITY_HIGH:
377 rprio = ABOVE_NORMAL_PRIORITY_CLASS;
383 case GNUNET_SCHEDULER_PRIORITY_DEFAULT:
385 rprio = NORMAL_PRIORITY_CLASS;
391 case GNUNET_SCHEDULER_PRIORITY_BACKGROUND:
393 rprio = BELOW_NORMAL_PRIORITY_CLASS;
399 case GNUNET_SCHEDULER_PRIORITY_IDLE:
401 rprio = IDLE_PRIORITY_CLASS;
408 return GNUNET_SYSERR;
411 /* Set process priority */
414 HANDLE h = proc->handle;
416 GNUNET_assert (h != NULL);
417 SetPriorityClass (h, rprio);
423 if ((0 == pid) || (pid == getpid ()))
426 int delta = rprio - have;
429 if ((delta != 0) && (rprio == nice (delta)) && (errno != 0))
431 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "nice");
432 return GNUNET_SYSERR;
437 if (0 != setpriority (PRIO_PROCESS, pid, rprio))
439 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
441 return GNUNET_SYSERR;
445 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
446 "Priority management not availabe for this platform\n");
454 CreateCustomEnvTable (char **vars)
456 char *win32_env_table;
461 size_t tablesize = 0;
462 size_t items_count = 0;
471 win32_env_table = GetEnvironmentStringsA ();
472 if (NULL == win32_env_table)
474 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ;
476 index = GNUNET_malloc (sizeof (char *) * n_var);
477 for (c = 0; c < n_var; c++)
479 for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++)
481 size_t len = strlen (ptr);
484 for (var_ptr = vars; *var_ptr; var_ptr++)
488 var_len = strlen (var);
489 if (strncmp (var, ptr, var_len) == 0)
493 tablesize += var_len + strlen (val) + 1;
498 tablesize += len + 1;
501 for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
506 n_found += strlen (var) + strlen (val) + 1;
508 result = GNUNET_malloc (tablesize + n_found + 1);
509 for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
511 size_t len = strlen (ptr);
514 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
518 var_len = strlen (var);
519 if (strncmp (var, ptr, var_len) == 0)
527 strcpy (result_ptr, ptr);
528 result_ptr += len + 1;
532 strcpy (result_ptr, var);
533 result_ptr += var_len;
534 strcpy (result_ptr, val);
535 result_ptr += strlen (val) + 1;
539 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
543 var_len = strlen (var);
546 strcpy (result_ptr, var);
547 result_ptr += var_len;
548 strcpy (result_ptr, val);
549 result_ptr += strlen (val) + 1;
552 FreeEnvironmentStrings (win32_env_table);
561 * Open '/dev/null' and make the result the given
564 * @param target_fd desired FD to point to /dev/null
565 * @param flags open flags (O_RDONLY, O_WRONLY)
568 open_dev_null (int target_fd,
573 fd = open ("/dev/null", flags);
576 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", "/dev/null");
581 if (-1 == dup2 (fd, target_fd))
583 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
587 GNUNET_break (0 == close (fd));
595 * @param pipe_control should a pipe be used to send signals to the child?
596 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
597 * std handles of the parent are inherited by the child.
598 * pipe_stdin and pipe_stdout take priority over std_inheritance
599 * (when they are non-NULL).
600 * @param pipe_stdin pipe to use to send input to child process (or NULL)
601 * @param pipe_stdout pipe to use to get output from child process (or NULL)
602 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
603 * must be NULL on platforms where dup is not supported
604 * @param filename name of the binary
605 * @param argv NULL-terminated list of arguments to the process
606 * @return process ID of the new process, -1 on error
608 static struct GNUNET_OS_Process *
609 start_process (int pipe_control,
610 enum GNUNET_OS_InheritStdioFlags std_inheritance,
611 struct GNUNET_DISK_PipeHandle *pipe_stdin,
612 struct GNUNET_DISK_PipeHandle *pipe_stdout,
613 const SOCKTYPE *lsocks,
614 const char *filename,
621 struct GNUNET_OS_Process *gnunet_proc;
622 struct GNUNET_DISK_FileHandle *childpipe_read;
623 struct GNUNET_DISK_FileHandle *childpipe_write;
624 int childpipe_read_fd;
637 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
638 return NULL; /* not executable */
639 if (GNUNET_YES == pipe_control)
641 struct GNUNET_DISK_PipeHandle *childpipe;
642 int dup_childpipe_read_fd = -1;
643 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
644 if (NULL == childpipe)
646 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
647 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
648 GNUNET_DISK_pipe_close (childpipe);
649 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
650 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
651 &childpipe_read_fd, sizeof (int))) ||
652 (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
655 GNUNET_DISK_file_close (childpipe_read);
657 GNUNET_DISK_file_close (childpipe_write);
658 if (0 <= dup_childpipe_read_fd)
659 close (dup_childpipe_read_fd);
662 childpipe_read_fd = dup_childpipe_read_fd;
663 GNUNET_DISK_file_close (childpipe_read);
667 childpipe_read = NULL;
668 childpipe_write = NULL;
669 childpipe_read_fd = -1;
671 if (NULL != pipe_stdout)
673 GNUNET_assert (GNUNET_OK ==
674 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
676 GNUNET_DISK_PIPE_END_WRITE),
677 &fd_stdout_write, sizeof (int)));
678 GNUNET_assert (GNUNET_OK ==
679 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
680 (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
681 &fd_stdout_read, sizeof (int)));
683 if (NULL != pipe_stdin)
685 GNUNET_assert (GNUNET_OK ==
686 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
687 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
688 &fd_stdin_read, sizeof (int)));
689 GNUNET_assert (GNUNET_OK ==
690 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
691 (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
692 &fd_stdin_write, sizeof (int)));
699 while (-1 != (k = lsocks[i++]))
700 GNUNET_array_append (lscp, ls, k);
701 GNUNET_array_append (lscp, ls, -1);
704 /* see https://gnunet.org/vfork */
712 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
713 GNUNET_array_grow (lscp, ls, 0);
715 GNUNET_DISK_file_close (childpipe_read);
717 GNUNET_DISK_file_close (childpipe_write);
718 if (0 <= childpipe_read_fd)
719 close (childpipe_read_fd);
725 unsetenv (GNUNET_OS_CONTROL_PIPE);
726 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
727 gnunet_proc->pid = ret;
728 gnunet_proc->control_pipe = childpipe_write;
729 if (GNUNET_YES == pipe_control)
731 close (childpipe_read_fd);
733 GNUNET_array_grow (lscp, ls, 0);
736 if (0 <= childpipe_read_fd)
740 /* due to vfork, we must NOT free memory on DARWIN! */
741 GNUNET_DISK_file_close (childpipe_write);
743 snprintf (fdbuf, 100, "%x", childpipe_read_fd);
744 setenv (GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
747 unsetenv (GNUNET_OS_CONTROL_PIPE);
748 if (NULL != pipe_stdin)
750 GNUNET_break (0 == close (fd_stdin_write));
751 if (-1 == dup2 (fd_stdin_read, 0))
752 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
753 GNUNET_break (0 == close (fd_stdin_read));
755 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
757 GNUNET_break (0 == close (0));
758 open_dev_null (0, O_RDONLY);
760 if (NULL != pipe_stdout)
762 GNUNET_break (0 == close (fd_stdout_read));
763 if (-1 == dup2 (fd_stdout_write, 1))
764 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
765 GNUNET_break (0 == close (fd_stdout_write));
767 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
769 GNUNET_break (0 == close (1));
770 open_dev_null (1, O_WRONLY);
772 if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
774 GNUNET_break (0 == close (2));
775 open_dev_null (2, O_WRONLY);
779 /* read systemd documentation... */
780 GNUNET_snprintf (lpid, sizeof (lpid), "%u", getpid ());
781 setenv ("LISTEN_PID", lpid, 1);
784 while (-1 != lscp[i])
787 while (-1 != lscp[j])
793 GNUNET_assert (-1 != k);
794 GNUNET_assert (0 == close (lscp[j]));
802 /* Bury any existing FD, no matter what; they should all be closed
803 * on exec anyway and the important onces have been dup'ed away */
805 GNUNET_assert (-1 != dup2 (lscp[i], tgt));
807 /* unset close-on-exec flag */
808 flags = fcntl (tgt, F_GETFD);
809 GNUNET_assert (flags >= 0);
810 flags &= ~FD_CLOEXEC;
812 (void) fcntl (tgt, F_SETFD, flags);
816 GNUNET_snprintf (fds, sizeof (fds), "%u", i);
817 setenv ("LISTEN_FDS", fds, 1);
820 /* due to vfork, we must NOT free memory on DARWIN! */
821 GNUNET_array_grow (lscp, ls, 0);
823 execvp (filename, argv);
824 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
827 struct GNUNET_DISK_FileHandle *childpipe_read;
828 struct GNUNET_DISK_FileHandle *childpipe_write;
829 HANDLE childpipe_read_handle;
831 char **non_const_argv;
836 PROCESS_INFORMATION proc;
838 struct GNUNET_OS_Process *gnunet_proc;
839 char path[MAX_PATH + 1];
840 char *our_env[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
841 char *env_block = NULL;
849 char *non_const_filename;
850 char win_path[MAX_PATH + 1];
851 struct GNUNET_DISK_PipeHandle *lsocks_pipe;
852 const struct GNUNET_DISK_FileHandle *lsocks_write_fd;
863 HANDLE stdout_handle;
864 HANDLE stdih, stdoh, stdeh;
865 DWORD stdif, stdof, stdef;
869 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
870 return NULL; /* not executable */
872 /* Search in prefix dir (hopefully - the directory from which
873 * the current module was loaded), bindir and libdir, then in PATH
875 self_prefix = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_SELF_PREFIX);
876 bindir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
877 libdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
879 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
882 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
885 pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
888 ptr += sprintf (pathbuf, "%s;%s;%s;", self_prefix, bindir, libdir);
889 GNUNET_free (self_prefix);
890 GNUNET_free (bindir);
891 GNUNET_free (libdir);
893 alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len);
894 if (alloc_len != pathbuf_len - 1)
896 GNUNET_free (pathbuf);
897 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */
901 cmdlen = strlen (filename);
902 if ( (cmdlen < 5) || (0 != strcmp (&filename[cmdlen - 4], ".exe")) )
903 GNUNET_asprintf (&non_const_filename, "%s.exe", filename);
905 GNUNET_asprintf (&non_const_filename, "%s", filename);
907 /* It could be in POSIX form, convert it to a DOS path early on */
908 if (ERROR_SUCCESS != (lRet = plibc_conv_to_win_path (non_const_filename, win_path)))
910 SetErrnoFromWinError (lRet);
911 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "plibc_conv_to_win_path",
913 GNUNET_free (non_const_filename);
914 GNUNET_free (pathbuf);
917 GNUNET_free (non_const_filename);
918 non_const_filename = GNUNET_strdup (win_path);
919 /* Check that this is the full path. If it isn't, search. */
920 /* FIXME: convert it to wchar_t and use SearchPathW?
921 * Remember: arguments to _start_process() are technically in UTF-8...
923 if (non_const_filename[1] == ':')
925 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
926 LOG (GNUNET_ERROR_TYPE_DEBUG,
927 "Using path `%s' as-is. PATH is %s\n", path, ptr);
929 else if (!SearchPathA
930 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char),
933 SetErrnoFromWinError (GetLastError ());
934 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
936 GNUNET_free (non_const_filename);
937 GNUNET_free (pathbuf);
941 LOG (GNUNET_ERROR_TYPE_DEBUG,
942 "Found `%s' in PATH `%s'\n", path, pathbuf);
943 GNUNET_free (pathbuf);
944 GNUNET_free (non_const_filename);
946 /* Count the number of arguments */
947 arg = (char **) argv;
954 /* Allocate a copy argv */
955 non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1));
957 /* Copy all argv strings */
959 arg = (char **) argv;
963 non_const_argv[argcount] = GNUNET_strdup (path);
965 non_const_argv[argcount] = GNUNET_strdup (*arg);
969 non_const_argv[argcount] = NULL;
973 arg = non_const_argv;
976 cmdlen = cmdlen + strlen (*arg) + 4;
980 /* Allocate and create cmd */
981 cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen);
982 arg = non_const_argv;
985 char arg_last_char = (*arg)[strlen (*arg) - 1];
986 idx += sprintf (idx, "\"%s%s\"%s", *arg,
987 arg_last_char == '\\' ? "\\" : "", *(arg + 1) ? " " : "");
992 GNUNET_free (non_const_argv[--argcount]);
993 GNUNET_free (non_const_argv);
995 memset (&start, 0, sizeof (start));
996 start.cb = sizeof (start);
997 if ((pipe_stdin != NULL) || (pipe_stdout != NULL) || (std_inheritance != 0))
998 start.dwFlags |= STARTF_USESTDHANDLES;
1000 stdih = GetStdHandle (STD_INPUT_HANDLE);
1001 GetHandleInformation (stdih, &stdif);
1002 if (pipe_stdin != NULL)
1004 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1005 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
1006 &stdin_handle, sizeof (HANDLE));
1007 start.hStdInput = stdin_handle;
1011 if (std_inheritance & GNUNET_OS_INHERIT_STD_IN)
1013 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 1);
1014 if (pipe_stdin == NULL)
1015 start.hStdInput = stdih;
1018 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 0);
1022 stdoh = GetStdHandle (STD_OUTPUT_HANDLE);
1023 GetHandleInformation (stdoh, &stdof);
1024 if (NULL != pipe_stdout)
1026 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1028 GNUNET_DISK_PIPE_END_WRITE),
1029 &stdout_handle, sizeof (HANDLE));
1030 start.hStdOutput = stdout_handle;
1034 if (std_inheritance & GNUNET_OS_INHERIT_STD_OUT)
1036 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 1);
1037 if (pipe_stdout == NULL)
1038 start.hStdOutput = stdoh;
1041 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 0);
1044 stdeh = GetStdHandle (STD_ERROR_HANDLE);
1045 GetHandleInformation (stdeh, &stdef);
1048 if (std_inheritance & GNUNET_OS_INHERIT_STD_ERR)
1050 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 1);
1051 start.hStdError = stdeh;
1054 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 0);
1057 if (GNUNET_YES == pipe_control)
1059 struct GNUNET_DISK_PipeHandle *childpipe;
1060 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
1061 if (NULL == childpipe)
1063 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
1064 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
1065 GNUNET_DISK_pipe_close (childpipe);
1066 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
1067 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
1068 &childpipe_read_handle, sizeof (HANDLE))))
1071 GNUNET_DISK_file_close (childpipe_read);
1072 if (childpipe_write)
1073 GNUNET_DISK_file_close (childpipe_write);
1077 /* Unlike *nix variant, we don't dup the handle, so can't close
1078 * filehandle right now.
1080 SetHandleInformation (childpipe_read_handle, HANDLE_FLAG_INHERIT, 1);
1084 childpipe_read = NULL;
1085 childpipe_write = NULL;
1088 if (lsocks != NULL && lsocks[0] != INVALID_SOCKET)
1090 lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1092 if (lsocks_pipe == NULL)
1095 GNUNET_DISK_pipe_close (lsocks_pipe);
1096 if (GNUNET_YES == pipe_control)
1098 GNUNET_DISK_file_close (childpipe_write);
1099 GNUNET_DISK_file_close (childpipe_read);
1103 lsocks_write_fd = GNUNET_DISK_pipe_handle (lsocks_pipe,
1104 GNUNET_DISK_PIPE_END_WRITE);
1105 GNUNET_DISK_internal_file_handle_ (lsocks_write_fd,
1106 &lsocks_write, sizeof (HANDLE));
1107 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1108 (lsocks_pipe, GNUNET_DISK_PIPE_END_READ),
1109 &lsocks_read, sizeof (HANDLE));
1115 if (GNUNET_YES == pipe_control)
1117 GNUNET_asprintf (&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE);
1118 GNUNET_asprintf (&our_env[env_off++], "%p", childpipe_read_handle);
1120 if ( (lsocks != NULL) && (lsocks[0] != INVALID_SOCKET))
1122 /*This will tell the child that we're going to send lsocks over the pipe*/
1123 GNUNET_asprintf (&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS");
1124 GNUNET_asprintf (&our_env[env_off++], "%lu", lsocks_read);
1126 our_env[env_off++] = NULL;
1127 env_block = CreateCustomEnvTable (our_env);
1129 GNUNET_free_non_null (our_env[--env_off]);
1132 if (NULL == (wpath = u8_to_u16 ((uint8_t *) path, 1 + strlen (path), NULL, &wpath_len)))
1134 LOG (GNUNET_ERROR_TYPE_DEBUG,
1135 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", path, errno);
1136 GNUNET_free (env_block);
1139 GNUNET_DISK_pipe_close (lsocks_pipe);
1140 if (GNUNET_YES == pipe_control)
1142 GNUNET_DISK_file_close (childpipe_write);
1143 GNUNET_DISK_file_close (childpipe_read);
1149 if (NULL == (wcmd = u8_to_u16 ((uint8_t *) cmd, 1 + strlen (cmd), NULL, &wcmd_len)))
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", cmd, errno);
1153 GNUNET_free (env_block);
1157 GNUNET_DISK_pipe_close (lsocks_pipe);
1158 if (GNUNET_YES == pipe_control)
1160 GNUNET_DISK_file_close (childpipe_write);
1161 GNUNET_DISK_file_close (childpipe_read);
1166 bresult = CreateProcessW (wpath, wcmd, NULL, NULL, GNUNET_YES,
1167 DETACHED_PROCESS | CREATE_SUSPENDED, env_block, NULL, &start, &proc);
1168 error_code = GetLastError ();
1170 if ((NULL == pipe_stdin) && (stdih))
1171 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, stdif);
1174 if ((NULL == pipe_stdout) && (stdoh))
1175 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, stdof);
1178 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, stdef);
1181 LOG (GNUNET_ERROR_TYPE_ERROR, "CreateProcess(%s, %s) failed: %lu\n", path, cmd, error_code);
1183 GNUNET_free (env_block);
1187 if (GNUNET_YES == pipe_control)
1189 GNUNET_DISK_file_close (childpipe_read);
1194 if (GNUNET_YES == pipe_control)
1196 GNUNET_DISK_file_close (childpipe_write);
1199 GNUNET_DISK_pipe_close (lsocks_pipe);
1200 SetErrnoFromWinError (error_code);
1204 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
1205 gnunet_proc->pid = proc.dwProcessId;
1206 gnunet_proc->handle = proc.hProcess;
1207 gnunet_proc->control_pipe = childpipe_write;
1209 CreateThread (NULL, 64000, &child_wait_thread, (void *) gnunet_proc, 0, NULL);
1211 ResumeThread (proc.hThread);
1212 CloseHandle (proc.hThread);
1214 if ( (NULL == lsocks) || (INVALID_SOCKET == lsocks[0]) )
1217 GNUNET_DISK_pipe_close_end (lsocks_pipe, GNUNET_DISK_PIPE_END_READ);
1219 /* This is a replacement for "goto error" that doesn't use goto */
1228 /* Tell the number of sockets */
1229 for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++);
1231 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1232 if (sizeof (count) != wrote)
1234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1235 "Failed to write %u count bytes to the child: %u\n",
1236 sizeof (count), GetLastError ());
1239 for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++)
1241 WSAPROTOCOL_INFOA pi;
1242 /* Get a socket duplication info */
1243 if (SOCKET_ERROR == WSADuplicateSocketA (lsocks[i], gnunet_proc->pid, &pi))
1245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1246 "Failed to duplicate an socket[%llu]: %u\n", i,
1250 /* Synchronous I/O is not nice, but we can't schedule this:
1251 * lsocks will be closed/freed by the caller soon, and until
1252 * the child creates a duplicate, closing a socket here will
1253 * close it for good.
1255 /* Send the size of the structure
1256 * (the child might be built with different headers...)
1259 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &size, sizeof (size));
1260 if (sizeof (size) != wrote)
1262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1263 "Failed to write %u size[%llu] bytes to the child: %u\n",
1264 sizeof (size), i, GetLastError ());
1267 /* Finally! Send the data */
1268 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &pi, sizeof (pi));
1269 if (sizeof (pi) != wrote)
1271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1272 "Failed to write %u socket[%llu] bytes to the child: %u\n",
1273 sizeof (pi), i, GetLastError ());
1277 /* This will block us until the child makes a final read or closes
1278 * the pipe (hence no 'wrote' check), since we have to wait for it
1279 * to duplicate the last socket, before we return and start closing
1282 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1287 GNUNET_DISK_file_sync (lsocks_write_fd);
1288 GNUNET_DISK_pipe_close (lsocks_pipe);
1292 /* If we can't pass on the socket(s), the child will block forever,
1293 * better put it out of its misery.
1295 SafeTerminateProcess (gnunet_proc->handle, 0, 0);
1296 CloseHandle (gnunet_proc->handle);
1297 if (NULL != gnunet_proc->control_pipe)
1298 GNUNET_DISK_file_close (gnunet_proc->control_pipe);
1299 GNUNET_free (gnunet_proc);
1312 * @param pipe_control should a pipe be used to send signals to the child?
1313 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1314 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1315 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1316 * @param filename name of the binary
1317 * @param argv NULL-terminated array of arguments to the process
1318 * @return pointer to process structure of the new process, NULL on error
1320 struct GNUNET_OS_Process *
1321 GNUNET_OS_start_process_vap (int pipe_control,
1322 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1323 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1324 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1325 const char *filename,
1328 return start_process (pipe_control,
1341 * @param pipe_control should a pipe be used to send signals to the child?
1342 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1343 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1344 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1345 * @param filename name of the binary
1346 * @param va NULL-terminated list of arguments to the process
1347 * @return pointer to process structure of the new process, NULL on error
1349 struct GNUNET_OS_Process *
1350 GNUNET_OS_start_process_va (int pipe_control,
1351 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1352 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1353 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1354 const char *filename, va_list va)
1356 struct GNUNET_OS_Process *ret;
1363 while (NULL != va_arg (ap, char *))
1366 argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
1369 while (NULL != (argv[argc] = va_arg (ap, char *)))
1372 ret = GNUNET_OS_start_process_vap (pipe_control,
1386 * @param pipe_control should a pipe be used to send signals to the child?
1387 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1388 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1389 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1390 * @param filename name of the binary
1391 * @param ... NULL-terminated list of arguments to the process
1392 * @return pointer to process structure of the new process, NULL on error
1394 struct GNUNET_OS_Process *
1395 GNUNET_OS_start_process (int pipe_control,
1396 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1397 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1398 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1399 const char *filename, ...)
1401 struct GNUNET_OS_Process *ret;
1404 va_start (ap, filename);
1405 ret = GNUNET_OS_start_process_va (pipe_control, std_inheritance, pipe_stdin,
1406 pipe_stdout, filename, ap);
1415 * @param pipe_control should a pipe be used to send signals to the child?
1416 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
1417 * std handles of the parent are inherited by the child.
1418 * pipe_stdin and pipe_stdout take priority over std_inheritance
1419 * (when they are non-NULL).
1420 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1421 * must be NULL on platforms where dup is not supported
1422 * @param filename name of the binary
1423 * @param argv NULL-terminated list of arguments to the process
1424 * @return process ID of the new process, -1 on error
1426 struct GNUNET_OS_Process *
1427 GNUNET_OS_start_process_v (int pipe_control,
1428 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1429 const SOCKTYPE *lsocks,
1430 const char *filename,
1433 return start_process (pipe_control,
1444 * Retrieve the status of a process, waiting on him if dead.
1445 * Nonblocking version.
1447 * @param proc process ID
1448 * @param type status type
1449 * @param code return code/signal number
1450 * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise
1453 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1454 enum GNUNET_OS_ProcessStatusType *type,
1455 unsigned long *code)
1461 GNUNET_assert (0 != proc);
1462 ret = waitpid (proc->pid, &status, WNOHANG);
1465 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1466 return GNUNET_SYSERR;
1470 *type = GNUNET_OS_PROCESS_RUNNING;
1474 if (proc->pid != ret)
1476 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1477 return GNUNET_SYSERR;
1479 if (WIFEXITED (status))
1481 *type = GNUNET_OS_PROCESS_EXITED;
1482 *code = WEXITSTATUS (status);
1484 else if (WIFSIGNALED (status))
1486 *type = GNUNET_OS_PROCESS_SIGNALED;
1487 *code = WTERMSIG (status);
1489 else if (WIFSTOPPED (status))
1491 *type = GNUNET_OS_PROCESS_SIGNALED;
1492 *code = WSTOPSIG (status);
1495 else if (WIFCONTINUED (status))
1497 *type = GNUNET_OS_PROCESS_RUNNING;
1503 *type = GNUNET_OS_PROCESS_UNKNOWN;
1508 DWORD c, error_code, ret;
1512 if (h == NULL || ret == 0)
1514 LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n",
1516 return GNUNET_SYSERR;
1519 h = GetCurrentProcess ();
1522 ret = GetExitCodeProcess (h, &c);
1523 error_code = GetLastError ();
1524 if (ret == 0 || error_code != NO_ERROR)
1526 SetErrnoFromWinError (error_code);
1527 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
1528 return GNUNET_SYSERR;
1530 if (STILL_ACTIVE == c)
1532 *type = GNUNET_OS_PROCESS_RUNNING;
1536 *type = GNUNET_OS_PROCESS_EXITED;
1545 * Wait for a process
1547 * @param proc pointer to process structure
1548 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1551 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1554 pid_t pid = proc->pid;
1557 while ( (pid != (ret = waitpid (pid, NULL, 0))) &&
1558 (EINTR == errno) ) ;
1561 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1562 return GNUNET_SYSERR;
1571 LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n",
1573 return GNUNET_SYSERR;
1576 h = GetCurrentProcess ();
1578 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1580 SetErrnoFromWinError (GetLastError ());
1581 return GNUNET_SYSERR;
1589 * Handle to a command.
1591 struct GNUNET_OS_CommandHandle
1597 struct GNUNET_OS_Process *eip;
1600 * Handle to the output pipe.
1602 struct GNUNET_DISK_PipeHandle *opipe;
1605 * Read-end of output pipe.
1607 const struct GNUNET_DISK_FileHandle *r;
1610 * Function to call on each line of output.
1612 GNUNET_OS_LineProcessor proc;
1615 * Closure for 'proc'.
1620 * Buffer for the output.
1625 * Task reading from pipe.
1627 GNUNET_SCHEDULER_TaskIdentifier rtask;
1632 struct GNUNET_TIME_Absolute timeout;
1635 * Current read offset in buf.
1642 * Stop/kill a command. Must ONLY be called either from
1643 * the callback after 'NULL' was passed for 'line' *OR*
1644 * from an independent task (not within the line processor).
1646 * @param cmd handle to the process
1649 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
1651 if (NULL != cmd->proc)
1653 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cmd->rtask);
1654 GNUNET_SCHEDULER_cancel (cmd->rtask);
1656 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1657 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
1658 GNUNET_OS_process_destroy (cmd->eip);
1659 GNUNET_DISK_pipe_close (cmd->opipe);
1665 * Read from the process and call the line processor.
1667 * @param cls the 'struct GNUNET_OS_CommandHandle'
1668 * @param tc scheduler context
1671 cmd_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1673 struct GNUNET_OS_CommandHandle *cmd = cls;
1674 GNUNET_OS_LineProcessor proc;
1678 cmd->rtask = GNUNET_SCHEDULER_NO_TASK;
1679 if (GNUNET_YES != GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r))
1681 /* timeout, shutdown, etc. */
1684 proc (cmd->proc_cls, NULL);
1688 GNUNET_DISK_file_read (cmd->r, &cmd->buf[cmd->off],
1689 sizeof (cmd->buf) - cmd->off);
1692 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
1694 cmd->buf[cmd->off] = '\0';
1695 cmd->proc (cmd->proc_cls, cmd->buf);
1699 proc (cmd->proc_cls, NULL);
1702 end = memchr (&cmd->buf[cmd->off], '\n', ret);
1707 cmd->proc (cmd->proc_cls, cmd->buf);
1708 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1709 cmd->off -= (end + 1 - cmd->buf);
1710 end = memchr (cmd->buf, '\n', cmd->off);
1713 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
1714 (cmd->timeout), cmd->r, &cmd_read, cmd);
1719 * Run the given command line and call the given function
1720 * for each line of the output.
1722 * @param proc function to call for each line of the output
1723 * @param proc_cls closure for proc
1724 * @param timeout when to time out
1725 * @param binary command to run
1726 * @param ... arguments to command
1727 * @return NULL on error
1729 struct GNUNET_OS_CommandHandle *
1730 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
1731 struct GNUNET_TIME_Relative timeout, const char *binary,
1734 struct GNUNET_OS_CommandHandle *cmd;
1735 struct GNUNET_OS_Process *eip;
1736 struct GNUNET_DISK_PipeHandle *opipe;
1739 opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
1742 va_start (ap, binary);
1743 /* redirect stdout, don't inherit stderr/stdin */
1744 eip = GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL, opipe, binary, ap);
1748 GNUNET_DISK_pipe_close (opipe);
1751 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
1752 cmd = GNUNET_malloc (sizeof (struct GNUNET_OS_CommandHandle));
1753 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1757 cmd->proc_cls = proc_cls;
1758 cmd->r = GNUNET_DISK_pipe_handle (opipe, GNUNET_DISK_PIPE_END_READ);
1759 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd);
1764 /* end of os_priority.c */