Coverage Report

Created: 2026-03-12 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/control.c
Line
Count
Source
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2012 Nicholas Marriott <nicholas.marriott@gmail.com>
5
 * Copyright (c) 2012 George Nachman <tmux@georgester.com>
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
#include <sys/types.h>
21
22
#include <stdlib.h>
23
#include <string.h>
24
#include <time.h>
25
#include <unistd.h>
26
27
#include "tmux.h"
28
29
/*
30
 * Block of data to output. Each client has one "all" queue of blocks and
31
 * another queue for each pane (in struct client_offset). %output blocks are
32
 * added to both queues and other output lines (notifications) added only to
33
 * the client queue.
34
 *
35
 * When a client becomes writeable, data from blocks on the pane queue are sent
36
 * up to the maximum size (CLIENT_BUFFER_HIGH). If a block is entirely written,
37
 * it is removed from both pane and client queues and if this means non-%output
38
 * blocks are now at the head of the client queue, they are written.
39
 *
40
 * This means a %output block holds up any subsequent non-%output blocks until
41
 * it is written which enforces ordering even if the client cannot accept the
42
 * entire block in one go.
43
 */
44
struct control_block {
45
  size_t         size;
46
  char        *line;
47
  uint64_t       t;
48
49
  TAILQ_ENTRY(control_block)   entry;
50
  TAILQ_ENTRY(control_block)   all_entry;
51
};
52
53
/* Control client pane. */
54
struct control_pane {
55
  u_int        pane;
56
57
  /*
58
   * Offsets into the pane data. The first (offset) is the data we have
59
   * written; the second (queued) the data we have queued (pointed to by
60
   * a block).
61
   */
62
  struct window_pane_offset  offset;
63
  struct window_pane_offset  queued;
64
65
  int        flags;
66
0
#define CONTROL_PANE_OFF 0x1
67
0
#define CONTROL_PANE_PAUSED 0x2
68
69
  int        pending_flag;
70
  TAILQ_ENTRY(control_pane)  pending_entry;
71
72
  TAILQ_HEAD(, control_block)  blocks;
73
74
  RB_ENTRY(control_pane)     entry;
75
};
76
RB_HEAD(control_panes, control_pane);
77
78
/* Subscription pane. */
79
struct control_sub_pane {
80
  u_int        pane;
81
  u_int        idx;
82
  char        *last;
83
84
  RB_ENTRY(control_sub_pane)   entry;
85
};
86
RB_HEAD(control_sub_panes, control_sub_pane);
87
88
/* Subscription window. */
89
struct control_sub_window {
90
  u_int        window;
91
  u_int        idx;
92
  char        *last;
93
94
  RB_ENTRY(control_sub_window)   entry;
95
};
96
RB_HEAD(control_sub_windows, control_sub_window);
97
98
/* Control client subscription. */
99
struct control_sub {
100
  char        *name;
101
  char        *format;
102
103
  enum control_sub_type    type;
104
  u_int        id;
105
106
  char        *last;
107
  struct control_sub_panes   panes;
108
  struct control_sub_windows   windows;
109
110
  RB_ENTRY(control_sub)    entry;
111
};
112
RB_HEAD(control_subs, control_sub);
113
114
/* Control client state. */
115
struct control_state {
116
  struct control_panes     panes;
117
118
  TAILQ_HEAD(, control_pane)   pending_list;
119
  u_int        pending_count;
120
121
  TAILQ_HEAD(, control_block)  all_blocks;
122
123
  struct bufferevent    *read_event;
124
  struct bufferevent    *write_event;
125
126
  struct control_subs    subs;
127
  struct event       subs_timer;
128
};
129
130
/* Low and high watermarks. */
131
0
#define CONTROL_BUFFER_LOW 512
132
0
#define CONTROL_BUFFER_HIGH 8192
133
134
/* Minimum to write to each client. */
135
0
#define CONTROL_WRITE_MINIMUM 32
136
137
/* Maximum age for clients that are not using pause mode. */
138
0
#define CONTROL_MAXIMUM_AGE 300000
139
140
/* Flags to ignore client. */
141
#define CONTROL_IGNORE_FLAGS \
142
0
  (CLIENT_CONTROL_NOOUTPUT| \
143
0
   CLIENT_UNATTACHEDFLAGS)
144
145
/* Compare client panes. */
146
static int
147
control_pane_cmp(struct control_pane *cp1, struct control_pane *cp2)
148
0
{
149
0
  if (cp1->pane < cp2->pane)
150
0
    return (-1);
151
0
  if (cp1->pane > cp2->pane)
152
0
    return (1);
153
0
  return (0);
154
0
}
155
0
RB_GENERATE_STATIC(control_panes, control_pane, entry, control_pane_cmp);
Unexecuted instantiation: control.c:control_panes_RB_MINMAX
Unexecuted instantiation: control.c:control_panes_RB_REMOVE
Unexecuted instantiation: control.c:control_panes_RB_REMOVE_COLOR
Unexecuted instantiation: control.c:control_panes_RB_FIND
Unexecuted instantiation: control.c:control_panes_RB_INSERT
156
0
157
0
/* Compare client subs. */
158
0
static int
159
0
control_sub_cmp(struct control_sub *csub1, struct control_sub *csub2)
160
0
{
161
0
  return (strcmp(csub1->name, csub2->name));
162
0
}
163
0
RB_GENERATE_STATIC(control_subs, control_sub, entry, control_sub_cmp);
Unexecuted instantiation: control.c:control_subs_RB_MINMAX
Unexecuted instantiation: control.c:control_subs_RB_REMOVE
Unexecuted instantiation: control.c:control_subs_RB_REMOVE_COLOR
Unexecuted instantiation: control.c:control_subs_RB_FIND
Unexecuted instantiation: control.c:control_subs_RB_INSERT
164
0
165
0
/* Compare client subscription panes. */
166
0
static int
167
0
control_sub_pane_cmp(struct control_sub_pane *csp1,
168
0
    struct control_sub_pane *csp2)
