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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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;
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;
159 /* already done, we've been called twice... */
163 env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
164 if ( (NULL == env_buf) || (strlen (env_buf) <= 0) )
166 LOG (GNUNET_ERROR_TYPE_DEBUG,
167 "Not installing a handler because $%s is empty\n",
168 GNUNET_OS_CONTROL_PIPE);
169 putenv (GNUNET_OS_CONTROL_PIPE "=");
173 pipe_fd = strtoull (env_buf, &env_buf_end, 16);
174 if ((0 != errno) || (env_buf == env_buf_end))
176 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
179 putenv (GNUNET_OS_CONTROL_PIPE "=");
182 #if !defined (WINDOWS)
183 if (pipe_fd >= FD_SETSIZE)
185 if ((FILE_TYPE_UNKNOWN == GetFileType ((HANDLE) (uintptr_t) pipe_fd))
186 && (0 != GetLastError ()))
189 LOG (GNUNET_ERROR_TYPE_ERROR,
190 "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n",
192 putenv (GNUNET_OS_CONTROL_PIPE "=");
196 control_pipe = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) (uintptr_t) pipe_fd);
198 control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
200 if (NULL == control_pipe)
202 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
205 putenv (GNUNET_OS_CONTROL_PIPE "=");
208 LOG (GNUNET_ERROR_TYPE_DEBUG,
209 "Adding parent control handler pipe `%s' to the scheduler\n",
211 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
213 &parent_control_handler,
215 spch = GNUNET_SCHEDULER_add_shutdown (&shutdown_pch,
217 putenv (GNUNET_OS_CONTROL_PIPE "=");
222 * Get process structure for current process
224 * The pointer it returns points to static memory location and must
225 * not be deallocated/closed.
227 * @return pointer to the process sturcutre for this process
229 struct GNUNET_OS_Process *
230 GNUNET_OS_process_current ()
233 current_process.pid = GetCurrentProcessId ();
234 current_process.handle = GetCurrentProcess ();
236 current_process.pid = 0;
238 return ¤t_process;
243 * Sends a signal to the process
245 * @param proc pointer to process structure
247 * @return 0 on success, -1 on error
250 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc,
257 if (NULL != proc->control_pipe)
259 LOG (GNUNET_ERROR_TYPE_DEBUG,
260 "Sending signal %d to pid: %u via pipe\n",
263 ret = GNUNET_DISK_file_write (proc->control_pipe,
266 if (sizeof (csig) == ret)
269 /* pipe failed or non-existent, try other methods */
272 #if !defined (WINDOWS)
278 #if (SIGTERM != GNUNET_TERM_SIG)
279 case GNUNET_TERM_SIG:
281 #if defined(WINDOWS) && !defined(__CYGWIN__)
284 int must_kill = GNUNET_YES;
285 if (0 != GetExitCodeProcess (proc->handle, &exitcode))
286 must_kill = (exitcode == STILL_ACTIVE) ? GNUNET_YES : GNUNET_NO;
287 if (GNUNET_YES == must_kill)
289 if (0 == SafeTerminateProcess (proc->handle, 0, 0))
291 DWORD error_code = GetLastError ();
292 if ( (error_code != WAIT_TIMEOUT) &&
293 (error_code != ERROR_PROCESS_ABORTED) )
295 LOG ((error_code == ERROR_ACCESS_DENIED) ?
296 GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
297 "SafeTermiateProcess failed with code %lu\n",
299 /* The problem here is that a process that is already dying
300 * might cause SafeTerminateProcess to fail with
301 * ERROR_ACCESS_DENIED, but the process WILL die eventually.
302 * If we really had a permissions problem, hanging up (which
303 * is what will happen in process_wait() in that case) is
306 if (ERROR_ACCESS_DENIED == error_code)
312 SetErrnoFromWinError (error_code);
321 LOG (GNUNET_ERROR_TYPE_DEBUG,
322 "Sending signal %d to pid: %u via system call\n",
325 return PLIBC_KILL (proc->pid, sig);
328 #if defined (WINDOWS)
332 LOG (GNUNET_ERROR_TYPE_DEBUG,
333 "Sending signal %d to pid: %u via system call\n",
336 return PLIBC_KILL (proc->pid, sig);
343 * Get the pid of the process in question
345 * @param proc the process to get the pid of
347 * @return the current process id
350 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process * proc)
357 * Cleans up process structure contents (OS-dependent) and deallocates
360 * @param proc pointer to process structure
363 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
365 if (NULL != proc->control_pipe)
366 GNUNET_DISK_file_close (proc->control_pipe);
367 #if defined (WINDOWS)
368 if (NULL != proc->handle)
369 CloseHandle (proc->handle);
376 #include "gnunet_signal_lib.h"
378 extern GNUNET_SIGNAL_Handler w32_sigchld_handler;
381 * Make seaspider happy.
383 #define DWORD_WINAPI DWORD WINAPI
387 * @brief Waits for a process to terminate and invokes the SIGCHLD handler
388 * @param proc pointer to process structure
391 child_wait_thread (void *arg)
393 struct GNUNET_OS_Process *proc = (struct GNUNET_OS_Process *) arg;
395 WaitForSingleObject (proc->handle, INFINITE);
397 if (w32_sigchld_handler)
398 w32_sigchld_handler ();
407 CreateCustomEnvTable (char **vars)
409 char *win32_env_table;
414 size_t tablesize = 0;
415 size_t items_count = 0;
424 win32_env_table = GetEnvironmentStringsA ();
425 if (NULL == win32_env_table)
427 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ;
429 index = GNUNET_malloc (sizeof (char *) * n_var);
430 for (c = 0; c < n_var; c++)
432 for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++)
434 size_t len = strlen (ptr);
437 for (var_ptr = vars; *var_ptr; var_ptr++)
441 var_len = strlen (var);
442 if (strncmp (var, ptr, var_len) == 0)
446 tablesize += var_len + strlen (val) + 1;
451 tablesize += len + 1;
454 for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
459 n_found += strlen (var) + strlen (val) + 1;
461 result = GNUNET_malloc (tablesize + n_found + 1);
462 for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
464 size_t len = strlen (ptr);
467 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
471 var_len = strlen (var);
472 if (strncmp (var, ptr, var_len) == 0)
480 strcpy (result_ptr, ptr);
481 result_ptr += len + 1;
485 strcpy (result_ptr, var);
486 result_ptr += var_len;
487 strcpy (result_ptr, val);
488 result_ptr += strlen (val) + 1;
492 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
496 var_len = strlen (var);
499 strcpy (result_ptr, var);
500 result_ptr += var_len;
501 strcpy (result_ptr, val);
502 result_ptr += strlen (val) + 1;
505 FreeEnvironmentStrings (win32_env_table);
514 * Open '/dev/null' and make the result the given
517 * @param target_fd desired FD to point to /dev/null
518 * @param flags open flags (O_RDONLY, O_WRONLY)
521 open_dev_null (int target_fd,
526 fd = open ("/dev/null", flags);
529 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
536 if (-1 == dup2 (fd, target_fd))
538 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
542 GNUNET_break (0 == close (fd));
550 * @param pipe_control should a pipe be used to send signals to the child?
551 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
552 * std handles of the parent are inherited by the child.
553 * pipe_stdin and pipe_stdout take priority over std_inheritance
554 * (when they are non-NULL).
555 * @param pipe_stdin pipe to use to send input to child process (or NULL)
556 * @param pipe_stdout pipe to use to get output from child process (or NULL)
557 * @param pipe_stderr pipe to use for stderr for child process (or NULL)
558 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
559 * must be NULL on platforms where dup is not supported
560 * @param filename name of the binary
561 * @param argv NULL-terminated list of arguments to the process
562 * @return process ID of the new process, -1 on error
564 static struct GNUNET_OS_Process *
565 start_process (int pipe_control,
566 enum GNUNET_OS_InheritStdioFlags std_inheritance,
567 struct GNUNET_DISK_PipeHandle *pipe_stdin,
568 struct GNUNET_DISK_PipeHandle *pipe_stdout,
569 struct GNUNET_DISK_PipeHandle *pipe_stderr,
570 const SOCKTYPE *lsocks,
571 const char *filename,
577 struct GNUNET_OS_Process *gnunet_proc;
578 struct GNUNET_DISK_FileHandle *childpipe_read;
579 struct GNUNET_DISK_FileHandle *childpipe_write;
580 int childpipe_read_fd;
596 GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
597 return NULL; /* not executable */
598 if (GNUNET_YES == pipe_control)
600 struct GNUNET_DISK_PipeHandle *childpipe;
601 int dup_childpipe_read_fd = -1;
603 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
604 GNUNET_YES, GNUNET_NO);
605 if (NULL == childpipe)
607 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe,
608 GNUNET_DISK_PIPE_END_READ);
609 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe,
610 GNUNET_DISK_PIPE_END_WRITE);
611 GNUNET_DISK_pipe_close (childpipe);
612 if ( (NULL == childpipe_read) ||
613 (NULL == childpipe_write) ||
615 GNUNET_DISK_internal_file_handle_ (childpipe_read,
618 (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
620 if (NULL != childpipe_read)
621 GNUNET_DISK_file_close (childpipe_read);
622 if (NULL != childpipe_write)
623 GNUNET_DISK_file_close (childpipe_write);
624 if (0 <= dup_childpipe_read_fd)
625 close (dup_childpipe_read_fd);
628 childpipe_read_fd = dup_childpipe_read_fd;
629 GNUNET_DISK_file_close (childpipe_read);
633 childpipe_write = NULL;
634 childpipe_read_fd = -1;
636 if (NULL != pipe_stdin)
638 GNUNET_assert (GNUNET_OK ==
639 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
640 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
641 &fd_stdin_read, sizeof (int)));
642 GNUNET_assert (GNUNET_OK ==
643 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
644 (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
645 &fd_stdin_write, sizeof (int)));
647 if (NULL != pipe_stdout)
649 GNUNET_assert (GNUNET_OK ==
650 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
652 GNUNET_DISK_PIPE_END_WRITE),
653 &fd_stdout_write, sizeof (int)));
654 GNUNET_assert (GNUNET_OK ==
655 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
656 (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
657 &fd_stdout_read, sizeof (int)));
659 if (NULL != pipe_stderr)
661 GNUNET_assert (GNUNET_OK ==
662 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
664 GNUNET_DISK_PIPE_END_READ),
665 &fd_stderr_read, sizeof (int)));
666 GNUNET_assert (GNUNET_OK ==
667 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
669 GNUNET_DISK_PIPE_END_WRITE),
670 &fd_stderr_write, sizeof (int)));
677 while (-1 != (k = lsocks[i++]))
678 GNUNET_array_append (lscp, ls, k);
679 GNUNET_array_append (lscp, ls, -1);
682 /* see https://gnunet.org/vfork */
690 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
691 GNUNET_array_grow (lscp, ls, 0);
692 if (NULL != childpipe_write)
693 GNUNET_DISK_file_close (childpipe_write);
694 if (0 <= childpipe_read_fd)
695 close (childpipe_read_fd);
701 unsetenv (GNUNET_OS_CONTROL_PIPE);
702 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
703 gnunet_proc->pid = ret;
704 gnunet_proc->control_pipe = childpipe_write;
705 if (GNUNET_YES == pipe_control)
707 close (childpipe_read_fd);
709 GNUNET_array_grow (lscp, ls, 0);
712 if (0 <= childpipe_read_fd)
716 /* due to vfork, we must NOT free memory on DARWIN! */
717 GNUNET_DISK_file_close (childpipe_write);
719 snprintf (fdbuf, 100, "%x", childpipe_read_fd);
720 setenv (GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
723 unsetenv (GNUNET_OS_CONTROL_PIPE);
724 if (NULL != pipe_stdin)
726 GNUNET_break (0 == close (fd_stdin_write));
727 if (-1 == dup2 (fd_stdin_read, 0))
728 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
729 GNUNET_break (0 == close (fd_stdin_read));
731 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
733 GNUNET_break (0 == close (0));
734 open_dev_null (0, O_RDONLY);
736 if (NULL != pipe_stdout)
738 GNUNET_break (0 == close (fd_stdout_read));
739 if (-1 == dup2 (fd_stdout_write, 1))
740 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
741 GNUNET_break (0 == close (fd_stdout_write));
743 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
745 GNUNET_break (0 == close (1));
746 open_dev_null (1, O_WRONLY);
748 if (NULL != pipe_stderr)
750 GNUNET_break (0 == close (fd_stderr_read));
751 if (-1 == dup2 (fd_stderr_write, 2))
752 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
753 GNUNET_break (0 == close (fd_stderr_write));
755 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
757 GNUNET_break (0 == close (2));
758 open_dev_null (2, O_WRONLY);
762 /* read systemd documentation... */
765 while (-1 != lscp[i])
768 while (-1 != lscp[j])
774 GNUNET_assert (-1 != k);
775 GNUNET_assert (0 == close (lscp[j]));
783 /* Bury any existing FD, no matter what; they should all be closed
784 * on exec anyway and the important onces have been dup'ed away */
786 GNUNET_assert (-1 != dup2 (lscp[i], tgt));
788 /* unset close-on-exec flag */
789 flags = fcntl (tgt, F_GETFD);
790 GNUNET_assert (flags >= 0);
791 flags &= ~FD_CLOEXEC;
793 (void) fcntl (tgt, F_SETFD, flags);
797 GNUNET_snprintf (fds, sizeof (fds), "%u", i);
798 setenv ("LISTEN_FDS", fds, 1);
801 /* due to vfork, we must NOT free memory on DARWIN! */
802 GNUNET_array_grow (lscp, ls, 0);
804 execvp (filename, argv);
805 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
808 struct GNUNET_DISK_FileHandle *childpipe_read;
809 struct GNUNET_DISK_FileHandle *childpipe_write;
810 HANDLE childpipe_read_handle;
812 char **non_const_argv;
817 PROCESS_INFORMATION proc;
819 struct GNUNET_OS_Process *gnunet_proc;
820 char path[MAX_PATH + 1];
821 char *our_env[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
822 char *env_block = NULL;
830 char *non_const_filename;
831 char win_path[MAX_PATH + 1];
832 struct GNUNET_DISK_PipeHandle *lsocks_pipe;
833 const struct GNUNET_DISK_FileHandle *lsocks_write_fd;
844 HANDLE stdout_handle;
845 HANDLE stdih, stdoh, stdeh;
846 DWORD stdif, stdof, stdef;
849 DWORD create_no_window;
851 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
852 return NULL; /* not executable */
854 /* Search in prefix dir (hopefully - the directory from which
855 * the current module was loaded), bindir and libdir, then in PATH
857 self_prefix = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_SELF_PREFIX);
858 bindir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
859 libdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
861 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
864 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
867 pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
870 ptr += sprintf (pathbuf, "%s;%s;%s;", self_prefix, bindir, libdir);
871 GNUNET_free (self_prefix);
872 GNUNET_free (bindir);
873 GNUNET_free (libdir);
875 alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len);
876 if (alloc_len != pathbuf_len - 1)
878 GNUNET_free (pathbuf);
879 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */
883 cmdlen = strlen (filename);
884 if ( (cmdlen < 5) || (0 != strcmp (&filename[cmdlen - 4], ".exe")) )
885 GNUNET_asprintf (&non_const_filename, "%s.exe", filename);
887 GNUNET_asprintf (&non_const_filename, "%s", filename);
889 /* It could be in POSIX form, convert it to a DOS path early on */
890 if (ERROR_SUCCESS != (lRet = plibc_conv_to_win_path (non_const_filename, win_path)))
892 SetErrnoFromWinError (lRet);
893 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "plibc_conv_to_win_path",
895 GNUNET_free (non_const_filename);
896 GNUNET_free (pathbuf);
899 GNUNET_free (non_const_filename);
900 non_const_filename = GNUNET_strdup (win_path);
901 /* Check that this is the full path. If it isn't, search. */
902 /* FIXME: convert it to wchar_t and use SearchPathW?
903 * Remember: arguments to _start_process() are technically in UTF-8...
905 if (non_const_filename[1] == ':')
907 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
908 LOG (GNUNET_ERROR_TYPE_DEBUG,
909 "Using path `%s' as-is. PATH is %s\n", path, ptr);
911 else if (!SearchPathA
912 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char),
915 SetErrnoFromWinError (GetLastError ());
916 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
918 GNUNET_free (non_const_filename);
919 GNUNET_free (pathbuf);
923 LOG (GNUNET_ERROR_TYPE_DEBUG,
924 "Found `%s' in PATH `%s'\n", path, pathbuf);
925 GNUNET_free (pathbuf);
926 GNUNET_free (non_const_filename);
928 /* Count the number of arguments */
929 arg = (char **) argv;
936 /* Allocate a copy argv */
937 non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1));
939 /* Copy all argv strings */
941 arg = (char **) argv;
945 non_const_argv[argcount] = GNUNET_strdup (path);
947 non_const_argv[argcount] = GNUNET_strdup (*arg);
951 non_const_argv[argcount] = NULL;
955 arg = non_const_argv;
958 cmdlen = cmdlen + strlen (*arg) + 4;
962 /* Allocate and create cmd */
963 cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen);
964 arg = non_const_argv;
967 char arg_last_char = (*arg)[strlen (*arg) - 1];
968 idx += sprintf (idx, "\"%s%s\"%s", *arg,
969 arg_last_char == '\\' ? "\\" : "", *(arg + 1) ? " " : "");
974 GNUNET_free (non_const_argv[--argcount]);
975 GNUNET_free (non_const_argv);
977 memset (&start, 0, sizeof (start));
978 start.cb = sizeof (start);
979 if ((pipe_stdin != NULL) || (pipe_stdout != NULL) || (std_inheritance != 0))
980 start.dwFlags |= STARTF_USESTDHANDLES;
982 stdih = GetStdHandle (STD_INPUT_HANDLE);
983 GetHandleInformation (stdih, &stdif);
984 if (pipe_stdin != NULL)
986 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
987 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
988 &stdin_handle, sizeof (HANDLE));
989 start.hStdInput = stdin_handle;
993 if (std_inheritance & GNUNET_OS_INHERIT_STD_IN)
995 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 1);
996 if (pipe_stdin == NULL)
997 start.hStdInput = stdih;
1000 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 0);
1004 stdoh = GetStdHandle (STD_OUTPUT_HANDLE);
1005 GetHandleInformation (stdoh, &stdof);
1006 if (NULL != pipe_stdout)
1008 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1010 GNUNET_DISK_PIPE_END_WRITE),
1011 &stdout_handle, sizeof (HANDLE));
1012 start.hStdOutput = stdout_handle;
1016 if (std_inheritance & GNUNET_OS_INHERIT_STD_OUT)
1018 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 1);
1019 if (pipe_stdout == NULL)
1020 start.hStdOutput = stdoh;
1023 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 0);
1026 stdeh = GetStdHandle (STD_ERROR_HANDLE);
1027 GetHandleInformation (stdeh, &stdef);
1030 if (std_inheritance & GNUNET_OS_INHERIT_STD_ERR)
1032 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 1);
1033 start.hStdError = stdeh;
1036 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 0);
1039 if (GNUNET_YES == pipe_control)
1041 struct GNUNET_DISK_PipeHandle *childpipe;
1042 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
1043 if (NULL == childpipe)
1045 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
1046 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
1047 GNUNET_DISK_pipe_close (childpipe);
1048 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
1049 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
1050 &childpipe_read_handle, sizeof (HANDLE))))
1053 GNUNET_DISK_file_close (childpipe_read);
1054 if (childpipe_write)
1055 GNUNET_DISK_file_close (childpipe_write);
1059 /* Unlike *nix variant, we don't dup the handle, so can't close
1060 * filehandle right now.
1062 SetHandleInformation (childpipe_read_handle, HANDLE_FLAG_INHERIT, 1);
1066 childpipe_read = NULL;
1067 childpipe_write = NULL;
1070 if (lsocks != NULL && lsocks[0] != INVALID_SOCKET)
1072 lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1074 if (lsocks_pipe == NULL)
1077 GNUNET_DISK_pipe_close (lsocks_pipe);
1078 if (GNUNET_YES == pipe_control)
1080 GNUNET_DISK_file_close (childpipe_write);
1081 GNUNET_DISK_file_close (childpipe_read);
1085 lsocks_write_fd = GNUNET_DISK_pipe_handle (lsocks_pipe,
1086 GNUNET_DISK_PIPE_END_WRITE);
1087 GNUNET_DISK_internal_file_handle_ (lsocks_write_fd,
1088 &lsocks_write, sizeof (HANDLE));
1089 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1090 (lsocks_pipe, GNUNET_DISK_PIPE_END_READ),
1091 &lsocks_read, sizeof (HANDLE));
1097 if (GNUNET_YES == pipe_control)
1099 GNUNET_asprintf (&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE);
1100 GNUNET_asprintf (&our_env[env_off++], "%p", childpipe_read_handle);
1102 if ( (lsocks != NULL) && (lsocks[0] != INVALID_SOCKET))
1104 /*This will tell the child that we're going to send lsocks over the pipe*/
1105 GNUNET_asprintf (&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS");
1106 GNUNET_asprintf (&our_env[env_off++], "%lu", lsocks_read);
1108 our_env[env_off++] = NULL;
1109 env_block = CreateCustomEnvTable (our_env);
1111 GNUNET_free_non_null (our_env[--env_off]);
1114 if (NULL == (wpath = u8_to_u16 ((uint8_t *) path, 1 + strlen (path), NULL, &wpath_len)))
1116 LOG (GNUNET_ERROR_TYPE_DEBUG,
1117 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", path, errno);
1118 GNUNET_free (env_block);
1121 GNUNET_DISK_pipe_close (lsocks_pipe);
1122 if (GNUNET_YES == pipe_control)
1124 GNUNET_DISK_file_close (childpipe_write);
1125 GNUNET_DISK_file_close (childpipe_read);
1131 if (NULL == (wcmd = u8_to_u16 ((uint8_t *) cmd, 1 + strlen (cmd), NULL, &wcmd_len)))
1133 LOG (GNUNET_ERROR_TYPE_DEBUG,
1134 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n",
1137 GNUNET_free (env_block);
1141 GNUNET_DISK_pipe_close (lsocks_pipe);
1142 if (GNUNET_YES == pipe_control)
1144 GNUNET_DISK_file_close (childpipe_write);
1145 GNUNET_DISK_file_close (childpipe_read);
1150 create_no_window = 0;
1152 HANDLE console_input = CreateFile ("CONIN$", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1153 if (INVALID_HANDLE_VALUE == console_input)
1154 create_no_window = CREATE_NO_WINDOW;
1156 CloseHandle (console_input);
1159 bresult = CreateProcessW (wpath, wcmd, NULL, NULL, GNUNET_YES,
1160 create_no_window | CREATE_SUSPENDED, env_block, NULL, &start, &proc);
1161 error_code = GetLastError ();
1163 if ((NULL == pipe_stdin) && (stdih))
1164 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, stdif);
1167 if ((NULL == pipe_stdout) && (stdoh))
1168 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, stdof);
1171 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, stdef);
1174 LOG (GNUNET_ERROR_TYPE_ERROR,
1175 "CreateProcess(%s, %s) failed: %lu\n",
1180 GNUNET_free (env_block);
1184 if (GNUNET_YES == pipe_control)
1186 GNUNET_DISK_file_close (childpipe_read);
1191 if (GNUNET_YES == pipe_control)
1193 GNUNET_DISK_file_close (childpipe_write);
1196 GNUNET_DISK_pipe_close (lsocks_pipe);
1197 SetErrnoFromWinError (error_code);
1201 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
1202 gnunet_proc->pid = proc.dwProcessId;
1203 gnunet_proc->handle = proc.hProcess;
1204 gnunet_proc->control_pipe = childpipe_write;
1206 CreateThread (NULL, 64000, &child_wait_thread, (void *) gnunet_proc, 0, NULL);
1208 ResumeThread (proc.hThread);
1209 CloseHandle (proc.hThread);
1211 if ( (NULL == lsocks) || (INVALID_SOCKET == lsocks[0]) )
1214 GNUNET_DISK_pipe_close_end (lsocks_pipe, GNUNET_DISK_PIPE_END_READ);
1216 /* This is a replacement for "goto error" that doesn't use goto */
1225 /* Tell the number of sockets */
1226 for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++);
1228 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1229 if (sizeof (count) != wrote)
1231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1232 "Failed to write %u count bytes to the child: %u\n",
1233 sizeof (count), GetLastError ());
1236 for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++)
1238 WSAPROTOCOL_INFOA pi;
1239 /* Get a socket duplication info */
1240 if (SOCKET_ERROR == WSADuplicateSocketA (lsocks[i], gnunet_proc->pid, &pi))
1242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1243 "Failed to duplicate an socket[%llu]: %u\n", i,
1247 /* Synchronous I/O is not nice, but we can't schedule this:
1248 * lsocks will be closed/freed by the caller soon, and until
1249 * the child creates a duplicate, closing a socket here will
1250 * close it for good.
1252 /* Send the size of the structure
1253 * (the child might be built with different headers...)
1256 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &size, sizeof (size));
1257 if (sizeof (size) != wrote)
1259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1260 "Failed to write %u size[%llu] bytes to the child: %u\n",
1261 sizeof (size), i, GetLastError ());
1264 /* Finally! Send the data */
1265 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &pi, sizeof (pi));
1266 if (sizeof (pi) != wrote)
1268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1269 "Failed to write %u socket[%llu] bytes to the child: %u\n",
1270 sizeof (pi), i, GetLastError ());
1274 /* This will block us until the child makes a final read or closes
1275 * the pipe (hence no 'wrote' check), since we have to wait for it
1276 * to duplicate the last socket, before we return and start closing
1279 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1284 GNUNET_DISK_file_sync (lsocks_write_fd);
1285 GNUNET_DISK_pipe_close (lsocks_pipe);
1289 /* If we can't pass on the socket(s), the child will block forever,
1290 * better put it out of its misery.
1292 SafeTerminateProcess (gnunet_proc->handle, 0, 0);
1293 CloseHandle (gnunet_proc->handle);
1294 if (NULL != gnunet_proc->control_pipe)
1295 GNUNET_DISK_file_close (gnunet_proc->control_pipe);
1296 GNUNET_free (gnunet_proc);
1307 * @param pipe_control should a pipe be used to send signals to the child?
1308 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1309 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1310 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1311 * @param pipe_stderr pipe to use to get output from child process (or NULL)
1312 * @param filename name of the binary
1313 * @param argv NULL-terminated array of arguments to the process
1314 * @return pointer to process structure of the new process, NULL on error
1316 struct GNUNET_OS_Process *
1317 GNUNET_OS_start_process_vap (int pipe_control,
1318 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1319 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1320 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1321 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1322 const char *filename,
1325 return start_process (pipe_control,
1339 * @param pipe_control should a pipe be used to send signals to the child?
1340 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1341 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1342 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1343 * @param pipe_stderr pipe to use to get output from child process (or NULL)
1344 * @param filename name of the binary
1345 * @param va NULL-terminated list of arguments to the process
1346 * @return pointer to process structure of the new process, NULL on error
1348 struct GNUNET_OS_Process *
1349 GNUNET_OS_start_process_va (int pipe_control,
1350 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1351 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1352 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1353 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1354 const char *filename, va_list va)
1356 struct GNUNET_OS_Process *ret;
1363 while (NULL != va_arg (ap, char *))
1366 argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
1369 while (NULL != (argv[argc] = va_arg (ap, char *)))
1372 ret = GNUNET_OS_start_process_vap (pipe_control,
1387 * @param pipe_control should a pipe be used to send signals to the child?
1388 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1389 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1390 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1391 * @param filename name of the binary
1392 * @param ... NULL-terminated list of arguments to the process
1393 * @return pointer to process structure of the new process, NULL on error
1395 struct GNUNET_OS_Process *
1396 GNUNET_OS_start_process (int pipe_control,
1397 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1398 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1399 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1400 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1401 const char *filename, ...)
1403 struct GNUNET_OS_Process *ret;
1406 va_start (ap, filename);
1407 ret = GNUNET_OS_start_process_va (pipe_control,
1422 * @param pipe_control should a pipe be used to send signals to the child?
1423 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
1424 * std handles of the parent are inherited by the child.
1425 * pipe_stdin and pipe_stdout take priority over std_inheritance
1426 * (when they are non-NULL).
1427 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1428 * must be NULL on platforms where dup is not supported
1429 * @param filename name of the binary
1430 * @param argv NULL-terminated list of arguments to the process
1431 * @return process ID of the new process, -1 on error
1433 struct GNUNET_OS_Process *
1434 GNUNET_OS_start_process_v (int pipe_control,
1435 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1436 const SOCKTYPE *lsocks,
1437 const char *filename,
1440 return start_process (pipe_control,
1452 * Start a process. This function is similar to the GNUNET_OS_start_process_*
1453 * except that the filename and arguments can have whole strings which contain
1454 * the arguments. These arguments are to be separated by spaces and are parsed
1455 * in the order they appear. Arguments containing spaces can be used by
1456 * quoting them with @em ".
1458 * @param pipe_control should a pipe be used to send signals to the child?
1459 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1460 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1461 * must be NULL on platforms where dup is not supported
1462 * @param filename name of the binary. It is valid to have the arguments
1463 * in this string when they are separated by spaces.
1464 * @param ... more arguments. Should be of type `char *`. It is valid
1465 * to have the arguments in these strings when they are separated by
1466 * spaces. The last argument MUST be NULL.
1467 * @return pointer to process structure of the new process, NULL on error
1469 struct GNUNET_OS_Process *
1470 GNUNET_OS_start_process_s (int pipe_control,
1471 unsigned int std_inheritance,
1472 const SOCKTYPE * lsocks,
1473 const char *filename, ...)
1477 unsigned int argv_size;
1483 struct GNUNET_OS_Process *proc;
1490 va_start (ap, filename);
1497 while ('\0' != *rpos)
1506 if ( (' ' == *rpos) && (0 == quote_on) )
1512 while (' ' == *rpos)
1515 if ( (NULL == last) && ('\0' != *rpos) ) // FIXME: == or !=?
1523 while (NULL != (arg = (va_arg (ap, const char*))));
1526 argv = GNUNET_malloc (argv_size * sizeof (char *));
1528 va_start (ap, filename);
1533 cp = GNUNET_strdup (arg);
1536 while ('\0' != *pos)
1545 if ( (' ' == *pos) && (0 == quote_on) )
1549 argv[argv_size++] = GNUNET_strdup (last);
1555 if ( (NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
1561 argv[argv_size++] = GNUNET_strdup (last);
1565 while (NULL != (arg = (va_arg (ap, const char*))));
1567 argv[argv_size] = NULL;
1569 for(i = 0; i < argv_size; i++)
1571 len = strlen (argv[i]);
1572 if ( (argv[i][0] == '"') && (argv[i][len-1] == '"'))
1574 memmove (&argv[i][0], &argv[i][1], len - 2);
1575 argv[i][len-2] = '\0';
1578 binary_path = argv[0];
1579 proc = GNUNET_OS_start_process_v (pipe_control, std_inheritance, lsocks,
1581 while (argv_size > 0)
1582 GNUNET_free (argv[--argv_size]);
1589 * Retrieve the status of a process, waiting on him if dead.
1590 * Nonblocking version.
1592 * @param proc process ID
1593 * @param type status type
1594 * @param code return code/signal number
1595 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1598 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1599 enum GNUNET_OS_ProcessStatusType *type,
1600 unsigned long *code)
1606 GNUNET_assert (0 != proc);
1607 ret = waitpid (proc->pid, &status, WNOHANG);
1610 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1612 return GNUNET_SYSERR;
1616 *type = GNUNET_OS_PROCESS_RUNNING;
1620 if (proc->pid != ret)
1622 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1623 return GNUNET_SYSERR;
1625 if (WIFEXITED (status))
1627 *type = GNUNET_OS_PROCESS_EXITED;
1628 *code = WEXITSTATUS (status);
1630 else if (WIFSIGNALED (status))
1632 *type = GNUNET_OS_PROCESS_SIGNALED;
1633 *code = WTERMSIG (status);
1635 else if (WIFSTOPPED (status))
1637 *type = GNUNET_OS_PROCESS_SIGNALED;
1638 *code = WSTOPSIG (status);
1641 else if (WIFCONTINUED (status))
1643 *type = GNUNET_OS_PROCESS_RUNNING;
1649 *type = GNUNET_OS_PROCESS_UNKNOWN;
1654 DWORD c, error_code, ret;
1658 if (h == NULL || ret == 0)
1660 LOG (GNUNET_ERROR_TYPE_WARNING,
1661 "Invalid process information {%d, %08X}\n",
1663 return GNUNET_SYSERR;
1666 h = GetCurrentProcess ();
1669 ret = GetExitCodeProcess (h, &c);
1670 error_code = GetLastError ();
1671 if (ret == 0 || error_code != NO_ERROR)
1673 SetErrnoFromWinError (error_code);
1674 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
1675 return GNUNET_SYSERR;
1677 if (STILL_ACTIVE == c)
1679 *type = GNUNET_OS_PROCESS_RUNNING;
1683 *type = GNUNET_OS_PROCESS_EXITED;
1692 * Wait for a process to terminate. The return code is discarded.
1693 * You must not use #GNUNET_OS_process_status() on the same process
1694 * after calling this function! This function is blocking and should
1695 * thus only be used if the child process is known to have terminated
1696 * or to terminate very soon.
1698 * @param proc pointer to process structure
1699 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1702 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1705 pid_t pid = proc->pid;
1708 while ( (pid != (ret = waitpid (pid, NULL, 0))) &&
1709 (EINTR == errno) ) ;
1712 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1714 return GNUNET_SYSERR;
1723 LOG (GNUNET_ERROR_TYPE_WARNING,
1724 "Invalid process information {%d, %08X}\n",
1726 return GNUNET_SYSERR;
1729 h = GetCurrentProcess ();
1731 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1733 SetErrnoFromWinError (GetLastError ());
1734 return GNUNET_SYSERR;
1742 * Handle to a command.
1744 struct GNUNET_OS_CommandHandle
1750 struct GNUNET_OS_Process *eip;
1753 * Handle to the output pipe.
1755 struct GNUNET_DISK_PipeHandle *opipe;
1758 * Read-end of output pipe.
1760 const struct GNUNET_DISK_FileHandle *r;
1763 * Function to call on each line of output.
1765 GNUNET_OS_LineProcessor proc;
1768 * Closure for @e proc.
1773 * Buffer for the output.
1778 * Task reading from pipe.
1780 struct GNUNET_SCHEDULER_Task *rtask;
1785 struct GNUNET_TIME_Absolute timeout;
1788 * Current read offset in buf.
1795 * Stop/kill a command. Must ONLY be called either from
1796 * the callback after 'NULL' was passed for 'line' *OR*
1797 * from an independent task (not within the line processor).
1799 * @param cmd handle to the process
1802 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
1804 if (NULL != cmd->proc)
1806 GNUNET_assert (NULL != cmd->rtask);
1807 GNUNET_SCHEDULER_cancel (cmd->rtask);
1809 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1810 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
1811 GNUNET_OS_process_destroy (cmd->eip);
1812 GNUNET_DISK_pipe_close (cmd->opipe);
1818 * Read from the process and call the line processor.
1820 * @param cls the `struct GNUNET_OS_CommandHandle *`
1823 cmd_read (void *cls)
1825 struct GNUNET_OS_CommandHandle *cmd = cls;
1826 const struct GNUNET_SCHEDULER_TaskContext *tc;
1827 GNUNET_OS_LineProcessor proc;
1832 tc = GNUNET_SCHEDULER_get_task_context ();
1834 GNUNET_NETWORK_fdset_handle_isset (tc->read_ready,
1840 proc (cmd->proc_cls, NULL);
1843 ret = GNUNET_DISK_file_read (cmd->r,
1844 &cmd->buf[cmd->off],
1845 sizeof (cmd->buf) - cmd->off);
1848 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
1850 cmd->buf[cmd->off] = '\0';
1851 cmd->proc (cmd->proc_cls, cmd->buf);
1855 proc (cmd->proc_cls, NULL);
1858 end = memchr (&cmd->buf[cmd->off], '\n', ret);
1863 cmd->proc (cmd->proc_cls, cmd->buf);
1864 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1865 cmd->off -= (end + 1 - cmd->buf);
1866 end = memchr (cmd->buf, '\n', cmd->off);
1869 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
1877 * Run the given command line and call the given function
1878 * for each line of the output.
1880 * @param proc function to call for each line of the output
1881 * @param proc_cls closure for @a proc
1882 * @param timeout when to time out
1883 * @param binary command to run
1884 * @param ... arguments to command
1885 * @return NULL on error
1887 struct GNUNET_OS_CommandHandle *
1888 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
1890 struct GNUNET_TIME_Relative timeout,
1894 struct GNUNET_OS_CommandHandle *cmd;
1895 struct GNUNET_OS_Process *eip;
1896 struct GNUNET_DISK_PipeHandle *opipe;
1899 opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES,
1900 GNUNET_NO, GNUNET_YES);
1903 va_start (ap, binary);
1904 /* redirect stdout, don't inherit stderr/stdin */
1905 eip = GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL,
1906 opipe, NULL, binary,
1911 GNUNET_DISK_pipe_close (opipe);
1914 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
1915 cmd = GNUNET_new (struct GNUNET_OS_CommandHandle);
1916 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1920 cmd->proc_cls = proc_cls;
1921 cmd->r = GNUNET_DISK_pipe_handle (opipe,
1922 GNUNET_DISK_PIPE_END_READ);
1923 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout,
1931 /* end of os_priority.c */