blob: 568d042206d8208dbdd2139d9e21543281cb4c13 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020014#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020015#include <common/errors.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020016#include <common/time.h>
17
Willy Tarreauec6c5df2008-07-15 00:22:45 +020018#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019
Willy Tarreau21faa912012-10-10 08:27:36 +020020/* List head of all known server keywords */
21static struct srv_kw_list srv_keywords = {
22 .list = LIST_HEAD_INIT(srv_keywords.list)
23};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020024
Willy Tarreau21faa912012-10-10 08:27:36 +020025int srv_downtime(struct server *s)
26{
Krzysztof Oledzki85130942007-10-22 16:21:10 +020027 if ((s->state & SRV_RUNNING) && s->last_change < now.tv_sec) // ignore negative time
28 return s->down_time;
29
30 return now.tv_sec - s->last_change + s->down_time;
31}
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
Willy Tarreau21faa912012-10-10 08:27:36 +020033int srv_getinter(struct server *s)
34{
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010035 if ((s->state & SRV_CHECKED) && (s->health == s->rise + s->fall - 1))
36 return s->inter;
37
38 if (!(s->state & SRV_RUNNING) && s->health==0)
39 return (s->downinter)?(s->downinter):(s->inter);
40
41 return (s->fastinter)?(s->fastinter):(s->inter);
42}
43
Willy Tarreau21faa912012-10-10 08:27:36 +020044/*
45 * Registers the server keyword list <kwl> as a list of valid keywords for next
46 * parsing sessions.
47 */
48void srv_register_keywords(struct srv_kw_list *kwl)
49{
50 LIST_ADDQ(&srv_keywords.list, &kwl->list);
51}
52
53/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
54 * keyword is found with a NULL ->parse() function, then an attempt is made to
55 * find one with a valid ->parse() function. This way it is possible to declare
56 * platform-dependant, known keywords as NULL, then only declare them as valid
57 * if some options are met. Note that if the requested keyword contains an
58 * opening parenthesis, everything from this point is ignored.
59 */
60struct srv_kw *srv_find_kw(const char *kw)
61{
62 int index;
63 const char *kwend;
64 struct srv_kw_list *kwl;
65 struct srv_kw *ret = NULL;
66
67 kwend = strchr(kw, '(');
68 if (!kwend)
69 kwend = kw + strlen(kw);
70
71 list_for_each_entry(kwl, &srv_keywords.list, list) {
72 for (index = 0; kwl->kw[index].kw != NULL; index++) {
73 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
74 kwl->kw[index].kw[kwend-kw] == 0) {
75 if (kwl->kw[index].parse)
76 return &kwl->kw[index]; /* found it !*/
77 else
78 ret = &kwl->kw[index]; /* may be OK */
79 }
80 }
81 }
82 return ret;
83}
84
85/* Dumps all registered "server" keywords to the <out> string pointer. The
86 * unsupported keywords are only dumped if their supported form was not
87 * found.
88 */
89void srv_dump_kws(char **out)
90{
91 struct srv_kw_list *kwl;
92 int index;
93
94 *out = NULL;
95 list_for_each_entry(kwl, &srv_keywords.list, list) {
96 for (index = 0; kwl->kw[index].kw != NULL; index++) {
97 if (kwl->kw[index].parse ||
98 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
99 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
100 kwl->scope,
101 kwl->kw[index].kw,
102 kwl->kw[index].skip ? " <arg>" : "",
103 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
104 kwl->kw[index].parse ? "" : " (not supported)");
105 }
106 }
107 }
108}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100109
Willy Tarreaudff55432012-10-10 17:51:05 +0200110/* parse the "id" server keyword */
111static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
112{
113 struct eb32_node *node;
114
115 if (!*args[*cur_arg + 1]) {
116 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
117 return ERR_ALERT | ERR_FATAL;
118 }
119
120 newsrv->puid = atol(args[*cur_arg + 1]);
121 newsrv->conf.id.key = newsrv->puid;
122
123 if (newsrv->puid <= 0) {
124 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
125 return ERR_ALERT | ERR_FATAL;
126 }
127
128 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
129 if (node) {
130 struct server *target = container_of(node, struct server, conf.id);
131 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
132 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
133 target->id);
134 return ERR_ALERT | ERR_FATAL;
135 }
136
137 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
138 return 0;
139}
140
141/* Note: must not be declared <const> as its list will be overwritten.
142 * Please take care of keeping this list alphabetically sorted, doing so helps
143 * all code contributors.
144 * Optional keywords are also declared with a NULL ->parse() function so that
145 * the config parser can report an appropriate error when a known keyword was
146 * not enabled.
147 */
148static struct srv_kw_list srv_kws = { "ALL", { }, {
149 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
150 { NULL, NULL, 0 },
151}};
152
153__attribute__((constructor))
154static void __listener_init(void)
155{
156 srv_register_keywords(&srv_kws);
157}
158
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159/*
160 * Local variables:
161 * c-indent-level: 8
162 * c-basic-offset: 8
163 * End:
164 */