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) 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 "=");
166 /* Gcc will issue a warning here. What to do with it? */
168 control_pipe = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) pipe_fd);
170 control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
172 if (NULL == control_pipe)
174 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", env_buf);
175 putenv (GNUNET_OS_CONTROL_PIPE "=");
178 LOG (GNUNET_ERROR_TYPE_DEBUG,
179 "Adding parent control handler pipe `%s' to the scheduler\n", env_buf);
180 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, control_pipe,
181 &parent_control_handler, control_pipe);
182 putenv (GNUNET_OS_CONTROL_PIPE "=");
187 * Get process structure for current process
189 * The pointer it returns points to static memory location and must not be
192 * @return pointer to the process sturcutre for this process
194 struct GNUNET_OS_Process *
195 GNUNET_OS_process_current ()
198 current_process.pid = GetCurrentProcessId ();
199 current_process.handle = GetCurrentProcess ();
201 current_process.pid = 0;
203 return ¤t_process;
208 * Sends a signal to the process
210 * @param proc pointer to process structure
212 * @return 0 on success, -1 on error
215 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
221 if (NULL != proc->control_pipe)
223 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending signal %d to pid: %u via pipe\n", sig, proc->pid);
224 ret = GNUNET_DISK_file_write (proc->control_pipe, &csig, sizeof (csig));
225 if (sizeof (csig) == ret)
228 /* pipe failed or non-existent, try other methods */
231 #if !defined (WINDOWS)
237 #if defined(WINDOWS) && !defined(__CYGWIN__)
240 int must_kill = GNUNET_YES;
241 if (0 != GetExitCodeProcess (proc->handle, &exitcode))
242 must_kill = (exitcode == STILL_ACTIVE) ? GNUNET_YES : GNUNET_NO;
243 if (GNUNET_YES == must_kill)
244 if (0 == SafeTerminateProcess (proc->handle, 0, 0))
246 DWORD error_code = GetLastError ();
247 if ((error_code != WAIT_TIMEOUT) && (error_code != ERROR_PROCESS_ABORTED))
249 LOG ((error_code == ERROR_ACCESS_DENIED) ?
250 GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
251 "SafeTermiateProcess failed with code %lu\n", error_code);
252 /* The problem here is that a process that is already dying
253 * might cause SafeTerminateProcess to fail with
254 * ERROR_ACCESS_DENIED, but the process WILL die eventually.
255 * If we really had a permissions problem, hanging up (which
256 * is what will happen in process_wait() in that case) is
259 if (ERROR_ACCESS_DENIED == error_code)
265 SetErrnoFromWinError (error_code);
273 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending signal %d to pid: %u via system call\n", sig, proc->pid);
274 return PLIBC_KILL (proc->pid, sig);
277 #if defined (WINDOWS)
281 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending signal %d to pid: %u via system call\n", sig, proc->pid);
282 return PLIBC_KILL (proc->pid, sig);
288 * Get the pid of the process in question
290 * @param proc the process to get the pid of
292 * @return the current process id
295 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process * proc)
302 * Cleans up process structure contents (OS-dependent) and deallocates it
304 * @param proc pointer to process structure
307 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
309 if (NULL != proc->control_pipe)
310 GNUNET_DISK_file_close (proc->control_pipe);
311 #if defined (WINDOWS)
312 if (proc->handle != NULL)
313 CloseHandle (proc->handle);
319 #include "gnunet_signal_lib.h"
321 extern GNUNET_SIGNAL_Handler w32_sigchld_handler;
324 * Make seaspider happy.
326 #define DWORD_WINAPI DWORD WINAPI
329 * @brief Waits for a process to terminate and invokes the SIGCHLD handler
330 * @param proc pointer to process structure
333 child_wait_thread (void *arg)
335 struct GNUNET_OS_Process *proc = (struct GNUNET_OS_Process *) arg;
337 WaitForSingleObject (proc->handle, INFINITE);
339 if (w32_sigchld_handler)
340 w32_sigchld_handler ();
348 * Set process priority
350 * @param proc pointer to process structure
351 * @param prio priority value
352 * @return GNUNET_OK on success, GNUNET_SYSERR on error
355 GNUNET_OS_set_process_priority (struct GNUNET_OS_Process *proc,
356 enum GNUNET_SCHEDULER_Priority prio)
360 GNUNET_assert (prio < GNUNET_SCHEDULER_PRIORITY_COUNT);
361 if (GNUNET_SCHEDULER_PRIORITY_KEEP == prio)
364 /* convert to MINGW/Unix values */
367 case GNUNET_SCHEDULER_PRIORITY_UI:
368 case GNUNET_SCHEDULER_PRIORITY_URGENT:
370 rprio = HIGH_PRIORITY_CLASS;
376 case GNUNET_SCHEDULER_PRIORITY_HIGH:
378 rprio = ABOVE_NORMAL_PRIORITY_CLASS;
384 case GNUNET_SCHEDULER_PRIORITY_DEFAULT:
386 rprio = NORMAL_PRIORITY_CLASS;
392 case GNUNET_SCHEDULER_PRIORITY_BACKGROUND:
394 rprio = BELOW_NORMAL_PRIORITY_CLASS;
400 case GNUNET_SCHEDULER_PRIORITY_IDLE:
402 rprio = IDLE_PRIORITY_CLASS;
409 return GNUNET_SYSERR;
412 /* Set process priority */
415 HANDLE h = proc->handle;
417 GNUNET_assert (h != NULL);
418 SetPriorityClass (h, rprio);
424 if ((0 == pid) || (pid == getpid ()))
427 int delta = rprio - have;
430 if ((delta != 0) && (rprio == nice (delta)) && (errno != 0))
432 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "nice");
433 return GNUNET_SYSERR;
438 if (0 != setpriority (PRIO_PROCESS, pid, rprio))
440 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
442 return GNUNET_SYSERR;
446 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
447 "Priority management not availabe for this platform\n");
455 CreateCustomEnvTable (char **vars)
457 char *win32_env_table;
462 size_t tablesize = 0;
463 size_t items_count = 0;
472 win32_env_table = GetEnvironmentStringsA ();
473 if (NULL == win32_env_table)
475 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ;
477 index = GNUNET_malloc (sizeof (char *) * n_var);
478 for (c = 0; c < n_var; c++)
480 for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++)
482 size_t len = strlen (ptr);
485 for (var_ptr = vars; *var_ptr; var_ptr++)
489 var_len = strlen (var);
490 if (strncmp (var, ptr, var_len) == 0)
494 tablesize += var_len + strlen (val) + 1;
499 tablesize += len + 1;
502 for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
507 n_found += strlen (var) + strlen (val) + 1;
509 result = GNUNET_malloc (tablesize + n_found + 1);
510 for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
512 size_t len = strlen (ptr);
515 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
519 var_len = strlen (var);
520 if (strncmp (var, ptr, var_len) == 0)
528 strcpy (result_ptr, ptr);
529 result_ptr += len + 1;
533 strcpy (result_ptr, var);
534 result_ptr += var_len;
535 strcpy (result_ptr, val);
536 result_ptr += strlen (val) + 1;
540 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
544 var_len = strlen (var);
547 strcpy (result_ptr, var);
548 result_ptr += var_len;
549 strcpy (result_ptr, val);
550 result_ptr += strlen (val) + 1;
553 FreeEnvironmentStrings (win32_env_table);
562 * Open '/dev/null' and make the result the given
565 * @param target_fd desired FD to point to /dev/null
566 * @param flags open flags (O_RDONLY, O_WRONLY)
569 open_dev_null (int target_fd,
574 fd = open ("/dev/null", flags);
577 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", "/dev/null");
582 if (-1 == dup2 (fd, target_fd))
584 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
588 GNUNET_break (0 == close (fd));
596 * @param pipe_control should a pipe be used to send signals to the child?
597 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
598 * std handles of the parent are inherited by the child.
599 * pipe_stdin and pipe_stdout take priority over std_inheritance
600 * (when they are non-NULL).
601 * @param pipe_stdin pipe to use to send input to child process (or NULL)
602 * @param pipe_stdout pipe to use to get output from child process (or NULL)
603 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
604 * must be NULL on platforms where dup is not supported
605 * @param filename name of the binary
606 * @param argv NULL-terminated list of arguments to the process
607 * @return process ID of the new process, -1 on error
609 static struct GNUNET_OS_Process *
610 start_process (int pipe_control,
611 enum GNUNET_OS_InheritStdioFlags std_inheritance,
612 struct GNUNET_DISK_PipeHandle *pipe_stdin,
613 struct GNUNET_DISK_PipeHandle *pipe_stdout,
614 const SOCKTYPE *lsocks,
615 const char *filename,
622 struct GNUNET_OS_Process *gnunet_proc;
623 struct GNUNET_DISK_FileHandle *childpipe_read;
624 struct GNUNET_DISK_FileHandle *childpipe_write;
625 int childpipe_read_fd;
638 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename))
639 return NULL; /* not executable */
640 if (GNUNET_YES == pipe_control)
642 struct GNUNET_DISK_PipeHandle *childpipe;
643 int dup_childpipe_read_fd = -1;
644 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
645 if (NULL == childpipe)
647 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
648 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
649 GNUNET_DISK_pipe_close (childpipe);
650 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
651 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
652 &childpipe_read_fd, sizeof (int))) ||
653 (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
656 GNUNET_DISK_file_close (childpipe_read);
658 GNUNET_DISK_file_close (childpipe_write);
659 if (0 <= dup_childpipe_read_fd)
660 close (dup_childpipe_read_fd);
663 childpipe_read_fd = dup_childpipe_read_fd;
664 GNUNET_DISK_file_close (childpipe_read);
668 childpipe_read = NULL;
669 childpipe_write = NULL;
670 childpipe_read_fd = -1;
672 if (NULL != pipe_stdout)
674 GNUNET_assert (GNUNET_OK ==
675 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
677 GNUNET_DISK_PIPE_END_WRITE),
678 &fd_stdout_write, sizeof (int)));
679 GNUNET_assert (GNUNET_OK ==
680 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
681 (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
682 &fd_stdout_read, sizeof (int)));
684 if (NULL != pipe_stdin)
686 GNUNET_assert (GNUNET_OK ==
687 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
688 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
689 &fd_stdin_read, sizeof (int)));
690 GNUNET_assert (GNUNET_OK ==
691 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
692 (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
693 &fd_stdin_write, sizeof (int)));
700 while (-1 != (k = lsocks[i++]))
701 GNUNET_array_append (lscp, ls, k);
702 GNUNET_array_append (lscp, ls, -1);
705 /* see https://gnunet.org/vfork */
713 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
714 GNUNET_array_grow (lscp, ls, 0);
716 GNUNET_DISK_file_close (childpipe_read);
718 GNUNET_DISK_file_close (childpipe_write);
719 if (0 <= childpipe_read_fd)
720 close (childpipe_read_fd);
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))
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);
1078 /* Unlike *nix variant, we don't dup the handle, so can't close
1079 * filehandle right now.
1081 SetHandleInformation (childpipe_read_handle, HANDLE_FLAG_INHERIT, 1);
1085 childpipe_read = NULL;
1086 childpipe_write = NULL;
1089 if (lsocks != NULL && lsocks[0] != INVALID_SOCKET)
1091 lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1093 if (lsocks_pipe == NULL)
1097 GNUNET_DISK_pipe_close (lsocks_pipe);
1098 if (GNUNET_YES == pipe_control)
1100 GNUNET_DISK_file_close (childpipe_write);
1101 GNUNET_DISK_file_close (childpipe_read);
1105 lsocks_write_fd = GNUNET_DISK_pipe_handle (lsocks_pipe,
1106 GNUNET_DISK_PIPE_END_WRITE);
1107 GNUNET_DISK_internal_file_handle_ (lsocks_write_fd,
1108 &lsocks_write, sizeof (HANDLE));
1109 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1110 (lsocks_pipe, GNUNET_DISK_PIPE_END_READ),
1111 &lsocks_read, sizeof (HANDLE));
1117 if (GNUNET_YES == pipe_control)
1119 GNUNET_asprintf (&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE);
1120 GNUNET_asprintf (&our_env[env_off++], "%p", childpipe_read_handle);
1122 if ( (lsocks != NULL) && (lsocks[0] != INVALID_SOCKET))
1124 /*This will tell the child that we're going to send lsocks over the pipe*/
1125 GNUNET_asprintf (&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS");
1126 GNUNET_asprintf (&our_env[env_off++], "%lu", lsocks_read);
1128 our_env[env_off++] = NULL;
1129 env_block = CreateCustomEnvTable (our_env);
1131 GNUNET_free_non_null (our_env[--env_off]);
1134 if (NULL == (wpath = u8_to_u16 ((uint8_t *) path, 1 + strlen (path), NULL, &wpath_len)))
1136 LOG (GNUNET_ERROR_TYPE_DEBUG,
1137 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", path, errno);
1138 GNUNET_free (env_block);
1141 GNUNET_DISK_pipe_close (lsocks_pipe);
1142 if (GNUNET_YES == pipe_control)
1144 GNUNET_DISK_file_close (childpipe_write);
1145 GNUNET_DISK_file_close (childpipe_read);
1151 if (NULL == (wcmd = u8_to_u16 ((uint8_t *) cmd, 1 + strlen (cmd), NULL, &wcmd_len)))
1153 LOG (GNUNET_ERROR_TYPE_DEBUG,
1154 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", cmd, errno);
1155 GNUNET_free (env_block);
1159 GNUNET_DISK_pipe_close (lsocks_pipe);
1160 if (GNUNET_YES == pipe_control)
1162 GNUNET_DISK_file_close (childpipe_write);
1163 GNUNET_DISK_file_close (childpipe_read);
1168 bresult = CreateProcessW (wpath, wcmd, NULL, NULL, TRUE,
1169 DETACHED_PROCESS | CREATE_SUSPENDED, env_block, NULL, &start, &proc);
1170 error_code = GetLastError ();
1172 if ((NULL == pipe_stdin) && (stdih))
1173 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, stdif);
1176 if ((NULL == pipe_stdout) && (stdoh))
1177 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, stdof);
1180 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, stdef);
1183 LOG (GNUNET_ERROR_TYPE_ERROR, "CreateProcess(%s, %s) failed: %lu\n", path, cmd, error_code);
1185 GNUNET_free (env_block);
1190 if (GNUNET_YES == pipe_control)
1192 GNUNET_DISK_file_close (childpipe_read);
1197 if (GNUNET_YES == pipe_control)
1199 GNUNET_DISK_file_close (childpipe_write);
1202 GNUNET_DISK_pipe_close (lsocks_pipe);
1203 SetErrnoFromWinError (error_code);
1207 gnunet_proc = GNUNET_malloc (sizeof (struct GNUNET_OS_Process));
1208 gnunet_proc->pid = proc.dwProcessId;
1209 gnunet_proc->handle = proc.hProcess;
1210 gnunet_proc->control_pipe = childpipe_write;
1212 CreateThread (NULL, 64000, &child_wait_thread, (void *) gnunet_proc, 0, NULL);
1214 ResumeThread (proc.hThread);
1215 CloseHandle (proc.hThread);
1217 if ( (NULL == lsocks) || (INVALID_SOCKET == lsocks[0]) )
1220 GNUNET_DISK_pipe_close_end (lsocks_pipe, GNUNET_DISK_PIPE_END_READ);
1222 /* This is a replacement for "goto error" that doesn't use goto */
1231 /* Tell the number of sockets */
1232 for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++);
1234 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1235 if (sizeof (count) != wrote)
1237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1238 "Failed to write %u count bytes to the child: %u\n",
1239 sizeof (count), GetLastError ());
1242 for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++)
1244 WSAPROTOCOL_INFOA pi;
1245 /* Get a socket duplication info */
1246 if (SOCKET_ERROR == WSADuplicateSocketA (lsocks[i], gnunet_proc->pid, &pi))
1248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1249 "Failed to duplicate an socket[%llu]: %u\n", i,
1253 /* Synchronous I/O is not nice, but we can't schedule this:
1254 * lsocks will be closed/freed by the caller soon, and until
1255 * the child creates a duplicate, closing a socket here will
1256 * close it for good.
1258 /* Send the size of the structure
1259 * (the child might be built with different headers...)
1262 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &size, sizeof (size));
1263 if (sizeof (size) != wrote)
1265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1266 "Failed to write %u size[%llu] bytes to the child: %u\n",
1267 sizeof (size), i, GetLastError ());
1270 /* Finally! Send the data */
1271 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &pi, sizeof (pi));
1272 if (sizeof (pi) != wrote)
1274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1275 "Failed to write %u socket[%llu] bytes to the child: %u\n",
1276 sizeof (pi), i, GetLastError ());
1280 /* This will block us until the child makes a final read or closes
1281 * the pipe (hence no 'wrote' check), since we have to wait for it
1282 * to duplicate the last socket, before we return and start closing
1285 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1290 GNUNET_DISK_file_sync (lsocks_write_fd);
1291 GNUNET_DISK_pipe_close (lsocks_pipe);
1295 /* If we can't pass on the socket(s), the child will block forever,
1296 * better put it out of its misery.
1298 SafeTerminateProcess (gnunet_proc->handle, 0, 0);
1299 CloseHandle (gnunet_proc->handle);
1300 if (NULL != gnunet_proc->control_pipe)
1301 GNUNET_DISK_file_close (gnunet_proc->control_pipe);
1302 GNUNET_free (gnunet_proc);
1315 * @param pipe_control should a pipe be used to send signals to the child?
1316 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1317 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1318 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1319 * @param filename name of the binary
1320 * @param argv NULL-terminated array of arguments to the process
1321 * @return pointer to process structure of the new process, NULL on error
1323 struct GNUNET_OS_Process *
1324 GNUNET_OS_start_process_vap (int pipe_control,
1325 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1326 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1327 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1328 const char *filename,
1331 return start_process (pipe_control,
1344 * @param pipe_control should a pipe be used to send signals to the child?
1345 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1346 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1347 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1348 * @param filename name of the binary
1349 * @param va NULL-terminated list of arguments to the process
1350 * @return pointer to process structure of the new process, NULL on error
1352 struct GNUNET_OS_Process *
1353 GNUNET_OS_start_process_va (int pipe_control,
1354 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1355 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1356 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1357 const char *filename, va_list va)
1359 struct GNUNET_OS_Process *ret;
1366 while (NULL != va_arg (ap, char *))
1369 argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
1372 while (NULL != (argv[argc] = va_arg (ap, char *)))
1375 ret = GNUNET_OS_start_process_vap (pipe_control,
1389 * @param pipe_control should a pipe be used to send signals to the child?
1390 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1391 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1392 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1393 * @param filename name of the binary
1394 * @param ... NULL-terminated list of arguments to the process
1395 * @return pointer to process structure of the new process, NULL on error
1397 struct GNUNET_OS_Process *
1398 GNUNET_OS_start_process (int pipe_control,
1399 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1400 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1401 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1402 const char *filename, ...)
1404 struct GNUNET_OS_Process *ret;
1407 va_start (ap, filename);
1408 ret = GNUNET_OS_start_process_va (pipe_control, std_inheritance, pipe_stdin,
1409 pipe_stdout, filename, ap);
1418 * @param pipe_control should a pipe be used to send signals to the child?
1419 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
1420 * std handles of the parent are inherited by the child.
1421 * pipe_stdin and pipe_stdout take priority over std_inheritance
1422 * (when they are non-NULL).
1423 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1424 * must be NULL on platforms where dup is not supported
1425 * @param filename name of the binary
1426 * @param argv NULL-terminated list of arguments to the process
1427 * @return process ID of the new process, -1 on error
1429 struct GNUNET_OS_Process *
1430 GNUNET_OS_start_process_v (int pipe_control,
1431 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1432 const SOCKTYPE *lsocks,
1433 const char *filename,
1436 return start_process (pipe_control,
1447 * Retrieve the status of a process, waiting on him if dead.
1448 * Nonblocking version.
1450 * @param proc process ID
1451 * @param type status type
1452 * @param code return code/signal number
1453 * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise
1456 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1457 enum GNUNET_OS_ProcessStatusType *type,
1458 unsigned long *code)
1464 GNUNET_assert (0 != proc);
1465 ret = waitpid (proc->pid, &status, WNOHANG);
1468 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1469 return GNUNET_SYSERR;
1473 *type = GNUNET_OS_PROCESS_RUNNING;
1477 if (proc->pid != ret)
1479 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1480 return GNUNET_SYSERR;
1482 if (WIFEXITED (status))
1484 *type = GNUNET_OS_PROCESS_EXITED;
1485 *code = WEXITSTATUS (status);
1487 else if (WIFSIGNALED (status))
1489 *type = GNUNET_OS_PROCESS_SIGNALED;
1490 *code = WTERMSIG (status);
1492 else if (WIFSTOPPED (status))
1494 *type = GNUNET_OS_PROCESS_SIGNALED;
1495 *code = WSTOPSIG (status);
1498 else if (WIFCONTINUED (status))
1500 *type = GNUNET_OS_PROCESS_RUNNING;
1506 *type = GNUNET_OS_PROCESS_UNKNOWN;
1511 DWORD c, error_code, ret;
1515 if (h == NULL || ret == 0)
1517 LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n",
1519 return GNUNET_SYSERR;
1522 h = GetCurrentProcess ();
1525 ret = GetExitCodeProcess (h, &c);
1526 error_code = GetLastError ();
1527 if (ret == 0 || error_code != NO_ERROR)
1529 SetErrnoFromWinError (error_code);
1530 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
1531 return GNUNET_SYSERR;
1533 if (STILL_ACTIVE == c)
1535 *type = GNUNET_OS_PROCESS_RUNNING;
1539 *type = GNUNET_OS_PROCESS_EXITED;
1548 * Wait for a process
1550 * @param proc pointer to process structure
1551 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1554 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1557 pid_t pid = proc->pid;
1560 while ( (pid != (ret = waitpid (pid, NULL, 0))) &&
1561 (EINTR == errno) ) ;
1564 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1565 return GNUNET_SYSERR;
1574 LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n",
1576 return GNUNET_SYSERR;
1579 h = GetCurrentProcess ();
1581 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1583 SetErrnoFromWinError (GetLastError ());
1584 return GNUNET_SYSERR;
1592 * Handle to a command.
1594 struct GNUNET_OS_CommandHandle
1600 struct GNUNET_OS_Process *eip;
1603 * Handle to the output pipe.
1605 struct GNUNET_DISK_PipeHandle *opipe;
1608 * Read-end of output pipe.
1610 const struct GNUNET_DISK_FileHandle *r;
1613 * Function to call on each line of output.
1615 GNUNET_OS_LineProcessor proc;
1618 * Closure for 'proc'.
1623 * Buffer for the output.
1628 * Task reading from pipe.
1630 GNUNET_SCHEDULER_TaskIdentifier rtask;
1635 struct GNUNET_TIME_Absolute timeout;
1638 * Current read offset in buf.
1645 * Stop/kill a command. Must ONLY be called either from
1646 * the callback after 'NULL' was passed for 'line' *OR*
1647 * from an independent task (not within the line processor).
1649 * @param cmd handle to the process
1652 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
1654 if (NULL != cmd->proc)
1656 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cmd->rtask);
1657 GNUNET_SCHEDULER_cancel (cmd->rtask);
1659 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1660 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
1661 GNUNET_OS_process_destroy (cmd->eip);
1662 GNUNET_DISK_pipe_close (cmd->opipe);
1668 * Read from the process and call the line processor.
1670 * @param cls the 'struct GNUNET_OS_CommandHandle'
1671 * @param tc scheduler context
1674 cmd_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1676 struct GNUNET_OS_CommandHandle *cmd = cls;
1677 GNUNET_OS_LineProcessor proc;
1681 cmd->rtask = GNUNET_SCHEDULER_NO_TASK;
1682 if (GNUNET_YES != GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r))
1684 /* timeout, shutdown, etc. */
1687 proc (cmd->proc_cls, NULL);
1691 GNUNET_DISK_file_read (cmd->r, &cmd->buf[cmd->off],
1692 sizeof (cmd->buf) - cmd->off);
1695 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
1697 cmd->buf[cmd->off] = '\0';
1698 cmd->proc (cmd->proc_cls, cmd->buf);
1702 proc (cmd->proc_cls, NULL);
1705 end = memchr (&cmd->buf[cmd->off], '\n', ret);
1710 cmd->proc (cmd->proc_cls, cmd->buf);
1711 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1712 cmd->off -= (end + 1 - cmd->buf);
1713 end = memchr (cmd->buf, '\n', cmd->off);
1716 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
1717 (cmd->timeout), cmd->r, &cmd_read, cmd);
1722 * Run the given command line and call the given function
1723 * for each line of the output.
1725 * @param proc function to call for each line of the output
1726 * @param proc_cls closure for proc
1727 * @param timeout when to time out
1728 * @param binary command to run
1729 * @param ... arguments to command
1730 * @return NULL on error
1732 struct GNUNET_OS_CommandHandle *
1733 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
1734 struct GNUNET_TIME_Relative timeout, const char *binary,
1737 struct GNUNET_OS_CommandHandle *cmd;
1738 struct GNUNET_OS_Process *eip;
1739 struct GNUNET_DISK_PipeHandle *opipe;
1742 opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
1745 va_start (ap, binary);
1746 /* redirect stdout, don't inherit stderr/stdin */
1747 eip = GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL, opipe, binary, ap);
1751 GNUNET_DISK_pipe_close (opipe);
1754 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
1755 cmd = GNUNET_malloc (sizeof (struct GNUNET_OS_CommandHandle));
1756 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1760 cmd->proc_cls = proc_cls;
1761 cmd->r = GNUNET_DISK_pipe_handle (opipe, GNUNET_DISK_PIPE_END_READ);
1762 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd);
1767 /* end of os_priority.c */