Debian Codegraph @

src/ws/cockpitchannel.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 /* This gets logged as part of the (more verbose) protocol logging */
9 #ifdef G_LOG_DOMAIN
10 #undef G_LOG_DOMAIN
11 #endif
12 #define G_LOG_DOMAIN "cockpit-protocol"
13
14 #include "cockpitchannel.h"
15
16 #include "cockpitflow.h"
17 #include "cockpitjson.h"
18 #include "cockpitunicode.h"
19
20 #include <json-glib/json-glib.h>
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 /**
26 * CockpitChannel:
27 *
28 * A base class for channels. Derived classes implement the actual payload
29 * contents, opening the channel etc...
30 *
31 * A CockpitChannel is the base class for C code where messages on
32 * the internal protocol are translated to IO of another type, be
33 * that HTTP, stdio, DBus, WebSocket, file access, or whatever. Another
34 * similar analogue to this code is the javascript cockpit.channel code
35 * in cockpit.js.
36 *
37 * Most uses of this code are in the bridges, but cockpit-ws also (rarely)
38 * uses CockpitChannel to translate the frontend side of channels to HTTP
39 * or WebSocket responses.
40 *
41 * The channel queues messages received until unfrozen. The caller can
42 * start off a channel as frozen, and then the implementation later
43 * indicates that it's open and ready to receive messages.
44 *
45 * A channel sends messages over a #CockpitTransport. If the transport
46 * closes then the channel closes, but the channel can also close
47 * individually either for failure reasons, or with an orderly shutdown.
48 *
49 * See doc/protocol.md for information about channels.
50 *
51 * A channel can do flow control in two ways:
52 *
53 * - It can throttle its peer sending data, by delaying responding to "ping"
54 * messages. It can listen to a "pressure" signal to control this.
55 * - It can optionally control another flow, by emitting a "pressure" signal
56 * when its peer receiving data does not respond to "ping" messages within
57 * a given window.
58 */
59
60 /* Every 16K Send a ping */
61 #define CHANNEL_FLOW_PING (16L * 1024L)
62
63 /* Allow up to 1MB of data to be sent without ack */
64 #define CHANNEL_FLOW_WINDOW (2L * 1024L * 1024L)
65
66 typedef struct {
67 gulong recv_sig;
68 gulong close_sig;
69 gulong control_sig;
70
71 /* Construct arguments */
72 CockpitTransport *transport;
73 gchar *id;
74 JsonObject *open_options;
75 gchar **capabilities;
76
77 /* Queued messages before channel is ready */
78 gboolean prepared;
79 guint prepare_tag;
80
81 /* Whether we've sent a closed message */
82 gboolean sent_close;
83
84 /* Whether we called the close vfunc */
85 gboolean emitted_close;
86
87 /* Whether the transport closed (before we did) */
88 gboolean transport_closed;
89
90 /* EOF flags */
91 gboolean sent_done;
92 gboolean received_done;
93
94 /* Binary options */
95 gboolean binary_ok;
96
97 /* Other state */
98 JsonObject *close_options;
99
100 /* Buffer for incomplete unicode bytes */
101 GBytes *out_buffer;
102 gint buffer_timeout;
103
104 /* The number of bytes sent, and current flow control window */
105 gint64 out_sequence;
106 gint64 out_window;
107
108 /* Another object giving back-pressure on received data */
109 gboolean flow_control;
110 CockpitFlow *pressure;
111 gulong pressure_sig;
112 GQueue *throttled;
113 } CockpitChannelPrivate;
114
115 enum {
116 PROP_0,
117 PROP_TRANSPORT,
118 PROP_ID,
119 PROP_OPTIONS,
120 PROP_CAPABILITIES,
121 };
122
123 static guint cockpit_channel_sig_closed;
124
125 static void cockpit_channel_flow_iface_init (CockpitFlowInterface *iface);
126
127 G_DEFINE_TYPE_WITH_CODE (CockpitChannel, cockpit_channel, G_TYPE_OBJECT,
128 G_IMPLEMENT_INTERFACE (COCKPIT_TYPE_FLOW, cockpit_channel_flow_iface_init)
129 G_ADD_PRIVATE (CockpitChannel));
130
131 static gboolean
132 on_idle_prepare (gpointer data)
133 {
134 CockpitChannel *self = COCKPIT_CHANNEL (data);
135 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
136 g_object_ref (self);
137 priv->prepare_tag = 0;
138 cockpit_channel_prepare (self);
139 g_object_unref (self);
140 return FALSE;
141 }
142
143 static void
144 cockpit_channel_init (CockpitChannel *self)
145 {
146 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
147
148 priv->out_sequence = 0;
149 priv->out_window = CHANNEL_FLOW_WINDOW;
150 }
151
152 static void
153 process_recv (CockpitChannel *self,
154 GBytes *payload)
155 {
156 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
157 CockpitChannelClass *klass;
158
159 if (priv->received_done)
160 {
161 cockpit_channel_fail (self, "protocol-error", "channel received message after done");
162 }
163 else
164 {
165 klass = COCKPIT_CHANNEL_GET_CLASS (self);
166 if (klass->recv)
167 (klass->recv) (self, payload);
168 }
169 }
170
171 static gboolean
172 on_transport_recv (CockpitTransport *transport,
173 const gchar *channel_id,
174 GBytes *data,
175 gpointer user_data)
176 {
177 CockpitChannel *self = user_data;
178 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
179
180 if (g_strcmp0 (channel_id, priv->id) != 0)
181 return FALSE;
182
183 process_recv (self, data);
184 return TRUE;
185 }
186
187 static gboolean
188 process_ping (CockpitChannel *self,
189 JsonObject *ping)
190 {
191 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
192 GBytes *payload;
193
194 if (priv->throttled)
195 {
196 g_debug ("%s: received ping while throttled", priv->id);
197 g_queue_push_tail (priv->throttled, json_object_ref (ping));
198 return FALSE;
199 }
200 else
201 {
202 g_debug ("%s: replying to ping with pong", priv->id);
203 json_object_set_string_member (ping, "command", "pong");
204 payload = cockpit_json_write_bytes (ping);
205 cockpit_transport_send (priv->transport, NULL, payload);
206 g_bytes_unref (payload);
207 return TRUE;
208 }
209 }
210
211 static void
212 process_pong (CockpitChannel *self,
213 JsonObject *pong)
214 {
215 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
216 gint64 sequence;
217
218 if (!priv->flow_control)
219 return;
220
221 if (!cockpit_json_get_int (pong, "sequence", -1, &sequence))
222 {
223 g_message ("%s: received invalid \"pong\" \"sequence\" field", priv->id);
224 sequence = -1;
225 }
226
227 g_debug ("%s: received pong with sequence: %" G_GINT64_FORMAT, priv->id, sequence);
228 if (sequence > priv->out_window + (CHANNEL_FLOW_WINDOW * 10))
229 {
230 g_message ("%s: received a flow control ack with a suspiciously large sequence: %" G_GINT64_FORMAT,
231 priv->id, sequence);
232 }
233
234 if (sequence >= priv->out_window)
235 {
236 /* Up to this point has been confirmed received */
237 priv->out_window = sequence + CHANNEL_FLOW_WINDOW;
238
239 /* If our sent bytes are within the window, no longer under pressure */
240 if (priv->out_sequence <= priv->out_window)
241 {
242 g_debug ("%s: got acknowledge of enough data, relieving back pressure", priv->id);
243 cockpit_flow_emit_pressure (COCKPIT_FLOW (self), FALSE);
244 }
245 }
246 }
247
248 static void
249 process_control (CockpitChannel *self,
250 const gchar *command,
251 JsonObject *options)
252 {
253 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
254 CockpitChannelClass *klass;
255 const gchar *problem;
256
257 if (g_str_equal (command, "close"))
258 {
259 g_debug ("close channel %s", priv->id);
260 if (!cockpit_json_get_string (options, "problem", NULL, &problem))
261 problem = NULL;
262 cockpit_channel_close (self, problem);
263 return;
264 }
265
266 if (g_str_equal (command, "ping"))
267 {
268 process_ping (self, options);
269 return;
270 }
271 else if (g_str_equal (command, "pong"))
272 {
273 process_pong (self, options);
274 return;
275 }
276 else if (g_str_equal (command, "done"))
277 {
278 if (priv->received_done)
279 cockpit_channel_fail (self, "protocol-error", "channel received second done");
280 else
281 priv->received_done = TRUE;
282 }
283
284 klass = COCKPIT_CHANNEL_GET_CLASS (self);
285 if (klass->control)
286 (klass->control) (self, command, options);
287 }
288
289 static gboolean
290 on_transport_control (CockpitTransport *transport,
291 const char *command,
292 const gchar *channel_id,
293 JsonObject *options,
294 GBytes *payload,
295 gpointer user_data)
296 {
297 CockpitChannel *self = user_data;
298 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
299
300 if (g_strcmp0 (channel_id, priv->id) != 0)
301 return FALSE;
302
303 process_control (self, command, options);
304 return TRUE;
305 }
306
307 static void
308 on_transport_closed (CockpitTransport *transport,
309 const gchar *problem,
310 gpointer user_data)
311 {
312 CockpitChannel *self = COCKPIT_CHANNEL (user_data);
313 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
314 priv->transport_closed = TRUE;
315 if (problem == NULL)
316 problem = "disconnected";
317 if (!priv->emitted_close)
318 cockpit_channel_close (self, problem);
319 }
320
321 static void
322 cockpit_channel_actual_send (CockpitChannel *self,
323 GBytes *payload,
324 gboolean trust_is_utf8)
325 {
326 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
327 GBytes *validated = NULL;
328 guint64 out_sequence;
329 JsonObject *ping;
330 gsize size;
331
332 g_return_if_fail (priv->out_buffer == NULL);
333 g_return_if_fail (priv->buffer_timeout == 0);
334
335 if (!trust_is_utf8)
336 {
337 if (!priv->binary_ok)
338 payload = validated = cockpit_unicode_force_utf8 (payload);
339 }
340
341 cockpit_transport_send (priv->transport, priv->id, payload);
342
343 /* A wraparound of our gint64 size? */
344 if (priv->flow_control)
345 {
346 gboolean trigger_pressure;
347 size = g_bytes_get_size (payload);
348 g_return_if_fail (G_MAXINT64 - size > priv->out_sequence);
349
350 /* How many bytes have been sent (queued) */
351 out_sequence = priv->out_sequence + size;
352
353 /* If we've sent more than the window, we just got under pressure;
354 * do an edge trigger instead of level trigger to avoid ping/signal loops */
355 trigger_pressure = (priv->out_sequence <= priv->out_window) && (out_sequence > priv->out_window);
356
357 /* Every CHANNEL_FLOW_PING bytes we send a ping; also when applying back
358 * pressure as there is otherwise nothing more to send and generate pings for */
359 if ((out_sequence / CHANNEL_FLOW_PING != priv->out_sequence / CHANNEL_FLOW_PING) || trigger_pressure)
360 {
361 ping = json_object_new ();
362 json_object_set_int_member (ping, "sequence", out_sequence);
363 cockpit_channel_control (self, "ping", ping);
364 g_debug ("%s: sending ping with sequence: %" G_GINT64_FORMAT, priv->id, out_sequence);
365 json_object_unref (ping);
366 }
367
368 priv->out_sequence = out_sequence;
369
370 if (trigger_pressure)
371 {
372 g_debug ("%s: sent too much data without acknowledgement, emitting back pressure until %"
373 G_GINT64_FORMAT, priv->id, priv->out_window);
374 cockpit_flow_emit_pressure (COCKPIT_FLOW (self), TRUE);
375 }
376 }
377
378 if (validated)
379 g_bytes_unref (validated);
380 }
381
382 static gboolean
383 flush_buffer (gpointer user_data)
384 {
385 CockpitChannel *self = user_data;
386 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
387 GBytes *payload;
388
389 if (priv->out_buffer)
390 {
391 payload = g_bytes_ref (priv->out_buffer);
392 g_bytes_unref (priv->out_buffer);
393 priv->out_buffer = NULL;
394 if (priv->buffer_timeout)
395 g_source_remove(priv->buffer_timeout);
396 priv->buffer_timeout = 0;
397
398 cockpit_channel_actual_send (self, payload, FALSE);
399 g_bytes_unref (payload);
400 }
401
402 return FALSE;
403 }
404
405 static void
406 cockpit_channel_constructed (GObject *object)
407 {
408 CockpitChannel *self = COCKPIT_CHANNEL (object);
409 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
410
411 G_OBJECT_CLASS (cockpit_channel_parent_class)->constructed (object);
412
413 g_return_if_fail (priv->id != NULL);
414 g_return_if_fail (priv->transport != NULL);
415
416 priv->capabilities = NULL;
417 priv->recv_sig = g_signal_connect (priv->transport, "recv",
418 G_CALLBACK (on_transport_recv), self);
419 priv->control_sig = g_signal_connect (priv->transport, "control",
420 G_CALLBACK (on_transport_control), self);
421 priv->close_sig = g_signal_connect (priv->transport, "closed",
422 G_CALLBACK (on_transport_closed), self);
423
424 /* Freeze this channel's messages until ready */
425 cockpit_transport_freeze (priv->transport, priv->id);
426 priv->prepare_tag = g_idle_add_full (G_PRIORITY_HIGH, on_idle_prepare, self, NULL);
427 }
428
429 static gboolean
430 cockpit_channel_ensure_capable (CockpitChannel *self,
431 JsonObject *options)
432 {
433 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
434 const gchar **capabilities = NULL;
435 JsonObject *close_options = NULL; // owned by channel
436 gboolean missing = FALSE;
437 gboolean ret = FALSE;
438 gint len;
439 gint i;
440
441 if (!cockpit_json_get_strv (options, "capabilities", NULL, &capabilities))
442 {
443 cockpit_channel_fail (self, "protocol-error", "got invalid capabilities field in open message");
444 goto out;
445 }
446
447 if (!capabilities)
448 {
449 ret = TRUE;
450 goto out;
451 }
452
453 len = g_strv_length ((gchar **) capabilities);
454 for (i = 0; i < len; i++)
455 {
456 if (priv->capabilities == NULL || !g_strv_contains((const gchar **) priv->capabilities, capabilities[i]))
457 {
458 g_message ("%s: unsupported capability required: %s", priv->id, capabilities[i]);
459 missing = TRUE;
460 }
461 }
462
463 if (missing)
464 {
465 JsonArray *arr = json_array_new (); // owned by closed options
466
467 if (priv->capabilities != NULL)
468 {
469 len = g_strv_length (priv->capabilities);
470 for (i = 0; i < len; i++)
471 json_array_add_string_element (arr, priv->capabilities[i]);
472 }
473
474 close_options = cockpit_channel_close_options (self);
475 json_object_set_array_member (close_options, "capabilities", arr);
476 cockpit_channel_close (self, "not-supported");
477 }
478
479 ret = !missing;
480
481 out:
482 g_free (capabilities);
483 return ret;
484 }
485
486 static void
487 cockpit_channel_real_prepare (CockpitChannel *self)
488 {
489 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
490 JsonObject *options;
491 const gchar *binary;
492
493 options = cockpit_channel_get_options (self);
494
495 if (!cockpit_channel_ensure_capable (self, options))
496 return;
497
498 if (G_OBJECT_TYPE (self) == COCKPIT_TYPE_CHANNEL)
499 {
500 cockpit_channel_close (self, "not-supported");
501 return;
502 }
503
504 if (!cockpit_json_get_string (options, "binary", NULL, &binary))
505 {
506 cockpit_channel_fail (self, "protocol-error", "channel has invalid \"binary\" option");
507 }
508 else if (binary != NULL)
509 {
510 priv->binary_ok = TRUE;
511 if (!g_str_equal (binary, "raw"))
512 {
513 cockpit_channel_fail (self, "protocol-error",
514 "channel has invalid \"binary\" option: %s", binary);
515 }
516 }
517
518 /*
519 * The default here, can change from FALSE to TRUE over time once we assume that all
520 * cockpit-ws participants have been upgraded sufficiently. The default when we're
521 * on the channel creation side is to handle flow control.
522 */
523 if (!cockpit_json_get_bool (options, "flow-control", FALSE, &priv->flow_control))
524 {
525 cockpit_channel_fail (self, "protocol-error", "channel has invalid \"flow-control\" option");
526 }
527 }
528
529 static void
530 cockpit_channel_get_property (GObject *object,
531 guint prop_id,
532 GValue *value,
533 GParamSpec *pspec)
534 {
535 CockpitChannel *self = COCKPIT_CHANNEL (object);
536 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
537
538 switch (prop_id)
539 {
540 case PROP_TRANSPORT:
541 g_value_set_object (value, priv->transport);
542 break;
543 case PROP_ID:
544 g_value_set_string (value, priv->id);
545 break;
546 default:
547 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
548 break;
549 }
550 }
551
552 static void
553 cockpit_channel_set_property (GObject *object,
554 guint prop_id,
555 const GValue *value,
556 GParamSpec *pspec)
557 {
558 CockpitChannel *self = COCKPIT_CHANNEL (object);
559 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
560
561 switch (prop_id)
562 {
563 case PROP_TRANSPORT:
564 priv->transport = g_value_dup_object (value);
565 break;
566 case PROP_ID:
567 priv->id = g_value_dup_string (value);
568 break;
569 case PROP_OPTIONS:
570 priv->open_options = g_value_dup_boxed (value);
571 break;
572 case PROP_CAPABILITIES:
573 g_return_if_fail (priv->capabilities == NULL);
574 priv->capabilities = g_value_dup_boxed (value);
575 break;
576 default:
577 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
578 break;
579 }
580 }
581
582 static void
583 cockpit_channel_dispose (GObject *object)
584 {
585 CockpitChannel *self = COCKPIT_CHANNEL (object);
586 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
587
588 /*
589 * This object was destroyed before going to the main loop
590 * no need to wait until later before we fire various signals.
591 */
592 if (priv->prepare_tag)
593 {
594 g_source_remove (priv->prepare_tag);
595 priv->prepare_tag = 0;
596 }
597
598 if (priv->recv_sig)
599 g_signal_handler_disconnect (priv->transport, priv->recv_sig);
600 priv->recv_sig = 0;
601
602 if (priv->control_sig)
603 g_signal_handler_disconnect (priv->transport, priv->control_sig);
604 priv->control_sig = 0;
605
606 if (priv->close_sig)
607 g_signal_handler_disconnect (priv->transport, priv->close_sig);
608 priv->close_sig = 0;
609
610 if (!priv->emitted_close)
611 cockpit_channel_close (self, "terminated");
612
613 if (priv->buffer_timeout)
614 g_source_remove(priv->buffer_timeout);
615 priv->buffer_timeout = 0;
616
617 if (priv->out_buffer)
618 g_bytes_unref (priv->out_buffer);
619 priv->out_buffer = NULL;
620
621 cockpit_flow_throttle (COCKPIT_FLOW (self), NULL);
622 g_assert (priv->pressure == NULL);
623 if (priv->throttled)
624 g_queue_free_full (priv->throttled, (GDestroyNotify)json_object_unref);
625 priv->throttled = NULL;
626
627 G_OBJECT_CLASS (cockpit_channel_parent_class)->dispose (object);
628 }
629
630 static void
631 cockpit_channel_finalize (GObject *object)
632 {
633 CockpitChannel *self = COCKPIT_CHANNEL (object);
634 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
635
636 g_object_unref (priv->transport);
637 if (priv->open_options)
638 json_object_unref (priv->open_options);
639 if (priv->close_options)
640 json_object_unref (priv->close_options);
641
642 g_strfreev (priv->capabilities);
643 g_free (priv->id);
644
645 G_OBJECT_CLASS (cockpit_channel_parent_class)->finalize (object);
646 }
647
648 static void
649 cockpit_channel_real_close (CockpitChannel *self,
650 const gchar *problem)
651 {
652 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
653 JsonObject *object;
654 GBytes *message;
655
656 if (priv->sent_close)
657 return;
658
659 priv->sent_close = TRUE;
660
661 if (!priv->transport_closed)
662 {
663 flush_buffer (self);
664
665 if (priv->close_options)
666 {
667 object = priv->close_options;
668 priv->close_options = NULL;
669 }
670 else
671 {
672 object = json_object_new ();
673 }
674
675 json_object_set_string_member (object, "command", "close");
676 json_object_set_string_member (object, "channel", priv->id);
677 if (problem)
678 json_object_set_string_member (object, "problem", problem);
679
680 message = cockpit_json_write_bytes (object);
681 json_object_unref (object);
682
683 cockpit_transport_send (priv->transport, NULL, message);
684 g_bytes_unref (message);
685 }
686
687 g_signal_emit (self, cockpit_channel_sig_closed, 0, problem);
688 }
689
690 static void
691 cockpit_channel_class_init (CockpitChannelClass *klass)
692 {
693 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
694
695 gobject_class->constructed = cockpit_channel_constructed;
696 gobject_class->get_property = cockpit_channel_get_property;
697 gobject_class->set_property = cockpit_channel_set_property;
698 gobject_class->dispose = cockpit_channel_dispose;
699 gobject_class->finalize = cockpit_channel_finalize;
700
701 klass->prepare = cockpit_channel_real_prepare;
702 klass->close = cockpit_channel_real_close;
703
704 /**
705 * CockpitChannel:transport:
706 *
707 * The transport to send and receive messages over.
708 */
709 g_object_class_install_property (gobject_class, PROP_TRANSPORT,
710 g_param_spec_object ("transport", "transport", "transport", COCKPIT_TYPE_TRANSPORT,
711 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
712
713 /**
714 * CockpitChannel:channel:
715 *
716 * The numeric channel to receive and send messages on.
717 */
718 g_object_class_install_property (gobject_class, PROP_ID,
719 g_param_spec_string ("id", "id", "id", NULL,
720 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
721
722 /**
723 * CockpitChannel:options:
724 *
725 * The JSON options used to open this channel. The exact contents are
726 * dependent on the derived channel class ... but this must at the
727 * very least contain a 'payload' field describing what kind of channel
728 * this should be.
729 */
730 g_object_class_install_property (gobject_class, PROP_OPTIONS,
731 g_param_spec_boxed ("options", "options", "options", JSON_TYPE_OBJECT,
732 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
733
734 /**
735 * CockpitChannel:capabilities:
736 *
737 * The capabilities that this channel supports.
738 */
739 g_object_class_install_property (gobject_class, PROP_CAPABILITIES,
740 g_param_spec_boxed ("capabilities",
741 "Capabilities",
742 "Channel Capabilities",
743 G_TYPE_STRV,
744 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
745
746 /**
747 * CockpitChannel::closed:
748 *
749 * Emitted when the channel closes. This is similar to CockpitTransport::closed
750 * but only applies to the individual channel.
751 *
752 * The channel will also be closed when the transport closes.
753 */
754 cockpit_channel_sig_closed = g_signal_new ("closed", COCKPIT_TYPE_CHANNEL, G_SIGNAL_RUN_LAST,
755 G_STRUCT_OFFSET (CockpitChannelClass, closed),
756 NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_STRING);
757 }
758
759 /**
760 * cockpit_channel_close:
761 * @self: a channel
762 * @problem: the problem or NULL
763 *
764 * Close the channel. This can be called multiple times.
765 *
766 * It may be that the channel doesn't close immediately.
767 * The channel will emit the CockpitChannel::closed signal when the
768 * channel actually closes.
769 *
770 * If this is called immediately after or during construction then
771 * the closing will happen after the main loop so that handlers
772 * can connect appropriately.
773 *
774 * A @problem of NULL represents an orderly close.
775 */
776 void
777 cockpit_channel_close (CockpitChannel *self,
778 const gchar *problem)
779 {
780 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
781 CockpitChannelClass *klass;
782
783 g_return_if_fail (COCKPIT_IS_CHANNEL (self));
784
785 /* No further messages should be received */
786 if (priv->recv_sig)
787 g_signal_handler_disconnect (priv->transport, priv->recv_sig);
788 priv->recv_sig = 0;
789
790 if (priv->control_sig)
791 g_signal_handler_disconnect (priv->transport, priv->control_sig);
792 priv->control_sig = 0;
793
794 if (priv->close_sig)
795 g_signal_handler_disconnect (priv->transport, priv->close_sig);
796 priv->close_sig = 0;
797
798 klass = COCKPIT_CHANNEL_GET_CLASS (self);
799 g_assert (klass->close != NULL);
800 priv->emitted_close = TRUE;
801 (klass->close) (self, problem);
802 }
803
804 /*
805 * cockpit_channel_fail:
806 * @self: a channel
807 * @problem: the problem
808 *
809 * Close the channel with a @problem. In addition a "message" field
810 * will be set on the channel, using the @format argument to build
811 * the message. The message will also be logged.
812 *
813 * See cockpit_channel_close() for further info.
814 */
815 void
816 cockpit_channel_fail (CockpitChannel *self,
817 const gchar *problem,
818 const gchar *format,
819 ...)
820 {
821 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
822 JsonObject *options;
823 gchar *message;
824 va_list va;
825
826 g_return_if_fail (problem != NULL);
827 g_return_if_fail (COCKPIT_IS_CHANNEL (self));
828
829 va_start (va, format);
830 message = g_strdup_vprintf (format, va);
831 va_end (va);
832
833 options = cockpit_channel_close_options (self);
834 if (!json_object_has_member (options, "message"))
835 json_object_set_string_member (options, "message", message);
836 g_message ("%s: %s", priv->id, message);
837 g_free (message);
838
839 cockpit_channel_close (self, problem);
840 }
841
842 /* Used by implementations */
843
844 /**
845 * cockpit_channel_ready:
846 * @self: a pipe
847 * @message: an optional control message, or NULL
848 *
849 * Called by channel implementations to signal when they're
850 * ready. Any messages received before the channel was ready
851 * will be delivered to the channel's recv() vfunc in the order
852 * that they were received.
853 *
854 * If this is called immediately after or during construction then
855 * the closing will happen after the main loop so that handlers
856 * can connect appropriately.
857 */
858 void
859 cockpit_channel_ready (CockpitChannel *self,
860 JsonObject *message)
861 {
862 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
863
864 g_object_ref (self);
865
866 cockpit_transport_thaw (priv->transport, priv->id);
867 cockpit_channel_control (self, "ready", message);
868
869 g_object_unref (self);
870 }
871
872 /**
873 * cockpit_channel_send:
874 * @self: a pipe
875 * @payload: the message payload to send
876 * @trust_is_utf8: set to true if sure data is UTF8
877 *
878 * Called by implementations to send a message over the transport
879 * on the right channel.
880 *
881 * This message is queued, and sent once the transport can.
882 */
883 void
884 cockpit_channel_send (CockpitChannel *self,
885 GBytes *payload,
886 gboolean trust_is_utf8)
887 {
888 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
889 const guint8 *data;
890 gsize length;
891 GBytes *send_data = payload;
892 GByteArray *combined;
893
894 if (priv->buffer_timeout)
895 g_source_remove(priv->buffer_timeout);
896 priv->buffer_timeout = 0;
897
898 if (priv->out_buffer)
899 {
900 combined = g_bytes_unref_to_array (priv->out_buffer);
901 priv->out_buffer = NULL;
902
903 data = g_bytes_get_data (payload, &length);
904 g_byte_array_append (combined, data, length);
905 send_data = g_byte_array_free_to_bytes (combined);
906
907 trust_is_utf8 = FALSE;
908 }
909
910 if (!trust_is_utf8 && !priv->binary_ok)
911 {
912 if (cockpit_unicode_has_incomplete_ending (send_data))
913 {
914 priv->out_buffer = g_bytes_ref (send_data);
915 priv->buffer_timeout = g_timeout_add (500, flush_buffer, self);
916 }
917 }
918
919 if (!priv->buffer_timeout)
920 cockpit_channel_actual_send (self, send_data, trust_is_utf8);
921
922 if (send_data != payload)
923 g_bytes_unref (send_data);
924 }
925
926 /**
927 * cockpit_channel_get_option:
928 * @self: a channel
929 *
930 * Called by implementations to get the channel's open options.
931 *
932 * Returns: (transfer none): the open options, should not be NULL
933 */
934 JsonObject *
935 cockpit_channel_get_options (CockpitChannel *self)
936 {
937 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
938
939 g_return_val_if_fail (COCKPIT_IS_CHANNEL (self), NULL);
940 return priv->open_options;
941 }
942
943 /**
944 * cockpit_channel_get_transport:
945 * @self: a channel
946 *
947 * Called by implementations to get the channel's transtport.
948 *
949 * Returns: (transfer none): the transport, should not be NULL
950 */
951 CockpitTransport *
952 cockpit_channel_get_transport (CockpitChannel *self)
953 {
954 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
955
956 g_return_val_if_fail (COCKPIT_IS_CHANNEL (self), NULL);
957 return priv->transport;
958 }
959
960 /**
961 * cockpit_channel_close_options
962 * @self: a channel
963 *
964 * Called by implementations to get the channel's close options.
965 *
966 * Returns: (transfer none): the close options, should not be NULL
967 */
968 JsonObject *
969 cockpit_channel_close_options (CockpitChannel *self)
970 {
971 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
972
973 g_return_val_if_fail (COCKPIT_IS_CHANNEL (self), NULL);
974 if (!priv->close_options)
975 priv->close_options = json_object_new ();
976 return priv->close_options;
977 }
978
979 /**
980 * cockpit_channel_get_id:
981 * @self a channel
982 *
983 * Get the identifier for this channel.
984 *
985 * Returns: (transfer none): the identifier
986 */
987 const gchar *
988 cockpit_channel_get_id (CockpitChannel *self)
989 {
990 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
991
992 g_return_val_if_fail (COCKPIT_IS_CHANNEL (self), NULL);
993 return priv->id;
994 }
995
996 /**
997 * cockpit_channel_prepare:
998 * @self: the channel
999 *
1000 * Usually this is automatically called after the channel is
1001 * created and control returns to the mainloop. However you
1002 * can preempt that by calling this function. In the case of
1003 * a frozen channel, this method needs to be called to set
1004 * things in motion.
1005 */
1006 void
1007 cockpit_channel_prepare (CockpitChannel *self)
1008 {
1009 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
1010 CockpitChannelClass *klass;
1011
1012 g_return_if_fail (COCKPIT_IS_CHANNEL (self));
1013
1014 if (priv->prepared)
1015 return;
1016
1017 if (priv->prepare_tag)
1018 {
1019 g_source_remove (priv->prepare_tag);
1020 priv->prepare_tag = 0;
1021 }
1022
1023 priv->prepared = TRUE;
1024 if (!priv->emitted_close)
1025 {
1026 klass = COCKPIT_CHANNEL_GET_CLASS (self);
1027 g_assert (klass->prepare);
1028 (klass->prepare) (self);
1029 }
1030 }
1031
1032 /**
1033 * cockpit_channel_control:
1034 * @self: the channel
1035 * @command: the control command
1036 * @options: optional control message or NULL
1037 *
1038 * Send a control message to the other side.
1039 *
1040 * If @options is not NULL, then it may be modified by this code.
1041 *
1042 * With @command of "done" will send an EOF to the other side. This
1043 * should only be called once. Whether an EOF should be sent or not
1044 * depends on the payload type.
1045 */
1046 void
1047 cockpit_channel_control (CockpitChannel *self,
1048 const gchar *command,
1049 JsonObject *options)
1050 {
1051 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
1052 JsonObject *object;
1053 GBytes *message;
1054 const gchar *problem = NULL;
1055 gchar *problem_copy = NULL;
1056
1057 g_return_if_fail (COCKPIT_IS_CHANNEL (self));
1058 g_return_if_fail (command != NULL);
1059
1060 if (g_str_equal (command, "done"))
1061 {
1062 g_return_if_fail (priv->sent_done == FALSE);
1063 priv->sent_done = TRUE;
1064 }
1065
1066 /* If closing save the close options
1067 * and let close send the message */
1068 else if (g_str_equal (command, "close"))
1069 {
1070 if (options)
1071 {
1072 if (!priv->close_options)
1073 {
1074 /* Ref for close_options, freed in parent */
1075 priv->close_options = json_object_ref (options);
1076 }
1077
1078 if (!cockpit_json_get_string (options, "problem", NULL, &problem))
1079 problem = NULL;
1080 }
1081
1082 /* Use a problem copy so it out lasts the value in close_options */
1083 problem_copy = g_strdup (problem);
1084 cockpit_channel_close (self, problem_copy);
1085 goto out;
1086 }
1087
1088 if (options)
1089 object = json_object_ref (options);
1090 else
1091 object = json_object_new ();
1092
1093 json_object_set_string_member (object, "command", command);
1094 json_object_set_string_member (object, "channel", priv->id);
1095
1096 message = cockpit_json_write_bytes (object);
1097 json_object_unref (object);
1098
1099 cockpit_transport_send (priv->transport, NULL, message);
1100 g_bytes_unref (message);
1101
1102 out:
1103 g_free (problem_copy);
1104 }
1105
1106 static void
1107 on_throttle_pressure (GObject *object,
1108 gboolean throttle,
1109 gpointer user_data)
1110 {
1111 CockpitChannel *self = COCKPIT_CHANNEL (user_data);
1112 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
1113 GQueue *throttled;
1114 JsonObject *ping;
1115
1116 if (throttle)
1117 {
1118 if (!priv->throttled)
1119 priv->throttled = g_queue_new ();
1120 }
1121 else
1122 {
1123 throttled = priv->throttled;
1124 priv->throttled = NULL;
1125 while (throttled)
1126 {
1127 ping = g_queue_pop_head (throttled);
1128 if (!ping)
1129 {
1130 g_queue_free (throttled);
1131 throttled = NULL;
1132 }
1133 else
1134 {
1135 if (!process_ping (self, ping))
1136 g_assert_not_reached (); /* Because throttle is FALSE */
1137 json_object_unref (ping);
1138 }
1139 }
1140 }
1141 }
1142
1143 static void
1144 cockpit_channel_throttle (CockpitFlow *flow,
1145 CockpitFlow *controlling)
1146 {
1147 CockpitChannel *self = COCKPIT_CHANNEL (flow);
1148 CockpitChannelPrivate *priv = cockpit_channel_get_instance_private (self);
1149
1150 if (priv->pressure)
1151 {
1152 g_signal_handler_disconnect (priv->pressure, priv->pressure_sig);
1153 g_object_remove_weak_pointer (G_OBJECT (priv->pressure), (gpointer *)&priv->pressure);
1154 priv->pressure = NULL;
1155 }
1156
1157 if (controlling)
1158 {
1159 priv->pressure = controlling;
1160 g_object_add_weak_pointer (G_OBJECT (priv->pressure), (gpointer *)&priv->pressure);
1161 priv->pressure_sig = g_signal_connect (controlling, "pressure", G_CALLBACK (on_throttle_pressure), self);
1162 }
1163 }
1164
1165 static void
1166 cockpit_channel_flow_iface_init (CockpitFlowInterface *iface)
1167 {
1168 iface->throttle = cockpit_channel_throttle;
1169 }