Hi,

> These routines will be good to have. I think your names are fine, but
> thought I'd point that out that there seems to be some sort of
> convention in other projects I've seen to name them safe_read(),
> safe_write(). They are usually accompanied by full_read() and
> full_write() which handle short return counts as well.

I've seen _nointr as well, but I don't really care.

Regarding full variants: lxc-attach only transfers miniscule amounts of
data (pid_t and int are at most 8 bytes long on any architecture I know,
usually only 4) in one call, so that should never be split up by the
kernel, especially if the fds are blocking. But I have no objection to
adding additional variants for transfering more data.

> Also, it might be good to have their return type be ssize_t to match
> actual read(3) and write(3). Thanks!

I've attached an updated version of this specific patch with the fixed
return values.

(The other patches should still apply regardless.)

-- Christian
>From 20c3cc93835e6148a61686cd32ceb09f39eb1bfc Mon Sep 17 00:00:00 2001
From: Christian Seiler <christ...@iwakd.de>
Date: Wed, 8 May 2013 14:37:15 +0200
Subject: [PATCH 4/5 v2] Implement simple utility functions for reading and
 writing to fds

Signed-off-by: Christian Seiler <christ...@iwakd.de>
---
 src/lxc/utils.c |   35 +++++++++++++++++++++++++++++++++++
 src/lxc/utils.h |    5 +++++
 2 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index 66bd19d..05475c0 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -281,3 +281,38 @@ again:
                goto again;
        return status;
 }
+
+ssize_t lxc_write_nointr(int fd, const void* buf, size_t count)
+{
+       ssize_t ret;
+again:
+       ret = write(fd, buf, count);
+       if (ret < 0 && errno == EINTR)
+               goto again;
+       return ret;
+}
+
+ssize_t lxc_read_nointr(int fd, void* buf, size_t count)
+{
+       ssize_t ret;
+again:
+       ret = read(fd, buf, count);
+       if (ret < 0 && errno == EINTR)
+               goto again;
+       return ret;
+}
+
+ssize_t lxc_read_nointr_expect(int fd, void* buf, size_t count, const void* 
expected_buf)
+{
+       ssize_t ret;
+       ret = lxc_read_nointr(fd, buf, count);
+       if (ret <= 0)
+               return ret;
+       if ((size_t)ret != count)
+               return -1;
+       if (expected_buf && memcmp(buf, expected_buf, count) != 0) {
+               errno = EINVAL;
+               return -1;
+       }
+       return ret;
+}
diff --git a/src/lxc/utils.h b/src/lxc/utils.h
index be1a8a8..126f726 100644
--- a/src/lxc/utils.h
+++ b/src/lxc/utils.h
@@ -68,4 +68,9 @@ extern int __build_bug_on_failed;
 extern int wait_for_pid(pid_t pid);
 extern int lxc_wait_for_pid_status(pid_t pid);
 
+/* send and receive buffers completely */
+extern ssize_t lxc_write_nointr(int fd, const void* buf, size_t count);
+extern ssize_t lxc_read_nointr(int fd, void* buf, size_t count);
+extern ssize_t lxc_read_nointr_expect(int fd, void* buf, size_t count, const 
void* expected_buf);
+
 #endif
-- 
1.7.8.6

------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Lxc-devel mailing list
Lxc-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/lxc-devel

Reply via email to