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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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-os-priority", __VA_ARGS__)
34 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-os-priority", syscall)
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-os-priority", 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;
69 * Handle for the #parent_control_handler() Task.
71 static struct GNUNET_SCHEDULER_Task *pch;
74 * Handle for the #shutdown_pch() Task.
76 static struct GNUNET_SCHEDULER_Task *spch;
80 * This handler is called on shutdown to remove the #pch.
82 * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe
85 shutdown_pch (void *cls)
87 struct GNUNET_DISK_FileHandle *control_pipe = cls;
89 GNUNET_SCHEDULER_cancel (pch);
91 GNUNET_DISK_file_close (control_pipe);
97 * This handler is called when there are control data to be read on the pipe
99 * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe
102 parent_control_handler (void *cls)
104 struct GNUNET_DISK_FileHandle *control_pipe = cls;
110 ret = GNUNET_DISK_file_read (control_pipe,
113 if (sizeof (sig) != ret)
116 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
117 "GNUNET_DISK_file_read");
118 LOG (GNUNET_ERROR_TYPE_DEBUG,
119 "Closing control pipe\n");
120 GNUNET_DISK_file_close (control_pipe);
122 GNUNET_SCHEDULER_cancel (spch);
126 pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE);
127 GNUNET_assert ( (NULL == pipe_fd) ||
128 (strlen (pipe_fd) <= 0) );
129 LOG (GNUNET_ERROR_TYPE_DEBUG,
130 "Got control code %d from parent via pipe %s\n",
133 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
135 &parent_control_handler,
137 GNUNET_SIGNAL_raise ((int) sig);
142 * Task that connects this process to its parent via pipe;
143 * essentially, the parent control handler will read signal numbers
144 * from the #GNUNET_OS_CONTROL_PIPE (as given in an environment
145 * variable) and raise those signals.
147 * @param cls closure (unused)
150 GNUNET_OS_install_parent_control_handler (void *cls)
154 struct GNUNET_DISK_FileHandle *control_pipe;
160 /* already done, we've been called twice... */
164 env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
165 if ( (NULL == env_buf) || (strlen (env_buf) <= 0) )
167 LOG (GNUNET_ERROR_TYPE_DEBUG,
168 "Not installing a handler because $%s is empty\n",
169 GNUNET_OS_CONTROL_PIPE);
170 putenv (GNUNET_OS_CONTROL_PIPE "=");
174 pipe_fd = strtoull (env_buf, &env_buf_end, 16);
175 if ((0 != errno) || (env_buf == env_buf_end))
177 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
180 putenv (GNUNET_OS_CONTROL_PIPE "=");
183 #if !defined (WINDOWS)
184 if (pipe_fd >= FD_SETSIZE)
186 if ((FILE_TYPE_UNKNOWN == GetFileType ((HANDLE) (uintptr_t) pipe_fd))
187 && (0 != GetLastError ()))
190 LOG (GNUNET_ERROR_TYPE_ERROR,
191 "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n",
193 putenv (GNUNET_OS_CONTROL_PIPE "=");
197 control_pipe = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) (uintptr_t) pipe_fd);
199 control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
201 if (NULL == control_pipe)
203 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
206 putenv (GNUNET_OS_CONTROL_PIPE "=");
209 LOG (GNUNET_ERROR_TYPE_DEBUG,
210 "Adding parent control handler pipe `%s' to the scheduler\n",
212 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
214 &parent_control_handler,
216 spch = GNUNET_SCHEDULER_add_shutdown (&shutdown_pch,
218 putenv (GNUNET_OS_CONTROL_PIPE "=");
223 * Get process structure for current process
225 * The pointer it returns points to static memory location and must
226 * not be deallocated/closed.
228 * @return pointer to the process sturcutre for this process
230 struct GNUNET_OS_Process *
231 GNUNET_OS_process_current ()
234 current_process.pid = GetCurrentProcessId ();
235 current_process.handle = GetCurrentProcess ();
237 current_process.pid = 0;
239 return ¤t_process;
244 * Sends a signal to the process
246 * @param proc pointer to process structure
248 * @return 0 on success, -1 on error
251 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc,
258 if (NULL != proc->control_pipe)
260 LOG (GNUNET_ERROR_TYPE_DEBUG,
261 "Sending signal %d to pid: %u via pipe\n",
264 ret = GNUNET_DISK_file_write (proc->control_pipe,
267 if (sizeof (csig) == ret)
270 /* pipe failed or non-existent, try other methods */
273 #if !defined (WINDOWS)
279 #if (SIGTERM != GNUNET_TERM_SIG)
280 case GNUNET_TERM_SIG:
282 #if defined(WINDOWS) && !defined(__CYGWIN__)
285 int must_kill = GNUNET_YES;
286 if (0 != GetExitCodeProcess (proc->handle, &exitcode))
287 must_kill = (exitcode == STILL_ACTIVE) ? GNUNET_YES : GNUNET_NO;
288 if (GNUNET_YES == must_kill)
290 if (0 == SafeTerminateProcess (proc->handle, 0, 0))
292 DWORD error_code = GetLastError ();
293 if ( (error_code != WAIT_TIMEOUT) &&
294 (error_code != ERROR_PROCESS_ABORTED) )
296 LOG ((error_code == ERROR_ACCESS_DENIED) ?
297 GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
298 "SafeTermiateProcess failed with code %lu\n",
300 /* The problem here is that a process that is already dying
301 * might cause SafeTerminateProcess to fail with
302 * ERROR_ACCESS_DENIED, but the process WILL die eventually.
303 * If we really had a permissions problem, hanging up (which
304 * is what will happen in process_wait() in that case) is
307 if (ERROR_ACCESS_DENIED == error_code)
313 SetErrnoFromWinError (error_code);
322 LOG (GNUNET_ERROR_TYPE_DEBUG,
323 "Sending signal %d to pid: %u via system call\n",
326 return PLIBC_KILL (proc->pid, sig);
329 #if defined (WINDOWS)
333 LOG (GNUNET_ERROR_TYPE_DEBUG,
334 "Sending signal %d to pid: %u via system call\n",
337 return PLIBC_KILL (proc->pid, sig);
344 * Get the pid of the process in question
346 * @param proc the process to get the pid of
348 * @return the current process id
351 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process * proc)
358 * Cleans up process structure contents (OS-dependent) and deallocates
361 * @param proc pointer to process structure
364 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
366 if (NULL != proc->control_pipe)
367 GNUNET_DISK_file_close (proc->control_pipe);
368 #if defined (WINDOWS)
369 if (NULL != proc->handle)
370 CloseHandle (proc->handle);
377 #include "gnunet_signal_lib.h"
379 extern GNUNET_SIGNAL_Handler w32_sigchld_handler;
382 * Make seaspider happy.
384 #define DWORD_WINAPI DWORD WINAPI
388 * @brief Waits for a process to terminate and invokes the SIGCHLD handler
389 * @param proc pointer to process structure
392 child_wait_thread (void *arg)
394 struct GNUNET_OS_Process *proc = (struct GNUNET_OS_Process *) arg;
396 WaitForSingleObject (proc->handle, INFINITE);
398 if (w32_sigchld_handler)
399 w32_sigchld_handler ();
408 CreateCustomEnvTable (char **vars)
410 char *win32_env_table;
415 size_t tablesize = 0;
416 size_t items_count = 0;
425 win32_env_table = GetEnvironmentStringsA ();
426 if (NULL == win32_env_table)
428 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ;
430 index = GNUNET_malloc (sizeof (char *) * n_var);
431 for (c = 0; c < n_var; c++)
433 for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++)
435 size_t len = strlen (ptr);
438 for (var_ptr = vars; *var_ptr; var_ptr++)
442 var_len = strlen (var);
443 if (strncmp (var, ptr, var_len) == 0)
447 tablesize += var_len + strlen (val) + 1;
452 tablesize += len + 1;
455 for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
460 n_found += strlen (var) + strlen (val) + 1;
462 result = GNUNET_malloc (tablesize + n_found + 1);
463 for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
465 size_t len = strlen (ptr);
468 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
472 var_len = strlen (var);
473 if (strncmp (var, ptr, var_len) == 0)
481 strcpy (result_ptr, ptr);
482 result_ptr += len + 1;
486 strcpy (result_ptr, var);
487 result_ptr += var_len;
488 strcpy (result_ptr, val);
489 result_ptr += strlen (val) + 1;
493 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
497 var_len = strlen (var);
500 strcpy (result_ptr, var);
501 result_ptr += var_len;
502 strcpy (result_ptr, val);
503 result_ptr += strlen (val) + 1;
506 FreeEnvironmentStrings (win32_env_table);
515 * Open '/dev/null' and make the result the given
518 * @param target_fd desired FD to point to /dev/null
519 * @param flags open flags (O_RDONLY, O_WRONLY)
522 open_dev_null (int target_fd,
527 fd = open ("/dev/null", flags);
530 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
537 if (-1 == dup2 (fd, target_fd))
539 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
543 GNUNET_break (0 == close (fd));
551 * @param pipe_control should a pipe be used to send signals to the child?
552 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
553 * std handles of the parent are inherited by the child.
554 * pipe_stdin and pipe_stdout take priority over std_inheritance
555 * (when they are non-NULL).
556 * @param pipe_stdin pipe to use to send input to child process (or NULL)
557 * @param pipe_stdout pipe to use to get output from child process (or NULL)
558 * @param pipe_stderr pipe to use for stderr for child process (or NULL)
559 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
560 * must be NULL on platforms where dup is not supported
561 * @param filename name of the binary
562 * @param argv NULL-terminated list of arguments to the process
563 * @return process ID of the new process, -1 on error
565 static struct GNUNET_OS_Process *
566 start_process (int pipe_control,
567 enum GNUNET_OS_InheritStdioFlags std_inheritance,
568 struct GNUNET_DISK_PipeHandle *pipe_stdin,
569 struct GNUNET_DISK_PipeHandle *pipe_stdout,
570 struct GNUNET_DISK_PipeHandle *pipe_stderr,
571 const SOCKTYPE *lsocks,
572 const char *filename,
578 struct GNUNET_OS_Process *gnunet_proc;
579 struct GNUNET_DISK_FileHandle *childpipe_read;
580 struct GNUNET_DISK_FileHandle *childpipe_write;
581 int childpipe_read_fd;
597 GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
598 return NULL; /* not executable */
599 if (GNUNET_YES == pipe_control)
601 struct GNUNET_DISK_PipeHandle *childpipe;
602 int dup_childpipe_read_fd = -1;
604 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
605 GNUNET_YES, GNUNET_NO);
606 if (NULL == childpipe)
608 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe,
609 GNUNET_DISK_PIPE_END_READ);
610 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe,
611 GNUNET_DISK_PIPE_END_WRITE);
612 GNUNET_DISK_pipe_close (childpipe);
613 if ( (NULL == childpipe_read) ||
614 (NULL == childpipe_write) ||
616 GNUNET_DISK_internal_file_handle_ (childpipe_read,
619 (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
621 if (NULL != childpipe_read)
622 GNUNET_DISK_file_close (childpipe_read);
623 if (NULL != childpipe_write)
624 GNUNET_DISK_file_close (childpipe_write);
625 if (0 <= dup_childpipe_read_fd)
626 close (dup_childpipe_read_fd);
629 childpipe_read_fd = dup_childpipe_read_fd;
630 GNUNET_DISK_file_close (childpipe_read);
634 childpipe_write = NULL;
635 childpipe_read_fd = -1;
637 if (NULL != pipe_stdin)
639 GNUNET_assert (GNUNET_OK ==
640 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
641 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
642 &fd_stdin_read, sizeof (int)));
643 GNUNET_assert (GNUNET_OK ==
644 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
645 (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
646 &fd_stdin_write, sizeof (int)));
648 if (NULL != pipe_stdout)
650 GNUNET_assert (GNUNET_OK ==
651 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
653 GNUNET_DISK_PIPE_END_WRITE),
654 &fd_stdout_write, sizeof (int)));
655 GNUNET_assert (GNUNET_OK ==
656 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
657 (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
658 &fd_stdout_read, sizeof (int)));
660 if (NULL != pipe_stderr)
662 GNUNET_assert (GNUNET_OK ==
663 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
665 GNUNET_DISK_PIPE_END_READ),
666 &fd_stderr_read, sizeof (int)));
667 GNUNET_assert (GNUNET_OK ==
668 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
670 GNUNET_DISK_PIPE_END_WRITE),
671 &fd_stderr_write, sizeof (int)));
678 while (-1 != (k = lsocks[i++]))
679 GNUNET_array_append (lscp, ls, k);
680 GNUNET_array_append (lscp, ls, -1);
683 /* see https://gnunet.org/vfork */
691 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
692 GNUNET_array_grow (lscp, ls, 0);
693 if (NULL != childpipe_write)
694 GNUNET_DISK_file_close (childpipe_write);
695 if (0 <= childpipe_read_fd)
696 close (childpipe_read_fd);
702 unsetenv (GNUNET_OS_CONTROL_PIPE);
703 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
704 gnunet_proc->pid = ret;
705 gnunet_proc->control_pipe = childpipe_write;
706 if (GNUNET_YES == pipe_control)
708 close (childpipe_read_fd);
710 GNUNET_array_grow (lscp, ls, 0);
713 if (0 <= childpipe_read_fd)
717 /* due to vfork, we must NOT free memory on DARWIN! */
718 GNUNET_DISK_file_close (childpipe_write);
720 snprintf (fdbuf, 100, "%x", childpipe_read_fd);
721 setenv (GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
724 unsetenv (GNUNET_OS_CONTROL_PIPE);
725 if (NULL != pipe_stdin)
727 GNUNET_break (0 == close (fd_stdin_write));
728 if (-1 == dup2 (fd_stdin_read, 0))
729 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
730 GNUNET_break (0 == close (fd_stdin_read));
732 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
734 GNUNET_break (0 == close (0));
735 open_dev_null (0, O_RDONLY);
737 if (NULL != pipe_stdout)
739 GNUNET_break (0 == close (fd_stdout_read));
740 if (-1 == dup2 (fd_stdout_write, 1))
741 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
742 GNUNET_break (0 == close (fd_stdout_write));
744 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
746 GNUNET_break (0 == close (1));
747 open_dev_null (1, O_WRONLY);
749 if (NULL != pipe_stderr)
751 GNUNET_break (0 == close (fd_stderr_read));
752 if (-1 == dup2 (fd_stderr_write, 2))
753 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
754 GNUNET_break (0 == close (fd_stderr_write));
756 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
758 GNUNET_break (0 == close (2));
759 open_dev_null (2, O_WRONLY);
763 /* read systemd documentation... */
766 while (-1 != lscp[i])
769 while (-1 != lscp[j])
775 GNUNET_assert (-1 != k);
776 GNUNET_assert (0 == close (lscp[j]));
784 /* Bury any existing FD, no matter what; they should all be closed
785 * on exec anyway and the important onces have been dup'ed away */
787 GNUNET_assert (-1 != dup2 (lscp[i], tgt));
789 /* unset close-on-exec flag */
790 flags = fcntl (tgt, F_GETFD);
791 GNUNET_assert (flags >= 0);
792 flags &= ~FD_CLOEXEC;
794 (void) fcntl (tgt, F_SETFD, flags);
798 GNUNET_snprintf (fds, sizeof (fds), "%u", i);
799 setenv ("LISTEN_FDS", fds, 1);
802 /* due to vfork, we must NOT free memory on DARWIN! */
803 GNUNET_array_grow (lscp, ls, 0);
805 execvp (filename, argv);
806 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
809 struct GNUNET_DISK_FileHandle *childpipe_read;
810 struct GNUNET_DISK_FileHandle *childpipe_write;
811 HANDLE childpipe_read_handle;
813 char **non_const_argv;
818 PROCESS_INFORMATION proc;
820 struct GNUNET_OS_Process *gnunet_proc;
821 char path[MAX_PATH + 1];
822 char *our_env[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
823 char *env_block = NULL;
831 char *non_const_filename;
832 char win_path[MAX_PATH + 1];
833 struct GNUNET_DISK_PipeHandle *lsocks_pipe;
834 const struct GNUNET_DISK_FileHandle *lsocks_write_fd;
845 HANDLE stdout_handle;
846 HANDLE stdih, stdoh, stdeh;
847 DWORD stdif, stdof, stdef;
850 DWORD create_no_window;
852 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
853 return NULL; /* not executable */
855 /* Search in prefix dir (hopefully - the directory from which
856 * the current module was loaded), bindir and libdir, then in PATH
858 self_prefix = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_SELF_PREFIX);
859 bindir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
860 libdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
862 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
865 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
868 pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
871 ptr += sprintf (pathbuf, "%s;%s;%s;", self_prefix, bindir, libdir);
872 GNUNET_free (self_prefix);
873 GNUNET_free (bindir);
874 GNUNET_free (libdir);
876 alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len);
877 if (alloc_len != pathbuf_len - 1)
879 GNUNET_free (pathbuf);
880 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */
884 cmdlen = strlen (filename);
885 if ( (cmdlen < 5) || (0 != strcmp (&filename[cmdlen - 4], ".exe")) )
886 GNUNET_asprintf (&non_const_filename, "%s.exe", filename);
888 GNUNET_asprintf (&non_const_filename, "%s", filename);
890 /* It could be in POSIX form, convert it to a DOS path early on */
891 if (ERROR_SUCCESS != (lRet = plibc_conv_to_win_path (non_const_filename, win_path)))
893 SetErrnoFromWinError (lRet);
894 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "plibc_conv_to_win_path",
896 GNUNET_free (non_const_filename);
897 GNUNET_free (pathbuf);
900 GNUNET_free (non_const_filename);
901 non_const_filename = GNUNET_strdup (win_path);
902 /* Check that this is the full path. If it isn't, search. */
903 /* FIXME: convert it to wchar_t and use SearchPathW?
904 * Remember: arguments to _start_process() are technically in UTF-8...
906 if (non_const_filename[1] == ':')
908 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
909 LOG (GNUNET_ERROR_TYPE_DEBUG,
910 "Using path `%s' as-is. PATH is %s\n", path, ptr);
912 else if (!SearchPathA
913 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char),
916 SetErrnoFromWinError (GetLastError ());
917 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
919 GNUNET_free (non_const_filename);
920 GNUNET_free (pathbuf);
924 LOG (GNUNET_ERROR_TYPE_DEBUG,
925 "Found `%s' in PATH `%s'\n", path, pathbuf);
926 GNUNET_free (pathbuf);
927 GNUNET_free (non_const_filename);
929 /* Count the number of arguments */
930 arg = (char **) argv;
937 /* Allocate a copy argv */
938 non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1));
940 /* Copy all argv strings */
942 arg = (char **) argv;
946 non_const_argv[argcount] = GNUNET_strdup (path);
948 non_const_argv[argcount] = GNUNET_strdup (*arg);
952 non_const_argv[argcount] = NULL;
956 arg = non_const_argv;
959 cmdlen = cmdlen + strlen (*arg) + 4;
963 /* Allocate and create cmd */
964 cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen);
965 arg = non_const_argv;
968 char arg_last_char = (*arg)[strlen (*arg) - 1];
969 idx += sprintf (idx, "\"%s%s\"%s", *arg,
970 arg_last_char == '\\' ? "\\" : "", *(arg + 1) ? " " : "");
975 GNUNET_free (non_const_argv[--argcount]);
976 GNUNET_free (non_const_argv);
978 memset (&start, 0, sizeof (start));
979 start.cb = sizeof (start);
980 if ((pipe_stdin != NULL) || (pipe_stdout != NULL) || (std_inheritance != 0))
981 start.dwFlags |= STARTF_USESTDHANDLES;
983 stdih = GetStdHandle (STD_INPUT_HANDLE);
984 GetHandleInformation (stdih, &stdif);
985 if (pipe_stdin != NULL)
987 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
988 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
989 &stdin_handle, sizeof (HANDLE));
990 start.hStdInput = stdin_handle;
994 if (std_inheritance & GNUNET_OS_INHERIT_STD_IN)
996 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 1);
997 if (pipe_stdin == NULL)
998 start.hStdInput = stdih;
1001 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 0);
1005 stdoh = GetStdHandle (STD_OUTPUT_HANDLE);
1006 GetHandleInformation (stdoh, &stdof);
1007 if (NULL != pipe_stdout)
1009 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1011 GNUNET_DISK_PIPE_END_WRITE),
1012 &stdout_handle, sizeof (HANDLE));
1013 start.hStdOutput = stdout_handle;
1017 if (std_inheritance & GNUNET_OS_INHERIT_STD_OUT)
1019 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 1);
1020 if (pipe_stdout == NULL)
1021 start.hStdOutput = stdoh;
1024 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 0);
1027 stdeh = GetStdHandle (STD_ERROR_HANDLE);
1028 GetHandleInformation (stdeh, &stdef);
1031 if (std_inheritance & GNUNET_OS_INHERIT_STD_ERR)
1033 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 1);
1034 start.hStdError = stdeh;
1037 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 0);
1040 if (GNUNET_YES == pipe_control)
1042 struct GNUNET_DISK_PipeHandle *childpipe;
1043 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
1044 if (NULL == childpipe)
1046 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
1047 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
1048 GNUNET_DISK_pipe_close (childpipe);
1049 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
1050 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
1051 &childpipe_read_handle, sizeof (HANDLE))))
1054 GNUNET_DISK_file_close (childpipe_read);
1055 if (childpipe_write)
1056 GNUNET_DISK_file_close (childpipe_write);
1060 /* Unlike *nix variant, we don't dup the handle, so can't close
1061 * filehandle right now.
1063 SetHandleInformation (childpipe_read_handle, HANDLE_FLAG_INHERIT, 1);
1067 childpipe_read = NULL;
1068 childpipe_write = NULL;
1071 if (lsocks != NULL && lsocks[0] != INVALID_SOCKET)
1073 lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1075 if (lsocks_pipe == NULL)
1078 GNUNET_DISK_pipe_close (lsocks_pipe);
1079 if (GNUNET_YES == pipe_control)
1081 GNUNET_DISK_file_close (childpipe_write);
1082 GNUNET_DISK_file_close (childpipe_read);
1086 lsocks_write_fd = GNUNET_DISK_pipe_handle (lsocks_pipe,
1087 GNUNET_DISK_PIPE_END_WRITE);
1088 GNUNET_DISK_internal_file_handle_ (lsocks_write_fd,
1089 &lsocks_write, sizeof (HANDLE));
1090 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1091 (lsocks_pipe, GNUNET_DISK_PIPE_END_READ),
1092 &lsocks_read, sizeof (HANDLE));
1097 lsocks_write_fd = NULL;
1101 if (GNUNET_YES == pipe_control)
1103 GNUNET_asprintf (&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE);
1104 GNUNET_asprintf (&our_env[env_off++], "%p", childpipe_read_handle);
1106 if ( (lsocks != NULL) && (lsocks[0] != INVALID_SOCKET))
1108 /*This will tell the child that we're going to send lsocks over the pipe*/
1109 GNUNET_asprintf (&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS");
1110 GNUNET_asprintf (&our_env[env_off++], "%lu", lsocks_read);
1112 our_env[env_off++] = NULL;
1113 env_block = CreateCustomEnvTable (our_env);
1115 GNUNET_free_non_null (our_env[--env_off]);
1118 if (NULL == (wpath = u8_to_u16 ((uint8_t *) path, 1 + strlen (path), NULL, &wpath_len)))
1120 LOG (GNUNET_ERROR_TYPE_DEBUG,
1121 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", path, errno);
1122 GNUNET_free (env_block);
1125 GNUNET_DISK_pipe_close (lsocks_pipe);
1126 if (GNUNET_YES == pipe_control)
1128 GNUNET_DISK_file_close (childpipe_write);
1129 GNUNET_DISK_file_close (childpipe_read);
1135 if (NULL == (wcmd = u8_to_u16 ((uint8_t *) cmd, 1 + strlen (cmd), NULL, &wcmd_len)))
1137 LOG (GNUNET_ERROR_TYPE_DEBUG,
1138 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n",
1141 GNUNET_free (env_block);
1145 GNUNET_DISK_pipe_close (lsocks_pipe);
1146 if (GNUNET_YES == pipe_control)
1148 GNUNET_DISK_file_close (childpipe_write);
1149 GNUNET_DISK_file_close (childpipe_read);
1154 create_no_window = 0;
1156 HANDLE console_input = CreateFile ("CONIN$", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1157 if (INVALID_HANDLE_VALUE == console_input)
1158 create_no_window = CREATE_NO_WINDOW;
1160 CloseHandle (console_input);
1163 bresult = CreateProcessW (wpath, wcmd, NULL, NULL, GNUNET_YES,
1164 create_no_window | CREATE_SUSPENDED, env_block, NULL, &start, &proc);
1165 error_code = GetLastError ();
1167 if ((NULL == pipe_stdin) && (stdih))
1168 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, stdif);
1171 if ((NULL == pipe_stdout) && (stdoh))
1172 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, stdof);
1175 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, stdef);
1178 LOG (GNUNET_ERROR_TYPE_ERROR,
1179 "CreateProcess(%s, %s) failed: %lu\n",
1184 GNUNET_free (env_block);
1188 if (GNUNET_YES == pipe_control)
1190 GNUNET_DISK_file_close (childpipe_read);
1195 if (GNUNET_YES == pipe_control)
1197 GNUNET_DISK_file_close (childpipe_write);
1200 GNUNET_DISK_pipe_close (lsocks_pipe);
1201 SetErrnoFromWinError (error_code);
1205 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
1206 gnunet_proc->pid = proc.dwProcessId;
1207 gnunet_proc->handle = proc.hProcess;
1208 gnunet_proc->control_pipe = childpipe_write;
1210 CreateThread (NULL, 64000, &child_wait_thread, (void *) gnunet_proc, 0, NULL);
1212 ResumeThread (proc.hThread);
1213 CloseHandle (proc.hThread);
1215 if ( (NULL == lsocks) || (INVALID_SOCKET == lsocks[0]) )
1218 GNUNET_DISK_pipe_close_end (lsocks_pipe, GNUNET_DISK_PIPE_END_READ);
1220 /* This is a replacement for "goto error" that doesn't use goto */
1229 /* Tell the number of sockets */
1230 for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++);
1232 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1233 if (sizeof (count) != wrote)
1235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1236 "Failed to write %u count bytes to the child: %lu\n",
1237 sizeof (count), GetLastError ());
1240 for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++)
1242 WSAPROTOCOL_INFOA pi;
1243 /* Get a socket duplication info */
1244 if (SOCKET_ERROR == WSADuplicateSocketA (lsocks[i], gnunet_proc->pid, &pi))
1246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1247 "Failed to duplicate an socket[%u]: %lu\n", i,
1251 /* Synchronous I/O is not nice, but we can't schedule this:
1252 * lsocks will be closed/freed by the caller soon, and until
1253 * the child creates a duplicate, closing a socket here will
1254 * close it for good.
1256 /* Send the size of the structure
1257 * (the child might be built with different headers...)
1260 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &size, sizeof (size));
1261 if (sizeof (size) != wrote)
1263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1264 "Failed to write %u size[%u] bytes to the child: %lu\n",
1265 sizeof (size), i, GetLastError ());
1268 /* Finally! Send the data */
1269 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &pi, sizeof (pi));
1270 if (sizeof (pi) != wrote)
1272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1273 "Failed to write %u socket[%u] bytes to the child: %lu\n",
1274 sizeof (pi), i, GetLastError ());
1278 /* This will block us until the child makes a final read or closes
1279 * the pipe (hence no 'wrote' check), since we have to wait for it
1280 * to duplicate the last socket, before we return and start closing
1283 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1288 GNUNET_DISK_file_sync (lsocks_write_fd);
1289 GNUNET_DISK_pipe_close (lsocks_pipe);
1293 /* If we can't pass on the socket(s), the child will block forever,
1294 * better put it out of its misery.
1296 SafeTerminateProcess (gnunet_proc->handle, 0, 0);
1297 CloseHandle (gnunet_proc->handle);
1298 if (NULL != gnunet_proc->control_pipe)
1299 GNUNET_DISK_file_close (gnunet_proc->control_pipe);
1300 GNUNET_free (gnunet_proc);
1311 * @param pipe_control should a pipe be used to send signals to the child?
1312 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1313 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1314 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1315 * @param pipe_stderr pipe to use to get output from child process (or NULL)
1316 * @param filename name of the binary
1317 * @param argv NULL-terminated array of arguments to the process
1318 * @return pointer to process structure of the new process, NULL on error
1320 struct GNUNET_OS_Process *
1321 GNUNET_OS_start_process_vap (int pipe_control,
1322 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1323 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1324 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1325 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1326 const char *filename,
1329 return start_process (pipe_control,
1343 * @param pipe_control should a pipe be used to send signals to the child?
1344 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1345 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1346 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1347 * @param pipe_stderr 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 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1358 const char *filename, va_list va)
1360 struct GNUNET_OS_Process *ret;
1367 while (NULL != va_arg (ap, char *))
1370 argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
1373 while (NULL != (argv[argc] = va_arg (ap, char *)))
1376 ret = GNUNET_OS_start_process_vap (pipe_control,
1391 * @param pipe_control should a pipe be used to send signals to the child?
1392 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1393 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1394 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1395 * @param filename name of the binary
1396 * @param ... NULL-terminated list of arguments to the process
1397 * @return pointer to process structure of the new process, NULL on error
1399 struct GNUNET_OS_Process *
1400 GNUNET_OS_start_process (int pipe_control,
1401 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1402 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1403 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1404 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1405 const char *filename, ...)
1407 struct GNUNET_OS_Process *ret;
1410 va_start (ap, filename);
1411 ret = GNUNET_OS_start_process_va (pipe_control,
1426 * @param pipe_control should a pipe be used to send signals to the child?
1427 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
1428 * std handles of the parent are inherited by the child.
1429 * pipe_stdin and pipe_stdout take priority over std_inheritance
1430 * (when they are non-NULL).
1431 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1432 * must be NULL on platforms where dup is not supported
1433 * @param filename name of the binary
1434 * @param argv NULL-terminated list of arguments to the process
1435 * @return process ID of the new process, -1 on error
1437 struct GNUNET_OS_Process *
1438 GNUNET_OS_start_process_v (int pipe_control,
1439 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1440 const SOCKTYPE *lsocks,
1441 const char *filename,
1444 return start_process (pipe_control,
1456 * Start a process. This function is similar to the GNUNET_OS_start_process_*
1457 * except that the filename and arguments can have whole strings which contain
1458 * the arguments. These arguments are to be separated by spaces and are parsed
1459 * in the order they appear. Arguments containing spaces can be used by
1460 * quoting them with @em ".
1462 * @param pipe_control should a pipe be used to send signals to the child?
1463 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1464 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1465 * must be NULL on platforms where dup is not supported
1466 * @param filename name of the binary. It is valid to have the arguments
1467 * in this string when they are separated by spaces.
1468 * @param ... more arguments. Should be of type `char *`. It is valid
1469 * to have the arguments in these strings when they are separated by
1470 * spaces. The last argument MUST be NULL.
1471 * @return pointer to process structure of the new process, NULL on error
1473 struct GNUNET_OS_Process *
1474 GNUNET_OS_start_process_s (int pipe_control,
1475 unsigned int std_inheritance,
1476 const SOCKTYPE * lsocks,
1477 const char *filename, ...)
1481 unsigned int argv_size;
1487 struct GNUNET_OS_Process *proc;
1494 va_start (ap, filename);
1501 while ('\0' != *rpos)
1510 if ( (' ' == *rpos) && (0 == quote_on) )
1516 while (' ' == *rpos)
1519 if ( (NULL == last) && ('\0' != *rpos) ) // FIXME: == or !=?
1527 while (NULL != (arg = (va_arg (ap, const char*))));
1530 argv = GNUNET_malloc (argv_size * sizeof (char *));
1532 va_start (ap, filename);
1537 cp = GNUNET_strdup (arg);
1540 while ('\0' != *pos)
1549 if ( (' ' == *pos) && (0 == quote_on) )
1553 argv[argv_size++] = GNUNET_strdup (last);
1559 if ( (NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
1565 argv[argv_size++] = GNUNET_strdup (last);
1569 while (NULL != (arg = (va_arg (ap, const char*))));
1571 argv[argv_size] = NULL;
1573 for(i = 0; i < argv_size; i++)
1575 len = strlen (argv[i]);
1576 if ( (argv[i][0] == '"') && (argv[i][len-1] == '"'))
1578 memmove (&argv[i][0], &argv[i][1], len - 2);
1579 argv[i][len-2] = '\0';
1582 binary_path = argv[0];
1583 proc = GNUNET_OS_start_process_v (pipe_control, std_inheritance, lsocks,
1585 while (argv_size > 0)
1586 GNUNET_free (argv[--argv_size]);
1593 * Retrieve the status of a process, waiting on it if dead.
1594 * Nonblocking version.
1596 * @param proc process ID
1597 * @param type status type
1598 * @param code return code/signal number
1599 * @param options WNOHANG if non-blocking is desired
1600 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1603 process_status (struct GNUNET_OS_Process *proc,
1604 enum GNUNET_OS_ProcessStatusType *type,
1605 unsigned long *code,
1612 GNUNET_assert (0 != proc);
1613 ret = waitpid (proc->pid, &status, options);
1616 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1618 return GNUNET_SYSERR;
1622 *type = GNUNET_OS_PROCESS_RUNNING;
1626 if (proc->pid != ret)
1628 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1629 return GNUNET_SYSERR;
1631 if (WIFEXITED (status))
1633 *type = GNUNET_OS_PROCESS_EXITED;
1634 *code = WEXITSTATUS (status);
1636 else if (WIFSIGNALED (status))
1638 *type = GNUNET_OS_PROCESS_SIGNALED;
1639 *code = WTERMSIG (status);
1641 else if (WIFSTOPPED (status))
1643 *type = GNUNET_OS_PROCESS_SIGNALED;
1644 *code = WSTOPSIG (status);
1647 else if (WIFCONTINUED (status))
1649 *type = GNUNET_OS_PROCESS_RUNNING;
1655 *type = GNUNET_OS_PROCESS_UNKNOWN;
1660 #define WNOHANG 42 /* just a flag for W32, purely internal at this point */
1664 DWORD c, error_code, ret;
1668 if (h == NULL || ret == 0)
1670 LOG (GNUNET_ERROR_TYPE_WARNING,
1671 "Invalid process information {%d, %08X}\n",
1673 return GNUNET_SYSERR;
1676 h = GetCurrentProcess ();
1678 if (WNOHANG != options)
1680 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1682 SetErrnoFromWinError (GetLastError ());
1683 return GNUNET_SYSERR;
1687 ret = GetExitCodeProcess (h, &c);
1688 error_code = GetLastError ();
1689 if (ret == 0 || error_code != NO_ERROR)
1691 SetErrnoFromWinError (error_code);
1692 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
1693 return GNUNET_SYSERR;
1695 if (STILL_ACTIVE == c)
1697 *type = GNUNET_OS_PROCESS_RUNNING;
1701 *type = GNUNET_OS_PROCESS_EXITED;
1710 * Retrieve the status of a process, waiting on it if dead.
1711 * Nonblocking version.
1713 * @param proc process ID
1714 * @param type status type
1715 * @param code return code/signal number
1716 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1719 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1720 enum GNUNET_OS_ProcessStatusType *type,
1721 unsigned long *code)
1723 return process_status (proc,
1731 * Retrieve the status of a process, waiting on it if dead.
1734 * @param proc pointer to process structure
1735 * @param type status type
1736 * @param code return code/signal number
1737 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1740 GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc,
1741 enum GNUNET_OS_ProcessStatusType *type,
1742 unsigned long *code)
1744 return process_status (proc,
1752 * Wait for a process to terminate. The return code is discarded.
1753 * You must not use #GNUNET_OS_process_status() on the same process
1754 * after calling this function! This function is blocking and should
1755 * thus only be used if the child process is known to have terminated
1756 * or to terminate very soon.
1758 * @param proc pointer to process structure
1759 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1762 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1765 pid_t pid = proc->pid;
1768 while ( (pid != (ret = waitpid (pid, NULL, 0))) &&
1769 (EINTR == errno) ) ;
1772 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1774 return GNUNET_SYSERR;
1783 LOG (GNUNET_ERROR_TYPE_WARNING,
1784 "Invalid process information {%d, %08X}\n",
1786 return GNUNET_SYSERR;
1789 h = GetCurrentProcess ();
1791 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1793 SetErrnoFromWinError (GetLastError ());
1794 return GNUNET_SYSERR;
1802 * Handle to a command.
1804 struct GNUNET_OS_CommandHandle
1810 struct GNUNET_OS_Process *eip;
1813 * Handle to the output pipe.
1815 struct GNUNET_DISK_PipeHandle *opipe;
1818 * Read-end of output pipe.
1820 const struct GNUNET_DISK_FileHandle *r;
1823 * Function to call on each line of output.
1825 GNUNET_OS_LineProcessor proc;
1828 * Closure for @e proc.
1833 * Buffer for the output.
1838 * Task reading from pipe.
1840 struct GNUNET_SCHEDULER_Task *rtask;
1845 struct GNUNET_TIME_Absolute timeout;
1848 * Current read offset in buf.
1855 * Stop/kill a command. Must ONLY be called either from
1856 * the callback after 'NULL' was passed for 'line' *OR*
1857 * from an independent task (not within the line processor).
1859 * @param cmd handle to the process
1862 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
1864 if (NULL != cmd->proc)
1866 GNUNET_assert (NULL != cmd->rtask);
1867 GNUNET_SCHEDULER_cancel (cmd->rtask);
1869 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1870 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
1871 GNUNET_OS_process_destroy (cmd->eip);
1872 GNUNET_DISK_pipe_close (cmd->opipe);
1878 * Read from the process and call the line processor.
1880 * @param cls the `struct GNUNET_OS_CommandHandle *`
1883 cmd_read (void *cls)
1885 struct GNUNET_OS_CommandHandle *cmd = cls;
1886 const struct GNUNET_SCHEDULER_TaskContext *tc;
1887 GNUNET_OS_LineProcessor proc;
1892 tc = GNUNET_SCHEDULER_get_task_context ();
1894 GNUNET_NETWORK_fdset_handle_isset (tc->read_ready,
1900 proc (cmd->proc_cls, NULL);
1903 ret = GNUNET_DISK_file_read (cmd->r,
1904 &cmd->buf[cmd->off],
1905 sizeof (cmd->buf) - cmd->off);
1908 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
1910 cmd->buf[cmd->off] = '\0';
1911 cmd->proc (cmd->proc_cls, cmd->buf);
1915 proc (cmd->proc_cls, NULL);
1918 end = memchr (&cmd->buf[cmd->off], '\n', ret);
1923 cmd->proc (cmd->proc_cls, cmd->buf);
1924 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1925 cmd->off -= (end + 1 - cmd->buf);
1926 end = memchr (cmd->buf, '\n', cmd->off);
1929 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
1937 * Run the given command line and call the given function
1938 * for each line of the output.
1940 * @param proc function to call for each line of the output
1941 * @param proc_cls closure for @a proc
1942 * @param timeout when to time out
1943 * @param binary command to run
1944 * @param ... arguments to command
1945 * @return NULL on error
1947 struct GNUNET_OS_CommandHandle *
1948 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
1950 struct GNUNET_TIME_Relative timeout,
1954 struct GNUNET_OS_CommandHandle *cmd;
1955 struct GNUNET_OS_Process *eip;
1956 struct GNUNET_DISK_PipeHandle *opipe;
1959 opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES,
1960 GNUNET_NO, GNUNET_YES);
1963 va_start (ap, binary);
1964 /* redirect stdout, don't inherit stderr/stdin */
1965 eip = GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL,
1966 opipe, NULL, binary,
1971 GNUNET_DISK_pipe_close (opipe);
1974 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
1975 cmd = GNUNET_new (struct GNUNET_OS_CommandHandle);
1976 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1980 cmd->proc_cls = proc_cls;
1981 cmd->r = GNUNET_DISK_pipe_handle (opipe,
1982 GNUNET_DISK_PIPE_END_READ);
1983 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout,
1991 /* end of os_priority.c */