2 This file is part of GNUnet
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2011 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file util/os_priority.c
18 * @brief Methods to set process priority
23 #include "gnunet_util_lib.h"
27 #define LOG(kind,...) GNUNET_log_from (kind, "util-os-priority", __VA_ARGS__)
29 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-os-priority", syscall)
31 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-os-priority", syscall, filename)
33 #define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE"
36 struct GNUNET_OS_Process
51 * Pipe we use to signal the process.
52 * NULL if unused, or if process was deemed uncontrollable.
54 struct GNUNET_DISK_FileHandle *control_pipe;
59 * Handle for 'this' process.
61 static struct GNUNET_OS_Process current_process;
64 * Handle for the #parent_control_handler() Task.
66 static struct GNUNET_SCHEDULER_Task *pch;
69 * Handle for the #shutdown_pch() Task.
71 static struct GNUNET_SCHEDULER_Task *spch;
75 * This handler is called on shutdown to remove the #pch.
77 * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe
80 shutdown_pch (void *cls)
82 struct GNUNET_DISK_FileHandle *control_pipe = cls;
84 GNUNET_SCHEDULER_cancel (pch);
86 GNUNET_DISK_file_close (control_pipe);
92 * This handler is called when there are control data to be read on the pipe
94 * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe
97 parent_control_handler (void *cls)
99 struct GNUNET_DISK_FileHandle *control_pipe = cls;
105 ret = GNUNET_DISK_file_read (control_pipe,
108 if (sizeof (sig) != ret)
111 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
112 "GNUNET_DISK_file_read");
113 LOG (GNUNET_ERROR_TYPE_DEBUG,
114 "Closing control pipe\n");
115 GNUNET_DISK_file_close (control_pipe);
117 GNUNET_SCHEDULER_cancel (spch);
121 pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE);
122 GNUNET_assert ( (NULL == pipe_fd) ||
123 (strlen (pipe_fd) <= 0) );
124 LOG (GNUNET_ERROR_TYPE_DEBUG,
125 "Got control code %d from parent via pipe %s\n",
128 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
130 &parent_control_handler,
132 GNUNET_SIGNAL_raise ((int) sig);
137 * Task that connects this process to its parent via pipe;
138 * essentially, the parent control handler will read signal numbers
139 * from the #GNUNET_OS_CONTROL_PIPE (as given in an environment
140 * variable) and raise those signals.
142 * @param cls closure (unused)
145 GNUNET_OS_install_parent_control_handler (void *cls)
149 struct GNUNET_DISK_FileHandle *control_pipe;
155 /* already done, we've been called twice... */
159 env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
160 if ( (NULL == env_buf) || (strlen (env_buf) <= 0) )
162 LOG (GNUNET_ERROR_TYPE_DEBUG,
163 "Not installing a handler because $%s is empty\n",
164 GNUNET_OS_CONTROL_PIPE);
165 putenv (GNUNET_OS_CONTROL_PIPE "=");
169 pipe_fd = strtoull (env_buf, &env_buf_end, 16);
170 if ((0 != errno) || (env_buf == env_buf_end))
172 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
175 putenv (GNUNET_OS_CONTROL_PIPE "=");
178 #if !defined (WINDOWS)
179 if (pipe_fd >= FD_SETSIZE)
181 if ((FILE_TYPE_UNKNOWN == GetFileType ((HANDLE) (uintptr_t) pipe_fd))
182 && (0 != GetLastError ()))
185 LOG (GNUNET_ERROR_TYPE_ERROR,
186 "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n",
188 putenv (GNUNET_OS_CONTROL_PIPE "=");
192 control_pipe = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) (uintptr_t) pipe_fd);
194 control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
196 if (NULL == control_pipe)
198 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
201 putenv (GNUNET_OS_CONTROL_PIPE "=");
204 LOG (GNUNET_ERROR_TYPE_DEBUG,
205 "Adding parent control handler pipe `%s' to the scheduler\n",
207 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
209 &parent_control_handler,
211 spch = GNUNET_SCHEDULER_add_shutdown (&shutdown_pch,
213 putenv (GNUNET_OS_CONTROL_PIPE "=");
218 * Get process structure for current process
220 * The pointer it returns points to static memory location and must
221 * not be deallocated/closed.
223 * @return pointer to the process sturcutre for this process
225 struct GNUNET_OS_Process *
226 GNUNET_OS_process_current ()
229 current_process.pid = GetCurrentProcessId ();
230 current_process.handle = GetCurrentProcess ();
232 current_process.pid = 0;
234 return ¤t_process;
239 * Sends a signal to the process
241 * @param proc pointer to process structure
243 * @return 0 on success, -1 on error
246 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc,
253 if (NULL != proc->control_pipe)
255 LOG (GNUNET_ERROR_TYPE_DEBUG,
256 "Sending signal %d to pid: %u via pipe\n",
259 ret = GNUNET_DISK_file_write (proc->control_pipe,
262 if (sizeof (csig) == ret)
265 /* pipe failed or non-existent, try other methods */
268 #if !defined (WINDOWS)
274 #if (SIGTERM != GNUNET_TERM_SIG)
275 case GNUNET_TERM_SIG:
277 #if defined(WINDOWS) && !defined(__CYGWIN__)
280 int must_kill = GNUNET_YES;
281 if (0 != GetExitCodeProcess (proc->handle, &exitcode))
282 must_kill = (exitcode == STILL_ACTIVE) ? GNUNET_YES : GNUNET_NO;
283 if (GNUNET_YES == must_kill)
285 if (0 == SafeTerminateProcess (proc->handle, 0, 0))
287 DWORD error_code = GetLastError ();
288 if ( (error_code != WAIT_TIMEOUT) &&
289 (error_code != ERROR_PROCESS_ABORTED) )
291 LOG ((error_code == ERROR_ACCESS_DENIED) ?
292 GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
293 "SafeTermiateProcess failed with code %lu\n",
295 /* The problem here is that a process that is already dying
296 * might cause SafeTerminateProcess to fail with
297 * ERROR_ACCESS_DENIED, but the process WILL die eventually.
298 * If we really had a permissions problem, hanging up (which
299 * is what will happen in process_wait() in that case) is
302 if (ERROR_ACCESS_DENIED == error_code)
308 SetErrnoFromWinError (error_code);
317 LOG (GNUNET_ERROR_TYPE_DEBUG,
318 "Sending signal %d to pid: %u via system call\n",
321 return PLIBC_KILL (proc->pid, sig);
324 #if defined (WINDOWS)
328 LOG (GNUNET_ERROR_TYPE_DEBUG,
329 "Sending signal %d to pid: %u via system call\n",
332 return PLIBC_KILL (proc->pid, sig);
339 * Get the pid of the process in question
341 * @param proc the process to get the pid of
343 * @return the current process id
346 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process * proc)
353 * Cleans up process structure contents (OS-dependent) and deallocates
356 * @param proc pointer to process structure
359 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
361 if (NULL != proc->control_pipe)
362 GNUNET_DISK_file_close (proc->control_pipe);
363 #if defined (WINDOWS)
364 if (NULL != proc->handle)
365 CloseHandle (proc->handle);
372 #include "gnunet_signal_lib.h"
374 extern GNUNET_SIGNAL_Handler w32_sigchld_handler;
377 * Make seaspider happy.
379 #define DWORD_WINAPI DWORD WINAPI
383 * @brief Waits for a process to terminate and invokes the SIGCHLD handler
384 * @param proc pointer to process structure
387 child_wait_thread (void *arg)
389 struct GNUNET_OS_Process *proc = (struct GNUNET_OS_Process *) arg;
391 WaitForSingleObject (proc->handle, INFINITE);
393 if (w32_sigchld_handler)
394 w32_sigchld_handler ();
403 CreateCustomEnvTable (char **vars)
405 char *win32_env_table;
410 size_t tablesize = 0;
411 size_t items_count = 0;
420 win32_env_table = GetEnvironmentStringsA ();
421 if (NULL == win32_env_table)
423 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ;
425 index = GNUNET_malloc (sizeof (char *) * n_var);
426 for (c = 0; c < n_var; c++)
428 for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++)
430 size_t len = strlen (ptr);
433 for (var_ptr = vars; *var_ptr; var_ptr++)
437 var_len = strlen (var);
438 if (strncmp (var, ptr, var_len) == 0)
442 tablesize += var_len + strlen (val) + 1;
447 tablesize += len + 1;
450 for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
455 n_found += strlen (var) + strlen (val) + 1;
457 result = GNUNET_malloc (tablesize + n_found + 1);
458 for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
460 size_t len = strlen (ptr);
463 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
467 var_len = strlen (var);
468 if (strncmp (var, ptr, var_len) == 0)
476 strcpy (result_ptr, ptr);
477 result_ptr += len + 1;
481 strcpy (result_ptr, var);
482 result_ptr += var_len;
483 strcpy (result_ptr, val);
484 result_ptr += strlen (val) + 1;
488 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
492 var_len = strlen (var);
495 strcpy (result_ptr, var);
496 result_ptr += var_len;
497 strcpy (result_ptr, val);
498 result_ptr += strlen (val) + 1;
501 FreeEnvironmentStrings (win32_env_table);
510 * Open '/dev/null' and make the result the given
513 * @param target_fd desired FD to point to /dev/null
514 * @param flags open flags (O_RDONLY, O_WRONLY)
517 open_dev_null (int target_fd,
522 fd = open ("/dev/null", flags);
525 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
532 if (-1 == dup2 (fd, target_fd))
534 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
538 GNUNET_break (0 == close (fd));
546 * @param pipe_control should a pipe be used to send signals to the child?
547 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
548 * std handles of the parent are inherited by the child.
549 * pipe_stdin and pipe_stdout take priority over std_inheritance
550 * (when they are non-NULL).
551 * @param pipe_stdin pipe to use to send input to child process (or NULL)
552 * @param pipe_stdout pipe to use to get output from child process (or NULL)
553 * @param pipe_stderr pipe to use for stderr for child process (or NULL)
554 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
555 * must be NULL on platforms where dup is not supported
556 * @param filename name of the binary
557 * @param argv NULL-terminated list of arguments to the process
558 * @return process ID of the new process, -1 on error
560 static struct GNUNET_OS_Process *
561 start_process (int pipe_control,
562 enum GNUNET_OS_InheritStdioFlags std_inheritance,
563 struct GNUNET_DISK_PipeHandle *pipe_stdin,
564 struct GNUNET_DISK_PipeHandle *pipe_stdout,
565 struct GNUNET_DISK_PipeHandle *pipe_stderr,
566 const SOCKTYPE *lsocks,
567 const char *filename,
573 struct GNUNET_OS_Process *gnunet_proc;
574 struct GNUNET_DISK_FileHandle *childpipe_read;
575 struct GNUNET_DISK_FileHandle *childpipe_write;
576 int childpipe_read_fd;
592 GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
593 return NULL; /* not executable */
594 if (GNUNET_YES == pipe_control)
596 struct GNUNET_DISK_PipeHandle *childpipe;
597 int dup_childpipe_read_fd = -1;
599 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
600 GNUNET_YES, GNUNET_NO);
601 if (NULL == childpipe)
603 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe,
604 GNUNET_DISK_PIPE_END_READ);
605 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe,
606 GNUNET_DISK_PIPE_END_WRITE);
607 GNUNET_DISK_pipe_close (childpipe);
608 if ( (NULL == childpipe_read) ||
609 (NULL == childpipe_write) ||
611 GNUNET_DISK_internal_file_handle_ (childpipe_read,
614 (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
616 if (NULL != childpipe_read)
617 GNUNET_DISK_file_close (childpipe_read);
618 if (NULL != childpipe_write)
619 GNUNET_DISK_file_close (childpipe_write);
620 if (0 <= dup_childpipe_read_fd)
621 close (dup_childpipe_read_fd);
624 childpipe_read_fd = dup_childpipe_read_fd;
625 GNUNET_DISK_file_close (childpipe_read);
629 childpipe_write = NULL;
630 childpipe_read_fd = -1;
632 if (NULL != pipe_stdin)
634 GNUNET_assert (GNUNET_OK ==
635 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
636 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
637 &fd_stdin_read, sizeof (int)));
638 GNUNET_assert (GNUNET_OK ==
639 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
640 (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
641 &fd_stdin_write, sizeof (int)));
643 if (NULL != pipe_stdout)
645 GNUNET_assert (GNUNET_OK ==
646 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
648 GNUNET_DISK_PIPE_END_WRITE),
649 &fd_stdout_write, sizeof (int)));
650 GNUNET_assert (GNUNET_OK ==
651 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
652 (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
653 &fd_stdout_read, sizeof (int)));
655 if (NULL != pipe_stderr)
657 GNUNET_assert (GNUNET_OK ==
658 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
660 GNUNET_DISK_PIPE_END_READ),
661 &fd_stderr_read, sizeof (int)));
662 GNUNET_assert (GNUNET_OK ==
663 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
665 GNUNET_DISK_PIPE_END_WRITE),
666 &fd_stderr_write, sizeof (int)));
673 while (-1 != (k = lsocks[i++]))
674 GNUNET_array_append (lscp, ls, k);
675 GNUNET_array_append (lscp, ls, -1);
678 /* see https://gnunet.org/vfork */
686 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
687 GNUNET_array_grow (lscp, ls, 0);
688 if (NULL != childpipe_write)
689 GNUNET_DISK_file_close (childpipe_write);
690 if (0 <= childpipe_read_fd)
691 close (childpipe_read_fd);
697 unsetenv (GNUNET_OS_CONTROL_PIPE);
698 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
699 gnunet_proc->pid = ret;
700 gnunet_proc->control_pipe = childpipe_write;
701 if (GNUNET_YES == pipe_control)
703 close (childpipe_read_fd);
705 GNUNET_array_grow (lscp, ls, 0);
708 if (0 <= childpipe_read_fd)
712 /* due to vfork, we must NOT free memory on DARWIN! */
713 GNUNET_DISK_file_close (childpipe_write);
715 snprintf (fdbuf, 100, "%x", childpipe_read_fd);
716 setenv (GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
719 unsetenv (GNUNET_OS_CONTROL_PIPE);
720 if (NULL != pipe_stdin)
722 GNUNET_break (0 == close (fd_stdin_write));
723 if (-1 == dup2 (fd_stdin_read, 0))
724 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
725 GNUNET_break (0 == close (fd_stdin_read));
727 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
729 GNUNET_break (0 == close (0));
730 open_dev_null (0, O_RDONLY);
732 if (NULL != pipe_stdout)
734 GNUNET_break (0 == close (fd_stdout_read));
735 if (-1 == dup2 (fd_stdout_write, 1))
736 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
737 GNUNET_break (0 == close (fd_stdout_write));
739 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
741 GNUNET_break (0 == close (1));
742 open_dev_null (1, O_WRONLY);
744 if (NULL != pipe_stderr)
746 GNUNET_break (0 == close (fd_stderr_read));
747 if (-1 == dup2 (fd_stderr_write, 2))
748 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
749 GNUNET_break (0 == close (fd_stderr_write));
751 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
753 GNUNET_break (0 == close (2));
754 open_dev_null (2, O_WRONLY);
758 /* read systemd documentation... */
761 while (-1 != lscp[i])
764 while (-1 != lscp[j])
770 GNUNET_assert (-1 != k);
771 GNUNET_assert (0 == close (lscp[j]));
779 /* Bury any existing FD, no matter what; they should all be closed
780 * on exec anyway and the important onces have been dup'ed away */
782 GNUNET_assert (-1 != dup2 (lscp[i], tgt));
784 /* unset close-on-exec flag */
785 flags = fcntl (tgt, F_GETFD);
786 GNUNET_assert (flags >= 0);
787 flags &= ~FD_CLOEXEC;
789 (void) fcntl (tgt, F_SETFD, flags);
793 GNUNET_snprintf (fds, sizeof (fds), "%u", i);
794 setenv ("LISTEN_FDS", fds, 1);
797 /* due to vfork, we must NOT free memory on DARWIN! */
798 GNUNET_array_grow (lscp, ls, 0);
800 execvp (filename, argv);
801 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
804 struct GNUNET_DISK_FileHandle *childpipe_read;
805 struct GNUNET_DISK_FileHandle *childpipe_write;
806 HANDLE childpipe_read_handle;
808 char **non_const_argv;
813 PROCESS_INFORMATION proc;
815 struct GNUNET_OS_Process *gnunet_proc;
816 char path[MAX_PATH + 1];
817 char *our_env[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
818 char *env_block = NULL;
826 char *non_const_filename;
827 char win_path[MAX_PATH + 1];
828 struct GNUNET_DISK_PipeHandle *lsocks_pipe;
829 const struct GNUNET_DISK_FileHandle *lsocks_write_fd;
840 HANDLE stdout_handle;
841 HANDLE stdih, stdoh, stdeh;
842 DWORD stdif, stdof, stdef;
845 DWORD create_no_window;
847 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
848 return NULL; /* not executable */
850 /* Search in prefix dir (hopefully - the directory from which
851 * the current module was loaded), bindir and libdir, then in PATH
853 self_prefix = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_SELF_PREFIX);
854 bindir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
855 libdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
857 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
860 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
863 pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
866 ptr += sprintf (pathbuf, "%s;%s;%s;", self_prefix, bindir, libdir);
867 GNUNET_free (self_prefix);
868 GNUNET_free (bindir);
869 GNUNET_free (libdir);
871 alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len);
872 if (alloc_len != pathbuf_len - 1)
874 GNUNET_free (pathbuf);
875 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */
879 cmdlen = strlen (filename);
880 if ( (cmdlen < 5) || (0 != strcmp (&filename[cmdlen - 4], ".exe")) )
881 GNUNET_asprintf (&non_const_filename, "%s.exe", filename);
883 GNUNET_asprintf (&non_const_filename, "%s", filename);
885 /* It could be in POSIX form, convert it to a DOS path early on */
886 if (ERROR_SUCCESS != (lRet = plibc_conv_to_win_path (non_const_filename, win_path)))
888 SetErrnoFromWinError (lRet);
889 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "plibc_conv_to_win_path",
891 GNUNET_free (non_const_filename);
892 GNUNET_free (pathbuf);
895 GNUNET_free (non_const_filename);
896 non_const_filename = GNUNET_strdup (win_path);
897 /* Check that this is the full path. If it isn't, search. */
898 /* FIXME: convert it to wchar_t and use SearchPathW?
899 * Remember: arguments to _start_process() are technically in UTF-8...
901 if (non_const_filename[1] == ':')
903 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
904 LOG (GNUNET_ERROR_TYPE_DEBUG,
905 "Using path `%s' as-is. PATH is %s\n", path, ptr);
907 else if (!SearchPathA
908 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char),
911 SetErrnoFromWinError (GetLastError ());
912 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
914 GNUNET_free (non_const_filename);
915 GNUNET_free (pathbuf);
919 LOG (GNUNET_ERROR_TYPE_DEBUG,
920 "Found `%s' in PATH `%s'\n", path, pathbuf);
921 GNUNET_free (pathbuf);
922 GNUNET_free (non_const_filename);
924 /* Count the number of arguments */
925 arg = (char **) argv;
932 /* Allocate a copy argv */
933 non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1));
935 /* Copy all argv strings */
937 arg = (char **) argv;
941 non_const_argv[argcount] = GNUNET_strdup (path);
943 non_const_argv[argcount] = GNUNET_strdup (*arg);
947 non_const_argv[argcount] = NULL;
951 arg = non_const_argv;
954 cmdlen = cmdlen + strlen (*arg) + 4;
958 /* Allocate and create cmd */
959 cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen);
960 arg = non_const_argv;
963 char arg_last_char = (*arg)[strlen (*arg) - 1];
964 idx += sprintf (idx, "\"%s%s\"%s", *arg,
965 arg_last_char == '\\' ? "\\" : "", *(arg + 1) ? " " : "");
970 GNUNET_free (non_const_argv[--argcount]);
971 GNUNET_free (non_const_argv);
973 memset (&start, 0, sizeof (start));
974 start.cb = sizeof (start);
975 if ((pipe_stdin != NULL) || (pipe_stdout != NULL) || (std_inheritance != 0))
976 start.dwFlags |= STARTF_USESTDHANDLES;
978 stdih = GetStdHandle (STD_INPUT_HANDLE);
979 GetHandleInformation (stdih, &stdif);
980 if (pipe_stdin != NULL)
982 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
983 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
984 &stdin_handle, sizeof (HANDLE));
985 start.hStdInput = stdin_handle;
989 if (std_inheritance & GNUNET_OS_INHERIT_STD_IN)
991 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 1);
992 if (pipe_stdin == NULL)
993 start.hStdInput = stdih;
996 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 0);
1000 stdoh = GetStdHandle (STD_OUTPUT_HANDLE);
1001 GetHandleInformation (stdoh, &stdof);
1002 if (NULL != pipe_stdout)
1004 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1006 GNUNET_DISK_PIPE_END_WRITE),
1007 &stdout_handle, sizeof (HANDLE));
1008 start.hStdOutput = stdout_handle;
1012 if (std_inheritance & GNUNET_OS_INHERIT_STD_OUT)
1014 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 1);
1015 if (pipe_stdout == NULL)
1016 start.hStdOutput = stdoh;
1019 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 0);
1022 stdeh = GetStdHandle (STD_ERROR_HANDLE);
1023 GetHandleInformation (stdeh, &stdef);
1026 if (std_inheritance & GNUNET_OS_INHERIT_STD_ERR)
1028 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 1);
1029 start.hStdError = stdeh;
1032 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 0);
1035 if (GNUNET_YES == pipe_control)
1037 struct GNUNET_DISK_PipeHandle *childpipe;
1038 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
1039 if (NULL == childpipe)
1041 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
1042 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
1043 GNUNET_DISK_pipe_close (childpipe);
1044 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
1045 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
1046 &childpipe_read_handle, sizeof (HANDLE))))
1049 GNUNET_DISK_file_close (childpipe_read);
1050 if (childpipe_write)
1051 GNUNET_DISK_file_close (childpipe_write);
1055 /* Unlike *nix variant, we don't dup the handle, so can't close
1056 * filehandle right now.
1058 SetHandleInformation (childpipe_read_handle, HANDLE_FLAG_INHERIT, 1);
1062 childpipe_read = NULL;
1063 childpipe_write = NULL;
1066 if (lsocks != NULL && lsocks[0] != INVALID_SOCKET)
1068 lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1070 if (lsocks_pipe == NULL)
1073 GNUNET_DISK_pipe_close (lsocks_pipe);
1074 if (GNUNET_YES == pipe_control)
1076 GNUNET_DISK_file_close (childpipe_write);
1077 GNUNET_DISK_file_close (childpipe_read);
1081 lsocks_write_fd = GNUNET_DISK_pipe_handle (lsocks_pipe,
1082 GNUNET_DISK_PIPE_END_WRITE);
1083 GNUNET_DISK_internal_file_handle_ (lsocks_write_fd,
1084 &lsocks_write, sizeof (HANDLE));
1085 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1086 (lsocks_pipe, GNUNET_DISK_PIPE_END_READ),
1087 &lsocks_read, sizeof (HANDLE));
1092 lsocks_write_fd = NULL;
1096 if (GNUNET_YES == pipe_control)
1098 GNUNET_asprintf (&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE);
1099 GNUNET_asprintf (&our_env[env_off++], "%p", childpipe_read_handle);
1101 if ( (lsocks != NULL) && (lsocks[0] != INVALID_SOCKET))
1103 /*This will tell the child that we're going to send lsocks over the pipe*/
1104 GNUNET_asprintf (&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS");
1105 GNUNET_asprintf (&our_env[env_off++], "%lu", lsocks_read);
1107 our_env[env_off++] = NULL;
1108 env_block = CreateCustomEnvTable (our_env);
1110 GNUNET_free_non_null (our_env[--env_off]);
1113 if (NULL == (wpath = u8_to_u16 ((uint8_t *) path, 1 + strlen (path), NULL, &wpath_len)))
1115 LOG (GNUNET_ERROR_TYPE_DEBUG,
1116 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", path, errno);
1117 GNUNET_free (env_block);
1120 GNUNET_DISK_pipe_close (lsocks_pipe);
1121 if (GNUNET_YES == pipe_control)
1123 GNUNET_DISK_file_close (childpipe_write);
1124 GNUNET_DISK_file_close (childpipe_read);
1130 if (NULL == (wcmd = u8_to_u16 ((uint8_t *) cmd, 1 + strlen (cmd), NULL, &wcmd_len)))
1132 LOG (GNUNET_ERROR_TYPE_DEBUG,
1133 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n",
1136 GNUNET_free (env_block);
1140 GNUNET_DISK_pipe_close (lsocks_pipe);
1141 if (GNUNET_YES == pipe_control)
1143 GNUNET_DISK_file_close (childpipe_write);
1144 GNUNET_DISK_file_close (childpipe_read);
1149 create_no_window = 0;
1151 HANDLE console_input = CreateFile ("CONIN$", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1152 if (INVALID_HANDLE_VALUE == console_input)
1153 create_no_window = CREATE_NO_WINDOW;
1155 CloseHandle (console_input);
1158 bresult = CreateProcessW (wpath, wcmd, NULL, NULL, GNUNET_YES,
1159 create_no_window | CREATE_SUSPENDED, env_block, NULL, &start, &proc);
1160 error_code = GetLastError ();
1162 if ((NULL == pipe_stdin) && (stdih))
1163 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, stdif);
1166 if ((NULL == pipe_stdout) && (stdoh))
1167 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, stdof);
1170 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, stdef);
1173 LOG (GNUNET_ERROR_TYPE_ERROR,
1174 "CreateProcess(%s, %s) failed: %lu\n",
1179 GNUNET_free (env_block);
1183 if (GNUNET_YES == pipe_control)
1185 GNUNET_DISK_file_close (childpipe_read);
1190 if (GNUNET_YES == pipe_control)
1192 GNUNET_DISK_file_close (childpipe_write);
1195 GNUNET_DISK_pipe_close (lsocks_pipe);
1196 SetErrnoFromWinError (error_code);
1200 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
1201 gnunet_proc->pid = proc.dwProcessId;
1202 gnunet_proc->handle = proc.hProcess;
1203 gnunet_proc->control_pipe = childpipe_write;
1205 CreateThread (NULL, 64000, &child_wait_thread, (void *) gnunet_proc, 0, NULL);
1207 ResumeThread (proc.hThread);
1208 CloseHandle (proc.hThread);
1210 if ( (NULL == lsocks) || (INVALID_SOCKET == lsocks[0]) )
1213 GNUNET_DISK_pipe_close_end (lsocks_pipe, GNUNET_DISK_PIPE_END_READ);
1215 /* This is a replacement for "goto error" that doesn't use goto */
1224 /* Tell the number of sockets */
1225 for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++);
1227 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1228 if (sizeof (count) != wrote)
1230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1231 "Failed to write %u count bytes to the child: %lu\n",
1232 sizeof (count), GetLastError ());
1235 for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++)
1237 WSAPROTOCOL_INFOA pi;
1238 /* Get a socket duplication info */
1239 if (SOCKET_ERROR == WSADuplicateSocketA (lsocks[i], gnunet_proc->pid, &pi))
1241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1242 "Failed to duplicate an socket[%u]: %lu\n", i,
1246 /* Synchronous I/O is not nice, but we can't schedule this:
1247 * lsocks will be closed/freed by the caller soon, and until
1248 * the child creates a duplicate, closing a socket here will
1249 * close it for good.
1251 /* Send the size of the structure
1252 * (the child might be built with different headers...)
1255 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &size, sizeof (size));
1256 if (sizeof (size) != wrote)
1258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1259 "Failed to write %u size[%u] bytes to the child: %lu\n",
1260 sizeof (size), i, GetLastError ());
1263 /* Finally! Send the data */
1264 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &pi, sizeof (pi));
1265 if (sizeof (pi) != wrote)
1267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1268 "Failed to write %u socket[%u] bytes to the child: %lu\n",
1269 sizeof (pi), i, GetLastError ());
1273 /* This will block us until the child makes a final read or closes
1274 * the pipe (hence no 'wrote' check), since we have to wait for it
1275 * to duplicate the last socket, before we return and start closing
1278 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1283 GNUNET_DISK_file_sync (lsocks_write_fd);
1284 GNUNET_DISK_pipe_close (lsocks_pipe);
1288 /* If we can't pass on the socket(s), the child will block forever,
1289 * better put it out of its misery.
1291 SafeTerminateProcess (gnunet_proc->handle, 0, 0);
1292 CloseHandle (gnunet_proc->handle);
1293 if (NULL != gnunet_proc->control_pipe)
1294 GNUNET_DISK_file_close (gnunet_proc->control_pipe);
1295 GNUNET_free (gnunet_proc);
1306 * @param pipe_control should a pipe be used to send signals to the child?
1307 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1308 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1309 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1310 * @param pipe_stderr pipe to use to get output from child process (or NULL)
1311 * @param filename name of the binary
1312 * @param argv NULL-terminated array of arguments to the process
1313 * @return pointer to process structure of the new process, NULL on error
1315 struct GNUNET_OS_Process *
1316 GNUNET_OS_start_process_vap (int pipe_control,
1317 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1318 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1319 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1320 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1321 const char *filename,
1324 return start_process (pipe_control,
1338 * @param pipe_control should a pipe be used to send signals to the child?
1339 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1340 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1341 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1342 * @param pipe_stderr pipe to use to get output from child process (or NULL)
1343 * @param filename name of the binary
1344 * @param va NULL-terminated list of arguments to the process
1345 * @return pointer to process structure of the new process, NULL on error
1347 struct GNUNET_OS_Process *
1348 GNUNET_OS_start_process_va (int pipe_control,
1349 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1350 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1351 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1352 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1353 const char *filename, va_list va)
1355 struct GNUNET_OS_Process *ret;
1362 while (NULL != va_arg (ap, char *))
1365 argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
1368 while (NULL != (argv[argc] = va_arg (ap, char *)))
1371 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 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1400 const char *filename, ...)
1402 struct GNUNET_OS_Process *ret;
1405 va_start (ap, filename);
1406 ret = GNUNET_OS_start_process_va (pipe_control,
1421 * @param pipe_control should a pipe be used to send signals to the child?
1422 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
1423 * std handles of the parent are inherited by the child.
1424 * pipe_stdin and pipe_stdout take priority over std_inheritance
1425 * (when they are non-NULL).
1426 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1427 * must be NULL on platforms where dup is not supported
1428 * @param filename name of the binary
1429 * @param argv NULL-terminated list of arguments to the process
1430 * @return process ID of the new process, -1 on error
1432 struct GNUNET_OS_Process *
1433 GNUNET_OS_start_process_v (int pipe_control,
1434 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1435 const SOCKTYPE *lsocks,
1436 const char *filename,
1439 return start_process (pipe_control,
1451 * Start a process. This function is similar to the GNUNET_OS_start_process_*
1452 * except that the filename and arguments can have whole strings which contain
1453 * the arguments. These arguments are to be separated by spaces and are parsed
1454 * in the order they appear. Arguments containing spaces can be used by
1455 * quoting them with @em ".
1457 * @param pipe_control should a pipe be used to send signals to the child?
1458 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1459 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1460 * must be NULL on platforms where dup is not supported
1461 * @param filename name of the binary. It is valid to have the arguments
1462 * in this string when they are separated by spaces.
1463 * @param ... more arguments. Should be of type `char *`. It is valid
1464 * to have the arguments in these strings when they are separated by
1465 * spaces. The last argument MUST be NULL.
1466 * @return pointer to process structure of the new process, NULL on error
1468 struct GNUNET_OS_Process *
1469 GNUNET_OS_start_process_s (int pipe_control,
1470 unsigned int std_inheritance,
1471 const SOCKTYPE * lsocks,
1472 const char *filename, ...)
1476 unsigned int argv_size;
1482 struct GNUNET_OS_Process *proc;
1489 va_start (ap, filename);
1496 while ('\0' != *rpos)
1505 if ( (' ' == *rpos) && (0 == quote_on) )
1511 while (' ' == *rpos)
1514 if ( (NULL == last) && ('\0' != *rpos) ) // FIXME: == or !=?
1522 while (NULL != (arg = (va_arg (ap, const char*))));
1525 argv = GNUNET_malloc (argv_size * sizeof (char *));
1527 va_start (ap, filename);
1532 cp = GNUNET_strdup (arg);
1535 while ('\0' != *pos)
1544 if ( (' ' == *pos) && (0 == quote_on) )
1548 argv[argv_size++] = GNUNET_strdup (last);
1554 if ( (NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
1560 argv[argv_size++] = GNUNET_strdup (last);
1564 while (NULL != (arg = (va_arg (ap, const char*))));
1566 argv[argv_size] = NULL;
1568 for(i = 0; i < argv_size; i++)
1570 len = strlen (argv[i]);
1571 if ( (argv[i][0] == '"') && (argv[i][len-1] == '"'))
1573 memmove (&argv[i][0], &argv[i][1], len - 2);
1574 argv[i][len-2] = '\0';
1577 binary_path = argv[0];
1578 proc = GNUNET_OS_start_process_v (pipe_control, std_inheritance, lsocks,
1580 while (argv_size > 0)
1581 GNUNET_free (argv[--argv_size]);
1588 * Retrieve the status of a process, waiting on him if dead.
1589 * Nonblocking version.
1591 * @param proc process ID
1592 * @param type status type
1593 * @param code return code/signal number
1594 * @param options WNOHANG if non-blocking is desired
1595 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1598 process_status (struct GNUNET_OS_Process *proc,
1599 enum GNUNET_OS_ProcessStatusType *type,
1600 unsigned long *code,
1607 GNUNET_assert (0 != proc);
1608 ret = waitpid (proc->pid, &status, options);
1611 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1613 return GNUNET_SYSERR;
1617 *type = GNUNET_OS_PROCESS_RUNNING;
1621 if (proc->pid != ret)
1623 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1624 return GNUNET_SYSERR;
1626 if (WIFEXITED (status))
1628 *type = GNUNET_OS_PROCESS_EXITED;
1629 *code = WEXITSTATUS (status);
1631 else if (WIFSIGNALED (status))
1633 *type = GNUNET_OS_PROCESS_SIGNALED;
1634 *code = WTERMSIG (status);
1636 else if (WIFSTOPPED (status))
1638 *type = GNUNET_OS_PROCESS_SIGNALED;
1639 *code = WSTOPSIG (status);
1642 else if (WIFCONTINUED (status))
1644 *type = GNUNET_OS_PROCESS_RUNNING;
1650 *type = GNUNET_OS_PROCESS_UNKNOWN;
1655 #define WNOHANG 42 /* just a flag for W32, purely internal at this point */
1659 DWORD c, error_code, ret;
1663 if (h == NULL || ret == 0)
1665 LOG (GNUNET_ERROR_TYPE_WARNING,
1666 "Invalid process information {%d, %08X}\n",
1668 return GNUNET_SYSERR;
1671 h = GetCurrentProcess ();
1673 if (WNOHANG != options)
1675 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1677 SetErrnoFromWinError (GetLastError ());
1678 return GNUNET_SYSERR;
1682 ret = GetExitCodeProcess (h, &c);
1683 error_code = GetLastError ();
1684 if (ret == 0 || error_code != NO_ERROR)
1686 SetErrnoFromWinError (error_code);
1687 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
1688 return GNUNET_SYSERR;
1690 if (STILL_ACTIVE == c)
1692 *type = GNUNET_OS_PROCESS_RUNNING;
1696 *type = GNUNET_OS_PROCESS_EXITED;
1705 * Retrieve the status of a process, waiting on him if dead.
1706 * Nonblocking version.
1708 * @param proc process ID
1709 * @param type status type
1710 * @param code return code/signal number
1711 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1714 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1715 enum GNUNET_OS_ProcessStatusType *type,
1716 unsigned long *code)
1718 return process_status (proc,
1726 * Retrieve the status of a process, waiting on him if dead.
1729 * @param proc pointer to process structure
1730 * @param type status type
1731 * @param code return code/signal number
1732 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1735 GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc,
1736 enum GNUNET_OS_ProcessStatusType *type,
1737 unsigned long *code)
1739 return process_status (proc,
1747 * Wait for a process to terminate. The return code is discarded.
1748 * You must not use #GNUNET_OS_process_status() on the same process
1749 * after calling this function! This function is blocking and should
1750 * thus only be used if the child process is known to have terminated
1751 * or to terminate very soon.
1753 * @param proc pointer to process structure
1754 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1757 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1760 pid_t pid = proc->pid;
1763 while ( (pid != (ret = waitpid (pid, NULL, 0))) &&
1764 (EINTR == errno) ) ;
1767 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1769 return GNUNET_SYSERR;
1778 LOG (GNUNET_ERROR_TYPE_WARNING,
1779 "Invalid process information {%d, %08X}\n",
1781 return GNUNET_SYSERR;
1784 h = GetCurrentProcess ();
1786 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1788 SetErrnoFromWinError (GetLastError ());
1789 return GNUNET_SYSERR;
1797 * Handle to a command.
1799 struct GNUNET_OS_CommandHandle
1805 struct GNUNET_OS_Process *eip;
1808 * Handle to the output pipe.
1810 struct GNUNET_DISK_PipeHandle *opipe;
1813 * Read-end of output pipe.
1815 const struct GNUNET_DISK_FileHandle *r;
1818 * Function to call on each line of output.
1820 GNUNET_OS_LineProcessor proc;
1823 * Closure for @e proc.
1828 * Buffer for the output.
1833 * Task reading from pipe.
1835 struct GNUNET_SCHEDULER_Task *rtask;
1840 struct GNUNET_TIME_Absolute timeout;
1843 * Current read offset in buf.
1850 * Stop/kill a command. Must ONLY be called either from
1851 * the callback after 'NULL' was passed for 'line' *OR*
1852 * from an independent task (not within the line processor).
1854 * @param cmd handle to the process
1857 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
1859 if (NULL != cmd->proc)
1861 GNUNET_assert (NULL != cmd->rtask);
1862 GNUNET_SCHEDULER_cancel (cmd->rtask);
1864 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1865 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
1866 GNUNET_OS_process_destroy (cmd->eip);
1867 GNUNET_DISK_pipe_close (cmd->opipe);
1873 * Read from the process and call the line processor.
1875 * @param cls the `struct GNUNET_OS_CommandHandle *`
1878 cmd_read (void *cls)
1880 struct GNUNET_OS_CommandHandle *cmd = cls;
1881 const struct GNUNET_SCHEDULER_TaskContext *tc;
1882 GNUNET_OS_LineProcessor proc;
1887 tc = GNUNET_SCHEDULER_get_task_context ();
1889 GNUNET_NETWORK_fdset_handle_isset (tc->read_ready,
1895 proc (cmd->proc_cls, NULL);
1898 ret = GNUNET_DISK_file_read (cmd->r,
1899 &cmd->buf[cmd->off],
1900 sizeof (cmd->buf) - cmd->off);
1903 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
1905 cmd->buf[cmd->off] = '\0';
1906 cmd->proc (cmd->proc_cls, cmd->buf);
1910 proc (cmd->proc_cls, NULL);
1913 end = memchr (&cmd->buf[cmd->off], '\n', ret);
1918 cmd->proc (cmd->proc_cls, cmd->buf);
1919 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1920 cmd->off -= (end + 1 - cmd->buf);
1921 end = memchr (cmd->buf, '\n', cmd->off);
1924 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
1932 * Run the given command line and call the given function
1933 * for each line of the output.
1935 * @param proc function to call for each line of the output
1936 * @param proc_cls closure for @a proc
1937 * @param timeout when to time out
1938 * @param binary command to run
1939 * @param ... arguments to command
1940 * @return NULL on error
1942 struct GNUNET_OS_CommandHandle *
1943 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
1945 struct GNUNET_TIME_Relative timeout,
1949 struct GNUNET_OS_CommandHandle *cmd;
1950 struct GNUNET_OS_Process *eip;
1951 struct GNUNET_DISK_PipeHandle *opipe;
1954 opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES,
1955 GNUNET_NO, GNUNET_YES);
1958 va_start (ap, binary);
1959 /* redirect stdout, don't inherit stderr/stdin */
1960 eip = GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL,
1961 opipe, NULL, binary,
1966 GNUNET_DISK_pipe_close (opipe);
1969 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
1970 cmd = GNUNET_new (struct GNUNET_OS_CommandHandle);
1971 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1975 cmd->proc_cls = proc_cls;
1976 cmd->r = GNUNET_DISK_pipe_handle (opipe,
1977 GNUNET_DISK_PIPE_END_READ);
1978 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout,
1986 /* end of os_priority.c */