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

* Re: [PATCH] nologin: use sendfile() to submit message to user
  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
  1 sibling, 0 replies; 4+ messages in thread
From: Karel Zak @ 2020-09-02  9:21 UTC (permalink / raw)
  To: Sami Kerola; +Cc: util-linux, Sami Kerola

On Tue, Sep 01, 2020 at 08:01:31PM +0100, Sami Kerola wrote:
> 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.

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

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


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

* Re: [PATCH] nologin: use sendfile() to submit message to user
  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
  1 sibling, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2020-09-02 14:17 UTC (permalink / raw)
  To: Sami Kerola; +Cc: util-linux, Sami Kerola

On Tue, Sep 01, 2020 at 08:01:31PM +0100, Sami Kerola wrote:
> 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.

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.

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

* Re: [PATCH] nologin: use sendfile() to submit message to user
  2020-09-02 14:17 ` Christoph Hellwig
@ 2020-09-02 15:26   ` Sami Kerola
  0 siblings, 0 replies; 4+ messages in thread
From: Sami Kerola @ 2020-09-02 15:26 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: util-linux, Sami Kerola

On Wed, 2 Sep 2020 at 15:17, Christoph Hellwig <hch@infradead.org> wrote:
> On Tue, Sep 01, 2020 at 08:01:31PM +0100, Sami Kerola wrote:
> > 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.
>
> 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.

-- 
Sami Kerola
http://www.iki.fi/kerolasa/

^ permalink raw reply	[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