A question on passing descriptors between processes (in C)
Hi all! I am writing a little irc-like server to learn unix programming in C. As a cool excercise, I would like to do a way to make my program upgrade without any downtime. So here it is my idea: First process: loader This is not the part of the server which must be upgraded. The only thing to do is getting a deamon, then forking and execing the real server. Then sleeping until a signal awakes it to fork and exec the next (upgraded) server. Second process: the server It must be upgraded. It does the real job, handles connections and so on. It handles also the downloading of the upgraded server. It works until it receives a signal that put it in the "greeting mode" which I describe below. Third process: the upgraded server It does the real job, but first he must have all data and socket descriptors from the "Second process", so it starts in "coming mode". The greeting and coming modes are those when the data (and socket descriptors) exchange occur. I am thinking about unix domain sockets to make socket descriptors and data pass (as described in Stevens' book, the first one on network programming). So, this is my question: there is a simpler way to do that? Is my idea good? Praise
On 18 Apr 2003 at 17:40, Praise wrote:
From: Praise
Hi all!
I am writing a little irc-like server to learn unix programming in C. As a cool excercise, I would like to do a way to make my program upgrade without any downtime. So here it is my idea:
First process: loader This is not the part of the server which must be upgraded. The only thing to do is getting a deamon, then forking and execing the real server. Then sleeping until a signal awakes it to fork and exec the next (upgraded) server.
Second process: the server It must be upgraded. It does the real job, handles connections and so on. It handles also the downloading of the upgraded server. It works until it receives a signal that put it in the "greeting mode" which I describe below.
Third process: the upgraded server It does the real job, but first he must have all data and socket descriptors from the "Second process", so it starts in "coming mode".
The greeting and coming modes are those when the data (and socket descriptors) exchange occur. I am thinking about unix domain sockets to make socket descriptors and data pass (as described in Stevens' book, the first one on network programming). So, this is my question: there is a simpler way to do that? Is my idea good?
Praise
As far as I know the process described in Stevens book is the standard way of transferring socket descriptors in cases where you have unrelated processes. However with if you had the old server fork() the new one, then the new one would inherit all the descriptors from the old server :) alan -- http://www.ibgames.net/alan Registered Linux user #6822 http://counter.li.org Winding Down - Weekly Tech Newsletter - subscribe at http://www.ibgames.net/alan/winding/mailing.html
As far as I know the process described in Stevens book is the standard way of transferring socket descriptors in cases where you have unrelated processes. However with if you had the old server fork() the new one, then the new one would inherit all the descriptors from the old server :)
Right, but I should exec* anyway, and I think exec clear all descriptors, or am I wrong? Praise
On Fri, 18 Apr 2003 20:01:05 +0200
Praise
As far as I know the process described in Stevens book is the standard way of transferring socket descriptors in cases where you have unrelated processes. However with if you had the old server fork() the new one, then the new one would inherit all the descriptors from the old server :)
Right, but I should exec* anyway, and I think exec clear all descriptors, or am I wrong?
File descriptors are inherited over exec.
One of the things init does is to close all file descriptors.
--
Jerry Feldman
Alle 21:13, venerdì 18 aprile 2003, Jerry Feldman ha scritto:
On Fri, 18 Apr 2003 20:01:05 +0200
Praise
wrote: As far as I know the process described in Stevens book is the standard way of transferring socket descriptors in cases where you have unrelated processes. However with if you had the old server fork() the new one, then the new one would inherit all the descriptors from the old server :)
Right, but I should exec* anyway, and I think exec clear all descriptors, or am I wrong?
File descriptors are inherited over exec. One of the things init does is to close all file descriptors.
And how could I access them? If I exec a new program it never knows about the former descriptors, even if they remain opened... Praise
Praise
And how could I access them? If I exec a new program it never knows about the former descriptors, even if they remain opened...
In some instances it does. How do think the shell sets STDIN and STDOUT (think pipes and redirection as well as controlling terminal)for programs it launches? Or the consequences (for the subprocess) of calling popen(3).
Alle 23:20, venerdì 18 aprile 2003, Graham Murray ha scritto:
Praise
writes: And how could I access them? If I exec a new program it never knows about the former descriptors, even if they remain opened...
In some instances it does. How do think the shell sets STDIN and STDOUT (think pipes and redirection as well as controlling terminal)for programs it launches? Or the consequences (for the subprocess) of calling popen(3).
Well, I had not clear that:) I supposed that before, but I did not know perfectly. From the manual page: "The exec family of functions replaces the current process image with a new process image. " I thought descriptors (at least, not the standard descriptors) were part of a process image. Praise
Praise
Well, I had not clear that:) I supposed that before, but I did not know perfectly. From the manual page:
"The exec family of functions replaces the current process image with a new process image. "
I usually use the info pages rather than man for libc functions, and the info page for the exec family includes File descriptors open in the existing process image remain open in the new process image, unless they have the `FD_CLOEXEC' (close-on-exec) flag set. The files that remain open inherit all attributes of the open file description from the existing process image, including file locks. File descriptors are discussed in *Note Low-Level I/O::.
On 18 Apr 2003 at 22:46, Praise wrote:
From: Praise
Alle 21:13, venerdì 18 aprile 2003, Jerry Feldman ha scritto:
On Fri, 18 Apr 2003 20:01:05 +0200
Praise
wrote: As far as I know the process described in Stevens book is the standard way of transferring socket descriptors in cases where you have unrelated processes. However with if you had the old server fork() the new one, then the new one would inherit all the descriptors from the old server :)
Right, but I should exec* anyway, and I think exec clear all descriptors, or am I wrong?
File descriptors are inherited over exec. One of the things init does is to close all file descriptors.
And how could I access them? If I exec a new program it never knows about the former descriptors, even if they remain opened...
Praise
You could use poll (man 2 poll). Call it over the the max possible range of file descriptors and see which ones return POLLNVAL... alan -- http://www.ibgames.net/alan Registered Linux user #6822 http://counter.li.org Winding Down - Weekly Tech Newsletter - subscribe at http://www.ibgames.net/alan/winding/mailing.html
On Fri, 2003-04-18 at 21:13, Jerry Feldman wrote:
File descriptors are inherited over exec. One of the things init does is to close all file descriptors.
That depends on the value of FD_CLOEXEC. See "man fcntl", specifically the section on F_GETFD and F_SETFD
participants (5)
-
alan@ibgames.com
-
Anders Johansson
-
Graham Murray
-
Jerry Feldman
-
Praise