summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svc/startd/transition.c
blob: aeb625dceecd391ce35fff4da9c253767ba97297 (plain)
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2016-2018 RackTop Systems.
 */


/*
 * transition.c - Graph State Machine
 *
 * The graph state machine is implemented here, with a typical approach
 * of a function per state.  Separating the implementation allows more
 * clarity into the actions taken on notification of state change, as well
 * as a place for future expansion including hooks for configurable actions.
 * All functions are called with dgraph_lock held.
 *
 * The start action for this state machine is not explicit.  The states
 * (ONLINE and DEGRADED) which need to know when they're entering the state
 * due to a daemon restart implement this understanding by checking for
 * transition from uninitialized.  In the future, this would likely be better
 * as an explicit start action instead of relying on an overloaded transition.
 *
 * All gt_enter functions use the same set of return codes.
 *    0              success
 *    ECONNABORTED   repository connection aborted
 */

#include "startd.h"

static int
gt_running(restarter_instance_state_t state)
{
	if (state == RESTARTER_STATE_ONLINE ||
	    state == RESTARTER_STATE_DEGRADED)
		return (1);

	return (0);
}

static int
gt_enter_uninit(scf_handle_t *h, graph_vertex_t *v,
    restarter_instance_state_t old_state, restarter_error_t rerr)
{
	int err;
	scf_instance_t *inst;

	/* Initialize instance by refreshing it. */

	err = libscf_fmri_get_instance(h, v->gv_name, &inst);
	switch (err) {
	case 0:
		break;

	case ECONNABORTED:
		return (ECONNABORTED);

	case ENOENT:
		return (0);

	case EINVAL:
	case ENOTSUP:
	default:
		bad_error("libscf_fmri_get_instance", err);
	}

	err = refresh_vertex(v, inst);
	if (err == 0)
		graph_enable_by_vertex(v, v->gv_flags & GV_ENABLED, 0);

	scf_instance_destroy(inst);

	/* If the service was running, propagate a stop event. */
	if (gt_running(old_state)) {
		log_framework(LOG_DEBUG, "Propagating stop of %s.\n",
		    v->gv_name);

		graph_transition_propagate(v, PROPAGATE_STOP, rerr);
	}

	graph_transition_sulogin(RESTARTER_STATE_UNINIT, old_state);
	return (0);
}

/* ARGSUSED */
static int
gt_enter_maint(scf_handle_t *h, graph_vertex_t *v,
    restarter_instance_state_t old_state, restarter_error_t rerr)
{
	int to_offline = v->gv_flags & GV_TOOFFLINE;

	/*
	 * If the service was running, propagate a stop event.  If the
	 * service was not running the maintenance transition may satisfy
	 * optional dependencies and should be propagated to determine
	 * whether new dependents are satisfiable.
	 * Instances that transition to maintenance and have the GV_TOOFFLINE
	 * flag are special because they can expose new subtree leaves so
	 * propagate the offline to the instance dependencies.
	 */

	/* instance transitioning to maintenance is considered disabled */
	v->gv_flags &= ~GV_TODISABLE;
	v->gv_flags &= ~GV_TOOFFLINE;

	if (gt_running(old_state)) {
		/*
		 * Handle state change during instance disabling.
		 * Propagate offline to the new exposed leaves.
		 */
		if (to_offline) {
			log_framework(LOG_DEBUG, "%s removed from subtree\n",
			    v->gv_name);

			graph_offline_subtree_leaves(v, (void *)h);
		}

		log_framework(LOG_DEBUG, "Propagating maintenance (stop) of "
		    "%s.\n", v->gv_name);

		graph_transition_propagate(v, PROPAGATE_STOP, rerr);

		/*
		 * The maintenance transition may satisfy optional_all/restart
		 * dependencies and should be propagated to determine
		 * whether new dependents are satisfiable.
		 */
		graph_transition_propagate(v, PROPAGATE_SAT, rerr);
	} else {
		log_framework(LOG_DEBUG, "Propagating maintenance of %s.\n",
		    v->gv_name);

		graph_transition_propagate(v, PROPAGATE_SAT, rerr);
	}

	graph_transition_sulogin(RESTARTER_STATE_MAINT, old_state);
	return (0);
}

