Coverage Report

Created: 2025-07-11 06:33

/src/PROJ/curl/lib/sigpipe.h
Line
Count
Source (jump to first uncovered line)
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
28
#if defined(HAVE_SIGACTION) &&        \
29
  (defined(USE_OPENSSL) || defined(USE_MBEDTLS) || defined(USE_WOLFSSL))
30
#include <signal.h>
31
32
struct sigpipe_ignore {
33
  struct sigaction old_pipe_act;
34
  BIT(no_signal);
35
};
36
37
0
#define SIGPIPE_VARIABLE(x) struct sigpipe_ignore x
38
#define SIGPIPE_MEMBER(x)   struct sigpipe_ignore x
39
40
static void sigpipe_init(struct sigpipe_ignore *ig)
41
0
{
42
0
  memset(ig, 0, sizeof(*ig));
43
0
  ig->no_signal = TRUE;
44
0
}
Unexecuted instantiation: easy.c:sigpipe_init
Unexecuted instantiation: multi.c:sigpipe_init
Unexecuted instantiation: conncache.c:sigpipe_init
Unexecuted instantiation: cshutdn.c:sigpipe_init
45
46
/*
47
 * sigpipe_ignore() makes sure we ignore SIGPIPE while running libcurl
48
 * internals, and then sigpipe_restore() will restore the situation when we
49
 * return from libcurl again.
50
 */
51
static void sigpipe_ignore(struct Curl_easy *data,
52
                           struct sigpipe_ignore *ig)
53
0
{
54
  /* get a local copy of no_signal because the Curl_easy might not be
55
     around when we restore */
56
0
  ig->no_signal = data->set.no_signal;
57
0
  if(!data->set.no_signal) {
58
0
    struct sigaction action;
59
    /* first, extract the existing situation */
60
0
    sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
61
0
    action = ig->old_pipe_act;
62
    /* ignore this signal */
63
0
    action.sa_handler = SIG_IGN;
64
0
    sigaction(SIGPIPE, &action, NULL);
65
0
  }
66
0
}
Unexecuted instantiation: easy.c:sigpipe_ignore
Unexecuted instantiation: multi.c:sigpipe_ignore
Unexecuted instantiation: conncache.c:sigpipe_ignore
Unexecuted instantiation: cshutdn.c:sigpipe_ignore
67
68
/*
69
 * sigpipe_restore() puts back the outside world's opinion of signal handler
70
 * and SIGPIPE handling. It MUST only be called after a corresponding
71
 * sigpipe_ignore() was used.
72
 */
73
static void sigpipe_restore(struct sigpipe_ignore *ig)
74
0
{
75
0
  if(!ig->no_signal)
76
    /* restore the outside state */
77
0
    sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
78
0
}
Unexecuted instantiation: easy.c:sigpipe_restore
Unexecuted instantiation: multi.c:sigpipe_restore
Unexecuted instantiation: conncache.c:sigpipe_restore
Unexecuted instantiation: cshutdn.c:sigpipe_restore
79
80
static void sigpipe_apply(struct Curl_easy *data,
81
                          struct sigpipe_ignore *ig)
82
0
{
83
0
  if(data->set.no_signal != ig->no_signal) {
84
0
    sigpipe_restore(ig);
85
0
    sigpipe_ignore(data, ig);
86
0
  }
87
0
}
Unexecuted instantiation: easy.c:sigpipe_apply
Unexecuted instantiation: multi.c:sigpipe_apply
Unexecuted instantiation: conncache.c:sigpipe_apply
Unexecuted instantiation: cshutdn.c:sigpipe_apply
88
89
#else
90
/* for systems without sigaction */
91
#define sigpipe_ignore(x,y) Curl_nop_stmt
92
#define sigpipe_apply(x,y) Curl_nop_stmt
93
#define sigpipe_init(x)  Curl_nop_stmt
94
#define sigpipe_restore(x)  Curl_nop_stmt
95
#define SIGPIPE_VARIABLE(x)
96
#define SIGPIPE_MEMBER(x)   bool x
97
#endif
98
99
#endif /* HEADER_CURL_SIGPIPE_H */