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