DragonFlyBSD Kernel Audit
sys/kern/kern_checkpoint.c
← back
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
/*-
 * Copyright (c) 2003 Kip Macy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/module.h>
#include <sys/sysent.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/nlookup.h>

#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/signal.h>
#include <vm/vm_param.h>
#include <vm/vm.h>
#include <sys/imgact_elf.h>
#include <sys/procfs.h>

#include <sys/lock.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
#include <vm/vm_extern.h>
#include <sys/mman.h>
#include <sys/sysmsg.h>
#include <sys/resource.h>
#include <sys/resourcevar.h>
#include <sys/malloc.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/vnode.h>
#include <machine/inttypes.h>
#include <machine/limits.h>
#include <machine/frame.h>
#include <sys/signalvar.h>
#include <sys/syslog.h>
#include <sys/sysctl.h>
#include <machine/sigframe.h>
#include <sys/exec.h>
#include <sys/unistd.h>
#include <sys/time.h>
#include <sys/kern_syscall.h>
#include <sys/checkpoint.h>
#include <sys/mount.h>
#include <sys/ckpt.h>

#include <sys/mplock2.h>
#include <sys/file2.h>

static int elf_loadphdrs(struct file *fp,  Elf_Phdr *phdr, int numsegs);
static int elf_getnotes(struct lwp *lp, struct file *fp, size_t notesz);
static int elf_demarshalnotes(void *src, prpsinfo_t *psinfo,
		 prstatus_t *status, prfpregset_t *fpregset, int nthreads);
static int elf_loadnotes(struct lwp *, prpsinfo_t *, prstatus_t *,
		 prfpregset_t *);
static int elf_getsigs(struct lwp *lp, struct file *fp);
static int elf_getfiles(struct lwp *lp, struct file *fp);
static int elf_gettextvp(struct proc *p, struct file *fp);
static char *ckpt_expand_name(const char *name, uid_t uid, pid_t pid);

static int ckptgroup = 0;       /* wheel only, -1 for any group */
SYSCTL_INT(_kern, OID_AUTO, ckptgroup, CTLFLAG_RW, &ckptgroup, 0, "");

/* ref count to see how many processes that are being checkpointed */
static int chptinuse = 0;

static __inline
int
read_check(struct file *fp, void *buf, size_t nbyte)
{
	size_t nread;
	int error;

	PRINTF(("reading %zd bytes\n", nbyte));
	error = fp_read(fp, buf, nbyte, &nread, 1, UIO_SYSSPACE);
	if (error) {
                PRINTF(("read failed - %d", error));
	} else if (nread != nbyte) {
                PRINTF(("wanted to read %zd - read %zd\n", nbyte, nread));
		error = EINVAL;
	}
	return error;
}

static int
elf_gethdr(struct file *fp, Elf_Ehdr *ehdr)
{
	size_t nbyte = sizeof(Elf_Ehdr);
	int error;

	if ((error = read_check(fp, ehdr, nbyte)) != 0)
		goto done;
	if (!(ehdr->e_ehsize == sizeof(Elf_Ehdr))) {
		PRINTF(("wrong elf header size: %d\n"
		       "expected size        : %zd\n",
		       ehdr->e_ehsize, sizeof(Elf_Ehdr)));
		return EINVAL;
	}
	if (!(ehdr->e_phentsize == sizeof(Elf_Phdr))) {
		PRINTF(("wrong program header size: %d\n"
		       "expected size            : %zd\n",
		       ehdr->e_phentsize, sizeof(Elf_Phdr)));
		return EINVAL;
	}

	if (!(ehdr->e_ident[EI_MAG0] == ELFMAG0 &&
	      ehdr->e_ident[EI_MAG1] == ELFMAG1 &&
	      ehdr->e_ident[EI_MAG2] == ELFMAG2 &&
	      ehdr->e_ident[EI_MAG3] == ELFMAG3 &&
	      ehdr->e_ident[EI_CLASS] == ELF_CLASS &&
	      ehdr->e_ident[EI_DATA] == ELF_DATA &&
	      ehdr->e_ident[EI_VERSION] == EV_CURRENT &&
	      ehdr->e_ident[EI_OSABI] == ELFOSABI_NONE &&
	      ehdr->e_ident[EI_ABIVERSION] == 0)) {
		PRINTF(("bad elf header\n there are %d segments\n",
		       ehdr->e_phnum));
		return EINVAL;

	}
	PRINTF(("Elf header size:           %d\n", ehdr->e_ehsize));
	PRINTF(("Program header size:       %d\n", ehdr->e_phentsize));
	PRINTF(("Number of Program headers: %d\n", ehdr->e_phnum));
 done:
	return error;
}

