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