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/>.
20 * @file util/os_priority.c
21 * @brief Methods to set process priority
26 #include "gnunet_util_lib.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util-os-priority", __VA_ARGS__)
32 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-os-priority", syscall)
34 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-os-priority", syscall, filename)
36 #define GNUNET_OS_CONTROL_PIPE "GNUNET_OS_CONTROL_PIPE"
39 struct GNUNET_OS_Process
54 * Pipe we use to signal the process.
55 * NULL if unused, or if process was deemed uncontrollable.
57 struct GNUNET_DISK_FileHandle *control_pipe;
62 * Handle for 'this' process.
64 static struct GNUNET_OS_Process current_process;
67 * Handle for the #parent_control_handler() Task.
69 static struct GNUNET_SCHEDULER_Task *pch;
72 * Handle for the #shutdown_pch() Task.
74 static struct GNUNET_SCHEDULER_Task *spch;
78 * This handler is called on shutdown to remove the #pch.
80 * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe
83 shutdown_pch (void *cls)
85 struct GNUNET_DISK_FileHandle *control_pipe = cls;
87 GNUNET_SCHEDULER_cancel (pch);
89 GNUNET_DISK_file_close (control_pipe);
95 * This handler is called when there are control data to be read on the pipe
97 * @param cls the `struct GNUNET_DISK_FileHandle` of the control pipe
100 parent_control_handler (void *cls)
102 struct GNUNET_DISK_FileHandle *control_pipe = cls;
108 ret = GNUNET_DISK_file_read (control_pipe,
111 if (sizeof (sig) != ret)
114 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
115 "GNUNET_DISK_file_read");
116 LOG (GNUNET_ERROR_TYPE_DEBUG,
117 "Closing control pipe\n");
118 GNUNET_DISK_file_close (control_pipe);
120 GNUNET_SCHEDULER_cancel (spch);
124 pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE);
125 GNUNET_assert ( (NULL == pipe_fd) ||
126 (strlen (pipe_fd) <= 0) );
127 LOG (GNUNET_ERROR_TYPE_DEBUG,
128 "Got control code %d from parent via pipe %s\n",
131 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
133 &parent_control_handler,
135 GNUNET_SIGNAL_raise ((int) sig);
140 * Task that connects this process to its parent via pipe;
141 * essentially, the parent control handler will read signal numbers
142 * from the #GNUNET_OS_CONTROL_PIPE (as given in an environment
143 * variable) and raise those signals.
145 * @param cls closure (unused)
148 GNUNET_OS_install_parent_control_handler (void *cls)
152 struct GNUNET_DISK_FileHandle *control_pipe;
158 /* already done, we've been called twice... */
162 env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
163 if ( (NULL == env_buf) || (strlen (env_buf) <= 0) )
165 LOG (GNUNET_ERROR_TYPE_DEBUG,
166 "Not installing a handler because $%s is empty\n",
167 GNUNET_OS_CONTROL_PIPE);
168 putenv (GNUNET_OS_CONTROL_PIPE "=");
172 pipe_fd = strtoull (env_buf, &env_buf_end, 16);
173 if ((0 != errno) || (env_buf == env_buf_end))
175 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
178 putenv (GNUNET_OS_CONTROL_PIPE "=");
181 #if !defined (WINDOWS)
182 if (pipe_fd >= FD_SETSIZE)
184 if ((FILE_TYPE_UNKNOWN == GetFileType ((HANDLE) (uintptr_t) pipe_fd))
185 && (0 != GetLastError ()))
188 LOG (GNUNET_ERROR_TYPE_ERROR,
189 "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n",
191 putenv (GNUNET_OS_CONTROL_PIPE "=");
195 control_pipe = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) (uintptr_t) pipe_fd);
197 control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
199 if (NULL == control_pipe)
201 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
204 putenv (GNUNET_OS_CONTROL_PIPE "=");
207 LOG (GNUNET_ERROR_TYPE_DEBUG,
208 "Adding parent control handler pipe `%s' to the scheduler\n",
210 pch = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
212 &parent_control_handler,
214 spch = GNUNET_SCHEDULER_add_shutdown (&shutdown_pch,
216 putenv (GNUNET_OS_CONTROL_PIPE "=");
221 * Get process structure for current process
223 * The pointer it returns points to static memory location and must
224 * not be deallocated/closed.
226 * @return pointer to the process sturcutre for this process
228 struct GNUNET_OS_Process *
229 GNUNET_OS_process_current ()
232 current_process.pid = GetCurrentProcessId ();
233 current_process.handle = GetCurrentProcess ();
235 current_process.pid = 0;
237 return ¤t_process;
242 * Sends a signal to the process
244 * @param proc pointer to process structure
246 * @return 0 on success, -1 on error
249 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc,
256 if (NULL != proc->control_pipe)
258 LOG (GNUNET_ERROR_TYPE_DEBUG,
259 "Sending signal %d to pid: %u via pipe\n",
262 ret = GNUNET_DISK_file_write (proc->control_pipe,
265 if (sizeof (csig) == ret)
268 /* pipe failed or non-existent, try other methods */
271 #if !defined (WINDOWS)
277 #if (SIGTERM != GNUNET_TERM_SIG)
278 case GNUNET_TERM_SIG:
280 #if defined(WINDOWS) && !defined(__CYGWIN__)
283 int must_kill = GNUNET_YES;
284 if (0 != GetExitCodeProcess (proc->handle, &exitcode))
285 must_kill = (exitcode == STILL_ACTIVE) ? GNUNET_YES : GNUNET_NO;
286 if (GNUNET_YES == must_kill)
288 if (0 == SafeTerminateProcess (proc->handle, 0, 0))
290 DWORD error_code = GetLastError ();
291 if ( (error_code != WAIT_TIMEOUT) &&
292 (error_code != ERROR_PROCESS_ABORTED) )
294 LOG ((error_code == ERROR_ACCESS_DENIED) ?
295 GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
296 "SafeTermiateProcess failed with code %lu\n",
298 /* The problem here is that a process that is already dying
299 * might cause SafeTerminateProcess to fail with
300 * ERROR_ACCESS_DENIED, but the process WILL die eventually.
301 * If we really had a permissions problem, hanging up (which
302 * is what will happen in process_wait() in that case) is
305 if (ERROR_ACCESS_DENIED == error_code)
311 SetErrnoFromWinError (error_code);
320 LOG (GNUNET_ERROR_TYPE_DEBUG,
321 "Sending signal %d to pid: %u via system call\n",
324 return PLIBC_KILL (proc->pid, sig);
327 #if defined (WINDOWS)
331 LOG (GNUNET_ERROR_TYPE_DEBUG,
332 "Sending signal %d to pid: %u via system call\n",
335 return PLIBC_KILL (proc->pid, sig);
342 * Get the pid of the process in question
344 * @param proc the process to get the pid of
346 * @return the current process id
349 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process * proc)
356 * Cleans up process structure contents (OS-dependent) and deallocates
359 * @param proc pointer to process structure
362 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
364 if (NULL != proc->control_pipe)
365 GNUNET_DISK_file_close (proc->control_pipe);
366 #if defined (WINDOWS)
367 if (NULL != proc->handle)
368 CloseHandle (proc->handle);
375 #include "gnunet_signal_lib.h"
377 extern GNUNET_SIGNAL_Handler w32_sigchld_handler;
380 * Make seaspider happy.
382 #define DWORD_WINAPI DWORD WINAPI
386 * @brief Waits for a process to terminate and invokes the SIGCHLD handler
387 * @param proc pointer to process structure
390 child_wait_thread (void *arg)
392 struct GNUNET_OS_Process *proc = (struct GNUNET_OS_Process *) arg;
394 WaitForSingleObject (proc->handle, INFINITE);
396 if (w32_sigchld_handler)
397 w32_sigchld_handler ();
406 CreateCustomEnvTable (char **vars)
408 char *win32_env_table;
413 size_t tablesize = 0;
414 size_t items_count = 0;
423 win32_env_table = GetEnvironmentStringsA ();
424 if (NULL == win32_env_table)
426 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ;
428 index = GNUNET_malloc (sizeof (char *) * n_var);
429 for (c = 0; c < n_var; c++)
431 for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++)
433 size_t len = strlen (ptr);
436 for (var_ptr = vars; *var_ptr; var_ptr++)
440 var_len = strlen (var);
441 if (strncmp (var, ptr, var_len) == 0)
445 tablesize += var_len + strlen (val) + 1;
450 tablesize += len + 1;
453 for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
458 n_found += strlen (var) + strlen (val) + 1;
460 result = GNUNET_malloc (tablesize + n_found + 1);
461 for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
463 size_t len = strlen (ptr);
466 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
470 var_len = strlen (var);
471 if (strncmp (var, ptr, var_len) == 0)
479 strcpy (result_ptr, ptr);
480 result_ptr += len + 1;
484 strcpy (result_ptr, var);
485 result_ptr += var_len;
486 strcpy (result_ptr, val);
487 result_ptr += strlen (val) + 1;
491 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
495 var_len = strlen (var);
498 strcpy (result_ptr, var);
499 result_ptr += var_len;
500 strcpy (result_ptr, val);
501 result_ptr += strlen (val) + 1;
504 FreeEnvironmentStrings (win32_env_table);
513 * Open '/dev/null' and make the result the given
516 * @param target_fd desired FD to point to /dev/null
517 * @param flags open flags (O_RDONLY, O_WRONLY)
520 open_dev_null (int target_fd,
525 fd = open ("/dev/null", flags);
528 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
535 if (-1 == dup2 (fd, target_fd))
537 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
541 GNUNET_break (0 == close (fd));
549 * @param pipe_control should a pipe be used to send signals to the child?
550 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
551 * std handles of the parent are inherited by the child.
552 * pipe_stdin and pipe_stdout take priority over std_inheritance
553 * (when they are non-NULL).
554 * @param pipe_stdin pipe to use to send input to child process (or NULL)
555 * @param pipe_stdout pipe to use to get output from child process (or NULL)
556 * @param pipe_stderr pipe to use for stderr for child process (or NULL)
557 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
558 * must be NULL on platforms where dup is not supported
559 * @param filename name of the binary
560 * @param argv NULL-terminated list of arguments to the process
561 * @return process ID of the new process, -1 on error
563 static struct GNUNET_OS_Process *
564 start_process (int pipe_control,
565 enum GNUNET_OS_InheritStdioFlags std_inheritance,
566 struct GNUNET_DISK_PipeHandle *pipe_stdin,
567 struct GNUNET_DISK_PipeHandle *pipe_stdout,
568 struct GNUNET_DISK_PipeHandle *pipe_stderr,
569 const SOCKTYPE *lsocks,
570 const char *filename,
576 struct GNUNET_OS_Process *gnunet_proc;
577 struct GNUNET_DISK_FileHandle *childpipe_read;
578 struct GNUNET_DISK_FileHandle *childpipe_write;
579 int childpipe_read_fd;
595 GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
596 return NULL; /* not executable */
597 if (GNUNET_YES == pipe_control)
599 struct GNUNET_DISK_PipeHandle *childpipe;
600 int dup_childpipe_read_fd = -1;
602 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
603 GNUNET_YES, GNUNET_NO);
604 if (NULL == childpipe)
606 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe,
607 GNUNET_DISK_PIPE_END_READ);
608 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe,
609 GNUNET_DISK_PIPE_END_WRITE);
610 GNUNET_DISK_pipe_close (childpipe);
611 if ( (NULL == childpipe_read) ||
612 (NULL == childpipe_write) ||
614 GNUNET_DISK_internal_file_handle_ (childpipe_read,
617 (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
619 if (NULL != childpipe_read)
620 GNUNET_DISK_file_close (childpipe_read);
621 if (NULL != childpipe_write)
622 GNUNET_DISK_file_close (childpipe_write);
623 if (0 <= dup_childpipe_read_fd)
624 close (dup_childpipe_read_fd);
627 childpipe_read_fd = dup_childpipe_read_fd;
628 GNUNET_DISK_file_close (childpipe_read);
632 childpipe_write = NULL;
633 childpipe_read_fd = -1;
635 if (NULL != pipe_stdin)
637 GNUNET_assert (GNUNET_OK ==
638 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
639 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
640 &fd_stdin_read, sizeof (int)));
641 GNUNET_assert (GNUNET_OK ==
642 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
643 (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
644 &fd_stdin_write, sizeof (int)));
646 if (NULL != pipe_stdout)
648 GNUNET_assert (GNUNET_OK ==
649 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
651 GNUNET_DISK_PIPE_END_WRITE),
652 &fd_stdout_write, sizeof (int)));
653 GNUNET_assert (GNUNET_OK ==
654 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
655 (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
656 &fd_stdout_read, sizeof (int)));
658 if (NULL != pipe_stderr)
660 GNUNET_assert (GNUNET_OK ==
661 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
663 GNUNET_DISK_PIPE_END_READ),
664 &fd_stderr_read, sizeof (int)));
665 GNUNET_assert (GNUNET_OK ==
666 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
668 GNUNET_DISK_PIPE_END_WRITE),
669 &fd_stderr_write, sizeof (int)));
676 while (-1 != (k = lsocks[i++]))
677 GNUNET_array_append (lscp, ls, k);
678 GNUNET_array_append (lscp, ls, -1);
681 /* see https://gnunet.org/vfork */
689 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
690 GNUNET_array_grow (lscp, ls, 0);
691 if (NULL != childpipe_write)
692 GNUNET_DISK_file_close (childpipe_write);
693 if (0 <= childpipe_read_fd)
694 close (childpipe_read_fd);
700 unsetenv (GNUNET_OS_CONTROL_PIPE);
701 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
702 gnunet_proc->pid = ret;
703 gnunet_proc->control_pipe = childpipe_write;
704 if (GNUNET_YES == pipe_control)
706 close (childpipe_read_fd);
708 GNUNET_array_grow (lscp, ls, 0);
711 if (0 <= childpipe_read_fd)
715 /* due to vfork, we must NOT free memory on DARWIN! */
716 GNUNET_DISK_file_close (childpipe_write);
718 snprintf (fdbuf, 100, "%x", childpipe_read_fd);
719 setenv (GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
722 unsetenv (GNUNET_OS_CONTROL_PIPE);
723 if (NULL != pipe_stdin)
725 GNUNET_break (0 == close (fd_stdin_write));
726 if (-1 == dup2 (fd_stdin_read, 0))
727 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
728 GNUNET_break (0 == close (fd_stdin_read));
730 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
732 GNUNET_break (0 == close (0));
733 open_dev_null (0, O_RDONLY);
735 if (NULL != pipe_stdout)
737 GNUNET_break (0 == close (fd_stdout_read));
738 if (-1 == dup2 (fd_stdout_write, 1))
739 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
740 GNUNET_break (0 == close (fd_stdout_write));
742 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
744 GNUNET_break (0 == close (1));
745 open_dev_null (1, O_WRONLY);
747 if (NULL != pipe_stderr)
749 GNUNET_break (0 == close (fd_stderr_read));
750 if (-1 == dup2 (fd_stderr_write, 2))
751 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
752 GNUNET_break (0 == close (fd_stderr_write));
754 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
756 GNUNET_break (0 == close (2));
757 open_dev_null (2, O_WRONLY);
761 /* read systemd documentation... */
764 while (-1 != lscp[i])
767 while (-1 != lscp[j])
773 GNUNET_assert (-1 != k);
774 GNUNET_assert (0 == close (lscp[j]));
782 /* Bury any existing FD, no matter what; they should all be closed
783 * on exec anyway and the important onces have been dup'ed away */
785 GNUNET_assert (-1 != dup2 (lscp[i], tgt));
787 /* unset close-on-exec flag */
788 flags = fcntl (tgt, F_GETFD);
789 GNUNET_assert (flags >= 0);
790 flags &= ~FD_CLOEXEC;
792 (void) fcntl (tgt, F_SETFD, flags);
796 GNUNET_snprintf (fds, sizeof (fds), "%u", i);
797 setenv ("LISTEN_FDS", fds, 1);
800 /* due to vfork, we must NOT free memory on DARWIN! */
801 GNUNET_array_grow (lscp, ls, 0);
803 execvp (filename, argv);
804 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
807 struct GNUNET_DISK_FileHandle *childpipe_read;
808 struct GNUNET_DISK_FileHandle *childpipe_write;
809 HANDLE childpipe_read_handle;
811 char **non_const_argv;
816 PROCESS_INFORMATION proc;
818 struct GNUNET_OS_Process *gnunet_proc;
819 char path[MAX_PATH + 1];
820 char *our_env[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
821 char *env_block = NULL;
829 char *non_const_filename;
830 char win_path[MAX_PATH + 1];
831 struct GNUNET_DISK_PipeHandle *lsocks_pipe;
832 const struct GNUNET_DISK_FileHandle *lsocks_write_fd;
843 HANDLE stdout_handle;
844 HANDLE stdih, stdoh, stdeh;
845 DWORD stdif, stdof, stdef;
848 DWORD create_no_window;
850 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
851 return NULL; /* not executable */
853 /* Search in prefix dir (hopefully - the directory from which
854 * the current module was loaded), bindir and libdir, then in PATH
856 self_prefix = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_SELF_PREFIX);
857 bindir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
858 libdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
860 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
863 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
866 pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
869 ptr += sprintf (pathbuf, "%s;%s;%s;", self_prefix, bindir, libdir);
870 GNUNET_free (self_prefix);
871 GNUNET_free (bindir);
872 GNUNET_free (libdir);
874 alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len);
875 if (alloc_len != pathbuf_len - 1)
877 GNUNET_free (pathbuf);
878 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */
882 cmdlen = strlen (filename);
883 if ( (cmdlen < 5) || (0 != strcmp (&filename[cmdlen - 4], ".exe")) )
884 GNUNET_asprintf (&non_const_filename, "%s.exe", filename);
886 GNUNET_asprintf (&non_const_filename, "%s", filename);
888 /* It could be in POSIX form, convert it to a DOS path early on */
889 if (ERROR_SUCCESS != (lRet = plibc_conv_to_win_path (non_const_filename, win_path)))
891 SetErrnoFromWinError (lRet);
892 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "plibc_conv_to_win_path",
894 GNUNET_free (non_const_filename);
895 GNUNET_free (pathbuf);
898 GNUNET_free (non_const_filename);
899 non_const_filename = GNUNET_strdup (win_path);
900 /* Check that this is the full path. If it isn't, search. */
901 /* FIXME: convert it to wchar_t and use SearchPathW?
902 * Remember: arguments to _start_process() are technically in UTF-8...
904 if (non_const_filename[1] == ':')
906 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
907 LOG (GNUNET_ERROR_TYPE_DEBUG,
908 "Using path `%s' as-is. PATH is %s\n", path, ptr);
910 else if (!SearchPathA
911 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char),
914 SetErrnoFromWinError (GetLastError ());
915 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
917 GNUNET_free (non_const_filename);
918 GNUNET_free (pathbuf);
922 LOG (GNUNET_ERROR_TYPE_DEBUG,
923 "Found `%s' in PATH `%s'\n", path, pathbuf);
924 GNUNET_free (pathbuf);
925 GNUNET_free (non_const_filename);
927 /* Count the number of arguments */
928 arg = (char **) argv;
935 /* Allocate a copy argv */
936 non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1));
938 /* Copy all argv strings */
940 arg = (char **) argv;
944 non_const_argv[argcount] = GNUNET_strdup (path);
946 non_const_argv[argcount] = GNUNET_strdup (*arg);
950 non_const_argv[argcount] = NULL;
954 arg = non_const_argv;
957 cmdlen = cmdlen + strlen (*arg) + 4;
961 /* Allocate and create cmd */
962 cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen);
963 arg = non_const_argv;
966 char arg_last_char = (*arg)[strlen (*arg) - 1];
967 idx += sprintf (idx, "\"%s%s\"%s", *arg,
968 arg_last_char == '\\' ? "\\" : "", *(arg + 1) ? " " : "");
973 GNUNET_free (non_const_argv[--argcount]);
974 GNUNET_free (non_const_argv);
976 memset (&start, 0, sizeof (start));
977 start.cb = sizeof (start);
978 if ((pipe_stdin != NULL) || (pipe_stdout != NULL) || (std_inheritance != 0))
979 start.dwFlags |= STARTF_USESTDHANDLES;
981 stdih = GetStdHandle (STD_INPUT_HANDLE);
982 GetHandleInformation (stdih, &stdif);
983 if (pipe_stdin != NULL)
985 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
986 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
987 &stdin_handle, sizeof (HANDLE));
988 start.hStdInput = stdin_handle;
992 if (std_inheritance & GNUNET_OS_INHERIT_STD_IN)
994 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 1);
995 if (pipe_stdin == NULL)
996 start.hStdInput = stdih;
999 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 0);
1003 stdoh = GetStdHandle (STD_OUTPUT_HANDLE);
1004 GetHandleInformation (stdoh, &stdof);
1005 if (NULL != pipe_stdout)
1007 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1009 GNUNET_DISK_PIPE_END_WRITE),
1010 &stdout_handle, sizeof (HANDLE));
1011 start.hStdOutput = stdout_handle;
1015 if (std_inheritance & GNUNET_OS_INHERIT_STD_OUT)
1017 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 1);
1018 if (pipe_stdout == NULL)
1019 start.hStdOutput = stdoh;
1022 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 0);
1025 stdeh = GetStdHandle (STD_ERROR_HANDLE);
1026 GetHandleInformation (stdeh, &stdef);
1029 if (std_inheritance & GNUNET_OS_INHERIT_STD_ERR)
1031 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 1);
1032 start.hStdError = stdeh;
1035 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 0);
1038 if (GNUNET_YES == pipe_control)
1040 struct GNUNET_DISK_PipeHandle *childpipe;
1041 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
1042 if (NULL == childpipe)
1044 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
1045 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
1046 GNUNET_DISK_pipe_close (childpipe);
1047 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
1048 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
1049 &childpipe_read_handle, sizeof (HANDLE))))
1052 GNUNET_DISK_file_close (childpipe_read);
1053 if (childpipe_write)
1054 GNUNET_DISK_file_close (childpipe_write);
1058 /* Unlike *nix variant, we don't dup the handle, so can't close
1059 * filehandle right now.
1061 SetHandleInformation (childpipe_read_handle, HANDLE_FLAG_INHERIT, 1);
1065 childpipe_read = NULL;
1066 childpipe_write = NULL;
1069 if (lsocks != NULL && lsocks[0] != INVALID_SOCKET)
1071 lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1073 if (lsocks_pipe == NULL)
1076 GNUNET_DISK_pipe_close (lsocks_pipe);
1077 if (GNUNET_YES == pipe_control)
1079 GNUNET_DISK_file_close (childpipe_write);
1080 GNUNET_DISK_file_close (childpipe_read);
1084 lsocks_write_fd = GNUNET_DISK_pipe_handle (lsocks_pipe,
1085 GNUNET_DISK_PIPE_END_WRITE);
1086 GNUNET_DISK_internal_file_handle_ (lsocks_write_fd,
1087 &lsocks_write, sizeof (HANDLE));
1088 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1089 (lsocks_pipe, GNUNET_DISK_PIPE_END_READ),
1090 &lsocks_read, sizeof (HANDLE));
1095 lsocks_write_fd = NULL;
1099 if (GNUNET_YES == pipe_control)
1101 GNUNET_asprintf (&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE);
1102 GNUNET_asprintf (&our_env[env_off++], "%p", childpipe_read_handle);
1104 if ( (lsocks != NULL) && (lsocks[0] != INVALID_SOCKET))
1106 /*This will tell the child that we're going to send lsocks over the pipe*/
1107 GNUNET_asprintf (&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS");
1108 GNUNET_asprintf (&our_env[env_off++], "%lu", lsocks_read);
1110 our_env[env_off++] = NULL;
1111 env_block = CreateCustomEnvTable (our_env);
1113 GNUNET_free_non_null (our_env[--env_off]);
1116 if (NULL == (wpath = u8_to_u16 ((uint8_t *) path, 1 + strlen (path), NULL, &wpath_len)))
1118 LOG (GNUNET_ERROR_TYPE_DEBUG,
1119 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", path, errno);
1120 GNUNET_free (env_block);
1123 GNUNET_DISK_pipe_close (lsocks_pipe);
1124 if (GNUNET_YES == pipe_control)
1126 GNUNET_DISK_file_close (childpipe_write);
1127 GNUNET_DISK_file_close (childpipe_read);
1133 if (NULL == (wcmd = u8_to_u16 ((uint8_t *) cmd, 1 + strlen (cmd), NULL, &wcmd_len)))
1135 LOG (GNUNET_ERROR_TYPE_DEBUG,
1136 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n",
1139 GNUNET_free (env_block);
1143 GNUNET_DISK_pipe_close (lsocks_pipe);
1144 if (GNUNET_YES == pipe_control)
1146 GNUNET_DISK_file_close (childpipe_write);
1147 GNUNET_DISK_file_close (childpipe_read);
1152 create_no_window = 0;
1154 HANDLE console_input = CreateFile ("CONIN$", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1155 if (INVALID_HANDLE_VALUE == console_input)
1156 create_no_window = CREATE_NO_WINDOW;
1158 CloseHandle (console_input);
1161 bresult = CreateProcessW (wpath, wcmd, NULL, NULL, GNUNET_YES,
1162 create_no_window | CREATE_SUSPENDED, env_block, NULL, &start, &proc);
1163 error_code = GetLastError ();
1165 if ((NULL == pipe_stdin) && (stdih))
1166 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, stdif);
1169 if ((NULL == pipe_stdout) && (stdoh))
1170 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, stdof);
1173 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, stdef);
1176 LOG (GNUNET_ERROR_TYPE_ERROR,
1177 "CreateProcess(%s, %s) failed: %lu\n",
1182 GNUNET_free (env_block);
1186 if (GNUNET_YES == pipe_control)
1188 GNUNET_DISK_file_close (childpipe_read);
1193 if (GNUNET_YES == pipe_control)
1195 GNUNET_DISK_file_close (childpipe_write);
1198 GNUNET_DISK_pipe_close (lsocks_pipe);
1199 SetErrnoFromWinError (error_code);
1203 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
1204 gnunet_proc->pid = proc.dwProcessId;
1205 gnunet_proc->handle = proc.hProcess;
1206 gnunet_proc->control_pipe = childpipe_write;
1208 CreateThread (NULL, 64000, &child_wait_thread, (void *) gnunet_proc, 0, NULL);
1210 ResumeThread (proc.hThread);
1211 CloseHandle (proc.hThread);
1213 if ( (NULL == lsocks) || (INVALID_SOCKET == lsocks[0]) )
1216 GNUNET_DISK_pipe_close_end (lsocks_pipe, GNUNET_DISK_PIPE_END_READ);
1218 /* This is a replacement for "goto error" that doesn't use goto */
1227 /* Tell the number of sockets */
1228 for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++);
1230 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1231 if (sizeof (count) != wrote)
1233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1234 "Failed to write %u count bytes to the child: %lu\n",
1235 sizeof (count), GetLastError ());
1238 for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++)
1240 WSAPROTOCOL_INFOA pi;
1241 /* Get a socket duplication info */
1242 if (SOCKET_ERROR == WSADuplicateSocketA (lsocks[i], gnunet_proc->pid, &pi))
1244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1245 "Failed to duplicate an socket[%u]: %lu\n", i,
1249 /* Synchronous I/O is not nice, but we can't schedule this:
1250 * lsocks will be closed/freed by the caller soon, and until
1251 * the child creates a duplicate, closing a socket here will
1252 * close it for good.
1254 /* Send the size of the structure
1255 * (the child might be built with different headers...)
1258 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &size, sizeof (size));
1259 if (sizeof (size) != wrote)
1261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1262 "Failed to write %u size[%u] bytes to the child: %lu\n",
1263 sizeof (size), i, GetLastError ());
1266 /* Finally! Send the data */
1267 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &pi, sizeof (pi));
1268 if (sizeof (pi) != wrote)
1270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1271 "Failed to write %u socket[%u] bytes to the child: %lu\n",
1272 sizeof (pi), i, GetLastError ());
1276 /* This will block us until the child makes a final read or closes
1277 * the pipe (hence no 'wrote' check), since we have to wait for it
1278 * to duplicate the last socket, before we return and start closing
1281 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1286 GNUNET_DISK_file_sync (lsocks_write_fd);
1287 GNUNET_DISK_pipe_close (lsocks_pipe);
1291 /* If we can't pass on the socket(s), the child will block forever,
1292 * better put it out of its misery.
1294 SafeTerminateProcess (gnunet_proc->handle, 0, 0);
1295 CloseHandle (gnunet_proc->handle);
1296 if (NULL != gnunet_proc->control_pipe)
1297 GNUNET_DISK_file_close (gnunet_proc->control_pipe);
1298 GNUNET_free (gnunet_proc);
1309 * @param pipe_control should a pipe be used to send signals to the child?
1310 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1311 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1312 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1313 * @param pipe_stderr pipe to use to get output from child process (or NULL)
1314 * @param filename name of the binary
1315 * @param argv NULL-terminated array of arguments to the process
1316 * @return pointer to process structure of the new process, NULL on error
1318 struct GNUNET_OS_Process *
1319 GNUNET_OS_start_process_vap (int pipe_control,
1320 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1321 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1322 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1323 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1324 const char *filename,
1327 return start_process (pipe_control,
1341 * @param pipe_control should a pipe be used to send signals to the child?
1342 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1343 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1344 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1345 * @param pipe_stderr pipe to use to get output from child process (or NULL)
1346 * @param filename name of the binary
1347 * @param va NULL-terminated list of arguments to the process
1348 * @return pointer to process structure of the new process, NULL on error
1350 struct GNUNET_OS_Process *
1351 GNUNET_OS_start_process_va (int pipe_control,
1352 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1353 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1354 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1355 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1356 const char *filename, va_list va)
1358 struct GNUNET_OS_Process *ret;
1365 while (NULL != va_arg (ap, char *))
1368 argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
1371 while (NULL != (argv[argc] = va_arg (ap, char *)))
1374 ret = GNUNET_OS_start_process_vap (pipe_control,
1389 * @param pipe_control should a pipe be used to send signals to the child?
1390 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1391 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1392 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1393 * @param filename name of the binary
1394 * @param ... NULL-terminated list of arguments to the process
1395 * @return pointer to process structure of the new process, NULL on error
1397 struct GNUNET_OS_Process *
1398 GNUNET_OS_start_process (int pipe_control,
1399 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1400 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1401 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1402 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1403 const char *filename, ...)
1405 struct GNUNET_OS_Process *ret;
1408 va_start (ap, filename);
1409 ret = GNUNET_OS_start_process_va (pipe_control,
1424 * @param pipe_control should a pipe be used to send signals to the child?
1425 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
1426 * std handles of the parent are inherited by the child.
1427 * pipe_stdin and pipe_stdout take priority over std_inheritance
1428 * (when they are non-NULL).
1429 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1430 * must be NULL on platforms where dup is not supported
1431 * @param filename name of the binary
1432 * @param argv NULL-terminated list of arguments to the process
1433 * @return process ID of the new process, -1 on error
1435 struct GNUNET_OS_Process *
1436 GNUNET_OS_start_process_v (int pipe_control,
1437 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1438 const SOCKTYPE *lsocks,
1439 const char *filename,
1442 return start_process (pipe_control,
1454 * Start a process. This function is similar to the GNUNET_OS_start_process_*
1455 * except that the filename and arguments can have whole strings which contain
1456 * the arguments. These arguments are to be separated by spaces and are parsed
1457 * in the order they appear. Arguments containing spaces can be used by
1458 * quoting them with @em ".
1460 * @param pipe_control should a pipe be used to send signals to the child?
1461 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1462 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1463 * must be NULL on platforms where dup is not supported
1464 * @param filename name of the binary. It is valid to have the arguments
1465 * in this string when they are separated by spaces.
1466 * @param ... more arguments. Should be of type `char *`. It is valid
1467 * to have the arguments in these strings when they are separated by
1468 * spaces. The last argument MUST be NULL.
1469 * @return pointer to process structure of the new process, NULL on error
1471 struct GNUNET_OS_Process *
1472 GNUNET_OS_start_process_s (int pipe_control,
1473 unsigned int std_inheritance,
1474 const SOCKTYPE * lsocks,
1475 const char *filename, ...)
1479 unsigned int argv_size;
1485 struct GNUNET_OS_Process *proc;
1492 va_start (ap, filename);
1499 while ('\0' != *rpos)
1508 if ( (' ' == *rpos) && (0 == quote_on) )
1514 while (' ' == *rpos)
1517 if ( (NULL == last) && ('\0' != *rpos) ) // FIXME: == or !=?
1525 while (NULL != (arg = (va_arg (ap, const char*))));
1528 argv = GNUNET_malloc (argv_size * sizeof (char *));
1530 va_start (ap, filename);
1535 cp = GNUNET_strdup (arg);
1538 while ('\0' != *pos)
1547 if ( (' ' == *pos) && (0 == quote_on) )
1551 argv[argv_size++] = GNUNET_strdup (last);
1557 if ( (NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
1563 argv[argv_size++] = GNUNET_strdup (last);
1567 while (NULL != (arg = (va_arg (ap, const char*))));
1569 argv[argv_size] = NULL;
1571 for(i = 0; i < argv_size; i++)
1573 len = strlen (argv[i]);
1574 if ( (argv[i][0] == '"') && (argv[i][len-1] == '"'))
1576 memmove (&argv[i][0], &argv[i][1], len - 2);
1577 argv[i][len-2] = '\0';
1580 binary_path = argv[0];
1581 proc = GNUNET_OS_start_process_v (pipe_control, std_inheritance, lsocks,
1583 while (argv_size > 0)
1584 GNUNET_free (argv[--argv_size]);
1591 * Retrieve the status of a process, waiting on him if dead.
1592 * Nonblocking version.
1594 * @param proc process ID
1595 * @param type status type
1596 * @param code return code/signal number
1597 * @param options WNOHANG if non-blocking is desired
1598 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1601 process_status (struct GNUNET_OS_Process *proc,
1602 enum GNUNET_OS_ProcessStatusType *type,
1603 unsigned long *code,
1610 GNUNET_assert (0 != proc);
1611 ret = waitpid (proc->pid, &status, options);
1614 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1616 return GNUNET_SYSERR;
1620 *type = GNUNET_OS_PROCESS_RUNNING;
1624 if (proc->pid != ret)
1626 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1627 return GNUNET_SYSERR;
1629 if (WIFEXITED (status))
1631 *type = GNUNET_OS_PROCESS_EXITED;
1632 *code = WEXITSTATUS (status);
1634 else if (WIFSIGNALED (status))
1636 *type = GNUNET_OS_PROCESS_SIGNALED;
1637 *code = WTERMSIG (status);
1639 else if (WIFSTOPPED (status))
1641 *type = GNUNET_OS_PROCESS_SIGNALED;
1642 *code = WSTOPSIG (status);
1645 else if (WIFCONTINUED (status))
1647 *type = GNUNET_OS_PROCESS_RUNNING;
1653 *type = GNUNET_OS_PROCESS_UNKNOWN;
1658 #define WNOHANG 42 /* just a flag for W32, purely internal at this point */
1662 DWORD c, error_code, ret;
1666 if (h == NULL || ret == 0)
1668 LOG (GNUNET_ERROR_TYPE_WARNING,
1669 "Invalid process information {%d, %08X}\n",
1671 return GNUNET_SYSERR;
1674 h = GetCurrentProcess ();
1676 if (WNOHANG != options)
1678 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1680 SetErrnoFromWinError (GetLastError ());
1681 return GNUNET_SYSERR;
1685 ret = GetExitCodeProcess (h, &c);
1686 error_code = GetLastError ();
1687 if (ret == 0 || error_code != NO_ERROR)
1689 SetErrnoFromWinError (error_code);
1690 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
1691 return GNUNET_SYSERR;
1693 if (STILL_ACTIVE == c)
1695 *type = GNUNET_OS_PROCESS_RUNNING;
1699 *type = GNUNET_OS_PROCESS_EXITED;
1708 * Retrieve the status of a process, waiting on him if dead.
1709 * Nonblocking version.
1711 * @param proc process ID
1712 * @param type status type
1713 * @param code return code/signal number
1714 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1717 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1718 enum GNUNET_OS_ProcessStatusType *type,
1719 unsigned long *code)
1721 return process_status (proc,
1729 * Retrieve the status of a process, waiting on him if dead.
1732 * @param proc pointer to process structure
1733 * @param type status type
1734 * @param code return code/signal number
1735 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1738 GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc,
1739 enum GNUNET_OS_ProcessStatusType *type,
1740 unsigned long *code)
1742 return process_status (proc,
1750 * Wait for a process to terminate. The return code is discarded.
1751 * You must not use #GNUNET_OS_process_status() on the same process
1752 * after calling this function! This function is blocking and should
1753 * thus only be used if the child process is known to have terminated
1754 * or to terminate very soon.
1756 * @param proc pointer to process structure
1757 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1760 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1763 pid_t pid = proc->pid;
1766 while ( (pid != (ret = waitpid (pid, NULL, 0))) &&
1767 (EINTR == errno) ) ;
1770 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1772 return GNUNET_SYSERR;
1781 LOG (GNUNET_ERROR_TYPE_WARNING,
1782 "Invalid process information {%d, %08X}\n",
1784 return GNUNET_SYSERR;
1787 h = GetCurrentProcess ();
1789 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1791 SetErrnoFromWinError (GetLastError ());
1792 return GNUNET_SYSERR;
1800 * Handle to a command.
1802 struct GNUNET_OS_CommandHandle
1808 struct GNUNET_OS_Process *eip;
1811 * Handle to the output pipe.
1813 struct GNUNET_DISK_PipeHandle *opipe;
1816 * Read-end of output pipe.
1818 const struct GNUNET_DISK_FileHandle *r;
1821 * Function to call on each line of output.
1823 GNUNET_OS_LineProcessor proc;
1826 * Closure for @e proc.
1831 * Buffer for the output.
1836 * Task reading from pipe.
1838 struct GNUNET_SCHEDULER_Task *rtask;
1843 struct GNUNET_TIME_Absolute timeout;
1846 * Current read offset in buf.
1853 * Stop/kill a command. Must ONLY be called either from
1854 * the callback after 'NULL' was passed for 'line' *OR*
1855 * from an independent task (not within the line processor).
1857 * @param cmd handle to the process
1860 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
1862 if (NULL != cmd->proc)
1864 GNUNET_assert (NULL != cmd->rtask);
1865 GNUNET_SCHEDULER_cancel (cmd->rtask);
1867 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1868 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
1869 GNUNET_OS_process_destroy (cmd->eip);
1870 GNUNET_DISK_pipe_close (cmd->opipe);
1876 * Read from the process and call the line processor.
1878 * @param cls the `struct GNUNET_OS_CommandHandle *`
1881 cmd_read (void *cls)
1883 struct GNUNET_OS_CommandHandle *cmd = cls;
1884 const struct GNUNET_SCHEDULER_TaskContext *tc;
1885 GNUNET_OS_LineProcessor proc;
1890 tc = GNUNET_SCHEDULER_get_task_context ();
1892 GNUNET_NETWORK_fdset_handle_isset (tc->read_ready,
1898 proc (cmd->proc_cls, NULL);
1901 ret = GNUNET_DISK_file_read (cmd->r,
1902 &cmd->buf[cmd->off],
1903 sizeof (cmd->buf) - cmd->off);
1906 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
1908 cmd->buf[cmd->off] = '\0';
1909 cmd->proc (cmd->proc_cls, cmd->buf);
1913 proc (cmd->proc_cls, NULL);
1916 end = memchr (&cmd->buf[cmd->off], '\n', ret);
1921 cmd->proc (cmd->proc_cls, cmd->buf);
1922 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1923 cmd->off -= (end + 1 - cmd->buf);
1924 end = memchr (cmd->buf, '\n', cmd->off);
1927 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
1935 * Run the given command line and call the given function
1936 * for each line of the output.
1938 * @param proc function to call for each line of the output
1939 * @param proc_cls closure for @a proc
1940 * @param timeout when to time out
1941 * @param binary command to run
1942 * @param ... arguments to command
1943 * @return NULL on error
1945 struct GNUNET_OS_CommandHandle *
1946 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
1948 struct GNUNET_TIME_Relative timeout,
1952 struct GNUNET_OS_CommandHandle *cmd;
1953 struct GNUNET_OS_Process *eip;
1954 struct GNUNET_DISK_PipeHandle *opipe;
1957 opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES,
1958 GNUNET_NO, GNUNET_YES);
1961 va_start (ap, binary);
1962 /* redirect stdout, don't inherit stderr/stdin */
1963 eip = GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL,
1964 opipe, NULL, binary,
1969 GNUNET_DISK_pipe_close (opipe);
1972 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
1973 cmd = GNUNET_new (struct GNUNET_OS_CommandHandle);
1974 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1978 cmd->proc_cls = proc_cls;
1979 cmd->r = GNUNET_DISK_pipe_handle (opipe,
1980 GNUNET_DISK_PIPE_END_READ);
1981 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout,
1989 /* end of os_priority.c */