Code Coverage: vinyl-cache/bin/vinyld/mgt/mgt_util.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2011 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
 * The management process, various utility functions
31
 */
32
33
#include "config.h"
34
35
#include <sys/utsname.h>
36
37
#include <stdarg.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <syslog.h>
42
#include <unistd.h>
43
44
#include "mgt/mgt.h"
45
46
#include "common/heritage.h"
47
48
#include "vav.h"
49
#include "vct.h"
50
51
int complain_to_stderr;
52
53
/*--------------------------------------------------------------------*/
54
55
char *
56
mgt_HostName(void)
57 24
{
58 24
        char *p;
59 24
        char buf[1024];
60
61 24
        AZ(gethostname(buf, sizeof buf));
62 24
        p = strdup(buf);
63 24
        AN(p);
64 24
        return (p);
65 24
}
66
67
/*--------------------------------------------------------------------*/
68
69
void
70
mgt_ProcTitle(const char *comp)
71 2081
{
72 2081
#ifdef HAVE_SETPROCTITLE
73 2081
        if (vstrcmp(heritage.identity, "vinyld"))
74 2081
                setproctitle("vinyld-%s -i %s", comp, heritage.identity);
75 0
        else
76 0
                setproctitle("vinyld-%s", comp);
77
#else
78
        (void)comp;
79
#endif
80 2081
}
81
82
/*--------------------------------------------------------------------*/
83
84
static void
85
mgt_sltm(const char *tag, const char *sdesc, const char *ldesc)
86 94
{
87 94
        int i;
88
89 94
        assert(sdesc != NULL && ldesc != NULL);
90 94
        assert(*sdesc != '\0' || *ldesc != '\0');
91 94
        printf("\n%s\n", tag);
92 94
        i = vstrlen(tag);
93 94
        printf("%*.*s\n\n", i, i, "------------------------------------");
94 94
        if (*ldesc != '\0')
95 89
                printf("%s\n", ldesc);
96 5
        else if (*sdesc != '\0')
97 5
                printf("%s\n", sdesc);
98 94
}
99
100
/*lint -e{506} constant value boolean */
101
void
102
mgt_DumpRstVsl(void)
103 1
{
104
105 1
        printf(
106 1
            "\n.. The following is autogenerated output from "
107 1
            "vinyld -x vsl\n\n");
108
109 94
#define SLTM(tag, flags, sdesc, ldesc) mgt_sltm(#tag, sdesc, ldesc);
110 1
#include "tbl/vsl_tags.h"
111 1
}
112
113
/*--------------------------------------------------------------------*/
114
115
struct vsb *
116
mgt_BuildVident(void)
117 1099
{
118 1099
        struct utsname uts;
119 1099
        struct vsb *vsb;
120
121 1099
        vsb = VSB_new_auto();
122 1099
        AN(vsb);
123 1099
        if (!uname(&uts)) {
124 1099
                VSB_printf(vsb, ",%s", uts.sysname);
125 1099
                VSB_printf(vsb, ",%s", uts.release);
126 1099
                VSB_printf(vsb, ",%s", uts.machine);
127 1099
        }
128 1099
        return (vsb);
129 1099
}
130
131
/*--------------------------------------------------------------------
132
 * 'Ello, I wish to register a complaint...
133
 */
134
135
#ifndef LOG_AUTHPRIV
136
#  define LOG_AUTHPRIV 0
137
#endif
138
139
const char C_ERR[] = "Error:";
140
const char C_INFO[] = "Info:";
141
const char C_DEBUG[] = "Debug:";
142
const char C_SECURITY[] = "Security:";
143
const char C_CLI[] = "Cli:";
144
145
void
146
MGT_ComplainVSB(const char *loud, struct vsb *vsb)
147 42158
{
148 42158
        int sf;
149
150 42158
        AZ(VSB_finish(vsb));
151
152 42158
        if (VSB_len(vsb) == 0 ||
153 42158
            (loud == C_CLI && !mgt_param.syslog_cli_traffic))
154 31296
                return;
155
156 10862
        if (loud == C_ERR)
157 67
                sf = LOG_ERR;
158 10795
        else if (loud == C_INFO)
159 5532
                sf = LOG_INFO;
160 5263
        else if (loud == C_DEBUG)
161 5153
                sf = LOG_DEBUG;
162 110
        else if (loud == C_SECURITY)
163 0
                sf = LOG_WARNING | LOG_AUTHPRIV;
164 110
        else if (loud == C_CLI)
165 110
                sf = LOG_INFO;
166 0
        else
167 0
                WRONG("Wrong complaint loudness");
168
169 10862
        if (loud != C_CLI && (complain_to_stderr || loud != C_DEBUG))
170 10730
                fprintf(stderr, "%s %s\n", loud, VSB_data(vsb));
171
172 10862
        if (!MGT_DO_DEBUG(DBG_VTC_MODE))
173 198
                syslog(sf, "%s", VSB_data(vsb));
174 10862
}
175
176
void
177
MGT_Complain(const char *loud, const char *fmt, ...)
178 39021
{
179 39021
        va_list ap;
180 39021
        struct vsb *vsb;
181
182 39021
        vsb = VSB_new_auto();
183 39021
        AN(vsb);
184 39021
        va_start(ap, fmt);
185 39021
        VSB_vprintf(vsb, fmt, ap);
186 39021
        va_end(ap);
187 39021
        MGT_ComplainVSB(loud, vsb);
188 39021
        VSB_destroy(&vsb);
189 39021
}
190
191
/*--------------------------------------------------------------------*/
192
193
const void *
194
MGT_Pick(const struct choice *cp, const char *which, const char *kind)
195 1055
{
196
197 4173
        for (; cp->name != NULL; cp++) {
198 4172
                if (!vstrcmp(cp->name, which))
199 1054
                        return (cp->ptr);
200 4172
        }
201 1
        ARGV_ERR("Unknown %s method \"%s\"\n", kind, which);
202 1
}
203
204
/*--------------------------------------------------------------------*/
205
206
char **
207
MGT_NamedArg(const char *spec, const char **name, const char *what)
208 4239
{
209 4239
        const char *p, *q;
210 4239
        char *r;
211 4239
        char **av;
212 4239
        int l;
213
214 4239
        ASSERT_MGT();
215 4239
        p = strchr(spec, '=');
216 4239
        q = strchr(spec, ',');
217 4239
        if (p == NULL || (q != NULL && q < p)) {
218 2106
                av = VAV_Parse(spec, NULL, ARGV_COMMA);
219 2106
                p = NULL;
220 2133
        } else if (VCT_invalid_name(spec, p) != NULL) {
221 1
                ARGV_ERR("invalid %s name \"%.*s\"=[...]\n",
222 1
                    what, (int)(p - spec), spec);
223 2132
        } else if (p[1] == '\0') {
224 1
                ARGV_ERR("Empty named %s argument \"%s\"\n", what, spec);
225 2131
        } else {
226 2131
                av = VAV_Parse(p + 1, NULL, ARGV_COMMA);
227 2131
        }
228 4237
        AN(av);
229
230 4237
        if (av[0] != NULL)
231 0
                ARGV_ERR("%s\n", av[0]);
232 4237
        if (p == NULL) {
233 2106
                *name = NULL;
234 2131
        } else {
235 2131
                l = p - spec;
236 2131
                r = malloc(1L + l);
237 2131
                AN(r);
238 2131
                vmemcpy(r, spec, l);
239 2131
                r[l] = '\0';
240 2131
                *name = r;
241 2131
        }
242 4237
        return (av);
243 4237
}