Re: [dev] [PATCH 1/1] remove useless dup()

From: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
Date: Wed, 8 Jul 2015 12:00:35 +0200

> We are ignoring return value of dup(), so just remove it.


From dup(3):

       The dup() system call creates a copy of the file descriptor oldfd,
       using the lowest-numbered unused descriptor for the new descriptor.


> if((cmdfd = open(opt_line, O_RDWR)) < 0)
> die("open line failed: %s\n", strerror(errno));
> close(STDIN_FILENO);
> - dup(cmdfd);
> stty();

So, it means we are assigning cmdfd to stdin. If you see the code in stty()
you will see:

void
stty(void)
{
        char cmd[_POSIX_ARG_MAX], **p, *q, *s;
        size_t n, siz;

        if((n = strlen(stty_args)) > sizeof(cmd)-1)
                die("incorrect stty parameters\n");
        memcpy(cmd, stty_args, n);
        q = cmd + n;
        siz = sizeof(cmd) - n;
        for(p = opt_cmd; p && (s = *p); ++p) {
                if((n = strlen(s)) > siz-1)
                        die("stty parameter length too long\n");
                *q++ = ' ';
                q = memcpy(q, s, n);
                q += n;
                siz-= n + 1;
        }
        *q = '\0';
        if (system(cmd) != 0)
            perror("Couldn't call stty");
}

and then 'system(cmd)' takes cmdfd as stdin. If you remove
tha call to dup(), then stty command will not have the serial line
as stdin, and then it will not have the serial line as controlling
terminal, and the stty execution will affect the terminal where
the user launched st.

In fact, the only thing that can be done here is:

                  close(STDIN_FILENO);
                 dup(cmdfd);
+ close(cmdfd);
                  stty();

because we are not going to use this file descriptor anymore,
but I think we will not have a significant improvement.


Regards,
Received on Wed Jul 08 2015 - 12:00:35 CEST

This archive was generated by hypermail 2.3.0 : Wed Jul 08 2015 - 12:12:08 CEST