-
Notifications
You must be signed in to change notification settings - Fork 2
/
libdsbmc.c
1082 lines (985 loc) · 27.4 KB
/
libdsbmc.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
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
/*-
* Copyright (c) 2017 Marcel Kaiser. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdarg.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/select.h>
#include "libdsbmc.h"
#define BUFSZ 64
#define ERR_SYS_FATAL (DSBMC_ERR_SYS|DSBMC_ERR_FATAL)
#define PATH_DSBMD_SOCKET "/var/run/dsbmd.socket"
#define ERROR(dh, ret, error, prepend, fmt, ...) do { \
set_error(dh, error, prepend, fmt, ##__VA_ARGS__); \
return (ret); \
} while (0)
#define VALIDATE(dh, dev) do { \
if (dev == NULL || dev->removed) \
ERROR(dh, -1, DSBMC_ERR_INVALID_DEVICE, false, \
"Invalid device"); \
} while (0)
#define LOOKUP_DEV(dh, arg, dev) do { \
VALIDATE(dh, arg); \
dev = dsbmc_dev_from_id(dh, arg->id); \
VALIDATE(dh, dev); \
} while (0)
typedef enum kwtype_e {
KWTYPE_CHAR = 1, KWTYPE_STRING, KWTYPE_COMMANDS, KWTYPE_INTEGER,
KWTYPE_UINT64, KWTYPE_UINT8, KWTYPE_DSKTYPE
} kwtype_t;
typedef union val_u val_t;
struct dsbmdkeyword_s {
const char *key;
kwtype_t type;
union val_u {
int *integer;
char *character;
char **string;
uint8_t *uint8;
uint64_t *uint64;
} val;
};
static const struct cmdtbl_s {
char *name;
uint8_t cmd;
} cmdtbl[] = {
{ "mount", DSBMC_CMD_MOUNT },
{ "unmount", DSBMC_CMD_UNMOUNT },
{ "eject", DSBMC_CMD_EJECT },
{ "speed", DSBMC_CMD_SPEED },
{ "size", DSBMC_CMD_SIZE },
{ "mdattach", DSBMC_CMD_MDATTACH }
};
#define NCMDS (sizeof(cmdtbl) / sizeof(struct cmdtbl_s))
/*
* Struct to assign disk type strings to disktype IDs.
*/
static const struct disktypetbl_s {
char *name;
uint8_t type;
} disktypetbl[] = {
{ "AUDIOCD", DSBMC_DT_AUDIOCD },
{ "DATACD", DSBMC_DT_DATACD },
{ "RAWCD", DSBMC_DT_RAWCD },
{ "USBDISK", DSBMC_DT_USBDISK },
{ "FLOPPY", DSBMC_DT_FLOPPY },
{ "DVD", DSBMC_DT_DVD },
{ "VCD", DSBMC_DT_VCD },
{ "SVCD", DSBMC_DT_SVCD },
{ "HDD", DSBMC_DT_HDD },
{ "MMC", DSBMC_DT_MMC },
{ "MTP", DSBMC_DT_MTP },
{ "PTP", DSBMC_DT_PTP }
};
#define NDSKTYPES (sizeof(disktypetbl) / sizeof(struct disktypetbl_s))
/*
* Error code translation.
*/
static const struct errmsg_s {
int code;
char *msg;
} errmsgs[] = {
{ DSBMC_ERR_ALREADY_MOUNTED, "Device already mounted" },
{ DSBMC_ERR_PERMISSION_DENIED, "Permission denied" },
{ DSBMC_ERR_NOT_MOUNTED, "Device not mounted" },
{ DSBMC_ERR_DEVICE_BUSY, "Device busy" },
{ DSBMC_ERR_NO_SUCH_DEVICE, "No such device" },
{ DSBMC_ERR_NOT_EJECTABLE, "Device not ejectable" },
{ DSBMC_ERR_UNKNOWN_COMMAND, "Unknown command" },
{ DSBMC_ERR_UNKNOWN_OPTION, "Unknown option" },
{ DSBMC_ERR_SYNTAX_ERROR, "Syntax error" },
{ DSBMC_ERR_NO_MEDIA, "No media" },
{ DSBMC_ERR_UNKNOWN_FILESYSTEM, "Unknown filesystem" },
{ DSBMC_ERR_UNKNOWN_ERROR, "Unknown error" },
{ DSBMC_ERR_MNTCMD_FAILED, "Mount command failed" },
{ DSBMC_ERR_INVALID_ARGUMENT, "Invalid argument" },
{ DSBMC_ERR_MAX_CONN_REACHED, "Max. number of connections reached" },
{ DSBMC_ERR_STRING_TOO_LONG, "Command string too long" },
{ DSBMC_ERR_BAD_STRING, "Invalid command string" },
{ DSBMC_ERR_TIMEOUT, "Timeout" },
{ DSBMC_ERR_NOT_A_FILE, "Not a regular file" },
};
#define NERRMSGS (sizeof(errmsgs) / sizeof(struct errmsg_s))
static int uconnect(dsbmc_t *, const char *);
static int send_string(dsbmc_t *, const char *);
static int push_event(dsbmc_t *, const char *);
static int parse_event(dsbmc_t *, const char *);
static int process_event(dsbmc_t *, char *);
static int dsbmc_send(dsbmc_t *, const char *, ...);
static int dsbmc_send_async(dsbmc_t *, dsbmc_dev_t *,
void (*cb)(int, const dsbmc_dev_t *), const char *, ...);
static void dsbmc_clearerr(dsbmc_t *);
static void set_error(dsbmc_t *, int, bool, const char *, ...);
static void del_device(dsbmc_t *, const char *);
static void cleanup(dsbmc_t *);
static char *readln(dsbmc_t *);
static char *read_event(dsbmc_t *, bool);
static char *pull_event(dsbmc_t *);
static dsbmc_dev_t *add_device(dsbmc_t *, const dsbmc_dev_t *);
static dsbmc_dev_t *lookup_device(dsbmc_t *, const char *);
int
dsbmc_mount(dsbmc_t *dh, const dsbmc_dev_t *d)
{
int ret;
dsbmc_dev_t *dev;
LOOKUP_DEV(dh, d, dev);
if ((ret = dsbmc_send(dh, "mount %s\n", dev->dev)) == 0) {
if (dh->event.devinfo.mntpt == NULL) {
ERROR(dh, -1, DSBMC_ERR_UNKNOWN_ERROR, false,
"mntpt == NULL");
}
dev->mounted = true; free(dev->mntpt);
dev->mntpt = strdup(dh->event.devinfo.mntpt);
if (dev->mntpt == NULL)
ERROR(dh, -1, ERR_SYS_FATAL, false, "strdup()");
}
return (ret);
}
int
dsbmc_unmount(dsbmc_t *dh, const dsbmc_dev_t *d, bool force)
{
int ret;
dsbmc_dev_t *dev;
LOOKUP_DEV(dh, d, dev);
if ((ret = dsbmc_send(dh, force ? "unmount -f %s\n" : "unmount %s\n",
d->dev)) == 0) {
dev->mounted = false;
}
return (ret);
}
int
dsbmc_eject(dsbmc_t *dh, const dsbmc_dev_t *d, bool force)
{
dsbmc_dev_t *dev;
LOOKUP_DEV(dh, d, dev);
return (dsbmc_send(dh, force ? "eject -f %s\n" : "eject %s\n", d->dev));
}
int
dsbmc_size(dsbmc_t *dh, const dsbmc_dev_t *d)
{
int ret;
dsbmc_dev_t *dev;
LOOKUP_DEV(dh, d, dev);
if ((ret = dsbmc_send(dh, "size %s\n", d->dev)) == 0) {
dev->mediasize = dh->event.mediasize;
dev->used = dh->event.used;
dev->free = dh->event.free;
}
return (ret);
}
int
dsbmc_set_speed(dsbmc_t *dh, const dsbmc_dev_t *d, int speed)
{
int ret;
dsbmc_dev_t *dev;
LOOKUP_DEV(dh, d, dev);
if ((ret = dsbmc_send(dh, "speed %s %d\n", dev->dev, speed)) == 0)
dev->speed = dh->event.devinfo.speed;
return (ret);
}
int
dsbmc_mdattach(dsbmc_t *dh, const char *image)
{
return (dsbmc_send(dh, "mdattach \"%s\"\n", image));
}
int
dsbmc_mount_async(dsbmc_t *dh, const dsbmc_dev_t *d,
void (*cb)(int, const dsbmc_dev_t *))
{
dsbmc_dev_t *dev;
LOOKUP_DEV(dh, d, dev);
return (dsbmc_send_async(dh, dev, cb, "mount %s\n", dev->dev));
}
int
dsbmc_unmount_async(dsbmc_t *dh, const dsbmc_dev_t *d, bool force,
void (*cb)(int, const dsbmc_dev_t *))
{
dsbmc_dev_t *dev;
LOOKUP_DEV(dh, d, dev);
return (dsbmc_send_async(dh, dev, cb, force ? "unmount -f %s\n" : \
"unmount %s\n", dev->dev));
}
int
dsbmc_eject_async(dsbmc_t *dh, const dsbmc_dev_t *d, bool force,
void (*cb)(int, const dsbmc_dev_t *))
{
dsbmc_dev_t *dev;
LOOKUP_DEV(dh, d, dev);
return (dsbmc_send_async(dh, dev, cb, force ? "eject -f %s\n" : \
"eject %s\n", dev->dev));
}
int
dsbmc_set_speed_async(dsbmc_t *dh, const dsbmc_dev_t *d, int speed,
void (*cb)(int, const dsbmc_dev_t *))
{
dsbmc_dev_t *dev;
LOOKUP_DEV(dh, d, dev);
return (dsbmc_send_async(dh, dev, cb, "speed %s %d\n", dev->dev, speed));
}
int
dsbmc_size_async(dsbmc_t *dh, const dsbmc_dev_t *d,
void (*cb)(int, const dsbmc_dev_t *))
{
dsbmc_dev_t *dev;
LOOKUP_DEV(dh, d, dev);
return (dsbmc_send_async(dh, dev, cb, "size %s\n", dev->dev));
}
int
dsbmc_mdattach_async(dsbmc_t *dh, const char *image,
void (*cb)(int, const dsbmc_dev_t *))
{
return (dsbmc_send_async(dh, NULL, cb, "mdattach \"%s\"\n", image));
}
void
dsbmc_disconnect(dsbmc_t *dh)
{
(void)dsbmc_send(dh, "quit\n");
(void)close(dh->socket);
}
void
dsbmc_free_dev(dsbmc_t *dh, const dsbmc_dev_t *dev)
{
if (dev == NULL || !dev->removed)
return;
del_device(dh, dev->dev);
}
static void
cleanup(dsbmc_t *dh)
{
size_t i;
for (i = 0; i < dh->ndevs; i++) {
dh->devs[i]->removed = true;
del_device(dh, dh->devs[i]->dev);
}
for (i = 0; i < dh->evq.n; i++)
free(dh->evq.ln[i]);
while (dh->cmdqsz != 0 && --(dh->cmdqsz) > 0)
free(dh->sender[dh->cmdqsz].cmd);
free(dh->lbuf);
free(dh->pbuf);
free(dh->sbuf);
free(dh);
}
int
dsbmc_get_err(dsbmc_t *dh, const char **errmsg)
{
if (!dh->error)
return (0);
if (errmsg != NULL)
*errmsg = dh->errormsg;
return (dh->error);
}
int
dsbmc_get_fd(dsbmc_t *dh)
{
return (dh->socket);
}
const char *
dsbmc_errstr(dsbmc_t *dh)
{
return (dh->errormsg);
}
const char *
dsbmc_errcode_to_str(int code)
{
size_t i;
if (code < (1 << 8))
return (strerror(code));
for (i = 0; i < NERRMSGS; i++) {
if (errmsgs[i].code == code)
return (errmsgs[i].msg);
}
return (NULL);
}
int
dsbmc_fetch_event(dsbmc_t *dh, dsbmc_event_t *ev)
{
int error;
char *e;
dsbmc_clearerr(dh);
while ((e = read_event(dh, false)) != NULL) {
if (push_event(dh, e) == -1)
ERROR(dh, -1, 0, true, "push_event()");
}
if (dsbmc_get_err(dh, NULL) != 0)
ERROR(dh, -1, 0, true, "read_event()");
if ((e = pull_event(dh)) == NULL)
return (0);
if ((error = process_event(dh, e)) == 1) {
ev->type = dh->event.type;
ev->code = dh->event.code;
if (dh->event.devinfo.dev != NULL)
ev->dev = lookup_device(dh, dh->event.devinfo.dev);
}
return (error);
}
dsbmc_t *
dsbmc_alloc_handle()
{
dsbmc_t *dh;
if ((dh = malloc(sizeof(dsbmc_t))) == NULL)
return (NULL);
bzero(dh, sizeof(dsbmc_t));
return (dh);
}
void
dsbmc_free_handle(dsbmc_t *dh)
{
cleanup(dh);
}
int
dsbmc_connect(dsbmc_t *dh)
{
char *e;
if ((dh->socket = uconnect(dh, PATH_DSBMD_SOCKET)) == -1) {
ERROR(dh, -1, ERR_SYS_FATAL, true, "uconnect(%s)",
PATH_DSBMD_SOCKET);
}
/* Get device list */
while ((e = read_event(dh, true)) != NULL) {
if (process_event(dh, e) == -1)
ERROR(dh, -1, 0, true, "parse_event()");
if (dh->event.type == DSBMC_EVENT_ERROR_MSG &&
dh->event.code == DSBMC_ERR_PERMISSION_DENIED) {
ERROR(dh, -1, DSBMC_ERR_FATAL | DSBMC_ERR_CONN_DENIED,
false, "Permission denied");
}
if (dh->event.type != DSBMC_EVENT_ADD_DEVICE &&
dh->event.type != DSBMC_EVENT_END_OF_LIST) {
ERROR(dh, -1, ERR_SYS_FATAL, false,
"Unexpected event (%d) received", dh->event.type);
} else if (dh->event.type == DSBMC_EVENT_END_OF_LIST)
break;
}
return (0);
}
int
dsbmc_get_devlist(dsbmc_t *dh, const dsbmc_dev_t ***list)
{
*list = (const dsbmc_dev_t **)dh->devs;
return (dh->ndevs);
}
dsbmc_dev_t *
dsbmc_dev_from_id(dsbmc_t *dh, int id)
{
int i;
for (i = dh->ndevs - 1; i >= 0; i--) {
if (dh->devs[i]->id == id)
return (dh->devs[i]);
}
return (NULL);
}
dsbmc_dev_t *
dsbmc_dev_from_name(dsbmc_t *dh, const char *dev)
{
return (lookup_device(dh, dev));
}
dsbmc_dev_t *
dsbmc_next_dev(dsbmc_t *dh, int *idx, bool removed)
{
int n;
if (*idx < 0 || (size_t)*idx >= dh->ndevs)
return (NULL);
for (n = dh->ndevs - 1; n - *idx >= 0; (*idx)++) {
if (dh->devs[n - *idx]->removed) {
if (removed)
return (dh->devs[n - (*idx)++]);
} else
return (dh->devs[n - (*idx)++]);
}
return (NULL);
}
static void
dsbmc_clearerr(dsbmc_t *dh)
{
dh->error = 0;
}
static void
set_error(dsbmc_t *dh, int error, bool prepend, const char *fmt, ...)
{
int _errno;
char errbuf[sizeof(dh->errormsg)];
size_t len;
va_list ap;
_errno = errno;
va_start(ap, fmt);
if (prepend) {
dh->error |= error;
if (error & DSBMC_ERR_FATAL) {
if (strncmp(dh->errormsg, "Fatal error: ", 13) == 0) {
memmove(dh->errormsg, dh->errormsg + 13,
strlen(dh->errormsg) - 12);
}
(void)strlcpy(errbuf, "Fatal error: ", sizeof(errbuf));
len = strlen(errbuf);
} else if (strncmp(dh->errormsg, "Error: ", 7) == 0) {
memmove(dh->errormsg, dh->errormsg + 7,
strlen(dh->errormsg) - 6);
(void)strlcpy(errbuf, "Error: ", sizeof(errbuf));
len = strlen(errbuf);
} else
len = 0;
(void)vsnprintf(errbuf + len, sizeof(errbuf) - len, fmt, ap);
len = strlen(errbuf);
(void)snprintf(errbuf + len, sizeof(errbuf) - len, ": %s",
dh->errormsg);
(void)strcpy(dh->errormsg, errbuf);
} else {
dh->error = error;
(void)vsnprintf(dh->errormsg, sizeof(dh->errormsg), fmt, ap);
if (error == DSBMC_ERR_FATAL) {
(void)snprintf(errbuf, sizeof(errbuf),
"Fatal error: %s", dh->errormsg);
} else {
(void)snprintf(errbuf, sizeof(errbuf),
"Error: %s", dh->errormsg);
}
(void)strcpy(dh->errormsg, errbuf);
}
if ((error & DSBMC_ERR_SYS) && _errno != 0) {
len = strlen(dh->errormsg);
(void)snprintf(dh->errormsg + len, sizeof(dh->errormsg) - len,
": %s", strerror(_errno));
errno = 0;
}
}
static int
uconnect(dsbmc_t *dh, const char *path)
{
int s;
struct sockaddr_un saddr;
if ((s = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
ERROR(dh, -1, ERR_SYS_FATAL, false, "socket()");
(void)memset(&saddr, (unsigned char)0, sizeof(saddr));
(void)snprintf(saddr.sun_path, sizeof(saddr.sun_path), "%s", path);
saddr.sun_family = AF_LOCAL;
if (connect(s, (struct sockaddr *)&saddr, sizeof(saddr)) == -1)
ERROR(dh, -1, ERR_SYS_FATAL, false, "connect(%s)", path);
if (fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK) == -1)
ERROR(dh, -1, ERR_SYS_FATAL, false, "setvbuf()/fcntl()");
return (s);
}
static dsbmc_dev_t *
add_device(dsbmc_t *dh, const dsbmc_dev_t *d)
{
static int id = 1;
dsbmc_dev_t *dp, **dl;
if ((dp = lookup_device(dh, d->dev)) != NULL && !dp->removed)
return (NULL);
dl = realloc(dh->devs, sizeof(dsbmc_dev_t *) * (dh->ndevs + 1));
if (dl == NULL)
ERROR(dh, NULL, ERR_SYS_FATAL, false, "realloc()");
dh->devs = dl;
if ((dh->devs[dh->ndevs] = malloc(sizeof(dsbmc_dev_t))) == NULL)
ERROR(dh, NULL, ERR_SYS_FATAL, false, "malloc()");
dp = dh->devs[dh->ndevs];
if ((dp->dev = strdup(d->dev)) == NULL)
ERROR(dh, NULL, ERR_SYS_FATAL, false, "strdup()");
if (d->volid != NULL) {
if ((dp->volid = strdup(d->volid)) == NULL)
ERROR(dh, NULL, ERR_SYS_FATAL, false, "strdup()");
} else
dp->volid = NULL;
if (d->mntpt != NULL) {
if ((dp->mntpt = strdup(d->mntpt)) == NULL)
ERROR(dh, NULL, ERR_SYS_FATAL, false, "strdup()");
dp->mounted = true;
} else {
dp->mntpt = NULL;
dp->mounted = false;
}
if (d->fsname != NULL) {
if ((dp->fsname = strdup(d->fsname)) == NULL)
ERROR(dh, NULL, ERR_SYS_FATAL, false, "strdup()");
} else
dp->fsname = NULL;
dp->id = id++;
dp->type = d->type;
dp->cmds = d->cmds;
dp->speed = d->speed;
dp->removed = false;
/* Add our own commands to the device's command list, and set VolIDs. */
switch (d->type) {
case DSBMC_DT_AUDIOCD:
dp->volid = strdup("Audio CD");
case DSBMC_DT_DVD:
if (dp->volid == NULL)
dp->volid = strdup("DVD");
case DSBMC_DT_SVCD:
if (dp->volid == NULL)
dp->volid = strdup("SVCD");
case DSBMC_DT_VCD:
if (dp->volid == NULL)
dp->volid = strdup("VCD");
/* Playable media. */
dp->cmds |= DSBMC_CMD_PLAY;
}
if (dp->volid == NULL)
dp->volid = strdup(d->dev);
if (dp->volid == NULL)
ERROR(dh, NULL, ERR_SYS_FATAL, false, "strdup()");
if ((d->cmds & DSBMC_CMD_MOUNT)) {
/* Device we can open in a filemanager. */
dp->cmds |= DSBMC_CMD_OPEN;
}
return (dh->devs[dh->ndevs++]);
}
static void
del_device(dsbmc_t *dh, const char *dev)
{
int i;
for (i = (int)dh->ndevs - 1; i >= 0; i--) {
if (!dh->devs[i]->removed)
continue;
if (strcmp(dh->devs[i]->dev, dev) == 0)
break;
}
if (i < 0)
return;
free(dh->devs[i]->dev);
free(dh->devs[i]->volid);
free(dh->devs[i]->mntpt);
free(dh->devs[i]);
for (; i < (int)dh->ndevs - 1; i++)
dh->devs[i] = dh->devs[i + 1];
dh->ndevs--;
}
static void
set_removed(dsbmc_t *dh, const char *dev)
{
dsbmc_dev_t *dp;
if ((dp = lookup_device(dh, dev)) == NULL)
return;
dp->removed = true;
}
dsbmc_dev_t *
lookup_device(dsbmc_t *dh, const char *dev)
{
int i;
if (dev == NULL)
return (NULL);
for (i = dh->ndevs - 1; i >= 0; i--) {
if (dh->devs[i]->removed)
continue;
if (strcmp(dh->devs[i]->dev, dev) == 0)
return (dh->devs[i]);
}
for (i = dh->ndevs - 1; i >= 0; i--) {
if (strcmp(dh->devs[i]->dev, dev) == 0)
return (dh->devs[i]);
}
return (NULL);
}
char *
readln(dsbmc_t *dh)
{
int n;
size_t i;
if (dh->lbuf == NULL) {
if ((dh->lbuf = malloc(BUFSZ)) == NULL)
return (NULL);
dh->lbufsz = BUFSZ;
}
n = 0;
do {
dh->rd += n;
if (dh->slen > 0) {
(void)memmove(dh->lbuf, dh->lbuf + dh->slen,
dh->rd - dh->slen);
}
dh->rd -= dh->slen;
dh->slen = 0;
for (i = 0; i < dh->rd && dh->lbuf[i] != '\n'; i++)
;
if (i < dh->rd) {
dh->lbuf[i] = '\0';
dh->slen = i + 1;
if (dh->slen >= dh->lbufsz)
dh->slen = dh->rd = 0;
return (dh->lbuf);
}
if (dh->rd >= dh->lbufsz) {
dh->lbuf = realloc(dh->lbuf, dh->lbufsz + BUFSZ);
if (dh->lbuf == NULL) {
ERROR(dh, NULL, ERR_SYS_FATAL, false,
"realloc()");
}
dh->lbufsz += BUFSZ;
}
} while ((n = read(dh->socket, dh->lbuf + dh->rd,
dh->lbufsz - dh->rd)) > 0);
if (n == 0) {
dh->rd = dh->slen = 0;
ERROR(dh, NULL, DSBMC_ERR_LOST_CONNECTION, false,
"Lost connection to DSBMD");
} else if (n == -1) {
dh->rd = dh->slen = 0;
if (errno != EINTR && errno != EAGAIN)
ERROR(dh, NULL, ERR_SYS_FATAL, false, "read()");
}
return (NULL);
}
static char *
read_event(dsbmc_t *dh, bool block)
{
char *ln;
fd_set rset;
if ((ln = readln(dh)) == NULL) {
if ((dh->error & DSBMC_ERR_LOST_CONNECTION) || !block)
return (NULL);
} else
return (ln);
FD_ZERO(&rset); FD_SET(dh->socket, &rset);
/* Block until data is available. */
while (select(dh->socket + 1, &rset, NULL, NULL, NULL) == -1) {
if (errno != EINTR)
return (NULL);
else
ERROR(dh, NULL, ERR_SYS_FATAL, false, "select()");
}
return (readln(dh));
}
static int
push_event(dsbmc_t *dh, const char *e)
{
if (dh->evq.n >= DSBMC_MAXEQSZ)
ERROR(dh, -1, ERR_SYS_FATAL, false, "DSBMC_MAXEQSZ exceeded.");
if ((dh->evq.ln[dh->evq.n] = strdup(e)) == NULL)
ERROR(dh, -1, ERR_SYS_FATAL, false, "strdup()");
dh->evq.n++;
return (0);
}
static char *
pull_event(dsbmc_t *dh)
{
size_t i;
if (dh->evq.i >= dh->evq.n) {
/* Reset queue */
for (i = 0; i < dh->evq.n; i++) {
free(dh->evq.ln[i]);
dh->evq.ln[i] = NULL;
}
dh->evq.n = dh->evq.i = 0;
return (NULL);
}
return (dh->evq.ln[dh->evq.i++]);
}
static int
parse_event(dsbmc_t *dh, const char *str)
{
char *p, *q, *tmp, *k, *c;
size_t i, len;
struct dsbmdkeyword_s kwords[] = {
{ "+", KWTYPE_CHAR, (val_t)&dh->event.type },
{ "-", KWTYPE_CHAR, (val_t)&dh->event.type },
{ "E", KWTYPE_CHAR, (val_t)&dh->event.type },
{ "O", KWTYPE_CHAR, (val_t)&dh->event.type },
{ "M", KWTYPE_CHAR, (val_t)&dh->event.type },
{ "U", KWTYPE_CHAR, (val_t)&dh->event.type },
{ "V", KWTYPE_CHAR, (val_t)&dh->event.type },
{ "S", KWTYPE_CHAR, (val_t)&dh->event.type },
{ "=", KWTYPE_CHAR, (val_t)&dh->event.type },
{ "command=", KWTYPE_STRING, (val_t)&dh->event.command },
{ "dev=", KWTYPE_STRING, (val_t)&dh->event.devinfo.dev },
{ "fs=", KWTYPE_STRING, (val_t)&dh->event.devinfo.fsname },
{ "volid=", KWTYPE_STRING, (val_t)&dh->event.devinfo.volid },
{ "mntpt=", KWTYPE_STRING, (val_t)&dh->event.devinfo.mntpt },
{ "type=", KWTYPE_DSKTYPE, (val_t)&dh->event.devinfo.type },
{ "speed=", KWTYPE_UINT8, (val_t)&dh->event.devinfo.speed },
{ "code=", KWTYPE_INTEGER, (val_t)&dh->event.code },
{ "cmds=", KWTYPE_COMMANDS,(val_t)(char *)0 },
{ "mntcmderr=", KWTYPE_INTEGER, (val_t)&dh->event.mntcmderr },
{ "mediasize=", KWTYPE_UINT64, (val_t)&dh->event.mediasize },
{ "used=", KWTYPE_UINT64, (val_t)&dh->event.used },
{ "free=", KWTYPE_UINT64, (val_t)&dh->event.free }
};
const size_t nkwords = sizeof(kwords) / sizeof(kwords[0]);
/* Init */
len = strlen(str) + 1;
if (dh->pbufsz < len) {
dh->pbuf = realloc(dh->pbuf, len);
if (dh->pbuf == NULL)
ERROR(dh, -1, ERR_SYS_FATAL, false, "realloc()");
dh->pbufsz = len;
}
bzero(&dh->event, sizeof(dh->event));
(void)strlcpy(dh->pbuf, str, len);
for (i = 0; i < nkwords; i++) {
if (kwords[i].val.string != NULL)
*kwords[i].val.string = NULL;
}
for (p = dh->pbuf; (k = strsep(&p, ":\n")) != NULL;) {
for (i = 0; i < nkwords; i++) {
len = strlen(kwords[i].key);
if (strncmp(kwords[i].key, k, len) == 0)
break;
}
if (i == nkwords) {
warnx("Unknown keyword '%s'", k);
continue;
}
switch (kwords[i].type) {
case KWTYPE_STRING:
*kwords[i].val.string = k + len;
break;
case KWTYPE_CHAR:
*kwords[i].val.character = *k;
break;
case KWTYPE_UINT8:
*kwords[i].val.uint8 =
(uint8_t)strtol(k + len, NULL, 10);
break;
case KWTYPE_INTEGER:
*kwords[i].val.integer =
strtol(k + len, NULL, 10);
break;
case KWTYPE_UINT64:
*kwords[i].val.uint64 =
(uint64_t)strtoll(k + len, NULL, 10);
break;
case KWTYPE_COMMANDS:
dh->event.devinfo.cmds = 0;
if ((q = tmp = c = strdup(k + len)) == NULL)
ERROR(dh, -1, ERR_SYS_FATAL, false, "strdup()");
while ((c = strsep(&q, ",")) != NULL) {
for (i = 0; i < NCMDS; i++) {
if (strcmp(cmdtbl[i].name, c) == 0) {
dh->event.devinfo.cmds |=
cmdtbl[i].cmd;
}
}
}
free(tmp);
break;
case KWTYPE_DSKTYPE:
for (i = 0; i < NDSKTYPES; i++) {
if (strcmp(disktypetbl[i].name, k + len) == 0) {
dh->event.devinfo.type =
disktypetbl[i].type;
break;
}
}
break;
}
}
return (0);
}
/*
* Parses a string read from the dsbmd socket. Depending on the event type,
* process_event() adds or deletes drives from the list, or updates a drive's
* status.
*/
static int
process_event(dsbmc_t *dh, char *buf)
{
size_t i;
dsbmc_dev_t *d;
if (parse_event(dh, buf) != 0)
ERROR(dh, -1, 0, true, "parse_event()");
if (dh->event.type == DSBMC_EVENT_SUCCESS_MSG) {
if (dh->cmdqsz <= 0)
return (0);
d = dh->sender[0].dev;
} else {
switch (dh->event.type) {
case DSBMC_EVENT_MOUNT:
case DSBMC_EVENT_UNMOUNT:
case DSBMC_EVENT_SPEED:
d = lookup_device(dh, dh->event.devinfo.dev);
if (d == NULL) {
warnx("Unknown device %s",
dh->event.devinfo.dev);
return (-1);
}
}
}
switch (dh->event.type) {
case DSBMC_EVENT_SUCCESS_MSG:
if (dh->cmdqsz <= 0)
return (0);
if (dh->sender[0].id == DSBMC_CMD_MOUNT) {
d->mounted = true; free(d->mntpt);
d->mntpt = strdup(dh->event.devinfo.mntpt);
if (d->mntpt == NULL)
ERROR(dh, -1, ERR_SYS_FATAL, false, "strdup()");
} else if (dh->sender[0].id == DSBMC_CMD_UNMOUNT) {
d->mounted = false;
} else if (dh->sender[0].id == DSBMC_CMD_SIZE) {
d->mediasize = dh->event.mediasize;
d->used = dh->event.used;
d->free = dh->event.free;
} else if (dh->sender[0].id == DSBMC_CMD_SPEED) {
d->speed = dh->event.devinfo.speed;
}
case DSBMC_EVENT_ERROR_MSG:
if (dh->cmdqsz <= 0)
return (0);
dh->sender[0].callback(dh->event.code, dh->sender[0].dev);
free(dh->sender[0].cmd);
for (i = 0; i < dh->cmdqsz - 1; i++) {
dh->sender[i].dev = dh->sender[i + 1].dev;
dh->sender[i].callback = dh->sender[i + 1].callback;
dh->sender[i].cmd = dh->sender[i + 1].cmd;
dh->sender[i].id = dh->sender[i + 1].id;
}
if (--(dh->cmdqsz) == 0)
return (0);
if (send_string(dh, dh->sender[0].cmd) == -1)
ERROR(dh, -1, 0, true, "send_string()");
return (0);
case DSBMC_EVENT_ADD_DEVICE:
if ((d = add_device(dh, &dh->event.devinfo)) == NULL)
ERROR(dh, -1, 0, true, "add_device()");
return (1);
case DSBMC_EVENT_DEL_DEVICE:
set_removed(dh, dh->event.devinfo.dev);
return (1);
case DSBMC_EVENT_END_OF_LIST:
return (0);
case DSBMC_EVENT_SHUTDOWN:
return (1);
case DSBMC_EVENT_MOUNT:
d->mounted = true; free(d->mntpt);
d->mntpt = strdup(dh->event.devinfo.mntpt);
if (d->mntpt == NULL)
ERROR(dh, -1, ERR_SYS_FATAL, false, "strdup()");
return (1);
case DSBMC_EVENT_UNMOUNT:
d->mounted = false;
return (1);
case DSBMC_EVENT_SPEED:
d->speed = dh->event.devinfo.speed;
return (1);
default:
warnx("Invalid event received.");
return (-1);
}
return (0);
}
static int
dsbmc_send_async(dsbmc_t *dh, dsbmc_dev_t *dev,
void (*cb)(int, const dsbmc_dev_t *), const char *cmd, ...)
{
char *p;
size_t i, len;
va_list ap;
dsbmc_clearerr(dh);
if (dh->cmdqsz >= DSBMC_CMDQMAXSZ)
ERROR(dh, -1, DSBMC_ERR_CMDQ_BUSY, false, "Command queue busy");
if (dh->sbufsz == 0) {
if ((dh->sbuf = malloc(BUFSZ)) == NULL)
ERROR(dh, -1, ERR_SYS_FATAL, false, "malloc()");
dh->sbufsz = BUFSZ;
}
for (;;) {
va_start(ap, cmd);
if ((size_t)vsnprintf(dh->sbuf, dh->sbufsz, cmd, ap) < dh->sbufsz)
break;
/* Send buffer too small. */
if ((p = realloc(dh->sbuf, BUFSZ + dh->sbufsz)) == NULL)