sigpipe.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef HEADER_CURL_SIGPIPE_H
  2. #define HEADER_CURL_SIGPIPE_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 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. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #if defined(HAVE_SIGACTION) && \
  28. (defined(USE_OPENSSL) || defined(USE_MBEDTLS) || defined(USE_WOLFSSL))
  29. #include <signal.h>
  30. struct sigpipe_ignore {
  31. struct sigaction old_pipe_act;
  32. bool no_signal;
  33. };
  34. #define SIGPIPE_VARIABLE(x) struct sigpipe_ignore x
  35. /*
  36. * sigpipe_ignore() makes sure we ignore SIGPIPE while running libcurl
  37. * internals, and then sigpipe_restore() will restore the situation when we
  38. * return from libcurl again.
  39. */
  40. static void sigpipe_ignore(struct Curl_easy *data,
  41. struct sigpipe_ignore *ig)
  42. {
  43. /* get a local copy of no_signal because the Curl_easy might not be
  44. around when we restore */
  45. ig->no_signal = data->set.no_signal;
  46. if(!data->set.no_signal) {
  47. struct sigaction action;
  48. /* first, extract the existing situation */
  49. sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
  50. action = ig->old_pipe_act;
  51. /* ignore this signal */
  52. action.sa_handler = SIG_IGN;
  53. sigaction(SIGPIPE, &action, NULL);
  54. }
  55. }
  56. /*
  57. * sigpipe_restore() puts back the outside world's opinion of signal handler
  58. * and SIGPIPE handling. It MUST only be called after a corresponding
  59. * sigpipe_ignore() was used.
  60. */
  61. static void sigpipe_restore(struct sigpipe_ignore *ig)
  62. {
  63. if(!ig->no_signal)
  64. /* restore the outside state */
  65. sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
  66. }
  67. #else
  68. /* for systems without sigaction */
  69. #define sigpipe_ignore(x,y) Curl_nop_stmt
  70. #define sigpipe_restore(x) Curl_nop_stmt
  71. #define SIGPIPE_VARIABLE(x)
  72. #endif
  73. #endif /* HEADER_CURL_SIGPIPE_H */