xref: /onnv-gate/usr/src/cmd/svc/startd/specials.c (revision 5345:44060de1d838)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51273Sgm149974  * Common Development and Distribution License (the "License").
61273Sgm149974  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
225017Seschrock  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * specials.c - knowledge of special services
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * svc.startd(1M) has duties that cannot be carried out without knowledge of the
320Sstevel@tonic-gate  * transition of various services, such as the milestones, to their online
330Sstevel@tonic-gate  * states.  Hooks are called with the restarter instance's ri_lock held, so
340Sstevel@tonic-gate  * operations on all instances (or on the graph) should be performed
350Sstevel@tonic-gate  * asynchronously.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <sys/statvfs.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <assert.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <libintl.h>
430Sstevel@tonic-gate #include <limits.h>
440Sstevel@tonic-gate #include <locale.h>
450Sstevel@tonic-gate #include <pthread.h>
460Sstevel@tonic-gate #include <signal.h>
470Sstevel@tonic-gate #include <stdio.h>
480Sstevel@tonic-gate #include <string.h>
490Sstevel@tonic-gate #include <strings.h>
500Sstevel@tonic-gate #include <time.h>
510Sstevel@tonic-gate #include <zone.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include "startd.h"
540Sstevel@tonic-gate 
550Sstevel@tonic-gate void
special_null_transition()560Sstevel@tonic-gate special_null_transition()
570Sstevel@tonic-gate {
580Sstevel@tonic-gate }
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static void
special_fsroot_post_online()610Sstevel@tonic-gate special_fsroot_post_online()
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	static int once;
640Sstevel@tonic-gate 	char *locale;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * /usr, with timezone and locale data, is now available.
680Sstevel@tonic-gate 	 */
690Sstevel@tonic-gate 	if (!st->st_log_timezone_known) {
700Sstevel@tonic-gate 		tzset();
710Sstevel@tonic-gate 		st->st_log_timezone_known = 1;
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if (!st->st_log_locale_known) {
75724Sjwadams 		locale = st->st_locale;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 		(void) setlocale(LC_ALL, "");
780Sstevel@tonic-gate 		st->st_locale = setlocale(LC_MESSAGES, "");
790Sstevel@tonic-gate 		if (st->st_locale) {
800Sstevel@tonic-gate 			st->st_locale = safe_strdup(st->st_locale);
810Sstevel@tonic-gate 			xstr_sanitize(st->st_locale);
820Sstevel@tonic-gate 			free(locale);
830Sstevel@tonic-gate 		} else {
840Sstevel@tonic-gate 			st->st_locale = locale;
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 		(void) textdomain(TEXT_DOMAIN);
880Sstevel@tonic-gate 		st->st_log_locale_known = 1;
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (once)
920Sstevel@tonic-gate 		return;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	/*
950Sstevel@tonic-gate 	 * ctime(3C) ends with '\n\0'.
960Sstevel@tonic-gate 	 */
970Sstevel@tonic-gate 	once++;
980Sstevel@tonic-gate 	log_framework(LOG_INFO, "system start time was %s",
990Sstevel@tonic-gate 	    ctime(&st->st_start_time.tv_sec));
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static void
special_fsminimal_post_online(void)103*5345Seschrock special_fsminimal_post_online(void)
1040Sstevel@tonic-gate {
105*5345Seschrock 	ulong_t rfsid, fsid;
1060Sstevel@tonic-gate 	pid_t init_pid;
107*5345Seschrock 	int ret;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "special_fsminimal_post_online hook "
1100Sstevel@tonic-gate 	    "executed\n");
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	/*
113*5345Seschrock 	 * If /var is still read-only, and it is on a separate filesystem, then
114*5345Seschrock 	 * attempt to mount it read-write now.
1150Sstevel@tonic-gate 	 */
116*5345Seschrock 	if ((ret = fs_is_read_only("/var", &fsid)) == 1) {
117*5345Seschrock 		(void) fs_is_read_only("/", &rfsid);
1180Sstevel@tonic-gate 
119*5345Seschrock 		if (rfsid != fsid) {
1200Sstevel@tonic-gate 			log_framework(LOG_WARNING, "/var filesystem "
1210Sstevel@tonic-gate 			    "read-only after system/filesystem/minimal\n");
1220Sstevel@tonic-gate 			if (fs_remount("/var"))
1230Sstevel@tonic-gate 				log_framework(LOG_WARNING, "/var "
1245017Seschrock 				    "filesystem remount failed\n");
1250Sstevel@tonic-gate 		}
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
128*5345Seschrock 	if ((ret = fs_is_read_only("/var", &fsid)) != 1) {
129*5345Seschrock 		if (ret != 0)
130*5345Seschrock 			log_error(LOG_WARNING, gettext("couldn't check status "
131*5345Seschrock 			    "of /var filesystem: %s\n"), strerror(errno));
1320Sstevel@tonic-gate 
133*5345Seschrock 		/*
134*5345Seschrock 		 * Clear (dead) entries and record boot time.
135*5345Seschrock 		 */
136*5345Seschrock 		utmpx_clear_old();
137*5345Seschrock 		utmpx_write_boottime();
138*5345Seschrock 
139*5345Seschrock 		/*
140*5345Seschrock 		 * Reinitialize the logs to point to LOG_PREFIX_NORMAL.
141*5345Seschrock 		 */
142*5345Seschrock 		log_init();
1430Sstevel@tonic-gate 
144*5345Seschrock 		/*
145*5345Seschrock 		 * Poke init so it will create /var/run/initpipe.
146*5345Seschrock 		 */
147*5345Seschrock 		if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
148*5345Seschrock 		    sizeof (init_pid)) != sizeof (init_pid)) {
149*5345Seschrock 			log_error(LOG_WARNING, "Could not get pid of init: "
150*5345Seschrock 			    "%s.\n", strerror(errno));
151*5345Seschrock 		} else {
152*5345Seschrock 			if (kill(init_pid, SIGHUP) != 0) {
153*5345Seschrock 				switch (errno) {
154*5345Seschrock 				case EPERM:
155*5345Seschrock 				case ESRCH:
156*5345Seschrock 					log_error(LOG_WARNING,
157*5345Seschrock 					    "Could not signal init: %s.\n",
158*5345Seschrock 					    strerror(errno));
159*5345Seschrock 					break;
1600Sstevel@tonic-gate 
161*5345Seschrock 				case EINVAL:
162*5345Seschrock 				default:
163*5345Seschrock 					bad_error("kill", errno);
164*5345Seschrock 				}
1650Sstevel@tonic-gate 			}
1660Sstevel@tonic-gate 		}
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
169*5345Seschrock 	if ((ret = fs_is_read_only("/etc/svc", &fsid)) != 1) {
170*5345Seschrock 		if (ret != 0)
171*5345Seschrock 			log_error(LOG_WARNING, gettext("couldn't check status "
172*5345Seschrock 			    "of /etc/svc filesystem: %s\n"), strerror(errno));
173*5345Seschrock 
174*5345Seschrock 		/*
175*5345Seschrock 		 * Take pending snapshots and create a svc.startd instance.
176*5345Seschrock 		 */
177*5345Seschrock 		(void) startd_thread_create(restarter_post_fsminimal_thread,
178*5345Seschrock 		    NULL);
179*5345Seschrock 	}
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate static void
special_single_post_online(void)183*5345Seschrock special_single_post_online(void)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	int r;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "special_single_post_online hook executed\n");
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * Un-set the special reconfig reboot property.
1910Sstevel@tonic-gate 	 */
1920Sstevel@tonic-gate 	r = libscf_set_reconfig(0);
1930Sstevel@tonic-gate 	switch (r) {
1940Sstevel@tonic-gate 	case 0:
1950Sstevel@tonic-gate 	case ENOENT:
1960Sstevel@tonic-gate 		break;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	case EPERM:
1990Sstevel@tonic-gate 	case EACCES:
2000Sstevel@tonic-gate 	case EROFS:
2010Sstevel@tonic-gate 		log_error(LOG_WARNING, "Could not clear reconfiguration "
2020Sstevel@tonic-gate 		    "property: %s.\n", strerror(r));
2030Sstevel@tonic-gate 		break;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	default:
2060Sstevel@tonic-gate 		bad_error("libscf_set_reconfig", r);
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	if (booting_to_single_user)
2100Sstevel@tonic-gate 		(void) startd_thread_create(single_user_thread, NULL);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate static service_hook_assn_t special_svcs[] = {
2140Sstevel@tonic-gate 	{ "svc:/system/filesystem/root:default",
2150Sstevel@tonic-gate 		special_null_transition,
2160Sstevel@tonic-gate 		special_fsroot_post_online,
2170Sstevel@tonic-gate 		special_null_transition },
2180Sstevel@tonic-gate 	{ "svc:/system/filesystem/minimal:default",
2190Sstevel@tonic-gate 		special_null_transition,
2200Sstevel@tonic-gate 		special_fsminimal_post_online,
2210Sstevel@tonic-gate 		special_null_transition },
2220Sstevel@tonic-gate 	{ "svc:/milestone/single-user:default",
2230Sstevel@tonic-gate 		special_null_transition,
2240Sstevel@tonic-gate 		special_single_post_online,
2250Sstevel@tonic-gate 		special_null_transition },
2260Sstevel@tonic-gate };
2270Sstevel@tonic-gate 
2280Sstevel@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)2290Sstevel@tonic-gate special_online_hooks_get(const char *fmri, instance_hook_t *pre_onp,
2300Sstevel@tonic-gate     instance_hook_t *post_onp, instance_hook_t *post_offp)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	int i;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	for (i = 0; i < sizeof (special_svcs) / sizeof (service_hook_assn_t);
2350Sstevel@tonic-gate 	    i++)
2360Sstevel@tonic-gate 		if (strcmp(fmri, special_svcs[i].sh_fmri) == 0) {
2370Sstevel@tonic-gate 			*pre_onp = special_svcs[i].sh_pre_online_hook;
2380Sstevel@tonic-gate 			*post_onp = special_svcs[i].sh_post_online_hook;
2391273Sgm149974 			*post_offp = special_svcs[i].sh_post_offline_hook;
2400Sstevel@tonic-gate 			return;
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	*pre_onp = *post_onp = *post_offp = special_null_transition;
2440Sstevel@tonic-gate }
245