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 | |
| 31 | #include "core.h" |
| 32 | #include "pub_core_libcbase.h" |
| 33 | #include "pub_core_libcassert.h" |
| 34 | #include "pub_core_libcfile.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 35 | #include "pub_core_mallocfree.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 36 | #include "pub_core_options.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 37 | #include "vki_unistd.h" |
| 38 | |
| 39 | /* --------------------------------------------------------------------- |
| 40 | File stuff |
| 41 | ------------------------------------------------------------------ */ |
| 42 | |
njn | 63004dc | 2005-06-10 22:39:04 +0000 | [diff] [blame] | 43 | /* Application-visible file descriptor limits */ |
| 44 | Int VG_(fd_soft_limit) = -1; |
| 45 | Int VG_(fd_hard_limit) = -1; |
| 46 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 47 | static 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 */ |
| 55 | Int 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. */ |
| 72 | Int VG_(open) ( const Char* pathname, Int flags, Int mode ) |
| 73 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 74 | SysRes res = VG_(do_syscall3)(__NR_open, (UWord)pathname, flags, mode); |
| 75 | return res.isError ? -1 : res.val; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void VG_(close) ( Int fd ) |
| 79 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 80 | (void)VG_(do_syscall1)(__NR_close, fd); |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | Int VG_(read) ( Int fd, void* buf, Int count) |
| 84 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 85 | SysRes res = VG_(do_syscall3)(__NR_read, fd, (UWord)buf, count); |
| 86 | return res.isError ? -1 : res.val; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | Int VG_(write) ( Int fd, const void* buf, Int count) |
| 90 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 91 | SysRes res = VG_(do_syscall3)(__NR_write, fd, (UWord)buf, count); |
| 92 | return res.isError ? -1 : res.val; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | Int VG_(pipe) ( Int fd[2] ) |
| 96 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 97 | SysRes res = VG_(do_syscall1)(__NR_pipe, (UWord)fd); |
| 98 | return res.isError ? -1 : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | OffT VG_(lseek) ( Int fd, OffT offset, Int whence) |
| 102 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 103 | SysRes res = VG_(do_syscall3)(__NR_lseek, fd, offset, whence); |
| 104 | return res.isError ? (-1) : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | Int VG_(stat) ( Char* file_name, struct vki_stat* buf ) |
| 108 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 109 | SysRes res = VG_(do_syscall2)(__NR_stat, (UWord)file_name, (UWord)buf); |
| 110 | return res.isError ? (-1) : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | Int VG_(fstat) ( Int fd, struct vki_stat* buf ) |
| 114 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 115 | SysRes res = VG_(do_syscall2)(__NR_fstat, fd, (UWord)buf); |
| 116 | return res.isError ? (-1) : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | Int VG_(dup2) ( Int oldfd, Int newfd ) |
| 120 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 121 | SysRes res = VG_(do_syscall2)(__NR_dup2, oldfd, newfd); |
| 122 | return res.isError ? (-1) : res.val; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 123 | } |
| 124 | |
njn | 327fe8a | 2005-06-12 02:49:35 +0000 | [diff] [blame] | 125 | /* Returns -1 on error. */ |
| 126 | Int 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 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 132 | Int VG_(rename) ( Char* old_name, Char* new_name ) |
| 133 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 134 | SysRes res = VG_(do_syscall2)(__NR_rename, (UWord)old_name, (UWord)new_name); |
| 135 | return res.isError ? (-1) : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | Int VG_(unlink) ( Char* file_name ) |
| 139 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 140 | SysRes res = VG_(do_syscall1)(__NR_unlink, (UWord)file_name); |
| 141 | return res.isError ? (-1) : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 142 | } |
| 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() */ |
| 146 | Char* VG_(getcwd) ( Char* buf, SizeT size ) |
| 147 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 148 | SysRes res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 149 | vg_assert(buf != NULL); |
| 150 | res = VG_(do_syscall2)(__NR_getcwd, (UWord)buf, size); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 151 | return res.isError ? ((Char*)NULL) : (Char*)res.val; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* Alternative version that does allocate the memory. Easier to use. */ |
| 155 | Bool 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 | |
| 173 | Int VG_(readlink) (Char* path, Char* buf, UInt bufsiz) |
| 174 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 175 | SysRes res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 176 | /* res = readlink( path, buf, bufsiz ); */ |
| 177 | res = VG_(do_syscall3)(__NR_readlink, (UWord)path, (UWord)buf, bufsiz); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 178 | return res.isError ? -1 : res.val; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | Int VG_(getdents) (UInt fd, struct vki_dirent *dirp, UInt count) |
| 182 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 183 | SysRes res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 184 | /* res = getdents( fd, dirp, count ); */ |
| 185 | res = VG_(do_syscall3)(__NR_getdents, fd, (UWord)dirp, count); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 186 | return res.isError ? -1 : res.val; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* --------------------------------------------------------------------- |
| 190 | Socket-related stuff. This is very Linux-kernel specific. |
| 191 | ------------------------------------------------------------------ */ |
| 192 | |
| 193 | static |
| 194 | Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port ); |
| 195 | |
| 196 | static |
| 197 | Int my_socket ( Int domain, Int type, Int protocol ); |
| 198 | |
| 199 | static |
| 200 | Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr, |
| 201 | Int addrlen ); |
| 202 | |
| 203 | static |
| 204 | UInt 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 | |
| 211 | static |
| 212 | UShort 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 | */ |
| 229 | Int 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 | */ |
| 271 | Int 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 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 312 | static |
| 313 | Int my_socket ( Int domain, Int type, Int protocol ) |
| 314 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 315 | # if defined(VGP_x86_linux) |
| 316 | SysRes res; |
| 317 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 318 | args[0] = domain; |
| 319 | args[1] = type; |
| 320 | args[2] = protocol; |
| 321 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SOCKET, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 322 | 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 |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static |
| 331 | Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr, |
| 332 | Int addrlen ) |
| 333 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 334 | # if defined(VGP_x86_linux) |
| 335 | SysRes res; |
| 336 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 337 | 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); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 341 | 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 |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | Int VG_(write_socket)( Int sd, void *msg, Int count ) |
| 350 | { |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 351 | /* This is actually send(). */ |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 352 | /* 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 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 357 | # if defined(VGP_x86_linux) |
| 358 | SysRes res; |
| 359 | UWord args[4]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 360 | 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); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 365 | 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 |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen) |
| 374 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 375 | SysRes res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 376 | |
| 377 | # if defined(VGP_x86_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 378 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 379 | 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); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 383 | return res.isError ? -1 : res.val; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 384 | |
| 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 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 390 | # else |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 391 | I_die_here; |
| 392 | # endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen) |
| 396 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 397 | SysRes res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 398 | |
| 399 | # if defined(VGP_x86_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 400 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 401 | 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); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 405 | return res.isError ? -1 : res.val; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 406 | |
| 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 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 412 | # else |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 413 | I_die_here; |
| 414 | # endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval, |
| 418 | Int *optlen) |
| 419 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 420 | SysRes res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 421 | |
| 422 | # if defined(VGP_x86_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 423 | UWord args[5]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 424 | 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); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 430 | return res.isError ? -1 : res.val; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 431 | |
| 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 | |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 438 | # else |
| 439 | I_die_here; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 440 | # endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | |
| 444 | |
| 445 | /*--------------------------------------------------------------------*/ |
| 446 | /*--- end ---*/ |
| 447 | /*--------------------------------------------------------------------*/ |
| 448 | |