113498266Sopenharmony_ci#ifndef HEADER_CURL_SIGPIPE_H
213498266Sopenharmony_ci#define HEADER_CURL_SIGPIPE_H
313498266Sopenharmony_ci/***************************************************************************
413498266Sopenharmony_ci *                                  _   _ ____  _
513498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
613498266Sopenharmony_ci *                             / __| | | | |_) | |
713498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
813498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1113498266Sopenharmony_ci *
1213498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1313498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1413498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1513498266Sopenharmony_ci *
1613498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1713498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1813498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1913498266Sopenharmony_ci *
2013498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2113498266Sopenharmony_ci * KIND, either express or implied.
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci * SPDX-License-Identifier: curl
2413498266Sopenharmony_ci *
2513498266Sopenharmony_ci ***************************************************************************/
2613498266Sopenharmony_ci#include "curl_setup.h"
2713498266Sopenharmony_ci
2813498266Sopenharmony_ci#if defined(HAVE_SIGACTION) &&        \
2913498266Sopenharmony_ci  (defined(USE_OPENSSL) || defined(USE_MBEDTLS) || defined(USE_WOLFSSL))
3013498266Sopenharmony_ci#include <signal.h>
3113498266Sopenharmony_ci
3213498266Sopenharmony_cistruct sigpipe_ignore {
3313498266Sopenharmony_ci  struct sigaction old_pipe_act;
3413498266Sopenharmony_ci  bool no_signal;
3513498266Sopenharmony_ci};
3613498266Sopenharmony_ci
3713498266Sopenharmony_ci#define SIGPIPE_VARIABLE(x) struct sigpipe_ignore x
3813498266Sopenharmony_ci
3913498266Sopenharmony_ci/*
4013498266Sopenharmony_ci * sigpipe_ignore() makes sure we ignore SIGPIPE while running libcurl
4113498266Sopenharmony_ci * internals, and then sigpipe_restore() will restore the situation when we
4213498266Sopenharmony_ci * return from libcurl again.
4313498266Sopenharmony_ci */
4413498266Sopenharmony_cistatic void sigpipe_ignore(struct Curl_easy *data,
4513498266Sopenharmony_ci                           struct sigpipe_ignore *ig)
4613498266Sopenharmony_ci{
4713498266Sopenharmony_ci  /* get a local copy of no_signal because the Curl_easy might not be
4813498266Sopenharmony_ci     around when we restore */
4913498266Sopenharmony_ci  ig->no_signal = data->set.no_signal;
5013498266Sopenharmony_ci  if(!data->set.no_signal) {
5113498266Sopenharmony_ci    struct sigaction action;
5213498266Sopenharmony_ci    /* first, extract the existing situation */
5313498266Sopenharmony_ci    sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
5413498266Sopenharmony_ci    action = ig->old_pipe_act;
5513498266Sopenharmony_ci    /* ignore this signal */
5613498266Sopenharmony_ci    action.sa_handler = SIG_IGN;
5713498266Sopenharmony_ci    sigaction(SIGPIPE, &action, NULL);
5813498266Sopenharmony_ci  }
5913498266Sopenharmony_ci}
6013498266Sopenharmony_ci
6113498266Sopenharmony_ci/*
6213498266Sopenharmony_ci * sigpipe_restore() puts back the outside world's opinion of signal handler
6313498266Sopenharmony_ci * and SIGPIPE handling. It MUST only be called after a corresponding
6413498266Sopenharmony_ci * sigpipe_ignore() was used.
6513498266Sopenharmony_ci */
6613498266Sopenharmony_cistatic void sigpipe_restore(struct sigpipe_ignore *ig)
6713498266Sopenharmony_ci{
6813498266Sopenharmony_ci  if(!ig->no_signal)
6913498266Sopenharmony_ci    /* restore the outside state */
7013498266Sopenharmony_ci    sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
7113498266Sopenharmony_ci}
7213498266Sopenharmony_ci
7313498266Sopenharmony_ci#else
7413498266Sopenharmony_ci/* for systems without sigaction */
7513498266Sopenharmony_ci#define sigpipe_ignore(x,y) Curl_nop_stmt
7613498266Sopenharmony_ci#define sigpipe_restore(x)  Curl_nop_stmt
7713498266Sopenharmony_ci#define SIGPIPE_VARIABLE(x)
7813498266Sopenharmony_ci#endif
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci#endif /* HEADER_CURL_SIGPIPE_H */
81