xref: /titanic_44/usr/src/uts/common/syscall/uadmin.c (revision 3441f6a1af86b9b2f883f3323bf02c9dd0f7a94d)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/sysmacros.h>
32 #include <sys/systm.h>
33 #include <sys/errno.h>
34 #include <sys/vfs.h>
35 #include <sys/vnode.h>
36 #include <sys/swap.h>
37 #include <sys/file.h>
38 #include <sys/proc.h>
39 #include <sys/var.h>
40 #include <sys/uadmin.h>
41 #include <sys/signal.h>
42 #include <sys/time.h>
43 #include <vm/seg_kmem.h>
44 #include <sys/modctl.h>
45 #include <sys/callb.h>
46 #include <sys/dumphdr.h>
47 #include <sys/debug.h>
48 #include <sys/ftrace.h>
49 #include <sys/cmn_err.h>
50 #include <sys/panic.h>
51 #include <sys/ddi.h>
52 #include <sys/sunddi.h>
53 #include <sys/policy.h>
54 #include <sys/zone.h>
55 #include <sys/condvar.h>
56 #include <sys/thread.h>
57 
58 /*
59  * Administrivia system call.  We provide this in two flavors: one for calling
60  * from the system call path (uadmin), and the other for calling from elsewhere
61  * within the kernel (kadmin).  Callers must beware that certain uadmin cmd
62  * values (specifically A_SWAPCTL) are only supported by uadmin and not kadmin.
63  */
64 
65 extern ksema_t fsflush_sema;
66 kmutex_t ualock;
67 kcondvar_t uacond;
68 kthread_t *ua_shutdown_thread = NULL;
69 
70 int sys_shutdown = 0;
71 
72 /*
73  * Kill all user processes in said zone.  A special argument of ALL_ZONES is
74  * passed in when the system as a whole is shutting down.  The lack of per-zone
75  * process lists is likely to make the following a performance bottleneck on a
76  * system with many zones.
77  */
78 void
79 killall(zoneid_t zoneid)
80 {
81 	proc_t *p;
82 
83 	ASSERT(zoneid != GLOBAL_ZONEID);
84 	/*
85 	 * Kill all processes except kernel daemons and ourself.
86 	 * Make a first pass to stop all processes so they won't
87 	 * be trying to restart children as we kill them.
88 	 */
89 	mutex_enter(&pidlock);
90 	for (p = practive; p != NULL; p = p->p_next) {
91 		if ((zoneid == ALL_ZONES || p->p_zone->zone_id == zoneid) &&
92 		    p->p_exec != NULLVP &&	/* kernel daemons */
93 		    p->p_as != &kas &&
94 		    p->p_stat != SZOMB) {
95 			mutex_enter(&p->p_lock);
96 			p->p_flag |= SNOWAIT;
97 			sigtoproc(p, NULL, SIGSTOP);
98 			mutex_exit(&p->p_lock);
99 		}
100 	}
101 	p = practive;
102 	while (p != NULL) {
103 		if ((zoneid == ALL_ZONES || p->p_zone->zone_id == zoneid) &&
104 		    p->p_exec != NULLVP &&	/* kernel daemons */
105 		    p->p_as != &kas &&
106 		    p->p_stat != SIDL &&
107 		    p->p_stat != SZOMB) {
108 			mutex_enter(&p->p_lock);
109 			if (sigismember(&p->p_sig, SIGKILL)) {
110 				mutex_exit(&p->p_lock);
111 				p = p->p_next;
112 			} else {
113 				sigtoproc(p, NULL, SIGKILL);
114 				mutex_exit(&p->p_lock);
115 				(void) cv_timedwait(&p->p_srwchan_cv, &pidlock,
116 				    lbolt + hz);
117 				p = practive;
118 			}
119 		} else {
120 			p = p->p_next;
121 		}
122 	}
123 	mutex_exit(&pidlock);
124 }
125 
126 int
127 kadmin(int cmd, int fcn, void *mdep, cred_t *credp)
128 {
129 	int error = 0;
130 	char *buf;
131 	size_t buflen = 0;
132 	boolean_t invoke_cb = B_FALSE;
133 
134 	/*
135 	 * We might be called directly by the kernel's fault-handling code, so
136 	 * we can't assert that the caller is in the global zone.
137 	 */
138 
139 	/*
140 	 * Make sure that cmd is one of the valid <sys/uadmin.h> command codes
141 	 * and that we have appropriate privileges for this action.
142 	 */
143 	switch (cmd) {
144 	case A_FTRACE:
145 	case A_SHUTDOWN:
146 	case A_REBOOT:
147 	case A_REMOUNT:
148 	case A_FREEZE:
149 	case A_DUMP:
150 		if (secpolicy_sys_config(credp, B_FALSE) != 0)
151 			return (EPERM);
152 		break;
153 
154 	default:
155 		return (EINVAL);
156 	}
157 
158 	/*
159 	 * Serialize these operations on ualock.  If it is held, the
160 	 * system should shutdown, reboot, or remount shortly, unless there is
161 	 * an error.  We need a cv rather than just a mutex because proper
162 	 * functioning of A_REBOOT relies on being able to interrupt blocked
163 	 * userland callers.
164 	 *
165 	 * We only clear ua_shutdown_thread after A_REMOUNT, because A_SHUTDOWN
166 	 * and A_REBOOT should never return.
167 	 */
168 	if (cmd == A_SHUTDOWN || cmd == A_REBOOT || cmd == A_REMOUNT) {
169 		mutex_enter(&ualock);
170 		while (ua_shutdown_thread != NULL) {
171 			if (cv_wait_sig(&uacond, &ualock) == 0) {
172 				/*
173 				 * If we were interrupted, leave, and handle
174 				 * the signal (or exit, depending on what
175 				 * happened)
176 				 */
177 				mutex_exit(&ualock);
178 				return (EINTR);
179 			}
180 		}
181 		ua_shutdown_thread = curthread;
182 		mutex_exit(&ualock);
183 	}
184 
185 	switch (cmd) {
186 	case A_SHUTDOWN:
187 	{
188 		proc_t *p = ttoproc(curthread);
189 
190 		/*
191 		 * Release (almost) all of our own resources if we are called
192 		 * from a user context, however if we are calling kadmin() from
193 		 * a kernel context then we do not release these resources.
194 		 */
195 		if (p != &p0) {
196 			proc_is_exiting(p);
197 			if ((error = exitlwps(0)) != 0) {
198 				/*
199 				 * Another thread in this process also called
200 				 * exitlwps().
201 				 */
202 				mutex_enter(&ualock);
203 				ua_shutdown_thread = NULL;
204 				cv_signal(&uacond);
205 				mutex_exit(&ualock);
206 				return (error);
207 			}
208 			mutex_enter(&p->p_lock);
209 			p->p_flag |= SNOWAIT;
210 			sigfillset(&p->p_ignore);
211 			curthread->t_lwp->lwp_cursig = 0;
212 			curthread->t_lwp->lwp_extsig = 0;
213 			if (p->p_exec) {
214 				vnode_t *exec_vp = p->p_exec;
215 				p->p_exec = NULLVP;
216 				mutex_exit(&p->p_lock);
217 				VN_RELE(exec_vp);
218 			} else {
219 				mutex_exit(&p->p_lock);
220 			}
221 
222 			pollcleanup();
223 			closeall(P_FINFO(curproc));
224 			relvm();
225 
226 		} else {
227 			/*
228 			 * Reset t_cred if not set because much of the
229 			 * filesystem code depends on CRED() being valid.
230 			 */
231 			if (curthread->t_cred == NULL)
232 				curthread->t_cred = kcred;
233 		}
234 
235 		/* indicate shutdown in progress */
236 		sys_shutdown = 1;
237 
238 		/*
239 		 * Communcate that init shouldn't be restarted.
240 		 */
241 		zone_shutdown_global();
242 
243 		killall(ALL_ZONES);
244 		/*
245 		 * If we are calling kadmin() from a kernel context then we
246 		 * do not release these resources.
247 		 */
248 		if (ttoproc(curthread) != &p0) {
249 			VN_RELE(PTOU(curproc)->u_cdir);
250 			if (PTOU(curproc)->u_rdir)
251 				VN_RELE(PTOU(curproc)->u_rdir);
252 			if (PTOU(curproc)->u_cwd)
253 				refstr_rele(PTOU(curproc)->u_cwd);
254 
255 			PTOU(curproc)->u_cdir = rootdir;
256 			PTOU(curproc)->u_rdir = NULL;
257 			PTOU(curproc)->u_cwd = NULL;
258 		}
259 
260 		/*
261 		 * Allow the reboot/halt/poweroff code a chance to do
262 		 * anything it needs to whilst we still have filesystems
263 		 * mounted, like loading any modules necessary for later
264 		 * performing the actual poweroff.
265 		 */
266 		if ((mdep != NULL) && (*(char *)mdep == '/')) {
267 			buf = i_convert_boot_device_name(mdep, NULL, &buflen);
268 			mdpreboot(cmd, fcn, buf);
269 		} else
270 			mdpreboot(cmd, fcn, mdep);
271 
272 		/*
273 		 * Allow fsflush to finish running and then prevent it
274 		 * from ever running again so that vfs_unmountall() and
275 		 * vfs_syncall() can acquire the vfs locks they need.
276 		 */
277 		sema_p(&fsflush_sema);
278 		(void) callb_execute_class(CB_CL_UADMIN_PRE_VFS, NULL);
279 
280 		vfs_unmountall();
281 		(void) VFS_MOUNTROOT(rootvfs, ROOT_UNMOUNT);
282 		vfs_syncall();
283 
284 		dump_ereports();
285 		dump_messages();
286 
287 		invoke_cb = B_TRUE;
288 
289 		/* FALLTHROUGH */
290 	}
291 
292 	case A_REBOOT:
293 		if ((mdep != NULL) && (*(char *)mdep == '/')) {
294 			buf = i_convert_boot_device_name(mdep, NULL, &buflen);
295 			mdboot(cmd, fcn, buf, invoke_cb);
296 		} else
297 			mdboot(cmd, fcn, mdep, invoke_cb);
298 		/* no return expected */
299 		break;
300 
301 	case A_REMOUNT:
302 		(void) VFS_MOUNTROOT(rootvfs, ROOT_REMOUNT);
303 		/* Let other threads enter the shutdown path now */
304 		mutex_enter(&ualock);
305 		ua_shutdown_thread = NULL;
306 		cv_signal(&uacond);
307 		mutex_exit(&ualock);
308 		break;
309 
310 	case A_FREEZE:
311 	{
312 		/* XXX: declare in some header file */
313 		extern int cpr(int);
314 
315 		if (modload("misc", "cpr") == -1)
316 			return (ENOTSUP);
317 		error = cpr(fcn);
318 		break;
319 	}
320 
321 	case A_FTRACE:
322 	{
323 		switch (fcn) {
324 		case AD_FTRACE_START:
325 			(void) FTRACE_START();
326 			break;
327 		case AD_FTRACE_STOP:
328 			(void) FTRACE_STOP();
329 			break;
330 		default:
331 			error = EINVAL;
332 		}
333 		break;
334 	}
335 
336 	case A_DUMP:
337 	{
338 		if (fcn == AD_NOSYNC) {
339 			in_sync = 1;
340 			break;
341 		}
342 
343 		panic_bootfcn = fcn;
344 		panic_forced = 1;
345 
346 		if ((mdep != NULL) && (*(char *)mdep == '/')) {
347 			panic_bootstr = i_convert_boot_device_name(mdep,
348 			    NULL, &buflen);
349 		} else
350 			panic_bootstr = mdep;
351 
352 		panic("forced crash dump initiated at user request");
353 		/*NOTREACHED*/
354 	}
355 
356 	default:
357 		error = EINVAL;
358 	}
359 
360 	return (error);
361 }
362 
363 int
364 uadmin(int cmd, int fcn, uintptr_t mdep)
365 {
366 	int error = 0, rv = 0;
367 	size_t nbytes = 0;
368 	cred_t *credp = CRED();
369 	char *bootargs = NULL;
370 
371 	/*
372 	 * The swapctl system call doesn't have its own entry point: it uses
373 	 * uadmin as a wrapper so we just call it directly from here.
374 	 */
375 	if (cmd == A_SWAPCTL) {
376 		if (get_udatamodel() == DATAMODEL_NATIVE)
377 			error = swapctl(fcn, (void *)mdep, &rv);
378 #if defined(_SYSCALL32_IMPL)
379 		else
380 			error = swapctl32(fcn, (void *)mdep, &rv);
381 #endif /* _SYSCALL32_IMPL */
382 		return (error ? set_errno(error) : rv);
383 	}
384 
385 	/*
386 	 * Certain subcommands intepret a non-NULL mdep value as a pointer to
387 	 * a boot string.  We pull that in as bootargs, if applicable.
388 	 */
389 	if (mdep != NULL &&
390 	    (cmd == A_SHUTDOWN || cmd == A_REBOOT || cmd == A_DUMP)) {
391 		bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP);
392 		if ((error = copyinstr((const char *)mdep, bootargs,
393 		    BOOTARGS_MAX, &nbytes)) != 0) {
394 			kmem_free(bootargs, BOOTARGS_MAX);
395 			return (set_errno(error));
396 		}
397 	}
398 
399 	/*
400 	 * Invoke the appropriate kadmin() routine.
401 	 */
402 	if (getzoneid() != GLOBAL_ZONEID)
403 		error = zone_kadmin(cmd, fcn, bootargs, credp);
404 	else
405 		error = kadmin(cmd, fcn, bootargs, credp);
406 
407 	if (bootargs != NULL)
408 		kmem_free(bootargs, BOOTARGS_MAX);
409 	return (error ? set_errno(error) : 0);
410 }
411