2 This file is part of GNUnet.
3 Copyright (C) 2010, 2011, 2012 Christian Grothoff
4 Copyright (C) 2019 GNUnet e.V.
6 GNUnet is free software: you can redistribute it and/or modify it
7 under the terms of the GNU Affero General Public License as published
8 by the Free Software Foundation, either version 3 of the License,
9 or (at your option) any later version.
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Affero General Public License for more details.
16 You should have received a copy of the GNU Affero General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 SPDX-License-Identifier: AGPL3.0-or-later
25 // FIXME: These functions are copied from dns/gnunet-helper-dns.c,
26 // move them into a common library. Or think about implementing a even
27 // more elaborate version.
31 * Open '/dev/null' and make the result the given
34 * @param target_fd desired FD to point to /dev/null
35 * @param flags open flags (O_RDONLY, O_WRONLY)
38 open_dev_null (int target_fd,
43 fd = open ("/dev/null", flags);
48 if (-1 == dup2 (fd, target_fd))
57 * Run the given command and wait for it to complete.
59 * @param file name of the binary to run
60 * @param cmd command line arguments (as given to 'execv')
61 * @return 0 on success, 1 on any error
64 fork_and_exec (const char *file,
81 /* we are the child process */
82 /* close stdin/stdout to not cause interference
83 with the helper's main protocol! */
85 open_dev_null (0, O_RDONLY);
87 open_dev_null (1, O_WRONLY);
88 (void) execv (file, cmd);
89 /* can only get here on error */
91 "exec `%s' failed: %s\n",
96 /* keep running waitpid as long as the only error we get is 'EINTR' */
97 while ( (-1 == (ret = waitpid (pid, &status, 0))) &&
102 "waitpid failed: %s\n",
106 if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status))))
108 /* child process completed and returned success, we're happy */