File: | src/usr.sbin/relayd/carp.c |
Warning: | line 64, column 7 Although the value stored to 'c' is used in the enclosing expression, the value is never actually read from 'c' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: carp.c,v 1.12 2016/08/18 00:45:52 jsg Exp $ */ |
2 | |
3 | /* |
4 | * Copyright (c) 2006 Henning Brauer <henning@openbsd.org> |
5 | * |
6 | * Permission to use, copy, modify, and distribute this software for any |
7 | * purpose with or without fee is hereby granted, provided that the above |
8 | * copyright notice and this permission notice appear in all copies. |
9 | * |
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | */ |
18 | |
19 | #include <sys/types.h> |
20 | #include <sys/socket.h> |
21 | #include <sys/queue.h> |
22 | #include <sys/ioctl.h> |
23 | |
24 | #include <net/if.h> |
25 | |
26 | #include <errno(*__errno()).h> |
27 | #include <string.h> |
28 | #include <stdlib.h> |
29 | #include <unistd.h> |
30 | |
31 | #include "relayd.h" |
32 | |
33 | struct carpgroup { |
34 | TAILQ_ENTRY(carpgroup)struct { struct carpgroup *tqe_next; struct carpgroup **tqe_prev ; } entry; |
35 | char *group; |
36 | int do_demote; |
37 | int changed_by; |
38 | }; |
39 | |
40 | TAILQ_HEAD(carpgroups, carpgroup)struct carpgroups { struct carpgroup *tqh_first; struct carpgroup **tqh_last; } carpgroups = |
41 | TAILQ_HEAD_INITIALIZER(carpgroups){ ((void*)0), &(carpgroups).tqh_first }; |
42 | |
43 | struct carpgroup *carp_group_find(char *group); |
44 | int carp_demote_ioctl(char *, int); |
45 | |
46 | struct carpgroup * |
47 | carp_group_find(char *group) |
48 | { |
49 | struct carpgroup *c; |
50 | |
51 | TAILQ_FOREACH(c, &carpgroups, entry)for((c) = ((&carpgroups)->tqh_first); (c) != ((void*)0 ); (c) = ((c)->entry.tqe_next)) |
52 | if (!strcmp(c->group, group)) |
53 | return (c); |
54 | |
55 | return (NULL((void*)0)); |
56 | } |
57 | |
58 | int |
59 | carp_demote_init(char *group, int force) |
60 | { |
61 | struct carpgroup *c; |
62 | int level; |
63 | |
64 | if ((c = carp_group_find(group)) == NULL((void*)0)) { |
Although the value stored to 'c' is used in the enclosing expression, the value is never actually read from 'c' | |
65 | if ((c = calloc(1, sizeof(struct carpgroup))) == NULL((void*)0)) { |
66 | log_warn("%s: calloc", __func__); |
67 | return (-1); |
68 | } |
69 | if ((c->group = strdup(group)) == NULL((void*)0)) { |
70 | log_warn("%s: strdup", __func__); |
71 | free(c); |
72 | return (-1); |
73 | } |
74 | |
75 | /* only demote if this group already is demoted */ |
76 | if ((level = carp_demote_get(group)) == -1) { |
77 | free(c->group); |
78 | free(c); |
79 | return (-1); |
80 | } |
81 | if (level > 0 || force) |
82 | c->do_demote = 1; |
83 | |
84 | TAILQ_INSERT_TAIL(&carpgroups, c, entry)do { (c)->entry.tqe_next = ((void*)0); (c)->entry.tqe_prev = (&carpgroups)->tqh_last; *(&carpgroups)->tqh_last = (c); (&carpgroups)->tqh_last = &(c)->entry.tqe_next ; } while (0); |
85 | } |
86 | |
87 | return (0); |
88 | } |
89 | |
90 | void |
91 | carp_demote_shutdown(void) |
92 | { |
93 | struct carpgroup *c; |
94 | |
95 | while ((c = TAILQ_FIRST(&carpgroups)((&carpgroups)->tqh_first)) != NULL((void*)0)) { |
96 | TAILQ_REMOVE(&carpgroups, c, entry)do { if (((c)->entry.tqe_next) != ((void*)0)) (c)->entry .tqe_next->entry.tqe_prev = (c)->entry.tqe_prev; else ( &carpgroups)->tqh_last = (c)->entry.tqe_prev; *(c)-> entry.tqe_prev = (c)->entry.tqe_next; ; ; } while (0); |
97 | if (c->do_demote && c->changed_by > 0) |
98 | carp_demote_ioctl(c->group, -c->changed_by); |
99 | |
100 | free(c->group); |
101 | free(c); |
102 | } |
103 | } |
104 | |
105 | int |
106 | carp_demote_get(char *group) |
107 | { |
108 | int s; |
109 | struct ifgroupreq ifgr; |
110 | |
111 | if ((s = socket(AF_INET2, SOCK_DGRAM2, 0)) == -1) { |
112 | log_warn("%s: socket", __func__); |
113 | return (-1); |
114 | } |
115 | |
116 | bzero(&ifgr, sizeof(ifgr)); |
117 | if (strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name)) >= |
118 | sizeof(ifgr.ifgr_name)) { |
119 | log_warn("%s: invalid group", __func__); |
120 | close(s); |
121 | return (-1); |
122 | } |
123 | |
124 | if (ioctl(s, SIOCGIFGATTR(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof (struct ifgroupreq) & 0x1fff) << 16) | ((('i')) << 8) | ((139))), (caddr_t)&ifgr) == -1) { |
125 | if (errno(*__errno()) == ENOENT2) |
126 | log_warnx("%s: group \"%s\" does not exist", |
127 | __func__, group); |
128 | else |
129 | log_warn("%s: ioctl", __func__); |
130 | close(s); |
131 | return (-1); |
132 | } |
133 | |
134 | close(s); |
135 | return ((int)ifgr.ifgr_attribifgr_ifgru.ifgru_attrib.ifg_carp_demoted); |
136 | } |
137 | |
138 | int |
139 | carp_demote_set(char *group, int demote) |
140 | { |
141 | struct carpgroup *c; |
142 | |
143 | if ((c = carp_group_find(group)) == NULL((void*)0)) { |
144 | log_warnx("%s: carp group %s not found", __func__, group); |
145 | return (-1); |
146 | } |
147 | |
148 | if (c->changed_by + demote < 0) { |
149 | log_warnx("%s: changed_by + demote < 0", __func__); |
150 | return (-1); |
151 | } |
152 | |
153 | if (c->do_demote && carp_demote_ioctl(group, demote) == -1) |
154 | return (-1); |
155 | |
156 | c->changed_by += demote; |
157 | |
158 | /* enable demotion when we return to 0, i. e. all sessions up */ |
159 | if (demote < 0 && c->changed_by == 0) |
160 | c->do_demote = 1; |
161 | |
162 | return (0); |
163 | } |
164 | |
165 | int |
166 | carp_demote_reset(char *group, int value) |
167 | { |
168 | int level; |
169 | int demote = 0; |
170 | |
171 | if (value < 0) { |
172 | log_warnx("%s: value < 0", __func__); |
173 | return (-1); |
174 | } |
175 | |
176 | if ((level = carp_demote_get(group)) == -1) |
177 | return (-1); |
178 | if (level == value) |
179 | return (0); |
180 | |
181 | demote -= level; |
182 | demote += value; |
183 | |
184 | if (carp_demote_ioctl(group, demote) == -1) |
185 | return (-1); |
186 | |
187 | return (0); |
188 | } |
189 | |
190 | int |
191 | carp_demote_ioctl(char *group, int demote) |
192 | { |
193 | int s, res; |
194 | struct ifgroupreq ifgr; |
195 | |
196 | if ((s = socket(AF_INET2, SOCK_DGRAM2, 0)) == -1) { |
197 | log_warn("%s: socket", __func__); |
198 | return (-1); |
199 | } |
200 | |
201 | bzero(&ifgr, sizeof(ifgr)); |
202 | if (strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name)) >= |
203 | sizeof(ifgr.ifgr_name)) { |
204 | log_warn("%s: invalid group", __func__); |
205 | close(s); |
206 | return (-1); |
207 | } |
208 | ifgr.ifgr_attribifgr_ifgru.ifgru_attrib.ifg_carp_demoted = demote; |
209 | |
210 | if ((res = ioctl(s, SIOCSIFGATTR((unsigned long)0x80000000 | ((sizeof(struct ifgroupreq) & 0x1fff) << 16) | ((('i')) << 8) | ((140))), (caddr_t)&ifgr)) == -1) |
211 | log_warn("%s: unable to %s the demote state " |
212 | "of group '%s'", __func__, |
213 | (demote > 0) ? "increment" : "decrement", group); |
214 | else |
215 | log_info("%s the demote state of group '%s'", |
216 | (demote > 0) ? "incremented" : "decremented", group); |
217 | |
218 | close(s); |
219 | return (res); |
220 | } |