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.5 2004/06/03 10:00:06 eirikn 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, "");
87 /* ref count to see how many processes that are being checkpointed */
88 static int chptinuse = 0;
92 read_check(struct file *fp, void *buf, size_t nbyte)
97 PRINTF(("reading %d bytes\n", nbyte));
98 error = fp_read(fp, buf, nbyte, &nread);
100 PRINTF(("read failed - %d", error));
101 } else if (nread != nbyte) {
102 PRINTF(("wanted to read %d - read %d\n", nbyte, nread));
109 elf_gethdr(struct file *fp, Elf_Ehdr *ehdr)
111 size_t nbyte = sizeof(Elf_Ehdr);
114 if ((error = read_check(fp, ehdr, nbyte)) != 0)
116 if (!(ehdr->e_ehsize == sizeof(Elf_Ehdr))) {
117 PRINTF(("wrong elf header size: %d\n"
118 "expected size : %d\n",
119 ehdr->e_ehsize, sizeof(Elf_Ehdr)));
122 if (!(ehdr->e_phentsize == sizeof(Elf_Phdr))) {
123 PRINTF(("wrong program header size: %d\n"
124 "expected size : %d\n",
125 ehdr->e_phentsize, sizeof(Elf_Phdr)));
129 if (!(ehdr->e_ident[EI_MAG0] == ELFMAG0 &&
130 ehdr->e_ident[EI_MAG1] == ELFMAG1 &&
131 ehdr->e_ident[EI_MAG2] == ELFMAG2 &&
132 ehdr->e_ident[EI_MAG3] == ELFMAG3 &&
133 ehdr->e_ident[EI_CLASS] == ELF_CLASS &&
134 ehdr->e_ident[EI_DATA] == ELF_DATA &&
135 ehdr->e_ident[EI_VERSION] == EV_CURRENT &&
136 ehdr->e_ident[EI_OSABI] == ELFOSABI_FREEBSD &&
137 ehdr->e_ident[EI_ABIVERSION] == 0)) {
138 PRINTF(("bad elf header\n there are %d segments\n",
143 PRINTF(("Elf header size: %d\n", ehdr->e_ehsize));
144 PRINTF(("Program header size: %d\n", ehdr->e_phentsize));
145 PRINTF(("Number of Program headers: %d\n", ehdr->e_phnum));
151 elf_getphdrs(struct file *fp, Elf_Phdr *phdr, size_t nbyte)
155 int nheaders = nbyte/sizeof(Elf_Phdr);
157 PRINTF(("reading phdrs section\n"));
158 if ((error = read_check(fp, phdr, nbyte)) != 0)
160 printf("headers section:\n");
161 for (i = 0; i < nheaders; i++) {
162 printf("entry type: %d\n", phdr[i].p_type);
163 printf("file offset: %d\n", phdr[i].p_offset);
164 printf("virt address: %p\n", (uint32_t *)phdr[i].p_vaddr);
165 printf("file size: %d\n", phdr[i].p_filesz);
166 printf("memory size: %d\n", phdr[i].p_memsz);
175 elf_getnotes(struct proc *p, struct file *fp, size_t notesz)
182 prfpregset_t *fpregset;
184 nthreads = (notesz - sizeof(prpsinfo_t))/(sizeof(prstatus_t) +
185 sizeof(prfpregset_t));
186 PRINTF(("reading notes header nthreads=%d\n", nthreads));
187 if (nthreads <= 0 || nthreads > CKPT_MAXTHREADS)
190 psinfo = malloc(sizeof(prpsinfo_t), M_TEMP, M_ZERO | M_WAITOK);
191 status = malloc(nthreads*sizeof(prstatus_t), M_TEMP, M_WAITOK);
192 fpregset = malloc(nthreads*sizeof(prfpregset_t), M_TEMP, M_WAITOK);
193 note = malloc(notesz, M_TEMP, M_WAITOK);
196 PRINTF(("reading notes section\n"));
197 if ((error = read_check(fp, note, notesz)) != 0)
199 error = elf_demarshalnotes(note, psinfo, status, fpregset, nthreads);
202 /* fetch register state from notes */
203 error = elf_loadnotes(p, psinfo, status, fpregset);
206 free(psinfo, M_TEMP);
208 free(status, M_TEMP);
210 free(fpregset, M_TEMP);
217 ckpt_thaw_proc(struct proc *p, struct file *fp)
220 Elf_Phdr *phdr = NULL;
221 Elf_Ehdr *ehdr = NULL;
227 ehdr = malloc(sizeof(Elf_Ehdr), M_TEMP, M_ZERO | M_WAITOK);
229 if ((error = elf_gethdr(fp, ehdr)) != 0)
231 nbyte = sizeof(Elf_Phdr) * ehdr->e_phnum;
232 phdr = malloc(nbyte, M_TEMP, M_WAITOK);
234 /* fetch description of program writable mappings */
235 if ((error = elf_getphdrs(fp, phdr, nbyte)) != 0)
238 /* fetch notes section containing register state */
239 if ((error = elf_getnotes(p, fp, phdr->p_filesz)) != 0)
242 /* fetch program text vnodes */
243 if ((error = elf_gettextvp(p, fp)) != 0)
246 /* fetch signal disposition */
247 if ((error = elf_getsigs(p, fp)) != 0)
250 /* fetch open files */
251 if ((error = elf_getfiles(p, fp)) != 0)
254 /* handle mappings last in case we are reading from a socket */
255 error = elf_loadphdrs(fp, phdr, ehdr->e_phnum);
266 elf_loadnotes(struct proc *p, prpsinfo_t *psinfo, prstatus_t *status,
267 prfpregset_t *fpregset)
271 /* validate status and psinfo */
273 if (status->pr_version != PRSTATUS_VERSION ||
274 status->pr_statussz != sizeof(prstatus_t) ||
275 status->pr_gregsetsz != sizeof(gregset_t) ||
276 status->pr_fpregsetsz != sizeof(fpregset_t) ||
277 psinfo->pr_version != PRPSINFO_VERSION ||
278 psinfo->pr_psinfosz != sizeof(prpsinfo_t)) {
279 PRINTF(("status check failed\n"));
283 if ((error = set_regs(p, &status->pr_reg)) != 0)
285 error = set_fpregs(p, fpregset);
286 /* strncpy(psinfo->pr_psargs, p->p_comm, PRARGSZ); */
293 elf_getnote(void *src, size_t *off, const char *name, unsigned int type,
294 void **desc, size_t descsz)
304 bcopy((char *)src + *off, ¬e, sizeof note);
306 PRINTF(("at offset: %d expected note of type: %d - got: %d\n",
307 *off, type, note.n_type));
309 if (type != note.n_type) {
314 if (strncmp(name, (char *) src + *off, note.n_namesz) != 0) {
318 *off += roundup2(note.n_namesz, sizeof(Elf_Size));
319 if (note.n_descsz != descsz) {
325 bcopy((char *)src + *off, *desc, note.n_descsz);
326 *off += roundup2(note.n_descsz, sizeof(Elf_Size));
334 elf_demarshalnotes(void *src, prpsinfo_t *psinfo, prstatus_t *status,
335 prfpregset_t *fpregset, int nthreads)
342 error = elf_getnote(src, &off, "FreeBSD", NT_PRSTATUS,
343 (void **)&status, sizeof(prstatus_t));
346 error = elf_getnote(src, &off, "FreeBSD", NT_FPREGSET,
347 (void **)&fpregset, sizeof(prfpregset_t));
350 error = elf_getnote(src, &off, "FreeBSD", NT_PRPSINFO,
351 (void **)&psinfo, sizeof(prpsinfo_t));
356 * The remaining portion needs to be an integer multiple
357 * of prstatus_t and prfpregset_t
359 for (i = 0 ; i < nthreads - 1; i++) {
360 status++; fpregset++;
361 error = elf_getnote(src, &off, "FreeBSD", NT_PRSTATUS,
362 (void **)&status, sizeof (prstatus_t));
365 error = elf_getnote(src, &off, "FreeBSD", NT_FPREGSET,
366 (void **)&fpregset, sizeof(prfpregset_t));
378 mmap_phdr(struct file *fp, Elf_Phdr *phdr)
388 pos = phdr->p_offset;
389 len = phdr->p_filesz;
390 addr = (void *)phdr->p_vaddr;
391 flags = MAP_FIXED | MAP_NOSYNC | MAP_PRIVATE;
393 if (phdr->p_flags & PF_R)
395 if (phdr->p_flags & PF_W)
397 if (phdr->p_flags & PF_X)
399 if ((error = fp_mmap(addr, len, prot, flags, fp, pos, &addr)) != 0) {
400 PRINTF(("mmap failed: %d\n", error); );
402 PRINTF(("map @%08x-%08x fileoff %08x-%08x\n", (int)addr, (int)((char *)addr + len), (int)pos, (int)(pos + len)));
409 elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs)
415 for (i = 1; i < numsegs; i++) {
416 if ((error = mmap_phdr(fp, &phdr[i])) != 0)
424 elf_getsigs(struct proc *p, struct file *fp)
427 struct ckpt_siginfo *csi;
428 struct sigacts *tmpsigacts;
431 csi = malloc(sizeof(struct ckpt_siginfo), M_TEMP, M_ZERO | M_WAITOK);
432 if ((error = read_check(fp, csi, sizeof(struct ckpt_siginfo))) != 0)
435 if (csi->csi_ckptpisz != sizeof(struct ckpt_siginfo)) {
440 tmpsigacts = p->p_procsig->ps_sigacts;
441 bcopy(&csi->csi_procsig, p->p_procsig, sizeof(struct procsig));
442 p->p_procsig->ps_sigacts = tmpsigacts;
443 bcopy(&csi->csi_sigacts, p->p_procsig->ps_sigacts, sizeof(struct sigacts));
444 bcopy(&csi->csi_itimerval, &p->p_realtimer, sizeof(struct itimerval));
445 p->p_sigparent = csi->csi_sigparent;
455 ckpt_fhtovp(fhandle_t *fh, struct vnode **vpp)
461 mp = vfs_getvfs(&fh->fh_fsid);
465 PRINTF(("failed to get mount - ESTALE\n"));
469 error = VFS_FHTOVP(mp, &fh->fh_fid, vpp);
471 PRINTF(("failed with: %d\n", error));
481 mmap_vp(struct vn_hdr *vnh)
483 struct vnode **vpp, *vp;
490 phdr = &vnh->vnh_phdr;
492 if ((error = ckpt_fhtovp(&vnh->vnh_fh, vpp)) != 0)
495 * XXX O_RDONLY -> or O_RDWR if file is PROT_WRITE, MAP_SHARED
497 if ((error = fp_vpopen(*vpp, O_RDONLY, &fp)) != 0)
499 error = mmap_phdr(fp, phdr);
507 elf_gettextvp(struct proc *p, struct file *fp)
512 struct ckpt_vminfo vminfo;
513 struct vn_hdr *vnh = NULL;
516 if ((error = read_check(fp, &vminfo, sizeof(vminfo))) != 0)
518 if (vminfo.cvm_dsize < 0 ||
519 vminfo.cvm_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur ||
520 vminfo.cvm_tsize < 0 ||
521 (u_quad_t)vminfo.cvm_tsize > maxtsiz ||
522 vminfo.cvm_daddr >= (caddr_t)VM_MAXUSER_ADDRESS ||
523 vminfo.cvm_taddr >= (caddr_t)VM_MAXUSER_ADDRESS
528 p->p_vmspace->vm_daddr = vminfo.cvm_daddr;
529 p->p_vmspace->vm_dsize = vminfo.cvm_dsize;
530 p->p_vmspace->vm_taddr = vminfo.cvm_taddr;
531 p->p_vmspace->vm_tsize = vminfo.cvm_tsize;
532 if ((error = read_check(fp, &vpcount, sizeof(int))) != 0)
534 vnh = malloc(sizeof(struct vn_hdr) * vpcount, M_TEMP, M_WAITOK);
535 if ((error = read_check(fp, vnh, sizeof(struct vn_hdr)*vpcount)) != 0)
537 for (i = 0; i < vpcount; i++) {
538 if ((error = mmap_vp(&vnh[i])) != 0)
553 elf_getfiles(struct proc *p, struct file *fp)
558 struct ckpt_filehdr filehdr;
559 struct ckpt_fileinfo *cfi_base = NULL;
564 if ((error = read_check(fp, &filehdr, sizeof(filehdr))) != 0)
566 filecount = filehdr.cfh_nfiles;
567 cfi_base = malloc(filecount*sizeof(struct ckpt_fileinfo), M_TEMP, M_WAITOK);
568 error = read_check(fp, cfi_base, filecount*sizeof(struct ckpt_fileinfo));
572 for (i = 0; i < filecount; i++) {
573 struct ckpt_fileinfo *cfi= &cfi_base[i];
575 * Ignore placeholder entries where cfi_index is less then
576 * zero. This will occur if the elf core dump code thinks
577 * it can save a vnode but winds up not being able to.
579 if (cfi->cfi_index < 0)
581 if (cfi->cfi_index >= p->p_fd->fd_nfiles) {
582 PRINTF(("can't currently restore fd: %d\n",
586 if ((error = ckpt_fhtovp(&cfi->cfi_fh, &vp)) != 0)
588 if ((error = fp_vpopen(vp, OFLAGS(cfi->cfi_flags), &tempfp)) != 0)
590 tempfp->f_offset = cfi->cfi_offset;
591 /* XXX bail for now if we the index is
592 * larger than the current file table
595 PRINTF(("restoring %d\n", cfi->cfi_index));
596 p->p_fd->fd_ofiles[cfi->cfi_index] = tempfp;
602 free(cfi_base, M_TEMP);
608 ckpt_freeze_proc (struct proc *p, struct file *fp)
613 PRINTF(("calling generic_elf_coredump\n"));
614 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
616 error = generic_elf_coredump(p, fp, limit);
624 /* THIS CAN'T WORK */
626 ckpt_freeze_pid(int pid, struct file *fp)
630 if ((p = pfind(pid)) == NULL)
632 return ckpt_freeze_proc(p, fp);
637 ckpt_proc(void *uap /* struct ckpt_args */)
641 struct proc *p = curthread->td_proc;
642 struct ckpt_args *ca = (struct ckpt_args *)uap;
645 res = &((struct nosys_args *)uap)->sysmsg_result;
648 * Only certain groups (to reduce our security exposure). -1
651 if (ckptgroup >= 0 && groupmember(ckptgroup, p->p_ucred) == 0) {
655 switch (ca->args.gen.type) {
657 if ((fp = holdfp(p->p_fd, ca->args.gen.fd, FWRITE)) == NULL)
659 error = ckpt_freeze_proc(p, fp);
660 fdrop(fp, curthread);
663 if ((fp = holdfp(p->p_fd, ca->args.gen.fd, FREAD)) == NULL)
665 *res = ca->args.cta.retval;
666 error = ckpt_thaw_proc(p, fp);
667 fdrop(fp, curthread);
674 if ((fp = holdfp(p->p_fd, ca->args.gen.fd, FWRITE)) == NULL)
676 error = ckpt_freeze_pid(ca->args.cfpa.pid, fp);
677 fdrop(fp, curthread);
685 if ((fp = holdfp(p->p_fd, ca->args.gen.fd, FREAD)) == NULL)
687 error = ckpt_thaw_bin(p, fp,
688 ca->args.ctba.bin, ca->args.ctba.binlen);
689 fdrop(fp, curthread);
697 PRINTF(("error of ckpt_proc is %d, retval is %d\n", error, *res));
702 ckpt_handler(struct proc *p)
711 * Being able to checkpoint an suid or sgid program is not a good
714 if (sugid_coredump == 0 && (p->p_flag & P_SUGID)) {
719 buf = ckpt_expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
725 log(LOG_INFO, "pid %d (%s), uid %d: checkpointing to %s\n",
727 (p->p_ucred ? p->p_ucred->cr_uid : -1),
730 PRINTF(("ckpt handler called, using '%s'\n", buf));
733 * Use the same safety flags that the coredump code uses.
735 error = fp_open(buf, O_WRONLY|O_CREAT|O_NOFOLLOW, 0600, &fp);
737 (void)ckpt_freeze_proc(p, fp);
740 printf("checkpoint failed with open - error: %d\n", error);
748 * The `sysent' for the new syscall
750 static struct sysent ckpt_sysent = {
752 ckpt_proc /* sy_call */
755 static int ckpt_offset = 210;
758 load (struct module *module, int cmd, void *arg)
764 PRINTF( ("ckpt loaded at %d\n", ckpt_offset));
765 (void)register_ckpt_func(ckpt_handler);
770 PRINTF(("chpt in progress, unable to unload ckpt module\n"));
773 (void)register_ckpt_func(NULL);
774 PRINTF(("ckpt unloaded from %d\n", ckpt_offset));
784 SYSCALL_MODULE(syscall, &ckpt_offset, &ckpt_sysent, load, NULL);