/src/lldpd/src/daemon/privsep_io.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c; c-file-style: "openbsd" -*- */ |
2 | | |
3 | | #include "lldpd.h" |
4 | | |
5 | | #include <sys/param.h> |
6 | | #include <sys/uio.h> |
7 | | #include <sys/types.h> |
8 | | #include <sys/socket.h> |
9 | | #include <sys/stat.h> |
10 | | #include <errno.h> |
11 | | #include <fcntl.h> |
12 | | #include <signal.h> |
13 | | #include <stdio.h> |
14 | | #include <stdlib.h> |
15 | | #include <string.h> |
16 | | #include <unistd.h> |
17 | | |
18 | | /* Stolen from sbin/pflogd/privsep.c from OpenBSD */ |
19 | | /* |
20 | | * Copyright (c) 2003 Can Erkin Acar |
21 | | * Copyright (c) 2003 Anil Madhavapeddy <anil@recoil.org> |
22 | | * |
23 | | * Permission to use, copy, modify, and/or distribute this software for any |
24 | | * purpose with or without fee is hereby granted, provided that the above |
25 | | * copyright notice and this permission notice appear in all copies. |
26 | | * |
27 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
28 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
29 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
30 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
31 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
32 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
33 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
34 | | */ |
35 | | |
36 | | /* Read all data or return 1 for error. */ |
37 | | int |
38 | | may_read(enum priv_context ctx, void *buf, size_t n) |
39 | 0 | { |
40 | 0 | char *s = buf; |
41 | 0 | ssize_t res, pos = 0; |
42 | |
|
43 | 0 | while (n > pos) { |
44 | 0 | res = read(priv_fd(ctx), s + pos, n - pos); |
45 | 0 | switch (res) { |
46 | 0 | case -1: |
47 | 0 | if (errno == EINTR || errno == EAGAIN) continue; |
48 | 0 | return 1; |
49 | 0 | case 0: |
50 | 0 | return 1; |
51 | 0 | default: |
52 | 0 | pos += res; |
53 | 0 | } |
54 | 0 | } |
55 | 0 | return (0); |
56 | 0 | } |
57 | | |
58 | | /* Read data with the assertion that it all must come through, or |
59 | | * else abort the process. Based on atomicio() from openssh. */ |
60 | | void |
61 | | must_read(enum priv_context ctx, void *buf, size_t n) |
62 | 0 | { |
63 | 0 | char *s = buf; |
64 | 0 | ssize_t res, pos = 0; |
65 | |
|
66 | 0 | while (n > pos) { |
67 | 0 | res = read(priv_fd(ctx), s + pos, n - pos); |
68 | 0 | switch (res) { |
69 | 0 | case -1: |
70 | 0 | if (errno == EINTR || errno == EAGAIN) continue; |
71 | 0 | _exit(0); |
72 | 0 | case 0: |
73 | 0 | _exit(0); |
74 | 0 | default: |
75 | 0 | pos += res; |
76 | 0 | } |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | /* Write data with the assertion that it all has to be written, or |
81 | | * else abort the process. Based on atomicio() from openssh. */ |
82 | | void |
83 | | must_write(enum priv_context ctx, const void *buf, size_t n) |
84 | 0 | { |
85 | 0 | const char *s = buf; |
86 | 0 | ssize_t res, pos = 0; |
87 | |
|
88 | 0 | while (n > pos) { |
89 | 0 | res = write(priv_fd(ctx), s + pos, n - pos); |
90 | 0 | switch (res) { |
91 | 0 | case -1: |
92 | 0 | if (errno == EINTR || errno == EAGAIN) continue; |
93 | 0 | _exit(0); |
94 | 0 | case 0: |
95 | 0 | _exit(0); |
96 | 0 | default: |
97 | 0 | pos += res; |
98 | 0 | } |
99 | 0 | } |
100 | 0 | } |