2 /*--------------------------------------------------------------------*/
3 /*--- File- and socket-related libc stuff. m_libcfile.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2012 Julian Seward
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.
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.
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
28 The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics.h"
32 #include "pub_core_vki.h"
33 #include "pub_core_vkiscnums.h"
34 #include "pub_core_debuglog.h"
35 #include "pub_core_libcbase.h"
36 #include "pub_core_libcassert.h"
37 #include "pub_core_libcfile.h"
38 #include "pub_core_libcprint.h" // VG_(sprintf)
39 #include "pub_core_libcproc.h" // VG_(getpid), VG_(getppid)
40 #include "pub_core_xarray.h"
41 #include "pub_core_clientstate.h" // VG_(fd_hard_limit)
42 #include "pub_core_syscall.h"
44 /* IMPORTANT: on Darwin it is essential to use the _nocancel versions
45 of syscalls rather than the vanilla version, if a _nocancel version
46 is available. See docs/internals/Darwin-notes.txt for the reason
49 /* ---------------------------------------------------------------------
51 ------------------------------------------------------------------ */
53 static inline Bool fd_exists(Int fd)
56 return VG_(fstat)(fd, &st) == 0;
59 /* Move an fd into the Valgrind-safe range */
60 Int VG_(safe_fd)(Int oldfd)
64 vg_assert(VG_(fd_hard_limit) != -1);
66 newfd = VG_(fcntl)(oldfd, VKI_F_DUPFD, VG_(fd_hard_limit));
70 /* Set the close-on-exec flag for this fd. */
71 VG_(fcntl)(newfd, VKI_F_SETFD, VKI_FD_CLOEXEC);
73 vg_assert(newfd >= VG_(fd_hard_limit));
77 /* Given a file descriptor, attempt to deduce its filename. To do
78 this, we use /proc/self/fd/<FD>. If this doesn't point to a file,
79 or if it doesn't exist, we return False. */
80 Bool VG_(resolve_filename) ( Int fd, HChar* buf, Int n_buf )
82 # if defined(VGO_linux)
84 VG_(sprintf)(tmp, "/proc/self/fd/%d", fd);
85 VG_(memset)(buf, 0, n_buf);
86 if (VG_(readlink)(tmp, buf, n_buf) > 0 && buf[0] == '/')
91 # elif defined(VGO_darwin)
92 HChar tmp[VKI_MAXPATHLEN+1];
93 if (0 == VG_(fcntl)(fd, VKI_F_GETPATH, (UWord)tmp)) {
95 VG_(strncpy)( buf, tmp, n_buf < sizeof(tmp) ? n_buf : sizeof(tmp) );
98 if (tmp[0] == '/') return True;
107 SysRes VG_(mknod) ( const Char* pathname, Int mode, UWord dev )
109 # if defined(VGO_linux) || defined(VGO_darwin)
110 SysRes res = VG_(do_syscall3)(__NR_mknod,
111 (UWord)pathname, mode, dev);
118 SysRes VG_(open) ( const Char* pathname, Int flags, Int mode )
120 # if defined(VGO_linux)
121 SysRes res = VG_(do_syscall3)(__NR_open,
122 (UWord)pathname, flags, mode);
123 # elif defined(VGO_darwin)
124 SysRes res = VG_(do_syscall3)(__NR_open_nocancel,
125 (UWord)pathname, flags, mode);
132 Int VG_(fd_open) (const Char* pathname, Int flags, Int mode)
135 sr = VG_(open) (pathname, flags, mode);
142 void VG_(close) ( Int fd )
144 /* Hmm. Return value is not checked. That's uncool. */
145 # if defined(VGO_linux)
146 (void)VG_(do_syscall1)(__NR_close, fd);
147 # elif defined(VGO_darwin)
148 (void)VG_(do_syscall1)(__NR_close_nocancel, fd);
154 Int VG_(read) ( Int fd, void* buf, Int count)
157 # if defined(VGO_linux)
158 SysRes res = VG_(do_syscall3)(__NR_read, fd, (UWord)buf, count);
159 # elif defined(VGO_darwin)
160 SysRes res = VG_(do_syscall3)(__NR_read_nocancel, fd, (UWord)buf, count);
164 if (sr_isError(res)) {
165 ret = - (Int)(Word)sr_Err(res);
168 ret = (Int)(Word)sr_Res(res);
174 Int VG_(write) ( Int fd, const void* buf, Int count)
177 # if defined(VGO_linux)
178 SysRes res = VG_(do_syscall3)(__NR_write, fd, (UWord)buf, count);
179 # elif defined(VGO_darwin)
180 SysRes res = VG_(do_syscall3)(__NR_write_nocancel, fd, (UWord)buf, count);
184 if (sr_isError(res)) {
185 ret = - (Int)(Word)sr_Err(res);
188 ret = (Int)(Word)sr_Res(res);
195 Int VG_(pipe) ( Int fd[2] )
197 # if defined(VGP_mips32_linux)
198 /* __NR_pipe has a strange return convention on mips32-linux. */
199 SysRes res = VG_(do_syscall0)(__NR_pipe);
200 if (!sr_isError(res)) {
201 fd[0] = (Int)sr_Res(res);
202 fd[1] = (Int)sr_ResEx(res);
207 # elif defined(VGO_linux)
208 SysRes res = VG_(do_syscall1)(__NR_pipe, (UWord)fd);
209 return sr_isError(res) ? -1 : 0;
210 # elif defined(VGO_darwin)
211 /* __NR_pipe is UX64, so produces a double-word result */
212 SysRes res = VG_(do_syscall0)(__NR_pipe);
213 if (!sr_isError(res)) {
214 fd[0] = (Int)sr_Res(res);
215 fd[1] = (Int)sr_ResHI(res);
217 return sr_isError(res) ? -1 : 0;
223 Off64T VG_(lseek) ( Int fd, Off64T offset, Int whence )
225 # if defined(VGO_linux) || defined(VGP_amd64_darwin)
226 # if defined(__NR__llseek)
228 SysRes res = VG_(do_syscall5)(__NR__llseek, fd,
229 offset >> 32, offset & 0xffffffff,
230 (UWord)&result, whence);
231 return sr_isError(res) ? (-1) : result;
233 SysRes res = VG_(do_syscall3)(__NR_lseek, fd, offset, whence);
234 vg_assert(sizeof(Off64T) == sizeof(Word));
235 return sr_isError(res) ? (-1) : sr_Res(res);
237 # elif defined(VGP_x86_darwin)
238 SysRes res = VG_(do_syscall4)(__NR_lseek, fd,
239 offset & 0xffffffff, offset >> 32, whence);
240 return sr_isError(res) ? (-1) : sr_Res(res);
242 # error "Unknown plat"
244 /* if you change the error-reporting conventions of this, also
245 change all usage points. */
249 /* stat/fstat support. It's uggerly. We have impedance-match into a
250 'struct vg_stat' in order to have a single structure that callers
251 can use consistently on all platforms. */
253 #define TRANSLATE_TO_vg_stat(_p_vgstat, _p_vkistat) \
255 (_p_vgstat)->dev = (ULong)( (_p_vkistat)->st_dev ); \
256 (_p_vgstat)->ino = (ULong)( (_p_vkistat)->st_ino ); \
257 (_p_vgstat)->nlink = (ULong)( (_p_vkistat)->st_nlink ); \
258 (_p_vgstat)->mode = (UInt) ( (_p_vkistat)->st_mode ); \
259 (_p_vgstat)->uid = (UInt) ( (_p_vkistat)->st_uid ); \
260 (_p_vgstat)->gid = (UInt) ( (_p_vkistat)->st_gid ); \
261 (_p_vgstat)->rdev = (ULong)( (_p_vkistat)->st_rdev ); \
262 (_p_vgstat)->size = (Long) ( (_p_vkistat)->st_size ); \
263 (_p_vgstat)->blksize = (ULong)( (_p_vkistat)->st_blksize ); \
264 (_p_vgstat)->blocks = (ULong)( (_p_vkistat)->st_blocks ); \
265 (_p_vgstat)->atime = (ULong)( (_p_vkistat)->st_atime ); \
266 (_p_vgstat)->atime_nsec = (ULong)( (_p_vkistat)->st_atime_nsec ); \
267 (_p_vgstat)->mtime = (ULong)( (_p_vkistat)->st_mtime ); \
268 (_p_vgstat)->mtime_nsec = (ULong)( (_p_vkistat)->st_mtime_nsec ); \
269 (_p_vgstat)->ctime = (ULong)( (_p_vkistat)->st_ctime ); \
270 (_p_vgstat)->ctime_nsec = (ULong)( (_p_vkistat)->st_ctime_nsec ); \
273 SysRes VG_(stat) ( const Char* file_name, struct vg_stat* vgbuf )
276 VG_(memset)(vgbuf, 0, sizeof(*vgbuf));
278 # if defined(VGO_linux) || defined(VGO_darwin)
279 /* First try with stat64. If that doesn't work out, fall back to
280 the vanilla version. */
281 # if defined(__NR_stat64)
282 { struct vki_stat64 buf64;
283 res = VG_(do_syscall2)(__NR_stat64, (UWord)file_name, (UWord)&buf64);
284 if (!(sr_isError(res) && sr_Err(res) == VKI_ENOSYS)) {
285 /* Success, or any failure except ENOSYS */
286 if (!sr_isError(res))
287 TRANSLATE_TO_vg_stat(vgbuf, &buf64);
291 # endif /* defined(__NR_stat64) */
292 { struct vki_stat buf;
293 res = VG_(do_syscall2)(__NR_stat, (UWord)file_name, (UWord)&buf);
294 if (!sr_isError(res))
295 TRANSLATE_TO_vg_stat(vgbuf, &buf);
304 Int VG_(fstat) ( Int fd, struct vg_stat* vgbuf )
307 VG_(memset)(vgbuf, 0, sizeof(*vgbuf));
309 # if defined(VGO_linux) || defined(VGO_darwin)
310 /* First try with fstat64. If that doesn't work out, fall back to
311 the vanilla version. */
312 # if defined(__NR_fstat64)
313 { struct vki_stat64 buf64;
314 res = VG_(do_syscall2)(__NR_fstat64, (UWord)fd, (UWord)&buf64);
315 if (!(sr_isError(res) && sr_Err(res) == VKI_ENOSYS)) {
316 /* Success, or any failure except ENOSYS */
317 if (!sr_isError(res))
318 TRANSLATE_TO_vg_stat(vgbuf, &buf64);
319 return sr_isError(res) ? (-1) : 0;
322 # endif /* if defined(__NR_fstat64) */
323 { struct vki_stat buf;
324 res = VG_(do_syscall2)(__NR_fstat, (UWord)fd, (UWord)&buf);
325 if (!sr_isError(res))
326 TRANSLATE_TO_vg_stat(vgbuf, &buf);
327 return sr_isError(res) ? (-1) : 0;
335 #undef TRANSLATE_TO_vg_stat
338 Long VG_(fsize) ( Int fd )
341 Int res = VG_(fstat)( fd, &buf );
342 return (res == -1) ? (-1LL) : buf.size;
345 Bool VG_(is_dir) ( const HChar* f )
348 SysRes res = VG_(stat)(f, &buf);
349 return sr_isError(res) ? False
350 : VKI_S_ISDIR(buf.mode) ? True : False;
353 SysRes VG_(dup) ( Int oldfd )
355 return VG_(do_syscall1)(__NR_dup, oldfd);
358 SysRes VG_(dup2) ( Int oldfd, Int newfd )
360 # if defined(VGO_linux) || defined(VGO_darwin)
361 return VG_(do_syscall2)(__NR_dup2, oldfd, newfd);
367 /* Returns -1 on error. */
368 Int VG_(fcntl) ( Int fd, Int cmd, Addr arg )
370 # if defined(VGO_linux)
371 SysRes res = VG_(do_syscall3)(__NR_fcntl, fd, cmd, arg);
372 # elif defined(VGO_darwin)
373 SysRes res = VG_(do_syscall3)(__NR_fcntl_nocancel, fd, cmd, arg);
377 return sr_isError(res) ? -1 : sr_Res(res);
380 Int VG_(rename) ( const Char* old_name, const Char* new_name )
382 SysRes res = VG_(do_syscall2)(__NR_rename, (UWord)old_name, (UWord)new_name);
383 return sr_isError(res) ? (-1) : 0;
386 Int VG_(unlink) ( const Char* file_name )
388 SysRes res = VG_(do_syscall1)(__NR_unlink, (UWord)file_name);
389 return sr_isError(res) ? (-1) : 0;
392 /* The working directory at startup. AIX doesn't provide an easy
393 system call to do getcwd, but fortunately we don't need arbitrary
394 getcwd support. All that is really needed is to note the cwd at
395 process startup. Hence VG_(record_startup_wd) notes it (in a
396 platform dependent way) and VG_(get_startup_wd) produces the noted
398 static HChar startup_wd[VKI_PATH_MAX];
399 static Bool startup_wd_acquired = False;
401 /* Record the process' working directory at startup. Is intended to
402 be called exactly once, at startup, before the working directory
403 changes. Return True for success, False for failure, so that the
404 caller can bomb out suitably without creating module cycles if
405 there is a problem. */
406 Bool VG_(record_startup_wd) ( void )
408 const Int szB = sizeof(startup_wd);
409 vg_assert(!startup_wd_acquired);
410 vg_assert(szB >= 512 && szB <= 16384/*let's say*/); /* stay sane */
411 VG_(memset)(startup_wd, 0, szB);
412 # if defined(VGO_linux)
413 /* Simple: just ask the kernel */
415 = VG_(do_syscall2)(__NR_getcwd, (UWord)startup_wd, szB-1);
416 vg_assert(startup_wd[szB-1] == 0);
417 if (sr_isError(res)) {
420 startup_wd_acquired = True;
424 # elif defined(VGO_darwin)
425 /* We can't ask the kernel, so instead rely on launcher-*.c to
426 tell us the startup path. Note the env var is keyed to the
427 parent's PID, not ours, since our parent is the launcher
431 VG_(memset)(envvar, 0, sizeof(envvar));
432 VG_(sprintf)(envvar, "VALGRIND_STARTUP_PWD_%d_XYZZY",
433 (Int)VG_(getppid)());
434 wd = VG_(getenv)( envvar );
435 if (wd == NULL || (1+VG_(strlen)(wd) >= szB))
437 VG_(strncpy_safely)(startup_wd, wd, szB);
438 vg_assert(startup_wd[szB-1] == 0);
439 startup_wd_acquired = True;
447 /* Copy the previously acquired startup_wd into buf[0 .. size-1],
448 or return False if buf isn't big enough. */
449 Bool VG_(get_startup_wd) ( Char* buf, SizeT size )
451 vg_assert(startup_wd_acquired);
452 vg_assert(startup_wd[ sizeof(startup_wd)-1 ] == 0);
453 if (1+VG_(strlen)(startup_wd) >= size)
455 VG_(strncpy_safely)(buf, startup_wd, size);
459 Int VG_(poll) (struct vki_pollfd *fds, Int nfds, Int timeout)
462 # if defined(VGO_linux)
463 res = VG_(do_syscall3)(__NR_poll, (UWord)fds, nfds, timeout);
464 # elif defined(VGO_darwin)
465 res = VG_(do_syscall3)(__NR_poll_nocancel, (UWord)fds, nfds, timeout);
469 return sr_isError(res) ? -1 : sr_Res(res);
473 Int VG_(readlink) (const Char* path, Char* buf, UInt bufsiz)
476 /* res = readlink( path, buf, bufsiz ); */
477 res = VG_(do_syscall3)(__NR_readlink, (UWord)path, (UWord)buf, bufsiz);
478 return sr_isError(res) ? -1 : sr_Res(res);
481 Int VG_(getdents) (Int fd, struct vki_dirent *dirp, UInt count)
483 # if defined(VGO_linux)
485 /* res = getdents( fd, dirp, count ); */
486 res = VG_(do_syscall3)(__NR_getdents, fd, (UWord)dirp, count);
487 return sr_isError(res) ? -1 : sr_Res(res);
488 # elif defined(VGO_darwin)
495 /* Check accessibility of a file. Returns zero for access granted,
496 nonzero otherwise. */
497 Int VG_(access) ( const HChar* path, Bool irusr, Bool iwusr, Bool ixusr )
499 # if defined(VGO_linux)
500 /* Very annoyingly, I cannot find any definition for R_OK et al in
501 the kernel interfaces. Therefore I reluctantly resort to
502 hardwiring in these magic numbers that I determined by
509 UWord w = (irusr ? VKI_R_OK : 0)
510 | (iwusr ? VKI_W_OK : 0)
511 | (ixusr ? VKI_X_OK : 0);
512 SysRes res = VG_(do_syscall2)(__NR_access, (UWord)path, w);
513 return sr_isError(res) ? 1 : 0;
515 # if defined(VGO_linux)
523 Emulate the normal Unix permissions checking algorithm.
525 If owner matches, then use the owner permissions, else
526 if group matches, then use the group permissions, else
527 use other permissions.
529 Note that we can't deal properly with SUID/SGID. By default
530 (allow_setuid == False), we refuse to run them (otherwise the
531 executable may misbehave if it doesn't have the permissions it
532 thinks it does). However, the caller may indicate that setuid
533 executables are allowed, for example if we are going to exec them
534 but not trace into them (iow, client sys_execve when
535 clo_trace_children == False).
537 If VKI_EACCES is returned (iow, permission was refused), then
538 *is_setuid is set to True iff permission was refused because the
539 executable is setuid.
541 /* returns: 0 = success, non-0 is failure */
542 Int VG_(check_executable)(/*OUT*/Bool* is_setuid,
543 const HChar* f, Bool allow_setuid)
546 SysRes res = VG_(stat)(f, &st);
551 if (sr_isError(res)) {
555 if ( (st.mode & (VKI_S_ISUID | VKI_S_ISGID)) && !allow_setuid ) {
561 if (VG_(geteuid)() == st.uid) {
562 if (!(st.mode & VKI_S_IXUSR))
567 if (VG_(getegid)() == st.gid)
571 Int ngrp = VG_(getgroups)(32, groups);
573 /* ngrp will be -1 if VG_(getgroups) failed. */
574 for (i = 0; i < ngrp; i++) {
575 if (groups[i] == st.gid) {
583 if (!(st.mode & VKI_S_IXGRP)) {
586 } else if (!(st.mode & VKI_S_IXOTH)) {
594 SysRes VG_(pread) ( Int fd, void* buf, Int count, OffT offset )
597 // on 32 bits platforms, we receive a 32 bits OffT but
598 // we must extend it to pass a long long 64 bits.
599 # if defined(VGP_x86_linux)
600 vg_assert(sizeof(OffT) == 4);
601 res = VG_(do_syscall5)(__NR_pread64, fd, (UWord)buf, count,
602 offset, 0); // Little endian long long
604 # elif defined(VGP_arm_linux)
605 vg_assert(sizeof(OffT) == 4);
606 res = VG_(do_syscall5)(__NR_pread64, fd, (UWord)buf, count,
607 0, offset); // Big endian long long
609 # elif defined(VGP_ppc32_linux)
610 vg_assert(sizeof(OffT) == 4);
611 res = VG_(do_syscall6)(__NR_pread64, fd, (UWord)buf, count,
612 0, // Padding needed on PPC32
613 0, offset); // Big endian long long
615 # elif defined(VGP_mips32_linux) && VKI_LITTLE_ENDIAN
616 vg_assert(sizeof(OffT) == 4);
617 res = VG_(do_syscall6)(__NR_pread64, fd, (UWord)buf, count,
620 # elif defined(VGP_mips32_linux) && VKI_BIG_ENDIAN
621 vg_assert(sizeof(OffT) == 4);
622 res = VG_(do_syscall6)(__NR_pread64, fd, (UWord)buf, count,
625 # elif defined(VGP_amd64_linux) \
626 || defined(VGP_ppc64_linux) || defined(VGP_s390x_linux)
627 res = VG_(do_syscall4)(__NR_pread64, fd, (UWord)buf, count, offset);
629 # elif defined(VGP_amd64_darwin)
630 vg_assert(sizeof(OffT) == 8);
631 res = VG_(do_syscall4)(__NR_pread_nocancel, fd, (UWord)buf, count, offset);
633 # elif defined(VGP_x86_darwin)
634 vg_assert(sizeof(OffT) == 8);
635 res = VG_(do_syscall5)(__NR_pread_nocancel, fd, (UWord)buf, count,
636 offset & 0xffffffff, offset >> 32);
639 # error "Unknown platform"
643 /* Return the name of a directory for temporary files. */
644 const HChar *VG_(tmpdir)(void)
648 tmpdir = VG_(getenv)("TMPDIR");
649 if (tmpdir == NULL || *tmpdir == '\0') tmpdir = VG_TMPDIR;
650 if (tmpdir == NULL || *tmpdir == '\0') tmpdir = "/tmp"; /* fallback */
655 /* Create and open (-rw------) a tmp file name incorporating said arg.
656 Returns -1 on failure, else the fd of the file. If fullname is
657 non-NULL, the file's name is written into it. The number of bytes
658 written is guaranteed not to exceed 64+strlen(part_of_name). */
660 Int VG_(mkstemp) ( HChar* part_of_name, /*OUT*/HChar* fullname )
668 vg_assert(part_of_name);
669 n = VG_(strlen)(part_of_name);
670 vg_assert(n > 0 && n < 100);
672 seed = (VG_(getpid)() << 9) ^ VG_(getppid)();
674 /* Determine sensible location for temporary files */
675 tmpdir = VG_(tmpdir)();
681 VG_(sprintf)( buf, "%s/valgrind_%s_%08x",
682 tmpdir, part_of_name, VG_(random)( &seed ));
684 VG_(printf)("VG_(mkstemp): trying: %s\n", buf);
686 sres = VG_(open)(buf,
687 VKI_O_CREAT|VKI_O_RDWR|VKI_O_EXCL|VKI_O_TRUNC,
688 VKI_S_IRUSR|VKI_S_IWUSR);
689 if (sr_isError(sres)) {
690 VG_(umsg)("VG_(mkstemp): failed to create temp file: %s\n", buf);
693 /* VG_(safe_fd) doesn't return if it fails. */
694 fd = VG_(safe_fd)( sr_Res(sres) );
696 VG_(strcpy)( fullname, buf );
703 /* ---------------------------------------------------------------------
704 Socket-related stuff.
705 ------------------------------------------------------------------ */
708 Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port );
711 Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr, Int addrlen );
713 UInt VG_(htonl) ( UInt x )
715 # if defined(VG_BIGENDIAN)
719 (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8)
720 | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24);
724 UInt VG_(ntohl) ( UInt x )
726 # if defined(VG_BIGENDIAN)
730 (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8)
731 | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24);
735 UShort VG_(htons) ( UShort x )
737 # if defined(VG_BIGENDIAN)
741 (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8);
745 UShort VG_(ntohs) ( UShort x )
747 # if defined(VG_BIGENDIAN)
751 (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8);
756 /* The main function.
758 Supplied string contains either an ip address "192.168.0.1" or
759 an ip address and port pair, "192.168.0.1:1500". Parse these,
761 -1 if there is a parse error
762 -2 if no parse error, but specified host:port cannot be opened
763 the relevant file (socket) descriptor, otherwise.
766 Int VG_(connect_via_socket)( UChar* str )
768 # if defined(VGO_linux) || defined(VGO_darwin)
770 struct vki_sockaddr_in servAddr;
772 UShort port = VG_CLO_DEFAULT_LOGPORT;
773 Bool ok = parse_inet_addr_and_port(str, &ip, &port);
778 // VG_(printf)("ip = %d.%d.%d.%d, port %d\n",
779 // (ip >> 24) & 0xFF, (ip >> 16) & 0xFF,
780 // (ip >> 8) & 0xFF, ip & 0xFF,
783 servAddr.sin_family = VKI_AF_INET;
784 servAddr.sin_addr.s_addr = VG_(htonl)(ip);
785 servAddr.sin_port = VG_(htons)(port);
788 sd = VG_(socket)(VKI_AF_INET, VKI_SOCK_STREAM, 0 /* IPPROTO_IP ? */);
790 /* this shouldn't happen ... nevertheless */
794 /* connect to server */
795 res = my_connect(sd, &servAddr, sizeof(servAddr));
797 /* connection failed */
809 /* Let d = one or more digits. Accept either:
812 static Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port )
814 # define GET_CH ((*str) ? (*str++) : 0)
815 UInt ipa, i, j, c, any;
817 for (i = 0; i < 4; i++) {
822 if (c < '0' || c > '9') break;
823 j = 10 * j + (int)(c - '0');
826 if (any == 0 || j > 255) goto syntaxerr;
827 ipa = (ipa << 8) + j;
828 if (i <= 2 && c != '.') goto syntaxerr;
830 if (c == 0 || c == ':')
833 if (c != ':') goto syntaxerr;
838 if (c < '0' || c > '9') break;
839 j = j * 10 + (int)(c - '0');
841 if (j > 65535) goto syntaxerr;
843 if (any == 0 || c != 0) goto syntaxerr;
844 if (j < 1024) goto syntaxerr;
853 // GrP fixme safe_fd?
854 Int VG_(socket) ( Int domain, Int type, Int protocol )
856 # if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) \
857 || defined(VGP_ppc64_linux) || defined(VGP_s390x_linux)
863 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SOCKET, (UWord)&args);
864 return sr_isError(res) ? -1 : sr_Res(res);
866 # elif defined(VGP_amd64_linux) || defined(VGP_arm_linux) \
867 || defined(VGP_mips32_linux)
869 res = VG_(do_syscall3)(__NR_socket, domain, type, protocol );
870 return sr_isError(res) ? -1 : sr_Res(res);
872 # elif defined(VGO_darwin)
874 res = VG_(do_syscall3)(__NR_socket, domain, type, protocol);
875 if (!sr_isError(res)) {
876 // Set SO_NOSIGPIPE so write() returns EPIPE instead of raising SIGPIPE
879 res2 = VG_(do_syscall5)(__NR_setsockopt, sr_Res(res), VKI_SOL_SOCKET,
880 VKI_SO_NOSIGPIPE, (UWord)&optval,
882 // ignore setsockopt() error
884 return sr_isError(res) ? -1 : sr_Res(res);
887 # error "Unknown arch"
893 Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr, Int addrlen )
895 # if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) \
896 || defined(VGP_ppc64_linux) || defined(VGP_s390x_linux)
900 args[1] = (UWord)serv_addr;
902 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_CONNECT, (UWord)&args);
903 return sr_isError(res) ? -1 : sr_Res(res);
905 # elif defined(VGP_amd64_linux) || defined(VGP_arm_linux) \
906 || defined(VGP_mips32_linux)
908 res = VG_(do_syscall3)(__NR_connect, sockfd, (UWord)serv_addr, addrlen);
909 return sr_isError(res) ? -1 : sr_Res(res);
911 # elif defined(VGO_darwin)
913 res = VG_(do_syscall3)(__NR_connect_nocancel,
914 sockfd, (UWord)serv_addr, addrlen);
915 return sr_isError(res) ? -1 : sr_Res(res);
918 # error "Unknown arch"
922 Int VG_(write_socket)( Int sd, const void *msg, Int count )
924 /* This is actually send(). */
926 /* For Linux, VKI_MSG_NOSIGNAL is a request not to send SIGPIPE on
927 errors on stream oriented sockets when the other end breaks the
928 connection. The EPIPE error is still returned.
930 For Darwin, VG_(socket)() sets SO_NOSIGPIPE to get EPIPE instead of
933 # if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) \
934 || defined(VGP_ppc64_linux) || defined(VGP_s390x_linux)
938 args[1] = (UWord)msg;
940 args[3] = VKI_MSG_NOSIGNAL;
941 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SEND, (UWord)&args);
942 return sr_isError(res) ? -1 : sr_Res(res);
944 # elif defined(VGP_amd64_linux) || defined(VGP_arm_linux) \
945 || defined(VGP_mips32_linux)
947 res = VG_(do_syscall6)(__NR_sendto, sd, (UWord)msg,
948 count, VKI_MSG_NOSIGNAL, 0,0);
949 return sr_isError(res) ? -1 : sr_Res(res);
951 # elif defined(VGP_x86_darwin) || defined(VGP_amd64_darwin)
953 res = VG_(do_syscall3)(__NR_write_nocancel, sd, (UWord)msg, count);
954 return sr_isError(res) ? -1 : sr_Res(res);
957 # error "Unknown platform"
961 Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen)
963 # if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) \
964 || defined(VGP_ppc64_linux) || defined(VGP_s390x_linux) \
965 || defined(VGP_mips32_linux)
969 args[1] = (UWord)name;
970 args[2] = (UWord)namelen;
971 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKNAME, (UWord)&args);
972 return sr_isError(res) ? -1 : sr_Res(res);
974 # elif defined(VGP_amd64_linux) || defined(VGP_arm_linux)
976 res = VG_(do_syscall3)( __NR_getsockname,
977 (UWord)sd, (UWord)name, (UWord)namelen );
978 return sr_isError(res) ? -1 : sr_Res(res);
980 # elif defined(VGO_darwin)
982 res = VG_(do_syscall3)( __NR_getsockname,
983 (UWord)sd, (UWord)name, (UWord)namelen );
984 return sr_isError(res) ? -1 : sr_Res(res);
987 # error "Unknown platform"
991 Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen)
993 # if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) \
994 || defined(VGP_ppc64_linux) || defined(VGP_s390x_linux) \
995 || defined(VGP_mips32_linux)
999 args[1] = (UWord)name;
1000 args[2] = (UWord)namelen;
1001 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETPEERNAME, (UWord)&args);
1002 return sr_isError(res) ? -1 : sr_Res(res);
1004 # elif defined(VGP_amd64_linux) || defined(VGP_arm_linux)
1006 res = VG_(do_syscall3)( __NR_getpeername,
1007 (UWord)sd, (UWord)name, (UWord)namelen );
1008 return sr_isError(res) ? -1 : sr_Res(res);
1010 # elif defined(VGO_darwin)
1012 res = VG_(do_syscall3)( __NR_getpeername,
1013 (UWord)sd, (UWord)name, (UWord)namelen );
1014 return sr_isError(res) ? -1 : sr_Res(res);
1017 # error "Unknown platform"
1021 Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval,
1024 # if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) \
1025 || defined(VGP_ppc64_linux) || defined(VGP_s390x_linux)
1031 args[3] = (UWord)optval;
1032 args[4] = (UWord)optlen;
1033 res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKOPT, (UWord)&args);
1034 return sr_isError(res) ? -1 : sr_Res(res);
1036 # elif defined(VGP_amd64_linux) || defined(VGP_arm_linux) \
1037 || defined(VGP_mips32_linux)
1039 res = VG_(do_syscall5)( __NR_getsockopt,
1040 (UWord)sd, (UWord)level, (UWord)optname,
1041 (UWord)optval, (UWord)optlen );
1042 return sr_isError(res) ? -1 : sr_Res(res);
1044 # elif defined(VGO_darwin)
1046 res = VG_(do_syscall5)( __NR_getsockopt,
1047 (UWord)sd, (UWord)level, (UWord)optname,
1048 (UWord)optval, (UWord)optlen );
1049 return sr_isError(res) ? -1 : sr_Res(res);
1052 # error "Unknown platform"
1057 Char *VG_(basename)(const Char *path)
1059 static Char buf[VKI_PATH_MAX];
1061 const Char *p, *end;
1064 0 == VG_(strcmp)(path, ""))
1069 p = path + VG_(strlen)(path);
1070 while (p > path && *p == '/') {
1071 // skip all trailing '/'
1075 if (p == path && *p == '/') return "/"; // all slashes
1079 while (p > path && *p != '/') {
1086 VG_(strncpy)(buf, p, end-p+1);
1087 buf[end-p+1] = '\0';
1093 Char *VG_(dirname)(const Char *path)
1095 static Char buf[VKI_PATH_MAX];
1100 0 == VG_(strcmp)(path, "") ||
1101 0 == VG_(strcmp)(path, "/"))
1106 p = path + VG_(strlen)(path);
1107 while (p > path && *p == '/') {
1108 // skip all trailing '/'
1112 while (p > path && *p != '/') {
1118 if (*p == '/') return "/"; // all slashes
1119 else return "."; // no slashes
1122 while (p > path && *p == '/') {
1127 VG_(strncpy)(buf, path, p-path+1);
1128 buf[p-path+1] = '\0';
1134 /*--------------------------------------------------------------------*/
1136 /*--------------------------------------------------------------------*/