vinyl-cache/bin/vinyld/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 8
vext_tryopen(void *priv, const char *fn)
65
{
66
        struct vext *vp;
67
68 8
        CAST_OBJ_NOTNULL(vp, priv, VEXT_MAGIC);
69 8
        vp->fd = open(fn, O_RDONLY);
70 8
        if (vp->fd < 0)
71 0
                return (1);
72 8
        return (0);
73 8
}
74
75
void
76 12
vext_argument(const char *arg)
77
{
78
        struct vext *vp;
79
        char fn[1024];
80 12
        char *fno = NULL;
81 12
        struct vfil_path *vmod_path = NULL;
82
83 12
        fprintf(stderr, "EEE <%s>\n", arg);
84 12
        ALLOC_OBJ(vp, VEXT_MAGIC);
85 12
        AN(vp);
86 12
        vp->argv = VAV_Parse(arg, NULL, ARGV_COMMA);
87 12
        AN(vp->argv);
88 12
        if (vp->argv[0] != NULL)
89 0
                ARGV_ERR("\tParse failure in argument: %s\n\t%s\n",
90
                    arg, vp->argv[0]);
91 12
        VTAILQ_INSERT_TAIL(&vext_list, vp, list);
92
93 12
        if (strchr(vp->argv[1], '/'))
94 4
                bstrcpy(fn, vp->argv[1]);
95
        else
96 8
                bprintf(fn, "libvmod_%s.so", vp->argv[1]);
97
98 12
        VFIL_setpath(&vmod_path, mgt_vmod_path);
99 12
        if (VFIL_searchpath(vmod_path, vext_tryopen, vp, fn, &fno)) {
100 4
                ARGV_ERR("\tCannot open %s\n\t%s\n",
101
                    fn, strerror(errno));
102 0
        }
103
104 8
        fprintf(stderr, "eee <%s>\n", fno);
105 8
        free(fno);
106 8
}
107
108
void
109 6376
vext_iter(vext_iter_f *func, void *priv)
110
{
111
        struct vext *vp;
112
113 6384
        VTAILQ_FOREACH(vp, &vext_list, list)
114 8
                func(VSB_data(vp->vsb), priv);
115 6376
}
116
117
void
118 4060
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 4068
        VTAILQ_FOREACH(vp, &vext_list, list) {
128 8
                if (vp->vsb == NULL) {
129 8
                        vp->vsb = VSB_new_auto();
130 8
                        AN(vp->vsb);
131 8
                }
132 8
                VSB_clear(vp->vsb);
133 8
                p = strrchr(vp->argv[1], '/');
134 8
                if (p != NULL)
135 4
                        p++;
136
                else
137 4
                        p = vp->argv[1];
138 8
                VSB_printf(vi, ",-E%s", p);
139 8
                VSB_printf(vp->vsb, "vext_cache/%s,", p);
140 72
                for (i = 0; i < 8; i++) {
141 64
                        AZ(VRND_RandomCrypto(&u, sizeof u));
142 64
                        u %= 26;
143 64
                        VSB_printf(vp->vsb, "%c", 'a' + (char)u);
144 64
                }
145 8
                VSB_cat(vp->vsb, ".so");
146 8
                AZ(VSB_finish(vp->vsb));
147 8
                fprintf(stderr, "ee2 %s\n", VSB_data(vp->vsb));
148 8
                fdo = open(VSB_data(vp->vsb), O_WRONLY|O_CREAT|O_EXCL, 0755);
149 8
                xxxassert(fdo >= 0);
150 8
                AZ(lseek(vp->fd, 0, SEEK_SET));
151 8
                do {
152 1360
                        sz = read(vp->fd, buf, sizeof buf);
153 1360
                        if (sz > 0) {
154 1352
                                szw = write(fdo, buf, sz);
155 1352
                                xxxassert(szw == sz);
156 1352
                        }
157 1360
                } while (sz > 0);
158 8
                closefd(&fdo);
159 8
                closefd(&vp->fd);
160 8
        }
161 4060
}
162
163
void
164 3920
vext_load(void)
165
{
166
        struct vext *vp;
167
168 3928
        VTAILQ_FOREACH(vp, &vext_list, list) {
169 8
                vp->dlptr = dlopen(
170 8
                    VSB_data(vp->vsb),
171
                    RTLD_NOW | RTLD_GLOBAL
172
                );
173 8
                if (vp->dlptr == NULL) {
174 0
                        XXXAN(vp->dlptr);
175 0
                }
176 8
                fprintf(stderr, "Loaded -E %s\n", VSB_data(vp->vsb));
177 8
        }
178 3920
}
179
180
void
181 3992
vext_cleanup(int do_unlink)
182
{
183
        struct vext *vp;
184
185 4000
        VTAILQ_FOREACH(vp, &vext_list, list) {
186 8
                if (vp->vsb != NULL && VSB_len(vp->vsb) > 0) {
187 8
                        if (do_unlink)
188 8
                                XXXAZ(unlink(VSB_data(vp->vsb)));
189 8
                        VSB_clear(vp->vsb);
190 8
                }
191 8
        }
192 3992
}