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