[CRIU] Re: [PATCH 5/5] util: move network socket to free FD during
restore
Adrian Reber
adrian at lisas.de
Wed Aug 22 05:26:43 EDT 2012
On Tue, Aug 21, 2012 at 08:39:24PM +0400, Pavel Emelyanov wrote:
> On 08/21/2012 07:20 PM, Adrian Reber wrote:
> > The socket listening on the network has to be moved during the
> > restore so that it does not collide with FDs which are trying
> > to be restored.
> >
> > Signed-off-by: Adrian Reber <adrian at lisas.de>
> > ---
> > image.c | 7 +++++++
> > include/crtools.h | 1 +
> > util.c | 9 +++++++++
> > 3 files changed, 17 insertions(+), 0 deletions(-)
> >
> > diff --git a/image.c b/image.c
> > index 969017a..0d84c5c 100644
> > --- a/image.c
> > +++ b/image.c
> > @@ -366,3 +366,10 @@ int reopen_listen_socket()
> > return open_listen_socket(network_host, network_port);
> > return 0;
> > }
> > +
> > +void update_listen_socket(int fd)
> > +{
> > + if (sockfd == -1)
> > + return;
> > + sockfd = fd;
> > +}
> > diff --git a/include/crtools.h b/include/crtools.h
> > index c092802..f056327 100644
> > --- a/include/crtools.h
> > +++ b/include/crtools.h
> > @@ -135,6 +135,7 @@ extern void close_image_dir(void);
> > extern int open_listen_socket(char *host, int port);
> > extern void close_listen_socket();
> > extern int reopen_listen_socket();
> > +extern void update_listen_socket(int fd);
> >
> > int open_image(int type, unsigned long flags, ...);
> > #define open_image_ro(type, ...) open_image(type, O_RDONLY, ##__VA_ARGS__)
> > diff --git a/util.c b/util.c
> > index 4969e8d..91795d1 100644
> > --- a/util.c
> > +++ b/util.c
> > @@ -83,6 +83,15 @@ int reopen_fd_as_safe(char *file, int line, int new_fd, int old_fd, bool allow_r
> > if (old_fd != new_fd) {
> >
> > if (!allow_reuse_fd) {
> > + /* see if we are running a network migration */
> > + tmp = open_listen_socket(NULL, 0);
> > + if (tmp != -1) {
> > + int tmp2;
> > + /* yes, network socket is open; let's move it */
> > + tmp2 = fcntl(tmp, F_DUPFD, new_fd);
> > + update_listen_socket(tmp2);
> > + close(tmp);
>
> You should just place a call to move_img_fd to proper place to prevent
> it from being overwritten by the restore fd.
The problem with move_img_fd() was that it moved my socket to FD 1 and
then the program to be restored segfaulted. Moving it with fcntl moves
it to a descriptor with a higher number and does the "right thing".
What is the correct way to use move_img_fd() without blocking of the
lower FDs?
Adrian
More information about the CRIU
mailing list