kernel - Major signal path adjustments to fix races, tsleep race fixes, +more
[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.
0ba6fbbf
MD
25 */
26
27#include <sys/types.h>
28#include <sys/param.h>
29#include <sys/proc.h>
30#include <sys/module.h>
31#include <sys/sysent.h>
32#include <sys/kernel.h>
33#include <sys/systm.h>
34#include <sys/nlookup.h>
35
36#include <sys/file.h>
0ba6fbbf
MD
37#include <sys/fcntl.h>
38#include <sys/signal.h>
39#include <vm/vm_param.h>
40#include <vm/vm.h>
41#include <sys/imgact_elf.h>
42#include <sys/procfs.h>
43
44#include <sys/lock.h>
45#include <vm/pmap.h>
46#include <vm/vm_map.h>
47#include <vm/vm_extern.h>
48#include <sys/mman.h>
0ba6fbbf
MD
49#include <sys/sysproto.h>
50#include <sys/resource.h>
51#include <sys/resourcevar.h>
52#include <sys/malloc.h>
53#include <sys/stat.h>
54#include <sys/uio.h>
55#include <sys/namei.h>
56#include <sys/vnode.h>
664f5072 57#include <machine/inttypes.h>
0ba6fbbf 58#include <machine/limits.h>
f8334305 59#include <machine/frame.h>
0ba6fbbf
MD
60#include <sys/signalvar.h>
61#include <sys/syslog.h>
62#include <sys/sysctl.h>
f8334305 63#include <machine/sigframe.h>
0ba6fbbf
MD
64#include <sys/exec.h>
65#include <sys/unistd.h>
66#include <sys/time.h>
67#include <sys/kern_syscall.h>
68#include <sys/checkpoint.h>
69#include <sys/mount.h>
70#include <sys/ckpt.h>
71
684a93c4
MD
72#include <sys/mplock2.h>
73#include <sys/file2.h>
74
0ba6fbbf 75static int elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs);
7d20a8ff 76static int elf_getnotes(struct lwp *lp, struct file *fp, size_t notesz);
0ba6fbbf
MD
77static int elf_demarshalnotes(void *src, prpsinfo_t *psinfo,
78 prstatus_t *status, prfpregset_t *fpregset, int nthreads);
7d20a8ff 79static int elf_loadnotes(struct lwp *, prpsinfo_t *, prstatus_t *,
0ba6fbbf 80 prfpregset_t *);
7d20a8ff 81static int elf_getsigs(struct lwp *lp, struct file *fp);
f3a2d8c4 82static int elf_getfiles(struct lwp *lp, struct file *fp);
0ba6fbbf
MD
83static int elf_gettextvp(struct proc *p, struct file *fp);
84static char *ckpt_expand_name(const char *name, uid_t uid, pid_t pid);
85
86static int ckptgroup = 0; /* wheel only, -1 for any group */
87SYSCTL_INT(_kern, OID_AUTO, ckptgroup, CTLFLAG_RW, &ckptgroup, 0, "");
88
89/* ref count to see how many processes that are being checkpointed */
90static int chptinuse = 0;
91
92static __inline
93int
94read_check(struct file *fp, void *buf, size_t nbyte)
95{
96 size_t nread;
97 int error;
98
664f5072 99 PRINTF(("reading %zd bytes\n", nbyte));
e7440b28 100 error = fp_read(fp, buf, nbyte, &nread, 1, UIO_SYSSPACE);
0ba6fbbf
MD
101 if (error) {
102 PRINTF(("read failed - %d", error));
103 } else if (nread != nbyte) {
664f5072 104 PRINTF(("wanted to read %zd - read %zd\n", nbyte, nread));
0ba6fbbf
MD
105 error = EINVAL;
106 }
107 return error;
108}
109
110static int
111elf_gethdr(struct file *fp, Elf_Ehdr *ehdr)
112{
113 size_t nbyte = sizeof(Elf_Ehdr);
114 int error;
115
116 if ((error = read_check(fp, ehdr, nbyte)) != 0)
117 goto done;
118 if (!(ehdr->e_ehsize == sizeof(Elf_Ehdr))) {
119 PRINTF(("wrong elf header size: %d\n"
664f5072 120 "expected size : %zd\n",
0ba6fbbf
MD
121 ehdr->e_ehsize, sizeof(Elf_Ehdr)));
122 return EINVAL;
123 }
124 if (!(ehdr->e_phentsize == sizeof(Elf_Phdr))) {
125 PRINTF(("wrong program header size: %d\n"
664f5072 126 "expected size : %zd\n",
0ba6fbbf
MD
127 ehdr->e_phentsize, sizeof(Elf_Phdr)));
128 return EINVAL;
129 }
130
131 if (!(ehdr->e_ident[EI_MAG0] == ELFMAG0 &&
132 ehdr->e_ident[EI_MAG1] == ELFMAG1 &&
133 ehdr->e_ident[EI_MAG2] == ELFMAG2 &&
134 ehdr->e_ident[EI_MAG3] == ELFMAG3 &&
135 ehdr->e_ident[EI_CLASS] == ELF_CLASS &&
136 ehdr->e_ident[EI_DATA] == ELF_DATA &&
137 ehdr->e_ident[EI_VERSION] == EV_CURRENT &&
2d4fb35d 138 ehdr->e_ident[EI_OSABI] == ELFOSABI_NONE &&
0ba6fbbf
MD
139 ehdr->e_ident[EI_ABIVERSION] == 0)) {
140 PRINTF(("bad elf header\n there are %d segments\n",
141 ehdr->e_phnum));
142 return EINVAL;
143
144 }
145 PRINTF(("Elf header size: %d\n", ehdr->e_ehsize));
146 PRINTF(("Program header size: %d\n", ehdr->e_phentsize));
147 PRINTF(("Number of Program headers: %d\n", ehdr->e_phnum));
148 done:
149 return error;
150}
151
152static int
153elf_getphdrs(struct file *fp, Elf_Phdr *phdr, size_t nbyte)
154{
155 int i;
156 int error;
157 int nheaders = nbyte/sizeof(Elf_Phdr);
158
159 PRINTF(("reading phdrs section\n"));
160 if ((error = read_check(fp, phdr, nbyte)) != 0)
161 goto done;
ff7a3478 162 PRINTF(("headers section:\n"));
0ba6fbbf 163 for (i = 0; i < nheaders; i++) {
ff7a3478 164 PRINTF(("entry type: %d\n", phdr[i].p_type));
36484f1d 165 PRINTF(("file offset: %jd\n", (intmax_t)phdr[i].p_offset));
ff7a3478 166 PRINTF(("virt address: %p\n", (uint32_t *)phdr[i].p_vaddr));
36484f1d
SW
167 PRINTF(("file size: %jd\n", (intmax_t)phdr[i].p_filesz));
168 PRINTF(("memory size: %jd\n", (intmax_t)phdr[i].p_memsz));
ff7a3478 169 PRINTF(("\n"));
0ba6fbbf
MD
170 }
171 done:
172 return error;
173}
174
175
176static int
7d20a8ff 177elf_getnotes(struct lwp *lp, struct file *fp, size_t notesz)
0ba6fbbf
MD
178{
179 int error;
180 int nthreads;
181 char *note;
182 prpsinfo_t *psinfo;
183 prstatus_t *status;
184 prfpregset_t *fpregset;
185
186 nthreads = (notesz - sizeof(prpsinfo_t))/(sizeof(prstatus_t) +
187 sizeof(prfpregset_t));
188 PRINTF(("reading notes header nthreads=%d\n", nthreads));
189 if (nthreads <= 0 || nthreads > CKPT_MAXTHREADS)
190 return EINVAL;
191
efda3bd0
MD
192 psinfo = kmalloc(sizeof(prpsinfo_t), M_TEMP, M_ZERO | M_WAITOK);
193 status = kmalloc(nthreads*sizeof(prstatus_t), M_TEMP, M_WAITOK);
194 fpregset = kmalloc(nthreads*sizeof(prfpregset_t), M_TEMP, M_WAITOK);
195 note = kmalloc(notesz, M_TEMP, M_WAITOK);
0ba6fbbf
MD
196
197
198 PRINTF(("reading notes section\n"));
199 if ((error = read_check(fp, note, notesz)) != 0)
200 goto done;
201 error = elf_demarshalnotes(note, psinfo, status, fpregset, nthreads);
202 if (error)
203 goto done;
204 /* fetch register state from notes */
7d20a8ff 205 error = elf_loadnotes(lp, psinfo, status, fpregset);
0ba6fbbf
MD
206 done:
207 if (psinfo)
efda3bd0 208 kfree(psinfo, M_TEMP);
0ba6fbbf 209 if (status)
efda3bd0 210 kfree(status, M_TEMP);
0ba6fbbf 211 if (fpregset)
efda3bd0 212 kfree(fpregset, M_TEMP);
0ba6fbbf 213 if (note)
efda3bd0 214 kfree(note, M_TEMP);
0ba6fbbf
MD
215 return error;
216}
217
218static int
7d20a8ff 219ckpt_thaw_proc(struct lwp *lp, struct file *fp)
0ba6fbbf 220{
7d20a8ff 221 struct proc *p = lp->lwp_proc;
0ba6fbbf
MD
222 Elf_Phdr *phdr = NULL;
223 Elf_Ehdr *ehdr = NULL;
224 int error;
225 size_t nbyte;
226
227 TRACE_ENTER;
228
efda3bd0 229 ehdr = kmalloc(sizeof(Elf_Ehdr), M_TEMP, M_ZERO | M_WAITOK);
0ba6fbbf
MD
230
231 if ((error = elf_gethdr(fp, ehdr)) != 0)
232 goto done;
233 nbyte = sizeof(Elf_Phdr) * ehdr->e_phnum;
efda3bd0 234 phdr = kmalloc(nbyte, M_TEMP, M_WAITOK);
0ba6fbbf
MD
235
236 /* fetch description of program writable mappings */
237 if ((error = elf_getphdrs(fp, phdr, nbyte)) != 0)
238 goto done;
239
240 /* fetch notes section containing register state */
7d20a8ff 241 if ((error = elf_getnotes(lp, fp, phdr->p_filesz)) != 0)
0ba6fbbf
MD
242 goto done;
243
244 /* fetch program text vnodes */
245 if ((error = elf_gettextvp(p, fp)) != 0)
246 goto done;
247
248 /* fetch signal disposition */
7d20a8ff 249 if ((error = elf_getsigs(lp, fp)) != 0) {
6ea70f76 250 kprintf("failure in recovering signals\n");
0ba6fbbf 251 goto done;
4f12bfd3 252 }
0ba6fbbf
MD
253
254 /* fetch open files */
f3a2d8c4 255 if ((error = elf_getfiles(lp, fp)) != 0)
0ba6fbbf
MD
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;
2247fe02 272 vsetflags(p->p_textvp, VCKPT);
0ba6fbbf
MD
273 vref(p->p_textvp);
274 }
275done:
276 if (ehdr)
efda3bd0 277 kfree(ehdr, M_TEMP);
0ba6fbbf 278 if (phdr)
efda3bd0 279 kfree(phdr, M_TEMP);
0ba6fbbf
MD
280 TRACE_EXIT;
281 return error;
282}
283
284static int
7d20a8ff 285elf_loadnotes(struct lwp *lp, prpsinfo_t *psinfo, prstatus_t *status,
0ba6fbbf
MD
286 prfpregset_t *fpregset)
287{
7d20a8ff 288 struct proc *p = lp->lwp_proc;
0ba6fbbf
MD
289 int error;
290
291 /* validate status and psinfo */
292 TRACE_ENTER;
293 if (status->pr_version != PRSTATUS_VERSION ||
294 status->pr_statussz != sizeof(prstatus_t) ||
295 status->pr_gregsetsz != sizeof(gregset_t) ||
296 status->pr_fpregsetsz != sizeof(fpregset_t) ||
297 psinfo->pr_version != PRPSINFO_VERSION ||
298 psinfo->pr_psinfosz != sizeof(prpsinfo_t)) {
299 PRINTF(("status check failed\n"));
300 error = EINVAL;
301 goto done;
302 }
7d20a8ff 303 /* XXX lwp handle more than one lwp*/
08f2f1bb 304 if ((error = set_regs(lp, &status->pr_reg)) != 0)
0ba6fbbf 305 goto done;
08f2f1bb 306 error = set_fpregs(lp, fpregset);
0ba6fbbf
MD
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
314static int
315elf_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
664f5072 328 PRINTF(("at offset: %zd expected note of type: %d - got: %d\n",
0ba6fbbf
MD
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
355static int
356elf_demarshalnotes(void *src, prpsinfo_t *psinfo, prstatus_t *status,
357 prfpregset_t *fpregset, int nthreads)
358{
359 int i;
360 int error;
973c11b9 361 size_t off = 0;
0ba6fbbf
MD
362
363 TRACE_ENTER;
2d4fb35d 364 error = elf_getnote(src, &off, "CORE", NT_PRPSINFO,
075a756c
MD
365 (void **)&psinfo, sizeof(prpsinfo_t));
366 if (error)
367 goto done;
2d4fb35d 368 error = elf_getnote(src, &off, "CORE", NT_PRSTATUS,
0ba6fbbf
MD
369 (void **)&status, sizeof(prstatus_t));
370 if (error)
371 goto done;
2d4fb35d 372 error = elf_getnote(src, &off, "CORE", NT_FPREGSET,
0ba6fbbf
MD
373 (void **)&fpregset, sizeof(prfpregset_t));
374 if (error)
375 goto done;
0ba6fbbf
MD
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++;
2d4fb35d 383 error = elf_getnote(src, &off, "CORE", NT_PRSTATUS,
0ba6fbbf
MD
384 (void **)&status, sizeof (prstatus_t));
385 if (error)
386 goto done;
2d4fb35d 387 error = elf_getnote(src, &off, "CORE", NT_FPREGSET,
0ba6fbbf
MD
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
399static int
400mmap_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 }
664f5072
SW
424 PRINTF(("map @%08"PRIxPTR"-%08"PRIxPTR" fileoff %08x-%08x\n", (uintptr_t)addr,
425 (uintptr_t)((char *)addr + len), (int)pos, (int)(pos + len)));
0ba6fbbf
MD
426 TRACE_EXIT;
427 return error;
428}
429
ff7a3478
MD
430/*
431 * Load memory mapped segments. The segments are backed by the checkpoint
432 * file.
433 */
0ba6fbbf
MD
434static int
435elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs)
436{
437 int i;
438 int error = 0;
439
440 TRACE_ENTER;
441 for (i = 1; i < numsegs; i++) {
442 if ((error = mmap_phdr(fp, &phdr[i])) != 0)
443 break;
444 }
445 TRACE_EXIT;
446 return error;
447}
448
449static int
7d20a8ff 450elf_getsigs(struct lwp *lp, struct file *fp)
0ba6fbbf 451{
7d20a8ff 452 struct proc *p = lp->lwp_proc;
0ba6fbbf
MD
453 int error;
454 struct ckpt_siginfo *csi;
0ba6fbbf
MD
455
456 TRACE_ENTER;
efda3bd0 457 csi = kmalloc(sizeof(struct ckpt_siginfo), M_TEMP, M_ZERO | M_WAITOK);
0ba6fbbf
MD
458 if ((error = read_check(fp, csi, sizeof(struct ckpt_siginfo))) != 0)
459 goto done;
460
461 if (csi->csi_ckptpisz != sizeof(struct ckpt_siginfo)) {
462 TRACE_ERR;
463 error = EINVAL;
464 goto done;
465 }
b1b4e5a6 466 bcopy(&csi->csi_sigacts, p->p_sigacts, sizeof(p->p_sigacts));
0ba6fbbf 467 bcopy(&csi->csi_itimerval, &p->p_realtimer, sizeof(struct itimerval));
4f12bfd3 468 SIG_CANTMASK(csi->csi_sigmask);
7d20a8ff 469 /* XXX lwp handle more than one lwp */
08f2f1bb 470 bcopy(&csi->csi_sigmask, &lp->lwp_sigmask, sizeof(sigset_t));
0ba6fbbf
MD
471 p->p_sigparent = csi->csi_sigparent;
472 done:
473 if (csi)
efda3bd0 474 kfree(csi, M_TEMP);
0ba6fbbf
MD
475 TRACE_EXIT;
476 return error;
477}
478
479/*
480 * Returns a locked, refd vnode
481 */
482static int
483ckpt_fhtovp(fhandle_t *fh, struct vnode **vpp)
484{
485 struct mount *mp;
486 int error;
487
488 TRACE_ENTER;
489 mp = vfs_getvfs(&fh->fh_fsid);
490
491 if (!mp) {
492 TRACE_ERR;
493 PRINTF(("failed to get mount - ESTALE\n"));
494 TRACE_EXIT;
495 return ESTALE;
496 }
67863d04 497 error = VFS_FHTOVP(mp, NULL, &fh->fh_fid, vpp);
0ba6fbbf
MD
498 if (error) {
499 PRINTF(("failed with: %d\n", error));
500 TRACE_ERR;
501 TRACE_EXIT;
502 return error;
503 }
504 TRACE_EXIT;
505 return 0;
506}
507
508static int
509mmap_vp(struct vn_hdr *vnh)
510{
511 struct vnode *vp;
512 Elf_Phdr *phdr;
513 struct file *fp;
514 int error;
515 TRACE_ENTER;
516
517 phdr = &vnh->vnh_phdr;
518
519 if ((error = ckpt_fhtovp(&vnh->vnh_fh, &vp)) != 0)
520 return error;
521 /*
522 * XXX O_RDONLY -> or O_RDWR if file is PROT_WRITE, MAP_SHARED
523 */
524 if ((error = fp_vpopen(vp, O_RDONLY, &fp)) != 0) {
525 vput(vp);
526 return error;
527 }
528 error = mmap_phdr(fp, phdr);
529 fp_close(fp);
530 TRACE_EXIT;
531 return error;
532}
533
534
535static int
536elf_gettextvp(struct proc *p, struct file *fp)
537{
538 int i;
539 int error;
540 int vpcount;
541 struct ckpt_vminfo vminfo;
542 struct vn_hdr *vnh = NULL;
543
544 TRACE_ENTER;
545 if ((error = read_check(fp, &vminfo, sizeof(vminfo))) != 0)
546 goto done;
547 if (vminfo.cvm_dsize < 0 ||
548 vminfo.cvm_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur ||
549 vminfo.cvm_tsize < 0 ||
550 (u_quad_t)vminfo.cvm_tsize > maxtsiz ||
88181b08
MD
551 vminfo.cvm_daddr >= (caddr_t)VM_MAX_USER_ADDRESS ||
552 vminfo.cvm_taddr >= (caddr_t)VM_MAX_USER_ADDRESS
0ba6fbbf
MD
553 ) {
554 error = ERANGE;
555 goto done;
556 }
557
558 vmspace_exec(p, NULL);
559 p->p_vmspace->vm_daddr = vminfo.cvm_daddr;
560 p->p_vmspace->vm_dsize = vminfo.cvm_dsize;
561 p->p_vmspace->vm_taddr = vminfo.cvm_taddr;
562 p->p_vmspace->vm_tsize = vminfo.cvm_tsize;
563 if ((error = read_check(fp, &vpcount, sizeof(int))) != 0)
564 goto done;
efda3bd0 565 vnh = kmalloc(sizeof(struct vn_hdr) * vpcount, M_TEMP, M_WAITOK);
0ba6fbbf
MD
566 if ((error = read_check(fp, vnh, sizeof(struct vn_hdr)*vpcount)) != 0)
567 goto done;
568 for (i = 0; i < vpcount; i++) {
569 if ((error = mmap_vp(&vnh[i])) != 0)
570 goto done;
571 }
572
573 done:
574 if (vnh)
efda3bd0 575 kfree(vnh, M_TEMP);
0ba6fbbf
MD
576 TRACE_EXIT;
577 return error;
578}
579
580
581
582/* place holder */
583static int
f3a2d8c4 584elf_getfiles(struct lwp *lp, struct file *fp)
0ba6fbbf
MD
585{
586 int error;
587 int i;
588 int filecount;
589 int fd;
590 struct ckpt_filehdr filehdr;
591 struct ckpt_fileinfo *cfi_base = NULL;
f3a2d8c4 592 struct filedesc *fdp = lp->lwp_proc->p_fd;
0ba6fbbf
MD
593 struct vnode *vp;
594 struct file *tempfp;
595 struct file *ofp;
596
597 TRACE_ENTER;
598 if ((error = read_check(fp, &filehdr, sizeof(filehdr))) != 0)
599 goto done;
600 filecount = filehdr.cfh_nfiles;
efda3bd0 601 cfi_base = kmalloc(filecount*sizeof(struct ckpt_fileinfo), M_TEMP, M_WAITOK);
0ba6fbbf
MD
602 error = read_check(fp, cfi_base, filecount*sizeof(struct ckpt_fileinfo));
603 if (error)
604 goto done;
605
606 /*
4f12bfd3 607 * Close all file descriptors >= 3. These descriptors are from the
0ba6fbbf 608 * checkpt(1) program itself and should not be retained.
4f12bfd3
MD
609 *
610 * XXX we need a flag so a checkpoint restore can opt to supply the
611 * descriptors, or the non-regular-file descripors.
0ba6fbbf 612 */
f3a2d8c4 613 for (i = 3; i < fdp->fd_nfiles; ++i)
0ba6fbbf
MD
614 kern_close(i);
615
616 /*
617 * Scan files to load
618 */
619 for (i = 0; i < filecount; i++) {
620 struct ckpt_fileinfo *cfi= &cfi_base[i];
621 /*
622 * Ignore placeholder entries where cfi_index is less then
623 * zero. This will occur if the elf core dump code thinks
624 * it can save a vnode but winds up not being able to.
625 */
626 if (cfi->cfi_index < 0)
627 continue;
628
ff7a3478
MD
629 /*
630 * Restore a saved file descriptor. If CKFIF_ISCKPTFD is
631 * set the descriptor represents the checkpoint file itself,
632 * probably due to the user calling sys_checkpoint(). We
633 * want to use the fp being used to restore the checkpoint
634 * instead of trying to restore the original filehandle.
635 */
636 if (cfi->cfi_ckflags & CKFIF_ISCKPTFD) {
637 fhold(fp);
638 tempfp = fp;
639 error = 0;
640 } else {
641 error = ckpt_fhtovp(&cfi->cfi_fh, &vp);
642 if (error == 0) {
643 error = fp_vpopen(vp, OFLAGS(cfi->cfi_flags),
644 &tempfp);
645 if (error)
646 vput(vp);
647 }
0ba6fbbf 648 }
ff7a3478
MD
649 if (error)
650 break;
0ba6fbbf
MD
651 tempfp->f_offset = cfi->cfi_offset;
652
653 /*
654 * If overwriting a descriptor close the old descriptor. This
655 * only occurs if the saved core saved descriptors that we
656 * have not already closed.
657 */
f3a2d8c4
MD
658 if (cfi->cfi_index < fdp->fd_nfiles &&
659 (ofp = fdp->fd_files[cfi->cfi_index].fp) != NULL) {
0ba6fbbf
MD
660 kern_close(cfi->cfi_index);
661 }
662
663 /*
664 * Allocate the descriptor we want.
665 */
f3a2d8c4 666 if (fdalloc(lp->lwp_proc, cfi->cfi_index, &fd) != 0) {
0ba6fbbf
MD
667 PRINTF(("can't currently restore fd: %d\n",
668 cfi->cfi_index));
669 fp_close(fp);
670 goto done;
671 }
672 KKASSERT(fd == cfi->cfi_index);
f3a2d8c4 673 fsetfd(fdp, tempfp, fd);
259b8ea0 674 fdrop(tempfp);
0ba6fbbf
MD
675 cfi++;
676 PRINTF(("restoring %d\n", cfi->cfi_index));
677 }
678
679 done:
680 if (cfi_base)
efda3bd0 681 kfree(cfi_base, M_TEMP);
0ba6fbbf
MD
682 TRACE_EXIT;
683 return error;
684}
685
686static int
7d20a8ff 687ckpt_freeze_proc(struct lwp *lp, struct file *fp)
0ba6fbbf 688{
7d20a8ff 689 struct proc *p = lp->lwp_proc;
0ba6fbbf
MD
690 rlim_t limit;
691 int error;
692
5686ec5a 693 lwkt_gettoken(&p->p_token); /* needed for proc_*() calls */
98c2b8ac 694
0ba6fbbf
MD
695 PRINTF(("calling generic_elf_coredump\n"));
696 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
697 if (limit) {
6acb4ed8
SS
698 proc_stop(p);
699 while (p->p_nstopped < p->p_nthreads - 1)
700 tsleep(&p->p_nstopped, 0, "freeze", 1);
7d20a8ff 701 error = generic_elf_coredump(lp, SIGCKPT, fp, limit);
6acb4ed8 702 proc_unstop(p);
0ba6fbbf
MD
703 } else {
704 error = ERANGE;
705 }
5686ec5a 706 lwkt_reltoken(&p->p_token);
0ba6fbbf
MD
707 return error;
708}
709
3919ced0
MD
710/*
711 * MPALMOSTSAFE
712 */
0ba6fbbf 713int
753fd850 714sys_sys_checkpoint(struct sys_checkpoint_args *uap)
0ba6fbbf
MD
715{
716 int error = 0;
9910d07b
MD
717 struct thread *td = curthread;
718 struct proc *p = td->td_proc;
f3a2d8c4 719 struct filedesc *fdp = p->p_fd;
0ba6fbbf
MD
720 struct file *fp;
721
722 /*
723 * Only certain groups (to reduce our security exposure). -1
724 * allows any group.
725 */
9910d07b 726 if (ckptgroup >= 0 && groupmember(ckptgroup, td->td_ucred) == 0)
0ba6fbbf
MD
727 return (EPERM);
728
729 /*
730 * For now we can only checkpoint the current process
731 */
732 if (uap->pid != -1 && uap->pid != p->p_pid)
733 return (EINVAL);
734
3919ced0
MD
735 get_mplock();
736
0ba6fbbf
MD
737 switch (uap->type) {
738 case CKPT_FREEZE:
739 fp = NULL;
740 if (uap->fd == -1 && uap->pid == (pid_t)-1)
9910d07b 741 error = checkpoint_signal_handler(td->td_lwp);
f3a2d8c4 742 else if ((fp = holdfp(fdp, uap->fd, FWRITE)) == NULL)
0ba6fbbf
MD
743 error = EBADF;
744 else
9910d07b 745 error = ckpt_freeze_proc(td->td_lwp, fp);
0ba6fbbf 746 if (fp)
9f87144f 747 fdrop(fp);
0ba6fbbf
MD
748 break;
749 case CKPT_THAW:
3919ced0
MD
750 if (uap->pid != -1) {
751 error = EINVAL;
752 break;
753 }
f3a2d8c4 754 if ((fp = holdfp(fdp, uap->fd, FREAD)) == NULL) {
3919ced0
MD
755 error = EBADF;
756 break;
757 }
0ba6fbbf 758 uap->sysmsg_result = uap->retval;
9910d07b 759 error = ckpt_thaw_proc(td->td_lwp, fp);
9f87144f 760 fdrop(fp);
0ba6fbbf
MD
761 break;
762 default:
763 error = EOPNOTSUPP;
764 break;
765 }
3919ced0 766 rel_mplock();
0ba6fbbf
MD
767 return error;
768}
769
770int
7d20a8ff 771checkpoint_signal_handler(struct lwp *lp)
0ba6fbbf 772{
9910d07b 773 struct thread *td = lp->lwp_thread;
7d20a8ff 774 struct proc *p = lp->lwp_proc;
0ba6fbbf
MD
775 char *buf;
776 struct file *fp;
777 struct nlookupdata nd;
778 int error;
779
780 chptinuse++;
781
782 /*
783 * Being able to checkpoint an suid or sgid program is not a good
784 * idea.
785 */
4643740a 786 if (sugid_coredump == 0 && (p->p_flags & P_SUGID)) {
0ba6fbbf
MD
787 chptinuse--;
788 return (EPERM);
789 }
790
9910d07b 791 buf = ckpt_expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
0ba6fbbf
MD
792 if (buf == NULL) {
793 chptinuse--;
794 return (ENOMEM);
795 }
796
797 log(LOG_INFO, "pid %d (%s), uid %d: checkpointing to %s\n",
798 p->p_pid, p->p_comm,
9910d07b 799 (td->td_ucred ? td->td_ucred->cr_uid : -1),
0ba6fbbf
MD
800 buf);
801
802 PRINTF(("ckpt handler called, using '%s'\n", buf));
803
804 /*
805 * Use the same safety flags that the coredump code uses. Remove
806 * any previous checkpoint file before writing out the new one in
807 * case we are re-checkpointing a program that had been checkpt
808 * restored. Otherwise we will corrupt the program space (which is
809 * made up of mmap()ings of the previous checkpoint file) while we
810 * write out the new one.
811 */
812 error = nlookup_init(&nd, buf, UIO_SYSSPACE, 0);
813 if (error == 0)
814 error = kern_unlink(&nd);
815 nlookup_done(&nd);
816 error = fp_open(buf, O_WRONLY|O_CREAT|O_TRUNC|O_NOFOLLOW, 0600, &fp);
817 if (error == 0) {
7d20a8ff 818 error = ckpt_freeze_proc(lp, fp);
0ba6fbbf
MD
819 fp_close(fp);
820 } else {
6ea70f76 821 kprintf("checkpoint failed with open - error: %d\n", error);
0ba6fbbf 822 }
efda3bd0 823 kfree(buf, M_TEMP);
0ba6fbbf
MD
824 chptinuse--;
825 return (error);
826}
827
828static char ckptfilename[MAXPATHLEN] = {"%N.ckpt"};
829SYSCTL_STRING(_kern, OID_AUTO, ckptfile, CTLFLAG_RW, ckptfilename,
830 sizeof(ckptfilename), "process checkpoint name format string");
831
832/*
833 * expand_name(name, uid, pid)
834 * Expand the name described in corefilename, using name, uid, and pid.
6ea70f76 835 * corefilename is a kprintf-like string, with three format specifiers:
0ba6fbbf
MD
836 * %N name of process ("name")
837 * %P process id (pid)
838 * %U user id (uid)
839 * For example, "%N.core" is the default; they can be disabled completely
840 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
841 * This is controlled by the sysctl variable kern.corefile (see above).
842 *
843 * -- taken from the coredump code
844 */
845
846static
847char *
848ckpt_expand_name(const char *name, uid_t uid, pid_t pid)
849{
850 char *temp;
851 char *bp;
852 char buf[11]; /* Buffer for pid/uid -- max 4B */
853 int error;
854 int i;
855 int n;
856 char *format = ckptfilename;
857 size_t namelen;
858
efda3bd0 859 temp = kmalloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
0ba6fbbf
MD
860 if (temp == NULL)
861 return NULL;
862 namelen = strlen(name);
863 n = 0;
864 if (ckptfilename[0] != '/') {
865 if ((bp = kern_getcwd(temp, MAXPATHLEN - 1, &error)) == NULL) {
efda3bd0 866 kfree(temp, M_TEMP);
0ba6fbbf
MD
867 return NULL;
868 }
869 n = strlen(bp);
870 bcopy(bp, temp, n + 1); /* normalize location of the path */
871 temp[n++] = '/';
872 temp[n] = '\0';
873 }
874 for (i= 0; n < MAXPATHLEN && format[i]; i++) {
875 int l;
876 switch (format[i]) {
877 case '%': /* Format character */
878 i++;
879 switch (format[i]) {
880 case '%':
881 temp[n++] = '%';
882 break;
883 case 'N': /* process name */
884 if ((n + namelen) > MAXPATHLEN) {
885 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
886 pid, name, uid, temp, name);
efda3bd0 887 kfree(temp, M_TEMP);
0ba6fbbf
MD
888 return NULL;
889 }
890 memcpy(temp+n, name, namelen);
891 n += namelen;
892 break;
893 case 'P': /* process id */
f8c7a42d 894 l = ksprintf(buf, "%u", pid);
0ba6fbbf
MD
895 if ((n + l) > MAXPATHLEN) {
896 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
897 pid, name, uid, temp, name);
efda3bd0 898 kfree(temp, M_TEMP);
0ba6fbbf
MD
899 return NULL;
900 }
901 memcpy(temp+n, buf, l);
902 n += l;
903 break;
904 case 'U': /* user id */
f8c7a42d 905 l = ksprintf(buf, "%u", uid);
0ba6fbbf
MD
906 if ((n + l) > MAXPATHLEN) {
907 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
908 pid, name, uid, temp, name);
efda3bd0 909 kfree(temp, M_TEMP);
0ba6fbbf
MD
910 return NULL;
911 }
912 memcpy(temp+n, buf, l);
913 n += l;
914 break;
915 default:
916 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
917 }
918 break;
919 default:
920 temp[n++] = format[i];
921 }
922 }
923 temp[n] = '\0';
924 return temp;
925}
926