1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2017 RackTop Systems.
26 */
27
28 #include "libscf_impl.h"
29
30 #include <assert.h>
31 #include <libuutil.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <sys/param.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <assert.h>
39 #include "midlevel_impl.h"
40 #include "lowlevel_impl.h"
41
42 #ifndef NDEBUG
43 #define bad_error(func, err) { \
44 uu_warn("%s:%d: %s failed with unexpected error %d. Aborting.\n", \
45 __FILE__, __LINE__, func, err); \
46 abort(); \
47 }
48 #else
49 #define bad_error(func, err) abort()
50 #endif
51
52 /* Path to speedy files area must end with a slash */
53 #define SMF_SPEEDY_FILES_PATH "/etc/svc/volatile/"
54
55 void
56 scf_simple_handle_destroy(scf_simple_handle_t *simple_h)
57 {
58 if (simple_h == NULL)
59 return;
60
61 scf_pg_destroy(simple_h->running_pg);
62 scf_pg_destroy(simple_h->editing_pg);
63 scf_snapshot_destroy(simple_h->snap);
64 scf_instance_destroy(simple_h->inst);
65 scf_handle_destroy(simple_h->h);
66 uu_free(simple_h);
67 }
68
69 /*
70 * Given a base service FMRI and the names of a property group and property,
71 * assemble_fmri() merges them into a property FMRI. Note that if the base
72 * FMRI is NULL, assemble_fmri() gets the base FMRI from scf_myname().
73 */
74
75 static char *
76 assemble_fmri(scf_handle_t *h, const char *base, const char *pg,
77 const char *prop)
78 {
79 size_t fmri_sz, pglen;
80 ssize_t baselen;
81 char *fmri_buf;
82
83 if (prop == NULL) {
84 (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
85 return (NULL);
86 }
87
88 if (pg == NULL)
89 pglen = strlen(SCF_PG_APP_DEFAULT);
90 else
91 pglen = strlen(pg);
92
93 if (base == NULL) {
94 if ((baselen = scf_myname(h, NULL, 0)) == -1)
95 return (NULL);
96 } else {
97 baselen = strlen(base);
98 }
99
100 fmri_sz = baselen + sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1 +
101 pglen + sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1 +
102 strlen(prop) + 1;
103
104 if ((fmri_buf = malloc(fmri_sz)) == NULL) {
105 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
106 return (NULL);
107 }
108
109 if (base == NULL) {
110 if (scf_myname(h, fmri_buf, fmri_sz) == -1) {
111 free(fmri_buf);
112 return (NULL);
113 }
114 } else {
115 (void) strcpy(fmri_buf, base);
116 }
117
118 (void) strcat(fmri_buf, SCF_FMRI_PROPERTYGRP_PREFIX);
119
120 if (pg == NULL)
121 (void) strcat(fmri_buf, SCF_PG_APP_DEFAULT);
122 else
123 (void) strcat(fmri_buf, pg);
124
125 (void) strcat(fmri_buf, SCF_FMRI_PROPERTY_PREFIX);
126 (void) strcat(fmri_buf, prop);
127 return (fmri_buf);
128 }
129
130 /*
131 * Given a property, this function allocates and fills an scf_simple_prop_t
132 * with the data it contains.
133 */
134
135 static scf_simple_prop_t *
136 fill_prop(scf_property_t *prop, const char *pgname, const char *propname,
137 scf_handle_t *h)
138 {
139 scf_simple_prop_t *ret;
140 scf_iter_t *iter;
141 scf_value_t *val;
142 int iterret, i;
143 ssize_t valsize, numvals;
144 union scf_simple_prop_val *vallist = NULL, *vallist_backup = NULL;
145
146 if ((ret = malloc(sizeof (*ret))) == NULL) {
147 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
148 return (NULL);
149 }
150
151 ret->pr_next = NULL;
152 ret->pr_pg = NULL;
153 ret->pr_iter = 0;
154
155 if (pgname == NULL)
156 ret->pr_pgname = strdup(SCF_PG_APP_DEFAULT);
157 else
158 ret->pr_pgname = strdup(pgname);
159
160 if (ret->pr_pgname == NULL) {
161 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
162 free(ret);
163 return (NULL);
164 }
165
166 if ((ret->pr_propname = strdup(propname)) == NULL) {
167 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
168 free(ret->pr_pgname);
169 free(ret);
170 return (NULL);
171 }
172
173 if (scf_property_type(prop, &ret->pr_type) == -1)
174 goto error3;
175
176 if ((iter = scf_iter_create(h)) == NULL)
177 goto error3;
178 if ((val = scf_value_create(h)) == NULL) {
179 scf_iter_destroy(iter);
180 goto error3;
181 }
182
183 if (scf_iter_property_values(iter, prop) == -1)
184 goto error1;
185
186 for (numvals = 0; (iterret = scf_iter_next_value(iter, val)) == 1;
187 numvals++) {
188 vallist_backup = vallist;
189 if ((vallist = realloc(vallist, (numvals + 1) *
190 sizeof (*vallist))) == NULL) {
191 vallist = vallist_backup;
192 goto error1;
193 }
194
195 switch (ret->pr_type) {
196 case SCF_TYPE_BOOLEAN:
197 if (scf_value_get_boolean(val,
198 &vallist[numvals].pv_bool) == -1)
199 goto error1;
200 break;
201
202 case SCF_TYPE_COUNT:
203 if (scf_value_get_count(val,
204 &vallist[numvals].pv_uint) == -1)
205 goto error1;
206 break;
207
208 case SCF_TYPE_INTEGER:
209 if (scf_value_get_integer(val,
210 &vallist[numvals].pv_int) == -1)
211 goto error1;
212 break;
213
214 case SCF_TYPE_TIME:
215 if (scf_value_get_time(val,
216 &vallist[numvals].pv_time.t_sec,
217 &vallist[numvals].pv_time.t_nsec) == -1)
218 goto error1;
219 break;
220
221 case SCF_TYPE_ASTRING:
222 vallist[numvals].pv_str = NULL;
223 if ((valsize = scf_value_get_astring(val, NULL, 0)) ==
224 -1)
225 goto error1;
226 if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
227 NULL) {
228 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
229 goto error1;
230 }
231 if (scf_value_get_astring(val,
232 vallist[numvals].pv_str, valsize+1) == -1) {
233 free(vallist[numvals].pv_str);
234 goto error1;
235 }
236 break;
237
238 case SCF_TYPE_USTRING:
239 case SCF_TYPE_HOST:
240 case SCF_TYPE_HOSTNAME:
241 case SCF_TYPE_NET_ADDR:
242 case SCF_TYPE_NET_ADDR_V4:
243 case SCF_TYPE_NET_ADDR_V6:
244 case SCF_TYPE_URI:
245 case SCF_TYPE_FMRI:
246 vallist[numvals].pv_str = NULL;
247 if ((valsize = scf_value_get_ustring(val, NULL, 0)) ==
248 -1)
249 goto error1;
250 if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
251 NULL) {
252 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
253 goto error1;
254 }
255 if (scf_value_get_ustring(val,
256 vallist[numvals].pv_str, valsize+1) == -1) {
257 free(vallist[numvals].pv_str);
258 goto error1;
259 }
260 break;
261
262 case SCF_TYPE_OPAQUE:
263 vallist[numvals].pv_opaque.o_value = NULL;
264 if ((valsize = scf_value_get_opaque(val, NULL, 0)) ==
265 -1)
266 goto error1;
267 if ((vallist[numvals].pv_opaque.o_value =
268 malloc(valsize)) == NULL) {
269 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
270 goto error1;
271 }
272 vallist[numvals].pv_opaque.o_size = valsize;
273 if (scf_value_get_opaque(val,
274 vallist[numvals].pv_opaque.o_value,
275 valsize) == -1) {
276 free(vallist[numvals].pv_opaque.o_value);
277 goto error1;
278 }
279 break;
280
281 default:
282 (void) scf_set_error(SCF_ERROR_INTERNAL);
283 goto error1;
284
285 }
286 }
287
288 if (iterret == -1) {
289 int err = scf_error();
290 if (err != SCF_ERROR_CONNECTION_BROKEN &&
291 err != SCF_ERROR_PERMISSION_DENIED)
292 (void) scf_set_error(SCF_ERROR_INTERNAL);
293 goto error1;
294 }
295
296 ret->pr_vallist = vallist;
297 ret->pr_numvalues = numvals;
298
299 scf_iter_destroy(iter);
300 (void) scf_value_destroy(val);
301
302 return (ret);
303
304 /*
305 * Exit point for a successful call. Below this line are exit points
306 * for failures at various stages during the function.
307 */
308
309 error1:
310 if (vallist == NULL)
311 goto error2;
312
313 switch (ret->pr_type) {
314 case SCF_TYPE_ASTRING:
315 case SCF_TYPE_USTRING:
316 case SCF_TYPE_HOST:
317 case SCF_TYPE_HOSTNAME:
318 case SCF_TYPE_NET_ADDR:
319 case SCF_TYPE_NET_ADDR_V4:
320 case SCF_TYPE_NET_ADDR_V6:
321 case SCF_TYPE_URI:
322 case SCF_TYPE_FMRI: {
323 for (i = 0; i < numvals; i++) {
324 free(vallist[i].pv_str);
325 }
326 break;
327 }
328 case SCF_TYPE_OPAQUE: {
329 for (i = 0; i < numvals; i++) {
330 free(vallist[i].pv_opaque.o_value);
331 }
332 break;
333 }
334 default:
335 break;
336 }
337
338 free(vallist);
339
340 error2:
341 scf_iter_destroy(iter);
342 (void) scf_value_destroy(val);
343
344 error3:
345 free(ret->pr_pgname);
346 free(ret->pr_propname);
347 free(ret);
348 return (NULL);
349 }
350
351 /*
352 * insert_app_props iterates over a property iterator, getting all the
353 * properties from a property group, and adding or overwriting them into
354 * a simple_app_props_t. This is used by scf_simple_app_props_get to provide
355 * service/instance composition while filling the app_props_t.
356 * insert_app_props iterates over a single property group.
357 */
358
359 static int
360 insert_app_props(scf_iter_t *propiter, char *pgname, char *propname, struct
361 scf_simple_pg *thispg, scf_property_t *prop, size_t namelen,
362 scf_handle_t *h)
363 {
364 scf_simple_prop_t *thisprop, *prevprop, *newprop;
365 uint8_t found;
366 int propiter_ret;
367
368 while ((propiter_ret = scf_iter_next_property(propiter, prop)) == 1) {
369
370 if (scf_property_get_name(prop, propname, namelen) < 0) {
371 if (scf_error() == SCF_ERROR_NOT_SET)
372 (void) scf_set_error(SCF_ERROR_INTERNAL);
373 return (-1);
374 }
375
376 thisprop = thispg->pg_proplist;
377 prevprop = thispg->pg_proplist;
378 found = 0;
379
380 while ((thisprop != NULL) && (!found)) {
381 if (strcmp(thisprop->pr_propname, propname) == 0) {
382 found = 1;
383 if ((newprop = fill_prop(prop, pgname,
384 propname, h)) == NULL)
385 return (-1);
386
387 if (thisprop == thispg->pg_proplist)
388 thispg->pg_proplist = newprop;
389 else
390 prevprop->pr_next = newprop;
391
392 newprop->pr_pg = thispg;
393 newprop->pr_next = thisprop->pr_next;
394 scf_simple_prop_free(thisprop);
395 thisprop = NULL;
396 } else {
397 if (thisprop != thispg->pg_proplist)
398 prevprop = prevprop->pr_next;
399 thisprop = thisprop->pr_next;
400 }
401 }
402
403 if (!found) {
404 if ((newprop = fill_prop(prop, pgname, propname, h)) ==
405 NULL)
406 return (-1);
407
408 if (thispg->pg_proplist == NULL)
409 thispg->pg_proplist = newprop;
410 else
411 prevprop->pr_next = newprop;
412
413 newprop->pr_pg = thispg;
414 }
415 }
416
417 if (propiter_ret == -1) {
418 if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
419 (void) scf_set_error(SCF_ERROR_INTERNAL);
420 return (-1);
421 }
422
423 return (0);
424 }
425
426
427 /*
428 * Sets up e in tx to set pname's values. Returns 0 on success or -1 on
429 * failure, with scf_error() set to
430 * SCF_ERROR_HANDLE_MISMATCH - tx & e are derived from different handles
431 * SCF_ERROR_INVALID_ARGUMENT - pname or ty are invalid
432 * SCF_ERROR_NOT_BOUND - handle is not bound
433 * SCF_ERROR_CONNECTION_BROKEN - connection was broken
434 * SCF_ERROR_NOT_SET - tx has not been started
435 * SCF_ERROR_DELETED - the pg tx was started on was deleted
436 */
437 static int
438 transaction_property_set(scf_transaction_t *tx, scf_transaction_entry_t *e,
439 const char *pname, scf_type_t ty)
440 {
441 for (;;) {
442 if (scf_transaction_property_change_type(tx, e, pname, ty) == 0)
443 return (0);
444
445 switch (scf_error()) {
446 case SCF_ERROR_HANDLE_MISMATCH:
447 case SCF_ERROR_INVALID_ARGUMENT:
448 case SCF_ERROR_NOT_BOUND:
449 case SCF_ERROR_CONNECTION_BROKEN:
450 case SCF_ERROR_NOT_SET:
451 case SCF_ERROR_DELETED:
452 default:
453 return (-1);
454
455 case SCF_ERROR_NOT_FOUND:
456 break;
457 }
458
459 if (scf_transaction_property_new(tx, e, pname, ty) == 0)
460 return (0);
461
462 switch (scf_error()) {
463 case SCF_ERROR_HANDLE_MISMATCH:
464 case SCF_ERROR_INVALID_ARGUMENT:
465 case SCF_ERROR_NOT_BOUND:
466 case SCF_ERROR_CONNECTION_BROKEN:
467 case SCF_ERROR_NOT_SET:
468 case SCF_ERROR_DELETED:
469 default:
470 return (-1);
471
472 case SCF_ERROR_EXISTS:
473 break;
474 }
475 }
476 }
477
478 static int
479 get_inst_enabled(const scf_instance_t *inst, const char *pgname)
480 {
481 scf_propertygroup_t *gpg = NULL;
482 scf_property_t *eprop = NULL;
483 scf_value_t *v = NULL;
484 scf_handle_t *h = NULL;
485 uint8_t enabled;
486 int ret = -1;
487
488 if ((h = scf_instance_handle(inst)) == NULL)
489 return (-1);
490
491 if ((gpg = scf_pg_create(h)) == NULL ||
492 (eprop = scf_property_create(h)) == NULL ||
493 (v = scf_value_create(h)) == NULL)
494 goto out;
495
496 if (scf_instance_get_pg(inst, pgname, gpg) ||
497 scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) ||
498 scf_property_get_value(eprop, v) ||
499 scf_value_get_boolean(v, &enabled))
500 goto out;
501 ret = enabled;
502
503 out:
504 scf_pg_destroy(gpg);
505 scf_property_destroy(eprop);
506 scf_value_destroy(v);
507 return (ret);
508 }
509
510 /*
511 * set_inst_enabled() is a "master" enable/disable call that takes the
512 * instance and the desired state for the enabled bit in the instance's
513 * named property group. If the group doesn't exist, it's created with the
514 * given flags. Called by smf_{dis,en}able_instance().
515 */
516 static int
517 set_inst_enabled(const scf_instance_t *inst, uint8_t desired,
518 const char *pgname, uint32_t pgflags)
519 {
520 scf_transaction_t *tx = NULL;
521 scf_transaction_entry_t *ent = NULL;
522 scf_propertygroup_t *gpg = NULL;
523 scf_property_t *eprop = NULL;
524 scf_value_t *v = NULL;
525 scf_handle_t *h = NULL;
526 int ret = -1;
527 int committed;
528 uint8_t b;
529
530 if ((h = scf_instance_handle(inst)) == NULL)
531 return (-1);
532
533 if ((gpg = scf_pg_create(h)) == NULL ||
534 (eprop = scf_property_create(h)) == NULL ||
535 (v = scf_value_create(h)) == NULL ||
536 (tx = scf_transaction_create(h)) == NULL ||
537 (ent = scf_entry_create(h)) == NULL)
538 goto out;
539
540 general_pg_get:
541 if (scf_instance_get_pg(inst, SCF_PG_GENERAL, gpg) == -1) {
542 if (scf_error() != SCF_ERROR_NOT_FOUND)
543 goto out;
544
545 if (scf_instance_add_pg(inst, SCF_PG_GENERAL,
546 SCF_GROUP_FRAMEWORK, SCF_PG_GENERAL_FLAGS, gpg) == -1) {
547 if (scf_error() != SCF_ERROR_EXISTS)
548 goto out;
549 goto general_pg_get;
550 }
551 }
552
553 if (strcmp(pgname, SCF_PG_GENERAL) != 0) {
554 get:
555 if (scf_instance_get_pg(inst, pgname, gpg) == -1) {
556 if (scf_error() != SCF_ERROR_NOT_FOUND)
557 goto out;
558
559 if (scf_instance_add_pg(inst, pgname,
560 SCF_GROUP_FRAMEWORK, pgflags, gpg) == -1) {
561 if (scf_error() != SCF_ERROR_EXISTS)
562 goto out;
563 goto get;
564 }
565 }
566 }
567
568 if (scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) == -1) {
569 if (scf_error() != SCF_ERROR_NOT_FOUND)
570 goto out;
571 else
572 goto set;
573 }
574
575 /*
576 * If it's already set the way we want, forgo the transaction.
577 */
578 if (scf_property_get_value(eprop, v) == -1) {
579 switch (scf_error()) {
580 case SCF_ERROR_CONSTRAINT_VIOLATED:
581 case SCF_ERROR_NOT_FOUND:
582 /* Misconfigured, so set anyway. */
583 goto set;
584
585 default:
586 goto out;
587 }
588 }
589 if (scf_value_get_boolean(v, &b) == -1) {
590 if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
591 goto out;
592 goto set;
593 }
594 if (b == desired) {
595 ret = 0;
596 goto out;
597 }
598
599 set:
600 do {
601 if (scf_transaction_start(tx, gpg) == -1)
602 goto out;
603
604 if (transaction_property_set(tx, ent, SCF_PROPERTY_ENABLED,
605 SCF_TYPE_BOOLEAN) != 0) {
606 switch (scf_error()) {
607 case SCF_ERROR_CONNECTION_BROKEN:
608 case SCF_ERROR_DELETED:
609 default:
610 goto out;
611
612 case SCF_ERROR_HANDLE_MISMATCH:
613 case SCF_ERROR_INVALID_ARGUMENT:
614 case SCF_ERROR_NOT_BOUND:
615 case SCF_ERROR_NOT_SET:
616 bad_error("transaction_property_set",
617 scf_error());
618 }
619 }
620
621 scf_value_set_boolean(v, desired);
622 if (scf_entry_add_value(ent, v) == -1)
623 goto out;
624
625 committed = scf_transaction_commit(tx);
626 if (committed == -1)
627 goto out;
628
629 scf_transaction_reset(tx);
630
631 if (committed == 0) { /* out-of-sync */
632 if (scf_pg_update(gpg) == -1)
633 goto out;
634 }
635 } while (committed == 0);
636
637 ret = 0;
638
639 out:
640 scf_value_destroy(v);
641 scf_entry_destroy(ent);
642 scf_transaction_destroy(tx);
643 scf_property_destroy(eprop);
644 scf_pg_destroy(gpg);
645
646 return (ret);
647 }
648
649 static int
650 delete_inst_enabled(const scf_instance_t *inst, const char *pgname)
651 {
652 scf_transaction_t *tx = NULL;
653 scf_transaction_entry_t *ent = NULL;
654 scf_propertygroup_t *gpg = NULL;
655 scf_handle_t *h = NULL;
656 int ret = -1;
657 int committed;
658
659 if ((h = scf_instance_handle(inst)) == NULL)
660 return (-1);
661
662 if ((gpg = scf_pg_create(h)) == NULL ||
663 (tx = scf_transaction_create(h)) == NULL ||
664 (ent = scf_entry_create(h)) == NULL)
665 goto out;
666
667 if (scf_instance_get_pg(inst, pgname, gpg) != 0)
668 goto error;
669 do {
670 if (scf_transaction_start(tx, gpg) == -1 ||
671 scf_transaction_property_delete(tx, ent,
672 SCF_PROPERTY_ENABLED) == -1 ||
673 (committed = scf_transaction_commit(tx)) == -1)
674 goto error;
675
676 scf_transaction_reset(tx);
677
678 if (committed == 0 && scf_pg_update(gpg) == -1)
679 goto error;
680 } while (committed == 0);
681
682 ret = 0;
683 goto out;
684
685 error:
686 switch (scf_error()) {
687 case SCF_ERROR_DELETED:
688 case SCF_ERROR_NOT_FOUND:
689 /* success */
690 ret = 0;
691 }
692
693 out:
694 scf_entry_destroy(ent);
695 scf_transaction_destroy(tx);
696 scf_pg_destroy(gpg);
697
698 return (ret);
699 }
700
701 /*
702 * Returns 0 on success or -1 on failure. On failure leaves scf_error() set to
703 * SCF_ERROR_HANDLE_DESTROYED - inst's handle has been destroyed
704 * SCF_ERROR_NOT_BOUND - inst's handle is not bound
705 * SCF_ERROR_CONNECTION_BROKEN - the repository connection was broken
706 * SCF_ERROR_NOT_SET - inst is not set
707 * SCF_ERROR_DELETED - inst was deleted
708 * SCF_ERROR_PERMISSION_DENIED
709 * SCF_ERROR_BACKEND_ACCESS
710 * SCF_ERROR_BACKEND_READONLY
711 */
712 static int
713 set_inst_action_inst(scf_instance_t *inst, const char *action)
714 {
715 scf_handle_t *h;
716 scf_transaction_t *tx = NULL;
717 scf_transaction_entry_t *ent = NULL;
718 scf_propertygroup_t *pg = NULL;
719 scf_property_t *prop = NULL;
720 scf_value_t *v = NULL;
721 int trans, ret = -1;
722 int64_t t;
723 hrtime_t timestamp;
724
725 if ((h = scf_instance_handle(inst)) == NULL ||
726 (pg = scf_pg_create(h)) == NULL ||
727 (prop = scf_property_create(h)) == NULL ||
728 (v = scf_value_create(h)) == NULL ||
729 (tx = scf_transaction_create(h)) == NULL ||
730 (ent = scf_entry_create(h)) == NULL)
731 goto out;
732
733 get:
734 if (scf_instance_get_pg(inst, SCF_PG_RESTARTER_ACTIONS, pg) == -1) {
735 switch (scf_error()) {
736 case SCF_ERROR_NOT_BOUND:
737 case SCF_ERROR_CONNECTION_BROKEN:
738 case SCF_ERROR_NOT_SET:
739 case SCF_ERROR_DELETED:
740 default:
741 goto out;
742
743 case SCF_ERROR_NOT_FOUND:
744 break;
745
746 case SCF_ERROR_HANDLE_MISMATCH:
747 case SCF_ERROR_INVALID_ARGUMENT:
748 bad_error("scf_instance_get_pg", scf_error());
749 }
750
751 /* Try creating the restarter_actions property group. */
752 add:
753 if (scf_instance_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
754 SCF_PG_RESTARTER_ACTIONS_TYPE,
755 SCF_PG_RESTARTER_ACTIONS_FLAGS, pg) == -1) {
756 switch (scf_error()) {
757 case SCF_ERROR_NOT_BOUND:
758 case SCF_ERROR_CONNECTION_BROKEN:
759 case SCF_ERROR_NOT_SET:
760 case SCF_ERROR_DELETED:
761 case SCF_ERROR_PERMISSION_DENIED:
762 case SCF_ERROR_BACKEND_ACCESS:
763 case SCF_ERROR_BACKEND_READONLY:
764 default:
765 goto out;
766
767 case SCF_ERROR_EXISTS:
768 goto get;
769
770 case SCF_ERROR_HANDLE_MISMATCH:
771 case SCF_ERROR_INVALID_ARGUMENT:
772 bad_error("scf_instance_add_pg", scf_error());
773 }
774 }
775 }
776
777 for (;;) {
778 timestamp = gethrtime();
779
780 if (scf_pg_get_property(pg, action, prop) != 0) {
781 switch (scf_error()) {
782 case SCF_ERROR_CONNECTION_BROKEN:
783 default:
784 goto out;
785
786 case SCF_ERROR_DELETED:
787 goto add;
788
789 case SCF_ERROR_NOT_FOUND:
790 break;
791
792 case SCF_ERROR_HANDLE_MISMATCH:
793 case SCF_ERROR_INVALID_ARGUMENT:
794 case SCF_ERROR_NOT_BOUND:
795 case SCF_ERROR_NOT_SET:
796 bad_error("scf_pg_get_property", scf_error());
797 }
798 } else if (scf_property_get_value(prop, v) != 0) {
799 switch (scf_error()) {
800 case SCF_ERROR_CONNECTION_BROKEN:
801 default:
802 goto out;
803
804 case SCF_ERROR_DELETED:
805 goto add;
806
807 case SCF_ERROR_CONSTRAINT_VIOLATED:
808 case SCF_ERROR_NOT_FOUND:
809 break;
810
811 case SCF_ERROR_HANDLE_MISMATCH:
812 case SCF_ERROR_NOT_BOUND:
813 case SCF_ERROR_NOT_SET:
814 bad_error("scf_property_get_value",
815 scf_error());
816 }
817 } else if (scf_value_get_integer(v, &t) != 0) {
818 bad_error("scf_value_get_integer", scf_error());
819 } else if (t > timestamp) {
820 break;
821 }
822
823 if (scf_transaction_start(tx, pg) == -1) {
824 switch (scf_error()) {
825 case SCF_ERROR_NOT_BOUND:
826 case SCF_ERROR_CONNECTION_BROKEN:
827 case SCF_ERROR_PERMISSION_DENIED:
828 case SCF_ERROR_BACKEND_ACCESS:
829 case SCF_ERROR_BACKEND_READONLY:
830 default:
831 goto out;
832
833 case SCF_ERROR_DELETED:
834 goto add;
835
836 case SCF_ERROR_HANDLE_MISMATCH:
837 case SCF_ERROR_NOT_SET:
838 case SCF_ERROR_IN_USE:
839 bad_error("scf_transaction_start", scf_error());
840 }
841 }
842
843 if (transaction_property_set(tx, ent, action,
844 SCF_TYPE_INTEGER) != 0) {
845 switch (scf_error()) {
846 case SCF_ERROR_NOT_BOUND:
847 case SCF_ERROR_CONNECTION_BROKEN:
848 case SCF_ERROR_DELETED:
849 default:
850 goto out;
851
852 case SCF_ERROR_HANDLE_MISMATCH:
853 case SCF_ERROR_INVALID_ARGUMENT:
854 case SCF_ERROR_NOT_SET:
855 bad_error("transaction_property_set",
856 scf_error());
857 }
858 }
859
860 scf_value_set_integer(v, timestamp);
861 if (scf_entry_add_value(ent, v) == -1)
862 bad_error("scf_entry_add_value", scf_error());
863
864 trans = scf_transaction_commit(tx);
865 if (trans == 1)
866 break;
867
868 if (trans != 0) {
869 switch (scf_error()) {
870 case SCF_ERROR_CONNECTION_BROKEN:
871 case SCF_ERROR_PERMISSION_DENIED:
872 case SCF_ERROR_BACKEND_ACCESS:
873 case SCF_ERROR_BACKEND_READONLY:
874 default:
875 goto out;
876
877 case SCF_ERROR_DELETED:
878 scf_transaction_reset(tx);
879 goto add;
880
881 case SCF_ERROR_INVALID_ARGUMENT:
882 case SCF_ERROR_NOT_BOUND:
883 case SCF_ERROR_NOT_SET:
884 bad_error("scf_transaction_commit",
885 scf_error());
886 }
887 }
888
889 scf_transaction_reset(tx);
890 if (scf_pg_update(pg) == -1) {
891 switch (scf_error()) {
892 case SCF_ERROR_CONNECTION_BROKEN:
893 default:
894 goto out;
895
896 case SCF_ERROR_DELETED:
897 goto add;
898
899 case SCF_ERROR_NOT_SET:
900 case SCF_ERROR_NOT_BOUND:
901 bad_error("scf_pg_update", scf_error());
902 }
903 }
904 }
905
906 ret = 0;
907
908 out:
909 scf_value_destroy(v);
910 scf_entry_destroy(ent);
911 scf_transaction_destroy(tx);
912 scf_property_destroy(prop);
913 scf_pg_destroy(pg);
914 return (ret);
915 }
916
917 static int
918 set_inst_action(const char *fmri, const char *action)
919 {
920 scf_handle_t *h;
921 scf_instance_t *inst;
922 int ret = -1;
923
924 h = _scf_handle_create_and_bind(SCF_VERSION);
925 if (h == NULL)
926 return (-1);
927
928 inst = scf_instance_create(h);
929
930 if (inst != NULL) {
931 if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
932 NULL, SCF_DECODE_FMRI_EXACT) == 0) {
933 ret = set_inst_action_inst(inst, action);
934 if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
935 (void) scf_set_error(SCF_ERROR_NOT_FOUND);
936 } else {
937 switch (scf_error()) {
938 case SCF_ERROR_CONSTRAINT_VIOLATED:
939 (void) scf_set_error(
940 SCF_ERROR_INVALID_ARGUMENT);
941 break;
942 case SCF_ERROR_DELETED:
943 (void) scf_set_error(SCF_ERROR_NOT_FOUND);
944 break;
945 }
946 }
947
948 scf_instance_destroy(inst);
949 }
950
951 scf_handle_destroy(h);
952
953 return (ret);
954 }
955
956
957 /*
958 * get_inst_state() gets the state string from an instance, and returns
959 * the SCF_STATE_* constant that coincides with the instance's current state.
960 */
961
962 static int
963 get_inst_state(scf_instance_t *inst, scf_handle_t *h)
964 {
965 scf_propertygroup_t *pg = NULL;
966 scf_property_t *prop = NULL;
967 scf_value_t *val = NULL;
968 char state[MAX_SCF_STATE_STRING_SZ];
969 int ret = -1;
970
971 if (((pg = scf_pg_create(h)) == NULL) ||
972 ((prop = scf_property_create(h)) == NULL) ||
973 ((val = scf_value_create(h)) == NULL))
974 goto out;
975
976 /* Pull the state property from the instance */
977
978 if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) == -1 ||
979 scf_pg_get_property(pg, SCF_PROPERTY_STATE, prop) == -1 ||
980 scf_property_get_value(prop, val) == -1) {
981 if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
982 (void) scf_set_error(SCF_ERROR_INTERNAL);
983 goto out;
984 }
985
986 if (scf_value_get_astring(val, state, sizeof (state)) <= 0) {
987 (void) scf_set_error(SCF_ERROR_INTERNAL);
988 goto out;
989 }
990
991 if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0) {
992 ret = SCF_STATE_UNINIT;
993 } else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
994 ret = SCF_STATE_MAINT;
995 } else if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0) {
996 ret = SCF_STATE_OFFLINE;
997 } else if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0) {
998 ret = SCF_STATE_DISABLED;
999 } else if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
1000 ret = SCF_STATE_ONLINE;
1001 } else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0) {
1002 ret = SCF_STATE_DEGRADED;
1003 }
1004
1005 out:
1006 scf_pg_destroy(pg);
1007 scf_property_destroy(prop);
1008 (void) scf_value_destroy(val);
1009
1010 return (ret);
1011 }
1012
1013 /*
1014 * Sets an instance to be enabled or disabled after reboot, using the
1015 * temporary (overriding) general_ovr property group to reflect the
1016 * present state, if it is different.
1017 */
1018 static int
1019 set_inst_enabled_atboot(scf_instance_t *inst, uint8_t desired)
1020 {
1021 int enabled;
1022 int persistent;
1023 int ret = -1;
1024
1025 if ((persistent = get_inst_enabled(inst, SCF_PG_GENERAL)) < 0) {
1026 if (scf_error() != SCF_ERROR_NOT_FOUND)
1027 goto out;
1028 persistent = B_FALSE;
1029 }
1030 if ((enabled = get_inst_enabled(inst, SCF_PG_GENERAL_OVR)) < 0) {
1031 enabled = persistent;
1032 if (persistent != desired) {
1033 /*
1034 * Temporarily store the present enabled state.
1035 */
1036 if (set_inst_enabled(inst, persistent,
1037 SCF_PG_GENERAL_OVR, SCF_PG_GENERAL_OVR_FLAGS))
1038 goto out;
1039 }
1040 }
1041 if (persistent != desired)
1042 if (set_inst_enabled(inst, desired, SCF_PG_GENERAL,
1043 SCF_PG_GENERAL_FLAGS))
1044 goto out;
1045 if (enabled == desired)
1046 ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
1047 else
1048 ret = 0;
1049
1050 out:
1051 return (ret);
1052 }
1053
1054 static int
1055 set_inst_enabled_flags(const char *fmri, int flags, uint8_t desired)
1056 {
1057 int ret = -1;
1058 scf_handle_t *h;
1059 scf_instance_t *inst;
1060
1061 if (flags & ~(SMF_TEMPORARY | SMF_AT_NEXT_BOOT) ||
1062 flags & SMF_TEMPORARY && flags & SMF_AT_NEXT_BOOT) {
1063 (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
1064 return (ret);
1065 }
1066
1067 if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL)
1068 return (ret);
1069
1070 if ((inst = scf_instance_create(h)) == NULL) {
1071 scf_handle_destroy(h);
1072 return (ret);
1073 }
1074
1075 if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
1076 SCF_DECODE_FMRI_EXACT) == -1) {
1077 if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
1078 (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
1079 goto out;
1080 }
1081
1082 if (flags & SMF_AT_NEXT_BOOT) {
1083 ret = set_inst_enabled_atboot(inst, desired);
1084 } else {
1085 if (set_inst_enabled(inst, desired, flags & SMF_TEMPORARY ?
1086 SCF_PG_GENERAL_OVR : SCF_PG_GENERAL, flags & SMF_TEMPORARY ?
1087 SCF_PG_GENERAL_OVR_FLAGS : SCF_PG_GENERAL_FLAGS))
1088 goto out;
1089
1090 /*
1091 * Make the persistent value effective by deleting the
1092 * temporary one.
1093 */
1094 if (flags & SMF_TEMPORARY)
1095 ret = 0;
1096 else
1097 ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
1098 }
1099
1100 out:
1101 scf_instance_destroy(inst);
1102 scf_handle_destroy(h);
1103 if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
1104 (void) scf_set_error(SCF_ERROR_NOT_FOUND);
1105 return (ret);
1106 }
1107
1108 /*
1109 * Create and return a pg from the instance associated with the given handle.
1110 * This function is only called in scf_transaction_setup and
1111 * scf_transaction_restart where the h->rh_instance pointer is properly filled
1112 * in by scf_general_setup_pg().
1113 */
1114 static scf_propertygroup_t *
1115 get_instance_pg(scf_simple_handle_t *simple_h)
1116 {
1117 scf_propertygroup_t *ret_pg = scf_pg_create(simple_h->h);
1118 char *pg_name;
1119 ssize_t namelen;
1120
1121 if (ret_pg == NULL) {
1122 return (NULL);
1123 }
1124
1125 namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
1126 assert(namelen > 0);
1127
1128 if ((pg_name = malloc(namelen)) == NULL) {
1129 if (scf_error() == SCF_ERROR_NOT_SET) {
1130 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
1131 }
1132 return (NULL);
1133 }
1134
1135 if (scf_pg_get_name(simple_h->running_pg, pg_name, namelen) < 0) {
1136 if (scf_error() == SCF_ERROR_NOT_SET) {
1137 (void) scf_set_error(SCF_ERROR_INTERNAL);
1138 }
1139 return (NULL);
1140 }
1141
1142 /* Get pg from instance */
1143 if (scf_instance_get_pg(simple_h->inst, pg_name, ret_pg) == -1) {
1144 return (NULL);
1145 }
1146
1147 return (ret_pg);
1148 }
1149
1150 int
1151 smf_enable_instance(const char *fmri, int flags)
1152 {
1153 return (set_inst_enabled_flags(fmri, flags, B_TRUE));
1154 }
1155
1156 int
1157 smf_disable_instance(const char *fmri, int flags)
1158 {
1159 return (set_inst_enabled_flags(fmri, flags, B_FALSE));
1160 }
1161
1162 int
1163 _smf_refresh_instance_i(scf_instance_t *inst)
1164 {
1165 return (set_inst_action_inst(inst, SCF_PROPERTY_REFRESH));
1166 }
1167
1168 int
1169 _smf_refresh_all_instances(scf_service_t *s)
1170 {
1171 scf_handle_t *h = scf_service_handle(s);
1172 scf_instance_t *i = scf_instance_create(h);
1173 scf_iter_t *it = scf_iter_create(h);
1174 int err, r = -1;
1175
1176 if (h == NULL || i == NULL || it == NULL)
1177 goto error;
1178
1179 if (scf_iter_service_instances(it, s) != 0)
1180 goto error;
1181
1182 while ((err = scf_iter_next_instance(it, i)) == 1)
1183 if (_smf_refresh_instance_i(i) != 0)
1184 goto error;
1185
1186 if (err == -1)
1187 goto error;
1188
1189 r = 0;
1190 error:
1191 scf_instance_destroy(i);
1192 scf_iter_destroy(it);
1193
1194 return (r);
1195 }
1196
1197 int
1198 smf_refresh_instance(const char *instance)
1199 {
1200 return (set_inst_action(instance, SCF_PROPERTY_REFRESH));
1201 }
1202
1203 int
1204 smf_restart_instance(const char *instance)
1205 {
1206 return (set_inst_action(instance, SCF_PROPERTY_RESTART));
1207 }
1208
1209 int
1210 smf_maintain_instance(const char *instance, int flags)
1211 {
1212 if (flags & SMF_TEMPORARY)
1213 return (set_inst_action(instance,
1214 (flags & SMF_IMMEDIATE) ?
1215 SCF_PROPERTY_MAINT_ON_IMMTEMP :
1216 SCF_PROPERTY_MAINT_ON_TEMPORARY));
1217 else
1218 return (set_inst_action(instance,
1219 (flags & SMF_IMMEDIATE) ?
1220 SCF_PROPERTY_MAINT_ON_IMMEDIATE :
1221 SCF_PROPERTY_MAINT_ON));
1222 }
1223
1224 int
1225 smf_degrade_instance(const char *instance, int flags)
1226 {
1227 scf_simple_prop_t *prop;
1228 const char *state_str;
1229
1230 if (flags & SMF_TEMPORARY)
1231 return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
1232
1233 if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
1234 SCF_PROPERTY_STATE)) == NULL)
1235 return (SCF_FAILED);
1236
1237 if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
1238 scf_simple_prop_free(prop);
1239 return (SCF_FAILED);
1240 }
1241
1242 if (strcmp(state_str, SCF_STATE_STRING_ONLINE) != 0) {
1243 scf_simple_prop_free(prop);
1244 return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
1245 }
1246 scf_simple_prop_free(prop);
1247
1248 return (set_inst_action(instance, (flags & SMF_IMMEDIATE) ?
1249 SCF_PROPERTY_DEGRADE_IMMEDIATE : SCF_PROPERTY_DEGRADED));
1250 }
1251
1252 int
1253 smf_restore_instance(const char *instance)
1254 {
1255 scf_simple_prop_t *prop;
1256 const char *state_str;
1257 int ret;
1258
1259 if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
1260 SCF_PROPERTY_STATE)) == NULL)
1261 return (SCF_FAILED);
1262
1263 if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
1264 scf_simple_prop_free(prop);
1265 return (SCF_FAILED);
1266 }
1267
1268 if (strcmp(state_str, SCF_STATE_STRING_MAINT) == 0) {
1269 ret = set_inst_action(instance, SCF_PROPERTY_MAINT_OFF);
1270 } else if (strcmp(state_str, SCF_STATE_STRING_DEGRADED) == 0) {
1271 ret = set_inst_action(instance, SCF_PROPERTY_RESTORE);
1272 } else {
1273 ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED);
1274 }
1275
1276 scf_simple_prop_free(prop);
1277 return (ret);
1278 }
1279
1280 /*
1281 * Wait for the given instance to finish transitioning. This can be gleamed
1282 * from looking at the restarter/next_state property, which settles to "none"
1283 * when the service has finished. This is true even in the case of failure.
1284 *
1285 * Some services take their sweet time so we don't bother with a timeout here.
1286 * Instead we let svc.startd deal with timing out the service, after which
1287 * restarter/next_state gets set to "none" and we return.
1288 */
1289 static int
1290 wait_for_transition(const char *instance, const char *state)
1291 {
1292 scf_simple_prop_t *prop;
1293 const char *state_str;
1294
1295 while (1) {
1296 if ((prop = scf_simple_prop_get(NULL, instance,
1297 SCF_PG_RESTARTER, SCF_PROPERTY_NEXT_STATE)) == NULL)
1298 return (SCF_FAILED);
1299
1300 if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
1301 scf_simple_prop_free(prop);
1302 return (SCF_FAILED);
1303 }
1304
1305 if (strcmp(state_str, SCF_STATE_STRING_NONE) == 0) {
1306 scf_simple_prop_free(prop);
1307 break;
1308 }
1309
1310 scf_simple_prop_free(prop);
1311 (void) sleep(1);
1312 }
1313
1314 if (state != NULL) {
1315 if ((prop = scf_simple_prop_get(NULL, instance,
1316 SCF_PG_RESTARTER, SCF_PROPERTY_STATE)) == NULL)
1317 return (SCF_FAILED);
1318
1319 if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
1320 scf_simple_prop_free(prop);
1321 return (SCF_FAILED);
1322 }
1323
1324 if (strcmp(state_str, state) != 0) {
1325 scf_simple_prop_free(prop);
1326 return (SCF_FAILED);
1327 }
1328
1329 scf_simple_prop_free(prop);
1330 }
1331
1332 return (SCF_SUCCESS);
1333 }
1334
1335 int
1336 smf_enable_instance_synchronous(const char *instance, int flags)
1337 {
1338 int ret;
1339
1340 if ((ret = smf_enable_instance(instance, flags)) != SCF_SUCCESS)
1341 return (ret);
1342
1343 return (wait_for_transition(instance, SCF_STATE_STRING_ONLINE));
1344 }
1345
1346 int
1347 smf_disable_instance_synchronous(const char *instance, int flags)
1348 {
1349 int ret;
1350
1351 if ((ret = smf_disable_instance(instance, flags)) != SCF_SUCCESS)
1352 return (ret);
1353
1354 return (wait_for_transition(instance, SCF_STATE_STRING_DISABLED));
1355 }
1356
1357 int
1358 smf_refresh_instance_synchronous(const char *instance)
1359 {
1360 int ret;
1361
1362 if ((ret = smf_refresh_instance(instance)) != SCF_SUCCESS)
1363 return (ret);
1364
1365 return (wait_for_transition(instance, NULL)); /* ignore state */
1366 }
1367
1368 int
1369 smf_restart_instance_synchronous(const char *instance)
1370 {
1371 int ret;
1372
1373 if ((ret = smf_restart_instance(instance)) != SCF_SUCCESS)
1374 return (ret);
1375
1376 return (wait_for_transition(instance, NULL)); /* ignore state */
1377 }
1378
1379 int
1380 smf_maintain_instance_synchronous(const char *instance, int flags)
1381 {
1382 int ret;
1383
1384 if ((ret = smf_maintain_instance(instance, flags)) != SCF_SUCCESS)
1385 return (ret);
1386
1387 return (wait_for_transition(instance, SCF_STATE_STRING_MAINT));
1388 }
1389
1390 int
1391 smf_degrade_instance_synchronous(const char *instance, int flags)
1392 {
1393 int ret;
1394
1395 if ((ret = smf_degrade_instance(instance, flags)) != SCF_SUCCESS)
1396 return (ret);
1397
1398 return (wait_for_transition(instance, SCF_STATE_STRING_DEGRADED));
1399 }
1400
1401 int
1402 smf_restore_instance_synchronous(const char *instance)
1403 {
1404 int ret;
1405
1406 if ((ret = smf_restore_instance(instance)) != SCF_SUCCESS)
1407 return (ret);
1408
1409 return (wait_for_transition(instance, SCF_STATE_STRING_ONLINE));
1410 }
1411
1412 char *
1413 smf_get_state(const char *instance)
1414 {
1415 scf_simple_prop_t *prop;
1416 const char *state_str;
1417 char *ret;
1418
1419 if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
1420 SCF_PROPERTY_STATE)) == NULL)
1421 return (NULL);
1422
1423 if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
1424 scf_simple_prop_free(prop);
1425 return (NULL);
1426 }
1427
1428 if ((ret = strdup(state_str)) == NULL)
1429 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
1430
1431 scf_simple_prop_free(prop);
1432 return (ret);
1433 }
1434
1435 /*
1436 * scf_general_pg_setup(fmri, pg_name)
1437 * Create a scf_simple_handle_t and fill in the instance, snapshot, and
1438 * property group fields associated with the given fmri and property group
1439 * name.
1440 * Returns:
1441 * Handle on success
1442 * Null on error with scf_error set to:
1443 * SCF_ERROR_HANDLE_MISMATCH,
1444 * SCF_ERROR_INVALID_ARGUMENT,
1445 * SCF_ERROR_CONSTRAINT_VIOLATED,
1446 * SCF_ERROR_NOT_FOUND,
1447 * SCF_ERROR_NOT_SET,
1448 * SCF_ERROR_DELETED,
1449 * SCF_ERROR_NOT_BOUND,
1450 * SCF_ERROR_CONNECTION_BROKEN,
1451 * SCF_ERROR_INTERNAL,
1452 * SCF_ERROR_NO_RESOURCES,
1453 * SCF_ERROR_BACKEND_ACCESS
1454 */
1455 scf_simple_handle_t *
1456 scf_general_pg_setup(const char *fmri, const char *pg_name)
1457 {
1458 scf_simple_handle_t *ret;
1459
1460 ret = uu_zalloc(sizeof (*ret));
1461 if (ret == NULL) {
1462 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
1463 return (NULL);
1464 } else {
1465
1466 ret->h = _scf_handle_create_and_bind(SCF_VERSION);
1467 ret->inst = scf_instance_create(ret->h);
1468 ret->snap = scf_snapshot_create(ret->h);
1469 ret->running_pg = scf_pg_create(ret->h);
1470 }
1471
1472 if ((ret->h == NULL) || (ret->inst == NULL) ||
1473 (ret->snap == NULL) || (ret->running_pg == NULL)) {
1474 goto out;
1475 }
1476
1477 if (scf_handle_decode_fmri(ret->h, fmri, NULL, NULL, ret->inst,
1478 NULL, NULL, NULL) == -1) {
1479 goto out;
1480 }
1481
1482 if ((scf_instance_get_snapshot(ret->inst, "running", ret->snap))
1483 != 0) {
1484 goto out;
1485 }
1486
1487 if (scf_instance_get_pg_composed(ret->inst, ret->snap, pg_name,
1488 ret->running_pg) != 0) {
1489 goto out;
1490 }
1491
1492 return (ret);
1493
1494 out:
1495 scf_simple_handle_destroy(ret);
1496 return (NULL);
1497 }
1498
1499 /*
1500 * scf_transaction_setup(h)
1501 * creates and starts the transaction
1502 * Returns:
1503 * transaction on success
1504 * NULL on failure with scf_error set to:
1505 * SCF_ERROR_NO_MEMORY,
1506 * SCF_ERROR_INVALID_ARGUMENT,
1507 * SCF_ERROR_HANDLE_DESTROYED,
1508 * SCF_ERROR_INTERNAL,
1509 * SCF_ERROR_NO_RESOURCES,
1510 * SCF_ERROR_NOT_BOUND,
1511 * SCF_ERROR_CONNECTION_BROKEN,
1512 * SCF_ERROR_NOT_SET,
1513 * SCF_ERROR_DELETED,
1514 * SCF_ERROR_CONSTRAINT_VIOLATED,
1515 * SCF_ERROR_HANDLE_MISMATCH,
1516 * SCF_ERROR_BACKEND_ACCESS,
1517 * SCF_ERROR_IN_USE
1518 */
1519 scf_transaction_t *
1520 scf_transaction_setup(scf_simple_handle_t *simple_h)
1521 {
1522 scf_transaction_t *tx = NULL;
1523
1524 if ((tx = scf_transaction_create(simple_h->h)) == NULL) {
1525 return (NULL);
1526 }
1527
1528 if ((simple_h->editing_pg = get_instance_pg(simple_h)) == NULL) {
1529 return (NULL);
1530 }
1531
1532 if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
1533 scf_pg_destroy(simple_h->editing_pg);
1534 simple_h->editing_pg = NULL;
1535 return (NULL);
1536 }
1537
1538 return (tx);
1539 }
1540
1541 int
1542 scf_transaction_restart(scf_simple_handle_t *simple_h, scf_transaction_t *tx)
1543 {
1544 scf_transaction_reset(tx);
1545
1546 if (scf_pg_update(simple_h->editing_pg) == -1) {
1547 return (SCF_FAILED);
1548 }
1549
1550 if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
1551 return (SCF_FAILED);
1552 }
1553
1554 return (SCF_SUCCESS);
1555 }
1556
1557 /*
1558 * scf_read_count_property(scf_simple_handle_t *simple_h, char *prop_name,
1559 * uint64_t *ret_count)
1560 *
1561 * For the given property name, return the count value.
1562 * RETURNS:
1563 * SCF_SUCCESS
1564 * SCF_FAILED on failure with scf_error() set to:
1565 * SCF_ERROR_HANDLE_DESTROYED
1566 * SCF_ERROR_INTERNAL
1567 * SCF_ERROR_NO_RESOURCES
1568 * SCF_ERROR_NO_MEMORY
1569 * SCF_ERROR_HANDLE_MISMATCH
1570 * SCF_ERROR_INVALID_ARGUMENT
1571 * SCF_ERROR_NOT_BOUND
1572 * SCF_ERROR_CONNECTION_BROKEN
1573 * SCF_ERROR_NOT_SET
1574 * SCF_ERROR_DELETED
1575 * SCF_ERROR_BACKEND_ACCESS
1576 * SCF_ERROR_CONSTRAINT_VIOLATED
1577 * SCF_ERROR_TYPE_MISMATCH
1578 */
1579 int
1580 scf_read_count_property(
1581 scf_simple_handle_t *simple_h,
1582 char *prop_name,
1583 uint64_t *ret_count)
1584 {
1585 scf_property_t *prop = scf_property_create(simple_h->h);
1586 scf_value_t *val = scf_value_create(simple_h->h);
1587 int ret = SCF_FAILED;
1588
1589 if ((val == NULL) || (prop == NULL)) {
1590 goto out;
1591 }
1592
1593 /*
1594 * Get the property struct that goes with this property group and
1595 * property name.
1596 */
1597 if (scf_pg_get_property(simple_h->running_pg, prop_name, prop) != 0) {
1598 goto out;
1599 }
1600
1601 /* Get the value structure */
1602 if (scf_property_get_value(prop, val) == -1) {
1603 goto out;
1604 }
1605
1606 /*
1607 * Now get the count value.
1608 */
1609 if (scf_value_get_count(val, ret_count) == -1) {
1610 goto out;
1611 }
1612
1613 ret = SCF_SUCCESS;
1614
1615 out:
1616 scf_property_destroy(prop);
1617 scf_value_destroy(val);
1618 return (ret);
1619 }
1620
1621 /*
1622 * scf_trans_add_count_property(trans, propname, count, create_flag)
1623 *
1624 * Set a count property transaction entry into the pending SMF transaction.
1625 * The transaction is created and committed outside of this function.
1626 * Returns:
1627 * SCF_SUCCESS
1628 * SCF_FAILED on failure with scf_error() set to:
1629 * SCF_ERROR_HANDLE_DESTROYED,
1630 * SCF_ERROR_INVALID_ARGUMENT,
1631 * SCF_ERROR_NO_MEMORY,
1632 * SCF_ERROR_HANDLE_MISMATCH,
1633 * SCF_ERROR_NOT_SET,
1634 * SCF_ERROR_IN_USE,
1635 * SCF_ERROR_NOT_FOUND,
1636 * SCF_ERROR_EXISTS,
1637 * SCF_ERROR_TYPE_MISMATCH,
1638 * SCF_ERROR_NOT_BOUND,
1639 * SCF_ERROR_CONNECTION_BROKEN,
1640 * SCF_ERROR_INTERNAL,
1641 * SCF_ERROR_DELETED,
1642 * SCF_ERROR_NO_RESOURCES,
1643 * SCF_ERROR_BACKEND_ACCESS
1644 */
1645 int
1646 scf_set_count_property(
1647 scf_transaction_t *trans,
1648 char *propname,
1649 uint64_t count,
1650 boolean_t create_flag)
1651 {
1652 scf_handle_t *handle = scf_transaction_handle(trans);
1653 scf_value_t *value = scf_value_create(handle);
1654 scf_transaction_entry_t *entry = scf_entry_create(handle);
1655
1656 if ((value == NULL) || (entry == NULL)) {
1657 return (SCF_FAILED);
1658 }
1659
1660 /*
1661 * Property must be set in transaction and won't take
1662 * effect until the transaction is committed.
1663 *
1664 * Attempt to change the current value. However, create new property
1665 * if it doesn't exist and the create flag is set.
1666 */
1667 if (scf_transaction_property_change(trans, entry, propname,
1668 SCF_TYPE_COUNT) == 0) {
1669 scf_value_set_count(value, count);
1670 if (scf_entry_add_value(entry, value) == 0) {
1671 return (SCF_SUCCESS);
1672 }
1673 } else {
1674 if ((create_flag == B_TRUE) &&
1675 (scf_error() == SCF_ERROR_NOT_FOUND)) {
1676 if (scf_transaction_property_new(trans, entry, propname,
1677 SCF_TYPE_COUNT) == 0) {
1678 scf_value_set_count(value, count);
1679 if (scf_entry_add_value(entry, value) == 0) {
1680 return (SCF_SUCCESS);
1681 }
1682 }
1683 }
1684 }
1685
1686 /*
1687 * cleanup if there were any errors that didn't leave these
1688 * values where they would be cleaned up later.
1689 */
1690 if (value != NULL)
1691 scf_value_destroy(value);
1692 if (entry != NULL)
1693 scf_entry_destroy(entry);
1694 return (SCF_FAILED);
1695 }
1696
1697 int
1698 scf_simple_walk_instances(uint_t state_flags, void *private,
1699 int (*inst_callback)(scf_handle_t *, scf_instance_t *, void *))
1700 {
1701 scf_scope_t *scope = NULL;
1702 scf_service_t *svc = NULL;
1703 scf_instance_t *inst = NULL;
1704 scf_iter_t *svc_iter = NULL, *inst_iter = NULL;
1705 scf_handle_t *h = NULL;
1706 int ret = SCF_FAILED;
1707 int svc_iter_ret, inst_iter_ret;
1708 int inst_state;
1709
1710 if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL)
1711 return (ret);
1712
1713 if (((scope = scf_scope_create(h)) == NULL) ||
1714 ((svc = scf_service_create(h)) == NULL) ||
1715 ((inst = scf_instance_create(h)) == NULL) ||
1716 ((svc_iter = scf_iter_create(h)) == NULL) ||
1717 ((inst_iter = scf_iter_create(h)) == NULL))
1718 goto out;
1719
1720 /*
1721 * Get the local scope, and set up nested iteration through every
1722 * local service, and every instance of every service.
1723 */
1724
1725 if ((scf_handle_get_local_scope(h, scope) != SCF_SUCCESS) ||
1726 (scf_iter_scope_services(svc_iter, scope) != SCF_SUCCESS))
1727 goto out;
1728
1729 while ((svc_iter_ret = scf_iter_next_service(svc_iter, svc)) > 0) {
1730
1731 if ((scf_iter_service_instances(inst_iter, svc)) !=
1732 SCF_SUCCESS)
1733 goto out;
1734
1735 while ((inst_iter_ret =
1736 scf_iter_next_instance(inst_iter, inst)) > 0) {
1737 /*
1738 * If get_inst_state fails from an internal error,
1739 * IE, being unable to get the property group or
1740 * property containing the state of the instance,
1741 * we continue instead of failing, as this might just
1742 * be an improperly configured instance.
1743 */
1744 if ((inst_state = get_inst_state(inst, h)) == -1) {
1745 if (scf_error() == SCF_ERROR_INTERNAL) {
1746 continue;
1747 } else {
1748 goto out;
1749 }
1750 }
1751
1752 if ((uint_t)inst_state & state_flags) {
1753 if (inst_callback(h, inst, private) !=
1754 SCF_SUCCESS) {
1755 (void) scf_set_error(
1756 SCF_ERROR_CALLBACK_FAILED);
1757 goto out;
1758 }
1759 }
1760 }
1761
1762 if (inst_iter_ret == -1)
1763 goto out;
1764 scf_iter_reset(inst_iter);
1765 }
1766
1767 if (svc_iter_ret != -1)
1768 ret = SCF_SUCCESS;
1769
1770 out:
1771 scf_scope_destroy(scope);
1772 scf_service_destroy(svc);
1773 scf_instance_destroy(inst);
1774 scf_iter_destroy(svc_iter);
1775 scf_iter_destroy(inst_iter);
1776 scf_handle_destroy(h);
1777
1778 return (ret);
1779 }
1780
1781
1782 scf_simple_prop_t *
1783 scf_simple_prop_get(scf_handle_t *hin, const char *instance, const char *pgname,
1784 const char *propname)
1785 {
1786 char *fmri_buf, *svcfmri = NULL;
1787 ssize_t fmri_sz;
1788 scf_property_t *prop = NULL;
1789 scf_service_t *svc = NULL;
1790 scf_simple_prop_t *ret;
1791 scf_handle_t *h = NULL;
1792 boolean_t local_h = B_TRUE;
1793
1794 /* If the user passed in a handle, use it. */
1795 if (hin != NULL) {
1796 h = hin;
1797 local_h = B_FALSE;
1798 }
1799
1800 if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
1801 return (NULL);
1802
1803 if ((fmri_buf = assemble_fmri(h, instance, pgname, propname)) == NULL) {
1804 if (local_h)
1805 scf_handle_destroy(h);
1806 return (NULL);
1807 }
1808
1809 if ((svc = scf_service_create(h)) == NULL ||
1810 (prop = scf_property_create(h)) == NULL)
1811 goto error1;
1812 if (scf_handle_decode_fmri(h, fmri_buf, NULL, NULL, NULL, NULL, prop,
1813 SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
1814 switch (scf_error()) {
1815 /*
1816 * If the property isn't found in the instance, we grab the
1817 * underlying service, create an FMRI out of it, and then
1818 * query the datastore again at the service level for the
1819 * property.
1820 */
1821 case SCF_ERROR_NOT_FOUND:
1822 if (scf_handle_decode_fmri(h, fmri_buf, NULL, svc,
1823 NULL, NULL, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1)
1824 goto error1;
1825
1826 fmri_sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1;
1827 assert(fmri_sz > 0);
1828
1829 if (scf_service_to_fmri(svc, fmri_buf, fmri_sz) == -1)
1830 goto error1;
1831 if ((svcfmri = assemble_fmri(h, fmri_buf, pgname,
1832 propname)) == NULL)
1833 goto error1;
1834 if (scf_handle_decode_fmri(h, svcfmri, NULL, NULL,
1835 NULL, NULL, prop, 0) == -1) {
1836 free(svcfmri);
1837 goto error1;
1838 }
1839 free(svcfmri);
1840 break;
1841 case SCF_ERROR_CONSTRAINT_VIOLATED:
1842 (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
1843 default:
1844 goto error1;
1845 }
1846 }
1847 /*
1848 * At this point, we've successfully pulled the property from the
1849 * datastore, and simply need to copy its innards into an
1850 * scf_simple_prop_t.
1851 */
1852 if ((ret = fill_prop(prop, pgname, propname, h)) == NULL)
1853 goto error1;
1854
1855 scf_service_destroy(svc);
1856 scf_property_destroy(prop);
1857 free(fmri_buf);
1858 if (local_h)
1859 scf_handle_destroy(h);
1860 return (ret);
1861
1862 /*
1863 * Exit point for a successful call. Below this line are exit points
1864 * for failures at various stages during the function.
1865 */
1866
1867 error1:
1868 scf_service_destroy(svc);
1869 scf_property_destroy(prop);
1870 error2:
1871 free(fmri_buf);
1872 if (local_h)
1873 scf_handle_destroy(h);
1874 return (NULL);
1875 }
1876
1877
1878 void
1879 scf_simple_prop_free(scf_simple_prop_t *prop)
1880 {
1881 int i;
1882
1883 if (prop == NULL)
1884 return;
1885
1886 free(prop->pr_propname);
1887 free(prop->pr_pgname);
1888 switch (prop->pr_type) {
1889 case SCF_TYPE_OPAQUE: {
1890 for (i = 0; i < prop->pr_numvalues; i++) {
1891 free(prop->pr_vallist[i].pv_opaque.o_value);
1892 }
1893 break;
1894 }
1895 case SCF_TYPE_ASTRING:
1896 case SCF_TYPE_USTRING:
1897 case SCF_TYPE_HOST:
1898 case SCF_TYPE_HOSTNAME:
1899 case SCF_TYPE_NET_ADDR:
1900 case SCF_TYPE_NET_ADDR_V4:
1901 case SCF_TYPE_NET_ADDR_V6:
1902 case SCF_TYPE_URI:
1903 case SCF_TYPE_FMRI: {
1904 for (i = 0; i < prop->pr_numvalues; i++) {
1905 free(prop->pr_vallist[i].pv_str);
1906 }
1907 break;
1908 }
1909 default:
1910 break;
1911 }
1912 free(prop->pr_vallist);
1913 free(prop);
1914 }
1915
1916
1917 scf_simple_app_props_t *
1918 scf_simple_app_props_get(scf_handle_t *hin, const char *inst_fmri)
1919 {
1920 scf_instance_t *inst = NULL;
1921 scf_service_t *svc = NULL;
1922 scf_propertygroup_t *pg = NULL;
1923 scf_property_t *prop = NULL;
1924 scf_simple_app_props_t *ret = NULL;
1925 scf_iter_t *pgiter = NULL, *propiter = NULL;
1926 struct scf_simple_pg *thispg = NULL, *nextpg;
1927 scf_simple_prop_t *thisprop, *nextprop;
1928 scf_handle_t *h = NULL;
1929 int pgiter_ret, propiter_ret;
1930 ssize_t namelen;
1931 char *propname = NULL, *pgname = NULL, *sys_fmri;
1932 uint8_t found;
1933 boolean_t local_h = B_TRUE;
1934
1935 /* If the user passed in a handle, use it. */
1936 if (hin != NULL) {
1937 h = hin;
1938 local_h = B_FALSE;
1939 }
1940
1941 if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
1942 return (NULL);
1943
1944 if (inst_fmri == NULL) {
1945 if ((namelen = scf_myname(h, NULL, 0)) == -1) {
1946 if (local_h)
1947 scf_handle_destroy(h);
1948 return (NULL);
1949 }
1950 if ((sys_fmri = malloc(namelen + 1)) == NULL) {
1951 if (local_h)
1952 scf_handle_destroy(h);
1953 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
1954 return (NULL);
1955 }
1956 if (scf_myname(h, sys_fmri, namelen + 1) == -1) {
1957 if (local_h)
1958 scf_handle_destroy(h);
1959 free(sys_fmri);
1960 return (NULL);
1961 }
1962 } else {
1963 if ((sys_fmri = strdup(inst_fmri)) == NULL) {
1964 if (local_h)
1965 scf_handle_destroy(h);
1966 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
1967 return (NULL);
1968 }
1969 }
1970
1971 namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
1972 assert(namelen > 0);
1973
1974 if ((inst = scf_instance_create(h)) == NULL ||
1975 (svc = scf_service_create(h)) == NULL ||
1976 (pgiter = scf_iter_create(h)) == NULL ||
1977 (propiter = scf_iter_create(h)) == NULL ||
1978 (pg = scf_pg_create(h)) == NULL ||
1979 (prop = scf_property_create(h)) == NULL) {
1980 free(sys_fmri);
1981 goto error2;
1982 }
1983
1984 if (scf_handle_decode_fmri(h, sys_fmri, NULL, svc, inst, NULL, NULL,
1985 SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
1986 free(sys_fmri);
1987 if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
1988 (void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
1989 goto error2;
1990 }
1991
1992 if ((ret = malloc(sizeof (*ret))) == NULL ||
1993 (thispg = malloc(sizeof (*thispg))) == NULL ||
1994 (propname = malloc(namelen)) == NULL ||
1995 (pgname = malloc(namelen)) == NULL) {
1996 free(thispg);
1997 free(ret);
1998 free(sys_fmri);
1999 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2000 goto error2;
2001 }
2002
2003 ret->ap_fmri = sys_fmri;
2004 thispg->pg_name = NULL;
2005 thispg->pg_proplist = NULL;
2006 thispg->pg_next = NULL;
2007 ret->ap_pglist = thispg;
2008
2009 if (scf_iter_service_pgs_typed(pgiter, svc, SCF_GROUP_APPLICATION) !=
2010 0) {
2011 if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2012 (void) scf_set_error(SCF_ERROR_INTERNAL);
2013 goto error1;
2014 }
2015
2016 while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
2017 if (thispg->pg_name != NULL) {
2018 if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
2019 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2020 goto error1;
2021 }
2022 nextpg->pg_name = NULL;
2023 nextpg->pg_next = NULL;
2024 nextpg->pg_proplist = NULL;
2025 thispg->pg_next = nextpg;
2026 thispg = nextpg;
2027 } else {
2028 /* This is the first iteration */
2029 nextpg = thispg;
2030 }
2031
2032 if ((nextpg->pg_name = malloc(namelen)) == NULL) {
2033 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2034 goto error1;
2035 }
2036
2037 if (scf_pg_get_name(pg, nextpg->pg_name, namelen) < 0) {
2038 if (scf_error() == SCF_ERROR_NOT_SET)
2039 (void) scf_set_error(SCF_ERROR_INTERNAL);
2040 goto error1;
2041 }
2042
2043 thisprop = NULL;
2044
2045 scf_iter_reset(propiter);
2046
2047 if (scf_iter_pg_properties(propiter, pg) != 0) {
2048 if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2049 (void) scf_set_error(SCF_ERROR_INTERNAL);
2050 goto error1;
2051 }
2052
2053 while ((propiter_ret = scf_iter_next_property(propiter, prop))
2054 == 1) {
2055 if (scf_property_get_name(prop, propname, namelen) <
2056 0) {
2057 if (scf_error() == SCF_ERROR_NOT_SET)
2058 (void) scf_set_error(
2059 SCF_ERROR_INTERNAL);
2060 goto error1;
2061 }
2062 if (thisprop != NULL) {
2063 if ((nextprop = fill_prop(prop,
2064 nextpg->pg_name, propname, h)) == NULL)
2065 goto error1;
2066 thisprop->pr_next = nextprop;
2067 thisprop = nextprop;
2068 } else {
2069 /* This is the first iteration */
2070 if ((thisprop = fill_prop(prop,
2071 nextpg->pg_name, propname, h)) == NULL)
2072 goto error1;
2073 nextpg->pg_proplist = thisprop;
2074 nextprop = thisprop;
2075 }
2076 nextprop->pr_pg = nextpg;
2077 nextprop->pr_next = NULL;
2078 }
2079
2080 if (propiter_ret == -1) {
2081 if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2082 (void) scf_set_error(SCF_ERROR_INTERNAL);
2083 goto error1;
2084 }
2085 }
2086
2087 if (pgiter_ret == -1) {
2088 if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2089 (void) scf_set_error(SCF_ERROR_INTERNAL);
2090 goto error1;
2091 }
2092
2093 /*
2094 * At this point, we've filled the scf_simple_app_props_t with all the
2095 * properties at the service level. Now we iterate over all the
2096 * properties at the instance level, overwriting any duplicate
2097 * properties, in order to provide service/instance composition.
2098 */
2099
2100 scf_iter_reset(pgiter);
2101 scf_iter_reset(propiter);
2102
2103 if (scf_iter_instance_pgs_typed(pgiter, inst, SCF_GROUP_APPLICATION)
2104 != 0) {
2105 if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2106 (void) scf_set_error(SCF_ERROR_INTERNAL);
2107 goto error1;
2108 }
2109
2110 while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
2111
2112 thispg = ret->ap_pglist;
2113 found = 0;
2114
2115 /*
2116 * Find either the end of the list, so we can append the
2117 * property group, or an existing property group that matches
2118 * it, so we can insert/overwrite its properties.
2119 */
2120
2121 if (scf_pg_get_name(pg, pgname, namelen) < 0) {
2122 if (scf_error() == SCF_ERROR_NOT_SET)
2123 (void) scf_set_error(SCF_ERROR_INTERNAL);
2124 goto error1;
2125 }
2126
2127 while ((thispg != NULL) && (thispg->pg_name != NULL)) {
2128 if (strcmp(thispg->pg_name, pgname) == 0) {
2129 found = 1;
2130 break;
2131 }
2132 if (thispg->pg_next == NULL)
2133 break;
2134
2135 thispg = thispg->pg_next;
2136 }
2137
2138 scf_iter_reset(propiter);
2139
2140 if (scf_iter_pg_properties(propiter, pg) != 0) {
2141 if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2142 (void) scf_set_error(SCF_ERROR_INTERNAL);
2143 goto error1;
2144 }
2145
2146 if (found) {
2147 /*
2148 * insert_app_props inserts or overwrites the
2149 * properties in thispg.
2150 */
2151
2152 if (insert_app_props(propiter, pgname, propname,
2153 thispg, prop, namelen, h) == -1)
2154 goto error1;
2155
2156 } else {
2157 /*
2158 * If the property group wasn't found, we're adding
2159 * a newly allocated property group to the end of the
2160 * list.
2161 */
2162
2163 if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
2164 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2165 goto error1;
2166 }
2167 nextpg->pg_next = NULL;
2168 nextpg->pg_proplist = NULL;
2169 thisprop = NULL;
2170
2171 if ((nextpg->pg_name = strdup(pgname)) == NULL) {
2172 (void) scf_set_error(SCF_ERROR_NO_MEMORY);
2173 free(nextpg);
2174 goto error1;
2175 }
2176
2177 if (thispg->pg_name == NULL) {
2178 free(thispg);
2179 ret->ap_pglist = nextpg;
2180 } else {
2181 thispg->pg_next = nextpg;
2182 }
2183
2184 while ((propiter_ret =
2185 scf_iter_next_property(propiter, prop)) == 1) {
2186 if (scf_property_get_name(prop, propname,
2187 namelen) < 0) {
2188 if (scf_error() == SCF_ERROR_NOT_SET)
2189 (void) scf_set_error(
2190 SCF_ERROR_INTERNAL);
2191 goto error1;
2192 }
2193 if (thisprop != NULL) {
2194 if ((nextprop = fill_prop(prop,
2195 pgname, propname, h)) ==
2196 NULL)
2197 goto error1;
2198 thisprop->pr_next = nextprop;
2199 thisprop = nextprop;
2200 } else {
2201 /* This is the first iteration */
2202 if ((thisprop = fill_prop(prop,
2203 pgname, propname, h)) ==
2204 NULL)
2205 goto error1;
2206 nextpg->pg_proplist = thisprop;
2207 nextprop = thisprop;
2208 }
2209 nextprop->pr_pg = nextpg;
2210 nextprop->pr_next = NULL;
2211 }
2212
2213 if (propiter_ret == -1) {
2214 if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2215 (void) scf_set_error(
2216 SCF_ERROR_INTERNAL);
2217 goto error1;
2218 }
2219 }
2220
2221 }
2222
2223 if (pgiter_ret == -1) {
2224 if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2225 (void) scf_set_error(SCF_ERROR_INTERNAL);
2226 goto error1;
2227 }
2228
2229 if (ret->ap_pglist->pg_name == NULL)
2230 goto error1;
2231
2232 scf_iter_destroy(pgiter);
2233 scf_iter_destroy(propiter);
2234 scf_pg_destroy(pg);
2235 scf_property_destroy(prop);
2236 scf_instance_destroy(inst);
2237 scf_service_destroy(svc);
2238 free(propname);
2239 free(pgname);
2240 if (local_h)
2241 scf_handle_destroy(h);
2242
2243 return (ret);
2244
2245 /*
2246 * Exit point for a successful call. Below this line are exit points
2247 * for failures at various stages during the function.
2248 */
2249
2250 error1:
2251 scf_simple_app_props_free(ret);
2252
2253 error2:
2254 scf_iter_destroy(pgiter);
2255 scf_iter_destroy(propiter);
2256 scf_pg_destroy(pg);
2257 scf_property_destroy(prop);
2258 scf_instance_destroy(inst);
2259 scf_service_destroy(svc);
2260 free(propname);
2261 free(pgname);
2262 if (local_h)
2263 scf_handle_destroy(h);
2264 return (NULL);
2265 }
2266
2267
2268 void
2269 scf_simple_app_props_free(scf_simple_app_props_t *propblock)
2270 {
2271 struct scf_simple_pg *pgthis, *pgnext;
2272 scf_simple_prop_t *propthis, *propnext;
2273
2274 if ((propblock == NULL) || (propblock->ap_pglist == NULL))
2275 return;
2276
2277 for (pgthis = propblock->ap_pglist; pgthis != NULL; pgthis = pgnext) {
2278 pgnext = pgthis->pg_next;
2279
2280 propthis = pgthis->pg_proplist;
2281
2282 while (propthis != NULL) {
2283 propnext = propthis->pr_next;
2284 scf_simple_prop_free(propthis);
2285 propthis = propnext;
2286 }
2287
2288 free(pgthis->pg_name);
2289 free(pgthis);
2290 }
2291
2292 free(propblock->ap_fmri);
2293 free(propblock);
2294 }
2295
2296 const scf_simple_prop_t *
2297 scf_simple_app_props_next(const scf_simple_app_props_t *propblock,
2298 scf_simple_prop_t *last)
2299 {
2300 struct scf_simple_pg *this;
2301
2302 if (propblock == NULL) {
2303 (void) scf_set_error(SCF_ERROR_NOT_SET);
2304 return (NULL);
2305 }
2306
2307 this = propblock->ap_pglist;
2308
2309 /*
2310 * We're looking for the first property in this block if last is
2311 * NULL
2312 */
2313
2314 if (last == NULL) {
2315 /* An empty pglist is legal, it just means no properties */
2316 if (this == NULL) {
2317 (void) scf_set_error(SCF_ERROR_NONE);
2318 return (NULL);
2319 }
2320 /*
2321 * Walk until we find a pg with a property in it, or we run
2322 * out of property groups.
2323 */
2324 while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
2325 this = this->pg_next;
2326
2327 if (this->pg_proplist == NULL) {
2328 (void) scf_set_error(SCF_ERROR_NONE);
2329 return (NULL);
2330 }
2331
2332 return (this->pg_proplist);
2333
2334 }
2335 /*
2336 * If last isn't NULL, then return the next prop in the property group,
2337 * or walk the property groups until we find another property, or
2338 * run out of property groups.
2339 */
2340 if (last->pr_next != NULL)
2341 return (last->pr_next);
2342
2343 if (last->pr_pg->pg_next == NULL) {
2344 (void) scf_set_error(SCF_ERROR_NONE);
2345 return (NULL);
2346 }
2347
2348 this = last->pr_pg->pg_next;
2349
2350 while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
2351 this = this->pg_next;
2352
2353 if (this->pg_proplist == NULL) {
2354 (void) scf_set_error(SCF_ERROR_NONE);
2355 return (NULL);
2356 }
2357
2358 return (this->pg_proplist);
2359 }
2360
2361 const scf_simple_prop_t *
2362 scf_simple_app_props_search(const scf_simple_app_props_t *propblock,
2363 const char *pgname, const char *propname)
2364 {
2365 struct scf_simple_pg *pg;
2366 scf_simple_prop_t *prop;
2367
2368 if ((propblock == NULL) || (propname == NULL)) {
2369 (void) scf_set_error(SCF_ERROR_NOT_SET);
2370 return (NULL);
2371 }
2372
2373 pg = propblock->ap_pglist;
2374
2375 /*
2376 * If pgname is NULL, we're searching the default application
2377 * property group, otherwise we look for the specified group.
2378 */
2379 if (pgname == NULL) {
2380 while ((pg != NULL) &&
2381 (strcmp(SCF_PG_APP_DEFAULT, pg->pg_name) != 0))
2382 pg = pg->pg_next;
2383 } else {
2384 while ((pg != NULL) && (strcmp(pgname, pg->pg_name) != 0))
2385 pg = pg->pg_next;
2386 }
2387
2388 if (pg == NULL) {
2389 (void) scf_set_error(SCF_ERROR_NOT_FOUND);
2390 return (NULL);
2391 }
2392
2393 prop = pg->pg_proplist;
2394
2395 while ((prop != NULL) && (strcmp(propname, prop->pr_propname) != 0))
2396 prop = prop->pr_next;
2397
2398 if (prop == NULL) {
2399 (void) scf_set_error(SCF_ERROR_NOT_FOUND);
2400 return (NULL);
2401 }
2402
2403 return (prop);
2404 }
2405
2406 void
2407 scf_simple_prop_next_reset(scf_simple_prop_t *prop)
2408 {
2409 if (prop == NULL)
2410 return;
2411 prop->pr_iter = 0;
2412 }
2413
2414 ssize_t
2415 scf_simple_prop_numvalues(const scf_simple_prop_t *prop)
2416 {
2417 if (prop == NULL)
2418 return (scf_set_error(SCF_ERROR_NOT_SET));
2419
2420 return (prop->pr_numvalues);
2421 }
2422
2423
2424 scf_type_t
2425 scf_simple_prop_type(const scf_simple_prop_t *prop)
2426 {
2427 if (prop == NULL)
2428 return (scf_set_error(SCF_ERROR_NOT_SET));
2429
2430 return (prop->pr_type);
2431 }
2432
2433
2434 char *
2435 scf_simple_prop_name(const scf_simple_prop_t *prop)
2436 {
2437 if ((prop == NULL) || (prop->pr_propname == NULL)) {
2438 (void) scf_set_error(SCF_ERROR_NOT_SET);
2439 return (NULL);
2440 }
2441
2442 return (prop->pr_propname);
2443 }
2444
2445
2446 char *
2447 scf_simple_prop_pgname(const scf_simple_prop_t *prop)
2448 {
2449 if ((prop == NULL) || (prop->pr_pgname == NULL)) {
2450 (void) scf_set_error(SCF_ERROR_NOT_SET);
2451 return (NULL);
2452 }
2453
2454 return (prop->pr_pgname);
2455 }
2456
2457
2458 static union scf_simple_prop_val *
2459 scf_next_val(scf_simple_prop_t *prop, scf_type_t type)
2460 {
2461 if (prop == NULL) {
2462 (void) scf_set_error(SCF_ERROR_NOT_SET);
2463 return (NULL);
2464 }
2465
2466 switch (prop->pr_type) {
2467 case SCF_TYPE_USTRING:
2468 case SCF_TYPE_HOST:
2469 case SCF_TYPE_HOSTNAME:
2470 case SCF_TYPE_NET_ADDR:
2471 case SCF_TYPE_NET_ADDR_V4:
2472 case SCF_TYPE_NET_ADDR_V6:
2473 case SCF_TYPE_URI:
2474 case SCF_TYPE_FMRI: {
2475 if (type != SCF_TYPE_USTRING) {
2476 (void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
2477 return (NULL);
2478 }
2479 break;
2480 }
2481 default: {
2482 if (type != prop->pr_type) {
2483 (void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
2484 return (NULL);
2485 }
2486 break;
2487 }
2488 }
2489
2490 if (prop->pr_iter >= prop->pr_numvalues) {
2491 (void) scf_set_error(SCF_ERROR_NONE);
2492 return (NULL);
2493 }
2494
2495 return (&prop->pr_vallist[prop->pr_iter++]);
2496 }
2497
2498
2499 uint8_t *
2500 scf_simple_prop_next_boolean(scf_simple_prop_t *prop)
2501 {
2502 union scf_simple_prop_val *ret;
2503
2504 ret = scf_next_val(prop, SCF_TYPE_BOOLEAN);
2505
2506 if (ret == NULL)
2507 return (NULL);
2508
2509 return (&ret->pv_bool);
2510 }
2511
2512
2513 uint64_t *
2514 scf_simple_prop_next_count(scf_simple_prop_t *prop)
2515 {
2516 union scf_simple_prop_val *ret;
2517
2518 ret = scf_next_val(prop, SCF_TYPE_COUNT);
2519
2520 if (ret == NULL)
2521 return (NULL);
2522
2523 return (&ret->pv_uint);
2524 }
2525
2526
2527 int64_t *
2528 scf_simple_prop_next_integer(scf_simple_prop_t *prop)
2529 {
2530 union scf_simple_prop_val *ret;
2531
2532 ret = scf_next_val(prop, SCF_TYPE_INTEGER);
2533
2534 if (ret == NULL)
2535 return (NULL);
2536
2537 return (&ret->pv_int);
2538 }
2539
2540 int64_t *
2541 scf_simple_prop_next_time(scf_simple_prop_t *prop, int32_t *nsec)
2542 {
2543 union scf_simple_prop_val *ret;
2544
2545 ret = scf_next_val(prop, SCF_TYPE_TIME);
2546
2547 if (ret == NULL)
2548 return (NULL);
2549
2550 if (nsec != NULL)
2551 *nsec = ret->pv_time.t_nsec;
2552
2553 return (&ret->pv_time.t_sec);
2554 }
2555
2556 char *
2557 scf_simple_prop_next_astring(scf_simple_prop_t *prop)
2558 {
2559 union scf_simple_prop_val *ret;
2560
2561 ret = scf_next_val(prop, SCF_TYPE_ASTRING);
2562
2563 if (ret == NULL)
2564 return (NULL);
2565
2566 return (ret->pv_str);
2567 }
2568
2569 char *
2570 scf_simple_prop_next_ustring(scf_simple_prop_t *prop)
2571 {
2572 union scf_simple_prop_val *ret;
2573
2574 ret = scf_next_val(prop, SCF_TYPE_USTRING);
2575
2576 if (ret == NULL)
2577 return (NULL);
2578
2579 return (ret->pv_str);
2580 }
2581
2582 void *
2583 scf_simple_prop_next_opaque(scf_simple_prop_t *prop, size_t *length)
2584 {
2585 union scf_simple_prop_val *ret;
2586
2587 ret = scf_next_val(prop, SCF_TYPE_OPAQUE);
2588
2589 if (ret == NULL) {
2590 *length = 0;
2591 return (NULL);
2592 }
2593
2594 *length = ret->pv_opaque.o_size;
2595 return (ret->pv_opaque.o_value);
2596 }
2597
2598 /*
2599 * Generate a filename based on the fmri and the given name and return
2600 * it in the buffer of MAXPATHLEN provided by the caller.
2601 * If temp_filename is non-zero, also generate a temporary, unique filename
2602 * and return it in the temp buffer of MAXPATHLEN provided by the caller.
2603 * The path to the generated pathname is also created.
2604 * Given fmri should begin with a scheme such as "svc:".
2605 * Returns
2606 * 0 on success
2607 * -1 if filename would exceed MAXPATHLEN or
2608 * -2 if unable to create directory to filename path
2609 */
2610 int
2611 gen_filenms_from_fmri(const char *fmri, const char *name, char *filename,
2612 char *temp_filename)
2613 {
2614 int len;
2615
2616 len = strlen(SMF_SPEEDY_FILES_PATH);
2617 len += strlen(fmri);
2618 len += 2; /* for slash and null */
2619 len += strlen(name);
2620 len += 6; /* For X's needed for mkstemp */
2621
2622 if (len > MAXPATHLEN)
2623 return (-1);
2624
2625 /* Construct directory name first - speedy path ends in slash */
2626 (void) strcpy(filename, SMF_SPEEDY_FILES_PATH);
2627 (void) strcat(filename, fmri);
2628 if (mkdirp(filename, 0755) == -1) {
2629 /* errno is set */
2630 if (errno != EEXIST)
2631 return (-2);
2632 }
2633
2634 (void) strcat(filename, "/");
2635 (void) strcat(filename, name);
2636
2637 if (temp_filename) {
2638 (void) strcpy(temp_filename, filename);
2639 (void) strcat(temp_filename, "XXXXXX");
2640 }
2641
2642 return (0);
2643 }
2644
2645 scf_type_t
2646 scf_true_base_type(scf_type_t type)
2647 {
2648 scf_type_t base = type;
2649
2650 do {
2651 type = base;
2652 (void) scf_type_base_type(type, &base);
2653 } while (base != type);
2654
2655 return (base);
2656 }
2657
2658 /*
2659 * Convenience routine which frees all strings and opaque data
2660 * allocated by scf_read_propvec.
2661 *
2662 * Like free(3C), this function preserves the value of errno.
2663 */
2664 void
2665 scf_clean_propvec(scf_propvec_t *propvec)
2666 {
2667 int saved_errno = errno;
2668 scf_propvec_t *prop;
2669
2670 for (prop = propvec; prop->pv_prop != NULL; prop++) {
2671 assert(prop->pv_type != SCF_TYPE_INVALID);
2672 if (prop->pv_type == SCF_TYPE_OPAQUE) {
2673 scf_opaque_t *o = prop->pv_ptr;
2674
2675 if (o->so_addr != NULL)
2676 free(o->so_addr);
2677 } else if (scf_true_base_type(prop->pv_type) ==
2678 SCF_TYPE_ASTRING) {
2679 if (*(char **)prop->pv_ptr != NULL)
2680 free(*(char **)prop->pv_ptr);
2681 }
2682 }
2683
2684 errno = saved_errno;
2685 }
2686
2687 static int
2688 count_props(scf_propvec_t *props)
2689 {
2690 int count = 0;
2691
2692 for (; props->pv_prop != NULL; props++)
2693 count++;
2694 return (count);
2695 }
2696
2697 /*
2698 * Reads a vector of properties from the specified fmri/property group.
2699 * If 'running' is true, reads from the running snapshot instead of the
2700 * editing snapshot.
2701 *
2702 * For string types, a buffer is allocated using malloc(3C) to hold the
2703 * zero-terminated string, a pointer to which is stored in the
2704 * caller-provided char **. It is the caller's responsbility to free
2705 * this string. To simplify error handling, unread strings are
2706 * initialized to NULL.
2707 *
2708 * For opaque types, a buffer is allocated using malloc(3C) to hold the
2709 * opaque data. A pointer to this buffer and its size are stored in
2710 * the caller-provided scf_opaque_t. It is the caller's responsibility
2711 * to free this buffer. To simplify error handling, the address fields
2712 * for unread opaque data are initialized to NULL.
2713 *
2714 * All other data is stored directly in caller-provided variables or
2715 * structures.
2716 *
2717 * If this function fails to read a specific property, *badprop is set
2718 * to point at that property's entry in the properties array.
2719 *
2720 * On all failures, all memory allocated by this function is freed.
2721 */
2722 int
2723 scf_read_propvec(const char *fmri, const char *pgname, boolean_t running,
2724 scf_propvec_t *properties, scf_propvec_t **badprop)
2725 {
2726 scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
2727 scf_service_t *s = scf_service_create(h);
2728 scf_instance_t *i = scf_instance_create(h);
2729 scf_snapshot_t *snap = running ? scf_snapshot_create(h) : NULL;
2730 scf_propertygroup_t *pg = scf_pg_create(h);
2731 scf_property_t *p = scf_property_create(h);
2732 scf_value_t *v = scf_value_create(h);
2733 boolean_t instance = B_TRUE;
2734 scf_propvec_t *prop;
2735 int error = 0;
2736
2737 if (h == NULL || s == NULL || i == NULL || (running && snap == NULL) ||
2738 pg == NULL || p == NULL || v == NULL)
2739 goto scferror;
2740
2741 if (scf_handle_decode_fmri(h, fmri, NULL, s, i, NULL, NULL, 0) == -1)
2742 goto scferror;
2743
2744 if (scf_instance_to_fmri(i, NULL, 0) == -1) {
2745 if (scf_error() != SCF_ERROR_NOT_SET)
2746 goto scferror;
2747 instance = B_FALSE;
2748 }
2749
2750 if (running) {
2751 if (!instance) {
2752 error = SCF_ERROR_TYPE_MISMATCH;
2753 goto out;
2754 }
2755
2756 if (scf_instance_get_snapshot(i, "running", snap) !=
2757 SCF_SUCCESS)
2758 goto scferror;
2759 }
2760
2761 if ((instance ? scf_instance_get_pg_composed(i, snap, pgname, pg) :
2762 scf_service_get_pg(s, pgname, pg)) == -1)
2763 goto scferror;
2764
2765 for (prop = properties; prop->pv_prop != NULL; prop++) {
2766 if (prop->pv_type == SCF_TYPE_OPAQUE)
2767 ((scf_opaque_t *)prop->pv_ptr)->so_addr = NULL;
2768 else if (scf_true_base_type(prop->pv_type) == SCF_TYPE_ASTRING)
2769 *((char **)prop->pv_ptr) = NULL;
2770 }
2771
2772 for (prop = properties; prop->pv_prop != NULL; prop++) {
2773 int ret = 0;
2774
2775 if (scf_pg_get_property(pg, prop->pv_prop, p) == -1 ||
2776 scf_property_get_value(p, v) == -1) {
2777 *badprop = prop;
2778 goto scferror;
2779 }
2780 switch (prop->pv_type) {
2781 case SCF_TYPE_BOOLEAN: {
2782 uint8_t b;
2783
2784 ret = scf_value_get_boolean(v, &b);
2785 if (ret == -1)
2786 break;
2787 if (prop->pv_aux != 0) {
2788 uint64_t *bits = prop->pv_ptr;
2789 *bits = b ? (*bits | prop->pv_aux) :
2790 (*bits & ~prop->pv_aux);
2791 } else {
2792 boolean_t *bool = prop->pv_ptr;
2793 *bool = b ? B_TRUE : B_FALSE;
2794 }
2795 break;
2796 }
2797 case SCF_TYPE_COUNT:
2798 ret = scf_value_get_count(v, prop->pv_ptr);
2799 break;
2800 case SCF_TYPE_INTEGER:
2801 ret = scf_value_get_integer(v, prop->pv_ptr);
2802 break;
2803 case SCF_TYPE_TIME: {
2804 scf_time_t *time = prop->pv_ptr;
2805
2806 ret = scf_value_get_time(v, &time->t_seconds,
2807 &time->t_ns);
2808 break;
2809 }
2810 case SCF_TYPE_OPAQUE: {
2811 scf_opaque_t *opaque = prop->pv_ptr;
2812 ssize_t size = scf_value_get_opaque(v, NULL, 0);
2813
2814 if (size == -1) {
2815 *badprop = prop;
2816 goto scferror;
2817 }
2818 if ((opaque->so_addr = malloc(size)) == NULL) {
2819 error = SCF_ERROR_NO_MEMORY;
2820 goto out;
2821 }
2822 opaque->so_size = size;
2823 ret = scf_value_get_opaque(v, opaque->so_addr, size);
2824 break;
2825 }
2826 default: {
2827 char *s;
2828 ssize_t size;
2829
2830 assert(scf_true_base_type(prop->pv_type) ==
2831 SCF_TYPE_ASTRING);
2832
2833 size = scf_value_get_astring(v, NULL, 0);
2834 if (size == -1) {
2835 *badprop = prop;
2836 goto scferror;
2837 }
2838 if ((s = malloc(++size)) == NULL) {
2839 error = SCF_ERROR_NO_MEMORY;
2840 goto out;
2841 }
2842 ret = scf_value_get_astring(v, s, size);
2843 *(char **)prop->pv_ptr = s;
2844 }
2845
2846 if (ret == -1) {
2847 *badprop = prop;
2848 goto scferror;
2849 }
2850
2851 }
2852 }
2853
2854 goto out;
2855
2856 scferror:
2857 error = scf_error();
2858 scf_clean_propvec(properties);
2859
2860 out:
2861 scf_value_destroy(v);
2862 scf_property_destroy(p);
2863 scf_pg_destroy(pg);
2864 scf_snapshot_destroy(snap);
2865 scf_instance_destroy(i);
2866 scf_service_destroy(s);
2867 scf_handle_destroy(h);
2868
2869 if (error != 0) {
2870 (void) scf_set_error(error);
2871 return (SCF_FAILED);
2872 }
2873
2874 return (SCF_SUCCESS);
2875 }
2876
2877 /*
2878 * Writes a vector of properties to the specified fmri/property group.
2879 *
2880 * If this function fails to write a specific property, *badprop is set
2881 * to point at that property's entry in the properties array.
2882 *
2883 * One significant difference between this function and the
2884 * scf_read_propvec function is that for string types, pv_ptr is a
2885 * char *, not a char **. This means that you can't write a propvec
2886 * you just read, but makes other uses (hopefully the majority) simpler.
2887 */
2888 int
2889 scf_write_propvec(const char *fmri, const char *pgname,
2890 scf_propvec_t *properties, scf_propvec_t **badprop)
2891 {
2892 scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
2893 scf_service_t *s = scf_service_create(h);
2894 scf_instance_t *inst = scf_instance_create(h);
2895 scf_snapshot_t *snap = scf_snapshot_create(h);
2896 scf_propertygroup_t *pg = scf_pg_create(h);
2897 scf_property_t *p = scf_property_create(h);
2898 scf_transaction_t *tx = scf_transaction_create(h);
2899 scf_value_t **v = NULL;
2900 scf_transaction_entry_t **e = NULL;
2901 boolean_t instance = B_TRUE;
2902 int i, n;
2903 scf_propvec_t *prop;
2904 int error = 0, ret;
2905
2906 n = count_props(properties);
2907 v = calloc(n, sizeof (scf_value_t *));
2908 e = calloc(n, sizeof (scf_transaction_entry_t *));
2909
2910 if (v == NULL || e == NULL) {
2911 error = SCF_ERROR_NO_MEMORY;
2912 goto out;
2913 }
2914
2915 if (h == NULL || s == NULL || inst == NULL || pg == NULL || p == NULL ||
2916 tx == NULL)
2917 goto scferror;
2918
2919 for (i = 0; i < n; i++) {
2920 v[i] = scf_value_create(h);
2921 e[i] = scf_entry_create(h);
2922 if (v[i] == NULL || e[i] == NULL)
2923 goto scferror;
2924 }
2925
2926 if (scf_handle_decode_fmri(h, fmri, NULL, s, inst, NULL, NULL, 0)
2927 != SCF_SUCCESS)
2928 goto scferror;
2929
2930 if (scf_instance_to_fmri(inst, NULL, 0) == -1) {
2931 if (scf_error() != SCF_ERROR_NOT_SET)
2932 goto scferror;
2933 instance = B_FALSE;
2934 }
2935
2936 if ((instance ? scf_instance_get_pg(inst, pgname, pg) :
2937 scf_service_get_pg(s, pgname, pg)) == -1)
2938 goto scferror;
2939
2940 top:
2941 if (scf_transaction_start(tx, pg) == -1)
2942 goto scferror;
2943
2944 for (prop = properties, i = 0; prop->pv_prop != NULL; prop++, i++) {
2945 ret = scf_transaction_property_change(tx, e[i], prop->pv_prop,
2946 prop->pv_type);
2947 if (ret == -1 && scf_error() == SCF_ERROR_NOT_FOUND)
2948 ret = scf_transaction_property_new(tx, e[i],
2949 prop->pv_prop, prop->pv_type);
2950
2951 if (ret == -1) {
2952 *badprop = prop;
2953 goto scferror;
2954 }
2955
2956 switch (prop->pv_type) {
2957 case SCF_TYPE_BOOLEAN: {
2958 boolean_t b = (prop->pv_aux != 0) ?
2959 (*(uint64_t *)prop->pv_ptr & prop->pv_aux) != 0 :
2960 *(boolean_t *)prop->pv_ptr;
2961
2962 scf_value_set_boolean(v[i], b ? 1 : 0);
2963 break;
2964 }
2965 case SCF_TYPE_COUNT:
2966 scf_value_set_count(v[i], *(uint64_t *)prop->pv_ptr);
2967 break;
2968 case SCF_TYPE_INTEGER:
2969 scf_value_set_integer(v[i], *(int64_t *)prop->pv_ptr);
2970 break;
2971 case SCF_TYPE_TIME: {
2972 scf_time_t *time = prop->pv_ptr;
2973
2974 ret = scf_value_set_time(v[i], time->t_seconds,
2975 time->t_ns);
2976 break;
2977 }
2978 case SCF_TYPE_OPAQUE: {
2979 scf_opaque_t *opaque = prop->pv_ptr;
2980
2981 ret = scf_value_set_opaque(v[i], opaque->so_addr,
2982 opaque->so_size);
2983 break;
2984 }
2985 case SCF_TYPE_ASTRING:
2986 ret = scf_value_set_astring(v[i],
2987 (const char *)prop->pv_ptr);
2988 break;
2989 default:
2990 ret = scf_value_set_from_string(v[i], prop->pv_type,
2991 (const char *)prop->pv_ptr);
2992 }
2993
2994 if (ret == -1 || scf_entry_add_value(e[i], v[i]) == -1) {
2995 *badprop = prop;
2996 goto scferror;
2997 }
2998 }
2999
3000 ret = scf_transaction_commit(tx);
3001 if (ret == 1)
3002 goto out;
3003
3004 if (ret == 0 && scf_pg_update(pg) != -1) {
3005 scf_transaction_reset(tx);
3006 goto top;
3007 }
3008
3009 scferror:
3010 error = scf_error();
3011
3012 out:
3013 if (v != NULL) {
3014 for (i = 0; i < n; i++)
3015 scf_value_destroy(v[i]);
3016 free(v);
3017 }
3018
3019 if (e != NULL) {
3020 for (i = 0; i < n; i++)
3021 scf_entry_destroy(e[i]);
3022 free(e);
3023 }
3024
3025 scf_transaction_destroy(tx);
3026 scf_property_destroy(p);
3027 scf_pg_destroy(pg);
3028 scf_snapshot_destroy(snap);
3029 scf_instance_destroy(inst);
3030 scf_service_destroy(s);
3031 scf_handle_destroy(h);
3032
3033 if (error != 0) {
3034 (void) scf_set_error(error);
3035 return (SCF_FAILED);
3036 }
3037
3038 return (SCF_SUCCESS);
3039 }
3040
3041 /*
3042 * Returns
3043 * 0 - success
3044 * ECONNABORTED - repository connection broken
3045 * ECANCELED - inst was deleted
3046 * EPERM
3047 * EACCES
3048 * EROFS
3049 * ENOMEM
3050 */
3051 int
3052 scf_instance_delete_prop(scf_instance_t *inst, const char *pgname,
3053 const char *pname)
3054 {
3055 scf_handle_t *h;
3056 scf_propertygroup_t *pg;
3057 scf_transaction_t *tx;
3058 scf_transaction_entry_t *e;
3059 int error = 0, ret = 1, r;
3060
3061 h = scf_instance_handle(inst);
3062
3063 if ((pg = scf_pg_create(h)) == NULL) {
3064 return (ENOMEM);
3065 }
3066
3067 if (scf_instance_get_pg(inst, pgname, pg) != 0) {
3068 error = scf_error();
3069 scf_pg_destroy(pg);
3070 switch (error) {
3071 case SCF_ERROR_NOT_FOUND:
3072 return (SCF_SUCCESS);
3073
3074 case SCF_ERROR_DELETED:
3075 return (ECANCELED);
3076
3077 case SCF_ERROR_CONNECTION_BROKEN:
3078 default:
3079 return (ECONNABORTED);
3080
3081 case SCF_ERROR_NOT_SET:
3082 bad_error("scf_instance_get_pg", scf_error());
3083 }
3084 }
3085
3086 tx = scf_transaction_create(h);
3087 e = scf_entry_create(h);
3088 if (tx == NULL || e == NULL) {
3089 ret = ENOMEM;
3090 goto out;
3091 }
3092
3093 for (;;) {
3094 if (scf_transaction_start(tx, pg) != 0) {
3095 goto scferror;
3096 }
3097
3098 if (scf_transaction_property_delete(tx, e, pname) != 0) {
3099 goto scferror;
3100 }
3101
3102 if ((r = scf_transaction_commit(tx)) == 1) {
3103 ret = 0;
3104 goto out;
3105 }
3106
3107 if (r == -1) {
3108 goto scferror;
3109 }
3110
3111 scf_transaction_reset(tx);
3112 if (scf_pg_update(pg) == -1) {
3113 goto scferror;
3114 }
3115 }
3116
3117 scferror:
3118 switch (scf_error()) {
3119 case SCF_ERROR_DELETED:
3120 case SCF_ERROR_NOT_FOUND:
3121 ret = 0;
3122 break;
3123
3124 case SCF_ERROR_PERMISSION_DENIED:
3125 ret = EPERM;
3126 break;
3127
3128 case SCF_ERROR_BACKEND_ACCESS:
3129 ret = EACCES;
3130 break;
3131
3132 case SCF_ERROR_BACKEND_READONLY:
3133 ret = EROFS;
3134 break;
3135
3136 case SCF_ERROR_CONNECTION_BROKEN:
3137 default:
3138 ret = ECONNABORTED;
3139 break;
3140
3141 case SCF_ERROR_HANDLE_MISMATCH:
3142 case SCF_ERROR_INVALID_ARGUMENT:
3143 case SCF_ERROR_NOT_BOUND:
3144 case SCF_ERROR_NOT_SET:
3145 bad_error("scf_instance_delete_prop", scf_error());
3146 }
3147
3148 out:
3149 scf_transaction_destroy(tx);
3150 scf_entry_destroy(e);
3151 scf_pg_destroy(pg);
3152
3153 return (ret);
3154 }
3155
3156 /*
3157 * Check the "application/auto_enable" property for the passed FMRI.
3158 * scf_simple_prop_get() should find the property on an instance
3159 * or on the service FMRI. The routine returns:
3160 * -1: inconclusive (likely no such property or FMRI)
3161 * 0: auto_enable is false
3162 * 1: auto_enable is true
3163 */
3164 static int
3165 is_auto_enabled(char *fmri)
3166 {
3167 scf_simple_prop_t *prop;
3168 int retval = -1;
3169 uint8_t *ret;
3170
3171 prop = scf_simple_prop_get(NULL, fmri, SCF_GROUP_APPLICATION,
3172 "auto_enable");
3173 if (!prop)
3174 return (retval);
3175 ret = scf_simple_prop_next_boolean(prop);
3176 retval = (*ret != 0);
3177 scf_simple_prop_free(prop);
3178 return (retval);
3179 }
3180
3181 /*
3182 * Check an array of services and enable any that don't have the
3183 * "application/auto_enable" property set to "false", which is
3184 * the interface to turn off this behaviour (see PSARC 2004/739).
3185 */
3186 void
3187 _check_services(char **svcs)
3188 {
3189 char *s;
3190
3191 for (; *svcs; svcs++) {
3192 if (is_auto_enabled(*svcs) == 0)
3193 continue;
3194 if ((s = smf_get_state(*svcs)) != NULL) {
3195 if (strcmp(SCF_STATE_STRING_DISABLED, s) == 0)
3196 (void) smf_enable_instance(*svcs,
3197 SMF_TEMPORARY);
3198 free(s);
3199 }
3200 }
3201 }
3202
3203 /*ARGSUSED*/
3204 static int
3205 str_compare(const char *s1, const char *s2, size_t n)
3206 {
3207 return (strcmp(s1, s2));
3208 }
3209
3210 static int
3211 str_n_compare(const char *s1, const char *s2, size_t n)
3212 {
3213 return (strncmp(s1, s2, n));
3214 }
3215
3216 int32_t
3217 state_from_string(const char *state, size_t l)
3218 {
3219 int (*str_cmp)(const char *, const char *, size_t);
3220
3221 if (l == 0)
3222 str_cmp = str_compare;
3223 else
3224 str_cmp = str_n_compare;
3225
3226 if (str_cmp(SCF_STATE_STRING_UNINIT, state, l) == 0)
3227 return (SCF_STATE_UNINIT);
3228 else if (str_cmp(SCF_STATE_STRING_MAINT, state, l) == 0)
3229 return (SCF_STATE_MAINT);
3230 else if (str_cmp(SCF_STATE_STRING_OFFLINE, state, l) == 0)
3231 return (SCF_STATE_OFFLINE);
3232 else if (str_cmp(SCF_STATE_STRING_DISABLED, state, l) == 0)
3233 return (SCF_STATE_DISABLED);
3234 else if (str_cmp(SCF_STATE_STRING_ONLINE, state, l) == 0)
3235 return (SCF_STATE_ONLINE);
3236 else if (str_cmp(SCF_STATE_STRING_DEGRADED, state, l) == 0)
3237 return (SCF_STATE_DEGRADED);
3238 else if (str_cmp("all", state, l) == 0)
3239 return (SCF_STATE_ALL);
3240 else
3241 return (-1);
3242 }
3243
3244 /*
3245 * int32_t smf_state_from_string()
3246 * return the value of the macro SCF_STATE_* for the corresponding state
3247 * it returns SCF_STATE_ALL if "all" is passed. -1 if the string passed doesn't
3248 * correspond to any valid state.
3249 */
3250 int32_t
3251 smf_state_from_string(const char *state)
3252 {
3253 return (state_from_string(state, 0));
3254 }
3255
3256 /*
3257 * smf_state_to_string()
3258 * Takes an int32_t representing an SMF state and returns
3259 * the corresponding string. The string is read only and need not to be
3260 * freed.
3261 * returns NULL on invalid input.
3262 */
3263 const char *
3264 smf_state_to_string(int32_t s)
3265 {
3266 switch (s) {
3267 case SCF_STATE_UNINIT:
3268 return (SCF_STATE_STRING_UNINIT);
3269 case SCF_STATE_MAINT:
3270 return (SCF_STATE_STRING_MAINT);
3271 case SCF_STATE_OFFLINE:
3272 return (SCF_STATE_STRING_OFFLINE);
3273 case SCF_STATE_DISABLED:
3274 return (SCF_STATE_STRING_DISABLED);
3275 case SCF_STATE_ONLINE:
3276 return (SCF_STATE_STRING_ONLINE);
3277 case SCF_STATE_DEGRADED:
3278 return (SCF_STATE_STRING_DEGRADED);
3279 case SCF_STATE_ALL:
3280 return ("all");
3281 default:
3282 return (NULL);
3283 }
3284 }