blob: 3a044f5ec152a181e339be537be94b2a5ebba9da [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
31#include "core.h"
32#include "pub_core_libcbase.h"
33#include "pub_core_libcassert.h"
34#include "pub_core_libcfile.h"
njnaf1d7df2005-06-11 01:31:52 +000035#include "pub_core_mallocfree.h"
njneb8896b2005-06-04 20:03:55 +000036#include "pub_core_options.h"
njneb8896b2005-06-04 20:03:55 +000037#include "vki_unistd.h"
38
39/* ---------------------------------------------------------------------
40 File stuff
41 ------------------------------------------------------------------ */
42
njn63004dc2005-06-10 22:39:04 +000043/* Application-visible file descriptor limits */
44Int VG_(fd_soft_limit) = -1;
45Int VG_(fd_hard_limit) = -1;
46
njneb8896b2005-06-04 20:03:55 +000047static inline Bool fd_exists(Int fd)
48{
49 struct vki_stat st;
50
51 return VG_(fstat)(fd, &st) == 0;
52}
53
54/* Move an fd into the Valgrind-safe range */
55Int VG_(safe_fd)(Int oldfd)
56{
57 Int newfd;
58
59 vg_assert(VG_(fd_hard_limit) != -1);
60
61 newfd = VG_(fcntl)(oldfd, VKI_F_DUPFD, VG_(fd_hard_limit));
62 if (newfd != -1)
63 VG_(close)(oldfd);
64
65 VG_(fcntl)(newfd, VKI_F_SETFD, VKI_FD_CLOEXEC);
66
67 vg_assert(newfd >= VG_(fd_hard_limit));
68 return newfd;
69}
70
71/* Returns -1 on failure. */
72Int VG_(open) ( const Char* pathname, Int flags, Int mode )
73{
sewardja8d8e232005-06-07 20:04:56 +000074 SysRes res = VG_(do_syscall3)(__NR_open, (UWord)pathname, flags, mode);
75 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +000076}
77
78void VG_(close) ( Int fd )
79{
sewardja8d8e232005-06-07 20:04:56 +000080 (void)VG_(do_syscall1)(__NR_close, fd);
njneb8896b2005-06-04 20:03:55 +000081}
82
83Int VG_(read) ( Int fd, void* buf, Int count)
84{
sewardja8d8e232005-06-07 20:04:56 +000085 SysRes res = VG_(do_syscall3)(__NR_read, fd, (UWord)buf, count);
86 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +000087}
88
89Int VG_(write) ( Int fd, const void* buf, Int count)
90{
sewardja8d8e232005-06-07 20:04:56 +000091 SysRes res = VG_(do_syscall3)(__NR_write, fd, (UWord)buf, count);
92 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +000093}
94
95Int VG_(pipe) ( Int fd[2] )
96{
sewardja8d8e232005-06-07 20:04:56 +000097 SysRes res = VG_(do_syscall1)(__NR_pipe, (UWord)fd);
98 return res.isError ? -1 : 0;
njneb8896b2005-06-04 20:03:55 +000099}
100
101OffT VG_(lseek) ( Int fd, OffT offset, Int whence)
102{
sewardja8d8e232005-06-07 20:04:56 +0000103 SysRes res = VG_(do_syscall3)(__NR_lseek, fd, offset, whence);
104 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000105}
106
107Int VG_(stat) ( Char* file_name, struct vki_stat* buf )
108{
sewardja8d8e232005-06-07 20:04:56 +0000109 SysRes res = VG_(do_syscall2)(__NR_stat, (UWord)file_name, (UWord)buf);
110 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000111}
112
113Int VG_(fstat) ( Int fd, struct vki_stat* buf )
114{
sewardja8d8e232005-06-07 20:04:56 +0000115 SysRes res = VG_(do_syscall2)(__NR_fstat, fd, (UWord)buf);
116 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000117}
118
119Int VG_(dup2) ( Int oldfd, Int newfd )
120{
sewardja8d8e232005-06-07 20:04:56 +0000121 SysRes res = VG_(do_syscall2)(__NR_dup2, oldfd, newfd);
122 return res.isError ? (-1) : res.val;
njneb8896b2005-06-04 20:03:55 +0000123}
124
njn327fe8a2005-06-12 02:49:35 +0000125/* Returns -1 on error. */
126Int VG_(fcntl) ( Int fd, Int cmd, Int arg )
127{
128 SysRes res = VG_(do_syscall3)(__NR_fcntl, fd, cmd, arg);
129 return res.isError ? -1 : res.val;
130}
131
njneb8896b2005-06-04 20:03:55 +0000132Int VG_(rename) ( Char* old_name, Char* new_name )
133{
sewardja8d8e232005-06-07 20:04:56 +0000134 SysRes res = VG_(do_syscall2)(__NR_rename, (UWord)old_name, (UWord)new_name);
135 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000136}
137
138Int VG_(unlink) ( Char* file_name )
139{
sewardja8d8e232005-06-07 20:04:56 +0000140 SysRes res = VG_(do_syscall1)(__NR_unlink, (UWord)file_name);
141 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000142}
143
144/* Nb: we do not allow the Linux extension which malloc()s memory for the
145 buffer if buf==NULL, because we don't want Linux calling malloc() */
146Char* VG_(getcwd) ( Char* buf, SizeT size )
147{
sewardja8d8e232005-06-07 20:04:56 +0000148 SysRes res;
njneb8896b2005-06-04 20:03:55 +0000149 vg_assert(buf != NULL);
150 res = VG_(do_syscall2)(__NR_getcwd, (UWord)buf, size);
sewardja8d8e232005-06-07 20:04:56 +0000151 return res.isError ? ((Char*)NULL) : (Char*)res.val;
njneb8896b2005-06-04 20:03:55 +0000152}
153
154/* Alternative version that does allocate the memory. Easier to use. */
155Bool VG_(getcwd_alloc) ( Char** out )
156{
157 SizeT size = 4;
158
159 *out = NULL;
160 while (True) {
161 *out = VG_(malloc)(size);
162 if (NULL == VG_(getcwd)(*out, size)) {
163 VG_(free)(*out);
164 if (size > 65535)
165 return False;
166 size *= 2;
167 } else {
168 return True;
169 }
170 }
171}
172
173Int VG_(readlink) (Char* path, Char* buf, UInt bufsiz)
174{
sewardja8d8e232005-06-07 20:04:56 +0000175 SysRes res;
njneb8896b2005-06-04 20:03:55 +0000176 /* res = readlink( path, buf, bufsiz ); */
177 res = VG_(do_syscall3)(__NR_readlink, (UWord)path, (UWord)buf, bufsiz);
sewardja8d8e232005-06-07 20:04:56 +0000178 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +0000179}
180
181Int VG_(getdents) (UInt fd, struct vki_dirent *dirp, UInt count)
182{
sewardja8d8e232005-06-07 20:04:56 +0000183 SysRes res;
njneb8896b2005-06-04 20:03:55 +0000184 /* res = getdents( fd, dirp, count ); */
185 res = VG_(do_syscall3)(__NR_getdents, fd, (UWord)dirp, count);
sewardja8d8e232005-06-07 20:04:56 +0000186 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +0000187}
188
189/* ---------------------------------------------------------------------
190 Socket-related stuff. This is very Linux-kernel specific.
191 ------------------------------------------------------------------ */
192
193static
194Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port );
195
196static
197Int my_socket ( Int domain, Int type, Int protocol );
198
199static
200Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr,
201 Int addrlen );
202
203static
204UInt my_htonl ( UInt x )
205{
206 return
207 (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8)
208 | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24);
209}
210
211static
212UShort my_htons ( UShort x )
213{
214 return
215 (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8);
216}
217
218
219/* The main function.
220
221 Supplied string contains either an ip address "192.168.0.1" or
222 an ip address and port pair, "192.168.0.1:1500". Parse these,
223 and return:
224 -1 if there is a parse error
225 -2 if no parse error, but specified host:port cannot be opened
226 the relevant file (socket) descriptor, otherwise.
227 is used.
228*/
229Int VG_(connect_via_socket)( UChar* str )
230{
231 Int sd, res;
232 struct vki_sockaddr_in servAddr;
233 UInt ip = 0;
234 UShort port = VG_CLO_DEFAULT_LOGPORT;
235 Bool ok = parse_inet_addr_and_port(str, &ip, &port);
236 if (!ok)
237 return -1;
238
239 //if (0)
240 // VG_(printf)("ip = %d.%d.%d.%d, port %d\n",
241 // (ip >> 24) & 0xFF, (ip >> 16) & 0xFF,
242 // (ip >> 8) & 0xFF, ip & 0xFF,
243 // (UInt)port );
244
245 servAddr.sin_family = VKI_AF_INET;
246 servAddr.sin_addr.s_addr = my_htonl(ip);
247 servAddr.sin_port = my_htons(port);
248
249 /* create socket */
250 sd = my_socket(VKI_AF_INET, VKI_SOCK_STREAM, 0 /* IPPROTO_IP ? */);
251 if (sd < 0) {
252 /* this shouldn't happen ... nevertheless */
253 return -2;
254 }
255
256 /* connect to server */
257 res = my_connect(sd, (struct vki_sockaddr_in *) &servAddr,
258 sizeof(servAddr));
259 if (res < 0) {
260 /* connection failed */
261 return -2;
262 }
263
264 return sd;
265}
266
267
268/* Let d = one or more digits. Accept either:
269 d.d.d.d or d.d.d.d:d
270*/
271Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port )
272{
273# define GET_CH ((*str) ? (*str++) : 0)
274 UInt ipa, i, j, c, any;
275 ipa = 0;
276 for (i = 0; i < 4; i++) {
277 j = 0;
278 any = 0;
279 while (1) {
280 c = GET_CH;
281 if (c < '0' || c > '9') break;
282 j = 10 * j + (int)(c - '0');
283 any = 1;
284 }
285 if (any == 0 || j > 255) goto syntaxerr;
286 ipa = (ipa << 8) + j;
287 if (i <= 2 && c != '.') goto syntaxerr;
288 }
289 if (c == 0 || c == ':')
290 *ip_addr = ipa;
291 if (c == 0) goto ok;
292 if (c != ':') goto syntaxerr;
293 j = 0;
294 any = 0;
295 while (1) {
296 c = GET_CH;
297 if (c < '0' || c > '9') break;
298 j = j * 10 + (int)(c - '0');
299 any = 1;
300 if (j > 65535) goto syntaxerr;
301 }
302 if (any == 0 || c != 0) goto syntaxerr;
303 if (j < 1024) goto syntaxerr;
304 *port = (UShort)j;
305 ok:
306 return 1;
307 syntaxerr:
308 return 0;
309# undef GET_CH
310}
311
njneb8896b2005-06-04 20:03:55 +0000312static
313Int my_socket ( Int domain, Int type, Int protocol )
314{
sewardja8d8e232005-06-07 20:04:56 +0000315# if defined(VGP_x86_linux)
316 SysRes res;
317 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000318 args[0] = domain;
319 args[1] = type;
320 args[2] = protocol;
321 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SOCKET, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000322 return res.isError ? -1 : res.val;
323# else
324 // AMD64/Linux doesn't define __NR_socketcall... see comment above
325 // VG_(sigpending)() for more details.
326 I_die_here;
327# endif
njneb8896b2005-06-04 20:03:55 +0000328}
329
330static
331Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr,
332 Int addrlen )
333{
sewardja8d8e232005-06-07 20:04:56 +0000334# if defined(VGP_x86_linux)
335 SysRes res;
336 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000337 args[0] = sockfd;
338 args[1] = (UWord)serv_addr;
339 args[2] = addrlen;
340 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_CONNECT, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000341 return res.isError ? -1 : res.val;
342# else
343 // AMD64/Linux doesn't define __NR_socketcall... see comment above
344 // VG_(sigpending)() for more details.
345 I_die_here;
346# endif
njneb8896b2005-06-04 20:03:55 +0000347}
348
349Int VG_(write_socket)( Int sd, void *msg, Int count )
350{
njneb8896b2005-06-04 20:03:55 +0000351 /* This is actually send(). */
njneb8896b2005-06-04 20:03:55 +0000352 /* Requests not to send SIGPIPE on errors on stream oriented
353 sockets when the other end breaks the connection. The EPIPE
354 error is still returned. */
355 Int flags = VKI_MSG_NOSIGNAL;
356
sewardja8d8e232005-06-07 20:04:56 +0000357# if defined(VGP_x86_linux)
358 SysRes res;
359 UWord args[4];
njneb8896b2005-06-04 20:03:55 +0000360 args[0] = sd;
361 args[1] = (UWord)msg;
362 args[2] = count;
363 args[3] = flags;
364 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SEND, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000365 return res.isError ? -1 : res.val;
366# else
367 // AMD64/Linux doesn't define __NR_socketcall... see comment above
368 // VG_(sigpending)() for more details.
369 I_die_here;
370# endif
njneb8896b2005-06-04 20:03:55 +0000371}
372
373Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen)
374{
sewardja8d8e232005-06-07 20:04:56 +0000375 SysRes res;
sewardja5c9e4a2005-06-09 13:21:58 +0000376
377# if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000378 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000379 args[0] = sd;
380 args[1] = (UWord)name;
381 args[2] = (UWord)namelen;
382 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKNAME, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000383 return res.isError ? -1 : res.val;
sewardja5c9e4a2005-06-09 13:21:58 +0000384
385# elif defined(VGP_amd64_linux)
386 res = VG_(do_syscall3)( __NR_getsockname,
387 (UWord)sd, (UWord)name, (UWord)namelen );
388 return res.isError ? -1 : res.val;
389
sewardja8d8e232005-06-07 20:04:56 +0000390# else
sewardja8d8e232005-06-07 20:04:56 +0000391 I_die_here;
392# endif
njneb8896b2005-06-04 20:03:55 +0000393}
394
395Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen)
396{
sewardja8d8e232005-06-07 20:04:56 +0000397 SysRes res;
sewardja5c9e4a2005-06-09 13:21:58 +0000398
399# if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000400 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000401 args[0] = sd;
402 args[1] = (UWord)name;
403 args[2] = (UWord)namelen;
404 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETPEERNAME, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000405 return res.isError ? -1 : res.val;
sewardja5c9e4a2005-06-09 13:21:58 +0000406
407# elif defined(VGP_amd64_linux)
408 res = VG_(do_syscall3)( __NR_getpeername,
409 (UWord)sd, (UWord)name, (UWord)namelen );
410 return res.isError ? -1 : res.val;
411
sewardja8d8e232005-06-07 20:04:56 +0000412# else
sewardja8d8e232005-06-07 20:04:56 +0000413 I_die_here;
414# endif
njneb8896b2005-06-04 20:03:55 +0000415}
416
417Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval,
418 Int *optlen)
419{
sewardja8d8e232005-06-07 20:04:56 +0000420 SysRes res;
sewardja5c9e4a2005-06-09 13:21:58 +0000421
422# if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000423 UWord args[5];
njneb8896b2005-06-04 20:03:55 +0000424 args[0] = sd;
425 args[1] = level;
426 args[2] = optname;
427 args[3] = (UWord)optval;
428 args[4] = (UWord)optlen;
429 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKOPT, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000430 return res.isError ? -1 : res.val;
sewardja5c9e4a2005-06-09 13:21:58 +0000431
432# elif defined(VGP_amd64_linux)
433 res = VG_(do_syscall5)( __NR_getsockopt,
434 (UWord)sd, (UWord)level, (UWord)optname,
435 (UWord)optval, (UWord)optlen );
436 return res.isError ? -1 : res.val;
437
sewardja8d8e232005-06-07 20:04:56 +0000438# else
439 I_die_here;
sewardja8d8e232005-06-07 20:04:56 +0000440# endif
njneb8896b2005-06-04 20:03:55 +0000441}
442
443
444
445/*--------------------------------------------------------------------*/
446/*--- end ---*/
447/*--------------------------------------------------------------------*/
448