Skip to contentSkip to conversation

Auto closing of file descriptors and fork.

Xenomai-core Gilles Chanteperdrix 3 participants 9 messages Dec 20, 2007 – Dec 27, 2007

lore
Share & tools
lore
#0 lore raw

Hi,

I have a problem with auto closing of file descriptors and fork. I have an application (a modified pppd, to name it), which in a certain mode (pppd "updetach" mode) opens some real-time file descriptors, forks and exit the parent, continuing to use the file descriptors in the child. The problem is that when exiting the parent, file descriptors are automatically destroyed and therefore can not be used in the child.

Any ideas for a fix ?

Signature · 1 line
                                               Gilles Chanteperdrix
#1 lore raw
Quoted 11 lines · Gilles Chanteperdrix

Hi,

I have a problem with auto closing of file descriptors and fork. I have an application (a modified pppd, to name it), which in a certain mode (pppd "updetach" mode) opens some real-time file descriptors, forks and exit the parent, continuing to use the file descriptors in the child. The problem is that when exiting the parent, file descriptors are automatically destroyed and therefore can not be used in the child.

Any ideas for a fix ?

Maybe not exiting the child but saying waitpid() ?

Daniel Schnell.

#2 lore raw
Quoted 11 lines · Daniel Schnell
Quoted 10 lines · Gilles Chanteperdrix

Hi,

I have a problem with auto closing of file descriptors and fork. I have an application (a modified pppd, to name it), which in a certain mode (pppd "updetach" mode) opens some real-time file descriptors, forks and exit the parent, continuing to use the file descriptors in the child. The problem is that when exiting the parent, file descriptors are automatically destroyed and therefore can not be used in the child.

Quoted 3 lines
Quoted 1 line

Any ideas for a fix ?

Maybe not exiting the child but saying waitpid() ?

The problem is that the aim of pppd updetach is to exit the parent once the connection is established, so that the script running pppd can continue.

Signature · 1 line
                                               Gilles Chanteperdrix
#3 lore raw
In reply to Gilles Chanteperdrix
Quoted 11 lines · Gilles Chanteperdrix

Hi,

I have a problem with auto closing of file descriptors and fork. I have an application (a modified pppd, to name it), which in a certain mode (pppd "updetach" mode) opens some real-time file descriptors, forks and exit the parent, continuing to use the file descriptors in the child. The problem is that when exiting the parent, file descriptors are automatically destroyed and therefore can not be used in the child.

Any ideas for a fix ?

More precisely. We need to trap the "fork" event, and handle it in the skins event callbacks. We will need to create a new ppd structure for the new process, but what will we do with the skin objects ? If we keep the same objects for the child and increment a reference count, we will end up with an object that need to be inserted in two per-process lists. If we create new objects, how will we manage for user-space references (inherited accross fork) to remain valid ?

Signature · 1 line
					    Gilles Chanteperdrix.
#4 lore raw
Quoted 13 lines · Gilles Chanteperdrix
Quoted 11 lines · Gilles Chanteperdrix

Hi,

I have a problem with auto closing of file descriptors and fork. I have an application (a modified pppd, to name it), which in a certain mode (pppd "updetach" mode) opens some real-time file descriptors, forks and exit the parent, continuing to use the file descriptors in the child. The problem is that when exiting the parent, file descriptors are automatically destroyed and therefore can not be used in the child.

Any ideas for a fix ?

Quoted 7 lines

More precisely. We need to trap the "fork" event, and handle it in the skins event callbacks. We will need to create a new ppd structure for the new process, but what will we do with the skin objects ? If we keep the same objects for the child and increment a reference count, we will end up with an object that need to be inserted in two per-process lists. If we create new objects, how will we manage for user-space references (inherited accross fork) to remain valid ?

The only way I see are process-private file descriptor tables + reference counters for the underlying objects. On fork, the descriptor table would be cloned and the reference counter of the contained object would be incremented, creating another reference to them. Or should we rather re-open those objects, creating another instance?

BTW, by adding a fork hook, my old idea of storing the ppd reference in some I-pipe ptdkey comes into reach again.

Jan

#5 lore raw
Quoted 23 lines · Jan Kiszka
Quoted 13 lines · Gilles Chanteperdrix
Quoted 11 lines · Gilles Chanteperdrix

