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 3, 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_util_lib.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
38 #define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE"
41 struct GNUNET_OS_Process
56 * Pipe we use to signal the process.
57 * NULL if unused, or if process was deemed uncontrollable.
59 struct GNUNET_DISK_FileHandle *control_pipe;
64 * Handle for 'this' process.
66 static struct GNUNET_OS_Process current_process;
70 * This handler is called when there are control data to be read on the pipe
72 * @param cls the 'struct GNUNET_DISK_FileHandle' of the control pipe
73 * @param tc scheduler context
76 parent_control_handler (void *cls,
77 const struct GNUNET_SCHEDULER_TaskContext *tc)
79 struct GNUNET_DISK_FileHandle *control_pipe = cls;
84 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' invoked because of %d\n", __FUNCTION__,
86 if (0 != (tc->reason &
87 (GNUNET_SCHEDULER_REASON_SHUTDOWN | GNUNET_SCHEDULER_REASON_TIMEOUT)))
89 GNUNET_DISK_file_close (control_pipe);
93 ret = GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig));
94 if (sizeof (sig) != ret)
97 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
98 LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing control pipe\n");
99 GNUNET_DISK_file_close (control_pipe);
103 pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE);
104 GNUNET_assert ( (NULL == pipe_fd) || (strlen (pipe_fd) <= 0) );
105 LOG (GNUNET_ERROR_TYPE_DEBUG,
106 "Got control code %d from parent via pipe %s\n", sig, pipe_fd);
107 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
108 control_pipe, &parent_control_handler,
110 GNUNET_SIGNAL_raise ((int) sig);
115 * Task that connects this process to its parent via pipe;
116 * essentially, the parent control handler will read signal numbers
117 * from the 'GNUNET_OS_CONTROL_PIPE' (as given in an environment
118 * variable) and raise those signals.
120 * @param cls closure (unused)
121 * @param tc scheduler context (unused)
124 GNUNET_OS_install_parent_control_handler (void *cls,
126 GNUNET_SCHEDULER_TaskContext *tc)
130 struct GNUNET_DISK_FileHandle *control_pipe;
133 env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
134 if ( (NULL == env_buf) || (strlen (env_buf) <= 0) )
136 LOG (GNUNET_ERROR_TYPE_DEBUG,
137 "Not installing a handler because $%s is empty\n",
138 GNUNET_OS_CONTROL_PIPE);
139 putenv (GNUNET_OS_CONTROL_PIPE "=");
143 pipe_fd = strtoull (env_buf, &env_buf_end, 16);
144 if ((0 != errno) || (env_buf == env_buf_end))
146 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "strtoull", env_buf);
147 putenv (GNUNET_OS_CONTROL_PIPE "=");
150 #if !defined (WINDOWS)
151 if (pipe_fd >= FD_SETSIZE)
153 if ((FILE_TYPE_UNKNOWN == GetFileType ((HANDLE) (uintptr_t) pipe_fd))
154 && (0 != GetLastError ()))
157 LOG (GNUNET_ERROR_TYPE_ERROR,
158 "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n", env_buf);
159 putenv (GNUNET_OS_CONTROL_PIPE "=");
163 control_pipe = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) (uintptr_t) pipe_fd);
165 control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
167 if (NULL == control_pipe)
169 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", env_buf);
170 putenv (GNUNET_OS_CONTROL_PIPE "=");
173 LOG (GNUNET_ERROR_TYPE_DEBUG,
174 "Adding parent control handler pipe `%s' to the scheduler\n", env_buf);
175 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, control_pipe,
176 &parent_control_handler, control_pipe);
177 putenv (GNUNET_OS_CONTROL_PIPE "=");
182 * Get process structure for current process
184 * The pointer it returns points to static memory location and must not be
187 * @return pointer to the process sturcutre for this process
189 struct GNUNET_OS_Process *
190 GNUNET_OS_process_current ()
193 current_process.pid = GetCurrentProcessId ();
194 current_process.handle = GetCurrentProcess ();
196 current_process.pid = 0;
198 return ¤t_process;
203 * Sends a signal to the process
205 * @param proc pointer to process structure
207 * @return 0 on success, -1 on error
210 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
216 if (NULL != proc->control_pipe)
218 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending signal %d to pid: %u via pipe\n", sig, proc->pid);
219 ret = GNUNET_DISK_file_write (proc->control_pipe, &csig, sizeof (csig));
220 if (sizeof (csig) == ret)
223 /* pipe failed or non-existent, try other methods */
226 #if !defined (WINDOWS)
232 #if (SIGTERM != GNUNET_TERM_SIG)
233 case GNUNET_TERM_SIG:
235 #if defined(WINDOWS) && !defined(__CYGWIN__)
238 int must_kill = GNUNET_YES;
239 if (0 != GetExitCodeProcess (proc->handle, &exitcode))
240 must_kill = (exitcode == STILL_ACTIVE) ? GNUNET_YES : GNUNET_NO;
241 if (GNUNET_YES == must_kill)
242 if (0 == SafeTerminateProcess (proc->handle, 0, 0))
244 DWORD error_code = GetLastError ();
245 if ((error_code != WAIT_TIMEOUT) && (error_code != ERROR_PROCESS_ABORTED))
247 LOG ((error_code == ERROR_ACCESS_DENIED) ?
248 GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
249 "SafeTermiateProcess failed with code %lu\n", error_code);
250 /* The problem here is that a process that is already dying
251 * might cause SafeTerminateProcess to fail with
252 * ERROR_ACCESS_DENIED, but the process WILL die eventually.
253 * If we really had a permissions problem, hanging up (which
254 * is what will happen in process_wait() in that case) is
257 if (ERROR_ACCESS_DENIED == error_code)
263 SetErrnoFromWinError (error_code);
271 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending signal %d to pid: %u via system call\n", sig, proc->pid);
272 return PLIBC_KILL (proc->pid, sig);
275 #if defined (WINDOWS)
279 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending signal %d to pid: %u via system call\n", sig, proc->pid);
280 return PLIBC_KILL (proc->pid, sig);
286 * Get the pid of the process in question
288 * @param proc the process to get the pid of
290 * @return the current process id
293 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process * proc)
300 * Cleans up process structure contents (OS-dependent) and deallocates it
302 * @param proc pointer to process structure
305 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
307 if (NULL != proc->control_pipe)
308 GNUNET_DISK_file_close (proc->control_pipe);
309 #if defined (WINDOWS)
310 if (proc->handle != NULL)
311 CloseHandle (proc->handle);
317 #include "gnunet_signal_lib.h"
319 extern GNUNET_SIGNAL_Handler w32_sigchld_handler;
322 * Make seaspider happy.
324 #define DWORD_WINAPI DWORD WINAPI
327 * @brief Waits for a process to terminate and invokes the SIGCHLD handler
328 * @param proc pointer to process structure
331 child_wait_thread (void *arg)
333 struct GNUNET_OS_Process *proc = (struct GNUNET_OS_Process *) arg;
335 WaitForSingleObject (proc->handle, INFINITE);
337 if (w32_sigchld_handler)
338 w32_sigchld_handler ();
347 CreateCustomEnvTable (char **vars)
349 char *win32_env_table;
354 size_t tablesize = 0;
355 size_t items_count = 0;
364 win32_env_table = GetEnvironmentStringsA ();
365 if (NULL == win32_env_table)
367 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ;
369 index = GNUNET_malloc (sizeof (char *) * n_var);
370 for (c = 0; c < n_var; c++)
372 for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++)
374 size_t len = strlen (ptr);
377 for (var_ptr = vars; *var_ptr; var_ptr++)
381 var_len = strlen (var);
382 if (strncmp (var, ptr, var_len) == 0)
386 tablesize += var_len + strlen (val) + 1;
391 tablesize += len + 1;
394 for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
399 n_found += strlen (var) + strlen (val) + 1;
401 result = GNUNET_malloc (tablesize + n_found + 1);
402 for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
404 size_t len = strlen (ptr);
407 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
411 var_len = strlen (var);
412 if (strncmp (var, ptr, var_len) == 0)
420 strcpy (result_ptr, ptr);
421 result_ptr += len + 1;
425 strcpy (result_ptr, var);
426 result_ptr += var_len;
427 strcpy (result_ptr, val);
428 result_ptr += strlen (val) + 1;
432 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
436 var_len = strlen (var);
439 strcpy (result_ptr, var);
440 result_ptr += var_len;
441 strcpy (result_ptr, val);
442 result_ptr += strlen (val) + 1;
445 FreeEnvironmentStrings (win32_env_table);
454 * Open '/dev/null' and make the result the given
457 * @param target_fd desired FD to point to /dev/null
458 * @param flags open flags (O_RDONLY, O_WRONLY)
461 open_dev_null (int target_fd,
466 fd = open ("/dev/null", flags);
469 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", "/dev/null");
474 if (-1 == dup2 (fd, target_fd))
476 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
480 GNUNET_break (0 == close (fd));
488 * @param pipe_control should a pipe be used to send signals to the child?
489 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
490 * std handles of the parent are inherited by the child.
491 * pipe_stdin and pipe_stdout take priority over std_inheritance
492 * (when they are non-NULL).
493 * @param pipe_stdin pipe to use to send input to child process (or NULL)
494 * @param pipe_stdout pipe to use to get output from child process (or NULL)
495 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
496 * must be NULL on platforms where dup is not supported
497 * @param filename name of the binary
498 * @param argv NULL-terminated list of arguments to the process
499 * @return process ID of the new process, -1 on error
501 static struct GNUNET_OS_Process *
502 start_process (int pipe_control,
503 enum GNUNET_OS_InheritStdioFlags std_inheritance,
504 struct GNUNET_DISK_PipeHandle *pipe_stdin,
505 struct GNUNET_DISK_PipeHandle *pipe_stdout,
506 const SOCKTYPE *lsocks,
507 const char *filename,
513 struct GNUNET_OS_Process *gnunet_proc;
514 struct GNUNET_DISK_FileHandle *childpipe_read;
515 struct GNUNET_DISK_FileHandle *childpipe_write;
516 int childpipe_read_fd;
529 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
530 return NULL; /* not executable */
531 if (GNUNET_YES == pipe_control)
533 struct GNUNET_DISK_PipeHandle *childpipe;
534 int dup_childpipe_read_fd = -1;
536 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
537 if (NULL == childpipe)
539 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
540 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
541 GNUNET_DISK_pipe_close (childpipe);
542 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
543 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
544 &childpipe_read_fd, sizeof (int))) ||
545 (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
547 if (NULL != childpipe_read)
548 GNUNET_DISK_file_close (childpipe_read);
549 if (NULL != childpipe_write)
550 GNUNET_DISK_file_close (childpipe_write);
551 if (0 <= dup_childpipe_read_fd)
552 close (dup_childpipe_read_fd);
555 childpipe_read_fd = dup_childpipe_read_fd;
556 GNUNET_DISK_file_close (childpipe_read);
560 childpipe_write = NULL;
561 childpipe_read_fd = -1;
563 if (NULL != pipe_stdout)
565 GNUNET_assert (GNUNET_OK ==
566 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
568 GNUNET_DISK_PIPE_END_WRITE),
569 &fd_stdout_write, sizeof (int)));
570 GNUNET_assert (GNUNET_OK ==
571 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
572 (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
573 &fd_stdout_read, sizeof (int)));
575 if (NULL != pipe_stdin)
577 GNUNET_assert (GNUNET_OK ==
578 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
579 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
580 &fd_stdin_read, sizeof (int)));
581 GNUNET_assert (GNUNET_OK ==
582 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
583 (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
584 &fd_stdin_write, sizeof (int)));
591 while (-1 != (k = lsocks[i++]))
592 GNUNET_array_append (lscp, ls, k);
593 GNUNET_array_append (lscp, ls, -1);
596 /* see https://gnunet.org/vfork */
604 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
605 GNUNET_array_grow (lscp, ls, 0);
606 if (NULL != childpipe_write)
607 GNUNET_DISK_file_close (childpipe_write);
608 if (0 <= childpipe_read_fd)
609 close (childpipe_read_fd);
615 unsetenv (GNUNET_OS_CONTROL_PIPE);
616 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
617 gnunet_proc->pid = ret;
618 gnunet_proc->control_pipe = childpipe_write;
619 if (GNUNET_YES == pipe_control)
621 close (childpipe_read_fd);
623 GNUNET_array_grow (lscp, ls, 0);
626 if (0 <= childpipe_read_fd)
630 /* due to vfork, we must NOT free memory on DARWIN! */
631 GNUNET_DISK_file_close (childpipe_write);
633 snprintf (fdbuf, 100, "%x", childpipe_read_fd);
634 setenv (GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
637 unsetenv (GNUNET_OS_CONTROL_PIPE);
638 if (NULL != pipe_stdin)
640 GNUNET_break (0 == close (fd_stdin_write));
641 if (-1 == dup2 (fd_stdin_read, 0))
642 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
643 GNUNET_break (0 == close (fd_stdin_read));
645 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
647 GNUNET_break (0 == close (0));
648 open_dev_null (0, O_RDONLY);
650 if (NULL != pipe_stdout)
652 GNUNET_break (0 == close (fd_stdout_read));
653 if (-1 == dup2 (fd_stdout_write, 1))
654 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
655 GNUNET_break (0 == close (fd_stdout_write));
657 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
659 GNUNET_break (0 == close (1));
660 open_dev_null (1, O_WRONLY);
662 if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
664 GNUNET_break (0 == close (2));
665 open_dev_null (2, O_WRONLY);
669 /* read systemd documentation... */
672 while (-1 != lscp[i])
675 while (-1 != lscp[j])
681 GNUNET_assert (-1 != k);
682 GNUNET_assert (0 == close (lscp[j]));
690 /* Bury any existing FD, no matter what; they should all be closed
691 * on exec anyway and the important onces have been dup'ed away */
693 GNUNET_assert (-1 != dup2 (lscp[i], tgt));
695 /* unset close-on-exec flag */
696 flags = fcntl (tgt, F_GETFD);
697 GNUNET_assert (flags >= 0);
698 flags &= ~FD_CLOEXEC;
700 (void) fcntl (tgt, F_SETFD, flags);
704 GNUNET_snprintf (fds, sizeof (fds), "%u", i);
705 setenv ("LISTEN_FDS", fds, 1);
708 /* due to vfork, we must NOT free memory on DARWIN! */
709 GNUNET_array_grow (lscp, ls, 0);
711 execvp (filename, argv);
712 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
715 struct GNUNET_DISK_FileHandle *childpipe_read;
716 struct GNUNET_DISK_FileHandle *childpipe_write;
717 HANDLE childpipe_read_handle;
719 char **non_const_argv;
724 PROCESS_INFORMATION proc;
726 struct GNUNET_OS_Process *gnunet_proc;
727 char path[MAX_PATH + 1];
728 char *our_env[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
729 char *env_block = NULL;
737 char *non_const_filename;
738 char win_path[MAX_PATH + 1];
739 struct GNUNET_DISK_PipeHandle *lsocks_pipe;
740 const struct GNUNET_DISK_FileHandle *lsocks_write_fd;
751 HANDLE stdout_handle;
752 HANDLE stdih, stdoh, stdeh;
753 DWORD stdif, stdof, stdef;
756 DWORD create_no_window;
758 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
759 return NULL; /* not executable */
761 /* Search in prefix dir (hopefully - the directory from which
762 * the current module was loaded), bindir and libdir, then in PATH
764 self_prefix = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_SELF_PREFIX);
765 bindir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
766 libdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
768 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
771 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
774 pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
777 ptr += sprintf (pathbuf, "%s;%s;%s;", self_prefix, bindir, libdir);
778 GNUNET_free (self_prefix);
779 GNUNET_free (bindir);
780 GNUNET_free (libdir);
782 alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len);
783 if (alloc_len != pathbuf_len - 1)
785 GNUNET_free (pathbuf);
786 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */
790 cmdlen = strlen (filename);
791 if ( (cmdlen < 5) || (0 != strcmp (&filename[cmdlen - 4], ".exe")) )
792 GNUNET_asprintf (&non_const_filename, "%s.exe", filename);
794 GNUNET_asprintf (&non_const_filename, "%s", filename);
796 /* It could be in POSIX form, convert it to a DOS path early on */
797 if (ERROR_SUCCESS != (lRet = plibc_conv_to_win_path (non_const_filename, win_path)))
799 SetErrnoFromWinError (lRet);
800 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "plibc_conv_to_win_path",
802 GNUNET_free (non_const_filename);
803 GNUNET_free (pathbuf);
806 GNUNET_free (non_const_filename);
807 non_const_filename = GNUNET_strdup (win_path);
808 /* Check that this is the full path. If it isn't, search. */
809 /* FIXME: convert it to wchar_t and use SearchPathW?
810 * Remember: arguments to _start_process() are technically in UTF-8...
812 if (non_const_filename[1] == ':')
814 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
815 LOG (GNUNET_ERROR_TYPE_DEBUG,
816 "Using path `%s' as-is. PATH is %s\n", path, ptr);
818 else if (!SearchPathA
819 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char),
822 SetErrnoFromWinError (GetLastError ());
823 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
825 GNUNET_free (non_const_filename);
826 GNUNET_free (pathbuf);
830 LOG (GNUNET_ERROR_TYPE_DEBUG,
831 "Found `%s' in PATH `%s'\n", path, pathbuf);
832 GNUNET_free (pathbuf);
833 GNUNET_free (non_const_filename);
835 /* Count the number of arguments */
836 arg = (char **) argv;
843 /* Allocate a copy argv */
844 non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1));
846 /* Copy all argv strings */
848 arg = (char **) argv;
852 non_const_argv[argcount] = GNUNET_strdup (path);
854 non_const_argv[argcount] = GNUNET_strdup (*arg);
858 non_const_argv[argcount] = NULL;
862 arg = non_const_argv;
865 cmdlen = cmdlen + strlen (*arg) + 4;
869 /* Allocate and create cmd */
870 cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen);
871 arg = non_const_argv;
874 char arg_last_char = (*arg)[strlen (*arg) - 1];
875 idx += sprintf (idx, "\"%s%s\"%s", *arg,
876 arg_last_char == '\\' ? "\\" : "", *(arg + 1) ? " " : "");
881 GNUNET_free (non_const_argv[--argcount]);
882 GNUNET_free (non_const_argv);
884 memset (&start, 0, sizeof (start));
885 start.cb = sizeof (start);
886 if ((pipe_stdin != NULL) || (pipe_stdout != NULL) || (std_inheritance != 0))
887 start.dwFlags |= STARTF_USESTDHANDLES;
889 stdih = GetStdHandle (STD_INPUT_HANDLE);
890 GetHandleInformation (stdih, &stdif);
891 if (pipe_stdin != NULL)
893 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
894 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
895 &stdin_handle, sizeof (HANDLE));
896 start.hStdInput = stdin_handle;
900 if (std_inheritance & GNUNET_OS_INHERIT_STD_IN)
902 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 1);
903 if (pipe_stdin == NULL)
904 start.hStdInput = stdih;
907 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 0);
911 stdoh = GetStdHandle (STD_OUTPUT_HANDLE);
912 GetHandleInformation (stdoh, &stdof);
913 if (NULL != pipe_stdout)
915 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
917 GNUNET_DISK_PIPE_END_WRITE),
918 &stdout_handle, sizeof (HANDLE));
919 start.hStdOutput = stdout_handle;
923 if (std_inheritance & GNUNET_OS_INHERIT_STD_OUT)
925 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 1);
926 if (pipe_stdout == NULL)
927 start.hStdOutput = stdoh;
930 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 0);
933 stdeh = GetStdHandle (STD_ERROR_HANDLE);
934 GetHandleInformation (stdeh, &stdef);
937 if (std_inheritance & GNUNET_OS_INHERIT_STD_ERR)
939 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 1);
940 start.hStdError = stdeh;
943 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 0);
946 if (GNUNET_YES == pipe_control)
948 struct GNUNET_DISK_PipeHandle *childpipe;
949 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
950 if (NULL == childpipe)
952 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
953 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
954 GNUNET_DISK_pipe_close (childpipe);
955 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
956 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
957 &childpipe_read_handle, sizeof (HANDLE))))
960 GNUNET_DISK_file_close (childpipe_read);
962 GNUNET_DISK_file_close (childpipe_write);
966 /* Unlike *nix variant, we don't dup the handle, so can't close
967 * filehandle right now.
969 SetHandleInformation (childpipe_read_handle, HANDLE_FLAG_INHERIT, 1);
973 childpipe_read = NULL;
974 childpipe_write = NULL;
977 if (lsocks != NULL && lsocks[0] != INVALID_SOCKET)
979 lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
981 if (lsocks_pipe == NULL)
984 GNUNET_DISK_pipe_close (lsocks_pipe);
985 if (GNUNET_YES == pipe_control)
987 GNUNET_DISK_file_close (childpipe_write);
988 GNUNET_DISK_file_close (childpipe_read);
992 lsocks_write_fd = GNUNET_DISK_pipe_handle (lsocks_pipe,
993 GNUNET_DISK_PIPE_END_WRITE);
994 GNUNET_DISK_internal_file_handle_ (lsocks_write_fd,
995 &lsocks_write, sizeof (HANDLE));
996 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
997 (lsocks_pipe, GNUNET_DISK_PIPE_END_READ),
998 &lsocks_read, sizeof (HANDLE));
1004 if (GNUNET_YES == pipe_control)
1006 GNUNET_asprintf (&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE);
1007 GNUNET_asprintf (&our_env[env_off++], "%p", childpipe_read_handle);
1009 if ( (lsocks != NULL) && (lsocks[0] != INVALID_SOCKET))
1011 /*This will tell the child that we're going to send lsocks over the pipe*/
1012 GNUNET_asprintf (&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS");
1013 GNUNET_asprintf (&our_env[env_off++], "%lu", lsocks_read);
1015 our_env[env_off++] = NULL;
1016 env_block = CreateCustomEnvTable (our_env);
1018 GNUNET_free_non_null (our_env[--env_off]);
1021 if (NULL == (wpath = u8_to_u16 ((uint8_t *) path, 1 + strlen (path), NULL, &wpath_len)))
1023 LOG (GNUNET_ERROR_TYPE_DEBUG,
1024 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", path, errno);
1025 GNUNET_free (env_block);
1028 GNUNET_DISK_pipe_close (lsocks_pipe);
1029 if (GNUNET_YES == pipe_control)
1031 GNUNET_DISK_file_close (childpipe_write);
1032 GNUNET_DISK_file_close (childpipe_read);
1038 if (NULL == (wcmd = u8_to_u16 ((uint8_t *) cmd, 1 + strlen (cmd), NULL, &wcmd_len)))
1040 LOG (GNUNET_ERROR_TYPE_DEBUG,
1041 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", cmd, errno);
1042 GNUNET_free (env_block);
1046 GNUNET_DISK_pipe_close (lsocks_pipe);
1047 if (GNUNET_YES == pipe_control)
1049 GNUNET_DISK_file_close (childpipe_write);
1050 GNUNET_DISK_file_close (childpipe_read);
1055 create_no_window = 0;
1057 HANDLE console_input = CreateFile ("CONIN$", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1058 if (INVALID_HANDLE_VALUE == console_input)
1059 create_no_window = CREATE_NO_WINDOW;
1061 CloseHandle (console_input);
1064 bresult = CreateProcessW (wpath, wcmd, NULL, NULL, GNUNET_YES,
1065 create_no_window | CREATE_SUSPENDED, env_block, NULL, &start, &proc);
1066 error_code = GetLastError ();
1068 if ((NULL == pipe_stdin) && (stdih))
1069 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, stdif);
1072 if ((NULL == pipe_stdout) && (stdoh))
1073 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, stdof);
1076 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, stdef);
1079 LOG (GNUNET_ERROR_TYPE_ERROR, "CreateProcess(%s, %s) failed: %lu\n", path, cmd, error_code);
1081 GNUNET_free (env_block);
1085 if (GNUNET_YES == pipe_control)
1087 GNUNET_DISK_file_close (childpipe_read);
1092 if (GNUNET_YES == pipe_control)
1094 GNUNET_DISK_file_close (childpipe_write);
1097 GNUNET_DISK_pipe_close (lsocks_pipe);
1098 SetErrnoFromWinError (error_code);
1102 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
1103 gnunet_proc->pid = proc.dwProcessId;
1104 gnunet_proc->handle = proc.hProcess;
1105 gnunet_proc->control_pipe = childpipe_write;
1107 CreateThread (NULL, 64000, &child_wait_thread, (void *) gnunet_proc, 0, NULL);
1109 ResumeThread (proc.hThread);
1110 CloseHandle (proc.hThread);
1112 if ( (NULL == lsocks) || (INVALID_SOCKET == lsocks[0]) )
1115 GNUNET_DISK_pipe_close_end (lsocks_pipe, GNUNET_DISK_PIPE_END_READ);
1117 /* This is a replacement for "goto error" that doesn't use goto */
1126 /* Tell the number of sockets */
1127 for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++);
1129 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1130 if (sizeof (count) != wrote)
1132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1133 "Failed to write %u count bytes to the child: %u\n",
1134 sizeof (count), GetLastError ());
1137 for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++)
1139 WSAPROTOCOL_INFOA pi;
1140 /* Get a socket duplication info */
1141 if (SOCKET_ERROR == WSADuplicateSocketA (lsocks[i], gnunet_proc->pid, &pi))
1143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1144 "Failed to duplicate an socket[%llu]: %u\n", i,
1148 /* Synchronous I/O is not nice, but we can't schedule this:
1149 * lsocks will be closed/freed by the caller soon, and until
1150 * the child creates a duplicate, closing a socket here will
1151 * close it for good.
1153 /* Send the size of the structure
1154 * (the child might be built with different headers...)
1157 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &size, sizeof (size));
1158 if (sizeof (size) != wrote)
1160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1161 "Failed to write %u size[%llu] bytes to the child: %u\n",
1162 sizeof (size), i, GetLastError ());
1165 /* Finally! Send the data */
1166 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &pi, sizeof (pi));
1167 if (sizeof (pi) != wrote)
1169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1170 "Failed to write %u socket[%llu] bytes to the child: %u\n",
1171 sizeof (pi), i, GetLastError ());
1175 /* This will block us until the child makes a final read or closes
1176 * the pipe (hence no 'wrote' check), since we have to wait for it
1177 * to duplicate the last socket, before we return and start closing
1180 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1185 GNUNET_DISK_file_sync (lsocks_write_fd);
1186 GNUNET_DISK_pipe_close (lsocks_pipe);
1190 /* If we can't pass on the socket(s), the child will block forever,
1191 * better put it out of its misery.
1193 SafeTerminateProcess (gnunet_proc->handle, 0, 0);
1194 CloseHandle (gnunet_proc->handle);
1195 if (NULL != gnunet_proc->control_pipe)
1196 GNUNET_DISK_file_close (gnunet_proc->control_pipe);
1197 GNUNET_free (gnunet_proc);
1210 * @param pipe_control should a pipe be used to send signals to the child?
1211 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1212 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1213 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1214 * @param filename name of the binary
1215 * @param argv NULL-terminated array of arguments to the process
1216 * @return pointer to process structure of the new process, NULL on error
1218 struct GNUNET_OS_Process *
1219 GNUNET_OS_start_process_vap (int pipe_control,
1220 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1221 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1222 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1223 const char *filename,
1226 return start_process (pipe_control,
1239 * @param pipe_control should a pipe be used to send signals to the child?
1240 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1241 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1242 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1243 * @param filename name of the binary
1244 * @param va NULL-terminated list of arguments to the process
1245 * @return pointer to process structure of the new process, NULL on error
1247 struct GNUNET_OS_Process *
1248 GNUNET_OS_start_process_va (int pipe_control,
1249 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1250 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1251 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1252 const char *filename, va_list va)
1254 struct GNUNET_OS_Process *ret;
1261 while (NULL != va_arg (ap, char *))
1264 argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
1267 while (NULL != (argv[argc] = va_arg (ap, char *)))
1270 ret = GNUNET_OS_start_process_vap (pipe_control,
1284 * @param pipe_control should a pipe be used to send signals to the child?
1285 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1286 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1287 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1288 * @param filename name of the binary
1289 * @param ... NULL-terminated list of arguments to the process
1290 * @return pointer to process structure of the new process, NULL on error
1292 struct GNUNET_OS_Process *
1293 GNUNET_OS_start_process (int pipe_control,
1294 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1295 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1296 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1297 const char *filename, ...)
1299 struct GNUNET_OS_Process *ret;
1302 va_start (ap, filename);
1303 ret = GNUNET_OS_start_process_va (pipe_control, std_inheritance, pipe_stdin,
1304 pipe_stdout, filename, ap);
1313 * @param pipe_control should a pipe be used to send signals to the child?
1314 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
1315 * std handles of the parent are inherited by the child.
1316 * pipe_stdin and pipe_stdout take priority over std_inheritance
1317 * (when they are non-NULL).
1318 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1319 * must be NULL on platforms where dup is not supported
1320 * @param filename name of the binary
1321 * @param argv NULL-terminated list of arguments to the process
1322 * @return process ID of the new process, -1 on error
1324 struct GNUNET_OS_Process *
1325 GNUNET_OS_start_process_v (int pipe_control,
1326 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1327 const SOCKTYPE *lsocks,
1328 const char *filename,
1331 return start_process (pipe_control,
1342 * Start a process. This function is similar to the GNUNET_OS_start_process_*
1343 * except that the filename and arguments can have whole strings which contain
1344 * the arguments. These arguments are to be separated by spaces and are parsed
1345 * in the order they appear. Arguments containing spaces can be used by
1346 * quoting them with @em ".
1348 * @param pipe_control should a pipe be used to send signals to the child?
1349 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1350 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1351 * must be NULL on platforms where dup is not supported
1352 * @param filename name of the binary. It is valid to have the arguments
1353 * in this string when they are separated by spaces.
1354 * @param ... more arguments. Should be of type `char *`. It is valid
1355 * to have the arguments in these strings when they are separated by
1356 * spaces. The last argument MUST be NULL.
1357 * @return pointer to process structure of the new process, NULL on error
1359 struct GNUNET_OS_Process *
1360 GNUNET_OS_start_process_s (int pipe_control,
1361 unsigned int std_inheritance,
1362 const SOCKTYPE * lsocks,
1363 const char *filename, ...)
1367 unsigned int argv_size;
1373 struct GNUNET_OS_Process *proc;
1380 va_start (ap, filename);
1387 while ('\0' != *rpos)
1396 if ( (' ' == *rpos) && (0 == quote_on) )
1402 while (' ' == *rpos)
1405 if ( (NULL == last) && ('\0' != *rpos) ) // FIXME: == or !=?
1413 while (NULL != (arg = (va_arg (ap, const char*))));
1416 argv = GNUNET_malloc (argv_size * sizeof (char *));
1418 va_start (ap, filename);
1423 cp = GNUNET_strdup (arg);
1426 while ('\0' != *pos)
1435 if ( (' ' == *pos) && (0 == quote_on) )
1439 argv[argv_size++] = GNUNET_strdup (last);
1445 if ( (NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
1451 argv[argv_size++] = GNUNET_strdup (last);
1455 while (NULL != (arg = (va_arg (ap, const char*))));
1457 argv[argv_size] = NULL;
1459 for(i = 0; i < argv_size; i++)
1461 len = strlen (argv[i]);
1462 if ( (argv[i][0] == '"') && (argv[i][len-1] == '"'))
1464 memmove (&argv[i][0], &argv[i][1], len - 2);
1465 argv[i][len-2] = '\0';
1468 binary_path = argv[0];
1469 proc = GNUNET_OS_start_process_v (pipe_control, std_inheritance, lsocks,
1471 while (argv_size > 0)
1472 GNUNET_free (argv[--argv_size]);
1479 * Retrieve the status of a process, waiting on him if dead.
1480 * Nonblocking version.
1482 * @param proc process ID
1483 * @param type status type
1484 * @param code return code/signal number
1485 * @return GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise
1488 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1489 enum GNUNET_OS_ProcessStatusType *type,
1490 unsigned long *code)
1496 GNUNET_assert (0 != proc);
1497 ret = waitpid (proc->pid, &status, WNOHANG);
1500 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1501 return GNUNET_SYSERR;
1505 *type = GNUNET_OS_PROCESS_RUNNING;
1509 if (proc->pid != ret)
1511 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1512 return GNUNET_SYSERR;
1514 if (WIFEXITED (status))
1516 *type = GNUNET_OS_PROCESS_EXITED;
1517 *code = WEXITSTATUS (status);
1519 else if (WIFSIGNALED (status))
1521 *type = GNUNET_OS_PROCESS_SIGNALED;
1522 *code = WTERMSIG (status);
1524 else if (WIFSTOPPED (status))
1526 *type = GNUNET_OS_PROCESS_SIGNALED;
1527 *code = WSTOPSIG (status);
1530 else if (WIFCONTINUED (status))
1532 *type = GNUNET_OS_PROCESS_RUNNING;
1538 *type = GNUNET_OS_PROCESS_UNKNOWN;
1543 DWORD c, error_code, ret;
1547 if (h == NULL || ret == 0)
1549 LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n",
1551 return GNUNET_SYSERR;
1554 h = GetCurrentProcess ();
1557 ret = GetExitCodeProcess (h, &c);
1558 error_code = GetLastError ();
1559 if (ret == 0 || error_code != NO_ERROR)
1561 SetErrnoFromWinError (error_code);
1562 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
1563 return GNUNET_SYSERR;
1565 if (STILL_ACTIVE == c)
1567 *type = GNUNET_OS_PROCESS_RUNNING;
1571 *type = GNUNET_OS_PROCESS_EXITED;
1580 * Wait for a process
1582 * @param proc pointer to process structure
1583 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
1586 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1589 pid_t pid = proc->pid;
1592 while ( (pid != (ret = waitpid (pid, NULL, 0))) &&
1593 (EINTR == errno) ) ;
1596 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1597 return GNUNET_SYSERR;
1606 LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid process information {%d, %08X}\n",
1608 return GNUNET_SYSERR;
1611 h = GetCurrentProcess ();
1613 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1615 SetErrnoFromWinError (GetLastError ());
1616 return GNUNET_SYSERR;
1624 * Handle to a command.
1626 struct GNUNET_OS_CommandHandle
1632 struct GNUNET_OS_Process *eip;
1635 * Handle to the output pipe.
1637 struct GNUNET_DISK_PipeHandle *opipe;
1640 * Read-end of output pipe.
1642 const struct GNUNET_DISK_FileHandle *r;
1645 * Function to call on each line of output.
1647 GNUNET_OS_LineProcessor proc;
1650 * Closure for 'proc'.
1655 * Buffer for the output.
1660 * Task reading from pipe.
1662 GNUNET_SCHEDULER_TaskIdentifier rtask;
1667 struct GNUNET_TIME_Absolute timeout;
1670 * Current read offset in buf.
1677 * Stop/kill a command. Must ONLY be called either from
1678 * the callback after 'NULL' was passed for 'line' *OR*
1679 * from an independent task (not within the line processor).
1681 * @param cmd handle to the process
1684 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
1686 if (NULL != cmd->proc)
1688 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != cmd->rtask);
1689 GNUNET_SCHEDULER_cancel (cmd->rtask);
1691 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1692 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
1693 GNUNET_OS_process_destroy (cmd->eip);
1694 GNUNET_DISK_pipe_close (cmd->opipe);
1700 * Read from the process and call the line processor.
1702 * @param cls the 'struct GNUNET_OS_CommandHandle'
1703 * @param tc scheduler context
1706 cmd_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1708 struct GNUNET_OS_CommandHandle *cmd = cls;
1709 GNUNET_OS_LineProcessor proc;
1713 cmd->rtask = GNUNET_SCHEDULER_NO_TASK;
1714 if (GNUNET_YES != GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r))
1716 /* timeout, shutdown, etc. */
1719 proc (cmd->proc_cls, NULL);
1723 GNUNET_DISK_file_read (cmd->r, &cmd->buf[cmd->off],
1724 sizeof (cmd->buf) - cmd->off);
1727 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
1729 cmd->buf[cmd->off] = '\0';
1730 cmd->proc (cmd->proc_cls, cmd->buf);
1734 proc (cmd->proc_cls, NULL);
1737 end = memchr (&cmd->buf[cmd->off], '\n', ret);
1742 cmd->proc (cmd->proc_cls, cmd->buf);
1743 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1744 cmd->off -= (end + 1 - cmd->buf);
1745 end = memchr (cmd->buf, '\n', cmd->off);
1748 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
1749 (cmd->timeout), cmd->r, &cmd_read, cmd);
1754 * Run the given command line and call the given function
1755 * for each line of the output.
1757 * @param proc function to call for each line of the output
1758 * @param proc_cls closure for proc
1759 * @param timeout when to time out
1760 * @param binary command to run
1761 * @param ... arguments to command
1762 * @return NULL on error
1764 struct GNUNET_OS_CommandHandle *
1765 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls,
1766 struct GNUNET_TIME_Relative timeout, const char *binary,
1769 struct GNUNET_OS_CommandHandle *cmd;
1770 struct GNUNET_OS_Process *eip;
1771 struct GNUNET_DISK_PipeHandle *opipe;
1774 opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
1777 va_start (ap, binary);
1778 /* redirect stdout, don't inherit stderr/stdin */
1779 eip = GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL, opipe, binary, ap);
1783 GNUNET_DISK_pipe_close (opipe);
1786 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
1787 cmd = GNUNET_new (struct GNUNET_OS_CommandHandle);
1788 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1792 cmd->proc_cls = proc_cls;
1793 cmd->r = GNUNET_DISK_pipe_handle (opipe, GNUNET_DISK_PIPE_END_READ);
1794 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd);
1799 /* end of os_priority.c */