-
Notifications
You must be signed in to change notification settings - Fork 412
/
fu-io-channel.c
536 lines (496 loc) · 11.9 KB
/
fu-io-channel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
* Copyright (C) 2017 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#define G_LOG_DOMAIN "FuIOChannel"
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <gio/gio.h>
#include <glib/gstdio.h>
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#include <string.h>
#include <sys/stat.h>
#include "fwupd-error.h"
#include "fu-common.h"
#include "fu-io-channel.h"
/**
* FuIOChannel:
*
* A bidirectional IO channel which can be read from and written to.
*/
struct _FuIOChannel {
GObject parent_instance;
gint fd;
};
G_DEFINE_TYPE(FuIOChannel, fu_io_channel, G_TYPE_OBJECT)
/**
* fu_io_channel_unix_get_fd:
* @self: a #FuIOChannel
*
* Gets the file descriptor for the device.
*
* Returns: fd, or -1 for not open.
*
* Since: 1.2.2
**/
gint
fu_io_channel_unix_get_fd(FuIOChannel *self)
{
g_return_val_if_fail(FU_IS_IO_CHANNEL(self), -1);
return self->fd;
}
/**
* fu_io_channel_shutdown:
* @self: a #FuIOChannel
* @error: (nullable): optional return location for an error
*
* Closes the file descriptor for the device.
*
* Returns: %TRUE if all the FD was closed.
*
* Since: 1.2.2
**/
gboolean
fu_io_channel_shutdown(FuIOChannel *self, GError **error)
{
g_return_val_if_fail(FU_IS_IO_CHANNEL(self), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (!g_close(self->fd, error))
return FALSE;
self->fd = -1;
return TRUE;
}
static gboolean
fu_io_channel_flush_input(FuIOChannel *self, GError **error)
{
GPollFD poll = {
.fd = self->fd,
.events = G_IO_IN | G_IO_ERR,
};
while (g_poll(&poll, 1, 0) > 0) {
gchar c;
gint r = read(self->fd, &c, 1);
if (r < 0 && errno != EINTR)
break;
}
return TRUE;
}
/**
* fu_io_channel_write_bytes:
* @self: a #FuIOChannel
* @bytes: buffer to write
* @timeout_ms: timeout in ms
* @flags: channel flags, e.g. %FU_IO_CHANNEL_FLAG_SINGLE_SHOT
* @error: (nullable): optional return location for an error
*
* Writes bytes to the TTY, that will fail if exceeding @timeout_ms.
*
* Returns: %TRUE if all the bytes was written
*
* Since: 1.2.2
**/
gboolean
fu_io_channel_write_bytes(FuIOChannel *self,
GBytes *bytes,
guint timeout_ms,
FuIOChannelFlags flags,
GError **error)
{
gsize bufsz = 0;
const guint8 *buf = g_bytes_get_data(bytes, &bufsz);
return fu_io_channel_write_raw(self, buf, bufsz, timeout_ms, flags, error);
}
/**
* fu_io_channel_write_byte_array:
* @self: a #FuIOChannel
* @buf: buffer to write
* @timeout_ms: timeout in ms
* @flags: channel flags, e.g. %FU_IO_CHANNEL_FLAG_SINGLE_SHOT
* @error: (nullable): optional return location for an error
*
* Writes bytes to the TTY, that will fail if exceeding @timeout_ms.
*
* Returns: %TRUE if all the bytes was written
*
* Since: 1.3.2
**/
gboolean
fu_io_channel_write_byte_array(FuIOChannel *self,
GByteArray *buf,
guint timeout_ms,
FuIOChannelFlags flags,
GError **error)
{
return fu_io_channel_write_raw(self, buf->data, buf->len, timeout_ms, flags, error);
}
/**
* fu_io_channel_write_raw:
* @self: a #FuIOChannel
* @data: buffer to write
* @datasz: size of @data
* @timeout_ms: timeout in ms
* @flags: channel flags, e.g. %FU_IO_CHANNEL_FLAG_SINGLE_SHOT
* @error: (nullable): optional return location for an error
*
* Writes bytes to the TTY, that will fail if exceeding @timeout_ms.
*
* Returns: %TRUE if all the bytes was written
*
* Since: 1.2.2
**/
gboolean
fu_io_channel_write_raw(FuIOChannel *self,
const guint8 *data,
gsize datasz,
guint timeout_ms,
FuIOChannelFlags flags,
GError **error)
{
gsize idx = 0;
g_return_val_if_fail(FU_IS_IO_CHANNEL(self), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* flush pending reads */
if (flags & FU_IO_CHANNEL_FLAG_FLUSH_INPUT) {
if (!fu_io_channel_flush_input(self, error))
return FALSE;
}
/* blocking IO */
if (flags & FU_IO_CHANNEL_FLAG_USE_BLOCKING_IO) {
gssize wrote = write(self->fd, data, datasz);
if (wrote != (gssize)datasz) {
if (errno == EPROTO) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_FOUND,
"failed to write: %s",
strerror(errno));
return FALSE;
}
g_set_error(error,
G_IO_ERROR,
G_IO_ERROR_FAILED,
"failed to write: "
"wrote %" G_GSSIZE_FORMAT " of %" G_GSIZE_FORMAT,
wrote,
datasz);
return FALSE;
}
return TRUE;
}
/* nonblocking IO */
while (idx < datasz) {
gint rc;
GPollFD fds = {
.fd = self->fd,
.events = G_IO_OUT | G_IO_ERR,
};
/* wait for data to be allowed to write without blocking */
rc = g_poll(&fds, 1, (gint)timeout_ms);
if (rc == 0)
break;
if (rc < 0) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_READ,
"failed to poll %i",
self->fd);
return FALSE;
}
/* we can write data */
if (fds.revents & G_IO_OUT) {
gssize len = write(self->fd, data + idx, datasz - idx);
if (len < 0) {
if (errno == EAGAIN) {
g_debug("got EAGAIN, trying harder");
continue;
}
if (errno == EPROTO) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_FOUND,
"failed to write: %s",
strerror(errno));
return FALSE;
}
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_WRITE,
"failed to write %" G_GSIZE_FORMAT " bytes to %i: %s",
datasz,
self->fd,
strerror(errno));
return FALSE;
}
if (flags & FU_IO_CHANNEL_FLAG_SINGLE_SHOT)
break;
idx += len;
}
}
return TRUE;
}
/**
* fu_io_channel_read_bytes:
* @self: a #FuIOChannel
* @max_size: maximum size of the returned blob, or -1 for no limit
* @timeout_ms: timeout in ms
* @flags: channel flags, e.g. %FU_IO_CHANNEL_FLAG_SINGLE_SHOT
* @error: (nullable): optional return location for an error
*
* Reads bytes from the TTY, that will fail if exceeding @timeout_ms.
*
* Returns: a #GBytes, or %NULL for error
*
* Since: 1.2.2
**/
GBytes *
fu_io_channel_read_bytes(FuIOChannel *self,
gssize max_size,
guint timeout_ms,
FuIOChannelFlags flags,
GError **error)
{
GByteArray *buf = fu_io_channel_read_byte_array(self, max_size, timeout_ms, flags, error);
if (buf == NULL)
return NULL;
return g_byte_array_free_to_bytes(buf);
}
/**
* fu_io_channel_read_byte_array:
* @self: a #FuIOChannel
* @max_size: maximum size of the returned blob, or -1 for no limit
* @timeout_ms: timeout in ms
* @flags: channel flags, e.g. %FU_IO_CHANNEL_FLAG_SINGLE_SHOT
* @error: (nullable): optional return location for an error
*
* Reads bytes from the TTY, that will fail if exceeding @timeout_ms.
*
* Returns: (transfer full): a #GByteArray, or %NULL for error
*
* Since: 1.3.2
**/
GByteArray *
fu_io_channel_read_byte_array(FuIOChannel *self,
gssize max_size,
guint timeout_ms,
FuIOChannelFlags flags,
GError **error)
{
GPollFD fds = {
.fd = self->fd,
.events = G_IO_IN | G_IO_PRI | G_IO_ERR,
};
g_autoptr(GByteArray) buf2 = g_byte_array_new();
g_return_val_if_fail(FU_IS_IO_CHANNEL(self), NULL);
/* blocking IO */
if (flags & FU_IO_CHANNEL_FLAG_USE_BLOCKING_IO) {
guint8 buf[1024];
gssize len = read(self->fd, buf, sizeof(buf));
if (len < 0) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_READ,
"failed to read %i: %s",
self->fd,
strerror(errno));
return NULL;
}
if (len > 0)
g_byte_array_append(buf2, buf, len);
return g_steal_pointer(&buf2);
}
/* nonblocking IO */
while (TRUE) {
/* wait for data to appear */
gint rc = g_poll(&fds, 1, (gint)timeout_ms);
if (rc == 0) {
g_set_error(error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT, "timeout");
return NULL;
}
if (rc < 0) {
if (errno == EINTR)
continue;
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_READ,
"failed to poll %i",
self->fd);
return NULL;
}
/* we have data to read */
if (fds.revents & G_IO_IN) {
guint8 buf[1024];
gssize len = read(self->fd, buf, sizeof(buf));
if (len < 0) {
if (errno == EINTR)
continue;
if (errno == EAGAIN)
continue;
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_READ,
"failed to read %i: %s",
self->fd,
strerror(errno));
return NULL;
}
if (len > 0)
g_byte_array_append(buf2, buf, len);
/* check maximum size */
if (max_size > 0 && buf2->len >= (guint)max_size)
break;
if (flags & FU_IO_CHANNEL_FLAG_SINGLE_SHOT)
break;
continue;
}
if (fds.revents & G_IO_ERR) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_READ,
"error condition");
return NULL;
}
if (fds.revents & G_IO_HUP) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_READ,
"connection hung up");
return NULL;
}
if (fds.revents & G_IO_NVAL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_READ,
"invalid request");
return NULL;
}
}
/* no data */
if (buf2->len == 0) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_READ,
"no data received from device in %ums",
timeout_ms);
return NULL;
}
/* return blob */
return g_steal_pointer(&buf2);
}
/**
* fu_io_channel_read_raw:
* @self: a #FuIOChannel
* @buf: (nullable): optional buffer
* @bufsz: size of @buf
* @bytes_read: (out) (nullable): data written to @buf
* @timeout_ms: timeout in ms
* @flags: channel flags, e.g. %FU_IO_CHANNEL_FLAG_SINGLE_SHOT
* @error: (nullable): optional return location for an error
*
* Reads bytes from the TTY, that will fail if exceeding @timeout_ms.
*
* Returns: a #GBytes, or %NULL for error
*
* Since: 1.2.2
**/
gboolean
fu_io_channel_read_raw(FuIOChannel *self,
guint8 *buf,
gsize bufsz,
gsize *bytes_read,
guint timeout_ms,
FuIOChannelFlags flags,
GError **error)
{
const guint8 *tmpbuf = NULL;
gsize bytes_read_tmp;
g_autoptr(GBytes) tmp = NULL;
g_return_val_if_fail(FU_IS_IO_CHANNEL(self), FALSE);
tmp = fu_io_channel_read_bytes(self, bufsz, timeout_ms, flags, error);
if (tmp == NULL)
return FALSE;
tmpbuf = g_bytes_get_data(tmp, &bytes_read_tmp);
if (tmpbuf != NULL)
memcpy(buf, tmpbuf, bytes_read_tmp);
if (bytes_read != NULL)
*bytes_read = bytes_read_tmp;
return TRUE;
}
static void
fu_io_channel_finalize(GObject *object)
{
FuIOChannel *self = FU_IO_CHANNEL(object);
if (self->fd != -1)
g_close(self->fd, NULL);
G_OBJECT_CLASS(fu_io_channel_parent_class)->finalize(object);
}
static void
fu_io_channel_class_init(FuIOChannelClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->finalize = fu_io_channel_finalize;
}
static void
fu_io_channel_init(FuIOChannel *self)
{
self->fd = -1;
}
/**
* fu_io_channel_unix_new:
* @fd: file descriptor
*
* Creates a new object to write and read from.
*
* Returns: a #FuIOChannel
*
* Since: 1.2.2
**/
FuIOChannel *
fu_io_channel_unix_new(gint fd)
{
FuIOChannel *self;
self = g_object_new(FU_TYPE_IO_CHANNEL, NULL);
self->fd = fd;
return FU_IO_CHANNEL(self);
}
/**
* fu_io_channel_new_file:
* @filename: device file
* @error: (nullable): optional return location for an error
*
* Creates a new object to write and read from.
*
* Returns: a #FuIOChannel
*
* Since: 1.2.2
**/
FuIOChannel *
fu_io_channel_new_file(const gchar *filename, GError **error)
{
#ifdef HAVE_POLL_H
gint fd;
g_return_val_if_fail(filename != NULL, NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
fd = g_open(filename, O_RDWR | O_NONBLOCK, S_IRWXU);
if (fd < 0) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"failed to open %s",
filename);
return NULL;
}
return fu_io_channel_unix_new(fd);
#else
g_return_val_if_fail(filename != NULL, NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"Not supported as <poll.h> is unavailable");
return NULL;
#endif
}