vinyl-cache/lib/libvinyl/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 18942
VSUB_closefrom(int fd)
63
{
64
65 18942
        assert(fd >= 0);
66
67
#ifdef HAVE_CLOSEFROM
68 18942
        closefrom(fd);
69 18942
        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;
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' || i <= fd || i == dirfd(d))
90
                                continue;
91
                        (void)close(i);
92
                }
93
                AZ(closedir(d));
94
        } else {
95
                /* NB: Very slow if called with a high FD limit */
96
                i = sysconf(_SC_OPEN_MAX);
97
                assert(i > 0);
98
                for (; i > fd; i--)
99
                        (void)close(i);
100
        }
101
#endif
102
}
103
104
static int
105 23568
vsub_vlu(void *priv, const char *str)
106
{
107
        struct vsub_priv *sp;
108
109 23568
        sp = priv;
110 23568
        if (!sp->lines++)
111 1071
                VSB_printf(sp->sb, "Message from %s:\n", sp->name);
112 13743
        if (sp->maxlines < 0 || sp->lines <= sp->maxlines)
113 10620
                VSB_printf(sp->sb, "%s\n", str);
114 23568
        return (0);
115
}
116
117
/* returns an exit code */
118
unsigned
119 12540
VSUB_run(struct vsb *sb, vsub_func_f *func, void *priv, const char *name,
120
    int maxlines)
121
{
122
        int rv, p[2], status;
123
        pid_t pid;
124
        struct vsub_priv sp;
125
126 12540
        sp.sb = sb;
127 12540
        sp.name = name;
128 12540
        sp.lines = 0;
129 12540
        sp.maxlines = maxlines;
130
131 12540
        if (pipe(p) < 0) {
132 0
                VSB_printf(sb, "Starting %s: pipe() failed: %s",
133 0
                    name, strerror(errno));
134 0
                return (1);
135
        }
136 12540
        assert(p[0] > STDERR_FILENO);
137 12540
        assert(p[1] > STDERR_FILENO);
138 12540
        if ((pid = fork()) < 0) {
139 0
                VSB_printf(sb, "Starting %s: fork() failed: %s",
140 0
                    name, strerror(errno));
141 0
                closefd(&p[0]);
142 0
                closefd(&p[1]);
143 0
                return (1);
144
        }
145 25002
        if (pid == 0) {
146 12462
                VFIL_null_fd(STDIN_FILENO);
147 12462
                assert(dup2(p[1], STDOUT_FILENO) == STDOUT_FILENO);
148 12462
                assert(dup2(p[1], STDERR_FILENO) == STDERR_FILENO);
149
                /* Close all other fds */
150 12462
                VSUB_closefrom(STDERR_FILENO + 1);
151 12462
                func(priv);
152
                /*
153
                 * func should either exec or exit, so getting here should be
154
                 * treated like an assertion failure - except that we don't know
155
                 * if it's safe to trigger an actual assertion
156
                 */
157 12462
                _exit(4);
158
        }
159 12540
        closefd(&p[1]);
160 12540
        (void)VLU_File(p[0], vsub_vlu, &sp, 0);
161 12540
        closefd(&p[0]);
162 12540
        if (sp.maxlines >= 0 && sp.lines > sp.maxlines)
163 156
                VSB_printf(sb, "[%d lines truncated]\n",
164 78
                    sp.lines - sp.maxlines);
165 12540
        do {
166 12540
                rv = waitpid(pid, &status, 0);
167 12540
                if (rv < 0 && errno != EINTR) {
168 0
                        VSB_printf(sb, "Running %s: waitpid() failed: %s\n",
169 0
                            name, strerror(errno));
170 0
                        return (1);
171
                }
172 12540
        } while (rv < 0);
173 12540
        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
174 909
                rv = -1;
175 909
                VSB_printf(sb, "Running %s failed", name);
176 909
                if (WIFEXITED(status)) {
177 909
                        rv = WEXITSTATUS(status);
178 909
                        VSB_printf(sb, ", exited with %d", rv);
179 909
                }
180 909
                if (WIFSIGNALED(status)) {
181 0
                        rv = 2;
182 0
                        VSB_printf(sb, ", signal %d", WTERMSIG(status));
183 0
                }
184 0
                if (WCOREDUMP(status))
185 0
                        VSB_cat(sb, ", core dumped");
186 909
                VSB_cat(sb, "\n");
187 909
                assert(rv != -1);
188 909
                return (rv);
189
        }
190 11631
        return (0);
191 12540
}