[CRIU] Re: [PATCH 2/4] image: add functions to close and re-open the network listen socket

Pavel Emelyanov xemul at parallels.com
Thu Aug 16 12:10:41 EDT 2012


On 08/16/2012 07:56 PM, Adrian Reber wrote:
> On Wed, Aug 15, 2012 at 02:53:13PM +0400, Pavel Emelyanov wrote:
>> On 08/15/2012 01:54 PM, Adrian Reber wrote:
>>> During the restore of the file descriptors all open network
>>> sockets have to be closed.
>>
>> I don't quite understand this requirement. Can you elaborate, please?
> 
> I think it is during prepare_fs(). If the network socket is still open
> (using fd=3) prepare_fs() fails if the program to restore wants to open
> that file descriptor. Maybe there is another way to handle it but
> closing and later reopening it was my only idea.

We face this when keep an image file with fds open. There's the move_img_fd()
thing for resolving this conflict, you should use it for image socket as well.

>>> The network hostname and port are stored during open_listen_socket()
>>> and after close_listen_socket() they can be re-used by calling
>>> reopen_listen_socket().
>>>
>>> Signed-off-by: Adrian Reber <adrian at lisas.de>
>>> ---
>>>  image.c           |   28 ++++++++++++++++++++++++++++
>>>  include/crtools.h |    2 ++
>>>  2 files changed, 30 insertions(+), 0 deletions(-)
>>>
>>> diff --git a/image.c b/image.c
>>> index 2115412..969017a 100644
>>> --- a/image.c
>>> +++ b/image.c
>>> @@ -218,6 +218,8 @@ struct cr_fdset *cr_glob_fdset_open(int mode)
>>>  
>>>  static int image_dir_fd = -1;
>>>  static int sockfd = -1;
>>> +static char network_host[255] = "";
>>> +static int network_port = -1;
>>>  
>>>  int open_image(int type, unsigned long flags, ...)
>>>  {
>>> @@ -309,6 +311,15 @@ int open_listen_socket(char *host, int port)
>>>  	struct hostent *server;
>>>  	struct sockaddr_in serveraddr;
>>>  
>>> +	if (sockfd != -1)
>>> +		return sockfd;
>>> +
>>> +	/* if host is not specified return early */
>>> +	if (host == NULL)
>>> +		return -1;
>>> +
>>> +	pr_info("Listening on %s:%d\n", host, port);
>>> +
>>>  	sockfd = socket(AF_INET, SOCK_STREAM, 0);
>>>  	if (sockfd < 0) {
>>>  		pr_perror("ERROR opening socket");
>>> @@ -336,5 +347,22 @@ int open_listen_socket(char *host, int port)
>>>  		return -1;
>>>  	}
>>>  
>>> +	strncpy(network_host, host, 255);
>>> +	network_port = port;
>>>  	return sockfd;
>>>  }
>>> +
>>> +void close_listen_socket()
>>> +{
>>> +	if (sockfd == -1)
>>> +		return;
>>> +	close_safe(&sockfd);
>>> +	sockfd = -1;
>>> +}
>>> +
>>> +int reopen_listen_socket()
>>> +{
>>> +	if (network_port != -1)
>>> +		return open_listen_socket(network_host, network_port);
>>> +	return 0;
>>> +}
>>> diff --git a/include/crtools.h b/include/crtools.h
>>> index c2ccbe0..c092802 100644
>>> --- a/include/crtools.h
>>> +++ b/include/crtools.h
>>> @@ -133,6 +133,8 @@ extern int open_image_dir(void);
>>>  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();
>>>  
>>>  int open_image(int type, unsigned long flags, ...);
>>>  #define open_image_ro(type, ...) open_image(type, O_RDONLY, ##__VA_ARGS__)
> .
> 



More information about the CRIU mailing list