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