static int
elf_getphdrs(struct file *fp, Elf_Phdr *phdr, size_t nbyte)
{
	int i;
	int error;
	int nheaders = nbyte/sizeof(Elf_Phdr);

	PRINTF(("reading phdrs section\n"));
	if ((error = read_check(fp, phdr, nbyte)) != 0)
		goto done;
	PRINTF(("headers section:\n"));
	for (i = 0; i < nheaders; i++) {
		PRINTF(("entry type:   %d\n", phdr[i].p_type));
		PRINTF(("file offset:  %jd\n", (intmax_t)phdr[i].p_offset));
		PRINTF(("virt address: %p\n", (uint32_t *)phdr[i].p_vaddr));
		PRINTF(("file size:    %jd\n", (intmax_t)phdr[i].p_filesz));
		PRINTF(("memory size:  %jd\n", (intmax_t)phdr[i].p_memsz));
		PRINTF(("\n"));
	}
 done:
	return error;
}


static int
elf_getnotes(struct lwp *lp, struct file *fp, size_t notesz)
{
	int error;
	int nthreads;
	char *note;
	prpsinfo_t *psinfo;
	prstatus_t *status;
	prfpregset_t *fpregset;

	nthreads = (notesz - sizeof(prpsinfo_t))/(sizeof(prstatus_t) +
						  sizeof(prfpregset_t));
	PRINTF(("reading notes header nthreads=%d\n", nthreads));
	if (nthreads <= 0 || nthreads > CKPT_MAXTHREADS)
		return EINVAL;

	psinfo  = kmalloc(sizeof(prpsinfo_t), M_TEMP, M_ZERO | M_WAITOK);
	status  = kmalloc(nthreads*sizeof(prstatus_t), M_TEMP, M_WAITOK);
	fpregset  = kmalloc(nthreads*sizeof(prfpregset_t), M_TEMP, M_WAITOK);
	note = kmalloc(notesz, M_TEMP, M_WAITOK);


	PRINTF(("reading notes section\n"));
	if ((error = read_check(fp, note, notesz)) != 0)
		goto done;
	error = elf_demarshalnotes(note, psinfo, status, fpregset, nthreads);
	if (error)
		goto done;
	/* fetch register state from notes */
	error = elf_loadnotes(lp, psinfo, status, fpregset);
 done:
	if (psinfo)
		kfree(psinfo, M_TEMP);
	if (status)
		kfree(status, M_TEMP);
	if (fpregset)
		kfree(fpregset, M_TEMP);
	if (note)
		kfree(note, M_TEMP);
	return error;
}

static int
ckpt_thaw_proc(struct lwp *lp, struct file *fp)
{
	struct proc *p = lp->lwp_proc;
	Elf_Phdr *phdr = NULL;
	Elf_Ehdr *ehdr = NULL;
	int error;
	size_t nbyte;

	TRACE_ENTER;

	ehdr = kmalloc(sizeof(Elf_Ehdr), M_TEMP, M_ZERO | M_WAITOK);

	if ((error = elf_gethdr(fp, ehdr)) != 0)
		goto done;
	nbyte = sizeof(Elf_Phdr) * ehdr->e_phnum;
	phdr = kmalloc(nbyte, M_TEMP, M_WAITOK);

	/* fetch description of program writable mappings */
	if ((error = elf_getphdrs(fp, phdr, nbyte)) != 0)
		goto done;

	/* fetch notes section containing register state */
	if ((error = elf_getnotes(lp, fp, phdr->p_filesz)) != 0)
		goto done;

	/* fetch program text vnodes */
	if ((error = elf_gettextvp(p, fp)) != 0)
		goto done;

	/* fetch signal disposition */
	if ((error = elf_getsigs(lp, fp)) != 0) {
		kprintf("failure in recovering signals\n");
		goto done;
	}

	/* fetch open files */
	if ((error = elf_getfiles(lp, fp)) != 0)
		goto done;

	/* handle mappings last in case we are reading from a socket */
	error = elf_loadphdrs(fp, phdr, ehdr->e_phnum);

	/*
	 * Set the textvp to the checkpoint file and mark the vnode so
	 * a future checkpointing of this checkpoint-restored program
	 * will copy out the contents of the mappings rather then trying
	 * to record the vnode info related to the checkpoint file, which
	 * is likely going to be destroyed when the program is re-checkpointed.
	 */
	if (error == 0 && fp->f_data && fp->f_type == DTYPE_VNODE) {
		if (p->p_textvp)
			vrele(p->p_textvp);
		p->p_textvp = (struct vnode *)fp->f_data;
		vsetflags(p->p_textvp, VCKPT);
		vref(p->p_textvp);
	}
done:
	if (ehdr)
		kfree(ehdr, M_TEMP);
	if (phdr)
		kfree(phdr, M_TEMP);
	TRACE_EXIT;
	return error;
}

