kernel - Add per-process token, adjust signal code to use it.
[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 *
67863d04 26 * $DragonFly: src/sys/kern/kern_checkpoint.c,v 1.20 2008/09/17 21:44:18 dillon Exp $
0ba6fbbf
MD
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>
0ba6fbbf
MD
39#include <sys/fcntl.h>
40#include <sys/signal.h>
41#include <vm/vm_param.h>
42#include <vm/vm.h>
43#include <sys/imgact_elf.h>
44#include <sys/procfs.h>
45
46#include <sys/lock.h>
47#include <vm/pmap.h>
48#include <vm/vm_map.h>
49#include <vm/vm_extern.h>
50#include <sys/mman.h>
51#include <sys/sysent.h>
52#include <sys/sysproto.h>
53#include <sys/resource.h>
54#include <sys/resourcevar.h>
55#include <sys/malloc.h>
56#include <sys/stat.h>
57#include <sys/uio.h>
58#include <sys/namei.h>
59#include <sys/vnode.h>
60#include <machine/limits.h>
f8334305 61#include <machine/frame.h>
0ba6fbbf
MD
62#include <sys/signalvar.h>
63#include <sys/syslog.h>
64#include <sys/sysctl.h>
f8334305 65#include <machine/sigframe.h>
0ba6fbbf
MD
66#include <sys/exec.h>
67#include <sys/unistd.h>
68#include <sys/time.h>
69#include <sys/kern_syscall.h>
70#include <sys/checkpoint.h>
71#include <sys/mount.h>
72#include <sys/ckpt.h>
73
684a93c4
MD
74#include <sys/mplock2.h>
75#include <sys/file2.h>
76
0ba6fbbf 77static int elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs);
7d20a8ff 78static int elf_getnotes(struct lwp *lp, struct file *fp, size_t notesz);
0ba6fbbf
MD
79static int elf_demarshalnotes(void *src, prpsinfo_t *psinfo,
80 prstatus_t *status, prfpregset_t *fpregset, int nthreads);
7d20a8ff 81static int elf_loadnotes(struct lwp *, prpsinfo_t *, prstatus_t *,
0ba6fbbf 82 prfpregset_t *);
7d20a8ff 83static int elf_getsigs(struct lwp *lp, struct file *fp);
f3a2d8c4 84static int elf_getfiles(struct lwp *lp, struct file *fp);
0ba6fbbf
MD
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
374a52ac 101 PRINTF(("reading %d bytes\n", nbyte));
e7440b28 102 error = fp_read(fp, buf, nbyte, &nread, 1, UIO_SYSSPACE);
0ba6fbbf
MD
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 &&
2d4fb35d 140 ehdr->e_ident[EI_OSABI] == ELFOSABI_NONE &&
0ba6fbbf
MD
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;
ff7a3478 164 PRINTF(("headers section:\n"));
0ba6fbbf 165 for (i = 0; i < nheaders; i++) {
ff7a3478
MD
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"));
0ba6fbbf
MD
172 }
173 done:
174 return error;
175}
176
177
178static int
7d20a8ff 179elf_getnotes(struct lwp *lp, struct file *fp, size_t notesz)
0ba6fbbf
MD
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
efda3bd0
MD
194 psinfo = kmalloc(sizeof(prpsinfo_t), M_TEMP, M_ZERO | M_WAITOK);
195 status = kmalloc(nthreads*sizeof(prstatus_t), M_TEMP, M_WAITOK);
196 fpregset = kmalloc(nthreads*sizeof(prfpregset_t), M_TEMP, M_WAITOK);
197 note = kmalloc(notesz, M_TEMP, M_WAITOK);
0ba6fbbf
MD
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 */
7d20a8ff 207 error = elf_loadnotes(lp, psinfo, status, fpregset);
0ba6fbbf
MD
208 done:
209 if (psinfo)
efda3bd0 210 kfree(psinfo, M_TEMP);
0ba6fbbf 211 if (status)
efda3bd0 212 kfree(status, M_TEMP);
0ba6fbbf 213 if (fpregset)
efda3bd0 214 kfree(fpregset, M_TEMP);
0ba6fbbf 215 if (note)
efda3bd0 216 kfree(note, M_TEMP);
0ba6fbbf
MD
217 return error;
218}
219
220static int
7d20a8ff 221ckpt_thaw_proc(struct lwp *lp, struct file *fp)
0ba6fbbf 222{
7d20a8ff 223 struct proc *p = lp->lwp_proc;
0ba6fbbf
MD
224 Elf_Phdr *phdr = NULL;
225 Elf_Ehdr *ehdr = NULL;
226 int error;
227 size_t nbyte;
228
229 TRACE_ENTER;
230
efda3bd0 231 ehdr = kmalloc(sizeof(Elf_Ehdr), M_TEMP, M_ZERO | M_WAITOK);
0ba6fbbf
MD
232
233 if ((error = elf_gethdr(fp, ehdr)) != 0)
234 goto done;
235 nbyte = sizeof(Elf_Phdr) * ehdr->e_phnum;
efda3bd0 236 phdr = kmalloc(nbyte, M_TEMP, M_WAITOK);
0ba6fbbf
MD
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 */
7d20a8ff 243 if ((error = elf_getnotes(lp, fp, phdr->p_filesz)) != 0)
0ba6fbbf
MD
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 */
7d20a8ff 251 if ((error = elf_getsigs(lp, fp)) != 0) {
6ea70f76 252 kprintf("failure in recovering signals\n");
0ba6fbbf 253 goto done;
4f12bfd3 254 }
0ba6fbbf
MD
255
256 /* fetch open files */
f3a2d8c4 257 if ((error = elf_getfiles(lp, fp)) != 0)
0ba6fbbf
MD
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;
2247fe02 274 vsetflags(p->p_textvp, VCKPT);
0ba6fbbf
MD
275 vref(p->p_textvp);
276 }
277done:
278 if (ehdr)
efda3bd0 279 kfree(ehdr, M_TEMP);
0ba6fbbf 280 if (phdr)
efda3bd0 281 kfree(phdr, M_TEMP);
0ba6fbbf
MD
282 TRACE_EXIT;
283 return error;
284}
285
286static int
7d20a8ff 287elf_loadnotes(struct lwp *lp, prpsinfo_t *psinfo, prstatus_t *status,
0ba6fbbf
MD
288 prfpregset_t *fpregset)
289{
7d20a8ff 290 struct proc *p = lp->lwp_proc;
0ba6fbbf
MD
291 int error;
292
293 /* validate status and psinfo */
294 TRACE_ENTER;
295 if (status->pr_version != PRSTATUS_VERSION ||
296 status->pr_statussz != sizeof(prstatus_t) ||
297 status->pr_gregsetsz != sizeof(gregset_t) ||
298 status->pr_fpregsetsz != sizeof(fpregset_t) ||
299 psinfo->pr_version != PRPSINFO_VERSION ||
300 psinfo->pr_psinfosz != sizeof(prpsinfo_t)) {
301 PRINTF(("status check failed\n"));
302 error = EINVAL;
303 goto done;
304 }
7d20a8ff 305 /* XXX lwp handle more than one lwp*/
08f2f1bb 306 if ((error = set_regs(lp, &status->pr_reg)) != 0)
0ba6fbbf 307 goto done;
08f2f1bb 308 error = set_fpregs(lp, fpregset);
0ba6fbbf
MD
309 strlcpy(p->p_comm, psinfo->pr_fname, sizeof(p->p_comm));
310 /* XXX psinfo->pr_psargs not yet implemented */
311 done:
312 TRACE_EXIT;
313 return error;
314}
315
316static int
317elf_getnote(void *src, size_t *off, const char *name, unsigned int type,
318 void **desc, size_t descsz)
319{
320 Elf_Note note;
321 int error;
322
323 TRACE_ENTER;
324 if (src == NULL) {
325 error = EFAULT;
326 goto done;
327 }
328 bcopy((char *)src + *off, &note, sizeof note);
329
330 PRINTF(("at offset: %d expected note of type: %d - got: %d\n",
331 *off, type, note.n_type));
332 *off += sizeof note;
333 if (type != note.n_type) {
334 TRACE_ERR;
335 error = EINVAL;
336 goto done;
337 }
338 if (strncmp(name, (char *) src + *off, note.n_namesz) != 0) {
339 error = EINVAL;
340 goto done;
341 }
342 *off += roundup2(note.n_namesz, sizeof(Elf_Size));
343 if (note.n_descsz != descsz) {
344 TRACE_ERR;
345 error = EINVAL;
346 goto done;
347 }
348 if (desc)
349 bcopy((char *)src + *off, *desc, note.n_descsz);
350 *off += roundup2(note.n_descsz, sizeof(Elf_Size));
351 error = 0;
352 done:
353 TRACE_EXIT;
354 return error;
355}
356
357static int
358elf_demarshalnotes(void *src, prpsinfo_t *psinfo, prstatus_t *status,
359 prfpregset_t *fpregset, int nthreads)
360{
361 int i;
362 int error;
973c11b9 363 size_t off = 0;
0ba6fbbf
MD
364
365 TRACE_ENTER;
2d4fb35d 366 error = elf_getnote(src, &off, "CORE", NT_PRPSINFO,
075a756c
MD
367 (void **)&psinfo, sizeof(prpsinfo_t));
368 if (error)
369 goto done;
2d4fb35d 370 error = elf_getnote(src, &off, "CORE", NT_PRSTATUS,
0ba6fbbf
MD
371 (void **)&status, sizeof(prstatus_t));
372 if (error)
373 goto done;
2d4fb35d 374 error = elf_getnote(src, &off, "CORE", NT_FPREGSET,
0ba6fbbf
MD
375 (void **)&fpregset, sizeof(prfpregset_t));
376 if (error)
377 goto done;
0ba6fbbf
MD
378
379 /*
380 * The remaining portion needs to be an integer multiple
381 * of prstatus_t and prfpregset_t
382 */
383 for (i = 0 ; i < nthreads - 1; i++) {
384 status++; fpregset++;
2d4fb35d 385 error = elf_getnote(src, &off, "CORE", NT_PRSTATUS,
0ba6fbbf
MD
386 (void **)&status, sizeof (prstatus_t));
387 if (error)
388 goto done;
2d4fb35d 389 error = elf_getnote(src, &off, "CORE", NT_FPREGSET,
0ba6fbbf
MD
390 (void **)&fpregset, sizeof(prfpregset_t));
391 if (error)
392 goto done;
393 }
394
395 done:
396 TRACE_EXIT;
397 return error;
398}
399
400
401static int
402mmap_phdr(struct file *fp, Elf_Phdr *phdr)
403{
404 int error;
405 size_t len;
406 int prot;
407 void *addr;
408 int flags;
409 off_t pos;
410
411 TRACE_ENTER;
412 pos = phdr->p_offset;
413 len = phdr->p_filesz;
414 addr = (void *)phdr->p_vaddr;
415 flags = MAP_FIXED | MAP_NOSYNC | MAP_PRIVATE;
416 prot = 0;
417 if (phdr->p_flags & PF_R)
418 prot |= PROT_READ;
419 if (phdr->p_flags & PF_W)
420 prot |= PROT_WRITE;
421 if (phdr->p_flags & PF_X)
422 prot |= PROT_EXEC;
423 if ((error = fp_mmap(addr, len, prot, flags, fp, pos, &addr)) != 0) {
424 PRINTF(("mmap failed: %d\n", error); );
425 }
374a52ac
HP
426 PRINTF(("map @%08x-%08x fileoff %08x-%08x\n", (int)addr,
427 (int)((char *)addr + len), (int)pos, (int)(pos + len)));
0ba6fbbf
MD
428 TRACE_EXIT;
429 return error;
430}
431
ff7a3478
MD
432/*
433 * Load memory mapped segments. The segments are backed by the checkpoint
434 * file.
435 */
0ba6fbbf
MD
436static int
437elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs)
438{
439 int i;
440 int error = 0;
441
442 TRACE_ENTER;
443 for (i = 1; i < numsegs; i++) {
444 if ((error = mmap_phdr(fp, &phdr[i])) != 0)
445 break;
446 }
447 TRACE_EXIT;
448 return error;
449}
450
451static int
7d20a8ff 452elf_getsigs(struct lwp *lp, struct file *fp)
0ba6fbbf 453{
7d20a8ff 454 struct proc *p = lp->lwp_proc;
0ba6fbbf
MD
455 int error;
456 struct ckpt_siginfo *csi;
0ba6fbbf
MD
457
458 TRACE_ENTER;
efda3bd0 459 csi = kmalloc(sizeof(struct ckpt_siginfo), M_TEMP, M_ZERO | M_WAITOK);
0ba6fbbf
MD
460 if ((error = read_check(fp, csi, sizeof(struct ckpt_siginfo))) != 0)
461 goto done;
462
463 if (csi->csi_ckptpisz != sizeof(struct ckpt_siginfo)) {
464 TRACE_ERR;
465 error = EINVAL;
466 goto done;
467 }
b1b4e5a6 468 bcopy(&csi->csi_sigacts, p->p_sigacts, sizeof(p->p_sigacts));
0ba6fbbf 469 bcopy(&csi->csi_itimerval, &p->p_realtimer, sizeof(struct itimerval));
4f12bfd3 470 SIG_CANTMASK(csi->csi_sigmask);
7d20a8ff 471 /* XXX lwp handle more than one lwp */
08f2f1bb 472 bcopy(&csi->csi_sigmask, &lp->lwp_sigmask, sizeof(sigset_t));
0ba6fbbf
MD
473 p->p_sigparent = csi->csi_sigparent;
474 done:
475 if (csi)
efda3bd0 476 kfree(csi, M_TEMP);
0ba6fbbf
MD
477 TRACE_EXIT;
478 return error;
479}
480
481/*
482 * Returns a locked, refd vnode
483 */
484static int
485ckpt_fhtovp(fhandle_t *fh, struct vnode **vpp)
486{
487 struct mount *mp;
488 int error;
489
490 TRACE_ENTER;
491 mp = vfs_getvfs(&fh->fh_fsid);
492
493 if (!mp) {
494 TRACE_ERR;
495 PRINTF(("failed to get mount - ESTALE\n"));
496 TRACE_EXIT;
497 return ESTALE;
498 }
67863d04 499 error = VFS_FHTOVP(mp, NULL, &fh->fh_fid, vpp);
0ba6fbbf
MD
500 if (error) {
501 PRINTF(("failed with: %d\n", error));
502 TRACE_ERR;
503 TRACE_EXIT;
504 return error;
505 }
506 TRACE_EXIT;
507 return 0;
508}
509
510static int
511mmap_vp(struct vn_hdr *vnh)
512{
513 struct vnode *vp;
514 Elf_Phdr *phdr;
515 struct file *fp;
516 int error;
517 TRACE_ENTER;
518
519 phdr = &vnh->vnh_phdr;
520
521 if ((error = ckpt_fhtovp(&vnh->vnh_fh, &vp)) != 0)
522 return error;
523 /*
524 * XXX O_RDONLY -> or O_RDWR if file is PROT_WRITE, MAP_SHARED
525 */
526 if ((error = fp_vpopen(vp, O_RDONLY, &fp)) != 0) {
527 vput(vp);
528 return error;
529 }
530 error = mmap_phdr(fp, phdr);
531 fp_close(fp);
532 TRACE_EXIT;
533 return error;
534}
535
536
537static int
538elf_gettextvp(struct proc *p, struct file *fp)
539{
540 int i;
541 int error;
542 int vpcount;
543 struct ckpt_vminfo vminfo;
544 struct vn_hdr *vnh = NULL;
545
546 TRACE_ENTER;
547 if ((error = read_check(fp, &vminfo, sizeof(vminfo))) != 0)
548 goto done;
549 if (vminfo.cvm_dsize < 0 ||
550 vminfo.cvm_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur ||
551 vminfo.cvm_tsize < 0 ||
552 (u_quad_t)vminfo.cvm_tsize > maxtsiz ||
88181b08
MD
553 vminfo.cvm_daddr >= (caddr_t)VM_MAX_USER_ADDRESS ||
554 vminfo.cvm_taddr >= (caddr_t)VM_MAX_USER_ADDRESS
0ba6fbbf
MD
555 ) {
556 error = ERANGE;
557 goto done;
558 }
559
560 vmspace_exec(p, NULL);
561 p->p_vmspace->vm_daddr = vminfo.cvm_daddr;
562 p->p_vmspace->vm_dsize = vminfo.cvm_dsize;
563 p->p_vmspace->vm_taddr = vminfo.cvm_taddr;
564 p->p_vmspace->vm_tsize = vminfo.cvm_tsize;
565 if ((error = read_check(fp, &vpcount, sizeof(int))) != 0)
566 goto done;
efda3bd0 567 vnh = kmalloc(sizeof(struct vn_hdr) * vpcount, M_TEMP, M_WAITOK);
0ba6fbbf
MD
568 if ((error = read_check(fp, vnh, sizeof(struct vn_hdr)*vpcount)) != 0)
569 goto done;
570 for (i = 0; i < vpcount; i++) {
571 if ((error = mmap_vp(&vnh[i])) != 0)
572 goto done;
573 }
574
575 done:
576 if (vnh)
efda3bd0 577 kfree(vnh, M_TEMP);
0ba6fbbf
MD
578 TRACE_EXIT;
579 return error;
580}
581
582
583
584/* place holder */
585static int
f3a2d8c4 586elf_getfiles(struct lwp *lp, struct file *fp)
0ba6fbbf
MD
587{
588 int error;
589 int i;
590 int filecount;
591 int fd;
592 struct ckpt_filehdr filehdr;
593 struct ckpt_fileinfo *cfi_base = NULL;
f3a2d8c4 594 struct filedesc *fdp = lp->lwp_proc->p_fd;
0ba6fbbf
MD
595 struct vnode *vp;
596 struct file *tempfp;
597 struct file *ofp;
598
599 TRACE_ENTER;
600 if ((error = read_check(fp, &filehdr, sizeof(filehdr))) != 0)
601 goto done;
602 filecount = filehdr.cfh_nfiles;
efda3bd0 603 cfi_base = kmalloc(filecount*sizeof(struct ckpt_fileinfo), M_TEMP, M_WAITOK);
0ba6fbbf
MD
604 error = read_check(fp, cfi_base, filecount*sizeof(struct ckpt_fileinfo));
605 if (error)
606 goto done;
607
608 /*
4f12bfd3 609 * Close all file descriptors >= 3. These descriptors are from the
0ba6fbbf 610 * checkpt(1) program itself and should not be retained.
4f12bfd3
MD
611 *
612 * XXX we need a flag so a checkpoint restore can opt to supply the
613 * descriptors, or the non-regular-file descripors.
0ba6fbbf 614 */
f3a2d8c4 615 for (i = 3; i < fdp->fd_nfiles; ++i)
0ba6fbbf
MD
616 kern_close(i);
617
618 /*
619 * Scan files to load
620 */
621 for (i = 0; i < filecount; i++) {
622 struct ckpt_fileinfo *cfi= &cfi_base[i];
623 /*
624 * Ignore placeholder entries where cfi_index is less then
625 * zero. This will occur if the elf core dump code thinks
626 * it can save a vnode but winds up not being able to.
627 */
628 if (cfi->cfi_index < 0)
629 continue;
630
ff7a3478
MD
631 /*
632 * Restore a saved file descriptor. If CKFIF_ISCKPTFD is
633 * set the descriptor represents the checkpoint file itself,
634 * probably due to the user calling sys_checkpoint(). We
635 * want to use the fp being used to restore the checkpoint
636 * instead of trying to restore the original filehandle.
637 */
638 if (cfi->cfi_ckflags & CKFIF_ISCKPTFD) {
639 fhold(fp);
640 tempfp = fp;
641 error = 0;
642 } else {
643 error = ckpt_fhtovp(&cfi->cfi_fh, &vp);
644 if (error == 0) {
645 error = fp_vpopen(vp, OFLAGS(cfi->cfi_flags),
646 &tempfp);
647 if (error)
648 vput(vp);
649 }
0ba6fbbf 650 }
ff7a3478
MD
651 if (error)
652 break;
0ba6fbbf
MD
653 tempfp->f_offset = cfi->cfi_offset;
654
655 /*
656 * If overwriting a descriptor close the old descriptor. This
657 * only occurs if the saved core saved descriptors that we
658 * have not already closed.
659 */
f3a2d8c4
MD
660 if (cfi->cfi_index < fdp->fd_nfiles &&
661 (ofp = fdp->fd_files[cfi->cfi_index].fp) != NULL) {
0ba6fbbf
MD
662 kern_close(cfi->cfi_index);
663 }
664
665 /*
666 * Allocate the descriptor we want.
667 */
f3a2d8c4 668 if (fdalloc(lp->lwp_proc, cfi->cfi_index, &fd) != 0) {
0ba6fbbf
MD
669 PRINTF(("can't currently restore fd: %d\n",
670 cfi->cfi_index));
671 fp_close(fp);
672 goto done;
673 }
674 KKASSERT(fd == cfi->cfi_index);
f3a2d8c4 675 fsetfd(fdp, tempfp, fd);
259b8ea0 676 fdrop(tempfp);
0ba6fbbf
MD
677 cfi++;
678 PRINTF(("restoring %d\n", cfi->cfi_index));
679 }
680
681 done:
682 if (cfi_base)
efda3bd0 683 kfree(cfi_base, M_TEMP);
0ba6fbbf
MD
684 TRACE_EXIT;
685 return error;
686}
687
688static int
7d20a8ff 689ckpt_freeze_proc(struct lwp *lp, struct file *fp)
0ba6fbbf 690{
7d20a8ff 691 struct proc *p = lp->lwp_proc;
0ba6fbbf
MD
692 rlim_t limit;
693 int error;
694
5686ec5a 695 lwkt_gettoken(&p->p_token); /* needed for proc_*() calls */
98c2b8ac 696
0ba6fbbf
MD
697 PRINTF(("calling generic_elf_coredump\n"));
698 limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
699 if (limit) {
6acb4ed8
SS
700 proc_stop(p);
701 while (p->p_nstopped < p->p_nthreads - 1)
702 tsleep(&p->p_nstopped, 0, "freeze", 1);
7d20a8ff 703 error = generic_elf_coredump(lp, SIGCKPT, fp, limit);
6acb4ed8 704 proc_unstop(p);
0ba6fbbf
MD
705 } else {
706 error = ERANGE;
707 }
5686ec5a 708 lwkt_reltoken(&p->p_token);
0ba6fbbf
MD
709 return error;
710}
711
3919ced0
MD
712/*
713 * MPALMOSTSAFE
714 */
0ba6fbbf 715int
753fd850 716sys_sys_checkpoint(struct sys_checkpoint_args *uap)
0ba6fbbf
MD
717{
718 int error = 0;
9910d07b
MD
719 struct thread *td = curthread;
720 struct proc *p = td->td_proc;
f3a2d8c4 721 struct filedesc *fdp = p->p_fd;
0ba6fbbf
MD
722 struct file *fp;
723
724 /*
725 * Only certain groups (to reduce our security exposure). -1
726 * allows any group.
727 */
9910d07b 728 if (ckptgroup >= 0 && groupmember(ckptgroup, td->td_ucred) == 0)
0ba6fbbf
MD
729 return (EPERM);
730
731 /*
732 * For now we can only checkpoint the current process
733 */
734 if (uap->pid != -1 && uap->pid != p->p_pid)
735 return (EINVAL);
736
3919ced0
MD
737 get_mplock();
738
0ba6fbbf
MD
739 switch (uap->type) {
740 case CKPT_FREEZE:
741 fp = NULL;
742 if (uap->fd == -1 && uap->pid == (pid_t)-1)
9910d07b 743 error = checkpoint_signal_handler(td->td_lwp);
f3a2d8c4 744 else if ((fp = holdfp(fdp, uap->fd, FWRITE)) == NULL)
0ba6fbbf
MD
745 error = EBADF;
746 else
9910d07b 747 error = ckpt_freeze_proc(td->td_lwp, fp);
0ba6fbbf 748 if (fp)
9f87144f 749 fdrop(fp);
0ba6fbbf
MD
750 break;
751 case CKPT_THAW:
3919ced0
MD
752 if (uap->pid != -1) {
753 error = EINVAL;
754 break;
755 }
f3a2d8c4 756 if ((fp = holdfp(fdp, uap->fd, FREAD)) == NULL) {
3919ced0
MD
757 error = EBADF;
758 break;
759 }
0ba6fbbf 760 uap->sysmsg_result = uap->retval;
9910d07b 761 error = ckpt_thaw_proc(td->td_lwp, fp);
9f87144f 762 fdrop(fp);
0ba6fbbf
MD
763 break;
764 default:
765 error = EOPNOTSUPP;
766 break;
767 }
3919ced0 768 rel_mplock();
0ba6fbbf
MD
769 return error;
770}
771
772int
7d20a8ff 773checkpoint_signal_handler(struct lwp *lp)
0ba6fbbf 774{
9910d07b 775 struct thread *td = lp->lwp_thread;
7d20a8ff 776 struct proc *p = lp->lwp_proc;
0ba6fbbf
MD
777 char *buf;
778 struct file *fp;
779 struct nlookupdata nd;
780 int error;
781
782 chptinuse++;
783
784 /*
785 * Being able to checkpoint an suid or sgid program is not a good
786 * idea.
787 */
788 if (sugid_coredump == 0 && (p->p_flag & P_SUGID)) {
789 chptinuse--;
790 return (EPERM);
791 }
792
9910d07b 793 buf = ckpt_expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
0ba6fbbf
MD
794 if (buf == NULL) {
795 chptinuse--;
796 return (ENOMEM);
797 }
798
799 log(LOG_INFO, "pid %d (%s), uid %d: checkpointing to %s\n",
800 p->p_pid, p->p_comm,
9910d07b 801 (td->td_ucred ? td->td_ucred->cr_uid : -1),
0ba6fbbf
MD
802 buf);
803
804 PRINTF(("ckpt handler called, using '%s'\n", buf));
805
806 /*
807 * Use the same safety flags that the coredump code uses. Remove
808 * any previous checkpoint file before writing out the new one in
809 * case we are re-checkpointing a program that had been checkpt
810 * restored. Otherwise we will corrupt the program space (which is
811 * made up of mmap()ings of the previous checkpoint file) while we
812 * write out the new one.
813 */
814 error = nlookup_init(&nd, buf, UIO_SYSSPACE, 0);
815 if (error == 0)
816 error = kern_unlink(&nd);
817 nlookup_done(&nd);
818 error = fp_open(buf, O_WRONLY|O_CREAT|O_TRUNC|O_NOFOLLOW, 0600, &fp);
819 if (error == 0) {
7d20a8ff 820 error = ckpt_freeze_proc(lp, fp);
0ba6fbbf
MD
821 fp_close(fp);
822 } else {
6ea70f76 823 kprintf("checkpoint failed with open - error: %d\n", error);
0ba6fbbf 824 }
efda3bd0 825 kfree(buf, M_TEMP);
0ba6fbbf
MD
826 chptinuse--;
827 return (error);
828}
829
830static char ckptfilename[MAXPATHLEN] = {"%N.ckpt"};
831SYSCTL_STRING(_kern, OID_AUTO, ckptfile, CTLFLAG_RW, ckptfilename,
832 sizeof(ckptfilename), "process checkpoint name format string");
833
834/*
835 * expand_name(name, uid, pid)
836 * Expand the name described in corefilename, using name, uid, and pid.
6ea70f76 837 * corefilename is a kprintf-like string, with three format specifiers:
0ba6fbbf
MD
838 * %N name of process ("name")
839 * %P process id (pid)
840 * %U user id (uid)
841 * For example, "%N.core" is the default; they can be disabled completely
842 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
843 * This is controlled by the sysctl variable kern.corefile (see above).
844 *
845 * -- taken from the coredump code
846 */
847
848static
849char *
850ckpt_expand_name(const char *name, uid_t uid, pid_t pid)
851{
852 char *temp;
853 char *bp;
854 char buf[11]; /* Buffer for pid/uid -- max 4B */
855 int error;
856 int i;
857 int n;
858 char *format = ckptfilename;
859 size_t namelen;
860
efda3bd0 861 temp = kmalloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
0ba6fbbf
MD
862 if (temp == NULL)
863 return NULL;
864 namelen = strlen(name);
865 n = 0;
866 if (ckptfilename[0] != '/') {
867 if ((bp = kern_getcwd(temp, MAXPATHLEN - 1, &error)) == NULL) {
efda3bd0 868 kfree(temp, M_TEMP);
0ba6fbbf
MD
869 return NULL;
870 }
871 n = strlen(bp);
872 bcopy(bp, temp, n + 1); /* normalize location of the path */
873 temp[n++] = '/';
874 temp[n] = '\0';
875 }
876 for (i= 0; n < MAXPATHLEN && format[i]; i++) {
877 int l;
878 switch (format[i]) {
879 case '%': /* Format character */
880 i++;
881 switch (format[i]) {
882 case '%':
883 temp[n++] = '%';
884 break;
885 case 'N': /* process name */
886 if ((n + namelen) > MAXPATHLEN) {
887 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
888 pid, name, uid, temp, name);
efda3bd0 889 kfree(temp, M_TEMP);
0ba6fbbf
MD
890 return NULL;
891 }
892 memcpy(temp+n, name, namelen);
893 n += namelen;
894 break;
895 case 'P': /* process id */
f8c7a42d 896 l = ksprintf(buf, "%u", pid);
0ba6fbbf
MD
897 if ((n + l) > MAXPATHLEN) {
898 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
899 pid, name, uid, temp, name);
efda3bd0 900 kfree(temp, M_TEMP);
0ba6fbbf
MD
901 return NULL;
902 }
903 memcpy(temp+n, buf, l);
904 n += l;
905 break;
906 case 'U': /* user id */
f8c7a42d 907 l = ksprintf(buf, "%u", uid);
0ba6fbbf
MD
908 if ((n + l) > MAXPATHLEN) {
909 log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n",
910 pid, name, uid, temp, name);
efda3bd0 911 kfree(temp, M_TEMP);
0ba6fbbf
MD
912 return NULL;
913 }
914 memcpy(temp+n, buf, l);
915 n += l;
916 break;
917 default:
918 log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
919 }
920 break;
921 default:
922 temp[n++] = format[i];
923 }
924 }
925 temp[n] = '\0';
926 return temp;
927}
928