fcntl --- fcntl dan ioctl panggilan-panggilan system¶
Modul ini melakukan kontrol file dan I/O pada deskriptor file. Ini adalah antarmuka ke fcntl() dan rutinitas Unix ioctl(). Lihat halaman manual Unix fcntl(2) dan ioctl(2) untuk detail lengkap.
Ketersediaan
Semua fungsi dalam modul ini mengambil deskriptor file fd sebagai argumen pertama mereka. Ini bisa berupa deskriptor file bilangan bulat, seperti yang dikembalikan oleh sys.stdin.fileno(), atau objek io.IOBase, seperti sys.stdin itu sendiri, yang menyediakan fileno() yang mengembalikan deskriptor file yang sebenarnya.
Berubah pada versi 3.3: Operasi dalam modul ini digunakan untuk mengaktifkan IOError di mana sekarang mereka memunculkan OSError.
Berubah pada versi 3.8: The fcntl module now contains F_ADD_SEALS, F_GET_SEALS, and
F_SEAL_* constants for sealing of os.memfd_create() file
descriptors.
Berubah pada versi 3.9: On macOS, the fcntl module exposes the F_GETPATH constant, which obtains
the path of a file from a file descriptor.
On Linux(>=3.15), the fcntl module exposes the F_OFD_GETLK, F_OFD_SETLK
and F_OFD_SETLKW constants, which are used when working with open file
description locks.
Berubah pada versi 3.10: On Linux >= 2.6.11, the fcntl module exposes the F_GETPIPE_SZ and
F_SETPIPE_SZ constants, which allow to check and modify a pipe's size
respectively.
Berubah pada versi 3.11: On FreeBSD, the fcntl module exposes the F_DUP2FD and F_DUP2FD_CLOEXEC
constants, which allow to duplicate a file descriptor, the latter setting
FD_CLOEXEC flag in addition.
Berubah pada versi 3.12: On Linux >= 4.5, the fcntl module exposes the FICLONE and
FICLONERANGE constants, which allow to share some data of one file with
another file by reflinking on some filesystems (e.g., btrfs, OCFS2, and
XFS). This behavior is commonly referred to as "copy-on-write".
Modul ini mendefinisikan fungsi-fungsi berikut:
- fcntl.fcntl(fd, cmd, arg=0)¶
Perform the operation cmd on file descriptor fd (file objects providing a
fileno()method are accepted as well). The values used for cmd are operating system dependent, and are available as constants in thefcntlmodule, using the same names as used in the relevant C header files. The argument arg can either be an integer value, or abytesobject. With an integer value, the return value of this function is the integer return value of the Cfcntl()call. When the argument is bytes it represents a binary structure, e.g. created bystruct.pack(). The binary data is copied to a buffer whose address is passed to the Cfcntl()call. The return value after a successful call is the contents of the buffer, converted to abytesobject. The length of the returned object will be the same as the length of the arg argument. This is limited to 1024 bytes. If the information returned in the buffer by the operating system is larger than 1024 bytes, this is most likely to result in a segmentation violation or a more subtle data corruption.Jika panggilan
fcntl()gagal, sebuahOSErrorakan dimunculkan.Memunculkan auditing event
fcntl.fcntldengan argumenfd,cmd,arg.
- fcntl.ioctl(fd, request, arg=0, mutate_flag=True)¶
Fungsi ini identik dengan fungsi
fcntl(), kecuali bahwa penanganan argumen bahkan lebih rumit.The request parameter is limited to values that can fit in 32-bits. Additional constants of interest for use as the request argument can be found in the
termiosmodule, under the same names as used in the relevant C header files.The parameter arg can be one of an integer, an object supporting the read-only buffer interface (like
bytes) or an object supporting the read-write buffer interface (likebytearray).In all but the last case, behaviour is as for the
fcntl()function.If a mutable buffer is passed, then the behaviour is determined by the value of the mutate_flag parameter.
If it is false, the buffer's mutability is ignored and behaviour is as for a read-only buffer, except that the 1024 byte limit mentioned above is avoided -- so long as the buffer you pass is at least as long as what the operating system wants to put there, things should work.
If mutate_flag is true (the default), then the buffer is (in effect) passed to the underlying
ioctl()system call, the latter's return code is passed back to the calling Python, and the buffer's new contents reflect the action of theioctl(). This is a slight simplification, because if the supplied buffer is less than 1024 bytes long it is first copied into a static buffer 1024 bytes long which is then passed toioctl()and copied back into the supplied buffer.Jika panggilan
ioctl()gagal, sebuah pengecualianOSErrordimunculkan.Sebuah contoh:
>>> import array, fcntl, struct, termios, os >>> os.getpgrp() 13341 >>> struct.unpack('h', fcntl.ioctl(0, termios.TIOCGPGRP, " "))[0] 13341 >>> buf = array.array('h', [0]) >>> fcntl.ioctl(0, termios.TIOCGPGRP, buf, 1) 0 >>> buf array('h', [13341])
Memunculkan sebuah auditing event
fcntl.ioctldengan argumenfd,request,arg.
- fcntl.flock(fd, operation)¶
Lakukan operasi kunci operation pada deskriptor file fd (objek file yang menyediakan metoda
fileno()juga diterima). Lihat manual Unix flock(2) untuk detail. (Di beberapa sistem, fungsi ini disimulasikan menggunakanfcntl().)Jika panggilan
flock()gagal, sebuah pengecualianOSErrorakan dimunculkan.Memunculkan sebuah auditing event
fcntl.flockdengan argumenfd,operation.
- fcntl.lockf(fd, cmd, len=0, start=0, whence=0)¶
Ini pada dasarnya adalah pembungkus di sekitar panggilan penguncian
fcntl(). fd adalah deskriptor file (objek file yang menyediakan metodafileno()juga diterima) dari file yang akan dikunci atau dibuka kuncinya, dan cmd adalah salah satu dari nilai berikut:- fcntl.LOCK_UN¶
Rilis kunci yang ada.
- fcntl.LOCK_SH¶
Ambil kunci bersama.
- fcntl.LOCK_EX¶
Ambil kunci eksklusif.
- fcntl.LOCK_NB¶
Bitwise OR dengan salah satu dari tiga konstanta
LOCK_*lainnya untuk membuat permintaan tidak memblokir.
Jika
LOCK_NBdigunakan dan kunci tidak dapat diperoleh, sebuahOSErrorakan dimunculkan dan pengecualian tersebut akan memiliki atribut errno yang disetel keEACCESatauEAGAIN(tergantung pada sistem operasi; untuk portabilitas, periksa kedua nilai tersebut). Di setidaknya beberapa sistem,LOCK_EXhanya dapat digunakan jika deskriptor file merujuk pada file yang dibuka untuk penulisan.len adalah jumlah byte yang akan dikunci, start adalah offset byte di mana kunci dimulai, relatif terhadap whence, dan whence seperti pada
io.IOBase.seek(), secara khusus:0-- relatif terhadap awal file (os.SEEK_SET)1-- relatif terhadap posisi buffer saat ini (os.SEEK_CUR)2-- relatif terhadap akhir file (os.SEEK_END)
Bawaan untuk start adalah 0, yang berarti mulai dari awal file. Bawaan untuk len adalah 0 yang berarti mengunci hingga akhir file. Bawaan untuk whence juga 0.
Memunculkan sebuah auditing event
fcntl.lockfdengan argumenfd,cmd,len,start,whence.
Contoh-contoh (semua di sistem yang sesuai dengan SVR4):
import struct, fcntl, os
f = buka(...)
rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NDELAY)
lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
Perhatikan bahwa dalam contoh pertama variabel nilai rv akan menyimpan nilai integer; dalam contoh kedua ia akan menyimpan objek :kelas:`bytes`. Tata letak struktur untuk variabel lockdata tergantung pada sistem --- oleh karena itu menggunakan panggilan flock() mungkin lebih baik.