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 919077
VCA__iter(struct acceptor ** const pvca)
59
{
60
61 919077
        AN(pvca);
62 919077
        CHECK_OBJ_ORNULL(*pvca, ACCEPTOR_MAGIC);
63 919077
        if (*pvca != NULL)
64 547182
                *pvca = VTAILQ_NEXT(*pvca, list);
65
        else
66 371895
                *pvca = VTAILQ_FIRST(&acceptors);
67 919077
        return (*pvca != NULL);
68
}
69
70
static struct acceptor *
71 119160
VCA_Find(const char *name)
72
{
73
        struct acceptor *vca;
74
75 160880
        VCA_Foreach(vca) {
76 81280
                CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
77
78 81280
                if (!strcmp(vca->name, name))
79 39560
                        return (vca);
80
        }
81
82 79600
        return (NULL);
83 119160
}
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 37560
VCA_reopen_sockets(void)
92
{
93
        struct acceptor *vca;
94
        int fail;
95
        int err;
96
97 37560
        fail = 0;
98
99 112680
        VCA_Foreach(vca) {
100 75120
                CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
101 75120
                err = vca->reopen();
102 75120
                fail = vmax(fail, err);
103
        }
104
105 37560
        return (fail);
106
}
107
108
/*--------------------------------------------------------------------*/
109
110
void
111 39560
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 39560
        av = MGT_NamedArg(spec, &name, "-a");
123 39560
        AN(av);
124
125 39560
        ALLOC_OBJ(la, LISTEN_ARG_MAGIC);
126 39560
        AN(la);
127 39560
        VTAILQ_INIT(&la->socks);
128 39560
        VTAILQ_INSERT_TAIL(&listen_args, la, list);
129 39560
        la->endpoint = av[1];
130
131 39560
        if (name == NULL) {
132 39280
                bprintf(name_buf, "a%u", seq++);
133 39280
                name = strdup(name_buf);
134 39280
                AN(name);
135 39280
        }
136
137 39560
        la->name = name;
138
139 39560
        if (VUS_is(la->endpoint))
140 1920
                vca = VCA_Find("uds");
141
        else
142 37640
                vca = VCA_Find("tcp");
143
144 39560
        AN(vca);
145 39560
        error = vca->open(av + 2, la, &err);
146
147 39560
        if (error) {
148 40
                ARGV_ERR("Got no socket(s) for %s=%s (%s)\n",
149
                    la->name, la->endpoint, err);
150 0
        }
151 39520
        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 39520
        VAV_Free(av);
156 39520
}
157
158
void
159 79600
VCA_Add(struct acceptor *vca)
160
{
161
162 79600
        CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC);
163 79600
        AN(vca->name);
164 79600
        AN(vca->config);
165 79600
        AN(vca->init);
166 79600
        AN(vca->open);
167 79600
        AN(vca->reopen);
168 79600
        AN(vca->start);
169 79600
        AN(vca->event);
170 79600
        AN(vca->accept);
171 79600
        AN(vca->update);
172 79600
        AN(vca->shutdown);
173
174 79600
        if (VCA_Find(vca->name) != NULL)
175 0
                ARGV_ERR("Acceptor '%s' already exist\n", vca->name);
176
177 79600
        VTAILQ_INIT(&vca->socks);
178 79600
        VTAILQ_INSERT_TAIL(&acceptors, vca, list);
179
180 79600
        if (vca->config())
181 0
                ARGV_ERR("Acceptor '%s' failed to initialize\n", vca->name);
182 79600
}
183
184
void
185 39800
VCA_Config(void)
186
{
187
188 39800
        VCA_Add(&TCP_acceptor);
189 39800
        VCA_Add(&UDS_acceptor);
190 39800
}