| Commit | Line | Data |
|---|---|---|
| 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> | |
| 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> | |
| f8334305 | 63 | #include <machine/frame.h> |
| 0ba6fbbf MD |
64 | #include <sys/signalvar.h> |
| 65 | #include <sys/syslog.h> | |
| 66 | #include <sys/sysctl.h> | |
| f8334305 | 67 | #include <machine/sigframe.h> |
| 0ba6fbbf MD |
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 | ||
| 0ba6fbbf | 76 | static int elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs); |
| 7d20a8ff | 77 | static int elf_getnotes(struct lwp *lp, struct file *fp, size_t notesz); |
| 0ba6fbbf MD |
78 | static int elf_demarshalnotes(void *src, prpsinfo_t *psinfo, |
| 79 | prstatus_t *status, prfpregset_t *fpregset, int nthreads); | |
| 7d20a8ff | 80 | static int elf_loadnotes(struct lwp *, prpsinfo_t *, prstatus_t *, |
| 0ba6fbbf | 81 | prfpregset_t *); |
| 7d20a8ff | 82 | static int elf_getsigs(struct lwp *lp, struct file *fp); |
| 0ba6fbbf MD |
83 | static int elf_getfiles(struct proc *p, struct file *fp); |
| 84 | static int elf_gettextvp(struct proc *p, struct file *fp); | |
| 85 | static char *ckpt_expand_name(const char *name, uid_t uid, pid_t pid); | |
| 86 | ||
| 87 | static int ckptgroup = 0; /* wheel only, -1 for any group */ | |
| 88 | SYSCTL_INT(_kern, OID_AUTO, ckptgroup, CTLFLAG_RW, &ckptgroup, 0, ""); | |
| 89 | ||
| 90 | /* ref count to see how many processes that are being checkpointed */ | |
| 91 | static int chptinuse = 0; | |
| 92 | ||
| 93 | static __inline | |
| 94 | int | |
| 95 | read_check(struct file *fp, void *buf, size_t nbyte) | |
| 96 | { | |
| 97 | size_t nread; | |
| 98 | int error; | |
| 99 | ||
| 374a52ac | 100 | PRINTF(("reading %d bytes\n", nbyte)); |
| e7440b28 | 101 | error = fp_read(fp, buf, nbyte, &nread, 1, UIO_SYSSPACE); |
| 0ba6fbbf MD |
102 | if (error) { |
| 103 | PRINTF(("read failed - %d", error)); | |
| 104 | } else if (nread != nbyte) { | |
| 105 | PRINTF(("wanted to read %d - read %d\n", nbyte, nread)); | |
| 106 | error = EINVAL; | |
| 107 | } | |
| 108 | return error; | |
| 109 | } | |
| 110 | ||
| 111 | static int | |
| 112 | elf_gethdr(struct file *fp, Elf_Ehdr *ehdr) | |
| 113 | { | |
| 114 | size_t nbyte = sizeof(Elf_Ehdr); | |
| 115 | int error; | |
| 116 | ||
| 117 | if ((error = read_check(fp, ehdr, nbyte)) != 0) | |
| 118 | goto done; | |
| 119 | if (!(ehdr->e_ehsize == sizeof(Elf_Ehdr))) { | |
| 120 | PRINTF(("wrong elf header size: %d\n" | |
| 121 | "expected size : %d\n", | |
| 122 | ehdr->e_ehsize, sizeof(Elf_Ehdr))); | |
| 123 | return EINVAL; | |
| 124 | } | |
| 125 | if (!(ehdr->e_phentsize == sizeof(Elf_Phdr))) { | |
| 126 | PRINTF(("wrong program header size: %d\n" | |
| 127 | "expected size : %d\n", | |
| 128 | ehdr->e_phentsize, sizeof(Elf_Phdr))); | |
| 129 | return EINVAL; | |
| 130 | } | |
| 131 | ||
| 132 | if (!(ehdr->e_ident[EI_MAG0] == ELFMAG0 && | |
| 133 | ehdr->e_ident[EI_MAG1] == ELFMAG1 && | |
| 134 | ehdr->e_ident[EI_MAG2] == ELFMAG2 && | |
| 135 | ehdr->e_ident[EI_MAG3] == ELFMAG3 && | |
| 136 | ehdr->e_ident[EI_CLASS] == ELF_CLASS && | |
| 137 | ehdr->e_ident[EI_DATA] == ELF_DATA && | |
| 138 | ehdr->e_ident[EI_VERSION] == EV_CURRENT && | |
| 139 | ehdr->e_ident[EI_OSABI] == ELFOSABI_FREEBSD && | |
| 140 | ehdr->e_ident[EI_ABIVERSION] == 0)) { | |
| 141 | PRINTF(("bad elf header\n there are %d segments\n", | |
| 142 | ehdr->e_phnum)); | |
| 143 | return EINVAL; | |
| 144 | ||
| 145 | } | |
| 146 | PRINTF(("Elf header size: %d\n", ehdr->e_ehsize)); | |
| 147 | PRINTF(("Program header size: %d\n", ehdr->e_phentsize)); | |
| 148 | PRINTF(("Number of Program headers: %d\n", ehdr->e_phnum)); | |
| 149 | done: | |
| 150 | return error; | |
| 151 | } | |
| 152 | ||
| 153 | static int | |
| 154 | elf_getphdrs(struct file *fp, Elf_Phdr *phdr, size_t nbyte) | |
| 155 | { | |
| 156 | int i; | |
| 157 | int error; | |
| 158 | int nheaders = nbyte/sizeof(Elf_Phdr); | |
| 159 | ||
| 160 | PRINTF(("reading phdrs section\n")); | |
| 161 | if ((error = read_check(fp, phdr, nbyte)) != 0) | |
| 162 | goto done; | |
| ff7a3478 | 163 | PRINTF(("headers section:\n")); |
| 0ba6fbbf | 164 | for (i = 0; i < nheaders; i++) { |
| ff7a3478 MD |
165 | PRINTF(("entry type: %d\n", phdr[i].p_type)); |
| 166 | PRINTF(("file offset: %d\n", phdr[i].p_offset)); | |
| 167 | PRINTF(("virt address: %p\n", (uint32_t *)phdr[i].p_vaddr)); | |
| 168 | PRINTF(("file size: %d\n", phdr[i].p_filesz)); | |
| 169 | PRINTF(("memory size: %d\n", phdr[i].p_memsz)); | |
| 170 | PRINTF(("\n")); | |
| 0ba6fbbf MD |
171 | } |
| 172 | done: | |
| 173 | return error; | |
| 174 | } | |
| 175 | ||
| 176 | ||
| 177 | static int | |
| 7d20a8ff | 178 | elf_getnotes(struct lwp *lp, struct file *fp, size_t notesz) |
| 0ba6fbbf MD |
179 | { |
| 180 | int error; | |
| 181 | int nthreads; | |
| 182 | char *note; | |
| 183 | prpsinfo_t *psinfo; | |
| 184 | prstatus_t *status; | |
| 185 | prfpregset_t *fpregset; | |
| 186 | ||
| 187 | nthreads = (notesz - sizeof(prpsinfo_t))/(sizeof(prstatus_t) + | |
| 188 | sizeof(prfpregset_t)); | |
| 189 | PRINTF(("reading notes header nthreads=%d\n", nthreads)); | |
| 190 | if (nthreads <= 0 || nthreads > CKPT_MAXTHREADS) | |
| 191 | return EINVAL; | |
| 192 | ||
| efda3bd0 MD |
193 | psinfo = kmalloc(sizeof(prpsinfo_t), M_TEMP, M_ZERO | M_WAITOK); |
| 194 | status = kmalloc(nthreads*sizeof(prstatus_t), M_TEMP, M_WAITOK); | |
| 195 | fpregset = kmalloc(nthreads*sizeof(prfpregset_t), M_TEMP, M_WAITOK); | |
| 196 | note = kmalloc(notesz, M_TEMP, M_WAITOK); | |
| 0ba6fbbf MD |
197 | |
| 198 | ||
| 199 | PRINTF(("reading notes section\n")); | |
| 200 | if ((error = read_check(fp, note, notesz)) != 0) | |
| 201 | goto done; | |
| 202 | error = elf_demarshalnotes(note, psinfo, status, fpregset, nthreads); | |
| 203 | if (error) | |
| 204 | goto done; | |
| 205 | /* fetch register state from notes */ | |
| 7d20a8ff | 206 | error = elf_loadnotes(lp, psinfo, status, fpregset); |
| 0ba6fbbf MD |
207 | done: |
| 208 | if (psinfo) | |
| efda3bd0 | 209 | kfree(psinfo, M_TEMP); |
| 0ba6fbbf | 210 | if (status) |
| efda3bd0 | 211 | kfree(status, M_TEMP); |
| 0ba6fbbf | 212 | if (fpregset) |
| efda3bd0 | 213 | kfree(fpregset, M_TEMP); |
| 0ba6fbbf | 214 | if (note) |
| efda3bd0 | 215 | kfree(note, M_TEMP); |
| 0ba6fbbf MD |
216 | return error; |
| 217 | } | |
| 218 | ||
| 219 | static int | |
| 7d20a8ff | 220 | ckpt_thaw_proc(struct lwp *lp, struct file *fp) |
| 0ba6fbbf | 221 | { |
| 7d20a8ff | 222 | struct proc *p = lp->lwp_proc; |
| 0ba6fbbf MD |
223 | Elf_Phdr *phdr = NULL; |
| 224 | Elf_Ehdr *ehdr = NULL; | |
| 225 | int error; | |
| 226 | size_t nbyte; | |
| 227 | ||
| 228 | TRACE_ENTER; | |
| 229 | ||
| efda3bd0 | 230 | ehdr = kmalloc(sizeof(Elf_Ehdr), M_TEMP, M_ZERO | M_WAITOK); |
| 0ba6fbbf MD |
231 | |
| 232 | if ((error = elf_gethdr(fp, ehdr)) != 0) | |
| 233 | goto done; | |
| 234 | nbyte = sizeof(Elf_Phdr) * ehdr->e_phnum; | |
| efda3bd0 | 235 | phdr = kmalloc(nbyte, M_TEMP, M_WAITOK); |
| 0ba6fbbf MD |
236 | |
| 237 | /* fetch description of program writable mappings */ | |
| 238 | if ((error = elf_getphdrs(fp, phdr, nbyte)) != 0) | |
| 239 | goto done; | |
| 240 | ||
| 241 | /* fetch notes section containing register state */ | |
| 7d20a8ff | 242 | if ((error = elf_getnotes(lp, fp, phdr->p_filesz)) != 0) |
| 0ba6fbbf MD |
243 | goto done; |
| 244 | ||
| 245 | /* fetch program text vnodes */ | |
| 246 | if ((error = elf_gettextvp(p, fp)) != 0) | |
| 247 | goto done; | |
| 248 | ||
| 249 | /* fetch signal disposition */ | |
| 7d20a8ff | 250 | if ((error = elf_getsigs(lp, fp)) != 0) { |
| 6ea70f76 | 251 | kprintf("failure in recovering signals\n"); |
| 0ba6fbbf | 252 | goto done; |
| 4f12bfd3 | 253 | } |
| 0ba6fbbf MD |
254 | |
| 255 | /* fetch open files */ | |
| 256 | if ((error = elf_getfiles(p, fp)) != 0) | |
| 257 | goto done; | |
| 258 | ||
| 259 | /* handle mappings last in case we are reading from a socket */ | |
| 260 | error = elf_loadphdrs(fp, phdr, ehdr->e_phnum); | |
| 261 | ||
| 262 | /* | |
| 263 | * Set the textvp to the checkpoint file and mark the vnode so | |
| 264 | * a future checkpointing of this checkpoint-restored program | |
| 265 | * will copy out the contents of the mappings rather then trying | |
| 266 | * to record the vnode info related to the checkpoint file, which | |
| 267 | * is likely going to be destroyed when the program is re-checkpointed. | |
| 268 | */ | |
| 269 | if (error == 0 && fp->f_data && fp->f_type == DTYPE_VNODE) { | |
| 270 | if (p->p_textvp) | |
| 271 | vrele(p->p_textvp); | |
| 272 | p->p_textvp = (struct vnode *)fp->f_data; | |
| 273 | p->p_textvp->v_flag |= VCKPT; | |
| 274 | vref(p->p_textvp); | |
| 275 | } | |
| 276 | done: | |
| 277 | if (ehdr) | |
| efda3bd0 | 278 | kfree(ehdr, M_TEMP); |
| 0ba6fbbf | 279 | if (phdr) |
| efda3bd0 | 280 | kfree(phdr, M_TEMP); |
| 0ba6fbbf MD |
281 | TRACE_EXIT; |
| 282 | return error; | |
| 283 | } | |
| 284 | ||
| 285 | static int | |
| 7d20a8ff | 286 | elf_loadnotes(struct lwp *lp, prpsinfo_t *psinfo, prstatus_t *status, |
| 0ba6fbbf MD |
287 | prfpregset_t *fpregset) |
| 288 | { | |
| 7d20a8ff | 289 | struct proc *p = lp->lwp_proc; |
| 0ba6fbbf MD |
290 | int error; |
| 291 | ||
| 292 | /* validate status and psinfo */ | |
| 293 | TRACE_ENTER; | |
| 294 | if (status->pr_version != PRSTATUS_VERSION || | |
| 295 | status->pr_statussz != sizeof(prstatus_t) || | |
| 296 | status->pr_gregsetsz != sizeof(gregset_t) || | |
| 297 | status->pr_fpregsetsz != sizeof(fpregset_t) || | |
| 298 | psinfo->pr_version != PRPSINFO_VERSION || | |
| 299 | psinfo->pr_psinfosz != sizeof(prpsinfo_t)) { | |
| 300 | PRINTF(("status check failed\n")); | |
| 301 | error = EINVAL; | |
| 302 | goto done; | |
| 303 | } | |
| 7d20a8ff | 304 | /* XXX lwp handle more than one lwp*/ |
| 08f2f1bb | 305 | if ((error = set_regs(lp, &status->pr_reg)) != 0) |
| 0ba6fbbf | 306 | goto done; |
| 08f2f1bb | 307 | error = set_fpregs(lp, fpregset); |
| 0ba6fbbf MD |
308 | strlcpy(p->p_comm, psinfo->pr_fname, sizeof(p->p_comm)); |
| 309 | /* XXX psinfo->pr_psargs not yet implemented */ | |
| 310 | done: | |
| 311 | TRACE_EXIT; | |
| 312 | return error; | |
| 313 | } | |
| 314 | ||
| 315 | static int | |
| 316 | elf_getnote(void *src, size_t *off, const char *name, unsigned int type, | |
| 317 | void **desc, size_t descsz) | |
| 318 | { | |
| 319 | Elf_Note note; | |
| 320 | int error; | |
| 321 | ||
| 322 | TRACE_ENTER; | |
| 323 | if (src == NULL) { | |
| 324 | error = EFAULT; | |
| 325 | goto done; | |
| 326 | } | |
| 327 | bcopy((char *)src + *off, ¬e, sizeof note); | |
| 328 | ||
| 329 | PRINTF(("at offset: %d expected note of type: %d - got: %d\n", | |
| 330 | *off, type, note.n_type)); | |
| 331 | *off += sizeof note; | |
| 332 | if (type != note.n_type) { | |
| 333 | TRACE_ERR; | |
| 334 | error = EINVAL; | |
| 335 | goto done; | |
| 336 | } | |
| 337 | if (strncmp(name, (char *) src + *off, note.n_namesz) != 0) { | |
| 338 | error = EINVAL; | |
| 339 | goto done; | |
| 340 | } | |
| 341 | *off += roundup2(note.n_namesz, sizeof(Elf_Size)); | |
| 342 | if (note.n_descsz != descsz) { | |
| 343 | TRACE_ERR; | |
| 344 | error = EINVAL; | |
| 345 | goto done; | |
| 346 | } | |
| 347 | if (desc) | |
| 348 | bcopy((char *)src + *off, *desc, note.n_descsz); | |
| 349 | *off += roundup2(note.n_descsz, sizeof(Elf_Size)); | |
| 350 | error = 0; | |
| 351 | done: | |
| 352 | TRACE_EXIT; | |
| 353 | return error; | |
| 354 | } | |
| 355 | ||
| 356 | static int | |
| 357 | elf_demarshalnotes(void *src, prpsinfo_t *psinfo, prstatus_t *status, | |
| 358 | prfpregset_t *fpregset, int nthreads) | |
| 359 | { | |
| 360 | int i; | |
| 361 | int error; | |
| 973c11b9 | 362 | size_t off = 0; |
| 0ba6fbbf MD |
363 | |
| 364 | TRACE_ENTER; | |
| 075a756c MD |
365 | error = elf_getnote(src, &off, "FreeBSD", NT_PRPSINFO, |
| 366 | (void **)&psinfo, sizeof(prpsinfo_t)); | |
| 367 | if (error) | |
| 368 | goto done; | |
| 0ba6fbbf MD |
369 | error = elf_getnote(src, &off, "FreeBSD", NT_PRSTATUS, |
| 370 | (void **)&status, sizeof(prstatus_t)); | |
| 371 | if (error) | |
| 372 | goto done; | |
| 373 | error = elf_getnote(src, &off, "FreeBSD", NT_FPREGSET, | |
| 374 | (void **)&fpregset, sizeof(prfpregset_t)); | |
| 375 | if (error) | |
| 376 | goto done; | |
| 0ba6fbbf MD |
377 | |
| 378 | /* | |
| 379 | * The remaining portion needs to be an integer multiple | |
| 380 | * of prstatus_t and prfpregset_t | |
| 381 | */ | |
| 382 | for (i = 0 ; i < nthreads - 1; i++) { | |
| 383 | status++; fpregset++; | |
| 384 | error = elf_getnote(src, &off, "FreeBSD", NT_PRSTATUS, | |
| 385 | (void **)&status, sizeof (prstatus_t)); | |
| 386 | if (error) | |
| 387 | goto done; | |
| 388 | error = elf_getnote(src, &off, "FreeBSD", NT_FPREGSET, | |
| 389 | (void **)&fpregset, sizeof(prfpregset_t)); | |
| 390 | if (error) | |
| 391 | goto done; | |
| 392 | } | |
| 393 | ||
| 394 | done: | |
| 395 | TRACE_EXIT; | |
| 396 | return error; | |
| 397 | } | |
| 398 | ||
| 399 | ||
| 400 | static int | |
| 401 | mmap_phdr(struct file *fp, Elf_Phdr *phdr) | |
| 402 | { | |
| 403 | int error; | |
| 404 | size_t len; | |
| 405 | int prot; | |
| 406 | void *addr; | |
| 407 | int flags; | |
| 408 | off_t pos; | |
| 409 | ||
| 410 | TRACE_ENTER; | |
| 411 | pos = phdr->p_offset; | |
| 412 | len = phdr->p_filesz; | |
| 413 | addr = (void *)phdr->p_vaddr; | |
| 414 | flags = MAP_FIXED | MAP_NOSYNC | MAP_PRIVATE; | |
| 415 | prot = 0; | |
| 416 | if (phdr->p_flags & PF_R) | |
| 417 | prot |= PROT_READ; | |
| 418 | if (phdr->p_flags & PF_W) | |
| 419 | prot |= PROT_WRITE; | |
| 420 | if (phdr->p_flags & PF_X) | |
| 421 | prot |= PROT_EXEC; | |
| 422 | if ((error = fp_mmap(addr, len, prot, flags, fp, pos, &addr)) != 0) { | |
| 423 | PRINTF(("mmap failed: %d\n", error); ); | |
| 424 | } | |
| 374a52ac HP |
425 | PRINTF(("map @%08x-%08x fileoff %08x-%08x\n", (int)addr, |
| 426 | (int)((char *)addr + len), (int)pos, (int)(pos + len))); | |
| 0ba6fbbf MD |
427 | TRACE_EXIT; |
| 428 | return error; | |
| 429 | } | |
| 430 | ||
| ff7a3478 MD |
431 | /* |
| 432 | * Load memory mapped segments. The segments are backed by the checkpoint | |
| 433 | * file. | |
| 434 | */ | |
| 0ba6fbbf MD |
435 | static int |
| 436 | elf_loadphdrs(struct file *fp, Elf_Phdr *phdr, int numsegs) | |
| 437 | { | |
| 438 | int i; | |
| 439 | int error = 0; | |
| 440 | ||
| 441 | TRACE_ENTER; | |
| 442 | for (i = 1; i < numsegs; i++) { | |
| 443 | if ((error = mmap_phdr(fp, &phdr[i])) != 0) | |
| 444 | break; | |
| 445 | } | |
| 446 | TRACE_EXIT; | |
| 447 | return error; | |
| 448 | } | |
| 449 | ||
| 450 | static int | |
| 7d20a8ff | 451 | elf_getsigs(struct lwp *lp, struct file *fp) |
| 0ba6fbbf | 452 | { |
| 7d20a8ff | 453 | struct proc *p = lp->lwp_proc; |
| 0ba6fbbf MD |
454 | int error; |
| 455 | struct ckpt_siginfo *csi; | |
| 0ba6fbbf MD |
456 | |
| 457 | TRACE_ENTER; | |
| efda3bd0 | 458 | csi = kmalloc(sizeof(struct ckpt_siginfo), M_TEMP, M_ZERO | M_WAITOK); |
| 0ba6fbbf MD |
459 | if ((error = read_check(fp, csi, sizeof(struct ckpt_siginfo))) != 0) |
| 460 | goto done; | |
| 461 | ||
| 462 | if (csi->csi_ckptpisz != sizeof(struct ckpt_siginfo)) { | |
| 463 | TRACE_ERR; | |
| 464 | error = EINVAL; | |
| 465 | goto done; | |
| 466 | } | |
| b1b4e5a6 | 467 | bcopy(&csi->csi_sigacts, p->p_sigacts, sizeof(p->p_sigacts)); |
| 0ba6fbbf | 468 | bcopy(&csi->csi_itimerval, &p->p_realtimer, sizeof(struct itimerval)); |
| 4f12bfd3 | 469 | SIG_CANTMASK(csi->csi_sigmask); |
| 7d20a8ff | 470 | /* XXX lwp handle more than one lwp */ |
| 08f2f1bb | 471 | bcopy(&csi->csi_sigmask, &lp->lwp_sigmask, sizeof(sigset_t)); |
| 0ba6fbbf MD |
472 | p->p_sigparent = csi->csi_sigparent; |
| 473 | done: | |
| 474 | if (csi) | |
| efda3bd0 | 475 | kfree(csi, M_TEMP); |
| 0ba6fbbf MD |
476 | TRACE_EXIT; |
| 477 | return error; | |
| 478 | } | |
| 479 | ||
| 480 | /* | |
| 481 | * Returns a locked, refd vnode | |
| 482 | */ | |
| 483 | static int | |
| 484 | ckpt_fhtovp(fhandle_t *fh, struct vnode **vpp) | |
| 485 | { | |
| 486 | struct mount *mp; | |
| 487 | int error; | |
| 488 | ||
| 489 | TRACE_ENTER; | |
| 490 | mp = vfs_getvfs(&fh->fh_fsid); | |
| 491 | ||
| 492 | if (!mp) { | |
| 493 | TRACE_ERR; | |
| 494 | PRINTF(("failed to get mount - ESTALE\n")); | |
| 495 | TRACE_EXIT; | |
| 496 | return ESTALE; | |
| 497 | } | |
| 67863d04 | 498 | error = VFS_FHTOVP(mp, NULL, &fh->fh_fid, vpp); |
| 0ba6fbbf MD |
499 | if (error) { |
| 500 | PRINTF(("failed with: %d\n", error)); | |
| 501 | TRACE_ERR; | |
| 502 | TRACE_EXIT; | |
| 503 | return error; | |
| 504 | } | |
| 505 | TRACE_EXIT; | |
| 506 | return 0; | |
| 507 | } | |
| 508 | ||
| 509 | static int | |
| 510 | mmap_vp(struct vn_hdr *vnh) | |
| 511 | { | |
| 512 | struct vnode *vp; | |
| 513 | Elf_Phdr *phdr; | |
| 514 | struct file *fp; | |
| 515 | int error; | |
| 516 | TRACE_ENTER; | |
| 517 | ||
| 518 | phdr = &vnh->vnh_phdr; | |
| 519 | ||
| 520 | if ((error = ckpt_fhtovp(&vnh->vnh_fh, &vp)) != 0) | |
| 521 | return error; | |
| 522 | /* | |
| 523 | * XXX O_RDONLY -> or O_RDWR if file is PROT_WRITE, MAP_SHARED | |
| 524 | */ | |
| 525 | if ((error = fp_vpopen(vp, O_RDONLY, &fp)) != 0) { | |
| 526 | vput(vp); | |
| 527 | return error; | |
| 528 | } | |
| 529 | error = mmap_phdr(fp, phdr); | |
| 530 | fp_close(fp); | |
| 531 | TRACE_EXIT; | |
| 532 | return error; | |
| 533 | } | |
| 534 | ||
| 535 | ||
| 536 | static int | |
| 537 | elf_gettextvp(struct proc *p, struct file *fp) | |
| 538 | { | |
| 539 | int i; | |
| 540 | int error; | |
| 541 | int vpcount; | |
| 542 | struct ckpt_vminfo vminfo; | |
| 543 | struct vn_hdr *vnh = NULL; | |
| 544 | ||
| 545 | TRACE_ENTER; | |
| 546 | if ((error = read_check(fp, &vminfo, sizeof(vminfo))) != 0) | |
| 547 | goto done; | |
| 548 | if (vminfo.cvm_dsize < 0 || | |
| 549 | vminfo.cvm_dsize > p->p_rlimit[RLIMIT_DATA].rlim_cur || | |
| 550 | vminfo.cvm_tsize < 0 || | |
| 551 | (u_quad_t)vminfo.cvm_tsize > maxtsiz || | |
| 88181b08 MD |
552 | vminfo.cvm_daddr >= (caddr_t)VM_MAX_USER_ADDRESS || |
| 553 | vminfo.cvm_taddr >= (caddr_t)VM_MAX_USER_ADDRESS | |
| 0ba6fbbf MD |
554 | ) { |
| 555 | error = ERANGE; | |
| 556 | goto done; | |
| 557 | } | |
| 558 | ||
| 559 | vmspace_exec(p, NULL); | |
| 560 | p->p_vmspace->vm_daddr = vminfo.cvm_daddr; | |
| 561 | p->p_vmspace->vm_dsize = vminfo.cvm_dsize; | |
| 562 | p->p_vmspace->vm_taddr = vminfo.cvm_taddr; | |
| 563 | p->p_vmspace->vm_tsize = vminfo.cvm_tsize; | |
| 564 | if ((error = read_check(fp, &vpcount, sizeof(int))) != 0) | |
| 565 | goto done; | |
| efda3bd0 | 566 | vnh = kmalloc(sizeof(struct vn_hdr) * vpcount, M_TEMP, M_WAITOK); |
| 0ba6fbbf MD |
567 | if ((error = read_check(fp, vnh, sizeof(struct vn_hdr)*vpcount)) != 0) |
| 568 | goto done; | |
| 569 | for (i = 0; i < vpcount; i++) { | |
| 570 | if ((error = mmap_vp(&vnh[i])) != 0) | |
| 571 | goto done; | |
| 572 | } | |
| 573 | ||
| 574 | done: | |
| 575 | if (vnh) | |
| efda3bd0 | 576 | kfree(vnh, M_TEMP); |
| 0ba6fbbf MD |
577 | TRACE_EXIT; |
| 578 | return error; | |
| 579 | } | |
| 580 | ||
| 581 | ||
| 582 | ||
| 583 | /* place holder */ | |
| 584 | static int | |
| 585 | elf_getfiles(struct proc *p, struct file *fp) | |
| 586 | { | |
| 587 | int error; | |
| 588 | int i; | |
| 589 | int filecount; | |
| 590 | int fd; | |
| 591 | struct ckpt_filehdr filehdr; | |
| 592 | struct ckpt_fileinfo *cfi_base = NULL; | |
| 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 MD |
612 | */ |
| 613 | for (i = 3; i < p->p_fd->fd_nfiles; ++i) | |
| 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 | */ | |
| 658 | if (cfi->cfi_index < p->p_fd->fd_nfiles && | |
| 0679adc4 | 659 | (ofp = p->p_fd->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 | */ | |
| 666 | if (fdalloc(p, cfi->cfi_index, &fd) != 0) { | |
| 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); | |
| 259b8ea0 MD |
673 | fsetfd(p, tempfp, fd); |
| 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 | ||
| 686 | static int | |
| 7d20a8ff | 687 | ckpt_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 | ||
| 693 | PRINTF(("calling generic_elf_coredump\n")); | |
| 694 | limit = p->p_rlimit[RLIMIT_CORE].rlim_cur; | |
| 695 | if (limit) { | |
| 6acb4ed8 SS |
696 | proc_stop(p); |
| 697 | while (p->p_nstopped < p->p_nthreads - 1) | |
| 698 | tsleep(&p->p_nstopped, 0, "freeze", 1); | |
| 7d20a8ff | 699 | error = generic_elf_coredump(lp, SIGCKPT, fp, limit); |
| 6acb4ed8 | 700 | proc_unstop(p); |
| 0ba6fbbf MD |
701 | } else { |
| 702 | error = ERANGE; | |
| 703 | } | |
| 704 | return error; | |
| 705 | } | |
| 706 | ||
| 707 | int | |
| 753fd850 | 708 | sys_sys_checkpoint(struct sys_checkpoint_args *uap) |
| 0ba6fbbf MD |
709 | { |
| 710 | int error = 0; | |
| 7d20a8ff | 711 | struct lwp *lp = curthread->td_lwp; |
| 0ba6fbbf MD |
712 | struct proc *p = curthread->td_proc; |
| 713 | struct file *fp; | |
| 714 | ||
| 715 | /* | |
| 716 | * Only certain groups (to reduce our security exposure). -1 | |
| 717 | * allows any group. | |
| 718 | */ | |
| 719 | if (ckptgroup >= 0 && groupmember(ckptgroup, p->p_ucred) == 0) | |
| 720 | return (EPERM); | |
| 721 | ||
| 722 | /* | |
| 723 | * For now we can only checkpoint the current process | |
| 724 | */ | |
| 725 | if (uap->pid != -1 && uap->pid != p->p_pid) | |
| 726 | return (EINVAL); | |
| 727 | ||
| 728 | switch (uap->type) { | |
| 729 | case CKPT_FREEZE: | |
| 730 | fp = NULL; | |
| 731 | if (uap->fd == -1 && uap->pid == (pid_t)-1) | |
| 7d20a8ff | 732 | error = checkpoint_signal_handler(lp); |
| 0ba6fbbf MD |
733 | else if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL) |
| 734 | error = EBADF; | |
| 735 | else | |
| 7d20a8ff | 736 | error = ckpt_freeze_proc(lp, fp); |
| 0ba6fbbf | 737 | if (fp) |
| 9f87144f | 738 | fdrop(fp); |
| 0ba6fbbf MD |
739 | break; |
| 740 | case CKPT_THAW: | |
| 741 | if (uap->pid != -1) | |
| 742 | return EINVAL; | |
| 743 | if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL) | |
| 744 | return EBADF; | |
| 745 | uap->sysmsg_result = uap->retval; | |
| 7d20a8ff | 746 | error = ckpt_thaw_proc(lp, fp); |
| 9f87144f | 747 | fdrop(fp); |
| 0ba6fbbf MD |
748 | break; |
| 749 | default: | |
| 750 | error = EOPNOTSUPP; | |
| 751 | break; | |
| 752 | } | |
| 753 | return error; | |
| 754 | } | |
| 755 | ||
| 756 | int | |
| 7d20a8ff | 757 | checkpoint_signal_handler(struct lwp *lp) |
| 0ba6fbbf | 758 | { |
| 7d20a8ff | 759 | struct proc *p = lp->lwp_proc; |
| 0ba6fbbf MD |
760 | char *buf; |
| 761 | struct file *fp; | |
| 762 | struct nlookupdata nd; | |
| 763 | int error; | |
| 764 | ||
| 765 | chptinuse++; | |
| 766 | ||
| 767 | /* | |
| 768 | * Being able to checkpoint an suid or sgid program is not a good | |
| 769 | * idea. | |
| 770 | */ | |
| 771 | if (sugid_coredump == 0 && (p->p_flag & P_SUGID)) { | |
| 772 | chptinuse--; | |
| 773 | return (EPERM); | |
| 774 | } | |
| 775 | ||
| 776 | buf = ckpt_expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid); | |
| 777 | if (buf == NULL) { | |
| 778 | chptinuse--; | |
| 779 | return (ENOMEM); | |
| 780 | } | |
| 781 | ||
| 782 | log(LOG_INFO, "pid %d (%s), uid %d: checkpointing to %s\n", | |
| 783 | p->p_pid, p->p_comm, | |
| 784 | (p->p_ucred ? p->p_ucred->cr_uid : -1), | |
| 785 | buf); | |
| 786 | ||
| 787 | PRINTF(("ckpt handler called, using '%s'\n", buf)); | |
| 788 | ||
| 789 | /* | |
| 790 | * Use the same safety flags that the coredump code uses. Remove | |
| 791 | * any previous checkpoint file before writing out the new one in | |
| 792 | * case we are re-checkpointing a program that had been checkpt | |
| 793 | * restored. Otherwise we will corrupt the program space (which is | |
| 794 | * made up of mmap()ings of the previous checkpoint file) while we | |
| 795 | * write out the new one. | |
| 796 | */ | |
| 797 | error = nlookup_init(&nd, buf, UIO_SYSSPACE, 0); | |
| 798 | if (error == 0) | |
| 799 | error = kern_unlink(&nd); | |
| 800 | nlookup_done(&nd); | |
| 801 | error = fp_open(buf, O_WRONLY|O_CREAT|O_TRUNC|O_NOFOLLOW, 0600, &fp); | |
| 802 | if (error == 0) { | |
| 7d20a8ff | 803 | error = ckpt_freeze_proc(lp, fp); |
| 0ba6fbbf MD |
804 | fp_close(fp); |
| 805 | } else { | |
| 6ea70f76 | 806 | kprintf("checkpoint failed with open - error: %d\n", error); |
| 0ba6fbbf | 807 | } |
| efda3bd0 | 808 | kfree(buf, M_TEMP); |
| 0ba6fbbf MD |
809 | chptinuse--; |
| 810 | return (error); | |
| 811 | } | |
| 812 | ||
| 813 | static char ckptfilename[MAXPATHLEN] = {"%N.ckpt"}; | |
| 814 | SYSCTL_STRING(_kern, OID_AUTO, ckptfile, CTLFLAG_RW, ckptfilename, | |
| 815 | sizeof(ckptfilename), "process checkpoint name format string"); | |
| 816 | ||
| 817 | /* | |
| 818 | * expand_name(name, uid, pid) | |
| 819 | * Expand the name described in corefilename, using name, uid, and pid. | |
| 6ea70f76 | 820 | * corefilename is a kprintf-like string, with three format specifiers: |
| 0ba6fbbf MD |
821 | * %N name of process ("name") |
| 822 | * %P process id (pid) | |
| 823 | * %U user id (uid) | |
| 824 | * For example, "%N.core" is the default; they can be disabled completely | |
| 825 | * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P". | |
| 826 | * This is controlled by the sysctl variable kern.corefile (see above). | |
| 827 | * | |
| 828 | * -- taken from the coredump code | |
| 829 | */ | |
| 830 | ||
| 831 | static | |
| 832 | char * | |
| 833 | ckpt_expand_name(const char *name, uid_t uid, pid_t pid) | |
| 834 | { | |
| 835 | char *temp; | |
| 836 | char *bp; | |
| 837 | char buf[11]; /* Buffer for pid/uid -- max 4B */ | |
| 838 | int error; | |
| 839 | int i; | |
| 840 | int n; | |
| 841 | char *format = ckptfilename; | |
| 842 | size_t namelen; | |
| 843 | ||
| efda3bd0 | 844 | temp = kmalloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT); |
| 0ba6fbbf MD |
845 | if (temp == NULL) |
| 846 | return NULL; | |
| 847 | namelen = strlen(name); | |
| 848 | n = 0; | |
| 849 | if (ckptfilename[0] != '/') { | |
| 850 | if ((bp = kern_getcwd(temp, MAXPATHLEN - 1, &error)) == NULL) { | |
| efda3bd0 | 851 | kfree(temp, M_TEMP); |
| 0ba6fbbf MD |
852 | return NULL; |
| 853 | } | |
| 854 | n = strlen(bp); | |
| 855 | bcopy(bp, temp, n + 1); /* normalize location of the path */ | |
| 856 | temp[n++] = '/'; | |
| 857 | temp[n] = '\0'; | |
| 858 | } | |
| 859 | for (i= 0; n < MAXPATHLEN && format[i]; i++) { | |
| 860 | int l; | |
| 861 | switch (format[i]) { | |
| 862 | case '%': /* Format character */ | |
| 863 | i++; | |
| 864 | switch (format[i]) { | |
| 865 | case '%': | |
| 866 | temp[n++] = '%'; | |
| 867 | break; | |
| 868 | case 'N': /* process name */ | |
| 869 | if ((n + namelen) > MAXPATHLEN) { | |
| 870 | log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", | |
| 871 | pid, name, uid, temp, name); | |
| efda3bd0 | 872 | kfree(temp, M_TEMP); |
| 0ba6fbbf MD |
873 | return NULL; |
| 874 | } | |
| 875 | memcpy(temp+n, name, namelen); | |
| 876 | n += namelen; | |
| 877 | break; | |
| 878 | case 'P': /* process id */ | |
| f8c7a42d | 879 | l = ksprintf(buf, "%u", pid); |
| 0ba6fbbf MD |
880 | if ((n + l) > MAXPATHLEN) { |
| 881 | log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", | |
| 882 | pid, name, uid, temp, name); | |
| efda3bd0 | 883 | kfree(temp, M_TEMP); |
| 0ba6fbbf MD |
884 | return NULL; |
| 885 | } | |
| 886 | memcpy(temp+n, buf, l); | |
| 887 | n += l; | |
| 888 | break; | |
| 889 | case 'U': /* user id */ | |
| f8c7a42d | 890 | l = ksprintf(buf, "%u", uid); |
| 0ba6fbbf MD |
891 | if ((n + l) > MAXPATHLEN) { |
| 892 | log(LOG_ERR, "pid %d (%s), uid (%u): Path `%s%s' is too long\n", | |
| 893 | pid, name, uid, temp, name); | |
| efda3bd0 | 894 | kfree(temp, M_TEMP); |
| 0ba6fbbf MD |
895 | return NULL; |
| 896 | } | |
| 897 | memcpy(temp+n, buf, l); | |
| 898 | n += l; | |
| 899 | break; | |
| 900 | default: | |
| 901 | log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format); | |
| 902 | } | |
| 903 | break; | |
| 904 | default: | |
| 905 | temp[n++] = format[i]; | |
| 906 | } | |
| 907 | } | |
| 908 | temp[n] = '\0'; | |
| 909 | return temp; | |
| 910 | } | |
| 911 |