[PATCH 2/2] ash: Use F_DUPFD_CLOEXEC where ever we can

Denys Vlasenko vda.linux at googlemail.com
Fri Mar 30 20:19:24 UTC 2018


Applied, thanks!

On Mon, Mar 19, 2018 at 10:31 AM,  <mark.marshall at omicronenergy.com> wrote:
> From: Mark Marshall <mark.marshall at omicronenergy.com>
>
> This should result in smaller code, and is also the preferred way to set
> the CLOEXEC bit (The bit should ideally always be set when the file
> handle is created, not afterwards).
>
> Signed-off-by: Mark Marshall <mark.marshall at omicronenergy.com>
> ---
>  shell/ash.c | 28 ++++++++++++++++++++++++++--
>  1 file changed, 26 insertions(+), 2 deletions(-)
>
> diff --git a/shell/ash.c b/shell/ash.c
> index 6cf980c..0b9e71f 100644
> --- a/shell/ash.c
> +++ b/shell/ash.c
> @@ -3969,12 +3969,18 @@ setjobctl(int on)
>                                         goto out;
>                 }
>                 /* fd is a tty at this point */
> +#if defined(F_DUPFD_CLOEXEC)
> +               fd = fcntl(fd, F_DUPFD_CLOEXEC, 10);
> +#else
>                 fd = fcntl(fd, F_DUPFD, 10);
> +#endif
>                 if (ofd >= 0) /* if it is "/dev/tty", close. If 0/1/2, don't */
>                         close(ofd);
>                 if (fd < 0)
>                         goto out; /* F_DUPFD failed */
> +#if !defined(F_DUPFD_CLOEXEC)
>                 close_on_exec_on(fd);
> +#endif
>                 while (1) { /* while we are in the background */
>                         pgrp = tcgetpgrp(fd);
>                         if (pgrp < 0) {
> @@ -5424,13 +5430,19 @@ savefd(int from)
>         int newfd;
>         int err;
>
> +#if defined(F_DUPFD_CLOEXEC)
> +       newfd = fcntl(from, F_DUPFD_CLOEXEC, 10);
> +#else
>         newfd = fcntl(from, F_DUPFD, 10);
> +#endif
>         err = newfd < 0 ? errno : 0;
>         if (err != EBADF) {
>                 if (err)
>                         ash_msg_and_raise_perror("%d", from);
>                 close(from);
> -               fcntl(newfd, F_SETFD, FD_CLOEXEC);
> +#if !defined(F_DUPFD_CLOEXEC)
> +               close_on_exec_on(newfd);
> +#endif
>         }
>
>         return newfd;
> @@ -5473,7 +5485,11 @@ xdup_CLOEXEC_and_close(int fd, int avoid_fd)
>  {
>         int newfd;
>   repeat:
> +#if defined(F_DUPFD_CLOEXEC)
> +       newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
> +#else
>         newfd = fcntl(fd, F_DUPFD, avoid_fd + 1);
> +#endif
>         if (newfd < 0) {
>                 if (errno == EBUSY)
>                         goto repeat;
> @@ -5484,7 +5500,9 @@ xdup_CLOEXEC_and_close(int fd, int avoid_fd)
>                         return fd;
>                 ash_msg_and_raise_perror("%d", newfd);
>         }
> -       fcntl(newfd, F_SETFD, FD_CLOEXEC);
> +#if !defined(F_DUPFD_CLOEXEC)
> +       close_on_exec_on(newfd);
> +#endif
>         close(fd);
>         return newfd;
>  }
> @@ -10754,7 +10772,11 @@ setinputfile(const char *fname, int flags)
>         int fd;
>
>         INT_OFF;
> +#if defined(O_CLOEXEC)
> +       fd = open(fname, O_RDONLY | O_CLOEXEC);
> +#else
>         fd = open(fname, O_RDONLY);
> +#endif
>         if (fd < 0) {
>                 if (flags & INPUT_NOFILE_OK)
>                         goto out;
> @@ -10763,8 +10785,10 @@ setinputfile(const char *fname, int flags)
>         }
>         if (fd < 10)
>                 fd = savefd(fd);
> +#if !defined(O_CLOEXEC)
>         else
>                 close_on_exec_on(fd);
> +#endif
>         setinputfd(fd, flags & INPUT_PUSH_FILE);
>   out:
>         INT_ON;
> --
> 2.7.4
>
> _______________________________________________
> busybox mailing list
> busybox at busybox.net
> http://lists.busybox.net/mailman/listinfo/busybox


More information about the busybox mailing list