njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 1 | |
| 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 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 31 | #include "pub_core_basics.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 32 | #include "pub_core_libcbase.h" |
| 33 | #include "pub_core_libcassert.h" |
| 34 | #include "pub_core_libcfile.h" |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 35 | #include "pub_core_libcprint.h" // VG_(sprintf) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 36 | #include "pub_core_libcproc.h" // VG_(getpid), VG_(getppid) |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 37 | #include "pub_core_clientstate.h" // VG_(fd_hard_limit) |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 38 | #include "pub_core_syscall.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 39 | #include "vki_unistd.h" |
| 40 | |
| 41 | /* --------------------------------------------------------------------- |
| 42 | File stuff |
| 43 | ------------------------------------------------------------------ */ |
| 44 | |
| 45 | static inline Bool fd_exists(Int fd) |
| 46 | { |
| 47 | struct vki_stat st; |
| 48 | |
| 49 | return VG_(fstat)(fd, &st) == 0; |
| 50 | } |
| 51 | |
| 52 | /* Move an fd into the Valgrind-safe range */ |
| 53 | Int VG_(safe_fd)(Int oldfd) |
| 54 | { |
| 55 | Int newfd; |
| 56 | |
| 57 | vg_assert(VG_(fd_hard_limit) != -1); |
| 58 | |
| 59 | newfd = VG_(fcntl)(oldfd, VKI_F_DUPFD, VG_(fd_hard_limit)); |
| 60 | if (newfd != -1) |
| 61 | VG_(close)(oldfd); |
| 62 | |
| 63 | VG_(fcntl)(newfd, VKI_F_SETFD, VKI_FD_CLOEXEC); |
| 64 | |
| 65 | vg_assert(newfd >= VG_(fd_hard_limit)); |
| 66 | return newfd; |
| 67 | } |
| 68 | |
njn | ae7359b | 2005-06-19 21:10:42 +0000 | [diff] [blame] | 69 | /* Given a file descriptor, attempt to deduce its filename. To do |
| 70 | this, we use /proc/self/fd/<FD>. If this doesn't point to a file, |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 71 | or if it doesn't exist, we return False. */ |
| 72 | Bool VG_(resolve_filename) ( Int fd, HChar* buf, Int n_buf ) |
njn | ae7359b | 2005-06-19 21:10:42 +0000 | [diff] [blame] | 73 | { |
| 74 | HChar tmp[64]; |
| 75 | |
| 76 | VG_(sprintf)(tmp, "/proc/self/fd/%d", fd); |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 77 | VG_(memset)(buf, 0, n_buf); |
njn | ae7359b | 2005-06-19 21:10:42 +0000 | [diff] [blame] | 78 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 79 | if (VG_(readlink)(tmp, buf, n_buf) > 0 && buf[0] == '/') |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 80 | return True; |
| 81 | else |
| 82 | return False; |
njn | ae7359b | 2005-06-19 21:10:42 +0000 | [diff] [blame] | 83 | } |
| 84 | |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 85 | SysRes VG_(open) ( const Char* pathname, Int flags, Int mode ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 86 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 87 | SysRes res = VG_(do_syscall3)(__NR_open, (UWord)pathname, flags, mode); |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 88 | return res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void VG_(close) ( Int fd ) |
| 92 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 93 | (void)VG_(do_syscall1)(__NR_close, fd); |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | Int VG_(read) ( Int fd, void* buf, Int count) |
| 97 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 98 | SysRes res = VG_(do_syscall3)(__NR_read, fd, (UWord)buf, count); |
| 99 | return res.isError ? -1 : res.val; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | Int VG_(write) ( Int fd, const void* buf, Int count) |
| 103 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 104 | SysRes res = VG_(do_syscall3)(__NR_write, fd, (UWord)buf, count); |
| 105 | return res.isError ? -1 : res.val; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | Int VG_(pipe) ( Int fd[2] ) |
| 109 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 110 | SysRes res = VG_(do_syscall1)(__NR_pipe, (UWord)fd); |
| 111 | return res.isError ? -1 : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 112 | } |
| 113 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 114 | OffT VG_(lseek) ( Int fd, OffT offset, Int whence ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 115 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 116 | SysRes res = VG_(do_syscall3)(__NR_lseek, fd, offset, whence); |
| 117 | return res.isError ? (-1) : 0; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 118 | /* if you change the error-reporting conventions of this, also |
| 119 | change VG_(pread) and all other usage points. */ |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 120 | } |
| 121 | |
tom | 6c93c4f | 2005-08-05 07:46:32 +0000 | [diff] [blame] | 122 | SysRes VG_(stat) ( Char* file_name, struct vki_stat* buf ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 123 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 124 | SysRes res = VG_(do_syscall2)(__NR_stat, (UWord)file_name, (UWord)buf); |
tom | 6c93c4f | 2005-08-05 07:46:32 +0000 | [diff] [blame] | 125 | return res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | Int VG_(fstat) ( Int fd, struct vki_stat* buf ) |
| 129 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 130 | SysRes res = VG_(do_syscall2)(__NR_fstat, fd, (UWord)buf); |
| 131 | return res.isError ? (-1) : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 132 | } |
| 133 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 134 | Int VG_(fsize) ( Int fd ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 135 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 136 | struct vki_stat buf; |
| 137 | SysRes res = VG_(do_syscall2)(__NR_fstat, fd, (UWord)&buf); |
| 138 | return res.isError ? (-1) : buf.st_size; |
| 139 | } |
| 140 | |
| 141 | SysRes VG_(dup) ( Int oldfd ) |
| 142 | { |
| 143 | return VG_(do_syscall1)(__NR_dup, oldfd); |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 144 | } |
| 145 | |
njn | 327fe8a | 2005-06-12 02:49:35 +0000 | [diff] [blame] | 146 | /* Returns -1 on error. */ |
| 147 | Int VG_(fcntl) ( Int fd, Int cmd, Int arg ) |
| 148 | { |
| 149 | SysRes res = VG_(do_syscall3)(__NR_fcntl, fd, cmd, arg); |
| 150 | return res.isError ? -1 : res.val; |
| 151 | } |
| 152 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 153 | Int VG_(rename) ( Char* old_name, Char* new_name ) |
| 154 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 155 | SysRes res = VG_(do_syscall2)(__NR_rename, (UWord)old_name, (UWord)new_name); |
| 156 | return res.isError ? (-1) : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | Int VG_(unlink) ( Char* file_name ) |
| 160 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 161 | SysRes res = VG_(do_syscall1)(__NR_unlink, (UWord)file_name); |
| 162 | return res.isError ? (-1) : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Nb: we do not allow the Linux extension which malloc()s memory for the |
| 166 | buffer if buf==NULL, because we don't want Linux calling malloc() */ |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 167 | Bool VG_(getcwd) ( Char* buf, SizeT size ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 168 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 169 | SysRes res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 170 | vg_assert(buf != NULL); |
| 171 | res = VG_(do_syscall2)(__NR_getcwd, (UWord)buf, size); |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 172 | return res.isError ? False : True; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | Int VG_(readlink) (Char* path, Char* buf, UInt bufsiz) |
| 176 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 177 | SysRes res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 178 | /* res = readlink( path, buf, bufsiz ); */ |
| 179 | res = VG_(do_syscall3)(__NR_readlink, (UWord)path, (UWord)buf, bufsiz); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 180 | return res.isError ? -1 : res.val; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | Int VG_(getdents) (UInt fd, struct vki_dirent *dirp, UInt count) |
| 184 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 185 | SysRes res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 186 | /* res = getdents( fd, dirp, count ); */ |
| 187 | res = VG_(do_syscall3)(__NR_getdents, fd, (UWord)dirp, count); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 188 | return res.isError ? -1 : res.val; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 189 | } |
| 190 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 191 | /* Check accessibility of a file. Returns zero for access granted, |
| 192 | nonzero otherwise. */ |
| 193 | Int VG_(access) ( HChar* path, Bool irusr, Bool iwusr, Bool ixusr ) |
| 194 | { |
| 195 | #if defined(VGO_linux) |
| 196 | /* Very annoyingly, I cannot find any definition for R_OK et al in |
| 197 | the kernel interfaces. Therefore I reluctantly resort to |
| 198 | hardwiring in these magic numbers that I determined by |
| 199 | experimentation. */ |
| 200 | UWord w = (irusr ? 4/*R_OK*/ : 0) |
| 201 | | (iwusr ? 2/*W_OK*/ : 0) |
| 202 | | (ixusr ? 1/*X_OK*/ : 0); |
| 203 | SysRes res = VG_(do_syscall2)(__NR_access, (UWord)path, w); |
| 204 | return res.isError ? 1 : res.val; |
| 205 | #else |
| 206 | # error "Don't know how to do VG_(access) on this OS" |
| 207 | #endif |
| 208 | } |
| 209 | |
| 210 | SysRes VG_(pread) ( Int fd, void* buf, Int count, Int offset ) |
| 211 | { |
| 212 | OffT off = VG_(lseek)( fd, (OffT)offset, VKI_SEEK_SET); |
| 213 | if (off != 0) |
| 214 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
tom | 60a4b0b | 2005-10-12 10:45:27 +0000 | [diff] [blame^] | 215 | return VG_(do_syscall3)(__NR_read, fd, (UWord)buf, count ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* Create and open (-rw------) a tmp file name incorporating said arg. |
| 219 | Returns -1 on failure, else the fd of the file. If fullname is |
| 220 | non-NULL, the file's name is written into it. The number of bytes |
| 221 | written is guaranteed not to exceed 64+strlen(part_of_name). */ |
| 222 | |
| 223 | Int VG_(mkstemp) ( HChar* part_of_name, /*OUT*/HChar* fullname ) |
| 224 | { |
| 225 | HChar buf[200]; |
| 226 | Int n, tries, fd; |
| 227 | UInt seed; |
| 228 | SysRes sres; |
| 229 | |
| 230 | vg_assert(part_of_name); |
| 231 | n = VG_(strlen)(part_of_name); |
| 232 | vg_assert(n > 0 && n < 100); |
| 233 | |
| 234 | seed = (VG_(getpid)() << 9) ^ VG_(getppid)(); |
| 235 | |
| 236 | tries = 0; |
| 237 | while (True) { |
| 238 | if (tries > 10) |
| 239 | return -1; |
| 240 | VG_(sprintf)( buf, "/tmp/valgrind_%s_%08x", |
| 241 | part_of_name, VG_(random)( &seed )); |
| 242 | if (0) |
| 243 | VG_(printf)("VG_(mkstemp): trying: %s\n", buf); |
| 244 | |
| 245 | sres = VG_(open)(buf, |
| 246 | VKI_O_CREAT|VKI_O_RDWR|VKI_O_EXCL|VKI_O_TRUNC, |
| 247 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 248 | if (sres.isError) |
| 249 | continue; |
| 250 | /* VG_(safe_fd) doesn't return if it fails. */ |
| 251 | fd = VG_(safe_fd)( sres.val ); |
| 252 | if (fullname) |
| 253 | VG_(strcpy)( fullname, buf ); |
| 254 | return fd; |
| 255 | } |
| 256 | /* NOTREACHED */ |
| 257 | } |
| 258 | |
| 259 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 260 | /* --------------------------------------------------------------------- |
| 261 | Socket-related stuff. This is very Linux-kernel specific. |
| 262 | ------------------------------------------------------------------ */ |
| 263 | |
| 264 | static |
| 265 | Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port ); |
| 266 | |
| 267 | static |
| 268 | Int my_socket ( Int domain, Int type, Int protocol ); |
| 269 | |
| 270 | static |
| 271 | Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr, |
| 272 | Int addrlen ); |
| 273 | |
| 274 | static |
| 275 | UInt my_htonl ( UInt x ) |
| 276 | { |
| 277 | return |
| 278 | (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8) |
| 279 | | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24); |
| 280 | } |
| 281 | |
| 282 | static |
| 283 | UShort my_htons ( UShort x ) |
| 284 | { |
| 285 | return |
| 286 | (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /* The main function. |
| 291 | |
| 292 | Supplied string contains either an ip address "192.168.0.1" or |
| 293 | an ip address and port pair, "192.168.0.1:1500". Parse these, |
| 294 | and return: |
| 295 | -1 if there is a parse error |
| 296 | -2 if no parse error, but specified host:port cannot be opened |
| 297 | the relevant file (socket) descriptor, otherwise. |
| 298 | is used. |
| 299 | */ |
| 300 | Int VG_(connect_via_socket)( UChar* str ) |
| 301 | { |
| 302 | Int sd, res; |
| 303 | struct vki_sockaddr_in servAddr; |
| 304 | UInt ip = 0; |
| 305 | UShort port = VG_CLO_DEFAULT_LOGPORT; |
| 306 | Bool ok = parse_inet_addr_and_port(str, &ip, &port); |
| 307 | if (!ok) |
| 308 | return -1; |
| 309 | |
| 310 | //if (0) |
| 311 | // VG_(printf)("ip = %d.%d.%d.%d, port %d\n", |
| 312 | // (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, |
| 313 | // (ip >> 8) & 0xFF, ip & 0xFF, |
| 314 | // (UInt)port ); |
| 315 | |
| 316 | servAddr.sin_family = VKI_AF_INET; |
| 317 | servAddr.sin_addr.s_addr = my_htonl(ip); |
| 318 | servAddr.sin_port = my_htons(port); |
| 319 | |
| 320 | /* create socket */ |
| 321 | sd = my_socket(VKI_AF_INET, VKI_SOCK_STREAM, 0 /* IPPROTO_IP ? */); |
| 322 | if (sd < 0) { |
| 323 | /* this shouldn't happen ... nevertheless */ |
| 324 | return -2; |
| 325 | } |
| 326 | |
| 327 | /* connect to server */ |
| 328 | res = my_connect(sd, (struct vki_sockaddr_in *) &servAddr, |
| 329 | sizeof(servAddr)); |
| 330 | if (res < 0) { |
| 331 | /* connection failed */ |
| 332 | return -2; |
| 333 | } |
| 334 | |
| 335 | return sd; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | /* Let d = one or more digits. Accept either: |
| 340 | d.d.d.d or d.d.d.d:d |
| 341 | */ |
| 342 | Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port ) |
| 343 | { |
| 344 | # define GET_CH ((*str) ? (*str++) : 0) |
| 345 | UInt ipa, i, j, c, any; |
| 346 | ipa = 0; |
| 347 | for (i = 0; i < 4; i++) { |
| 348 | j = 0; |
| 349 | any = 0; |
| 350 | while (1) { |
| 351 | c = GET_CH; |
| 352 | if (c < '0' || c > '9') break; |
| 353 | j = 10 * j + (int)(c - '0'); |
| 354 | any = 1; |
| 355 | } |
| 356 | if (any == 0 || j > 255) goto syntaxerr; |
| 357 | ipa = (ipa << 8) + j; |
| 358 | if (i <= 2 && c != '.') goto syntaxerr; |
| 359 | } |
| 360 | if (c == 0 || c == ':') |
| 361 | *ip_addr = ipa; |
| 362 | if (c == 0) goto ok; |
| 363 | if (c != ':') goto syntaxerr; |
| 364 | j = 0; |
| 365 | any = 0; |
| 366 | while (1) { |
| 367 | c = GET_CH; |
| 368 | if (c < '0' || c > '9') break; |
| 369 | j = j * 10 + (int)(c - '0'); |
| 370 | any = 1; |
| 371 | if (j > 65535) goto syntaxerr; |
| 372 | } |
| 373 | if (any == 0 || c != 0) goto syntaxerr; |
| 374 | if (j < 1024) goto syntaxerr; |
| 375 | *port = (UShort)j; |
| 376 | ok: |
| 377 | return 1; |
| 378 | syntaxerr: |
| 379 | return 0; |
| 380 | # undef GET_CH |
| 381 | } |
| 382 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 383 | static |
| 384 | Int my_socket ( Int domain, Int type, Int protocol ) |
| 385 | { |
sewardj | 21ccba8 | 2005-10-07 23:04:17 +0000 | [diff] [blame] | 386 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 387 | SysRes res; |
| 388 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 389 | args[0] = domain; |
| 390 | args[1] = type; |
| 391 | args[2] = protocol; |
| 392 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SOCKET, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 393 | return res.isError ? -1 : res.val; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 394 | |
| 395 | #elif defined(VGP_amd64_linux) |
sewardj | 1e86656 | 2005-09-28 19:58:58 +0000 | [diff] [blame] | 396 | SysRes res; |
| 397 | res = VG_(do_syscall3)(__NR_socket, domain, type, protocol ); |
| 398 | return res.isError ? -1 : res.val; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 399 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 400 | #else |
| 401 | # error Unknown arch |
| 402 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static |
| 406 | Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr, |
| 407 | Int addrlen ) |
| 408 | { |
sewardj | 21ccba8 | 2005-10-07 23:04:17 +0000 | [diff] [blame] | 409 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 410 | SysRes res; |
| 411 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 412 | args[0] = sockfd; |
| 413 | args[1] = (UWord)serv_addr; |
| 414 | args[2] = addrlen; |
| 415 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_CONNECT, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 416 | return res.isError ? -1 : res.val; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 417 | |
| 418 | #elif defined(VGP_amd64_linux) |
sewardj | 1e86656 | 2005-09-28 19:58:58 +0000 | [diff] [blame] | 419 | SysRes res; |
| 420 | res = VG_(do_syscall3)(__NR_connect, sockfd, (UWord)serv_addr, addrlen); |
| 421 | return res.isError ? -1 : res.val; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 422 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 423 | #else |
| 424 | # error Unknown arch |
| 425 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | Int VG_(write_socket)( Int sd, void *msg, Int count ) |
| 429 | { |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 430 | /* This is actually send(). */ |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 431 | /* Requests not to send SIGPIPE on errors on stream oriented |
| 432 | sockets when the other end breaks the connection. The EPIPE |
| 433 | error is still returned. */ |
| 434 | Int flags = VKI_MSG_NOSIGNAL; |
| 435 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 436 | #if defined(VGP_x86_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 437 | SysRes res; |
| 438 | UWord args[4]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 439 | args[0] = sd; |
| 440 | args[1] = (UWord)msg; |
| 441 | args[2] = count; |
| 442 | args[3] = flags; |
| 443 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SEND, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 444 | return res.isError ? -1 : res.val; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 445 | |
| 446 | #elif defined(VGP_amd64_linux) |
sewardj | 1e86656 | 2005-09-28 19:58:58 +0000 | [diff] [blame] | 447 | SysRes res; |
| 448 | res = VG_(do_syscall6)(__NR_sendto, sd, (UWord)msg, count, flags, 0,0); |
| 449 | return res.isError ? -1 : res.val; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 450 | |
| 451 | #elif defined(VGP_ppc32_linux) |
sewardj | 8a85128 | 2005-07-02 10:13:05 +0000 | [diff] [blame] | 452 | //CAB: TODO |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 453 | I_die_here; |
| 454 | flags = 0; // stop compiler complaints |
| 455 | |
| 456 | #else |
| 457 | # error Unknown arch |
| 458 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen) |
| 462 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 463 | SysRes res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 464 | |
sewardj | 8a85128 | 2005-07-02 10:13:05 +0000 | [diff] [blame] | 465 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 466 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 467 | args[0] = sd; |
| 468 | args[1] = (UWord)name; |
| 469 | args[2] = (UWord)namelen; |
| 470 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKNAME, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 471 | return res.isError ? -1 : res.val; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 472 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 473 | #elif defined(VGP_amd64_linux) |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 474 | res = VG_(do_syscall3)( __NR_getsockname, |
| 475 | (UWord)sd, (UWord)name, (UWord)namelen ); |
| 476 | return res.isError ? -1 : res.val; |
| 477 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 478 | #else |
| 479 | # error Unknown arch |
| 480 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen) |
| 484 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 485 | SysRes res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 486 | |
sewardj | 8a85128 | 2005-07-02 10:13:05 +0000 | [diff] [blame] | 487 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 488 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 489 | args[0] = sd; |
| 490 | args[1] = (UWord)name; |
| 491 | args[2] = (UWord)namelen; |
| 492 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETPEERNAME, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 493 | return res.isError ? -1 : res.val; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 494 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 495 | #elif defined(VGP_amd64_linux) |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 496 | res = VG_(do_syscall3)( __NR_getpeername, |
| 497 | (UWord)sd, (UWord)name, (UWord)namelen ); |
| 498 | return res.isError ? -1 : res.val; |
| 499 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 500 | #else |
sewardj | 8a85128 | 2005-07-02 10:13:05 +0000 | [diff] [blame] | 501 | # error Unknown archx |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 502 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval, |
| 506 | Int *optlen) |
| 507 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 508 | SysRes res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 509 | |
sewardj | 8a85128 | 2005-07-02 10:13:05 +0000 | [diff] [blame] | 510 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 511 | UWord args[5]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 512 | args[0] = sd; |
| 513 | args[1] = level; |
| 514 | args[2] = optname; |
| 515 | args[3] = (UWord)optval; |
| 516 | args[4] = (UWord)optlen; |
| 517 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKOPT, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 518 | return res.isError ? -1 : res.val; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 519 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 520 | #elif defined(VGP_amd64_linux) |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 521 | res = VG_(do_syscall5)( __NR_getsockopt, |
| 522 | (UWord)sd, (UWord)level, (UWord)optname, |
| 523 | (UWord)optval, (UWord)optlen ); |
| 524 | return res.isError ? -1 : res.val; |
| 525 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 526 | #else |
| 527 | # error Unknown arch |
| 528 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
| 532 | |
| 533 | /*--------------------------------------------------------------------*/ |
| 534 | /*--- end ---*/ |
| 535 | /*--------------------------------------------------------------------*/ |
| 536 | |