Coverage Report

Created: 2026-10-22 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/sigpipe.h
Line
Count
Source
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) && !defined(USE_SO_NOSIGPIPE)
29
#include <signal.h>
30
31
struct Curl_sigpipe_ctx {
32
  struct sigaction old_pipe_act;
33
  BIT(no_signal);
34
};
35
36
static CURL_INLINE void sigpipe_init(struct Curl_sigpipe_ctx *ig)
37
39.6k
{
38
39.6k
  memset(ig, 0, sizeof(*ig));
39
39.6k
  ig->no_signal = TRUE;
40
39.6k
}
Unexecuted instantiation: easy.c:sigpipe_init
multi.c:sigpipe_init
Line
Count
Source
37
37.5k
{
38
37.5k
  memset(ig, 0, sizeof(*ig));
39
  ig->no_signal = TRUE;
40
37.5k
}
conncache.c:sigpipe_init
Line
Count
Source
37
2.10k
{
38
2.10k
  memset(ig, 0, sizeof(*ig));
39
  ig->no_signal = TRUE;
40
2.10k
}
cshutdn.c:sigpipe_init
Line
Count
Source
37
2
{
38
2
  memset(ig, 0, sizeof(*ig));
39
  ig->no_signal = TRUE;
40
2
}
41
42
/*
43
 * sigpipe_ignore() makes sure we ignore SIGPIPE while running libcurl
44
 * internals, and then sigpipe_restore() will restore the situation when we
45
 * return from libcurl again.
46
 */
47
static CURL_INLINE void sigpipe_ignore(struct Curl_easy *data,
48
                                       struct Curl_sigpipe_ctx *ig)
49
65.2k
{
50
  /* get a local copy of no_signal because the Curl_easy might not be
51
     around when we restore */
52
65.2k
  ig->no_signal = data->set.no_signal;
53
65.2k
  if(!data->set.no_signal) {
54
65.1k
    struct sigaction action;
55
    /* first, extract the existing situation */
56
65.1k
    sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
57
65.1k
    action = ig->old_pipe_act;
58
    /* ignore this signal */
59
65.1k
    action.sa_handler = SIG_IGN;
60
65.1k
#ifdef SA_SIGINFO
61
    /* clear SA_SIGINFO flag since we are using sa_handler */
62
65.1k
    action.sa_flags &= ~SA_SIGINFO;
63
65.1k
#endif
64
65.1k
    sigaction(SIGPIPE, &action, NULL);
65
65.1k
  }
66
65.2k
}
easy.c:sigpipe_ignore
Line
Count
Source
49
25.6k
{
50
  /* get a local copy of no_signal because the Curl_easy might not be
51
     around when we restore */
52
25.6k
  ig->no_signal = data->set.no_signal;
53
25.6k
  if(!data->set.no_signal) {
54
25.6k
    struct sigaction action;
55
    /* first, extract the existing situation */
56
25.6k
    sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
57
25.6k
    action = ig->old_pipe_act;
58
    /* ignore this signal */
59
25.6k
    action.sa_handler = SIG_IGN;
60
25.6k
#ifdef SA_SIGINFO
61
    /* clear SA_SIGINFO flag since we are using sa_handler */
62
25.6k
    action.sa_flags &= ~SA_SIGINFO;
63
25.6k
#endif
64
25.6k
    sigaction(SIGPIPE, &action, NULL);
65
25.6k
  }
66
25.6k
}
multi.c:sigpipe_ignore
Line
Count
Source
49
37.4k
{
50
  /* get a local copy of no_signal because the Curl_easy might not be
51
     around when we restore */
52
37.4k
  ig->no_signal = data->set.no_signal;
53
37.4k
  if(!data->set.no_signal) {
54
37.4k
    struct sigaction action;
55
    /* first, extract the existing situation */
56
37.4k
    sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
57
37.4k
    action = ig->old_pipe_act;
58
    /* ignore this signal */
59
37.4k
    action.sa_handler = SIG_IGN;
60
37.4k
#ifdef SA_SIGINFO
61
    /* clear SA_SIGINFO flag since we are using sa_handler */
62
37.4k
    action.sa_flags &= ~SA_SIGINFO;
63
37.4k
#endif
64
37.4k
    sigaction(SIGPIPE, &action, NULL);
65
37.4k
  }
66
37.4k
}
conncache.c:sigpipe_ignore
Line
Count
Source
49
2.10k
{
50
  /* get a local copy of no_signal because the Curl_easy might not be
51
     around when we restore */
52
2.10k
  ig->no_signal = data->set.no_signal;
53
2.10k
  if(!data->set.no_signal) {
54
2.10k
    struct sigaction action;
55
    /* first, extract the existing situation */
56
2.10k
    sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
57
2.10k
    action = ig->old_pipe_act;
58
    /* ignore this signal */
59
2.10k
    action.sa_handler = SIG_IGN;
60
2.10k
#ifdef SA_SIGINFO
61
    /* clear SA_SIGINFO flag since we are using sa_handler */
62
2.10k
    action.sa_flags &= ~SA_SIGINFO;
63
2.10k
#endif
64
2.10k
    sigaction(SIGPIPE, &action, NULL);
65
2.10k
  }
66
2.10k
}
cshutdn.c:sigpipe_ignore
Line
Count
Source
49
1
{
50
  /* get a local copy of no_signal because the Curl_easy might not be
51
     around when we restore */
52
1
  ig->no_signal = data->set.no_signal;
53
1
  if(!data->set.no_signal) {
54
1
    struct sigaction action;
55
    /* first, extract the existing situation */
56
1
    sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
57
1
    action = ig->old_pipe_act;
58
    /* ignore this signal */
59
1
    action.sa_handler = SIG_IGN;
60
1
#ifdef SA_SIGINFO
61
    /* clear SA_SIGINFO flag since we are using sa_handler */
62
1
    action.sa_flags &= ~SA_SIGINFO;
63
1
#endif
64
1
    sigaction(SIGPIPE, &action, NULL);
65
1
  }
66
1
}
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 CURL_INLINE void sigpipe_restore(struct Curl_sigpipe_ctx *ig)
74
104k
{
75
104k
  if(!ig->no_signal)
76
    /* restore the outside state */
77
65.1k
    sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
78
104k
}
easy.c:sigpipe_restore
Line
Count
Source
74
25.6k
{
75
25.6k
  if(!ig->no_signal)
76
    /* restore the outside state */
77
25.6k
    sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
78
25.6k
}
multi.c:sigpipe_restore
Line
Count
Source
74
75.0k
{
75
75.0k
  if(!ig->no_signal)
76
    /* restore the outside state */
77
37.4k
    sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
78
75.0k
}
conncache.c:sigpipe_restore
Line
Count
Source
74
4.21k
{
75
4.21k
  if(!ig->no_signal)
76
    /* restore the outside state */
77
2.10k
    sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
78
4.21k
}
cshutdn.c:sigpipe_restore
Line
Count
Source
74
3
{
75
3
  if(!ig->no_signal)
76
    /* restore the outside state */
77
1
    sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
78
3
}
79
80
static CURL_INLINE void sigpipe_apply(struct Curl_easy *data,
81
                                      struct Curl_sigpipe_ctx *ig)
