| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
|
| 3 |
/* |
| 4 |
* libqmi-glib -- GLib/GIO based library to control QMI devices |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the |
| 18 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 |
* Boston, MA 02110-1301 USA. |
| 20 |
* |
| 21 |
* Copyright (C) 2013-2017 <Aleksander Morgado <[email protected]> |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <string.h> |
| 25 |
#include <ctype.h> |
| 26 |
#include <sys/file.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <errno.h> |
| 29 |
|
| 30 |
#include <glib.h> |
| 31 |
#include <glib/gstdio.h> |
| 32 |
#include <gio/gunixsocketaddress.h> |
| 33 |
|
| 34 |
#include "config.h" |
| 35 |
#include "qmi-enum-types.h" |
| 36 |
#include "qmi-flag-types.h" |
| 37 |
#include "qmi-error-types.h" |
| 38 |
#include "qmi-device.h" |
| 39 |
#include "qmi-ctl.h" |
| 40 |
#include "qmi-helpers.h" |
| 41 |
#include "qmi-proxy.h" |
| 42 |
#include "qmi-version.h" |
| 43 |
|
| 44 |
#if QMI_QRTR_SUPPORTED |
| 45 |
# include "libqrtr-glib.h" |
| 46 |
#endif |
| 47 |
|
| 48 |
#define BUFFER_SIZE 512 |
| 49 |
|
| 50 |
#define QMI_MESSAGE_OUTPUT_TLV_RESULT 0x02 |
| 51 |
#define QMI_MESSAGE_OUTPUT_TLV_ALLOCATION_INFO 0x01 |
| 52 |
#define QMI_MESSAGE_CTL_ALLOCATE_CID 0x0022 |
| 53 |
#define QMI_MESSAGE_CTL_INTERNAL_ALLOCATE_CID_QRTR 0xFF22 |
| 54 |
|
| 55 |
#define QMI_MESSAGE_INPUT_TLV_RELEASE_INFO 0x01 |
| 56 |
#define QMI_MESSAGE_CTL_RELEASE_CID 0x0023 |
| 57 |
#define QMI_MESSAGE_CTL_INTERNAL_RELEASE_CID_QRTR 0xFF23 |
| 58 |
|
| 59 |
#define QMI_MESSAGE_CTL_INTERNAL_PROXY_OPEN 0xFF00 |
| 60 |
#define QMI_MESSAGE_CTL_INTERNAL_PROXY_OPEN_INPUT_TLV_DEVICE_PATH 0x01 |
| 61 |
|
| 62 |
G_DEFINE_TYPE (QmiProxy, qmi_proxy, G_TYPE_OBJECT) |
| 63 |
|
| 64 |
enum { |
| 65 |
PROP_0, |
| 66 |
PROP_N_CLIENTS, |
| 67 |
PROP_LAST |
| 68 |
}; |
| 69 |
|
| 70 |
static GParamSpec *properties[PROP_LAST]; |
| 71 |
|
| 72 |
struct _QmiProxyPrivate { |
| 73 |
/* Unix socket service */ |
| 74 |
GSocketService *socket_service; |
| 75 |
|
| 76 |
/* Client applications */ |
| 77 |
GList *clients; |
| 78 |
|
| 79 |
/* Devices */ |
| 80 |
GList *devices; |
| 81 |
|
| 82 |
/* Array of QMI client infos that are not owned by any client |
| 83 |
* application (e.g. they were allocated by a client application but |
| 84 |
* then not explicitly released). */ |
| 85 |
GArray *disowned_qmi_client_info_array; |
| 86 |
|
| 87 |
#if QMI_QRTR_SUPPORTED |
| 88 |
QrtrBus *qrtr_bus; |
| 89 |
#endif |
| 90 |
}; |
| 91 |
|
| 92 |
/*****************************************************************************/ |
| 93 |
|
| 94 |
guint |
| 95 |
qmi_proxy_get_n_clients (QmiProxy *self) |
| 96 |
{ |
| 97 |
g_return_val_if_fail (QMI_IS_PROXY (self), 0); |
| 98 |
|
| 99 |
return g_list_length (self->priv->clients); |
| 100 |
} |
| 101 |
|
| 102 |
/*****************************************************************************/ |
| 103 |
|
| 104 |
typedef struct { |
| 105 |
QmiService service; |
| 106 |
guint8 cid; |
| 107 |
} QmiClientInfo; |
| 108 |
|
| 109 |
typedef struct { |
| 110 |
volatile gint ref_count; |
| 111 |
|
| 112 |
QmiProxy *proxy; /* not full ref */ |
| 113 |
|
| 114 |
/* client socket connection context */ |
| 115 |
GSocketConnection *connection; |
| 116 |
GSource *connection_readable_source; |
| 117 |
GByteArray *buffer; |
| 118 |
|
| 119 |
/* QMI device associated to connection */ |
| 120 |
QmiDevice *device; |
| 121 |
QmiMessage *internal_proxy_open_request; |
| 122 |
GArray *qmi_client_info_array; |
| 123 |
guint indication_id; |
| 124 |
guint device_removed_id; |
| 125 |
#if QMI_QRTR_SUPPORTED |
| 126 |
guint node_id; |
| 127 |
#endif |
| 128 |
} Client; |
| 129 |
|
| 130 |
static gboolean connection_readable_cb (GSocket *socket, GIOCondition condition, Client *client); |
| 131 |
static void track_client (QmiProxy *self, Client *client); |
| 132 |
static void untrack_client (QmiProxy *self, Client *client); |
| 133 |
|
| 134 |
static void |
| 135 |
client_disconnect (Client *client) |
| 136 |
{ |
| 137 |
if (client->connection_readable_source) { |
| 138 |
g_source_destroy (client->connection_readable_source); |
| 139 |
g_source_unref (client->connection_readable_source); |
| 140 |
client->connection_readable_source = 0; |
| 141 |
} |
| 142 |
|
| 143 |
if (client->connection) { |
| 144 |
g_debug ("Client (%d) connection closed...", g_socket_get_fd (g_socket_connection_get_socket (client->connection))); |
| 145 |
g_output_stream_close (g_io_stream_get_output_stream (G_IO_STREAM (client->connection)), NULL, NULL); |
| 146 |
g_object_unref (client->connection); |
| 147 |
client->connection = NULL; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
static void |
| 152 |
client_unref (Client *client) |
| 153 |
{ |
| 154 |
if (g_atomic_int_dec_and_test (&client->ref_count)) { |
| 155 |
/* Ensure disconnected */ |
| 156 |
client_disconnect (client); |
| 157 |
|
| 158 |
if (client->device) { |
| 159 |
if (g_signal_handler_is_connected (client->device, client->indication_id)) |
| 160 |
g_signal_handler_disconnect (client->device, client->indication_id); |
| 161 |
if (g_signal_handler_is_connected (client->device, client->device_removed_id)) |
| 162 |
g_signal_handler_disconnect (client->device, client->device_removed_id); |
| 163 |
g_object_unref (client->device); |
| 164 |
} |
| 165 |
|
| 166 |
g_clear_pointer (&client->buffer, g_byte_array_unref); |
| 167 |
g_clear_pointer (&client->internal_proxy_open_request, g_byte_array_unref); |
| 168 |
g_clear_pointer (&client->qmi_client_info_array, g_array_unref); |
| 169 |
|
| 170 |
g_slice_free (Client, client); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC (Client, client_unref) |
| 175 |
|
| 176 |
static Client * |
| 177 |
client_ref (Client *client) |
| 178 |
{ |
| 179 |
g_atomic_int_inc (&client->ref_count); |
| 180 |
return client; |
| 181 |
} |
| 182 |
|
| 183 |
static gboolean |
| 184 |
client_send_message (Client *client, |
| 185 |
QmiMessage *message, |
| 186 |
GError **error) |
| 187 |
{ |
| 188 |
if (!client->connection) { |
| 189 |
g_set_error (error, |
| 190 |
QMI_CORE_ERROR, |
| 191 |
QMI_CORE_ERROR_WRONG_STATE, |
| 192 |
"Cannot send message: not connected"); |
| 193 |
return FALSE; |
| 194 |
} |
| 195 |
|
| 196 |
g_debug ("Client (%d) TX: %u bytes", g_socket_get_fd (g_socket_connection_get_socket (client->connection)), message->len); |
| 197 |
if (!g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (client->connection)), |
| 198 |
message->data, |
| 199 |
message->len, |
| 200 |
NULL, /* bytes_written */ |
| 201 |
NULL, /* cancellable */ |
| 202 |
error)) { |
| 203 |
g_prefix_error (error, "Cannot send message to client: "); |
| 204 |
return FALSE; |
| 205 |
} |
| 206 |
|
| 207 |
return TRUE; |
| 208 |
} |
| 209 |
|
| 210 |
/*****************************************************************************/ |
| 211 |
/* Track/untrack clients */ |
| 212 |
|
| 213 |
static void |
| 214 |
track_client (QmiProxy *self, |
| 215 |
Client *client) |
| 216 |
{ |
| 217 |
self->priv->clients = g_list_append (self->priv->clients, client_ref (client)); |
| 218 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_CLIENTS]); |
| 219 |
} |
| 220 |
|
| 221 |
static void |
| 222 |
disown_not_released_clients (QmiProxy *self, |
| 223 |
Client *client) |
| 224 |
{ |
| 225 |
guint i; |
| 226 |
|
| 227 |
if (!client->qmi_client_info_array || !client->qmi_client_info_array->len) |
| 228 |
return; |
| 229 |
|
| 230 |
for (i = 0; i < client->qmi_client_info_array->len; i++) { |
| 231 |
QmiClientInfo *info; |
| 232 |
|
| 233 |
info = &g_array_index (client->qmi_client_info_array, QmiClientInfo, i); |
| 234 |
g_debug ("QMI client disowned [%s,%s,%u]", |
| 235 |
qmi_device_get_path_display (client->device), |
| 236 |
qmi_service_get_string (info->service), |
| 237 |
info->cid); |
| 238 |
} |
| 239 |
|
| 240 |
if (!self->priv->disowned_qmi_client_info_array) |
| 241 |
self->priv->disowned_qmi_client_info_array = g_steal_pointer (&client->qmi_client_info_array); |
| 242 |
else { |
| 243 |
self->priv->disowned_qmi_client_info_array = g_array_append_vals (self->priv->disowned_qmi_client_info_array, |
| 244 |
client->qmi_client_info_array->data, |
| 245 |
client->qmi_client_info_array->len); |
| 246 |
g_clear_pointer (&client->qmi_client_info_array, g_array_unref); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
static void device_close_if_unused (QmiProxy *self, |
| 251 |
QmiDevice *device); |
| 252 |
|
| 253 |
static void |
| 254 |
untrack_client (QmiProxy *self, |
| 255 |
Client *client) |
| 256 |
{ |
| 257 |
g_autoptr(QmiDevice) device = NULL; |
| 258 |
|
| 259 |
device = client->device ? g_object_ref (client->device) : NULL; |
| 260 |
|
| 261 |
/* Disconnect the client explicitly when untracking */ |
| 262 |
client_disconnect (client); |
| 263 |
|
| 264 |
/* Disown all QMI clients that were not explicitly released */ |
| 265 |
disown_not_released_clients (self, client); |
| 266 |
|
| 267 |
if (g_list_find (self->priv->clients, client)) { |
| 268 |
self->priv->clients = g_list_remove (self->priv->clients, client); |
| 269 |
client_unref (client); |
| 270 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_CLIENTS]); |
| 271 |
} |
| 272 |
|
| 273 |
if (device) |
| 274 |
device_close_if_unused (self, device); |
| 275 |
} |
| 276 |
|
| 277 |
static QmiDevice * |
| 278 |
find_device_for_path (QmiProxy *self, |
| 279 |
const gchar *path) |
| 280 |
{ |
| 281 |
GList *l; |
| 282 |
|
| 283 |
for (l = self->priv->devices; l; l = g_list_next (l)) { |
| 284 |
QmiDevice *device; |
| 285 |
|
| 286 |
device = (QmiDevice *)l->data; |
| 287 |
|
| 288 |
/* Return if found */ |
| 289 |
if (g_str_equal (qmi_device_get_path (device), path)) |
| 290 |
return device; |
| 291 |
} |
| 292 |
|
| 293 |
return NULL; |
| 294 |
} |
| 295 |
|
| 296 |
static void |
| 297 |
complete_internal_proxy_open (QmiProxy *self, |
| 298 |
Client *client) |
| 299 |
{ |
| 300 |
QmiMessage *response; |
| 301 |
GError *error = NULL; |
| 302 |
|
| 303 |
g_debug ("connection to QMI device '%s' established", qmi_device_get_path (client->device)); |
| 304 |
|
| 305 |
g_assert (client->internal_proxy_open_request != NULL); |
| 306 |
response = qmi_message_response_new (client->internal_proxy_open_request, QMI_PROTOCOL_ERROR_NONE); |
| 307 |
qmi_message_unref (client->internal_proxy_open_request); |
| 308 |
client->internal_proxy_open_request = NULL; |
| 309 |
|
| 310 |
if (!client_send_message (client, response, &error)) { |
| 311 |
g_warning ("couldn't send proxy open response to client: %s", error->message); |
| 312 |
g_error_free (error); |
| 313 |
untrack_client (self, client); |
| 314 |
} |
| 315 |
|
| 316 |
qmi_message_unref (response); |
| 317 |
} |
| 318 |
|
| 319 |
static void |
| 320 |
indication_cb (QmiDevice *device, |
| 321 |
QmiMessage *message, |
| 322 |
Client *client) |
| 323 |
{ |
| 324 |
guint i; |
| 325 |
|
| 326 |
if (!client->qmi_client_info_array) |
| 327 |
return; |
| 328 |
|
| 329 |
for (i = 0; i < client->qmi_client_info_array->len; i++) { |
| 330 |
QmiClientInfo *info; |
| 331 |
|
| 332 |
info = &g_array_index (client->qmi_client_info_array, QmiClientInfo, i); |
| 333 |
|
| 334 |
/* If service and CID match; or if service and broadcast, forward to |
| 335 |
* the remote client. This message may therefore be forwarded to multiple |
| 336 |
* clients, all that match the conditions. */ |
| 337 |
if ((qmi_message_get_service (message) == info->service) && |
| 338 |
(qmi_message_get_client_id (message) == info->cid || |
| 339 |
qmi_message_get_client_id (message) == QMI_CID_BROADCAST)) { |
| 340 |
GError *error = NULL; |
| 341 |
|
| 342 |
if (!client_send_message (client, message, &error)) { |
| 343 |
g_warning ("couldn't forward indication to client: %s", error->message); |
| 344 |
g_error_free (error); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
static void |
| 351 |
device_removed_cb (QmiDevice *device, |
| 352 |
Client *client) |
| 353 |
{ |
| 354 |
untrack_client (client->proxy, client); |
| 355 |
} |
| 356 |
|
| 357 |
static void |
| 358 |
register_signal_handlers (Client *client) |
| 359 |
{ |
| 360 |
/* Register for device indications */ |
| 361 |
client->indication_id = g_signal_connect (client->device, |
| 362 |
"indication", |
| 363 |
G_CALLBACK (indication_cb), |
| 364 |
client); |
| 365 |
client->device_removed_id = g_signal_connect (client->device, |
| 366 |
"device-removed", |
| 367 |
G_CALLBACK (device_removed_cb), |
| 368 |
client); |
| 369 |
} |
| 370 |
|
| 371 |
static void |
| 372 |
device_open_ready (QmiDevice *device, |
| 373 |
GAsyncResult *res, |
| 374 |
Client *client) |
| 375 |
{ |
| 376 |
QmiProxy *self = client->proxy; |
| 377 |
QmiDevice *existing; |
| 378 |
GError *error = NULL; |
| 379 |
|
| 380 |
/* Note: we get a full client ref */ |
| 381 |
|
| 382 |
if (!qmi_device_open_finish (device, res, &error)) { |
| 383 |
g_debug ("couldn't open QMI device: %s", error->message); |
| 384 |
g_error_free (error); |
| 385 |
untrack_client (self, client); |
| 386 |
goto out; |
| 387 |
} |
| 388 |
|
| 389 |
/* Store device in the proxy independently */ |
| 390 |
existing = find_device_for_path (self, qmi_device_get_path (client->device)); |
| 391 |
if (existing) { |
| 392 |
/* Race condition, we created two QmiDevices for the same port, just skip ours, no big deal */ |
| 393 |
g_object_unref (client->device); |
| 394 |
client->device = g_object_ref (existing); |
| 395 |
} else { |
| 396 |
/* Keep the newly added device in the proxy */ |
| 397 |
self->priv->devices = g_list_append (self->priv->devices, g_object_ref (client->device)); |
| 398 |
} |
| 399 |
|
| 400 |
register_signal_handlers (client); |
| 401 |
|
| 402 |
complete_internal_proxy_open (self, client); |
| 403 |
|
| 404 |
out: |
| 405 |
/* Balance out the reference we got */ |
| 406 |
client_unref (client); |
| 407 |
} |
| 408 |
|
| 409 |
static void |
| 410 |
device_new_ready (GObject *source, |
| 411 |
GAsyncResult *res, |
| 412 |
Client *client) |
| 413 |
{ |
| 414 |
QmiProxy *self = client->proxy; |
| 415 |
GError *error = NULL; |
| 416 |
|
| 417 |
/* Note: we get a full client ref */ |
| 418 |
|
| 419 |
client->device = qmi_device_new_finish (res, &error); |
| 420 |
if (!client->device) { |
| 421 |
g_debug ("couldn't open QMI device: %s", error->message); |
| 422 |
g_error_free (error); |
| 423 |
untrack_client (self, client); |
| 424 |
goto out; |
| 425 |
} |
| 426 |
|
| 427 |
qmi_device_open (client->device, |
| 428 |
QMI_DEVICE_OPEN_FLAGS_NONE, |
| 429 |
10, |
| 430 |
NULL, |
| 431 |
(GAsyncReadyCallback)device_open_ready, |
| 432 |
client_ref (client)); /* Full ref */ |
| 433 |
|
| 434 |
out: |
| 435 |
/* Balance out the reference we got */ |
| 436 |
client_unref (client); |
| 437 |
} |
| 438 |
|
| 439 |
#if QMI_QRTR_SUPPORTED |
| 440 |
|
| 441 |
static void |
| 442 |
device_from_node (QmiProxy *self, |
| 443 |
Client *client) |
| 444 |
{ |
| 445 |
QrtrNode *node; |
| 446 |
|
| 447 |
g_assert (self->priv->qrtr_bus); |
| 448 |
|
| 449 |
node = qrtr_bus_peek_node (self->priv->qrtr_bus, client->node_id); |
| 450 |
if (!node) { |
| 451 |
g_debug ("node with id %u not found in QRTR bus", client->node_id); |
| 452 |
untrack_client (self, client); |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
qmi_device_new_from_node (node, |
| 457 |
NULL, |
| 458 |
(GAsyncReadyCallback)device_new_ready, |
| 459 |
client_ref (client)); /* full reference */ |
| 460 |
} |
| 461 |
|
| 462 |
static void |
| 463 |
bus_new_ready (GObject *source, |
| 464 |
GAsyncResult *res, |
| 465 |
Client *client) |
| 466 |
{ |
| 467 |
QmiProxy *self; |
| 468 |
g_autoptr(QrtrBus) qrtr_bus = NULL; |
| 469 |
g_autoptr(GError) error = NULL; |
| 470 |
|
| 471 |
/* Note: we get a full client ref */ |
| 472 |
|
| 473 |
self = client->proxy; |
| 474 |
|
| 475 |
qrtr_bus = qrtr_bus_new_finish (res, &error); |
| 476 |
if (!qrtr_bus) { |
| 477 |
g_debug ("couldn't access QRTR bus: %s", error->message); |
| 478 |
untrack_client (self, client); |
| 479 |
client_unref (client); |
| 480 |
return; |
| 481 |
} |
| 482 |
|
| 483 |
/* Don't store the new bus if some other concurrent request already did |
| 484 |
* it. We don't really care which bus object to use, we just need to use |
| 485 |
* always the same */ |
| 486 |
if (!self->priv->qrtr_bus) |
| 487 |
self->priv->qrtr_bus = g_object_ref (qrtr_bus); |
| 488 |
|
| 489 |
device_from_node (self, client); |
| 490 |
client_unref (client); |
| 491 |
} |
| 492 |
|
| 493 |
#endif |
| 494 |
|
| 495 |
static gboolean |
| 496 |
process_internal_proxy_open (QmiProxy *self, |
| 497 |
Client *client, |
| 498 |
QmiMessage *message) |
| 499 |
{ |
| 500 |
gsize offset = 0; |
| 501 |
gsize init_offset; |
| 502 |
g_autofree gchar *incoming_path = NULL; |
| 503 |
g_autofree gchar *device_file_path = NULL; |
| 504 |
g_autoptr(GError) error = NULL; |
| 505 |
|
| 506 |
if ((init_offset = qmi_message_tlv_read_init (message, QMI_MESSAGE_CTL_INTERNAL_PROXY_OPEN_INPUT_TLV_DEVICE_PATH, NULL, &error)) == 0) { |
| 507 |
g_debug ("ignoring message from client: invalid proxy open request: %s", error->message); |
| 508 |
return FALSE; |
| 509 |
} |
| 510 |
|
| 511 |
if (!qmi_message_tlv_read_string (message, init_offset, &offset, 0, 0, &incoming_path, &error)) { |
| 512 |
g_debug ("ignoring message from client: invalid device file path: %s", error->message); |
| 513 |
return FALSE; |
| 514 |
} |
| 515 |
|
| 516 |
/* The incoming path may be a symlink. In the proxy, we always use the real path of the |
| 517 |
* device, so that clients using different symlinks for the same file don't collide with |
| 518 |
* each other. */ |
| 519 |
device_file_path = qmi_helpers_get_devpath (incoming_path, &error); |
| 520 |
if (!device_file_path) { |
| 521 |
g_warning ("Error looking up real device path: %s", error->message); |
| 522 |
return FALSE; |
| 523 |
} |
| 524 |
|
| 525 |
/* The remaining size of the buffer needs to be 0 if we successfully read the TLV */ |
| 526 |
if ((offset = qmi_message_tlv_read_remaining_size (message, init_offset, offset)) > 0) |
| 527 |
g_warning ("Left '%" G_GSIZE_FORMAT "' bytes unread when getting the 'Device Path' TLV", offset); |
| 528 |
|
| 529 |
g_debug ("valid request to open connection to QMI device file: %s", device_file_path); |
| 530 |
|
| 531 |
/* Keep it */ |
| 532 |
client->internal_proxy_open_request = qmi_message_ref (message); |
| 533 |
|
| 534 |
client->device = find_device_for_path (self, device_file_path); |
| 535 |
|
| 536 |
/* Need to create a device ourselves */ |
| 537 |
if (!client->device) { |
| 538 |
#if QMI_QRTR_SUPPORTED |
| 539 |
if (qrtr_get_node_for_uri (device_file_path, &client->node_id)) { |
| 540 |
if (!self->priv->qrtr_bus) { |
| 541 |
qrtr_bus_new (1000, /* ms */ |
| 542 |
NULL, |
| 543 |
(GAsyncReadyCallback)bus_new_ready, |
| 544 |
client_ref (client)); /* Full ref */ |
| 545 |
return TRUE; |
| 546 |
} |
| 547 |
|
| 548 |
device_from_node (self, client); |
| 549 |
return TRUE; |
| 550 |
} |
| 551 |
#endif |
| 552 |
{ |
| 553 |
g_autoptr(GFile) file = NULL; |
| 554 |
|
| 555 |
file = g_file_new_for_path (device_file_path); |
| 556 |
qmi_device_new (file, |
| 557 |
NULL, |
| 558 |
(GAsyncReadyCallback)device_new_ready, |
| 559 |
client_ref (client)); /* Full ref */ |
| 560 |
return TRUE; |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
register_signal_handlers (client); |
| 565 |
|
| 566 |
/* Keep a reference to the device in the client */ |
| 567 |
g_object_ref (client->device); |
| 568 |
|
| 569 |
complete_internal_proxy_open (self, client); |
| 570 |
return FALSE; |
| 571 |
} |
| 572 |
|
| 573 |
static gint |
| 574 |
qmi_client_info_array_lookup_cid (GArray *array, |
| 575 |
QmiService service, |
| 576 |
guint8 cid) |
| 577 |
{ |
| 578 |
guint i; |
| 579 |
|
| 580 |
if (!array) |
| 581 |
return -1; |
| 582 |
|
| 583 |
for (i = 0; i < array->len; i++) { |
| 584 |
QmiClientInfo *item; |
| 585 |
|
| 586 |
item = &g_array_index (array, QmiClientInfo, i); |
| 587 |
if (item->service == service && item->cid == cid) |
| 588 |
return (gint)i; |
| 589 |
} |
| 590 |
return -1; |
| 591 |
} |
| 592 |
|
| 593 |
static void |
| 594 |
track_cid (Client *client, |
| 595 |
QmiMessage *message) |
| 596 |
{ |
| 597 |
g_autoptr(GError) error = NULL; |
| 598 |
gsize offset = 0; |
| 599 |
gsize init_offset; |
| 600 |
guint16 error_status; |
| 601 |
guint16 error_code; |
| 602 |
QmiClientInfo info; |
| 603 |
gint i; |
| 604 |
|
| 605 |
g_assert_cmpuint (qmi_message_get_service (message), ==, QMI_SERVICE_CTL); |
| 606 |
g_assert (qmi_message_is_response (message)); |
| 607 |
|
| 608 |
if (((init_offset = qmi_message_tlv_read_init (message, QMI_MESSAGE_OUTPUT_TLV_RESULT, NULL, &error)) == 0) || |
| 609 |
!qmi_message_tlv_read_guint16 (message, init_offset, &offset, QMI_ENDIAN_LITTLE, &error_status, &error) || |
| 610 |
!qmi_message_tlv_read_guint16 (message, init_offset, &offset, QMI_ENDIAN_LITTLE, &error_code, &error)) { |
| 611 |
g_warning ("invalid 'CTL allocate CID' response: missing or invalid result TLV: %s", error->message); |
| 612 |
return; |
| 613 |
} |
| 614 |
g_warn_if_fail (qmi_message_tlv_read_remaining_size (message, init_offset, offset) == 0); |
| 615 |
if ((error_status != 0x00) || (error_code != QMI_PROTOCOL_ERROR_NONE)) |
| 616 |
return; |
| 617 |
|
| 618 |
offset = 0; |
| 619 |
if ((init_offset = qmi_message_tlv_read_init (message, QMI_MESSAGE_OUTPUT_TLV_ALLOCATION_INFO, NULL, &error)) == 0) { |
| 620 |
g_warning ("invalid 'CTL allocate CID' response: missing allocation info TLV: %s", error->message); |
| 621 |
return; |
| 622 |
} |
| 623 |
|
| 624 |
if (qmi_message_get_message_id (message) == QMI_MESSAGE_CTL_ALLOCATE_CID) { |
| 625 |
guint8 service_tmp; |
| 626 |
|
| 627 |
if (!qmi_message_tlv_read_guint8 (message, init_offset, &offset, &service_tmp, &error)) { |
| 628 |
g_warning ("invalid 'CTL allocate CID' request: failed to read service: %s", error->message); |
| 629 |
return; |
| 630 |
} |
| 631 |
info.service = (QmiService)service_tmp; |
| 632 |
} else if (qmi_message_get_message_id (message) == QMI_MESSAGE_CTL_INTERNAL_ALLOCATE_CID_QRTR) { |
| 633 |
guint16 service_tmp; |
| 634 |
|
| 635 |
if (!qmi_message_tlv_read_guint16 (message, init_offset, &offset, QMI_ENDIAN_LITTLE, &service_tmp, &error)) { |
| 636 |
g_warning ("invalid 'CTL allocate CID QRTR' request: failed to read service: %s", error->message); |
| 637 |
return; |
| 638 |
} |
| 639 |
info.service = (QmiService)service_tmp; |
| 640 |
} |
| 641 |
|
| 642 |
if (!qmi_message_tlv_read_guint8 (message, init_offset, &offset, &(info.cid), &error)) { |
| 643 |
g_warning ("invalid 'CTL allocate CID' request: failed to read client id: %s", error->message); |
| 644 |
return; |
| 645 |
} |
| 646 |
|
| 647 |
/* Check if it already exists */ |
| 648 |
i = qmi_client_info_array_lookup_cid (client->qmi_client_info_array, info.service, info.cid); |
| 649 |
if (i < 0) { |
| 650 |
g_debug ("QMI client tracked [%s,%s,%u]", |
| 651 |
qmi_device_get_path_display (client->device), |
| 652 |
qmi_service_get_string (info.service), |
| 653 |
info.cid); |
| 654 |
g_array_append_val (client->qmi_client_info_array, info); |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
static void |
| 659 |
untrack_cid (QmiProxy *self, |
| 660 |
Client *client, |
| 661 |
QmiMessage *message) |
| 662 |
{ |
| 663 |
g_autoptr(GError) error = NULL; |
| 664 |
gsize offset = 0; |
| 665 |
gsize init_offset; |
| 666 |
QmiClientInfo info; |
| 667 |
gint i; |
| 668 |
|
| 669 |
g_assert_cmpuint (qmi_message_get_service (message), ==, QMI_SERVICE_CTL); |
| 670 |
g_assert (qmi_message_is_request (message)); |
| 671 |
|
| 672 |
if ((init_offset = qmi_message_tlv_read_init (message, QMI_MESSAGE_INPUT_TLV_RELEASE_INFO, NULL, &error)) == 0) { |
| 673 |
g_warning ("invalid 'CTL release CID' request: missing release info TLV: %s", error->message); |
| 674 |
return; |
| 675 |
} |
| 676 |
|
| 677 |
if (qmi_message_get_message_id (message) == QMI_MESSAGE_CTL_RELEASE_CID) { |
| 678 |
guint8 service_tmp; |
| 679 |
|
| 680 |
if (!qmi_message_tlv_read_guint8 (message, init_offset, &offset, &service_tmp, &error)) { |
| 681 |
g_warning ("invalid 'CTL release CID' request: failed to read service: %s", error->message); |
| 682 |
return; |
| 683 |
} |
| 684 |
info.service = (QmiService)service_tmp; |
| 685 |
} else if (qmi_message_get_message_id (message) == QMI_MESSAGE_CTL_INTERNAL_RELEASE_CID_QRTR) { |
| 686 |
guint16 service_tmp; |
| 687 |
|
| 688 |
if (!qmi_message_tlv_read_guint16 (message, init_offset, &offset, QMI_ENDIAN_LITTLE, &service_tmp, &error)) { |
| 689 |
g_warning ("invalid 'CTL release CID QRTR' request: failed to read service: %s", error->message); |
| 690 |
return; |
| 691 |
} |
| 692 |
info.service = (QmiService)service_tmp; |
| 693 |
} else |
| 694 |
g_assert_not_reached (); |
| 695 |
|
| 696 |
if (!qmi_message_tlv_read_guint8 (message, init_offset, &offset, &(info.cid), &error)) { |
| 697 |
g_warning ("invalid 'CTL release CID' request: failed to read client id: %s", error->message); |
| 698 |
return; |
| 699 |
} |
| 700 |
|
| 701 |
/* Check if it already exists in the client */ |
| 702 |
i = qmi_client_info_array_lookup_cid (client->qmi_client_info_array, info.service, info.cid); |
| 703 |
if (i >= 0) { |
| 704 |
g_debug ("QMI client untracked [%s,%s,%u]", |
| 705 |
qmi_device_get_path_display (client->device), |
| 706 |
qmi_service_get_string (info.service), |
| 707 |
info.cid); |
| 708 |
g_array_remove_index (client->qmi_client_info_array, i); |
| 709 |
return; |
| 710 |
} |
| 711 |
|
| 712 |
/* Otherwise, check if it wasn't onwned */ |
| 713 |
i = qmi_client_info_array_lookup_cid (self->priv->disowned_qmi_client_info_array, info.service, info.cid); |
| 714 |
if (i >= 0) { |
| 715 |
g_debug ("disowned QMI client untracked [%s,%s,%u]", |
| 716 |
qmi_device_get_path_display (client->device), |
| 717 |
qmi_service_get_string (info.service), |
| 718 |
info.cid); |
| 719 |
g_array_remove_index (self->priv->disowned_qmi_client_info_array, i); |
| 720 |
return; |
| 721 |
} |
| 722 |
|
| 723 |
g_debug ("unexpected attempt to release QMI client [%s,%s,%u]", |
| 724 |
qmi_device_get_path_display (client->device), |
| 725 |
qmi_service_get_string (info.service), |
| 726 |
info.cid); |
| 727 |
} |
| 728 |
|
| 729 |
static void |
| 730 |
track_implicit_cid (QmiProxy *self, |
| 731 |
Client *client, |
| 732 |
QmiMessage *message) |
| 733 |
{ |
| 734 |
QmiClientInfo info; |
| 735 |
gint i; |
| 736 |
|
| 737 |
info.service = qmi_message_get_service (message); |
| 738 |
info.cid = qmi_message_get_client_id (message); |
| 739 |
|
| 740 |
g_assert_cmpuint (info.service, !=, QMI_SERVICE_CTL); |
| 741 |
|
| 742 |
/* Check if the QMI client already exists in the client application, and if |
| 743 |
* so, nothing else to do */ |
| 744 |
i = qmi_client_info_array_lookup_cid (client->qmi_client_info_array, info.service, info.cid); |
| 745 |
if (i >= 0) |
| 746 |
return; |
| 747 |
|
| 748 |
/* The QMI client doesn't exist in the client application, see if it |
| 749 |
* was disowned previously */ |
| 750 |
i = qmi_client_info_array_lookup_cid (self->priv->disowned_qmi_client_info_array, info.service, info.cid); |
| 751 |
if (i >= 0) { |
| 752 |
/* Remove client info from array of disowned ones, and append it to the client */ |
| 753 |
g_debug ("QMI client reowned [%s,%s,%u]", |
| 754 |
qmi_device_get_path_display (client->device), |
| 755 |
qmi_service_get_string (info.service), |
| 756 |
info.cid); |
| 757 |
g_array_remove_index (self->priv->disowned_qmi_client_info_array, i); |
| 758 |
g_array_append_val (client->qmi_client_info_array, info); |
| 759 |
return; |
| 760 |
} |
| 761 |
|
| 762 |
/* The QMI client wasn't disowned earlier either. Well, this could be due to the proxy |
| 763 |
* having crashed and restarted. Just create a new client info from scratch. */ |
| 764 |
g_debug ("QMI client tracked implicitly [%s,%s,%u]", |
| 765 |
qmi_device_get_path_display (client->device), |
| 766 |
qmi_service_get_string (info.service), |
| 767 |
info.cid); |
| 768 |
g_array_append_val (client->qmi_client_info_array, info); |
| 769 |
} |
| 770 |
|
| 771 |
/*****************************************************************************/ |
| 772 |
|
| 773 |
#define TRACK_CTL_QUARK_STR "track-ctl-data" |
| 774 |
static GQuark track_ctl_quark; |
| 775 |
|
| 776 |
static void |
| 777 |
device_track_ctl_request (QmiDevice *device) |
| 778 |
{ |
| 779 |
guint ongoing_ctl; |
| 780 |
|
| 781 |
if (G_UNLIKELY (!track_ctl_quark)) { |
| 782 |
track_ctl_quark = g_quark_from_static_string (TRACK_CTL_QUARK_STR); |
| 783 |
ongoing_ctl = 0; |
| 784 |
} else |
| 785 |
ongoing_ctl = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (device), track_ctl_quark)); |
| 786 |
g_object_set_qdata (G_OBJECT (device), track_ctl_quark, GUINT_TO_POINTER (ongoing_ctl + 1)); |
| 787 |
} |
| 788 |
|
| 789 |
static void |
| 790 |
device_untrack_ctl_request (QmiDevice *device) |
| 791 |
{ |
| 792 |
guint ongoing_ctl; |
| 793 |
|
| 794 |
g_assert (track_ctl_quark != 0); |
| 795 |
ongoing_ctl = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (device), track_ctl_quark)); |
| 796 |
g_assert_cmpuint (ongoing_ctl, >, 0); |
| 797 |
g_object_set_qdata (G_OBJECT (device), track_ctl_quark, GUINT_TO_POINTER (ongoing_ctl - 1)); |
| 798 |
} |
| 799 |
|
| 800 |
static void |
| 801 |
device_close_if_unused (QmiProxy *self, |
| 802 |
QmiDevice *device) |
| 803 |
{ |
| 804 |
GList *l; |
| 805 |
|
| 806 |
/* If there is at least one client using the device, |
| 807 |
* no need to close */ |
| 808 |
for (l = self->priv->clients; l; l = g_list_next (l)) { |
| 809 |
Client *client = l->data; |
| 810 |
|
| 811 |
if (client->device && |
| 812 |
(device == client->device || |
| 813 |
g_str_equal (qmi_device_get_path (device), qmi_device_get_path (client->device)))) |
| 814 |
return; |
| 815 |
} |
| 816 |
|
| 817 |
/* If there are no clients using the device BUT there |
| 818 |
* are still ongoing CTL requests ongoing, no need to |
| 819 |
* close */ |
| 820 |
if (GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (device), track_ctl_quark)) > 0) |
| 821 |
return; |
| 822 |
|
| 823 |
/* Now, untrack device from proxy and close it */ |
| 824 |
for (l = self->priv->devices; l; l = g_list_next (l)) { |
| 825 |
QmiDevice *device_in_list = QMI_DEVICE (l->data); |
| 826 |
|
| 827 |
if (device_in_list && |
| 828 |
(device == device_in_list || |
| 829 |
g_str_equal (qmi_device_get_path (device), qmi_device_get_path (device_in_list)))) { |
| 830 |
g_debug ("closing device '%s': no longer used", qmi_device_get_path_display (device)); |
| 831 |
qmi_device_close_async (device_in_list, 0, NULL, NULL, NULL); |
| 832 |
g_object_unref (device_in_list); |
| 833 |
self->priv->devices = g_list_remove (self->priv->devices, device_in_list); |
| 834 |
return; |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
/*****************************************************************************/ |
| 840 |
|
| 841 |
typedef struct { |
| 842 |
QmiProxy *self; /* Full ref */ |
| 843 |
Client *client; /* Full ref */ |
| 844 |
guint8 in_trid; |
| 845 |
gboolean ctl; |
| 846 |
} Request; |
| 847 |
|
| 848 |
static void |
| 849 |
request_free (Request *request) |
| 850 |
{ |
| 851 |
if (!request) |
| 852 |
return; |
| 853 |
client_unref (request->client); |
| 854 |
g_object_unref (request->self); |
| 855 |
g_slice_free (Request, request); |
| 856 |
} |
| 857 |
|
| 858 |
static void |
| 859 |
device_command_ready (QmiDevice *device, |
| 860 |
GAsyncResult *res, |
| 861 |
Request *request) |
| 862 |
{ |
| 863 |
g_autoptr(QmiMessage) response = NULL; |
| 864 |
g_autoptr(GError) error = NULL; |
| 865 |
|
| 866 |
response = qmi_device_command_full_finish (device, res, &error); |
| 867 |
if (!response) { |
| 868 |
g_warning ("sending request to device failed: %s", error->message); |
| 869 |
goto out; |
| 870 |
} |
| 871 |
|
| 872 |
if (qmi_message_get_service (response) == QMI_SERVICE_CTL) { |
| 873 |
qmi_message_set_transaction_id (response, request->in_trid); |
| 874 |
if (qmi_message_get_message_id (response) == QMI_MESSAGE_CTL_ALLOCATE_CID || |
| 875 |
qmi_message_get_message_id (response) == QMI_MESSAGE_CTL_INTERNAL_ALLOCATE_CID_QRTR) |
| 876 |
track_cid (request->client, response); |
| 877 |
} |
| 878 |
|
| 879 |
if (!client_send_message (request->client, response, &error)) { |
| 880 |
/* ignore errors when client is not connected, because it really didn't |
| 881 |
* need this response back */ |
| 882 |
if (!g_error_matches (error, QMI_CORE_ERROR, QMI_CORE_ERROR_WRONG_STATE)) |
| 883 |
g_warning ("forwarding response to client failed: %s", error->message); |
| 884 |
untrack_client (request->self, request->client); |
| 885 |
} |
| 886 |
|
| 887 |
out: |
| 888 |
if (request->ctl) { |
| 889 |
device_untrack_ctl_request (device); |
| 890 |
device_close_if_unused (request->self, device); |
| 891 |
} |
| 892 |
request_free (request); |
| 893 |
} |
| 894 |
|
| 895 |
static gboolean |
| 896 |
process_message (QmiProxy *self, |
| 897 |
Client *client, |
| 898 |
QmiMessage *message) |
| 899 |
{ |
| 900 |
Request *request; |
| 901 |
|
| 902 |
/* Accept only request messages from the client */ |
| 903 |
if (!qmi_message_is_request (message)) { |
| 904 |
g_debug ("invalid message from client: not a request message"); |
| 905 |
return FALSE; |
| 906 |
} |
| 907 |
|
| 908 |
if (qmi_message_get_service (message) == QMI_SERVICE_CTL && |
| 909 |
qmi_message_get_message_id (message) == QMI_MESSAGE_CTL_INTERNAL_PROXY_OPEN) |
| 910 |
return process_internal_proxy_open (self, client, message); |
| 911 |
|
| 912 |
request = g_slice_new0 (Request); |
| 913 |
request->self = g_object_ref (self); |
| 914 |
request->client = client_ref (client); |
| 915 |
|
| 916 |
if (qmi_message_get_service (message) == QMI_SERVICE_CTL) { |
| 917 |
/* Keep track of how many CTL requests are ongoing */ |
| 918 |
device_track_ctl_request (client->device); |
| 919 |
request->ctl = TRUE; |
| 920 |
request->in_trid = qmi_message_get_transaction_id (message); |
| 921 |
qmi_message_set_transaction_id (message, 0); |
| 922 |
/* Try to untrack QMI client as soon as we detect the associated |
| 923 |
* release message, no need to wait for the response. */ |
| 924 |
if (qmi_message_get_message_id (message) == QMI_MESSAGE_CTL_RELEASE_CID || |
| 925 |
qmi_message_get_message_id (message) == QMI_MESSAGE_CTL_INTERNAL_RELEASE_CID_QRTR) |
| 926 |
untrack_cid (self, client, message); |
| 927 |
} else |
| 928 |
track_implicit_cid (self, client, message); |
| 929 |
|
| 930 |
/* The timeout needs to be big enough for any kind of transaction to |
| 931 |
* complete, otherwise the remote clients will lose the reply if they |
| 932 |
* configured a timeout bigger than this internal one. We should likely |
| 933 |
* make this value configurable per-client, instead of a hardcoded value. |
| 934 |
* |
| 935 |
* Note: the proxy will not translate vendor-specific messages in its |
| 936 |
* logs (as it doesn't have the original message context with the vendor |
| 937 |
* id). |
| 938 |
*/ |
| 939 |
qmi_device_command_full (client->device, |
| 940 |
message, |
| 941 |
NULL, |
| 942 |
300, |
| 943 |
NULL, |
| 944 |
(GAsyncReadyCallback)device_command_ready, |
| 945 |
request); |
| 946 |
return TRUE; |
| 947 |
} |
| 948 |
|
| 949 |
static void |
| 950 |
parse_request (QmiProxy *self, |
| 951 |
Client *client) |
| 952 |
{ |
| 953 |
do { |
| 954 |
GError *error = NULL; |
| 955 |
QmiMessage *message; |
| 956 |
|
| 957 |
/* Every message received must start with the QMUX marker. |
| 958 |
* If it doesn't, we broke framing :-/ |
| 959 |
* If we broke framing, an error should be reported and the device |
| 960 |
* should get closed */ |
| 961 |
if (client->buffer->len > 0 && |
| 962 |
client->buffer->data[0] != QMI_MESSAGE_QMUX_MARKER && |
| 963 |
client->buffer->data[0] != QMI_MESSAGE_QRTR_MARKER) { |
| 964 |
/* TODO: Report fatal error */ |
| 965 |
g_warning ("QMI framing error detected"); |
| 966 |
return; |
| 967 |
} |
| 968 |
|
| 969 |
message = qmi_message_new_from_raw (client->buffer, &error); |
| 970 |
if (!message) { |
| 971 |
if (!error) |
| 972 |
/* More data we need */ |
| 973 |
return; |
| 974 |
|
| 975 |
/* Warn about the issue */ |
| 976 |
g_warning ("Invalid QMI message received: '%s'", |
| 977 |
error->message); |
| 978 |
g_error_free (error); |
| 979 |
} else { |
| 980 |
/* Play with the received message */ |
| 981 |
process_message (self, client, message); |
| 982 |
qmi_message_unref (message); |
| 983 |
} |
| 984 |
} while (client->buffer->len > 0); |
| 985 |
} |
| 986 |
|
| 987 |
static gboolean |
| 988 |
connection_readable_cb (GSocket *socket, |
| 989 |
GIOCondition condition, |
| 990 |
Client *_client) |
| 991 |
{ |
| 992 |
g_autoptr(Client) client = NULL; |
| 993 |
QmiProxy *self; |
| 994 |
guint8 buffer[BUFFER_SIZE]; |
| 995 |
GError *error = NULL; |
| 996 |
gssize r; |
| 997 |
|
| 998 |
client = client_ref (_client); |
| 999 |
self = client->proxy; |
| 1000 |
|
| 1001 |
if (condition & G_IO_IN || condition & G_IO_PRI) { |
| 1002 |
r = g_input_stream_read (g_io_stream_get_input_stream (G_IO_STREAM (client->connection)), |
| 1003 |
buffer, |
| 1004 |
BUFFER_SIZE, |
| 1005 |
NULL, |
| 1006 |
&error); |
| 1007 |
if (r < 0) { |
| 1008 |
g_warning ("Error reading from istream: %s", error ? error->message : "unknown"); |
| 1009 |
if (error) |
| 1010 |
g_error_free (error); |
| 1011 |
untrack_client (self, client); |
| 1012 |
return FALSE; |
| 1013 |
} |
| 1014 |
|
| 1015 |
if (r > 0) { |
| 1016 |
if (!G_UNLIKELY (client->buffer)) |
| 1017 |
client->buffer = g_byte_array_sized_new (r); |
| 1018 |
g_byte_array_append (client->buffer, buffer, r); |
| 1019 |
|
| 1020 |
/* Try to parse input messages */ |
| 1021 |
parse_request (self, client); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
if (condition & G_IO_HUP || condition & G_IO_ERR) { |
| 1026 |
untrack_client (self, client); |
| 1027 |
return FALSE; |
| 1028 |
} |
| 1029 |
|
| 1030 |
return TRUE; |
| 1031 |
} |
| 1032 |
|
| 1033 |
static void |
| 1034 |
incoming_cb (GSocketService *service, |
| 1035 |
GSocketConnection *connection, |
| 1036 |
GObject *unused, |
| 1037 |
QmiProxy *self) |
| 1038 |
{ |
| 1039 |
Client *client; |
| 1040 |
g_autoptr(GCredentials) credentials = NULL; |
| 1041 |
g_autoptr(GError) error = NULL; |
| 1042 |
g_autoptr(GError) gr_error = NULL; |
| 1043 |
uid_t uid; |
| 1044 |
|
| 1045 |
g_debug ("Client (%d) connection open...", g_socket_get_fd (g_socket_connection_get_socket (connection))); |
| 1046 |
|
| 1047 |
credentials = g_socket_get_credentials (g_socket_connection_get_socket (connection), &error); |
| 1048 |
if (!credentials) { |
| 1049 |
g_warning ("Client not allowed: Error getting socket credentials: %s", error->message); |
| 1050 |
return; |
| 1051 |
} |
| 1052 |
|
| 1053 |
uid = g_credentials_get_unix_user (credentials, &error); |
| 1054 |
if (error) { |
| 1055 |
g_warning ("Client not allowed: Error getting unix user id: %s", error->message); |
| 1056 |
return; |
| 1057 |
} |
| 1058 |
if (!qmi_helpers_check_user_allowed (uid, &error)) { |
| 1059 |
if (!qmi_helpers_check_group_allowed (uid, &gr_error)) { |
| 1060 |
if (gr_error) |
| 1061 |
g_warning ("Client group not allowed: %s", gr_error->message); |
| 1062 |
else |
| 1063 |
g_warning ("Client not allowed: %s", error->message); |
| 1064 |
return; |
| 1065 |
} |
| 1066 |
g_debug ("Client member of allowed group"); |
| 1067 |
} |
| 1068 |
|
| 1069 |
/* Create client */ |
| 1070 |
client = g_slice_new0 (Client); |
| 1071 |
client->ref_count = 1; |
| 1072 |
client->proxy = self; |
| 1073 |
client->connection = g_object_ref (connection); |
| 1074 |
client->connection_readable_source = g_socket_create_source (g_socket_connection_get_socket (client->connection), |
| 1075 |
G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, |
| 1076 |
NULL); |
| 1077 |
g_source_set_callback (client->connection_readable_source, |
| 1078 |
(GSourceFunc)connection_readable_cb, |
| 1079 |
client, |
| 1080 |
NULL); |
| 1081 |
g_source_attach (client->connection_readable_source, g_main_context_get_thread_default ()); |
| 1082 |
client->qmi_client_info_array = g_array_sized_new (FALSE, FALSE, sizeof (QmiClientInfo), 8); |
| 1083 |
|
| 1084 |
/* Keep the client info around */ |
| 1085 |
track_client (self, client); |
| 1086 |
|
| 1087 |
client_unref (client); |
| 1088 |
} |
| 1089 |
|
| 1090 |
static gboolean |
| 1091 |
setup_socket_service (QmiProxy *self, |
| 1092 |
GError **error) |
| 1093 |
{ |
| 1094 |
GSocketAddress *socket_address; |
| 1095 |
GSocket *socket; |
| 1096 |
|
| 1097 |
socket = g_socket_new (G_SOCKET_FAMILY_UNIX, |
| 1098 |
G_SOCKET_TYPE_STREAM, |
| 1099 |
G_SOCKET_PROTOCOL_DEFAULT, |
| 1100 |
error); |
| 1101 |
if (!socket) |
| 1102 |
return FALSE; |
| 1103 |
|
| 1104 |
/* Bind to address */ |
| 1105 |
socket_address = (g_unix_socket_address_new_with_type ( |
| 1106 |
QMI_PROXY_SOCKET_PATH, |
| 1107 |
-1, |
| 1108 |
G_UNIX_SOCKET_ADDRESS_ABSTRACT)); |
| 1109 |
if (!g_socket_bind (socket, socket_address, TRUE, error)) |
| 1110 |
return FALSE; |
| 1111 |
g_object_unref (socket_address); |
| 1112 |
|
| 1113 |
g_debug ("creating UNIX socket service..."); |
| 1114 |
|
| 1115 |
/* Listen */ |
| 1116 |
if (!g_socket_listen (socket, error)) { |
| 1117 |
g_object_unref (socket); |
| 1118 |
return FALSE; |
| 1119 |
} |
| 1120 |
|
| 1121 |
/* Create socket service */ |
| 1122 |
self->priv->socket_service = g_socket_service_new (); |
| 1123 |
g_signal_connect (self->priv->socket_service, "incoming", G_CALLBACK (incoming_cb), self); |
| 1124 |
if (!g_socket_listener_add_socket (G_SOCKET_LISTENER (self->priv->socket_service), |
| 1125 |
socket, |
| 1126 |
NULL, /* don't pass an object, will take a reference */ |
| 1127 |
error)) { |
| 1128 |
g_prefix_error (error, "Error adding socket at '%s' to socket service: ", QMI_PROXY_SOCKET_PATH); |
| 1129 |
g_object_unref (socket); |
| 1130 |
return FALSE; |
| 1131 |
} |
| 1132 |
|
| 1133 |
g_debug ("starting UNIX socket service at '%s'...", QMI_PROXY_SOCKET_PATH); |
| 1134 |
g_socket_service_start (self->priv->socket_service); |
| 1135 |
g_object_unref (socket); |
| 1136 |
return TRUE; |
| 1137 |
} |
| 1138 |
|
| 1139 |
/*****************************************************************************/ |
| 1140 |
|
| 1141 |
QmiProxy * |
| 1142 |
qmi_proxy_new (GError **error) |
| 1143 |
{ |
| 1144 |
QmiProxy *self; |
| 1145 |
uid_t uid; |
| 1146 |
|
| 1147 |
uid = getuid (); |
| 1148 |
|
| 1149 |
/* Allow proxy creation if one of [uid, group] are allowed */ |
| 1150 |
if (!qmi_helpers_check_user_allowed (uid, error)) { |
| 1151 |
g_clear_error (error); |
| 1152 |
|
| 1153 |
if (!qmi_helpers_check_group_allowed (uid, error)) |
| 1154 |
return NULL; |
| 1155 |
} |
| 1156 |
|
| 1157 |
self = g_object_new (QMI_TYPE_PROXY, NULL); |
| 1158 |
if (!setup_socket_service (self, error)) |
| 1159 |
g_clear_object (&self); |
| 1160 |
return self; |
| 1161 |
} |
| 1162 |
|
| 1163 |
static void |
| 1164 |
qmi_proxy_init (QmiProxy *self) |
| 1165 |
{ |
| 1166 |
/* Setup private data */ |
| 1167 |
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, |
| 1168 |
QMI_TYPE_PROXY, |
| 1169 |
QmiProxyPrivate); |
| 1170 |
} |
| 1171 |
|
| 1172 |
static void |
| 1173 |
get_property (GObject *object, |
| 1174 |
guint prop_id, |
| 1175 |
GValue *value, |
| 1176 |
GParamSpec *pspec) |
| 1177 |
{ |
| 1178 |
QmiProxy *self = QMI_PROXY (object); |
| 1179 |
|
| 1180 |
switch (prop_id) { |
| 1181 |
case PROP_N_CLIENTS: |
| 1182 |
g_value_set_uint (value, g_list_length (self->priv->clients)); |
| 1183 |
break; |
| 1184 |
default: |
| 1185 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 1186 |
break; |
| 1187 |
} |
| 1188 |
} |
| 1189 |
|
| 1190 |
static void |
| 1191 |
dispose (GObject *object) |
| 1192 |
{ |
| 1193 |
QmiProxyPrivate *priv = QMI_PROXY (object)->priv; |
| 1194 |
|
| 1195 |
g_clear_pointer (&priv->disowned_qmi_client_info_array, g_array_unref); |
| 1196 |
g_list_free_full (g_steal_pointer (&priv->clients), (GDestroyNotify) client_unref); |
| 1197 |
|
| 1198 |
if (priv->socket_service) { |
| 1199 |
if (g_socket_service_is_active (priv->socket_service)) |
| 1200 |
g_socket_service_stop (priv->socket_service); |
| 1201 |
g_clear_object (&priv->socket_service); |
| 1202 |
g_debug ("UNIX socket service at '%s' stopped", QMI_PROXY_SOCKET_PATH); |
| 1203 |
} |
| 1204 |
|
| 1205 |
#if QMI_QRTR_SUPPORTED |
| 1206 |
g_clear_object (&priv->qrtr_bus); |
| 1207 |
#endif |
| 1208 |
|
| 1209 |
G_OBJECT_CLASS (qmi_proxy_parent_class)->dispose (object); |
| 1210 |
} |
| 1211 |
|
| 1212 |
static void |
| 1213 |
qmi_proxy_class_init (QmiProxyClass *proxy_class) |
| 1214 |
{ |
| 1215 |
GObjectClass *object_class = G_OBJECT_CLASS (proxy_class); |
| 1216 |
|
| 1217 |
g_type_class_add_private (object_class, sizeof (QmiProxyPrivate)); |
| 1218 |
|
| 1219 |
object_class->get_property = get_property; |
| 1220 |
object_class->dispose = dispose; |
| 1221 |
|
| 1222 |
/** |
| 1223 |
* QmiProxy:qmi-proxy-n-clients |
| 1224 |
* |
| 1225 |
* Since: 1.8 |
| 1226 |
*/ |
| 1227 |
properties[PROP_N_CLIENTS] = |
| 1228 |
g_param_spec_uint (QMI_PROXY_N_CLIENTS, |
| 1229 |
"Number of clients", |
| 1230 |
"Number of clients currently connected to the proxy", |
| 1231 |
0, |
| 1232 |
G_MAXUINT, |
| 1233 |
0, |
| 1234 |
G_PARAM_READABLE); |
| 1235 |
g_object_class_install_property (object_class, PROP_N_CLIENTS, properties[PROP_N_CLIENTS]); |
| 1236 |
} |