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