static int
elf_loadnotes(struct lwp *lp, prpsinfo_t *psinfo, prstatus_t *status,
	   prfpregset_t *fpregset)
{
	struct proc *p = lp->lwp_proc;
	int error;

	/* validate status and psinfo */
	TRACE_ENTER;
	if (status->pr_version != PRSTATUS_VERSION ||
	    status->pr_statussz != sizeof(prstatus_t) ||
	    status->pr_gregsetsz != sizeof(gregset_t) ||
	    status->pr_fpregsetsz != sizeof(fpregset_t) ||
	    psinfo->pr_version != PRPSINFO_VERSION ||
	    psinfo->pr_psinfosz != sizeof(prpsinfo_t)) {
	        PRINTF(("status check failed\n"));
		error = EINVAL;
		goto done;
	}
	/* XXX lwp handle more than one lwp*/
	if ((error = set_regs(lp, &status->pr_reg)) != 0)
		goto done;
	error = set_fpregs(lp, fpregset);
	strlcpy(p->p_comm, psinfo->pr_fname, sizeof(p->p_comm));
	/* XXX psinfo->pr_psargs not yet implemented */
 done:
	TRACE_EXIT;
	return error;
}

static int
elf_getnote(void *src, size_t *off, const char *name, unsigned int type,
	    void **desc, size_t descsz)
{
	Elf_Note note;
	int error;

	TRACE_ENTER;
	if (src == NULL) {
		error = EFAULT;
		goto done;
	}
	bcopy((char *)src + *off, &note, sizeof note);

	PRINTF(("at offset: %zd expected note of type: %d - got: %d\n",
	       *off, type, note.n_type));
	*off += sizeof note;
	if (type != note.n_type) {
		TRACE_ERR;
		error = EINVAL;
		goto done;
	}
	if (strncmp(name, (char *) src + *off, note.n_namesz) != 0) {
		error = EINVAL;
		goto done;
	}
	*off += roundup2(note.n_namesz, sizeof(Elf_Size));
	if (note.n_descsz != descsz) {
		TRACE_ERR;
		error = EINVAL;
		goto done;
	}
	if (desc)
	        bcopy((char *)src + *off, *desc, note.n_descsz);
	*off += roundup2(note.n_descsz, sizeof(Elf_Size));
	error = 0;
 done:
	TRACE_EXIT;
	return error;
}

static int
elf_demarshalnotes(void *src, prpsinfo_t *psinfo, prstatus_t *status,
		   prfpregset_t *fpregset, int nthreads)
{
	int i;
	int error;
	size_t off = 0;

	TRACE_ENTER;
	error = elf_getnote(src, &off, "CORE", NT_PRPSINFO,
			   (void **)&psinfo, sizeof(prpsinfo_t));
	if (error)
		goto done;
	error = elf_getnote(src, &off, "CORE", NT_PRSTATUS,
			   (void **)&status, sizeof(prstatus_t));
	if (error)
		goto done;
	error = elf_getnote(src, &off, "CORE", NT_FPREGSET,
			   (void **)&fpregset, sizeof(prfpregset_t));
	if (error)
		goto done;

	/*
	 * The remaining portion needs to be an integer multiple
	 * of prstatus_t and prfpregset_t
	 */
	for (i = 0 ; i < nthreads - 1; i++) {
		status++; fpregset++;
		error = elf_getnote(src, &off, "CORE", NT_PRSTATUS,
				   (void **)&status, sizeof (prstatus_t));
		if (error)
			goto done;
		error = elf_getnote(src, &off, "CORE", NT_FPREGSET,
				   (void **)&fpregset, sizeof(prfpregset_t));
		if (error)
			goto done;
	}

 done:
	TRACE_EXIT;
	return error;
}


