varnish-cache/bin/varnishd/mgt/mgt_util.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2011 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * The management process, various utility functions
30
 */
31
32
#include "config.h"
33
34
#include <sys/utsname.h>
35
36
#include <stdarg.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <syslog.h>
41
#include <unistd.h>
42
43
#include "mgt/mgt.h"
44
45
#include "common/heritage.h"
46
47
#include "vav.h"
48
#include "vct.h"
49
50
/*--------------------------------------------------------------------*/
51
52
char *
53 1258
mgt_HostName(void)
54
{
55
        char *p;
56
        char buf[1024];
57
58 1258
        AZ(gethostname(buf, sizeof buf));
59 1258
        p = strdup(buf);
60 1258
        AN(p);
61 1258
        return (p);
62
}
63
64
/*--------------------------------------------------------------------*/
65
66
void
67 2490
mgt_ProcTitle(const char *comp)
68
{
69
#ifdef HAVE_SETPROCTITLE
70 2490
        if (strcmp(heritage.identity, "varnishd"))
71 2490
                setproctitle("Varnish-%s -i %s", comp, heritage.identity);
72
        else
73 0
                setproctitle("Varnish-%s", comp);
74
#else
75
        (void)comp;
76
#endif
77 2490
}
78
79
/*--------------------------------------------------------------------*/
80
81
static void
82 364
mgt_sltm(const char *tag, const char *sdesc, const char *ldesc)
83
{
84
        int i;
85
86 364
        assert(sdesc != NULL && ldesc != NULL);
87 364
        assert(*sdesc != '\0' || *ldesc != '\0');
88 364
        printf("\n%s\n", tag);
89 364
        i = strlen(tag);
90 364
        printf("%*.*s\n\n", i, i, "------------------------------------");
91 364
        if (*ldesc != '\0')
92 344
                printf("%s\n", ldesc);
93 20
        else if (*sdesc != '\0')
94 20
                printf("%s\n", sdesc);
95 364
}
96
97
/*lint -e{506} constant value boolean */
98
void
99 4
mgt_DumpRstVsl(void)
100
{
101
102 4
        printf(
103
            "\n.. The following is autogenerated output from "
104
            "varnishd -x vsl\n\n");
105
106
#define SLTM(tag, flags, sdesc, ldesc) mgt_sltm(#tag, sdesc, ldesc);
107
#include "tbl/vsl_tags.h"
108 4
}
109
110
/*--------------------------------------------------------------------*/
111
112
struct vsb *
113 1324
mgt_BuildVident(void)
114
{
115
        struct utsname uts;
116
        struct vsb *vsb;
117
118 1324
        vsb = VSB_new_auto();
119 1324
        AN(vsb);
120 1324
        if (!uname(&uts)) {
121 1324
                VSB_printf(vsb, ",%s", uts.sysname);
122 1324
                VSB_printf(vsb, ",%s", uts.release);
123 1324
                VSB_printf(vsb, ",%s", uts.machine);
124
        }
125 1324
        return (vsb);
126
}
127
128
/*--------------------------------------------------------------------
129
 * 'Ello, I wish to register a complaint...
130
 */
131
132
#ifndef LOG_AUTHPRIV
133
#  define LOG_AUTHPRIV 0
134
#endif
135
136
const char C_ERR[] = "Error:";
137
const char C_INFO[] = "Info:";
138
const char C_DEBUG[] = "Debug:";
139
const char C_SECURITY[] = "Security:";
140
const char C_CLI[] = "Cli:";
141
142
void
143 45572
MGT_Complain(const char *loud, const char *fmt, ...)
144
{
145
        va_list ap;
146
        struct vsb *vsb;
147
        int sf;
148
149 45572
        if (loud == C_CLI && !mgt_param.syslog_cli_traffic)
150 79584
                return;
151 11560
        vsb = VSB_new_auto();
152 11560
        AN(vsb);
153 11560
        va_start(ap, fmt);
154 11560
        VSB_vprintf(vsb, fmt, ap);
155 11560
        va_end(ap);
156 11560
        AZ(VSB_finish(vsb));
157
158 11560
        if (loud == C_ERR)
159 34
                sf = LOG_ERR;
160 11526
        else if (loud == C_INFO)
161 6466
                sf = LOG_INFO;
162 5060
        else if (loud == C_DEBUG)
163 4928
                sf = LOG_DEBUG;
164 132
        else if (loud == C_SECURITY)
165 0
                sf = LOG_WARNING | LOG_AUTHPRIV;
166 132
        else if (loud == C_CLI)
167 132
                sf = LOG_INFO;
168
        else
169 0
                WRONG("Wrong complaint loudness");
170
171 11560
        if (loud != C_CLI)
172 11428
                fprintf(stderr, "%s %s\n", loud, VSB_data(vsb));
173
174 11560
        if (!MGT_DO_DEBUG(DBG_VTC_MODE))
175 158
                syslog(sf, "%s", VSB_data(vsb));
176 11560
        VSB_destroy(&vsb);
177
}
178
179
/*--------------------------------------------------------------------*/
180
181
const void *
182 3818
MGT_Pick(const struct choice *cp, const char *which, const char *kind)
183
{
184
185 17612
        for (; cp->name != NULL; cp++) {
186 17608
                if (!strcmp(cp->name, which))
187 7628
                        return (cp->ptr);
188
        }
189 4
        ARGV_ERR("Unknown %s method \"%s\"\n", kind, which);
190
}
191
192
/*--------------------------------------------------------------------*/
193
194
char **
195 3834
MGT_NamedArg(const char *spec, const char **name, const char *what)
196
{
197
        const char *p, *q;
198
        char *r;
199
        char **av;
200
        int l;
201
202 3834
        ASSERT_MGT();
203 3834
        p = strchr(spec, '=');
204 3834
        q = strchr(spec, ',');
205 3834
        if (p == NULL || (q != NULL && q < p)) {
206 2532
                av = VAV_Parse(spec, NULL, ARGV_COMMA);
207 2532
                p = NULL;
208 1302
        } else if (VCT_invalid_name(spec, p) != NULL) {
209 2
                ARGV_ERR("invalid %s name \"%.*s\"=[...]\n",
210
                    what, (int)(p - spec), spec);
211 1300
        } else if (p[1] == '\0') {
212 2
                ARGV_ERR("Empty named %s argument \"%s\"\n", what, spec);
213
        } else {
214 1298
                av = VAV_Parse(p + 1, NULL, ARGV_COMMA);
215
        }
216 3830
        AN(av);
217
218 3830
        if (av[0] != NULL)
219 0
                ARGV_ERR("%s\n", av[0]);
220 3830
        if (p == NULL) {
221 2532
                *name = NULL;
222
        } else {
223 1298
                l = p - spec;
224 1298
                r = malloc(1L + l);
225 1298
                AN(r);
226 1298
                memcpy(r, spec, l);
227 1298
                r[l] = '\0';
228 1298
                *name = r;
229
        }
230 3830
        return (av);
231
}