Questions tagged [fcntl]

fcntl refers to a function that provides control over the open file referenced by a file descriptor

fcntl refers to a function that provides control over the open file referenced by a file descriptor.

234 questions
34
votes
5 answers

How to get hard disk serial number using Python

How can I get the serial number of a hard disk drive using Python on Linux? I would like to use a Python module to do that instead of running an external program such as hdparm. Perhaps using the fcntl module?
Forge
  • 6,538
  • 6
  • 44
  • 64
26
votes
7 answers

Python fcntl does not lock as expected

On a Debian-based OS (Ubuntu, Debian Squeeze), I'm using Python (2.7, 3.2) fcntl to lock a file. As I understand from what I read, fnctl.flock locks a file in a way, that an exception will be thrown if another client wants to lock the same file. I…
Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
24
votes
1 answer

Is O_NONBLOCK being set a property of the file descriptor or underlying file?

From what I have been reading on The Open Group website on fcntl, open, read, and write, I get the impression that whether O_NONBLOCK is set on a file descriptor, and hence whether non-blocking I/O is used with the descriptor, should be a property…
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
18
votes
1 answer

python lockf and flock behaviour

I have read enough posts on stackoverflow regarding the difference between flock/lockf/fcntl but I am unable to answer the below observation: >>> import fcntl >>> a = open('/tmp/locktest', 'w') >>> b = open('/tmp/locktest', 'w') >>> fcntl.lockf(a,…
Jatin Kumar
  • 2,635
  • 9
  • 36
  • 46
16
votes
2 answers

Is it possible (and safe) to make an accepting socket non-blocking?

I'm looking for a way to interrupt an accept() call on a blocking socket. Using signals is not an option, as this is meant to be in a library and I don't want to clutter the user signals. Using select() is another option, buf for various reason it's…
Norswap
  • 11,740
  • 12
  • 47
  • 60
15
votes
2 answers

How to force linkage to older libc `fcntl` instead of `fcntl64`?

It seems GLIBC 2.28 (released August 2018) made a fairly aggressive change to fcntl. The definition was changed in to no longer be an external function, but a #define to fcntl64. The upshot is that if you compile your code on a system…
14
votes
3 answers

How do I atomically create a locked file in Linux?

Scenario: I have many processes running that need to fetch files over the net. If the file is already downloaded, I want it cached on disk. If another process is downloading the file, block until it is finished downloading. I've been trying to find…
UsAaR33
  • 3,536
  • 2
  • 34
  • 55
12
votes
1 answer

Setting stdout to non-blocking in python

Prior warning: I'm hacking around here out of curiosity. I have no specific reason to do what I'm doing below! Below is done on Python 2.7.13 on MacOS 10.12.5 I was hacking around with python and I thought it'd be interesting to see what happened if…
Andrew Parker
  • 1,425
  • 2
  • 20
  • 28
12
votes
3 answers

open() doesn't set O_CLOEXEC flag

I try to set O_CLOEXEC flag using open() and have no sucess. Consider the following microtest: #include #include int main() { int fd = open("test.c", O_RDONLY | O_CLOEXEC); int ret = fcntl(fd, F_GETFL); if(ret & O_CLOEXEC)…
Ivan Efremov
  • 158
  • 1
  • 6
11
votes
1 answer

How can I make a non-blocking request for an exclusive lock using File#flock?

How Should I Request a Non-Blocking Lock? Why doesn't Ruby's File#flock work as expected when separate attempts are made to lock a file? Locking the file in a block is not the correct solution for this issue because the point is to show the behavior…
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
10
votes
1 answer

How to specify a github repo as the source of dependency of a module in Raku?

My module depends on the Fcntl module (https://github.com/manchicken/perl6-Fcntl), which hasn't been updated in a long time and is broken. However, there's a fork (https://github.com/jonathanstowe/perl6-Fcntl) that works for me if I zef install it…
cowbaymoo
  • 1,202
  • 5
  • 14
10
votes
3 answers

How to properly convert a C ioctl call to a python fcntl.ioctl call?

Following an example on resetting a serial port in Linux I wanted to translate the following snippet fd = open(filename, O_WRONLY); ioctl(fd, USBDEVFS_RESET, 0); close(fd); into valid python code. Here is what I have tried so far file_handler =…
Alex
  • 41,580
  • 88
  • 260
  • 469
9
votes
4 answers

linux fcntl - unsetting flag

How do i unset a already set flag using fcntl? For e.g. I can set the socket to nonblocking mode using fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) Now, i want to unset the O_NONBLOCK flag. I tried fcntl(sockfd, F_SETFL, flags | ~O_NONBLOCK). It…
chappar
  • 7,275
  • 12
  • 44
  • 57
9
votes
1 answer

How to atomically create "close-on-exec" socket on Mac?

When I create socket on Linux, it's possible to specify the flag O_CLOEXEC on creation time: auto fd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0); So there is no way, that some other thread will do fork()+exec() with this socket remaining…
abyss.7
  • 13,882
  • 11
  • 56
  • 100
8
votes
2 answers

c - Usage of F_GETFL and F_SETFL

While trying to use fcntl() with command F_GETFL and F_SETFL, I got some questions: Why the flag returned by fcntl(fd, F_GETFL) only include a subset of bits of what I set when open file? Does it only show the ones that are modifiable? When use…
Eric
  • 22,183
  • 20
  • 145
  • 196
1
2 3
15 16