summaryrefslogtreecommitdiff
blob: e4cc09ea0527c4656debce5f2eef02b0df49c1b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
From 81f1e807fadb8111d71b78191e01ca688d74eac7 Mon Sep 17 00:00:00 2001
From: Andrew Cooper <andrew.cooper3@citrix.com>
Date: Thu, 25 Jul 2024 16:20:53 +0200
Subject: [PATCH 55/56] tools/libxs: Fix fcntl() invocation in set_cloexec()

set_cloexec() had a bit too much copy&pate from setnonblock(), and
insufficient testing on ancient versions of Linux...

As written (emulating ancient linux by undef'ing O_CLOEXEC), strace shows:

  open("/dev/xen/xenbus", O_RDWR)         = 3
  fcntl(3, F_GETFL)                       = 0x8002 (flags O_RDWR|O_LARGEFILE)
  fcntl(3, 0x8003 /* F_??? */, 0x7ffe4a771d90) = -1 EINVAL (Invalid argument)
  close(3)                                = 0

which is obviously nonsense.

Switch F_GETFL -> F_GETFD, and fix the second invocation to use F_SETFD.  With
this, strace is rather happer:

  open("/dev/xen/xenbus", O_RDWR)         = 3
  fcntl(3, F_GETFD)                       = 0
  fcntl(3, F_SETFD, FD_CLOEXEC)           = 0

Fixes: bf7c1464706a ("tools/libxs: Fix CLOEXEC handling in get_dev()")
Reported-by: Ross Lagerwall <ross.lagerwall@citrix.com>
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Ross Lagerwall <ross.lagerwall@citrix.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
master commit: 37810b52d003f8a04af41d7b1f85eff24af9f804
master date: 2024-07-09 15:32:18 +0100
---
 tools/libs/store/xs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c
index c8845b69e2..38a6ce3cf2 100644
--- a/tools/libs/store/xs.c
+++ b/tools/libs/store/xs.c
@@ -182,12 +182,12 @@ static bool setnonblock(int fd, int nonblock) {
 
 static bool set_cloexec(int fd)
 {
-	int flags = fcntl(fd, F_GETFL);
+	int flags = fcntl(fd, F_GETFD);
 
 	if (flags < 0)
 		return false;
 
-	return fcntl(fd, flags | FD_CLOEXEC) >= 0;
+	return fcntl(fd, F_SETFD, flags | FD_CLOEXEC) >= 0;
 }
 
 static int pipe_cloexec(int fds[2])
-- 
2.45.2