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