Coverage Report

Created: 2026-09-14 07:05

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
263k
{
38
263k
  memset(ig, 0, sizeof(*ig));
39
263k
  ig->no_signal = TRUE;
40
263k
}
Unexecuted instantiation: easy.c:sigpipe_init
multi.c:sigpipe_init
Line
Count
Source
37
243k
{
38
243k
  memset(ig, 0, sizeof(*ig));
39
  ig->no_signal = TRUE;
40
243k
}
conncache.c:sigpipe_init
Line
Count
Source
37
19.9k
{
38
19.9k
  memset(ig, 0, sizeof(*ig));
39
  ig->no_signal = TRUE;
40
19.9k
}
cshutdn.c:sigpipe_init
Line
Count
Source
37
71
{
38
71
  memset(ig, 0, sizeof(*ig));
39
  ig->no_signal = TRUE;
40
71
}
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
263k
{
50
  /* get a local copy of no_signal because the Curl_easy might not be
51
     around when we restore */
52
263k
  ig->no_signal = data->set.no_signal;
53
263k
  if(!data->set.no_signal) {
54
263k
    struct sigaction action;
55
    /* first, extract the existing situation */
56
263k
    sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
57
263k
    action = ig->old_pipe_act;
58
    /* ignore this signal */
59
263k
    action.sa_handler = SIG_IGN;
60
263k
#ifdef SA_SIGINFO
61
    /* clear SA_SIGINFO flag since we are using sa_handler */
62
263k
    action.sa_flags &= ~SA_SIGINFO;
63
263k
#endif
64
263k
    sigaction(SIGPIPE, &action, NULL);
65
263k
  }
66
263k
}
easy.c:sigpipe_ignore
Line
Count
Source
49
60.9k
{
50
  /* get a local copy of no_signal because the Curl_easy might not be
51
     around when we restore */
52
60.9k
  ig->no_signal = data->set.no_signal;
53
60.9k
  if(!data->set.no_signal) {
54
60.9k
    struct sigaction action;
55
    /* first, extract the existing situation */
56
60.9k
    sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
57
60.9k
    action = ig->old_pipe_act;
58
    /* ignore this signal */
59
60.9k
    action.sa_handler = SIG_IGN;
60
60.9k
#ifdef SA_SIGINFO
61
    /* clear SA_SIGINFO flag since we are using sa_handler */
62
60.9k
    action.sa_flags &= ~SA_SIGINFO;
63
60.9k
#endif
64
60.9k
    sigaction(SIGPIPE, &action, NULL);
65
60.9k
  }
66
60.9k
}
multi.c:sigpipe_ignore
Line
Count
Source
49
199k
{
50
  /* get a local copy of no_signal because the Curl_easy might not be
51
     around when we restore */
52
199k
  ig->no_signal = data->set.no_signal;
53
199k
  if(!data->set.no_signal) {
54
199k
    struct sigaction action;
55
    /* first, extract the existing situation */
56
199k
    sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
57
199k
    action = ig->old_pipe_act;
58
    /* ignore this signal */
59
199k
    action.sa_handler = SIG_IGN;
60
199k
#ifdef SA_SIGINFO
61
    /* clear SA_SIGINFO flag since we are using sa_handler */
62
199k
    action.sa_flags &= ~SA_SIGINFO;
63
199k
#endif
64
199k
    sigaction(SIGPIPE, &action, NULL);
65
199k
  }
66
199k
}
conncache.c:sigpipe_ignore
Line
Count
Source
49
3.81k
{
50
  /* get a local copy of no_signal because the Curl_easy might not be
51
     around when we restore */
52
3.81k
  ig->no_signal = data->set.no_signal;
53
3.81k
  if(!data->set.no_signal) {
54
3.81k
    struct sigaction action;
55
    /* first, extract the existing situation */
56
3.81k
    sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
57
3.81k
    action = ig->old_pipe_act;
58
    /* ignore this signal */
59
3.81k
    action.sa_handler = SIG_IGN;
60
3.81k
#ifdef SA_SIGINFO
61
    /* clear SA_SIGINFO flag since we are using sa_handler */
62
3.81k
    action.sa_flags &= ~SA_SIGINFO;
63
3.81k
#endif
64
3.81k
    sigaction(SIGPIPE, &action, NULL);
65
3.81k
  }
66
3.81k
}
cshutdn.c:sigpipe_ignore
Line
Count
Source
49
110
{
50
  /* get a local copy of no_signal because the Curl_easy might not be
51
     around when we restore */
52
110
  ig->no_signal = data->set.no_signal;
53
110
  if(!data->set.no_signal) {
54
110
    struct sigaction action;
55
    /* first, extract the existing situation */
56
110
    sigaction(SIGPIPE, NULL, &ig->old_pipe_act);
57
110
    action = ig->old_pipe_act;
58
    /* ignore this signal */
59
110
    action.sa_handler = SIG_IGN;
60
110
#ifdef SA_SIGINFO
61
    /* clear SA_SIGINFO flag since we are using sa_handler */
62
110
    action.sa_flags &= ~SA_SIGINFO;
63
110
#endif
64
110
    sigaction(SIGPIPE, &action, NULL);
65
110
  }
66
110
}
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
527k
{
75
527k
  if(!ig->no_signal)
76
    /* restore the outside state */
77
263k
    sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
78
527k
}
easy.c:sigpipe_restore
Line
Count
Source
74
60.9k
{
75
60.9k
  if(!ig->no_signal)
76
    /* restore the outside state */
77
60.9k
    sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
78
60.9k
}
multi.c:sigpipe_restore
Line
Count
Source
74
442k
{
75
442k
  if(!ig->no_signal)
76
    /* restore the outside state */
77
199k
    sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
78
442k
}
conncache.c:sigpipe_restore
Line
Count
Source
74
23.7k
{
75
23.7k
  if(!ig->no_signal)
76
    /* restore the outside state */
77
3.81k
    sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
78
23.7k
}
cshutdn.c:sigpipe_restore
Line
Count
Source
74
181
{
75
181
  if(!ig->no_signal)
76
    /* restore the outside state */
77
71
    sigaction(SIGPIPE, &ig->old_pipe_act, NULL);
78
181
}
79
80
static CURL_INLINE void sigpipe_apply(struct Curl_easy *data,
81
                                      struct Curl_sigpipe_ctx *ig)
82
473k
{
83
473k
  if(data && (data->set.no_signal != ig->no_signal)) {
84
202k
    sigpipe_restore(ig);
85
202k
    sigpipe_ignore(data, ig);
86
202k
  }
87
473k
}
Unexecuted instantiation: easy.c:sigpipe_apply
multi.c:sigpipe_apply
Line
Count
Source
82
469k
{
83
469k
  if(data && (data->set.no_signal != ig->no_signal)) {
84
199k
    sigpipe_restore(ig);
85
199k
    sigpipe_ignore(data, ig);
86
199k
  }
87
469k
}
conncache.c:sigpipe_apply
Line
Count
Source
82
3.81k
{
83
3.81k
  if(data && (data->set.no_signal != ig->no_signal)) {
84
3.81k
    sigpipe_restore(ig);
85
3.81k
    sigpipe_ignore(data, ig);
86
3.81k
  }
87
3.81k
}
cshutdn.c:sigpipe_apply
Line
Count
Source
82
110
{
83
110
  if(data && (data->set.no_signal != ig->no_signal)) {
84
110
    sigpipe_restore(ig);
85
110
    sigpipe_ignore(data, ig);
86
110
  }
87
110
}
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 */