blob: ea3c0705b50e6a93523566eb959f97741271e9b5 [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"
35#include "pub_core_options.h"
njneb8896b2005-06-04 20:03:55 +000036#include "vki_unistd.h"
37
38/* ---------------------------------------------------------------------
39 File stuff
40 ------------------------------------------------------------------ */
41
njn63004dc2005-06-10 22:39:04 +000042/* Application-visible file descriptor limits */
43Int VG_(fd_soft_limit) = -1;
44Int VG_(fd_hard_limit) = -1;
45
njneb8896b2005-06-04 20:03:55 +000046static inline Bool fd_exists(Int fd)
47{
48 struct vki_stat st;
49
50 return VG_(fstat)(fd, &st) == 0;
51}
52
53/* Move an fd into the Valgrind-safe range */
54Int VG_(safe_fd)(Int oldfd)
55{
56 Int newfd;
57
58 vg_assert(VG_(fd_hard_limit) != -1);
59
60 newfd = VG_(fcntl)(oldfd, VKI_F_DUPFD, VG_(fd_hard_limit));
61 if (newfd != -1)
62 VG_(close)(oldfd);
63
64 VG_(fcntl)(newfd, VKI_F_SETFD, VKI_FD_CLOEXEC);
65
66 vg_assert(newfd >= VG_(fd_hard_limit));
67 return newfd;
68}
69
70/* Returns -1 on failure. */
71Int VG_(open) ( const Char* pathname, Int flags, Int mode )
72{
sewardja8d8e232005-06-07 20:04:56 +000073 SysRes res = VG_(do_syscall3)(__NR_open, (UWord)pathname, flags, mode);
74 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +000075}
76
77void VG_(close) ( Int fd )
78{
sewardja8d8e232005-06-07 20:04:56 +000079 (void)VG_(do_syscall1)(__NR_close, fd);
njneb8896b2005-06-04 20:03:55 +000080}
81
82Int VG_(read) ( Int fd, void* buf, Int count)
83{
sewardja8d8e232005-06-07 20:04:56 +000084 SysRes res = VG_(do_syscall3)(__NR_read, fd, (UWord)buf, count);
85 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +000086}
87
88Int VG_(write) ( Int fd, const void* buf, Int count)
89{
sewardja8d8e232005-06-07 20:04:56 +000090 SysRes res = VG_(do_syscall3)(__NR_write, fd, (UWord)buf, count);
91 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +000092}
93
94Int VG_(pipe) ( Int fd[2] )
95{
sewardja8d8e232005-06-07 20:04:56 +000096 SysRes res = VG_(do_syscall1)(__NR_pipe, (UWord)fd);
97 return res.isError ? -1 : 0;
njneb8896b2005-06-04 20:03:55 +000098}
99
100OffT VG_(lseek) ( Int fd, OffT offset, Int whence)
101{
sewardja8d8e232005-06-07 20:04:56 +0000102 SysRes res = VG_(do_syscall3)(__NR_lseek, fd, offset, whence);
103 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000104}
105
106Int VG_(stat) ( Char* file_name, struct vki_stat* buf )
107{
sewardja8d8e232005-06-07 20:04:56 +0000108 SysRes res = VG_(do_syscall2)(__NR_stat, (UWord)file_name, (UWord)buf);
109 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000110}
111
112Int VG_(fstat) ( Int fd, struct vki_stat* buf )
113{
sewardja8d8e232005-06-07 20:04:56 +0000114 SysRes res = VG_(do_syscall2)(__NR_fstat, fd, (UWord)buf);
115 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000116}
117
118Int VG_(dup2) ( Int oldfd, Int newfd )
119{
sewardja8d8e232005-06-07 20:04:56 +0000120 SysRes res = VG_(do_syscall2)(__NR_dup2, oldfd, newfd);
121 return res.isError ? (-1) : res.val;
njneb8896b2005-06-04 20:03:55 +0000122}
123
124Int VG_(rename) ( Char* old_name, Char* new_name )
125{
sewardja8d8e232005-06-07 20:04:56 +0000126 SysRes res = VG_(do_syscall2)(__NR_rename, (UWord)old_name, (UWord)new_name);
127 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000128}
129
130Int VG_(unlink) ( Char* file_name )
131{
sewardja8d8e232005-06-07 20:04:56 +0000132 SysRes res = VG_(do_syscall1)(__NR_unlink, (UWord)file_name);
133 return res.isError ? (-1) : 0;
njneb8896b2005-06-04 20:03:55 +0000134}
135
136/* Nb: we do not allow the Linux extension which malloc()s memory for the
137 buffer if buf==NULL, because we don't want Linux calling malloc() */
138Char* VG_(getcwd) ( Char* buf, SizeT size )
139{
sewardja8d8e232005-06-07 20:04:56 +0000140 SysRes res;
njneb8896b2005-06-04 20:03:55 +0000141 vg_assert(buf != NULL);
142 res = VG_(do_syscall2)(__NR_getcwd, (UWord)buf, size);
sewardja8d8e232005-06-07 20:04:56 +0000143 return res.isError ? ((Char*)NULL) : (Char*)res.val;
njneb8896b2005-06-04 20:03:55 +0000144}
145
146/* Alternative version that does allocate the memory. Easier to use. */
147Bool VG_(getcwd_alloc) ( Char** out )
148{
149 SizeT size = 4;
150
151 *out = NULL;
152 while (True) {
153 *out = VG_(malloc)(size);
154 if (NULL == VG_(getcwd)(*out, size)) {
155 VG_(free)(*out);
156 if (size > 65535)
157 return False;
158 size *= 2;
159 } else {
160 return True;
161 }
162 }
163}
164
165Int VG_(readlink) (Char* path, Char* buf, UInt bufsiz)
166{
sewardja8d8e232005-06-07 20:04:56 +0000167 SysRes res;
njneb8896b2005-06-04 20:03:55 +0000168 /* res = readlink( path, buf, bufsiz ); */
169 res = VG_(do_syscall3)(__NR_readlink, (UWord)path, (UWord)buf, bufsiz);
sewardja8d8e232005-06-07 20:04:56 +0000170 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +0000171}
172
173Int VG_(getdents) (UInt fd, struct vki_dirent *dirp, UInt count)
174{
sewardja8d8e232005-06-07 20:04:56 +0000175 SysRes res;
njneb8896b2005-06-04 20:03:55 +0000176 /* res = getdents( fd, dirp, count ); */
177 res = VG_(do_syscall3)(__NR_getdents, fd, (UWord)dirp, count);
sewardja8d8e232005-06-07 20:04:56 +0000178 return res.isError ? -1 : res.val;
njneb8896b2005-06-04 20:03:55 +0000179}
180
181/* ---------------------------------------------------------------------
182 Socket-related stuff. This is very Linux-kernel specific.
183 ------------------------------------------------------------------ */
184
185static
186Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port );
187
188static
189Int my_socket ( Int domain, Int type, Int protocol );
190
191static
192Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr,
193 Int addrlen );
194
195static
196UInt my_htonl ( UInt x )
197{
198 return
199 (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8)
200 | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24);
201}
202
203static
204UShort my_htons ( UShort x )
205{
206 return
207 (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8);
208}
209
210
211/* The main function.
212
213 Supplied string contains either an ip address "192.168.0.1" or
214 an ip address and port pair, "192.168.0.1:1500". Parse these,
215 and return:
216 -1 if there is a parse error
217 -2 if no parse error, but specified host:port cannot be opened
218 the relevant file (socket) descriptor, otherwise.
219 is used.
220*/
221Int VG_(connect_via_socket)( UChar* str )
222{
223 Int sd, res;
224 struct vki_sockaddr_in servAddr;
225 UInt ip = 0;
226 UShort port = VG_CLO_DEFAULT_LOGPORT;
227 Bool ok = parse_inet_addr_and_port(str, &ip, &port);
228 if (!ok)
229 return -1;
230
231 //if (0)
232 // VG_(printf)("ip = %d.%d.%d.%d, port %d\n",
233 // (ip >> 24) & 0xFF, (ip >> 16) & 0xFF,
234 // (ip >> 8) & 0xFF, ip & 0xFF,
235 // (UInt)port );
236
237 servAddr.sin_family = VKI_AF_INET;
238 servAddr.sin_addr.s_addr = my_htonl(ip);
239 servAddr.sin_port = my_htons(port);
240
241 /* create socket */
242 sd = my_socket(VKI_AF_INET, VKI_SOCK_STREAM, 0 /* IPPROTO_IP ? */);
243 if (sd < 0) {
244 /* this shouldn't happen ... nevertheless */
245 return -2;
246 }
247
248 /* connect to server */
249 res = my_connect(sd, (struct vki_sockaddr_in *) &servAddr,
250 sizeof(servAddr));
251 if (res < 0) {
252 /* connection failed */
253 return -2;
254 }
255
256 return sd;
257}
258
259
260/* Let d = one or more digits. Accept either:
261 d.d.d.d or d.d.d.d:d
262*/
263Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port )
264{
265# define GET_CH ((*str) ? (*str++) : 0)
266 UInt ipa, i, j, c, any;
267 ipa = 0;
268 for (i = 0; i < 4; i++) {
269 j = 0;
270 any = 0;
271 while (1) {
272 c = GET_CH;
273 if (c < '0' || c > '9') break;
274 j = 10 * j + (int)(c - '0');
275 any = 1;
276 }
277 if (any == 0 || j > 255) goto syntaxerr;
278 ipa = (ipa << 8) + j;
279 if (i <= 2 && c != '.') goto syntaxerr;
280 }
281 if (c == 0 || c == ':')
282 *ip_addr = ipa;
283 if (c == 0) goto ok;
284 if (c != ':') goto syntaxerr;
285 j = 0;
286 any = 0;
287 while (1) {
288 c = GET_CH;
289 if (c < '0' || c > '9') break;
290 j = j * 10 + (int)(c - '0');
291 any = 1;
292 if (j > 65535) goto syntaxerr;
293 }
294 if (any == 0 || c != 0) goto syntaxerr;
295 if (j < 1024) goto syntaxerr;
296 *port = (UShort)j;
297 ok:
298 return 1;
299 syntaxerr:
300 return 0;
301# undef GET_CH
302}
303
njneb8896b2005-06-04 20:03:55 +0000304static
305Int my_socket ( Int domain, Int type, Int protocol )
306{
sewardja8d8e232005-06-07 20:04:56 +0000307# if defined(VGP_x86_linux)
308 SysRes res;
309 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000310 args[0] = domain;
311 args[1] = type;
312 args[2] = protocol;
313 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SOCKET, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000314 return res.isError ? -1 : res.val;
315# else
316 // AMD64/Linux doesn't define __NR_socketcall... see comment above
317 // VG_(sigpending)() for more details.
318 I_die_here;
319# endif
njneb8896b2005-06-04 20:03:55 +0000320}
321
322static
323Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr,
324 Int addrlen )
325{
sewardja8d8e232005-06-07 20:04:56 +0000326# if defined(VGP_x86_linux)
327 SysRes res;
328 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000329 args[0] = sockfd;
330 args[1] = (UWord)serv_addr;
331 args[2] = addrlen;
332 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_CONNECT, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000333 return res.isError ? -1 : res.val;
334# else
335 // AMD64/Linux doesn't define __NR_socketcall... see comment above
336 // VG_(sigpending)() for more details.
337 I_die_here;
338# endif
njneb8896b2005-06-04 20:03:55 +0000339}
340
341Int VG_(write_socket)( Int sd, void *msg, Int count )
342{
njneb8896b2005-06-04 20:03:55 +0000343 /* This is actually send(). */
njneb8896b2005-06-04 20:03:55 +0000344 /* Requests not to send SIGPIPE on errors on stream oriented
345 sockets when the other end breaks the connection. The EPIPE
346 error is still returned. */
347 Int flags = VKI_MSG_NOSIGNAL;
348
sewardja8d8e232005-06-07 20:04:56 +0000349# if defined(VGP_x86_linux)
350 SysRes res;
351 UWord args[4];
njneb8896b2005-06-04 20:03:55 +0000352 args[0] = sd;
353 args[1] = (UWord)msg;
354 args[2] = count;
355 args[3] = flags;
356 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SEND, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000357 return res.isError ? -1 : res.val;
358# else
359 // AMD64/Linux doesn't define __NR_socketcall... see comment above
360 // VG_(sigpending)() for more details.
361 I_die_here;
362# endif
njneb8896b2005-06-04 20:03:55 +0000363}
364
365Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen)
366{
sewardja8d8e232005-06-07 20:04:56 +0000367 SysRes res;
sewardja5c9e4a2005-06-09 13:21:58 +0000368
369# if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000370 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000371 args[0] = sd;
372 args[1] = (UWord)name;
373 args[2] = (UWord)namelen;
374 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKNAME, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000375 return res.isError ? -1 : res.val;
sewardja5c9e4a2005-06-09 13:21:58 +0000376
377# elif defined(VGP_amd64_linux)
378 res = VG_(do_syscall3)( __NR_getsockname,
379 (UWord)sd, (UWord)name, (UWord)namelen );
380 return res.isError ? -1 : res.val;
381
sewardja8d8e232005-06-07 20:04:56 +0000382# else
sewardja8d8e232005-06-07 20:04:56 +0000383 I_die_here;
384# endif
njneb8896b2005-06-04 20:03:55 +0000385}
386
387Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen)
388{
sewardja8d8e232005-06-07 20:04:56 +0000389 SysRes res;
sewardja5c9e4a2005-06-09 13:21:58 +0000390
391# if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000392 UWord args[3];
njneb8896b2005-06-04 20:03:55 +0000393 args[0] = sd;
394 args[1] = (UWord)name;
395 args[2] = (UWord)namelen;
396 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETPEERNAME, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000397 return res.isError ? -1 : res.val;
sewardja5c9e4a2005-06-09 13:21:58 +0000398
399# elif defined(VGP_amd64_linux)
400 res = VG_(do_syscall3)( __NR_getpeername,
401 (UWord)sd, (UWord)name, (UWord)namelen );
402 return res.isError ? -1 : res.val;
403
sewardja8d8e232005-06-07 20:04:56 +0000404# else
sewardja8d8e232005-06-07 20:04:56 +0000405 I_die_here;
406# endif
njneb8896b2005-06-04 20:03:55 +0000407}
408
409Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval,
410 Int *optlen)
411{
sewardja8d8e232005-06-07 20:04:56 +0000412 SysRes res;
sewardja5c9e4a2005-06-09 13:21:58 +0000413
414# if defined(VGP_x86_linux)
sewardja8d8e232005-06-07 20:04:56 +0000415 UWord args[5];
njneb8896b2005-06-04 20:03:55 +0000416 args[0] = sd;
417 args[1] = level;
418 args[2] = optname;
419 args[3] = (UWord)optval;
420 args[4] = (UWord)optlen;
421 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKOPT, (UWord)&args);
sewardja8d8e232005-06-07 20:04:56 +0000422 return res.isError ? -1 : res.val;
sewardja5c9e4a2005-06-09 13:21:58 +0000423
424# elif defined(VGP_amd64_linux)
425 res = VG_(do_syscall5)( __NR_getsockopt,
426 (UWord)sd, (UWord)level, (UWord)optname,
427 (UWord)optval, (UWord)optlen );
428 return res.isError ? -1 : res.val;
429
sewardja8d8e232005-06-07 20:04:56 +0000430# else
431 I_die_here;
sewardja8d8e232005-06-07 20:04:56 +0000432# endif
njneb8896b2005-06-04 20:03:55 +0000433}
434
435
436
437/*--------------------------------------------------------------------*/
438/*--- end ---*/
439/*--------------------------------------------------------------------*/
440