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.
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.
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]
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
27 * restarter.c - service manipulation
29 * This component manages services whose restarter is svc.startd, the standard
30 * restarter. It translates restarter protocol events from the graph engine
31 * into actions on processes, as a delegated restarter would do.
33 * The master restarter manages a number of always-running threads:
34 * - restarter event thread: events from the graph engine
35 * - timeout thread: thread to fire queued timeouts
36 * - contract thread: thread to handle contract events
37 * - wait thread: thread to handle wait-based services
39 * The other threads are created as-needed:
40 * - per-instance method threads
41 * - per-instance event processing threads
43 * The interaction of all threads must result in the following conditions
44 * being satisfied (on a per-instance basis):
45 * - restarter events must be processed in order
46 * - method execution must be serialized
47 * - instance delete must be held until outstanding methods are complete
48 * - contract events shouldn't be processed while a method is running
49 * - timeouts should fire even when a method is running
51 * Service instances are represented by restarter_inst_t's and are kept in the
55 * The current state of a service instance is kept in
56 * restarter_inst_t->ri_i.i_state. If transition to a new state could take
57 * some time, then before we effect the transition we set
58 * restarter_inst_t->ri_i.i_next_state to the target state, and afterwards we
59 * rotate i_next_state to i_state and set i_next_state to
60 * RESTARTER_STATE_NONE. So usually i_next_state is _NONE when ri_lock is not
61 * held. The exception is when we launch methods, which are done with
62 * a separate thread. To keep any other threads from grabbing ri_lock before
63 * method_thread() does, we set ri_method_thread to the thread id of the
64 * method thread, and when it is nonzero any thread with a different thread id
65 * waits on ri_method_cv.
67 * Method execution is serialized by blocking on ri_method_cv in
68 * inst_lookup_by_id() and waiting for a 0 value of ri_method_thread. This
69 * also prevents the instance structure from being deleted until all
70 * outstanding operations such as method_thread() have finished.
74 * dgraph_lock [can be held when taking:]
76 * dictionary->dict_lock
79 * ru->restarter_update_lock
80 * restarter_queue->rpeq_lock
81 * instance_list.ril_lock
83 * st->st_configd_live_lock
85 * instance_list.ril_lock
86 * graph_queue->gpeq_lock
88 * st->st_configd_live_lock
89 * dictionary->dict_lock
91 * graph_queue->gpeq_lock
100 * single_user_thread_lock
106 * logbuf_mutex nests inside pretty much everything.
109 #include <sys/contract/process.h>
110 #include <sys/ctfs.h>
111 #include <sys/stat.h>
112 #include <sys/time.h>
113 #include <sys/types.h>
115 #include <sys/wait.h>
119 #include <libcontract.h>
120 #include <libcontract_priv.h>
122 #include <librestart.h>
123 #include <librestart_priv.h>
124 #include <libuutil.h>
135 #include "protocol.h"
137 static uu_list_pool_t
*restarter_instance_pool
;
138 static restarter_instance_list_t instance_list
;
140 static uu_list_pool_t
*restarter_queue_pool
;
143 * Function used to reset the restart times for an instance, when
144 * an administrative task comes along and essentially makes the times
145 * in this array ineffective.
148 reset_start_times(restarter_inst_t
*inst
)
150 inst
->ri_start_index
= 0;
151 bzero(inst
->ri_start_time
, sizeof (inst
->ri_start_time
));
156 restarter_instance_compare(const void *lc_arg
, const void *rc_arg
,
159 int lc_id
= ((const restarter_inst_t
*)lc_arg
)->ri_id
;
160 int rc_id
= *(int *)rc_arg
;
169 static restarter_inst_t
*
170 inst_lookup_by_name(const char *name
)
174 id
= dict_lookup_byname(name
);
178 return (inst_lookup_by_id(id
));
182 inst_lookup_by_id(int id
)
184 restarter_inst_t
*inst
;
186 MUTEX_LOCK(&instance_list
.ril_lock
);
187 inst
= uu_list_find(instance_list
.ril_instance_list
, &id
, NULL
, NULL
);
189 MUTEX_LOCK(&inst
->ri_lock
);
190 MUTEX_UNLOCK(&instance_list
.ril_lock
);
193 while (inst
->ri_method_thread
!= 0 &&
194 !pthread_equal(inst
->ri_method_thread
, pthread_self())) {
195 ++inst
->ri_method_waiters
;
196 (void) pthread_cond_wait(&inst
->ri_method_cv
,
198 assert(inst
->ri_method_waiters
> 0);
199 --inst
->ri_method_waiters
;
206 static restarter_inst_t
*
207 inst_lookup_queue(const char *name
)
210 restarter_inst_t
*inst
;
212 id
= dict_lookup_byname(name
);
216 MUTEX_LOCK(&instance_list
.ril_lock
);
217 inst
= uu_list_find(instance_list
.ril_instance_list
, &id
, NULL
, NULL
);
219 MUTEX_LOCK(&inst
->ri_queue_lock
);
220 MUTEX_UNLOCK(&instance_list
.ril_lock
);
226 service_style(int flags
)
228 switch (flags
& RINST_STYLE_MASK
) {
229 case RINST_CONTRACT
: return ("contract");
230 case RINST_TRANSIENT
: return ("transient");
231 case RINST_WAIT
: return ("wait");
235 uu_warn("%s:%d: Bad flags 0x%x.\n", __FILE__
, __LINE__
, flags
);
243 * Fails with ECONNABORTED or ECANCELED.
246 check_contract(restarter_inst_t
*inst
, boolean_t primary
,
247 scf_instance_t
*scf_inst
)
252 ctidp
= primary
? &inst
->ri_i
.i_primary_ctid
:
253 &inst
->ri_i
.i_transient_ctid
;
257 fd
= contract_open(*ctidp
, NULL
, "status", O_RDONLY
);
264 r
= restarter_remove_contract(scf_inst
, *ctidp
, primary
?
265 RESTARTER_CONTRACT_PRIMARY
: RESTARTER_CONTRACT_TRANSIENT
);
274 uu_die("Out of memory\n");
278 uu_die("Insufficient privilege.\n");
282 uu_die("Repository backend access denied.\n");
286 log_error(LOG_INFO
, "Could not remove unusable contract id %ld "
287 "for %s from repository.\n", *ctidp
, inst
->ri_i
.i_fmri
);
299 static int stop_instance(scf_handle_t
*, restarter_inst_t
*, stop_cause_t
);
302 * int restarter_insert_inst(scf_handle_t *, char *)
303 * If the inst is already in the restarter list, return its id. If the inst
304 * is not in the restarter list, initialize a restarter_inst_t, initialize its
305 * states, insert it into the list, and return 0.
308 * ENOENT - name is not in the repository
311 restarter_insert_inst(scf_handle_t
*h
, const char *name
)
314 restarter_inst_t
*inst
;
316 scf_service_t
*scf_svc
;
317 scf_instance_t
*scf_inst
;
318 scf_snapshot_t
*snap
= NULL
;
319 scf_propertygroup_t
*pg
;
320 char *svc_name
, *inst_name
;
321 char logfilebuf
[PATH_MAX
];
323 boolean_t do_commit_states
;
324 restarter_instance_state_t state
, next_state
;
325 protocol_states_t
*ps
;
327 restarter_str_t reason
= restarter_str_insert_in_graph
;
329 MUTEX_LOCK(&instance_list
.ril_lock
);
332 * We don't use inst_lookup_by_name() here because we want the lookup
333 * & insert to be atomic.
335 id
= dict_lookup_byname(name
);
337 inst
= uu_list_find(instance_list
.ril_instance_list
, &id
, NULL
,
340 MUTEX_UNLOCK(&instance_list
.ril_lock
);
345 /* Allocate an instance */
346 inst
= startd_zalloc(sizeof (restarter_inst_t
));
347 inst
->ri_utmpx_prefix
= startd_alloc(max_scf_value_size
);
348 inst
->ri_utmpx_prefix
[0] = '\0';
350 inst
->ri_i
.i_fmri
= startd_alloc(strlen(name
) + 1);
351 (void) strcpy((char *)inst
->ri_i
.i_fmri
, name
);
353 inst
->ri_queue
= startd_list_create(restarter_queue_pool
, inst
, 0);
356 * id shouldn't be -1 since we use the same dictionary as graph.c, but
359 inst
->ri_id
= (id
!= -1 ? id
: dict_insert(name
));
361 special_online_hooks_get(name
, &inst
->ri_pre_online_hook
,
362 &inst
->ri_post_online_hook
, &inst
->ri_post_offline_hook
);
364 scf_svc
= safe_scf_service_create(h
);
365 scf_inst
= safe_scf_instance_create(h
);
366 pg
= safe_scf_pg_create(h
);
367 svc_name
= startd_alloc(max_scf_name_size
);
368 inst_name
= startd_alloc(max_scf_name_size
);
372 scf_snapshot_destroy(snap
);
373 if (inst
->ri_logstem
!= NULL
)
374 startd_free(inst
->ri_logstem
, PATH_MAX
);
375 if (inst
->ri_common_name
!= NULL
)
376 startd_free(inst
->ri_common_name
,
377 strlen(inst
->ri_common_name
) + 1);
378 if (inst
->ri_C_common_name
!= NULL
)
379 startd_free(inst
->ri_C_common_name
,
380 strlen(inst
->ri_C_common_name
) + 1);
382 inst
->ri_logstem
= NULL
;
383 inst
->ri_common_name
= NULL
;
384 inst
->ri_C_common_name
= NULL
;
386 if (scf_handle_decode_fmri(h
, name
, NULL
, scf_svc
, scf_inst
, NULL
,
387 NULL
, SCF_DECODE_FMRI_EXACT
) != 0) {
388 switch (scf_error()) {
389 case SCF_ERROR_CONNECTION_BROKEN
:
390 libscf_handle_rebind(h
);
393 case SCF_ERROR_NOT_FOUND
:
397 uu_die("Can't decode FMRI %s: %s\n", name
,
398 scf_strerror(scf_error()));
402 * If there's no running snapshot, then we execute using the editing
403 * snapshot. Pending snapshots will be taken later.
405 snap
= libscf_get_running_snapshot(scf_inst
);
407 if ((scf_service_get_name(scf_svc
, svc_name
, max_scf_name_size
) < 0) ||
408 (scf_instance_get_name(scf_inst
, inst_name
, max_scf_name_size
) <
410 switch (scf_error()) {
411 case SCF_ERROR_NOT_SET
:
414 case SCF_ERROR_CONNECTION_BROKEN
:
415 libscf_handle_rebind(h
);
426 (void) snprintf(logfilebuf
, PATH_MAX
, "%s:%s", svc_name
, inst_name
);
427 for (c
= logfilebuf
; *c
!= '\0'; c
++)
431 inst
->ri_logstem
= startd_alloc(PATH_MAX
);
432 (void) snprintf(inst
->ri_logstem
, PATH_MAX
, "%s%s", logfilebuf
,
436 * If the restarter group is missing, use uninit/none. Otherwise,
437 * we're probably being restarted & don't want to mess up the states
440 state
= RESTARTER_STATE_UNINIT
;
441 next_state
= RESTARTER_STATE_NONE
;
443 r
= scf_instance_get_pg(scf_inst
, SCF_PG_RESTARTER
, pg
);
445 switch (scf_error()) {
446 case SCF_ERROR_CONNECTION_BROKEN
:
447 libscf_handle_rebind(h
);
450 case SCF_ERROR_NOT_SET
:
453 case SCF_ERROR_NOT_FOUND
:
455 * This shouldn't happen since the graph engine should
456 * have initialized the state to uninitialized/none if
457 * there was no restarter pg. In case somebody
458 * deleted it, though....
460 do_commit_states
= B_TRUE
;
468 r
= libscf_read_states(pg
, &state
, &next_state
);
470 do_commit_states
= B_TRUE
;
472 if (next_state
!= RESTARTER_STATE_NONE
) {
474 * Force next_state to _NONE since we
475 * don't look for method processes.
477 next_state
= RESTARTER_STATE_NONE
;
478 do_commit_states
= B_TRUE
;
481 * The reason for transition will depend on
484 if (st
->st_initial
== 0)
485 reason
= restarter_str_startd_restart
;
486 else if (state
== RESTARTER_STATE_MAINT
)
487 reason
= restarter_str_bad_repo_state
;
489 * Inform the restarter of our state without
490 * changing the STIME in the repository.
492 ps
= startd_alloc(sizeof (*ps
));
493 inst
->ri_i
.i_state
= ps
->ps_state
= state
;
494 inst
->ri_i
.i_next_state
= ps
->ps_state_next
=
496 ps
->ps_reason
= reason
;
498 graph_protocol_send_event(inst
->ri_i
.i_fmri
,
499 GRAPH_UPDATE_STATE_CHANGE
, ps
);
501 do_commit_states
= B_FALSE
;
506 switch (libscf_get_startd_properties(scf_inst
, snap
, &inst
->ri_flags
,
507 &inst
->ri_utmpx_prefix
)) {
512 libscf_handle_rebind(h
);
520 * This is odd, because the graph engine should have required
521 * the general property group. So we'll just use default
522 * flags in anticipation of the graph engine sending us
523 * REMOVE_INSTANCE when it finds out that the general property
524 * group has been deleted.
526 inst
->ri_flags
= RINST_CONTRACT
;
534 r
= libscf_get_template_values(scf_inst
, snap
,
535 &inst
->ri_common_name
, &inst
->ri_C_common_name
);
538 * Copy our names to smaller buffers to reduce our memory footprint.
540 if (inst
->ri_common_name
!= NULL
) {
541 char *tmp
= safe_strdup(inst
->ri_common_name
);
542 startd_free(inst
->ri_common_name
, max_scf_value_size
);
543 inst
->ri_common_name
= tmp
;
546 if (inst
->ri_C_common_name
!= NULL
) {
547 char *tmp
= safe_strdup(inst
->ri_C_common_name
);
548 startd_free(inst
->ri_C_common_name
, max_scf_value_size
);
549 inst
->ri_C_common_name
= tmp
;
557 libscf_handle_rebind(h
);
572 switch (libscf_read_method_ids(h
, scf_inst
, inst
->ri_i
.i_fmri
,
573 &inst
->ri_i
.i_primary_ctid
, &inst
->ri_i
.i_transient_ctid
,
579 libscf_handle_rebind(h
);
590 if (inst
->ri_i
.i_primary_ctid
>= 1) {
591 contract_hash_store(inst
->ri_i
.i_primary_ctid
, inst
->ri_id
);
593 switch (check_contract(inst
, B_TRUE
, scf_inst
)) {
598 libscf_handle_rebind(h
);
610 if (inst
->ri_i
.i_transient_ctid
>= 1) {
611 switch (check_contract(inst
, B_FALSE
, scf_inst
)) {
616 libscf_handle_rebind(h
);
628 /* No more failures we live through, so add it to the list. */
629 (void) pthread_mutex_init(&inst
->ri_lock
, &mutex_attrs
);
630 (void) pthread_mutex_init(&inst
->ri_queue_lock
, &mutex_attrs
);
631 MUTEX_LOCK(&inst
->ri_lock
);
632 MUTEX_LOCK(&inst
->ri_queue_lock
);
634 (void) pthread_cond_init(&inst
->ri_method_cv
, NULL
);
636 uu_list_node_init(inst
, &inst
->ri_link
, restarter_instance_pool
);
637 uu_list_insert(instance_list
.ril_instance_list
, inst
, idx
);
638 MUTEX_UNLOCK(&instance_list
.ril_lock
);
640 if (start_pid
!= -1 &&
641 (inst
->ri_flags
& RINST_STYLE_MASK
) == RINST_WAIT
) {
643 ret
= wait_register(start_pid
, inst
->ri_i
.i_fmri
, 0, 1);
646 * Implication: if we can't reregister the
647 * instance, we will start another one. Two
648 * instances may or may not result in a resource
651 log_error(LOG_WARNING
,
652 "%s: couldn't reregister %ld for wait\n",
653 inst
->ri_i
.i_fmri
, start_pid
);
654 } else if (ret
== 1) {
656 * Leading PID has exited.
658 (void) stop_instance(h
, inst
, RSTOP_EXIT
);
665 if (do_commit_states
)
666 (void) restarter_instance_update_states(h
, inst
, state
,
667 next_state
, RERR_NONE
, reason
);
669 log_framework(LOG_DEBUG
, "%s is a %s-style service\n", name
,
670 service_style(inst
->ri_flags
));
672 MUTEX_UNLOCK(&inst
->ri_queue_lock
);
673 MUTEX_UNLOCK(&inst
->ri_lock
);
675 startd_free(svc_name
, max_scf_name_size
);
676 startd_free(inst_name
, max_scf_name_size
);
677 scf_snapshot_destroy(snap
);
678 scf_instance_destroy(scf_inst
);
679 scf_service_destroy(scf_svc
);
681 log_framework(LOG_DEBUG
, "%s: inserted instance into restarter list\n",
687 MUTEX_UNLOCK(&instance_list
.ril_lock
);
688 startd_free(inst_name
, max_scf_name_size
);
689 startd_free(svc_name
, max_scf_name_size
);
691 scf_snapshot_destroy(snap
);
693 scf_instance_destroy(scf_inst
);
694 scf_service_destroy(scf_svc
);
695 startd_free((void *)inst
->ri_i
.i_fmri
, strlen(inst
->ri_i
.i_fmri
) + 1);
696 uu_list_destroy(inst
->ri_queue
);
697 if (inst
->ri_logstem
!= NULL
)
698 startd_free(inst
->ri_logstem
, PATH_MAX
);
699 if (inst
->ri_common_name
!= NULL
)
700 startd_free(inst
->ri_common_name
,
701 strlen(inst
->ri_common_name
) + 1);
702 if (inst
->ri_C_common_name
!= NULL
)
703 startd_free(inst
->ri_C_common_name
,
704 strlen(inst
->ri_C_common_name
) + 1);
705 startd_free(inst
->ri_utmpx_prefix
, max_scf_value_size
);
706 startd_free(inst
, sizeof (restarter_inst_t
));
711 restarter_delete_inst(restarter_inst_t
*ri
)
714 restarter_inst_t
*rip
;
716 restarter_instance_qentry_t
*e
;
718 assert(MUTEX_HELD(&ri
->ri_lock
));
721 * Must drop the instance lock so we can pick up the instance_list
722 * lock & remove the instance.
725 MUTEX_UNLOCK(&ri
->ri_lock
);
727 MUTEX_LOCK(&instance_list
.ril_lock
);
729 rip
= uu_list_find(instance_list
.ril_instance_list
, &id
, NULL
, NULL
);
731 MUTEX_UNLOCK(&instance_list
.ril_lock
);
737 uu_list_remove(instance_list
.ril_instance_list
, ri
);
739 log_framework(LOG_DEBUG
, "%s: deleted instance from restarter list\n",
742 MUTEX_UNLOCK(&instance_list
.ril_lock
);
745 * We can lock the instance without holding the instance_list lock
746 * since we removed the instance from the list.
748 MUTEX_LOCK(&ri
->ri_lock
);
749 MUTEX_LOCK(&ri
->ri_queue_lock
);
751 if (ri
->ri_i
.i_primary_ctid
>= 1)
752 contract_hash_remove(ri
->ri_i
.i_primary_ctid
);
754 while (ri
->ri_method_thread
!= 0 || ri
->ri_method_waiters
> 0)
755 (void) pthread_cond_wait(&ri
->ri_method_cv
, &ri
->ri_lock
);
757 while ((e
= uu_list_teardown(ri
->ri_queue
, &cookie
)) != NULL
)
758 startd_free(e
, sizeof (*e
));
759 uu_list_destroy(ri
->ri_queue
);
761 startd_free((void *)ri
->ri_i
.i_fmri
, strlen(ri
->ri_i
.i_fmri
) + 1);
762 startd_free(ri
->ri_logstem
, PATH_MAX
);
763 if (ri
->ri_common_name
!= NULL
)
764 startd_free(ri
->ri_common_name
,
765 strlen(ri
->ri_common_name
) + 1);
766 if (ri
->ri_C_common_name
!= NULL
)
767 startd_free(ri
->ri_C_common_name
,
768 strlen(ri
->ri_C_common_name
) + 1);
769 startd_free(ri
->ri_utmpx_prefix
, max_scf_value_size
);
770 (void) pthread_mutex_destroy(&ri
->ri_lock
);
771 (void) pthread_mutex_destroy(&ri
->ri_queue_lock
);
772 startd_free(ri
, sizeof (restarter_inst_t
));
776 * instance_is_wait_style()
778 * Returns 1 if the given instance is a "wait-style" service instance.
781 instance_is_wait_style(restarter_inst_t
*inst
)
783 assert(MUTEX_HELD(&inst
->ri_lock
));
784 return ((inst
->ri_flags
& RINST_STYLE_MASK
) == RINST_WAIT
);
788 * instance_is_transient_style()
790 * Returns 1 if the given instance is a transient service instance.
793 instance_is_transient_style(restarter_inst_t
*inst
)
795 assert(MUTEX_HELD(&inst
->ri_lock
));
796 return ((inst
->ri_flags
& RINST_STYLE_MASK
) == RINST_TRANSIENT
);
800 * instance_in_transition()
801 * Returns 1 if instance is in transition, 0 if not
804 instance_in_transition(restarter_inst_t
*inst
)
806 assert(MUTEX_HELD(&inst
->ri_lock
));
807 if (inst
->ri_i
.i_next_state
== RESTARTER_STATE_NONE
)
813 * returns 1 if instance is already started, 0 if not
816 instance_started(restarter_inst_t
*inst
)
820 assert(MUTEX_HELD(&inst
->ri_lock
));
822 if (inst
->ri_i
.i_state
== RESTARTER_STATE_ONLINE
||
823 inst
->ri_i
.i_state
== RESTARTER_STATE_DEGRADED
)
834 * ECONNRESET - success, but h was rebound
837 restarter_instance_update_states(scf_handle_t
*h
, restarter_inst_t
*ri
,
838 restarter_instance_state_t new_state
,
839 restarter_instance_state_t new_state_next
, restarter_error_t err
,
840 restarter_str_t reason
)
842 protocol_states_t
*states
;
844 uint_t retry_count
= 0, msecs
= ALLOC_DELAY
;
845 boolean_t rebound
= B_FALSE
;
846 int prev_state_online
;
849 assert(MUTEX_HELD(&ri
->ri_lock
));
851 prev_state_online
= instance_started(ri
);
854 e
= _restarter_commit_states(h
, &ri
->ri_i
, new_state
, new_state_next
,
855 restarter_get_str_short(reason
));
862 if (retry_count
< ALLOC_RETRY
) {
863 (void) poll(NULL
, 0, msecs
);
864 msecs
*= ALLOC_DELAY_MULT
;
868 /* Like startd_alloc(). */
869 uu_die("Insufficient memory.\n");
873 libscf_handle_rebind(h
);
880 log_error(LOG_NOTICE
, "Could not commit state change for %s "
881 "to repository: %s.\n", ri
->ri_i
.i_fmri
, strerror(e
));
885 ri
->ri_i
.i_state
= new_state
;
886 ri
->ri_i
.i_next_state
= new_state_next
;
891 bad_error("_restarter_commit_states", e
);
894 states
= startd_alloc(sizeof (protocol_states_t
));
895 states
->ps_state
= new_state
;
896 states
->ps_state_next
= new_state_next
;
897 states
->ps_err
= err
;
898 states
->ps_reason
= reason
;
899 graph_protocol_send_event(ri
->ri_i
.i_fmri
, GRAPH_UPDATE_STATE_CHANGE
,
902 state_online
= instance_started(ri
);
904 if (prev_state_online
&& !state_online
)
905 ri
->ri_post_offline_hook();
906 else if (!prev_state_online
&& state_online
)
907 ri
->ri_post_online_hook();
909 return (rebound
? ECONNRESET
: 0);
913 restarter_mark_pending_snapshot(const char *fmri
, uint_t flag
)
915 restarter_inst_t
*inst
;
917 assert(flag
== RINST_RETAKE_RUNNING
|| flag
== RINST_RETAKE_START
);
919 inst
= inst_lookup_by_name(fmri
);
923 inst
->ri_flags
|= flag
;
925 MUTEX_UNLOCK(&inst
->ri_lock
);
929 restarter_take_pending_snapshots(scf_handle_t
*h
)
931 restarter_inst_t
*inst
;
934 MUTEX_LOCK(&instance_list
.ril_lock
);
936 for (inst
= uu_list_first(instance_list
.ril_instance_list
);
938 inst
= uu_list_next(instance_list
.ril_instance_list
, inst
)) {
940 scf_instance_t
*sinst
= NULL
;
942 MUTEX_LOCK(&inst
->ri_lock
);
945 * This is where we'd check inst->ri_method_thread and if it
946 * were nonzero we'd wait in anticipation of another thread
947 * executing a method for inst. Doing so with the instance_list
948 * locked, though, leads to deadlock. Since taking a snapshot
949 * during that window won't hurt anything, we'll just continue.
952 fmri
= inst
->ri_i
.i_fmri
;
954 if (inst
->ri_flags
& RINST_RETAKE_RUNNING
) {
955 scf_snapshot_t
*rsnap
;
957 (void) libscf_fmri_get_instance(h
, fmri
, &sinst
);
959 rsnap
= libscf_get_or_make_running_snapshot(sinst
,
962 scf_instance_destroy(sinst
);
965 inst
->ri_flags
&= ~RINST_RETAKE_RUNNING
;
967 scf_snapshot_destroy(rsnap
);
970 if (inst
->ri_flags
& RINST_RETAKE_START
) {
971 switch (r
= libscf_snapshots_poststart(h
, fmri
,
975 inst
->ri_flags
&= ~RINST_RETAKE_START
;
983 bad_error("libscf_snapshots_poststart", r
);
987 MUTEX_UNLOCK(&inst
->ri_lock
);
990 MUTEX_UNLOCK(&instance_list
.ril_lock
);
995 restarter_post_fsminimal_thread(void *unused
)
1000 h
= libscf_handle_create_bound_loop();
1003 r
= libscf_create_self(h
);
1007 assert(r
== ECONNABORTED
);
1008 libscf_handle_rebind(h
);
1011 restarter_take_pending_snapshots(h
);
1013 (void) scf_handle_unbind(h
);
1014 scf_handle_destroy(h
);
1020 * int stop_instance()
1022 * Stop the instance identified by the instance given as the second argument,
1023 * for the cause stated.
1027 * -1 - inst is in transition
1030 stop_instance(scf_handle_t
*local_handle
, restarter_inst_t
*inst
,
1036 restarter_error_t re
;
1037 restarter_str_t reason
;
1039 assert(MUTEX_HELD(&inst
->ri_lock
));
1040 assert(inst
->ri_method_thread
== 0);
1045 reason
= restarter_str_ct_ev_exit
;
1046 cp
= "all processes in service exited";
1050 reason
= restarter_str_ct_ev_core
;
1051 cp
= "process dumped core";
1055 reason
= restarter_str_ct_ev_signal
;
1056 cp
= "process received fatal signal from outside the service";
1060 reason
= restarter_str_ct_ev_hwerr
;
1061 cp
= "process killed due to uncorrectable hardware error";
1063 case RSTOP_DEPENDENCY
:
1065 reason
= restarter_str_dependency_activity
;
1066 cp
= "dependency activity requires stop";
1070 reason
= restarter_str_disable_request
;
1071 cp
= "service disabled";
1075 reason
= restarter_str_restart_request
;
1076 cp
= "service restarting";
1080 (void) fprintf(stderr
, "Unknown cause %d at %s:%d.\n",
1081 cause
, __FILE__
, __LINE__
);
1086 /* Services in the disabled and maintenance state are ignored */
1087 if (inst
->ri_i
.i_state
== RESTARTER_STATE_MAINT
||
1088 inst
->ri_i
.i_state
== RESTARTER_STATE_DISABLED
) {
1089 log_framework(LOG_DEBUG
,
1090 "%s: stop_instance -> is maint/disabled\n",
1095 /* Already stopped instances are left alone */
1096 if (instance_started(inst
) == 0) {
1097 log_framework(LOG_DEBUG
, "Restarter: %s is already stopped.\n",
1102 if (instance_in_transition(inst
)) {
1103 /* requeue event by returning -1 */
1104 log_framework(LOG_DEBUG
,
1105 "Restarter: Not stopping %s, in transition.\n",
1110 log_instance(inst
, B_TRUE
, "Stopping because %s.", cp
);
1112 log_framework(re
== RERR_FAULT
? LOG_INFO
: LOG_DEBUG
,
1113 "%s: Instance stopping because %s.\n", inst
->ri_i
.i_fmri
, cp
);
1115 if (instance_is_wait_style(inst
) && cause
== RSTOP_EXIT
) {
1117 * No need to stop instance, as child has exited; remove
1118 * contract and move the instance to the offline state.
1120 switch (err
= restarter_instance_update_states(local_handle
,
1121 inst
, inst
->ri_i
.i_state
, RESTARTER_STATE_OFFLINE
, re
,
1128 bad_error("restarter_instance_update_states", err
);
1131 (void) update_fault_count(inst
, FAULT_COUNT_RESET
);
1132 reset_start_times(inst
);
1134 if (inst
->ri_i
.i_primary_ctid
!= 0) {
1136 safe_scf_instance_create(local_handle
);
1137 inst
->ri_mi_deleted
= B_FALSE
;
1139 libscf_reget_instance(inst
);
1140 method_remove_contract(inst
, B_TRUE
, B_TRUE
);
1142 scf_instance_destroy(inst
->ri_m_inst
);
1143 inst
->ri_m_inst
= NULL
;
1146 switch (err
= restarter_instance_update_states(local_handle
,
1147 inst
, inst
->ri_i
.i_next_state
, RESTARTER_STATE_NONE
, re
,
1154 bad_error("restarter_instance_update_states", err
);
1158 } else if (instance_is_wait_style(inst
) && re
== RERR_RESTART
) {
1160 * Stopping a wait service through means other than the pid
1161 * exiting should keep wait_thread() from restarting the
1162 * service, by removing it from the wait list.
1163 * We cannot remove it right now otherwise the process will
1164 * end up <defunct> so mark it to be ignored.
1166 wait_ignore_by_fmri(inst
->ri_i
.i_fmri
);
1169 switch (err
= restarter_instance_update_states(local_handle
, inst
,
1170 inst
->ri_i
.i_state
, inst
->ri_i
.i_enabled
? RESTARTER_STATE_OFFLINE
:
1171 RESTARTER_STATE_DISABLED
, RERR_NONE
, reason
)) {
1177 bad_error("restarter_instance_update_states", err
);
1180 info
= startd_zalloc(sizeof (fork_info_t
));
1182 info
->sf_id
= inst
->ri_id
;
1183 info
->sf_method_type
= METHOD_STOP
;
1184 info
->sf_event_type
= re
;
1185 info
->sf_reason
= reason
;
1186 inst
->ri_method_thread
= startd_thread_create(method_thread
, info
);
1193 * ENOENT - fmri is not in instance_list
1195 * ECONNRESET - success, though handle was rebound
1196 * -1 - instance is in transition
1199 stop_instance_fmri(scf_handle_t
*h
, const char *fmri
, uint_t flags
)
1201 restarter_inst_t
*rip
;
1204 rip
= inst_lookup_by_name(fmri
);
1208 r
= stop_instance(h
, rip
, flags
);
1210 MUTEX_UNLOCK(&rip
->ri_lock
);
1216 unmaintain_instance(scf_handle_t
*h
, restarter_inst_t
*rip
,
1217 unmaint_cause_t cause
)
1220 scf_instance_t
*inst
;
1222 uint_t tries
= 0, msecs
= ALLOC_DELAY
;
1224 restarter_str_t reason
;
1226 assert(MUTEX_HELD(&rip
->ri_lock
));
1228 if (rip
->ri_i
.i_state
!= RESTARTER_STATE_MAINT
) {
1229 log_error(LOG_DEBUG
, "Restarter: "
1230 "Ignoring maintenance off command because %s is not in the "
1231 "maintenance state.\n", rip
->ri_i
.i_fmri
);
1236 case RUNMAINT_CLEAR
:
1237 cp
= "clear requested";
1238 reason
= restarter_str_clear_request
;
1240 case RUNMAINT_DISABLE
:
1241 cp
= "disable requested";
1242 reason
= restarter_str_disable_request
;
1246 (void) fprintf(stderr
, "Uncaught case for %d at %s:%d.\n",
1247 cause
, __FILE__
, __LINE__
);
1252 log_instance(rip
, B_TRUE
, "Leaving maintenance because %s.",
1254 log_framework(LOG_DEBUG
, "%s: Instance leaving maintenance because "
1255 "%s.\n", rip
->ri_i
.i_fmri
, cp
);
1257 (void) restarter_instance_update_states(h
, rip
, RESTARTER_STATE_UNINIT
,
1258 RESTARTER_STATE_NONE
, RERR_RESTART
, reason
);
1261 * If we did ADMIN_MAINT_ON_IMMEDIATE, then there might still be
1262 * a primary contract.
1264 if (rip
->ri_i
.i_primary_ctid
== 0)
1267 ctid
= rip
->ri_i
.i_primary_ctid
;
1268 contract_abandon(ctid
);
1269 rip
->ri_i
.i_primary_ctid
= 0;
1272 switch (r
= libscf_fmri_get_instance(h
, rip
->ri_i
.i_fmri
, &inst
)) {
1277 libscf_handle_rebind(h
);
1281 /* Must have been deleted. */
1287 bad_error("libscf_handle_rebind", r
);
1291 r
= restarter_remove_contract(inst
, ctid
, RESTARTER_CONTRACT_PRIMARY
);
1298 if (tries
< ALLOC_RETRY
) {
1299 (void) poll(NULL
, 0, msecs
);
1300 msecs
*= ALLOC_DELAY_MULT
;
1304 uu_die("Insufficient memory.\n");
1308 scf_instance_destroy(inst
);
1309 libscf_handle_rebind(h
);
1319 "Could not remove contract id %lu for %s (%s).\n", ctid
,
1320 rip
->ri_i
.i_fmri
, strerror(r
));
1326 bad_error("restarter_remove_contract", r
);
1329 scf_instance_destroy(inst
);
1334 * Set inst->ri_i.i_enabled. Expects 'e' to be _ENABLE, _DISABLE, or
1335 * _ADMIN_DISABLE. If the event is _ENABLE and inst is uninitialized or
1336 * disabled, move it to offline. If the event is _DISABLE or
1337 * _ADMIN_DISABLE, make sure inst will move to disabled.
1341 * ECONNRESET - h was rebound
1344 enable_inst(scf_handle_t
*h
, restarter_inst_t
*inst
,
1345 restarter_instance_qentry_t
*riq
)
1347 restarter_instance_state_t state
;
1348 restarter_event_type_t e
= riq
->riq_type
;
1349 restarter_str_t reason
= restarter_str_per_configuration
;
1352 assert(MUTEX_HELD(&inst
->ri_lock
));
1353 assert(e
== RESTARTER_EVENT_TYPE_ADMIN_DISABLE
||
1354 e
== RESTARTER_EVENT_TYPE_DISABLE
||
1355 e
== RESTARTER_EVENT_TYPE_ENABLE
);
1356 assert(instance_in_transition(inst
) == 0);
1358 state
= inst
->ri_i
.i_state
;
1360 if (e
== RESTARTER_EVENT_TYPE_ENABLE
) {
1361 inst
->ri_i
.i_enabled
= 1;
1363 if (state
== RESTARTER_STATE_UNINIT
||
1364 state
== RESTARTER_STATE_DISABLED
) {
1366 * B_FALSE: Don't log an error if the log_instance()
1367 * fails because it will fail on the miniroot before
1368 * install-discovery runs.
1370 log_instance(inst
, B_FALSE
, "Enabled.");
1371 log_framework(LOG_DEBUG
, "%s: Instance enabled.\n",
1375 * If we are coming from DISABLED, it was obviously an
1376 * enable request. If we are coming from UNINIT, it may
1377 * have been a sevice in MAINT that was cleared.
1379 if (riq
->riq_reason
== restarter_str_clear_request
)
1380 reason
= restarter_str_clear_request
;
1381 else if (state
== RESTARTER_STATE_DISABLED
)
1382 reason
= restarter_str_enable_request
;
1383 (void) restarter_instance_update_states(h
, inst
,
1384 RESTARTER_STATE_OFFLINE
, RESTARTER_STATE_NONE
,
1387 log_framework(LOG_DEBUG
, "Restarter: "
1388 "Not changing state of %s for enable command.\n",
1392 inst
->ri_i
.i_enabled
= 0;
1395 case RESTARTER_STATE_ONLINE
:
1396 case RESTARTER_STATE_DEGRADED
:
1397 r
= stop_instance(h
, inst
, RSTOP_DISABLE
);
1398 return (r
== ECONNRESET
? 0 : r
);
1400 case RESTARTER_STATE_OFFLINE
:
1401 case RESTARTER_STATE_UNINIT
:
1402 if (inst
->ri_i
.i_primary_ctid
!= 0) {
1403 inst
->ri_m_inst
= safe_scf_instance_create(h
);
1404 inst
->ri_mi_deleted
= B_FALSE
;
1406 libscf_reget_instance(inst
);
1407 method_remove_contract(inst
, B_TRUE
, B_TRUE
);
1409 scf_instance_destroy(inst
->ri_m_inst
);
1411 /* B_FALSE: See log_instance(..., "Enabled."); above */
1412 log_instance(inst
, B_FALSE
, "Disabled.");
1413 log_framework(LOG_DEBUG
, "%s: Instance disabled.\n",
1417 * If we are coming from OFFLINE, it was obviously a
1418 * disable request. But if we are coming from
1419 * UNINIT, it may have been a disable request for a
1422 if (riq
->riq_reason
== restarter_str_disable_request
||
1423 state
== RESTARTER_STATE_OFFLINE
)
1424 reason
= restarter_str_disable_request
;
1425 (void) restarter_instance_update_states(h
, inst
,
1426 RESTARTER_STATE_DISABLED
, RESTARTER_STATE_NONE
,
1427 RERR_RESTART
, reason
);
1430 case RESTARTER_STATE_DISABLED
:
1433 case RESTARTER_STATE_MAINT
:
1435 * We only want to pull the instance out of maintenance
1436 * if the disable is on adminstrative request. The
1437 * graph engine sends _DISABLE events whenever a
1438 * service isn't in the disabled state, and we don't
1439 * want to pull the service out of maintenance if,
1440 * for example, it is there due to a dependency cycle.
1442 if (e
== RESTARTER_EVENT_TYPE_ADMIN_DISABLE
)
1443 unmaintain_instance(h
, inst
, RUNMAINT_DISABLE
);
1448 (void) fprintf(stderr
, "Restarter instance %s has "
1449 "unknown state %d.\n", inst
->ri_i
.i_fmri
, state
);
1459 start_instance(scf_handle_t
*local_handle
, restarter_inst_t
*inst
,
1463 restarter_str_t new_reason
;
1465 assert(MUTEX_HELD(&inst
->ri_lock
));
1466 assert(instance_in_transition(inst
) == 0);
1467 assert(inst
->ri_method_thread
== 0);
1469 log_framework(LOG_DEBUG
, "%s: trying to start instance\n",
1473 * We want to keep the original reason for restarts and clear actions
1476 case restarter_str_restart_request
:
1477 case restarter_str_clear_request
:
1478 new_reason
= reason
;
1481 new_reason
= restarter_str_dependencies_satisfied
;
1484 /* Services in the disabled and maintenance state are ignored */
1485 if (inst
->ri_i
.i_state
== RESTARTER_STATE_MAINT
||
1486 inst
->ri_i
.i_state
== RESTARTER_STATE_DISABLED
||
1487 inst
->ri_i
.i_enabled
== 0) {
1488 log_framework(LOG_DEBUG
,
1489 "%s: start_instance -> is maint/disabled\n",
1494 /* Already started instances are left alone */
1495 if (instance_started(inst
) == 1) {
1496 log_framework(LOG_DEBUG
,
1497 "%s: start_instance -> is already started\n",
1502 log_framework(LOG_DEBUG
, "%s: starting instance.\n", inst
->ri_i
.i_fmri
);
1504 (void) restarter_instance_update_states(local_handle
, inst
,
1505 inst
->ri_i
.i_state
, RESTARTER_STATE_ONLINE
, RERR_NONE
, new_reason
);
1507 info
= startd_zalloc(sizeof (fork_info_t
));
1509 info
->sf_id
= inst
->ri_id
;
1510 info
->sf_method_type
= METHOD_START
;
1511 info
->sf_event_type
= RERR_NONE
;
1512 info
->sf_reason
= new_reason
;
1513 inst
->ri_method_thread
= startd_thread_create(method_thread
, info
);
1517 event_from_tty(scf_handle_t
*h
, restarter_inst_t
*rip
)
1519 scf_instance_t
*inst
;
1522 if (libscf_fmri_get_instance(h
, rip
->ri_i
.i_fmri
, &inst
))
1525 ret
= restarter_inst_ractions_from_tty(inst
);
1527 scf_instance_destroy(inst
);
1532 maintain_instance(scf_handle_t
*h
, restarter_inst_t
*rip
, int immediate
,
1533 restarter_str_t reason
)
1536 scf_instance_t
*scf_inst
= NULL
;
1538 assert(MUTEX_HELD(&rip
->ri_lock
));
1539 assert(reason
!= restarter_str_none
);
1540 assert(rip
->ri_method_thread
== 0);
1542 log_instance(rip
, B_TRUE
, "Stopping for maintenance due to %s.",
1543 restarter_get_str_short(reason
));
1544 log_framework(LOG_DEBUG
, "%s: stopping for maintenance due to %s.\n",
1545 rip
->ri_i
.i_fmri
, restarter_get_str_short(reason
));
1547 /* Services in the maintenance state are ignored */
1548 if (rip
->ri_i
.i_state
== RESTARTER_STATE_MAINT
) {
1549 log_framework(LOG_DEBUG
,
1550 "%s: maintain_instance -> is already in maintenance\n",
1556 * If reason state is restarter_str_service_request and
1557 * restarter_actions/auxiliary_fmri property is set with a valid fmri,
1558 * copy the fmri to restarter/auxiliary_fmri so svcs -x can use.
1560 if (reason
== restarter_str_service_request
&&
1561 libscf_fmri_get_instance(h
, rip
->ri_i
.i_fmri
, &scf_inst
) == 0) {
1562 if (restarter_inst_validate_ractions_aux_fmri(scf_inst
) == 0) {
1563 if (restarter_inst_set_aux_fmri(scf_inst
))
1564 log_framework(LOG_DEBUG
, "%s: "
1565 "restarter_inst_set_aux_fmri failed: ",
1568 log_framework(LOG_DEBUG
, "%s: "
1569 "restarter_inst_validate_ractions_aux_fmri "
1570 "failed: ", rip
->ri_i
.i_fmri
);
1572 if (restarter_inst_reset_aux_fmri(scf_inst
))
1573 log_framework(LOG_DEBUG
, "%s: "
1574 "restarter_inst_reset_aux_fmri failed: ",
1577 scf_instance_destroy(scf_inst
);
1580 if (immediate
|| !instance_started(rip
)) {
1581 if (rip
->ri_i
.i_primary_ctid
!= 0) {
1582 rip
->ri_m_inst
= safe_scf_instance_create(h
);
1583 rip
->ri_mi_deleted
= B_FALSE
;
1585 libscf_reget_instance(rip
);
1586 method_remove_contract(rip
, B_TRUE
, B_TRUE
);
1588 scf_instance_destroy(rip
->ri_m_inst
);
1591 (void) restarter_instance_update_states(h
, rip
,
1592 RESTARTER_STATE_MAINT
, RESTARTER_STATE_NONE
, RERR_RESTART
,
1597 (void) restarter_instance_update_states(h
, rip
, rip
->ri_i
.i_state
,
1598 RESTARTER_STATE_MAINT
, RERR_NONE
, reason
);
1600 log_transition(rip
, MAINT_REQUESTED
);
1602 info
= startd_zalloc(sizeof (*info
));
1603 info
->sf_id
= rip
->ri_id
;
1604 info
->sf_method_type
= METHOD_STOP
;
1605 info
->sf_event_type
= RERR_RESTART
;
1606 info
->sf_reason
= reason
;
1607 rip
->ri_method_thread
= startd_thread_create(method_thread
, info
);
1611 refresh_instance(scf_handle_t
*h
, restarter_inst_t
*rip
)
1613 scf_instance_t
*inst
;
1614 scf_snapshot_t
*snap
;
1618 assert(MUTEX_HELD(&rip
->ri_lock
));
1620 log_instance(rip
, B_TRUE
, "Rereading configuration.");
1621 log_framework(LOG_DEBUG
, "%s: rereading configuration.\n",
1625 r
= libscf_fmri_get_instance(h
, rip
->ri_i
.i_fmri
, &inst
);
1631 libscf_handle_rebind(h
);
1635 /* Must have been deleted. */
1641 bad_error("libscf_fmri_get_instance", r
);
1644 snap
= libscf_get_running_snapshot(inst
);
1646 r
= libscf_get_startd_properties(inst
, snap
, &rip
->ri_flags
,
1647 &rip
->ri_utmpx_prefix
);
1650 log_framework(LOG_DEBUG
, "%s is a %s-style service\n",
1651 rip
->ri_i
.i_fmri
, service_style(rip
->ri_flags
));
1655 scf_instance_destroy(inst
);
1656 scf_snapshot_destroy(snap
);
1657 libscf_handle_rebind(h
);
1662 /* Succeed in anticipation of REMOVE_INSTANCE. */
1666 bad_error("libscf_get_startd_properties", r
);
1669 if (instance_started(rip
)) {
1670 /* Refresh does not change the state. */
1671 (void) restarter_instance_update_states(h
, rip
,
1672 rip
->ri_i
.i_state
, rip
->ri_i
.i_state
, RERR_NONE
,
1673 restarter_str_refresh
);
1675 info
= startd_zalloc(sizeof (*info
));
1676 info
->sf_id
= rip
->ri_id
;
1677 info
->sf_method_type
= METHOD_REFRESH
;
1678 info
->sf_event_type
= RERR_REFRESH
;
1679 info
->sf_reason
= NULL
;
1681 assert(rip
->ri_method_thread
== 0);
1682 rip
->ri_method_thread
=
1683 startd_thread_create(method_thread
, info
);
1686 scf_snapshot_destroy(snap
);
1687 scf_instance_destroy(inst
);
1690 const char *event_names
[] = { "INVALID", "ADD_INSTANCE", "REMOVE_INSTANCE",
1691 "ENABLE", "DISABLE", "ADMIN_DEGRADED", "ADMIN_REFRESH",
1692 "ADMIN_RESTART", "ADMIN_MAINT_OFF", "ADMIN_MAINT_ON",
1693 "ADMIN_MAINT_ON_IMMEDIATE", "STOP", "START", "DEPENDENCY_CYCLE",
1694 "INVALID_DEPENDENCY", "ADMIN_DISABLE", "STOP_RESET"
1698 * void *restarter_process_events()
1700 * Called in a separate thread to process the events on an instance's
1701 * queue. Empties the queue completely, and tries to keep the thread
1702 * around for a little while after the queue is empty to save on
1706 restarter_process_events(void *arg
)
1709 restarter_instance_qentry_t
*event
;
1710 restarter_inst_t
*rip
;
1711 char *fmri
= (char *)arg
;
1714 assert(fmri
!= NULL
);
1716 h
= libscf_handle_create_bound_loop();
1718 /* grab the queue lock */
1719 rip
= inst_lookup_queue(fmri
);
1725 while ((event
= uu_list_first(rip
->ri_queue
)) != NULL
) {
1726 restarter_inst_t
*inst
;
1728 /* drop the queue lock */
1729 MUTEX_UNLOCK(&rip
->ri_queue_lock
);
1732 * Grab the inst lock -- this waits until any outstanding
1733 * method finishes running.
1735 inst
= inst_lookup_by_name(fmri
);
1737 /* Getting deleted in the middle isn't an error. */
1741 assert(instance_in_transition(inst
) == 0);
1743 /* process the event */
1744 switch (event
->riq_type
) {
1745 case RESTARTER_EVENT_TYPE_ENABLE
:
1746 case RESTARTER_EVENT_TYPE_DISABLE
:
1747 (void) enable_inst(h
, inst
, event
);
1750 case RESTARTER_EVENT_TYPE_ADMIN_DISABLE
:
1751 if (enable_inst(h
, inst
, event
) == 0)
1752 reset_start_times(inst
);
1755 case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE
:
1756 restarter_delete_inst(inst
);
1760 case RESTARTER_EVENT_TYPE_STOP_RESET
:
1761 reset_start_times(inst
);
1763 case RESTARTER_EVENT_TYPE_STOP
:
1764 (void) stop_instance(h
, inst
, RSTOP_DEPENDENCY
);
1767 case RESTARTER_EVENT_TYPE_START
:
1768 start_instance(h
, inst
, event
->riq_reason
);
1771 case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE
:
1772 maintain_instance(h
, inst
, 0,
1773 restarter_str_dependency_cycle
);
1776 case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY
:
1777 maintain_instance(h
, inst
, 0,
1778 restarter_str_invalid_dependency
);
1781 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON
:
1782 if (event_from_tty(h
, inst
) == 0)
1783 maintain_instance(h
, inst
, 0,
1784 restarter_str_service_request
);
1786 maintain_instance(h
, inst
, 0,
1787 restarter_str_administrative_request
);
1790 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE
:
1791 if (event_from_tty(h
, inst
) == 0)
1792 maintain_instance(h
, inst
, 1,
1793 restarter_str_service_request
);
1795 maintain_instance(h
, inst
, 1,
1796 restarter_str_administrative_request
);
1799 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF
:
1800 unmaintain_instance(h
, inst
, RUNMAINT_CLEAR
);
1801 reset_start_times(inst
);
1804 case RESTARTER_EVENT_TYPE_ADMIN_REFRESH
:
1805 refresh_instance(h
, inst
);
1808 case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED
:
1809 log_framework(LOG_WARNING
, "Restarter: "
1810 "%s command (for %s) unimplemented.\n",
1811 event_names
[event
->riq_type
], inst
->ri_i
.i_fmri
);
1814 case RESTARTER_EVENT_TYPE_ADMIN_RESTART
:
1815 if (!instance_started(inst
)) {
1816 log_framework(LOG_DEBUG
, "Restarter: "
1817 "Not restarting %s; not running.\n",
1821 * Stop the instance. If it can be restarted,
1822 * the graph engine will send a new event.
1824 if (stop_instance(h
, inst
, RSTOP_RESTART
) == 0)
1825 reset_start_times(inst
);
1829 case RESTARTER_EVENT_TYPE_ADD_INSTANCE
:
1832 uu_warn("%s:%d: Bad restarter event %d. "
1833 "Aborting.\n", __FILE__
, __LINE__
, event
->riq_type
);
1838 assert(inst
!= NULL
);
1839 MUTEX_UNLOCK(&inst
->ri_lock
);
1842 /* grab the queue lock */
1843 rip
= inst_lookup_queue(fmri
);
1847 /* delete the event */
1848 uu_list_remove(rip
->ri_queue
, event
);
1849 startd_free(event
, sizeof (restarter_instance_qentry_t
));
1852 assert(rip
!= NULL
);
1855 * Try to preserve the thread for a little while for future use.
1859 (void) pthread_cond_reltimedwait_np(&rip
->ri_queue_cv
,
1860 &rip
->ri_queue_lock
, &to
);
1862 if (uu_list_first(rip
->ri_queue
) != NULL
)
1865 rip
->ri_queue_thread
= 0;
1866 MUTEX_UNLOCK(&rip
->ri_queue_lock
);
1869 (void) scf_handle_unbind(h
);
1870 scf_handle_destroy(h
);
1876 is_admin_event(restarter_event_type_t t
) {
1879 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON
:
1880 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE
:
1881 case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF
:
1882 case RESTARTER_EVENT_TYPE_ADMIN_REFRESH
:
1883 case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED
:
1884 case RESTARTER_EVENT_TYPE_ADMIN_RESTART
:
1892 restarter_queue_event(restarter_inst_t
*ri
, restarter_protocol_event_t
*e
)
1894 restarter_instance_qentry_t
*qe
;
1897 assert(MUTEX_HELD(&ri
->ri_queue_lock
));
1898 assert(!MUTEX_HELD(&ri
->ri_lock
));
1900 qe
= startd_zalloc(sizeof (restarter_instance_qentry_t
));
1901 qe
->riq_type
= e
->rpe_type
;
1902 qe
->riq_reason
= e
->rpe_reason
;
1904 uu_list_node_init(qe
, &qe
->riq_link
, restarter_queue_pool
);
1905 r
= uu_list_insert_before(ri
->ri_queue
, NULL
, qe
);
1910 * void *restarter_event_thread()
1912 * Handle incoming graph events by placing them on a per-instance
1913 * queue. We can't lock the main part of the instance structure, so
1914 * just modify the seprarately locked event queue portion.
1918 restarter_event_thread(void *unused
)
1923 * This is a new thread, and thus, gets its own handle
1924 * to the repository.
1926 h
= libscf_handle_create_bound_loop();
1928 MUTEX_LOCK(&ru
->restarter_update_lock
);
1932 restarter_protocol_event_t
*e
;
1934 while (ru
->restarter_update_wakeup
== 0)
1935 (void) pthread_cond_wait(&ru
->restarter_update_cv
,
1936 &ru
->restarter_update_lock
);
1938 ru
->restarter_update_wakeup
= 0;
1940 while ((e
= restarter_event_dequeue()) != NULL
) {
1941 restarter_inst_t
*rip
;
1944 MUTEX_UNLOCK(&ru
->restarter_update_lock
);
1947 * ADD_INSTANCE is special: there's likely no
1948 * instance structure yet, so we need to handle the
1949 * addition synchronously.
1951 switch (e
->rpe_type
) {
1952 case RESTARTER_EVENT_TYPE_ADD_INSTANCE
:
1953 if (restarter_insert_inst(h
, e
->rpe_inst
) != 0)
1954 log_error(LOG_INFO
, "Restarter: "
1955 "Could not add %s.\n", e
->rpe_inst
);
1957 MUTEX_LOCK(&st
->st_load_lock
);
1958 if (--st
->st_load_instances
== 0)
1959 (void) pthread_cond_broadcast(
1961 MUTEX_UNLOCK(&st
->st_load_lock
);
1967 * Lookup the instance, locking only the event queue.
1968 * Can't grab ri_lock here because it might be held
1969 * by a long-running method.
1971 rip
= inst_lookup_queue(e
->rpe_inst
);
1973 log_error(LOG_INFO
, "Restarter: "
1974 "Ignoring %s command for unknown service "
1975 "%s.\n", event_names
[e
->rpe_type
],
1980 /* Keep ADMIN events from filling up the queue. */
1981 if (is_admin_event(e
->rpe_type
) &&
1982 uu_list_numnodes(rip
->ri_queue
) >
1983 RINST_QUEUE_THRESHOLD
) {
1984 MUTEX_UNLOCK(&rip
->ri_queue_lock
);
1985 log_instance(rip
, B_TRUE
, "Instance event "
1986 "queue overflow. Dropping administrative "
1988 log_framework(LOG_DEBUG
, "%s: Instance event "
1989 "queue overflow. Dropping administrative "
1990 "request.\n", rip
->ri_i
.i_fmri
);
1994 /* Now add the event to the instance queue. */
1995 restarter_queue_event(rip
, e
);
1997 if (rip
->ri_queue_thread
== 0) {
1999 * Start a thread if one isn't already
2002 fmri
= safe_strdup(e
->rpe_inst
);
2003 rip
->ri_queue_thread
= startd_thread_create(
2004 restarter_process_events
, (void *)fmri
);
2007 * Signal the existing thread that there's
2010 (void) pthread_cond_broadcast(
2014 MUTEX_UNLOCK(&rip
->ri_queue_lock
);
2016 restarter_event_release(e
);
2018 MUTEX_LOCK(&ru
->restarter_update_lock
);
2023 * Unreachable for now -- there's currently no graceful cleanup
2026 (void) scf_handle_unbind(h
);
2027 scf_handle_destroy(h
);
2031 static restarter_inst_t
*
2032 contract_to_inst(ctid_t ctid
)
2034 restarter_inst_t
*inst
;
2037 id
= lookup_inst_by_contract(ctid
);
2041 inst
= inst_lookup_by_id(id
);
2044 * Since ri_lock isn't held by the contract id lookup, this
2045 * instance may have been restarted and now be in a new
2046 * contract, making the old contract no longer valid for this
2049 if (ctid
!= inst
->ri_i
.i_primary_ctid
) {
2050 MUTEX_UNLOCK(&inst
->ri_lock
);
2058 * void contract_action()
2059 * Take action on contract events.
2062 contract_action(scf_handle_t
*h
, restarter_inst_t
*inst
, ctid_t id
,
2065 const char *fmri
= inst
->ri_i
.i_fmri
;
2067 assert(MUTEX_HELD(&inst
->ri_lock
));
2070 * If startd has stopped this contract, there is no need to
2073 if (inst
->ri_i
.i_primary_ctid
> 0 &&
2074 inst
->ri_i
.i_primary_ctid_stopped
)
2077 if ((type
& (CT_PR_EV_EMPTY
| CT_PR_EV_CORE
| CT_PR_EV_SIGNAL
2078 | CT_PR_EV_HWERR
)) == 0) {
2080 * There shouldn't be other events, since that's not how we set
2081 * the terms. Thus, just log an error and drive on.
2083 log_framework(LOG_NOTICE
,
2084 "%s: contract %ld received unexpected critical event "
2085 "(%d)\n", fmri
, id
, type
);
2089 assert(instance_in_transition(inst
) == 0);
2091 if (instance_is_wait_style(inst
)) {
2093 * We ignore all events; if they impact the
2094 * process we're monitoring, then the
2095 * wait_thread will stop the instance.
2097 log_framework(LOG_DEBUG
,
2098 "%s: ignoring contract event on wait-style service\n",
2102 * A CT_PR_EV_EMPTY event is an RSTOP_EXIT request.
2105 case CT_PR_EV_EMPTY
:
2106 (void) stop_instance(h
, inst
, RSTOP_EXIT
);
2109 (void) stop_instance(h
, inst
, RSTOP_CORE
);
2111 case CT_PR_EV_SIGNAL
:
2112 (void) stop_instance(h
, inst
, RSTOP_SIGNAL
);
2114 case CT_PR_EV_HWERR
:
2115 (void) stop_instance(h
, inst
, RSTOP_HWERR
);
2122 * void *restarter_contract_event_thread(void *)
2123 * Listens to the process contract bundle for critical events, taking action
2124 * on events from contracts we know we are responsible for.
2128 restarter_contracts_event_thread(void *unused
)
2131 scf_handle_t
*local_handle
;
2134 * Await graph load completion. That is, stop here, until we've scanned
2135 * the repository for contract - instance associations.
2137 MUTEX_LOCK(&st
->st_load_lock
);
2138 while (!(st
->st_load_complete
&& st
->st_load_instances
== 0))
2139 (void) pthread_cond_wait(&st
->st_load_cv
, &st
->st_load_lock
);
2140 MUTEX_UNLOCK(&st
->st_load_lock
);
2143 * This is a new thread, and thus, gets its own handle
2144 * to the repository.
2146 if ((local_handle
= libscf_handle_create_bound(SCF_VERSION
)) == NULL
)
2147 uu_die("Unable to bind a new repository handle: %s\n",
2148 scf_strerror(scf_error()));
2150 fd
= open64(CTFS_ROOT
"/process/pbundle", O_RDONLY
);
2152 uu_die("process bundle open failed");
2155 * Make sure we get all events (including those generated by configd
2156 * before this thread was started).
2158 err
= ct_event_reset(fd
);
2166 ct_stathdl_t status
;
2168 restarter_inst_t
*inst
;
2171 if (err
= ct_event_read_critical(fd
, &ev
)) {
2172 log_error(LOG_WARNING
,
2173 "Error reading next contract event: %s",
2178 evid
= ct_event_get_evid(ev
);
2179 ctid
= ct_event_get_ctid(ev
);
2180 type
= ct_event_get_type(ev
);
2183 if ((sfd
= contract_open(ctid
, "process", "status", O_RDONLY
))
2189 if (err
= ct_status_read(sfd
, CTD_COMMON
, &status
)) {
2190 log_framework(LOG_WARNING
, "Could not get status for "
2191 "contract %ld: %s\n", ctid
, strerror(err
));
2198 cookie
= ct_status_get_cookie(status
);
2200 log_framework(LOG_DEBUG
, "Received event %d for ctid %ld "
2201 "cookie %lld\n", type
, ctid
, cookie
);
2203 ct_status_free(status
);
2208 * svc.configd(1M) restart handling performed by the
2209 * fork_configd_thread. We don't acknowledge, as that thread
2212 if (cookie
== CONFIGD_COOKIE
) {
2218 if (storing_contract
!= 0 &&
2219 (inst
= contract_to_inst(ctid
)) == NULL
) {
2221 * This can happen for two reasons:
2222 * - method_run() has not yet stored the
2223 * the contract into the internal hash table.
2224 * - we receive an EMPTY event for an abandoned
2226 * If there is any contract in the process of
2227 * being stored into the hash table then re-read
2230 log_framework(LOG_DEBUG
,
2231 "Reset event %d for unknown "
2232 "contract id %ld\n", type
, ctid
);
2234 /* don't go too fast */
2235 (void) poll(NULL
, 0, 100);
2237 (void) ct_event_reset(fd
);
2243 * Do not call contract_to_inst() again if first
2247 inst
= contract_to_inst(ctid
);
2250 * This can happen if we receive an EMPTY
2251 * event for an abandoned contract.
2253 log_framework(LOG_DEBUG
,
2254 "Received event %d for unknown contract id "
2255 "%ld\n", type
, ctid
);
2257 log_framework(LOG_DEBUG
,
2258 "Received event %d for contract id "
2259 "%ld (%s)\n", type
, ctid
,
2262 contract_action(local_handle
, inst
, ctid
, type
);
2264 MUTEX_UNLOCK(&inst
->ri_lock
);
2267 efd
= contract_open(ct_event_get_ctid(ev
), "process", "ctl",
2270 (void) ct_ctl_ack(efd
, evid
);
2283 * Timeout queue, processed by restarter_timeouts_event_thread().
2285 timeout_queue_t
*timeouts
;
2286 static uu_list_pool_t
*timeout_pool
;
2288 typedef struct timeout_update
{
2289 pthread_mutex_t tu_lock
;
2290 pthread_cond_t tu_cv
;
2294 timeout_update_t
*tu
;
2296 static const char *timeout_ovr_svcs
[] = {
2297 "svc:/system/manifest-import:default",
2298 "svc:/network/initial:default",
2299 "svc:/network/service:default",
2300 "svc:/system/rmtmpfiles:default",
2301 "svc:/network/loopback:default",
2302 "svc:/network/physical:default",
2303 "svc:/system/device/local:default",
2304 "svc:/system/metainit:default",
2305 "svc:/system/filesystem/usr:default",
2306 "svc:/system/filesystem/minimal:default",
2307 "svc:/system/filesystem/local:default",
2312 is_timeout_ovr(restarter_inst_t
*inst
)
2316 for (i
= 0; timeout_ovr_svcs
[i
] != NULL
; ++i
) {
2317 if (strcmp(inst
->ri_i
.i_fmri
, timeout_ovr_svcs
[i
]) == 0) {
2318 log_instance(inst
, B_TRUE
, "Timeout override by "
2319 "svc.startd. Using infinite timeout.");
2329 timeout_compare(const void *lc_arg
, const void *rc_arg
, void *private)
2331 hrtime_t t1
= ((const timeout_entry_t
*)lc_arg
)->te_timeout
;
2332 hrtime_t t2
= ((const timeout_entry_t
*)rc_arg
)->te_timeout
;
2344 timeouts
= startd_zalloc(sizeof (timeout_queue_t
));
2346 (void) pthread_mutex_init(&timeouts
->tq_lock
, &mutex_attrs
);
2348 timeout_pool
= startd_list_pool_create("timeouts",
2349 sizeof (timeout_entry_t
), offsetof(timeout_entry_t
, te_link
),
2350 timeout_compare
, UU_LIST_POOL_DEBUG
);
2351 assert(timeout_pool
!= NULL
);
2353 timeouts
->tq_list
= startd_list_create(timeout_pool
,
2354 timeouts
, UU_LIST_SORTED
);
2355 assert(timeouts
->tq_list
!= NULL
);
2357 tu
= startd_zalloc(sizeof (timeout_update_t
));
2358 (void) pthread_cond_init(&tu
->tu_cv
, NULL
);
2359 (void) pthread_mutex_init(&tu
->tu_lock
, &mutex_attrs
);
2363 timeout_insert(restarter_inst_t
*inst
, ctid_t cid
, uint64_t timeout_sec
)
2365 hrtime_t now
, timeout
;
2366 timeout_entry_t
*entry
;
2367 uu_list_index_t idx
;
2369 assert(MUTEX_HELD(&inst
->ri_lock
));
2374 * If we overflow LLONG_MAX, we're never timing out anyways, so
2377 if (timeout_sec
>= (LLONG_MAX
- now
) / 1000000000LL) {
2378 log_instance(inst
, B_TRUE
, "timeout_seconds too large, "
2379 "treating as infinite.");
2383 /* hrtime is in nanoseconds. Convert timeout_sec. */
2384 timeout
= now
+ (timeout_sec
* 1000000000LL);
2386 entry
= startd_alloc(sizeof (timeout_entry_t
));
2387 entry
->te_timeout
= timeout
;
2388 entry
->te_ctid
= cid
;
2389 entry
->te_fmri
= safe_strdup(inst
->ri_i
.i_fmri
);
2390 entry
->te_logstem
= safe_strdup(inst
->ri_logstem
);
2391 entry
->te_fired
= 0;
2392 /* Insert the calculated timeout time onto the queue. */
2393 MUTEX_LOCK(&timeouts
->tq_lock
);
2394 (void) uu_list_find(timeouts
->tq_list
, entry
, NULL
, &idx
);
2395 uu_list_node_init(entry
, &entry
->te_link
, timeout_pool
);
2396 uu_list_insert(timeouts
->tq_list
, entry
, idx
);
2397 MUTEX_UNLOCK(&timeouts
->tq_lock
);
2399 assert(inst
->ri_timeout
== NULL
);
2400 inst
->ri_timeout
= entry
;
2402 MUTEX_LOCK(&tu
->tu_lock
);
2404 (void) pthread_cond_broadcast(&tu
->tu_cv
);
2405 MUTEX_UNLOCK(&tu
->tu_lock
);
2410 timeout_remove(restarter_inst_t
*inst
, ctid_t cid
)
2412 assert(MUTEX_HELD(&inst
->ri_lock
));
2414 if (inst
->ri_timeout
== NULL
)
2417 assert(inst
->ri_timeout
->te_ctid
== cid
);
2419 MUTEX_LOCK(&timeouts
->tq_lock
);
2420 uu_list_remove(timeouts
->tq_list
, inst
->ri_timeout
);
2421 MUTEX_UNLOCK(&timeouts
->tq_lock
);
2423 free(inst
->ri_timeout
->te_fmri
);
2424 free(inst
->ri_timeout
->te_logstem
);
2425 startd_free(inst
->ri_timeout
, sizeof (timeout_entry_t
));
2426 inst
->ri_timeout
= NULL
;
2439 * Walk through the (sorted) timeouts list. While the timeout
2440 * at the head of the list is <= the current time, kill the
2443 MUTEX_LOCK(&timeouts
->tq_lock
);
2445 for (e
= uu_list_first(timeouts
->tq_list
);
2446 e
!= NULL
&& e
->te_timeout
<= now
;
2447 e
= uu_list_next(timeouts
->tq_list
, e
)) {
2448 log_framework(LOG_WARNING
, "%s: Method or service exit timed "
2449 "out. Killing contract %ld.\n", e
->te_fmri
, e
->te_ctid
);
2450 log_instance_fmri(e
->te_fmri
, e
->te_logstem
, B_TRUE
,
2451 "Method or service exit timed out. Killing contract %ld.",
2454 (void) contract_kill(e
->te_ctid
, SIGKILL
, e
->te_fmri
);
2457 if (uu_list_numnodes(timeouts
->tq_list
) > 0)
2462 MUTEX_UNLOCK(&timeouts
->tq_lock
);
2468 * void *restarter_timeouts_event_thread(void *)
2469 * Responsible for monitoring the method timeouts. This thread must
2470 * be started before any methods are called.
2474 restarter_timeouts_event_thread(void *unused
)
2477 * Timeouts are entered on a priority queue, which is processed by
2478 * this thread. As timeouts are specified in seconds, we'll do
2479 * the necessary processing every second, as long as the queue
2486 * As long as the timeout list isn't empty, process it
2489 if (timeout_now() == 0) {
2494 /* The list is empty, wait until we have more timeouts. */
2495 MUTEX_LOCK(&tu
->tu_lock
);
2497 while (tu
->tu_wakeup
== 0)
2498 (void) pthread_cond_wait(&tu
->tu_cv
, &tu
->tu_lock
);
2501 MUTEX_UNLOCK(&tu
->tu_lock
);
2510 (void) startd_thread_create(restarter_timeouts_event_thread
, NULL
);
2511 (void) startd_thread_create(restarter_event_thread
, NULL
);
2512 (void) startd_thread_create(restarter_contracts_event_thread
, NULL
);
2513 (void) startd_thread_create(wait_thread
, NULL
);
2520 restarter_instance_pool
= startd_list_pool_create("restarter_instances",
2521 sizeof (restarter_inst_t
), offsetof(restarter_inst_t
,
2522 ri_link
), restarter_instance_compare
, UU_LIST_POOL_DEBUG
);
2523 (void) memset(&instance_list
, 0, sizeof (instance_list
));
2525 (void) pthread_mutex_init(&instance_list
.ril_lock
, &mutex_attrs
);
2526 instance_list
.ril_instance_list
= startd_list_create(
2527 restarter_instance_pool
, &instance_list
, UU_LIST_SORTED
);
2529 restarter_queue_pool
= startd_list_pool_create(
2530 "restarter_instance_queue", sizeof (restarter_instance_qentry_t
),
2531 offsetof(restarter_instance_qentry_t
, riq_link
), NULL
,
2532 UU_LIST_POOL_DEBUG
);
2534 contract_list_pool
= startd_list_pool_create(
2535 "contract_list", sizeof (contract_entry_t
),
2536 offsetof(contract_entry_t
, ce_link
), NULL
,
2537 UU_LIST_POOL_DEBUG
);
2538 contract_hash_init();
2540 log_framework(LOG_DEBUG
, "Initialized restarter\n");