82
41.5k
{
83
41.5k
  if(data && (data->set.no_signal != ig->no_signal)) {
84
39.5k
    sigpipe_restore(ig);
85
39.5k
    sigpipe_ignore(data, ig);
86
39.5k
  }
87
41.5k
}
Unexecuted instantiation: easy.c:sigpipe_apply
multi.c:sigpipe_apply
Line
Count
Source
82
39.4k
{
83
39.4k
  if(data && (data->set.no_signal != ig->no_signal)) {
84
37.4k
    sigpipe_restore(ig);
85
37.4k
    sigpipe_ignore(data, ig);
86
37.4k
  }
87
39.4k
}
conncache.c:sigpipe_apply
Line
Count
Source
82
2.10k
{
83
2.10k
  if(data && (data->set.no_signal != ig->no_signal)) {
84
2.10k
    sigpipe_restore(ig);
85
2.10k
    sigpipe_ignore(data, ig);
86
2.10k
  }
87
2.10k
}
cshutdn.c:sigpipe_apply
Line
Count
Source
82
2
{
83
2
  if(data && (data->set.no_signal != ig->no_signal)) {
84
1
    sigpipe_restore(ig);
85
1
    sigpipe_ignore(data, ig);
86
1
  }
87
2
}
88
89
#else /* !HAVE_SIGACTION || USE_SO_NOSIGPIPE */
90
/* for systems without sigaction or where SO_NOSIGPIPE is used. */
91
#define sigpipe_ignore(x, y) do { (void)(x); (void)(y); } while(0)
92
#define sigpipe_apply(x, y)  do { (void)(x); (void)(y); } while(0)
93
#define sigpipe_init(x)      do { (void)(x); } while(0)
94
#define sigpipe_restore(x)   do { (void)(x); } while(0)
95
96
struct Curl_sigpipe_ctx {
97
  bool dummy;
98
};
99
100
#endif /* else HAVE_SIGACTION && !USE_SO_NOSIGPIPE */
101
102
#endif /* HEADER_CURL_SIGPIPE_H */