gnunet-qr-utils.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010, 2011, 2012 Christian Grothoff
  4. Copyright (C) 2019 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.
  9. GNUnet is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Affero General Public License for more details.
  13. You should have received a copy of the GNU Affero General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. SPDX-License-Identifier: AGPL3.0-or-later
  16. */
  17. #include "platform.h"
  18. //
  19. // FIXME: These functions are copied from dns/gnunet-helper-dns.c,
  20. // move them into a common library. Or think about implementing a even
  21. // more elaborate version.
  22. //
  23. /**
  24. * Open '/dev/null' and make the result the given
  25. * file descriptor.
  26. *
  27. * @param target_fd desired FD to point to /dev/null
  28. * @param flags open flags (O_RDONLY, O_WRONLY)
  29. */
  30. static void
  31. open_dev_null (int target_fd,
  32. int flags)
  33. {
  34. int fd;
  35. fd = open ("/dev/null", flags);
  36. if (-1 == fd)
  37. abort ();
  38. if (fd == target_fd)
  39. return;
  40. if (-1 == dup2 (fd, target_fd))
  41. {
  42. (void) close (fd);
  43. abort ();
  44. }
  45. (void) close (fd);
  46. }
  47. /**
  48. * Run the given command and wait for it to complete.
  49. *
  50. * @param file name of the binary to run
  51. * @param cmd command line arguments (as given to 'execv')
  52. * @return 0 on success, 1 on any error
  53. */
  54. static int
  55. fork_and_exec (const char *file,
  56. char *const cmd[])
  57. {
  58. int status;
  59. pid_t pid;
  60. pid_t ret;
  61. pid = fork ();
  62. if (-1 == pid)
  63. {
  64. fprintf (stderr,
  65. "fork failed: %s\n",
  66. strerror (errno));
  67. return 1;
  68. }
  69. if (0 == pid)
  70. {
  71. /* we are the child process */
  72. /* close stdin/stdout to not cause interference
  73. with the helper's main protocol! */
  74. (void) close (0);
  75. open_dev_null (0, O_RDONLY);
  76. (void) close (1);
  77. open_dev_null (1, O_WRONLY);
  78. (void) execv (file, cmd);
  79. /* can only get here on error */
  80. fprintf (stderr,
  81. "exec `%s' failed: %s\n",
  82. file,
  83. strerror (errno));
  84. _exit (1);
  85. }
  86. /* keep running waitpid as long as the only error we get is 'EINTR' */
  87. while ( (-1 == (ret = waitpid (pid, &status, 0))) &&
  88. (errno == EINTR) );
  89. if (-1 == ret)
  90. {
  91. fprintf (stderr,
  92. "waitpid failed: %s\n",
  93. strerror (errno));
  94. return 1;
  95. }
  96. if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status))))
  97. return 1;
  98. /* child process completed and returned success, we're happy */
  99. return 0;
  100. }