2 * Copryight 1997 Sean Eric Fagan
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Sean Eric Fagan
15 * 4. Neither the name of the author may be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: src/usr.bin/truss/setup.c,v 1.10.2.2 2002/02/15 11:43:51 des Exp $
32 * $DragonFly: src/usr.bin/truss/setup.c,v 1.2 2003/06/17 04:29:33 dillon Exp $
36 * Various setup functions for truss. Not the cleanest-written code,
40 #include <sys/param.h>
41 #include <sys/ioctl.h>
42 #include <sys/pioctl.h>
56 static int evflags = 0;
59 * setup_and_wait() is called to start a process. All it really does
60 * is fork(), set itself up to stop on exec or exit, and then exec
61 * the given command. At that point, the child process stops, and
62 * the parent can wake up and deal with it.
66 setup_and_wait(char *command[]) {
67 struct procfs_status pfs;
75 err(1, "fork failed");
77 if (pid == 0) { /* Child */
78 int mask = S_EXEC | S_EXIT;
79 fd = open("/proc/curproc/mem", O_WRONLY);
81 err(2, "cannot open /proc/curproc/mem");
82 fcntl(fd, F_SETFD, 1);
83 if (ioctl(fd, PIOCBIS, mask) == -1)
87 * The PF_LINGER flag tells procfs not to wake up the
88 * process on last close; normally, this is the behaviour
91 if (ioctl(fd, PIOCSFL, flags) == -1)
92 warn("cannot set PF_LINGER");
93 execvp(command[0], command);
95 ioctl(fd, PIOCBIC, ~0);
96 err(4, "execvp %s", command[0]);
98 /* Only in the parent here */
100 if (waitpid(pid, NULL, WNOHANG) != 0) {
102 * Process exited before it got to us -- meaning the exec failed
103 * miserably -- so we just quietly exit.
108 sprintf(buf, "/proc/%d/mem", pid);
109 if ((fd = open(buf, O_RDWR)) == -1)
110 err(5, "cannot open %s", buf);
111 if (ioctl(fd, PIOCWAIT, &pfs) == -1)
113 if (pfs.why == S_EXIT) {
114 fprintf(stderr, "process exited before exec'ing\n");
115 ioctl(fd, PIOCCONT, 0);
124 * start_tracing picks up where setup_and_wait() dropped off -- namely,
125 * it sets the event mask for the given process id. Called for both
126 * monitoring an existing process and when we create our own.
130 start_tracing(int pid, int flags) {
133 struct procfs_status tmp;
134 sprintf(buf, "/proc/%d/mem", pid);
136 fd = open(buf, O_RDWR);
139 * The process may have run away before we could start -- this
140 * happens with SUGID programs. So we need to see if it still
141 * exists before we complain bitterly.
143 if (kill(pid, 0) == -1)
145 err(8, "cannot open %s", buf);
148 if (ioctl(fd, PIOCSTATUS, &tmp) == -1) {
149 err(10, "cannot get procfs status struct");
151 evflags = tmp.events;
153 if (ioctl(fd, PIOCBIS, flags) == -1)
154 err(9, "cannot set procfs event bit mask");
157 * This clears the PF_LINGER set above in setup_and_wait();
158 * if truss happens to die before this, then the process
159 * needs to be woken up via procctl.
162 if (ioctl(fd, PIOCSFL, 0) == -1)
163 warn("cannot clear PF_LINGER");
169 * Restore a process back to it's pre-truss state.
170 * Called for SIGINT, SIGTERM, SIGQUIT. This only
171 * applies if truss was told to monitor an already-existing
175 restore_proc(int signo __unused) {
178 ioctl(Procfd, PIOCBIC, ~0);
180 ioctl(Procfd, PIOCBIS, evflags);