Print this page
OS-1780 ipdadm accidentally a newline
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/ipdadm/ipdadm.c
+++ new/usr/src/cmd/ipdadm/ipdadm.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 /*
23 23 * Copyright (c) 2012 Joyent, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 #include <sys/types.h>
28 28 #include <sys/stat.h>
29 29 #include <values.h>
30 30 #include <fcntl.h>
31 31 #include <errno.h>
32 32 #include <string.h>
33 33 #include <strings.h>
34 34 #include <stdio.h>
35 35 #include <stdlib.h>
36 36 #include <unistd.h>
37 37 #include <stropts.h>
38 38 #include <zone.h>
39 39 #include <libgen.h>
40 40 #include <assert.h>
41 41
42 42 #include <libipd.h>
43 43
44 44 static char *g_pname;
45 45 static char g_zonename[ZONENAME_MAX];
46 46 static zoneid_t g_zid;
47 47
48 48 #define E_SUCCESS 0
49 49 #define E_ERROR 1
50 50 #define E_USAGE 2
51 51
52 52 typedef int (*idc_cmd_func_t)(int, char *[]);
53 53 typedef struct ipdadm_cmd {
54 54 const char *idc_name; /* subcommand name */
55 55 idc_cmd_func_t idc_func; /* subcommand function */
56 56 const char *idc_usage; /* subcommand help */
57 57 } ipdadm_cmd_t;
58 58
59 59 static int ipdadm_list(int, char *[]);
60 60 static int ipdadm_info(int, char *[]);
61 61 static int ipdadm_corrupt(int, char *[]);
62 62 static int ipdadm_delay(int, char *[]);
63 63 static int ipdadm_drop(int, char *[]);
64 64 static int ipdadm_remove(int, char *[]);
65 65
66 66 #define IPDADM_NCMDS 6
67 67 static ipdadm_cmd_t ipdadm_cmds[] = {
68 68 { "list", ipdadm_list, "list [-v]" },
69 69 { "info", ipdadm_info, "info" },
70 70 { "corrupt", ipdadm_corrupt, "corrupt <percentage>" },
71 71 { "delay", ipdadm_delay, "delay <microseconds>" },
72 72 { "drop", ipdadm_drop, "drop <percentage>" },
73 73 { "remove", ipdadm_remove, "remove [corrupt|delay|drop]" }
74 74 };
75 75
76 76 static int
77 77 usage(FILE *fp)
78 78 {
79 79 int ii;
80 80 ipdadm_cmd_t *cmd;
81 81
82 82 (void) fprintf(fp, "Usage: %s [-z zonename] subcommand "
83 83 "[subcommand opts]\n\n", g_pname);
84 84 (void) fprintf(fp, "Subcommands:\n");
85 85 for (ii = 0; ii < IPDADM_NCMDS; ii++) {
86 86 cmd = &ipdadm_cmds[ii];
87 87 (void) fprintf(fp, "\t%s\n", cmd->idc_usage);
88 88 }
89 89
90 90 return (E_USAGE);
91 91 }
92 92
93 93 static void
94 94 ipdadm_list_one(zoneid_t z, const ipd_config_t *icp, void *arg)
95 95 {
96 96 char zonename[ZONENAME_MAX];
97 97 int opt_v = (int)(intptr_t)arg;
98 98
99 99 if (getzonenamebyid(z, zonename, sizeof (zonename)) < 0)
100 100 (void) printf("%ld", z);
101 101 else
102 102 (void) printf("%s", zonename);
103 103
104 104 if (!opt_v) {
105 105 (void) printf("\n");
106 106 return;
107 107 }
108 108
109 109 (void) printf("\t%u\t%u\t%u\n", icp->ic_corrupt, icp->ic_drop,
110 110 icp->ic_delay);
111 111 }
112 112
113 113 static int
114 114 ipdadm_list(int argc, char *argv[])
115 115 {
116 116 int opt_v = 0;
117 117 int fd, rval;
118 118 ipd_stathdl_t hdl;
119 119
120 120 if (argc > 1)
121 121 return (usage(stderr));
122 122
123 123 if (argc == 1) {
124 124 if (strcmp(argv[0], "-v") == 0)
125 125 ++opt_v;
126 126 else
127 127 return (usage(stderr));
128 128 }
129 129
130 130 fd = ipd_open(NULL);
131 131 rval = ipd_status_read(fd, &hdl);
132 132 (void) ipd_close(fd);
133 133
134 134 if (rval != 0) {
135 135 (void) fprintf(stderr, "%s: failed to get list info: %s\n",
136 136 g_pname, ipd_errmsg);
137 137 return (E_ERROR);
138 138 }
139 139
140 140 ipd_status_foreach_zone(hdl, ipdadm_list_one, (void *)(intptr_t)opt_v);
141 141 ipd_status_free(hdl);
142 142
143 143 return (E_SUCCESS);
144 144 }
145 145
146 146 /*ARGSUSED*/
147 147 static int
148 148 ipdadm_info(int argc, char *argv[])
149 149 {
150 150 int rval, fd;
151 151 ipd_stathdl_t hdl;
152 152 ipd_config_t *icp;
153 153
154 154 if (argc != 0)
155 155 return (usage(stderr));
156 156
157 157 fd = ipd_open(NULL);
158 158 rval = ipd_status_read(fd, &hdl);
↓ open down ↓ |
158 lines elided |
↑ open up ↑ |
159 159 (void) ipd_close(fd);
160 160 if (rval != 0) {
161 161 (void) fprintf(stderr, "%s: failed to get info: %s\n",
162 162 g_pname, ipd_errmsg);
163 163 return (E_ERROR);
164 164 }
165 165
166 166 if (ipd_status_get_config(hdl, g_zid, &icp) != 0) {
167 167 if (ipd_errno == EIPD_ZC_NOENT) {
168 168 (void) printf("zone %s does not exist or has no "
169 - "ipd actions enabled", g_zonename);
169 + "ipd actions enabled\n", g_zonename);
170 170 return (E_SUCCESS);
171 171 }
172 172 (void) fprintf(stderr, "%s: failed to get info: %s\n",
173 173 g_pname, ipd_errmsg);
174 174 return (E_ERROR);
175 175 }
176 176
177 177 (void) printf("ipd information for zone %s:\n",
178 178 g_zonename);
179 179 (void) printf("\tcorrupt:\t%u%% chance of packet corruption\n",
180 180 icp->ic_corrupt);
181 181 (void) printf("\tdrop:\t\t%u%% chance of packet drop\n",
182 182 icp->ic_drop);
183 183 (void) printf("\tdelay:\t\t%u microsecond delay per packet\n",
184 184 icp->ic_delay);
185 185
186 186 ipd_status_free(hdl);
187 187
188 188 return (E_SUCCESS);
189 189 }
190 190
191 191 static long
192 192 ipdadm_parse_long(const char *str, const char *name, long min, long max)
193 193 {
194 194 long val;
195 195 char *end;
196 196
197 197 errno = 0;
198 198 val = strtol(str, &end, 10);
199 199 if (errno != 0) {
200 200 (void) fprintf(stderr, "%s: invalid value for %s: %s\n",
201 201 g_pname, name, str);
202 202 exit(E_ERROR);
203 203 }
204 204
205 205 /*
206 206 * We want to make sure that we got the whole string. If not that's an
207 207 * error. e.g. 23.42 should not be valid.
208 208 */
209 209 if (*end != '\0') {
210 210 (void) fprintf(stderr, "%s: %s value must be an integer\n",
211 211 g_pname, name);
212 212 exit(E_ERROR);
213 213 }
214 214
215 215 if (val < min || val > max) {
216 216 (void) fprintf(stderr, "%s: %s value must be between %ld and "
217 217 "%ld inclusive\n", g_pname, name, min, max);
218 218 exit(E_ERROR);
219 219 }
220 220
221 221 return (val);
222 222 }
223 223
224 224 static int
225 225 ipdadm_corrupt(int argc, char *argv[])
226 226 {
227 227 int rval, fd;
228 228 long val;
229 229 ipd_config_t ic;
230 230
231 231 if (argc != 1) {
232 232 (void) fprintf(stderr, "%s: corrupt <percentage>\n",
233 233 g_pname);
234 234 return (usage(stderr));
235 235 }
236 236
237 237 val = ipdadm_parse_long(argv[0], "corrupt", 0, 100);
238 238 bzero(&ic, sizeof (ic));
239 239 ic.ic_mask = IPDM_CORRUPT;
240 240 ic.ic_corrupt = val;
241 241
242 242 fd = ipd_open(NULL);
243 243 rval = ipd_ctl(fd, g_zid, &ic);
244 244 (void) ipd_close(fd);
245 245
246 246 if (rval != 0) {
247 247 (void) fprintf(stderr, "%s: failed to change corrupt "
248 248 "value: %s\n", g_pname, ipd_errmsg);
249 249 return (E_ERROR);
250 250 }
251 251
252 252 return (E_SUCCESS);
253 253 }
254 254
255 255 static int
256 256 ipdadm_delay(int argc, char *argv[])
257 257 {
258 258 long val;
259 259 int fd, rval;
260 260 ipd_config_t ic;
261 261
262 262 if (argc != 1) {
263 263 (void) fprintf(stderr, "%s: delay <microseconds>\n",
264 264 g_pname);
265 265 return (usage(stderr));
266 266 }
267 267
268 268 val = ipdadm_parse_long(argv[0], "delay", 0, MAXLONG);
269 269 bzero(&ic, sizeof (ic));
270 270 ic.ic_mask = IPDM_DELAY;
271 271 ic.ic_delay = val;
272 272
273 273 fd = ipd_open(NULL);
274 274 rval = ipd_ctl(fd, g_zid, &ic);
275 275 (void) ipd_close(fd);
276 276
277 277 if (rval != 0) {
278 278 (void) fprintf(stderr, "%s: failed to change delay value: %s\n",
279 279 g_pname, ipd_errmsg);
280 280 return (E_ERROR);
281 281 }
282 282
283 283 return (E_SUCCESS);
284 284 }
285 285
286 286 static int
287 287 ipdadm_drop(int argc, char *argv[])
288 288 {
289 289 long val;
290 290 int fd, rval;
291 291 ipd_config_t ic;
292 292
293 293 if (argc != 1) {
294 294 (void) fprintf(stderr, "%s: drop <percentage>\n",
295 295 g_pname);
296 296 return (usage(stderr));
297 297 }
298 298
299 299 val = ipdadm_parse_long(argv[0], "drop", 0, 100);
300 300 bzero(&ic, sizeof (ic));
301 301 ic.ic_mask = IPDM_DROP;
302 302 ic.ic_drop = val;
303 303
304 304 fd = ipd_open(NULL);
305 305 rval = ipd_ctl(fd, g_zid, &ic);
306 306 (void) ipd_close(fd);
307 307
308 308 if (rval != 0) {
309 309 (void) fprintf(stderr, "%s: failed to change drop value: %s\n",
310 310 g_pname, ipd_errmsg);
311 311 return (E_ERROR);
312 312 }
313 313
314 314 return (E_SUCCESS);
315 315 }
316 316
317 317 static int
318 318 ipdadm_remove_valid(const char *str)
319 319 {
320 320 if (strcmp(str, "corrupt") == 0) {
321 321 return (IPDM_CORRUPT);
322 322 } else if (strcmp(str, "drop") == 0) {
323 323 return (IPDM_DROP);
324 324 } else if (strcmp(str, "delay") == 0) {
325 325 return (IPDM_DELAY);
326 326 }
327 327
328 328 return (0);
329 329 }
330 330
331 331 static int
332 332 ipdadm_remove(int argc, char *argv[])
333 333 {
334 334 ipd_config_t ic;
335 335 char *cur, *res;
336 336 int rval, fd;
337 337
338 338 if (argc < 1) {
339 339 (void) fprintf(stderr, "%s: remove <arguments>\n",
340 340 g_pname);
341 341 return (usage(stderr));
342 342 }
343 343
344 344 if (argc > 1) {
345 345 (void) fprintf(stderr, "%s: remove's arguments must be "
346 346 "comma seperated\n", g_pname);
347 347 return (E_ERROR);
348 348 }
349 349
350 350 bzero(&ic, sizeof (ic));
351 351
352 352 cur = argv[0];
353 353 while ((res = strchr(cur, ',')) != NULL) {
354 354 *res = '\0';
355 355 if ((rval = ipdadm_remove_valid(cur)) == 0) {
356 356 (void) fprintf(stderr, "%s: unknown remove "
357 357 "argument: %s\n", g_pname, cur);
358 358 return (E_ERROR);
359 359 }
360 360 ic.ic_mask |= rval;
361 361 cur = res + 1;
362 362 }
363 363
364 364 if ((rval = ipdadm_remove_valid(cur)) == 0) {
365 365 (void) fprintf(stderr, "%s: unknown remove argument: %s\n",
366 366 g_pname, cur);
367 367 return (E_ERROR);
368 368 }
369 369 ic.ic_mask |= rval;
370 370
371 371 fd = ipd_open(NULL);
372 372 rval = ipd_ctl(fd, g_zid, &ic);
373 373 (void) ipd_close(fd);
374 374 if (rval == -1) {
375 375 (void) fprintf(stderr, "%s: failed to remove instances: %s\n",
376 376 g_pname, ipd_errmsg);
377 377 return (E_ERROR);
378 378 }
379 379
380 380 return (E_SUCCESS);
381 381 }
382 382
383 383
384 384 int
385 385 main(int argc, char *argv[])
386 386 {
387 387 int ii;
388 388 ipdadm_cmd_t *cmd;
389 389
390 390 g_pname = basename(argv[0]);
391 391
392 392 if (argc < 2)
393 393 return (usage(stderr));
394 394 argc--;
395 395 argv++;
396 396
397 397 g_zid = getzoneid();
398 398 if (strcmp("-z", argv[0]) == 0) {
399 399 argc--;
400 400 argv++;
401 401 if (argc < 1) {
402 402 (void) fprintf(stderr, "%s: -z requires an argument\n",
403 403 g_pname);
404 404 return (usage(stderr));
405 405 }
406 406
407 407 if (g_zid != GLOBAL_ZONEID) {
408 408 (void) fprintf(stderr, "%s: -z option only permitted "
409 409 "in global zone\n", g_pname);
410 410 return (usage(stderr));
411 411 }
412 412
413 413 g_zid = getzoneidbyname(argv[0]);
414 414 if (g_zid == -1) {
415 415 (void) fprintf(stderr, "%s: %s: invalid zone\n",
416 416 g_pname, argv[0]);
417 417 return (E_ERROR);
418 418 }
419 419 argc--;
420 420 argv++;
421 421 }
422 422
423 423 if (getzonenamebyid(g_zid, g_zonename, sizeof (g_zonename)) < 0) {
424 424 (void) fprintf(stderr, "%s: failed to get zonename: %s\n",
425 425 g_pname, strerror(errno));
426 426 return (E_ERROR);
427 427 }
428 428
429 429 if (argc < 1)
430 430 return (usage(stderr));
431 431
432 432 for (ii = 0; ii < IPDADM_NCMDS; ii++) {
433 433 cmd = &ipdadm_cmds[ii];
434 434 if (strcmp(argv[0], cmd->idc_name) == 0) {
435 435 argv++;
436 436 argc--;
437 437 assert(cmd->idc_func != NULL);
438 438 return (cmd->idc_func(argc, argv));
439 439 }
440 440 }
441 441
442 442 (void) fprintf(stderr, "%s: %s: unknown command\n", g_pname, argv[0]);
443 443 return (usage(stderr));
444 444 }
↓ open down ↓ |
265 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX