[lxc-devel] [lxc/master] state: s/sleep()/nanosleep()/

brauner on Github lxc-bot at linuxcontainers.org
Wed Aug 15 22:44:16 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 524 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180815/ba7ae1c0/attachment.bin>
-------------- next part --------------
From 746559f4611aeed3a3d4ec09c692f1f3b41c5fe1 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Thu, 16 Aug 2018 00:39:50 +0200
Subject: [PATCH] state: s/sleep()/nanosleep()/

sleep() is not thread-safe but nanosleep() is. Since no resources are allocated
in lxc_wait() it is safe to call nanosleep() without cancellation handlers.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/state.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/lxc/state.c b/src/lxc/state.c
index 5afce9a6a..d8a8dab25 100644
--- a/src/lxc/state.c
+++ b/src/lxc/state.c
@@ -100,8 +100,8 @@ static int fillwaitedstates(const char *strstates, lxc_state_t *states)
 	return 0;
 }
 
-extern int lxc_wait(const char *lxcname, const char *states, int timeout,
-		    const char *lxcpath)
+int lxc_wait(const char *lxcname, const char *states, int timeout,
+	     const char *lxcpath)
 {
 	int state = -1;
 	lxc_state_t s[MAX_STATE] = {0};
@@ -110,6 +110,11 @@ extern int lxc_wait(const char *lxcname, const char *states, int timeout,
 		return -1;
 
 	for (;;) {
+		struct timespec onesec = {
+		    .tv_sec = 1,
+		    .tv_nsec = 0,
+		};
+
 		state = lxc_cmd_sock_get_state(lxcname, lxcpath, s, timeout);
 		if (state >= 0)
 			break;
@@ -125,7 +130,7 @@ extern int lxc_wait(const char *lxcname, const char *states, int timeout,
 		if (timeout == 0)
 			return -1;
 
-		sleep(1);
+		(void)nanosleep(&onesec, NULL);
 	}
 
 	if (state < 0) {


More information about the lxc-devel mailing list