169
0
{
170
0
  if (csp1->pane < csp2->pane)
171
0
    return (-1);
172
0
  if (csp1->pane > csp2->pane)
173
0
    return (1);
174
0
  if (csp1->idx < csp2->idx)
175
0
    return (-1);
176
0
  if (csp1->idx > csp2->idx)
177
0
    return (1);
178
0
  return (0);
179
0
}
180
0
RB_GENERATE_STATIC(control_sub_panes, control_sub_pane, entry,
Unexecuted instantiation: control.c:control_sub_panes_RB_MINMAX
Unexecuted instantiation: control.c:control_sub_panes_RB_REMOVE
Unexecuted instantiation: control.c:control_sub_panes_RB_REMOVE_COLOR
Unexecuted instantiation: control.c:control_sub_panes_RB_FIND
Unexecuted instantiation: control.c:control_sub_panes_RB_INSERT
181
0
    control_sub_pane_cmp);
182
0
183
0
/* Compare client subscription windows. */
184
0
static int
185
0
control_sub_window_cmp(struct control_sub_window *csw1,
186
0
    struct control_sub_window *csw2)
187
0
{
188
0
  if (csw1->window < csw2->window)
189
0
    return (-1);
190
0
  if (csw1->window > csw2->window)
191
0
    return (1);
192
0
  if (csw1->idx < csw2->idx)
193
0
    return (-1);
194
0
  if (csw1->idx > csw2->idx)
195
0
    return (1);
196
0
  return (0);
197
0
}
198
0
RB_GENERATE_STATIC(control_sub_windows, control_sub_window, entry,
Unexecuted instantiation: control.c:control_sub_windows_RB_MINMAX
Unexecuted instantiation: control.c:control_sub_windows_RB_REMOVE
Unexecuted instantiation: control.c:control_sub_windows_RB_REMOVE_COLOR
Unexecuted instantiation: control.c:control_sub_windows_RB_FIND
Unexecuted instantiation: control.c:control_sub_windows_RB_INSERT
199
0
    control_sub_window_cmp);
