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
29acc0eea6SMark Johnston #include <sys/types.h>
30acc0eea6SMark Johnston #include <sys/sysctl.h>
314808a678SMark Johnston #include <sys/user.h>
32acc0eea6SMark Johnston #include <sys/wait.h>
33acc0eea6SMark Johnston
342c633af4SJohn Birrell #include <err.h>
352c633af4SJohn Birrell #include <errno.h>
362c633af4SJohn Birrell #include <fcntl.h>
372c633af4SJohn Birrell #include <limits.h>
382c633af4SJohn Birrell #include <stdlib.h>
392c633af4SJohn Birrell #include <string.h>
402c633af4SJohn Birrell #include <unistd.h>
41acc0eea6SMark Johnston
424808a678SMark Johnston #include <libelf.h>
434808a678SMark Johnston #include <libprocstat.h>
444808a678SMark Johnston
45acc0eea6SMark Johnston #include "_libproc.h"
46acc0eea6SMark Johnston
47cdaa2de8SMark Johnston extern char * const *environ;
48cdaa2de8SMark Johnston
494808a678SMark Johnston static int getelfclass(int);
504808a678SMark Johnston static int proc_init(pid_t, int, int, struct proc_handle **);
51acc0eea6SMark Johnston
52acc0eea6SMark Johnston static int
getelfclass(int fd)534808a678SMark Johnston getelfclass(int fd)
54acc0eea6SMark Johnston {
554808a678SMark Johnston GElf_Ehdr ehdr;
564808a678SMark Johnston Elf *e;
574808a678SMark Johnston int class;
584808a678SMark Johnston
594808a678SMark Johnston class = ELFCLASSNONE;
604808a678SMark Johnston
614808a678SMark Johnston if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
624808a678SMark Johnston goto out;
634808a678SMark Johnston if (gelf_getehdr(e, &ehdr) == NULL)
644808a678SMark Johnston goto out;
654808a678SMark Johnston class = ehdr.e_ident[EI_CLASS];
664808a678SMark Johnston out:
674808a678SMark Johnston (void)elf_end(e);
684808a678SMark Johnston return (class);
694808a678SMark Johnston }
704808a678SMark Johnston
714808a678SMark Johnston static int
proc_init(pid_t pid,int flags,int status,struct proc_handle ** pphdl)724808a678SMark Johnston proc_init(pid_t pid, int flags, int status, struct proc_handle **pphdl)
734808a678SMark Johnston {
744808a678SMark Johnston struct kinfo_proc *kp;
754808a678SMark Johnston struct proc_handle *phdl;
764808a678SMark Johnston int error, class, count, fd;
774808a678SMark Johnston
78ce601a26SConrad Meyer error = ENOMEM;
794808a678SMark Johnston if ((phdl = malloc(sizeof(*phdl))) == NULL)
80ce601a26SConrad Meyer goto out;
81acc0eea6SMark Johnston
82acc0eea6SMark Johnston memset(phdl, 0, sizeof(*phdl));
83b1bb30e5SMark Johnston phdl->public.pid = pid;
84acc0eea6SMark Johnston phdl->flags = flags;
85acc0eea6SMark Johnston phdl->status = status;
864808a678SMark Johnston phdl->procstat = procstat_open_sysctl();
874808a678SMark Johnston if (phdl->procstat == NULL)
88ce601a26SConrad Meyer goto out;
89acc0eea6SMark Johnston
904808a678SMark Johnston /* Obtain a path to the executable. */
914808a678SMark Johnston if ((kp = procstat_getprocs(phdl->procstat, KERN_PROC_PID, pid,
924808a678SMark Johnston &count)) == NULL)
93ce601a26SConrad Meyer goto out;
944808a678SMark Johnston error = procstat_getpathname(phdl->procstat, kp, phdl->execpath,
954808a678SMark Johnston sizeof(phdl->execpath));
964808a678SMark Johnston procstat_freeprocs(phdl->procstat, kp);
974808a678SMark Johnston if (error != 0)
98ce601a26SConrad Meyer goto out;
99acc0eea6SMark Johnston
1004808a678SMark Johnston /* Use it to determine the data model for the process. */
1014808a678SMark Johnston if ((fd = open(phdl->execpath, O_RDONLY)) < 0) {
1024808a678SMark Johnston error = errno;
1034808a678SMark Johnston goto out;
1044808a678SMark Johnston }
1054808a678SMark Johnston class = getelfclass(fd);
1064808a678SMark Johnston switch (class) {
1074808a678SMark Johnston case ELFCLASS64:
1084808a678SMark Johnston phdl->model = PR_MODEL_LP64;
1094808a678SMark Johnston break;
1104808a678SMark Johnston case ELFCLASS32:
1114808a678SMark Johnston phdl->model = PR_MODEL_ILP32;
1124808a678SMark Johnston break;
1134808a678SMark Johnston case ELFCLASSNONE:
1144808a678SMark Johnston default:
1154808a678SMark Johnston error = EINVAL;
1164808a678SMark Johnston break;
1174808a678SMark Johnston }
1184808a678SMark Johnston (void)close(fd);
1194808a678SMark Johnston
1204808a678SMark Johnston out:
1214808a678SMark Johnston *pphdl = phdl;
1224808a678SMark Johnston return (error);
123acc0eea6SMark Johnston }
1242c633af4SJohn Birrell
1252c633af4SJohn Birrell int
proc_attach(pid_t pid,int flags,struct proc_handle ** pphdl)1262c633af4SJohn Birrell proc_attach(pid_t pid, int flags, struct proc_handle **pphdl)
1272c633af4SJohn Birrell {
1282c633af4SJohn Birrell struct proc_handle *phdl;
1294808a678SMark Johnston int error, status;
1302c633af4SJohn Birrell
131b043b5dcSMark Johnston if (pid == 0 || (pid == getpid() && (flags & PATTACH_RDONLY) == 0))
1322c633af4SJohn Birrell return (EINVAL);
1334808a678SMark Johnston if (elf_version(EV_CURRENT) == EV_NONE)
1344808a678SMark Johnston return (ENOENT);
1352c633af4SJohn Birrell
1362c633af4SJohn Birrell /*
1372c633af4SJohn Birrell * Allocate memory for the process handle, a structure containing
1382c633af4SJohn Birrell * all things related to the process.
1392c633af4SJohn Birrell */
1404808a678SMark Johnston error = proc_init(pid, flags, PS_RUN, &phdl);
141acc0eea6SMark Johnston if (error != 0)
142acc0eea6SMark Johnston goto out;
143acc0eea6SMark Johnston
144b043b5dcSMark Johnston if ((flags & PATTACH_RDONLY) == 0) {
145b1bb30e5SMark Johnston if (ptrace(PT_ATTACH, proc_getpid(phdl), 0, 0) != 0) {
1462c633af4SJohn Birrell error = errno;
1478eb20f36SRui Paulo DPRINTF("ERROR: cannot ptrace child process %d", pid);
1488eb20f36SRui Paulo goto out;
1498eb20f36SRui Paulo }
1502c633af4SJohn Birrell
1512c633af4SJohn Birrell /* Wait for the child process to stop. */
1528eb20f36SRui Paulo if (waitpid(pid, &status, WUNTRACED) == -1) {
1538eb20f36SRui Paulo error = errno;
1548eb20f36SRui Paulo DPRINTF("ERROR: child process %d didn't stop as expected", pid);
1558eb20f36SRui Paulo goto out;
1568eb20f36SRui Paulo }
1572c633af4SJohn Birrell
1582c633af4SJohn Birrell /* Check for an unexpected status. */
1594808a678SMark Johnston if (!WIFSTOPPED(status))
16030e81f7eSMark Johnston DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
1612c633af4SJohn Birrell else
1622c633af4SJohn Birrell phdl->status = PS_STOP;
1632c633af4SJohn Birrell
164b043b5dcSMark Johnston if ((flags & PATTACH_NOSTOP) != 0)
165b043b5dcSMark Johnston proc_continue(phdl);
166b043b5dcSMark Johnston }
167b043b5dcSMark Johnston
168a8375da0SAndriy Gapon out:
169b043b5dcSMark Johnston if (error != 0 && phdl != NULL) {
1702c633af4SJohn Birrell proc_free(phdl);
1714808a678SMark Johnston phdl = NULL;
1724808a678SMark Johnston }
1732c633af4SJohn Birrell *pphdl = phdl;
1742c633af4SJohn Birrell return (error);
1752c633af4SJohn Birrell }
1762c633af4SJohn Birrell
1772c633af4SJohn Birrell int
proc_create(const char * file,char * const * argv,char * const * envp,proc_child_func * pcf,void * child_arg,struct proc_handle ** pphdl)1785577b8a7SMark Johnston proc_create(const char *file, char * const *argv, char * const *envp,
1795577b8a7SMark Johnston proc_child_func *pcf, void *child_arg, struct proc_handle **pphdl)
1802c633af4SJohn Birrell {
1812c633af4SJohn Birrell struct proc_handle *phdl;
1828440c5e6SMark Johnston int error, status;
1832c633af4SJohn Birrell pid_t pid;
1842c633af4SJohn Birrell
1854808a678SMark Johnston if (elf_version(EV_CURRENT) == EV_NONE)
1864808a678SMark Johnston return (ENOENT);
1878eb20f36SRui Paulo
188643fc6b2SMark Johnston error = 0;
189643fc6b2SMark Johnston phdl = NULL;
190643fc6b2SMark Johnston
1915577b8a7SMark Johnston if ((pid = fork()) == -1)
1922c633af4SJohn Birrell error = errno;
1932c633af4SJohn Birrell else if (pid == 0) {
1942c633af4SJohn Birrell /* The child expects to be traced. */
1952c633af4SJohn Birrell if (ptrace(PT_TRACE_ME, 0, 0, 0) != 0)
1962c633af4SJohn Birrell _exit(1);
1972c633af4SJohn Birrell
198820e0679SCraig Rodrigues if (pcf != NULL)
199820e0679SCraig Rodrigues (*pcf)(child_arg);
200820e0679SCraig Rodrigues
2015577b8a7SMark Johnston if (envp != NULL)
2025577b8a7SMark Johnston environ = envp;
2035577b8a7SMark Johnston
2042c633af4SJohn Birrell execvp(file, argv);
2052c633af4SJohn Birrell
2062c633af4SJohn Birrell _exit(2);
2074808a678SMark Johnston /* NOTREACHED */
2082c633af4SJohn Birrell } else {
2092c633af4SJohn Birrell /* Wait for the child process to stop. */
2108eb20f36SRui Paulo if (waitpid(pid, &status, WUNTRACED) == -1) {
2118eb20f36SRui Paulo error = errno;
2128eb20f36SRui Paulo DPRINTF("ERROR: child process %d didn't stop as expected", pid);
2138eb20f36SRui Paulo goto bad;
2142c633af4SJohn Birrell }
2152c633af4SJohn Birrell
2168eb20f36SRui Paulo /* Check for an unexpected status. */
2174808a678SMark Johnston if (!WIFSTOPPED(status)) {
2185577b8a7SMark Johnston error = ENOENT;
21930e81f7eSMark Johnston DPRINTFX("ERROR: child process %d status 0x%x", pid, status);
2208eb20f36SRui Paulo goto bad;
2218eb20f36SRui Paulo }
2225577b8a7SMark Johnston
2235577b8a7SMark Johnston /* The parent owns the process handle. */
2245577b8a7SMark Johnston error = proc_init(pid, 0, PS_IDLE, &phdl);
2255577b8a7SMark Johnston if (error == 0)
2268440c5e6SMark Johnston phdl->status = PS_STOP;
2278440c5e6SMark Johnston
2288eb20f36SRui Paulo bad:
2298440c5e6SMark Johnston if (error != 0 && phdl != NULL) {
2302c633af4SJohn Birrell proc_free(phdl);
2314808a678SMark Johnston phdl = NULL;
2324808a678SMark Johnston }
2338440c5e6SMark Johnston }
2342c633af4SJohn Birrell *pphdl = phdl;
2352c633af4SJohn Birrell return (error);
2362c633af4SJohn Birrell }
2372c633af4SJohn Birrell
2382c633af4SJohn Birrell void
proc_free(struct proc_handle * phdl)2392c633af4SJohn Birrell proc_free(struct proc_handle *phdl)
2402c633af4SJohn Birrell {
24107a9c2e6SMark Johnston struct file_info *file;
24207a9c2e6SMark Johnston size_t i;
2434808a678SMark Johnston
24407a9c2e6SMark Johnston for (i = 0; i < phdl->nmappings; i++) {
24507a9c2e6SMark Johnston file = phdl->mappings[i].file;
24607a9c2e6SMark Johnston if (file != NULL && --file->refs == 0) {
24707a9c2e6SMark Johnston if (file->elf != NULL) {
24807a9c2e6SMark Johnston (void)elf_end(file->elf);
24907a9c2e6SMark Johnston (void)close(file->fd);
250c156354fSMark Johnston if (file->symtab.nsyms > 0)
251c156354fSMark Johnston free(file->symtab.index);
252c156354fSMark Johnston if (file->dynsymtab.nsyms > 0)
253c156354fSMark Johnston free(file->dynsymtab.index);
25407a9c2e6SMark Johnston }
25507a9c2e6SMark Johnston free(file);
25607a9c2e6SMark Johnston }
25707a9c2e6SMark Johnston }
25807a9c2e6SMark Johnston if (phdl->maparrsz > 0)
25907a9c2e6SMark Johnston free(phdl->mappings);
2604808a678SMark Johnston if (phdl->procstat != NULL)
2614808a678SMark Johnston procstat_close(phdl->procstat);
26207a9c2e6SMark Johnston if (phdl->rdap != NULL)
26307a9c2e6SMark Johnston rd_delete(phdl->rdap);
2642c633af4SJohn Birrell free(phdl);
2652c633af4SJohn Birrell }
266