static int
mmap_phdr(struct file *fp, Elf_Phdr *phdr)
{
	int error;
	size_t len;
	int prot;
	void *addr;
	int flags;
	off_t pos;

	TRACE_ENTER;
	pos = phdr->p_offset;
	len = phdr->p_filesz;
	addr = (void *)phdr->p_vaddr;
	flags = MAP_FIXED | MAP_NOSYNC | MAP_PRIVATE;
	prot = 0;
	if (phdr->p_flags & PF_R)
		prot |= PROT_READ;
	if (phdr->p_flags & PF_W)
		prot |= PROT_WRITE;
	if (phdr->p_flags & PF_X)
		prot |= PROT_EXEC;
	if ((error = fp_mmap(addr, len, prot, flags, fp, pos, &addr)) != 0) {
		PRINTF(("mmap failed: %d\n", error);	   );
	}
	PRINTF(("map @%08"PRIxPTR"-%08"PRIxPTR" fileoff %08x-%08x\n", (uintptr_t)addr,
		   (uintptr_t)((char *)addr + len), (int)pos, (int)(pos + len)));
	TRACE_EXIT;
	return error;
}

/*
 * Load memory mapped segments.  The segments are backed by the checkpoint
 * file.
 */
static int
elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs)
{
	int i;
	int error = 0;

	TRACE_ENTER;
	for (i = 1; i < numsegs; i++)  {
		if ((error = mmap_phdr(fp, &phdr[i])) != 0)
			break;
	}
	TRACE_EXIT;
	return error;
}

static int
elf_getsigs(struct lwp *lp, struct file *fp)
{
	struct proc *p = lp->lwp_proc;
	int error;
	struct ckpt_siginfo *csi;

	TRACE_ENTER;
	csi = kmalloc(sizeof(struct ckpt_siginfo), M_TEMP, M_ZERO | M_WAITOK);
	if ((error = read_check(fp, csi, sizeof(struct ckpt_siginfo))) != 0)
		goto done;

	if (csi->csi_ckptpisz != sizeof(struct ckpt_siginfo)) {
		TRACE_ERR;
		error = EINVAL;
		goto done;
	}
	bcopy(&csi->csi_sigacts, p->p_sigacts, sizeof(struct sigacts));
	bcopy(&csi->csi_itimerval, &p->p_realtimer, sizeof(struct itimerval));
	SIG_CANTMASK(csi->csi_sigmask);
	/* XXX lwp handle more than one lwp */
	bcopy(&csi->csi_sigmask, &lp->lwp_sigmask, sizeof(sigset_t));
	p->p_sigparent = csi->csi_sigparent;
 done:
	if (csi)
		kfree(csi, M_TEMP);
	TRACE_EXIT;
	return error;
}

/*
 * Returns a locked, refd vnode
 */
static int
ckpt_fhtovp(fhandle_t *fh, struct vnode **vpp)
{
	struct mount *mp;
	int error;

	TRACE_ENTER;
	mp = vfs_getvfs(&fh->fh_fsid);

	if (!mp) {
		TRACE_ERR;
		PRINTF(("failed to get mount - ESTALE\n"));
	        TRACE_EXIT;
		return ESTALE;
	}
	error = VFS_FHTOVP(mp, NULL, &fh->fh_fid, vpp);
	mount_drop(mp);
	if (error) {
		PRINTF(("failed with: %d\n", error));
		TRACE_ERR;
	}
	TRACE_EXIT;
	return error;
}

static int
mmap_vp(struct vn_hdr *vnh)
{
	struct vnode *vp;
	Elf_Phdr *phdr;
	struct file *fp;
	int error;
	TRACE_ENTER;

	phdr = &vnh->vnh_phdr;

	if ((error = ckpt_fhtovp(&vnh->vnh_fh, &vp)) != 0)
		return error;
	/*
	 * XXX O_RDONLY -> or O_RDWR if file is PROT_WRITE, MAP_SHARED
	 */
	if ((error = fp_vpopen(vp, O_RDONLY, &fp)) != 0) {
		vput(vp);
		return error;
	}
	error = mmap_phdr(fp, phdr);
	fp_close(fp);
	TRACE_EXIT;
	return error;
}


