blob: 16efd4da1e0cee1e2ec21877876d9bcfd3428ef3 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
njnf39e9a32005-06-12 02:43:17 +00003/*--- Process-related libc stuff. m_libcproc.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00004/*--------------------------------------------------------------------*/
njned6b8242005-06-01 00:03:17 +00005
sewardjde4a1d02002-03-22 01:27:54 +00006/*
njnb9c427c2004-12-01 14:14:42 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
sewardjde4a1d02002-03-22 01:27:54 +00009
sewardje4b0bf02006-06-05 23:21:15 +000010 Copyright (C) 2000-2006 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000011 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000012
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
njn25e49d8e72002-09-23 09:36:25 +000028 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000029*/
30
njnc7561b92005-06-19 01:24:32 +000031#include "pub_core_basics.h"
njn97405b22005-06-02 03:39:33 +000032#include "pub_core_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000033#include "pub_core_libcassert.h"
njn36a20fa2005-06-03 03:08:39 +000034#include "pub_core_libcprint.h"
njnf39e9a32005-06-12 02:43:17 +000035#include "pub_core_libcproc.h"
njnaf1d7df2005-06-11 01:31:52 +000036#include "pub_core_mallocfree.h"
njn9abd6082005-06-17 21:31:45 +000037#include "pub_core_syscall.h"
sewardj45f4e7c2005-09-27 19:20:21 +000038#include "pub_core_clientstate.h"
sewardj4eee4762006-10-14 15:51:32 +000039#include "pub_core_vkiscnums.h"
sewardj1cf558c2005-04-25 01:36:56 +000040
njnf39e9a32005-06-12 02:43:17 +000041/* ---------------------------------------------------------------------
42 Command line and environment stuff
43 ------------------------------------------------------------------ */
sewardjde4a1d02002-03-22 01:27:54 +000044
njnf39e9a32005-06-12 02:43:17 +000045/* As deduced from sp_at_startup, the client's argc, argv[] and
46 envp[] as extracted from the client's stack at startup-time. */
njnf39e9a32005-06-12 02:43:17 +000047Char** VG_(client_envp);
48
njn8d9965c2005-06-12 17:47:24 +000049/* Path to library directory */
50const Char *VG_(libdir) = VG_LIBDIR;
51
njnf39e9a32005-06-12 02:43:17 +000052/* We do getenv without libc's help by snooping around in
53 VG_(client_envp) as determined at startup time. */
54Char *VG_(getenv)(Char *varname)
jsgf855d93d2003-10-13 22:26:55 +000055{
njnf39e9a32005-06-12 02:43:17 +000056 Int i, n;
57 n = VG_(strlen)(varname);
58 for (i = 0; VG_(client_envp)[i] != NULL; i++) {
59 Char* s = VG_(client_envp)[i];
60 if (VG_(strncmp)(varname, s, n) == 0 && s[n] == '=') {
61 return & s[n+1];
thughes8ef6d962004-10-16 10:46:01 +000062 }
63 }
njnf39e9a32005-06-12 02:43:17 +000064 return NULL;
nethercoteff9721d2004-01-26 17:10:01 +000065}
66
fitzhardinge98abfc72003-12-16 02:05:15 +000067void VG_(env_unsetenv) ( Char **env, const Char *varname )
68{
69 Char **from;
70 Char **to = NULL;
71 Int len = VG_(strlen)(varname);
72
sewardj45f4e7c2005-09-27 19:20:21 +000073 for (from = to = env; from && *from; from++) {
fitzhardinge98abfc72003-12-16 02:05:15 +000074 if (!(VG_(strncmp)(varname, *from, len) == 0 && (*from)[len] == '=')) {
75 *to = *from;
76 to++;
77 }
78 }
79 *to = *from;
80}
81
82/* set the environment; returns the old env if a new one was allocated */
83Char **VG_(env_setenv) ( Char ***envp, const Char* varname, const Char *val )
84{
85 Char **env = (*envp);
86 Char **cpp;
87 Int len = VG_(strlen)(varname);
88 Char *valstr = VG_(arena_malloc)(VG_AR_CORE, len + VG_(strlen)(val) + 2);
89 Char **oldenv = NULL;
90
91 VG_(sprintf)(valstr, "%s=%s", varname, val);
92
sewardj45f4e7c2005-09-27 19:20:21 +000093 for (cpp = env; cpp && *cpp; cpp++) {
fitzhardinge98abfc72003-12-16 02:05:15 +000094 if (VG_(strncmp)(varname, *cpp, len) == 0 && (*cpp)[len] == '=') {
95 *cpp = valstr;
96 return oldenv;
97 }
98 }
99
100 if (env == NULL) {
101 env = VG_(arena_malloc)(VG_AR_CORE, sizeof(Char **) * 2);
102 env[0] = valstr;
103 env[1] = NULL;
104
105 *envp = env;
106
107 } else {
108 Int envlen = (cpp-env) + 2;
109 Char **newenv = VG_(arena_malloc)(VG_AR_CORE, envlen * sizeof(Char **));
110
sewardj45f4e7c2005-09-27 19:20:21 +0000111 for (cpp = newenv; *env; )
fitzhardinge98abfc72003-12-16 02:05:15 +0000112 *cpp++ = *env++;
113 *cpp++ = valstr;
114 *cpp++ = NULL;
115
116 oldenv = *envp;
117
118 *envp = newenv;
119 }
120
121 return oldenv;
122}
123
nethercote60a96c52004-08-03 13:08:31 +0000124/* Walk through a colon-separated environment variable, and remove the
125 entries which match remove_pattern. It slides everything down over
126 the removed entries, and pads the remaining space with '\0'. It
127 modifies the entries in place (in the client address space), but it
128 shouldn't matter too much, since we only do this just before an
129 execve().
130
131 This is also careful to mop up any excess ':'s, since empty strings
132 delimited by ':' are considered to be '.' in a path.
133*/
134static void mash_colon_env(Char *varp, const Char *remove_pattern)
135{
136 Char *const start = varp;
137 Char *entry_start = varp;
138 Char *output = varp;
139
140 if (varp == NULL)
141 return;
142
143 while(*varp) {
144 if (*varp == ':') {
145 Char prev;
146 Bool match;
147
148 /* This is a bit subtle: we want to match against the entry
149 we just copied, because it may have overlapped with
150 itself, junking the original. */
151
152 prev = *output;
153 *output = '\0';
154
155 match = VG_(string_match)(remove_pattern, entry_start);
156
157 *output = prev;
158
159 if (match) {
160 output = entry_start;
161 varp++; /* skip ':' after removed entry */
162 } else
163 entry_start = output+1; /* entry starts after ':' */
164 }
165
166 *output++ = *varp++;
167 }
168
169 /* match against the last entry */
170 if (VG_(string_match)(remove_pattern, entry_start)) {
171 output = entry_start;
172 if (output > start) {
173 /* remove trailing ':' */
174 output--;
175 vg_assert(*output == ':');
176 }
177 }
178
179 /* pad out the left-overs with '\0' */
180 while(output < varp)
181 *output++ = '\0';
182}
183
184
185// Removes all the Valgrind-added stuff from the passed environment. Used
186// when starting child processes, so they don't see that added stuff.
187void VG_(env_remove_valgrind_env_stuff)(Char** envp)
188{
189 Int i;
190 Char* ld_preload_str = NULL;
191 Char* ld_library_path_str = NULL;
192 Char* buf;
193
194 // Find LD_* variables
195 for (i = 0; envp[i] != NULL; i++) {
196 if (VG_(strncmp)(envp[i], "LD_PRELOAD=", 11) == 0)
197 ld_preload_str = &envp[i][11];
198 if (VG_(strncmp)(envp[i], "LD_LIBRARY_PATH=", 16) == 0)
199 ld_library_path_str = &envp[i][16];
200 }
201
202 buf = VG_(arena_malloc)(VG_AR_CORE, VG_(strlen)(VG_(libdir)) + 20);
203
204 // Remove Valgrind-specific entries from LD_*.
nethercote60a96c52004-08-03 13:08:31 +0000205 VG_(sprintf)(buf, "%s*/vgpreload_*.so", VG_(libdir));
206 mash_colon_env(ld_preload_str, buf);
207 VG_(sprintf)(buf, "%s*", VG_(libdir));
208 mash_colon_env(ld_library_path_str, buf);
209
sewardj45f4e7c2005-09-27 19:20:21 +0000210 // Remove VALGRIND_LAUNCHER variable.
211 VG_(env_unsetenv)(envp, VALGRIND_LAUNCHER);
nethercote60a96c52004-08-03 13:08:31 +0000212
213 // XXX if variable becomes empty, remove it completely?
214
215 VG_(arena_free)(VG_AR_CORE, buf);
216}
217
njnf39e9a32005-06-12 02:43:17 +0000218/* ---------------------------------------------------------------------
219 Various important syscall wrappers
220 ------------------------------------------------------------------ */
221
222Int VG_(waitpid)(Int pid, Int *status, Int options)
223{
224 SysRes res = VG_(do_syscall4)(__NR_wait4, pid, (UWord)status, options, 0);
225 return res.isError ? -1 : res.val;
226}
227
njnf39e9a32005-06-12 02:43:17 +0000228/* clone the environment */
tom4e136952005-07-18 14:02:11 +0000229Char **VG_(env_clone) ( Char **oldenv )
njnf39e9a32005-06-12 02:43:17 +0000230{
231 Char **oldenvp;
232 Char **newenvp;
233 Char **newenv;
234 Int envlen;
235
236 for (oldenvp = oldenv; oldenvp && *oldenvp; oldenvp++);
237
238 envlen = oldenvp - oldenv + 1;
239
240 newenv = VG_(arena_malloc)(VG_AR_CORE, envlen * sizeof(Char **));
241
242 oldenvp = oldenv;
243 newenvp = newenv;
244
245 while (oldenvp && *oldenvp) {
246 *newenvp++ = *oldenvp++;
247 }
248
249 *newenvp = *oldenvp;
250
251 return newenv;
252}
253
sewardje6a25242002-04-21 22:03:07 +0000254/* Return -1 if error, else 0. NOTE does not indicate return code of
255 child! */
256Int VG_(system) ( Char* cmd )
257{
sewardja8d8e232005-06-07 20:04:56 +0000258 Int pid;
259 SysRes res;
sewardje6a25242002-04-21 22:03:07 +0000260 if (cmd == NULL)
261 return 1;
sewardja8d8e232005-06-07 20:04:56 +0000262 res = VG_(do_syscall0)(__NR_fork);
263 if (res.isError)
sewardje6a25242002-04-21 22:03:07 +0000264 return -1;
sewardja8d8e232005-06-07 20:04:56 +0000265 pid = res.val;
sewardje6a25242002-04-21 22:03:07 +0000266 if (pid == 0) {
267 /* child */
nethercoteff9721d2004-01-26 17:10:01 +0000268 static Char** envp = NULL;
sewardje6a25242002-04-21 22:03:07 +0000269 Char* argv[4];
nethercoteff9721d2004-01-26 17:10:01 +0000270
fitzhardingeb50068f2004-02-24 23:42:55 +0000271 /* restore the DATA rlimit for the child */
272 VG_(setrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data));
273
tom4e136952005-07-18 14:02:11 +0000274 envp = VG_(env_clone)(VG_(client_envp));
nethercote60a96c52004-08-03 13:08:31 +0000275 VG_(env_remove_valgrind_env_stuff)( envp );
nethercoteff9721d2004-01-26 17:10:01 +0000276
sewardje6a25242002-04-21 22:03:07 +0000277 argv[0] = "/bin/sh";
278 argv[1] = "-c";
279 argv[2] = cmd;
280 argv[3] = 0;
nethercoteff9721d2004-01-26 17:10:01 +0000281
njnca6fef02004-11-29 16:49:18 +0000282 (void)VG_(do_syscall3)(__NR_execve,
283 (UWord)"/bin/sh", (UWord)argv, (UWord)envp);
nethercoteff9721d2004-01-26 17:10:01 +0000284
sewardje6a25242002-04-21 22:03:07 +0000285 /* If we're still alive here, execve failed. */
fitzhardingeabab8392004-03-02 21:38:51 +0000286 VG_(exit)(1);
sewardje6a25242002-04-21 22:03:07 +0000287 } else {
288 /* parent */
sewardja8d8e232005-06-07 20:04:56 +0000289 Int zzz = VG_(waitpid)(pid, NULL, 0);
290 return zzz == -1 ? -1 : 0;
sewardje6a25242002-04-21 22:03:07 +0000291 }
292}
293
njnf39e9a32005-06-12 02:43:17 +0000294/* ---------------------------------------------------------------------
295 Resource limits
296 ------------------------------------------------------------------ */
297
njnf39e9a32005-06-12 02:43:17 +0000298/* Support for getrlimit. */
299Int VG_(getrlimit) (Int resource, struct vki_rlimit *rlim)
300{
301 SysRes res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
302 /* res = getrlimit( resource, rlim ); */
303# ifdef __NR_ugetrlimit
304 res = VG_(do_syscall2)(__NR_ugetrlimit, resource, (UWord)rlim);
305# endif
306 if (res.isError && res.val == VKI_ENOSYS)
307 res = VG_(do_syscall2)(__NR_getrlimit, resource, (UWord)rlim);
308 return res.isError ? -1 : res.val;
309}
310
311
312/* Support for setrlimit. */
313Int VG_(setrlimit) (Int resource, const struct vki_rlimit *rlim)
314{
315 SysRes res;
316 /* res = setrlimit( resource, rlim ); */
317 res = VG_(do_syscall2)(__NR_setrlimit, resource, (UWord)rlim);
318 return res.isError ? -1 : res.val;
319}
sewardje6a25242002-04-21 22:03:07 +0000320
sewardjde4a1d02002-03-22 01:27:54 +0000321/* ---------------------------------------------------------------------
njnf39e9a32005-06-12 02:43:17 +0000322 pids, etc
323 ------------------------------------------------------------------ */
324
325Int VG_(gettid)(void)
326{
327 SysRes res = VG_(do_syscall0)(__NR_gettid);
328
329 if (res.isError && res.val == VKI_ENOSYS) {
330 Char pid[16];
331 /*
332 * The gettid system call does not exist. The obvious assumption
333 * to make at this point would be that we are running on an older
334 * system where the getpid system call actually returns the ID of
335 * the current thread.
336 *
337 * Unfortunately it seems that there are some systems with a kernel
338 * where getpid has been changed to return the ID of the thread group
339 * leader but where the gettid system call has not yet been added.
340 *
341 * So instead of calling getpid here we use readlink to see where
342 * the /proc/self link is pointing...
343 */
344
345 res = VG_(do_syscall3)(__NR_readlink, (UWord)"/proc/self",
346 (UWord)pid, sizeof(pid));
347 if (!res.isError && res.val > 0) {
348 pid[res.val] = '\0';
349 res.val = VG_(atoll)(pid);
350 }
351 }
352
353 return res.val;
354}
355
356/* You'd be amazed how many places need to know the current pid. */
357Int VG_(getpid) ( void )
358{
359 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
360 return VG_(do_syscall0)(__NR_getpid) . val;
361}
362
363Int VG_(getpgrp) ( void )
364{
365 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
366 return VG_(do_syscall0)(__NR_getpgrp) . val;
367}
368
369Int VG_(getppid) ( void )
370{
371 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
372 return VG_(do_syscall0)(__NR_getppid) . val;
373}
374
sewardj45f4e7c2005-09-27 19:20:21 +0000375Int VG_(geteuid) ( void )
njnf39e9a32005-06-12 02:43:17 +0000376{
377 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
sewardj45f4e7c2005-09-27 19:20:21 +0000378 return VG_(do_syscall0)(__NR_geteuid) . val;
379}
380
381Int VG_(getegid) ( void )
382{
383 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
384 return VG_(do_syscall0)(__NR_getegid) . val;
385}
386
387/* Get supplementary groups into list[0 .. size-1]. Returns the
388 number of groups written, or -1 if error. Note that in order to be
389 portable, the groups are 32-bit unsigned ints regardless of the
390 platform. */
391Int VG_(getgroups)( Int size, UInt* list )
392{
sewardja48a4932005-09-29 11:09:56 +0000393# if defined(VGP_x86_linux) || defined(VGP_ppc32_linux)
sewardj45f4e7c2005-09-27 19:20:21 +0000394 Int i;
395 SysRes sres;
sewardj3ae3dd32005-12-27 02:08:03 +0000396 UShort list16[64];
sewardj45f4e7c2005-09-27 19:20:21 +0000397 if (size < 0) return -1;
sewardj3ae3dd32005-12-27 02:08:03 +0000398 if (size > 64) size = 64;
sewardj45f4e7c2005-09-27 19:20:21 +0000399 sres = VG_(do_syscall2)(__NR_getgroups, size, (Addr)list16);
400 if (sres.isError)
401 return -1;
sewardj3ae3dd32005-12-27 02:08:03 +0000402 if (sres.val > size)
sewardj45f4e7c2005-09-27 19:20:21 +0000403 return -1;
sewardj3ae3dd32005-12-27 02:08:03 +0000404 for (i = 0; i < sres.val; i++)
sewardj45f4e7c2005-09-27 19:20:21 +0000405 list[i] = (UInt)list16[i];
sewardj3ae3dd32005-12-27 02:08:03 +0000406 return sres.val;
sewardj45f4e7c2005-09-27 19:20:21 +0000407
sewardj2c48c7b2005-11-29 13:05:56 +0000408# elif defined(VGP_amd64_linux) || defined(VGP_ppc64_linux)
sewardj45f4e7c2005-09-27 19:20:21 +0000409 SysRes sres;
410 sres = VG_(do_syscall2)(__NR_getgroups, size, (Addr)list);
411 if (sres.isError)
412 return -1;
413 return sres.val;
414
415# else
416# error "VG_(getgroups): needs implementation on this platform"
417# endif
418}
419
420/* ---------------------------------------------------------------------
421 Process tracing
422 ------------------------------------------------------------------ */
423
424Int VG_(ptrace) ( Int request, Int pid, void *addr, void *data )
425{
426 SysRes res;
427 res = VG_(do_syscall4)(__NR_ptrace, request, pid, (UWord)addr, (UWord)data);
428 if (res.isError)
429 return -1;
430 return res.val;
431}
432
433/* ---------------------------------------------------------------------
434 Fork
435 ------------------------------------------------------------------ */
436
437Int VG_(fork) ( void )
438{
439 SysRes res;
440 res = VG_(do_syscall0)(__NR_fork);
441 if (res.isError)
442 return -1;
443 return res.val;
njnf39e9a32005-06-12 02:43:17 +0000444}
445
446/* ---------------------------------------------------------------------
447 Timing stuff
sewardj5f07b662002-04-23 16:52:51 +0000448 ------------------------------------------------------------------ */
449
sewardj5f07b662002-04-23 16:52:51 +0000450UInt VG_(read_millisecond_timer) ( void )
451{
fitzhardinge426f9e62004-01-25 03:44:18 +0000452 static ULong base = 0;
fitzhardinge66871692004-01-25 03:32:58 +0000453 struct vki_timeval tv_now;
sewardja8d8e232005-06-07 20:04:56 +0000454 ULong now;
455 SysRes res;
sewardj5f07b662002-04-23 16:52:51 +0000456
njnca6fef02004-11-29 16:49:18 +0000457 res = VG_(do_syscall2)(__NR_gettimeofday, (UWord)&tv_now, (UWord)NULL);
sewardj5f07b662002-04-23 16:52:51 +0000458
fitzhardinge66871692004-01-25 03:32:58 +0000459 now = tv_now.tv_sec * 1000000ULL + tv_now.tv_usec;
460
461 if (base == 0)
462 base = now;
sewardj5f07b662002-04-23 16:52:51 +0000463
fitzhardinge66871692004-01-25 03:32:58 +0000464 return (now - base) / 1000;
sewardj5f07b662002-04-23 16:52:51 +0000465}
466
467
sewardjb5f6f512005-03-10 23:59:00 +0000468void VG_(nanosleep)(struct vki_timespec *ts)
469{
sewardja8d8e232005-06-07 20:04:56 +0000470 (void)VG_(do_syscall2)(__NR_nanosleep, (UWord)ts, (UWord)NULL);
sewardjb5f6f512005-03-10 23:59:00 +0000471}
472
sewardj5f07b662002-04-23 16:52:51 +0000473/* ---------------------------------------------------------------------
njn310ed282005-06-26 15:11:37 +0000474 A trivial atfork() facility for Valgrind's internal use
sewardja8d8e232005-06-07 20:04:56 +0000475 ------------------------------------------------------------------ */
476
njn310ed282005-06-26 15:11:37 +0000477// Trivial because it only supports a single post-fork child action, which
478// is all we need.
sewardja8d8e232005-06-07 20:04:56 +0000479
njn310ed282005-06-26 15:11:37 +0000480static vg_atfork_t atfork_child = NULL;
sewardja8d8e232005-06-07 20:04:56 +0000481
njn310ed282005-06-26 15:11:37 +0000482void VG_(atfork_child)(vg_atfork_t child)
sewardja8d8e232005-06-07 20:04:56 +0000483{
njn310ed282005-06-26 15:11:37 +0000484 if (NULL != atfork_child)
485 VG_(core_panic)("More than one atfork_child handler requested");
sewardja8d8e232005-06-07 20:04:56 +0000486
njn310ed282005-06-26 15:11:37 +0000487 atfork_child = child;
sewardja8d8e232005-06-07 20:04:56 +0000488}
489
490void VG_(do_atfork_child)(ThreadId tid)
491{
njn310ed282005-06-26 15:11:37 +0000492 if (NULL != atfork_child)
493 (*atfork_child)(tid);
sewardja8d8e232005-06-07 20:04:56 +0000494}
495
sewardjde4a1d02002-03-22 01:27:54 +0000496/*--------------------------------------------------------------------*/
njnc6168192004-11-29 13:54:10 +0000497/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +0000498/*--------------------------------------------------------------------*/