/* ARGSUSED */
static int
gt_enter_offline(scf_handle_t *h, graph_vertex_t *v,
    restarter_instance_state_t old_state, restarter_error_t rerr)
{
	int to_offline = v->gv_flags & GV_TOOFFLINE;

	v->gv_flags &= ~GV_TOOFFLINE;

	/*
	 * If the instance should be disabled send it a disable command.
	 * Otherwise, if GV_TOOFFLINE was not set, see if we can start it.
	 */
	if (v->gv_flags & GV_TODISABLE) {
		if (gt_running(old_state) && v->gv_post_disable_f)
			v->gv_post_disable_f();

		vertex_send_event(v, RESTARTER_EVENT_TYPE_DISABLE);
	} else if (v->gv_flags & GV_ENABLED) {
		if (to_offline == 0)
			graph_start_if_satisfied(v);
	}

	/*
	 * If the service was running, propagate a stop event.  If the
	 * service was not running the offline transition may satisfy
	 * optional dependencies and should be propagated to determine
	 * whether new dependents are satisfiable.
	 * Instances that transition to offline and have the GV_TOOFFLINE flag
	 * are special because they can expose new subtree leaves so propagate
	 * the offline to the instance dependencies.
	 */
	if (gt_running(old_state)) {
		/*
		 * Handle state change during instance disabling.
		 * Propagate offline to the new exposed leaves.
		 */
		if (to_offline) {
			log_framework(LOG_DEBUG, "%s removed from subtree\n",
			    v->gv_name);

			graph_offline_subtree_leaves(v, (void *)h);
		}

		log_framework(LOG_DEBUG, "Propagating stop of %s.\n",
		    v->gv_name);

		graph_transition_propagate(v, PROPAGATE_STOP, rerr);

		/*
		 * The offline transition may satisfy require_any/restart
		 * dependencies and should be propagated to determine
		 * whether new dependents are satisfiable.
		 */
		graph_transition_propagate(v, PROPAGATE_SAT, rerr);
	} else {
		log_framework(LOG_DEBUG, "Propagating offline of %s.\n",
		    v->gv_name);

		graph_transition_propagate(v, PROPAGATE_SAT, rerr);
	}

	graph_transition_sulogin(RESTARTER_STATE_OFFLINE, old_state);
	return (0);
}

/* ARGSUSED */
static int
gt_enter_disabled(scf_handle_t *h, graph_vertex_t *v,
    restarter_instance_state_t old_state, restarter_error_t rerr)
{
	int to_offline = v->gv_flags & GV_TOOFFLINE;

	v->gv_flags &= ~GV_TODISABLE;
	v->gv_flags &= ~GV_TOOFFLINE;

	/*
	 * If the instance should be disabled, no problem.  Otherwise,
	 * send an enable command, which should result in the instance
	 * moving to OFFLINE unless the instance is part of a subtree
	 * (non root) and in this case the result is unpredictable.
	 */
	if (v->gv_flags & GV_ENABLED) {
		vertex_send_event(v, RESTARTER_EVENT_TYPE_ENABLE);
	} else if (gt_running(old_state) && v->gv_post_disable_f) {
		v->gv_post_disable_f();
	}

	/*
	 * If the service was running, propagate this as a stop.  If the
	 * service was not running the disabled transition may satisfy
	 * optional dependencies and should be propagated to determine
	 * whether new dependents are satisfiable.
	 */
	if (gt_running(old_state)) {
		/*
		 * We need to propagate the offline to new exposed leaves in
		 * case we've just disabled an instance that was part of a
		 * subtree.
		 */
		if (to_offline) {
			log_framework(LOG_DEBUG, "%s removed from subtree\n",
			    v->gv_name);

			/*
			 * Handle state change during instance disabling.
			 * Propagate offline to the new exposed leaves.
			 */
			graph_offline_subtree_leaves(v, (void *)h);
		}


		log_framework(LOG_DEBUG, "Propagating stop of %s.\n",
		    v->gv_name);

		graph_transition_propagate(v, PROPAGATE_STOP, rerr);

		/*
		 * The disable transition may satisfy optional_all/restart
		 * dependencies and should be propagated to determine
		 * whether new dependents are satisfiable.
		 */
		graph_transition_propagate(v, PROPAGATE_SAT, rerr);
	} else {
		log_framework(LOG_DEBUG, "Propagating disable of %s.\n",
		    v->gv_name);

		graph_transition_propagate(v, PROPAGATE_SAT, rerr);
	}

	graph_transition_sulogin(RESTARTER_STATE_DISABLED, old_state);
	return (0);
}

