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 | |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2007 Julian Seward |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 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" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 32 | #include "pub_core_vki.h" |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 33 | #include "pub_core_vkiscnums.h" |
sewardj | 26b8728 | 2006-10-17 12:49:31 +0000 | [diff] [blame] | 34 | #include "pub_core_debuglog.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 35 | #include "pub_core_libcbase.h" |
| 36 | #include "pub_core_libcassert.h" |
| 37 | #include "pub_core_libcfile.h" |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 38 | #include "pub_core_libcprint.h" // VG_(sprintf) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 39 | #include "pub_core_libcproc.h" // VG_(getpid), VG_(getppid) |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame^] | 40 | #include "pub_core_xarray.h" |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 41 | #include "pub_core_clientstate.h" // VG_(fd_hard_limit) |
njn | 9abd608 | 2005-06-17 21:31:45 +0000 | [diff] [blame] | 42 | #include "pub_core_syscall.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 43 | |
| 44 | /* --------------------------------------------------------------------- |
| 45 | File stuff |
| 46 | ------------------------------------------------------------------ */ |
| 47 | |
| 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 | |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 66 | /* Set the close-on-exec flag for this fd. */ |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 67 | VG_(fcntl)(newfd, VKI_F_SETFD, VKI_FD_CLOEXEC); |
| 68 | |
| 69 | vg_assert(newfd >= VG_(fd_hard_limit)); |
| 70 | return newfd; |
| 71 | } |
| 72 | |
njn | ae7359b | 2005-06-19 21:10:42 +0000 | [diff] [blame] | 73 | /* Given a file descriptor, attempt to deduce its filename. To do |
| 74 | 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] | 75 | or if it doesn't exist, we return False. */ |
| 76 | Bool VG_(resolve_filename) ( Int fd, HChar* buf, Int n_buf ) |
njn | ae7359b | 2005-06-19 21:10:42 +0000 | [diff] [blame] | 77 | { |
| 78 | HChar tmp[64]; |
| 79 | |
| 80 | VG_(sprintf)(tmp, "/proc/self/fd/%d", fd); |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 81 | VG_(memset)(buf, 0, n_buf); |
njn | ae7359b | 2005-06-19 21:10:42 +0000 | [diff] [blame] | 82 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 83 | if (VG_(readlink)(tmp, buf, n_buf) > 0 && buf[0] == '/') |
njn | f845f8f | 2005-06-23 02:26:47 +0000 | [diff] [blame] | 84 | return True; |
| 85 | else |
| 86 | return False; |
njn | ae7359b | 2005-06-19 21:10:42 +0000 | [diff] [blame] | 87 | } |
| 88 | |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 89 | SysRes VG_(open) ( const Char* pathname, Int flags, Int mode ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 90 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 91 | SysRes res = VG_(do_syscall3)(__NR_open, (UWord)pathname, flags, mode); |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 92 | return res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void VG_(close) ( Int fd ) |
| 96 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 97 | (void)VG_(do_syscall1)(__NR_close, fd); |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | Int VG_(read) ( Int fd, void* buf, Int count) |
| 101 | { |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 102 | Int ret; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 103 | SysRes res = VG_(do_syscall3)(__NR_read, fd, (UWord)buf, count); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 104 | if (res.isError) { |
| 105 | ret = - (Int)(Word)res.err; |
| 106 | vg_assert(ret < 0); |
| 107 | } else { |
| 108 | ret = (Int)(Word)res.res; |
| 109 | vg_assert(ret >= 0); |
| 110 | } |
| 111 | return ret; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | Int VG_(write) ( Int fd, const void* buf, Int count) |
| 115 | { |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 116 | Int ret; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 117 | SysRes res = VG_(do_syscall3)(__NR_write, fd, (UWord)buf, count); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 118 | if (res.isError) { |
| 119 | ret = - (Int)(Word)res.err; |
| 120 | vg_assert(ret < 0); |
| 121 | } else { |
| 122 | ret = (Int)(Word)res.res; |
| 123 | vg_assert(ret >= 0); |
| 124 | } |
| 125 | return ret; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | Int VG_(pipe) ( Int fd[2] ) |
| 129 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 130 | SysRes res = VG_(do_syscall1)(__NR_pipe, (UWord)fd); |
| 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 | OffT VG_(lseek) ( Int fd, OffT offset, Int whence ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 135 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 136 | SysRes res = VG_(do_syscall3)(__NR_lseek, fd, offset, whence); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 137 | return res.isError ? (-1) : res.res; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 138 | /* if you change the error-reporting conventions of this, also |
| 139 | change VG_(pread) and all other usage points. */ |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 140 | } |
| 141 | |
tom | 6c93c4f | 2005-08-05 07:46:32 +0000 | [diff] [blame] | 142 | SysRes VG_(stat) ( Char* file_name, struct vki_stat* buf ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 143 | { |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 144 | # if defined(VGO_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 145 | SysRes res = VG_(do_syscall2)(__NR_stat, (UWord)file_name, (UWord)buf); |
tom | 6c93c4f | 2005-08-05 07:46:32 +0000 | [diff] [blame] | 146 | return res; |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 147 | # elif defined(VGO_aix5) |
| 148 | SysRes res = VG_(do_syscall4)(__NR_AIX5_statx, |
| 149 | (UWord)file_name, |
| 150 | (UWord)buf, |
| 151 | sizeof(struct vki_stat), |
| 152 | VKI_STX_NORMAL); |
| 153 | return res; |
| 154 | # else |
| 155 | # error Unknown OS |
| 156 | # endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | Int VG_(fstat) ( Int fd, struct vki_stat* buf ) |
| 160 | { |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 161 | # if defined(VGO_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 162 | SysRes res = VG_(do_syscall2)(__NR_fstat, fd, (UWord)buf); |
| 163 | return res.isError ? (-1) : 0; |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 164 | # elif defined(VGO_aix5) |
| 165 | I_die_here; |
| 166 | # else |
| 167 | # error Unknown OS |
| 168 | # endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 169 | } |
| 170 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 171 | Int VG_(fsize) ( Int fd ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 172 | { |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 173 | # if defined(VGO_linux) && defined(__NR_fstat64) |
tom | 574b893 | 2006-07-05 17:47:46 +0000 | [diff] [blame] | 174 | struct vki_stat64 buf; |
| 175 | SysRes res = VG_(do_syscall2)(__NR_fstat64, fd, (UWord)&buf); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 176 | return res.isError ? (-1) : buf.st_size; |
| 177 | # elif defined(VGO_linux) && !defined(__NR_fstat64) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 178 | struct vki_stat buf; |
| 179 | SysRes res = VG_(do_syscall2)(__NR_fstat, fd, (UWord)&buf); |
| 180 | return res.isError ? (-1) : buf.st_size; |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 181 | # elif defined(VGO_aix5) |
| 182 | I_die_here; |
| 183 | # else |
| 184 | # error Unknown OS |
| 185 | # endif |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 186 | } |
| 187 | |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 188 | Bool VG_(is_dir) ( HChar* f ) |
| 189 | { |
| 190 | struct vki_stat buf; |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 191 | SysRes res = VG_(stat)(f, &buf); |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 192 | return res.isError ? False |
| 193 | : VKI_S_ISDIR(buf.st_mode) ? True : False; |
| 194 | } |
| 195 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 196 | SysRes VG_(dup) ( Int oldfd ) |
| 197 | { |
| 198 | return VG_(do_syscall1)(__NR_dup, oldfd); |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 199 | } |
| 200 | |
njn | 327fe8a | 2005-06-12 02:49:35 +0000 | [diff] [blame] | 201 | /* Returns -1 on error. */ |
| 202 | Int VG_(fcntl) ( Int fd, Int cmd, Int arg ) |
| 203 | { |
| 204 | SysRes res = VG_(do_syscall3)(__NR_fcntl, fd, cmd, arg); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 205 | return res.isError ? -1 : res.res; |
njn | 327fe8a | 2005-06-12 02:49:35 +0000 | [diff] [blame] | 206 | } |
| 207 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 208 | Int VG_(rename) ( Char* old_name, Char* new_name ) |
| 209 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 210 | SysRes res = VG_(do_syscall2)(__NR_rename, (UWord)old_name, (UWord)new_name); |
| 211 | return res.isError ? (-1) : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | Int VG_(unlink) ( Char* file_name ) |
| 215 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 216 | SysRes res = VG_(do_syscall1)(__NR_unlink, (UWord)file_name); |
| 217 | return res.isError ? (-1) : 0; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 218 | } |
| 219 | |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 220 | Bool VG_(getcwd) ( Char* buf, SizeT size ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 221 | { |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 222 | # if defined(VGO_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 223 | SysRes res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 224 | vg_assert(buf != NULL); |
| 225 | res = VG_(do_syscall2)(__NR_getcwd, (UWord)buf, size); |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 226 | return res.isError ? False : True; |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 227 | # elif defined(VGO_aix5) |
| 228 | static Int complaints = 3; |
| 229 | if (complaints-- > 0) |
| 230 | VG_(debugLog)(0, "libcfile", |
| 231 | "Warning: AIX5: m_libcfile.c: kludged 'getcwd'\n"); |
| 232 | if (size < 2) return False; |
| 233 | buf[0] = '.'; |
| 234 | buf[1] = 0; |
| 235 | return True; |
| 236 | # else |
| 237 | # error Unknown OS |
| 238 | # endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | Int VG_(readlink) (Char* path, Char* buf, UInt bufsiz) |
| 242 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 243 | SysRes res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 244 | /* res = readlink( path, buf, bufsiz ); */ |
| 245 | res = VG_(do_syscall3)(__NR_readlink, (UWord)path, (UWord)buf, bufsiz); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 246 | return res.isError ? -1 : res.res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | Int VG_(getdents) (UInt fd, struct vki_dirent *dirp, UInt count) |
| 250 | { |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 251 | SysRes res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 252 | /* res = getdents( fd, dirp, count ); */ |
| 253 | res = VG_(do_syscall3)(__NR_getdents, fd, (UWord)dirp, count); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 254 | return res.isError ? -1 : res.res; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 255 | } |
| 256 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 257 | /* Check accessibility of a file. Returns zero for access granted, |
| 258 | nonzero otherwise. */ |
| 259 | Int VG_(access) ( HChar* path, Bool irusr, Bool iwusr, Bool ixusr ) |
| 260 | { |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 261 | # if defined(VGO_linux) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 262 | /* Very annoyingly, I cannot find any definition for R_OK et al in |
| 263 | the kernel interfaces. Therefore I reluctantly resort to |
| 264 | hardwiring in these magic numbers that I determined by |
| 265 | experimentation. */ |
| 266 | UWord w = (irusr ? 4/*R_OK*/ : 0) |
| 267 | | (iwusr ? 2/*W_OK*/ : 0) |
| 268 | | (ixusr ? 1/*X_OK*/ : 0); |
| 269 | SysRes res = VG_(do_syscall2)(__NR_access, (UWord)path, w); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 270 | return res.isError ? 1 : 0; |
| 271 | # elif defined(VGO_aix5) |
| 272 | UWord w = (irusr ? VKI_R_OK : 0) |
| 273 | | (iwusr ? VKI_W_OK : 0) |
| 274 | | (ixusr ? VKI_X_OK : 0); |
| 275 | SysRes res = VG_(do_syscall2)(__NR_access, (UWord)path, w); |
| 276 | return res.isError ? 1 : 0; |
| 277 | # else |
| 278 | # error "Don't know how to do VG_(access) on this OS" |
| 279 | # endif |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 280 | } |
| 281 | |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 282 | /* |
| 283 | Emulate the normal Unix permissions checking algorithm. |
| 284 | |
| 285 | If owner matches, then use the owner permissions, else |
| 286 | if group matches, then use the group permissions, else |
| 287 | use other permissions. |
| 288 | |
| 289 | Note that we can't deal with SUID/SGID, so we refuse to run them |
| 290 | (otherwise the executable may misbehave if it doesn't have the |
| 291 | permissions it thinks it does). |
| 292 | */ |
| 293 | /* returns: 0 = success, non-0 is failure */ |
| 294 | Int VG_(check_executable)(HChar* f) |
| 295 | { |
sewardj | 26b8728 | 2006-10-17 12:49:31 +0000 | [diff] [blame] | 296 | /* This is something of a kludge. Really we should fix VG_(stat) to |
| 297 | do this itself, but not clear how to do it as it depends on |
| 298 | having a 'struct vki_stat64' which is different from 'struct |
| 299 | vki_stat'. */ |
| 300 | # if defined(VGO_linux) && defined(__NR_stat64) |
tom | a346c26 | 2006-09-29 08:12:08 +0000 | [diff] [blame] | 301 | struct vki_stat64 st; |
| 302 | SysRes res = VG_(do_syscall2)(__NR_stat64, (UWord)f, (UWord)&st); |
sewardj | 26b8728 | 2006-10-17 12:49:31 +0000 | [diff] [blame] | 303 | # else |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 304 | struct vki_stat st; |
sewardj | 26b8728 | 2006-10-17 12:49:31 +0000 | [diff] [blame] | 305 | SysRes res = VG_(stat)(f, &st); |
| 306 | # endif |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 307 | |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 308 | if (res.isError) { |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 309 | return res.err; |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (st.st_mode & (VKI_S_ISUID | VKI_S_ISGID)) { |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 313 | /* VG_(printf)("Can't execute suid/sgid executable %s\n", exe); */ |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 314 | return VKI_EACCES; |
| 315 | } |
| 316 | |
| 317 | if (VG_(geteuid)() == st.st_uid) { |
| 318 | if (!(st.st_mode & VKI_S_IXUSR)) |
| 319 | return VKI_EACCES; |
| 320 | } else { |
sewardj | 45406ef | 2006-10-18 00:33:46 +0000 | [diff] [blame] | 321 | Int grpmatch = 0; |
njn | 7375061 | 2005-10-14 03:11:30 +0000 | [diff] [blame] | 322 | |
| 323 | if (VG_(getegid)() == st.st_gid) |
| 324 | grpmatch = 1; |
| 325 | else { |
| 326 | UInt groups[32]; |
| 327 | Int ngrp = VG_(getgroups)(32, groups); |
| 328 | Int i; |
| 329 | /* ngrp will be -1 if VG_(getgroups) failed. */ |
| 330 | for (i = 0; i < ngrp; i++) { |
| 331 | if (groups[i] == st.st_gid) { |
| 332 | grpmatch = 1; |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if (grpmatch) { |
| 339 | if (!(st.st_mode & VKI_S_IXGRP)) { |
| 340 | return VKI_EACCES; |
| 341 | } |
| 342 | } else if (!(st.st_mode & VKI_S_IXOTH)) { |
| 343 | return VKI_EACCES; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 350 | SysRes VG_(pread) ( Int fd, void* buf, Int count, Int offset ) |
| 351 | { |
| 352 | OffT off = VG_(lseek)( fd, (OffT)offset, VKI_SEEK_SET); |
tom | f4c2310 | 2005-10-31 17:05:21 +0000 | [diff] [blame] | 353 | if (off < 0) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 354 | return VG_(mk_SysRes_Error)( VKI_EINVAL ); |
tom | 60a4b0b | 2005-10-12 10:45:27 +0000 | [diff] [blame] | 355 | return VG_(do_syscall3)(__NR_read, fd, (UWord)buf, count ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* Create and open (-rw------) a tmp file name incorporating said arg. |
| 359 | Returns -1 on failure, else the fd of the file. If fullname is |
| 360 | non-NULL, the file's name is written into it. The number of bytes |
| 361 | written is guaranteed not to exceed 64+strlen(part_of_name). */ |
| 362 | |
| 363 | Int VG_(mkstemp) ( HChar* part_of_name, /*OUT*/HChar* fullname ) |
| 364 | { |
| 365 | HChar buf[200]; |
| 366 | Int n, tries, fd; |
| 367 | UInt seed; |
| 368 | SysRes sres; |
| 369 | |
| 370 | vg_assert(part_of_name); |
| 371 | n = VG_(strlen)(part_of_name); |
| 372 | vg_assert(n > 0 && n < 100); |
| 373 | |
| 374 | seed = (VG_(getpid)() << 9) ^ VG_(getppid)(); |
| 375 | |
| 376 | tries = 0; |
| 377 | while (True) { |
| 378 | if (tries > 10) |
| 379 | return -1; |
| 380 | VG_(sprintf)( buf, "/tmp/valgrind_%s_%08x", |
| 381 | part_of_name, VG_(random)( &seed )); |
| 382 | if (0) |
| 383 | VG_(printf)("VG_(mkstemp): trying: %s\n", buf); |
| 384 | |
| 385 | sres = VG_(open)(buf, |
| 386 | VKI_O_CREAT|VKI_O_RDWR|VKI_O_EXCL|VKI_O_TRUNC, |
| 387 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 388 | if (sres.isError) |
| 389 | continue; |
| 390 | /* VG_(safe_fd) doesn't return if it fails. */ |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 391 | fd = VG_(safe_fd)( sres.res ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 392 | if (fullname) |
| 393 | VG_(strcpy)( fullname, buf ); |
| 394 | return fd; |
| 395 | } |
| 396 | /* NOTREACHED */ |
| 397 | } |
| 398 | |
| 399 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 400 | /* --------------------------------------------------------------------- |
| 401 | Socket-related stuff. This is very Linux-kernel specific. |
| 402 | ------------------------------------------------------------------ */ |
| 403 | |
| 404 | static |
| 405 | Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port ); |
| 406 | |
| 407 | static |
| 408 | Int my_socket ( Int domain, Int type, Int protocol ); |
| 409 | |
| 410 | static |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 411 | Int my_connect ( Int sockfd, |
| 412 | # if defined(VGO_linux) |
| 413 | struct vki_sockaddr_in* serv_addr, |
| 414 | # else |
| 415 | void* serv_addr, |
| 416 | # endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 417 | Int addrlen ); |
| 418 | |
tom | a4a4f41 | 2005-11-17 12:01:56 +0000 | [diff] [blame] | 419 | UInt VG_(htonl) ( UInt x ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 420 | { |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 421 | # if defined(VG_BIGENDIAN) |
| 422 | return x; |
| 423 | # else |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 424 | return |
| 425 | (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8) |
| 426 | | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24); |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 427 | # endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 428 | } |
| 429 | |
tom | a4a4f41 | 2005-11-17 12:01:56 +0000 | [diff] [blame] | 430 | UInt VG_(ntohl) ( UInt x ) |
| 431 | { |
| 432 | # if defined(VG_BIGENDIAN) |
| 433 | return x; |
| 434 | # else |
| 435 | return |
| 436 | (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8) |
| 437 | | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24); |
| 438 | # endif |
| 439 | } |
| 440 | |
| 441 | UShort VG_(htons) ( UShort x ) |
| 442 | { |
| 443 | # if defined(VG_BIGENDIAN) |
| 444 | return x; |
| 445 | # else |
| 446 | return |
| 447 | (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8); |
| 448 | # endif |
| 449 | } |
| 450 | |
| 451 | UShort VG_(ntohs) ( UShort x ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 452 | { |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 453 | # if defined(VG_BIGENDIAN) |
| 454 | return x; |
| 455 | # else |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 456 | return |
| 457 | (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8); |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 458 | # endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | |
| 462 | /* The main function. |
| 463 | |
| 464 | Supplied string contains either an ip address "192.168.0.1" or |
| 465 | an ip address and port pair, "192.168.0.1:1500". Parse these, |
| 466 | and return: |
| 467 | -1 if there is a parse error |
| 468 | -2 if no parse error, but specified host:port cannot be opened |
| 469 | the relevant file (socket) descriptor, otherwise. |
| 470 | is used. |
| 471 | */ |
| 472 | Int VG_(connect_via_socket)( UChar* str ) |
| 473 | { |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 474 | #if defined(VGO_aix5) |
| 475 | I_die_here; |
| 476 | #else /* Yay, Linux */ |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 477 | Int sd, res; |
| 478 | struct vki_sockaddr_in servAddr; |
| 479 | UInt ip = 0; |
| 480 | UShort port = VG_CLO_DEFAULT_LOGPORT; |
| 481 | Bool ok = parse_inet_addr_and_port(str, &ip, &port); |
| 482 | if (!ok) |
| 483 | return -1; |
| 484 | |
| 485 | //if (0) |
| 486 | // VG_(printf)("ip = %d.%d.%d.%d, port %d\n", |
| 487 | // (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, |
| 488 | // (ip >> 8) & 0xFF, ip & 0xFF, |
| 489 | // (UInt)port ); |
| 490 | |
| 491 | servAddr.sin_family = VKI_AF_INET; |
tom | a4a4f41 | 2005-11-17 12:01:56 +0000 | [diff] [blame] | 492 | servAddr.sin_addr.s_addr = VG_(htonl)(ip); |
| 493 | servAddr.sin_port = VG_(htons)(port); |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 494 | |
| 495 | /* create socket */ |
| 496 | sd = my_socket(VKI_AF_INET, VKI_SOCK_STREAM, 0 /* IPPROTO_IP ? */); |
| 497 | if (sd < 0) { |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 498 | /* this shouldn't happen ... nevertheless */ |
| 499 | return -2; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 500 | } |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 501 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 502 | /* connect to server */ |
| 503 | res = my_connect(sd, (struct vki_sockaddr_in *) &servAddr, |
| 504 | sizeof(servAddr)); |
| 505 | if (res < 0) { |
sewardj | a1c4bfb | 2005-10-18 02:15:39 +0000 | [diff] [blame] | 506 | /* connection failed */ |
| 507 | return -2; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | return sd; |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 511 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | |
| 515 | /* Let d = one or more digits. Accept either: |
| 516 | d.d.d.d or d.d.d.d:d |
| 517 | */ |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 518 | static Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port ) |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 519 | { |
| 520 | # define GET_CH ((*str) ? (*str++) : 0) |
| 521 | UInt ipa, i, j, c, any; |
| 522 | ipa = 0; |
| 523 | for (i = 0; i < 4; i++) { |
| 524 | j = 0; |
| 525 | any = 0; |
| 526 | while (1) { |
| 527 | c = GET_CH; |
| 528 | if (c < '0' || c > '9') break; |
| 529 | j = 10 * j + (int)(c - '0'); |
| 530 | any = 1; |
| 531 | } |
| 532 | if (any == 0 || j > 255) goto syntaxerr; |
| 533 | ipa = (ipa << 8) + j; |
| 534 | if (i <= 2 && c != '.') goto syntaxerr; |
| 535 | } |
| 536 | if (c == 0 || c == ':') |
| 537 | *ip_addr = ipa; |
| 538 | if (c == 0) goto ok; |
| 539 | if (c != ':') goto syntaxerr; |
| 540 | j = 0; |
| 541 | any = 0; |
| 542 | while (1) { |
| 543 | c = GET_CH; |
| 544 | if (c < '0' || c > '9') break; |
| 545 | j = j * 10 + (int)(c - '0'); |
| 546 | any = 1; |
| 547 | if (j > 65535) goto syntaxerr; |
| 548 | } |
| 549 | if (any == 0 || c != 0) goto syntaxerr; |
| 550 | if (j < 1024) goto syntaxerr; |
| 551 | *port = (UShort)j; |
| 552 | ok: |
| 553 | return 1; |
| 554 | syntaxerr: |
| 555 | return 0; |
| 556 | # undef GET_CH |
| 557 | } |
| 558 | |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 559 | static |
| 560 | Int my_socket ( Int domain, Int type, Int protocol ) |
| 561 | { |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 562 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 563 | SysRes res; |
| 564 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 565 | args[0] = domain; |
| 566 | args[1] = type; |
| 567 | args[2] = protocol; |
| 568 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SOCKET, (UWord)&args); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 569 | return res.isError ? -1 : res.res; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 570 | |
| 571 | #elif defined(VGP_amd64_linux) |
sewardj | 1e86656 | 2005-09-28 19:58:58 +0000 | [diff] [blame] | 572 | SysRes res; |
| 573 | res = VG_(do_syscall3)(__NR_socket, domain, type, protocol ); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 574 | return res.isError ? -1 : res.res; |
| 575 | |
| 576 | #elif defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 577 | I_die_here; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 578 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 579 | #else |
| 580 | # error Unknown arch |
| 581 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 585 | Int my_connect ( Int sockfd, |
| 586 | # if defined(VGO_linux) |
| 587 | struct vki_sockaddr_in* serv_addr, |
| 588 | # else |
| 589 | void* serv_addr, |
| 590 | # endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 591 | Int addrlen ) |
| 592 | { |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 593 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 594 | SysRes res; |
| 595 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 596 | args[0] = sockfd; |
| 597 | args[1] = (UWord)serv_addr; |
| 598 | args[2] = addrlen; |
| 599 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_CONNECT, (UWord)&args); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 600 | return res.isError ? -1 : res.res; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 601 | |
| 602 | #elif defined(VGP_amd64_linux) |
sewardj | 1e86656 | 2005-09-28 19:58:58 +0000 | [diff] [blame] | 603 | SysRes res; |
| 604 | res = VG_(do_syscall3)(__NR_connect, sockfd, (UWord)serv_addr, addrlen); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 605 | return res.isError ? -1 : res.res; |
| 606 | |
| 607 | #elif defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 608 | I_die_here; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 609 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 610 | #else |
| 611 | # error Unknown arch |
| 612 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | Int VG_(write_socket)( Int sd, void *msg, Int count ) |
| 616 | { |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 617 | /* This is actually send(). */ |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 618 | /* For Linux, VKI_MSG_NOSIGNAL is a request not to send SIGPIPE on |
| 619 | errors on stream oriented sockets when the other end breaks the |
| 620 | connection. The EPIPE error is still returned. */ |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 621 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 622 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 623 | SysRes res; |
| 624 | UWord args[4]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 625 | args[0] = sd; |
| 626 | args[1] = (UWord)msg; |
| 627 | args[2] = count; |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 628 | args[3] = VKI_MSG_NOSIGNAL; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 629 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_SEND, (UWord)&args); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 630 | return res.isError ? -1 : res.res; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 631 | |
| 632 | #elif defined(VGP_amd64_linux) |
sewardj | 1e86656 | 2005-09-28 19:58:58 +0000 | [diff] [blame] | 633 | SysRes res; |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 634 | res = VG_(do_syscall6)(__NR_sendto, sd, (UWord)msg, |
| 635 | count, VKI_MSG_NOSIGNAL, 0,0); |
| 636 | return res.isError ? -1 : res.res; |
| 637 | |
| 638 | #elif defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 639 | I_die_here; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 640 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 641 | #else |
| 642 | # error Unknown arch |
| 643 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen) |
| 647 | { |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 648 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 649 | SysRes res; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 650 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 651 | args[0] = sd; |
| 652 | args[1] = (UWord)name; |
| 653 | args[2] = (UWord)namelen; |
| 654 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKNAME, (UWord)&args); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 655 | return res.isError ? -1 : res.res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 656 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 657 | #elif defined(VGP_amd64_linux) |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 658 | SysRes res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 659 | res = VG_(do_syscall3)( __NR_getsockname, |
| 660 | (UWord)sd, (UWord)name, (UWord)namelen ); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 661 | return res.isError ? -1 : res.res; |
| 662 | |
| 663 | #elif defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 664 | I_die_here; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 665 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 666 | #else |
| 667 | # error Unknown arch |
| 668 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen) |
| 672 | { |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 673 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 674 | SysRes res; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 675 | UWord args[3]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 676 | args[0] = sd; |
| 677 | args[1] = (UWord)name; |
| 678 | args[2] = (UWord)namelen; |
| 679 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETPEERNAME, (UWord)&args); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 680 | return res.isError ? -1 : res.res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 681 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 682 | #elif defined(VGP_amd64_linux) |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 683 | SysRes res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 684 | res = VG_(do_syscall3)( __NR_getpeername, |
| 685 | (UWord)sd, (UWord)name, (UWord)namelen ); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 686 | return res.isError ? -1 : res.res; |
| 687 | |
| 688 | #elif defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 689 | I_die_here; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 690 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 691 | #else |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 692 | # error Unknown arch |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 693 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval, |
| 697 | Int *optlen) |
| 698 | { |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 699 | #if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 700 | SysRes res; |
sewardj | a8d8e23 | 2005-06-07 20:04:56 +0000 | [diff] [blame] | 701 | UWord args[5]; |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 702 | args[0] = sd; |
| 703 | args[1] = level; |
| 704 | args[2] = optname; |
| 705 | args[3] = (UWord)optval; |
| 706 | args[4] = (UWord)optlen; |
| 707 | res = VG_(do_syscall2)(__NR_socketcall, VKI_SYS_GETSOCKOPT, (UWord)&args); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 708 | return res.isError ? -1 : res.res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 709 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 710 | #elif defined(VGP_amd64_linux) |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 711 | SysRes res; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 712 | res = VG_(do_syscall5)( __NR_getsockopt, |
| 713 | (UWord)sd, (UWord)level, (UWord)optname, |
| 714 | (UWord)optval, (UWord)optlen ); |
sewardj | 9a66bb9 | 2006-10-17 01:38:13 +0000 | [diff] [blame] | 715 | return res.isError ? -1 : res.res; |
| 716 | |
| 717 | #elif defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 718 | I_die_here; |
sewardj | a5c9e4a | 2005-06-09 13:21:58 +0000 | [diff] [blame] | 719 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 720 | #else |
| 721 | # error Unknown arch |
| 722 | #endif |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | |
| 726 | |
| 727 | /*--------------------------------------------------------------------*/ |
| 728 | /*--- end ---*/ |
| 729 | /*--------------------------------------------------------------------*/ |
| 730 | |