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