200
0
201
0
/* Free a subscription. */
202
0
static void
203
0
control_free_sub(struct control_state *cs, struct control_sub *csub)
204
0
{
205
0
  struct control_sub_pane   *csp, *csp1;
206
0
  struct control_sub_window *csw, *csw1;
207
208
0
  RB_FOREACH_SAFE(csp, control_sub_panes, &csub->panes, csp1) {
209
0
    RB_REMOVE(control_sub_panes, &csub->panes, csp);
210
0
    free(csp);
211
0
  }
212
0
  RB_FOREACH_SAFE(csw, control_sub_windows, &csub->windows, csw1) {
213
0
    RB_REMOVE(control_sub_windows, &csub->windows, csw);
214
0
    free(csw);
215
0
  }
216
0
  free(csub->last);
217
218
0
  RB_REMOVE(control_subs, &cs->subs, csub);
219
0
  free(csub->name);
220
0
  free(csub->format);
221
0
  free(csub);
222
0
}
223
224
/* Free a block. */
225
static void
226
control_free_block(struct control_state *cs, struct control_block *cb)
227
0
{
228
0
  free(cb->line);
229
0
  TAILQ_REMOVE(&cs->all_blocks, cb, all_entry);
230
0
  free(cb);
231
0
}
232
233
/* Get pane offsets for this client. */
234
static struct control_pane *
235
control_get_pane(struct client *c, struct window_pane *wp)
236
0
{
237
0
  struct control_state  *cs = c->control_state;
238
0
  struct control_pane  cp = { .pane = wp->id };
239
240
0
  return (RB_FIND(control_panes, &cs->panes, &cp));
241
0
}
242
243
/* Add pane offsets for this client. */
244
static struct control_pane *
245
control_add_pane(struct client *c, struct window_pane *wp)
246
0
{
247
0
  struct control_state  *cs = c->control_state;
248
0
  struct control_pane *cp;
249
250
0
  cp = control_get_pane(c, wp);
251
0
  if (cp != NULL)
252
0
    return (cp);
253
254
0
  cp = xcalloc(1, sizeof *cp);
255
0
  cp->pane = wp->id;
256
0
  RB_INSERT(control_panes, &cs->panes, cp);
257
258
0
  memcpy(&cp->offset, &wp->offset, sizeof cp->offset);
259
0
  memcpy(&cp->queued, &wp->offset, sizeof cp->queued);
260
0
  TAILQ_INIT(&cp->blocks);
261
262
0
  return (cp);
263
0
}
264
265
/* Discard output for a pane. */
266
static void
267
control_discard_pane(struct client *c, struct control_pane *cp)
268
0
{
269
0
  struct control_state  *cs = c->control_state;
270
0
  struct control_block  *cb, *cb1;
271
272
0
  TAILQ_FOREACH_SAFE(cb, &cp->blocks, entry, cb1) {
273
0
    TAILQ_REMOVE(&cp->blocks, cb, entry);
274
0
    control_free_block(cs, cb);
275
0
  }
276
0
}
277
278
/* Get actual pane for this client. */
279
static struct window_pane *
280
control_window_pane(struct client *c, u_int pane)
281
0
{
282
0
  struct window_pane  *wp;
283
284
0
  if (c->session == NULL)
285
0
    return (NULL);
286
0
  if ((wp = window_pane_find_by_id(pane)) == NULL)
287
0
    return (NULL);
288
0
  if (winlink_find_by_window(&c->session->windows, wp->window) == NULL)
289
0
    return (NULL);
290
0
  return (wp);
291
0
}
292
293
/* Reset control offsets. */
294
void
295
control_reset_offsets(struct client *c)
296
0
{
297
0
  struct control_state  *cs = c->control_state;
298
0
  struct control_pane *cp, *cp1;
299
300
0
  RB_FOREACH_SAFE(cp, control_panes, &cs->panes, cp1) {
301
0
    RB_REMOVE(control_panes, &cs->panes, cp);
302
0
    free(cp);
303
0
  }
304
305
0
  TAILQ_INIT(&cs->pending_list);
306
0
  cs->pending_count = 0;
307
0
}
308
309
/* Get offsets for client. */
310
struct window_pane_offset *
311
control_pane_offset(struct client *c, struct window_pane *wp, int *off)
312
0
{
313
0
  struct control_state  *cs = c->control_state;
314
0
  struct control_pane *cp;
315
316
0
  if (c->flags & CLIENT_CONTROL_NOOUTPUT) {
317
0
    *off = 0;
318
0
    return (NULL);
319
0
  }
320
321
0
  cp = control_get_pane(c, wp);
322
0
  if (cp == NULL || (cp->flags & CONTROL_PANE_PAUSED)) {
323
0
    *off = 0;
324
0
    return (NULL);
325
0
  }
326
0
  if (cp->flags & CONTROL_PANE_OFF) {
327
0
    *off = 1;
328
0
    return (NULL);
329
0
  }
330
0
  *off = (EVBUFFER_LENGTH(cs->write_event->output) >= CONTROL_BUFFER_LOW);
331
0
  return (&cp->offset);
332
0
}
333
334
/* Set pane as on. */
335
void
336
control_set_pane_on(struct client *c, struct window_pane *wp)
337
0
{
338
0
  struct control_pane *cp;
339
340
0
  cp = control_get_pane(c, wp);
341
0
  if (cp != NULL && (cp->flags & CONTROL_PANE_OFF)) {
342
0
    cp->flags &= ~CONTROL_PANE_OFF;
343
0
    memcpy(&cp->offset, &wp->offset, sizeof cp->offset);
344
0
    memcpy(&cp->queued, &wp->offset, sizeof cp->queued);
345
0
  }
346
0
}
347
348
/* Set pane as off. */
349
void
350
control_set_pane_off(struct client *c, struct window_pane *wp)
351
0
{
352
0
  struct control_pane *cp;
353
354
0
  cp = control_add_pane(c, wp);
355
0
  cp->flags |= CONTROL_PANE_OFF;
356
0
}
357
358
/* Continue a paused pane. */
359
void
360
control_continue_pane(struct client *c, struct window_pane *wp)
361
0
{
362
0
  struct control_pane *cp;
363
364
0
  cp = control_get_pane(c, wp);
365
0
  if (cp != NULL && (cp->flags & CONTROL_PANE_PAUSED)) {
366
0
    cp->flags &= ~CONTROL_PANE_PAUSED;
367
0
    memcpy(&cp->offset, &wp->offset, sizeof cp->offset);
368
0
    memcpy(&cp->queued, &wp->offset, sizeof cp->queued);
369
0
    control_write(c, "%%continue %%%u", wp->id);
370
0
  }
371
0
}
372
373
/* Pause a pane. */
374
void
375
control_pause_pane(struct client *c, struct window_pane *wp)
376
0
{
377
0
  struct control_pane *cp;
378
379
0
  cp = control_add_pane(c, wp);
380
0
  if (~cp->flags & CONTROL_PANE_PAUSED) {
381
0
    cp->flags |= CONTROL_PANE_PAUSED;
382
0
    control_discard_pane(c, cp);
383
0
    control_write(c, "%%pause %%%u", wp->id);
384
0
  }
385
0
}
386
387
/* Write a line. */
388
static void printflike(2, 0)
389
control_vwrite(struct client *c, const char *fmt, va_list ap)
390
0
{
391
0
  struct control_state  *cs = c->control_state;
392
0
  char      *s;
393
394
0
  xvasprintf(&s, fmt, ap);
395
0
  log_debug("%s: %s: writing line: %s", __func__, c->name, s);
396
397
0
  bufferevent_write(cs->write_event, s, strlen(s));
398
0
  bufferevent_write(cs->write_event, "\n", 1);
399
400
0
  bufferevent_enable(cs->write_event, EV_WRITE);
401
0
  free(s);
402
0
}
403
404
/* Write a line. */
405
void
406
control_write(struct client *c, const char *fmt, ...)
407
0
{
408
0
  struct control_state  *cs = c->control_state;
409
0
  struct control_block  *cb;
410
0
  va_list      ap;
411
412
0
  va_start(ap, fmt);
413
414
0
  if (TAILQ_EMPTY(&cs->all_blocks)) {
415
0
    control_vwrite(c, fmt, ap);
416
0
    va_end(ap);
417
0
    return;
418
0
  }
419
420
0
  cb = xcalloc(1, sizeof *cb);
421
0
  xvasprintf(&cb->line, fmt, ap);
422
0
  TAILQ_INSERT_TAIL(&cs->all_blocks, cb, all_entry);
423
0
  cb->t = get_timer();
424
425
0
  log_debug("%s: %s: storing line: %s", __func__, c->name, cb->line);
426
0
  bufferevent_enable(cs->write_event, EV_WRITE);
427
428
0
  va_end(ap);
429
0
}
430
431
/* Check age for this pane. */
432
static int
433
control_check_age(struct client *c, struct window_pane *wp,
434
    struct control_pane *cp)
