sigpipe.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef HEADER_CURL_SIGPIPE_H
  2. #define HEADER_CURL_SIGPIPE_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if defined(HAVE_SIGNAL_H) && defined(HAVE_SIGACTION) && \
  26. (defined(USE_OPENSSL) || defined(USE_MBEDTLS) || defined(USE_WOLFSSL))
  27. #include <signal.h>
  28. struct sigpipe_ignore {
  29. struct sigaction old_pipe_act;
  30. bool no_signal;
  31. };
  32. #define SIGPIPE_VARIABLE(x) struct sigpipe_ignore x
  33. /*
  34. * sigpipe_ignore() makes sure we ignore SIGPIPE while running libcurl
  35. * internals, and then sigpipe_restore() will restore the situation when we
  36. * return from libcurl again.
  37. */
  38. static void sigpipe_ignore(struct Curl_easy *data,
  39. struct sigpipe_ignore *ig)
  40. {
  41. /* get a local copy of no_signal because the Curl_easy might not be
  42. around when we restore */
  43. ig->no_signal = data->set.no_signal;
  44. if(!data->set.no_signal) {
  45. struct sigaction action;
  46. /* first, extract the existing situation */
  47. memset(&ig->old_pipe_act, 0, sizeof(struct sigaction));
  48. sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
  49. action = ig->old_pipe_act;
  50. /* ignore this signal */
  51. action.sa_handler = SIG_IGN;
  52. sigaction(SIGPIPE, &action, NULL);
  53. }
  54. }
  55. /*
  56. * sigpipe_restore() puts back the outside world's opinion of signal handler
  57. * and SIGPIPE handling. It MUST only be called after a corresponding
  58. * sigpipe_ignore() was used.
  59. */
  60. static void sigpipe_restore(struct sigpipe_ignore *ig)
  61. {
  62. if(!ig->no_signal)
  63. /* restore the outside state */
  64. sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
  65. }
  66. #else
  67. /* for systems without sigaction */
  68. #define sigpipe_ignore(x,y) Curl_nop_stmt
  69. #define sigpipe_restore(x) Curl_nop_stmt
  70. #define SIGPIPE_VARIABLE(x)
  71. #endif
  72. #endif /* HEADER_CURL_SIGPIPE_H */