robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
21.3 KiB730 linesraw
1#include <assert.h>
2#include <inttypes.h>
3#include <stdbool.h>
4#include <stdio.h>
5#include <stdlib.h>
6
7#include "klib/kvec.h"
8#include "nvim/api/private/defs.h"
9#include "nvim/api/private/dispatch.h"
10#include "nvim/api/private/helpers.h"
11#include "nvim/api/ui.h"
12#include "nvim/channel.h"
13#include "nvim/channel_defs.h"
14#include "nvim/event/defs.h"
15#include "nvim/event/loop.h"
16#include "nvim/event/multiqueue.h"
17#include "nvim/event/proc.h"
18#include "nvim/event/rstream.h"
19#include "nvim/event/wstream.h"
20#include "nvim/globals.h"
21#include "nvim/log.h"
22#include "nvim/main.h"
23#include "nvim/map_defs.h"
24#include "nvim/memory.h"
25#include "nvim/message.h"
26#include "nvim/msgpack_rpc/channel.h"
27#include "nvim/msgpack_rpc/channel_defs.h"
28#include "nvim/msgpack_rpc/packer.h"
29#include "nvim/msgpack_rpc/packer_defs.h"
30#include "nvim/msgpack_rpc/unpacker.h"
31#include "nvim/os/input.h"
32#include "nvim/types_defs.h"
33#include "nvim/ui.h"
34#include "nvim/ui_client.h"
35
36#ifdef MSWIN
37# include "nvim/os/os_win_console.h"
38#endif
39
40#include "msgpack_rpc/channel.c.generated.h"
41
42#ifdef NVIM_LOG_DEBUG
43# define REQ "[request]  "
44# define RES "[response] "
45# define NOT "[notify]   "
46# define ERR "[error]    "
47
48# define SEND "->"
49# define RECV "<-"
50
51static void log_request(char *dir, uint64_t channel_id, uint32_t req_id, const char *name)
52{
53  logmsg(LOGLVL_DBG, "RPC: ", NULL, -1, false, "%s %" PRIu64 ": %s id=%u: %s\n", dir, channel_id,
54         REQ, req_id, name);
55}
56
57static void log_response(char *dir, uint64_t channel_id, char *kind, uint32_t req_id)
58{
59  logmsg(LOGLVL_DBG, "RPC: ", NULL, -1, false, "%s %" PRIu64 ": %s id=%u\n", dir, channel_id, kind,
60         req_id);
61}
62
63static void log_notify(char *dir, uint64_t channel_id, const char *name)
64{
65  logmsg(LOGLVL_DBG, "RPC: ", NULL, -1, false, "%s %" PRIu64 ": %s %s\n", dir, channel_id, NOT,
66         name);
67}
68
69#else
70# define log_request(...)
71# define log_response(...)
72# define log_notify(...)
73#endif
74
75void rpc_init(void)
76{
77  ch_before_blocking_events = multiqueue_new_child(main_loop.events);
78}
79
80void rpc_start(Channel *channel)
81{
82  channel_incref(channel);
83  channel->is_rpc = true;
84  RpcState *rpc = &channel->rpc;
85  rpc->closed = false;
86  rpc->unpacker = xcalloc(1, sizeof *rpc->unpacker);
87  unpacker_init(rpc->unpacker);
88  rpc->next_request_id = 1;
89  rpc->info = (Dict)ARRAY_DICT_INIT;
90  kv_init(rpc->call_stack);
91
92  if (channel->streamtype != kChannelStreamInternal) {
93    RStream *out = channel_outstream(channel);
94#ifdef NVIM_LOG_DEBUG
95    Stream *in = channel_instream(channel);
96    DLOG("rpc ch %" PRIu64 " in-stream=%p out-stream=%p", channel->id,
97         (void *)in, (void *)out);
98#endif
99
100    rstream_start(out, receive_msgpack, channel);
101  }
102}
103
104static Channel *find_rpc_channel(uint64_t id)
105{
106  Channel *chan = find_channel(id);
107  if (!chan || !chan->is_rpc || chan->rpc.closed) {
108    return NULL;
109  }
110  return chan;
111}
112
113/// Publishes an event to a channel (emits a notification to method `name`).
114///
115/// @param id Channel id, or 0 to broadcast to all RPC channels.
116/// @param name Event name (application-defined)
117/// @param args Array of event arguments
118/// @return True if the event was sent successfully, false otherwise.
119bool rpc_send_event(uint64_t id, const char *name, Array args)
120{
121  Channel *channel = NULL;
122
123  if (id && (!(channel = find_rpc_channel(id)))) {
124    return false;
125  }
126
127  log_notify(SEND, channel ? channel->id : 0, name);
128  if (channel) {
129    serialize_request(&channel, 1, 0, name, args);
130  } else {
131    broadcast_event(name, args);
132  }
133
134  return true;
135}
136
137/// Sends a method call to a channel
138///
139/// @param id The channel id
140/// @param method_name The method name, an arbitrary string
141/// @param args Array with method arguments
142/// @param[out] error True if the return value is an error
143/// @return Whatever the remote method returned
144Object rpc_send_call(uint64_t id, const char *method_name, Array args, ArenaMem *result_mem,
145                     Error *err)
146{
147  Channel *channel = NULL;
148
149  if (!(channel = find_rpc_channel(id))) {
150    api_set_error(err, kErrorTypeException, "Invalid channel: %" PRIu64, id);
151    return NIL;
152  }
153
154  channel_incref(channel);
155  RpcState *rpc = &channel->rpc;
156  uint32_t request_id = rpc->next_request_id++;
157  // Send the msgpack-rpc request
158  serialize_request(&channel, 1, request_id, method_name, args);
159
160  log_request(SEND, channel->id, request_id, method_name);
161
162  // Push the frame
163  ChannelCallFrame frame = { request_id, false, false, NIL, NULL };
164  kv_push(rpc->call_stack, &frame);
165  LOOP_PROCESS_EVENTS_UNTIL(&main_loop, channel->events, -1, frame.returned || rpc->closed);
166  (void)kv_pop(rpc->call_stack);
167
168  // !frame.returned implies rpc->closed.
169  // If frame.returned is true, its memory needs to be freed, so don't return here.
170  if (!frame.returned) {
171    api_set_error(err, kErrorTypeException, "Invalid channel: %" PRIu64, id);
172    channel_decref(channel);
173    return NIL;
174  }
175
176  if (frame.errored) {
177    if (frame.result.type == kObjectTypeString) {
178      api_set_error(err, kErrorTypeException, "%s",
179                    frame.result.data.string.data);
180    } else if (frame.result.type == kObjectTypeArray) {
181      // Should be an error in the form [type, message]
182      Array array = frame.result.data.array;
183      if (array.size == 2 && array.items[0].type == kObjectTypeInteger
184          && (array.items[0].data.integer == kErrorTypeException
185              || array.items[0].data.integer == kErrorTypeValidation)
186          && array.items[1].type == kObjectTypeString) {
187        api_set_error(err, (ErrorType)array.items[0].data.integer, "%s",
188                      array.items[1].data.string.data);
189      } else {
190        api_set_error(err, kErrorTypeException, "%s", "unknown error");
191      }
192    } else {
193      api_set_error(err, kErrorTypeException, "%s", "unknown error");
194    }
195
196    // frame.result was allocated in an arena
197    arena_mem_free(frame.result_mem);
198    frame.result_mem = NULL;
199  }
200
201  channel_decref(channel);
202
203  *result_mem = frame.result_mem;
204
205  return frame.errored ? NIL : frame.result;
206}
207
208static size_t receive_msgpack(RStream *stream, const char *rbuf, size_t c, void *data, bool eof)
209{
210  Channel *channel = data;
211  channel_incref(channel);
212  size_t consumed = 0;
213
214  DLOG("ch %" PRIu64 ": parsing %zu bytes from msgpack Stream: %p",
215       channel->id, c, (void *)stream);
216
217  if (c > 0) {
218    Unpacker *p = channel->rpc.unpacker;
219    p->read_ptr = rbuf;
220    p->read_size = c;
221    parse_msgpack(channel);
222
223    if (!unpacker_closed(p)) {
224      consumed = c - p->read_size;
225    }
226  }
227
228  if (eof) {
229    char buf[256];
230    snprintf(buf, sizeof(buf), "ch %" PRIu64 " was closed by the peer", channel->id);
231    chan_close_on_err(channel, buf, LOGLVL_INF);
232  }
233
234  channel_decref(channel);
235  return consumed;
236}
237
238static ChannelCallFrame *find_call_frame(RpcState *rpc, uint32_t request_id)
239{
240  for (size_t i = 0; i < kv_size(rpc->call_stack); i++) {
241    ChannelCallFrame *frame = kv_Z(rpc->call_stack, i);
242    if (frame->request_id == request_id) {
243      return frame;
244    }
245  }
246  return NULL;
247}
248
249static void parse_msgpack(Channel *channel)
250{
251  Unpacker *p = channel->rpc.unpacker;
252  while (unpacker_advance(p)) {
253    if (p->type == kMessageTypeRedrawEvent) {
254      if (ui_client_attached) {
255        if (p->has_grid_line_event) {
256          ui_client_event_raw_line(&p->grid_line_event);
257          p->has_grid_line_event = false;
258        } else if (p->ui_handler.fn != NULL && p->result.type == kObjectTypeArray) {
259          p->ui_handler.fn(p->result.data.array);
260        }
261      }
262      arena_mem_free(arena_finish(&p->arena));
263    } else if (p->type == kMessageTypeResponse) {
264      ChannelCallFrame *frame = channel->rpc.client_type == kClientTypeMsgpackRpc
265                                ? find_call_frame(&channel->rpc, p->request_id)
266                                : kv_last(channel->rpc.call_stack);
267      if (frame == NULL || p->request_id != frame->request_id) {
268        char buf[256];
269        snprintf(buf, sizeof(buf),
270                 "ch %" PRIu64 " (type=%" PRIu32 ") returned a response with an unknown request "
271                 "id %" PRIu32 ". Ensure the client is properly synchronized",
272                 channel->id, (unsigned)channel->rpc.client_type, p->request_id);
273        chan_close_on_err(channel, buf, LOGLVL_ERR);
274        return;
275      }
276      frame->returned = true;
277      frame->errored = (p->error.type != kObjectTypeNil);
278
279      if (frame->errored) {
280        frame->result = p->error;
281        // TODO(bfredl): p->result should not even be decoded
282        // api_free_object(p->result);
283      } else {
284        frame->result = p->result;
285      }
286      frame->result_mem = arena_finish(&p->arena);
287      log_response(RECV, channel->id, frame->errored ? ERR : RES, p->request_id);
288    } else {
289      if (p->type == kMessageTypeNotification) {
290        log_notify(RECV, channel->id, p->handler.name);
291      } else {
292        log_request(RECV, channel->id, p->request_id, p->handler.name);
293      }
294
295      Object res = p->result;
296      if (p->result.type != kObjectTypeArray) {
297        chan_close_on_err(channel, "msgpack-rpc request args must be an array", LOGLVL_ERR);
298        return;
299      }
300      Array arg = res.data.array;
301      handle_request(channel, p, arg);
302    }
303  }
304
305  if (unpacker_closed(p)) {
306    chan_close_on_err(channel, p->unpack_error.msg, LOGLVL_INF);
307    api_clear_error(&p->unpack_error);
308  }
309}
310
311/// Handles requests and notifications received on the channel.
312static void handle_request(Channel *channel, Unpacker *p, Array args)
313  FUNC_ATTR_NONNULL_ALL
314{
315  assert(p->type == kMessageTypeRequest || p->type == kMessageTypeNotification);
316
317  if (!p->handler.fn) {
318    send_error(channel, p->handler, p->type, p->request_id, p->unpack_error.msg);
319    api_clear_error(&p->unpack_error);
320    arena_mem_free(arena_finish(&p->arena));
321    return;
322  }
323
324  RequestEvent *evdata = xmalloc(sizeof(RequestEvent));
325  evdata->type = p->type;
326  evdata->channel = channel;
327  evdata->handler = p->handler;
328  evdata->args = args;
329  evdata->used_mem = p->arena;
330  p->arena = (Arena)ARENA_EMPTY;
331  evdata->request_id = p->request_id;
332  channel_incref(channel);
333  if (p->handler.fast) {
334    bool is_get_mode = p->handler.fn == handle_nvim_get_mode;
335
336    if (is_get_mode && !input_blocking()) {
337      // Defer the event to a special queue used by os/input.c. #6247
338      multiqueue_put(ch_before_blocking_events, request_event, evdata);
339    } else {
340      // Invoke immediately.
341      request_event((void **)&evdata);
342    }
343  } else {
344    bool is_resize = p->handler.fn == handle_nvim_ui_try_resize;
345    if (is_resize) {
346      Event ev = event_create_oneshot(event_create(request_event, evdata), 2);
347      multiqueue_put_event(channel->events, ev);
348      multiqueue_put_event(resize_events, ev);
349    } else {
350      multiqueue_put(channel->events, request_event, evdata);
351      DLOG("RPC: scheduled %.*s", (int)p->method_name_len, p->handler.name);
352    }
353  }
354}
355
356/// Handles a message, depending on the type:
357///   - Request: invokes method and writes the response (or error).
358///   - Notification: invokes method (emits `nvim_error_event` on error).
359static void request_event(void **argv)
360{
361  RequestEvent *e = argv[0];
362  Channel *channel = e->channel;
363  MsgpackRpcRequestHandler handler = e->handler;
364  Error error = ERROR_INIT;
365  if (channel->rpc.closed) {
366    // channel was closed, abort any pending requests
367    goto free_ret;
368  }
369
370  Object result = handler.fn(channel->id, e->args, &e->used_mem, &error);
371  if (e->type == kMessageTypeRequest || ERROR_SET(&error)) {
372    // Send the response.
373    serialize_response(channel, e->handler, e->type, e->request_id, &error, &result);
374  }
375  if (handler.ret_alloc) {
376    api_free_object(result);
377  }
378
379free_ret:
380  // e->args (and possibly result) are allocated in an arena
381  arena_mem_free(arena_finish(&e->used_mem));
382  channel_decref(channel);
383  xfree(e);
384  api_clear_error(&error);
385}
386
387bool rpc_write_raw(uint64_t id, WBuffer *buffer)
388{
389  Channel *channel = find_rpc_channel(id);
390  if (!channel) {
391    wstream_release_wbuffer(buffer);
392    return false;
393  }
394
395  return channel_write(channel, buffer);
396}
397
398static bool channel_write(Channel *channel, WBuffer *buffer)
399{
400  int err = 0;
401
402  if (channel->rpc.closed) {
403    wstream_release_wbuffer(buffer);
404    return false;
405  }
406
407  if (channel->streamtype == kChannelStreamInternal) {
408    channel_incref(channel);
409    CREATE_EVENT(channel->events, internal_read_event, channel, buffer);
410  } else {
411    Stream *in = channel_instream(channel);
412    err = wstream_write(in, buffer);
413  }
414
415  if (err != 0) {
416    // If the write failed for any reason, close the channel
417    char buf[256];
418    snprintf(buf,
419             sizeof(buf),
420             "ch %" PRIu64 ": stream write failed: %s. "
421             "RPC canceled; closing channel",
422             channel->id, os_strerror(err));
423    // UV_EPIPE can happen if pipe is closed by peer and shouldn't be an error.
424    chan_close_on_err(channel, buf, err == UV_EPIPE ? LOGLVL_INF : LOGLVL_ERR);
425  }
426
427  return err == 0;
428}
429
430static void internal_read_event(void **argv)
431{
432  Channel *channel = argv[0];
433  WBuffer *buffer = argv[1];
434  Unpacker *p = channel->rpc.unpacker;
435
436  p->read_ptr = buffer->data;
437  p->read_size = buffer->size;
438  if (unpacker_closed(p)) {
439    goto end;
440  }
441  parse_msgpack(channel);
442
443  if (p->read_size) {
444    // This should not happen, as WBuffer is one single serialized message.
445    if (!channel->rpc.closed) {
446      chan_close_on_err(channel, "internal channel: internal error", LOGLVL_ERR);
447    }
448  }
449
450end:
451  channel_decref(channel);
452  wstream_release_wbuffer(buffer);
453}
454
455static void send_error(Channel *chan, MsgpackRpcRequestHandler handler, MessageType type,
456                       uint32_t id, char *err)
457{
458  Error e = ERROR_INIT;
459  api_set_error(&e, kErrorTypeException, "%s", err);
460  serialize_response(chan, handler, type, id, &e, &NIL);
461  api_clear_error(&e);
462}
463
464/// Broadcasts a notification to all RPC channels.
465static void broadcast_event(const char *name, Array args)
466{
467  kvec_withinit_t(Channel *, 4) chans = KV_INITIAL_VALUE;
468  kvi_init(chans);
469  Channel *channel;
470
471  map_foreach_value(&channels, channel, {
472    if (channel->is_rpc) {
473      kv_push(chans, channel);
474    }
475  });
476
477  if (kv_size(chans)) {
478    serialize_request(chans.items, kv_size(chans), 0, name, args);
479  }
480
481  kvi_destroy(chans);
482}
483
484/// Mark rpc state as closed, and release its reference to the channel.
485/// Don't call this directly, call channel_close(id, kChannelPartRpc, &error)
486void rpc_close(Channel *channel)
487{
488  if (channel->rpc.closed) {
489    return;
490  }
491
492  channel->rpc.closed = true;
493
494  // Scheduled to avoid running UILeave autocommands in a libuv handler.
495  multiqueue_put(main_loop.fast_events, rpc_close_event, channel);
496}
497
498static void rpc_close_event(void **argv)
499{
500  Channel *channel = (Channel *)argv[0];
501  assert(channel);
502
503  channel_decref(channel);
504
505#ifdef MSWIN
506  // For ":detach!": unexpected disconnect does not call ui_detach_channel, unlike the normal
507  // ":detach" case, so we need to do some cleanup here.
508  bool detached_ui_stdio = channel->streamtype == kChannelStreamStdio
509                           && channel->detach && channel->rpc.ui != NULL;
510#endif
511
512  // No more I/O can happen on this channel. Remove UI if there is one attached.
513  // Do this here instead of in rpc_free() which isn't always called on exit, so that
514  // UILeave events behave consistently.
515  remote_ui_disconnect(channel->id, NULL, false);
516
517  bool is_ui_client = ui_client_channel_id && channel->id == ui_client_channel_id;
518  if (is_ui_client) {
519    if (!exiting && channel->streamtype != kChannelStreamProc) {
520      ui_client_attach_to_restarted_server(false);
521    }
522    if (ui_client_channel_id != channel->id) {
523      // Attached to new server. Don't exit.
524      return;
525    }
526    if (channel->streamtype == kChannelStreamProc && ui_client_error_exit < 0) {
527      // Wait for the embedded server to exit instead of exiting immediately,
528      // as it's necessary to get the server's exit code in on_proc_exit().
529      return;
530    }
531    exit_on_closed_chan(0);
532  } else if (channel->streamtype == kChannelStreamStdio && !channel->detach) {
533    exit_on_closed_chan(0);
534  }
535#ifdef MSWIN
536  else if (detached_ui_stdio) {
537    // Move this server off the now-dead console so it keeps working (CONIN$/CONOUT$).
538    os_swap_to_hidden_console();
539  }
540#endif
541}
542
543void rpc_free(Channel *channel)
544{
545  unpacker_teardown(channel->rpc.unpacker);
546  xfree(channel->rpc.unpacker);
547
548  kv_destroy(channel->rpc.call_stack);
549  api_free_dict(channel->rpc.info);
550}
551
552/// Closes a channel after receiving fatal error, and logs a message.
553static void chan_close_on_err(Channel *channel, char *msg, int loglevel)
554{
555  logmsg(loglevel, "RPC: ", NULL, -1, true, "%s", msg);
556  for (size_t i = 0; i < kv_size(channel->rpc.call_stack); i++) {
557    ChannelCallFrame *frame = kv_A(channel->rpc.call_stack, i);
558    if (frame->returned) {
559      continue;  // Don't overwrite an already received result. #24214
560    }
561    frame->returned = true;
562    frame->errored = true;
563    frame->result = CSTR_TO_ARENA_OBJ(&channel->rpc.unpacker->arena, msg);
564    frame->result_mem = arena_finish(&channel->rpc.unpacker->arena);
565  }
566
567  channel_close(channel->id, kChannelPartRpc, NULL);
568
569  LOG(loglevel, "RPC: %s", msg);
570}
571
572static void serialize_request(Channel **chans, size_t nchans, uint32_t request_id,
573                              const char *method, Array args)
574{
575  PackerBuffer packer;
576  packer_buffer_init_channels(chans, nchans, &packer);
577
578  mpack_array(&packer.ptr, request_id ? 4 : 3);
579  mpack_w(&packer.ptr, request_id ? 0 : 2);
580
581  if (request_id) {
582    mpack_uint(&packer.ptr, request_id);
583  }
584
585  mpack_str(cstr_as_string(method), &packer);
586  mpack_object_array(args, &packer);
587
588  packer_buffer_finish_channels(&packer);
589}
590
591void serialize_response(Channel *channel, MsgpackRpcRequestHandler handler, MessageType type,
592                        uint32_t response_id, Error *err, Object *arg)
593{
594  if (ERROR_SET(err) && type == kMessageTypeNotification) {
595    if (handler.fn == handle_nvim_paste) {
596      // TODO(bfredl): this is pretty much ad-hoc. maybe TUI and UI:s should be
597      // allowed to ask nvim to just scream directly in the users face
598      // instead of sending nvim_error_event, in general.
599      semsg("paste: %s", err->msg);
600      api_clear_error(err);
601    } else {
602      MAXSIZE_TEMP_ARRAY(args, 2);
603      ADD_C(args, INTEGER_OBJ(err->type));
604      ADD_C(args, CSTR_AS_OBJ(err->msg));
605      serialize_request(&channel, 1, 0, "nvim_error_event", args);
606    }
607    return;
608  }
609
610  PackerBuffer packer;
611  packer_buffer_init_channels(&channel, 1, &packer);
612
613  mpack_array(&packer.ptr, 4);
614  mpack_w(&packer.ptr, 1);
615  mpack_uint(&packer.ptr, response_id);
616
617  if (ERROR_SET(err)) {
618    // error represented by a [type, message] array
619    mpack_array(&packer.ptr, 2);
620    mpack_integer(&packer.ptr, err->type);
621    mpack_str(cstr_as_string(err->msg), &packer);
622    // Nil result
623    mpack_nil(&packer.ptr);
624  } else {
625    // Nil error
626    mpack_nil(&packer.ptr);
627    // Return value
628    mpack_object(arg, &packer);
629  }
630
631  packer_buffer_finish_channels(&packer);
632
633  log_response(SEND, channel->id, ERROR_SET(err) ? ERR : RES, response_id);
634}
635
636static void packer_buffer_init_channels(Channel **chans, size_t nchans, PackerBuffer *packer)
637{
638  for (size_t i = 0; i < nchans; i++) {
639    Channel *chan = chans[i];
640    if (chan->rpc.ui && chan->rpc.ui->incomplete_event) {
641      remote_ui_flush_pending_data(chan->rpc.ui);
642    }
643  }
644  packer->startptr = alloc_block();
645  packer->ptr = packer->startptr;
646  packer->endptr = packer->startptr + ARENA_BLOCK_SIZE;
647  packer->packer_flush = channel_flush_callback;
648  packer->anydata = chans;
649  packer->anyint = (int64_t)nchans;
650}
651
652static void packer_buffer_finish_channels(PackerBuffer *packer)
653{
654  size_t len = (size_t)(packer->ptr - packer->startptr);
655  if (len > 0) {
656    WBuffer *buf = wstream_new_buffer(packer->startptr, len, (size_t)packer->anyint, free_block);
657    Channel **chans = packer->anydata;
658    for (int64_t i = 0; i < packer->anyint; i++) {
659      channel_write(chans[i], buf);
660    }
661  } else {
662    free_block(packer->startptr);
663  }
664}
665
666static void channel_flush_callback(PackerBuffer *packer)
667{
668  packer_buffer_finish_channels(packer);
669  packer_buffer_init_channels(packer->anydata, (size_t)packer->anyint, packer);
670}
671
672void rpc_set_client_info(uint64_t id, Dict info)
673{
674  Channel *chan = find_rpc_channel(id);
675  if (!chan) {
676    abort();
677  }
678
679  api_free_dict(chan->rpc.info);
680  chan->rpc.info = info;
681
682  // Parse "type" on "info" and set "client_type"
683  const char *type = get_client_info(chan, "type");
684  if (type == NULL || strequal(type, "remote")) {
685    chan->rpc.client_type = kClientTypeRemote;
686  } else if (strequal(type, "msgpack-rpc")) {
687    chan->rpc.client_type = kClientTypeMsgpackRpc;
688  } else if (strequal(type, "ui")) {
689    chan->rpc.client_type = kClientTypeUi;
690  } else if (strequal(type, "embedder")) {
691    chan->rpc.client_type = kClientTypeEmbedder;
692  } else if (strequal(type, "host")) {
693    chan->rpc.client_type = kClientTypeHost;
694  } else if (strequal(type, "plugin")) {
695    chan->rpc.client_type = kClientTypePlugin;
696  } else {
697    chan->rpc.client_type = kClientTypeUnknown;
698  }
699
700  channel_event(chan, EVENT_CHANINFO);
701}
702
703Dict rpc_client_info(Channel *chan)
704{
705  return copy_dict(chan->rpc.info, NULL);
706}
707
708const char *get_client_info(Channel *chan, const char *key)
709  FUNC_ATTR_NONNULL_ALL
710{
711  if (!chan->is_rpc) {
712    return NULL;
713  }
714  Dict info = chan->rpc.info;
715  for (size_t i = 0; i < info.size; i++) {
716    if (strequal(key, info.items[i].key.data)
717        && info.items[i].value.type == kObjectTypeString) {
718      return info.items[i].value.data.string.data;
719    }
720  }
721
722  return NULL;
723}
724
725#ifdef EXITFREE
726void rpc_free_all_mem(void)
727{
728  multiqueue_free(ch_before_blocking_events);
729}
730#endif