435
0
{
436
0
  struct control_block  *cb;
437
0
  uint64_t     t, age;
438
439
0
  cb = TAILQ_FIRST(&cp->blocks);
440
0
  if (cb == NULL)
441
0
    return (0);
442
0
  t = get_timer();
443
0
  if (cb->t >= t)
444
0
    return (0);
445
446
0
  age = t - cb->t;
447
0
  log_debug("%s: %s: %%%u is %llu behind", __func__, c->name, wp->id,
448
0
      (unsigned long long)age);
449
450
0
  if (c->flags & CLIENT_CONTROL_PAUSEAFTER) {
451
0
    if (age < c->pause_age)
452
0
      return (0);
453
0
    cp->flags |= CONTROL_PANE_PAUSED;
454
0
    control_discard_pane(c, cp);
455
0
    control_write(c, "%%pause %%%u", wp->id);
456
0
  } else {
457
0
    if (age < CONTROL_MAXIMUM_AGE)
458
0
      return (0);
459
0
    c->exit_message = xstrdup("too far behind");
460
0
    c->flags |= CLIENT_EXIT;
461
0
    control_discard(c);
462
0
  }
463
0
  return (1);
464
0
}
465
466
/* Write output from a pane. */
467
void
468
control_write_output(struct client *c, struct window_pane *wp)
469
0
{
470
0
  struct control_state  *cs = c->control_state;
471
0
  struct control_pane *cp;
472
0
  struct control_block  *cb;
473
0
  size_t       new_size;
474
475
0
  if (winlink_find_by_window(&c->session->windows, wp->window) == NULL)
476
0
    return;
477
478
0
  if (c->flags & CONTROL_IGNORE_FLAGS) {
479
0
    cp = control_get_pane(c, wp);
480
0
    if (cp != NULL)
481
0
      goto ignore;
482
0
    return;
483
0
  }
484
0
  cp = control_add_pane(c, wp);
485
0
  if (cp->flags & (CONTROL_PANE_OFF|CONTROL_PANE_PAUSED))
486
0
    goto ignore;
487
0
  if (control_check_age(c, wp, cp))
488
0
    return;
489
490
0
  window_pane_get_new_data(wp, &cp->queued, &new_size);
491
0
  if (new_size == 0)
492
0
    return;
493
0
  window_pane_update_used_data(wp, &cp->queued, new_size);
494
495
0
  cb = xcalloc(1, sizeof *cb);
496
0
  cb->size = new_size;
497
0
  TAILQ_INSERT_TAIL(&cs->all_blocks, cb, all_entry);
498
0
  cb->t = get_timer();
499
500
0
  TAILQ_INSERT_TAIL(&cp->blocks, cb, entry);
501
0
  log_debug("%s: %s: new output block of %zu for %%%u", __func__, c->name,
502
0
      cb->size, wp->id);
503
504
0
  if (!cp->pending_flag) {
505
0
    log_debug("%s: %s: %%%u now pending", __func__, c->name,
506
0
        wp->id);
507
0
    TAILQ_INSERT_TAIL(&cs->pending_list, cp, pending_entry);
508
0
    cp->pending_flag = 1;
509
0
    cs->pending_count++;
510
0
  }
511
0
  bufferevent_enable(cs->write_event, EV_WRITE);
512
0
  return;
513
514
0
ignore:
515
0
  log_debug("%s: %s: ignoring pane %%%u", __func__, c->name, wp->id);
516
0
  window_pane_update_used_data(wp, &cp->offset, SIZE_MAX);
517
0
  window_pane_update_used_data(wp, &cp->queued, SIZE_MAX);
518
0
}
519
520
/* Control client error callback. */
521
static enum cmd_retval
522
control_error(struct cmdq_item *item, void *data)
523
0
{
524
0
  struct client *c = cmdq_get_client(item);
525
0
  char    *error = data;
526
527
0
  cmdq_guard(item, "begin", 1);
528
0
  control_write(c, "parse error: %s", error);
529
0
  cmdq_guard(item, "error", 1);
530
531
0
  free(error);
532
0
  return (CMD_RETURN_NORMAL);
533
0
}
534
535
/* Control client error callback. */
536
static void
537
control_error_callback(__unused struct bufferevent *bufev,
538
    __unused short what, void *data)
539
0
{
540
0
  struct client *c = data;
541
542
0
  c->flags |= CLIENT_EXIT;
543
0
}
544
545
/* Control client input callback. Read lines and fire commands. */
546
static void
547
control_read_callback(__unused struct bufferevent *bufev, void *data)
548
0
{
549
0
  struct client   *c = data;
550
0
  struct control_state  *cs = c->control_state;
551
0
  struct evbuffer   *buffer = cs->read_event->input;
552
0
  char      *line, *error;
553
0
  struct cmdq_state *state;
554
0
  enum cmd_parse_status  status;
555
556
0
  for (;;) {
557
0
    line = evbuffer_readln(buffer, NULL, EVBUFFER_EOL_LF);
558
0
    if (line == NULL)
559
0
      break;
560
0
    log_debug("%s: %s: %s", __func__, c->name, line);
561
0
    if (*line == '\0') { /* empty line detach */
562
0
      free(line);
563
0
      c->flags |= CLIENT_EXIT;
564
0
      break;
565
0
    }
566
567
0
    state = cmdq_new_state(NULL, NULL, CMDQ_STATE_CONTROL);
568
0
    status = cmd_parse_and_append(line, NULL, c, state, &error);
569
0
    if (status == CMD_PARSE_ERROR)
570
0
      cmdq_append(c, cmdq_get_callback(control_error, error));
571
0
    cmdq_free_state(state);
572
573
0
    free(line);
574
0
  }
575
0
}
576
577
/* Does this control client have outstanding data to write? */
578
int
579
control_all_done(struct client *c)
580
0
{
581
0
  struct control_state  *cs = c->control_state;
582
583
0
  if (!TAILQ_EMPTY(&cs->all_blocks))
584
0
    return (0);
585
0
  return (EVBUFFER_LENGTH(cs->write_event->output) == 0);
586
0
}
587
588
/* Flush all blocks until output. */
589
static void
590
control_flush_all_blocks(struct client *c)
591
0
{
592
0
  struct control_state  *cs = c->control_state;
593
0
  struct control_block  *cb, *cb1;
594
595
0
  TAILQ_FOREACH_SAFE(cb, &cs->all_blocks, all_entry, cb1) {
596
0
    if (cb->size != 0)
597
0
      break;
598
0
    log_debug("%s: %s: flushing line: %s", __func__, c->name,
599
0
        cb->line);
600
601
0
    bufferevent_write(cs->write_event, cb->line, strlen(cb->line));
602
0
    bufferevent_write(cs->write_event, "\n", 1);
603
0
    control_free_block(cs, cb);
604
0
  }
605
0
}
606
607
/* Append data to buffer. */
608
static struct evbuffer *
609
control_append_data(struct client *c, struct control_pane *cp, uint64_t age,
610
    struct evbuffer *message, struct window_pane *wp, size_t size)
