Debian Codegraph @

staprun/start_cmd.c

raw
1 /* -*- linux-c -*-
2 *
3 * start_cmd - cleanly launch a child process
4 *
5 * This file is part of systemtap, and is free software. You can
6 * redistribute it and/or modify it under the terms of the GNU General
7 * Public License (GPL); either version 2, or (at your option) any
8 * later version.
9 *
10 * Copyright (C) 2005-2021 Red Hat Inc.
11 */
12
13 #include "staprun.h"
14 #include <sys/utsname.h>
15 #include <sys/ptrace.h>
16 #include <sys/select.h>
17 #include <limits.h>
18 #include <search.h>
19 #include <wordexp.h>
20
21
22 #define WORKAROUND_BZ467568 1 /* PR 6964; XXX: autoconf when able; also in mainloop.c */
23
24 #ifndef OPEN_MAX
25 #define OPEN_MAX 256
26 #endif
27
28 void
29 closefrom(int lowfd)
30 {
31 long fd, maxfd;
32 char *endp;
33 struct dirent *dent;
34 DIR *dirp;
35
36 /* Check for a /proc/self/fd directory. */
37 if ((dirp = opendir("/proc/self/fd"))) {
38 int dir_fd = dirfd(dirp);
39 while ((dent = readdir(dirp)) != NULL) {
40 fd = strtol(dent->d_name, &endp, 10);
41 if (dent->d_name != endp && *endp == '\0'
42 && fd >= 0 && fd < INT_MAX && fd >= lowfd
43 && fd != dir_fd)
44 (void) close((int)fd);
45 }
46 (void) closedir(dirp);
47 }
48 else {
49 /*
50 * Here we fall back on sysconf(). Why? It is possible
51 * /proc isn't mounted, we're out of file descriptors,
52 * etc., which could cause the opendir() to fail. Also
53 * note thet it is possible to open a file descriptor
54 * and then drop the rlimit such that it is below the
55 * open fd.
56 */
57 maxfd = sysconf(_SC_OPEN_MAX);
58 if (maxfd < 0)
59 maxfd = OPEN_MAX;
60
61 for (fd = lowfd; fd < maxfd; fd++)
62 (void) close((int) fd);
63 }
64 }
65
66 #if WORKAROUND_BZ467568
67 /* When a SIGUSR1 signal arrives, set this variable. */
68 volatile sig_atomic_t usr1_interrupt = 0;
69
70 static void signal_usr1(int signum)
71 {
72 (void) signum;
73 usr1_interrupt = 1;
74 }
75 #else
76 static void signal_usr1(int signum)
77 {
78 (void) signum;
79 }
80 #endif /* WORKAROUND_BZ467568 */
81
82 /*
83 * start_cmd forks the command given on the command line with the "-c"
84 * option. It will wait just at the cusp of the exec until the
85 * stap module is ready to let it run. We do it this way because we
86 * must have the pid of the forked command so it can be set to the
87 * module and made available internally as _stp_target. PTRACE_DETACH
88 * is sent from resume_cmd() below when it receives STP_START from
89 * the module.
90 */
91 void start_cmd(void)
92 {
93 pid_t pid;
94 struct sigaction a;
95 #if WORKAROUND_BZ467568
96 struct sigaction usr1_action, old_action;
97 sigset_t blockmask, oldmask;
98 #endif /* WORKAROUND_BZ467568 */
99
100 /* if we are execing a target cmd, ignore ^C in stapio */
101 /* and let the target cmd get it. */
102 memset(&a, 0, sizeof(a));
103 sigemptyset(&a.sa_mask);
104 a.sa_flags = 0;
105 a.sa_handler = SIG_IGN;
106 sigaction(SIGINT, &a, NULL);
107
108 #if WORKAROUND_BZ467568
109 /* Set up the mask of signals to temporarily block. */
110 sigemptyset (&blockmask);
111 sigaddset (&blockmask, SIGUSR1);
112
113 /* Establish the SIGUSR1 signal handler. */
114 memset(&usr1_action, 0, sizeof(usr1_action));
115 sigfillset (&usr1_action.sa_mask);
116 usr1_action.sa_flags = 0;
117 usr1_action.sa_handler = signal_usr1;
118 sigaction (SIGUSR1, &usr1_action, &old_action);
119
120 /* Block SIGUSR1 */
121 sigprocmask(SIG_BLOCK, &blockmask, &oldmask);
122 #endif /* WORKAROUND_BZ467568 */
123
124 if ((pid = fork()) < 0) {
125 _perr("fork");
126 exit(1);
127 } else if (pid == 0) {
128 /* We're in the target process.
129 * Let's start the execve of target_cmd. */
130 int rc;
131 wordexp_t words;
132 char *sh_c_argv[4] = { NULL, NULL, NULL, NULL };
133
134 a.sa_handler = SIG_DFL;
135 sigaction(SIGINT, &a, NULL);
136
137 /* Close any FDs we still hold, similarly as though this were
138 * a program being spawned due to an system("") tapset function. */
139 closefrom(3);
140
141 /* We could call closefrom() here, to make sure we don't leak any
142 * fds to the target, but it really isn't needed here since
143 * close-on-exec should catch everything. We don't have the
144 * synchronizations issues here we have with system_cmd(). */
145
146 /* Formerly, we just execl'd(sh,-c,$target_cmd). But this does't
147 work well if target_cmd is a shell builtin. We really want to
148 probe a new child process, not a mishmash of shell-interpreted
149 stuff. */
150 rc = wordexp (target_cmd, & words, WRDE_NOCMD|WRDE_UNDEF);
151 if (rc == WRDE_BADCHAR)
152 {
153 /* The user must have used a shell metacharacter, thinking that
154 we use system(3) to evaluate 'stap -c CMD'. We could generate
155 an error message ... but let's just do what the user meant.
156 rhbz 467652. */
157 sh_c_argv[0] = "sh";
158 sh_c_argv[1] = "-c";
159 sh_c_argv[2] = target_cmd;
160 sh_c_argv[3] = NULL;
161
162 if (read_stdin)
163 {
164 /* close target_cmd's stdin to prevent a data race */
165 char *buf = malloc(sizeof(char) * (strlen(target_cmd) + 13));
166 if (buf == NULL)
167 {
168 _err (_("Failed to allocate memory.\n"));
169 _exit(1);
170 }
171 sprintf(buf, "%s < /dev/null", target_cmd);
172 target_cmd = buf;
173 }
174 }
175 else
176 {
177 switch (rc)
178 {
179 case 0:
180 break;
181 case WRDE_SYNTAX:
182 _err (_("wordexp: syntax error (unmatched quotes?) in -c COMMAND\n"));
183 _exit(1);
184 default:
185 _err (_("wordexp: parsing error (%d)\n"), rc);
186 _exit (1);
187 }
188 if (words.we_wordc < 1) { _err ("empty -c COMMAND"); _exit (1); }
189 }
190
191 /* PR 6964: when tracing all the user space process including the
192 child the signal will be messed due to uprobe module or utrace
193 bug. The kernel sometimes crashes. So as an alternative
194 approximation, we just wait here for a signal from the parent. */
195
196 dbug(1, "blocking briefly\n");
197 alarm(60); /* but not indefinitely */
198
199 #if WORKAROUND_BZ467568
200 {
201 /* Wait for the SIGUSR1 */
202 while (!usr1_interrupt)
203 sigsuspend(&oldmask);
204
205 /* Restore the old SIGUSR1 signal handler. */
206 sigaction (SIGUSR1, &old_action, NULL);
207
208 /* Restore the original signal mask */
209 sigprocmask(SIG_SETMASK, &oldmask, NULL);
210 }
211 #else /* !WORKAROUND_BZ467568 */
212 rc = ptrace (PTRACE_TRACEME, 0, 0, 0);
213 if (rc < 0) perror ("ptrace me");
214 raise (SIGCONT); /* Harmless; just passes control to parent. */
215 #endif /* !WORKAROUND_BZ467568 */
216
217 alarm(0); /* clear alarms */
218 dbug(1, "execing target_cmd %s\n", target_cmd);
219
220 /* Note that execvp() is not a direct system call; it does a $PATH
221 search in glibc. We would like to filter out these dummy syscalls
222 from the utrace events seen by scripts.
223
224 This filtering would be done for us for free, if we used ptrace
225 ... but see PR6964. XXX: Instead, we could open-code the
226 $PATH search here; put the pause() afterward; and run a direct
227 execve instead of execvp(). */
228
229 if (execvp ((sh_c_argv[0] == NULL ? words.we_wordv[0] : sh_c_argv[0]),
230 (sh_c_argv[0] == NULL ? words.we_wordv : sh_c_argv)) < 0)
231 perror(target_cmd);
232
233 /* (There is no need to wordfree() words; they are or will be gone.) */
234
235 _exit(1);
236 } else {
237 /* We're in the parent. The child will parse target_cmd and
238 execv() the result. It will be stopped thereabouts and send us
239 a SIGTRAP. Or rather, due to PR 6964, it will stop itself and wait for
240 us to release it. */
241 target_pid = pid;
242 #if WORKAROUND_BZ467568
243 /* Restore the old SIGUSR1 signal handler. */
244 sigaction (SIGUSR1, &old_action, NULL);
245
246 /* Restore the original signal mask */
247 sigprocmask(SIG_SETMASK, &oldmask, NULL);
248 #else /* !WORKAROUND_BZ467568 */
249 int status;
250 waitpid (target_pid, &status, 0);
251 dbug(1, "waited for target_cmd %s pid %d status %x\n", target_cmd, target_pid, (unsigned) status);
252 #endif /* !WORKAROUND_BZ467568 */
253 }
254 }
255
256 /*
257 * resume_cmd signals the command started by start_cmd()
258 * and identified by target_pid to continue running.
259 * Returns 0 on success, negative PTRACE_DETACH error code on failure. */
260 int resume_cmd(void)
261 {
262 #if WORKAROUND_BZ467568
263 /* Let's just send our pet signal to the child
264 process that should be waiting for us, mid-pause(). */
265 kill (target_pid, SIGUSR1);
266 #else
267 /* Were it not for PR6964, we'd like to do it this way: */
268 int rc = ptrace (PTRACE_DETACH, target_pid, 0, 0);
269 if (rc < 0)
270 {
271 perror (_("ptrace detach"));
272 if (target_cmd)
273 kill(target_pid, SIGKILL);
274 return rc;
275 }
276 #endif
277 return 0;
278 }