xref: /illumos-gate/usr/src/cmd/svc/startd/specials.c (revision bbf21555)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
521d7f835Sgm  * Common Development and Distribution License (the "License").
621d7f835Sgm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2292ba7109Seschrock  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * specials.c - knowledge of special services
287c478bd9Sstevel@tonic-gate  *
29*bbf21555SRichard Lowe  * svc.startd(8) has duties that cannot be carried out without knowledge of the
307c478bd9Sstevel@tonic-gate  * transition of various services, such as the milestones, to their online
317c478bd9Sstevel@tonic-gate  * states.  Hooks are called with the restarter instance's ri_lock held, so
327c478bd9Sstevel@tonic-gate  * operations on all instances (or on the graph) should be performed
337c478bd9Sstevel@tonic-gate  * asynchronously.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <assert.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <libintl.h>
417c478bd9Sstevel@tonic-gate #include <limits.h>
427c478bd9Sstevel@tonic-gate #include <locale.h>
437c478bd9Sstevel@tonic-gate #include <pthread.h>
447c478bd9Sstevel@tonic-gate #include <signal.h>
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include <strings.h>
487c478bd9Sstevel@tonic-gate #include <time.h>
497c478bd9Sstevel@tonic-gate #include <zone.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include "startd.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate void
special_null_transition()547c478bd9Sstevel@tonic-gate special_null_transition()
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static void
special_fsroot_post_online()597c478bd9Sstevel@tonic-gate special_fsroot_post_online()
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	static int once;
627c478bd9Sstevel@tonic-gate 	char *locale;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
657c478bd9Sstevel@tonic-gate 	 * /usr, with timezone and locale data, is now available.
667c478bd9Sstevel@tonic-gate 	 */
677c478bd9Sstevel@tonic-gate 	if (!st->st_log_timezone_known) {
687c478bd9Sstevel@tonic-gate 		tzset();
697c478bd9Sstevel@tonic-gate 		st->st_log_timezone_known = 1;
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (!st->st_log_locale_known) {
73237ad99bSjwadams 		locale = st->st_locale;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 		(void) setlocale(LC_ALL, "");
767c478bd9Sstevel@tonic-gate 		st->st_locale = setlocale(LC_MESSAGES, "");
777c478bd9Sstevel@tonic-gate 		if (st->st_locale) {
787c478bd9Sstevel@tonic-gate 			st->st_locale = safe_strdup(st->st_locale);
797c478bd9Sstevel@tonic-gate 			xstr_sanitize(st->st_locale);
807c478bd9Sstevel@tonic-gate 			free(locale);
817c478bd9Sstevel@tonic-gate 		} else {
827c478bd9Sstevel@tonic-gate 			st->st_locale = locale;
837c478bd9Sstevel@tonic-gate 		}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 		(void) textdomain(TEXT_DOMAIN);
867c478bd9Sstevel@tonic-gate 		st->st_log_locale_known = 1;
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (once)
907c478bd9Sstevel@tonic-gate 		return;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * ctime(3C) ends with '\n\0'.
947c478bd9Sstevel@tonic-gate 	 */
957c478bd9Sstevel@tonic-gate 	once++;
967c478bd9Sstevel@tonic-gate 	log_framework(LOG_INFO, "system start time was %s",
977c478bd9Sstevel@tonic-gate 	    ctime(&st->st_start_time.tv_sec));
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static void
special_fsminimal_post_online(void)1011af98250Seschrock special_fsminimal_post_online(void)
1027c478bd9Sstevel@tonic-gate {
1031af98250Seschrock 	ulong_t rfsid, fsid;
1047c478bd9Sstevel@tonic-gate 	pid_t init_pid;
1051af98250Seschrock 	int ret;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "special_fsminimal_post_online hook "
1087c478bd9Sstevel@tonic-gate 	    "executed\n");
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1111af98250Seschrock 	 * If /var is still read-only, and it is on a separate filesystem, then
1121af98250Seschrock 	 * attempt to mount it read-write now.
1137c478bd9Sstevel@tonic-gate 	 */
1141af98250Seschrock 	if ((ret = fs_is_read_only("/var", &fsid)) == 1) {
1151af98250Seschrock 		(void) fs_is_read_only("/", &rfsid);
1167c478bd9Sstevel@tonic-gate 
1171af98250Seschrock 		if (rfsid != fsid) {
1187c478bd9Sstevel@tonic-gate 			log_framework(LOG_WARNING, "/var filesystem "
1197c478bd9Sstevel@tonic-gate 			    "read-only after system/filesystem/minimal\n");
1207c478bd9Sstevel@tonic-gate 			if (fs_remount("/var"))
1217c478bd9Sstevel@tonic-gate 				log_framework(LOG_WARNING, "/var "
12292ba7109Seschrock 				    "filesystem remount failed\n");
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1261af98250Seschrock 	if ((ret = fs_is_read_only("/var", &fsid)) != 1) {
1271af98250Seschrock 		if (ret != 0)
1281af98250Seschrock 			log_error(LOG_WARNING, gettext("couldn't check status "
1291af98250Seschrock 			    "of /var filesystem: %s\n"), strerror(errno));
1301af98250Seschrock 
1311af98250Seschrock 		/*
1321af98250Seschrock 		 * Clear (dead) entries and record boot time.
1331af98250Seschrock 		 */
1341af98250Seschrock 		utmpx_clear_old();
1351af98250Seschrock 		utmpx_write_boottime();
1361af98250Seschrock 
1371af98250Seschrock 		/*
1381af98250Seschrock 		 * Reinitialize the logs to point to LOG_PREFIX_NORMAL.
1391af98250Seschrock 		 */
1401af98250Seschrock 		log_init();
1411af98250Seschrock 
1421af98250Seschrock 		/*
1431af98250Seschrock 		 * Poke init so it will create /var/run/initpipe.
1441af98250Seschrock 		 */
1451af98250Seschrock 		if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
1461af98250Seschrock 		    sizeof (init_pid)) != sizeof (init_pid)) {
1471af98250Seschrock 			log_error(LOG_WARNING, "Could not get pid of init: "
1481af98250Seschrock 			    "%s.\n", strerror(errno));
1491af98250Seschrock 		} else {
1501af98250Seschrock 			if (kill(init_pid, SIGHUP) != 0) {
1511af98250Seschrock 				switch (errno) {
1521af98250Seschrock 				case EPERM:
1531af98250Seschrock 				case ESRCH:
1541af98250Seschrock 					log_error(LOG_WARNING,
1551af98250Seschrock 					    "Could not signal init: %s.\n",
1561af98250Seschrock 					    strerror(errno));
1571af98250Seschrock 					break;
1581af98250Seschrock 
1591af98250Seschrock 				case EINVAL:
1601af98250Seschrock 				default:
1611af98250Seschrock 					bad_error("kill", errno);
1621af98250Seschrock 				}
1637c478bd9Sstevel@tonic-gate 			}
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1671af98250Seschrock 	if ((ret = fs_is_read_only("/etc/svc", &fsid)) != 1) {
1681af98250Seschrock 		if (ret != 0)
1691af98250Seschrock 			log_error(LOG_WARNING, gettext("couldn't check status "
1701af98250Seschrock 			    "of /etc/svc filesystem: %s\n"), strerror(errno));
1711af98250Seschrock 
1721af98250Seschrock 		/*
1731af98250Seschrock 		 * Take pending snapshots and create a svc.startd instance.
1741af98250Seschrock 		 */
1751af98250Seschrock 		(void) startd_thread_create(restarter_post_fsminimal_thread,
1761af98250Seschrock 		    NULL);
1771af98250Seschrock 	}
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static void
special_single_post_online(void)1811af98250Seschrock special_single_post_online(void)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	int r;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "special_single_post_online hook executed\n");
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/*
1887c478bd9Sstevel@tonic-gate 	 * Un-set the special reconfig reboot property.
1897c478bd9Sstevel@tonic-gate 	 */
1907c478bd9Sstevel@tonic-gate 	r = libscf_set_reconfig(0);
1917c478bd9Sstevel@tonic-gate 	switch (r) {
1927c478bd9Sstevel@tonic-gate 	case 0:
1937c478bd9Sstevel@tonic-gate 	case ENOENT:
1947c478bd9Sstevel@tonic-gate 		break;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	case EPERM:
1977c478bd9Sstevel@tonic-gate 	case EACCES:
1987c478bd9Sstevel@tonic-gate 	case EROFS:
1997c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Could not clear reconfiguration "
2007c478bd9Sstevel@tonic-gate 		    "property: %s.\n", strerror(r));
2017c478bd9Sstevel@tonic-gate 		break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	default:
2047c478bd9Sstevel@tonic-gate 		bad_error("libscf_set_reconfig", r);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (booting_to_single_user)
2087c478bd9Sstevel@tonic-gate 		(void) startd_thread_create(single_user_thread, NULL);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static service_hook_assn_t special_svcs[] = {
2127c478bd9Sstevel@tonic-gate 	{ "svc:/system/filesystem/root:default",
2137c478bd9Sstevel@tonic-gate 		special_null_transition,
2147c478bd9Sstevel@tonic-gate 		special_fsroot_post_online,
2157c478bd9Sstevel@tonic-gate 		special_null_transition },
2167c478bd9Sstevel@tonic-gate 	{ "svc:/system/filesystem/minimal:default",
2177c478bd9Sstevel@tonic-gate 		special_null_transition,
2187c478bd9Sstevel@tonic-gate 		special_fsminimal_post_online,
2197c478bd9Sstevel@tonic-gate 		special_null_transition },
2207c478bd9Sstevel@tonic-gate 	{ "svc:/milestone/single-user:default",
2217c478bd9Sstevel@tonic-gate 		special_null_transition,
2227c478bd9Sstevel@tonic-gate 		special_single_post_online,
2237c478bd9Sstevel@tonic-gate 		special_null_transition },
2247c478bd9Sstevel@tonic-gate };
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate void
special_online_hooks_get(const char * fmri,instance_hook_t * pre_onp,instance_hook_t * post_onp,instance_hook_t * post_offp)2277c478bd9Sstevel@tonic-gate special_online_hooks_get(const char *fmri, instance_hook_t *pre_onp,
2287c478bd9Sstevel@tonic-gate     instance_hook_t *post_onp, instance_hook_t *post_offp)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	int i;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (special_svcs) / sizeof (service_hook_assn_t);
2337c478bd9Sstevel@tonic-gate 	    i++)
2347c478bd9Sstevel@tonic-gate 		if (strcmp(fmri, special_svcs[i].sh_fmri) == 0) {
2357c478bd9Sstevel@tonic-gate 			*pre_onp = special_svcs[i].sh_pre_online_hook;
2367c478bd9Sstevel@tonic-gate 			*post_onp = special_svcs[i].sh_post_online_hook;
23721d7f835Sgm 			*post_offp = special_svcs[i].sh_post_offline_hook;
2387c478bd9Sstevel@tonic-gate 			return;
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	*pre_onp = *post_onp = *post_offp = special_null_transition;
2427c478bd9Sstevel@tonic-gate }
243