varnish-cache/bin/varnishd/common/common_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 <string.h>
40
#include <unistd.h>
41
42
#include "vdef.h"
43
#include "vas.h"
44
#include "miniobj.h"
45
#include "vav.h"
46
#include "vqueue.h"
47
#include "vrnd.h"
48
#include "vsb.h"
49
50
#include "heritage.h"
51
52
struct vext {
53
        unsigned                magic;
54
#define VEXT_MAGIC              0xd5063ef6
55
        VTAILQ_ENTRY(vext)      list;
56
57
        char                    **argv;
58
        int                     fd;
59
        struct vsb              *vsb;
60
        void                    *dlptr;
61
};
62
63
static VTAILQ_HEAD(,vext) vext_list =
64
    VTAILQ_HEAD_INITIALIZER(vext_list);
65
66
void
67 40
vext_argument(const char *arg)
68
{
69
        struct vext *vp;
70
71 40
        fprintf(stderr, "EEE <%s>\n", arg);
72 40
        ALLOC_OBJ(vp, VEXT_MAGIC);
73 40
        AN(vp);
74 40
        vp->argv = VAV_Parse(arg, NULL, ARGV_COMMA);
75 40
        AN(vp->argv);
76 40
        if (vp->argv[0] != NULL)
77 0
                ARGV_ERR("\tParse failure in argument: %s\n\t%s\n",
78
                    arg, vp->argv[0]);
79 40
        VTAILQ_INSERT_TAIL(&vext_list, vp, list);
80 40
        fprintf(stderr, "eee <%s>\n", vp->argv[1]);
81 40
        vp->fd = open(vp->argv[1], O_RDONLY);
82 40
        if (vp->fd < 0)
83 0
                ARGV_ERR("\tCannot open %s\n\t%s\n",
84
                    vp->argv[1], strerror(errno));
85 40
}
86
87
void
88 59920
vext_iter(vext_iter_f *func, void *priv)
89
{
90
        struct vext *vp;
91
92 59960
        VTAILQ_FOREACH(vp, &vext_list, list)
93 40
                func(VSB_data(vp->vsb), priv);
94 59920
}
95
96
void
97 38080
vext_copyin(struct vsb *vident)
98
{
99
        struct vext *vp;
100
        const char *p;
101
        int i, fdo;
102
        unsigned u;
103
        char buf[BUFSIZ];
104
        ssize_t sz, szw;
105
106 38120
        VTAILQ_FOREACH(vp, &vext_list, list) {
107 40
                if (vp->vsb == NULL) {
108 40
                        vp->vsb = VSB_new_auto();
109 40
                        AN(vp->vsb);
110 40
                }
111 40
                VSB_clear(vp->vsb);
112 40
                p = strrchr(vp->argv[1], '/');
113 40
                if (p != NULL)
114 40
                        p++;
115
                else
116 0
                        p = vp->argv[0];
117 40
                VSB_printf(vident, ",-E%s", p);
118 40
                VSB_printf(vp->vsb, "vext_cache/%s,", p);
119 360
                for (i = 0; i < 8; i++) {
120 320
                        AZ(VRND_RandomCrypto(&u, sizeof u));
121 320
                        u %= 26;
122 320
                        VSB_printf(vp->vsb, "%c", 'a' + (char)u);
123 320
                }
124 40
                VSB_cat(vp->vsb, ".so");
125 40
                AZ(VSB_finish(vp->vsb));
126 40
                fprintf(stderr, "ee2 %s\n", VSB_data(vp->vsb));
127 40
                fdo = open(VSB_data(vp->vsb), O_WRONLY|O_CREAT|O_EXCL, 0755);
128 40
                xxxassert(fdo >= 0);
129 40
                AZ(lseek(vp->fd, 0, SEEK_SET));
130 40
                do {
131 6960
                        sz = read(vp->fd, buf, sizeof buf);
132 6960
                        if (sz > 0) {
133 6920
                                szw = write(fdo, buf, sz);
134 6920
                                xxxassert(szw == sz);
135 6920
                        }
136 6960
                } while (sz > 0);
137 40
                closefd(&fdo);
138 40
                closefd(&vp->fd);
139 40
        }
140 38080
}
141
142
void
143 36836
vext_load(void)
144
{
145
        struct vext *vp;
146
147 36876
        VTAILQ_FOREACH(vp, &vext_list, list) {
148 40
                vp->dlptr = dlopen(
149 40
                    VSB_data(vp->vsb),
150
                    RTLD_NOW | RTLD_GLOBAL
151
                );
152 40
                if (vp->dlptr == NULL) {
153 0
                        XXXAN(vp->dlptr);
154 0
                }
155 40
                fprintf(stderr, "Loaded -E %s\n", VSB_data(vp->vsb));
156 40
        }
157 36836
}
158
159
void
160 37360
vext_cleanup(int do_unlink)
161
{
162
        struct vext *vp;
163
164 37400
        VTAILQ_FOREACH(vp, &vext_list, list) {
165 40
                if (vp->vsb != NULL && VSB_len(vp->vsb) > 0) {
166 40
                        if (do_unlink)
167 40
                                XXXAZ(unlink(VSB_data(vp->vsb)));
168 40
                        VSB_clear(vp->vsb);
169 40
                }
170 40
        }
171 37360
}