| 1 |
from rpython.rtyper.tool import rffi_platform as platform |
| 2 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 3 |
from pypy.interpreter.error import OperationError, oefmt, wrap_oserror |
| 4 |
from pypy.interpreter.gateway import unwrap_spec, WrappedDefault |
| 5 |
from rpython.rlib import rposix |
| 6 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 7 |
from rpython.rlib.rbigint import rbigint |
| 8 |
from rpython.rlib.rarithmetic import UINT_MAX |
| 9 |
|
| 10 |
import sys |
| 11 |
|
| 12 |
class CConfig: |
| 13 |
_compilation_info_ = ExternalCompilationInfo( |
| 14 |
includes = ['fcntl.h', 'sys/file.h', 'sys/ioctl.h'] |
| 15 |
) |
| 16 |
flock = platform.Struct("struct flock", |
| 17 |
[('l_start', rffi.LONGLONG), ('l_len', rffi.LONGLONG), |
| 18 |
('l_pid', rffi.LONG), ('l_type', rffi.SHORT), |
| 19 |
('l_whence', rffi.SHORT)]) |
| 20 |
has_flock = platform.Has('flock') |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
constants = {} |
| 26 |
constant_names = ['LOCK_SH', 'LOCK_EX', 'LOCK_NB', 'LOCK_UN', 'F_DUPFD', |
| 27 |
'F_GETFD', 'F_SETFD', 'F_GETFL', 'F_SETFL', 'F_UNLCK', 'FD_CLOEXEC', |
| 28 |
'LOCK_MAND', 'LOCK_READ', 'LOCK_WRITE', 'LOCK_RW', 'F_GETSIG', 'F_SETSIG', |
| 29 |
'F_GETLK64', 'F_SETLK64', 'F_SETLKW64', 'F_GETLK', 'F_SETLK', 'F_SETLKW', |
| 30 |
'F_GETOWN', 'F_SETOWN', 'F_RDLCK', 'F_WRLCK', 'F_SETLEASE', 'F_GETLEASE', |
| 31 |
'F_NOTIFY', 'F_EXLCK', 'F_SHLCK', 'DN_ACCESS', 'DN_MODIFY', 'DN_CREATE', |
| 32 |
'DN_DELETE', 'DN_RENAME', 'DN_ATTRIB', 'DN_MULTISHOT', 'I_NREAD', |
| 33 |
'I_PUSH', 'I_POP', 'I_LOOK', 'I_FLUSH', 'I_SRDOPT', 'I_GRDOPT', 'I_STR', |
| 34 |
'I_SETSIG', 'I_GETSIG', 'I_FIND', 'I_LINK', 'I_UNLINK', 'I_PEEK', |
| 35 |
'I_FDINSERT', 'I_SENDFD', 'I_RECVFD', 'I_SWROPT', 'I_LIST', 'I_PLINK', |
| 36 |
'I_PUNLINK', 'I_FLUSHBAND', 'I_CKBAND', 'I_GETBAND', 'I_ATMARK', |
| 37 |
'I_SETCLTIME', 'I_GETCLTIME', 'I_CANPUT'] |
| 38 |
for name in constant_names: |
| 39 |
setattr(CConfig, name, platform.DefinedConstantInteger(name)) |
| 40 |
|
| 41 |
class cConfig(object): |
| 42 |
pass |
| 43 |
|
| 44 |
for k, v in platform.configure(CConfig).items(): |
| 45 |
setattr(cConfig, k, v) |
| 46 |
cConfig.flock.__name__ = "_flock" |
| 47 |
|
| 48 |
if "linux" in sys.platform: |
| 49 |
cConfig.F_GETSIG = 11 |
| 50 |
cConfig.F_SETSIG = 10 |
| 51 |
cConfig.F_GETLEASE = 1025 |
| 52 |
cConfig.F_SETLEASE = 1024 |
| 53 |
|
| 54 |
|
| 55 |
for name in constant_names: |
| 56 |
value = getattr(cConfig, name) |
| 57 |
if value is not None: |
| 58 |
constants[name] = value |
| 59 |
locals().update(constants) |
| 60 |
|
| 61 |
def external(name, args, result, **kwds): |
| 62 |
return rffi.llexternal(name, args, result, |
| 63 |
compilation_info=CConfig._compilation_info_, |
| 64 |
**kwds) |
| 65 |
|
| 66 |
_flock = lltype.Ptr(cConfig.flock) |
| 67 |
if sys.platform == 'darwin': |
| 68 |
natural_arity = 2 |
| 69 |
else: |
| 70 |
natural_arity = -1 |
| 71 |
fcntl_int = external('fcntl', [rffi.INT, rffi.INT, rffi.INT], rffi.INT, |
| 72 |
save_err=rffi.RFFI_SAVE_ERRNO, natural_arity=natural_arity) |
| 73 |
fcntl_str = external('fcntl', [rffi.INT, rffi.INT, rffi.CCHARP], rffi.INT, |
| 74 |
save_err=rffi.RFFI_SAVE_ERRNO, natural_arity=natural_arity) |
| 75 |
fcntl_flock = external('fcntl', [rffi.INT, rffi.INT, _flock], rffi.INT, |
| 76 |
save_err=rffi.RFFI_SAVE_ERRNO, natural_arity=natural_arity) |
| 77 |
ioctl_int = external('ioctl', [rffi.INT, rffi.UINT, rffi.INT], rffi.INT, |
| 78 |
save_err=rffi.RFFI_SAVE_ERRNO, natural_arity=natural_arity) |
| 79 |
ioctl_str = external('ioctl', [rffi.INT, rffi.UINT, rffi.CCHARP], rffi.INT, |
| 80 |
save_err=rffi.RFFI_SAVE_ERRNO, natural_arity=natural_arity) |
| 81 |
|
| 82 |
has_flock = cConfig.has_flock |
| 83 |
if has_flock: |
| 84 |
c_flock = external('flock', [rffi.INT, rffi.INT], rffi.INT, |
| 85 |
save_err=rffi.RFFI_SAVE_ERRNO) |
| 86 |
|
| 87 |
def _raise_error_maybe(space, funcname): |
| 88 |
|
| 89 |
|
| 90 |
errno = rposix.get_saved_errno() |
| 91 |
wrap_oserror(space, OSError(errno, funcname), |
| 92 |
w_exception_class=space.w_IOError, eintr_retry=True) |
| 93 |
|
| 94 |
def _raise_error_always(space, funcname): |
| 95 |
|
| 96 |
errno = rposix.get_saved_errno() |
| 97 |
raise wrap_oserror(space, OSError(errno, funcname), |
| 98 |
w_exception_class=space.w_IOError, eintr_retry=False) |
| 99 |
|
| 100 |
@unwrap_spec(op=int, w_arg=WrappedDefault(0)) |
| 101 |
def fcntl(space, w_fd, op, w_arg): |
| 102 |
"""fcntl(fd, op, [arg]) |
| 103 |
|
| 104 |
Perform the requested operation on file descriptor fd. The operation |
| 105 |
is defined by op and is operating system dependent. These constants are |
| 106 |
available from the fcntl module. The argument arg is optional, and |
| 107 |
defaults to 0; it may be an int or a string. If arg is given as a string, |
| 108 |
the return value of fcntl is a string of that length, containing the |
| 109 |
resulting value put in the arg buffer by the operating system. If the |
| 110 |
arg given is an integer or if none is specified, the result value is an |
| 111 |
integer corresponding to the return value of the fcntl call in the C code. |
| 112 |
""" |
| 113 |
|
| 114 |
fd = space.c_filedescriptor_w(w_fd) |
| 115 |
op = rffi.cast(rffi.INT, op) |
| 116 |
|
| 117 |
try: |
| 118 |
arg = space.getarg_w('s#', w_arg) |
| 119 |
except OperationError as e: |
| 120 |
if not e.match(space, space.w_TypeError): |
| 121 |
raise |
| 122 |
else: |
| 123 |
with rffi.scoped_str2charp(arg) as ll_arg: |
| 124 |
while True: |
| 125 |
rv = fcntl_str(fd, op, ll_arg) |
| 126 |
if rv < 0: |
| 127 |
_raise_error_maybe(space, "fcntl") |
| 128 |
else: |
| 129 |
arg = rffi.charpsize2str(ll_arg, len(arg)) |
| 130 |
return space.newbytes(arg) |
| 131 |
|
| 132 |
intarg = space.int_w(w_arg) |
| 133 |
intarg = rffi.cast(rffi.INT, intarg) |
| 134 |
while True: |
| 135 |
rv = fcntl_int(fd, op, intarg) |
| 136 |
if rv < 0: |
| 137 |
_raise_error_maybe(space, "fcntl") |
| 138 |
else: |
| 139 |
return space.newint(rv) |
| 140 |
|
| 141 |
@unwrap_spec(op=int) |
| 142 |
def flock(space, w_fd, op): |
| 143 |
"""flock(fd, operation) |
| 144 |
|
| 145 |
Perform the lock operation op on file descriptor fd. See the Unix |
| 146 |
manual flock(3) for details. (On some systems, this function is |
| 147 |
emulated using fcntl().)""" |
| 148 |
|
| 149 |
if has_flock: |
| 150 |
fd = space.c_filedescriptor_w(w_fd) |
| 151 |
op = rffi.cast(rffi.INT, op) |
| 152 |
while True: |
| 153 |
rv = c_flock(fd, op) |
| 154 |
if rv < 0: |
| 155 |
_raise_error_maybe(space, "flock") |
| 156 |
else: |
| 157 |
return |
| 158 |
else: |
| 159 |
lockf(space, w_fd, op) |
| 160 |
|
| 161 |
@unwrap_spec(op=int, length=int, start=int, whence=int) |
| 162 |
def lockf(space, w_fd, op, length=0, start=0, whence=0): |
| 163 |
"""lockf (fd, operation, length=0, start=0, whence=0) |
| 164 |
|
| 165 |
This is essentially a wrapper around the fcntl() locking calls. fd is the |
| 166 |
file descriptor of the file to lock or unlock, and operation is one of the |
| 167 |
following values: |
| 168 |
|
| 169 |
LOCK_UN - unlock |
| 170 |
LOCK_SH - acquire a shared lock |
| 171 |
LOCK_EX - acquire an exclusive lock |
| 172 |
|
| 173 |
When operation is LOCK_SH or LOCK_EX, it can also be bit-wise OR'd with |
| 174 |
LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the |
| 175 |
lock cannot be acquired, an IOError will be raised and the exception will |
| 176 |
have an errno attribute set to EACCES or EAGAIN (depending on the |
| 177 |
operating system -- for portability, check for either value). |
| 178 |
|
| 179 |
length is the number of bytes to lock, with the default meaning to lock to |
| 180 |
EOF. start is the byte offset, relative to whence, to that the lock |
| 181 |
starts. whence is as with fileobj.seek(), specifically: |
| 182 |
|
| 183 |
0 - relative to the start of the file (SEEK_SET) |
| 184 |
1 - relative to the current buffer position (SEEK_CUR) |
| 185 |
2 - relative to the end of the file (SEEK_END)""" |
| 186 |
|
| 187 |
fd = space.c_filedescriptor_w(w_fd) |
| 188 |
|
| 189 |
if op == LOCK_UN: |
| 190 |
l_type = F_UNLCK |
| 191 |
elif op & LOCK_SH: |
| 192 |
l_type = F_RDLCK |
| 193 |
elif op & LOCK_EX: |
| 194 |
l_type = F_WRLCK |
| 195 |
else: |
| 196 |
raise oefmt(space.w_ValueError, "unrecognized lock operation") |
| 197 |
|
| 198 |
op = [F_SETLKW, F_SETLK][int(bool(op & LOCK_NB))] |
| 199 |
op = rffi.cast(rffi.INT, op) |
| 200 |
|
| 201 |
with lltype.scoped_alloc(_flock.TO) as l: |
| 202 |
rffi.setintfield(l, 'c_l_type', l_type) |
| 203 |
rffi.setintfield(l, 'c_l_start', int(start)) |
| 204 |
rffi.setintfield(l, 'c_l_len', int(length)) |
| 205 |
rffi.setintfield(l, 'c_l_whence', int(whence)) |
| 206 |
while True: |
| 207 |
rv = fcntl_flock(fd, op, l) |
| 208 |
if rv < 0: |
| 209 |
_raise_error_maybe(space, "fcntl") |
| 210 |
else: |
| 211 |
return |
| 212 |
|
| 213 |
@unwrap_spec(mutate_flag=int, w_arg=WrappedDefault(0)) |
| 214 |
def ioctl(space, w_fd, w_request, w_arg, mutate_flag=-1): |
| 215 |
"""ioctl(fd, op[, arg[, mutate_flag]]) |
| 216 |
|
| 217 |
Perform the operation op on file descriptor fd. The values used for op |
| 218 |
are operating system dependent, and are available as constants in the |
| 219 |
fcntl or termios library modules, using the same names as used in the |
| 220 |
relevant C header files. |
| 221 |
|
| 222 |
The argument arg is optional, and defaults to 0; it may be an int or a |
| 223 |
buffer containing character data (most likely a string or an array). |
| 224 |
|
| 225 |
If the argument is a mutable buffer (such as an array) and if the |
| 226 |
mutate_flag argument (which is only allowed in this case) is true then the |
| 227 |
buffer is (in effect) passed to the operating system and changes made by |
| 228 |
the OS will be reflected in the contents of the buffer after the call has |
| 229 |
returned. The return value is the integer returned by the ioctl system |
| 230 |
call. |
| 231 |
|
| 232 |
If the argument is a mutable buffer and the mutable_flag argument is not |
| 233 |
passed or is false, the behavior is as if a string had been passed. This |
| 234 |
behavior will change in future releases of Python. |
| 235 |
|
| 236 |
If the argument is an immutable buffer (most likely a string) then a copy |
| 237 |
of the buffer is passed to the operating system and the return value is a |
| 238 |
string of the same length containing whatever the operating system put in |
| 239 |
the buffer. The length of the arg buffer in this case is not allowed to |
| 240 |
exceed 1024 bytes. |
| 241 |
|
| 242 |
If the arg given is an integer or if none is specified, the result value is |
| 243 |
an integer corresponding to the return value of the ioctl call in the C |
| 244 |
code.""" |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
IOCTL_BUFSZ = 1024 |
| 252 |
|
| 253 |
fd = space.c_filedescriptor_w(w_fd) |
| 254 |
op = space.bigint_w(w_request).uintmask() |
| 255 |
op = rffi.cast(rffi.UINT, op) |
| 256 |
try: |
| 257 |
rwbuffer = space.writebuf_w(w_arg) |
| 258 |
except OperationError as e: |
| 259 |
if not (e.match(space, space.w_TypeError) or |
| 260 |
e.match(space, space.w_BufferError)): |
| 261 |
raise |
| 262 |
else: |
| 263 |
arg = rwbuffer.as_str() |
| 264 |
ll_arg = rffi.str2charp(arg) |
| 265 |
to_alloc = max(IOCTL_BUFSZ, len(arg)) |
| 266 |
try: |
| 267 |
with rffi.scoped_alloc_buffer(to_alloc) as buf: |
| 268 |
rffi.c_memcpy(rffi.cast(rffi.VOIDP, buf.raw), |
| 269 |
rffi.cast(rffi.CONST_VOIDP, ll_arg), len(arg)) |
| 270 |
rv = ioctl_str(fd, op, buf.raw) |
| 271 |
if rv < 0: |
| 272 |
_raise_error_always(space, "ioctl") |
| 273 |
arg = rffi.charpsize2str(buf.raw, len(arg)) |
| 274 |
if mutate_flag != 0: |
| 275 |
rwbuffer.setslice(0, arg) |
| 276 |
return space.newint(rv) |
| 277 |
return space.newbytes(arg) |
| 278 |
finally: |
| 279 |
lltype.free(ll_arg, flavor='raw') |
| 280 |
|
| 281 |
if mutate_flag != -1: |
| 282 |
raise oefmt(space.w_TypeError, |
| 283 |
"ioctl requires a file or file descriptor, an integer and " |
| 284 |
"optionally an integer or buffer argument") |
| 285 |
|
| 286 |
try: |
| 287 |
arg = space.getarg_w('s#', w_arg) |
| 288 |
except OperationError as e: |
| 289 |
if not e.match(space, space.w_TypeError): |
| 290 |
raise |
| 291 |
else: |
| 292 |
ll_arg = rffi.str2charp(arg) |
| 293 |
to_alloc = max(IOCTL_BUFSZ, len(arg)) |
| 294 |
try: |
| 295 |
with rffi.scoped_alloc_buffer(to_alloc) as buf: |
| 296 |
rffi.c_memcpy(rffi.cast(rffi.VOIDP, buf.raw), |
| 297 |
rffi.cast(rffi.CONST_VOIDP, ll_arg), len(arg)) |
| 298 |
rv = ioctl_str(fd, op, buf.raw) |
| 299 |
if rv < 0: |
| 300 |
_raise_error_always(space, "ioctl") |
| 301 |
arg = rffi.charpsize2str(buf.raw, len(arg)) |
| 302 |
return space.newbytes(arg) |
| 303 |
finally: |
| 304 |
lltype.free(ll_arg, flavor='raw') |
| 305 |
|
| 306 |
intarg = space.int_w(w_arg) |
| 307 |
intarg = rffi.cast(rffi.INT, intarg) |
| 308 |
rv = ioctl_int(fd, op, intarg) |
| 309 |
if rv < 0: |
| 310 |
_raise_error_always(space, "ioctl") |
| 311 |
return space.newint(rv) |