aboutsummaryrefslogtreecommitdiff
path: root/src/rc/runscript.c
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2008-09-27 18:50:21 +0000
committerRoy Marples <roy@marples.name>2008-09-27 18:50:21 +0000
commitbb41d2cc80927ba86fd49f98320bcce0d0d22edf (patch)
tree899bcaf7dc84e056a766dff58e631de9768a99bf /src/rc/runscript.c
parent4291b9140e2d5a63ee8d41ca612fd81a8cbe3dd8 (diff)
Use flock to lock PREFIX_LOCK so that multiple processes can cleanly write to the same tty.
Diffstat (limited to 'src/rc/runscript.c')
-rw-r--r--src/rc/runscript.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/rc/runscript.c b/src/rc/runscript.c
index 896592e9..c04f967a 100644
--- a/src/rc/runscript.c
+++ b/src/rc/runscript.c
@@ -32,6 +32,7 @@
#include <sys/select.h>
#include <sys/types.h>
#include <sys/ioctl.h>
+#include <sys/file.h>
#include <sys/param.h>
#include <sys/stat.h>
@@ -93,7 +94,6 @@ static bool in_background = false;
static RC_HOOK hook_out = 0;
static pid_t service_pid = 0;
static char *prefix = NULL;
-static bool prefix_locked = false;
static int signal_pipe[2] = { -1, -1 };
static int master_tty = -1;
@@ -315,8 +315,6 @@ static void cleanup(void)
restore_state();
if (! rc_in_plugin) {
- if (prefix_locked)
- unlink(PREFIX_LOCK);
if (hook_out) {
rc_plugin_run(hook_out, applet);
if (hook_out == RC_HOOK_SERVICE_START_DONE)
@@ -359,7 +357,16 @@ static int write_prefix(const char *buffer, size_t bytes, bool *prefixed) {
const char *ec = ecolor(ECOLOR_HILITE);
const char *ec_normal = ecolor(ECOLOR_NORMAL);
ssize_t ret = 0;
- int fd = fileno(stdout);
+ int fd = fileno(stdout), lock_fd = -1;
+
+ /* Spin until we lock the prefix */
+ for (;;) {
+ lock_fd = open(PREFIX_LOCK, O_WRONLY | O_CREAT, 0664);
+ if (lock_fd != -1)
+ if (flock(lock_fd, LOCK_EX) == 0)
+ break;
+ close(lock_fd);
+ }
for (i = 0; i < bytes; i++) {
/* We don't prefix escape codes, like eend */
@@ -379,6 +386,9 @@ static int write_prefix(const char *buffer, size_t bytes, bool *prefixed) {
ret += write(fd, buffer + i, 1);
}
+ /* Release the lock */
+ close(lock_fd);
+
return ret;
}