varnish-cache/lib/libvarnish/vsub.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2011 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * Run stuff in a child process
31
 */
32
33
#include "config.h"
34
35
#include <stdio.h>
36
#include <stdint.h>
37
#include <stdlib.h>             // Solaris closefrom(3c)
38
#include <string.h>
39
#include <unistd.h>             // BSD/Linux close_range(2)
40
#ifndef HAVE_CLOSEFROM
41
#  include <dirent.h>
42
#endif
43
44
#include "vdef.h"
45
46
#include "vapi/vsig.h"
47
48
#include "vas.h"
49
#include "vfil.h"
50
#include "vlu.h"
51
#include "vsb.h"
52
#include "vsub.h"
53
54
struct vsub_priv {
55
        const char      *name;
56
        struct vsb      *sb;
57
        int             lines;
58
        int             maxlines;
59
};
60
61
void
62 167650
VSUB_closefrom(int fd)
63
{
64
65 167650
        assert(fd >= 0);
66
67
#ifdef HAVE_CLOSEFROM
68 167650
        closefrom(fd);
69 167650
        return;
70
#else
71
#  ifdef HAVE_WORKING_CLOSE_RANGE
72
        if (close_range(fd, ~0U, 0) == 0)
73
                return;
74
#  endif
75
        char buf[128];
76
        int i, maxfd = 0;
77
        DIR *d;
78
        struct dirent *de;
79
        char *p;
80
81
        bprintf(buf, "/proc/%d/fd/", getpid());
82
        d = opendir(buf);
83
        if (d != NULL) {
84
                while (1) {
85
                        de = readdir(d);
86
                        if (de == NULL)
87
                                break;
88
                        i = strtoul(de->d_name, &p, 10);
89
                        if (*p != '\0')
90
                                continue;
91
                        if (i > maxfd)
92
                                maxfd = i;
93
                }
94
                AZ(closedir(d));
95
        }
96
97
        if (maxfd == 0)
98
                maxfd = sysconf(_SC_OPEN_MAX);
99
        assert(maxfd > 0);
100
        for (; maxfd > fd; maxfd--)
101
                (void)close(maxfd);
102
#endif
103
}
104
105
static int
106 132350
vsub_vlu(void *priv, const char *str)
107
{
108
        struct vsub_priv *sp;
109
110 132350
        sp = priv;
111 132350
        if (!sp->lines++)
112 8100
                VSB_printf(sp->sb, "Message from %s:\n", sp->name);
113 57850
        if (sp->maxlines < 0 || sp->lines <= sp->maxlines)
114 78100
                VSB_printf(sp->sb, "%s\n", str);
115 132350
        return (0);
116
}
117
118
/* returns an exit code */
119
unsigned
120 95050
VSUB_run(struct vsb *sb, vsub_func_f *func, void *priv, const char *name,
121
    int maxlines)
122
{
123
        int rv, p[2], status;
124
        pid_t pid;
125
        struct vsub_priv sp;
126
127 95050
        sp.sb = sb;
128 95050
        sp.name = name;
129 95050
        sp.lines = 0;
130 95050
        sp.maxlines = maxlines;
131
132 95050
        if (pipe(p) < 0) {
133 0
                VSB_printf(sb, "Starting %s: pipe() failed: %s",
134 0
                    name, strerror(errno));
135 0
                return (1);
136
        }
137 95050
        assert(p[0] > STDERR_FILENO);
138 95050
        assert(p[1] > STDERR_FILENO);
139 95050
        if ((pid = fork()) < 0) {
140 0
                VSB_printf(sb, "Starting %s: fork() failed: %s",
141 0
                    name, strerror(errno));
142 0
                closefd(&p[0]);
143 0
                closefd(&p[1]);
144 0
                return (1);
145
        }
146 189750
        if (pid == 0) {
147 94700
                VFIL_null_fd(STDIN_FILENO);
148 94700
                assert(dup2(p[1], STDOUT_FILENO) == STDOUT_FILENO);
149 94700
                assert(dup2(p[1], STDERR_FILENO) == STDERR_FILENO);
150
                /* Close all other fds */
151 94700
                VSUB_closefrom(STDERR_FILENO + 1);
152 94700
                func(priv);
153
                /*
154
                 * func should either exec or exit, so getting here should be
155
                 * treated like an assertion failure - except that we don't know
156
                 * if it's safe to trigger an actual assertion
157
                 */
158 94700
                _exit(4);
159
        }
160 95050
        closefd(&p[1]);
161 95050
        (void)VLU_File(p[0], vsub_vlu, &sp, 0);
162 95050
        closefd(&p[0]);
163 95050
        if (sp.maxlines >= 0 && sp.lines > sp.maxlines)
164 700
                VSB_printf(sb, "[%d lines truncated]\n",
165 350
                    sp.lines - sp.maxlines);
166 95050
        do {
167 95050
                rv = waitpid(pid, &status, 0);
168 95050
                if (rv < 0 && errno != EINTR) {
169 0
                        VSB_printf(sb, "Running %s: waitpid() failed: %s\n",
170 0
                            name, strerror(errno));
171 0
                        return (1);
172
                }
173 95050
        } while (rv < 0);
174 95050
        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
175 7275
                rv = -1;
176 7275
                VSB_printf(sb, "Running %s failed", name);
177 7275
                if (WIFEXITED(status)) {
178 7275
                        rv = WEXITSTATUS(status);
179 7275
                        VSB_printf(sb, ", exited with %d", rv);
180 7275
                }
181 7275
                if (WIFSIGNALED(status)) {
182 0
                        rv = 2;
183 0
                        VSB_printf(sb, ", signal %d", WTERMSIG(status));
184 0
                }
185 0
                if (WCOREDUMP(status))
186 0
                        VSB_cat(sb, ", core dumped");
187 7275
                VSB_cat(sb, "\n");
188 7275
                assert(rv != -1);
189 7275
                return (rv);
190
        }
191 87775
        return (0);
192 95050
}