Util-Linux package development
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: Sami Kerola <kerolasa@iki.fi>, Sami Kerola <kerolasa@cloudflare.com>
Subject: [PATCH] nologin: use sendfile() to submit message to user
Date: Tue,  1 Sep 2020 20:01:31 +0100	[thread overview]
Message-ID: <20200901190131.4153-1-kerolasa@iki.fi> (raw)

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


             reply	other threads:[~2020-09-01 19:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-01 19:01 Sami Kerola [this message]
2020-09-02  9:21 ` [PATCH] nologin: use sendfile() to submit message to user Karel Zak
2020-09-02 14:17 ` Christoph Hellwig
2020-09-02 15:26   ` Sami Kerola

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200901190131.4153-1-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=kerolasa@cloudflare.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox