Merge from vendor branch GCC:
[dragonfly.git] / sys / kern / kern_checkpoint.c
1 /*-
2  * Copyright (c) 2003 Kip Macy
3  * All rights reserved.
4  *
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.
13  *
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.
25  *
26  * $DragonFly: src/sys/kern/kern_checkpoint.c,v 1.2 2005/02/26 20:32:36 dillon Exp $
27  */
28
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>
37
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>
47
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 <i386/include/frame.h>
64 #include <sys/signalvar.h>
65 #include <sys/syslog.h>
66 #include <sys/sysctl.h>
67 #include <i386/include/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>
75
76
77 static int elf_loadphdrs(struct file *fp,  Elf_Phdr *phdr, int numsegs);
78 static int elf_getnotes(struct proc *p, 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 proc *, prpsinfo_t *, prstatus_t *, 
82                  prfpregset_t *);
83 static int elf_getsigs(struct proc *p, 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);
87
88 static int ckptgroup = 0;       /* wheel only, -1 for any group */
89 SYSCTL_INT(_kern, OID_AUTO, ckptgroup, CTLFLAG_RW, &ckptgroup, 0, "");
90
91 /* ref count to see how many processes that are being checkpointed */
92 static int chptinuse = 0;
93
94 static __inline
95 int
96 read_check(struct file *fp, void *buf, size_t nbyte)
97 {
98         size_t nread;
99         int error;
100
101         PRINTF(("reading %d bytes\n", nbyte));
102         error = fp_read(fp, buf, nbyte, &nread);
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;
108         }
109         return error;
110 }
111
112 static int
113 elf_gethdr(struct file *fp, Elf_Ehdr *ehdr) 
114 {
115         size_t nbyte = sizeof(Elf_Ehdr);
116         int error;
117
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;
125         }
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;
131         }
132
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;
145
146         }
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;
152
153
154 static int
155 elf_getphdrs(struct file *fp, Elf_Phdr *phdr, size_t nbyte) 
156 {
157         int i;
158         int error;
159         int nheaders = nbyte/sizeof(Elf_Phdr); 
160
161         PRINTF(("reading phdrs section\n"));
162         if ((error = read_check(fp, phdr, nbyte)) != 0)
163                 goto done;
164         printf("headers section:\n");
165         for (i = 0; i < nheaders; i++) {
166                 printf("entry type:   %d\n", phdr[i].p_type);
167                 printf("file offset:  %d\n", phdr[i].p_offset);
168                 printf("virt address: %p\n", (uint32_t *)phdr[i].p_vaddr);
169                 printf("file size:    %d\n", phdr[i].p_filesz);
170                 printf("memory size:  %d\n", phdr[i].p_memsz);
171                 printf("\n");
172         }
173  done:
174         return error;
175 }
176
177
178 static int
179 elf_getnotes(struct proc *p, struct file *fp, size_t notesz) 
180 {
181         int error;
182         int nthreads;
183         char *note;
184         prpsinfo_t *psinfo;
185         prstatus_t *status;
186         prfpregset_t *fpregset;
187
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;
193
194         psinfo  = malloc(sizeof(prpsinfo_t), M_TEMP, M_ZERO | M_WAITOK);
195         status  = malloc(nthreads*sizeof(prstatus_t), M_TEMP, M_WAITOK);
196         fpregset  = malloc(nthreads*sizeof(prfpregset_t), M_TEMP, M_WAITOK);
197         note = malloc(notesz, M_TEMP, M_WAITOK);
198
199         
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(p, psinfo, status, fpregset);
208  done:
209         if (psinfo)
210                 free(psinfo, M_TEMP);
211         if (status)
212                 free(status, M_TEMP);
213         if (fpregset)
214                 free(fpregset, M_TEMP);
215         if (note)
216                 free(note, M_TEMP);
217         return error;
218 }
219
220 static int
221 ckpt_thaw_proc(struct proc *p, struct file *fp)
222 {
223
224         Elf_Phdr *phdr = NULL;
225         Elf_Ehdr *ehdr = NULL;
226         int error;
227         size_t nbyte;
228
229         TRACE_ENTER;
230         
231         ehdr = malloc(sizeof(Elf_Ehdr), M_TEMP, M_ZERO | M_WAITOK);
232
233         if ((error = elf_gethdr(fp, ehdr)) != 0)
234                 goto done;
235         nbyte = sizeof(Elf_Phdr) * ehdr->e_phnum; 
236         phdr = malloc(nbyte, M_TEMP, M_WAITOK); 
237
238         /* fetch description of program writable mappings */
239         if ((error = elf_getphdrs(fp, phdr, nbyte)) != 0)
240                 goto done;
241
242         /* fetch notes section containing register state */
243         if ((error = elf_getnotes(p, fp, phdr->p_filesz)) != 0)
244                 goto done;
245
246         /* fetch program text vnodes */
247         if ((error = elf_gettextvp(p, fp)) != 0)
248                 goto done;
249
250         /* fetch signal disposition */
251         if ((error = elf_getsigs(p, fp)) != 0) {
252                 printf("failure in recovering signals\n");
253                 goto done;
254         }
255
256         /* fetch open files */
257         if ((error = elf_getfiles(p, fp)) != 0)
258                 goto done;
259
260         /* handle mappings last in case we are reading from a socket */
261         error = elf_loadphdrs(fp, phdr, ehdr->e_phnum);
262
263         /*
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.
269          */
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);
276         }
277 done:
278         if (ehdr)
279                 free(ehdr, M_TEMP);
280         if (phdr)
281                 free(phdr, M_TEMP);
282         TRACE_EXIT;
283         return error;
284 }
285
286 static int
287 elf_loadnotes(struct proc *p, prpsinfo_t *psinfo, prstatus_t *status, 
288            prfpregset_t *fpregset) 
289 {
290         int error;
291
292         /* validate status and psinfo */
293         TRACE_ENTER;
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"));
301                 error = EINVAL;
302                 goto done;
303         }
304         if ((error = set_regs(p, &status->pr_reg)) != 0)
305                 goto done;
306         error = set_fpregs(p, fpregset);
307         strlcpy(p->p_comm, psinfo->pr_fname, sizeof(p->p_comm));
308         /* XXX psinfo->pr_psargs not yet implemented */
309  done:  
310         TRACE_EXIT;
311         return error;
312 }
313
314 static int 
315 elf_getnote(void *src, size_t *off, const char *name, unsigned int type,
316             void **desc, size_t descsz) 
317 {
318         Elf_Note note;
319         int error;
320
321         TRACE_ENTER;
322         if (src == NULL) {
323                 error = EFAULT;
324                 goto done;
325         }
326         bcopy((char *)src + *off, &note, sizeof note);
327         
328         PRINTF(("at offset: %d expected note of type: %d - got: %d\n",
329                *off, type, note.n_type));
330         *off += sizeof note;
331         if (type != note.n_type) {
332                 TRACE_ERR;
333                 error = EINVAL;
334                 goto done;
335         }
336         if (strncmp(name, (char *) src + *off, note.n_namesz) != 0) {
337                 error = EINVAL;
338                 goto done;
339         }
340         *off += roundup2(note.n_namesz, sizeof(Elf_Size));
341         if (note.n_descsz != descsz) {
342                 TRACE_ERR;
343                 error = EINVAL;
344                 goto done;
345         }
346         if (desc)
347                 bcopy((char *)src + *off, *desc, note.n_descsz);
348         *off += roundup2(note.n_descsz, sizeof(Elf_Size));
349         error = 0;
350  done:
351         TRACE_EXIT;
352         return error;
353 }
354
355 static int
356 elf_demarshalnotes(void *src, prpsinfo_t *psinfo, prstatus_t *status, 
357                    prfpregset_t *fpregset, int nthreads) 
358 {
359         int i;
360         int error;
361         int off = 0;
362
363         TRACE_ENTER;
364         error = elf_getnote(src, &off, "FreeBSD", NT_PRSTATUS, 
365                            (void **)&status, sizeof(prstatus_t));
366         if (error)
367                 goto done;
368         error = elf_getnote(src, &off, "FreeBSD", NT_FPREGSET, 
369                            (void **)&fpregset, sizeof(prfpregset_t));
370         if (error)
371                 goto done;
372         error = elf_getnote(src, &off, "FreeBSD", NT_PRPSINFO, 
373                            (void **)&psinfo, sizeof(prpsinfo_t));
374         if (error)
375                 goto done;
376
377         /*
378          * The remaining portion needs to be an integer multiple
379          * of prstatus_t and prfpregset_t
380          */
381         for (i = 0 ; i < nthreads - 1; i++) {
382                 status++; fpregset++;
383                 error = elf_getnote(src, &off, "FreeBSD", NT_PRSTATUS, 
384                                    (void **)&status, sizeof (prstatus_t));
385                 if (error)
386                         goto done;
387                 error = elf_getnote(src, &off, "FreeBSD", NT_FPREGSET, 
388                                    (void **)&fpregset, sizeof(prfpregset_t));
389                 if (error)
390                         goto done;
391         }
392         
393  done:
394         TRACE_EXIT;
395         return error;
396 }
397
398
399 static int
400 mmap_phdr(struct file *fp, Elf_Phdr *phdr) 
401 {
402         int error;
403         size_t len;
404         int prot;
405         void *addr;
406         int flags;
407         off_t pos;
408
409         TRACE_ENTER;
410         pos = phdr->p_offset;
411         len = phdr->p_filesz;
412         addr = (void *)phdr->p_vaddr;
413         flags = MAP_FIXED | MAP_NOSYNC | MAP_PRIVATE;
414         prot = 0;
415         if (phdr->p_flags & PF_R)
416                 prot |= PROT_READ;
417         if (phdr->p_flags & PF_W)
418                 prot |= PROT_WRITE;
419         if (phdr->p_flags & PF_X)
420                 prot |= PROT_EXEC;      
421         if ((error = fp_mmap(addr, len, prot, flags, fp, pos, &addr)) != 0) {
422                 PRINTF(("mmap failed: %d\n", error);       );
423         }
424         PRINTF(("map @%08x-%08x fileoff %08x-%08x\n", (int)addr, (int)((char *)addr + len), (int)pos, (int)(pos + len)));
425         TRACE_EXIT;
426         return error;
427 }
428
429
430 static int
431 elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs) 
432 {
433         int i;
434         int error = 0;
435
436         TRACE_ENTER;
437         for (i = 1; i < numsegs; i++)  {
438                 if ((error = mmap_phdr(fp, &phdr[i])) != 0)
439                         break;
440         }
441         TRACE_EXIT;
442         return error;
443 }
444
445 static int
446 elf_getsigs(struct proc *p, struct file *fp) 
447 {
448         int error;
449         struct ckpt_siginfo *csi;
450         struct sigacts *tmpsigacts;
451
452         TRACE_ENTER;
453         csi = malloc(sizeof(struct ckpt_siginfo), M_TEMP, M_ZERO | M_WAITOK);
454         if ((error = read_check(fp, csi, sizeof(struct ckpt_siginfo))) != 0)
455                 goto done;
456
457         if (csi->csi_ckptpisz != sizeof(struct ckpt_siginfo)) {
458                 TRACE_ERR;
459                 error = EINVAL;
460                 goto done;
461         }
462         tmpsigacts = p->p_procsig->ps_sigacts;
463         bcopy(&csi->csi_procsig, p->p_procsig, sizeof(struct procsig));
464         p->p_procsig->ps_sigacts = tmpsigacts;
465         bcopy(&csi->csi_sigacts, p->p_procsig->ps_sigacts, sizeof(struct sigacts));
466         bcopy(&csi->csi_itimerval, &p->p_realtimer, sizeof(struct itimerval));
467         SIG_CANTMASK(csi->csi_sigmask);
468         bcopy(&csi->csi_sigmask, &p->p_sigmask, sizeof(sigset_t));
469         p->p_sigparent = csi->csi_sigparent;
470  done:
471         if (csi)
472                 free(csi, M_TEMP);
473         TRACE_EXIT;
474         return error;
475 }
476
477 /*
478  * Returns a locked, refd vnode
479  */
480 static int
481 ckpt_fhtovp(fhandle_t *fh, struct vnode **vpp) 
482 {
483         struct mount *mp;
484         int error;
485
486         TRACE_ENTER;
487         mp = vfs_getvfs(&fh->fh_fsid);
488
489         if (!mp) {
490                 TRACE_ERR;
491                 PRINTF(("failed to get mount - ESTALE\n"));
492                 TRACE_EXIT;
493                 return ESTALE;
494         }
495         error = VFS_FHTOVP(mp, &fh->fh_fid, vpp);
496         if (error) {
497                 PRINTF(("failed with: %d\n", error));
498                 TRACE_ERR;
499                 TRACE_EXIT;
500                 return error;
501         }
502         TRACE_EXIT;
503         return 0;
504 }
505
506 static int
507 mmap_vp(struct vn_hdr *vnh) 
508 {
509         struct vnode *vp;
510         Elf_Phdr *phdr;
511         struct file *fp;
512         int error;
513         TRACE_ENTER;
514
515         phdr = &vnh->vnh_phdr;
516
517         if ((error = ckpt_fhtovp(&vnh->vnh_fh, &vp)) != 0)
518                 return error;
519         /*
520          * XXX O_RDONLY -> or O_RDWR if file is PROT_WRITE, MAP_SHARED
521          */
522         if ((error = fp_vpopen(vp, O_RDONLY, &fp)) != 0) {
523                 vput(vp);
524                 return error;
525         }
526         error = mmap_phdr(fp, phdr);
527         fp_close(fp);
528         TRACE_EXIT;
529         return error;
530 }
531
532
533 static int
534 elf_gettextvp(struct proc *p, struct file *fp)
535 {
536         int i;
537         int error;
538         int vpcount;
539         struct ckpt_vminfo vminfo;
540         struct vn_hdr *vnh = NULL;
541
542         TRACE_ENTER;
543         if ((error = read_check(fp, &vminfo, sizeof(vminfo))) != 0)
544                 goto done;
545         if (vminfo.cvm_dsize < 0 || 
546             vminfo.cvm_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur ||
547             vminfo.cvm_tsize < 0 ||
548             (u_quad_t)vminfo.cvm_tsize > maxtsiz ||
549             vminfo.cvm_daddr >= (caddr_t)VM_MAXUSER_ADDRESS ||
550             vminfo.cvm_taddr >= (caddr_t)VM_MAXUSER_ADDRESS
551         ) {
552             error = ERANGE;
553             goto done;
554         }
555
556         vmspace_exec(p, NULL);
557         p->p_vmspace->vm_daddr = vminfo.cvm_daddr;
558         p->p_vmspace->vm_dsize = vminfo.cvm_dsize;
559         p->p_vmspace->vm_taddr = vminfo.cvm_taddr;
560         p->p_vmspace->vm_tsize = vminfo.cvm_tsize;
561         if ((error = read_check(fp, &vpcount, sizeof(int))) != 0)
562                 goto done;
563         vnh = malloc(sizeof(struct vn_hdr) * vpcount, M_TEMP, M_WAITOK);
564         if ((error = read_check(fp, vnh, sizeof(struct vn_hdr)*vpcount)) != 0)
565                 goto done;
566         for (i = 0; i < vpcount; i++) {
567                 if ((error = mmap_vp(&vnh[i])) != 0)
568                         goto done;
569         }
570         
571  done:
572         if (vnh)
573                 free(vnh, M_TEMP);
574         TRACE_EXIT;
575         return error;
576 }
577
578
579
580 /* place holder */
581 static int
582 elf_getfiles(struct proc *p, struct file *fp)
583 {
584         int error;
585         int i;
586         int filecount;
587         int fd;
588         struct ckpt_filehdr filehdr;
589         struct ckpt_fileinfo *cfi_base = NULL;
590         struct vnode *vp;
591         struct file *tempfp;
592         struct file *ofp;
593
594         TRACE_ENTER;
595         if ((error = read_check(fp, &filehdr, sizeof(filehdr))) != 0)
596                 goto done;
597         filecount = filehdr.cfh_nfiles;
598         cfi_base = malloc(filecount*sizeof(struct ckpt_fileinfo), M_TEMP, M_WAITOK);
599         error = read_check(fp, cfi_base, filecount*sizeof(struct ckpt_fileinfo));
600         if (error)
601                 goto done;
602
603         /*
604          * Close all file descriptors >= 3.  These descriptors are from the
605          * checkpt(1) program itself and should not be retained.
606          *
607          * XXX we need a flag so a checkpoint restore can opt to supply the
608          * descriptors, or the non-regular-file descripors.
609          */
610         for (i = 3; i < p->p_fd->fd_nfiles; ++i)
611                 kern_close(i);
612
613         /*
614          * Scan files to load
615          */
616         for (i = 0; i < filecount; i++) {
617                 struct ckpt_fileinfo *cfi= &cfi_base[i];
618                 /*
619                  * Ignore placeholder entries where cfi_index is less then
620                  * zero.  This will occur if the elf core dump code thinks
621                  * it can save a vnode but winds up not being able to.
622                  */
623                 if (cfi->cfi_index < 0)
624                         continue;
625
626                 if ((error = ckpt_fhtovp(&cfi->cfi_fh, &vp)) != 0)
627                         break;
628                 if ((error = fp_vpopen(vp, OFLAGS(cfi->cfi_flags), &tempfp)) != 0) {
629                         vput(vp);
630                         break;
631                 }
632                 tempfp->f_offset = cfi->cfi_offset;
633
634                 /*
635                  * If overwriting a descriptor close the old descriptor.  This
636                  * only occurs if the saved core saved descriptors that we 
637                  * have not already closed.
638                  */
639                 if (cfi->cfi_index < p->p_fd->fd_nfiles &&
640                     (ofp = p->p_fd->fd_ofiles[cfi->cfi_index]) != NULL) {
641                         kern_close(cfi->cfi_index);
642                 }
643
644                 /*
645                  * Allocate the descriptor we want.
646                  */
647                 if (fdalloc(p, cfi->cfi_index, &fd) != 0) {
648                         PRINTF(("can't currently restore fd: %d\n",
649                                cfi->cfi_index));
650                         fp_close(fp);
651                         goto done;
652                 }
653                 KKASSERT(fd == cfi->cfi_index);
654                 p->p_fd->fd_ofiles[cfi->cfi_index] = tempfp;            
655                 cfi++;
656                 PRINTF(("restoring %d\n", cfi->cfi_index));
657         }
658
659  done:
660         if (cfi_base)
661                 free(cfi_base, M_TEMP);
662         TRACE_EXIT;
663         return error;
664 }
665
666 static int
667 ckpt_freeze_proc (struct proc *p, struct file *fp)
668 {
669         rlim_t limit;
670         int error;
671
672         PRINTF(("calling generic_elf_coredump\n"));
673         limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
674         if (limit) {
675                 error = generic_elf_coredump(p, fp, limit);
676         } else {
677                 error = ERANGE;
678         }
679         return error;
680 }
681
682 int 
683 sys_checkpoint(struct sys_checkpoint_args *uap)
684 {
685         int error = 0;
686         struct proc *p = curthread->td_proc;
687         struct file *fp;
688
689         /*
690          * Only certain groups (to reduce our security exposure).  -1
691          * allows any group.
692          */
693         if (ckptgroup >= 0 && groupmember(ckptgroup, p->p_ucred) == 0)
694                 return (EPERM);
695
696         /*
697          * For now we can only checkpoint the current process
698          */
699         if (uap->pid != -1 && uap->pid != p->p_pid)
700                 return (EINVAL);
701
702         switch (uap->type) {
703         case CKPT_FREEZE:
704                 fp = NULL;
705                 if (uap->fd == -1 && uap->pid == (pid_t)-1)
706                         error = checkpoint_signal_handler(p);
707                 else if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
708                         error = EBADF;
709                 else
710                         error = ckpt_freeze_proc(p, fp);
711                 if (fp)
712                         fdrop(fp, curthread);
713                 break;
714         case CKPT_THAW:
715                 if (uap->pid != -1)
716                         return EINVAL;
717                 if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
718                         return EBADF;
719                 uap->sysmsg_result = uap->retval;
720                 error = ckpt_thaw_proc(p, fp);
721                 fdrop(fp, curthread);
722                 break;
723         default:
724                 error = EOPNOTSUPP;
725                 break;
726         }
727         return error;
728 }
729
730 int
731 checkpoint_signal_handler(struct proc *p) 
732 {
733         char *buf;
734         struct file *fp;
735         struct nlookupdata nd;
736         int error;
737
738         chptinuse++;
739
740         /*
741          * Being able to checkpoint an suid or sgid program is not a good
742          * idea.
743          */
744         if (sugid_coredump == 0 && (p->p_flag & P_SUGID)) {
745                 chptinuse--;
746                 return (EPERM);
747         }
748
749         buf = ckpt_expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
750         if (buf == NULL) {
751                 chptinuse--;
752                 return (ENOMEM);
753         }
754
755         log(LOG_INFO, "pid %d (%s), uid %d: checkpointing to %s\n",
756                 p->p_pid, p->p_comm, 
757                 (p->p_ucred ? p->p_ucred->cr_uid : -1),
758                 buf);
759
760         PRINTF(("ckpt handler called, using '%s'\n", buf));
761
762         /*
763          * Use the same safety flags that the coredump code uses.  Remove
764          * any previous checkpoint file before writing out the new one in
765          * case we are re-checkpointing a program that had been checkpt
766          * restored.  Otherwise we will corrupt the program space (which is
767          * made up of mmap()ings of the previous checkpoint file) while we
768          * write out the new one.
769          */
770         error = nlookup_init(&nd, buf, UIO_SYSSPACE, 0);
771         if (error == 0)
772                 error = kern_unlink(&nd);
773         nlookup_done(&nd);
774         error = fp_open(buf, O_WRONLY|O_CREAT|O_TRUNC|O_NOFOLLOW, 0600, &fp);
775         if (error == 0) {
776                 error = ckpt_freeze_proc(p, fp);
777                 fp_close(fp);
778         } else {
779                 printf("checkpoint failed with open - error: %d\n", error);
780         }
781         free(buf, M_TEMP);
782         chptinuse--;
783         return (error);
784 }
785
786 static char ckptfilename[MAXPATHLEN] = {"%N.ckpt"};
787 SYSCTL_STRING(_kern, OID_AUTO, ckptfile, CTLFLAG_RW, ckptfilename,
788               sizeof(ckptfilename), "process checkpoint name format string");
789
790 /*
791  * expand_name(name, uid, pid)
792  * Expand the name described in corefilename, using name, uid, and pid.
793  * corefilename is a printf-like string, with three format specifiers:
794  *      %N      name of process ("name")
795  *      %P      process id (pid)
796  *      %U      user id (uid)
797  * For example, "%N.core" is the default; they can be disabled completely
798  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
799  * This is controlled by the sysctl variable kern.corefile (see above).
800  *
801  * -- taken from the coredump code
802  */
803
804 static
805 char *
806 ckpt_expand_name(const char *name, uid_t uid, pid_t pid)
807 {
808         char *temp;
809         char *bp;
810         char buf[11];           /* Buffer for pid/uid -- max 4B */
811         int error;
812         int i;
813         int n;
814         char *format = ckptfilename;
815         size_t namelen;
816
817         temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
818         if (temp == NULL)
819                 return NULL;
820         namelen = strlen(name);
821         n = 0;
822         if (ckptfilename[0] != '/') {
823                 if ((bp = kern_getcwd(temp, MAXPATHLEN - 1, &error)) == NULL) {
824                         free(temp, M_TEMP);
825                         return NULL;
826                 }
827                 n = strlen(bp);
828                 bcopy(bp, temp, n + 1); /* normalize location of the path */
829                 temp[n++] = '/';
830                 temp[n] = '\0';
831         }
832         for (i= 0; n < MAXPATHLEN && format[i]; i++) {
833                 int l;
834                 switch (format[i]) {
835                 case '%':       /* Format character */
836                         i++;
837                         switch (format[i]) {
838                         case '%':
839                                 temp[n++] = '%';
840                                 break;
841                         case 'N':       /* process name */
842                                 if ((n + namelen) > MAXPATHLEN) {
843                                         log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
844                                             pid, name, uid, temp, name);
845                                         free(temp, M_TEMP);
846                                         return NULL;
847                                 }
848                                 memcpy(temp+n, name, namelen);
849                                 n += namelen;
850                                 break;
851                         case 'P':       /* process id */
852                                 l = sprintf(buf, "%u", pid);
853                                 if ((n + l) > MAXPATHLEN) {
854                                         log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
855                                             pid, name, uid, temp, name);
856                                         free(temp, M_TEMP);
857                                         return NULL;
858                                 }
859                                 memcpy(temp+n, buf, l);
860                                 n += l;
861                                 break;
862                         case 'U':       /* user id */
863                                 l = sprintf(buf, "%u", uid);
864                                 if ((n + l) > MAXPATHLEN) {
865                                         log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
866                                             pid, name, uid, temp, name);
867                                         free(temp, M_TEMP);
868                                         return NULL;
869                                 }
870                                 memcpy(temp+n, buf, l);
871                                 n += l;
872                                 break;
873                         default:
874                                 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
875                         }
876                         break;
877                 default:
878                         temp[n++] = format[i];
879                 }
880         }
881         temp[n] = '\0';
882         return temp;
883 }
884