Hi,

I have a problem with auto closing of file descriptors and fork. I have an application (a modified pppd, to name it), which in a certain mode (pppd "updetach" mode) opens some real-time file descriptors, forks and exit the parent, continuing to use the file descriptors in the child. The problem is that when exiting the parent, file descriptors are automatically destroyed and therefore can not be used in the child.

Any ideas for a fix ?

Quoted 7 lines

More precisely. We need to trap the "fork" event, and handle it in the skins event callbacks. We will need to create a new ppd structure for the new process, but what will we do with the skin objects ? If we keep the same objects for the child and increment a reference count, we will end up with an object that need to be inserted in two per-process lists. If we create new objects, how will we manage for user-space references (inherited accross fork) to remain valid ?

Quoted 5 lines

The only way I see are process-private file descriptor tables + reference counters for the underlying objects. On fork, the descriptor table would be cloned and the reference counter of the contained object would be incremented, creating another reference to them. Or should we rather re-open those objects, creating another instance?

That is essentially the two alternatives I propose.

The problem when cloning the file descriptor table is that if there is an xnholder_t (or something similar) in a file descriptor object, the file descriptor can only be linked to one table.

The problem of re-opening the objects is that the user-space references to the re-opened object should be updated. It is impossible for a file descriptor.

Signature · 1 line
                                               Gilles Chanteperdrix
#6 lore raw
Quoted 28 lines · Gilles Chanteperdrix
Quoted 21 lines · Jan Kiszka
Quoted 13 lines · Gilles Chanteperdrix
Quoted 11 lines · Gilles Chanteperdrix

Hi,

I have a problem with auto closing of file descriptors and fork. I have an application (a modified pppd, to name it), which in a certain mode (pppd "updetach" mode) opens some real-time file descriptors, forks and exit the parent, continuing to use the file descriptors in the child. The problem is that when exiting the parent, file descriptors are automatically destroyed and therefore can not be used in the child.

Any ideas for a fix ?

Quoted 7 lines

More precisely. We need to trap the "fork" event, and handle it in the skins event callbacks. We will need to create a new ppd structure for the new process, but what will we do with the skin objects ? If we keep the same objects for the child and increment a reference count, we will end up with an object that need to be inserted in two per-process lists. If we create new objects, how will we manage for user-space references (inherited accross fork) to remain valid ?

Quoted 5 lines

The only way I see are process-private file descriptor tables + reference counters for the underlying objects. On fork, the descriptor table would be cloned and the reference counter of the contained object would be incremented, creating another reference to them. Or should we rather re-open those objects, creating another instance?

Quoted 5 lines

That is essentially the two alternatives I propose.

The problem when cloning the file descriptor table is that if there is an xnholder_t (or something similar) in a file descriptor object, the file descriptor can only be linked to one table.

Hmm, where is there difference between this cloning and "normal" multi-threaded usage? The only difference I see is that there are now two separated address spaces from the user perspective. But for the kernel, it stays the same.

Quoted 4 lines

The problem of re-opening the objects is that the user-space references to the re-opened object should be updated. It is impossible for a file descriptor.

The only user space reference is the file descriptor - an index. And as this index stays the same across the fork, I don't see the problem.

Jan

#7 lore raw
Quoted 32 lines · Jan Kiszka
Quoted 30 lines · Gilles Chanteperdrix
Quoted 21 lines · Jan Kiszka
Quoted 13 lines · Gilles Chanteperdrix
Quoted 11 lines · Gilles Chanteperdrix

Hi,

I have a problem with auto closing of file descriptors and fork. I have an application (a modified pppd, to name it), which in a certain mode (pppd "updetach" mode) opens some real-time file descriptors, forks and exit the parent, continuing to use the file descriptors in the child. The problem is that when exiting the parent, file descriptors are automatically destroyed and therefore can not be used in the child.

Any ideas for a fix ?

Quoted 7 lines

More precisely. We need to trap the "fork" event, and handle it in the skins event callbacks. We will need to create a new ppd structure for the new process, but what will we do with the skin objects ? If we keep the same objects for the child and increment a reference count, we will end up with an object that need to be inserted in two per-process lists. If we create new objects, how will we manage for user-space references (inherited accross fork) to remain valid ?

