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 25
vext_argument(const char *arg)
68
{
69
        struct vext *vp;
70
71 25
        fprintf(stderr, "EEE <%s>\n", arg);
72 25
        ALLOC_OBJ(vp, VEXT_MAGIC);
73 25
        AN(vp);
74 25
        vp->argv = VAV_Parse(arg, NULL, ARGV_COMMA);
75 25
        AN(vp->argv);
76 25
        if (vp->argv[0] != NULL)
77 0
                ARGV_ERR("\tParse failure in argument: %s\n\t%s\n",
78
                    arg, vp->argv[0]);
79 25
        VTAILQ_INSERT_TAIL(&vext_list, vp, list);
80 25
        fprintf(stderr, "eee <%s>\n", vp->argv[1]);
81 25
        vp->fd = open(vp->argv[1], O_RDONLY);
82 25
        if (vp->fd < 0)
83 0
                ARGV_ERR("\tCannot open %s\n\t%s\n",
84
                    vp->argv[1], strerror(errno));
85 25
}
86
87
void
88 36500
vext_iter(vext_iter_f *func, void *priv)
89
{
90
        struct vext *vp;
91
92 36525
        VTAILQ_FOREACH(vp, &vext_list, list)
93 25
                func(VSB_data(vp->vsb), priv);
94 36500
}
95
96
void
97 23125
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 23150
        VTAILQ_FOREACH(vp, &vext_list, list) {
107 25
                if (vp->vsb == NULL) {
108 25
                        vp->vsb = VSB_new_auto();
109 25
                        AN(vp->vsb);
110 25
                }
111 25
                VSB_clear(vp->vsb);
112 25
                p = strrchr(vp->argv[1], '/');
113 25
                if (p != NULL)
114 25
                        p++;
115
                else
116 0
                        p = vp->argv[0];
117 25
                VSB_printf(vident, ",-E%s", p);
118 25
                VSB_printf(vp->vsb, "vext_cache/%s,", p);
119 225
                for (i = 0; i < 8; i++) {
120 200
                        AZ(VRND_RandomCrypto(&u, sizeof u));
121 200
                        u %= 26;
122 200
                        VSB_printf(vp->vsb, "%c", 'a' + (char)u);
123 200
                }
124 25
                VSB_cat(vp->vsb, ".so");
125 25
                AZ(VSB_finish(vp->vsb));
126 25
                fprintf(stderr, "ee2 %s\n", VSB_data(vp->vsb));
127 25
                fdo = open(VSB_data(vp->vsb), O_WRONLY|O_CREAT|O_EXCL, 0755);
128 25
                xxxassert(fdo >= 0);
129 25
                AZ(lseek(vp->fd, 0, SEEK_SET));
130 25
                do {
131 4325
                        sz = read(vp->fd, buf, sizeof buf);
132 4325
                        if (sz > 0) {
133 4300
                                szw = write(fdo, buf, sz);
134 4300
                                xxxassert(szw == sz);
135 4300
                        }
136 4325
                } while (sz > 0);
137 25
                closefd(&fdo);
138 25
                closefd(&vp->fd);
139 25
        }
140 23125
}
141
142
void
143 22297
vext_load(void)
144
{
145
        struct vext *vp;
146
147 22322
        VTAILQ_FOREACH(vp, &vext_list, list) {
148 25
                vp->dlptr = dlopen(
149 25
                    VSB_data(vp->vsb),
150
                    RTLD_NOW | RTLD_GLOBAL
151
                );
152 25
                if (vp->dlptr == NULL) {
153 0
                        XXXAN(vp->dlptr);
154 0
                }
155 25
                fprintf(stderr, "Loaded -E %s\n", VSB_data(vp->vsb));
156 25
        }
157 22297
}
158
159
void
160 22700
vext_cleanup(int do_unlink)
161
{
162
        struct vext *vp;
163
164 22725
        VTAILQ_FOREACH(vp, &vext_list, list) {
165 25
                if (vp->vsb != NULL && VSB_len(vp->vsb) > 0) {
166 25
                        if (do_unlink)
167 25
                                XXXAZ(unlink(VSB_data(vp->vsb)));
168 25
                        VSB_clear(vp->vsb);
169 25
                }
170 25
        }
171 22700
}