sigpipe.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #define SIGPIPE_MEMBER(x) struct sigpipe_ignore x
  36. static void sigpipe_init(struct sigpipe_ignore *ig)
  37. {
  38. memset(ig, 0, sizeof(*ig));
  39. ig->no_signal = TRUE;
  40. }
  41. /*
  42. * sigpipe_ignore() makes sure we ignore SIGPIPE while running libcurl
  43. * internals, and then sigpipe_restore() will restore the situation when we
  44. * return from libcurl again.
  45. */
  46. static void sigpipe_ignore(struct Curl_easy *data,
  47. struct sigpipe_ignore *ig)
  48. {
  49. /* get a local copy of no_signal because the Curl_easy might not be
  50. around when we restore */
  51. ig->no_signal = data->set.no_signal;
  52. if(!data->set.no_signal) {
  53. struct sigaction action;
  54. /* first, extract the existing situation */
  55. sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
  56. action = ig->old_pipe_act;
  57. /* ignore this signal */
  58. action.sa_handler = SIG_IGN;
  59. sigaction(SIGPIPE, &action, NULL);
  60. }
  61. }
  62. /*
  63. * sigpipe_restore() puts back the outside world's opinion of signal handler
  64. * and SIGPIPE handling. It MUST only be called after a corresponding
  65. * sigpipe_ignore() was used.
  66. */
  67. static void sigpipe_restore(struct sigpipe_ignore *ig)
  68. {
  69. if(!ig->no_signal)
  70. /* restore the outside state */
  71. sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
  72. }
  73. static void sigpipe_apply(struct Curl_easy *data,
  74. struct sigpipe_ignore *ig)
  75. {
  76. if(data->set.no_signal != ig->no_signal) {
  77. sigpipe_restore(ig);
  78. sigpipe_ignore(data, ig);
  79. }
  80. }
  81. #else
  82. /* for systems without sigaction */
  83. #define sigpipe_ignore(x,y) Curl_nop_stmt
  84. #define sigpipe_apply(x,y) Curl_nop_stmt
  85. #define sigpipe_init(x) Curl_nop_stmt
  86. #define sigpipe_restore(x) Curl_nop_stmt
  87. #define SIGPIPE_VARIABLE(x)
  88. #define SIGPIPE_MEMBER(x) bool x
  89. #endif
  90. #endif /* HEADER_CURL_SIGPIPE_H */