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>
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 180712
VSUB_closefrom(int fd)
63
{
64
65 180712
        assert(fd >= 0);
66
67
#ifdef HAVE_CLOSEFROM
68 180712
        closefrom(fd);
69 180712
        return;
70
#else
71
        char buf[128];
72
        int i, maxfd = 0;
73
        DIR *d;
74
        struct dirent *de;
75
        char *p;
76
77
        bprintf(buf, "/proc/%d/fd/", getpid());
78
        d = opendir(buf);
79
        if (d != NULL) {
80
                while (1) {
81
                        de = readdir(d);
82
                        if (de == NULL)
83
                                break;
84
                        i = strtoul(de->d_name, &p, 10);
85
                        if (*p != '\0')
86
                                continue;
87
                        if (i > maxfd)
88
                                maxfd = i;
89
                }
90
                AZ(closedir(d));
91
        }
92
93
        if (maxfd == 0)
94
                maxfd = sysconf(_SC_OPEN_MAX);
95
        assert(maxfd > 0);
96
        for (; maxfd > fd; maxfd--)
97
                (void)close(maxfd);
98
#endif
99
}
100
101
static int
102 138796
vsub_vlu(void *priv, const char *str)
103
{
104
        struct vsub_priv *sp;
105
106 138796
        sp = priv;
107 138796
        if (!sp->lines++)
108 8316
                VSB_printf(sp->sb, "Message from %s:\n", sp->name);
109 64764
        if (sp->maxlines < 0 || sp->lines <= sp->maxlines)
110 78036
                VSB_printf(sp->sb, "%s\n", str);
111 138796
        return (0);
112
}
113
114
/* returns an exit code */
115
unsigned
116 102368
VSUB_run(struct vsb *sb, vsub_func_f *func, void *priv, const char *name,
117
    int maxlines)
118
{
119
        int rv, p[2], status;
120
        pid_t pid;
121
        struct vsub_priv sp;
122
123 102368
        sp.sb = sb;
124 102368
        sp.name = name;
125 102368
        sp.lines = 0;
126 102368
        sp.maxlines = maxlines;
127
128 102368
        if (pipe(p) < 0) {
129 0
                VSB_printf(sb, "Starting %s: pipe() failed: %s",
130 0
                    name, strerror(errno));
131 0
                return (1);
132
        }
133 102368
        assert(p[0] > STDERR_FILENO);
134 102368
        assert(p[1] > STDERR_FILENO);
135 102368
        if ((pid = fork()) < 0) {
136 0
                VSB_printf(sb, "Starting %s: fork() failed: %s",
137 0
                    name, strerror(errno));
138 0
                closefd(&p[0]);
139 0
                closefd(&p[1]);
140 0
                return (1);
141
        }
142 204344
        if (pid == 0) {
143 101976
                VFIL_null_fd(STDIN_FILENO);
144 101976
                assert(dup2(p[1], STDOUT_FILENO) == STDOUT_FILENO);
145 101976
                assert(dup2(p[1], STDERR_FILENO) == STDERR_FILENO);
146
                /* Close all other fds */
147 101976
                VSUB_closefrom(STDERR_FILENO + 1);
148 101976
                func(priv);
149
                /*
150
                 * func should either exec or exit, so getting here should be
151
                 * treated like an assertion failure - except that we don't know
152
                 * if it's safe to trigger an actual assertion
153
                 */
154 101976
                _exit(4);
155
        }
156 102368
        closefd(&p[1]);
157 102368
        (void)VLU_File(p[0], vsub_vlu, &sp, 0);
158 102368
        closefd(&p[0]);
159 102368
        if (sp.maxlines >= 0 && sp.lines > sp.maxlines)
160 784
                VSB_printf(sb, "[%d lines truncated]\n",
161 392
                    sp.lines - sp.maxlines);
162 102368
        do {
163 102368
                rv = waitpid(pid, &status, 0);
164 102368
                if (rv < 0 && errno != EINTR) {
165 0
                        VSB_printf(sb, "Running %s: waitpid() failed: %s\n",
166 0
                            name, strerror(errno));
167 0
                        return (1);
168
                }
169 102368
        } while (rv < 0);
170 102368
        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
171 7420
                rv = -1;
172 7420
                VSB_printf(sb, "Running %s failed", name);
173 7420
                if (WIFEXITED(status)) {
174 7420
                        rv = WEXITSTATUS(status);
175 7420
                        VSB_printf(sb, ", exited with %d", rv);
176 7420
                }
177 7420
                if (WIFSIGNALED(status)) {
178 0
                        rv = 2;
179 0
                        VSB_printf(sb, ", signal %d", WTERMSIG(status));
180 0
                }
181 0
                if (WCOREDUMP(status))
182 0
                        VSB_cat(sb, ", core dumped");
183 7420
                VSB_cat(sb, "\n");
184 7420
                assert(rv != -1);
185 7420
                return (rv);
186
        }
187 94948
        return (0);
188 102368
}