Util-Linux package development
 help / color / mirror / Atom feed
* [PATCH] nologin: use sendfile() to submit message to user
@ 2020-09-01 19:01 Sami Kerola
  2020-09-02  9:21 ` Karel Zak
  2020-09-02 14:17 ` Christoph Hellwig
  0 siblings, 2 replies; 4+ messages in thread
From: Sami Kerola @ 2020-09-01 19:01 UTC (permalink / raw)
  To: util-linux; +Cc: Sami Kerola, Sami Kerola

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.

Signed-off-by: Sami Kerola <kerolasa@cloudflare.com>
---
 login-utils/nologin.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/login-utils/nologin.c b/login-utils/nologin.c
index f38a3aab0..ca4ca4e84 100644
--- a/login-utils/nologin.c
+++ b/login-utils/nologin.c
@@ -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;
 	}
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-09-02 15:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-01 19:01 [PATCH] nologin: use sendfile() to submit message to user Sami Kerola
2020-09-02  9:21 ` Karel Zak
2020-09-02 14:17 ` Christoph Hellwig
2020-09-02 15:26   ` Sami Kerola

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox