1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* synchronous svcadm logic
*/
#include <locale.h>
#include <libintl.h>
#include <libscf.h>
#include <libscf_priv.h>
#include <libuutil.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <sys/stat.h>
/*
* Definitions from svcadm.c.
*/
extern scf_handle_t *h;
extern ssize_t max_scf_fmri_sz;
extern void do_scfdie(int) __NORETURN;
extern int inst_get_state(scf_instance_t *, char *, const char *,
scf_propertygroup_t **);
extern ssize_t get_astring_prop(const scf_propertygroup_t *, const char *,
scf_property_t *, scf_value_t *, char *, size_t);
extern int get_bool_prop(scf_propertygroup_t *, const char *, uint8_t *);
#define scfdie() do_scfdie(__LINE__)
int has_potential(scf_instance_t *, int);
/*
* Determines if the specified instance is enabled, composing the
* general and general_ovr property groups. For simplicity, we map
* most errors to "not enabled".
*/
int
is_enabled(scf_instance_t *inst)
{
scf_propertygroup_t *pg;
uint8_t bp;
if ((pg = scf_pg_create(h)) == NULL)
scfdie();
if (scf_instance_get_pg(inst, SCF_PG_GENERAL_OVR, pg) == 0 &&
get_bool_prop(pg, SCF_PROPERTY_ENABLED, &bp) == 0) {
scf_pg_destroy(pg);
return (bp);
}
if (scf_instance_get_pg(inst, SCF_PG_GENERAL, pg) == 0 &&
get_bool_prop(pg, SCF_PROPERTY_ENABLED, &bp) == 0) {
scf_pg_destroy(pg);
return (bp);
}
scf_pg_destroy(pg);
return (B_FALSE);
}
/*
* Reads an astring property from a property group. If the named
* property doesn't exist, returns NULL. The result of a successful
* call should be freed.
*/
static char *
read_astring_prop(scf_propertygroup_t *pg, scf_value_t *val,
scf_property_t *prop, const char *name)
{
char *value;
size_t value_sz;
if (scf_pg_get_property(pg, name, prop) == -1) {
switch (scf_error()) {
case SCF_ERROR_NOT_FOUND:
case SCF_ERROR_DELETED:
return (NULL);
default:
scfdie();
}
}
if (scf_property_get_value(prop, val) != 0) {
switch (scf_error()) {
case SCF_ERROR_DELETED:
case SCF_ERROR_NOT_FOUND:
case SCF_ERROR_CONSTRAINT_VIOLATED:
return (NULL);
default:
scfdie();
}
}
value_sz = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
if ((value = malloc(value_sz)) == NULL)
scfdie();
if (scf_value_get_astring(val, value, value_sz) <= 0) {
free(value);
return (NULL);
}
return (value);
}
/*
* Creates and returns an scf_iter for the values of the named
* multi-value property. Returns NULL on failure.
*/
static scf_iter_t *
prop_walk_init(scf_propertygroup_t *pg, const char *name)
{
scf_iter_t *iter;
scf_property_t *prop;
if ((iter = scf_iter_create(h)) == NULL ||
(prop = scf_property_create(h)) == NULL)
scfdie();
if (scf_pg_get_property(pg, name, prop) != 0) {
switch (scf_error()) {
case SCF_ERROR_NOT_FOUND:
case SCF_ERROR_DELETED:
goto error;
default:
scfdie();
}
}
if (scf_iter_property_values(iter, prop) != 0) {
if (scf_error() != SCF_ERROR_DELETED)
scfdie();
goto error;
}
scf_property_destroy(prop);
return (iter);
error:
scf_property_destroy(prop);
scf_iter_destroy(iter);
return (NULL);
}
/*
* Reads the next value from the multi-value property using the
* scf_iter obtained by prop_walk_init, and places it in the buffer
* pointed to by fmri. Returns -1 on failure, 0 when done, and non-0
* when returning a value.
*/
static int
prop_walk_step(scf_iter_t *iter, char *fmri, size_t len)
{
int r;
scf_value_t *val;
if ((val = scf_value_create(h)) == NULL)
scfdie();
r = scf_iter_next_value(iter, val);
if (r == 0)
goto out;
if (r == -1) {
if (scf_error() != SCF_ERROR_DELETED)
scfdie();
goto out;
}
if (scf_value_get_astring(val, fmri, len) <= 0) {
r = -1;
goto out;
}
out:
scf_value_destroy(val);
return (r);
}
/*
* Determines if a file dependency is satisfied, taking into account
* whether it is an exclusion dependency or not. If we can't access
* the file, we err on the side of caution and assume the dependency
* isn't satisfied.
*/
static int
file_has_potential(char *fmri, int exclude)
{
const char *path;
struct stat st;
int good = exclude ? B_FALSE : B_TRUE;
if (scf_parse_file_fmri(fmri, NULL, &path) != 0)
return (good);
if (stat(path, &st) == 0)
return (good);
if (errno == EACCES) {
uu_warn(gettext("Unable to access \"%s\".\n"), path);
return (B_FALSE);
}
return (!good);
}
/*
* Determines if a dependency on a service instance is satisfiable.
* Returns 0 if not, 1 if it is, or 2 if it is an optional or exclude
* dependency and the service only "weakly" satisfies (i.e. is disabled
* or is in maintenance state).
*/
static int
inst_has_potential(scf_instance_t *inst, int enabled, int optional, int exclude)
{
char state[MAX_SCF_STATE_STRING_SZ];
if (!enabled)
return ((optional || exclude) ? 2 : 0);
/*
* Normally we would return a positive value on failure;
* relying on startd to place the service in maintenance. But
* if we can't read a service's state, we have to assume it is
* out to lunch.
*/
if (inst_get_state(inst, state, NULL, NULL) != 0)
return (0);
/*
* Optional dependencies which are offline always have a possibility of
* coming online.
*/
if (optional && strcmp(state, SCF_STATE_STRING_OFFLINE) == 0)
return (2);
if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
/*
* Enabled services in maintenance state satisfy
* optional-all dependencies.
*/
return ((optional || exclude) ? 2 : 0);
}
/*
* We're enabled and not in maintenance.
*/
if (exclude)
return (0);
if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0 ||
strcmp(state, SCF_STATE_STRING_DEGRADED) == 0)
return (1);
return (has_potential(inst, B_FALSE));
}
/*
* Determines if a dependency on an fmri is satisfiable, handling the
* separate cases for file, service, and instance fmris. Returns false
* if not, or true if it is. Takes into account if the dependency is
* an optional or exclusive one.
*/
static int
fmri_has_potential(char *fmri, int isfile, int optional, int exclude,
int restarter)
{
scf_instance_t *inst;
scf_service_t *svc;
scf_iter_t *iter;
int good = exclude ? B_FALSE : B_TRUE;
int enabled;
int r, result;
int optbad;
assert(!optional || !exclude);
if (isfile)
return (file_has_potential(fmri, exclude));
if ((inst = scf_instance_create(h)) == NULL ||
(svc = scf_service_create(h)) == NULL ||
(iter = scf_iter_create(h)) == NULL)
scfdie();
if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
SCF_DECODE_FMRI_EXACT) == 0) {
enabled = is_enabled(inst);
result =
(inst_has_potential(inst, enabled, optional, exclude) != 0);
goto out;
}
if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
SCF_DECODE_FMRI_EXACT) != 0) {
/*
* If we are checking a restarter dependency, a bad
* or nonexistent service will never be noticed.
*/
result = restarter ? B_FALSE : good;
goto out;
}
if (scf_iter_service_instances(iter, svc) != 0) {
if (scf_error() != SCF_ERROR_DELETED)
scfdie();
result = good;
goto out;
}
optbad = 0;
for (;;) {
r = scf_iter_next_instance(iter, inst);
if (r == 0) {
result = exclude || (optional && !optbad);
goto out;
}
if (r == -1) {
if (scf_error() != SCF_ERROR_DELETED)
scfdie();
result = good;
goto out;
}
enabled = is_enabled(inst);
r = inst_has_potential(inst, enabled, optional, exclude);
/*
* Exclusion dependencies over services map to
* require-none for its instances.
*/
if (exclude)
r = (r == 0);
if (r == 1) {
/*
* Remember, if this is an exclusion dependency
* (which means we are here because there
* exists an instance which wasn't satisfiable
* in that regard), good means bad.
*/
result = good;
goto out;
}
if (optional && r == 0)
optbad = 1;
}
out:
scf_instance_destroy(inst);
scf_service_destroy(svc);
scf_iter_destroy(iter);
return (result);
}
static int
eval_require_any(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
{
int r, empty = B_TRUE;
for (;;) {
/*
* For reasons unknown, an empty require_any dependency
* group is considered by startd to be satisfied.
* This insanity fortunately doesn't extend to
* dependencies on services with no instances.
*/
if ((r = prop_walk_step(iter, value, value_sz)) <= 0)
return ((r == 0 && empty) ? B_TRUE : r);
if (fmri_has_potential(value, isfile, B_FALSE, B_FALSE,
B_FALSE))
return (1);
empty = B_FALSE;
}
}
static int
eval_all(scf_iter_t *iter, char *value, size_t value_sz,
int isfile, int optional, int exclude)
{
int r;
for (;;) {
if ((r = prop_walk_step(iter, value, value_sz)) <= 0)
return ((r == 0) ? 1 : r);
if (!fmri_has_potential(value, isfile, optional, exclude,
B_FALSE))
return (0);
}
}
static int
eval_require_all(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
{
return (eval_all(iter, value, value_sz, isfile, B_FALSE, B_FALSE));
}
static int
eval_optional_all(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
{
return (eval_all(iter, value, value_sz, isfile, B_TRUE, B_FALSE));
}
static int
eval_exclude_all(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
{
return (eval_all(iter, value, value_sz, isfile, B_FALSE, B_TRUE));
}
/*
* Examines the state and health of an instance's restarter and
* dependencies, and determines the impact of both on the instance's
* ability to be brought on line. A true return value indicates that
* instance appears to be a likely candidate for the online club.
* False indicates that there is no hope for the instance.
*/
int
has_potential(scf_instance_t *inst, int restarter_only)
{
scf_snapshot_t *snap;
scf_iter_t *iter, *viter = NULL;
scf_propertygroup_t *pg;
scf_property_t *prop;
scf_value_t *val;
char *type = NULL, *grouping = NULL;
char *value;
size_t value_sz;
int result = B_TRUE, r;
int isfile;
value_sz = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
if ((iter = scf_iter_create(h)) == NULL ||
(snap = scf_snapshot_create(h)) == NULL ||
(pg = scf_pg_create(h)) == NULL ||
(val = scf_value_create(h)) == NULL ||
(prop = scf_property_create(h)) == NULL ||
(value = malloc(value_sz)) == NULL)
scfdie();
/*
* First we check our restarter as an implicit dependency.
*/
if (scf_instance_get_pg_composed(inst, NULL, SCF_PG_GENERAL, pg) != 0)
scfdie();
r = get_astring_prop(pg, SCF_PROPERTY_RESTARTER, prop, val, value,
value_sz);
if (r == -ENOENT) {
(void) strlcpy(value, SCF_SERVICE_STARTD, value_sz);
} else if (r < 0 || r > max_scf_fmri_sz) {
/*
* Normally we would return true and let the restarter
* tell our caller there is a problem by changing the
* instance's state, but that's not going to happen if
* the restarter is invalid.
*/
result = B_FALSE;
goto out;
}
if (!fmri_has_potential(value, B_FALSE, B_FALSE, B_FALSE, B_TRUE)) {
result = B_FALSE;
goto out;
}
if (restarter_only)
goto out;
/*
* Now we check explicit dependencies.
*/
if (scf_instance_get_snapshot(inst, "running", snap) != 0) {
if (scf_error() != SCF_ERROR_NOT_FOUND)
scfdie();
scf_snapshot_destroy(snap);
snap = NULL;
}
if (scf_iter_instance_pgs_typed_composed(iter, inst, snap,
SCF_GROUP_DEPENDENCY) != 0) {
if (scf_error() != SCF_ERROR_DELETED)
scfdie();
goto out;
}
for (;;) {
r = scf_iter_next_pg(iter, pg);
if (r == 0)
break;
if (r == -1) {
if (scf_error() != SCF_ERROR_DELETED)
scfdie();
goto out;
}
if ((grouping = read_astring_prop(pg, val, prop,
SCF_PROPERTY_GROUPING)) == NULL)
goto out;
if ((type = read_astring_prop(pg, val, prop,
SCF_PROPERTY_TYPE)) == NULL)
goto out;
if (strcmp(type, "path") == 0) {
isfile = B_TRUE;
} else if (strcmp(type, "service") == 0) {
isfile = B_FALSE;
} else {
free(type);
goto out;
}
free(type);
if ((viter = prop_walk_init(pg, SCF_PROPERTY_ENTITIES)) == NULL)
goto out;
if (strcmp(grouping, SCF_DEP_REQUIRE_ALL) == 0) {
r = eval_require_all(viter, value, value_sz, isfile);
} else if (strcmp(grouping, SCF_DEP_REQUIRE_ANY) == 0) {
r = eval_require_any(viter, value, value_sz, isfile);
} else if (strcmp(grouping, SCF_DEP_EXCLUDE_ALL) == 0) {
r = eval_exclude_all(viter, value, value_sz, isfile);
} else if (strcmp(grouping, SCF_DEP_OPTIONAL_ALL) == 0) {
r = eval_optional_all(viter, value, value_sz, isfile);
} else {
scf_iter_destroy(viter);
free(grouping);
grouping = NULL;
goto out;
}
scf_iter_destroy(viter);
free(grouping);
grouping = NULL;
if (r == 0) {
result = B_FALSE;
goto out;
} else if (r == -1) {
goto out;
}
}
out:
free(value);
scf_property_destroy(prop);
scf_value_destroy(val);
scf_pg_destroy(pg);
if (snap != NULL)
scf_snapshot_destroy(snap);
if (grouping != NULL)
free(grouping);
scf_iter_destroy(iter);
return (result);
}
|