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-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;
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));
1096 lsocks_write_fd = NULL;
1100 if (GNUNET_YES == pipe_control)
1102 GNUNET_asprintf (&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE);
1103 GNUNET_asprintf (&our_env[env_off++], "%p", childpipe_read_handle);
1105 if ( (lsocks != NULL) && (lsocks[0] != INVALID_SOCKET))
1107 /*This will tell the child that we're going to send lsocks over the pipe*/
1108 GNUNET_asprintf (&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS");
1109 GNUNET_asprintf (&our_env[env_off++], "%lu", lsocks_read);
1111 our_env[env_off++] = NULL;
1112 env_block = CreateCustomEnvTable (our_env);
1114 GNUNET_free_non_null (our_env[--env_off]);
1117 if (NULL == (wpath = u8_to_u16 ((uint8_t *) path, 1 + strlen (path), NULL, &wpath_len)))
1119 LOG (GNUNET_ERROR_TYPE_DEBUG,
1120 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", path, errno);
1121 GNUNET_free (env_block);
1124 GNUNET_DISK_pipe_close (lsocks_pipe);
1125 if (GNUNET_YES == pipe_control)
1127 GNUNET_DISK_file_close (childpipe_write);
1128 GNUNET_DISK_file_close (childpipe_read);
1134 if (NULL == (wcmd = u8_to_u16 ((uint8_t *) cmd, 1 + strlen (cmd), NULL, &wcmd_len)))
1136 LOG (GNUNET_ERROR_TYPE_DEBUG,
1137 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n",
1140 GNUNET_free (env_block);
1144 GNUNET_DISK_pipe_close (lsocks_pipe);
1145 if (GNUNET_YES == pipe_control)
1147 GNUNET_DISK_file_close (childpipe_write);
1148 GNUNET_DISK_file_close (childpipe_read);
1153 create_no_window = 0;
1155 HANDLE console_input = CreateFile ("CONIN$", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1156 if (INVALID_HANDLE_VALUE == console_input)
1157 create_no_window = CREATE_NO_WINDOW;
1159 CloseHandle (console_input);
1162 bresult = CreateProcessW (wpath, wcmd, NULL, NULL, GNUNET_YES,
1163 create_no_window | CREATE_SUSPENDED, env_block, NULL, &start, &proc);
1164 error_code = GetLastError ();
1166 if ((NULL == pipe_stdin) && (stdih))
1167 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, stdif);
1170 if ((NULL == pipe_stdout) && (stdoh))
1171 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, stdof);
1174 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, stdef);
1177 LOG (GNUNET_ERROR_TYPE_ERROR,
1178 "CreateProcess(%s, %s) failed: %lu\n",
1183 GNUNET_free (env_block);
1187 if (GNUNET_YES == pipe_control)
1189 GNUNET_DISK_file_close (childpipe_read);
1194 if (GNUNET_YES == pipe_control)
1196 GNUNET_DISK_file_close (childpipe_write);
1199 GNUNET_DISK_pipe_close (lsocks_pipe);
1200 SetErrnoFromWinError (error_code);
1204 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
1205 gnunet_proc->pid = proc.dwProcessId;
1206 gnunet_proc->handle = proc.hProcess;
1207 gnunet_proc->control_pipe = childpipe_write;
1209 CreateThread (NULL, 64000, &child_wait_thread, (void *) gnunet_proc, 0, NULL);
1211 ResumeThread (proc.hThread);
1212 CloseHandle (proc.hThread);
1214 if ( (NULL == lsocks) || (INVALID_SOCKET == lsocks[0]) )
1217 GNUNET_DISK_pipe_close_end (lsocks_pipe, GNUNET_DISK_PIPE_END_READ);
1219 /* This is a replacement for "goto error" that doesn't use goto */
1228 /* Tell the number of sockets */
1229 for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++);
1231 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1232 if (sizeof (count) != wrote)
1234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1235 "Failed to write %u count bytes to the child: %lu\n",
1236 sizeof (count), GetLastError ());
1239 for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++)
1241 WSAPROTOCOL_INFOA pi;
1242 /* Get a socket duplication info */
1243 if (SOCKET_ERROR == WSADuplicateSocketA (lsocks[i], gnunet_proc->pid, &pi))
1245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1246 "Failed to duplicate an socket[%u]: %lu\n", i,
1250 /* Synchronous I/O is not nice, but we can't schedule this:
1251 * lsocks will be closed/freed by the caller soon, and until
1252 * the child creates a duplicate, closing a socket here will
1253 * close it for good.
1255 /* Send the size of the structure
1256 * (the child might be built with different headers...)
1259 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &size, sizeof (size));
1260 if (sizeof (size) != wrote)
1262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1263 "Failed to write %u size[%u] bytes to the child: %lu\n",
1264 sizeof (size), i, GetLastError ());
1267 /* Finally! Send the data */
1268 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &pi, sizeof (pi));
1269 if (sizeof (pi) != wrote)
1271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1272 "Failed to write %u socket[%u] bytes to the child: %lu\n",
1273 sizeof (pi), i, GetLastError ());
1277 /* This will block us until the child makes a final read or closes
1278 * the pipe (hence no 'wrote' check), since we have to wait for it
1279 * to duplicate the last socket, before we return and start closing
1282 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1287 GNUNET_DISK_file_sync (lsocks_write_fd);
1288 GNUNET_DISK_pipe_close (lsocks_pipe);
1292 /* If we can't pass on the socket(s), the child will block forever,
1293 * better put it out of its misery.
1295 SafeTerminateProcess (gnunet_proc->handle, 0, 0);
1296 CloseHandle (gnunet_proc->handle);
1297 if (NULL != gnunet_proc->control_pipe)
1298 GNUNET_DISK_file_close (gnunet_proc->control_pipe);
1299 GNUNET_free (gnunet_proc);
1310 * @param pipe_control should a pipe be used to send signals to the child?
1311 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1312 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1313 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1314 * @param pipe_stderr pipe to use to get output from child process (or NULL)
1315 * @param filename name of the binary
1316 * @param argv NULL-terminated array of arguments to the process
1317 * @return pointer to process structure of the new process, NULL on error
1319 struct GNUNET_OS_Process *
1320 GNUNET_OS_start_process_vap (int pipe_control,
1321 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1322 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1323 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1324 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1325 const char *filename,
1328 return start_process (pipe_control,
1342 * @param pipe_control should a pipe be used to send signals to the child?
1343 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1344 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1345 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1346 * @param pipe_stderr pipe to use to get output from child process (or NULL)
1347 * @param filename name of the binary
1348 * @param va NULL-terminated list of arguments to the process
1349 * @return pointer to process structure of the new process, NULL on error
1351 struct GNUNET_OS_Process *
1352 GNUNET_OS_start_process_va (int pipe_control,
1353 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1354 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1355 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1356 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1357 const char *filename, va_list va)
1359 struct GNUNET_OS_Process *ret;
1366 while (NULL != va_arg (ap, char *))
1369 argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
1372 while (NULL != (argv[argc] = va_arg (ap, char *)))
1375 ret = GNUNET_OS_start_process_vap (pipe_control,
1390 * @param pipe_control should a pipe be used to send signals to the child?
1391 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1392 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1393 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1394 * @param filename name of the binary
1395 * @param ... NULL-terminated list of arguments to the process
1396 * @return pointer to process structure of the new process, NULL on error
1398 struct GNUNET_OS_Process *
1399 GNUNET_OS_start_process (int pipe_control,
1400 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1401 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1402 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1403 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1404 const char *filename, ...)
1406 struct GNUNET_OS_Process *ret;
1409 va_start (ap, filename);
1410 ret = GNUNET_OS_start_process_va (pipe_control,
1425 * @param pipe_control should a pipe be used to send signals to the child?
1426 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
1427 * std handles of the parent are inherited by the child.
1428 * pipe_stdin and pipe_stdout take priority over std_inheritance
1429 * (when they are non-NULL).
1430 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1431 * must be NULL on platforms where dup is not supported
1432 * @param filename name of the binary
1433 * @param argv NULL-terminated list of arguments to the process
1434 * @return process ID of the new process, -1 on error
1436 struct GNUNET_OS_Process *
1437 GNUNET_OS_start_process_v (int pipe_control,
1438 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1439 const SOCKTYPE *lsocks,
1440 const char *filename,
1443 return start_process (pipe_control,
1455 * Start a process. This function is similar to the GNUNET_OS_start_process_*
1456 * except that the filename and arguments can have whole strings which contain
1457 * the arguments. These arguments are to be separated by spaces and are parsed
1458 * in the order they appear. Arguments containing spaces can be used by
1459 * quoting them with @em ".
1461 * @param pipe_control should a pipe be used to send signals to the child?
1462 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1463 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1464 * must be NULL on platforms where dup is not supported
1465 * @param filename name of the binary. It is valid to have the arguments
1466 * in this string when they are separated by spaces.
1467 * @param ... more arguments. Should be of type `char *`. It is valid
1468 * to have the arguments in these strings when they are separated by
1469 * spaces. The last argument MUST be NULL.
1470 * @return pointer to process structure of the new process, NULL on error
1472 struct GNUNET_OS_Process *
1473 GNUNET_OS_start_process_s (int pipe_control,
1474 unsigned int std_inheritance,
1475 const SOCKTYPE * lsocks,
1476 const char *filename, ...)
1480 unsigned int argv_size;
1486 struct GNUNET_OS_Process *proc;
1493 va_start (ap, filename);
1500 while ('\0' != *rpos)
1509 if ( (' ' == *rpos) && (0 == quote_on) )
1515 while (' ' == *rpos)
1518 if ( (NULL == last) && ('\0' != *rpos) ) // FIXME: == or !=?
1526 while (NULL != (arg = (va_arg (ap, const char*))));
1529 argv = GNUNET_malloc (argv_size * sizeof (char *));
1531 va_start (ap, filename);
1536 cp = GNUNET_strdup (arg);
1539 while ('\0' != *pos)
1548 if ( (' ' == *pos) && (0 == quote_on) )
1552 argv[argv_size++] = GNUNET_strdup (last);
1558 if ( (NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
1564 argv[argv_size++] = GNUNET_strdup (last);
1568 while (NULL != (arg = (va_arg (ap, const char*))));
1570 argv[argv_size] = NULL;
1572 for(i = 0; i < argv_size; i++)
1574 len = strlen (argv[i]);
1575 if ( (argv[i][0] == '"') && (argv[i][len-1] == '"'))
1577 memmove (&argv[i][0], &argv[i][1], len - 2);
1578 argv[i][len-2] = '\0';
1581 binary_path = argv[0];
1582 proc = GNUNET_OS_start_process_v (pipe_control, std_inheritance, lsocks,
1584 while (argv_size > 0)
1585 GNUNET_free (argv[--argv_size]);
1592 * Retrieve the status of a process, waiting on him if dead.
1593 * Nonblocking version.
1595 * @param proc process ID
1596 * @param type status type
1597 * @param code return code/signal number
1598 * @param options WNOHANG if non-blocking is desired
1599 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1602 process_status (struct GNUNET_OS_Process *proc,
1603 enum GNUNET_OS_ProcessStatusType *type,
1604 unsigned long *code,
1611 GNUNET_assert (0 != proc);
1612 ret = waitpid (proc->pid, &status, options);
1615 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1617 return GNUNET_SYSERR;
1621 *type = GNUNET_OS_PROCESS_RUNNING;
1625 if (proc->pid != ret)
1627 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1628 return GNUNET_SYSERR;
1630 if (WIFEXITED (status))
1632 *type = GNUNET_OS_PROCESS_EXITED;
1633 *code = WEXITSTATUS (status);
1635 else if (WIFSIGNALED (status))
1637 *type = GNUNET_OS_PROCESS_SIGNALED;
1638 *code = WTERMSIG (status);
1640 else if (WIFSTOPPED (status))
1642 *type = GNUNET_OS_PROCESS_SIGNALED;
1643 *code = WSTOPSIG (status);
1646 else if (WIFCONTINUED (status))
1648 *type = GNUNET_OS_PROCESS_RUNNING;
1654 *type = GNUNET_OS_PROCESS_UNKNOWN;
1659 #define WNOHANG 42 /* just a flag for W32, purely internal at this point */
1663 DWORD c, error_code, ret;
1667 if (h == NULL || ret == 0)
1669 LOG (GNUNET_ERROR_TYPE_WARNING,
1670 "Invalid process information {%d, %08X}\n",
1672 return GNUNET_SYSERR;
1675 h = GetCurrentProcess ();
1677 if (WNOHANG != options)
1679 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1681 SetErrnoFromWinError (GetLastError ());
1682 return GNUNET_SYSERR;
1686 ret = GetExitCodeProcess (h, &c);
1687 error_code = GetLastError ();
1688 if (ret == 0 || error_code != NO_ERROR)
1690 SetErrnoFromWinError (error_code);
1691 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
1692 return GNUNET_SYSERR;
1694 if (STILL_ACTIVE == c)
1696 *type = GNUNET_OS_PROCESS_RUNNING;
1700 *type = GNUNET_OS_PROCESS_EXITED;
1709 * Retrieve the status of a process, waiting on him if dead.
1710 * Nonblocking version.
1712 * @param proc process ID
1713 * @param type status type
1714 * @param code return code/signal number
1715 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1718 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1719 enum GNUNET_OS_ProcessStatusType *type,
1720 unsigned long *code)
1722 return process_status (proc,
1730 * Retrieve the status of a process, waiting on him if dead.
1733 * @param proc pointer to process structure
1734 * @param type status type
1735 * @param code return code/signal number
1736 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1739 GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc,
1740 enum GNUNET_OS_ProcessStatusType *type,
1741 unsigned long *code)
1743 return process_status (proc,
1751 * Wait for a process to terminate. The return code is discarded.
1752 * You must not use #GNUNET_OS_process_status() on the same process
1753 * after calling this function! This function is blocking and should
1754 * thus only be used if the child process is known to have terminated
1755 * or to terminate very soon.
1757 * @param proc pointer to process structure
1758 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1761 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1764 pid_t pid = proc->pid;
1767 while ( (pid != (ret = waitpid (pid, NULL, 0))) &&
1768 (EINTR == errno) ) ;
1771 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1773 return GNUNET_SYSERR;
1782 LOG (GNUNET_ERROR_TYPE_WARNING,
1783 "Invalid process information {%d, %08X}\n",
1785 return GNUNET_SYSERR;
1788 h = GetCurrentProcess ();
1790 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1792 SetErrnoFromWinError (GetLastError ());
1793 return GNUNET_SYSERR;
1801 * Handle to a command.
1803 struct GNUNET_OS_CommandHandle
1809 struct GNUNET_OS_Process *eip;
1812 * Handle to the output pipe.
1814 struct GNUNET_DISK_PipeHandle *opipe;
1817 * Read-end of output pipe.
1819 const struct GNUNET_DISK_FileHandle *r;
1822 * Function to call on each line of output.
1824 GNUNET_OS_LineProcessor proc;
1827 * Closure for @e proc.
1832 * Buffer for the output.
1837 * Task reading from pipe.
1839 struct GNUNET_SCHEDULER_Task *rtask;
1844 struct GNUNET_TIME_Absolute timeout;
1847 * Current read offset in buf.
1854 * Stop/kill a command. Must ONLY be called either from
1855 * the callback after 'NULL' was passed for 'line' *OR*
1856 * from an independent task (not within the line processor).
1858 * @param cmd handle to the process
1861 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
1863 if (NULL != cmd->proc)
1865 GNUNET_assert (NULL != cmd->rtask);
1866 GNUNET_SCHEDULER_cancel (cmd->rtask);
1868 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1869 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
1870 GNUNET_OS_process_destroy (cmd->eip);
1871 GNUNET_DISK_pipe_close (cmd->opipe);
1877 * Read from the process and call the line processor.
1879 * @param cls the `struct GNUNET_OS_CommandHandle *`
1882 cmd_read (void *cls)
1884 struct GNUNET_OS_CommandHandle *cmd = cls;
1885 const struct GNUNET_SCHEDULER_TaskContext *tc;
1886 GNUNET_OS_LineProcessor proc;
1891 tc = GNUNET_SCHEDULER_get_task_context ();
1893 GNUNET_NETWORK_fdset_handle_isset (tc->read_ready,
1899 proc (cmd->proc_cls, NULL);
1902 ret = GNUNET_DISK_file_read (cmd->r,
1903 &cmd->buf[cmd->off],
1904 sizeof (cmd->buf) - cmd->off);
1907 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
1909 cmd->buf[cmd->off] = '\0';
1910 cmd->proc (cmd->proc_cls, cmd->buf);
1914 proc (cmd->proc_cls, NULL);
1917 end = memchr (&cmd->buf[cmd->off], '\n', ret);
1922 cmd->proc (cmd->proc_cls, cmd->buf);
1923 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1924 cmd->off -= (end + 1 - cmd->buf);
1925 end = memchr (cmd->buf, '\n', cmd->off);
1928 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
1936 * Run the given command line and call the given function
1937 * for each line of the output.
1939 * @param proc function to call for each line of the output
1940 * @param proc_cls closure for @a proc
1941 * @param timeout when to time out
1942 * @param binary command to run
1943 * @param ... arguments to command
1944 * @return NULL on error
1946 struct GNUNET_OS_CommandHandle *
1947 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
1949 struct GNUNET_TIME_Relative timeout,
1953 struct GNUNET_OS_CommandHandle *cmd;
1954 struct GNUNET_OS_Process *eip;
1955 struct GNUNET_DISK_PipeHandle *opipe;
1958 opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES,
1959 GNUNET_NO, GNUNET_YES);
1962 va_start (ap, binary);
1963 /* redirect stdout, don't inherit stderr/stdin */
1964 eip = GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL,
1965 opipe, NULL, binary,
1970 GNUNET_DISK_pipe_close (opipe);
1973 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
1974 cmd = GNUNET_new (struct GNUNET_OS_CommandHandle);
1975 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1979 cmd->proc_cls = proc_cls;
1980 cmd->r = GNUNET_DISK_pipe_handle (opipe,
1981 GNUNET_DISK_PIPE_END_READ);
1982 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout,
1990 /* end of os_priority.c */