611
0
{
612
0
  u_char  *new_data;
613
0
  size_t   new_size, start;
614
0
  u_int  i;
615
616
0
  if (message == NULL) {
617
0
    message = evbuffer_new();
618
0
    if (message == NULL)
619
0
      fatalx("out of memory");
620
0
    if (c->flags & CLIENT_CONTROL_PAUSEAFTER) {
621
0
      evbuffer_add_printf(message,
622
0
          "%%extended-output %%%u %llu : ", wp->id,
623
0
          (unsigned long long)age);
624
0
    } else
625
0
      evbuffer_add_printf(message, "%%output %%%u ", wp->id);
626
0
  }
627
628
0
  new_data = window_pane_get_new_data(wp, &cp->offset, &new_size);
629
0
  if (new_size < size)
630
0
    fatalx("not enough data: %zu < %zu", new_size, size);
631
0
  for (i = 0; i < size; i++) {
632
0
    if (new_data[i] < ' ' || new_data[i] == '\\') {
633
0
      evbuffer_add_printf(message, "\\%03o", new_data[i]);
634
0
    } else {
635
0
      start = i;
636
0
      while (i + 1 < size &&
637
0
          new_data[i + 1] >= ' ' &&
638
0
          new_data[i + 1] != '\\')
639
0
        i++;
640
0
      evbuffer_add(message, new_data + start, i - start + 1);
641
0
    }
642
0
  }
643
0
  window_pane_update_used_data(wp, &cp->offset, size);
644
0
  return (message);
645
0
}
646
647
/* Write buffer. */
648
static void
649
control_write_data(struct client *c, struct evbuffer *message)
650
0
{
651
0
  struct control_state  *cs = c->control_state;
652
653
0
  log_debug("%s: %s: %.*s", __func__, c->name,
654
0
      (int)EVBUFFER_LENGTH(message), EVBUFFER_DATA(message));
655
656
0
  evbuffer_add(message, "\n", 1);
657
0
  bufferevent_write_buffer(cs->write_event, message);
658
0
  evbuffer_free(message);
659
0
}
660
661
/* Write output to client. */
662
static int
663
control_write_pending(struct client *c, struct control_pane *cp, size_t limit)
664
0
{
665
0
  struct control_state  *cs = c->control_state;
666
0
  struct window_pane  *wp = NULL;
667
0
  struct evbuffer   *message = NULL;
668
0
  size_t       used = 0, size;
669
0
  struct control_block  *cb, *cb1;
670
0
  uint64_t     age, t = get_timer();
671
672
0
  wp = control_window_pane(c, cp->pane);
673
0
  if (wp == NULL || wp->fd == -1) {
674
0
    TAILQ_FOREACH_SAFE(cb, &cp->blocks, entry, cb1) {
675
0
      TAILQ_REMOVE(&cp->blocks, cb, entry);
676
0
      control_free_block(cs, cb);
677
0
    }
678
0
    control_flush_all_blocks(c);
679
0
    return (0);
680
0
  }
681
682
0
  while (used != limit && !TAILQ_EMPTY(&cp->blocks)) {
683
0
    if (control_check_age(c, wp, cp)) {
684
0
      if (message != NULL)
685
0
        evbuffer_free(message);
686
0
      message = NULL;
687
0
      break;
688
0
    }
689
690
0
    cb = TAILQ_FIRST(&cp->blocks);
691
0
    if (cb->t < t)
692
0
      age = t - cb->t;
693
0
    else
694
0
      age = 0;
695
0
    log_debug("%s: %s: output block %zu (age %llu) for %%%u "
696
0
        "(used %zu/%zu)", __func__, c->name, cb->size,
697
0
        (unsigned long long)age, cp->pane, used, limit);
698
699
0
    size = cb->size;
700
0
    if (size > limit - used)
701
0
      size = limit - used;
702
0
    used += size;
703
704
0
    message = control_append_data(c, cp, age, message, wp, size);
705
706
0
    cb->size -= size;
707
0
    if (cb->size == 0) {
708
0
      TAILQ_REMOVE(&cp->blocks, cb, entry);
709
0
      control_free_block(cs, cb);
710
711
0
      cb = TAILQ_FIRST(&cs->all_blocks);
712
0
      if (cb != NULL && cb->size == 0) {
713
0
        if (wp != NULL && message != NULL) {
714
0
          control_write_data(c, message);
715
0
          message = NULL;
716
0
        }
717
0
        control_flush_all_blocks(c);
718
0
      }
719
0
    }
720
0
  }
721
0
  if (message != NULL)
722
0
    control_write_data(c, message);
723
0
  return (!TAILQ_EMPTY(&cp->blocks));
724
0
}
725
726
/* Control client write callback. */
727
static void
728
control_write_callback(__unused struct bufferevent *bufev, void *data)
729
0
{
730
0
  struct client   *c = data;
731
0
  struct control_state  *cs = c->control_state;
732
0
  struct control_pane *cp, *cp1;
733
0
  struct evbuffer   *evb = cs->write_event->output;
734
0
  size_t       space, limit;
735
736
0
  control_flush_all_blocks(c);
737
738
0
  while (EVBUFFER_LENGTH(evb) < CONTROL_BUFFER_HIGH) {
739
0
    if (cs->pending_count == 0)
740
0
      break;
741
0
    space = CONTROL_BUFFER_HIGH - EVBUFFER_LENGTH(evb);
742
0
    log_debug("%s: %s: %zu bytes available, %u panes", __func__,
743
0
        c->name, space, cs->pending_count);
744
745
0
    limit = (space / cs->pending_count / 3); /* 3 bytes for \xxx */
746
0
    if (limit < CONTROL_WRITE_MINIMUM)
747
0
      limit = CONTROL_WRITE_MINIMUM;
748
749
0
    TAILQ_FOREACH_SAFE(cp, &cs->pending_list, pending_entry, cp1) {
750
0
      if (EVBUFFER_LENGTH(evb) >= CONTROL_BUFFER_HIGH)
751
0
        break;
752
0
      if (control_write_pending(c, cp, limit))
753
0
        continue;
754
0
      TAILQ_REMOVE(&cs->pending_list, cp, pending_entry);
755
0
      cp->pending_flag = 0;
756
0
      cs->pending_count--;
757
0
    }
758
0
  }
759
0
  if (EVBUFFER_LENGTH(evb) == 0)
760
0
    bufferevent_disable(cs->write_event, EV_WRITE);
761
0
}
762
763
/* Initialize for control mode. */
764
void
765
control_start(struct client *c)
766
0
{
767
0
  struct control_state  *cs;
768
769
0
  if (c->flags & CLIENT_CONTROLCONTROL) {
770
0
    close(c->out_fd);
771
0
    c->out_fd = -1;
772
0
  } else
773
0
    setblocking(c->out_fd, 0);
774
0
  setblocking(c->fd, 0);
775
776
0
  cs = c->control_state = xcalloc(1, sizeof *cs);
777
0
  RB_INIT(&cs->panes);
778
0
  TAILQ_INIT(&cs->pending_list);
779
0
  TAILQ_INIT(&cs->all_blocks);
780
0
  RB_INIT(&cs->subs);
781
782
0
  cs->read_event = bufferevent_new(c->fd, control_read_callback,
783
0
      control_write_callback, control_error_callback, c);
784
0
  if (cs->read_event == NULL)
785
0
    fatalx("out of memory");
786
787
0
  if (c->flags & CLIENT_CONTROLCONTROL)
788
0
    cs->write_event = cs->read_event;
789
0
  else {
790
0
    cs->write_event = bufferevent_new(c->out_fd, NULL,
791
0
        control_write_callback, control_error_callback, c);
792
0
    if (cs->write_event == NULL)
793
0
      fatalx("out of memory");
794
0
  }
795
0
  bufferevent_setwatermark(cs->write_event, EV_WRITE, CONTROL_BUFFER_LOW,
796
0
      0);
797
798
0
  if (c->flags & CLIENT_CONTROLCONTROL) {
799
0
    bufferevent_write(cs->write_event, "\033P1000p", 7);
800
0
    bufferevent_enable(cs->write_event, EV_WRITE);
801
0
  }
802
0
}
803
804
/* Control client ready. */
805
void
806
control_ready(struct client *c)
807
0
{
808
0
  bufferevent_enable(c->control_state->read_event, EV_READ);
809
0
}
810
811
/* Discard all output for a client. */
812
void
813
control_discard(struct client *c)
814
0
{
815
0
  struct control_state  *cs = c->control_state;
816
0
  struct control_pane *cp;
817
818
0
  RB_FOREACH(cp, control_panes, &cs->panes)
819
0
    control_discard_pane(c, cp);
820
0
  bufferevent_disable(cs->read_event, EV_READ);
821
0
}
822
823
/* Stop control mode. */
824
void
825
control_stop(struct client *c)
826
0
{
827
0
  struct control_state  *cs = c->control_state;
828
0
  struct control_block  *cb, *cb1;
829
0
  struct control_sub  *csub, *csub1;
830
831
0
  if (~c->flags & CLIENT_CONTROLCONTROL)
832
0
    bufferevent_free(cs->write_event);
833
0
  bufferevent_free(cs->read_event);
834
835
0
  RB_FOREACH_SAFE(csub, control_subs, &cs->subs, csub1)
836
0
    control_free_sub(cs, csub);
837
0
  if (evtimer_initialized(&cs->subs_timer))
838
0
    evtimer_del(&cs->subs_timer);
839
840
0
  TAILQ_FOREACH_SAFE(cb, &cs->all_blocks, all_entry, cb1)
841
0
    control_free_block(cs, cb);
842
0
  control_reset_offsets(c);
843
844
0
  free(cs);
845
0
}
846
847
/* Check session subscription. */
848
static void
849
control_check_subs_session(struct client *c, struct control_sub *csub,
850
    struct format_tree *ft)
