| | varnish-cache/bin/varnishd/mgt/mgt_vext.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2022 Varnish Software AS |
| 2 |
|
* All rights reserved. |
| 3 |
|
* |
| 4 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 5 |
|
* |
| 6 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 7 |
|
* |
| 8 |
|
* Redistribution and use in source and binary forms, with or without |
| 9 |
|
* modification, are permitted provided that the following conditions |
| 10 |
|
* are met: |
| 11 |
|
* 1. Redistributions of source code must retain the above copyright |
| 12 |
|
* notice, this list of conditions and the following disclaimer. |
| 13 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
|
* notice, this list of conditions and the following disclaimer in the |
| 15 |
|
* documentation and/or other materials provided with the distribution. |
| 16 |
|
* |
| 17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
|
* SUCH DAMAGE. |
| 28 |
|
* |
| 29 |
|
* Loadable extensions |
| 30 |
|
*/ |
| 31 |
|
|
| 32 |
|
#include "config.h" |
| 33 |
|
|
| 34 |
|
#include <dlfcn.h> |
| 35 |
|
#include <errno.h> |
| 36 |
|
#include <fcntl.h> |
| 37 |
|
#include <stdio.h> |
| 38 |
|
#include <stdlib.h> |
| 39 |
|
#include <unistd.h> |
| 40 |
|
|
| 41 |
|
#include "mgt.h" |
| 42 |
|
|
| 43 |
|
#include "vav.h" |
| 44 |
|
#include "vfil.h" |
| 45 |
|
#include "vrnd.h" |
| 46 |
|
|
| 47 |
|
#include "common/heritage.h" |
| 48 |
|
|
| 49 |
|
struct vext { |
| 50 |
|
unsigned magic; |
| 51 |
|
#define VEXT_MAGIC 0xd5063ef6 |
| 52 |
|
VTAILQ_ENTRY(vext) list; |
| 53 |
|
|
| 54 |
|
char **argv; |
| 55 |
|
int fd; |
| 56 |
|
struct vsb *vsb; |
| 57 |
|
void *dlptr; |
| 58 |
|
}; |
| 59 |
|
|
| 60 |
|
static VTAILQ_HEAD(,vext) vext_list = |
| 61 |
|
VTAILQ_HEAD_INITIALIZER(vext_list); |
| 62 |
|
|
| 63 |
|
static int |
| 64 |
80 |
vext_tryopen(void *priv, const char *fn) |
| 65 |
|
{ |
| 66 |
|
struct vext *vp; |
| 67 |
|
|
| 68 |
80 |
CAST_OBJ_NOTNULL(vp, priv, VEXT_MAGIC); |
| 69 |
80 |
vp->fd = open(fn, O_RDONLY); |
| 70 |
80 |
if (vp->fd < 0) |
| 71 |
0 |
return (1); |
| 72 |
80 |
return (0); |
| 73 |
80 |
} |
| 74 |
|
|
| 75 |
|
void |
| 76 |
120 |
vext_argument(const char *arg) |
| 77 |
|
{ |
| 78 |
|
struct vext *vp; |
| 79 |
|
char fn[1024]; |
| 80 |
120 |
char *fno = NULL; |
| 81 |
120 |
struct vfil_path *vmod_path = NULL; |
| 82 |
|
|
| 83 |
120 |
fprintf(stderr, "EEE <%s>\n", arg); |
| 84 |
120 |
ALLOC_OBJ(vp, VEXT_MAGIC); |
| 85 |
120 |
AN(vp); |
| 86 |
120 |
vp->argv = VAV_Parse(arg, NULL, ARGV_COMMA); |
| 87 |
120 |
AN(vp->argv); |
| 88 |
120 |
if (vp->argv[0] != NULL) |
| 89 |
0 |
ARGV_ERR("\tParse failure in argument: %s\n\t%s\n", |
| 90 |
|
arg, vp->argv[0]); |
| 91 |
120 |
VTAILQ_INSERT_TAIL(&vext_list, vp, list); |
| 92 |
|
|
| 93 |
120 |
if (strchr(vp->argv[1], '/')) |
| 94 |
40 |
bstrcpy(fn, vp->argv[1]); |
| 95 |
|
else |
| 96 |
80 |
bprintf(fn, "libvmod_%s.so", vp->argv[1]); |
| 97 |
|
|
| 98 |
120 |
VFIL_setpath(&vmod_path, mgt_vmod_path); |
| 99 |
120 |
if (VFIL_searchpath(vmod_path, vext_tryopen, vp, fn, &fno)) { |
| 100 |
40 |
ARGV_ERR("\tCannot open %s\n\t%s\n", |
| 101 |
|
fn, strerror(errno)); |
| 102 |
0 |
} |
| 103 |
|
|
| 104 |
80 |
fprintf(stderr, "eee <%s>\n", fno); |
| 105 |
80 |
free(fno); |
| 106 |
80 |
} |
| 107 |
|
|
| 108 |
|
void |
| 109 |
62800 |
vext_iter(vext_iter_f *func, void *priv) |
| 110 |
|
{ |
| 111 |
|
struct vext *vp; |
| 112 |
|
|
| 113 |
62880 |
VTAILQ_FOREACH(vp, &vext_list, list) |
| 114 |
80 |
func(VSB_data(vp->vsb), priv); |
| 115 |
62800 |
} |
| 116 |
|
|
| 117 |
|
void |
| 118 |
39760 |
vext_copyin(struct vsb *vi) |
| 119 |
|
{ |
| 120 |
|
struct vext *vp; |
| 121 |
|
const char *p; |
| 122 |
|
int i, fdo; |
| 123 |
|
unsigned u; |
| 124 |
|
char buf[BUFSIZ]; |
| 125 |
|
ssize_t sz, szw; |
| 126 |
|
|
| 127 |
39840 |
VTAILQ_FOREACH(vp, &vext_list, list) { |
| 128 |
80 |
if (vp->vsb == NULL) { |
| 129 |
80 |
vp->vsb = VSB_new_auto(); |
| 130 |
80 |
AN(vp->vsb); |
| 131 |
80 |
} |
| 132 |
80 |
VSB_clear(vp->vsb); |
| 133 |
80 |
p = strrchr(vp->argv[1], '/'); |
| 134 |
80 |
if (p != NULL) |
| 135 |
40 |
p++; |
| 136 |
|
else |
| 137 |
40 |
p = vp->argv[1]; |
| 138 |
80 |
VSB_printf(vi, ",-E%s", p); |
| 139 |
80 |
VSB_printf(vp->vsb, "vext_cache/%s,", p); |
| 140 |
720 |
for (i = 0; i < 8; i++) { |
| 141 |
640 |
AZ(VRND_RandomCrypto(&u, sizeof u)); |
| 142 |
640 |
u %= 26; |
| 143 |
640 |
VSB_printf(vp->vsb, "%c", 'a' + (char)u); |
| 144 |
640 |
} |
| 145 |
80 |
VSB_cat(vp->vsb, ".so"); |
| 146 |
80 |
AZ(VSB_finish(vp->vsb)); |
| 147 |
80 |
fprintf(stderr, "ee2 %s\n", VSB_data(vp->vsb)); |
| 148 |
80 |
fdo = open(VSB_data(vp->vsb), O_WRONLY|O_CREAT|O_EXCL, 0755); |
| 149 |
80 |
xxxassert(fdo >= 0); |
| 150 |
80 |
AZ(lseek(vp->fd, 0, SEEK_SET)); |
| 151 |
80 |
do { |
| 152 |
13920 |
sz = read(vp->fd, buf, sizeof buf); |
| 153 |
13920 |
if (sz > 0) { |
| 154 |
13840 |
szw = write(fdo, buf, sz); |
| 155 |
13840 |
xxxassert(szw == sz); |
| 156 |
13840 |
} |
| 157 |
13920 |
} while (sz > 0); |
| 158 |
80 |
closefd(&fdo); |
| 159 |
80 |
closefd(&vp->fd); |
| 160 |
80 |
} |
| 161 |
39760 |
} |
| 162 |
|
|
| 163 |
|
void |
| 164 |
38629 |
vext_load(void) |
| 165 |
|
{ |
| 166 |
|
struct vext *vp; |
| 167 |
|
|
| 168 |
38709 |
VTAILQ_FOREACH(vp, &vext_list, list) { |
| 169 |
80 |
vp->dlptr = dlopen( |
| 170 |
80 |
VSB_data(vp->vsb), |
| 171 |
|
RTLD_NOW | RTLD_GLOBAL |
| 172 |
|
); |
| 173 |
80 |
if (vp->dlptr == NULL) { |
| 174 |
0 |
XXXAN(vp->dlptr); |
| 175 |
0 |
} |
| 176 |
80 |
fprintf(stderr, "Loaded -E %s\n", VSB_data(vp->vsb)); |
| 177 |
80 |
} |
| 178 |
38629 |
} |
| 179 |
|
|
| 180 |
|
void |
| 181 |
38920 |
vext_cleanup(int do_unlink) |
| 182 |
|
{ |
| 183 |
|
struct vext *vp; |
| 184 |
|
|
| 185 |
39000 |
VTAILQ_FOREACH(vp, &vext_list, list) { |
| 186 |
80 |
if (vp->vsb != NULL && VSB_len(vp->vsb) > 0) { |
| 187 |
80 |
if (do_unlink) |
| 188 |
80 |
XXXAZ(unlink(VSB_data(vp->vsb))); |
| 189 |
80 |
VSB_clear(vp->vsb); |
| 190 |
80 |
} |
| 191 |
80 |
} |
| 192 |
38920 |
} |