forked from martinpitt/umockdev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathumockdev-ioctl.vala
1081 lines (901 loc) · 32.3 KB
/
umockdev-ioctl.vala
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace UMockdev {
/**
* SECTION:umockdev-ioctl
* @title: umockdev-ioctl
* @short_description: Emulate ioctl and read/write calls for devices.
*
* These classes permit emulation of ioctl and read/write calls including
* fully customizing the behaviour by creating an #UMockdevIoctlBase instance or
* subclass instance and attaching it using umockdev_testbed_attach_ioctl().
*/
/* The bindings are lacking g_signal_accumulator_true_handled, but easy enough
* to implement here.
*/
internal bool signal_accumulator_true_handled(GLib.SignalInvocationHint ihint,
GLib.Value return_accu,
GLib.Value handler_return,
void* data)
{
bool handled;
bool continue_emission;
handled = handler_return.get_boolean();
return_accu.set_boolean(handled);
continue_emission = !handled;
return continue_emission;
}
/**
* UMockdevIoctlData:
*
* The #UMockdevIoctlData struct is a container designed to read and write
* memory from the client process.
*
* After memory has been resolved, the corresponding pointer will point to
* local memory that can be used normally. The memory will automatically be
* synced back by umockdev_ioctl_client_complete().
*
* Since: 0.16
*/
public class IoctlData : GLib.Object {
/* Local cache to check if data is dirty before flushing. This is both an
* optimization as it avoids flushes, but is also necessary to avoid
* writing into read-only memory.
*/
private uint8[] client_data;
[CCode(array_length_cname="data_len")]
public uint8[] data;
public ulong client_addr;
private IOStream stream;
private IoctlData[] children;
private size_t[] children_offset;
internal IoctlData(IOStream stream)
{
this.stream = stream;
}
/**
* umockdev_ioctl_data_ref:
* @self: A #UMockdevIoctlData
*
* Deprecated, same as g_object_ref().
*/
[CCode(cname="umockdev_ioctl_data_ref")]
public new IoctlData? compat_ref()
{
return (IoctlData?) this;
}
/**
* umockdev_ioctl_data_unref:
* @self: A #UMockdevIoctlData
*
* Deprecated, same as g_object_unref().
*/
[CCode(cname="umockdev_ioctl_data_unref")]
public new void compat_unref()
{
this.unref();
}
/**
* umockdev_ioctl_data_resolve:
* @self: A #UMockdevIoctlData
* @offset: Byte offset of pointer inside data
* @len: Length of the to be resolved data
* @error: return location for a GError, or %NULL
*
* Resolve an address inside the data. After this operation, the pointer
* inside data points to a local copy of the memory. Any local modifications
* will be synced back by umockdev_ioctl_client_complete() and
* umockdev_ioctl_client_execute().
*
* You may call this multiple times on the same pointer in order to fetch
* the existing information.
*
* Returns: #UMockdevIoctlData, or #NULL on error
* Since: 0.16
*/
public IoctlData? resolve(size_t offset, size_t len) throws IOError {
IoctlData res;
for (int i = 0; i < children.length; i++) {
if (children_offset[i] == offset)
return children[i];
}
if (offset + sizeof(size_t) > data.length)
return null;
res = new IoctlData(stream);
res.data = new uint8[len];
res.client_addr = *((size_t*) &data[offset]);
children += res;
children_offset += offset;
/* Don't try to resolve null pointers. */
if (res.client_addr == 0 || len == 0)
return null;
*((size_t*) &data[offset]) = (size_t) res.data;
res.load_data();
return res;
}
/**
* umockdev_ioctl_data_set_ptr:
* @self: A #UMockdevIoctlData
* @offset: Byte offset of pointer inside data
* @child: Memory block that the pointer should point to
* @error: return location for a GError, or %NULL
*
* Basically the reverse operation of umockdev_ioctl_data_resolve(). It sets
* the pointer at @offset to point to the data from @child in a way that
* can be synchronised back to the client.
*
* Use of this is rare, but e.g. required to reap USB URBs.
*
* The function will only work correctly for pointer elements that have not
* been resolved before.
*
* Returns: %TRUE on success, %FALSE
* Since: 0.16
*/
public bool set_ptr(size_t offset, IoctlData child) {
foreach (size_t o in children_offset) {
assert(o != offset);
}
assert(offset + sizeof(size_t) <= data.length);
children += child;
children_offset += offset;
*((size_t*) &data[offset]) = (size_t) child.data;
return true;
}
/**
* umockdev_ioctl_data_reload:
* @self: A #UMockdevIoctlData
* @error: return location for a GError, or %NULL
*
* This function allows reloading the data from the client side in case
* you expect client modifications to have happened in the meantime (e.g.
* between two separate ioctl's).
* It is very unlikely that such an explicit reload is needed.
*
* Doing this unresolves any resolved pointers. Take care to re-resolve
* them and use the newly resolved #UMockdevIoctlData in case you need to
* access the data.
*
* Returns: #TRUE on success, #FALSE otherwise
* Since: 0.16
*/
public bool reload() throws IOError {
load_data();
children.resize(0);
children_offset.resize(0);
return true;
}
/**
* umockdev_ioctl_update:
* @self: A #UMockdevIoctlData
* @offset: Offset into data
* @new_data: (array length=length): Data to set
* @new_data_length1: Lenght of binary data, must be smaller or equal to actual length
*
* Set data to a specific value. This is essentially a memcpy call, it is
* only useful for e.g. python where the bindings cannot make the data
* writable otherwise.
*
* Since: 0.18
*/
public void update(size_t offset, uint8[] new_data) {
assert(offset + new_data.length <= data.length);
Posix.memcpy(&data[offset], new_data, new_data.length);
}
/**
* umockdev_ioctl_retrieve:
* @self: A #UMockdevIoctlData
* @read_data: (array length=length) (out): Data to set
* @read_data_length1: (out): Lenght of binary data, must be smaller or equal to actual length
*
* Simply returns the data struct member. This function purely exists for
* GIR based bindings, as the vala generated bindings do not correctly
* tag the array length, and direct access to the struct member is not
* possible.
*
* Since: 0.18
*/
public void retrieve(out uint8[] read_data) {
read_data = data;
}
internal void load_data() throws IOError {
OutputStream output = stream.get_output_stream();
InputStream input = stream.get_input_stream();
ulong args[3];
/* The original argument has no memory associated to it */
if (client_addr == 0)
return;
client_data = new uint8[data.length];
args[0] = 5; /* READ_MEM */
args[1] = client_addr;
args[2] = data.length;
output.write_all((uint8[])args, null, null);
input.read_all(client_data, null, null);
Posix.memcpy(data, client_data, data.length);
}
/*
* Recursively flushes out all data elements, transparently ensuring that
* pointers are submitted in terms of the client.
*/
internal async void flush() throws GLib.Error {
uint8[] submit_data = data;
for (int i = 0; i < children.length; i++) {
yield children[i].flush();
*((size_t*) &submit_data[children_offset[i]]) = children[i].client_addr;
}
if (client_addr != 0 &&
submit_data.length == client_data.length &&
Posix.memcmp(submit_data, client_data, submit_data.length) != 0) {
OutputStream output = stream.get_output_stream();
ulong args[3];
args[0] = 6; /* WRITE_MEM */
args[1] = client_addr;
args[2] = submit_data.length;
yield output.write_all_async((uint8[])args, 0, null, null);
yield output.write_all_async(submit_data, 0, null, null);
}
}
internal void flush_sync() throws IOError {
uint8[] submit_data = data;
for (int i = 0; i < children.length; i++) {
children[i].flush_sync();
*((ulong*) &submit_data[children_offset[i]]) = children[i].client_addr;
}
if (client_addr != 0 &&
submit_data.length == client_data.length &&
Posix.memcmp(submit_data, client_data, submit_data.length) != 0) {
OutputStream output = stream.get_output_stream();
ulong args[3];
args[0] = 6; /* WRITE_MEM */
args[1] = client_addr;
args[2] = submit_data.length;
output.write_all((uint8[])args, null, null);
output.write_all(submit_data, null, null);
}
}
}
/**
* UMockdevIoctlClient:
*
* The #UMockdevIoctlClient struct represents an opened client side FD in order
* to emulate ioctl calls on this device.
*
* Since: 0.16
*/
/**
* UMockdevIoctlClient::handle-ioctl:
* @client: A #UMockdevIoctlClient
*
* Called when an ioctl is requested by the client.
*
* This is the per-client signal. See #UMockdevIoctlBase::handle-ioctl on #UMockdevIoctlBase.
*
* Since: 0.16
*/
/**
* UMockdevIoctlClient::handle-read:
* @client: A #UMockdevIoctlClient
*
* Called when a read is requested by the client.
*
* This is the per-client signal. See #UMockdevIoctlBase::handle-read on #UMockdevIoctlBase.
*
* Since: 0.16
*/
/**
* UMockdevIoctlClient::handle-write:
* @client: A #UMockdevIoctlClient
*
* Called when a write is requested by the client.
*
* This is the per-client signal. See #UMockdevIoctlBase::handle-write on #UMockdevIoctlBase.
*
* Since: 0.16
*/
public class IoctlClient : GLib.Object {
private IoctlBase handler;
private IOStream stream;
private GLib.MainContext _ctx;
[Description(nick = "device node", blurb = "The device node the client opened")]
public string devnode { get; }
[Description(nick = "request", blurb = "The current ioctl request")]
public ulong request { get; }
[Description(nick = "argument", blurb = "The ioctl argument, for read/write the passed buffer")]
public IoctlData arg { get; }
[Description(nick = "connected", blurb = "Whether the client is still connected")]
public bool connected {
get {
return !stream.is_closed();
}
}
private ulong _cmd;
private bool _abort;
private long result;
private int result_errno;
static construct {
GLib.Signal.@new("handle-ioctl", typeof(IoctlClient), GLib.SignalFlags.RUN_LAST, 0, signal_accumulator_true_handled, null, null, typeof(bool), 0);
GLib.Signal.@new("handle-read", typeof(IoctlClient), GLib.SignalFlags.RUN_LAST, 0, signal_accumulator_true_handled, null, null, typeof(bool), 0);
GLib.Signal.@new("handle-write", typeof(IoctlClient), GLib.SignalFlags.RUN_LAST, 0, signal_accumulator_true_handled, null, null, typeof(bool), 0);
}
/**
* umockdev_ioctl_client_execute:
* @self: A #UMockdevIoctlClient
* @errno_: Return location for errno
* @error: return location for a GError, or %NULL
*
* This function is not generally useful. It exists for umockdev itself
* in order to implement recording.
*
* Execute the ioctl on the client side. Note that this flushes any
* modifications of the ioctl data. As such, pointers that were already
* resolved (including the initial ioctl argument itself) need to be
* resolved again.
*
* It is only valid to call this while an uncompleted command is being
* processed.
*
* This call is thread-safe.
*
* Returns: The client side result of the ioctl call
*
* Since: 0.16
*/
public int execute(out int errno_) throws IOError {
OutputStream output = stream.get_output_stream();
InputStream input = stream.get_input_stream();
ulong args[3];
assert(_cmd != 0);
arg.flush_sync();
args[0] = 4; /* RUN (original ioctl/read/write) */
args[1] = 0; /* unused */
args[2] = 0; /* unused */
output.write_all((uint8[])args, null, null);
input.read_all((uint8[])args, null, null);
assert(args[0] == 2); /* RES (result) */
/* Reload data, will usually not do anything for ioctl's as nothing
* will have been resolved. */
_arg.reload();
errno_ = (int) args[2];
return (int) args[1];
}
/**
* umockdev_ioctl_client_complete:
* @self: A #UMockdevIoctlClient
* @res: Return value of ioctl
* @errno_: errno of ioctl
*
* Asynchronously completes the ioctl invocation of the client. This is
* equivalent to calling umockdev_ioctl_client_complete() with the
* invocation.
*
* This call is thread-safe.
*
* Since: 0.16
*/
public void complete(long res, int errno_) {
/* Nullify some of the request information */
assert(_cmd != 0);
_cmd = 0;
_request = 0;
this.result = res;
this.result_errno = errno_;
/* Push us into the correct main context. */
_ctx.invoke(complete_idle);
}
/**
* umockdev_ioctl_client_abort:
* @self: A #UMockdevIoctlClient
*
* Asynchronously terminates the child by asking it to execute exit(1).
*
* This call is thread-safe.
*
* Since: 0.16
*/
public void abort() {
this._abort = true;
complete(0, 0);
}
private async void complete_async() throws GLib.Error {
OutputStream output = stream.get_output_stream();
ulong args[3];
yield _arg.flush();
if (!_abort) {
args[0] = 3; /* DONE */
args[1] = result;
args[2] = result_errno;
} else {
args[0] = 0xff; /* ABORT */
args[1] = 0;
args[2] = 0;
}
/* Nullify request information */
_cmd = 0;
_request = 0;
_arg = null;
result = 0;
result_errno = 0;
yield output.write_all_async((uint8[])args, 0, null, null);
/* And, finally re-queue ourself for receiving. */
read_ioctl.begin();
}
/* Start listening on the given stream.
* MUST be called from the correct thread! */
internal async void read_ioctl()
{
InputStream input = stream.get_input_stream();
size_t bytes;
ulong args[3];
try {
yield input.read_all_async((uint8[])args, 0, null, out bytes);
} catch (GLib.Error e) {
/* Probably we lost the client, ignore the error and make sure we are closed.
Closing needs to finish before the coroutine returns. */
try {
yield stream.close_async();
} catch (IOError e) {};
return;
}
if (input.is_closed() || bytes == 0) {
/* Closing needs to finish before the coroutine returns. */
try {
yield stream.close_async();
} catch (IOError e) {};
return;
}
assert(args[0] == 1 || args[0] == 7 || args[0] == 8);
_cmd = args[0];
if (args[0] == 1) {
_request = args[1];
_arg = new IoctlData(stream);
_arg.data = new uint8[sizeof(ulong)];
*(ulong*) _arg.data = args[2];
} else {
_request = 0;
_arg = new IoctlData(stream);
_arg.data = new uint8[args[2]];
_arg.client_addr = args[1];
try {
_arg.load_data();
} catch (IOError e) {
warning("Error resolving IOCtl data: %s", e.message);
complete(-100, 0);
return;
}
}
bool handled = false;
if (args[0] == 1)
GLib.Signal.emit_by_name(this, "handle-ioctl", out handled);
else if (args[0] == 7)
GLib.Signal.emit_by_name(this, "handle-read", out handled);
else
GLib.Signal.emit_by_name(this, "handle-write", out handled);
if (!handled) {
if (args[0] == 1)
GLib.Signal.emit_by_name(handler, "handle-ioctl", this, out handled);
else if (args[0] == 7)
GLib.Signal.emit_by_name(handler, "handle-read", this, out handled);
else
GLib.Signal.emit_by_name(handler, "handle-write", this, out handled);
}
if (!handled && args[0] == 1) {
IoctlTree.Tree tree = null;
IoctlData? data = null;
ulong size = IoctlTree.data_size_by_id(_request);
ulong type = (_request >> Ioctl._IOC_TYPESHIFT) & ((1 << Ioctl._IOC_TYPEBITS) - 1);
int ret = -1;
int my_errno;
try {
if (size > 0)
data = _arg.resolve(0, size);
} catch (IOError e) {
warning("Error resolving IOCtl data: %s", e.message);
complete(-100, 0);
return;
}
if ((char) type == 'E') {
Posix.errno = Posix.ENOENT;
} else {
Posix.errno = Posix.ENOTTY;
}
tree.execute(null, _request, *(void**) _arg.data, ref ret);
my_errno = Posix.errno;
Posix.errno = 0;
if (ret != -1) {
my_errno = 0;
}
complete(ret, my_errno);
} else if (!handled) {
complete(-100, 0);
}
}
private bool complete_idle()
{
/* Set completed early, so that an IO error will not trigger the
* warning from the destructor.
*/
complete_async.begin();
return false;
}
private bool notify_closed_idle()
{
notify_property("connected");
return false;
}
private void notify_closed_cb()
{
/* Force into correct thread. */
_ctx.invoke(notify_closed_idle);
}
internal IoctlClient(IoctlBase handler, IOStream stream, string devnode)
{
this.handler = handler;
this.stream = stream;
this._devnode = devnode;
this._ctx = GLib.MainContext.get_thread_default();
/* FIXME: There must be a better way to do this in vala? */
GLib.Signal.connect_object(this.stream, "notify::closed", (GLib.Callback) notify_closed_cb, this, GLib.ConnectFlags.SWAPPED);
}
~IoctlClient()
{
if (!stream.is_closed())
critical("Destroying IoctlClient with open stream!");
}
}
/**
* UMockdevIoctlBaseClass:
* @handle_ioctl: Override ioctl emulation
* @handle_read: Override read emulation
* @handle_write: Override write_emulation
* @client_connected: A device was opened
* @client_vanished: A device was closed
*
* The base class for an device ioctl and read/write handling. You can either
* override the corresponding vfuncs or connect to the signals to customize
* the emulation.
*
* Since: 0.16
*/
/**
* UMockdevIoctlBase:
*
* The #UMockdevIoctlBase class is a base class to emulate and record ioctl
* operations of a client. It can be attached to an emulated device in the
* testbed and will then be used.
*
* Since: 0.16
*/
/**
* UMockdevIoctlBase::handle-ioctl:
* @handler: A #UMockdevIoctlBase
* @client: A #UMockdevIoctlClient
*
* Called when an ioctl is requested by the client.
*
* Access the #UMockdevIoctlClient:arg property of @client to retrieve the
* argument of the ioctl. This is a pointer sized buffer initially with the
* original argument passed to the ioctl. If this is pointing to a struct, use
* umockdev_ioctl_data_resolve() to retrieve the underlying memory and update
* the pointer. Resolve any further pointers in the structure in the same way.
*
* After resolving the memory, you can access it as if it was local. The memory
* will be synced back to the client automatically if it has been modified
* locally.
*
* Once processing is done, use umockdev_ioctl_client_complete() to let the
* client continue with the result of the emulation. You can also use
* umockdev_ioctl_client_abort() to kill the client. Note that this handling
* does not need to be immediate. It is valid to immediately return #TRUE from
* this function and call umockdev_ioctl_client_complete() at a later point.
*
* Note that this function will be called from a worker thread with a private
* #GMainContext for the #UMockdevTestbed. Do not block this context for longer
* periods. The complete handler may be called from a different thread.
*
* Returns: #TRUE if the request is being handled, #FALSE otherwise.
* Since: 0.16
*/
/**
* UMockdevIoctlBase::handle-read:
* @handler: A #UMockdevIoctlBase
* @client: A #UMockdevIoctlClient
*
* Called when a read is requested by the client.
*
* The result buffer is represented by #UMockdevIoctlClient:arg of @client.
* Retrieve its length to find out the requested read length. The content of
* the buffer has already been retrieved, and you can freely use and update it.
*
* See #UMockdevIoctlBase::handle-ioctl for some more information.
*
* Returns: #TRUE if the request is being handled, #FALSE otherwise.
* Since: 0.16
*/
/**
* UMockdevIoctlBase::handle-write:
* @handler: A #UMockdevIoctlBase
* @client: A #UMockdevIoctlClient
*
* Called when a write is requested by the client.
*
* The written buffer is represented by #UMockdevIoctlClient:arg of @client.
* Retrieve its length to find out the requested write length. The content of
* the buffer has already been retrieved, and you can freely use it.
*
* See #UMockdevIoctlBase::handle-ioctl for some more information.
*
* Returns: #TRUE if the request is being handled, #FALSE otherwise.
* Since: 0.16
*/
private class StartListenClosure {
public IoctlBase handler;
public SocketListener listener;
public string devnode;
public StartListenClosure(IoctlBase handler, SocketListener listener, string devnode) {
this.handler = handler;
this.listener = listener;
this.devnode = devnode;
}
public bool cb() {
handler.socket_listen.begin(listener, devnode);
return false;
}
}
[ CCode(cname="G_STRUCT_OFFSET(UMockdevIoctlBaseClass, handle_ioctl)") ]
extern const int IOCTL_BASE_HANDLE_IOCTL_OFFSET;
[ CCode(cname="G_STRUCT_OFFSET(UMockdevIoctlBaseClass, handle_read)") ]
extern const int IOCTL_BASE_HANDLE_READ_OFFSET;
[ CCode(cname="G_STRUCT_OFFSET(UMockdevIoctlBaseClass, handle_write)") ]
extern const int IOCTL_BASE_HANDLE_WRITE_OFFSET;
public class IoctlBase: GLib.Object {
private HashTable<string,Cancellable> listeners;
static construct {
GLib.Signal.@new("handle-ioctl", typeof(IoctlBase), GLib.SignalFlags.RUN_LAST, IOCTL_BASE_HANDLE_IOCTL_OFFSET, signal_accumulator_true_handled, null, null, typeof(bool), 1, typeof(IoctlClient));
GLib.Signal.@new("handle-read", typeof(IoctlBase), GLib.SignalFlags.RUN_LAST, IOCTL_BASE_HANDLE_READ_OFFSET, signal_accumulator_true_handled, null, null, typeof(bool), 1, typeof(IoctlClient));
GLib.Signal.@new("handle-write", typeof(IoctlBase), GLib.SignalFlags.RUN_LAST, IOCTL_BASE_HANDLE_WRITE_OFFSET, signal_accumulator_true_handled, null, null, typeof(bool), 1, typeof(IoctlClient));
}
construct {
listeners = new HashTable<string,Cancellable>(str_hash, str_equal);
}
~IoctlBase()
{
}
internal async void socket_listen(SocketListener listener, string devnode)
{
Cancellable cancellable;
lock (listeners)
cancellable = listeners[devnode];
try {
while (true) {
SocketConnection connection;
IoctlClient client;
connection = yield listener.accept_async(cancellable);
client = new IoctlClient(this, connection, devnode);
client_connected(client);
client.read_ioctl.begin();
}
} catch (GLib.Error e) {
if (!(e is IOError.CANCELLED))
error("Could not accept new connection: %s", e.message);
}
listener.close();
}
#if INTERNAL_REGISTER_API
internal void register_path(GLib.MainContext? ctx, string devnode, string sockpath)
{
assert(DirUtils.create_with_parents(Path.get_dirname(sockpath), 0755) == 0);
Cancellable cancellable = new Cancellable();
cancellable.set_data("sockpath", sockpath);
/* We create new listener for each file; purely because we may not
* have the correct main context in construct yet. */
SocketListener listener;
SocketAddress addr;
listener = new SocketListener();
addr = new UnixSocketAddress(sockpath);
try {
listener.add_address(addr, SocketType.STREAM, SocketProtocol.DEFAULT, this, null);
} catch (GLib.Error e) {
warning("Error listening on ioctl socket for %s", devnode);
return;
}
lock (listeners)
listeners.insert(devnode, cancellable);
StartListenClosure tmp = new StartListenClosure(this, listener, devnode);
ctx.invoke(tmp.cb);
}
#if INTERNAL_UNREGISTER_PATH_API
internal void unregister_path(string devnode)
{
lock (listeners) {
listeners[devnode].cancel();
Posix.unlink(listeners[devnode].get_data("sockpath"));
listeners.remove(devnode);
}
}
#endif
#if INTERNAL_UNREGISTER_ALL_API
internal void unregister_all()
{
lock (listeners) {
listeners.foreach_remove((key, val) => {
val.cancel();
Posix.unlink(val.get_data("sockpath"));
return true;
});
}
}
#endif
#endif // INTERNAL_REGISTER_API
/* Not a normal signal because we need the accumulator. */
public virtual bool handle_ioctl(IoctlClient client) {
return false;
}
public virtual bool handle_read(IoctlClient client) {
return false;
}
public virtual bool handle_write(IoctlClient client) {
return false;
}
public virtual signal void client_connected(IoctlClient client) {
}
public virtual signal void client_vanished(IoctlClient client) {
}
}
/* Mirror of ioctl_tree.c usbdevfs_reapurb_execute submit_urb static variable */
private IoctlData? last_submit_urb = null;
internal class IoctlTreeHandler : IoctlBase {
private IoctlTree.Tree tree;
public IoctlTreeHandler(string file)
{
base ();
Posix.FILE f = Posix.FILE.open(file, "r");
tree = new IoctlTree.Tree(f);
}
public override bool handle_ioctl(IoctlClient client) {
void* last = null;
IoctlData? data = null;
ulong request = client.request;
ulong size = IoctlTree.data_size_by_id(request);
ulong type = (request >> Ioctl._IOC_TYPESHIFT) & ((1 << Ioctl._IOC_TYPEBITS) - 1);
int ret = -1;
int my_errno;
if (tree == null) {
debug("Aborting client because ioctl tree for %s is empty", client.devnode);
client.abort();
return true;
}
try {
if (size > 0)
data = client.arg.resolve(0, size);
/* NOTE: The C code assumes pointers are resolved, as such,
* all non-trivial structures need to be explicitly listed here.
*/
if (data != null) {
if (request == Ioctl.USBDEVFS_SUBMITURB) {
Ioctl.usbdevfs_urb *urb = (Ioctl.usbdevfs_urb*) data.data;
size_t offset = (ulong) &urb.buffer - (ulong) urb;
data.resolve(offset, urb.buffer_length);
}
}
} catch (IOError e) {
warning("Error resolving IOCtl data: %s", e.message);
return false;
}
last = client.get_data("last");
if ((char) type == 'E') {
Posix.errno = Posix.ENOENT;
} else {
Posix.errno = Posix.ENOTTY;
}
last = tree.execute(last, request, *(void**) client.arg.data, ref ret);
my_errno = Posix.errno;
Posix.errno = 0;
if (last != null)
client.set_data("last", last);
if (ret != -1) {
my_errno = 0;
}
if (request == Ioctl.USBDEVFS_SUBMITURB && ret == 0) {
last_submit_urb = data;
}
if ((request == Ioctl.USBDEVFS_REAPURB || request == Ioctl.USBDEVFS_REAPURBNDELAY) && last_submit_urb != null) {
/* Parameter points to a pointer, check whether that is a pointer
* to our last submit urb. If so, update it so the client sees
* the right information.
* This should only happen for REAPURB, but it does not hurt to
* just always check.
*/
if (*(void**) data.data == (void*) last_submit_urb.data) {
data.set_ptr(0, last_submit_urb);
last_submit_urb = null;
}
}
client.complete(ret, my_errno);
return true;
}
}
internal class IoctlTreeRecorder : IoctlBase {
bool write_log;
string logfile;
string device;
private IoctlTree.Tree tree;
public IoctlTreeRecorder(string device, string file)
{
string existing_device_path = null;
Posix.FILE log;
base ();
this.logfile = file;
this.device = device;
log = Posix.FILE.open(logfile, "r");
if (log == null)
return;
/* Check @DEV header and parse log */
if (log.scanf("@DEV %ms\n", &existing_device_path) == 1 && existing_device_path != device)
error("attempt to record two different devices to the same ioctl recording");
tree = new IoctlTree.Tree(log);
}
~IoctlTreeRecorder()
{
flush_log();
}