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