851
0
{
852
0
  struct session    *s = c->session;
853
0
  char      *value;
854
855
0
  value = format_expand(ft, csub->format);
856
857
0
  if (csub->last != NULL && strcmp(value, csub->last) == 0) {
858
0
    free(value);
859
0
    return;
860
0
  }
861
0
  control_write(c,
862
0
      "%%subscription-changed %s $%u - - - : %s",
863
0
      csub->name, s->id, value);
864
0
  free(csub->last);
865
0
  csub->last = value;
866
0
}
867
868
/* Check pane subscription. */
869
static void
870
control_check_subs_pane(struct client *c, struct control_sub *csub)
871
0
{
872
0
  struct session    *s = c->session;
873
0
  struct window_pane  *wp;
874
0
  struct window   *w;
875
0
  struct winlink    *wl;
876
0
  struct format_tree  *ft;
877
0
  char      *value;
878
0
  struct control_sub_pane *csp, find;
879
880
0
  wp = window_pane_find_by_id(csub->id);
881
0
  if (wp == NULL || wp->fd == -1)
882
0
    return;
883
0
  w = wp->window;
884
885
0
  TAILQ_FOREACH(wl, &w->winlinks, wentry) {
886
0
    if (wl->session != s)
887
0
      continue;
888
889
0
    ft = format_create_defaults(NULL, c, s, wl, wp);
890
0
    value = format_expand(ft, csub->format);
891
0
    format_free(ft);
892
893
0
    find.pane = wp->id;
894
0
    find.idx = wl->idx;
895
896
0
    csp = RB_FIND(control_sub_panes, &csub->panes, &find);
897
0
    if (csp == NULL) {
898
0
      csp = xcalloc(1, sizeof *csp);
899
0
      csp->pane = wp->id;
900
0
      csp->idx = wl->idx;
901
0
      RB_INSERT(control_sub_panes, &csub->panes, csp);
902
0
    }
903
904
0
    if (csp->last != NULL && strcmp(value, csp->last) == 0) {
905
0
      free(value);
906
0
      continue;
907
0
    }
908
0
    control_write(c,
909
0
        "%%subscription-changed %s $%u @%u %u %%%u : %s",
910
0
        csub->name, s->id, w->id, wl->idx, wp->id, value);
911
0
    free(csp->last);
912
0
    csp->last = value;
913
0
  }
914
0
}
915
916
/* Check all-panes subscription for a pane. */
917
static void
918
control_check_subs_all_panes_one(struct client *c, struct control_sub *csub,
919
    struct format_tree *ft, struct winlink *wl, struct window_pane *wp)
