| | varnish-cache/bin/varnishd/mgt/mgt_util.c |
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 |
880 |
mgt_HostName(void) |
57 |
|
{ |
58 |
|
char *p; |
59 |
|
char buf[1024]; |
60 |
|
|
61 |
880 |
AZ(gethostname(buf, sizeof buf)); |
62 |
880 |
p = strdup(buf); |
63 |
880 |
AN(p); |
64 |
880 |
return (p); |
65 |
|
} |
66 |
|
|
67 |
|
/*--------------------------------------------------------------------*/ |
68 |
|
|
69 |
|
void |
70 |
74830 |
mgt_ProcTitle(const char *comp) |
71 |
|
{ |
72 |
|
#ifdef HAVE_SETPROCTITLE |
73 |
74830 |
if (strcmp(heritage.identity, "varnishd")) |
74 |
74830 |
setproctitle("Varnish-%s -i %s", comp, heritage.identity); |
75 |
|
else |
76 |
0 |
setproctitle("Varnish-%s", comp); |
77 |
|
#else |
78 |
|
(void)comp; |
79 |
|
#endif |
80 |
74830 |
} |
81 |
|
|
82 |
|
/*--------------------------------------------------------------------*/ |
83 |
|
|
84 |
|
static void |
85 |
3720 |
mgt_sltm(const char *tag, const char *sdesc, const char *ldesc) |
86 |
|
{ |
87 |
|
int i; |
88 |
|
|
89 |
3720 |
assert(sdesc != NULL && ldesc != NULL); |
90 |
3720 |
assert(*sdesc != '\0' || *ldesc != '\0'); |
91 |
3720 |
printf("\n%s\n", tag); |
92 |
3720 |
i = strlen(tag); |
93 |
3720 |
printf("%*.*s\n\n", i, i, "------------------------------------"); |
94 |
3720 |
if (*ldesc != '\0') |
95 |
3520 |
printf("%s\n", ldesc); |
96 |
200 |
else if (*sdesc != '\0') |
97 |
200 |
printf("%s\n", sdesc); |
98 |
3720 |
} |
99 |
|
|
100 |
|
/*lint -e{506} constant value boolean */ |
101 |
|
void |
102 |
40 |
mgt_DumpRstVsl(void) |
103 |
|
{ |
104 |
|
|
105 |
40 |
printf( |
106 |
|
"\n.. The following is autogenerated output from " |
107 |
|
"varnishd -x vsl\n\n"); |
108 |
|
|
109 |
|
#define SLTM(tag, flags, sdesc, ldesc) mgt_sltm(#tag, sdesc, ldesc); |
110 |
|
#include "tbl/vsl_tags.h" |
111 |
|
} |
112 |
|
|
113 |
|
/*--------------------------------------------------------------------*/ |
114 |
|
|
115 |
|
struct vsb * |
116 |
39800 |
mgt_BuildVident(void) |
117 |
|
{ |
118 |
|
struct utsname uts; |
119 |
|
struct vsb *vsb; |
120 |
|
|
121 |
39800 |
vsb = VSB_new_auto(); |
122 |
39800 |
AN(vsb); |
123 |
39800 |
if (!uname(&uts)) { |
124 |
39800 |
VSB_printf(vsb, ",%s", uts.sysname); |
125 |
39800 |
VSB_printf(vsb, ",%s", uts.release); |
126 |
39800 |
VSB_printf(vsb, ",%s", uts.machine); |
127 |
39800 |
} |
128 |
39800 |
return (vsb); |
129 |
|
} |
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 |
1468286 |
MGT_Complain(const char *loud, const char *fmt, ...) |
147 |
|
{ |
148 |
|
va_list ap; |
149 |
|
struct vsb *vsb; |
150 |
|
int sf; |
151 |
|
|
152 |
1468286 |
if (loud == C_CLI && !mgt_param.syslog_cli_traffic) |
153 |
1060000 |
return; |
154 |
408286 |
vsb = VSB_new_auto(); |
155 |
408286 |
AN(vsb); |
156 |
408286 |
va_start(ap, fmt); |
157 |
408286 |
VSB_vprintf(vsb, fmt, ap); |
158 |
408286 |
va_end(ap); |
159 |
408286 |
AZ(VSB_finish(vsb)); |
160 |
|
|
161 |
408286 |
if (loud == C_ERR) |
162 |
3046 |
sf = LOG_ERR; |
163 |
405240 |
else if (loud == C_INFO) |
164 |
214040 |
sf = LOG_INFO; |
165 |
191200 |
else if (loud == C_DEBUG) |
166 |
187280 |
sf = LOG_DEBUG; |
167 |
3920 |
else if (loud == C_SECURITY) |
168 |
0 |
sf = LOG_WARNING | LOG_AUTHPRIV; |
169 |
3920 |
else if (loud == C_CLI) |
170 |
3920 |
sf = LOG_INFO; |
171 |
|
else |
172 |
0 |
WRONG("Wrong complaint loudness"); |
173 |
|
|
174 |
408286 |
if (loud != C_CLI && (complain_to_stderr || loud != C_DEBUG)) |
175 |
403486 |
fprintf(stderr, "%s %s\n", loud, VSB_data(vsb)); |
176 |
|
|
177 |
408286 |
if (!MGT_DO_DEBUG(DBG_VTC_MODE)) |
178 |
7610 |
syslog(sf, "%s", VSB_data(vsb)); |
179 |
408286 |
VSB_destroy(&vsb); |
180 |
1468286 |
} |
181 |
|
|
182 |
|
/*--------------------------------------------------------------------*/ |
183 |
|
|
184 |
|
const void * |
185 |
38320 |
MGT_Pick(const struct choice *cp, const char *which, const char *kind) |
186 |
|
{ |
187 |
|
|
188 |
151520 |
for (; cp->name != NULL; cp++) { |
189 |
151480 |
if (!strcmp(cp->name, which)) |
190 |
38280 |
return (cp->ptr); |
191 |
113200 |
} |
192 |
40 |
ARGV_ERR("Unknown %s method \"%s\"\n", kind, which); |
193 |
|
} |
194 |
|
|
195 |
|
/*--------------------------------------------------------------------*/ |
196 |
|
|
197 |
|
char ** |
198 |
116080 |
MGT_NamedArg(const char *spec, const char **name, const char *what) |
199 |
|
{ |
200 |
|
const char *p, *q; |
201 |
|
char *r; |
202 |
|
char **av; |
203 |
|
int l; |
204 |
|
|
205 |
116080 |
ASSERT_MGT(); |
206 |
116080 |
p = strchr(spec, '='); |
207 |
116080 |
q = strchr(spec, ','); |
208 |
116080 |
if (p == NULL || (q != NULL && q < p)) { |
209 |
76480 |
av = VAV_Parse(spec, NULL, ARGV_COMMA); |
210 |
76480 |
p = NULL; |
211 |
116080 |
} else if (VCT_invalid_name(spec, p) != NULL) { |
212 |
40 |
ARGV_ERR("invalid %s name \"%.*s\"=[...]\n", |
213 |
|
what, (int)(p - spec), spec); |
214 |
39560 |
} else if (p[1] == '\0') { |
215 |
40 |
ARGV_ERR("Empty named %s argument \"%s\"\n", what, spec); |
216 |
0 |
} else { |
217 |
39520 |
av = VAV_Parse(p + 1, NULL, ARGV_COMMA); |
218 |
|
} |
219 |
116000 |
AN(av); |
220 |
|
|
221 |
116000 |
if (av[0] != NULL) |
222 |
0 |
ARGV_ERR("%s\n", av[0]); |
223 |
116000 |
if (p == NULL) { |
224 |
76480 |
*name = NULL; |
225 |
76480 |
} else { |
226 |
39520 |
l = p - spec; |
227 |
39520 |
r = malloc(1L + l); |
228 |
39520 |
AN(r); |
229 |
39520 |
memcpy(r, spec, l); |
230 |
39520 |
r[l] = '\0'; |
231 |
39520 |
*name = r; |
232 |
|
} |
233 |
116000 |
return (av); |
234 |
|
} |