xref: /illumos-gate/usr/src/uts/common/syscall/uadmin.c (revision 03831d35)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/sysmacros.h>
33 #include <sys/systm.h>
34 #include <sys/errno.h>
35 #include <sys/vfs.h>
36 #include <sys/vnode.h>
37 #include <sys/swap.h>
38 #include <sys/file.h>
39 #include <sys/proc.h>
40 #include <sys/var.h>
41 #include <sys/uadmin.h>
42 #include <sys/signal.h>
43 #include <sys/time.h>
44 #include <vm/seg_kmem.h>
45 #include <sys/modctl.h>
46 #include <sys/callb.h>
47 #include <sys/dumphdr.h>
48 #include <sys/debug.h>
49 #include <sys/ftrace.h>
50 #include <sys/cmn_err.h>
51 #include <sys/panic.h>
52 #include <sys/ddi.h>
53 #include <sys/sunddi.h>
54 #include <sys/policy.h>
55 #include <sys/zone.h>
56 
57 /*
58  * Administrivia system call.  We provide this in two flavors: one for calling
59  * from the system call path (uadmin), and the other for calling from elsewhere
60  * within the kernel (kadmin).  Callers must beware that certain uadmin cmd
61  * values (specifically A_SWAPCTL) are only supported by uadmin and not kadmin.
62  */
63 
64 extern ksema_t fsflush_sema;
65 kmutex_t ualock;
66 
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,
112 					    &pidlock, 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 		/*
207 		 * Communcate that init shouldn't be restarted.
208 		 */
209 		zone_shutdown_global();
210 
211 		killall(ALL_ZONES);
212 		/*
213 		 * If we are calling kadmin() from a kernel context then we
214 		 * do not release these resources.
215 		 */
216 		if (ttoproc(curthread) != &p0) {
217 			VN_RELE(u.u_cdir);
218 			if (u.u_rdir)
219 				VN_RELE(u.u_rdir);
220 			if (u.u_cwd)
221 				refstr_rele(u.u_cwd);
222 
223 			u.u_cdir = rootdir;
224 			u.u_rdir = NULL;
225 			u.u_cwd = NULL;
226 		}
227 
228 		/*
229 		 * Allow the reboot/halt/poweroff code a chance to do
230 		 * anything it needs to whilst we still have filesystems
231 		 * mounted, like loading any modules necessary for later
232 		 * performing the actual poweroff.
233 		 */
234 		if ((mdep != NULL) && (*(char *)mdep == '/')) {
235 			buf = i_convert_boot_device_name(mdep, NULL, &buflen);
236 			mdpreboot(cmd, fcn, buf);
237 		} else
238 			mdpreboot(cmd, fcn, mdep);
239 
240 		/*
241 		 * Allow fsflush to finish running and then prevent it
242 		 * from ever running again so that vfs_unmountall() and
243 		 * vfs_syncall() can acquire the vfs locks they need.
244 		 */
245 		sema_p(&fsflush_sema);
246 		(void) callb_execute_class(CB_CL_UADMIN_PRE_VFS, NULL);
247 
248 		vfs_unmountall();
249 		(void) VFS_MOUNTROOT(rootvfs, ROOT_UNMOUNT);
250 		vfs_syncall();
251 
252 		dump_ereports();
253 		dump_messages();
254 
255 		invoke_cb = B_TRUE;
256 
257 		/* FALLTHROUGH */
258 	}
259 
260 	case A_REBOOT:
261 		if ((mdep != NULL) && (*(char *)mdep == '/')) {
262 			buf = i_convert_boot_device_name(mdep, NULL, &buflen);
263 			mdboot(cmd, fcn, buf, invoke_cb);
264 		} else
265 			mdboot(cmd, fcn, mdep, invoke_cb);
266 		/* no return expected */
267 		break;
268 
269 	case A_REMOUNT:
270 		(void) VFS_MOUNTROOT(rootvfs, ROOT_REMOUNT);
271 		break;
272 
273 	case A_FREEZE:
274 	{
275 		/* XXX: declare in some header file */
276 		extern int cpr(int);
277 
278 		if (modload("misc", "cpr") == -1)
279 			return (ENOTSUP);
280 		error = cpr(fcn);
281 		break;
282 	}
283 
284 	case A_FTRACE:
285 	{
286 		switch (fcn) {
287 		case AD_FTRACE_START:
288 			(void) FTRACE_START();
289 			break;
290 		case AD_FTRACE_STOP:
291 			(void) FTRACE_STOP();
292 			break;
293 		default:
294 			error = EINVAL;
295 		}
296 		break;
297 	}
298 
299 	case A_DUMP:
300 	{
301 		if (fcn == AD_NOSYNC) {
302 			in_sync = 1;
303 			break;
304 		}
305 
306 		panic_bootfcn = fcn;
307 		panic_forced = 1;
308 
309 		if ((mdep != NULL) && (*(char *)mdep == '/')) {
310 			panic_bootstr = i_convert_boot_device_name(mdep,
311 					    NULL, &buflen);
312 		} else
313 			panic_bootstr = mdep;
314 
315 		panic("forced crash dump initiated at user request");
316 		/*NOTREACHED*/
317 	}
318 
319 	default:
320 		error = EINVAL;
321 	}
322 
323 	if (locked)
324 		mutex_exit(&ualock);
325 
326 	return (error);
327 }
328 
329 int
330 uadmin(int cmd, int fcn, uintptr_t mdep)
331 {
332 	int error = 0, rv = 0;
333 	size_t nbytes = 0;
334 	char buf[257];
335 	cred_t *credp = CRED();
336 
337 	/*
338 	 * The swapctl system call doesn't have its own entry point: it uses
339 	 * uadmin as a wrapper so we just call it directly from here.
340 	 */
341 	if (cmd == A_SWAPCTL) {
342 		if (get_udatamodel() == DATAMODEL_NATIVE)
343 			error = swapctl(fcn, (void *)mdep, &rv);
344 #if defined(_SYSCALL32_IMPL)
345 		else
346 			error = swapctl32(fcn, (void *)mdep, &rv);
347 #endif /* _SYSCALL32_IMPL */
348 		return (error ? set_errno(error) : rv);
349 	}
350 
351 	/*
352 	 * Handle zones.
353 	 */
354 	if (getzoneid() != GLOBAL_ZONEID) {
355 		error = zone_uadmin(cmd, fcn, credp);
356 		return (error ? set_errno(error) : 0);
357 	}
358 
359 	/*
360 	 * Certain subcommands intepret a non-NULL mdep value as a pointer to
361 	 * a boot string.  Attempt to copy it in now, or reset mdep to NULL.
362 	 */
363 	if (cmd == A_SHUTDOWN || cmd == A_REBOOT || cmd == A_DUMP) {
364 		if (mdep != NULL && copyinstr((const char *)mdep, buf,
365 		    sizeof (buf) - 1, &nbytes) == 0) {
366 			buf[nbytes] = '\0';
367 			mdep = (uintptr_t)buf;
368 		} else
369 			mdep = NULL;
370 	}
371 
372 	if ((error = kadmin(cmd, fcn, (void *)mdep, credp)) != 0)
373 		return (set_errno(error));
374 
375 	return (0);
376 }
377