[PATCH v2 weston] log: Open log file CLOEXEC so child processes don't get the fd

Bryce Harrington bryce at osg.samsung.com
Thu Jul 16 19:28:53 PDT 2015


On Mon, Jun 08, 2015 at 11:37:31AM -0500, Derek Foreman wrote:
> Reviewed-by: Pekka Paalanen <pekka.paalanen at collabora.co.uk>
> Signed-off-by: Derek Foreman <derekf at osg.samsung.com>

Thanks, pushed:
To ssh://git.freedesktop.org/git/wayland/weston
   4a8a3a1..6bc33d6  master -> master


> ---
> 
> Nested the os_fd_set_cloexec() call so calling weston_log_file_open() multiple
> times behaves as expected.
> 
>  shared/os-compatibility.c | 22 ++++++++++++++--------
>  shared/os-compatibility.h |  3 +++
>  src/log.c                 |  7 ++++++-
>  3 files changed, 23 insertions(+), 9 deletions(-)
> 
> diff --git a/shared/os-compatibility.c b/shared/os-compatibility.c
> index 611e7c8..d0ff1c8 100644
> --- a/shared/os-compatibility.c
> +++ b/shared/os-compatibility.c
> @@ -33,8 +33,8 @@
>  
>  #include "os-compatibility.h"
>  
> -static int
> -set_cloexec_or_close(int fd)
> +int
> +os_fd_set_cloexec(int fd)
>  {
>  	long flags;
>  
> @@ -43,16 +43,22 @@ set_cloexec_or_close(int fd)
>  
>  	flags = fcntl(fd, F_GETFD);
>  	if (flags == -1)
> -		goto err;
> +		return -1;
>  
>  	if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
> -		goto err;
> +		return -1;
>  
> -	return fd;
> +	return 0;
> +}
>  
> -err:
> -	close(fd);
> -	return -1;
> +static int
> +set_cloexec_or_close(int fd)
> +{
> +	if (os_fd_set_cloexec(fd) != 0) {
> +		close(fd);
> +		return -1;
> +	}
> +	return fd;
>  }
>  
>  int
> diff --git a/shared/os-compatibility.h b/shared/os-compatibility.h
> index 172bb7e..60ae7fd 100644
> --- a/shared/os-compatibility.h
> +++ b/shared/os-compatibility.h
> @@ -38,6 +38,9 @@ backtrace(void **buffer, int size)
>  #endif
>  
>  int
> +os_fd_set_cloexec(int fd);
> +
> +int
>  os_socketpair_cloexec(int domain, int type, int protocol, int *sv);
>  
>  int
> diff --git a/src/log.c b/src/log.c
> index 99bbe18..0dae3b2 100644
> --- a/src/log.c
> +++ b/src/log.c
> @@ -33,6 +33,8 @@
>  
>  #include "compositor.h"
>  
> +#include "os-compatibility.h"
> +
>  static FILE *weston_logfile = NULL;
>  
>  static int cached_tm_mday = -1;
> @@ -74,8 +76,11 @@ weston_log_file_open(const char *filename)
>  {
>  	wl_log_set_handler_server(custom_handler);
>  
> -	if (filename != NULL)
> +	if (filename != NULL) {
>  		weston_logfile = fopen(filename, "a");
> +		if (weston_logfile)
> +			os_fd_set_cloexec(fileno(weston_logfile));
> +	}
>  
>  	if (weston_logfile == NULL)
>  		weston_logfile = stderr;
> -- 
> 2.1.4
> 
> _______________________________________________
> wayland-devel mailing list
> wayland-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/wayland-devel


More information about the wayland-devel mailing list