2 * Copyright (c) 2003 Kip Macy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/types.h>
28 #include <sys/param.h>
30 #include <sys/module.h>
31 #include <sys/sysent.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/nlookup.h>
37 #include <sys/fcntl.h>
38 #include <sys/signal.h>
39 #include <vm/vm_param.h>
41 #include <sys/imgact_elf.h>
42 #include <sys/procfs.h>
46 #include <vm/vm_map.h>
47 #include <vm/vm_extern.h>
49 #include <sys/sysent.h>
50 #include <sys/sysproto.h>
51 #include <sys/resource.h>
52 #include <sys/resourcevar.h>
53 #include <sys/malloc.h>
56 #include <sys/namei.h>
57 #include <sys/vnode.h>
58 #include <machine/inttypes.h>
59 #include <machine/limits.h>
60 #include <machine/frame.h>
61 #include <sys/signalvar.h>
62 #include <sys/syslog.h>
63 #include <sys/sysctl.h>
64 #include <machine/sigframe.h>
66 #include <sys/unistd.h>
68 #include <sys/kern_syscall.h>
69 #include <sys/checkpoint.h>
70 #include <sys/mount.h>
73 #include <sys/mplock2.h>
74 #include <sys/file2.h>
76 static int elf_loadphdrs(struct file
*fp
, Elf_Phdr
*phdr
, int numsegs
);
77 static int elf_getnotes(struct lwp
*lp
, struct file
*fp
, size_t notesz
);
78 static int elf_demarshalnotes(void *src
, prpsinfo_t
*psinfo
,
79 prstatus_t
*status
, prfpregset_t
*fpregset
, int nthreads
);
80 static int elf_loadnotes(struct lwp
*, prpsinfo_t
*, prstatus_t
*,
82 static int elf_getsigs(struct lwp
*lp
, struct file
*fp
);
83 static int elf_getfiles(struct lwp
*lp
, struct file
*fp
);
84 static int elf_gettextvp(struct proc
*p
, struct file
*fp
);
85 static char *ckpt_expand_name(const char *name
, uid_t uid
, pid_t pid
);
87 static int ckptgroup
= 0; /* wheel only, -1 for any group */
88 SYSCTL_INT(_kern
, OID_AUTO
, ckptgroup
, CTLFLAG_RW
, &ckptgroup
, 0, "");
90 /* ref count to see how many processes that are being checkpointed */
91 static int chptinuse
= 0;
95 read_check(struct file
*fp
, void *buf
, size_t nbyte
)
100 PRINTF(("reading %zd bytes\n", nbyte
));
101 error
= fp_read(fp
, buf
, nbyte
, &nread
, 1, UIO_SYSSPACE
);
103 PRINTF(("read failed - %d", error
));
104 } else if (nread
!= nbyte
) {
105 PRINTF(("wanted to read %zd - read %zd\n", nbyte
, nread
));
112 elf_gethdr(struct file
*fp
, Elf_Ehdr
*ehdr
)
114 size_t nbyte
= sizeof(Elf_Ehdr
);
117 if ((error
= read_check(fp
, ehdr
, nbyte
)) != 0)
119 if (!(ehdr
->e_ehsize
== sizeof(Elf_Ehdr
))) {
120 PRINTF(("wrong elf header size: %d\n"
121 "expected size : %zd\n",
122 ehdr
->e_ehsize
, sizeof(Elf_Ehdr
)));
125 if (!(ehdr
->e_phentsize
== sizeof(Elf_Phdr
))) {
126 PRINTF(("wrong program header size: %d\n"
127 "expected size : %zd\n",
128 ehdr
->e_phentsize
, sizeof(Elf_Phdr
)));
132 if (!(ehdr
->e_ident
[EI_MAG0
] == ELFMAG0
&&
133 ehdr
->e_ident
[EI_MAG1
] == ELFMAG1
&&
134 ehdr
->e_ident
[EI_MAG2
] == ELFMAG2
&&
135 ehdr
->e_ident
[EI_MAG3
] == ELFMAG3
&&
136 ehdr
->e_ident
[EI_CLASS
] == ELF_CLASS
&&
137 ehdr
->e_ident
[EI_DATA
] == ELF_DATA
&&
138 ehdr
->e_ident
[EI_VERSION
] == EV_CURRENT
&&
139 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
&&
140 ehdr
->e_ident
[EI_ABIVERSION
] == 0)) {
141 PRINTF(("bad elf header\n there are %d segments\n",
146 PRINTF(("Elf header size: %d\n", ehdr
->e_ehsize
));
147 PRINTF(("Program header size: %d\n", ehdr
->e_phentsize
));
148 PRINTF(("Number of Program headers: %d\n", ehdr
->e_phnum
));
154 elf_getphdrs(struct file
*fp
, Elf_Phdr
*phdr
, size_t nbyte
)
158 int nheaders
= nbyte
/sizeof(Elf_Phdr
);
160 PRINTF(("reading phdrs section\n"));
161 if ((error
= read_check(fp
, phdr
, nbyte
)) != 0)
163 PRINTF(("headers section:\n"));
164 for (i
= 0; i
< nheaders
; i
++) {
165 PRINTF(("entry type: %d\n", phdr
[i
].p_type
));
166 PRINTF(("file offset: %jd\n", (intmax_t)phdr
[i
].p_offset
));
167 PRINTF(("virt address: %p\n", (uint32_t *)phdr
[i
].p_vaddr
));
168 PRINTF(("file size: %jd\n", (intmax_t)phdr
[i
].p_filesz
));
169 PRINTF(("memory size: %jd\n", (intmax_t)phdr
[i
].p_memsz
));
178 elf_getnotes(struct lwp
*lp
, struct file
*fp
, size_t notesz
)
185 prfpregset_t
*fpregset
;
187 nthreads
= (notesz
- sizeof(prpsinfo_t
))/(sizeof(prstatus_t
) +
188 sizeof(prfpregset_t
));
189 PRINTF(("reading notes header nthreads=%d\n", nthreads
));
190 if (nthreads
<= 0 || nthreads
> CKPT_MAXTHREADS
)
193 psinfo
= kmalloc(sizeof(prpsinfo_t
), M_TEMP
, M_ZERO
| M_WAITOK
);
194 status
= kmalloc(nthreads
*sizeof(prstatus_t
), M_TEMP
, M_WAITOK
);
195 fpregset
= kmalloc(nthreads
*sizeof(prfpregset_t
), M_TEMP
, M_WAITOK
);
196 note
= kmalloc(notesz
, M_TEMP
, M_WAITOK
);
199 PRINTF(("reading notes section\n"));
200 if ((error
= read_check(fp
, note
, notesz
)) != 0)
202 error
= elf_demarshalnotes(note
, psinfo
, status
, fpregset
, nthreads
);
205 /* fetch register state from notes */
206 error
= elf_loadnotes(lp
, psinfo
, status
, fpregset
);
209 kfree(psinfo
, M_TEMP
);
211 kfree(status
, M_TEMP
);
213 kfree(fpregset
, M_TEMP
);
220 ckpt_thaw_proc(struct lwp
*lp
, struct file
*fp
)
222 struct proc
*p
= lp
->lwp_proc
;
223 Elf_Phdr
*phdr
= NULL
;
224 Elf_Ehdr
*ehdr
= NULL
;
230 ehdr
= kmalloc(sizeof(Elf_Ehdr
), M_TEMP
, M_ZERO
| M_WAITOK
);
232 if ((error
= elf_gethdr(fp
, ehdr
)) != 0)
234 nbyte
= sizeof(Elf_Phdr
) * ehdr
->e_phnum
;
235 phdr
= kmalloc(nbyte
, M_TEMP
, M_WAITOK
);
237 /* fetch description of program writable mappings */
238 if ((error
= elf_getphdrs(fp
, phdr
, nbyte
)) != 0)
241 /* fetch notes section containing register state */
242 if ((error
= elf_getnotes(lp
, fp
, phdr
->p_filesz
)) != 0)
245 /* fetch program text vnodes */
246 if ((error
= elf_gettextvp(p
, fp
)) != 0)
249 /* fetch signal disposition */
250 if ((error
= elf_getsigs(lp
, fp
)) != 0) {
251 kprintf("failure in recovering signals\n");
255 /* fetch open files */
256 if ((error
= elf_getfiles(lp
, fp
)) != 0)
259 /* handle mappings last in case we are reading from a socket */
260 error
= elf_loadphdrs(fp
, phdr
, ehdr
->e_phnum
);
263 * Set the textvp to the checkpoint file and mark the vnode so
264 * a future checkpointing of this checkpoint-restored program
265 * will copy out the contents of the mappings rather then trying
266 * to record the vnode info related to the checkpoint file, which
267 * is likely going to be destroyed when the program is re-checkpointed.
269 if (error
== 0 && fp
->f_data
&& fp
->f_type
== DTYPE_VNODE
) {
272 p
->p_textvp
= (struct vnode
*)fp
->f_data
;
273 vsetflags(p
->p_textvp
, VCKPT
);
286 elf_loadnotes(struct lwp
*lp
, prpsinfo_t
*psinfo
, prstatus_t
*status
,
287 prfpregset_t
*fpregset
)
289 struct proc
*p
= lp
->lwp_proc
;
292 /* validate status and psinfo */
294 if (status
->pr_version
!= PRSTATUS_VERSION
||
295 status
->pr_statussz
!= sizeof(prstatus_t
) ||
296 status
->pr_gregsetsz
!= sizeof(gregset_t
) ||
297 status
->pr_fpregsetsz
!= sizeof(fpregset_t
) ||
298 psinfo
->pr_version
!= PRPSINFO_VERSION
||
299 psinfo
->pr_psinfosz
!= sizeof(prpsinfo_t
)) {
300 PRINTF(("status check failed\n"));
304 /* XXX lwp handle more than one lwp*/
305 if ((error
= set_regs(lp
, &status
->pr_reg
)) != 0)
307 error
= set_fpregs(lp
, fpregset
);
308 strlcpy(p
->p_comm
, psinfo
->pr_fname
, sizeof(p
->p_comm
));
309 /* XXX psinfo->pr_psargs not yet implemented */
316 elf_getnote(void *src
, size_t *off
, const char *name
, unsigned int type
,
317 void **desc
, size_t descsz
)
327 bcopy((char *)src
+ *off
, ¬e
, sizeof note
);
329 PRINTF(("at offset: %zd expected note of type: %d - got: %d\n",
330 *off
, type
, note
.n_type
));
332 if (type
!= note
.n_type
) {
337 if (strncmp(name
, (char *) src
+ *off
, note
.n_namesz
) != 0) {
341 *off
+= roundup2(note
.n_namesz
, sizeof(Elf_Size
));
342 if (note
.n_descsz
!= descsz
) {
348 bcopy((char *)src
+ *off
, *desc
, note
.n_descsz
);
349 *off
+= roundup2(note
.n_descsz
, sizeof(Elf_Size
));
357 elf_demarshalnotes(void *src
, prpsinfo_t
*psinfo
, prstatus_t
*status
,
358 prfpregset_t
*fpregset
, int nthreads
)
365 error
= elf_getnote(src
, &off
, "CORE", NT_PRPSINFO
,
366 (void **)&psinfo
, sizeof(prpsinfo_t
));
369 error
= elf_getnote(src
, &off
, "CORE", NT_PRSTATUS
,
370 (void **)&status
, sizeof(prstatus_t
));
373 error
= elf_getnote(src
, &off
, "CORE", NT_FPREGSET
,
374 (void **)&fpregset
, sizeof(prfpregset_t
));
379 * The remaining portion needs to be an integer multiple
380 * of prstatus_t and prfpregset_t
382 for (i
= 0 ; i
< nthreads
- 1; i
++) {
383 status
++; fpregset
++;
384 error
= elf_getnote(src
, &off
, "CORE", NT_PRSTATUS
,
385 (void **)&status
, sizeof (prstatus_t
));
388 error
= elf_getnote(src
, &off
, "CORE", NT_FPREGSET
,
389 (void **)&fpregset
, sizeof(prfpregset_t
));
401 mmap_phdr(struct file
*fp
, Elf_Phdr
*phdr
)
411 pos
= phdr
->p_offset
;
412 len
= phdr
->p_filesz
;
413 addr
= (void *)phdr
->p_vaddr
;
414 flags
= MAP_FIXED
| MAP_NOSYNC
| MAP_PRIVATE
;
416 if (phdr
->p_flags
& PF_R
)
418 if (phdr
->p_flags
& PF_W
)
420 if (phdr
->p_flags
& PF_X
)
422 if ((error
= fp_mmap(addr
, len
, prot
, flags
, fp
, pos
, &addr
)) != 0) {
423 PRINTF(("mmap failed: %d\n", error
); );
425 PRINTF(("map @%08"PRIxPTR
"-%08"PRIxPTR
" fileoff %08x-%08x\n", (uintptr_t)addr
,
426 (uintptr_t)((char *)addr
+ len
), (int)pos
, (int)(pos
+ len
)));
432 * Load memory mapped segments. The segments are backed by the checkpoint
436 elf_loadphdrs(struct file
*fp
, Elf_Phdr
*phdr
, int numsegs
)
442 for (i
= 1; i
< numsegs
; i
++) {
443 if ((error
= mmap_phdr(fp
, &phdr
[i
])) != 0)
451 elf_getsigs(struct lwp
*lp
, struct file
*fp
)
453 struct proc
*p
= lp
->lwp_proc
;
455 struct ckpt_siginfo
*csi
;
458 csi
= kmalloc(sizeof(struct ckpt_siginfo
), M_TEMP
, M_ZERO
| M_WAITOK
);
459 if ((error
= read_check(fp
, csi
, sizeof(struct ckpt_siginfo
))) != 0)
462 if (csi
->csi_ckptpisz
!= sizeof(struct ckpt_siginfo
)) {
467 bcopy(&csi
->csi_sigacts
, p
->p_sigacts
, sizeof(p
->p_sigacts
));
468 bcopy(&csi
->csi_itimerval
, &p
->p_realtimer
, sizeof(struct itimerval
));
469 SIG_CANTMASK(csi
->csi_sigmask
);
470 /* XXX lwp handle more than one lwp */
471 bcopy(&csi
->csi_sigmask
, &lp
->lwp_sigmask
, sizeof(sigset_t
));
472 p
->p_sigparent
= csi
->csi_sigparent
;
481 * Returns a locked, refd vnode
484 ckpt_fhtovp(fhandle_t
*fh
, struct vnode
**vpp
)
490 mp
= vfs_getvfs(&fh
->fh_fsid
);
494 PRINTF(("failed to get mount - ESTALE\n"));
498 error
= VFS_FHTOVP(mp
, NULL
, &fh
->fh_fid
, vpp
);
500 PRINTF(("failed with: %d\n", error
));
510 mmap_vp(struct vn_hdr
*vnh
)
518 phdr
= &vnh
->vnh_phdr
;
520 if ((error
= ckpt_fhtovp(&vnh
->vnh_fh
, &vp
)) != 0)
523 * XXX O_RDONLY -> or O_RDWR if file is PROT_WRITE, MAP_SHARED
525 if ((error
= fp_vpopen(vp
, O_RDONLY
, &fp
)) != 0) {
529 error
= mmap_phdr(fp
, phdr
);
537 elf_gettextvp(struct proc
*p
, struct file
*fp
)
542 struct ckpt_vminfo vminfo
;
543 struct vn_hdr
*vnh
= NULL
;
546 if ((error
= read_check(fp
, &vminfo
, sizeof(vminfo
))) != 0)
548 if (vminfo
.cvm_dsize
< 0 ||
549 vminfo
.cvm_dsize
> p
->p_rlimit
[RLIMIT_DATA
].rlim_cur
||
550 vminfo
.cvm_tsize
< 0 ||
551 (u_quad_t
)vminfo
.cvm_tsize
> maxtsiz
||
552 vminfo
.cvm_daddr
>= (caddr_t
)VM_MAX_USER_ADDRESS
||
553 vminfo
.cvm_taddr
>= (caddr_t
)VM_MAX_USER_ADDRESS
559 vmspace_exec(p
, NULL
);
560 p
->p_vmspace
->vm_daddr
= vminfo
.cvm_daddr
;
561 p
->p_vmspace
->vm_dsize
= vminfo
.cvm_dsize
;
562 p
->p_vmspace
->vm_taddr
= vminfo
.cvm_taddr
;
563 p
->p_vmspace
->vm_tsize
= vminfo
.cvm_tsize
;
564 if ((error
= read_check(fp
, &vpcount
, sizeof(int))) != 0)
566 vnh
= kmalloc(sizeof(struct vn_hdr
) * vpcount
, M_TEMP
, M_WAITOK
);
567 if ((error
= read_check(fp
, vnh
, sizeof(struct vn_hdr
)*vpcount
)) != 0)
569 for (i
= 0; i
< vpcount
; i
++) {
570 if ((error
= mmap_vp(&vnh
[i
])) != 0)
585 elf_getfiles(struct lwp
*lp
, struct file
*fp
)
591 struct ckpt_filehdr filehdr
;
592 struct ckpt_fileinfo
*cfi_base
= NULL
;
593 struct filedesc
*fdp
= lp
->lwp_proc
->p_fd
;
599 if ((error
= read_check(fp
, &filehdr
, sizeof(filehdr
))) != 0)
601 filecount
= filehdr
.cfh_nfiles
;
602 cfi_base
= kmalloc(filecount
*sizeof(struct ckpt_fileinfo
), M_TEMP
, M_WAITOK
);
603 error
= read_check(fp
, cfi_base
, filecount
*sizeof(struct ckpt_fileinfo
));
608 * Close all file descriptors >= 3. These descriptors are from the
609 * checkpt(1) program itself and should not be retained.
611 * XXX we need a flag so a checkpoint restore can opt to supply the
612 * descriptors, or the non-regular-file descripors.
614 for (i
= 3; i
< fdp
->fd_nfiles
; ++i
)
620 for (i
= 0; i
< filecount
; i
++) {
621 struct ckpt_fileinfo
*cfi
= &cfi_base
[i
];
623 * Ignore placeholder entries where cfi_index is less then
624 * zero. This will occur if the elf core dump code thinks
625 * it can save a vnode but winds up not being able to.
627 if (cfi
->cfi_index
< 0)
631 * Restore a saved file descriptor. If CKFIF_ISCKPTFD is
632 * set the descriptor represents the checkpoint file itself,
633 * probably due to the user calling sys_checkpoint(). We
634 * want to use the fp being used to restore the checkpoint
635 * instead of trying to restore the original filehandle.
637 if (cfi
->cfi_ckflags
& CKFIF_ISCKPTFD
) {
642 error
= ckpt_fhtovp(&cfi
->cfi_fh
, &vp
);
644 error
= fp_vpopen(vp
, OFLAGS(cfi
->cfi_flags
),
652 tempfp
->f_offset
= cfi
->cfi_offset
;
655 * If overwriting a descriptor close the old descriptor. This
656 * only occurs if the saved core saved descriptors that we
657 * have not already closed.
659 if (cfi
->cfi_index
< fdp
->fd_nfiles
&&
660 (ofp
= fdp
->fd_files
[cfi
->cfi_index
].fp
) != NULL
) {
661 kern_close(cfi
->cfi_index
);
665 * Allocate the descriptor we want.
667 if (fdalloc(lp
->lwp_proc
, cfi
->cfi_index
, &fd
) != 0) {
668 PRINTF(("can't currently restore fd: %d\n",
673 KKASSERT(fd
== cfi
->cfi_index
);
674 fsetfd(fdp
, tempfp
, fd
);
677 PRINTF(("restoring %d\n", cfi
->cfi_index
));
682 kfree(cfi_base
, M_TEMP
);
688 ckpt_freeze_proc(struct lwp
*lp
, struct file
*fp
)
690 struct proc
*p
= lp
->lwp_proc
;
694 lwkt_gettoken(&p
->p_token
); /* needed for proc_*() calls */
696 PRINTF(("calling generic_elf_coredump\n"));
697 limit
= p
->p_rlimit
[RLIMIT_CORE
].rlim_cur
;
700 while (p
->p_nstopped
< p
->p_nthreads
- 1)
701 tsleep(&p
->p_nstopped
, 0, "freeze", 1);
702 error
= generic_elf_coredump(lp
, SIGCKPT
, fp
, limit
);
707 lwkt_reltoken(&p
->p_token
);
715 sys_sys_checkpoint(struct sys_checkpoint_args
*uap
)
718 struct thread
*td
= curthread
;
719 struct proc
*p
= td
->td_proc
;
720 struct filedesc
*fdp
= p
->p_fd
;
724 * Only certain groups (to reduce our security exposure). -1
727 if (ckptgroup
>= 0 && groupmember(ckptgroup
, td
->td_ucred
) == 0)
731 * For now we can only checkpoint the current process
733 if (uap
->pid
!= -1 && uap
->pid
!= p
->p_pid
)
741 if (uap
->fd
== -1 && uap
->pid
== (pid_t
)-1)
742 error
= checkpoint_signal_handler(td
->td_lwp
);
743 else if ((fp
= holdfp(fdp
, uap
->fd
, FWRITE
)) == NULL
)
746 error
= ckpt_freeze_proc(td
->td_lwp
, fp
);
751 if (uap
->pid
!= -1) {
755 if ((fp
= holdfp(fdp
, uap
->fd
, FREAD
)) == NULL
) {
759 uap
->sysmsg_result
= uap
->retval
;
760 error
= ckpt_thaw_proc(td
->td_lwp
, fp
);
772 checkpoint_signal_handler(struct lwp
*lp
)
774 struct thread
*td
= lp
->lwp_thread
;
775 struct proc
*p
= lp
->lwp_proc
;
778 struct nlookupdata nd
;
784 * Being able to checkpoint an suid or sgid program is not a good
787 if (sugid_coredump
== 0 && (p
->p_flag
& P_SUGID
)) {
792 buf
= ckpt_expand_name(p
->p_comm
, td
->td_ucred
->cr_uid
, p
->p_pid
);
798 log(LOG_INFO
, "pid %d (%s), uid %d: checkpointing to %s\n",
800 (td
->td_ucred
? td
->td_ucred
->cr_uid
: -1),
803 PRINTF(("ckpt handler called, using '%s'\n", buf
));
806 * Use the same safety flags that the coredump code uses. Remove
807 * any previous checkpoint file before writing out the new one in
808 * case we are re-checkpointing a program that had been checkpt
809 * restored. Otherwise we will corrupt the program space (which is
810 * made up of mmap()ings of the previous checkpoint file) while we
811 * write out the new one.
813 error
= nlookup_init(&nd
, buf
, UIO_SYSSPACE
, 0);
815 error
= kern_unlink(&nd
);
817 error
= fp_open(buf
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_NOFOLLOW
, 0600, &fp
);
819 error
= ckpt_freeze_proc(lp
, fp
);
822 kprintf("checkpoint failed with open - error: %d\n", error
);
829 static char ckptfilename
[MAXPATHLEN
] = {"%N.ckpt"};
830 SYSCTL_STRING(_kern
, OID_AUTO
, ckptfile
, CTLFLAG_RW
, ckptfilename
,
831 sizeof(ckptfilename
), "process checkpoint name format string");
834 * expand_name(name, uid, pid)
835 * Expand the name described in corefilename, using name, uid, and pid.
836 * corefilename is a kprintf-like string, with three format specifiers:
837 * %N name of process ("name")
838 * %P process id (pid)
840 * For example, "%N.core" is the default; they can be disabled completely
841 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
842 * This is controlled by the sysctl variable kern.corefile (see above).
844 * -- taken from the coredump code
849 ckpt_expand_name(const char *name
, uid_t uid
, pid_t pid
)
853 char buf
[11]; /* Buffer for pid/uid -- max 4B */
857 char *format
= ckptfilename
;
860 temp
= kmalloc(MAXPATHLEN
+ 1, M_TEMP
, M_NOWAIT
);
863 namelen
= strlen(name
);
865 if (ckptfilename
[0] != '/') {
866 if ((bp
= kern_getcwd(temp
, MAXPATHLEN
- 1, &error
)) == NULL
) {
871 bcopy(bp
, temp
, n
+ 1); /* normalize location of the path */
875 for (i
= 0; n
< MAXPATHLEN
&& format
[i
]; i
++) {
878 case '%': /* Format character */
884 case 'N': /* process name */
885 if ((n
+ namelen
) > MAXPATHLEN
) {
886 log(LOG_ERR
, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
887 pid
, name
, uid
, temp
, name
);
891 memcpy(temp
+n
, name
, namelen
);
894 case 'P': /* process id */
895 l
= ksprintf(buf
, "%u", pid
);
896 if ((n
+ l
) > MAXPATHLEN
) {
897 log(LOG_ERR
, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
898 pid
, name
, uid
, temp
, name
);
902 memcpy(temp
+n
, buf
, l
);
905 case 'U': /* user id */
906 l
= ksprintf(buf
, "%u", uid
);
907 if ((n
+ l
) > MAXPATHLEN
) {
908 log(LOG_ERR
, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
909 pid
, name
, uid
, temp
, name
);
913 memcpy(temp
+n
, buf
, l
);
917 log(LOG_ERR
, "Unknown format character %c in `%s'\n", format
[i
], format
);
921 temp
[n
++] = format
[i
];