Use flock to lock PREFIX_LOCK so that multiple processes can cleanly write to the same tty.
This commit is contained in:
parent
4291b9140e
commit
bb41d2cc80
@ -32,6 +32,7 @@
|
|||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/file.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
@ -93,7 +94,6 @@ static bool in_background = false;
|
|||||||
static RC_HOOK hook_out = 0;
|
static RC_HOOK hook_out = 0;
|
||||||
static pid_t service_pid = 0;
|
static pid_t service_pid = 0;
|
||||||
static char *prefix = NULL;
|
static char *prefix = NULL;
|
||||||
static bool prefix_locked = false;
|
|
||||||
static int signal_pipe[2] = { -1, -1 };
|
static int signal_pipe[2] = { -1, -1 };
|
||||||
static int master_tty = -1;
|
static int master_tty = -1;
|
||||||
|
|
||||||
@ -315,8 +315,6 @@ static void cleanup(void)
|
|||||||
restore_state();
|
restore_state();
|
||||||
|
|
||||||
if (! rc_in_plugin) {
|
if (! rc_in_plugin) {
|
||||||
if (prefix_locked)
|
|
||||||
unlink(PREFIX_LOCK);
|
|
||||||
if (hook_out) {
|
if (hook_out) {
|
||||||
rc_plugin_run(hook_out, applet);
|
rc_plugin_run(hook_out, applet);
|
||||||
if (hook_out == RC_HOOK_SERVICE_START_DONE)
|
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 = ecolor(ECOLOR_HILITE);
|
||||||
const char *ec_normal = ecolor(ECOLOR_NORMAL);
|
const char *ec_normal = ecolor(ECOLOR_NORMAL);
|
||||||
ssize_t ret = 0;
|
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++) {
|
for (i = 0; i < bytes; i++) {
|
||||||
/* We don't prefix escape codes, like eend */
|
/* 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);
|
ret += write(fd, buffer + i, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Release the lock */
|
||||||
|
close(lock_fd);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user