Gentoo Archives: gentoo-commits

From: Christian Ruppert <idl0r@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/openrc:master commit in: src/rc/
Date: Sat, 28 Jan 2012 15:45:44
Message-Id: 6be8a0679b8d64ea5b99ea98839eab2ce129988b.idl0r@gentoo
1 commit: 6be8a0679b8d64ea5b99ea98839eab2ce129988b
2 Author: Christian Ruppert <idl0r <AT> gentoo <DOT> org>
3 AuthorDate: Sat Jan 28 15:40:49 2012 +0000
4 Commit: Christian Ruppert <idl0r <AT> gentoo <DOT> org>
5 CommitDate: Sat Jan 28 15:43:54 2012 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/openrc.git;a=commit;h=6be8a067
7
8 Do not loop flock()
9
10 There's no need to loop until flock was successfully as flock() would simply
11 block till a previous lock has been released.
12 There's more to do to fix it properly, see my comments in the patch.
13
14 Reported-by: James Le Cuirot <chewi <AT> aura-online.co.uk>
15 X-Gentoo-Bug: 360013
16 X-Gentoo-Bug-URL: https://bugs.gentoo.org/360013
17
18 ---
19 src/rc/runscript.c | 28 +++++++++++++++++++++-------
20 1 files changed, 21 insertions(+), 7 deletions(-)
21
22 diff --git a/src/rc/runscript.c b/src/rc/runscript.c
23 index 8e0ced9..b0d8084 100644
24 --- a/src/rc/runscript.c
25 +++ b/src/rc/runscript.c
26 @@ -288,6 +288,13 @@ cleanup(void)
27 #endif
28 }
29
30 +/* Buffer and lock all output messages so that we get readable content */
31 +/* FIXME: Use a dynamic lock file that contains the tty/pts as well.
32 + * For example openrc-pts8.lock or openrc-tty1.lock.
33 + * Using a static lock file makes no sense, esp. in multi-user environments.
34 + * Why don't we use (f)printf, as it is thread-safe through POSIX already?
35 + * Bug: 360013
36 + */
37 static int
38 write_prefix(const char *buffer, size_t bytes, bool *prefixed)
39 {
40 @@ -297,14 +304,20 @@ write_prefix(const char *buffer, size_t bytes, bool *prefixed)
41 ssize_t ret = 0;
42 int fd = fileno(stdout), lock_fd = -1;
43
44 - /* Spin until we lock the prefix */
45 - for (;;) {
46 - lock_fd = open(PREFIX_LOCK, O_WRONLY | O_CREAT, 0664);
47 - if (lock_fd != -1)
48 - if (flock(lock_fd, LOCK_EX) == 0)
49 - break;
50 - close(lock_fd);
51 + /*
52 + * Lock the prefix.
53 + * open() may fail here when running as user, as RC_SVCDIR may not be writable.
54 + */
55 + lock_fd = open(PREFIX_LOCK, O_WRONLY | O_CREAT, 0664);
56 +
57 + if (lock_fd != -1) {
58 + if (flock(lock_fd, LOCK_EX) != 0)
59 + eerror("flock() failed: %s", strerror(errno));
60 }
61 +#ifdef RC_DEBUG
62 + else
63 + ewarn("Couldn't open the prefix lock, please make sure you have enough permissions");
64 +#endif
65
66 for (i = 0; i < bytes; i++) {
67 /* We don't prefix eend calls (cursor up) */
68 @@ -332,6 +345,7 @@ write_prefix(const char *buffer, size_t bytes, bool *prefixed)
69
70 /* Release the lock */
71 close(lock_fd);
72 +
73 return ret;
74 }