920
0
{
921
0
  struct session    *s = c->session;
922
0
  struct window   *w = wl->window;
923
0
  char      *value;
924
0
  struct control_sub_pane *csp, find;
925
926
0
  value = format_expand(ft, csub->format);
927
928
0
  find.pane = wp->id;
929
0
  find.idx = wl->idx;
930
931
0
  csp = RB_FIND(control_sub_panes, &csub->panes, &find);
932
0
  if (csp == NULL) {
933
0
    csp = xcalloc(1, sizeof *csp);
934
0
    csp->pane = wp->id;
935
0
    csp->idx = wl->idx;
936
0
    RB_INSERT(control_sub_panes, &csub->panes, csp);
937
0
  }
938
939
0
  if (csp->last != NULL && strcmp(value, csp->last) == 0) {
940
0
    free(value);
941
0
    return;
942
0
  }
943
0
  control_write(c,
944
0
      "%%subscription-changed %s $%u @%u %u %%%u : %s",
945
0
      csub->name, s->id, w->id, wl->idx, wp->id, value);
946
0
  free(csp->last);
947
0
  csp->last = value;
948
0
}
949
950
/* Check window subscription. */
951
static void
952
control_check_subs_window(struct client *c, struct control_sub *csub)
953
0
{
954
0
  struct session      *s = c->session;
955
0
  struct window     *w;
956
0
  struct winlink      *wl;
957
0
  struct format_tree    *ft;
958
0
  char        *value;
959
0
  struct control_sub_window *csw, find;
960
961
0
  w = window_find_by_id(csub->id);
962
0
  if (w == NULL)
963
0
    return;
964
965
0
  TAILQ_FOREACH(wl, &w->winlinks, wentry) {
966
0
    if (wl->session != s)
967
0
      continue;
968
969
0
    ft = format_create_defaults(NULL, c, s, wl, NULL);
970
0
    value = format_expand(ft, csub->format);
971
0
    format_free(ft);
972
973
0
    find.window = w->id;
974
0
    find.idx = wl->idx;
975
976
0
    csw = RB_FIND(control_sub_windows, &csub->windows, &find);
977
0
    if (csw == NULL) {
978
0
      csw = xcalloc(1, sizeof *csw);
979
0
      csw->window = w->id;
980
0
      csw->idx = wl->idx;
981
0
      RB_INSERT(control_sub_windows, &csub->windows, csw);
982
0
    }
983
984
0
    if (csw->last != NULL && strcmp(value, csw->last) == 0) {
985
0
      free(value);
986
0
      continue;
987
0
    }
988
0
    control_write(c,
989
0
        "%%subscription-changed %s $%u @%u %u - : %s",
990
0
        csub->name, s->id, w->id, wl->idx, value);
991
0
    free(csw->last);
992
0
    csw->last = value;
993
0
  }
994
0
}
995
996
/* Check all-windows subscription for a window. */
997
static void
998
control_check_subs_all_windows_one(struct client *c, struct control_sub *csub,
999
    struct format_tree *ft, struct winlink *wl)
