Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / truss / setup.c
1 /*
2  * Copryight 1997 Sean Eric Fagan
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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
17  *    permission.
18  *
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
29  * SUCH DAMAGE.
30  */
31
32 #ifndef lint
33 static const char rcsid[] =
34   "$FreeBSD: src/usr.bin/truss/setup.c,v 1.10.2.2 2002/02/15 11:43:51 des Exp $";
35 #endif /* not lint */
36
37 /*
38  * Various setup functions for truss.  Not the cleanest-written code,
39  * I'm afraid.
40  */
41
42 #include <sys/param.h>
43 #include <sys/ioctl.h>
44 #include <sys/pioctl.h>
45 #include <sys/wait.h>
46
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #include "extern.h"
57
58 static int evflags = 0;
59
60 /*
61  * setup_and_wait() is called to start a process.  All it really does
62  * is fork(), set itself up to stop on exec or exit, and then exec
63  * the given command.  At that point, the child process stops, and
64  * the parent can wake up and deal with it.
65  */
66
67 int
68 setup_and_wait(char *command[]) {
69   struct procfs_status pfs;
70   char buf[32];
71   int fd;
72   int pid;
73   int flags;
74
75   pid = fork();
76   if (pid == -1) {
77     err(1, "fork failed");
78   }
79   if (pid == 0) {       /* Child */
80     int mask = S_EXEC | S_EXIT;
81     fd = open("/proc/curproc/mem", O_WRONLY);
82     if (fd == -1)
83       err(2, "cannot open /proc/curproc/mem");
84     fcntl(fd, F_SETFD, 1);
85     if (ioctl(fd, PIOCBIS, mask) == -1)
86       err(3, "PIOCBIS");
87     flags = PF_LINGER;
88     /*
89      * The PF_LINGER flag tells procfs not to wake up the
90      * process on last close; normally, this is the behaviour
91      * we want.
92      */
93     if (ioctl(fd, PIOCSFL, flags) == -1)
94       warn("cannot set PF_LINGER");
95     execvp(command[0], command);
96     mask = ~0;
97     ioctl(fd, PIOCBIC, ~0);
98     err(4, "execvp %s", command[0]);
99   }
100   /* Only in the parent here */
101
102   if (waitpid(pid, NULL, WNOHANG) != 0) {
103     /*
104      * Process exited before it got to us -- meaning the exec failed
105      * miserably -- so we just quietly exit.
106      */
107     exit(1);
108   }
109
110   sprintf(buf, "/proc/%d/mem", pid);
111   if ((fd = open(buf, O_RDWR)) == -1)
112     err(5, "cannot open %s", buf);
113   if (ioctl(fd, PIOCWAIT, &pfs) == -1)
114     err(6, "PIOCWAIT");
115   if (pfs.why == S_EXIT) {
116     fprintf(stderr, "process exited before exec'ing\n");
117     ioctl(fd, PIOCCONT, 0);
118     wait(0);
119     exit(7);
120   }
121   close(fd);
122   return pid;
123 }
124
125 /*
126  * start_tracing picks up where setup_and_wait() dropped off -- namely,
127  * it sets the event mask for the given process id.  Called for both
128  * monitoring an existing process and when we create our own.
129  */
130
131 int
132 start_tracing(int pid, int flags) {
133   int fd;
134   char buf[32];
135   struct procfs_status tmp;
136   sprintf(buf, "/proc/%d/mem", pid);
137
138   fd = open(buf, O_RDWR);
139   if (fd == -1) {
140     /*
141      * The process may have run away before we could start -- this
142      * happens with SUGID programs.  So we need to see if it still
143      * exists before we complain bitterly.
144      */
145     if (kill(pid, 0) == -1)
146       return -1;
147     err(8, "cannot open %s", buf);
148   }
149
150   if (ioctl(fd, PIOCSTATUS, &tmp) == -1) {
151     err(10, "cannot get procfs status struct");
152   }
153   evflags = tmp.events;
154
155   if (ioctl(fd, PIOCBIS, flags) == -1)
156     err(9, "cannot set procfs event bit mask");
157
158   /*
159    * This clears the PF_LINGER set above in setup_and_wait();
160    * if truss happens to die before this, then the process
161    * needs to be woken up via procctl.
162    */
163
164   if (ioctl(fd, PIOCSFL, 0) == -1)
165     warn("cannot clear PF_LINGER");
166
167   return fd;
168 }
169
170 /*
171  * Restore a process back to it's pre-truss state.
172  * Called for SIGINT, SIGTERM, SIGQUIT.  This only
173  * applies if truss was told to monitor an already-existing
174  * process.
175  */
176 void
177 restore_proc(int signo __unused) {
178   extern int Procfd;
179
180   ioctl(Procfd, PIOCBIC, ~0);
181   if (evflags)
182     ioctl(Procfd, PIOCBIS, evflags);
183   exit(0);
184 }