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); |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 117 | return res.isError ? (-1) : res.val; |
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 | |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 141 | Bool VG_(is_dir) ( HChar* f ) |
| 142 | { |
| 143 | struct vki_stat buf; |
| 144 | SysRes res = VG_(do_syscall2)(__NR_stat, (UWord)f, (UWord)&buf); |
| 145 | return res.isError ? False |
| 146 | : VKI_S_ISDIR(buf.st_mode) ? True : False; |
| 147 | } |
| 148 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 149 | SysRes VG_(dup) ( Int oldfd ) |
| 150 | { |
| 151 | return VG_(do_syscall1)(__NR_dup, oldfd); |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 152 | } |
| 153 | |
njn | 327fe8a | 2005-06-12 02:49:35 +0000 | [diff] [blame] | 154 | /* Returns -1 on error. */ |
| 155 | Int VG_(fcntl) ( Int fd, Int cmd, Int arg ) |
| 156 | { |
| 157 | SysRes res = VG_(do_syscall3)(__NR_fcntl, fd, cmd, arg); |
| 158 | return res.isError ? -1 : res.val; |
| 159 | } |
| 160 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 161 | Int VG_(rename) ( Char* old_name, Char* new_name ) |
| 162 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 163 | SysRes res = VG_(do_syscall2)(__NR_rename, (UWord)old_name, (UWord)new_name); |
| 164 | return res.isError ? (-1) : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | Int VG_(unlink) ( Char* file_name ) |
| 168 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 169 | SysRes res = VG_(do_syscall1)(__NR_unlink, (UWord)file_name); |
| 170 | return res.isError ? (-1) : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* Nb: we do not allow the Linux extension which malloc()s memory for the |
| 174 | buffer if buf==NULL, because we don't want Linux calling malloc() */ |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 175 | Bool VG_(getcwd) ( Char* buf, SizeT size ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 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 | vg_assert(buf != NULL); |
| 179 | res = VG_(do_syscall2)(__NR_getcwd, (UWord)buf, size); |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 180 | return res.isError ? False : True; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | Int VG_(readlink) (Char* path, Char* buf, UInt bufsiz) |
| 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 = readlink( path, buf, bufsiz ); */ |
| 187 | res = VG_(do_syscall3)(__NR_readlink, (UWord)path, (UWord)buf, bufsiz); |
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 | |
| 191 | Int VG_(getdents) (UInt fd, struct vki_dirent *dirp, UInt count) |
| 192 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 193 | SysRes res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 194 | /* res = getdents( fd, dirp, count ); */ |
| 195 | res = VG_(do_syscall3)(__NR_getdents, fd, (UWord)dirp, count); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 196 | return res.isError ? -1 : res.val; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 197 | } |
| 198 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 199 | /* Check accessibility of a file. Returns zero for access granted, |
| 200 | nonzero otherwise. */ |
| 201 | Int VG_(access) ( HChar* path, Bool irusr, Bool iwusr, Bool ixusr ) |
| 202 | { |
| 203 | #if defined(VGO_linux) |
| 204 | /* Very annoyingly, I cannot find any definition for R_OK et al in |
| 205 | the kernel interfaces. Therefore I reluctantly resort to |
| 206 | hardwiring in these magic numbers that I determined by |
| 207 | experimentation. */ |
| 208 | UWord w = (irusr ? 4/*R_OK*/ : 0) |
| 209 | | (iwusr ? 2/*W_OK*/ : 0) |
| 210 | | (ixusr ? 1/*X_OK*/ : 0); |
| 211 | SysRes res = VG_(do_syscall2)(__NR_access, (UWord)path, w); |
| 212 | return res.isError ? 1 : res.val; |
| 213 | #else |
| 214 | # error "Don't know how to do VG_(access) on this OS" |
| 215 | #endif |
| 216 | } |
| 217 | |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 218 | /* |
| 219 | Emulate the normal Unix permissions checking algorithm. |
| 220 | |
| 221 | If owner matches, then use the owner permissions, else |
| 222 | if group matches, then use the group permissions, else |
| 223 | use other permissions. |
| 224 | |
| 225 | Note that we can't deal with SUID/SGID, so we refuse to run them |
| 226 | (otherwise the executable may misbehave if it doesn't have the |
| 227 | permissions it thinks it does). |
| 228 | */ |
| 229 | /* returns: 0 = success, non-0 is failure */ |
| 230 | Int VG_(check_executable)(HChar* f) |
| 231 | { |
| 232 | struct vki_stat st; |
| 233 | SysRes res; |
| 234 | |
| 235 | res = VG_(stat)(f, &st); |
| 236 | if (res.isError) { |
| 237 | return res.val; |
| 238 | } |
| 239 | |
| 240 | if (st.st_mode & (VKI_S_ISUID | VKI_S_ISGID)) { |
| 241 | //VG_(printf)("Can't execute suid/sgid executable %s\n", exe); |
| 242 | return VKI_EACCES; |
| 243 | } |
| 244 | |
| 245 | if (VG_(geteuid)() == st.st_uid) { |
| 246 | if (!(st.st_mode & VKI_S_IXUSR)) |
| 247 | return VKI_EACCES; |
| 248 | } else { |
| 249 | int grpmatch = 0; |
| 250 | |
| 251 | if (VG_(getegid)() == st.st_gid) |
| 252 | grpmatch = 1; |
| 253 | else { |
| 254 | UInt groups[32]; |
| 255 | Int ngrp = VG_(getgroups)(32, groups); |
| 256 | Int i; |
| 257 | /* ngrp will be -1 if VG_(getgroups) failed. */ |
| 258 | for (i = 0; i < ngrp; i++) { |
| 259 | if (groups[i] == st.st_gid) { |
| 260 | grpmatch = 1; |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (grpmatch) { |
| 267 | if (!(st.st_mode & VKI_S_IXGRP)) { |
| 268 | return VKI_EACCES; |
| 269 | } |
| 270 | } else if (!(st.st_mode & VKI_S_IXOTH)) { |
| 271 | return VKI_EACCES; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 278 | SysRes VG_(pread) ( Int fd, void* buf, Int count, Int offset ) |
| 279 | { |
| 280 | OffT off = VG_(lseek)( fd, (OffT)offset, VKI_SEEK_SET); |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 281 | if (off < 0) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 282 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
tom | 60a4b0b | 2005-10-12 10:45:27 +0000 | [diff] [blame] | 283 | return VG_(do_syscall3)(__NR_read, fd, (UWord)buf, count ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* Create and open (-rw------) a tmp file name incorporating said arg. |
| 287 | Returns -1 on failure, else the fd of the file. If fullname is |
| 288 | non-NULL, the file's name is written into it. The number of bytes |
| 289 | written is guaranteed not to exceed 64+strlen(part_of_name). */ |
| 290 | |
| 291 | Int VG_(mkstemp) ( HChar* part_of_name, /*OUT*/HChar* fullname ) |
| 292 | { |
| 293 | HChar buf[200]; |
| 294 | Int n, tries, fd; |
| 295 | UInt seed; |
| 296 | SysRes sres; |
| 297 | |
| 298 | vg_assert(part_of_name); |
| 299 | n = VG_(strlen)(part_of_name); |
| 300 | vg_assert(n > 0 && n < 100); |
| 301 | |
| 302 | seed = (VG_(getpid)() << 9) ^ VG_(getppid)(); |
| 303 | |
| 304 | tries = 0; |
| 305 | while (True) { |
| 306 | if (tries > 10) |
| 307 | return -1; |
| 308 | VG_(sprintf)( buf, "/tmp/valgrind_%s_%08x", |
| 309 | part_of_name, VG_(random)( &seed )); |
| 310 | if (0) |
| 311 | VG_(printf)("VG_(mkstemp): trying: %s\n", buf); |
| 312 | |
| 313 | sres = VG_(open)(buf, |
| 314 | VKI_O_CREAT|VKI_O_RDWR|VKI_O_EXCL|VKI_O_TRUNC, |
| 315 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 316 | if (sres.isError) |
| 317 | continue; |
| 318 | /* VG_(safe_fd) doesn't return if it fails. */ |
| 319 | fd = VG_(safe_fd)( sres.val ); |
| 320 | if (fullname) |
| 321 | VG_(strcpy)( fullname, buf ); |
| 322 | return fd; |
| 323 | } |
| 324 | /* NOTREACHED */ |
| 325 | } |
| 326 | |
| 327 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 328 | /* --------------------------------------------------------------------- |
| 329 | Socket-related stuff. This is very Linux-kernel specific. |
| 330 | ------------------------------------------------------------------ */ |
| 331 | |
| 332 | static |
| 333 | Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port ); |
| 334 | |
| 335 | static |
| 336 | Int my_socket ( Int domain, Int type, Int protocol ); |
| 337 | |
| 338 | static |
| 339 | Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr, |
| 340 | Int addrlen ); |
| 341 | |
tom | a4a4f41 | 2005-11-17 12:01:56 +0000 | [diff] [blame] | 342 | UInt VG_(htonl) ( UInt x ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 343 | { |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 344 | # if defined(VG_BIGENDIAN) |
| 345 | return x; |
| 346 | # else |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 347 | return |
| 348 | (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8) |
| 349 | | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24); |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 350 | # endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 351 | } |
| 352 | |
tom | a4a4f41 | 2005-11-17 12:01:56 +0000 | [diff] [blame] | 353 | UInt VG_(ntohl) ( UInt x ) |
| 354 | { |
| 355 | # if defined(VG_BIGENDIAN) |
| 356 | return x; |
| 357 | # else |
| 358 | return |
| 359 | (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8) |
| 360 | | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24); |
| 361 | # endif |
| 362 | } |
| 363 | |
| 364 | UShort VG_(htons) ( UShort x ) |
| 365 | { |
| 366 | # if defined(VG_BIGENDIAN) |
| 367 | return x; |
| 368 | # else |
| 369 | return |
| 370 | (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8); |
| 371 | # endif |
| 372 | } |
| 373 | |
| 374 | UShort VG_(ntohs) ( UShort x ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 375 | { |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 376 | # if defined(VG_BIGENDIAN) |
| 377 | return x; |
| 378 | # else |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 379 | return |
| 380 | (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8); |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 381 | # endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | |
| 385 | /* The main function. |
| 386 | |
| 387 | Supplied string contains either an ip address "192.168.0.1" or |
| 388 | an ip address and port pair, "192.168.0.1:1500". Parse these, |
| 389 | and return: |
| 390 | -1 if there is a parse error |
| 391 | -2 if no parse error, but specified host:port cannot be opened |
| 392 | the relevant file (socket) descriptor, otherwise. |
| 393 | is used. |
| 394 | */ |
| 395 | Int VG_(connect_via_socket)( UChar* str ) |
| 396 | { |
| 397 | Int sd, res; |
| 398 | struct vki_sockaddr_in servAddr; |
| 399 | UInt ip = 0; |
| 400 | UShort port = VG_CLO_DEFAULT_LOGPORT; |
| 401 | Bool ok = parse_inet_addr_and_port(str, &ip, &port); |
| 402 | if (!ok) |
| 403 | return -1; |
| 404 | |
| 405 | //if (0) |
| 406 | // VG_(printf)("ip = %d.%d.%d.%d, port %d\n", |
| 407 | // (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, |
| 408 | // (ip >> 8) & 0xFF, ip & 0xFF, |
| 409 | // (UInt)port ); |
| 410 | |
| 411 | servAddr.sin_family = VKI_AF_INET; |
tom | a4a4f41 | 2005-11-17 12:01:56 +0000 | [diff] [blame] | 412 | servAddr.sin_addr.s_addr = VG_(htonl)(ip); |
| 413 | servAddr.sin_port = VG_(htons)(port); |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 414 | |
| 415 | /* create socket */ |
| 416 | sd = my_socket(VKI_AF_INET, VKI_SOCK_STREAM, 0 /* IPPROTO_IP ? */); |
| 417 | if (sd < 0) { |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 418 | /* this shouldn't happen ... nevertheless */ |
| 419 | return -2; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 420 | } |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 421 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 422 | /* connect to server */ |
| 423 | res = my_connect(sd, (struct vki_sockaddr_in *) &servAddr, |
| 424 | sizeof(servAddr)); |
| 425 | if (res < 0) { |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 426 | /* connection failed */ |
| 427 | return -2; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | return sd; |
| 431 | } |
| 432 | |
| 433 | |
| 434 | /* Let d = one or more digits. Accept either: |
| 435 | d.d.d.d or d.d.d.d:d |
| 436 | */ |
| 437 | Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port ) |
| 438 | { |
| 439 | # define GET_CH ((*str) ? (*str++) : 0) |
| 440 | UInt ipa, i, j, c, any; |
| 441 | ipa = 0; |
| 442 | for (i = 0; i < 4; i++) { |
| 443 | j = 0; |
| 444 | any = 0; |
| 445 | while (1) { |
| 446 | c = GET_CH; |
| 447 | if (c < '0' || c > '9') break; |
| 448 | j = 10 * j + (int)(c - '0'); |
| 449 | any = 1; |
| 450 | } |
| 451 | if (any == 0 || j > 255) goto syntaxerr; |
| 452 | ipa = (ipa << 8) + j; |
| 453 | if (i <= 2 && c != '.') goto syntaxerr; |
| 454 | } |
| 455 | if (c == 0 || c == ':') |
| 456 | *ip_addr = ipa; |
| 457 | if (c == 0) goto ok; |
| 458 | if (c != ':') goto syntaxerr; |
| 459 | j = 0; |
| 460 | any = 0; |
| 461 | while (1) { |
| 462 | c = GET_CH; |
| 463 | if (c < '0' || c > '9') break; |
| 464 | j = j * 10 + (int)(c - '0'); |
| 465 | any = 1; |
| 466 | if (j > 65535) goto syntaxerr; |
| 467 | } |
| 468 | if (any == 0 || c != 0) goto syntaxerr; |
| 469 | if (j < 1024) goto syntaxerr; |
| 470 | *port = (UShort)j; |
| 471 | ok: |
| 472 | return 1; |
| 473 | syntaxerr: |
| 474 | return 0; |
| 475 | # undef GET_CH |
| 476 | } |
| 477 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 478 | static |
| 479 | Int my_socket ( Int domain, Int type, Int protocol ) |
| 480 | { |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame^] | 481 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 482 | SysRes res; |
| 483 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 484 | args[0] = domain; |
| 485 | args[1] = type; |
| 486 | args[2] = protocol; |
| 487 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SOCKET, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 488 | return res.isError ? -1 : res.val; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 489 | |
| 490 | #elif defined(VGP_amd64_linux) |
sewardj | 1e86656 | 2005-09-28 19:58:58 +0000 | [diff] [blame] | 491 | SysRes res; |
| 492 | res = VG_(do_syscall3)(__NR_socket, domain, type, protocol ); |
| 493 | return res.isError ? -1 : res.val; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 494 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 495 | #else |
| 496 | # error Unknown arch |
| 497 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static |
| 501 | Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr, |
| 502 | Int addrlen ) |
| 503 | { |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame^] | 504 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 505 | SysRes res; |
| 506 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 507 | args[0] = sockfd; |
| 508 | args[1] = (UWord)serv_addr; |
| 509 | args[2] = addrlen; |
| 510 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_CONNECT, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 511 | return res.isError ? -1 : res.val; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 512 | |
| 513 | #elif defined(VGP_amd64_linux) |
sewardj | 1e86656 | 2005-09-28 19:58:58 +0000 | [diff] [blame] | 514 | SysRes res; |
| 515 | res = VG_(do_syscall3)(__NR_connect, sockfd, (UWord)serv_addr, addrlen); |
| 516 | return res.isError ? -1 : res.val; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 517 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 518 | #else |
| 519 | # error Unknown arch |
| 520 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | Int VG_(write_socket)( Int sd, void *msg, Int count ) |
| 524 | { |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 525 | /* This is actually send(). */ |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 526 | /* Requests not to send SIGPIPE on errors on stream oriented |
| 527 | sockets when the other end breaks the connection. The EPIPE |
| 528 | error is still returned. */ |
| 529 | Int flags = VKI_MSG_NOSIGNAL; |
| 530 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame^] | 531 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 532 | SysRes res; |
| 533 | UWord args[4]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 534 | args[0] = sd; |
| 535 | args[1] = (UWord)msg; |
| 536 | args[2] = count; |
| 537 | args[3] = flags; |
| 538 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SEND, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 539 | return res.isError ? -1 : res.val; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 540 | |
| 541 | #elif defined(VGP_amd64_linux) |
sewardj | 1e86656 | 2005-09-28 19:58:58 +0000 | [diff] [blame] | 542 | SysRes res; |
| 543 | res = VG_(do_syscall6)(__NR_sendto, sd, (UWord)msg, count, flags, 0,0); |
| 544 | return res.isError ? -1 : res.val; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 545 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 546 | #else |
| 547 | # error Unknown arch |
| 548 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen) |
| 552 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 553 | SysRes res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 554 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame^] | 555 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 556 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 557 | args[0] = sd; |
| 558 | args[1] = (UWord)name; |
| 559 | args[2] = (UWord)namelen; |
| 560 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKNAME, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 561 | return res.isError ? -1 : res.val; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 562 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 563 | #elif defined(VGP_amd64_linux) |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 564 | res = VG_(do_syscall3)( __NR_getsockname, |
| 565 | (UWord)sd, (UWord)name, (UWord)namelen ); |
| 566 | return res.isError ? -1 : res.val; |
| 567 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 568 | #else |
| 569 | # error Unknown arch |
| 570 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen) |
| 574 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 575 | SysRes res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 576 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame^] | 577 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 578 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 579 | args[0] = sd; |
| 580 | args[1] = (UWord)name; |
| 581 | args[2] = (UWord)namelen; |
| 582 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETPEERNAME, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 583 | return res.isError ? -1 : res.val; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 584 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 585 | #elif defined(VGP_amd64_linux) |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 586 | res = VG_(do_syscall3)( __NR_getpeername, |
| 587 | (UWord)sd, (UWord)name, (UWord)namelen ); |
| 588 | return res.isError ? -1 : res.val; |
| 589 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 590 | #else |
sewardj | 8a85128 | 2005-07-02 10:13:05 +0000 | [diff] [blame] | 591 | # error Unknown archx |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 592 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval, |
| 596 | Int *optlen) |
| 597 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 598 | SysRes res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 599 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame^] | 600 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 601 | UWord args[5]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 602 | args[0] = sd; |
| 603 | args[1] = level; |
| 604 | args[2] = optname; |
| 605 | args[3] = (UWord)optval; |
| 606 | args[4] = (UWord)optlen; |
| 607 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKOPT, (UWord)&args); |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 608 | return res.isError ? -1 : res.val; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 609 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 610 | #elif defined(VGP_amd64_linux) |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 611 | res = VG_(do_syscall5)( __NR_getsockopt, |
| 612 | (UWord)sd, (UWord)level, (UWord)optname, |
| 613 | (UWord)optval, (UWord)optlen ); |
| 614 | return res.isError ? -1 : res.val; |
| 615 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 616 | #else |
| 617 | # error Unknown arch |
| 618 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | |
| 622 | |
| 623 | /*--------------------------------------------------------------------*/ |
| 624 | /*--- end ---*/ |
| 625 | /*--------------------------------------------------------------------*/ |
| 626 | |