1000
0
{
1001
0
  struct session      *s = c->session;
1002
0
  struct window     *w = wl->window;
1003
0
  char        *value;
1004
0
  struct control_sub_window *csw, find;
1005
1006
0
  value = format_expand(ft, csub->format);
1007
1008
0
  find.window = w->id;
1009
0
  find.idx = wl->idx;
1010
1011
0
  csw = RB_FIND(control_sub_windows, &csub->windows, &find);
1012
0
  if (csw == NULL) {
1013
0
    csw = xcalloc(1, sizeof *csw);
1014
0
    csw->window = w->id;
1015
0
    csw->idx = wl->idx;
1016
0
    RB_INSERT(control_sub_windows, &csub->windows, csw);
1017
0
  }
1018
1019
0
  if (csw->last != NULL && strcmp(value, csw->last) == 0) {
1020
0
    free(value);
1021
0
    return;
1022
0
  }
1023
0
  control_write(c,
1024
0
      "%%subscription-changed %s $%u @%u %u - : %s",
1025
0
      csub->name, s->id, w->id, wl->idx, value);
1026
0
  free(csw->last);
1027
0
  csw->last = value;
1028
0
}
1029
1030
/* Check subscriptions timer. */
1031
static void
1032
control_check_subs_timer(__unused int fd, __unused short events, void *data)
1033
0
{
1034
0
  struct client   *c = data;
1035
0
  struct control_state  *cs = c->control_state;
1036
0
  struct control_sub  *csub, *csub1;
1037
0
  struct session    *s = c->session;
1038
0
  struct format_tree  *ft;
1039
0
  struct winlink    *wl;
1040
0
  struct window_pane  *wp;
1041
0
  struct timeval     tv = { .tv_sec = 1 };
1042
0
  int      have_session = 0, have_all_panes = 0;
1043
0
  int      have_all_windows = 0;
1044
1045
0
  log_debug("%s: timer fired", __func__);
1046
0
  evtimer_add(&cs->subs_timer, &tv);
1047
1048
0
  if (s == NULL)
1049
0
    return;
1050
1051
  /* Find which subscription types are present. */
1052
0
  RB_FOREACH(csub, control_subs, &cs->subs) {
1053
0
    switch (csub->type) {
1054
0
    case CONTROL_SUB_SESSION:
1055
0
      have_session = 1;
1056
0
      break;
1057
0
    case CONTROL_SUB_ALL_PANES:
1058
0
      have_all_panes = 1;
1059
0
      break;
1060
0
    case CONTROL_SUB_ALL_WINDOWS:
1061
0
      have_all_windows = 1;
1062
0
      break;
1063
0
    default:
1064
0
      break;
1065
0
    }
1066
0
  }
1067
1068
  /* Check session subscriptions. */
1069
0
  if (have_session) {
1070
0
    ft = format_create_defaults(NULL, c, s, NULL, NULL);
1071
0
    RB_FOREACH_SAFE(csub, control_subs, &cs->subs, csub1) {
1072
0
      if (csub->type == CONTROL_SUB_SESSION)
1073
0
        control_check_subs_session(c, csub, ft);
1074
0
    }
1075
0
    format_free(ft);
1076
0
  }
1077
1078
  /* Check pane and window subscriptions. */
1079
0
  RB_FOREACH_SAFE(csub, control_subs, &cs->subs, csub1) {
1080
0
    switch (csub->type) {
1081
0
    case CONTROL_SUB_PANE:
1082
0
      control_check_subs_pane(c, csub);
1083
0
      break;
1084
0
    case CONTROL_SUB_WINDOW:
1085
0
      control_check_subs_window(c, csub);
1086
0
      break;
1087
0
    case CONTROL_SUB_SESSION:
1088
0
    case CONTROL_SUB_ALL_PANES:
1089
0
    case CONTROL_SUB_ALL_WINDOWS:
1090
0
      break;
1091
0
    }
1092
0
  }
1093
1094
  /* Check all-panes subscriptions. */
1095
0
  if (have_all_panes) {
1096
0
    RB_FOREACH(wl, winlinks, &s->windows) {
1097
0
      TAILQ_FOREACH(wp, &wl->window->panes, entry) {
1098
0
        ft = format_create_defaults(NULL, c, s, wl, wp);
1099
0
        RB_FOREACH_SAFE(csub, control_subs, &cs->subs,
1100
0
            csub1) {
1101
0
          if (csub->type != CONTROL_SUB_ALL_PANES)
1102
0
            continue;
1103
0
          control_check_subs_all_panes_one(c,
1104
0
              csub, ft, wl, wp);
1105
0
        }
1106
0
        format_free(ft);
1107
0
      }
1108
0
    }
1109
0
  }
1110
1111
  /* Check all-windows subscriptions. */
1112
0
  if (have_all_windows) {
1113
0
    RB_FOREACH(wl, winlinks, &s->windows) {
1114
0
      ft = format_create_defaults(NULL, c, s, wl, NULL);
1115
0
      RB_FOREACH_SAFE(csub, control_subs, &cs->subs,
1116
0
          csub1) {
1117
0
        if (csub->type != CONTROL_SUB_ALL_WINDOWS)
1118
0
          continue;
1119
0
        control_check_subs_all_windows_one(c, csub, ft,
1120
0
            wl);
1121
0
      }
1122
0
      format_free(ft);
1123
0
    }
1124
0
  }
1125
0
}
1126
1127
/* Add a subscription. */
1128
void
1129
control_add_sub(struct client *c, const char *name, enum control_sub_type type,
1130
    int id, const char *format)
1131
0
{
1132
0
  struct control_state  *cs = c->control_state;
1133
0
  struct control_sub  *csub, find;
1134
0
  struct timeval     tv = { .tv_sec = 1 };
1135
1136
0
  find.name = (char *)name;
1137
0
  if ((csub = RB_FIND(control_subs, &cs->subs, &find)) != NULL)
1138
0
    control_free_sub(cs, csub);
1139
1140
0
  csub = xcalloc(1, sizeof *csub);
1141
0
  csub->name = xstrdup(name);
1142
0
  csub->type = type;
1143
0
  csub->id = id;
1144
0
  csub->format = xstrdup(format);
1145
0
  RB_INSERT(control_subs, &cs->subs, csub);
1146
1147
0
  RB_INIT(&csub->panes);
1148
0
  RB_INIT(&csub->windows);
1149
1150
0
  if (!evtimer_initialized(&cs->subs_timer))
1151
0
    evtimer_set(&cs->subs_timer, control_check_subs_timer, c);
1152
0
  if (!evtimer_pending(&cs->subs_timer, NULL))
1153
0
    evtimer_add(&cs->subs_timer, &tv);
1154
0
}
1155
1156
/* Remove a subscription. */
1157
void
1158
control_remove_sub(struct client *c, const char *name)
1159
0
{
1160
0
  struct control_state  *cs = c->control_state;
1161
0
  struct control_sub  *csub, find;
1162
1163
0
  find.name = (char *)name;
1164
0
  if ((csub = RB_FIND(control_subs, &cs->subs, &find)) != NULL)
1165
0
    control_free_sub(cs, csub);
1166
0
  if (RB_EMPTY(&cs->subs))
1167
    evtimer_del(&cs->subs_timer);
1168
0
}