[CRIU] [PATCH 1/5] log: don't use global variables for saving service fds

Pavel Emelyanov xemul at parallels.com
Wed Jan 9 06:05:46 EST 2013


On 01/06/2013 02:48 PM, Andrey Vagin wrote:
> It's preparation for cloning service descriptors
> 
> Signed-off-by: Andrey Vagin <avagin at openvz.org>
> ---
>  log.c | 39 +++++++++++++++++++++++++--------------
>  1 file changed, 25 insertions(+), 14 deletions(-)
> 
> diff --git a/log.c b/log.c
> index 5fa1c6b..d4643ad 100644
> --- a/log.c
> +++ b/log.c
> @@ -20,8 +20,8 @@
>  #define DEFAULT_LOGFD		STDERR_FILENO
>  
>  static unsigned int current_loglevel = DEFAULT_LOGLEVEL;
> -static int current_logfd = DEFAULT_LOGFD;
> -static int logdir = -1;
> +static bool log_fd_inited = false;
> +static bool log_dir_inited = false;

Changing int into bool doesn't mean "don't use global variables". Do this:

static DECLARE_BITMAP(service_fds);

int open_service_fd(int type, int dest_fd)
{
	/*
	 * get fd number, dup2 dest_fd into it, close
	 * it then put a bit in service_fds mask
	 */
}

void close_service_fd(int type)
{
	/*
	 * check bitmap, return number or -1
	 */
}

int get_service_fd(int type)
{
	/*
	 * call close(), clear bit
	 */
}

>  static char buffer[PAGE_SIZE];
>  static char buf_off = 0;
> @@ -55,16 +55,18 @@ static void print_ts(void)
>  	buffer[TS_BUF_OFF - 1] = ' '; /* kill the '\0' produced by snprintf */
>  }
>  
> -
> -
>  int log_get_fd(void)
>  {
> -	return current_logfd;
> +	if (log_fd_inited)
> +		return get_service_fd(LOG_FD_OFF);
> +	else
> +		return DEFAULT_LOGFD;
>  }
>  
>  int log_init(const char *output)
>  {
>  	int new_logfd, sfd, dfd;
> +	int logfd = log_get_fd();
>  
>  	gettimeofday(&start, NULL);
>  	buf_off = TS_BUF_OFF;
> @@ -75,7 +77,7 @@ int log_init(const char *output)
>  		goto err;
>  	}
>  
> -	if (logdir < 0) {
> +	if (!log_dir_inited) {
>  		int tmp;
>  		tmp = open(".", O_RDONLY);
>  		if (tmp == -1) {
> @@ -86,7 +88,7 @@ int log_init(const char *output)
>  		if (reopen_fd_as(dfd, tmp) < 0)
>  			return -1;
>  
> -		logdir = dfd;
> +		log_dir_inited = true;
>  	}
>  
>  	sfd = get_service_fd(LOG_FD_OFF);
> @@ -96,14 +98,14 @@ int log_init(const char *output)
>  	}
>  
>  	if (output) {
> -		new_logfd = openat(logdir, output,
> +		new_logfd = openat(dfd, output,
>  					O_CREAT | O_TRUNC | O_WRONLY | O_APPEND, 0600);
>  		if (new_logfd < 0) {
>  			pr_perror("Can't create log file %s", output);
>  			return -1;
>  		}
>  
> -		if (sfd == current_logfd)
> +		if (sfd == logfd)
>  			close(sfd);
>  
>  		if (reopen_fd_as(sfd, new_logfd) < 0)
> @@ -116,7 +118,7 @@ int log_init(const char *output)
>  		}
>  	}
>  
> -	current_logfd = sfd;
> +	log_fd_inited = true;
>  
>  	return 0;
>  
> @@ -151,15 +153,24 @@ int log_init_by_pid(void)
>  
>  void log_fini(void)
>  {
> -	if (current_logfd > 2)
> -		close_safe(&current_logfd);
> +	int logfd = get_service_fd(LOG_FD_OFF);
>  
> -	current_logfd = DEFAULT_LOGFD;
> +	if (!log_fd_inited)
> +		return;
> +
> +	close_safe(&logfd);
> +	log_fd_inited = false;
>  }
>  
>  void log_closedir(void)
>  {
> +	int logdir = get_service_fd(LOG_DIR_FD_OFF);
> +
> +	if (!log_dir_inited)
> +		return;
> +
>  	close_safe(&logdir);
> +	log_dir_inited = false;
>  }
>  
>  void log_set_loglevel(unsigned int level)
> @@ -186,7 +197,7 @@ void print_on_level(unsigned int loglevel, const char *format, ...)
>  	} else {
>  		if (loglevel > current_loglevel)
>  			return;
> -		fd = current_logfd;
> +		fd = log_get_fd();
>  		print_ts();
>  		off = 0;
>  	}
> 




More information about the CRIU mailing list