varnish-cache/vmod/vmod_std_fileread.c
1
/*-
2
 * Copyright (c) 2010-2011 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Sanjoy Das <sanjoy@playingwithpointers.com>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * XXX: It might make sense to use just a single global list of all files
31
 * XXX: and use the call-private pointer to point to the file instance on
32
 * XXX: that list.
33
 * XXX: Duplicates in the global list can be avoided by examining the
34
 * XXX: dev+inode fields of the stat structure.
35
 * XXX: Individual files would need to be refcounted, so they can be
36
 * XXX: deleted when no VCL's reference them.
37
 *
38
 * XXX: We should periodically stat(2) the filename and check if the
39
 * XXX: underlying file has been updated.
40
 */
41
42
#include "config.h"
43
44
#include <stdlib.h>
45
#include <string.h>
46
47
#include "cache/cache.h"
48
49
#include "vfil.h"
50
51
#include "vcc_std_if.h"
52
53
struct frfile {
54
        unsigned                        magic;
55
#define CACHED_FILE_MAGIC 0xa8e9d87a
56
        char                            *file_name;
57
        void                            *contents;
58
        struct vrt_blob                 blob[1];
59
        int                             refcount;
60
        VTAILQ_ENTRY(frfile)            list;
61
};
62
63
static VTAILQ_HEAD(, frfile)    frlist = VTAILQ_HEAD_INITIALIZER(frlist);
64
static pthread_mutex_t          frmtx = PTHREAD_MUTEX_INITIALIZER;
65
66
static void
67 109
fini_frfile(VRT_CTX, void *ptr)
68
{
69
        struct frfile *frf;
70
71 109
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
72 109
        CAST_OBJ_NOTNULL(frf, ptr, CACHED_FILE_MAGIC);
73
74 109
        AZ(pthread_mutex_lock(&frmtx));
75 109
        if (--frf->refcount > 0)
76 105
                frf = NULL;
77
        else
78 4
                VTAILQ_REMOVE(&frlist, frf, list);
79 109
        AZ(pthread_mutex_unlock(&frmtx));
80 109
        if (frf != NULL) {
81 4
                free(frf->contents);
82 4
                free(frf->file_name);
83 4
                FREE_OBJ(frf);
84 4
        }
85 109
}
86
87
static const struct vmod_priv_methods frfile_methods[1] = {{
88
        .magic = VMOD_PRIV_METHODS_MAGIC,
89
        .type = "vmod_std_fileread",
90
        .fini = fini_frfile
91
}};
92
93
static struct frfile *
94 735
find_frfile(struct vmod_priv *priv, VCL_STRING file_name)
95
{
96 735
        struct frfile *frf = NULL;
97
        char *s;
98
        ssize_t sz;
99
100 735
        AN(priv);
101
102 735
        if (file_name == NULL)
103 0
                return (NULL);
104
105 735
        if (priv->priv != NULL) {
106 420
                CAST_OBJ_NOTNULL(frf, priv->priv, CACHED_FILE_MAGIC);
107 420
                if (!strcmp(file_name, frf->file_name))
108 84
                        return (frf);
109 336
        }
110
111 651
        AZ(pthread_mutex_lock(&frmtx));
112 651
        if (frf != NULL)
113 336
                frf->refcount--;
114 1365
        VTAILQ_FOREACH(frf, &frlist, list) {
115 1134
                if (!strcmp(file_name, frf->file_name)) {
116 420
                        frf->refcount++;
117 420
                        break;
118
                }
119 714
        }
120 651
        AZ(pthread_mutex_unlock(&frmtx));
121 651
        if (frf != NULL) {
122 420
                priv->methods = frfile_methods;
123 420
                priv->priv = frf;
124 420
                return (frf);
125
        }
126
127 231
        s = VFIL_readfile(NULL, file_name, &sz);
128 231
        if (s != NULL) {
129 147
                assert(sz > 0);
130 147
                ALLOC_OBJ(frf, CACHED_FILE_MAGIC);
131 147
                AN(frf);
132 147
                REPLACE(frf->file_name, file_name);
133 147
                frf->refcount = 1;
134 147
                frf->contents = s;
135 147
                frf->blob->blob = s;
136 147
                frf->blob->len = (size_t)sz;
137 147
                priv->methods = frfile_methods;
138 147
                priv->priv = frf;
139 147
                AZ(pthread_mutex_lock(&frmtx));
140 147
                VTAILQ_INSERT_HEAD(&frlist, frf, list);
141 147
                AZ(pthread_mutex_unlock(&frmtx));
142 147
        }
143 231
        return (frf);
144 735
}
145
146
VCL_STRING v_matchproto_(td_std_fileread)
147 546
vmod_fileread(VRT_CTX, struct vmod_priv *priv, VCL_STRING file_name)
148
{
149
        struct frfile *frf;
150
151 546
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
152 546
        AN(priv);
153
154 546
        frf = find_frfile(priv, file_name);
155 546
        if (frf == NULL)
156 84
                return (NULL);
157 462
        return (frf->contents);
158 546
}
159
160
VCL_BLOB v_matchproto_(td_std_blobread)
161 189
vmod_blobread(VRT_CTX, struct vmod_priv *priv, VCL_STRING file_name)
162
{
163
        struct frfile *frf;
164
165 189
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
166 189
        AN(priv);
167
168 189
        frf = find_frfile(priv, file_name);
169 189
        if (frf == NULL)
170 0
                return (NULL);
171 189
        return (frf->blob);
172 189
}