static int
elf_gettextvp(struct proc *p, struct file *fp)
{
	int i;
	int error;
	int vpcount;
	struct ckpt_vminfo vminfo;
	struct vn_hdr *vnh = NULL;

	TRACE_ENTER;
	if ((error = read_check(fp, &vminfo, sizeof(vminfo))) != 0)
		goto done;
	if (vminfo.cvm_dsize < 0 ||
	    vminfo.cvm_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur ||
	    vminfo.cvm_tsize < 0 ||
	    (u_quad_t)vminfo.cvm_tsize > maxtsiz ||
	    vminfo.cvm_daddr >= (caddr_t)VM_MAX_USER_ADDRESS ||
	    vminfo.cvm_taddr >= (caddr_t)VM_MAX_USER_ADDRESS
	) {
	    error = ERANGE;
	    goto done;
	}

	vmspace_exec(p, NULL);
	p->p_vmspace->vm_daddr = vminfo.cvm_daddr;
	p->p_vmspace->vm_dsize = ctob(vminfo.cvm_dsize);	/* in bytes */
	p->p_vmspace->vm_taddr = vminfo.cvm_taddr;
	p->p_vmspace->vm_tsize = ctob(vminfo.cvm_tsize);	/* in bytes */
	if ((error = read_check(fp, &vpcount, sizeof(int))) != 0)
		goto done;
	vnh = kmalloc(sizeof(struct vn_hdr) * vpcount, M_TEMP, M_WAITOK);
	if ((error = read_check(fp, vnh, sizeof(struct vn_hdr)*vpcount)) != 0)
		goto done;
	for (i = 0; i < vpcount; i++) {
		if ((error = mmap_vp(&vnh[i])) != 0)
			goto done;
	}

 done:
	if (vnh)
		kfree(vnh, M_TEMP);
	TRACE_EXIT;
	return error;
}



/* place holder */
static int
elf_getfiles(struct lwp *lp, struct file *fp)
{
	int error;
	int i;
	int filecount;
	int fd;
	struct ckpt_filehdr filehdr;
	struct ckpt_fileinfo *cfi_base = NULL;
	struct filedesc *fdp = lp->lwp_proc->p_fd;
	struct vnode *vp;
	struct file *tempfp;
	struct file *ofp;

	TRACE_ENTER;
	if ((error = read_check(fp, &filehdr, sizeof(filehdr))) != 0)
		goto done;
	filecount = filehdr.cfh_nfiles;
	cfi_base = kmalloc(filecount*sizeof(struct ckpt_fileinfo), M_TEMP, M_WAITOK);
	error = read_check(fp, cfi_base, filecount*sizeof(struct ckpt_fileinfo));
	if (error)
		goto done;

	/*
	 * Close all file descriptors >= 3.  These descriptors are from the
	 * checkpt(1) program itself and should not be retained.
	 *
	 * XXX we need a flag so a checkpoint restore can opt to supply the
	 * descriptors, or the non-regular-file descripors.
	 */
	for (i = 3; i < fdp->fd_nfiles; ++i)
		kern_close(i);

	/*
	 * Scan files to load
	 */
	for (i = 0; i < filecount; i++) {
		struct ckpt_fileinfo *cfi= &cfi_base[i];
		/*
		 * Ignore placeholder entries where cfi_index is less then
		 * zero.  This will occur if the elf core dump code thinks
		 * it can save a vnode but winds up not being able to.
		 */
		if (cfi->cfi_index < 0)
			continue;

		/*
		 * Restore a saved file descriptor.  If CKFIF_ISCKPTFD is
		 * set the descriptor represents the checkpoint file itself,
		 * probably due to the user calling sys_checkpoint().  We
		 * want to use the fp being used to restore the checkpoint
		 * instead of trying to restore the original filehandle.
		 */
		if (cfi->cfi_ckflags & CKFIF_ISCKPTFD) {
			fhold(fp);
			tempfp = fp;
			error = 0;
		} else {
			error = ckpt_fhtovp(&cfi->cfi_fh, &vp);
			if (error == 0) {
				error = fp_vpopen(vp, OFLAGS(cfi->cfi_flags),
						  &tempfp);
				if (error)
					vput(vp);
			}
		}
		if (error)
			break;
		tempfp->f_offset = cfi->cfi_offset;

		/*
		 * If overwriting a descriptor close the old descriptor.  This
		 * only occurs if the saved core saved descriptors that we
		 * have not already closed.
		 */
		if (cfi->cfi_index < fdp->fd_nfiles &&
		    (ofp = fdp->fd_files[cfi->cfi_index].fp) != NULL) {
			kern_close(cfi->cfi_index);
		}

		/*
		 * Allocate the descriptor we want.
		 */
		if (fdalloc(lp->lwp_proc, cfi->cfi_index, &fd) != 0) {
			PRINTF(("can't currently restore fd: %d\n",
			       cfi->cfi_index));
			fp_close(fp);
			goto done;
		}
		KKASSERT(fd == cfi->cfi_index);
		fsetfd(fdp, tempfp, fd);
		fdrop(tempfp);
		cfi++;
		PRINTF(("restoring %d\n", cfi->cfi_index));
	}

 done:
	if (cfi_base)
		kfree(cfi_base, M_TEMP);
	TRACE_EXIT;
	return error;
}

