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
26 * $DragonFly: src/sys/checkpt/Attic/checkpt.c,v 1.4 2003/12/20 04:07:03 dillon Exp $
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <sys/module.h>
33 #include <sys/sysent.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
38 /* only on dragonfly */
39 #include <sys/file2.h>
40 #include <sys/fcntl.h>
41 #include <sys/signal.h>
42 #include <vm/vm_param.h>
44 #include <sys/imgact_elf.h>
45 #include <sys/procfs.h>
49 #include <vm/vm_map.h>
52 #include <sys/sysproto.h>
53 #include <sys/resource.h>
54 #include <sys/resourcevar.h>
55 #include <sys/malloc.h>
58 #include <sys/namei.h>
59 #include <sys/vnode.h>
60 #include <machine/limits.h>
61 #include <i386/include/frame.h>
62 #include <sys/signalvar.h>
63 #include <sys/syslog.h>
64 #include <sys/sysctl.h>
65 #include <i386/include/sigframe.h>
67 #include <sys/unistd.h>
70 #include <sys/mount.h>
74 static int elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs);
75 static int elf_getnotes(struct proc *p, struct file *fp, size_t notesz);
76 static int elf_demarshalnotes(void *src, prpsinfo_t *psinfo,
77 prstatus_t *status, prfpregset_t *fpregset, int nthreads);
78 static int elf_loadnotes(struct proc *, prpsinfo_t *, prstatus_t *,
80 static int elf_getsigs(struct proc *p, struct file *fp);
81 static int elf_getfiles(struct proc *p, struct file *fp);
82 static int elf_gettextvp(struct proc *p, struct file *fp);
84 static int ckptgroup = 0; /* wheel only, -1 for any group */
85 SYSCTL_INT(_kern, OID_AUTO, ckptgroup, CTLFLAG_RW, &ckptgroup, 0, "");
89 read_check(struct file *fp, void *buf, size_t nbyte)
94 PRINTF(("reading %d bytes\n", nbyte));
95 error = fp_read(fp, buf, nbyte, &nread);
97 PRINTF(("read failed - %d", error));
98 } else if (nread != nbyte) {
99 PRINTF(("wanted to read %d - read %d\n", nbyte, nread));
106 elf_gethdr(struct file *fp, Elf_Ehdr *ehdr)
108 size_t nbyte = sizeof(Elf_Ehdr);
111 if ((error = read_check(fp, ehdr, nbyte)) != 0)
113 if (!(ehdr->e_ehsize == sizeof(Elf_Ehdr))) {
114 PRINTF(("wrong elf header size: %d\n"
115 "expected size : %d\n",
116 ehdr->e_ehsize, sizeof(Elf_Ehdr)));
119 if (!(ehdr->e_phentsize == sizeof(Elf_Phdr))) {
120 PRINTF(("wrong program header size: %d\n"
121 "expected size : %d\n",
122 ehdr->e_phentsize, sizeof(Elf_Phdr)));
126 if (!(ehdr->e_ident[EI_MAG0] == ELFMAG0 &&
127 ehdr->e_ident[EI_MAG1] == ELFMAG1 &&
128 ehdr->e_ident[EI_MAG2] == ELFMAG2 &&
129 ehdr->e_ident[EI_MAG3] == ELFMAG3 &&
130 ehdr->e_ident[EI_CLASS] == ELF_CLASS &&
131 ehdr->e_ident[EI_DATA] == ELF_DATA &&
132 ehdr->e_ident[EI_VERSION] == EV_CURRENT &&
133 ehdr->e_ident[EI_OSABI] == ELFOSABI_FREEBSD &&
134 ehdr->e_ident[EI_ABIVERSION] == 0)) {
135 PRINTF(("bad elf header\n there are %d segments\n",
140 PRINTF(("Elf header size: %d\n", ehdr->e_ehsize));
141 PRINTF(("Program header size: %d\n", ehdr->e_phentsize));
142 PRINTF(("Number of Program headers: %d\n", ehdr->e_phnum));
148 elf_getphdrs(struct file *fp, Elf_Phdr *phdr, size_t nbyte)
152 int nheaders = nbyte/sizeof(Elf_Phdr);
154 PRINTF(("reading phdrs section\n"));
155 if ((error = read_check(fp, phdr, nbyte)) != 0)
157 printf("headers section:\n");
158 for (i = 0; i < nheaders; i++) {
159 printf("entry type: %d\n", phdr[i].p_type);
160 printf("file offset: %d\n", phdr[i].p_offset);
161 printf("virt address: %p\n", (uint32_t *)phdr[i].p_vaddr);
162 printf("file size: %d\n", phdr[i].p_filesz);
163 printf("memory size: %d\n", phdr[i].p_memsz);
172 elf_getnotes(struct proc *p, struct file *fp, size_t notesz)
179 prfpregset_t *fpregset;
181 nthreads = (notesz - sizeof(prpsinfo_t))/(sizeof(prstatus_t) +
182 sizeof(prfpregset_t));
183 PRINTF(("reading notes header nthreads=%d\n", nthreads));
184 if (nthreads <= 0 || nthreads > CKPT_MAXTHREADS)
187 psinfo = malloc(sizeof(prpsinfo_t), M_TEMP, M_ZERO | M_WAITOK);
188 status = malloc(nthreads*sizeof(prstatus_t), M_TEMP, M_WAITOK);
189 fpregset = malloc(nthreads*sizeof(prfpregset_t), M_TEMP, M_WAITOK);
190 note = malloc(notesz, M_TEMP, M_WAITOK);
193 PRINTF(("reading notes section\n"));
194 if ((error = read_check(fp, note, notesz)) != 0)
196 error = elf_demarshalnotes(note, psinfo, status, fpregset, nthreads);
199 /* fetch register state from notes */
200 error = elf_loadnotes(p, psinfo, status, fpregset);
203 free(psinfo, M_TEMP);
205 free(status, M_TEMP);
207 free(fpregset, M_TEMP);
214 ckpt_thaw_proc(struct proc *p, struct file *fp)
217 Elf_Phdr *phdr = NULL;
218 Elf_Ehdr *ehdr = NULL;
224 ehdr = malloc(sizeof(Elf_Ehdr), M_TEMP, M_ZERO | M_WAITOK);
226 if ((error = elf_gethdr(fp, ehdr)) != 0)
228 nbyte = sizeof(Elf_Phdr) * ehdr->e_phnum;
229 phdr = malloc(nbyte, M_TEMP, M_WAITOK);
231 /* fetch description of program writable mappings */
232 if ((error = elf_getphdrs(fp, phdr, nbyte)) != 0)
235 /* fetch notes section containing register state */
236 if ((error = elf_getnotes(p, fp, phdr->p_filesz)) != 0)
239 /* fetch program text vnodes */
240 if ((error = elf_gettextvp(p, fp)) != 0)
243 /* fetch signal disposition */
244 if ((error = elf_getsigs(p, fp)) != 0)
247 /* fetch open files */
248 if ((error = elf_getfiles(p, fp)) != 0)
251 /* handle mappings last in case we are reading from a socket */
252 error = elf_loadphdrs(fp, phdr, ehdr->e_phnum);
263 elf_loadnotes(struct proc *p, prpsinfo_t *psinfo, prstatus_t *status,
264 prfpregset_t *fpregset)
268 /* validate status and psinfo */
270 if (status->pr_version != PRSTATUS_VERSION ||
271 status->pr_statussz != sizeof(prstatus_t) ||
272 status->pr_gregsetsz != sizeof(gregset_t) ||
273 status->pr_fpregsetsz != sizeof(fpregset_t) ||
274 psinfo->pr_version != PRPSINFO_VERSION ||
275 psinfo->pr_psinfosz != sizeof(prpsinfo_t)) {
276 PRINTF(("status check failed\n"));
280 if ((error = set_regs(p, &status->pr_reg)) != 0)
282 error = set_fpregs(p, fpregset);
283 /* strncpy(psinfo->pr_psargs, p->p_comm, PRARGSZ); */
290 elf_getnote(void *src, size_t *off, const char *name, unsigned int type,
291 void **desc, size_t descsz)
301 bcopy((char *)src + *off, ¬e, sizeof note);
303 PRINTF(("at offset: %d expected note of type: %d - got: %d\n",
304 *off, type, note.n_type));
306 if (type != note.n_type) {
311 if (strncmp(name, (char *) src + *off, note.n_namesz) != 0) {
315 *off += roundup2(note.n_namesz, sizeof(Elf_Size));
316 if (note.n_descsz != descsz) {
322 bcopy((char *)src + *off, *desc, note.n_descsz);
323 *off += roundup2(note.n_descsz, sizeof(Elf_Size));
331 elf_demarshalnotes(void *src, prpsinfo_t *psinfo, prstatus_t *status,
332 prfpregset_t *fpregset, int nthreads)
339 error = elf_getnote(src, &off, "FreeBSD", NT_PRSTATUS,
340 (void **)&status, sizeof(prstatus_t));
343 error = elf_getnote(src, &off, "FreeBSD", NT_FPREGSET,
344 (void **)&fpregset, sizeof(prfpregset_t));
347 error = elf_getnote(src, &off, "FreeBSD", NT_PRPSINFO,
348 (void **)&psinfo, sizeof(prpsinfo_t));
353 * The remaining portion needs to be an integer multiple
354 * of prstatus_t and prfpregset_t
356 for (i = 0 ; i < nthreads - 1; i++) {
357 status++; fpregset++;
358 error = elf_getnote(src, &off, "FreeBSD", NT_PRSTATUS,
359 (void **)&status, sizeof (prstatus_t));
362 error = elf_getnote(src, &off, "FreeBSD", NT_FPREGSET,
363 (void **)&fpregset, sizeof(prfpregset_t));
375 mmap_phdr(struct file *fp, Elf_Phdr *phdr)
385 pos = phdr->p_offset;
386 len = phdr->p_filesz;
387 addr = (void *)phdr->p_vaddr;
388 flags = MAP_FIXED | MAP_NOSYNC | MAP_PRIVATE;
390 if (phdr->p_flags & PF_R)
392 if (phdr->p_flags & PF_W)
394 if (phdr->p_flags & PF_X)
396 if ((error = fp_mmap(addr, len, prot, flags, fp, pos, &addr)) != 0) {
397 PRINTF(("mmap failed: %d\n", error); );
399 PRINTF(("map @%08x-%08x fileoff %08x-%08x\n", (int)addr, (int)((char *)addr + len), (int)pos, (int)(pos + len)));
406 elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs)
412 for (i = 1; i < numsegs; i++) {
413 if ((error = mmap_phdr(fp, &phdr[i])) != 0)
421 elf_getsigs(struct proc *p, struct file *fp)
424 struct ckpt_siginfo *csi;
425 struct sigacts *tmpsigacts;
428 csi = malloc(sizeof(struct ckpt_siginfo), M_TEMP, M_ZERO | M_WAITOK);
429 if ((error = read_check(fp, csi, sizeof(struct ckpt_siginfo))) != 0)
432 if (csi->csi_ckptpisz != sizeof(struct ckpt_siginfo)) {
437 tmpsigacts = p->p_procsig->ps_sigacts;
438 bcopy(&csi->csi_procsig, p->p_procsig, sizeof(struct procsig));
439 p->p_procsig->ps_sigacts = tmpsigacts;
440 bcopy(&csi->csi_sigacts, p->p_procsig->ps_sigacts, sizeof(struct sigacts));
441 bcopy(&csi->csi_itimerval, &p->p_realtimer, sizeof(struct itimerval));
442 p->p_sigparent = csi->csi_sigparent;
452 ckpt_fhtovp(fhandle_t *fh, struct vnode **vpp)
458 mp = vfs_getvfs(&fh->fh_fsid);
462 PRINTF(("failed to get mount - ESTALE\n"));
466 error = VFS_FHTOVP(mp, &fh->fh_fid, vpp);
468 PRINTF(("failed with: %d\n", error));
478 mmap_vp(struct vn_hdr *vnh)
480 struct vnode **vpp, *vp;
487 phdr = &vnh->vnh_phdr;
489 if ((error = ckpt_fhtovp(&vnh->vnh_fh, vpp)) != 0)
492 * XXX O_RDONLY -> or O_RDWR if file is PROT_WRITE, MAP_SHARED
494 if ((error = fp_vpopen(*vpp, O_RDONLY, &fp)) != 0)
496 error = mmap_phdr(fp, phdr);
504 elf_gettextvp(struct proc *p, struct file *fp)
509 struct ckpt_vminfo vminfo;
510 struct vn_hdr *vnh = NULL;
513 if ((error = read_check(fp, &vminfo, sizeof(vminfo))) != 0)
515 if (vminfo.cvm_dsize < 0 ||
516 vminfo.cvm_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur ||
517 vminfo.cvm_tsize < 0 ||
518 (u_quad_t)vminfo.cvm_tsize > maxtsiz ||
519 vminfo.cvm_daddr >= (caddr_t)VM_MAXUSER_ADDRESS ||
520 vminfo.cvm_taddr >= (caddr_t)VM_MAXUSER_ADDRESS
525 p->p_vmspace->vm_daddr = vminfo.cvm_daddr;
526 p->p_vmspace->vm_dsize = vminfo.cvm_dsize;
527 p->p_vmspace->vm_taddr = vminfo.cvm_taddr;
528 p->p_vmspace->vm_tsize = vminfo.cvm_tsize;
529 if ((error = read_check(fp, &vpcount, sizeof(int))) != 0)
531 vnh = malloc(sizeof(struct vn_hdr) * vpcount, M_TEMP, M_WAITOK);
532 if ((error = read_check(fp, vnh, sizeof(struct vn_hdr)*vpcount)) != 0)
534 for (i = 0; i < vpcount; i++) {
535 if ((error = mmap_vp(&vnh[i])) != 0)
550 elf_getfiles(struct proc *p, struct file *fp)
555 struct ckpt_filehdr filehdr;
556 struct ckpt_fileinfo *cfi_base = NULL;
561 if ((error = read_check(fp, &filehdr, sizeof(filehdr))) != 0)
563 filecount = filehdr.cfh_nfiles;
564 cfi_base = malloc(filecount*sizeof(struct ckpt_fileinfo), M_TEMP, M_WAITOK);
565 error = read_check(fp, cfi_base, filecount*sizeof(struct ckpt_fileinfo));
569 for (i = 0; i < filecount; i++) {
570 struct ckpt_fileinfo *cfi= &cfi_base[i];
572 * Ignore placeholder entries where cfi_index is less then
573 * zero. This will occur if the elf core dump code thinks
574 * it can save a vnode but winds up not being able to.
576 if (cfi->cfi_index < 0)
578 if (cfi->cfi_index >= p->p_fd->fd_nfiles) {
579 PRINTF(("can't currently restore fd: %d\n",
583 if ((error = ckpt_fhtovp(&cfi->cfi_fh, &vp)) != 0)
585 if ((error = fp_vpopen(vp, OFLAGS(cfi->cfi_flags), &tempfp)) != 0)
587 tempfp->f_offset = cfi->cfi_offset;
588 /* XXX bail for now if we the index is
589 * larger than the current file table
592 PRINTF(("restoring %d\n", cfi->cfi_index));
593 p->p_fd->fd_ofiles[cfi->cfi_index] = tempfp;
599 free(cfi_base, M_TEMP);
605 ckpt_freeze_proc (struct proc *p, struct file *fp)
610 PRINTF(("calling generic_elf_coredump\n"));
611 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
613 error = generic_elf_coredump(p, fp, limit);
621 /* THIS CAN'T WORK */
623 ckpt_freeze_pid(int pid, struct file *fp)
627 if ((p = pfind(pid)) == NULL)
629 return ckpt_freeze_proc(p, fp);
634 ckpt_proc(void *uap /* struct ckpt_args */)
638 struct proc *p = curthread->td_proc;
639 struct ckpt_args *ca = (struct ckpt_args *)uap;
642 res = &((struct nosys_args *)uap)->sysmsg_result;
645 * Only certain groups (to reduce our security exposure). -1
648 if (ckptgroup >= 0 && groupmember(ckptgroup, p->p_ucred) == 0) {
652 switch (ca->args.gen.type) {
654 if ((fp = holdfp(p->p_fd, ca->args.gen.fd, FWRITE)) == NULL)
656 error = ckpt_freeze_proc(p, fp);
657 fdrop(fp, curthread);
660 if ((fp = holdfp(p->p_fd, ca->args.gen.fd, FREAD)) == NULL)
662 *res = ca->args.cta.retval;
663 error = ckpt_thaw_proc(p, fp);
664 fdrop(fp, curthread);
671 if ((fp = holdfp(p->p_fd, ca->args.gen.fd, FWRITE)) == NULL)
673 error = ckpt_freeze_pid(ca->args.cfpa.pid, fp);
674 fdrop(fp, curthread);
682 if ((fp = holdfp(p->p_fd, ca->args.gen.fd, FREAD)) == NULL)
684 error = ckpt_thaw_bin(p, fp,
685 ca->args.ctba.bin, ca->args.ctba.binlen);
686 fdrop(fp, curthread);
694 PRINTF(("error of ckpt_proc is %d, retval is %d\n", error, *res));
699 ckpt_handler(struct proc *p)
706 * Being able to checkpoint an suid or sgid program is not a good
709 if (sugid_coredump == 0 && (p->p_flag & P_SUGID))
712 buf = ckpt_expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
716 log(LOG_INFO, "pid %d (%s), uid %d: checkpointing to %s\n",
718 (p->p_ucred ? p->p_ucred->cr_uid : -1),
721 PRINTF(("ckpt handler called, using '%s'\n", buf));
724 * Use the same safety flags that the coredump code uses.
726 error = fp_open(buf, O_WRONLY|O_CREAT|O_NOFOLLOW, 0600, &fp);
728 (void)ckpt_freeze_proc(p, fp);
731 printf("checkpoint failed with open - error: %d\n", error);
738 * The `sysent' for the new syscall
740 static struct sysent ckpt_sysent = {
742 ckpt_proc /* sy_call */
745 static int ckpt_offset = 210;
748 load (struct module *module, int cmd, void *arg)
754 PRINTF( ("ckpt loaded at %d\n", ckpt_offset));
755 (void)register_ckpt_func(ckpt_handler);
758 PRINTF( ("ckpt unloaded from %d\n", ckpt_offset); );
759 /* if we are unloaded while a process is being checkpointed
760 * the kernel will likely crash XXX
762 (void)register_ckpt_func(NULL);
771 SYSCALL_MODULE(syscall, &ckpt_offset, &ckpt_sysent, load, NULL);