Print this page
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libdhcpagent/common/dhcpagent_util.c
+++ new/usr/src/lib/libdhcpagent/common/dhcpagent_util.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 #include <sys/types.h>
27 27 #include <sys/ctfs.h>
28 28 #include <sys/contract/process.h>
29 29 #include <sys/socket.h>
30 30 #include <sys/time.h>
31 31 #include <sys/wait.h>
32 32 #include <fcntl.h>
33 33 #include <libcontract.h>
34 34 #include <libcontract_priv.h>
35 35 #include <unistd.h>
36 36 #include <stdio.h>
37 37 #include <stdlib.h>
38 38 #include <string.h>
39 39 #include <zone.h>
40 40
41 41 #include "dhcpagent_ipc.h"
42 42 #include "dhcpagent_util.h"
43 43
44 44 /*
45 45 * Strings returned by dhcp_status_hdr_string() and
46 46 * dhcp_status_reply_to_string(). The first define is the header line, and
47 47 * the second defines line printed underneath.
48 48 * The spacing of fields must match.
49 49 */
50 50 #define DHCP_STATUS_HDR "Interface State Sent Recv Declined Flags\n"
51 51 #define DHCP_STATUS_STR "%-10s %-12s %5d %5d %9d "
52 52
53 53 static const char *time_to_string(time_t abs_time);
54 54
55 55 /*
56 56 * dhcp_state_to_string(): given a state, provides the state's name
57 57 *
58 58 * input: DHCPSTATE: the state to get the name of
59 59 * output: const char *: the state's name
60 60 */
61 61
62 62 const char *
63 63 dhcp_state_to_string(DHCPSTATE state)
64 64 {
65 65 const char *states[] = {
66 66 "INIT",
67 67 "SELECTING",
68 68 "REQUESTING",
69 69 "PRE_BOUND",
70 70 "BOUND",
71 71 "RENEWING",
72 72 "REBINDING",
73 73 "INFORMATION",
74 74 "INIT_REBOOT",
75 75 "ADOPTING",
76 76 "INFORM_SENT",
77 77 "DECLINING",
78 78 "RELEASING"
79 79 };
80 80
81 81 if (state < 0 || state >= DHCP_NSTATES)
82 82 return ("<unknown>");
83 83
84 84 return (states[state]);
85 85 }
86 86
87 87 static int
88 88 init_template(void)
89 89 {
90 90 int fd;
91 91 int err = 0;
92 92
93 93 fd = open64(CTFS_ROOT "/process/template", O_RDWR);
94 94 if (fd == -1)
95 95 return (-1);
96 96
97 97 /*
98 98 * Deliver no events, don't inherit, and allow it to be orphaned.
99 99 */
100 100 err |= ct_tmpl_set_critical(fd, 0);
101 101 err |= ct_tmpl_set_informative(fd, 0);
102 102 err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
103 103 err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
104 104 if (err != 0 || ct_tmpl_activate(fd) != 0) {
105 105 (void) close(fd);
106 106 return (-1);
107 107 }
108 108
109 109 return (fd);
110 110 }
111 111
112 112 /*
113 113 * dhcp_start_agent(): starts the agent if not already running
114 114 *
115 115 * input: int: number of seconds to wait for agent to start (-1 is forever)
116 116 * output: int: 0 on success, -1 on failure
117 117 */
118 118
119 119 int
120 120 dhcp_start_agent(int timeout)
121 121 {
122 122 int error;
123 123 time_t start_time = time(NULL);
124 124 dhcp_ipc_request_t *request;
125 125 dhcp_ipc_reply_t *reply;
126 126 int ctfd;
127 127 pid_t childpid;
128 128 ctid_t ct;
129 129 char dhcpcmd[MAXPATHLEN];
130 130 const char *zroot = zone_get_nroot();
131 131
132 132 /* Prepend the root of the native code in the brand to the command */
133 133 (void) snprintf(dhcpcmd, sizeof (dhcpcmd), "%s%s", zroot != NULL ?
134 134 zroot : "", DHCP_AGENT_PATH);
135 135
136 136 /*
137 137 * just send a dummy request to the agent to find out if it's
138 138 * up. we do this instead of directly connecting to it since
139 139 * we want to make sure we follow its IPC conventions
140 140 * (otherwise, it will log warnings to syslog).
141 141 */
142 142
143 143 request = dhcp_ipc_alloc_request(DHCP_PING, "", NULL, 0,
144 144 DHCP_TYPE_NONE);
145 145 if (request == NULL)
146 146 return (-1);
147 147
148 148 error = dhcp_ipc_make_request(request, &reply, 0);
149 149 if (error == 0) {
150 150 free(reply);
151 151 free(request);
152 152 return (0);
153 153 }
154 154 if (error != DHCP_IPC_E_CONNECT)
155 155 goto fail;
156 156
157 157 if ((ctfd = init_template()) == -1)
158 158 goto fail;
159 159
160 160 childpid = fork();
161 161
162 162 (void) ct_tmpl_clear(ctfd);
163 163 (void) close(ctfd);
164 164
165 165 switch (childpid) {
166 166 case -1:
167 167 goto fail;
168 168
169 169 case 0:
170 170 (void) execl(dhcpcmd, dhcpcmd, (char *)0);
171 171 _exit(EXIT_FAILURE);
172 172
173 173 default:
174 174 break;
175 175 }
176 176
177 177 /* wait for the daemon to run and then abandon the contract */
178 178 (void) waitpid(childpid, NULL, 0);
179 179
180 180 if (contract_latest(&ct) != -1)
181 181 (void) contract_abandon_id(ct);
182 182
183 183 while ((timeout != -1) && (time(NULL) - start_time < timeout)) {
184 184 error = dhcp_ipc_make_request(request, &reply, 0);
185 185 if (error == 0) {
186 186 free(reply);
187 187 free(request);
188 188 return (0);
189 189 } else if (error != DHCP_IPC_E_CONNECT)
190 190 break;
191 191 (void) sleep(1);
192 192 }
193 193
194 194 fail:
195 195 free(request);
196 196 return (-1);
197 197 }
198 198
199 199 /*
200 200 * dhcp_status_hdr_string(): Return a string suitable to use as the header
201 201 * when printing DHCP_STATUS reply.
202 202 * output: const char *: newline terminated printable string
203 203 */
204 204 const char *
205 205 dhcp_status_hdr_string(void)
206 206 {
207 207 return (DHCP_STATUS_HDR);
208 208 }
209 209
210 210 /*
211 211 * time_to_string(): Utility routine for printing time
212 212 *
213 213 * input: time_t *: time_t to stringify
214 214 * output: const char *: printable time
215 215 */
216 216 static const char *
217 217 time_to_string(time_t abs_time)
218 218 {
219 219 static char time_buf[24];
220 220 time_t tm = abs_time;
221 221
222 222 if (tm == DHCP_PERM)
223 223 return ("Never");
224 224
225 225 if (strftime(time_buf, sizeof (time_buf), "%m/%d/%Y %R",
226 226 localtime(&tm)) == 0)
227 227 return ("<unknown>");
228 228
229 229 return (time_buf);
230 230 }
231 231
232 232 /*
233 233 * dhcp_status_reply_to_string(): Return DHCP IPC reply of type DHCP_STATUS
234 234 * as a printable string
235 235 *
236 236 * input: dhcp_reply_t *: contains the status structure to print
237 237 * output: const char *: newline terminated printable string
238 238 */
239 239 const char *
240 240 dhcp_status_reply_to_string(dhcp_ipc_reply_t *reply)
241 241 {
242 242 static char str[1024];
243 243 size_t reply_size;
244 244 dhcp_status_t *status;
245 245
246 246 status = dhcp_ipc_get_data(reply, &reply_size, NULL);
247 247 if (reply_size < DHCP_STATUS_VER1_SIZE)
248 248 return ("<Internal error: status msg size>\n");
249 249
250 250 (void) snprintf(str, sizeof (str), DHCP_STATUS_STR,
251 251 status->if_name, dhcp_state_to_string(status->if_state),
252 252 status->if_sent, status->if_recv, status->if_bad_offers);
253 253
254 254 if (status->if_dflags & DHCP_IF_PRIMARY)
255 255 (void) strlcat(str, "[PRIMARY] ", sizeof (str));
256 256
257 257 if (status->if_dflags & DHCP_IF_BOOTP)
258 258 (void) strlcat(str, "[BOOTP] ", sizeof (str));
259 259
260 260 if (status->if_dflags & DHCP_IF_FAILED)
261 261 (void) strlcat(str, "[FAILED] ", sizeof (str));
262 262
263 263 if (status->if_dflags & DHCP_IF_BUSY)
264 264 (void) strlcat(str, "[BUSY] ", sizeof (str));
265 265
266 266 if (status->if_dflags & DHCP_IF_V6)
267 267 (void) strlcat(str, "[V6] ", sizeof (str));
268 268
269 269 (void) strlcat(str, "\n", sizeof (str));
270 270
271 271 switch (status->if_state) {
272 272 case BOUND:
273 273 case RENEWING:
274 274 case REBINDING:
275 275 break;
276 276 default:
277 277 return (str);
278 278 }
279 279
280 280 (void) strlcat(str, "(Began, Expires, Renew) = (", sizeof (str));
281 281 (void) strlcat(str, time_to_string(status->if_began), sizeof (str));
282 282 (void) strlcat(str, ", ", sizeof (str));
283 283 (void) strlcat(str, time_to_string(status->if_lease), sizeof (str));
284 284 (void) strlcat(str, ", ", sizeof (str));
285 285 (void) strlcat(str, time_to_string(status->if_t1), sizeof (str));
286 286 (void) strlcat(str, ")\n", sizeof (str));
287 287 return (str);
288 288 }
|
↓ open down ↓ |
288 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX