blob: c9970bc95be79e0dcc96245076c2301ce7ed71e5 [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
125Int VG_(rename) ( Char* old_name, Char* new_name )
126{
sewardja8d8e232005-06-07 20:04:56 +0000127 SysRes res = VG_(do_syscall2)(__NR_rename, (UWord)old_name, (UWord)new_name);
128 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000129}
130
131Int VG_(unlink) ( Char* file_name )
132{
sewardja8d8e232005-06-07 20:04:56 +0000133 SysRes res = VG_(do_syscall1)(__NR_unlink, (UWord)file_name);
134 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000135}
136
137/* Nb: we do not allow the Linux extension which malloc()s memory for the
138 buffer if buf==NULL, because we don't want Linux calling malloc() */
139Char* VG_(getcwd) ( Char* buf, SizeT size )
140{
sewardja8d8e232005-06-07 20:04:56 +0000141 SysRes res;
njneb8896b2005-06-04 20:03:55 +0000142 vg_assert(buf != NULL);
143 res = VG_(do_syscall2)(__NR_getcwd, (UWord)buf, size);
sewardja8d8e232005-06-07 20:04:56 +0000144 return res.isError ? ((Char*)NULL) : (Char*)res.val;
njneb8896b2005-06-04 20:03:55 +0000145}
146
147/* Alternative version that does allocate the memory. Easier to use. */
148Bool VG_(getcwd_alloc) ( Char** out )
149{
150 SizeT size = 4;
151
152 *out = NULL;
153 while (True) {
154 *out = VG_(malloc)(size);
155 if (NULL == VG_(getcwd)(*out, size)) {
156 VG_(free)(*out);
157 if (size > 65535)
158 return False;
159 size *= 2;
160 } else {
161 return True;
162 }
163 }
164}
165
166Int VG_(readlink) (Char* path, Char* buf, UInt bufsiz)
167{
sewardja8d8e232005-06-07 20:04:56 +0000168 SysRes res;
njneb8896b2005-06-04 20:03:55 +0000169 /* res = readlink( path, buf, bufsiz ); */
170 res = VG_(do_syscall3)(__NR_readlink, (UWord)path, (UWord)buf, bufsiz);
sewardja8d8e232005-06-07 20:04:56 +0000171 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +0000172}
173
174Int VG_(getdents) (UInt fd, struct vki_dirent *dirp, UInt count)
175{
sewardja8d8e232005-06-07 20:04:56 +0000176 SysRes res;
njneb8896b2005-06-04 20:03:55 +0000177 /* res = getdents( fd, dirp, count ); */
178 res = VG_(do_syscall3)(__NR_getdents, fd, (UWord)dirp, count);
sewardja8d8e232005-06-07 20:04:56 +0000179 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +0000180}
181
182/* ---------------------------------------------------------------------
183 Socket-related stuff. This is very Linux-kernel specific.
184 ------------------------------------------------------------------ */
185
186static
187Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port );
188
189static
190Int my_socket ( Int domain, Int type, Int protocol );
191
192static
193Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr,
194 Int addrlen );
195
196static
197UInt my_htonl ( UInt x )
198{
199 return
200 (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8)
201 | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24);
202}
203
204static
205UShort my_htons ( UShort x )
206{
207 return
208 (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8);
209}
210
211
212/* The main function.
213
214 Supplied string contains either an ip address "192.168.0.1" or
215 an ip address and port pair, "192.168.0.1:1500". Parse these,
216 and return:
217 -1 if there is a parse error
218 -2 if no parse error, but specified host:port cannot be opened
219 the relevant file (socket) descriptor, otherwise.
220 is used.
221*/
222Int VG_(connect_via_socket)( UChar* str )
223{
224 Int sd, res;
225 struct vki_sockaddr_in servAddr;
226 UInt ip = 0;
227 UShort port = VG_CLO_DEFAULT_LOGPORT;
228 Bool ok = parse_inet_addr_and_port(str, &ip, &port);
229 if (!ok)
230 return -1;
231
232 //if (0)
233 // VG_(printf)("ip = %d.%d.%d.%d, port %d\n",
234 // (ip >> 24) & 0xFF, (ip >> 16) & 0xFF,
235 // (ip >> 8) & 0xFF, ip & 0xFF,
236 // (UInt)port );
237
238 servAddr.sin_family = VKI_AF_INET;
239 servAddr.sin_addr.s_addr = my_htonl(ip);
240 servAddr.sin_port = my_htons(port);
241
242 /* create socket */
243 sd = my_socket(VKI_AF_INET, VKI_SOCK_STREAM, 0 /* IPPROTO_IP ? */);
244 if (sd < 0) {
245 /* this shouldn't happen ... nevertheless */
246 return -2;
247 }
248
249 /* connect to server */
250 res = my_connect(sd, (struct vki_sockaddr_in *) &servAddr,
251 sizeof(servAddr));
252 if (res < 0) {
253 /* connection failed */
254 return -2;
255 }
256
257 return sd;
258}
259
260
261/* Let d = one or more digits. Accept either:
262 d.d.d.d or d.d.d.d:d
263*/
264Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port )
265{
266# define GET_CH ((*str) ? (*str++) : 0)
267 UInt ipa, i, j, c, any;
268 ipa = 0;
269 for (i = 0; i < 4; i++) {
270 j = 0;
271 any = 0;
272 while (1) {
273 c = GET_CH;
274 if (c < '0' || c > '9') break;
275 j = 10 * j + (int)(c - '0');
276 any = 1;
277 }
278 if (any == 0 || j > 255) goto syntaxerr;
279 ipa = (ipa << 8) + j;
280 if (i <= 2 && c != '.') goto syntaxerr;
281 }
282 if (c == 0 || c == ':')
283 *ip_addr = ipa;
284 if (c == 0) goto ok;
285 if (c != ':') goto syntaxerr;
286 j = 0;
287 any = 0;
288 while (1) {
289 c = GET_CH;
290 if (c < '0' || c > '9') break;
291 j = j * 10 + (int)(c - '0');
292 any = 1;
293 if (j > 65535) goto syntaxerr;
294 }
295 if (any == 0 || c != 0) goto syntaxerr;
296 if (j < 1024) goto syntaxerr;
297 *port = (UShort)j;
298 ok:
299 return 1;
300 syntaxerr:
301 return 0;
302# undef GET_CH
303}
304
njneb8896b2005-06-04 20:03:55 +0000305static
306Int my_socket ( Int domain, Int type, Int protocol )
307{
sewardja8d8e232005-06-07 20:04:56 +0000308# if defined(VGP_x86_linux)
309 SysRes res;
310 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000311 args[0] = domain;
312 args[1] = type;
313 args[2] = protocol;
314 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SOCKET, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000315 return res.isError ? -1 : res.val;
316# else
317 // AMD64/Linux doesn't define __NR_socketcall... see comment above
318 // VG_(sigpending)() for more details.
319 I_die_here;
320# endif
njneb8896b2005-06-04 20:03:55 +0000321}
322
323static
324Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr,
325 Int addrlen )
326{
sewardja8d8e232005-06-07 20:04:56 +0000327# if defined(VGP_x86_linux)
328 SysRes res;
329 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000330 args[0] = sockfd;
331 args[1] = (UWord)serv_addr;
332 args[2] = addrlen;
333 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_CONNECT, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000334 return res.isError ? -1 : res.val;
335# else
336 // AMD64/Linux doesn't define __NR_socketcall... see comment above
337 // VG_(sigpending)() for more details.
338 I_die_here;
339# endif
njneb8896b2005-06-04 20:03:55 +0000340}
341
342Int VG_(write_socket)( Int sd, void *msg, Int count )
343{
njneb8896b2005-06-04 20:03:55 +0000344 /* This is actually send(). */
njneb8896b2005-06-04 20:03:55 +0000345 /* Requests not to send SIGPIPE on errors on stream oriented
346 sockets when the other end breaks the connection. The EPIPE
347 error is still returned. */
348 Int flags = VKI_MSG_NOSIGNAL;
349
sewardja8d8e232005-06-07 20:04:56 +0000350# if defined(VGP_x86_linux)
351 SysRes res;
352 UWord args[4];
njneb8896b2005-06-04 20:03:55 +0000353 args[0] = sd;
354 args[1] = (UWord)msg;
355 args[2] = count;
356 args[3] = flags;
357 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SEND, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000358 return res.isError ? -1 : res.val;
359# else
360 // AMD64/Linux doesn't define __NR_socketcall... see comment above
361 // VG_(sigpending)() for more details.
362 I_die_here;
363# endif
njneb8896b2005-06-04 20:03:55 +0000364}
365
366Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen)
367{
sewardja8d8e232005-06-07 20:04:56 +0000368 SysRes res;
sewardja5c9e4a2005-06-09 13:21:58 +0000369
370# if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000371 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000372 args[0] = sd;
373 args[1] = (UWord)name;
374 args[2] = (UWord)namelen;
375 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKNAME, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000376 return res.isError ? -1 : res.val;
sewardja5c9e4a2005-06-09 13:21:58 +0000377
378# elif defined(VGP_amd64_linux)
379 res = VG_(do_syscall3)( __NR_getsockname,
380 (UWord)sd, (UWord)name, (UWord)namelen );
381 return res.isError ? -1 : res.val;
382
sewardja8d8e232005-06-07 20:04:56 +0000383# else
sewardja8d8e232005-06-07 20:04:56 +0000384 I_die_here;
385# endif
njneb8896b2005-06-04 20:03:55 +0000386}
387
388Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen)
389{
sewardja8d8e232005-06-07 20:04:56 +0000390 SysRes res;
sewardja5c9e4a2005-06-09 13:21:58 +0000391
392# if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000393 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000394 args[0] = sd;
395 args[1] = (UWord)name;
396 args[2] = (UWord)namelen;
397 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETPEERNAME, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000398 return res.isError ? -1 : res.val;
sewardja5c9e4a2005-06-09 13:21:58 +0000399
400# elif defined(VGP_amd64_linux)
401 res = VG_(do_syscall3)( __NR_getpeername,
402 (UWord)sd, (UWord)name, (UWord)namelen );
403 return res.isError ? -1 : res.val;
404
sewardja8d8e232005-06-07 20:04:56 +0000405# else
sewardja8d8e232005-06-07 20:04:56 +0000406 I_die_here;
407# endif
njneb8896b2005-06-04 20:03:55 +0000408}
409
410Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval,
411 Int *optlen)
412{
sewardja8d8e232005-06-07 20:04:56 +0000413 SysRes res;
sewardja5c9e4a2005-06-09 13:21:58 +0000414
415# if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000416 UWord args[5];
njneb8896b2005-06-04 20:03:55 +0000417 args[0] = sd;
418 args[1] = level;
419 args[2] = optname;
420 args[3] = (UWord)optval;
421 args[4] = (UWord)optlen;
422 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKOPT, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000423 return res.isError ? -1 : res.val;
sewardja5c9e4a2005-06-09 13:21:58 +0000424
425# elif defined(VGP_amd64_linux)
426 res = VG_(do_syscall5)( __NR_getsockopt,
427 (UWord)sd, (UWord)level, (UWord)optname,
428 (UWord)optval, (UWord)optlen );
429 return res.isError ? -1 : res.val;
430
sewardja8d8e232005-06-07 20:04:56 +0000431# else
432 I_die_here;
sewardja8d8e232005-06-07 20:04:56 +0000433# endif
njneb8896b2005-06-04 20:03:55 +0000434}
435
436
437
438/*--------------------------------------------------------------------*/
439/*--- end ---*/
440/*--------------------------------------------------------------------*/
441