xref: /titanic_41/usr/src/uts/common/syscall/uadmin.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
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 
130 	/*
131 	 * We might be called directly by the kernel's fault-handling code, so
132 	 * we can't assert that the caller is in the global zone.
133 	 */
134 
135 	/*
136 	 * Make sure that cmd is one of the valid <sys/uadmin.h> command codes
137 	 * and that we have appropriate privileges for this action.
138 	 */
139 	switch (cmd) {
140 	case A_FTRACE:
141 	case A_SHUTDOWN:
142 	case A_REBOOT:
143 	case A_REMOUNT:
144 	case A_FREEZE:
145 	case A_DUMP:
146 		if (secpolicy_sys_config(credp, B_FALSE) != 0)
147 			return (EPERM);
148 		break;
149 
150 	default:
151 		return (EINVAL);
152 	}
153 
154 	/*
155 	 * Serialize these operations on ualock.  If it is held, just return
156 	 * as if successful since the system will soon reset or remount.
157 	 */
158 	if (cmd == A_SHUTDOWN || cmd == A_REBOOT || cmd == A_REMOUNT) {
159 		if (!mutex_tryenter(&ualock))
160 			return (0);
161 		locked = 1;
162 	}
163 
164 	switch (cmd) {
165 	case A_SHUTDOWN:
166 	{
167 		proc_t *p = ttoproc(curthread);
168 
169 		/*
170 		 * Release (almost) all of our own resources if we are called
171 		 * from a user context, however if we are calling kadmin() from
172 		 * a kernel context then we do not release these resources.
173 		 */
174 		if (ttoproc(curthread) != &p0) {
175 			if ((error = exitlwps(0)) != 0)
176 				return (error);
177 			mutex_enter(&p->p_lock);
178 			p->p_flag |= SNOWAIT;
179 			sigfillset(&p->p_ignore);
180 			curthread->t_lwp->lwp_cursig = 0;
181 			curthread->t_lwp->lwp_extsig = 0;
182 			if (p->p_exec) {
183 				vnode_t *exec_vp = p->p_exec;
184 				p->p_exec = NULLVP;
185 				mutex_exit(&p->p_lock);
186 				VN_RELE(exec_vp);
187 			} else {
188 				mutex_exit(&p->p_lock);
189 			}
190 
191 			pollcleanup();
192 			closeall(P_FINFO(curproc));
193 			relvm();
194 
195 		} else {
196 			/*
197 			 * Reset t_cred if not set because much of the
198 			 * filesystem code depends on CRED() being valid.
199 			 */
200 			if (curthread->t_cred == NULL)
201 				curthread->t_cred = kcred;
202 		}
203 
204 		/*
205 		 * Communcate that init shouldn't be restarted.
206 		 */
207 		zone_shutdown_global();
208 
209 		killall(ALL_ZONES);
210 		/*
211 		 * If we are calling kadmin() from a kernel context then we
212 		 * do not release these resources.
213 		 */
214 		if (ttoproc(curthread) != &p0) {
215 			VN_RELE(u.u_cdir);
216 			if (u.u_rdir)
217 				VN_RELE(u.u_rdir);
218 			if (u.u_cwd)
219 				refstr_rele(u.u_cwd);
220 
221 			u.u_cdir = rootdir;
222 			u.u_rdir = NULL;
223 			u.u_cwd = NULL;
224 		}
225 
226 		/*
227 		 * Allow the reboot/halt/poweroff code a chance to do
228 		 * anything it needs to whilst we still have filesystems
229 		 * mounted, like loading any modules necessary for later
230 		 * performing the actual poweroff.
231 		 */
232 		if ((mdep != NULL) && (*(char *)mdep == '/')) {
233 			buf = i_convert_boot_device_name(mdep, NULL, &buflen);
234 			mdpreboot(cmd, fcn, buf);
235 		} else
236 			mdpreboot(cmd, fcn, mdep);
237 
238 		/*
239 		 * Allow fsflush to finish running and then prevent it
240 		 * from ever running again so that vfs_unmountall() and
241 		 * vfs_syncall() can acquire the vfs locks they need.
242 		 */
243 		sema_p(&fsflush_sema);
244 		(void) callb_execute_class(CB_CL_UADMIN_PRE_VFS, NULL);
245 
246 		vfs_unmountall();
247 		(void) VFS_MOUNTROOT(rootvfs, ROOT_UNMOUNT);
248 		vfs_syncall();
249 
250 		(void) callb_execute_class(CB_CL_UADMIN_POST_VFS, NULL);
251 		dump_ereports();
252 		dump_messages();
253 
254 		/* FALLTHROUGH */
255 	}
256 
257 	case A_REBOOT:
258 		if ((mdep != NULL) && (*(char *)mdep == '/')) {
259 			buf = i_convert_boot_device_name(mdep, NULL, &buflen);
260 			mdboot(cmd, fcn, buf);
261 		} else
262 			mdboot(cmd, fcn, mdep);
263 		/* no return expected */
264 		break;
265 
266 	case A_REMOUNT:
267 		(void) VFS_MOUNTROOT(rootvfs, ROOT_REMOUNT);
268 		break;
269 
270 	case A_FREEZE:
271 	{
272 		/* XXX: declare in some header file */
273 		extern int cpr(int);
274 
275 		if (modload("misc", "cpr") == -1)
276 			return (ENOTSUP);
277 		error = cpr(fcn);
278 		break;
279 	}
280 
281 	case A_FTRACE:
282 	{
283 		switch (fcn) {
284 		case AD_FTRACE_START:
285 			(void) FTRACE_START();
286 			break;
287 		case AD_FTRACE_STOP:
288 			(void) FTRACE_STOP();
289 			break;
290 		default:
291 			error = EINVAL;
292 		}
293 		break;
294 	}
295 
296 	case A_DUMP:
297 	{
298 		if (fcn == AD_NOSYNC) {
299 			in_sync = 1;
300 			break;
301 		}
302 
303 		panic_bootfcn = fcn;
304 		panic_forced = 1;
305 
306 		if ((mdep != NULL) && (*(char *)mdep == '/')) {
307 			panic_bootstr = i_convert_boot_device_name(mdep,
308 					    NULL, &buflen);
309 		} else
310 			panic_bootstr = mdep;
311 
312 		panic("forced crash dump initiated at user request");
313 		/*NOTREACHED*/
314 	}
315 
316 	default:
317 		error = EINVAL;
318 	}
319 
320 	if (locked)
321 		mutex_exit(&ualock);
322 
323 	return (error);
324 }
325 
326 int
327 uadmin(int cmd, int fcn, uintptr_t mdep)
328 {
329 	int error = 0, rv = 0;
330 	size_t nbytes = 0;
331 	char buf[257];
332 	cred_t *credp = CRED();
333 
334 	/*
335 	 * The swapctl system call doesn't have its own entry point: it uses
336 	 * uadmin as a wrapper so we just call it directly from here.
337 	 */
338 	if (cmd == A_SWAPCTL) {
339 		if (get_udatamodel() == DATAMODEL_NATIVE)
340 			error = swapctl(fcn, (void *)mdep, &rv);
341 #if defined(_SYSCALL32_IMPL)
342 		else
343 			error = swapctl32(fcn, (void *)mdep, &rv);
344 #endif /* _SYSCALL32_IMPL */
345 		return (error ? set_errno(error) : rv);
346 	}
347 
348 	/*
349 	 * Handle zones.
350 	 */
351 	if (getzoneid() != GLOBAL_ZONEID) {
352 		error = zone_uadmin(cmd, fcn, credp);
353 		return (error ? set_errno(error) : 0);
354 	}
355 
356 	/*
357 	 * Certain subcommands intepret a non-NULL mdep value as a pointer to
358 	 * a boot string.  Attempt to copy it in now, or reset mdep to NULL.
359 	 */
360 	if (cmd == A_SHUTDOWN || cmd == A_REBOOT || cmd == A_DUMP) {
361 		if (mdep != NULL && copyinstr((const char *)mdep, buf,
362 		    sizeof (buf) - 1, &nbytes) == 0) {
363 			buf[nbytes] = '\0';
364 			mdep = (uintptr_t)buf;
365 		} else
366 			mdep = NULL;
367 	}
368 
369 	if ((error = kadmin(cmd, fcn, (void *)mdep, credp)) != 0)
370 		return (set_errno(error));
371 
372 	return (0);
373 }
374