sigpipe.h 2.6 KB

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