nologin: use sendfile() to submit message to user
PATCH 1/1 nologin: use sendfile() to submit message to user
A read() write() pair can be replaced with sendfile(), and it should be more efficient than suffling bytes back and forth user and kernel space.
--- login-utils/nologin.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
login-utils/nologin.c +6 -5
@@ -3,6 +3,7 @@*/ #include <stdio.h> +#include <sys/sendfile.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h>
@@ -97,12 +98,12 @@ int main(int argc, char *argv[])if (c <0 || !S_ISREG (st.st_mode)) goto dflt;else { -char buf[BUFSIZ ]; -ssize_t rd; - -while ((rd =read (fd, buf,sizeof (buf))) >0 ) -ignore_result (write (STDOUT_FILENO , buf, rd) ); +int stdout_fd; + stdout_fd =fileno (stdout); +if (stdout_fd <0 ) + goto dflt; +ignore_result (sendfile (stdout_fd, fd,NULL , st.st_size) );close (fd);return EXIT_FAILURE ; }
Signature · 1 line
2.28.0
Karel Zak
maintainerQuoted 2 lines · Sami Kerola
A
read()write()pair can be replaced withsendfile(), and it should be more efficient than suffling bytes back and forth user and kernel space.
Good idea, but not sure how portable is it to another OS. Maybe add a fallback to include/fileutils.h or mark nologin by "if LINUX" in the Makefile.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Quoted 2 lines · Sami Kerola
A
read()write()pair can be replaced withsendfile(), and it should be more efficient than suffling bytes back and forth user and kernel space.
What kinds of fds are this? If this involves things like a tty sendfile will probably stop working in Linux 5.10, as the kernel fallback is pretty horrible and not exactly more efficient. Sendfile also hasn't always been supported on all kinds of files, so you'll still always need a fallback.
Quoted 9 lines · Christoph Hellwig
Quoted 2 lines · Sami Kerola
A
read()write()pair can be replaced withsendfile(), and it should be more efficient than suffling bytes back and forth user and kernel space.What kinds of fds are this? If this involves things like a tty sendfile will probably stop working in Linux 5.10, as the kernel fallback is pretty horrible and not exactly more efficient. Sendfile also hasn't always been supported on all kinds of files, so you'll still always need a fallback.
Thank you for a review Christoph. Considering fallback to read() && write() is always required I do not think there is any advantage to call sendfile(). Karel, please consider this change falling short in simplifying or improving anything and therefore rejected.
Signature · 2 lines
Sami Kerola http://www.iki.fi/kerolasa/