2013-03-25 10:09:35 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2013 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2013-03-25 10:09:35 +00:00
|
|
|
* Copyright (c) 2013 Thiago de Arruda <tpadilha84@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Block or wake a client on a named wait channel.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_wait_for_exec(struct cmd *, struct cmdq_item *);
|
2013-03-25 10:09:35 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_wait_for_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "wait-for",
|
|
|
|
.alias = "wait",
|
|
|
|
|
|
|
|
.args = { "LSU", 1, 1 },
|
|
|
|
.usage = "[-L|-S|-U] channel",
|
|
|
|
|
|
|
|
.flags = 0,
|
|
|
|
.exec = cmd_wait_for_exec
|
2013-03-25 10:09:35 +00:00
|
|
|
};
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
struct wait_item {
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *item;
|
2016-10-16 17:55:14 +00:00
|
|
|
TAILQ_ENTRY(wait_item) entry;
|
|
|
|
};
|
|
|
|
|
2013-03-25 10:09:35 +00:00
|
|
|
struct wait_channel {
|
|
|
|
const char *name;
|
|
|
|
int locked;
|
2015-09-04 12:02:44 +00:00
|
|
|
int woken;
|
2013-03-25 10:09:35 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
TAILQ_HEAD(, wait_item) waiters;
|
|
|
|
TAILQ_HEAD(, wait_item) lockers;
|
2013-03-25 10:09:35 +00:00
|
|
|
|
|
|
|
RB_ENTRY(wait_channel) entry;
|
|
|
|
};
|
|
|
|
RB_HEAD(wait_channels, wait_channel);
|
2016-10-10 21:51:39 +00:00
|
|
|
static struct wait_channels wait_channels = RB_INITIALIZER(wait_channels);
|
2013-03-25 10:09:35 +00:00
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static int wait_channel_cmp(struct wait_channel *, struct wait_channel *);
|
|
|
|
RB_GENERATE_STATIC(wait_channels, wait_channel, entry, wait_channel_cmp);
|
2013-03-25 10:09:35 +00:00
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static int
|
2013-03-25 10:09:35 +00:00
|
|
|
wait_channel_cmp(struct wait_channel *wc1, struct wait_channel *wc2)
|
|
|
|
{
|
|
|
|
return (strcmp(wc1->name, wc2->name));
|
|
|
|
}
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_wait_for_signal(struct cmdq_item *, const char *,
|
2016-10-10 21:51:39 +00:00
|
|
|
struct wait_channel *);
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_wait_for_wait(struct cmdq_item *, const char *,
|
2016-10-10 21:51:39 +00:00
|
|
|
struct wait_channel *);
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_wait_for_lock(struct cmdq_item *, const char *,
|
2016-10-10 21:51:39 +00:00
|
|
|
struct wait_channel *);
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_wait_for_unlock(struct cmdq_item *, const char *,
|
2016-10-10 21:51:39 +00:00
|
|
|
struct wait_channel *);
|
2013-03-25 10:09:35 +00:00
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static struct wait_channel *cmd_wait_for_add(const char *);
|
2016-10-16 19:04:05 +00:00
|
|
|
static void cmd_wait_for_remove(struct wait_channel *);
|
2015-09-04 12:02:44 +00:00
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static struct wait_channel *
|
2015-09-04 12:02:44 +00:00
|
|
|
cmd_wait_for_add(const char *name)
|
|
|
|
{
|
|
|
|
struct wait_channel *wc;
|
|
|
|
|
|
|
|
wc = xmalloc(sizeof *wc);
|
|
|
|
wc->name = xstrdup(name);
|
|
|
|
|
|
|
|
wc->locked = 0;
|
|
|
|
wc->woken = 0;
|
|
|
|
|
|
|
|
TAILQ_INIT(&wc->waiters);
|
|
|
|
TAILQ_INIT(&wc->lockers);
|
|
|
|
|
|
|
|
RB_INSERT(wait_channels, &wait_channels, wc);
|
|
|
|
|
|
|
|
log_debug("add wait channel %s", wc->name);
|
|
|
|
|
|
|
|
return (wc);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2015-09-04 12:02:44 +00:00
|
|
|
cmd_wait_for_remove(struct wait_channel *wc)
|
|
|
|
{
|
|
|
|
if (wc->locked)
|
|
|
|
return;
|
|
|
|
if (!TAILQ_EMPTY(&wc->waiters) || !wc->woken)
|
|
|
|
return;
|
|
|
|
|
|
|
|
log_debug("remove wait channel %s", wc->name);
|
|
|
|
|
|
|
|
RB_REMOVE(wait_channels, &wait_channels, wc);
|
|
|
|
|
|
|
|
free((void *)wc->name);
|
|
|
|
free(wc);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_wait_for_exec(struct cmd *self, struct cmdq_item *item)
|
2013-03-25 10:09:35 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2013-03-25 10:09:35 +00:00
|
|
|
const char *name = args->argv[0];
|
|
|
|
struct wait_channel *wc, wc0;
|
|
|
|
|
|
|
|
wc0.name = name;
|
|
|
|
wc = RB_FIND(wait_channels, &wait_channels, &wc0);
|
|
|
|
|
|
|
|
if (args_has(args, 'S'))
|
2016-10-16 19:04:05 +00:00
|
|
|
return (cmd_wait_for_signal(item, name, wc));
|
2013-03-25 10:09:35 +00:00
|
|
|
if (args_has(args, 'L'))
|
2016-10-16 19:04:05 +00:00
|
|
|
return (cmd_wait_for_lock(item, name, wc));
|
2013-03-25 10:09:35 +00:00
|
|
|
if (args_has(args, 'U'))
|
2016-10-16 19:04:05 +00:00
|
|
|
return (cmd_wait_for_unlock(item, name, wc));
|
|
|
|
return (cmd_wait_for_wait(item, name, wc));
|
2013-03-25 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_wait_for_signal(__unused struct cmdq_item *item, const char *name,
|
2013-03-25 10:09:35 +00:00
|
|
|
struct wait_channel *wc)
|
|
|
|
{
|
2016-10-16 17:55:14 +00:00
|
|
|
struct wait_item *wi, *wi1;
|
2013-03-25 10:09:35 +00:00
|
|
|
|
2015-09-04 12:02:44 +00:00
|
|
|
if (wc == NULL)
|
|
|
|
wc = cmd_wait_for_add(name);
|
|
|
|
|
|
|
|
if (TAILQ_EMPTY(&wc->waiters) && !wc->woken) {
|
|
|
|
log_debug("signal wait channel %s, no waiters", wc->name);
|
|
|
|
wc->woken = 1;
|
|
|
|
return (CMD_RETURN_NORMAL);
|
2013-03-25 10:09:35 +00:00
|
|
|
}
|
2015-09-04 12:02:44 +00:00
|
|
|
log_debug("signal wait channel %s, with waiters", wc->name);
|
2013-03-25 10:09:35 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
TAILQ_FOREACH_SAFE(wi, &wc->waiters, entry, wi1) {
|
2019-06-18 11:08:42 +00:00
|
|
|
cmdq_continue(wi->item);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
TAILQ_REMOVE(&wc->waiters, wi, entry);
|
|
|
|
free(wi);
|
2013-03-25 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 12:02:44 +00:00
|
|
|
cmd_wait_for_remove(wc);
|
2013-03-25 10:09:35 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_wait_for_wait(struct cmdq_item *item, const char *name,
|
|
|
|
struct wait_channel *wc)
|
2013-03-25 10:09:35 +00:00
|
|
|
{
|
2020-04-13 10:59:58 +00:00
|
|
|
struct client *c = cmdq_get_client(item);
|
2016-10-16 17:55:14 +00:00
|
|
|
struct wait_item *wi;
|
2015-09-04 12:02:44 +00:00
|
|
|
|
2018-11-07 07:58:16 +00:00
|
|
|
if (c == NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "not able to wait");
|
2013-03-25 10:09:35 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
|
2015-09-04 12:02:44 +00:00
|
|
|
if (wc == NULL)
|
|
|
|
wc = cmd_wait_for_add(name);
|
|
|
|
|
|
|
|
if (wc->woken) {
|
2015-10-20 21:12:08 +00:00
|
|
|
log_debug("wait channel %s already woken (%p)", wc->name, c);
|
2015-09-04 12:02:44 +00:00
|
|
|
cmd_wait_for_remove(wc);
|
|
|
|
return (CMD_RETURN_NORMAL);
|
2013-03-25 10:09:35 +00:00
|
|
|
}
|
2015-10-20 21:12:08 +00:00
|
|
|
log_debug("wait channel %s not woken (%p)", wc->name, c);
|
2013-03-25 10:09:35 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
wi = xcalloc(1, sizeof *wi);
|
2016-10-16 19:04:05 +00:00
|
|
|
wi->item = item;
|
2016-10-16 17:55:14 +00:00
|
|
|
TAILQ_INSERT_TAIL(&wc->waiters, wi, entry);
|
2013-03-25 10:09:35 +00:00
|
|
|
|
|
|
|
return (CMD_RETURN_WAIT);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_wait_for_lock(struct cmdq_item *item, const char *name,
|
|
|
|
struct wait_channel *wc)
|
2013-03-25 10:09:35 +00:00
|
|
|
{
|
2016-10-16 17:55:14 +00:00
|
|
|
struct wait_item *wi;
|
|
|
|
|
2020-04-13 10:59:58 +00:00
|
|
|
if (cmdq_get_client(item) == NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "not able to lock");
|
2013-03-25 10:09:35 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
|
2015-09-04 12:02:44 +00:00
|
|
|
if (wc == NULL)
|
|
|
|
wc = cmd_wait_for_add(name);
|
2013-03-25 10:09:35 +00:00
|
|
|
|
|
|
|
if (wc->locked) {
|
2016-10-16 17:55:14 +00:00
|
|
|
wi = xcalloc(1, sizeof *wi);
|
2016-10-16 19:04:05 +00:00
|
|
|
wi->item = item;
|
2016-10-16 17:55:14 +00:00
|
|
|
TAILQ_INSERT_TAIL(&wc->lockers, wi, entry);
|
2013-03-25 10:09:35 +00:00
|
|
|
return (CMD_RETURN_WAIT);
|
|
|
|
}
|
|
|
|
wc->locked = 1;
|
|
|
|
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_wait_for_unlock(struct cmdq_item *item, const char *name,
|
2013-03-25 10:09:35 +00:00
|
|
|
struct wait_channel *wc)
|
|
|
|
{
|
2016-10-16 17:55:14 +00:00
|
|
|
struct wait_item *wi;
|
2013-03-25 10:09:35 +00:00
|
|
|
|
|
|
|
if (wc == NULL || !wc->locked) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "channel %s not locked", name);
|
2013-03-25 10:09:35 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
if ((wi = TAILQ_FIRST(&wc->lockers)) != NULL) {
|
2019-06-18 11:08:42 +00:00
|
|
|
cmdq_continue(wi->item);
|
2016-10-16 17:55:14 +00:00
|
|
|
TAILQ_REMOVE(&wc->lockers, wi, entry);
|
|
|
|
free(wi);
|
2013-03-25 10:09:35 +00:00
|
|
|
} else {
|
|
|
|
wc->locked = 0;
|
2015-09-04 12:02:44 +00:00
|
|
|
cmd_wait_for_remove(wc);
|
2013-03-25 10:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2014-09-01 21:58:41 +00:00
|
|
|
void
|
|
|
|
cmd_wait_for_flush(void)
|
|
|
|
{
|
|
|
|
struct wait_channel *wc, *wc1;
|
2016-10-16 17:55:14 +00:00
|
|
|
struct wait_item *wi, *wi1;
|
2014-09-01 21:58:41 +00:00
|
|
|
|
|
|
|
RB_FOREACH_SAFE(wc, wait_channels, &wait_channels, wc1) {
|
2016-10-16 17:55:14 +00:00
|
|
|
TAILQ_FOREACH_SAFE(wi, &wc->waiters, entry, wi1) {
|
2019-06-18 11:08:42 +00:00
|
|
|
cmdq_continue(wi->item);
|
2016-10-16 17:55:14 +00:00
|
|
|
TAILQ_REMOVE(&wc->waiters, wi, entry);
|
|
|
|
free(wi);
|
2014-09-01 21:58:41 +00:00
|
|
|
}
|
2015-09-13 10:45:55 +00:00
|
|
|
wc->woken = 1;
|
2016-10-16 17:55:14 +00:00
|
|
|
TAILQ_FOREACH_SAFE(wi, &wc->lockers, entry, wi1) {
|
2019-06-18 11:08:42 +00:00
|
|
|
cmdq_continue(wi->item);
|
2016-10-16 17:55:14 +00:00
|
|
|
TAILQ_REMOVE(&wc->lockers, wi, entry);
|
|
|
|
free(wi);
|
2014-09-01 21:58:41 +00:00
|
|
|
}
|
2015-09-04 12:02:44 +00:00
|
|
|
wc->locked = 0;
|
|
|
|
cmd_wait_for_remove(wc);
|
2014-09-01 21:58:41 +00:00
|
|
|
}
|
|
|
|
}
|