From bb41d2cc80927ba86fd49f98320bcce0d0d22edf Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Sat, 27 Sep 2008 18:50:21 +0000 Subject: Use flock to lock PREFIX_LOCK so that multiple processes can cleanly write to the same tty. --- src/rc/runscript.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src') 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 #include #include +#include #include #include @@ -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; } -- cgit v1.2.3