12c633af4SJohn Birrell /*- 22c633af4SJohn Birrell * Copyright (c) 2008 John Birrell (jb@freebsd.org) 32c633af4SJohn Birrell * All rights reserved. 42c633af4SJohn Birrell * 52c633af4SJohn Birrell * Redistribution and use in source and binary forms, with or without 62c633af4SJohn Birrell * modification, are permitted provided that the following conditions 72c633af4SJohn Birrell * are met: 82c633af4SJohn Birrell * 1. Redistributions of source code must retain the above copyright 92c633af4SJohn Birrell * notice, this list of conditions and the following disclaimer. 102c633af4SJohn Birrell * 2. Redistributions in binary form must reproduce the above copyright 112c633af4SJohn Birrell * notice, this list of conditions and the following disclaimer in the 122c633af4SJohn Birrell * documentation and/or other materials provided with the distribution. 132c633af4SJohn Birrell * 142c633af4SJohn Birrell * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 152c633af4SJohn Birrell * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 162c633af4SJohn Birrell * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 172c633af4SJohn Birrell * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 182c633af4SJohn Birrell * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 192c633af4SJohn Birrell * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 202c633af4SJohn Birrell * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 212c633af4SJohn Birrell * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 222c633af4SJohn Birrell * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 232c633af4SJohn Birrell * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 242c633af4SJohn Birrell * SUCH DAMAGE. 252c633af4SJohn Birrell * 262c633af4SJohn Birrell * $FreeBSD$ 272c633af4SJohn Birrell */ 282c633af4SJohn Birrell 292c633af4SJohn Birrell #include "_libproc.h" 302c633af4SJohn Birrell #include <errno.h> 312c633af4SJohn Birrell #include <unistd.h> 322c633af4SJohn Birrell #include <sys/ptrace.h> 332c633af4SJohn Birrell #include <sys/wait.h> 342c633af4SJohn Birrell #include <stdio.h> 352c633af4SJohn Birrell 362c633af4SJohn Birrell int 372c633af4SJohn Birrell proc_clearflags(struct proc_handle *phdl, int mask) 382c633af4SJohn Birrell { 392c633af4SJohn Birrell if (phdl == NULL) 402c633af4SJohn Birrell return (EINVAL); 412c633af4SJohn Birrell 422c633af4SJohn Birrell phdl->flags &= ~mask; 432c633af4SJohn Birrell 442c633af4SJohn Birrell return (0); 452c633af4SJohn Birrell } 462c633af4SJohn Birrell 472c633af4SJohn Birrell int 482c633af4SJohn Birrell proc_continue(struct proc_handle *phdl) 492c633af4SJohn Birrell { 502c633af4SJohn Birrell if (phdl == NULL) 512c633af4SJohn Birrell return (EINVAL); 522c633af4SJohn Birrell 532c633af4SJohn Birrell if (ptrace(PT_CONTINUE, phdl->pid, (caddr_t)(uintptr_t) 1, 0) != 0) 542c633af4SJohn Birrell return (errno); 552c633af4SJohn Birrell 562c633af4SJohn Birrell phdl->status = PS_RUN; 572c633af4SJohn Birrell 582c633af4SJohn Birrell return (0); 592c633af4SJohn Birrell } 602c633af4SJohn Birrell 612c633af4SJohn Birrell int 622c633af4SJohn Birrell proc_detach(struct proc_handle *phdl) 632c633af4SJohn Birrell { 642c633af4SJohn Birrell if (phdl == NULL) 652c633af4SJohn Birrell return (EINVAL); 662c633af4SJohn Birrell 672c633af4SJohn Birrell if (ptrace(PT_DETACH, phdl->pid, 0, 0) != 0) 682c633af4SJohn Birrell return (errno); 692c633af4SJohn Birrell 702c633af4SJohn Birrell return (0); 712c633af4SJohn Birrell } 722c633af4SJohn Birrell 732c633af4SJohn Birrell int 742c633af4SJohn Birrell proc_getflags(struct proc_handle *phdl) 752c633af4SJohn Birrell { 762c633af4SJohn Birrell if (phdl == NULL) 772c633af4SJohn Birrell return (-1); 782c633af4SJohn Birrell 792c633af4SJohn Birrell return(phdl->flags); 802c633af4SJohn Birrell } 812c633af4SJohn Birrell 822c633af4SJohn Birrell int 832c633af4SJohn Birrell proc_setflags(struct proc_handle *phdl, int mask) 842c633af4SJohn Birrell { 852c633af4SJohn Birrell if (phdl == NULL) 862c633af4SJohn Birrell return (EINVAL); 872c633af4SJohn Birrell 882c633af4SJohn Birrell phdl->flags |= mask; 892c633af4SJohn Birrell 902c633af4SJohn Birrell return (0); 912c633af4SJohn Birrell } 922c633af4SJohn Birrell 932c633af4SJohn Birrell int 942c633af4SJohn Birrell proc_state(struct proc_handle *phdl) 952c633af4SJohn Birrell { 962c633af4SJohn Birrell if (phdl == NULL) 972c633af4SJohn Birrell return (-1); 982c633af4SJohn Birrell 992c633af4SJohn Birrell return (phdl->status); 1002c633af4SJohn Birrell } 1012c633af4SJohn Birrell 1022c633af4SJohn Birrell int 1032c633af4SJohn Birrell proc_wait(struct proc_handle *phdl) 1042c633af4SJohn Birrell { 1052c633af4SJohn Birrell int status = 0; 1062c633af4SJohn Birrell struct kevent kev; 1072c633af4SJohn Birrell 1082c633af4SJohn Birrell if (phdl == NULL) 1092c633af4SJohn Birrell return (EINVAL); 1102c633af4SJohn Birrell 1112c633af4SJohn Birrell if (kevent(phdl->kq, NULL, 0, &kev, 1, NULL) <= 0) 1122c633af4SJohn Birrell return (0); 1132c633af4SJohn Birrell 1142c633af4SJohn Birrell switch (kev.filter) { 1152c633af4SJohn Birrell /* Child has exited */ 1162c633af4SJohn Birrell case EVFILT_PROC: /* target has exited */ 1172c633af4SJohn Birrell phdl->status = PS_UNDEAD; 1182c633af4SJohn Birrell break; 1192c633af4SJohn Birrell default: 1202c633af4SJohn Birrell break; 1212c633af4SJohn Birrell } 1222c633af4SJohn Birrell 1232c633af4SJohn Birrell return (status); 1242c633af4SJohn Birrell } 1252c633af4SJohn Birrell 1262c633af4SJohn Birrell pid_t 1272c633af4SJohn Birrell proc_getpid(struct proc_handle *phdl) 1282c633af4SJohn Birrell { 1292c633af4SJohn Birrell if (phdl == NULL) 1302c633af4SJohn Birrell return (-1); 1312c633af4SJohn Birrell 1322c633af4SJohn Birrell return (phdl->pid); 1332c633af4SJohn Birrell } 134