remove unnecessary offset_t casts of 0
[unleashed.git] / kernel / os / core.c
bloba147c6ef3dd53f1d3220ba36d48c1d00393ee31d
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, Joyent Inc. All rights reserved.
25 * Copyright (c) 2016 by Delphix. All rights reserved.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/sysmacros.h>
35 #include <sys/proc.h>
36 #include <sys/systm.h>
37 #include <sys/cred.h>
38 #include <sys/user.h>
39 #include <sys/utsname.h>
40 #include <sys/errno.h>
41 #include <sys/signal.h>
42 #include <sys/siginfo.h>
43 #include <sys/fault.h>
44 #include <sys/syscall.h>
45 #include <sys/ucontext.h>
46 #include <sys/prsystm.h>
47 #include <sys/vnode.h>
48 #include <sys/var.h>
49 #include <sys/file.h>
50 #include <sys/pathname.h>
51 #include <sys/vfs.h>
52 #include <sys/exec.h>
53 #include <sys/debug.h>
54 #include <sys/stack.h>
55 #include <sys/kmem.h>
56 #include <sys/schedctl.h>
57 #include <sys/core.h>
58 #include <sys/corectl.h>
59 #include <sys/cmn_err.h>
60 #include <vm/as.h>
61 #include <sys/rctl.h>
62 #include <sys/nbmlock.h>
63 #include <sys/stat.h>
64 #include <sys/zone.h>
65 #include <sys/contract/process_impl.h>
66 #include <sys/ddi.h>
69 * Processes running within a zone potentially dump core in 3 locations,
70 * based on the per-process, per-zone, and the global zone's core settings.
72 * Per-zone and global zone settings are often referred to as "global"
73 * settings since they apply to the system (or zone) as a whole, as
74 * opposed to a particular process.
76 enum core_types {
77 CORE_PROC, /* Use per-process settings */
78 CORE_ZONE, /* Use per-zone settings */
79 CORE_GLOBAL /* Use global zone settings */
83 * Log information about "global" core dumps to syslog.
85 static void
86 core_log(struct core_globals *cg, int error, const char *why, const char *path,
87 zoneid_t zoneid)
89 proc_t *p = curproc;
90 pid_t pid = p->p_pid;
91 char *fn = PTOU(p)->u_comm;
93 if (!(cg->core_options & CC_GLOBAL_LOG))
94 return;
96 if (path == NULL)
97 zcmn_err(zoneid, CE_NOTE, "core_log: %s[%d] %s", fn, pid, why);
98 else if (error == 0)
99 zcmn_err(zoneid, CE_NOTE, "core_log: %s[%d] %s: %s", fn, pid,
100 why, path);
101 else
102 zcmn_err(zoneid, CE_NOTE, "core_log: %s[%d] %s, errno=%d: %s",
103 fn, pid, why, error, path);
107 * Private version of vn_remove().
108 * Refuse to unlink a directory or an unwritable file.
109 * Also allow the process to access files normally inaccessible due to
110 * chroot(2) or Zone limitations.
112 static int
113 remove_core_file(char *fp, enum core_types core_type)
115 vnode_t *vp = NULL; /* entry vnode */
116 vnode_t *dvp; /* ptr to parent dir vnode */
117 vfs_t *dvfsp;
118 int error;
119 int in_crit = 0;
120 pathname_t pn; /* name of entry */
121 vnode_t *startvp, *rootvp;
123 if ((error = pn_get(fp, UIO_SYSSPACE, &pn)) != 0)
124 return (error);
126 * Determine what rootvp to use.
128 if (core_type == CORE_PROC) {
129 rootvp = (PTOU(curproc)->u_rdir == NULL ?
130 curproc->p_zone->zone_rootvp : PTOU(curproc)->u_rdir);
131 startvp = (fp[0] == '/' ? rootvp : PTOU(curproc)->u_cdir);
132 } else if (core_type == CORE_ZONE) {
133 startvp = curproc->p_zone->zone_rootvp;
134 rootvp = curproc->p_zone->zone_rootvp;
135 } else {
136 ASSERT(core_type == CORE_GLOBAL);
137 startvp = rootdir;
138 rootvp = rootdir;
140 VN_HOLD(startvp);
141 if (rootvp != rootdir)
142 VN_HOLD(rootvp);
143 if ((error = lookuppnvp(&pn, NULL, NO_FOLLOW, &dvp, &vp, rootvp,
144 startvp, CRED())) != 0) {
145 pn_free(&pn);
146 return (error);
149 * Succeed if there is no file.
150 * Fail if the file is not a regular file.
151 * Fail if the filesystem is mounted read-only.
152 * Fail if the file is not writeable.
153 * Fail if the file has NBMAND share reservations.
155 if (vp == NULL)
156 error = 0;
157 else if (vp->v_type != VREG)
158 error = EACCES;
159 else if ((dvfsp = dvp->v_vfsp) != NULL &&
160 (dvfsp->vfs_flag & VFS_RDONLY))
161 error = EROFS;
162 else if ((error = fop_access(vp, VWRITE, 0, CRED(), NULL)) == 0) {
163 if (nbl_need_check(vp)) {
164 nbl_start_crit(vp, RW_READER);
165 in_crit = 1;
166 if (nbl_share_conflict(vp, NBL_REMOVE, NULL)) {
167 error = EACCES;
170 if (!error) {
171 error = fop_remove(dvp, pn.pn_path, CRED(), NULL, 0);
175 pn_free(&pn);
176 if (vp != NULL) {
177 if (in_crit)
178 nbl_end_crit(vp);
179 VN_RELE(vp);
181 VN_RELE(dvp);
182 return (error);
186 * Create the core file in a location that may be normally inaccessible due
187 * to chroot(2) or Zone limitations.
189 static int
190 create_core_file(char *fp, enum core_types core_type, vnode_t **vpp)
192 int error;
193 mode_t perms = (S_IRUSR | S_IWUSR);
194 pathname_t pn;
195 char *file;
196 vnode_t *vp;
197 vnode_t *dvp;
198 vattr_t vattr;
199 cred_t *credp = CRED();
201 if (core_type == CORE_PROC) {
202 file = fp;
203 dvp = NULL; /* regular lookup */
204 } else {
205 vnode_t *startvp, *rootvp;
207 ASSERT(core_type == CORE_ZONE || core_type == CORE_GLOBAL);
209 * This is tricky because we want to dump the core in
210 * a location which may normally be inaccessible
211 * to us (due to chroot(2) limitations, or zone
212 * membership), and hence need to overcome u_rdir
213 * restrictions. The basic idea is to separate
214 * the path from the filename, lookup the
215 * pathname separately (starting from the global
216 * zone's root directory), and then open the
217 * file starting at the directory vnode.
219 if (error = pn_get(fp, UIO_SYSSPACE, &pn))
220 return (error);
222 if (core_type == CORE_ZONE) {
223 startvp = rootvp = curproc->p_zone->zone_rootvp;
224 } else {
225 startvp = rootvp = rootdir;
228 * rootvp and startvp will be VN_RELE()'d by lookuppnvp() if
229 * necessary.
231 VN_HOLD(startvp);
232 if (rootvp != rootdir)
233 VN_HOLD(rootvp);
235 * Do a lookup on the full path, ignoring the actual file, but
236 * finding the vnode for the directory. It's OK if the file
237 * doesn't exist -- it most likely won't since we just removed
238 * it.
240 error = lookuppnvp(&pn, NULL, FOLLOW, &dvp, NULLVPP,
241 rootvp, startvp, credp);
242 pn_free(&pn);
243 if (error != 0)
244 return (error);
245 ASSERT(dvp != NULL);
247 * Now find the final component in the path (ie, the name of
248 * the core file).
250 if (error = pn_get(fp, UIO_SYSSPACE, &pn)) {
251 VN_RELE(dvp);
252 return (error);
254 pn_setlast(&pn);
255 file = pn.pn_path;
257 error = vn_openat(file, UIO_SYSSPACE,
258 FWRITE | FTRUNC | FEXCL | FCREAT | FOFFMAX,
259 perms, &vp, CRCREAT, PTOU(curproc)->u_cmask, dvp, -1);
260 if (core_type != CORE_PROC) {
261 VN_RELE(dvp);
262 pn_free(&pn);
265 * Don't dump a core file owned by "nobody".
267 vattr.va_mask = AT_UID;
268 if (error == 0 &&
269 (fop_getattr(vp, &vattr, 0, credp, NULL) != 0 ||
270 vattr.va_uid != crgetuid(credp))) {
271 (void) fop_close(vp, FWRITE, 1, 0,
272 credp, NULL);
273 VN_RELE(vp);
274 (void) remove_core_file(fp, core_type);
275 error = EACCES;
277 *vpp = vp;
278 return (error);
282 * Install the specified held cred into the process, and return a pointer to
283 * the held cred which was previously the value of p->p_cred.
285 static cred_t *
286 set_cred(proc_t *p, cred_t *newcr)
288 cred_t *oldcr;
289 uid_t olduid, newuid;
292 * Place a hold on the existing cred, and then install the new
293 * cred into the proc structure.
295 mutex_enter(&p->p_crlock);
296 oldcr = p->p_cred;
297 crhold(oldcr);
298 p->p_cred = newcr;
299 mutex_exit(&p->p_crlock);
301 ASSERT(crgetzoneid(oldcr) == crgetzoneid(newcr));
304 * If the real uid is changing, keep the per-user process
305 * counts accurate.
307 olduid = crgetruid(oldcr);
308 newuid = crgetruid(newcr);
309 if (olduid != newuid) {
310 zoneid_t zoneid = crgetzoneid(newcr);
312 mutex_enter(&pidlock);
313 upcount_dec(olduid, zoneid);
314 upcount_inc(newuid, zoneid);
315 mutex_exit(&pidlock);
319 * Broadcast the new cred to all the other threads. The old
320 * cred can be safely returned because we have a hold on it.
322 crset(p, newcr);
323 return (oldcr);
326 static int
327 do_core(char *fp, int sig, enum core_types core_type, struct core_globals *cg)
329 proc_t *p = curproc;
330 cred_t *credp = CRED();
331 rlim64_t rlimit;
332 vnode_t *vp;
333 int error = 0;
334 struct execsw *eswp;
335 cred_t *ocredp = NULL;
336 int is_setid = 0;
337 core_content_t content;
338 uid_t uid;
339 gid_t gid;
341 if (core_type == CORE_GLOBAL || core_type == CORE_ZONE) {
342 mutex_enter(&cg->core_lock);
343 content = cg->core_content;
344 mutex_exit(&cg->core_lock);
345 rlimit = cg->core_rlimit;
346 } else {
347 mutex_enter(&p->p_lock);
348 rlimit = rctl_enforced_value(rctlproc_legacy[RLIMIT_CORE],
349 p->p_rctls, p);
350 content = corectl_content_value(p->p_content);
351 mutex_exit(&p->p_lock);
354 if (rlimit == 0)
355 return (EFBIG);
358 * If SNOCD is set, or if the effective, real, and saved ids do
359 * not match up, no one but a privileged user is allowed to view
360 * this core file. Set the credentials and the owner to root.
362 if ((p->p_flag & SNOCD) ||
363 (uid = crgetuid(credp)) != crgetruid(credp) ||
364 uid != crgetsuid(credp) ||
365 (gid = crgetgid(credp)) != crgetrgid(credp) ||
366 gid != crgetsgid(credp)) {
368 * Because this is insecure against certain forms of file
369 * system attack, do it only if set-id core files have been
370 * enabled via corectl(CC_GLOBAL_SETID | CC_PROCESS_SETID).
372 if (((core_type == CORE_GLOBAL || core_type == CORE_ZONE) &&
373 !(cg->core_options & CC_GLOBAL_SETID)) ||
374 (core_type == CORE_PROC &&
375 !(cg->core_options & CC_PROCESS_SETID)))
376 return (ENOTSUP);
378 is_setid = 1;
382 * If we are doing a "global" core dump or a set-id core dump,
383 * use kcred to do the dumping.
385 if (core_type == CORE_GLOBAL || core_type == CORE_ZONE || is_setid) {
387 * Use the zone's "kcred" to prevent privilege
388 * escalation.
390 credp = zone_get_kcred(getzoneid());
391 ASSERT(credp != NULL);
392 ocredp = set_cred(p, credp);
396 * First remove any existing core file, then
397 * open the new core file with (O_EXCL|O_CREAT).
399 * The reasons for doing this are manifold:
401 * For security reasons, we don't want root processes
402 * to dump core through a symlink because that would
403 * allow a malicious user to clobber any file on
404 * the system if they could convince a root process,
405 * perhaps a set-uid root process that they started,
406 * to dump core in a directory writable by that user.
407 * Similar security reasons apply to hard links.
408 * For symmetry we do this unconditionally, not
409 * just for root processes.
411 * If the process has the core file mmap()d into the
412 * address space, we would be modifying the address
413 * space that we are trying to dump if we did not first
414 * remove the core file. (The command "file core"
415 * is the canonical example of this possibility.)
417 * Opening the core file with O_EXCL|O_CREAT ensures than
418 * two concurrent core dumps don't clobber each other.
419 * One is bound to lose; we don't want to make both lose.
421 if ((error = remove_core_file(fp, core_type)) == 0) {
422 error = create_core_file(fp, core_type, &vp);
426 * Now that vn_open is complete, reset the process's credentials if
427 * we changed them, and make 'credp' point to kcred used
428 * above. We use 'credp' to do i/o on the core file below, but leave
429 * p->p_cred set to the original credential to allow the core file
430 * to record this information.
432 if (ocredp != NULL)
433 credp = set_cred(p, ocredp);
435 if (error == 0) {
436 int closerr;
437 if ((eswp = PTOU(curproc)->u_execsw) == NULL ||
438 (eswp = findexec_by_magic(eswp->exec_magic)) == NULL) {
439 error = ENOSYS;
440 } else {
441 error = eswp->exec_core(vp, p, credp, rlimit, sig,
442 content);
443 rw_exit(eswp->exec_lock);
446 closerr = fop_close(vp, FWRITE, 1, 0, credp, NULL);
447 VN_RELE(vp);
448 if (error == 0)
449 error = closerr;
452 if (ocredp != NULL)
453 crfree(credp);
455 return (error);
459 * Convert a core name pattern to a pathname.
461 static int
462 expand_string(const char *pat, char *fp, int size, cred_t *cr)
464 proc_t *p = curproc;
465 char buf[24];
466 int len, i;
467 char *s;
468 char c;
470 while ((c = *pat++) != '\0') {
471 if (size < 2)
472 return (ENAMETOOLONG);
473 if (c != '%') {
474 size--;
475 *fp++ = c;
476 continue;
478 if ((c = *pat++) == '\0') {
479 size--;
480 *fp++ = '%';
481 break;
483 switch (c) {
484 case 'p': /* pid */
485 (void) sprintf((s = buf), "%d", p->p_pid);
486 break;
487 case 'u': /* effective uid */
488 (void) sprintf((s = buf), "%u", crgetuid(p->p_cred));
489 break;
490 case 'g': /* effective gid */
491 (void) sprintf((s = buf), "%u", crgetgid(p->p_cred));
492 break;
493 case 'f': /* exec'd filename */
494 s = PTOU(p)->u_comm;
495 break;
496 case 'd': /* exec'd dirname */
498 * Even if pathname caching is disabled, we should
499 * be able to lookup the pathname for a directory.
501 if (p->p_execdir != NULL && vnodetopath(NULL,
502 p->p_execdir, fp, size, cr) == 0) {
503 len = (int)strlen(fp);
504 ASSERT(len < size);
505 ASSERT(len >= 1);
506 ASSERT(fp[0] == '/');
509 * Strip off the leading slash.
511 for (i = 0; i < len; i++) {
512 fp[i] = fp[i + 1];
515 len--;
517 size -= len;
518 fp += len;
519 } else {
520 *fp = '\0';
523 continue;
524 case 'n': /* system nodename */
525 s = uts_nodename();
526 break;
527 case 'm': /* machine (sun4u, etc) */
528 s = utsname.machine;
529 break;
530 case 't': /* decimal value of time(2) */
531 (void) sprintf((s = buf), "%ld", gethrestime_sec());
532 break;
533 case 'z':
534 s = p->p_zone->zone_name;
535 break;
536 case 'Z':
537 /* This is zonepath + "/root/", except for GZ */
538 s = p->p_zone->zone_rootpath;
539 break;
540 case '%':
541 (void) strcpy((s = buf), "%");
542 break;
543 default:
544 s = buf;
545 buf[0] = '%';
546 buf[1] = c;
547 buf[2] = '\0';
548 break;
550 len = (int)strlen(s);
551 if ((size -= len) <= 0)
552 return (ENAMETOOLONG);
553 (void) strcpy(fp, s);
554 /* strip trailing "/root/" from non-GZ zonepath string */
555 if (c == 'Z' && len > 6) {
556 len -= 6;
557 ASSERT(strncmp(fp + len, "/root/", 6) == 0);
559 fp += len;
562 *fp = '\0';
563 return (0);
566 static int
567 dump_one_core(int sig, rlim64_t rlimit, enum core_types core_type,
568 struct core_globals *cg, char **name)
570 refstr_t *rp;
571 proc_t *p = curproc;
572 zoneid_t zoneid;
573 int error;
574 char *fp;
575 cred_t *cr;
577 ASSERT(core_type == CORE_ZONE || core_type == CORE_GLOBAL);
578 zoneid = (core_type == CORE_ZONE ? getzoneid() : GLOBAL_ZONEID);
580 mutex_enter(&cg->core_lock);
581 if ((rp = cg->core_file) != NULL)
582 refstr_hold(rp);
583 mutex_exit(&cg->core_lock);
584 if (rp == NULL) {
585 core_log(cg, 0, "no global core file pattern exists", NULL,
586 zoneid);
587 return (1); /* core file not generated */
589 fp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
590 cr = zone_get_kcred(getzoneid());
591 error = expand_string(refstr_value(rp), fp, MAXPATHLEN, cr);
592 crfree(cr);
593 if (error != 0) {
594 core_log(cg, 0, "global core file pattern too long",
595 refstr_value(rp), zoneid);
596 } else if ((error = do_core(fp, sig, core_type, cg)) == 0) {
597 core_log(cg, 0, "core dumped", fp, zoneid);
598 } else if (error == ENOTSUP) {
599 core_log(cg, 0, "setid process, core not dumped", fp, zoneid);
600 } else if (error == ENOSPC) {
601 core_log(cg, 0, "no space left on device, core truncated",
602 fp, zoneid);
603 } else if (error == EFBIG) {
604 if (rlimit == 0)
605 core_log(cg, 0, "core rlimit is zero, core not dumped",
606 fp, zoneid);
607 else
608 core_log(cg, 0, "core rlimit exceeded, core truncated",
609 fp, zoneid);
611 * In addition to the core result logging, we
612 * may also have explicit actions defined on
613 * core file size violations via the resource
614 * control framework.
616 mutex_enter(&p->p_lock);
617 (void) rctl_action(rctlproc_legacy[RLIMIT_CORE],
618 p->p_rctls, p, RCA_SAFE);
619 mutex_exit(&p->p_lock);
620 } else {
621 core_log(cg, error, "core dump failed", fp, zoneid);
623 refstr_rele(rp);
624 if (name != NULL)
625 *name = fp;
626 else
627 kmem_free(fp, MAXPATHLEN);
628 return (error);
632 core(int sig, int ext)
634 proc_t *p = curproc;
635 klwp_t *lwp = ttolwp(curthread);
636 refstr_t *rp;
637 char *fp_process = NULL, *fp_global = NULL, *fp_zone = NULL;
638 int error1 = 1;
639 int error2 = 1;
640 int error3 = 1;
641 k_sigset_t sigmask;
642 k_sigset_t sighold;
643 rlim64_t rlimit;
644 struct core_globals *my_cg, *global_cg;
646 global_cg = zone_getspecific(core_zone_key, global_zone);
647 ASSERT(global_cg != NULL);
649 my_cg = zone_getspecific(core_zone_key, curproc->p_zone);
650 ASSERT(my_cg != NULL);
652 /* core files suppressed? */
653 if (!(my_cg->core_options & (CC_PROCESS_PATH|CC_GLOBAL_PATH)) &&
654 !(global_cg->core_options & CC_GLOBAL_PATH)) {
655 if (!ext && p->p_ct_process != NULL)
656 contract_process_core(p->p_ct_process, p, sig,
657 NULL, NULL, NULL);
658 return (1);
662 * Block all signals except SIGHUP, SIGINT, SIGKILL, and SIGTERM; no
663 * other signal may interrupt a core dump. For each signal, we
664 * explicitly unblock it and set it in p_siginfo to allow for some
665 * minimal error reporting. Additionally, we get the current limit on
666 * core file size for handling later error reporting.
668 mutex_enter(&p->p_lock);
670 p->p_flag |= SDOCORE;
671 schedctl_finish_sigblock(curthread);
672 sigmask = curthread->t_hold; /* remember for later */
673 sigfillset(&sighold);
674 if (!sigismember(&sigmask, SIGHUP))
675 sigdelset(&sighold, SIGHUP);
676 if (!sigismember(&sigmask, SIGINT))
677 sigdelset(&sighold, SIGINT);
678 if (!sigismember(&sigmask, SIGKILL))
679 sigdelset(&sighold, SIGKILL);
680 if (!sigismember(&sigmask, SIGTERM))
681 sigdelset(&sighold, SIGTERM);
683 sigaddset(&p->p_siginfo, SIGHUP);
684 sigaddset(&p->p_siginfo, SIGINT);
685 sigaddset(&p->p_siginfo, SIGKILL);
686 sigaddset(&p->p_siginfo, SIGTERM);
688 curthread->t_hold = sighold;
690 rlimit = rctl_enforced_value(rctlproc_legacy[RLIMIT_CORE], p->p_rctls,
693 mutex_exit(&p->p_lock);
696 * Undo any watchpoints.
698 pr_free_watched_pages(p);
701 * The presence of a current signal prevents file i/o
702 * from succeeding over a network. We copy the current
703 * signal information to the side and cancel the current
704 * signal so that the core dump will succeed.
706 ASSERT(lwp->lwp_cursig == sig);
707 lwp->lwp_cursig = 0;
708 lwp->lwp_extsig = 0;
709 if (lwp->lwp_curinfo == NULL) {
710 bzero(&lwp->lwp_siginfo, sizeof (k_siginfo_t));
711 lwp->lwp_siginfo.si_signo = sig;
712 lwp->lwp_siginfo.si_code = SI_NOINFO;
713 } else {
714 bcopy(&lwp->lwp_curinfo->sq_info,
715 &lwp->lwp_siginfo, sizeof (k_siginfo_t));
716 siginfofree(lwp->lwp_curinfo);
717 lwp->lwp_curinfo = NULL;
721 * Convert the core file name patterns into path names
722 * and call do_core() to write the core files.
725 if (my_cg->core_options & CC_PROCESS_PATH) {
726 mutex_enter(&p->p_lock);
727 if (p->p_corefile != NULL)
728 rp = corectl_path_value(p->p_corefile);
729 else
730 rp = NULL;
731 mutex_exit(&p->p_lock);
732 if (rp != NULL) {
733 fp_process = kmem_alloc(MAXPATHLEN, KM_SLEEP);
734 error1 = expand_string(refstr_value(rp),
735 fp_process, MAXPATHLEN, p->p_cred);
736 if (error1 == 0)
737 error1 = do_core(fp_process, sig, CORE_PROC,
738 my_cg);
739 refstr_rele(rp);
743 if (my_cg->core_options & CC_GLOBAL_PATH)
744 error2 = dump_one_core(sig, rlimit, CORE_ZONE, my_cg,
745 &fp_global);
746 if (global_cg != my_cg && (global_cg->core_options & CC_GLOBAL_PATH))
747 error3 = dump_one_core(sig, rlimit, CORE_GLOBAL, global_cg,
748 &fp_zone);
751 * Restore the signal hold mask.
753 mutex_enter(&p->p_lock);
754 curthread->t_hold = sigmask;
755 mutex_exit(&p->p_lock);
757 if (!ext && p->p_ct_process != NULL)
758 contract_process_core(p->p_ct_process, p, sig,
759 error1 == 0 ? fp_process : NULL,
760 error2 == 0 ? fp_global : NULL,
761 error3 == 0 ? fp_zone : NULL);
763 if (fp_process != NULL)
764 kmem_free(fp_process, MAXPATHLEN);
765 if (fp_global != NULL)
766 kmem_free(fp_global, MAXPATHLEN);
767 if (fp_zone != NULL)
768 kmem_free(fp_zone, MAXPATHLEN);
771 * Return non-zero if no core file was created.
773 return (error1 != 0 && error2 != 0 && error3 != 0);
777 * Maximum chunk size for dumping core files,
778 * size in pages, patchable in /etc/system
780 uint_t core_chunk = 32;
783 * The delay between core_write() calls, in microseconds. The default
784 * matches one "normal" clock tick, or 10 milliseconds.
786 clock_t core_delay_usec = 10000;
789 * Common code to core dump process memory. The core_seg routine does i/o
790 * using core_write() below, and so it has the same failure semantics.
793 core_seg(proc_t *p, vnode_t *vp, offset_t offset, caddr_t addr, size_t size,
794 rlim64_t rlimit, cred_t *credp)
796 caddr_t eaddr;
797 caddr_t base;
798 size_t len;
799 int err = 0;
801 eaddr = addr + size;
802 for (base = addr; base < eaddr; base += len) {
803 len = eaddr - base;
804 if (as_memory(p->p_as, &base, &len) != 0)
805 return (0);
808 * Reduce len to a reasonable value so that we don't
809 * overwhelm the VM system with a monstrously large
810 * single write and cause pageout to stop running.
812 if (len > (size_t)core_chunk * PAGESIZE)
813 len = (size_t)core_chunk * PAGESIZE;
815 err = core_write(vp, UIO_USERSPACE,
816 offset + (size_t)(base - addr), base, len, rlimit, credp);
818 if (err)
819 return (err);
822 * If we have taken a signal, return EINTR to allow the dump
823 * to be aborted.
825 if (issig(JUSTLOOKING) && issig(FORREAL))
826 return (EINTR);
829 return (0);
833 * Wrapper around vn_rdwr to perform writes to a core file. For core files,
834 * we always want to write as much as we possibly can, and then make sure to
835 * return either 0 to the caller (for success), or the actual errno value.
836 * By using this function, the caller can omit additional code for handling
837 * retries and errors for partial writes returned by vn_rdwr. If vn_rdwr
838 * unexpectedly returns zero but no progress has been made, we return ENOSPC.
841 core_write(vnode_t *vp, enum uio_seg segflg, offset_t offset,
842 const void *buf, size_t len, rlim64_t rlimit, cred_t *credp)
844 ssize_t resid = len;
845 int error = 0;
847 while (len != 0) {
848 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)buf, len, offset,
849 segflg, 0, rlimit, credp, &resid);
851 if (error != 0)
852 break;
854 if (resid >= len)
855 return (ENOSPC);
857 buf = (const char *)buf + len - resid;
858 offset += len - resid;
859 len = resid;
862 return (error);