blob: edf9b82eb6f837b4676ca5d940749bf13c894a75 [file] [log] [blame]
njneb8896b2005-06-04 20:03:55 +00001
2/*--------------------------------------------------------------------*/
3/*--- File- and socket-related libc stuff. m_libcfile.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2000-2005 Julian Seward
11 jseward@acm.org
12
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
28 The GNU General Public License is contained in the file COPYING.
29*/
30
njnc7561b92005-06-19 01:24:32 +000031#include "pub_core_basics.h"
njneb8896b2005-06-04 20:03:55 +000032#include "pub_core_libcbase.h"
33#include "pub_core_libcassert.h"
34#include "pub_core_libcfile.h"
njn351d0062005-06-21 22:23:59 +000035#include "pub_core_libcprint.h" // For VG_(sprintf)()
njnaf1d7df2005-06-11 01:31:52 +000036#include "pub_core_mallocfree.h"
njn9abd6082005-06-17 21:31:45 +000037#include "pub_core_syscall.h"
njneb8896b2005-06-04 20:03:55 +000038#include "vki_unistd.h"
39
40/* ---------------------------------------------------------------------
41 File stuff
42 ------------------------------------------------------------------ */
43
njn63004dc2005-06-10 22:39:04 +000044/* Application-visible file descriptor limits */
45Int VG_(fd_soft_limit) = -1;
46Int VG_(fd_hard_limit) = -1;
47
njneb8896b2005-06-04 20:03:55 +000048static inline Bool fd_exists(Int fd)
49{
50 struct vki_stat st;
51
52 return VG_(fstat)(fd, &st) == 0;
53}
54
55/* Move an fd into the Valgrind-safe range */
56Int VG_(safe_fd)(Int oldfd)
57{
58 Int newfd;
59
60 vg_assert(VG_(fd_hard_limit) != -1);
61
62 newfd = VG_(fcntl)(oldfd, VKI_F_DUPFD, VG_(fd_hard_limit));
63 if (newfd != -1)
64 VG_(close)(oldfd);
65
66 VG_(fcntl)(newfd, VKI_F_SETFD, VKI_FD_CLOEXEC);
67
68 vg_assert(newfd >= VG_(fd_hard_limit));
69 return newfd;
70}
71
njnae7359b2005-06-19 21:10:42 +000072/* Given a file descriptor, attempt to deduce its filename. To do
73 this, we use /proc/self/fd/<FD>. If this doesn't point to a file,
74 or if it doesn't exist, we just return NULL. The caller is
75 responsible for copying the contents of buf out immediately. */
76static HChar resolve_filename_buf[VKI_PATH_MAX];
77HChar* VG_(resolve_filename_nodup) ( Int fd )
78{
79 HChar tmp[64];
80
81 VG_(sprintf)(tmp, "/proc/self/fd/%d", fd);
82 VG_(memset)(resolve_filename_buf, 0, VKI_PATH_MAX);
83
84 if (VG_(readlink)(tmp, resolve_filename_buf, VKI_PATH_MAX) == -1)
85 return NULL;
86
87 return (resolve_filename_buf[0] == '/')
88 ? resolve_filename_buf
89 : NULL;
90}
91
92/* Same as resolve_filename_nodup, except that the result is copied
93 into new memory which the caller is responsible for freeing. */
94HChar* VG_(resolve_filename) ( Int fd )
95{
96 HChar* transient = VG_(resolve_filename_nodup)(fd);
97 return transient
98 ? VG_(arena_strdup)(VG_AR_CORE, transient)
99 : NULL;
100}
101
njneb8896b2005-06-04 20:03:55 +0000102/* Returns -1 on failure. */
103Int VG_(open) ( const Char* pathname, Int flags, Int mode )
104{
sewardja8d8e232005-06-07 20:04:56 +0000105 SysRes res = VG_(do_syscall3)(__NR_open, (UWord)pathname, flags, mode);
106 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +0000107}
108
109void VG_(close) ( Int fd )
110{
sewardja8d8e232005-06-07 20:04:56 +0000111 (void)VG_(do_syscall1)(__NR_close, fd);
njneb8896b2005-06-04 20:03:55 +0000112}
113
114Int VG_(read) ( Int fd, void* buf, Int count)
115{
sewardja8d8e232005-06-07 20:04:56 +0000116 SysRes res = VG_(do_syscall3)(__NR_read, fd, (UWord)buf, count);
117 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +0000118}
119
120Int VG_(write) ( Int fd, const void* buf, Int count)
121{
sewardja8d8e232005-06-07 20:04:56 +0000122 SysRes res = VG_(do_syscall3)(__NR_write, fd, (UWord)buf, count);
123 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +0000124}
125
126Int VG_(pipe) ( Int fd[2] )
127{
sewardja8d8e232005-06-07 20:04:56 +0000128 SysRes res = VG_(do_syscall1)(__NR_pipe, (UWord)fd);
129 return res.isError ? -1 : 0;
njneb8896b2005-06-04 20:03:55 +0000130}
131
132OffT VG_(lseek) ( Int fd, OffT offset, Int whence)
133{
sewardja8d8e232005-06-07 20:04:56 +0000134 SysRes res = VG_(do_syscall3)(__NR_lseek, fd, offset, whence);
135 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000136}
137
138Int VG_(stat) ( Char* file_name, struct vki_stat* buf )
139{
sewardja8d8e232005-06-07 20:04:56 +0000140 SysRes res = VG_(do_syscall2)(__NR_stat, (UWord)file_name, (UWord)buf);
141 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000142}
143
144Int VG_(fstat) ( Int fd, struct vki_stat* buf )
145{
sewardja8d8e232005-06-07 20:04:56 +0000146 SysRes res = VG_(do_syscall2)(__NR_fstat, fd, (UWord)buf);
147 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000148}
149
150Int VG_(dup2) ( Int oldfd, Int newfd )
151{
sewardja8d8e232005-06-07 20:04:56 +0000152 SysRes res = VG_(do_syscall2)(__NR_dup2, oldfd, newfd);
153 return res.isError ? (-1) : res.val;
njneb8896b2005-06-04 20:03:55 +0000154}
155
njn327fe8a2005-06-12 02:49:35 +0000156/* Returns -1 on error. */
157Int VG_(fcntl) ( Int fd, Int cmd, Int arg )
158{
159 SysRes res = VG_(do_syscall3)(__NR_fcntl, fd, cmd, arg);
160 return res.isError ? -1 : res.val;
161}
162
njneb8896b2005-06-04 20:03:55 +0000163Int VG_(rename) ( Char* old_name, Char* new_name )
164{
sewardja8d8e232005-06-07 20:04:56 +0000165 SysRes res = VG_(do_syscall2)(__NR_rename, (UWord)old_name, (UWord)new_name);
166 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000167}
168
169Int VG_(unlink) ( Char* file_name )
170{
sewardja8d8e232005-06-07 20:04:56 +0000171 SysRes res = VG_(do_syscall1)(__NR_unlink, (UWord)file_name);
172 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000173}
174
175/* Nb: we do not allow the Linux extension which malloc()s memory for the
176 buffer if buf==NULL, because we don't want Linux calling malloc() */
njn57ca7ab2005-06-21 23:44:58 +0000177Bool VG_(getcwd) ( Char* buf, SizeT size )
njneb8896b2005-06-04 20:03:55 +0000178{
sewardja8d8e232005-06-07 20:04:56 +0000179 SysRes res;
njneb8896b2005-06-04 20:03:55 +0000180 vg_assert(buf != NULL);
181 res = VG_(do_syscall2)(__NR_getcwd, (UWord)buf, size);
njn57ca7ab2005-06-21 23:44:58 +0000182 return res.isError ? False : True;
njneb8896b2005-06-04 20:03:55 +0000183}
184
185Int VG_(readlink) (Char* path, Char* buf, UInt bufsiz)
186{
sewardja8d8e232005-06-07 20:04:56 +0000187 SysRes res;
njneb8896b2005-06-04 20:03:55 +0000188 /* res = readlink( path, buf, bufsiz ); */
189 res = VG_(do_syscall3)(__NR_readlink, (UWord)path, (UWord)buf, bufsiz);
sewardja8d8e232005-06-07 20:04:56 +0000190 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +0000191}
192
193Int VG_(getdents) (UInt fd, struct vki_dirent *dirp, UInt count)
194{
sewardja8d8e232005-06-07 20:04:56 +0000195 SysRes res;
njneb8896b2005-06-04 20:03:55 +0000196 /* res = getdents( fd, dirp, count ); */
197 res = VG_(do_syscall3)(__NR_getdents, fd, (UWord)dirp, count);
sewardja8d8e232005-06-07 20:04:56 +0000198 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +0000199}
200
201/* ---------------------------------------------------------------------
202 Socket-related stuff. This is very Linux-kernel specific.
203 ------------------------------------------------------------------ */
204
205static
206Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port );
207
208static
209Int my_socket ( Int domain, Int type, Int protocol );
210
211static
212Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr,
213 Int addrlen );
214
215static
216UInt my_htonl ( UInt x )
217{
218 return
219 (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8)
220 | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24);
221}
222
223static
224UShort my_htons ( UShort x )
225{
226 return
227 (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8);
228}
229
230
231/* The main function.
232
233 Supplied string contains either an ip address "192.168.0.1" or
234 an ip address and port pair, "192.168.0.1:1500". Parse these,
235 and return:
236 -1 if there is a parse error
237 -2 if no parse error, but specified host:port cannot be opened
238 the relevant file (socket) descriptor, otherwise.
239 is used.
240*/
241Int VG_(connect_via_socket)( UChar* str )
242{
243 Int sd, res;
244 struct vki_sockaddr_in servAddr;
245 UInt ip = 0;
246 UShort port = VG_CLO_DEFAULT_LOGPORT;
247 Bool ok = parse_inet_addr_and_port(str, &ip, &port);
248 if (!ok)
249 return -1;
250
251 //if (0)
252 // VG_(printf)("ip = %d.%d.%d.%d, port %d\n",
253 // (ip >> 24) & 0xFF, (ip >> 16) & 0xFF,
254 // (ip >> 8) & 0xFF, ip & 0xFF,
255 // (UInt)port );
256
257 servAddr.sin_family = VKI_AF_INET;
258 servAddr.sin_addr.s_addr = my_htonl(ip);
259 servAddr.sin_port = my_htons(port);
260
261 /* create socket */
262 sd = my_socket(VKI_AF_INET, VKI_SOCK_STREAM, 0 /* IPPROTO_IP ? */);
263 if (sd < 0) {
264 /* this shouldn't happen ... nevertheless */
265 return -2;
266 }
267
268 /* connect to server */
269 res = my_connect(sd, (struct vki_sockaddr_in *) &servAddr,
270 sizeof(servAddr));
271 if (res < 0) {
272 /* connection failed */
273 return -2;
274 }
275
276 return sd;
277}
278
279
280/* Let d = one or more digits. Accept either:
281 d.d.d.d or d.d.d.d:d
282*/
283Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port )
284{
285# define GET_CH ((*str) ? (*str++) : 0)
286 UInt ipa, i, j, c, any;
287 ipa = 0;
288 for (i = 0; i < 4; i++) {
289 j = 0;
290 any = 0;
291 while (1) {
292 c = GET_CH;
293 if (c < '0' || c > '9') break;
294 j = 10 * j + (int)(c - '0');
295 any = 1;
296 }
297 if (any == 0 || j > 255) goto syntaxerr;
298 ipa = (ipa << 8) + j;
299 if (i <= 2 && c != '.') goto syntaxerr;
300 }
301 if (c == 0 || c == ':')
302 *ip_addr = ipa;
303 if (c == 0) goto ok;
304 if (c != ':') goto syntaxerr;
305 j = 0;
306 any = 0;
307 while (1) {
308 c = GET_CH;
309 if (c < '0' || c > '9') break;
310 j = j * 10 + (int)(c - '0');
311 any = 1;
312 if (j > 65535) goto syntaxerr;
313 }
314 if (any == 0 || c != 0) goto syntaxerr;
315 if (j < 1024) goto syntaxerr;
316 *port = (UShort)j;
317 ok:
318 return 1;
319 syntaxerr:
320 return 0;
321# undef GET_CH
322}
323
njneb8896b2005-06-04 20:03:55 +0000324static
325Int my_socket ( Int domain, Int type, Int protocol )
326{
cerion85665ca2005-06-20 15:51:07 +0000327#if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000328 SysRes res;
329 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000330 args[0] = domain;
331 args[1] = type;
332 args[2] = protocol;
333 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SOCKET, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000334 return res.isError ? -1 : res.val;
cerion85665ca2005-06-20 15:51:07 +0000335
336#elif defined(VGP_amd64_linux)
sewardja8d8e232005-06-07 20:04:56 +0000337 // AMD64/Linux doesn't define __NR_socketcall... see comment above
338 // VG_(sigpending)() for more details.
339 I_die_here;
cerion85665ca2005-06-20 15:51:07 +0000340
341#elif defined(VGP_ppc32_linux)
342//CAB: TODO
343 I_die_here;
344
345#else
346# error Unknown arch
347#endif
njneb8896b2005-06-04 20:03:55 +0000348}
349
350static
351Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr,
352 Int addrlen )
353{
cerion85665ca2005-06-20 15:51:07 +0000354#if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000355 SysRes res;
356 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000357 args[0] = sockfd;
358 args[1] = (UWord)serv_addr;
359 args[2] = addrlen;
360 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_CONNECT, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000361 return res.isError ? -1 : res.val;
cerion85665ca2005-06-20 15:51:07 +0000362
363#elif defined(VGP_amd64_linux)
sewardja8d8e232005-06-07 20:04:56 +0000364 // AMD64/Linux doesn't define __NR_socketcall... see comment above
365 // VG_(sigpending)() for more details.
366 I_die_here;
cerion85665ca2005-06-20 15:51:07 +0000367
368#elif defined(VGP_ppc32_linux)
369//CAB: TODO
370 I_die_here;
371
372#else
373# error Unknown arch
374#endif
njneb8896b2005-06-04 20:03:55 +0000375}
376
377Int VG_(write_socket)( Int sd, void *msg, Int count )
378{
njneb8896b2005-06-04 20:03:55 +0000379 /* This is actually send(). */
njneb8896b2005-06-04 20:03:55 +0000380 /* Requests not to send SIGPIPE on errors on stream oriented
381 sockets when the other end breaks the connection. The EPIPE
382 error is still returned. */
383 Int flags = VKI_MSG_NOSIGNAL;
384
cerion85665ca2005-06-20 15:51:07 +0000385#if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000386 SysRes res;
387 UWord args[4];
njneb8896b2005-06-04 20:03:55 +0000388 args[0] = sd;
389 args[1] = (UWord)msg;
390 args[2] = count;
391 args[3] = flags;
392 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SEND, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000393 return res.isError ? -1 : res.val;
cerion85665ca2005-06-20 15:51:07 +0000394
395#elif defined(VGP_amd64_linux)
sewardja8d8e232005-06-07 20:04:56 +0000396 // AMD64/Linux doesn't define __NR_socketcall... see comment above
397 // VG_(sigpending)() for more details.
398 I_die_here;
cerion85665ca2005-06-20 15:51:07 +0000399
400#elif defined(VGP_ppc32_linux)
401//CAB: TODO
402 I_die_here;
403 flags = 0; // stop compiler complaints
404
405#else
406# error Unknown arch
407#endif
njneb8896b2005-06-04 20:03:55 +0000408}
409
410Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen)
411{
sewardja8d8e232005-06-07 20:04:56 +0000412 SysRes res;
sewardja5c9e4a2005-06-09 13:21:58 +0000413
cerion85665ca2005-06-20 15:51:07 +0000414#if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000415 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000416 args[0] = sd;
417 args[1] = (UWord)name;
418 args[2] = (UWord)namelen;
419 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKNAME, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000420 return res.isError ? -1 : res.val;
sewardja5c9e4a2005-06-09 13:21:58 +0000421
cerion85665ca2005-06-20 15:51:07 +0000422#elif defined(VGP_amd64_linux)
sewardja5c9e4a2005-06-09 13:21:58 +0000423 res = VG_(do_syscall3)( __NR_getsockname,
424 (UWord)sd, (UWord)name, (UWord)namelen );
425 return res.isError ? -1 : res.val;
426
cerion85665ca2005-06-20 15:51:07 +0000427#elif defined(VGP_ppc32_linux)
428//CAB: TODO
sewardja8d8e232005-06-07 20:04:56 +0000429 I_die_here;
cerion85665ca2005-06-20 15:51:07 +0000430 return res.isError ? -1 : res.val;
431#else
432# error Unknown arch
433#endif
njneb8896b2005-06-04 20:03:55 +0000434}
435
436Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen)
437{
sewardja8d8e232005-06-07 20:04:56 +0000438 SysRes res;
sewardja5c9e4a2005-06-09 13:21:58 +0000439
cerion85665ca2005-06-20 15:51:07 +0000440#if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000441 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000442 args[0] = sd;
443 args[1] = (UWord)name;
444 args[2] = (UWord)namelen;
445 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETPEERNAME, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000446 return res.isError ? -1 : res.val;
sewardja5c9e4a2005-06-09 13:21:58 +0000447
cerion85665ca2005-06-20 15:51:07 +0000448#elif defined(VGP_amd64_linux)
sewardja5c9e4a2005-06-09 13:21:58 +0000449 res = VG_(do_syscall3)( __NR_getpeername,
450 (UWord)sd, (UWord)name, (UWord)namelen );
451 return res.isError ? -1 : res.val;
452
cerion85665ca2005-06-20 15:51:07 +0000453#elif defined(VGP_ppc32_linux)
454//CAB: TODO
sewardja8d8e232005-06-07 20:04:56 +0000455 I_die_here;
cerion85665ca2005-06-20 15:51:07 +0000456 return res.isError ? -1 : res.val;
457
458#else
459# error Unknown arch
460#endif
njneb8896b2005-06-04 20:03:55 +0000461}
462
463Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval,
464 Int *optlen)
465{
sewardja8d8e232005-06-07 20:04:56 +0000466 SysRes res;
sewardja5c9e4a2005-06-09 13:21:58 +0000467
cerion85665ca2005-06-20 15:51:07 +0000468#if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000469 UWord args[5];
njneb8896b2005-06-04 20:03:55 +0000470 args[0] = sd;
471 args[1] = level;
472 args[2] = optname;
473 args[3] = (UWord)optval;
474 args[4] = (UWord)optlen;
475 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKOPT, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000476 return res.isError ? -1 : res.val;
sewardja5c9e4a2005-06-09 13:21:58 +0000477
cerion85665ca2005-06-20 15:51:07 +0000478#elif defined(VGP_amd64_linux)
sewardja5c9e4a2005-06-09 13:21:58 +0000479 res = VG_(do_syscall5)( __NR_getsockopt,
480 (UWord)sd, (UWord)level, (UWord)optname,
481 (UWord)optval, (UWord)optlen );
482 return res.isError ? -1 : res.val;
483
cerion85665ca2005-06-20 15:51:07 +0000484#elif defined(VGP_ppc32_linux)
485//CAB: TODO
sewardja8d8e232005-06-07 20:04:56 +0000486 I_die_here;
cerion85665ca2005-06-20 15:51:07 +0000487 return res.isError ? -1 : res.val;
488
489#else
490# error Unknown arch
491#endif
njneb8896b2005-06-04 20:03:55 +0000492}
493
494
495
496/*--------------------------------------------------------------------*/
497/*--- end ---*/
498/*--------------------------------------------------------------------*/
499