diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-28 12:35:46 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-28 12:35:46 +0000 |
commit | d4de975cd2860e321b44211ecef4d61b050f1304 (patch) | |
tree | 74ab2687bc80adc0a0b66010b9670b493c506bff | |
parent | 8b6024449f9b2eb69bf4b36e1d7e9c2b7e7b30fc (diff) | |
download | busybox-w32-d4de975cd2860e321b44211ecef4d61b050f1304.tar.gz busybox-w32-d4de975cd2860e321b44211ecef4d61b050f1304.tar.bz2 busybox-w32-d4de975cd2860e321b44211ecef4d61b050f1304.zip |
Cleanup error messages
-rw-r--r-- | networking/libiproute/libnetlink.c | 126 |
1 files changed, 65 insertions, 61 deletions
diff --git a/networking/libiproute/libnetlink.c b/networking/libiproute/libnetlink.c index a1f39d409..99672382a 100644 --- a/networking/libiproute/libnetlink.c +++ b/networking/libiproute/libnetlink.c | |||
@@ -24,6 +24,7 @@ | |||
24 | #include <sys/uio.h> | 24 | #include <sys/uio.h> |
25 | 25 | ||
26 | #include "libnetlink.h" | 26 | #include "libnetlink.h" |
27 | #include "libbb.h" | ||
27 | 28 | ||
28 | void rtnl_close(struct rtnl_handle *rth) | 29 | void rtnl_close(struct rtnl_handle *rth) |
29 | { | 30 | { |
@@ -38,7 +39,7 @@ int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions) | |||
38 | 39 | ||
39 | rth->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); | 40 | rth->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
40 | if (rth->fd < 0) { | 41 | if (rth->fd < 0) { |
41 | perror("Cannot open netlink socket"); | 42 | perror_msg("Cannot open netlink socket"); |
42 | return -1; | 43 | return -1; |
43 | } | 44 | } |
44 | 45 | ||
@@ -47,20 +48,20 @@ int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions) | |||
47 | rth->local.nl_groups = subscriptions; | 48 | rth->local.nl_groups = subscriptions; |
48 | 49 | ||
49 | if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) { | 50 | if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) { |
50 | perror("Cannot bind netlink socket"); | 51 | perror_msg("Cannot bind netlink socket"); |
51 | return -1; | 52 | return -1; |
52 | } | 53 | } |
53 | addr_len = sizeof(rth->local); | 54 | addr_len = sizeof(rth->local); |
54 | if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) { | 55 | if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) { |
55 | perror("Cannot getsockname"); | 56 | perror_msg("Cannot getsockname"); |
56 | return -1; | 57 | return -1; |
57 | } | 58 | } |
58 | if (addr_len != sizeof(rth->local)) { | 59 | if (addr_len != sizeof(rth->local)) { |
59 | fprintf(stderr, "Wrong address length %d\n", addr_len); | 60 | error_msg("Wrong address length %d", addr_len); |
60 | return -1; | 61 | return -1; |
61 | } | 62 | } |
62 | if (rth->local.nl_family != AF_NETLINK) { | 63 | if (rth->local.nl_family != AF_NETLINK) { |
63 | fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family); | 64 | error_msg("Wrong address family %d", rth->local.nl_family); |
64 | return -1; | 65 | return -1; |
65 | } | 66 | } |
66 | rth->seq = time(NULL); | 67 | rth->seq = time(NULL); |
@@ -148,16 +149,15 @@ int rtnl_dump_filter(struct rtnl_handle *rth, | |||
148 | if (status < 0) { | 149 | if (status < 0) { |
149 | if (errno == EINTR) | 150 | if (errno == EINTR) |
150 | continue; | 151 | continue; |
151 | perror("OVERRUN"); | 152 | perror_msg("OVERRUN"); |
152 | continue; | 153 | continue; |
153 | } | 154 | } |
154 | if (status == 0) { | 155 | if (status == 0) { |
155 | fprintf(stderr, "EOF on netlink\n"); | 156 | error_msg("EOF on netlink"); |
156 | return -1; | 157 | return -1; |
157 | } | 158 | } |
158 | if (msg.msg_namelen != sizeof(nladdr)) { | 159 | if (msg.msg_namelen != sizeof(nladdr)) { |
159 | fprintf(stderr, "sender address length == %d\n", msg.msg_namelen); | 160 | error_msg_and_die("sender address length == %d", msg.msg_namelen); |
160 | exit(1); | ||
161 | } | 161 | } |
162 | 162 | ||
163 | h = (struct nlmsghdr*)buf; | 163 | h = (struct nlmsghdr*)buf; |
@@ -168,38 +168,40 @@ int rtnl_dump_filter(struct rtnl_handle *rth, | |||
168 | h->nlmsg_seq != rth->dump) { | 168 | h->nlmsg_seq != rth->dump) { |
169 | if (junk) { | 169 | if (junk) { |
170 | err = junk(&nladdr, h, arg2); | 170 | err = junk(&nladdr, h, arg2); |
171 | if (err < 0) | 171 | if (err < 0) { |
172 | return err; | 172 | return err; |
173 | } | ||
173 | } | 174 | } |
174 | goto skip_it; | 175 | goto skip_it; |
175 | } | 176 | } |
176 | 177 | ||
177 | if (h->nlmsg_type == NLMSG_DONE) | 178 | if (h->nlmsg_type == NLMSG_DONE) { |
178 | return 0; | 179 | return 0; |
180 | } | ||
179 | if (h->nlmsg_type == NLMSG_ERROR) { | 181 | if (h->nlmsg_type == NLMSG_ERROR) { |
180 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); | 182 | struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h); |
181 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { | 183 | if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { |
182 | fprintf(stderr, "ERROR truncated\n"); | 184 | error_msg("ERROR truncated"); |
183 | } else { | 185 | } else { |
184 | errno = -err->error; | 186 | errno = -l_err->error; |
185 | perror("RTNETLINK answers"); | 187 | perror_msg("RTNETLINK answers"); |
186 | } | 188 | } |
187 | return -1; | 189 | return -1; |
188 | } | 190 | } |
189 | err = filter(&nladdr, h, arg1); | 191 | err = filter(&nladdr, h, arg1); |
190 | if (err < 0) | 192 | if (err < 0) { |
191 | return err; | 193 | return err; |
194 | } | ||
192 | 195 | ||
193 | skip_it: | 196 | skip_it: |
194 | h = NLMSG_NEXT(h, status); | 197 | h = NLMSG_NEXT(h, status); |
195 | } | 198 | } |
196 | if (msg.msg_flags & MSG_TRUNC) { | 199 | if (msg.msg_flags & MSG_TRUNC) { |
197 | fprintf(stderr, "Message truncated\n"); | 200 | error_msg("Message truncated"); |
198 | continue; | 201 | continue; |
199 | } | 202 | } |
200 | if (status) { | 203 | if (status) { |
201 | fprintf(stderr, "!!!Remnant of size %d\n", status); | 204 | error_msg_and_die("!!!Remnant of size %d", status); |
202 | exit(1); | ||
203 | } | 205 | } |
204 | } | 206 | } |
205 | } | 207 | } |
@@ -228,13 +230,13 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer, | |||
228 | nladdr.nl_groups = groups; | 230 | nladdr.nl_groups = groups; |
229 | 231 | ||
230 | n->nlmsg_seq = seq = ++rtnl->seq; | 232 | n->nlmsg_seq = seq = ++rtnl->seq; |
231 | if (answer == NULL) | 233 | if (answer == NULL) { |
232 | n->nlmsg_flags |= NLM_F_ACK; | 234 | n->nlmsg_flags |= NLM_F_ACK; |
233 | 235 | } | |
234 | status = sendmsg(rtnl->fd, &msg, 0); | 236 | status = sendmsg(rtnl->fd, &msg, 0); |
235 | 237 | ||
236 | if (status < 0) { | 238 | if (status < 0) { |
237 | perror("Cannot talk to rtnetlink"); | 239 | perror_msg("Cannot talk to rtnetlink"); |
238 | return -1; | 240 | return -1; |
239 | } | 241 | } |
240 | 242 | ||
@@ -245,39 +247,39 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer, | |||
245 | status = recvmsg(rtnl->fd, &msg, 0); | 247 | status = recvmsg(rtnl->fd, &msg, 0); |
246 | 248 | ||
247 | if (status < 0) { | 249 | if (status < 0) { |
248 | if (errno == EINTR) | 250 | if (errno == EINTR) { |
249 | continue; | 251 | continue; |
250 | perror("OVERRUN"); | 252 | } |
253 | perror_msg("OVERRUN"); | ||
251 | continue; | 254 | continue; |
252 | } | 255 | } |
253 | if (status == 0) { | 256 | if (status == 0) { |
254 | fprintf(stderr, "EOF on netlink\n"); | 257 | error_msg("EOF on netlink"); |
255 | return -1; | 258 | return -1; |
256 | } | 259 | } |
257 | if (msg.msg_namelen != sizeof(nladdr)) { | 260 | if (msg.msg_namelen != sizeof(nladdr)) { |
258 | fprintf(stderr, "sender address length == %d\n", msg.msg_namelen); | 261 | error_msg_and_die("sender address length == %d", msg.msg_namelen); |
259 | exit(1); | ||
260 | } | 262 | } |
261 | for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) { | 263 | for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) { |
262 | int err; | 264 | int l_err; |
263 | int len = h->nlmsg_len; | 265 | int len = h->nlmsg_len; |
264 | int l = len - sizeof(*h); | 266 | int l = len - sizeof(*h); |
265 | 267 | ||
266 | if (l<0 || len>status) { | 268 | if (l<0 || len>status) { |
267 | if (msg.msg_flags & MSG_TRUNC) { | 269 | if (msg.msg_flags & MSG_TRUNC) { |
268 | fprintf(stderr, "Truncated message\n"); | 270 | error_msg("Truncated message"); |
269 | return -1; | 271 | return -1; |
270 | } | 272 | } |
271 | fprintf(stderr, "!!!malformed message: len=%d\n", len); | 273 | error_msg_and_die("!!!malformed message: len=%d", len); |
272 | exit(1); | ||
273 | } | 274 | } |
274 | 275 | ||
275 | if (h->nlmsg_pid != rtnl->local.nl_pid || | 276 | if (h->nlmsg_pid != rtnl->local.nl_pid || |
276 | h->nlmsg_seq != seq) { | 277 | h->nlmsg_seq != seq) { |
277 | if (junk) { | 278 | if (junk) { |
278 | err = junk(&nladdr, h, jarg); | 279 | l_err = junk(&nladdr, h, jarg); |
279 | if (err < 0) | 280 | if (l_err < 0) { |
280 | return err; | 281 | return l_err; |
282 | } | ||
281 | } | 283 | } |
282 | continue; | 284 | continue; |
283 | } | 285 | } |
@@ -285,15 +287,16 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer, | |||
285 | if (h->nlmsg_type == NLMSG_ERROR) { | 287 | if (h->nlmsg_type == NLMSG_ERROR) { |
286 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); | 288 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); |
287 | if (l < sizeof(struct nlmsgerr)) { | 289 | if (l < sizeof(struct nlmsgerr)) { |
288 | fprintf(stderr, "ERROR truncated\n"); | 290 | error_msg("ERROR truncated"); |
289 | } else { | 291 | } else { |
290 | errno = -err->error; | 292 | errno = -err->error; |
291 | if (errno == 0) { | 293 | if (errno == 0) { |
292 | if (answer) | 294 | if (answer) { |
293 | memcpy(answer, h, h->nlmsg_len); | 295 | memcpy(answer, h, h->nlmsg_len); |
296 | } | ||
294 | return 0; | 297 | return 0; |
295 | } | 298 | } |
296 | perror("RTNETLINK answers"); | 299 | perror_msg("RTNETLINK answers"); |
297 | } | 300 | } |
298 | return -1; | 301 | return -1; |
299 | } | 302 | } |
@@ -302,18 +305,17 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer, | |||
302 | return 0; | 305 | return 0; |
303 | } | 306 | } |
304 | 307 | ||
305 | fprintf(stderr, "Unexpected reply!!!\n"); | 308 | error_msg("Unexpected reply!!!"); |
306 | 309 | ||
307 | status -= NLMSG_ALIGN(len); | 310 | status -= NLMSG_ALIGN(len); |
308 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); | 311 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); |
309 | } | 312 | } |
310 | if (msg.msg_flags & MSG_TRUNC) { | 313 | if (msg.msg_flags & MSG_TRUNC) { |
311 | fprintf(stderr, "Message truncated\n"); | 314 | error_msg("Message truncated"); |
312 | continue; | 315 | continue; |
313 | } | 316 | } |
314 | if (status) { | 317 | if (status) { |
315 | fprintf(stderr, "!!!Remnant of size %d\n", status); | 318 | error_msg_and_die("!!!Remnant of size %d", status); |
316 | exit(1); | ||
317 | } | 319 | } |
318 | } | 320 | } |
319 | } | 321 | } |
@@ -349,16 +351,15 @@ int rtnl_listen(struct rtnl_handle *rtnl, | |||
349 | if (status < 0) { | 351 | if (status < 0) { |
350 | if (errno == EINTR) | 352 | if (errno == EINTR) |
351 | continue; | 353 | continue; |
352 | perror("OVERRUN"); | 354 | perror_msg("OVERRUN"); |
353 | continue; | 355 | continue; |
354 | } | 356 | } |
355 | if (status == 0) { | 357 | if (status == 0) { |
356 | fprintf(stderr, "EOF on netlink\n"); | 358 | error_msg("EOF on netlink"); |
357 | return -1; | 359 | return -1; |
358 | } | 360 | } |
359 | if (msg.msg_namelen != sizeof(nladdr)) { | 361 | if (msg.msg_namelen != sizeof(nladdr)) { |
360 | fprintf(stderr, "Sender address length == %d\n", msg.msg_namelen); | 362 | error_msg_and_die("Sender address length == %d", msg.msg_namelen); |
361 | exit(1); | ||
362 | } | 363 | } |
363 | for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) { | 364 | for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) { |
364 | int err; | 365 | int err; |
@@ -367,27 +368,26 @@ int rtnl_listen(struct rtnl_handle *rtnl, | |||
367 | 368 | ||
368 | if (l<0 || len>status) { | 369 | if (l<0 || len>status) { |
369 | if (msg.msg_flags & MSG_TRUNC) { | 370 | if (msg.msg_flags & MSG_TRUNC) { |
370 | fprintf(stderr, "Truncated message\n"); | 371 | error_msg("Truncated message"); |
371 | return -1; | 372 | return -1; |
372 | } | 373 | } |
373 | fprintf(stderr, "!!!malformed message: len=%d\n", len); | 374 | error_msg_and_die("!!!malformed message: len=%d", len); |
374 | exit(1); | ||
375 | } | 375 | } |
376 | 376 | ||
377 | err = handler(&nladdr, h, jarg); | 377 | err = handler(&nladdr, h, jarg); |
378 | if (err < 0) | 378 | if (err < 0) { |
379 | return err; | 379 | return err; |
380 | } | ||
380 | 381 | ||
381 | status -= NLMSG_ALIGN(len); | 382 | status -= NLMSG_ALIGN(len); |
382 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); | 383 | h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); |
383 | } | 384 | } |
384 | if (msg.msg_flags & MSG_TRUNC) { | 385 | if (msg.msg_flags & MSG_TRUNC) { |
385 | fprintf(stderr, "Message truncated\n"); | 386 | error_msg("Message truncated"); |
386 | continue; | 387 | continue; |
387 | } | 388 | } |
388 | if (status) { | 389 | if (status) { |
389 | fprintf(stderr, "!!!Remnant of size %d\n", status); | 390 | error_msg_and_die("!!!Remnant of size %d", status); |
390 | exit(1); | ||
391 | } | 391 | } |
392 | } | 392 | } |
393 | } | 393 | } |
@@ -415,7 +415,7 @@ int rtnl_from_file(FILE *rtnl, | |||
415 | if (status < 0) { | 415 | if (status < 0) { |
416 | if (errno == EINTR) | 416 | if (errno == EINTR) |
417 | continue; | 417 | continue; |
418 | perror("rtnl_from_file: fread"); | 418 | perror_msg("rtnl_from_file: fread"); |
419 | return -1; | 419 | return -1; |
420 | } | 420 | } |
421 | if (status == 0) | 421 | if (status == 0) |
@@ -426,7 +426,7 @@ int rtnl_from_file(FILE *rtnl, | |||
426 | l = len - sizeof(*h); | 426 | l = len - sizeof(*h); |
427 | 427 | ||
428 | if (l<0 || len>sizeof(buf)) { | 428 | if (l<0 || len>sizeof(buf)) { |
429 | fprintf(stderr, "!!!malformed message: len=%d @%lu\n", | 429 | error_msg("!!!malformed message: len=%d @%lu", |
430 | len, ftell(rtnl)); | 430 | len, ftell(rtnl)); |
431 | return -1; | 431 | return -1; |
432 | } | 432 | } |
@@ -434,11 +434,11 @@ int rtnl_from_file(FILE *rtnl, | |||
434 | status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl); | 434 | status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl); |
435 | 435 | ||
436 | if (status < 0) { | 436 | if (status < 0) { |
437 | perror("rtnl_from_file: fread"); | 437 | perror_msg("rtnl_from_file: fread"); |
438 | return -1; | 438 | return -1; |
439 | } | 439 | } |
440 | if (status < l) { | 440 | if (status < l) { |
441 | fprintf(stderr, "rtnl-from_file: truncated message\n"); | 441 | error_msg("rtnl-from_file: truncated message"); |
442 | return -1; | 442 | return -1; |
443 | } | 443 | } |
444 | 444 | ||
@@ -482,8 +482,9 @@ int rta_addattr32(struct rtattr *rta, int maxlen, int type, __u32 data) | |||
482 | int len = RTA_LENGTH(4); | 482 | int len = RTA_LENGTH(4); |
483 | struct rtattr *subrta; | 483 | struct rtattr *subrta; |
484 | 484 | ||
485 | if (RTA_ALIGN(rta->rta_len) + len > maxlen) | 485 | if (RTA_ALIGN(rta->rta_len) + len > maxlen) { |
486 | return -1; | 486 | return -1; |
487 | } | ||
487 | subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); | 488 | subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); |
488 | subrta->rta_type = type; | 489 | subrta->rta_type = type; |
489 | subrta->rta_len = len; | 490 | subrta->rta_len = len; |
@@ -497,8 +498,9 @@ int rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen | |||
497 | struct rtattr *subrta; | 498 | struct rtattr *subrta; |
498 | int len = RTA_LENGTH(alen); | 499 | int len = RTA_LENGTH(alen); |
499 | 500 | ||
500 | if (RTA_ALIGN(rta->rta_len) + len > maxlen) | 501 | if (RTA_ALIGN(rta->rta_len) + len > maxlen) { |
501 | return -1; | 502 | return -1; |
503 | } | ||
502 | subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); | 504 | subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); |
503 | subrta->rta_type = type; | 505 | subrta->rta_type = type; |
504 | subrta->rta_len = len; | 506 | subrta->rta_len = len; |
@@ -511,11 +513,13 @@ int rta_addattr_l(struct rtattr *rta, int maxlen, int type, void *data, int alen | |||
511 | int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len) | 513 | int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len) |
512 | { | 514 | { |
513 | while (RTA_OK(rta, len)) { | 515 | while (RTA_OK(rta, len)) { |
514 | if (rta->rta_type <= max) | 516 | if (rta->rta_type <= max) { |
515 | tb[rta->rta_type] = rta; | 517 | tb[rta->rta_type] = rta; |
518 | } | ||
516 | rta = RTA_NEXT(rta,len); | 519 | rta = RTA_NEXT(rta,len); |
517 | } | 520 | } |
518 | if (len) | 521 | if (len) { |
519 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", len, rta->rta_len); | 522 | error_msg("!!!Deficit %d, rta_len=%d", len, rta->rta_len); |
523 | } | ||
520 | return 0; | 524 | return 0; |
521 | } | 525 | } |