Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

open regular file = lstat + open + fcntl + ioctl ?

61 views
Skip to first unread message

xavier.d...@gmail.com

unread,
Jan 21, 2018, 2:59:13 PM1/21/18
to
Hello,

Looking at what happens when opening a regular file in Tcl to read it. I discovered that a lot of syscall are made in addition to the 'open' call.

I am running on Linux, and tracing the "/path/to/file" file opening on Tcl 8.6.6 gives:

lstat("/path", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/path/to", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/path/to/file", O_RDONLY) = 3
fcntl(3, F_SETFD, FD_CLOEXEC) = 0
ioctl(3, TCGETS, 0x7fff5efc0740) = -1 ENOTTY (Inappropriate ioctl for device)

I am working on a Tcl software where hundreds of files in a directory tree need to be read [1]. I would like to optimize this process so I wonder how the total number of syscalls can be reduced.

Is there a way to open a file for read only that will only produce a 'open' call to the system?

Regards,
Xavier

[1] http://modules.sourceforge.net/

Brad Lanam

unread,
Jan 21, 2018, 4:02:59 PM1/21/18
to
If you want to reduce the number of calls to lstat(),
which is checking for symlinks and path permissions,
first change your working directory to the directory containing the files.

cd /path/to
set fh [open file1 r]

I don't think you will be able to reduce the number of system calls
much more than that.


briang

unread,
Jan 21, 2018, 5:50:33 PM1/21/18
to
If you are counting sys calls, you should be writing in C. You could write your own extension open command.

Eliminating those sys calls will reduce error detection and thus reliability.

-Brian

xavier.d...@gmail.com

unread,
Jan 23, 2018, 12:01:42 AM1/23/18
to
Thanks for these hints. I was thinking also to handle that with an extension written in C. But for the sake of the maintainability I would prefer that some enhancement would be done at the Tcl source code directly.

Especially I do not understand what are the benefit for:
* the "lstat" cascading: if there is a permission issue, "open" will tell it
* analysing the content of a directory, each file opening within the directory will redo the same "lstat" starting from filesystem root, maybe it can only be done once
* the "ioctl" call: it does not seem appropriate on a regular file

Regards,
Xavier

briang

unread,
Jan 23, 2018, 1:06:36 AM1/23/18
to
At any time a virtual file system can be mounted at any point along the path. For each open the path must be checked just in case it changed. The lstat is actually the last check.

-Brian

Rich

unread,
Jan 23, 2018, 6:31:20 AM1/23/18
to
xavier.d...@gmail.com wrote:
> * the "ioctl" call: it does not seem appropriate on a regular file

The tty devices are just "regular files" from the perspective of an
open() call. The ioctl is to see if the file, that was just opened, is
a tty device or not.

The reason is to likely do something different when the file /is/ a tty
device.
0 new messages