Quoted 5 lines

The only way I see are process-private file descriptor tables + reference counters for the underlying objects. On fork, the descriptor table would be cloned and the reference counter of the contained object would be incremented, creating another reference to them. Or should we rather re-open those objects, creating another instance?

That is essentially the two alternatives I propose.

Quoted 8 lines
Quoted 3 lines

The problem when cloning the file descriptor table is that if there is an xnholder_t (or something similar) in a file descriptor object, the file descriptor can only be linked to one table.

Hmm, where is there difference between this cloning and "normal" multi-threaded usage? The only difference I see is that there are now two separated address spaces from the user perspective. But for the kernel, it stays the same.

If there are two ppds, there are two lists (or hash table, or whatever data structure). So, we now have an object, with only one xnholder_t (or list_head) that must be in two lists. This does not work.

Quoted 8 lines
Quoted 5 lines

The problem of re-opening the objects is that the user-space references to the re-opened object should be updated. It is impossible for a file descriptor.

The only user space reference is the file descriptor - an index. And as this index stays the same across the fork, I don't see the problem.

The user-space file descriptor is __rtdm_fd_start + kernel-space file descriptor. If you re-open an object, you will obtain a new kernel-space file descriptor, and hence a different user-space file descriptor. So, no, the index will not stay the same across the fork.

Signature · 1 line
                                               Gilles Chanteperdrix
#8 lore raw
Quoted 34 lines · Gilles Chanteperdrix
Quoted 33 lines · Jan Kiszka
Quoted 32 lines · Gilles Chanteperdrix
Quoted 21 lines · Jan Kiszka
Quoted 13 lines · Gilles Chanteperdrix
Quoted 11 lines · Gilles Chanteperdrix

Hi,

I have a problem with auto closing of file descriptors and fork. I have an application (a modified pppd, to name it), which in a certain mode (pppd "updetach" mode) opens some real-time file descriptors, forks and exit the parent, continuing to use the file descriptors in the child. The problem is that when exiting the parent, file descriptors are automatically destroyed and therefore can not be used in the child.

Any ideas for a fix ?

Quoted 7 lines

More precisely. We need to trap the "fork" event, and handle it in the skins event callbacks. We will need to create a new ppd structure for the new process, but what will we do with the skin objects ? If we keep the same objects for the child and increment a reference count, we will end up with an object that need to be inserted in two per-process lists. If we create new objects, how will we manage for user-space references (inherited accross fork) to remain valid ?

Quoted 5 lines

The only way I see are process-private file descriptor tables + reference counters for the underlying objects. On fork, the descriptor table would be cloned and the reference counter of the contained object would be incremented, creating another reference to them. Or should we rather re-open those objects, creating another instance?

That is essentially the two alternatives I propose.

The problem when cloning the file descriptor table is that if there is an xnholder_t (or something similar) in a file descriptor object, the file descriptor can only be linked to one table.

Quoted 8 lines
Quoted 4 lines

Hmm, where is there difference between this cloning and "normal" multi-threaded usage? The only difference I see is that there are now two separated address spaces from the user perspective. But for the kernel, it stays the same.

If there are two ppds, there are two lists (or hash table, or whatever data structure). So, we now have an object, with only one xnholder_t (or list_head) that must be in two lists. This does not work.

At least for RTDM, objects that are addressed via file descriptors are not managed in ppd tables. Instead, the descriptor table already contains enough information to find those objects on process termination. At the moment, there is an owner field in the object required to tell the ownerships apart. With process-private descriptor tables this field would become obsolete.

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines

The problem of re-opening the objects is that the user-space references to the re-opened object should be updated. It is impossible for a file descriptor.

The only user space reference is the file descriptor - an index. And as this index stays the same across the fork, I don't see the problem.

The user-space file descriptor is __rtdm_fd_start + kernel-space file descriptor. If you re-open an object, you will obtain a new kernel-space file descriptor, and hence a different user-space file descriptor. So, no, the index will not stay the same across the fork.

Not with the current implementation, true. But as I said: the prerequisite for fork-persistent file descriptors are process-private descriptor tables anyway. And at that point, the indexes will stay the same.

Jan