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 236080
VSUB_closefrom(int fd)
63
{
64
65 236080
        assert(fd >= 0);
66
67
#ifdef HAVE_CLOSEFROM
68 236080
        closefrom(fd);
69 236080
        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 215760
vsub_vlu(void *priv, const char *str)
107
{
108
        struct vsub_priv *sp;
109
110 215760
        sp = priv;
111 215760
        if (!sp->lines++)
112 12960
                VSB_printf(sp->sb, "Message from %s:\n", sp->name);
113 96520
        if (sp->maxlines < 0 || sp->lines <= sp->maxlines)
114 125040
                VSB_printf(sp->sb, "%s\n", str);
115 215760
        return (0);
116
}
117
118
/* returns an exit code */
119
unsigned
120 156640
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 156640
        sp.sb = sb;
128 156640
        sp.name = name;
129 156640
        sp.lines = 0;
130 156640
        sp.maxlines = maxlines;
131
132 156640
        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 156640
        assert(p[0] > STDERR_FILENO);
138 156640
        assert(p[1] > STDERR_FILENO);
139 156640
        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 312720
        if (pid == 0) {
147 156080
                VFIL_null_fd(STDIN_FILENO);
148 156080
                assert(dup2(p[1], STDOUT_FILENO) == STDOUT_FILENO);
149 156080
                assert(dup2(p[1], STDERR_FILENO) == STDERR_FILENO);
150
                /* Close all other fds */
151 156080
                VSUB_closefrom(STDERR_FILENO + 1);
152 156080
                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 156080
                _exit(4);
159
        }
160 156640
        closefd(&p[1]);
161 156640
        (void)VLU_File(p[0], vsub_vlu, &sp, 0);
162 156640
        closefd(&p[0]);
163 156640
        if (sp.maxlines >= 0 && sp.lines > sp.maxlines)
164 1120
                VSB_printf(sb, "[%d lines truncated]\n",
165 560
                    sp.lines - sp.maxlines);
166 156640
        do {
167 156640
                rv = waitpid(pid, &status, 0);
168 156640
                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 156640
        } while (rv < 0);
174 156640
        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
175 11640
                rv = -1;
176 11640
                VSB_printf(sb, "Running %s failed", name);
177 11640
                if (WIFEXITED(status)) {
178 11640
                        rv = WEXITSTATUS(status);
179 11640
                        VSB_printf(sb, ", exited with %d", rv);
180 11640
                }
181 11640
                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 11640
                VSB_cat(sb, "\n");
188 11640
                assert(rv != -1);
189 11640
                return (rv);
190
        }
191 145000
        return (0);
192 156640
}