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