static int
gt_internal_online_or_degraded(scf_handle_t *h, graph_vertex_t *v,
    restarter_instance_state_t old_state, restarter_error_t rerr)
{
	int r;

	/*
	 * If the instance has just come up, update the start
	 * snapshot.
	 */
	if (gt_running(old_state) == 0) {
		/*
		 * Don't fire if we're just recovering state
		 * after a restart.
		 */
		if (old_state != RESTARTER_STATE_UNINIT &&
		    v->gv_post_online_f)
			v->gv_post_online_f();

		r = libscf_snapshots_poststart(h, v->gv_name, B_TRUE);
		switch (r) {
		case 0:
		case ENOENT:
			/*
			 * If ENOENT, the instance must have been
			 * deleted.  Pretend we were successful since
			 * we should get a delete event later.
			 */
			break;

		case ECONNABORTED:
			return (ECONNABORTED);

		case EACCES:
		case ENOTSUP:
		default:
			bad_error("libscf_snapshots_poststart", r);
		}
	}

	if (!(v->gv_flags & GV_ENABLED)) {
		vertex_send_event(v, RESTARTER_EVENT_TYPE_DISABLE);
	} else if (v->gv_flags & GV_TOOFFLINE) {
		/*
		 * If the vertex has the GV_TOOFFLINE flag set then that's
		 * because the instance was transitioning from offline to
		 * online and the reverse disable algorithm doesn't offline
		 * those instances because it was already appearing offline.
		 * So do it now.
		 */
		offline_vertex(v);
	}

	if (gt_running(old_state) == 0) {
		log_framework(LOG_DEBUG, "Propagating start of %s.\n",
		    v->gv_name);

		graph_transition_propagate(v, PROPAGATE_START, rerr);
	} else if (rerr == RERR_REFRESH) {
		/* For refresh we'll get a message sans state change */

		log_framework(LOG_DEBUG, "Propagating refresh of %s.\n",
		    v->gv_name);

		graph_transition_propagate(v, PROPAGATE_STOP, rerr);
	}

	return (0);
}

static int
gt_enter_online(scf_handle_t *h, graph_vertex_t *v,
    restarter_instance_state_t old_state, restarter_error_t rerr)
{
	int r;

	r = gt_internal_online_or_degraded(h, v, old_state, rerr);
	if (r != 0)
		return (r);

	graph_transition_sulogin(RESTARTER_STATE_ONLINE, old_state);
	return (0);
}

static int
gt_enter_degraded(scf_handle_t *h, graph_vertex_t *v,
    restarter_instance_state_t old_state, restarter_error_t rerr)
{
	int r;

	r = gt_internal_online_or_degraded(h, v, old_state, rerr);
	if (r != 0)
		return (r);

	graph_transition_sulogin(RESTARTER_STATE_DEGRADED, old_state);
	return (0);
}

/*
 * gt_transition() implements the state transition for the graph
 * state machine.  It can return:
 *    0              success
 *    ECONNABORTED   repository connection aborted
 *
 * v->gv_state should be set to the state we're transitioning to before
 * calling this function.
 */
int
gt_transition(scf_handle_t *h, graph_vertex_t *v, restarter_error_t rerr,
    restarter_instance_state_t old_state)
{
	int err;
	int lost_repository = 0;

	/*
	 * If there's a common set of work to be done on exit from the
	 * old_state, include it as a separate set of functions here.  For
	 * now there's no such work, so there are no gt_exit functions.
	 */

	err = vertex_subgraph_dependencies_shutdown(h, v, old_state);
	switch (err) {
	case 0:
		break;

	case ECONNABORTED:
		lost_repository = 1;
		break;

	default:
		bad_error("vertex_subgraph_dependencies_shutdown", err);
	}

	/*
	 * Now call the appropriate gt_enter function for the new state.
	 */
	switch (v->gv_state) {
	case RESTARTER_STATE_UNINIT:
		err = gt_enter_uninit(h, v, old_state, rerr);
		break;

	case RESTARTER_STATE_DISABLED:
		err = gt_enter_disabled(h, v, old_state, rerr);
		break;

	case RESTARTER_STATE_OFFLINE:
		err = gt_enter_offline(h, v, old_state, rerr);
		break;

	case RESTARTER_STATE_ONLINE:
		err = gt_enter_online(h, v, old_state, rerr);
		break;

	case RESTARTER_STATE_DEGRADED:
		err = gt_enter_degraded(h, v, old_state, rerr);
		break;

	case RESTARTER_STATE_MAINT:
		err = gt_enter_maint(h, v, old_state, rerr);
		break;

	default:
		/* Shouldn't be in an invalid state. */
#ifndef NDEBUG
		uu_warn("%s:%d: Invalid state %d.\n", __FILE__, __LINE__,
		    v->gv_state);
#endif
		abort();
	}

	switch (err) {
	case 0:
		break;

	case ECONNABORTED:
		lost_repository = 1;
		break;

	default:
#ifndef NDEBUG
		uu_warn("%s:%d: "
		    "gt_enter_%s() failed with unexpected error %d.\n",
		    __FILE__, __LINE__, instance_state_str[v->gv_state], err);
#endif
		abort();
	}

	return (lost_repository ? ECONNABORTED : 0);
}