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;
70 * This handler is called when there are control data to be read on the pipe
72 * @param cls the 'struct GNUNET_DISK_FileHandle' of the control pipe
75 parent_control_handler (void *cls)
77 struct GNUNET_DISK_FileHandle *control_pipe = cls;
78 const struct GNUNET_SCHEDULER_TaskContext *tc;
83 tc = GNUNET_SCHEDULER_get_task_context ();
84 LOG (GNUNET_ERROR_TYPE_DEBUG,
85 "`%s' invoked because of %d\n", __FUNCTION__,
87 if (0 != (tc->reason &
88 (GNUNET_SCHEDULER_REASON_SHUTDOWN | GNUNET_SCHEDULER_REASON_TIMEOUT)))
90 GNUNET_DISK_file_close (control_pipe);
94 ret = GNUNET_DISK_file_read (control_pipe, &sig, sizeof (sig));
95 if (sizeof (sig) != ret)
98 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
99 LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing control pipe\n");
100 GNUNET_DISK_file_close (control_pipe);
104 pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE);
105 GNUNET_assert ( (NULL == pipe_fd) || (strlen (pipe_fd) <= 0) );
106 LOG (GNUNET_ERROR_TYPE_DEBUG,
107 "Got control code %d from parent via pipe %s\n", sig, pipe_fd);
108 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
109 control_pipe, &parent_control_handler,
111 GNUNET_SIGNAL_raise ((int) sig);
116 * Task that connects this process to its parent via pipe;
117 * essentially, the parent control handler will read signal numbers
118 * from the 'GNUNET_OS_CONTROL_PIPE' (as given in an environment
119 * variable) and raise those signals.
121 * @param cls closure (unused)
124 GNUNET_OS_install_parent_control_handler (void *cls)
128 struct GNUNET_DISK_FileHandle *control_pipe;
131 env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
132 if ( (NULL == env_buf) || (strlen (env_buf) <= 0) )
134 LOG (GNUNET_ERROR_TYPE_DEBUG,
135 "Not installing a handler because $%s is empty\n",
136 GNUNET_OS_CONTROL_PIPE);
137 putenv (GNUNET_OS_CONTROL_PIPE "=");
141 pipe_fd = strtoull (env_buf, &env_buf_end, 16);
142 if ((0 != errno) || (env_buf == env_buf_end))
144 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "strtoull", env_buf);
145 putenv (GNUNET_OS_CONTROL_PIPE "=");
148 #if !defined (WINDOWS)
149 if (pipe_fd >= FD_SETSIZE)
151 if ((FILE_TYPE_UNKNOWN == GetFileType ((HANDLE) (uintptr_t) pipe_fd))
152 && (0 != GetLastError ()))
155 LOG (GNUNET_ERROR_TYPE_ERROR,
156 "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n", env_buf);
157 putenv (GNUNET_OS_CONTROL_PIPE "=");
161 control_pipe = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) (uintptr_t) pipe_fd);
163 control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
165 if (NULL == control_pipe)
167 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "open", env_buf);
168 putenv (GNUNET_OS_CONTROL_PIPE "=");
171 LOG (GNUNET_ERROR_TYPE_DEBUG,
172 "Adding parent control handler pipe `%s' to the scheduler\n", env_buf);
173 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, control_pipe,
174 &parent_control_handler, control_pipe);
175 putenv (GNUNET_OS_CONTROL_PIPE "=");
180 * Get process structure for current process
182 * The pointer it returns points to static memory location and must not be
185 * @return pointer to the process sturcutre for this process
187 struct GNUNET_OS_Process *
188 GNUNET_OS_process_current ()
191 current_process.pid = GetCurrentProcessId ();
192 current_process.handle = GetCurrentProcess ();
194 current_process.pid = 0;
196 return ¤t_process;
201 * Sends a signal to the process
203 * @param proc pointer to process structure
205 * @return 0 on success, -1 on error
208 GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
214 if (NULL != proc->control_pipe)
216 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending signal %d to pid: %u via pipe\n", sig, proc->pid);
217 ret = GNUNET_DISK_file_write (proc->control_pipe, &csig, sizeof (csig));
218 if (sizeof (csig) == ret)
221 /* pipe failed or non-existent, try other methods */
224 #if !defined (WINDOWS)
230 #if (SIGTERM != GNUNET_TERM_SIG)
231 case GNUNET_TERM_SIG:
233 #if defined(WINDOWS) && !defined(__CYGWIN__)
236 int must_kill = GNUNET_YES;
237 if (0 != GetExitCodeProcess (proc->handle, &exitcode))
238 must_kill = (exitcode == STILL_ACTIVE) ? GNUNET_YES : GNUNET_NO;
239 if (GNUNET_YES == must_kill)
240 if (0 == SafeTerminateProcess (proc->handle, 0, 0))
242 DWORD error_code = GetLastError ();
243 if ((error_code != WAIT_TIMEOUT) && (error_code != ERROR_PROCESS_ABORTED))
245 LOG ((error_code == ERROR_ACCESS_DENIED) ?
246 GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
247 "SafeTermiateProcess failed with code %lu\n", error_code);
248 /* The problem here is that a process that is already dying
249 * might cause SafeTerminateProcess to fail with
250 * ERROR_ACCESS_DENIED, but the process WILL die eventually.
251 * If we really had a permissions problem, hanging up (which
252 * is what will happen in process_wait() in that case) is
255 if (ERROR_ACCESS_DENIED == error_code)
261 SetErrnoFromWinError (error_code);
269 LOG (GNUNET_ERROR_TYPE_DEBUG,
270 "Sending signal %d to pid: %u via system call\n",
273 return PLIBC_KILL (proc->pid, sig);
276 #if defined (WINDOWS)
280 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending signal %d to pid: %u via system call\n", sig, proc->pid);
281 return PLIBC_KILL (proc->pid, sig);
287 * Get the pid of the process in question
289 * @param proc the process to get the pid of
291 * @return the current process id
294 GNUNET_OS_process_get_pid (struct GNUNET_OS_Process * proc)
301 * Cleans up process structure contents (OS-dependent) and deallocates it
303 * @param proc pointer to process structure
306 GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
308 if (NULL != proc->control_pipe)
309 GNUNET_DISK_file_close (proc->control_pipe);
310 #if defined (WINDOWS)
311 if (NULL != proc->handle)
312 CloseHandle (proc->handle);
318 #include "gnunet_signal_lib.h"
320 extern GNUNET_SIGNAL_Handler w32_sigchld_handler;
323 * Make seaspider happy.
325 #define DWORD_WINAPI DWORD WINAPI
328 * @brief Waits for a process to terminate and invokes the SIGCHLD handler
329 * @param proc pointer to process structure
332 child_wait_thread (void *arg)
334 struct GNUNET_OS_Process *proc = (struct GNUNET_OS_Process *) arg;
336 WaitForSingleObject (proc->handle, INFINITE);
338 if (w32_sigchld_handler)
339 w32_sigchld_handler ();
348 CreateCustomEnvTable (char **vars)
350 char *win32_env_table;
355 size_t tablesize = 0;
356 size_t items_count = 0;
365 win32_env_table = GetEnvironmentStringsA ();
366 if (NULL == win32_env_table)
368 for (c = 0, var_ptr = vars; *var_ptr; var_ptr += 2, c++) ;
370 index = GNUNET_malloc (sizeof (char *) * n_var);
371 for (c = 0; c < n_var; c++)
373 for (items_count = 0, ptr = win32_env_table; ptr[0] != 0; items_count++)
375 size_t len = strlen (ptr);
378 for (var_ptr = vars; *var_ptr; var_ptr++)
382 var_len = strlen (var);
383 if (strncmp (var, ptr, var_len) == 0)
387 tablesize += var_len + strlen (val) + 1;
392 tablesize += len + 1;
395 for (n_found = 0, c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
400 n_found += strlen (var) + strlen (val) + 1;
402 result = GNUNET_malloc (tablesize + n_found + 1);
403 for (result_ptr = result, ptr = win32_env_table; ptr[0] != 0;)
405 size_t len = strlen (ptr);
408 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
412 var_len = strlen (var);
413 if (strncmp (var, ptr, var_len) == 0)
421 strcpy (result_ptr, ptr);
422 result_ptr += len + 1;
426 strcpy (result_ptr, var);
427 result_ptr += var_len;
428 strcpy (result_ptr, val);
429 result_ptr += strlen (val) + 1;
433 for (c = 0, var_ptr = vars; *var_ptr; var_ptr++, c++)
437 var_len = strlen (var);
440 strcpy (result_ptr, var);
441 result_ptr += var_len;
442 strcpy (result_ptr, val);
443 result_ptr += strlen (val) + 1;
446 FreeEnvironmentStrings (win32_env_table);
455 * Open '/dev/null' and make the result the given
458 * @param target_fd desired FD to point to /dev/null
459 * @param flags open flags (O_RDONLY, O_WRONLY)
462 open_dev_null (int target_fd,
467 fd = open ("/dev/null", flags);
470 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", "/dev/null");
475 if (-1 == dup2 (fd, target_fd))
477 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
481 GNUNET_break (0 == close (fd));
489 * @param pipe_control should a pipe be used to send signals to the child?
490 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
491 * std handles of the parent are inherited by the child.
492 * pipe_stdin and pipe_stdout take priority over std_inheritance
493 * (when they are non-NULL).
494 * @param pipe_stdin pipe to use to send input to child process (or NULL)
495 * @param pipe_stdout pipe to use to get output from child process (or NULL)
496 * @param pipe_stderr pipe to use for stderr for child process (or NULL)
497 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
498 * must be NULL on platforms where dup is not supported
499 * @param filename name of the binary
500 * @param argv NULL-terminated list of arguments to the process
501 * @return process ID of the new process, -1 on error
503 static struct GNUNET_OS_Process *
504 start_process (int pipe_control,
505 enum GNUNET_OS_InheritStdioFlags std_inheritance,
506 struct GNUNET_DISK_PipeHandle *pipe_stdin,
507 struct GNUNET_DISK_PipeHandle *pipe_stdout,
508 struct GNUNET_DISK_PipeHandle *pipe_stderr,
509 const SOCKTYPE *lsocks,
510 const char *filename,
516 struct GNUNET_OS_Process *gnunet_proc;
517 struct GNUNET_DISK_FileHandle *childpipe_read;
518 struct GNUNET_DISK_FileHandle *childpipe_write;
519 int childpipe_read_fd;
534 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
535 return NULL; /* not executable */
536 if (GNUNET_YES == pipe_control)
538 struct GNUNET_DISK_PipeHandle *childpipe;
539 int dup_childpipe_read_fd = -1;
541 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
542 if (NULL == childpipe)
544 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
545 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
546 GNUNET_DISK_pipe_close (childpipe);
547 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
548 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
549 &childpipe_read_fd, sizeof (int))) ||
550 (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
552 if (NULL != childpipe_read)
553 GNUNET_DISK_file_close (childpipe_read);
554 if (NULL != childpipe_write)
555 GNUNET_DISK_file_close (childpipe_write);
556 if (0 <= dup_childpipe_read_fd)
557 close (dup_childpipe_read_fd);
560 childpipe_read_fd = dup_childpipe_read_fd;
561 GNUNET_DISK_file_close (childpipe_read);
565 childpipe_write = NULL;
566 childpipe_read_fd = -1;
568 if (NULL != pipe_stdin)
570 GNUNET_assert (GNUNET_OK ==
571 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
572 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
573 &fd_stdin_read, sizeof (int)));
574 GNUNET_assert (GNUNET_OK ==
575 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
576 (pipe_stdin, GNUNET_DISK_PIPE_END_WRITE),
577 &fd_stdin_write, sizeof (int)));
579 if (NULL != pipe_stdout)
581 GNUNET_assert (GNUNET_OK ==
582 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
584 GNUNET_DISK_PIPE_END_WRITE),
585 &fd_stdout_write, sizeof (int)));
586 GNUNET_assert (GNUNET_OK ==
587 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
588 (pipe_stdout, GNUNET_DISK_PIPE_END_READ),
589 &fd_stdout_read, sizeof (int)));
591 if (NULL != pipe_stderr)
593 GNUNET_assert (GNUNET_OK ==
594 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
596 GNUNET_DISK_PIPE_END_READ),
597 &fd_stderr_read, sizeof (int)));
598 GNUNET_assert (GNUNET_OK ==
599 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
601 GNUNET_DISK_PIPE_END_WRITE),
602 &fd_stderr_write, sizeof (int)));
609 while (-1 != (k = lsocks[i++]))
610 GNUNET_array_append (lscp, ls, k);
611 GNUNET_array_append (lscp, ls, -1);
614 /* see https://gnunet.org/vfork */
622 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
623 GNUNET_array_grow (lscp, ls, 0);
624 if (NULL != childpipe_write)
625 GNUNET_DISK_file_close (childpipe_write);
626 if (0 <= childpipe_read_fd)
627 close (childpipe_read_fd);
633 unsetenv (GNUNET_OS_CONTROL_PIPE);
634 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
635 gnunet_proc->pid = ret;
636 gnunet_proc->control_pipe = childpipe_write;
637 if (GNUNET_YES == pipe_control)
639 close (childpipe_read_fd);
641 GNUNET_array_grow (lscp, ls, 0);
644 if (0 <= childpipe_read_fd)
648 /* due to vfork, we must NOT free memory on DARWIN! */
649 GNUNET_DISK_file_close (childpipe_write);
651 snprintf (fdbuf, 100, "%x", childpipe_read_fd);
652 setenv (GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
655 unsetenv (GNUNET_OS_CONTROL_PIPE);
656 if (NULL != pipe_stdin)
658 GNUNET_break (0 == close (fd_stdin_write));
659 if (-1 == dup2 (fd_stdin_read, 0))
660 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
661 GNUNET_break (0 == close (fd_stdin_read));
663 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
665 GNUNET_break (0 == close (0));
666 open_dev_null (0, O_RDONLY);
668 if (NULL != pipe_stdout)
670 GNUNET_break (0 == close (fd_stdout_read));
671 if (-1 == dup2 (fd_stdout_write, 1))
672 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
673 GNUNET_break (0 == close (fd_stdout_write));
675 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
677 GNUNET_break (0 == close (1));
678 open_dev_null (1, O_WRONLY);
680 if (NULL != pipe_stderr)
682 GNUNET_break (0 == close (fd_stderr_read));
683 if (-1 == dup2 (fd_stderr_write, 2))
684 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
685 GNUNET_break (0 == close (fd_stderr_write));
687 else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
689 GNUNET_break (0 == close (2));
690 open_dev_null (2, O_WRONLY);
694 /* read systemd documentation... */
697 while (-1 != lscp[i])
700 while (-1 != lscp[j])
706 GNUNET_assert (-1 != k);
707 GNUNET_assert (0 == close (lscp[j]));
715 /* Bury any existing FD, no matter what; they should all be closed
716 * on exec anyway and the important onces have been dup'ed away */
718 GNUNET_assert (-1 != dup2 (lscp[i], tgt));
720 /* unset close-on-exec flag */
721 flags = fcntl (tgt, F_GETFD);
722 GNUNET_assert (flags >= 0);
723 flags &= ~FD_CLOEXEC;
725 (void) fcntl (tgt, F_SETFD, flags);
729 GNUNET_snprintf (fds, sizeof (fds), "%u", i);
730 setenv ("LISTEN_FDS", fds, 1);
733 /* due to vfork, we must NOT free memory on DARWIN! */
734 GNUNET_array_grow (lscp, ls, 0);
736 execvp (filename, argv);
737 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "execvp", filename);
740 struct GNUNET_DISK_FileHandle *childpipe_read;
741 struct GNUNET_DISK_FileHandle *childpipe_write;
742 HANDLE childpipe_read_handle;
744 char **non_const_argv;
749 PROCESS_INFORMATION proc;
751 struct GNUNET_OS_Process *gnunet_proc;
752 char path[MAX_PATH + 1];
753 char *our_env[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
754 char *env_block = NULL;
762 char *non_const_filename;
763 char win_path[MAX_PATH + 1];
764 struct GNUNET_DISK_PipeHandle *lsocks_pipe;
765 const struct GNUNET_DISK_FileHandle *lsocks_write_fd;
776 HANDLE stdout_handle;
777 HANDLE stdih, stdoh, stdeh;
778 DWORD stdif, stdof, stdef;
781 DWORD create_no_window;
783 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary (filename, GNUNET_NO, NULL))
784 return NULL; /* not executable */
786 /* Search in prefix dir (hopefully - the directory from which
787 * the current module was loaded), bindir and libdir, then in PATH
789 self_prefix = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_SELF_PREFIX);
790 bindir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
791 libdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
793 pathbuf_len = GetEnvironmentVariableA ("PATH", (char *) &pathbuf, 0);
796 pathbuf_len + 1 + strlen (self_prefix) + 1 + strlen (bindir) + 1 +
799 pathbuf = GNUNET_malloc (alloc_len * sizeof (char));
802 ptr += sprintf (pathbuf, "%s;%s;%s;", self_prefix, bindir, libdir);
803 GNUNET_free (self_prefix);
804 GNUNET_free (bindir);
805 GNUNET_free (libdir);
807 alloc_len = GetEnvironmentVariableA ("PATH", ptr, pathbuf_len);
808 if (alloc_len != pathbuf_len - 1)
810 GNUNET_free (pathbuf);
811 errno = ENOSYS; /* PATH changed on the fly. What kind of error is that? */
815 cmdlen = strlen (filename);
816 if ( (cmdlen < 5) || (0 != strcmp (&filename[cmdlen - 4], ".exe")) )
817 GNUNET_asprintf (&non_const_filename, "%s.exe", filename);
819 GNUNET_asprintf (&non_const_filename, "%s", filename);
821 /* It could be in POSIX form, convert it to a DOS path early on */
822 if (ERROR_SUCCESS != (lRet = plibc_conv_to_win_path (non_const_filename, win_path)))
824 SetErrnoFromWinError (lRet);
825 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "plibc_conv_to_win_path",
827 GNUNET_free (non_const_filename);
828 GNUNET_free (pathbuf);
831 GNUNET_free (non_const_filename);
832 non_const_filename = GNUNET_strdup (win_path);
833 /* Check that this is the full path. If it isn't, search. */
834 /* FIXME: convert it to wchar_t and use SearchPathW?
835 * Remember: arguments to _start_process() are technically in UTF-8...
837 if (non_const_filename[1] == ':')
839 snprintf (path, sizeof (path) / sizeof (char), "%s", non_const_filename);
840 LOG (GNUNET_ERROR_TYPE_DEBUG,
841 "Using path `%s' as-is. PATH is %s\n", path, ptr);
843 else if (!SearchPathA
844 (pathbuf, non_const_filename, NULL, sizeof (path) / sizeof (char),
847 SetErrnoFromWinError (GetLastError ());
848 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "SearchPath",
850 GNUNET_free (non_const_filename);
851 GNUNET_free (pathbuf);
855 LOG (GNUNET_ERROR_TYPE_DEBUG,
856 "Found `%s' in PATH `%s'\n", path, pathbuf);
857 GNUNET_free (pathbuf);
858 GNUNET_free (non_const_filename);
860 /* Count the number of arguments */
861 arg = (char **) argv;
868 /* Allocate a copy argv */
869 non_const_argv = GNUNET_malloc (sizeof (char *) * (argcount + 1));
871 /* Copy all argv strings */
873 arg = (char **) argv;
877 non_const_argv[argcount] = GNUNET_strdup (path);
879 non_const_argv[argcount] = GNUNET_strdup (*arg);
883 non_const_argv[argcount] = NULL;
887 arg = non_const_argv;
890 cmdlen = cmdlen + strlen (*arg) + 4;
894 /* Allocate and create cmd */
895 cmd = idx = GNUNET_malloc (sizeof (char) * cmdlen);
896 arg = non_const_argv;
899 char arg_last_char = (*arg)[strlen (*arg) - 1];
900 idx += sprintf (idx, "\"%s%s\"%s", *arg,
901 arg_last_char == '\\' ? "\\" : "", *(arg + 1) ? " " : "");
906 GNUNET_free (non_const_argv[--argcount]);
907 GNUNET_free (non_const_argv);
909 memset (&start, 0, sizeof (start));
910 start.cb = sizeof (start);
911 if ((pipe_stdin != NULL) || (pipe_stdout != NULL) || (std_inheritance != 0))
912 start.dwFlags |= STARTF_USESTDHANDLES;
914 stdih = GetStdHandle (STD_INPUT_HANDLE);
915 GetHandleInformation (stdih, &stdif);
916 if (pipe_stdin != NULL)
918 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
919 (pipe_stdin, GNUNET_DISK_PIPE_END_READ),
920 &stdin_handle, sizeof (HANDLE));
921 start.hStdInput = stdin_handle;
925 if (std_inheritance & GNUNET_OS_INHERIT_STD_IN)
927 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 1);
928 if (pipe_stdin == NULL)
929 start.hStdInput = stdih;
932 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, 0);
936 stdoh = GetStdHandle (STD_OUTPUT_HANDLE);
937 GetHandleInformation (stdoh, &stdof);
938 if (NULL != pipe_stdout)
940 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
942 GNUNET_DISK_PIPE_END_WRITE),
943 &stdout_handle, sizeof (HANDLE));
944 start.hStdOutput = stdout_handle;
948 if (std_inheritance & GNUNET_OS_INHERIT_STD_OUT)
950 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 1);
951 if (pipe_stdout == NULL)
952 start.hStdOutput = stdoh;
955 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, 0);
958 stdeh = GetStdHandle (STD_ERROR_HANDLE);
959 GetHandleInformation (stdeh, &stdef);
962 if (std_inheritance & GNUNET_OS_INHERIT_STD_ERR)
964 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 1);
965 start.hStdError = stdeh;
968 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, 0);
971 if (GNUNET_YES == pipe_control)
973 struct GNUNET_DISK_PipeHandle *childpipe;
974 childpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO);
975 if (NULL == childpipe)
977 childpipe_read = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_READ);
978 childpipe_write = GNUNET_DISK_pipe_detach_end (childpipe, GNUNET_DISK_PIPE_END_WRITE);
979 GNUNET_DISK_pipe_close (childpipe);
980 if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
981 (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
982 &childpipe_read_handle, sizeof (HANDLE))))
985 GNUNET_DISK_file_close (childpipe_read);
987 GNUNET_DISK_file_close (childpipe_write);
991 /* Unlike *nix variant, we don't dup the handle, so can't close
992 * filehandle right now.
994 SetHandleInformation (childpipe_read_handle, HANDLE_FLAG_INHERIT, 1);
998 childpipe_read = NULL;
999 childpipe_write = NULL;
1002 if (lsocks != NULL && lsocks[0] != INVALID_SOCKET)
1004 lsocks_pipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1006 if (lsocks_pipe == NULL)
1009 GNUNET_DISK_pipe_close (lsocks_pipe);
1010 if (GNUNET_YES == pipe_control)
1012 GNUNET_DISK_file_close (childpipe_write);
1013 GNUNET_DISK_file_close (childpipe_read);
1017 lsocks_write_fd = GNUNET_DISK_pipe_handle (lsocks_pipe,
1018 GNUNET_DISK_PIPE_END_WRITE);
1019 GNUNET_DISK_internal_file_handle_ (lsocks_write_fd,
1020 &lsocks_write, sizeof (HANDLE));
1021 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle
1022 (lsocks_pipe, GNUNET_DISK_PIPE_END_READ),
1023 &lsocks_read, sizeof (HANDLE));
1029 if (GNUNET_YES == pipe_control)
1031 GNUNET_asprintf (&our_env[env_off++], "%s=", GNUNET_OS_CONTROL_PIPE);
1032 GNUNET_asprintf (&our_env[env_off++], "%p", childpipe_read_handle);
1034 if ( (lsocks != NULL) && (lsocks[0] != INVALID_SOCKET))
1036 /*This will tell the child that we're going to send lsocks over the pipe*/
1037 GNUNET_asprintf (&our_env[env_off++], "%s=", "GNUNET_OS_READ_LSOCKS");
1038 GNUNET_asprintf (&our_env[env_off++], "%lu", lsocks_read);
1040 our_env[env_off++] = NULL;
1041 env_block = CreateCustomEnvTable (our_env);
1043 GNUNET_free_non_null (our_env[--env_off]);
1046 if (NULL == (wpath = u8_to_u16 ((uint8_t *) path, 1 + strlen (path), NULL, &wpath_len)))
1048 LOG (GNUNET_ERROR_TYPE_DEBUG,
1049 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n", path, errno);
1050 GNUNET_free (env_block);
1053 GNUNET_DISK_pipe_close (lsocks_pipe);
1054 if (GNUNET_YES == pipe_control)
1056 GNUNET_DISK_file_close (childpipe_write);
1057 GNUNET_DISK_file_close (childpipe_read);
1063 if (NULL == (wcmd = u8_to_u16 ((uint8_t *) cmd, 1 + strlen (cmd), NULL, &wcmd_len)))
1065 LOG (GNUNET_ERROR_TYPE_DEBUG,
1066 "Failed to convert `%s' from UTF-8 to UTF-16: %d\n",
1069 GNUNET_free (env_block);
1073 GNUNET_DISK_pipe_close (lsocks_pipe);
1074 if (GNUNET_YES == pipe_control)
1076 GNUNET_DISK_file_close (childpipe_write);
1077 GNUNET_DISK_file_close (childpipe_read);
1082 create_no_window = 0;
1084 HANDLE console_input = CreateFile ("CONIN$", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1085 if (INVALID_HANDLE_VALUE == console_input)
1086 create_no_window = CREATE_NO_WINDOW;
1088 CloseHandle (console_input);
1091 bresult = CreateProcessW (wpath, wcmd, NULL, NULL, GNUNET_YES,
1092 create_no_window | CREATE_SUSPENDED, env_block, NULL, &start, &proc);
1093 error_code = GetLastError ();
1095 if ((NULL == pipe_stdin) && (stdih))
1096 SetHandleInformation (stdih, HANDLE_FLAG_INHERIT, stdif);
1099 if ((NULL == pipe_stdout) && (stdoh))
1100 SetHandleInformation (stdoh, HANDLE_FLAG_INHERIT, stdof);
1103 SetHandleInformation (stdeh, HANDLE_FLAG_INHERIT, stdef);
1106 LOG (GNUNET_ERROR_TYPE_ERROR,
1107 "CreateProcess(%s, %s) failed: %lu\n",
1112 GNUNET_free (env_block);
1116 if (GNUNET_YES == pipe_control)
1118 GNUNET_DISK_file_close (childpipe_read);
1123 if (GNUNET_YES == pipe_control)
1125 GNUNET_DISK_file_close (childpipe_write);
1128 GNUNET_DISK_pipe_close (lsocks_pipe);
1129 SetErrnoFromWinError (error_code);
1133 gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
1134 gnunet_proc->pid = proc.dwProcessId;
1135 gnunet_proc->handle = proc.hProcess;
1136 gnunet_proc->control_pipe = childpipe_write;
1138 CreateThread (NULL, 64000, &child_wait_thread, (void *) gnunet_proc, 0, NULL);
1140 ResumeThread (proc.hThread);
1141 CloseHandle (proc.hThread);
1143 if ( (NULL == lsocks) || (INVALID_SOCKET == lsocks[0]) )
1146 GNUNET_DISK_pipe_close_end (lsocks_pipe, GNUNET_DISK_PIPE_END_READ);
1148 /* This is a replacement for "goto error" that doesn't use goto */
1157 /* Tell the number of sockets */
1158 for (count = 0; lsocks && lsocks[count] != INVALID_SOCKET; count++);
1160 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1161 if (sizeof (count) != wrote)
1163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1164 "Failed to write %u count bytes to the child: %u\n",
1165 sizeof (count), GetLastError ());
1168 for (i = 0; lsocks && lsocks[i] != INVALID_SOCKET; i++)
1170 WSAPROTOCOL_INFOA pi;
1171 /* Get a socket duplication info */
1172 if (SOCKET_ERROR == WSADuplicateSocketA (lsocks[i], gnunet_proc->pid, &pi))
1174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1175 "Failed to duplicate an socket[%llu]: %u\n", i,
1179 /* Synchronous I/O is not nice, but we can't schedule this:
1180 * lsocks will be closed/freed by the caller soon, and until
1181 * the child creates a duplicate, closing a socket here will
1182 * close it for good.
1184 /* Send the size of the structure
1185 * (the child might be built with different headers...)
1188 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &size, sizeof (size));
1189 if (sizeof (size) != wrote)
1191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1192 "Failed to write %u size[%llu] bytes to the child: %u\n",
1193 sizeof (size), i, GetLastError ());
1196 /* Finally! Send the data */
1197 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &pi, sizeof (pi));
1198 if (sizeof (pi) != wrote)
1200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1201 "Failed to write %u socket[%llu] bytes to the child: %u\n",
1202 sizeof (pi), i, GetLastError ());
1206 /* This will block us until the child makes a final read or closes
1207 * the pipe (hence no 'wrote' check), since we have to wait for it
1208 * to duplicate the last socket, before we return and start closing
1211 wrote = GNUNET_DISK_file_write (lsocks_write_fd, &count, sizeof (count));
1216 GNUNET_DISK_file_sync (lsocks_write_fd);
1217 GNUNET_DISK_pipe_close (lsocks_pipe);
1221 /* If we can't pass on the socket(s), the child will block forever,
1222 * better put it out of its misery.
1224 SafeTerminateProcess (gnunet_proc->handle, 0, 0);
1225 CloseHandle (gnunet_proc->handle);
1226 if (NULL != gnunet_proc->control_pipe)
1227 GNUNET_DISK_file_close (gnunet_proc->control_pipe);
1228 GNUNET_free (gnunet_proc);
1239 * @param pipe_control should a pipe be used to send signals to the child?
1240 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1241 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1242 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1243 * @param pipe_stderr pipe to use to get output from child process (or NULL)
1244 * @param filename name of the binary
1245 * @param argv NULL-terminated array of arguments to the process
1246 * @return pointer to process structure of the new process, NULL on error
1248 struct GNUNET_OS_Process *
1249 GNUNET_OS_start_process_vap (int pipe_control,
1250 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1251 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1252 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1253 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1254 const char *filename,
1257 return start_process (pipe_control,
1271 * @param pipe_control should a pipe be used to send signals to the child?
1272 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1273 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1274 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1275 * @param pipe_stderr pipe to use to get output from child process (or NULL)
1276 * @param filename name of the binary
1277 * @param va NULL-terminated list of arguments to the process
1278 * @return pointer to process structure of the new process, NULL on error
1280 struct GNUNET_OS_Process *
1281 GNUNET_OS_start_process_va (int pipe_control,
1282 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1283 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1284 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1285 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1286 const char *filename, va_list va)
1288 struct GNUNET_OS_Process *ret;
1295 while (NULL != va_arg (ap, char *))
1298 argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
1301 while (NULL != (argv[argc] = va_arg (ap, char *)))
1304 ret = GNUNET_OS_start_process_vap (pipe_control,
1319 * @param pipe_control should a pipe be used to send signals to the child?
1320 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags
1321 * @param pipe_stdin pipe to use to send input to child process (or NULL)
1322 * @param pipe_stdout pipe to use to get output from child process (or NULL)
1323 * @param filename name of the binary
1324 * @param ... NULL-terminated list of arguments to the process
1325 * @return pointer to process structure of the new process, NULL on error
1327 struct GNUNET_OS_Process *
1328 GNUNET_OS_start_process (int pipe_control,
1329 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1330 struct GNUNET_DISK_PipeHandle *pipe_stdin,
1331 struct GNUNET_DISK_PipeHandle *pipe_stdout,
1332 struct GNUNET_DISK_PipeHandle *pipe_stderr,
1333 const char *filename, ...)
1335 struct GNUNET_OS_Process *ret;
1338 va_start (ap, filename);
1339 ret = GNUNET_OS_start_process_va (pipe_control,
1354 * @param pipe_control should a pipe be used to send signals to the child?
1355 * @param std_inheritance a set of GNUNET_OS_INHERIT_STD_* flags controlling which
1356 * std handles of the parent are inherited by the child.
1357 * pipe_stdin and pipe_stdout take priority over std_inheritance
1358 * (when they are non-NULL).
1359 * @param lsocks array of listen sockets to dup systemd-style (or NULL);
1360 * must be NULL on platforms where dup is not supported
1361 * @param filename name of the binary
1362 * @param argv NULL-terminated list of arguments to the process
1363 * @return process ID of the new process, -1 on error
1365 struct GNUNET_OS_Process *
1366 GNUNET_OS_start_process_v (int pipe_control,
1367 enum GNUNET_OS_InheritStdioFlags std_inheritance,
1368 const SOCKTYPE *lsocks,
1369 const char *filename,
1372 return start_process (pipe_control,
1384 * Start a process. This function is similar to the GNUNET_OS_start_process_*
1385 * except that the filename and arguments can have whole strings which contain
1386 * the arguments. These arguments are to be separated by spaces and are parsed
1387 * in the order they appear. Arguments containing spaces can be used by
1388 * quoting them with @em ".
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 lsocks array of listen sockets to dup systemd-style (or NULL);
1393 * must be NULL on platforms where dup is not supported
1394 * @param filename name of the binary. It is valid to have the arguments
1395 * in this string when they are separated by spaces.
1396 * @param ... more arguments. Should be of type `char *`. It is valid
1397 * to have the arguments in these strings when they are separated by
1398 * spaces. The last argument MUST be NULL.
1399 * @return pointer to process structure of the new process, NULL on error
1401 struct GNUNET_OS_Process *
1402 GNUNET_OS_start_process_s (int pipe_control,
1403 unsigned int std_inheritance,
1404 const SOCKTYPE * lsocks,
1405 const char *filename, ...)
1409 unsigned int argv_size;
1415 struct GNUNET_OS_Process *proc;
1422 va_start (ap, filename);
1429 while ('\0' != *rpos)
1438 if ( (' ' == *rpos) && (0 == quote_on) )
1444 while (' ' == *rpos)
1447 if ( (NULL == last) && ('\0' != *rpos) ) // FIXME: == or !=?
1455 while (NULL != (arg = (va_arg (ap, const char*))));
1458 argv = GNUNET_malloc (argv_size * sizeof (char *));
1460 va_start (ap, filename);
1465 cp = GNUNET_strdup (arg);
1468 while ('\0' != *pos)
1477 if ( (' ' == *pos) && (0 == quote_on) )
1481 argv[argv_size++] = GNUNET_strdup (last);
1487 if ( (NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
1493 argv[argv_size++] = GNUNET_strdup (last);
1497 while (NULL != (arg = (va_arg (ap, const char*))));
1499 argv[argv_size] = NULL;
1501 for(i = 0; i < argv_size; i++)
1503 len = strlen (argv[i]);
1504 if ( (argv[i][0] == '"') && (argv[i][len-1] == '"'))
1506 memmove (&argv[i][0], &argv[i][1], len - 2);
1507 argv[i][len-2] = '\0';
1510 binary_path = argv[0];
1511 proc = GNUNET_OS_start_process_v (pipe_control, std_inheritance, lsocks,
1513 while (argv_size > 0)
1514 GNUNET_free (argv[--argv_size]);
1521 * Retrieve the status of a process, waiting on him if dead.
1522 * Nonblocking version.
1524 * @param proc process ID
1525 * @param type status type
1526 * @param code return code/signal number
1527 * @return #GNUNET_OK on success, #GNUNET_NO if the process is still running, #GNUNET_SYSERR otherwise
1530 GNUNET_OS_process_status (struct GNUNET_OS_Process *proc,
1531 enum GNUNET_OS_ProcessStatusType *type,
1532 unsigned long *code)
1538 GNUNET_assert (0 != proc);
1539 ret = waitpid (proc->pid, &status, WNOHANG);
1542 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1544 return GNUNET_SYSERR;
1548 *type = GNUNET_OS_PROCESS_RUNNING;
1552 if (proc->pid != ret)
1554 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "waitpid");
1555 return GNUNET_SYSERR;
1557 if (WIFEXITED (status))
1559 *type = GNUNET_OS_PROCESS_EXITED;
1560 *code = WEXITSTATUS (status);
1562 else if (WIFSIGNALED (status))
1564 *type = GNUNET_OS_PROCESS_SIGNALED;
1565 *code = WTERMSIG (status);
1567 else if (WIFSTOPPED (status))
1569 *type = GNUNET_OS_PROCESS_SIGNALED;
1570 *code = WSTOPSIG (status);
1573 else if (WIFCONTINUED (status))
1575 *type = GNUNET_OS_PROCESS_RUNNING;
1581 *type = GNUNET_OS_PROCESS_UNKNOWN;
1586 DWORD c, error_code, ret;
1590 if (h == NULL || ret == 0)
1592 LOG (GNUNET_ERROR_TYPE_WARNING,
1593 "Invalid process information {%d, %08X}\n",
1595 return GNUNET_SYSERR;
1598 h = GetCurrentProcess ();
1601 ret = GetExitCodeProcess (h, &c);
1602 error_code = GetLastError ();
1603 if (ret == 0 || error_code != NO_ERROR)
1605 SetErrnoFromWinError (error_code);
1606 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetExitCodeProcess");
1607 return GNUNET_SYSERR;
1609 if (STILL_ACTIVE == c)
1611 *type = GNUNET_OS_PROCESS_RUNNING;
1615 *type = GNUNET_OS_PROCESS_EXITED;
1624 * Wait for a process to terminate. The return code is discarded.
1625 * You must not use #GNUNET_OS_process_status() on the same process
1626 * after calling this function! This function is blocking and should
1627 * thus only be used if the child process is known to have terminated
1628 * or to terminate very soon.
1630 * @param proc pointer to process structure
1631 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1634 GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
1637 pid_t pid = proc->pid;
1640 while ( (pid != (ret = waitpid (pid, NULL, 0))) &&
1641 (EINTR == errno) ) ;
1644 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1646 return GNUNET_SYSERR;
1655 LOG (GNUNET_ERROR_TYPE_WARNING,
1656 "Invalid process information {%d, %08X}\n",
1658 return GNUNET_SYSERR;
1661 h = GetCurrentProcess ();
1663 if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1665 SetErrnoFromWinError (GetLastError ());
1666 return GNUNET_SYSERR;
1674 * Handle to a command.
1676 struct GNUNET_OS_CommandHandle
1682 struct GNUNET_OS_Process *eip;
1685 * Handle to the output pipe.
1687 struct GNUNET_DISK_PipeHandle *opipe;
1690 * Read-end of output pipe.
1692 const struct GNUNET_DISK_FileHandle *r;
1695 * Function to call on each line of output.
1697 GNUNET_OS_LineProcessor proc;
1700 * Closure for @e proc.
1705 * Buffer for the output.
1710 * Task reading from pipe.
1712 struct GNUNET_SCHEDULER_Task *rtask;
1717 struct GNUNET_TIME_Absolute timeout;
1720 * Current read offset in buf.
1727 * Stop/kill a command. Must ONLY be called either from
1728 * the callback after 'NULL' was passed for 'line' *OR*
1729 * from an independent task (not within the line processor).
1731 * @param cmd handle to the process
1734 GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
1736 if (NULL != cmd->proc)
1738 GNUNET_assert (NULL != cmd->rtask);
1739 GNUNET_SCHEDULER_cancel (cmd->rtask);
1741 (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1742 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (cmd->eip));
1743 GNUNET_OS_process_destroy (cmd->eip);
1744 GNUNET_DISK_pipe_close (cmd->opipe);
1750 * Read from the process and call the line processor.
1752 * @param cls the `struct GNUNET_OS_CommandHandle *`
1755 cmd_read (void *cls)
1757 struct GNUNET_OS_CommandHandle *cmd = cls;
1758 const struct GNUNET_SCHEDULER_TaskContext *tc;
1759 GNUNET_OS_LineProcessor proc;
1764 tc = GNUNET_SCHEDULER_get_task_context ();
1765 if (GNUNET_YES != GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, cmd->r))
1767 /* timeout, shutdown, etc. */
1770 proc (cmd->proc_cls, NULL);
1774 GNUNET_DISK_file_read (cmd->r, &cmd->buf[cmd->off],
1775 sizeof (cmd->buf) - cmd->off);
1778 if ((cmd->off > 0) && (cmd->off < sizeof (cmd->buf)))
1780 cmd->buf[cmd->off] = '\0';
1781 cmd->proc (cmd->proc_cls, cmd->buf);
1785 proc (cmd->proc_cls, NULL);
1788 end = memchr (&cmd->buf[cmd->off], '\n', ret);
1793 cmd->proc (cmd->proc_cls, cmd->buf);
1794 memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1795 cmd->off -= (end + 1 - cmd->buf);
1796 end = memchr (cmd->buf, '\n', cmd->off);
1799 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
1800 (cmd->timeout), cmd->r, &cmd_read, cmd);
1805 * Run the given command line and call the given function
1806 * for each line of the output.
1808 * @param proc function to call for each line of the output
1809 * @param proc_cls closure for @a proc
1810 * @param timeout when to time out
1811 * @param binary command to run
1812 * @param ... arguments to command
1813 * @return NULL on error
1815 struct GNUNET_OS_CommandHandle *
1816 GNUNET_OS_command_run (GNUNET_OS_LineProcessor proc,
1818 struct GNUNET_TIME_Relative timeout,
1822 struct GNUNET_OS_CommandHandle *cmd;
1823 struct GNUNET_OS_Process *eip;
1824 struct GNUNET_DISK_PipeHandle *opipe;
1827 opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
1830 va_start (ap, binary);
1831 /* redirect stdout, don't inherit stderr/stdin */
1832 eip = GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL, opipe, NULL, binary, ap);
1836 GNUNET_DISK_pipe_close (opipe);
1839 GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
1840 cmd = GNUNET_new (struct GNUNET_OS_CommandHandle);
1841 cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1845 cmd->proc_cls = proc_cls;
1846 cmd->r = GNUNET_DISK_pipe_handle (opipe, GNUNET_DISK_PIPE_END_READ);
1847 cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout, cmd->r, &cmd_read, cmd);
1852 /* end of os_priority.c */