Print this page
patch cleanup
6659 nvlist_free(NULL) is a no-op
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libcontract/common/libcontract.c
+++ new/usr/src/lib/libcontract/common/libcontract.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
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
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 2008 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 -#pragma ident "%Z%%M% %I% %E% SMI"
27 -
28 26 #include <sys/ctfs.h>
29 27 #include <sys/contract.h>
30 28 #include <string.h>
31 29 #include <libnvpair.h>
32 30 #include <assert.h>
33 31 #include <unistd.h>
34 32 #include <errno.h>
35 33 #include <libcontract.h>
36 34 #include "libcontract_impl.h"
37 35
38 36 /*
39 37 * Common template routines
40 38 */
41 39
42 40 int
43 41 ct_tmpl_activate(int fd)
44 42 {
45 43 if (ioctl(fd, CT_TACTIVATE) == -1)
46 44 return (errno);
47 45 return (0);
48 46 }
49 47
50 48 int
51 49 ct_tmpl_clear(int fd)
52 50 {
53 51 if (ioctl(fd, CT_TCLEAR) == -1)
54 52 return (errno);
55 53 return (0);
56 54 }
57 55
58 56 int
59 57 ct_tmpl_create(int fd, ctid_t *ctidp)
60 58 {
61 59 ctid_t ctid = ioctl(fd, CT_TCREATE);
62 60 if (ctid == -1)
63 61 return (errno);
64 62 *ctidp = ctid;
65 63 return (0);
66 64 }
67 65
68 66 int
69 67 ct_tmpl_set_internal(int fd, uint_t id, uintptr_t value)
70 68 {
71 69 ct_param_t param;
72 70 uint64_t param_value = value;
73 71
74 72 param.ctpm_id = id;
75 73 param.ctpm_size = sizeof (uint64_t);
76 74 param.ctpm_value = ¶m_value;
77 75 if (ioctl(fd, CT_TSET, ¶m) == -1)
78 76 return (errno);
79 77
80 78 return (0);
81 79 }
82 80
83 81 int
84 82 ct_tmpl_set_internal_string(int fd, uint_t id, const char *value)
85 83 {
86 84 ct_param_t param;
87 85
88 86 if (value == NULL)
89 87 return (EINVAL);
90 88 param.ctpm_id = id;
91 89 param.ctpm_size = strlen(value) + 1;
92 90 param.ctpm_value = (void *)value;
93 91 if (ioctl(fd, CT_TSET, ¶m) == -1)
94 92 return (errno);
95 93
96 94 return (0);
97 95 }
98 96
99 97 int
100 98 ct_tmpl_set_critical(int fd, uint_t events)
101 99 {
102 100 return (ct_tmpl_set_internal(fd, CTP_EV_CRITICAL, events));
103 101 }
104 102
105 103 int
106 104 ct_tmpl_set_informative(int fd, uint_t events)
107 105 {
108 106 return (ct_tmpl_set_internal(fd, CTP_EV_INFO, events));
109 107 }
110 108
111 109 int
112 110 ct_tmpl_set_cookie(int fd, uint64_t cookie)
113 111 {
114 112 ct_param_t param;
115 113 uint64_t param_value = cookie;
116 114
117 115 param.ctpm_id = CTP_COOKIE;
118 116 param.ctpm_size = sizeof (uint64_t);
119 117 param.ctpm_value = ¶m_value;
120 118 if (ioctl(fd, CT_TSET, ¶m) == -1)
121 119 return (errno);
122 120 return (0);
123 121 }
124 122
125 123 int
126 124 ct_tmpl_get_internal(int fd, uint_t id, uint_t *value)
127 125 {
128 126 ct_param_t param;
129 127 uint64_t param_value;
130 128
131 129 param.ctpm_id = id;
132 130 param.ctpm_size = sizeof (uint64_t);
133 131 param.ctpm_value = ¶m_value;
134 132 if (ioctl(fd, CT_TGET, ¶m) == -1)
135 133 return (errno);
136 134 *value = param_value;
137 135 return (0);
138 136 }
139 137
140 138 int
141 139 ct_tmpl_get_internal_string(int fd, uint32_t id, char *buf, size_t size)
142 140 {
143 141 ct_param_t param;
144 142
145 143 param.ctpm_id = id;
146 144 param.ctpm_size = size;
147 145 param.ctpm_value = buf;
148 146 if (ioctl(fd, CT_TGET, ¶m) == -1)
149 147 return (-1);
150 148 return (param.ctpm_size);
151 149 }
152 150
153 151 int
154 152 ct_tmpl_get_critical(int fd, uint_t *events)
155 153 {
156 154 return (ct_tmpl_get_internal(fd, CTP_EV_CRITICAL, events));
157 155 }
158 156
159 157 int
160 158 ct_tmpl_get_informative(int fd, uint_t *events)
161 159 {
162 160 return (ct_tmpl_get_internal(fd, CTP_EV_INFO, events));
163 161 }
164 162
165 163 int
166 164 ct_tmpl_get_cookie(int fd, uint64_t *cookie)
167 165 {
168 166 ct_param_t param;
169 167
170 168 param.ctpm_id = CTP_COOKIE;
171 169 param.ctpm_size = sizeof (uint64_t);
172 170 param.ctpm_value = cookie;
173 171 if (ioctl(fd, CT_TGET, ¶m) == -1)
174 172 return (errno);
175 173 return (0);
176 174 }
177 175
178 176 /*
179 177 * Common ctl routines
180 178 */
181 179
182 180 int
183 181 ct_ctl_adopt(int fd)
184 182 {
185 183 if (ioctl(fd, CT_CADOPT) == -1)
186 184 return (errno);
187 185 return (0);
188 186 }
189 187
190 188 int
191 189 ct_ctl_abandon(int fd)
192 190 {
193 191 if (ioctl(fd, CT_CABANDON) == -1)
194 192 return (errno);
195 193 return (0);
196 194 }
197 195
198 196 /*ARGSUSED*/
199 197 int
200 198 ct_ctl_newct(int cfd, ctevid_t evid, int tfd)
201 199 {
202 200 if (ioctl(cfd, CT_CNEWCT, tfd) == -1)
203 201 return (errno);
204 202 return (0);
205 203 }
206 204
207 205 int
208 206 ct_ctl_ack(int fd, ctevid_t event)
209 207 {
210 208 if (ioctl(fd, CT_CACK, &event) == -1)
211 209 return (errno);
212 210 return (0);
213 211 }
214 212
215 213 int
216 214 ct_ctl_nack(int fd, ctevid_t event)
217 215 {
218 216 if (ioctl(fd, CT_CNACK, &event) == -1)
219 217 return (errno);
220 218 return (0);
221 219 }
222 220
223 221 int
224 222 ct_ctl_qack(int fd, ctevid_t event)
225 223 {
226 224 if (ioctl(fd, CT_CQREQ, &event) == -1)
227 225 return (errno);
228 226 return (0);
229 227 }
230 228
231 229 /*
232 230 * Common status routines
233 231 */
234 232
235 233 int
236 234 ct_status_read(int fd, int detail, ct_stathdl_t *stathdl)
237 235 {
238 236 char *status_buffer = NULL;
239 237 int status_nbytes = 0;
240 238 struct ctlib_status_info *info;
241 239 int error;
242 240
243 241 info = malloc(sizeof (struct ctlib_status_info));
244 242 if (info == NULL)
245 243 return (errno);
246 244
247 245 info->status.ctst_detail = detail;
248 246 if (detail != CTD_COMMON) {
249 247 for (;;) {
250 248 info->status.ctst_nbytes = status_nbytes;
251 249 info->status.ctst_buffer = status_buffer;
252 250 do
253 251 error = ioctl(fd, CT_SSTATUS, &info->status);
254 252 while (error == -1 && errno == EINTR);
255 253 if (error == -1)
256 254 goto errout;
257 255 if (info->status.ctst_nbytes <= status_nbytes)
258 256 break;
259 257
260 258 if (status_buffer)
261 259 free(status_buffer);
262 260 status_nbytes = info->status.ctst_nbytes;
263 261 status_buffer = malloc(status_nbytes);
264 262 if (status_buffer == NULL)
265 263 goto errout;
266 264 }
267 265 if ((errno = nvlist_unpack(info->status.ctst_buffer,
268 266 info->status.ctst_nbytes, &info->nvl, 0)) != 0)
269 267 goto errout;
270 268
271 269 free(status_buffer);
272 270 status_buffer = NULL;
273 271
274 272 } else {
275 273 info->status.ctst_nbytes = 0;
276 274 info->nvl = NULL;
277 275 if (ioctl(fd, CT_SSTATUS, &info->status) == -1)
278 276 goto errout;
279 277 }
280 278
281 279 *stathdl = info;
282 280 return (0);
283 281
284 282 errout:
285 283 error = errno;
286 284 if (status_buffer)
287 285 free(status_buffer);
288 286 if (info)
289 287 free(info);
290 288 return (error);
291 289 }
292 290
293 291 void
294 292 ct_status_free(ct_stathdl_t stathdl)
295 293 {
296 294 struct ctlib_status_info *info = stathdl;
297 295
298 296 if (info->nvl) {
299 297 assert(info->status.ctst_detail != CTD_COMMON);
300 298 nvlist_free(info->nvl);
301 299 }
302 300
303 301 free(info);
304 302 }
305 303
306 304 ctid_t
307 305 ct_status_get_id(ct_stathdl_t stathdl)
308 306 {
309 307 struct ctlib_status_info *info = stathdl;
310 308 return (info->status.ctst_id);
311 309 }
312 310
313 311 zoneid_t
314 312 ct_status_get_zoneid(ct_stathdl_t stathdl)
315 313 {
316 314 struct ctlib_status_info *info = stathdl;
317 315 return (info->status.ctst_zoneid);
318 316 }
319 317
320 318 const char *
321 319 ct_status_get_type(ct_stathdl_t stathdl)
322 320 {
323 321 struct ctlib_status_info *info = stathdl;
324 322 return (types[info->status.ctst_type].type_name);
325 323 }
326 324
327 325 id_t
328 326 ct_status_get_holder(ct_stathdl_t stathdl)
329 327 {
330 328 struct ctlib_status_info *info = stathdl;
331 329 return (info->status.ctst_holder);
332 330 }
333 331
334 332 ctstate_t
335 333 ct_status_get_state(ct_stathdl_t stathdl)
336 334 {
337 335 struct ctlib_status_info *info = stathdl;
338 336 return (info->status.ctst_state);
339 337 }
340 338
341 339 int
342 340 ct_status_get_nevents(ct_stathdl_t stathdl)
343 341 {
344 342 struct ctlib_status_info *info = stathdl;
345 343 return (info->status.ctst_nevents);
346 344 }
347 345
348 346 int
349 347 ct_status_get_ntime(ct_stathdl_t stathdl)
350 348 {
351 349 struct ctlib_status_info *info = stathdl;
352 350 return (info->status.ctst_ntime);
353 351 }
354 352
355 353 int
356 354 ct_status_get_qtime(ct_stathdl_t stathdl)
357 355 {
358 356 struct ctlib_status_info *info = stathdl;
359 357 return (info->status.ctst_qtime);
360 358 }
361 359
362 360 ctevid_t
363 361 ct_status_get_nevid(ct_stathdl_t stathdl)
364 362 {
365 363 struct ctlib_status_info *info = stathdl;
366 364 return (info->status.ctst_nevid);
367 365 }
368 366
369 367 uint_t
370 368 ct_status_get_informative(ct_stathdl_t stathdl)
371 369 {
372 370 struct ctlib_status_info *info = stathdl;
373 371 return (info->status.ctst_informative);
374 372 }
375 373
376 374 uint_t
377 375 ct_status_get_critical(ct_stathdl_t stathdl)
378 376 {
379 377 struct ctlib_status_info *info = stathdl;
380 378 return (info->status.ctst_critical);
381 379 }
382 380
383 381 uint64_t
384 382 ct_status_get_cookie(ct_stathdl_t stathdl)
385 383 {
386 384 struct ctlib_status_info *info = stathdl;
387 385 return (info->status.ctst_cookie);
388 386 }
389 387
390 388 /*
391 389 * Common event routines
392 390 */
393 391
394 392 static int
395 393 unpack_and_merge(nvlist_t **nvl, char *buffer, size_t len)
396 394 {
397 395 nvlist_t *tmpnvl;
398 396 int error;
399 397
400 398 if ((error = nvlist_unpack(buffer, len, &tmpnvl, 0)) != 0)
401 399 return (error);
402 400
403 401 if (*nvl == NULL) {
404 402 *nvl = tmpnvl;
405 403 return (0);
406 404 }
407 405
408 406 error = nvlist_merge(*nvl, tmpnvl, 0);
409 407 nvlist_free(tmpnvl);
410 408 return (error);
411 409 }
412 410
413 411 static int
414 412 ct_event_read_internal(int fd, int cmd, ct_evthdl_t *evt)
415 413 {
416 414 char *event_buffer = NULL;
417 415 int event_nbytes = 0;
418 416 struct ctlib_event_info *info;
419 417 ct_event_t *event;
420 418 int error;
421 419
422 420 info = malloc(sizeof (struct ctlib_event_info));
423 421 if (info == NULL)
424 422 return (errno);
425 423 info->nvl = NULL;
426 424 event = &info->event;
427 425
428 426 for (;;) {
429 427 event->ctev_nbytes = event_nbytes;
430 428 event->ctev_buffer = event_buffer;
431 429 do
432 430 error = ioctl(fd, cmd, event);
433 431 while (error == -1 && errno == EINTR);
434 432 if (error == -1) {
435 433 error = errno;
436 434 goto errout;
437 435 }
438 436 if (event->ctev_nbytes <= event_nbytes)
439 437 break;
440 438
441 439 if (event_buffer)
442 440 free(event_buffer);
443 441 event_nbytes = event->ctev_nbytes;
444 442 event_buffer = malloc(event_nbytes);
445 443 if (event_buffer == NULL) {
446 444 error = errno;
447 445 goto errout;
448 446 }
449 447 }
450 448
451 449 if (event->ctev_goffset > 0 && (error = unpack_and_merge(&info->nvl,
452 450 event->ctev_buffer, event->ctev_goffset)) != 0)
453 451 goto errout;
454 452
455 453 if (event->ctev_goffset < event->ctev_nbytes &&
456 454 (error = unpack_and_merge(&info->nvl,
457 455 event->ctev_buffer + event->ctev_goffset,
458 456 event->ctev_nbytes - event->ctev_goffset)) != 0)
459 457 goto errout;
↓ open down ↓ |
422 lines elided |
↑ open up ↑ |
460 458
461 459 free(event_buffer);
462 460
463 461 *evt = info;
464 462 return (0);
465 463
466 464 errout:
467 465 if (event_buffer)
468 466 free(event_buffer);
469 467 if (info) {
470 - if (info->nvl)
471 - nvlist_free(info->nvl);
468 + nvlist_free(info->nvl);
472 469 free(info);
473 470 }
474 471 return (error);
475 472 }
476 473
477 474 int
478 475 ct_event_read(int fd, ct_evthdl_t *evthdl)
479 476 {
480 477 return (ct_event_read_internal(fd, CT_ERECV, evthdl));
481 478 }
482 479
483 480 int
484 481 ct_event_read_critical(int fd, ct_evthdl_t *evthdl)
485 482 {
486 483 return (ct_event_read_internal(fd, CT_ECRECV, evthdl));
487 484 }
488 485
489 486 int
490 487 ct_event_reset(int fd)
491 488 {
492 489 if (ioctl(fd, CT_ERESET) == -1)
493 490 return (errno);
494 491 return (0);
495 492 }
496 493
497 494 int
498 495 ct_event_reliable(int fd)
499 496 {
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
500 497 if (ioctl(fd, CT_ERELIABLE) == -1)
501 498 return (errno);
502 499 return (0);
503 500 }
504 501
505 502 void
506 503 ct_event_free(ct_evthdl_t evthdl)
507 504 {
508 505 struct ctlib_event_info *info = evthdl;
509 506
510 - if (info->nvl)
511 - nvlist_free(info->nvl);
507 + nvlist_free(info->nvl);
512 508 free(info);
513 509 }
514 510
515 511
516 512 uint_t
517 513 ct_event_get_flags(ct_evthdl_t evthdl)
518 514 {
519 515 struct ctlib_event_info *info = evthdl;
520 516 return (info->event.ctev_flags);
521 517 }
522 518
523 519 ctid_t
524 520 ct_event_get_ctid(ct_evthdl_t evthdl)
525 521 {
526 522 struct ctlib_event_info *info = evthdl;
527 523 return (info->event.ctev_id);
528 524 }
529 525
530 526 ctevid_t
531 527 ct_event_get_evid(ct_evthdl_t evthdl)
532 528 {
533 529 struct ctlib_event_info *info = evthdl;
534 530 return (info->event.ctev_evid);
535 531 }
536 532
537 533 uint_t
538 534 ct_event_get_type(ct_evthdl_t evthdl)
539 535 {
540 536 struct ctlib_event_info *info = evthdl;
541 537 return (info->event.ctev_type);
542 538 }
543 539
544 540 int
545 541 ct_event_get_nevid(ct_evthdl_t evthdl, ctevid_t *evidp)
546 542 {
547 543 struct ctlib_event_info *info = evthdl;
548 544 if (info->nvl == NULL ||
549 545 nvlist_lookup_uint64(info->nvl, CTS_NEVID, evidp))
550 546 return (EINVAL);
551 547 return (0);
552 548 }
553 549
554 550 int
555 551 ct_event_get_newct(ct_evthdl_t evthdl, ctid_t *ctidp)
556 552 {
557 553 struct ctlib_event_info *info = evthdl;
558 554 if (info->nvl == NULL ||
559 555 nvlist_lookup_int32(info->nvl, CTS_NEWCT, (int *)ctidp))
560 556 return (EINVAL);
561 557 return (0);
562 558 }
↓ open down ↓ |
41 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX