Get rid of struct user/UAREA.
[dragonfly.git] / sys / kern / kern_checkpoint.c
blob4e23340e882b9ed940e6ed60b487f2f6f7e19b68
1 /*-
2 * Copyright (c) 2003 Kip Macy
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 * $DragonFly: src/sys/kern/kern_checkpoint.c,v 1.18 2007/02/25 23:17:12 corecode Exp $
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/proc.h>
32 #include <sys/module.h>
33 #include <sys/sysent.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/nlookup.h>
38 #include <sys/file.h>
39 /* only on dragonfly */
40 #include <sys/file2.h>
41 #include <sys/fcntl.h>
42 #include <sys/signal.h>
43 #include <vm/vm_param.h>
44 #include <vm/vm.h>
45 #include <sys/imgact_elf.h>
46 #include <sys/procfs.h>
48 #include <sys/lock.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_extern.h>
52 #include <sys/mman.h>
53 #include <sys/sysent.h>
54 #include <sys/sysproto.h>
55 #include <sys/resource.h>
56 #include <sys/resourcevar.h>
57 #include <sys/malloc.h>
58 #include <sys/stat.h>
59 #include <sys/uio.h>
60 #include <sys/namei.h>
61 #include <sys/vnode.h>
62 #include <machine/limits.h>
63 #include <machine/frame.h>
64 #include <sys/signalvar.h>
65 #include <sys/syslog.h>
66 #include <sys/sysctl.h>
67 #include <machine/sigframe.h>
68 #include <sys/exec.h>
69 #include <sys/unistd.h>
70 #include <sys/time.h>
71 #include <sys/kern_syscall.h>
72 #include <sys/checkpoint.h>
73 #include <sys/mount.h>
74 #include <sys/ckpt.h>
77 static int elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs);
78 static int elf_getnotes(struct lwp *lp, struct file *fp, size_t notesz);
79 static int elf_demarshalnotes(void *src, prpsinfo_t *psinfo,
80 prstatus_t *status, prfpregset_t *fpregset, int nthreads);
81 static int elf_loadnotes(struct lwp *, prpsinfo_t *, prstatus_t *,
82 prfpregset_t *);
83 static int elf_getsigs(struct lwp *lp, struct file *fp);
84 static int elf_getfiles(struct proc *p, struct file *fp);
85 static int elf_gettextvp(struct proc *p, struct file *fp);
86 static char *ckpt_expand_name(const char *name, uid_t uid, pid_t pid);
88 static int ckptgroup = 0; /* wheel only, -1 for any group */
89 SYSCTL_INT(_kern, OID_AUTO, ckptgroup, CTLFLAG_RW, &ckptgroup, 0, "");
91 /* ref count to see how many processes that are being checkpointed */
92 static int chptinuse = 0;
94 static __inline
95 int
96 read_check(struct file *fp, void *buf, size_t nbyte)
98 size_t nread;
99 int error;
101 PRINTF(("reading %d bytes\n", nbyte));
102 error = fp_read(fp, buf, nbyte, &nread, 1, UIO_SYSSPACE);
103 if (error) {
104 PRINTF(("read failed - %d", error));
105 } else if (nread != nbyte) {
106 PRINTF(("wanted to read %d - read %d\n", nbyte, nread));
107 error = EINVAL;
109 return error;
112 static int
113 elf_gethdr(struct file *fp, Elf_Ehdr *ehdr)
115 size_t nbyte = sizeof(Elf_Ehdr);
116 int error;
118 if ((error = read_check(fp, ehdr, nbyte)) != 0)
119 goto done;
120 if (!(ehdr->e_ehsize == sizeof(Elf_Ehdr))) {
121 PRINTF(("wrong elf header size: %d\n"
122 "expected size : %d\n",
123 ehdr->e_ehsize, sizeof(Elf_Ehdr)));
124 return EINVAL;
126 if (!(ehdr->e_phentsize == sizeof(Elf_Phdr))) {
127 PRINTF(("wrong program header size: %d\n"
128 "expected size : %d\n",
129 ehdr->e_phentsize, sizeof(Elf_Phdr)));
130 return EINVAL;
133 if (!(ehdr->e_ident[EI_MAG0] == ELFMAG0 &&
134 ehdr->e_ident[EI_MAG1] == ELFMAG1 &&
135 ehdr->e_ident[EI_MAG2] == ELFMAG2 &&
136 ehdr->e_ident[EI_MAG3] == ELFMAG3 &&
137 ehdr->e_ident[EI_CLASS] == ELF_CLASS &&
138 ehdr->e_ident[EI_DATA] == ELF_DATA &&
139 ehdr->e_ident[EI_VERSION] == EV_CURRENT &&
140 ehdr->e_ident[EI_OSABI] == ELFOSABI_FREEBSD &&
141 ehdr->e_ident[EI_ABIVERSION] == 0)) {
142 PRINTF(("bad elf header\n there are %d segments\n",
143 ehdr->e_phnum));
144 return EINVAL;
147 PRINTF(("Elf header size: %d\n", ehdr->e_ehsize));
148 PRINTF(("Program header size: %d\n", ehdr->e_phentsize));
149 PRINTF(("Number of Program headers: %d\n", ehdr->e_phnum));
150 done:
151 return error;
154 static int
155 elf_getphdrs(struct file *fp, Elf_Phdr *phdr, size_t nbyte)
157 int i;
158 int error;
159 int nheaders = nbyte/sizeof(Elf_Phdr);
161 PRINTF(("reading phdrs section\n"));
162 if ((error = read_check(fp, phdr, nbyte)) != 0)
163 goto done;
164 kprintf("headers section:\n");
165 for (i = 0; i < nheaders; i++) {
166 kprintf("entry type: %d\n", phdr[i].p_type);
167 kprintf("file offset: %d\n", phdr[i].p_offset);
168 kprintf("virt address: %p\n", (uint32_t *)phdr[i].p_vaddr);
169 kprintf("file size: %d\n", phdr[i].p_filesz);
170 kprintf("memory size: %d\n", phdr[i].p_memsz);
171 kprintf("\n");
173 done:
174 return error;
178 static int
179 elf_getnotes(struct lwp *lp, struct file *fp, size_t notesz)
181 int error;
182 int nthreads;
183 char *note;
184 prpsinfo_t *psinfo;
185 prstatus_t *status;
186 prfpregset_t *fpregset;
188 nthreads = (notesz - sizeof(prpsinfo_t))/(sizeof(prstatus_t) +
189 sizeof(prfpregset_t));
190 PRINTF(("reading notes header nthreads=%d\n", nthreads));
191 if (nthreads <= 0 || nthreads > CKPT_MAXTHREADS)
192 return EINVAL;
194 psinfo = kmalloc(sizeof(prpsinfo_t), M_TEMP, M_ZERO | M_WAITOK);
195 status = kmalloc(nthreads*sizeof(prstatus_t), M_TEMP, M_WAITOK);
196 fpregset = kmalloc(nthreads*sizeof(prfpregset_t), M_TEMP, M_WAITOK);
197 note = kmalloc(notesz, M_TEMP, M_WAITOK);
200 PRINTF(("reading notes section\n"));
201 if ((error = read_check(fp, note, notesz)) != 0)
202 goto done;
203 error = elf_demarshalnotes(note, psinfo, status, fpregset, nthreads);
204 if (error)
205 goto done;
206 /* fetch register state from notes */
207 error = elf_loadnotes(lp, psinfo, status, fpregset);
208 done:
209 if (psinfo)
210 kfree(psinfo, M_TEMP);
211 if (status)
212 kfree(status, M_TEMP);
213 if (fpregset)
214 kfree(fpregset, M_TEMP);
215 if (note)
216 kfree(note, M_TEMP);
217 return error;
220 static int
221 ckpt_thaw_proc(struct lwp *lp, struct file *fp)
223 struct proc *p = lp->lwp_proc;
224 Elf_Phdr *phdr = NULL;
225 Elf_Ehdr *ehdr = NULL;
226 int error;
227 size_t nbyte;
229 TRACE_ENTER;
231 ehdr = kmalloc(sizeof(Elf_Ehdr), M_TEMP, M_ZERO | M_WAITOK);
233 if ((error = elf_gethdr(fp, ehdr)) != 0)
234 goto done;
235 nbyte = sizeof(Elf_Phdr) * ehdr->e_phnum;
236 phdr = kmalloc(nbyte, M_TEMP, M_WAITOK);
238 /* fetch description of program writable mappings */
239 if ((error = elf_getphdrs(fp, phdr, nbyte)) != 0)
240 goto done;
242 /* fetch notes section containing register state */
243 if ((error = elf_getnotes(lp, fp, phdr->p_filesz)) != 0)
244 goto done;
246 /* fetch program text vnodes */
247 if ((error = elf_gettextvp(p, fp)) != 0)
248 goto done;
250 /* fetch signal disposition */
251 if ((error = elf_getsigs(lp, fp)) != 0) {
252 kprintf("failure in recovering signals\n");
253 goto done;
256 /* fetch open files */
257 if ((error = elf_getfiles(p, fp)) != 0)
258 goto done;
260 /* handle mappings last in case we are reading from a socket */
261 error = elf_loadphdrs(fp, phdr, ehdr->e_phnum);
264 * Set the textvp to the checkpoint file and mark the vnode so
265 * a future checkpointing of this checkpoint-restored program
266 * will copy out the contents of the mappings rather then trying
267 * to record the vnode info related to the checkpoint file, which
268 * is likely going to be destroyed when the program is re-checkpointed.
270 if (error == 0 && fp->f_data && fp->f_type == DTYPE_VNODE) {
271 if (p->p_textvp)
272 vrele(p->p_textvp);
273 p->p_textvp = (struct vnode *)fp->f_data;
274 p->p_textvp->v_flag |= VCKPT;
275 vref(p->p_textvp);
277 done:
278 if (ehdr)
279 kfree(ehdr, M_TEMP);
280 if (phdr)
281 kfree(phdr, M_TEMP);
282 TRACE_EXIT;
283 return error;
286 static int
287 elf_loadnotes(struct lwp *lp, prpsinfo_t *psinfo, prstatus_t *status,
288 prfpregset_t *fpregset)
290 struct proc *p = lp->lwp_proc;
291 int error;
293 /* validate status and psinfo */
294 TRACE_ENTER;
295 if (status->pr_version != PRSTATUS_VERSION ||
296 status->pr_statussz != sizeof(prstatus_t) ||
297 status->pr_gregsetsz != sizeof(gregset_t) ||
298 status->pr_fpregsetsz != sizeof(fpregset_t) ||
299 psinfo->pr_version != PRPSINFO_VERSION ||
300 psinfo->pr_psinfosz != sizeof(prpsinfo_t)) {
301 PRINTF(("status check failed\n"));
302 error = EINVAL;
303 goto done;
305 /* XXX lwp handle more than one lwp*/
306 if ((error = set_regs(lp, &status->pr_reg)) != 0)
307 goto done;
308 error = set_fpregs(lp, fpregset);
309 strlcpy(p->p_comm, psinfo->pr_fname, sizeof(p->p_comm));
310 /* XXX psinfo->pr_psargs not yet implemented */
311 done:
312 TRACE_EXIT;
313 return error;
316 static int
317 elf_getnote(void *src, size_t *off, const char *name, unsigned int type,
318 void **desc, size_t descsz)
320 Elf_Note note;
321 int error;
323 TRACE_ENTER;
324 if (src == NULL) {
325 error = EFAULT;
326 goto done;
328 bcopy((char *)src + *off, &note, sizeof note);
330 PRINTF(("at offset: %d expected note of type: %d - got: %d\n",
331 *off, type, note.n_type));
332 *off += sizeof note;
333 if (type != note.n_type) {
334 TRACE_ERR;
335 error = EINVAL;
336 goto done;
338 if (strncmp(name, (char *) src + *off, note.n_namesz) != 0) {
339 error = EINVAL;
340 goto done;
342 *off += roundup2(note.n_namesz, sizeof(Elf_Size));
343 if (note.n_descsz != descsz) {
344 TRACE_ERR;
345 error = EINVAL;
346 goto done;
348 if (desc)
349 bcopy((char *)src + *off, *desc, note.n_descsz);
350 *off += roundup2(note.n_descsz, sizeof(Elf_Size));
351 error = 0;
352 done:
353 TRACE_EXIT;
354 return error;
357 static int
358 elf_demarshalnotes(void *src, prpsinfo_t *psinfo, prstatus_t *status,
359 prfpregset_t *fpregset, int nthreads)
361 int i;
362 int error;
363 int off = 0;
365 TRACE_ENTER;
366 error = elf_getnote(src, &off, "FreeBSD", NT_PRSTATUS,
367 (void **)&status, sizeof(prstatus_t));
368 if (error)
369 goto done;
370 error = elf_getnote(src, &off, "FreeBSD", NT_FPREGSET,
371 (void **)&fpregset, sizeof(prfpregset_t));
372 if (error)
373 goto done;
374 error = elf_getnote(src, &off, "FreeBSD", NT_PRPSINFO,
375 (void **)&psinfo, sizeof(prpsinfo_t));
376 if (error)
377 goto done;
380 * The remaining portion needs to be an integer multiple
381 * of prstatus_t and prfpregset_t
383 for (i = 0 ; i < nthreads - 1; i++) {
384 status++; fpregset++;
385 error = elf_getnote(src, &off, "FreeBSD", NT_PRSTATUS,
386 (void **)&status, sizeof (prstatus_t));
387 if (error)
388 goto done;
389 error = elf_getnote(src, &off, "FreeBSD", NT_FPREGSET,
390 (void **)&fpregset, sizeof(prfpregset_t));
391 if (error)
392 goto done;
395 done:
396 TRACE_EXIT;
397 return error;
401 static int
402 mmap_phdr(struct file *fp, Elf_Phdr *phdr)
404 int error;
405 size_t len;
406 int prot;
407 void *addr;
408 int flags;
409 off_t pos;
411 TRACE_ENTER;
412 pos = phdr->p_offset;
413 len = phdr->p_filesz;
414 addr = (void *)phdr->p_vaddr;
415 flags = MAP_FIXED | MAP_NOSYNC | MAP_PRIVATE;
416 prot = 0;
417 if (phdr->p_flags & PF_R)
418 prot |= PROT_READ;
419 if (phdr->p_flags & PF_W)
420 prot |= PROT_WRITE;
421 if (phdr->p_flags & PF_X)
422 prot |= PROT_EXEC;
423 if ((error = fp_mmap(addr, len, prot, flags, fp, pos, &addr)) != 0) {
424 PRINTF(("mmap failed: %d\n", error); );
426 PRINTF(("map @%08x-%08x fileoff %08x-%08x\n", (int)addr,
427 (int)((char *)addr + len), (int)pos, (int)(pos + len)));
428 TRACE_EXIT;
429 return error;
433 static int
434 elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs)
436 int i;
437 int error = 0;
439 TRACE_ENTER;
440 for (i = 1; i < numsegs; i++) {
441 if ((error = mmap_phdr(fp, &phdr[i])) != 0)
442 break;
444 TRACE_EXIT;
445 return error;
448 static int
449 elf_getsigs(struct lwp *lp, struct file *fp)
451 struct proc *p = lp->lwp_proc;
452 int error;
453 struct ckpt_siginfo *csi;
455 TRACE_ENTER;
456 csi = kmalloc(sizeof(struct ckpt_siginfo), M_TEMP, M_ZERO | M_WAITOK);
457 if ((error = read_check(fp, csi, sizeof(struct ckpt_siginfo))) != 0)
458 goto done;
460 if (csi->csi_ckptpisz != sizeof(struct ckpt_siginfo)) {
461 TRACE_ERR;
462 error = EINVAL;
463 goto done;
465 bcopy(&csi->csi_sigacts, p->p_sigacts, sizeof(p->p_sigacts));
466 bcopy(&csi->csi_itimerval, &p->p_realtimer, sizeof(struct itimerval));
467 SIG_CANTMASK(csi->csi_sigmask);
468 /* XXX lwp handle more than one lwp */
469 bcopy(&csi->csi_sigmask, &lp->lwp_sigmask, sizeof(sigset_t));
470 p->p_sigparent = csi->csi_sigparent;
471 done:
472 if (csi)
473 kfree(csi, M_TEMP);
474 TRACE_EXIT;
475 return error;
479 * Returns a locked, refd vnode
481 static int
482 ckpt_fhtovp(fhandle_t *fh, struct vnode **vpp)
484 struct mount *mp;
485 int error;
487 TRACE_ENTER;
488 mp = vfs_getvfs(&fh->fh_fsid);
490 if (!mp) {
491 TRACE_ERR;
492 PRINTF(("failed to get mount - ESTALE\n"));
493 TRACE_EXIT;
494 return ESTALE;
496 error = VFS_FHTOVP(mp, &fh->fh_fid, vpp);
497 if (error) {
498 PRINTF(("failed with: %d\n", error));
499 TRACE_ERR;
500 TRACE_EXIT;
501 return error;
503 TRACE_EXIT;
504 return 0;
507 static int
508 mmap_vp(struct vn_hdr *vnh)
510 struct vnode *vp;
511 Elf_Phdr *phdr;
512 struct file *fp;
513 int error;
514 TRACE_ENTER;
516 phdr = &vnh->vnh_phdr;
518 if ((error = ckpt_fhtovp(&vnh->vnh_fh, &vp)) != 0)
519 return error;
521 * XXX O_RDONLY -> or O_RDWR if file is PROT_WRITE, MAP_SHARED
523 if ((error = fp_vpopen(vp, O_RDONLY, &fp)) != 0) {
524 vput(vp);
525 return error;
527 error = mmap_phdr(fp, phdr);
528 fp_close(fp);
529 TRACE_EXIT;
530 return error;
534 static int
535 elf_gettextvp(struct proc *p, struct file *fp)
537 int i;
538 int error;
539 int vpcount;
540 struct ckpt_vminfo vminfo;
541 struct vn_hdr *vnh = NULL;
543 TRACE_ENTER;
544 if ((error = read_check(fp, &vminfo, sizeof(vminfo))) != 0)
545 goto done;
546 if (vminfo.cvm_dsize < 0 ||
547 vminfo.cvm_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur ||
548 vminfo.cvm_tsize < 0 ||
549 (u_quad_t)vminfo.cvm_tsize > maxtsiz ||
550 vminfo.cvm_daddr >= (caddr_t)VM_MAX_USER_ADDRESS ||
551 vminfo.cvm_taddr >= (caddr_t)VM_MAX_USER_ADDRESS
553 error = ERANGE;
554 goto done;
557 vmspace_exec(p, NULL);
558 p->p_vmspace->vm_daddr = vminfo.cvm_daddr;
559 p->p_vmspace->vm_dsize = vminfo.cvm_dsize;
560 p->p_vmspace->vm_taddr = vminfo.cvm_taddr;
561 p->p_vmspace->vm_tsize = vminfo.cvm_tsize;
562 if ((error = read_check(fp, &vpcount, sizeof(int))) != 0)
563 goto done;
564 vnh = kmalloc(sizeof(struct vn_hdr) * vpcount, M_TEMP, M_WAITOK);
565 if ((error = read_check(fp, vnh, sizeof(struct vn_hdr)*vpcount)) != 0)
566 goto done;
567 for (i = 0; i < vpcount; i++) {
568 if ((error = mmap_vp(&vnh[i])) != 0)
569 goto done;
572 done:
573 if (vnh)
574 kfree(vnh, M_TEMP);
575 TRACE_EXIT;
576 return error;
581 /* place holder */
582 static int
583 elf_getfiles(struct proc *p, struct file *fp)
585 int error;
586 int i;
587 int filecount;
588 int fd;
589 struct ckpt_filehdr filehdr;
590 struct ckpt_fileinfo *cfi_base = NULL;
591 struct vnode *vp;
592 struct file *tempfp;
593 struct file *ofp;
595 TRACE_ENTER;
596 if ((error = read_check(fp, &filehdr, sizeof(filehdr))) != 0)
597 goto done;
598 filecount = filehdr.cfh_nfiles;
599 cfi_base = kmalloc(filecount*sizeof(struct ckpt_fileinfo), M_TEMP, M_WAITOK);
600 error = read_check(fp, cfi_base, filecount*sizeof(struct ckpt_fileinfo));
601 if (error)
602 goto done;
605 * Close all file descriptors >= 3. These descriptors are from the
606 * checkpt(1) program itself and should not be retained.
608 * XXX we need a flag so a checkpoint restore can opt to supply the
609 * descriptors, or the non-regular-file descripors.
611 for (i = 3; i < p->p_fd->fd_nfiles; ++i)
612 kern_close(i);
615 * Scan files to load
617 for (i = 0; i < filecount; i++) {
618 struct ckpt_fileinfo *cfi= &cfi_base[i];
620 * Ignore placeholder entries where cfi_index is less then
621 * zero. This will occur if the elf core dump code thinks
622 * it can save a vnode but winds up not being able to.
624 if (cfi->cfi_index < 0)
625 continue;
627 if ((error = ckpt_fhtovp(&cfi->cfi_fh, &vp)) != 0)
628 break;
629 if ((error = fp_vpopen(vp, OFLAGS(cfi->cfi_flags), &tempfp)) != 0) {
630 vput(vp);
631 break;
633 tempfp->f_offset = cfi->cfi_offset;
636 * If overwriting a descriptor close the old descriptor. This
637 * only occurs if the saved core saved descriptors that we
638 * have not already closed.
640 if (cfi->cfi_index < p->p_fd->fd_nfiles &&
641 (ofp = p->p_fd->fd_files[cfi->cfi_index].fp) != NULL) {
642 kern_close(cfi->cfi_index);
646 * Allocate the descriptor we want.
648 if (fdalloc(p, cfi->cfi_index, &fd) != 0) {
649 PRINTF(("can't currently restore fd: %d\n",
650 cfi->cfi_index));
651 fp_close(fp);
652 goto done;
654 KKASSERT(fd == cfi->cfi_index);
655 fsetfd(p, tempfp, fd);
656 fdrop(tempfp);
657 cfi++;
658 PRINTF(("restoring %d\n", cfi->cfi_index));
661 done:
662 if (cfi_base)
663 kfree(cfi_base, M_TEMP);
664 TRACE_EXIT;
665 return error;
668 static int
669 ckpt_freeze_proc(struct lwp *lp, struct file *fp)
671 struct proc *p = lp->lwp_proc;
672 rlim_t limit;
673 int error;
675 PRINTF(("calling generic_elf_coredump\n"));
676 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
677 if (limit) {
678 error = generic_elf_coredump(lp, SIGCKPT, fp, limit);
679 } else {
680 error = ERANGE;
682 return error;
685 int
686 sys_sys_checkpoint(struct sys_checkpoint_args *uap)
688 int error = 0;
689 struct lwp *lp = curthread->td_lwp;
690 struct proc *p = curthread->td_proc;
691 struct file *fp;
694 * Only certain groups (to reduce our security exposure). -1
695 * allows any group.
697 if (ckptgroup >= 0 && groupmember(ckptgroup, p->p_ucred) == 0)
698 return (EPERM);
701 * For now we can only checkpoint the current process
703 if (uap->pid != -1 && uap->pid != p->p_pid)
704 return (EINVAL);
706 switch (uap->type) {
707 case CKPT_FREEZE:
708 fp = NULL;
709 if (uap->fd == -1 && uap->pid == (pid_t)-1)
710 error = checkpoint_signal_handler(lp);
711 else if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
712 error = EBADF;
713 else
714 error = ckpt_freeze_proc(lp, fp);
715 if (fp)
716 fdrop(fp);
717 break;
718 case CKPT_THAW:
719 if (uap->pid != -1)
720 return EINVAL;
721 if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
722 return EBADF;
723 uap->sysmsg_result = uap->retval;
724 error = ckpt_thaw_proc(lp, fp);
725 fdrop(fp);
726 break;
727 default:
728 error = EOPNOTSUPP;
729 break;
731 return error;
735 checkpoint_signal_handler(struct lwp *lp)
737 struct proc *p = lp->lwp_proc;
738 char *buf;
739 struct file *fp;
740 struct nlookupdata nd;
741 int error;
743 chptinuse++;
746 * Being able to checkpoint an suid or sgid program is not a good
747 * idea.
749 if (sugid_coredump == 0 && (p->p_flag & P_SUGID)) {
750 chptinuse--;
751 return (EPERM);
754 buf = ckpt_expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
755 if (buf == NULL) {
756 chptinuse--;
757 return (ENOMEM);
760 log(LOG_INFO, "pid %d (%s), uid %d: checkpointing to %s\n",
761 p->p_pid, p->p_comm,
762 (p->p_ucred ? p->p_ucred->cr_uid : -1),
763 buf);
765 PRINTF(("ckpt handler called, using '%s'\n", buf));
768 * Use the same safety flags that the coredump code uses. Remove
769 * any previous checkpoint file before writing out the new one in
770 * case we are re-checkpointing a program that had been checkpt
771 * restored. Otherwise we will corrupt the program space (which is
772 * made up of mmap()ings of the previous checkpoint file) while we
773 * write out the new one.
775 error = nlookup_init(&nd, buf, UIO_SYSSPACE, 0);
776 if (error == 0)
777 error = kern_unlink(&nd);
778 nlookup_done(&nd);
779 error = fp_open(buf, O_WRONLY|O_CREAT|O_TRUNC|O_NOFOLLOW, 0600, &fp);
780 if (error == 0) {
781 error = ckpt_freeze_proc(lp, fp);
782 fp_close(fp);
783 } else {
784 kprintf("checkpoint failed with open - error: %d\n", error);
786 kfree(buf, M_TEMP);
787 chptinuse--;
788 return (error);
791 static char ckptfilename[MAXPATHLEN] = {"%N.ckpt"};
792 SYSCTL_STRING(_kern, OID_AUTO, ckptfile, CTLFLAG_RW, ckptfilename,
793 sizeof(ckptfilename), "process checkpoint name format string");
796 * expand_name(name, uid, pid)
797 * Expand the name described in corefilename, using name, uid, and pid.
798 * corefilename is a kprintf-like string, with three format specifiers:
799 * %N name of process ("name")
800 * %P process id (pid)
801 * %U user id (uid)
802 * For example, "%N.core" is the default; they can be disabled completely
803 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
804 * This is controlled by the sysctl variable kern.corefile (see above).
806 * -- taken from the coredump code
809 static
810 char *
811 ckpt_expand_name(const char *name, uid_t uid, pid_t pid)
813 char *temp;
814 char *bp;
815 char buf[11]; /* Buffer for pid/uid -- max 4B */
816 int error;
817 int i;
818 int n;
819 char *format = ckptfilename;
820 size_t namelen;
822 temp = kmalloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
823 if (temp == NULL)
824 return NULL;
825 namelen = strlen(name);
826 n = 0;
827 if (ckptfilename[0] != '/') {
828 if ((bp = kern_getcwd(temp, MAXPATHLEN - 1, &error)) == NULL) {
829 kfree(temp, M_TEMP);
830 return NULL;
832 n = strlen(bp);
833 bcopy(bp, temp, n + 1); /* normalize location of the path */
834 temp[n++] = '/';
835 temp[n] = '\0';
837 for (i= 0; n < MAXPATHLEN && format[i]; i++) {
838 int l;
839 switch (format[i]) {
840 case '%': /* Format character */
841 i++;
842 switch (format[i]) {
843 case '%':
844 temp[n++] = '%';
845 break;
846 case 'N': /* process name */
847 if ((n + namelen) > MAXPATHLEN) {
848 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
849 pid, name, uid, temp, name);
850 kfree(temp, M_TEMP);
851 return NULL;
853 memcpy(temp+n, name, namelen);
854 n += namelen;
855 break;
856 case 'P': /* process id */
857 l = ksprintf(buf, "%u", pid);
858 if ((n + l) > MAXPATHLEN) {
859 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
860 pid, name, uid, temp, name);
861 kfree(temp, M_TEMP);
862 return NULL;
864 memcpy(temp+n, buf, l);
865 n += l;
866 break;
867 case 'U': /* user id */
868 l = ksprintf(buf, "%u", uid);
869 if ((n + l) > MAXPATHLEN) {
870 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
871 pid, name, uid, temp, name);
872 kfree(temp, M_TEMP);
873 return NULL;
875 memcpy(temp+n, buf, l);
876 n += l;
877 break;
878 default:
879 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
881 break;
882 default:
883 temp[n++] = format[i];
886 temp[n] = '\0';
887 return temp;