static int
ckpt_freeze_proc(struct lwp *lp, struct file *fp)
{
	struct proc *p = lp->lwp_proc;
	rlim_t limit;
	int error;

	lwkt_gettoken(&p->p_token);	/* needed for proc_*() calls */

        PRINTF(("calling generic_elf_coredump\n"));
	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
	if (limit) {
		if (p->p_stat != SCORE) {
			proc_stop(p, SCORE);
			while (p->p_nstopped < p->p_nthreads - 1)
				tsleep(&p->p_nstopped, 0, "freeze", 1);
			error = generic_elf_coredump(lp, SIGCKPT, fp, limit);
			proc_unstop(p, SCORE);
		} else {
			error = ERANGE;
		}
	} else {
		error = ERANGE;
	}
	lwkt_reltoken(&p->p_token);
	return error;
}

/*
 * MPALMOSTSAFE
 */
int
sys_sys_checkpoint(struct sysmsg *sysmsg,
		   const struct sys_checkpoint_args *uap)
{
        int error = 0;
	struct thread *td = curthread;
	struct proc *p = td->td_proc;
	struct file *fp;

	/*
	 * Only certain groups (to reduce our security exposure).  -1
	 * allows any group.
	 */
	if (ckptgroup >= 0 && groupmember(ckptgroup, td->td_ucred) == 0)
		return (EPERM);

	/*
	 * For now we can only checkpoint the current process
	 */
	if (uap->pid != -1 && uap->pid != p->p_pid)
		return (EINVAL);

	get_mplock();

	switch (uap->type) {
	case CKPT_FREEZE:
		fp = NULL;
		if (uap->fd == -1 && uap->pid == (pid_t)-1)
			error = checkpoint_signal_handler(td->td_lwp);
		else if ((fp = holdfp(td, uap->fd, FWRITE)) == NULL)
			error = EBADF;
		else
			error = ckpt_freeze_proc(td->td_lwp, fp);
		if (fp)
			dropfp(td, uap->fd, fp);
		break;
	case CKPT_THAW:
		if (uap->pid != -1) {
			error = EINVAL;
			break;
		}
		if ((fp = holdfp(td, uap->fd, FREAD)) == NULL) {
			error = EBADF;
			break;
		}
		sysmsg->sysmsg_result = uap->retval;
	        error = ckpt_thaw_proc(td->td_lwp, fp);
		dropfp(td, uap->fd, fp);
		break;
	default:
	        error = EOPNOTSUPP;
		break;
	}
	rel_mplock();
	return error;
}

