12c633af4SJohn Birrell /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 35e53a4f9SPedro F. Giffuni * 42c633af4SJohn Birrell * Copyright (c) 2008 John Birrell (jb@freebsd.org) 52c633af4SJohn Birrell * All rights reserved. 62c633af4SJohn Birrell * 72c633af4SJohn Birrell * Redistribution and use in source and binary forms, with or without 82c633af4SJohn Birrell * modification, are permitted provided that the following conditions 92c633af4SJohn Birrell * are met: 102c633af4SJohn Birrell * 1. Redistributions of source code must retain the above copyright 112c633af4SJohn Birrell * notice, this list of conditions and the following disclaimer. 122c633af4SJohn Birrell * 2. Redistributions in binary form must reproduce the above copyright 132c633af4SJohn Birrell * notice, this list of conditions and the following disclaimer in the 142c633af4SJohn Birrell * documentation and/or other materials provided with the distribution. 152c633af4SJohn Birrell * 162c633af4SJohn Birrell * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 172c633af4SJohn Birrell * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 182c633af4SJohn Birrell * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 192c633af4SJohn Birrell * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 202c633af4SJohn Birrell * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 212c633af4SJohn Birrell * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 222c633af4SJohn Birrell * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 232c633af4SJohn Birrell * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 242c633af4SJohn Birrell * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 252c633af4SJohn Birrell * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 262c633af4SJohn Birrell * SUCH DAMAGE. 272c633af4SJohn Birrell */ 282c633af4SJohn Birrell 29fcf9fc10SMark Johnston #include <sys/cdefs.h> 30fcf9fc10SMark Johnston __FBSDID("$FreeBSD$"); 31fcf9fc10SMark Johnston 32acc0eea6SMark Johnston #include <sys/types.h> 33acc0eea6SMark Johnston #include <sys/sysctl.h> 344808a678SMark Johnston #include <sys/user.h> 35acc0eea6SMark Johnston #include <sys/wait.h> 36acc0eea6SMark Johnston 372c633af4SJohn Birrell #include <err.h> 382c633af4SJohn Birrell #include <errno.h> 392c633af4SJohn Birrell #include <fcntl.h> 402c633af4SJohn Birrell #include <limits.h> 412c633af4SJohn Birrell #include <stdlib.h> 422c633af4SJohn Birrell #include <string.h> 432c633af4SJohn Birrell #include <unistd.h> 44acc0eea6SMark Johnston 454808a678SMark Johnston #include <libelf.h> 464808a678SMark Johnston #include <libprocstat.h> 474808a678SMark Johnston 48acc0eea6SMark Johnston #include "_libproc.h" 49acc0eea6SMark Johnston 50cdaa2de8SMark Johnston extern char * const *environ; 51cdaa2de8SMark Johnston 524808a678SMark Johnston static int getelfclass(int); 534808a678SMark Johnston static int proc_init(pid_t, int, int, struct proc_handle **); 54acc0eea6SMark Johnston 55acc0eea6SMark Johnston static int 564808a678SMark Johnston getelfclass(int fd) 57acc0eea6SMark Johnston { 584808a678SMark Johnston GElf_Ehdr ehdr; 594808a678SMark Johnston Elf *e; 604808a678SMark Johnston int class; 614808a678SMark Johnston 624808a678SMark Johnston class = ELFCLASSNONE; 634808a678SMark Johnston 644808a678SMark Johnston if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) 654808a678SMark Johnston goto out; 664808a678SMark Johnston if (gelf_getehdr(e, &ehdr) == NULL) 674808a678SMark Johnston goto out; 684808a678SMark Johnston class = ehdr.e_ident[EI_CLASS]; 694808a678SMark Johnston out: 704808a678SMark Johnston (void)elf_end(e); 714808a678SMark Johnston return (class); 724808a678SMark Johnston } 734808a678SMark Johnston 744808a678SMark Johnston static int 754808a678SMark Johnston proc_init(pid_t pid, int flags, int status, struct proc_handle **pphdl) 764808a678SMark Johnston { 774808a678SMark Johnston struct kinfo_proc *kp; 784808a678SMark Johnston struct proc_handle *phdl; 794808a678SMark Johnston int error, class, count, fd; 804808a678SMark Johnston 81ce601a26SConrad Meyer error = ENOMEM; 824808a678SMark Johnston if ((phdl = malloc(sizeof(*phdl))) == NULL) 83ce601a26SConrad Meyer goto out; 84acc0eea6SMark Johnston 85acc0eea6SMark Johnston memset(phdl, 0, sizeof(*phdl)); 86b1bb30e5SMark Johnston phdl->public.pid = pid; 87acc0eea6SMark Johnston phdl->flags = flags; 88acc0eea6SMark Johnston phdl->status = status; 894808a678SMark Johnston phdl->procstat = procstat_open_sysctl(); 904808a678SMark Johnston if (phdl->procstat == NULL) 91ce601a26SConrad Meyer goto out; 92acc0eea6SMark Johnston 934808a678SMark Johnston /* Obtain a path to the executable. */ 944808a678SMark Johnston if ((kp = procstat_getprocs(phdl->procstat, KERN_PROC_PID, pid, 954808a678SMark Johnston &count)) == NULL) 96ce601a26SConrad Meyer goto out; 974808a678SMark Johnston error = procstat_getpathname(phdl->procstat, kp, phdl->execpath, 984808a678SMark Johnston sizeof(phdl->execpath)); 994808a678SMark Johnston procstat_freeprocs(phdl->procstat, kp); 1004808a678SMark Johnston if (error != 0) 101ce601a26SConrad Meyer goto out; 102acc0eea6SMark Johnston 1034808a678SMark Johnston /* Use it to determine the data model for the process. */ 1044808a678SMark Johnston if ((fd = open(phdl->execpath, O_RDONLY)) < 0) { 1054808a678SMark Johnston error = errno; 1064808a678SMark Johnston goto out; 1074808a678SMark Johnston } 1084808a678SMark Johnston class = getelfclass(fd); 1094808a678SMark Johnston switch (class) { 1104808a678SMark Johnston case ELFCLASS64: 1114808a678SMark Johnston phdl->model = PR_MODEL_LP64; 1124808a678SMark Johnston break; 1134808a678SMark Johnston case ELFCLASS32: 1144808a678SMark Johnston phdl->model = PR_MODEL_ILP32; 1154808a678SMark Johnston break; 1164808a678SMark Johnston case ELFCLASSNONE: 1174808a678SMark Johnston default: 1184808a678SMark Johnston error = EINVAL; 1194808a678SMark Johnston break; 1204808a678SMark Johnston } 1214808a678SMark Johnston (void)close(fd); 1224808a678SMark Johnston 1234808a678SMark Johnston out: 1244808a678SMark Johnston *pphdl = phdl; 1254808a678SMark Johnston return (error); 126acc0eea6SMark Johnston } 1272c633af4SJohn Birrell 1282c633af4SJohn Birrell int 1292c633af4SJohn Birrell proc_attach(pid_t pid, int flags, struct proc_handle **pphdl) 1302c633af4SJohn Birrell { 1312c633af4SJohn Birrell struct proc_handle *phdl; 1324808a678SMark Johnston int error, status; 1332c633af4SJohn Birrell 134b043b5dcSMark Johnston if (pid == 0 || (pid == getpid() && (flags & PATTACH_RDONLY) == 0)) 1352c633af4SJohn Birrell return (EINVAL); 1364808a678SMark Johnston if (elf_version(EV_CURRENT) == EV_NONE) 1374808a678SMark Johnston return (ENOENT); 1382c633af4SJohn Birrell 1392c633af4SJohn Birrell /* 1402c633af4SJohn Birrell * Allocate memory for the process handle, a structure containing 1412c633af4SJohn Birrell * all things related to the process. 1422c633af4SJohn Birrell */ 1434808a678SMark Johnston error = proc_init(pid, flags, PS_RUN, &phdl); 144acc0eea6SMark Johnston if (error != 0) 145acc0eea6SMark Johnston goto out; 146acc0eea6SMark Johnston 147b043b5dcSMark Johnston if ((flags & PATTACH_RDONLY) == 0) { 148b1bb30e5SMark Johnston if (ptrace(PT_ATTACH, proc_getpid(phdl), 0, 0) != 0) { 1492c633af4SJohn Birrell error = errno; 1508eb20f36SRui Paulo DPRINTF("ERROR: cannot ptrace child process %d", pid); 1518eb20f36SRui Paulo goto out; 1528eb20f36SRui Paulo } 1532c633af4SJohn Birrell 1542c633af4SJohn Birrell /* Wait for the child process to stop. */ 1558eb20f36SRui Paulo if (waitpid(pid, &status, WUNTRACED) == -1) { 1568eb20f36SRui Paulo error = errno; 1578eb20f36SRui Paulo DPRINTF("ERROR: child process %d didn't stop as expected", pid); 1588eb20f36SRui Paulo goto out; 1598eb20f36SRui Paulo } 1602c633af4SJohn Birrell 1612c633af4SJohn Birrell /* Check for an unexpected status. */ 1624808a678SMark Johnston if (!WIFSTOPPED(status)) 16330e81f7eSMark Johnston DPRINTFX("ERROR: child process %d status 0x%x", pid, status); 1642c633af4SJohn Birrell else 1652c633af4SJohn Birrell phdl->status = PS_STOP; 1662c633af4SJohn Birrell 167b043b5dcSMark Johnston if ((flags & PATTACH_NOSTOP) != 0) 168b043b5dcSMark Johnston proc_continue(phdl); 169b043b5dcSMark Johnston } 170b043b5dcSMark Johnston 171a8375da0SAndriy Gapon out: 172b043b5dcSMark Johnston if (error != 0 && phdl != NULL) { 1732c633af4SJohn Birrell proc_free(phdl); 1744808a678SMark Johnston phdl = NULL; 1754808a678SMark Johnston } 1762c633af4SJohn Birrell *pphdl = phdl; 1772c633af4SJohn Birrell return (error); 1782c633af4SJohn Birrell } 1792c633af4SJohn Birrell 1802c633af4SJohn Birrell int 1815577b8a7SMark Johnston proc_create(const char *file, char * const *argv, char * const *envp, 1825577b8a7SMark Johnston proc_child_func *pcf, void *child_arg, struct proc_handle **pphdl) 1832c633af4SJohn Birrell { 1842c633af4SJohn Birrell struct proc_handle *phdl; 1858440c5e6SMark Johnston int error, status; 1862c633af4SJohn Birrell pid_t pid; 1872c633af4SJohn Birrell 1884808a678SMark Johnston if (elf_version(EV_CURRENT) == EV_NONE) 1894808a678SMark Johnston return (ENOENT); 1908eb20f36SRui Paulo 191643fc6b2SMark Johnston error = 0; 192643fc6b2SMark Johnston phdl = NULL; 193643fc6b2SMark Johnston 1945577b8a7SMark Johnston if ((pid = fork()) == -1) 1952c633af4SJohn Birrell error = errno; 1962c633af4SJohn Birrell else if (pid == 0) { 1972c633af4SJohn Birrell /* The child expects to be traced. */ 1982c633af4SJohn Birrell if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0) 1992c633af4SJohn Birrell _exit(1); 2002c633af4SJohn Birrell 201820e0679SCraig Rodrigues if (pcf != NULL) 202820e0679SCraig Rodrigues (*pcf)(child_arg); 203820e0679SCraig Rodrigues 2045577b8a7SMark Johnston if (envp != NULL) 2055577b8a7SMark Johnston environ = envp; 2065577b8a7SMark Johnston 2072c633af4SJohn Birrell execvp(file, argv); 2082c633af4SJohn Birrell 2092c633af4SJohn Birrell _exit(2); 2104808a678SMark Johnston /* NOTREACHED */ 2112c633af4SJohn Birrell } else { 2122c633af4SJohn Birrell /* Wait for the child process to stop. */ 2138eb20f36SRui Paulo if (waitpid(pid, &status, WUNTRACED) == -1) { 2148eb20f36SRui Paulo error = errno; 2158eb20f36SRui Paulo DPRINTF("ERROR: child process %d didn't stop as expected", pid); 2168eb20f36SRui Paulo goto bad; 2172c633af4SJohn Birrell } 2182c633af4SJohn Birrell 2198eb20f36SRui Paulo /* Check for an unexpected status. */ 2204808a678SMark Johnston if (!WIFSTOPPED(status)) { 2215577b8a7SMark Johnston error = ENOENT; 22230e81f7eSMark Johnston DPRINTFX("ERROR: child process %d status 0x%x", pid, status); 2238eb20f36SRui Paulo goto bad; 2248eb20f36SRui Paulo } 2255577b8a7SMark Johnston 2265577b8a7SMark Johnston /* The parent owns the process handle. */ 2275577b8a7SMark Johnston error = proc_init(pid, 0, PS_IDLE, &phdl); 2285577b8a7SMark Johnston if (error == 0) 2298440c5e6SMark Johnston phdl->status = PS_STOP; 2308440c5e6SMark Johnston 2318eb20f36SRui Paulo bad: 2328440c5e6SMark Johnston if (error != 0 && phdl != NULL) { 2332c633af4SJohn Birrell proc_free(phdl); 2344808a678SMark Johnston phdl = NULL; 2354808a678SMark Johnston } 2368440c5e6SMark Johnston } 2372c633af4SJohn Birrell *pphdl = phdl; 2382c633af4SJohn Birrell return (error); 2392c633af4SJohn Birrell } 2402c633af4SJohn Birrell 2412c633af4SJohn Birrell void 2422c633af4SJohn Birrell proc_free(struct proc_handle *phdl) 2432c633af4SJohn Birrell { 24407a9c2e6SMark Johnston struct file_info *file; 24507a9c2e6SMark Johnston size_t i; 2464808a678SMark Johnston 24707a9c2e6SMark Johnston for (i = 0; i < phdl->nmappings; i++) { 24807a9c2e6SMark Johnston file = phdl->mappings[i].file; 24907a9c2e6SMark Johnston if (file != NULL && --file->refs == 0) { 25007a9c2e6SMark Johnston if (file->elf != NULL) { 25107a9c2e6SMark Johnston (void)elf_end(file->elf); 25207a9c2e6SMark Johnston (void)close(file->fd); 253c156354fSMark Johnston if (file->symtab.nsyms > 0) 254c156354fSMark Johnston free(file->symtab.index); 255c156354fSMark Johnston if (file->dynsymtab.nsyms > 0) 256c156354fSMark Johnston free(file->dynsymtab.index); 25707a9c2e6SMark Johnston } 25807a9c2e6SMark Johnston free(file); 25907a9c2e6SMark Johnston } 26007a9c2e6SMark Johnston } 26107a9c2e6SMark Johnston if (phdl->maparrsz > 0) 26207a9c2e6SMark Johnston free(phdl->mappings); 2634808a678SMark Johnston if (phdl->procstat != NULL) 2644808a678SMark Johnston procstat_close(phdl->procstat); 26507a9c2e6SMark Johnston if (phdl->rdap != NULL) 26607a9c2e6SMark Johnston rd_delete(phdl->rdap); 2672c633af4SJohn Birrell free(phdl); 2682c633af4SJohn Birrell } 269