varnish-cache/bin/varnishd/acceptor/mgt_acceptor.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
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
 * Acceptor socket management
31
 */
32
33
#include "config.h"
34
35
#include <sys/types.h>
36
#include <sys/socket.h>
37
#include <sys/un.h>
38
#include <sys/stat.h>
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <string.h>
42
#include <unistd.h>
43
44
#include "mgt/mgt.h"
45
#include "acceptor/cache_acceptor.h"    // XXX mgt_acceptor should not use
46
#include "acceptor/mgt_acceptor.h"
47
#include "common/heritage.h"
48
49
#include "vav.h"
50
#include "vus.h"
51
52
static VTAILQ_HEAD(,listen_arg) listen_args =
53
    VTAILQ_HEAD_INITIALIZER(listen_args);
54
55
static VTAILQ_HEAD(,acceptor) acceptors = VTAILQ_HEAD_INITIALIZER(acceptors);
56
57
int
58 920346
VCA__iter(struct acceptor ** const pvca)
59
{
60
61 920346
        AN(pvca);
62 920346
        CHECK_OBJ_ORNULL(*pvca, ACCEPTOR_MAGIC);
63 920346
        if (*pvca != NULL)
64 547962
                *pvca = VTAILQ_NEXT(*pvca, list);
65
        else
66 372384
                *pvca = VTAILQ_FIRST(&acceptors);
67 920346
        return (*pvca != NULL);
68
}
69
70
static struct acceptor *
71 119280
VCA_Find(const char *name)
72
{
73
        struct acceptor *vca;
74
75 161040
        VCA_Foreach(vca) {
76 81360
                CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
77
78 81360
                if (!strcmp(vca->name, name))
79 39600
                        return (vca);
80
        }
81
82 79680
        return (NULL);
83 119280
}
84
85
/*=====================================================================
86
 * Reopen the accept sockets to get rid of listen status.
87
 * returns the highest errno encountered, 0 for success
88
 */
89
90
int
91 37600
VCA_reopen_sockets(void)
92
{
93
        struct acceptor *vca;
94
        int fail;
95
        int err;
96
97 37600
        fail = 0;
98
99 112800
        VCA_Foreach(vca) {
100 75200
                CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
101 75200
                err = vca->reopen();
102 75200
                fail = vmax(fail, err);
103
        }
104
105 37600
        return (fail);
106
}
107
108
/*--------------------------------------------------------------------*/
109
110
void
111 39600
VCA_Arg(const char *spec)
112
{
113
        struct acceptor *vca;
114
        char **av;
115
        struct listen_arg *la;
116
        const char *err;
117
        int error;
118
        const char *name;
119
        char name_buf[8];
120
        static unsigned seq = 0;
121
122 39600
        av = MGT_NamedArg(spec, &name, "-a");
123 39600
        AN(av);
124
125 39600
        ALLOC_OBJ(la, LISTEN_ARG_MAGIC);
126 39600
        AN(la);
127 39600
        VTAILQ_INIT(&la->socks);
128 39600
        VTAILQ_INSERT_TAIL(&listen_args, la, list);
129 39600
        la->endpoint = av[1];
130
131 39600
        if (name == NULL) {
132 39320
                bprintf(name_buf, "a%u", seq++);
133 39320
                name = strdup(name_buf);
134 39320
                AN(name);
135 39320
        }
136
137 39600
        la->name = name;
138
139 39600
        if (VUS_is(la->endpoint))
140 1920
                vca = VCA_Find("uds");
141
        else
142 37680
                vca = VCA_Find("tcp");
143
144 39600
        AN(vca);
145 39600
        error = vca->open(av + 2, la, &err);
146
147 39600
        if (error) {
148 40
                ARGV_ERR("Got no socket(s) for %s=%s (%s)\n",
149
                    la->name, la->endpoint, err);
150 0
        }
151 39560
        else if (VTAILQ_EMPTY(&la->socks)) {
152 0
                ARGV_ERR("Got no socket(s) for %s=%s\n",
153
                    la->name, la->endpoint);
154 0
        }
155 39560
        VAV_Free(av);
156 39560
}
157
158
void
159 79680
VCA_Add(struct acceptor *vca)
160
{
161
162 79680
        CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
163 79680
        AN(vca->name);
164 79680
        AN(vca->config);
165 79680
        AN(vca->init);
166 79680
        AN(vca->open);
167 79680
        AN(vca->reopen);
168 79680
        AN(vca->start);
169 79680
        AN(vca->event);
170 79680
        AN(vca->accept);
171 79680
        AN(vca->update);
172 79680
        AN(vca->shutdown);
173
174 79680
        if (VCA_Find(vca->name) != NULL)
175 0
                ARGV_ERR("Acceptor '%s' already exist\n", vca->name);
176
177 79680
        VTAILQ_INIT(&vca->socks);
178 79680
        VTAILQ_INSERT_TAIL(&acceptors, vca, list);
179
180 79680
        if (vca->config())
181 0
                ARGV_ERR("Acceptor '%s' failed to initialize\n", vca->name);
182 79680
}
183
184
void
185 39840
VCA_Config(void)
186
{
187
188 39840
        VCA_Add(&TCP_acceptor);
189 39840
        VCA_Add(&UDS_acceptor);
190 39840
}