int
checkpoint_signal_handler(struct lwp *lp)
{
	struct thread *td = lp->lwp_thread;
	struct proc *p = lp->lwp_proc;
	char *buf;
	struct file *fp;
	struct nlookupdata nd;
	int error;

	chptinuse++;

	/*
	 * Being able to checkpoint an suid or sgid program is not a good
	 * idea.
	 */
	if (sugid_coredump == 0 && (p->p_flags & P_SUGID)) {
		chptinuse--;
		return (EPERM);
	}

	buf = ckpt_expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
	if (buf == NULL) {
		chptinuse--;
		return (ENOMEM);
	}

	log(LOG_INFO, "pid %d (%s), uid %d: checkpointing to %s\n",
		p->p_pid, p->p_comm,
		(td->td_ucred ? td->td_ucred->cr_uid : -1),
		buf);

	PRINTF(("ckpt handler called, using '%s'\n", buf));

	/*
	 * Use the same safety flags that the coredump code uses.  Remove
	 * any previous checkpoint file before writing out the new one in
	 * case we are re-checkpointing a program that had been checkpt
	 * restored.  Otherwise we will corrupt the program space (which is
	 * made up of mmap()ings of the previous checkpoint file) while we
	 * write out the new one.
	 */
	error = nlookup_init(&nd, buf, UIO_SYSSPACE, 0);
	if (error == 0)
		error = kern_unlink(&nd);
	nlookup_done(&nd);
	error = fp_open(buf, O_WRONLY|O_CREAT|O_TRUNC|O_NOFOLLOW, 0600, &fp);
	if (error == 0) {
		error = ckpt_freeze_proc(lp, fp);
		fp_close(fp);
	} else {
		kprintf("checkpoint failed with open - error: %d\n", error);
	}
	kfree(buf, M_TEMP);
	chptinuse--;
	return (error);
}

static char ckptfilename[MAXPATHLEN] = {"%N.ckpt"};
SYSCTL_STRING(_kern, OID_AUTO, ckptfile, CTLFLAG_RW, ckptfilename,
	      sizeof(ckptfilename), "process checkpoint name format string");

/*
 * expand_name(name, uid, pid)
 * Expand the name described in corefilename, using name, uid, and pid.
 * corefilename is a kprintf-like string, with three format specifiers:
 *	%N	name of process ("name")
 *	%P	process id (pid)
 *	%U	user id (uid)
 * For example, "%N.core" is the default; they can be disabled completely
 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
 * This is controlled by the sysctl variable kern.corefile (see above).
 *
 * -- taken from the coredump code
 */

static
char *
ckpt_expand_name(const char *name, uid_t uid, pid_t pid)
{
	char *temp;
	char *bp;
	char buf[11];		/* Buffer for pid/uid -- max 4B */
	int error;
	int i;
	int n;
	char *format = ckptfilename;
	size_t namelen;

	temp = kmalloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
	if (temp == NULL)
		return NULL;
	namelen = strlen(name);
	n = 0;
	if (ckptfilename[0] != '/') {
		if ((bp = kern_getcwd(temp, MAXPATHLEN - 1, &error)) == NULL) {
			kfree(temp, M_TEMP);
			return NULL;
		}
		n = strlen(bp);
		bcopy(bp, temp, n + 1);	/* normalize location of the path */
		temp[n++] = '/';
		temp[n] = '\0';
	}
	for (i= 0; n < MAXPATHLEN && format[i]; i++) {
		int l;
		switch (format[i]) {
		case '%':	/* Format character */
			i++;
			switch (format[i]) {
			case '%':
				temp[n++] = '%';
				break;
			case 'N':	/* process name */
				if ((n + namelen) > MAXPATHLEN) {
					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
					    pid, name, uid, temp, name);
					kfree(temp, M_TEMP);
					return NULL;
				}
				memcpy(temp+n, name, namelen);
				n += namelen;
				break;
			case 'P':	/* process id */
				l = ksprintf(buf, "%u", pid);
				if ((n + l) > MAXPATHLEN) {
					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
					    pid, name, uid, temp, name);
					kfree(temp, M_TEMP);
					return NULL;
				}
				memcpy(temp+n, buf, l);
				n += l;
				break;
			case 'U':	/* user id */
				l = ksprintf(buf, "%u", uid);
				if ((n + l) > MAXPATHLEN) {
					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
					    pid, name, uid, temp, name);
					kfree(temp, M_TEMP);
					return NULL;
				}
				memcpy(temp+n, buf, l);
				n += l;
				break;
			default:
			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
			}
			break;
		default:
			temp[n++] = format[i];
		}
	}
	temp[n] = '\0';
	return temp;
}