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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
|
/***************************************************************************
* CVSID: $Id$
*
* Block device class
*
* Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
*
* Licensed under the Academic Free License version 2.0
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#define _GNU_SOURCE 1
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <assert.h>
#include <unistd.h>
#include <stdarg.h>
#include <limits.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/kdev_t.h>
#include <linux/cdrom.h>
#include <linux/fs.h>
#include <glib.h>
#include "../hald.h"
#include "../hald_dbus.h"
#include "../logger.h"
#include "../device_store.h"
#include "../callout.h"
#include "class_device.h"
#include "common.h"
#include "volume_id/volume_id.h"
#include "linux_dvd_rw_utils.h"
/**
* @defgroup HalDaemonLinuxBlock Block device class
* @ingroup HalDaemonLinux
* @brief Block device class
* @{
*/
static void etc_mtab_process_all_block_devices (dbus_bool_t force);
static void detect_fs (HalDevice *d);
static void
block_class_visit (ClassDeviceHandler *self,
const char *path,
struct sysfs_class_device *class_device)
{
HalDevice *d;
char *parent_sysfs_path;
ClassAsyncData *cad;
/* only care about given sysfs class name */
if (strcmp (class_device->classname, "block") != 0)
return;
d = hal_device_new ();
hal_device_store_add (hald_get_tdl (), d);
hal_device_property_set_string (d, "info.bus", self->hal_class_name);
hal_device_property_set_string (d, "linux.sysfs_path", path);
hal_device_property_set_string (d, "linux.sysfs_path_device", path);
if (class_device->sysdevice == NULL) {
parent_sysfs_path = get_parent_sysfs_path (path);
hal_device_property_set_bool (d, "block.is_volume", TRUE);
} else {
parent_sysfs_path = class_device->sysdevice->path;
hal_device_property_set_bool (d, "block.is_volume", FALSE);
}
/* temporary property used for _udev_event() */
hal_device_property_set_string (d, ".udev.sysfs_path", path);
hal_device_property_set_string (d, ".udev.class_name", "block");
/* Property name we should store the device file in */
hal_device_property_set_string (d, ".target_dev", "block.device");
/* Ask udev about the device file if we are probing */
if (self->require_device_file && hald_is_initialising) {
char dev_file[SYSFS_PATH_MAX];
if (!class_device_get_device_file (path, dev_file,
SYSFS_PATH_MAX)) {
HAL_WARNING (("Couldn't get device file for class "
"device with sysfs path", path));
return;
}
/* If we are not probing this function will be called upon
* receiving a dbus event */
self->udev_event (self, d, dev_file);
}
/* Now find the parent device; this happens asynchronously as it
* might be added later. */
cad = g_new0 (ClassAsyncData, 1);
cad->device = d;
cad->handler = self;
hal_device_store_match_key_value_string_async (
hald_get_gdl (),
"linux.sysfs_path_device",
parent_sysfs_path,
class_device_got_parent_device, cad,
HAL_LINUX_HOTPLUG_TIMEOUT);
}
static char *
strip_space (char *str)
{
int i, len;
len = strlen (str);
for (i = len - 1; i > 0 && isspace (str[i]); --i)
str[i] = '\0';
return str;
}
static void
cdrom_check(HalDevice *d, const char *device_file)
{
int fd, capabilities;
int read_speed, write_speed;
/* Check handling */
fd = open (device_file, O_RDONLY | O_NONBLOCK);
if (fd < 0)
return;
ioctl (fd, CDROM_SET_OPTIONS, CDO_USE_FFLAGS);
capabilities = ioctl (fd, CDROM_GET_CAPABILITY, 0);
if (capabilities < 0) {
close(fd);
return;
}
hal_device_property_set_bool (d, "storage.cdrom.cdr", FALSE);
hal_device_property_set_bool (d, "storage.cdrom.cdrw", FALSE);
hal_device_property_set_bool (d, "storage.cdrom.dvd", FALSE);
hal_device_property_set_bool (d, "storage.cdrom.dvdr", FALSE);
hal_device_property_set_bool (d, "storage.cdrom.dvdram", FALSE);
hal_device_property_set_bool (d, "storage.cdrom.dvdplusr", FALSE);
hal_device_property_set_bool (d, "storage.cdrom.dvdplusrw", FALSE);
if (capabilities & CDC_CD_R) {
hal_device_property_set_bool (d, "storage.cdrom.cdr", TRUE);
}
if (capabilities & CDC_CD_RW) {
hal_device_property_set_bool (d, "storage.cdrom.cdrw", TRUE);
}
if (capabilities & CDC_DVD) {
int profile;
hal_device_property_set_bool (d, "storage.cdrom.dvd", TRUE);
profile = get_dvd_r_rw_profile (fd);
HAL_INFO (("profile %d\n", profile));
if (profile == 2) {
hal_device_property_set_bool (d, "storage.cdrom.dvdplusr", TRUE);
hal_device_property_set_bool (d, "storage.cdrom.dvdplusrw", TRUE);
} else if (profile == 0) {
hal_device_property_set_bool(d, "storage.cdrom.dvdplusr",
TRUE);
} else if (profile == 1) {
hal_device_property_set_bool (d, "storage.cdrom.dvdplusrw", TRUE);
}
}
if (capabilities & CDC_DVD_R) {
hal_device_property_set_bool (d, "storage.cdrom.dvdr", TRUE);
}
if (capabilities & CDC_DVD_RAM) {
hal_device_property_set_bool (d, "storage.dvdram", TRUE);
}
/* while we're at it, check if we support media changed */
if (ioctl (fd, CDROM_MEDIA_CHANGED) >= 0) {
hal_device_property_set_bool (d, "storage.cdrom.support_media_changed", TRUE);
} else {
hal_device_property_set_bool (d, "storage.cdrom.support_media_changed", FALSE);
}
if (get_read_write_speed(fd, &read_speed, &write_speed) >= 0) {
hal_device_property_set_int (d, "storage.cdrom.read_speed", read_speed);
if (write_speed > 0)
hal_device_property_set_int(d, "storage.cdrom.write_speed", write_speed);
else
hal_device_property_set_int(d, "storage.cdrom.write_speed", 0);
}
close (fd);
}
/** Force unmount of a patition. Must have block.volume=1 and valid
* block.device
*
* @param d Device
*/
static void
force_unmount (HalDevice * d)
{
const char *device_file;
const char *device_mount_point;
const char *umount_argv[4] = { "/bin/umount", "-l", NULL, NULL };
char *umount_stdout;
char *umount_stderr;
int umount_exitcode;
device_file = hal_device_property_get_string (d, "block.device");
device_mount_point =
hal_device_property_get_string (d, "volume.mount_point");
umount_argv[2] = device_file;
if (hal_device_has_property (d, "block.is_volume") &&
hal_device_property_get_bool (d, "block.is_volume") &&
device_mount_point != NULL &&
strlen (device_mount_point) > 0) {
HAL_INFO (("attempting /bin/umount -l %s", device_file));
/* invoke umount */
if (g_spawn_sync ("/",
(char **) umount_argv,
NULL,
0,
NULL,
NULL,
&umount_stdout,
&umount_stderr,
&umount_exitcode, NULL) != TRUE) {
HAL_ERROR (("Couldn't invoke /bin/umount"));
}
if (umount_exitcode != 0) {
HAL_INFO (("/bin/umount returned %d",
umount_exitcode));
} else {
/* Tell clients we are going to unmount so they close
* can files - otherwise this unmount is going to stall
*
* One candidate for catching this would be FAM - the
* File Alteration Monitor
*
* Lazy unmount been in Linux since 2.4.11, so we're
* homefree (but other OS'es might not support this)
*/
HAL_INFO (("Goint to emit BlockForcedUnmountPartition('%s', '%s', TRUE)", device_file, device_mount_point));
device_send_signal_condition (d,
"BlockForcedUnmountPartition",
DBUS_TYPE_STRING,
device_file,
DBUS_TYPE_STRING,
device_mount_point,
DBUS_TYPE_BOOLEAN,
TRUE,
DBUS_TYPE_INVALID);
/* Woohoo, have to change volume.mount_point *afterwards*, other
* wise device_mount_point points to garbage and D-BUS throws
* us off the bus, in fact it's doing exiting with code 1
* for us - not nice
*/
device_property_atomic_update_begin ();
hal_device_property_set_string (d, "volume.mount_point",
"");
hal_device_property_set_string (d, "volume.fstype", "");
hal_device_property_set_bool (d, "volume.is_mounted",
FALSE);
device_property_atomic_update_end ();
}
}
}
/** Unmount all partitions that stems from this block device. Must have
* block.is_volume==0
*
* @param d Device
*/
static void
force_unmount_of_all_childs (HalDevice * d)
{
int fd;
const char *device_file;
GSList *children;
device_file = hal_device_property_get_string (d, "block.device");
children = hal_device_store_match_multiple_key_value_string (
hald_get_gdl (),
"info.parent",
hal_device_get_udi (d));
if (children != NULL) {
GSList *iter;
for (iter = children; iter != NULL; iter = iter->next) {
HalDevice *child = HAL_DEVICE (iter->data);
force_unmount (child);
} /* for all children */
g_slist_free (children);
HAL_INFO (("Rereading partition table for %s",
device_file));
fd = open (device_file, O_RDONLY | O_NONBLOCK);
if (fd != -1) {
ioctl (fd, BLKRRPART);
}
close (fd);
/* All this work should generate hotplug events to actually
* remove the child devices
*/
/* Finally, send a single signal on the device - this
* is useful for desktop policy clients such as g-v-m
* such that only a single annoying "dude, you need to
* *stop* the device before pulling it out" popup is
* displayed */
HAL_INFO (("Goint to emit BlockForcedUnmount('%s')",
device_file));
device_send_signal_condition (d, "BlockForcedUnmount",
DBUS_TYPE_STRING, device_file,
DBUS_TYPE_INVALID);
} /* childs!=NULL */
}
static void
disc_add_to_gdl (HalDevice *device, gpointer user_data)
{
hal_device_store_remove (hald_get_tdl (), device);
hal_device_store_add (hald_get_gdl (), device);
g_signal_handlers_disconnect_by_func (device, disc_add_to_gdl,
user_data);
}
static void
disc_remove_from_gdl (HalDevice *device, gpointer user_data)
{
hal_device_store_remove (hald_get_gdl (), device);
g_signal_handlers_disconnect_by_func (device, disc_remove_from_gdl,
user_data);
g_object_unref (device);
}
/** Check for media on a block device that is not a volume
*
* @param d Device to inspect; can be any device, but
* it will only have effect if the device is
* in the GDL and is of capability block and
* is not a volume
* @param TRUE iff the GDL was modified
*/
static dbus_bool_t
detect_media (HalDevice * d)
{
int fd;
dbus_bool_t is_cdrom;
const char *device_file;
HalDevice *child;
/* need to be in GDL */
if (!hal_device_store_find (hald_get_gdl (),
hal_device_get_udi (d)))
return FALSE;
/* need to have a device and not be a volume */
if (!hal_device_has_property (d, "block.is_volume") ||
!hal_device_has_property (d, "block.device") ||
hal_device_property_get_bool (d, "block.is_volume"))
return FALSE;
device_file = hal_device_property_get_string (d, "block.device");
if (device_file == NULL)
return FALSE;
/* we do special treatment for optical discs */
is_cdrom = hal_device_has_property (d, "storage.drive_type") &&
strcmp (hal_device_property_get_string (d, "storage.drive_type"),
"cdrom") == 0
&& hal_device_property_get_bool (d,
"storage.cdrom.support_media_changed");
if (!is_cdrom) {
fd = open (device_file, O_RDONLY);
if (fd == -1) {
/* open failed */
HAL_WARNING (("open(\"%s\", O_RDONLY) failed, "
"errno=%d", device_file, errno));
if (errno == ENOMEDIUM) {
force_unmount_of_all_childs (d);
}
}
} /* device is not an optical drive */
else {
int drive;
dbus_bool_t got_disc = FALSE;
fd = open (device_file, O_RDONLY | O_NONBLOCK | O_EXCL);
if (fd == -1) {
/* open failed */
/*HAL_WARNING (("open(\"%s\", O_RDONLY|O_NONBLOCK|O_EXCL) failed, " "errno=%d", device_file, errno));*/
return FALSE;
}
drive = ioctl (fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
switch (drive) {
/* explicit fallthrough */
case CDS_NO_INFO:
case CDS_NO_DISC:
case CDS_TRAY_OPEN:
case CDS_DRIVE_NOT_READY:
break;
case CDS_DISC_OK:
got_disc = TRUE;
break;
default:
break;
}
if (!got_disc) {
/* we get to here if there is no disc in the drive */
child = hal_device_store_match_key_value_string (
hald_get_gdl (), "info.parent",
hal_device_get_udi (d));
if (child != NULL) {
HAL_INFO (("Removing volume for optical device %s", device_file));
g_signal_connect (child, "callouts_finished",
G_CALLBACK (disc_remove_from_gdl), NULL);
hal_callout_device (child, FALSE);
close (fd);
/* GDL was modified */
return TRUE;
}
close (fd);
return FALSE;
}
/* got a disc in drive, */
/* disc in drive; check if the HAL device representing
* the optical drive already got a child (it can have
* only one child)
*/
child = hal_device_store_match_key_value_string (
hald_get_gdl (), "info.parent",
hal_device_get_udi (d));
if (child == NULL) {
child = hal_device_store_match_key_value_string (
hald_get_tdl (), "info.parent",
hal_device_get_udi (d));
}
if (child == NULL) {
int type;
char udi[256];
/* nope, add child */
HAL_INFO (("Adding volume for optical device %s",
device_file));
child = hal_device_new ();
hal_device_store_add (hald_get_tdl (), child);
/* copy from parent */
hal_device_merge (child, d);
/* modify some properties */
hal_device_property_set_string (child, "info.parent",
d->udi);
hal_device_property_set_bool (child, "block.is_volume",
TRUE);
hal_device_property_set_string (child, "info.capabilities",
"block volume");
hal_device_property_set_string (child, "info.category",
"volume");
hal_device_property_set_string (child, "info.product",
"Disc");
/* set defaults */
hal_device_property_set_string (
child, "volume.label", "");
hal_device_property_set_string (
child, "volume.uuid", "");
hal_device_property_set_string (
child, "volume.fstype", "");
hal_device_property_set_string (
child, "volume.mount_point", "");
hal_device_property_set_bool (
child, "volume.is_mounted", FALSE);
hal_device_property_set_bool (
child, "volume.is_disc", TRUE);
hal_device_property_set_string (
child, "volume.disc.type", "unknown");
hal_device_property_set_bool (
child, "volume.disc.has_audio", FALSE);
hal_device_property_set_bool (
child, "volume.disc.has_data", FALSE);
hal_device_property_set_bool (
child, "volume.disc.is_blank", FALSE);
hal_device_property_set_bool (
child, "volume.disc.is_appendable", FALSE);
hal_device_property_set_bool (
child, "volume.disc.is_rewritable", FALSE);
/* set UDI as appropriate */
strncpy (udi,
hal_device_property_get_string (d, "info.udi"),
256);
strncat (udi, "-disc", 256);
hal_device_property_set_string (child, "info.udi", udi);
hal_device_set_udi (child, udi);
/* check for audio/data/blank */
type = ioctl (fd, CDROM_DISC_STATUS, CDSL_CURRENT);
switch (type) {
case CDS_AUDIO: /* audio CD */
hal_device_property_set_bool (
child, "volume.disc.has_audio", TRUE);
break;
case CDS_MIXED: /* mixed mode CD */
hal_device_property_set_bool (
child, "volume.disc.has_audio", TRUE);
hal_device_property_set_bool (
child, "volume.disc.has_data", TRUE);
break;
case CDS_DATA_1: /* data CD */
case CDS_DATA_2:
case CDS_XA_2_1:
case CDS_XA_2_2:
hal_device_property_set_bool (
child, "volume.disc.has_data", TRUE);
break;
case CDS_NO_INFO: /* blank or invalid CD */
hal_device_property_set_bool (
child, "volume.disc.is_blank", TRUE);
break;
default: /* should never see this */
hal_device_property_set_string (child,
"volume.disc_type",
"unknown");
break;
}
/* see table 373 in MMC-3 for details on disc type
* http://www.t10.org/drafts.htm#mmc3
*/
type = get_disc_type (fd);
HAL_INFO (("get_disc_type returned 0x%02x", type));
if (type != -1) {
switch (type) {
case 0x08: /* CD-ROM */
hal_device_property_set_string (
child,
"volume.disc.type",
"cd_rom");
break;
case 0x09: /* CD-R */
hal_device_property_set_string (
child,
"volume.disc.type",
"cd_r");
break;
case 0x0a: /* CD-RW */
hal_device_property_set_string (
child,
"volume.disc.type",
"cd_rw");
hal_device_property_set_bool (
child,
"volume.disc.is_rewritable",
TRUE);
break;
case 0x10: /* DVD-ROM */
hal_device_property_set_string (
child,
"volume.disc.type",
"dvd_rom");
break;
case 0x11: /* DVD-R Sequential */
hal_device_property_set_string (
child,
"volume.disc.type",
"dvd_r");
break;
case 0x12: /* DVD-RAM */
hal_device_property_set_string (
child,
"volume.disc.type",
"dvd_ram");
hal_device_property_set_bool (
child,
"volume.disc.is_rewritable",
TRUE);
break;
case 0x13: /* DVD-RW Restricted Overwrite */
hal_device_property_set_string (
child,
"volume.disc.type",
"dvd_rw");
hal_device_property_set_bool (
child,
"volume.disc.is_rewritable",
TRUE);
break;
case 0x14: /* DVD-RW Sequential */
hal_device_property_set_string (
child,
"volume.disc.type",
"dvd_rw");
hal_device_property_set_bool (
child,
"volume.disc.is_rewritable",
TRUE);
break;
case 0x1A: /* DVD+RW */
hal_device_property_set_string (
child,
"volume.disc.type",
"dvd_plus_rw");
hal_device_property_set_bool (
child,
"volume.disc.is_rewritable",
TRUE);
break;
case 0x1B: /* DVD+R */
hal_device_property_set_string (
child,
"volume.disc.type",
"dvd_plus_r");
hal_device_property_set_bool (
child,
"volume.disc.is_rewritable",
TRUE);
break;
default:
break;
}
}
HAL_INFO (("BAR"));
if (disc_is_appendable (fd)) {
hal_device_property_set_bool (
child,
"volume.disc.is_appendable",
TRUE);
}
HAL_INFO (("FOO"));
close(fd);
detect_fs (child);
/* add new device */
g_signal_connect (child, "callouts_finished",
G_CALLBACK (disc_add_to_gdl), NULL);
hal_callout_device (child, TRUE);
/* GDL was modified */
return TRUE;
}
} /* if( is optical drive ) */
close (fd);
return FALSE;
}
static void
block_class_got_udi (ClassDeviceHandler *self,
HalDevice *d,
const char *udi)
{
const char *stordev_udi;
char temp_prefix[] = "/org/freedesktop/Hal/devices/temp";
/* fixup from setting block.storage_device below to a temporary
* device */
stordev_udi = hal_device_property_get_string (d,
"block.storage_device");
if (strncmp (stordev_udi, temp_prefix, strlen(temp_prefix)) == 0) {
hal_device_property_set_string (d,
"block.storage_device",
udi);
}
}
static void
detect_fs (HalDevice *d)
{
struct volume_id *vid;
const char *device_file;
int rc;
device_file = hal_device_property_get_string (d, "block.device");
vid = volume_id_open_node(device_file);
if (vid == NULL)
return;
rc = volume_id_probe(vid, ALL);
if (rc != 0) {
volume_id_close(vid);
return;
}
hal_device_property_set_string (d, "volume.fstype", vid->fs_name);
if (vid->label_string[0] != '\0')
hal_device_property_set_string (d, "volume.label", vid->label_string);
if (vid->uuid_string[0] != '\0')
hal_device_property_set_string (d, "volume.uuid", vid->uuid_string);
volume_id_close(vid);
return;
}
static void
block_class_pre_process (ClassDeviceHandler *self,
HalDevice *d,
const char *sysfs_path,
struct sysfs_class_device *class_device)
{
int major, minor;
HalDevice *parent;
HalDevice *stordev = NULL;
HalDevice *physdev = NULL;
HalDevice *scsidev = NULL;
const char *stordev_udi;
const char *device_file;
dbus_bool_t has_removable_media = FALSE;
dbus_bool_t is_hotpluggable = FALSE;
parent = hal_device_store_find (hald_get_gdl (),
hal_device_property_get_string (
d, "info.parent"));
assert (parent != NULL);
/* add capabilities for device */
hal_device_property_set_string (d, "info.category", "block");
hal_device_add_capability (d, "block");
class_device_get_major_minor (sysfs_path, &major, &minor);
hal_device_property_set_int (d, "block.major", major);
hal_device_property_set_int (d, "block.minor", minor);
device_file = hal_device_property_get_string (d, "block.device");
/* Determine physical device that is backing this block device */
if (hal_device_property_get_bool (d, "block.is_volume")) {
/* Take the block parent */
stordev_udi = hal_device_property_get_string (
parent, "block.storage_device");
stordev = hal_device_store_find (hald_get_gdl (), stordev_udi);
} else {
const char *udi_it;
const char *physdev_udi = NULL;
/* Set ourselves to be the storage.* keeper */
stordev_udi = d->udi;
stordev = d;
/* Defaults */
hal_device_property_set_string (
stordev, "storage.bus", "unknown");
hal_device_property_set_string (stordev,
"storage.model", "");
hal_device_property_set_string (stordev,
"storage.vendor", "");
/* walk up the device chain to find the physical device,
* start with our parent. On the way, optionally pick up
* the scsi_device if it exists */
udi_it = parent->udi;
while (udi_it != NULL) {
HalDevice *d_it;
const char *bus;
/* Find device */
d_it = hal_device_store_find (hald_get_gdl (), udi_it);
assert (d_it != NULL);
/* Check info.bus */
bus = hal_device_property_get_string (d_it,"info.bus");
if (strcmp (bus, "scsi_device") == 0) {
scsidev = d_it;
}
if (strcmp (bus, "usb") == 0) {
physdev = d_it;
physdev_udi = udi_it;
is_hotpluggable = TRUE;
hal_device_property_set_string (
stordev, "storage.bus", "usb");
break;
} else if (strcmp (bus, "ieee1394") == 0) {
physdev = d_it;
physdev_udi = udi_it;
is_hotpluggable = TRUE;
hal_device_property_set_string (
stordev, "storage.bus", "ieee1394");
break;
} else if (strcmp (bus, "ide") == 0) {
physdev = d_it;
physdev_udi = udi_it;
hal_device_property_set_string (
stordev, "storage.bus", "ide");
break;
}
/* Go to parent */
udi_it = hal_device_property_get_string (
d_it, "info.parent");
}
if (physdev_udi != NULL) {
hal_device_property_set_string (
stordev,
"storage.physical_device",
physdev_udi);
}
}
hal_device_property_set_string (d, "block.storage_device",
stordev_udi);
if (hal_device_property_get_bool (d, "block.is_volume")) {
/* We are a volume */
find_and_set_physical_device (d);
hal_device_property_set_bool (d, "info.virtual", TRUE);
hal_device_add_capability (d, "volume");
hal_device_property_set_string (d, "info.category", "volume");
hal_device_property_set_string (d, "volume.label", "");
hal_device_property_set_string (d, "volume.uuid", "");
hal_device_property_set_bool (d, "volume.is_disc", FALSE);
/* block device that is a partition; e.g. a storage volume */
/** @todo Guestimate product name; use volume label */
hal_device_property_set_string (d, "info.product", "Volume");
/* Detect filesystem and volume label */
detect_fs (d);
/* Not much to do for volumes; our parent already set the
* appropriate values for the storage device backing us */
return;
}
/* We are a disk or cdrom drive; maybe we even offer
* removable media
*/
hal_device_property_set_string (d, "info.category", "block");
HAL_INFO (("Bus type is %s!",
hal_device_property_get_string (parent, "info.bus")));
if (strcmp (hal_device_property_get_string (parent, "info.bus"),
"ide") == 0) {
const char *ide_name;
char *model;
char *media;
ide_name = get_last_element (hal_device_property_get_string
(d, "linux.sysfs_path"));
model = read_single_line ("/proc/ide/%s/model", ide_name);
if (model != NULL) {
hal_device_property_set_string (stordev,
"storage.model",
model);
hal_device_property_set_string (d,
"info.product",
model);
}
/* According to the function proc_ide_read_media() in
* drivers/ide/ide-proc.c in the Linux sources, media
* can only assume "disk", "cdrom", "tape", "floppy",
* "UNKNOWN"
*/
/** @todo Given floppy how
* do we determine it's LS120?
*/
media = read_single_line ("/proc/ide/%s/media",
ide_name);
if (media != NULL) {
hal_device_property_set_string (stordev,
"storage.drive_type",
media);
/* Set for removable media */
if (strcmp (media, "disk") == 0) {
/* left blank */
} else if (strcmp (media, "cdrom") == 0) {
has_removable_media = TRUE;
} else if (strcmp (media, "floppy") == 0) {
has_removable_media = TRUE;
} else if (strcmp (media, "tape") == 0) {
has_removable_media = TRUE;
}
}
} else if (strcmp (hal_device_property_get_string (parent,
"info.bus"),
"scsi_device") == 0) {
const char *sysfs_path;
char attr_path[SYSFS_PATH_MAX];
struct sysfs_attribute *attr;
sysfs_path = hal_device_property_get_string (
d, "linux.sysfs_path");
snprintf (attr_path, SYSFS_PATH_MAX,
"%s/device/vendor", sysfs_path);
attr = sysfs_open_attribute (attr_path);
if (sysfs_read_attribute (attr) >= 0) {
strip_space (attr->value);
hal_device_property_set_string (d, "info.vendor",
attr->value);
hal_device_property_set_string (stordev,
"storage.vendor",
attr->value);
sysfs_close_attribute (attr);
}
snprintf (attr_path, SYSFS_PATH_MAX,
"%s/device/model", sysfs_path);
attr = sysfs_open_attribute (attr_path);
if (sysfs_read_attribute (attr) >= 0) {
strip_space (attr->value);
hal_device_property_set_string (d,
"info.product",
attr->value);
hal_device_property_set_string (stordev,
"storage.model",
attr->value);
sysfs_close_attribute (attr);
}
snprintf (attr_path, SYSFS_PATH_MAX,
"%s/device/type", sysfs_path);
attr = sysfs_open_attribute (attr_path);
if (sysfs_read_attribute (attr) >= 0) {
int type = parse_dec (attr->value);
switch (type) {
case 0: /* Disk */
hal_device_property_set_string (
stordev,
"storage.drive_type",
"disk");
break;
case 1: /* Tape */
has_removable_media = TRUE;
hal_device_property_set_string (
stordev,
"storage.drive_type",
"tape");
has_removable_media = TRUE;
break;
case 5: /* CD-ROM */
hal_device_property_set_string (
stordev,
"storage.drive_type",
"cdrom");
has_removable_media = TRUE;
break;
default:
/** @todo add more SCSI types */
HAL_WARNING (("Don't know how to "
"handle SCSI type %d",
type));
}
}
} else {
/** @todo block device on non-IDE and non-SCSI device;
* how to find the name and the media-type?
*/
/* guestimate product name */
hal_device_property_set_string (d, "info.product", "Disk");
}
hal_device_property_set_bool (
stordev,
"storage.removable",
has_removable_media);
if (hal_device_has_property (stordev, "storage.drive_type") &&
strcmp (hal_device_property_get_string (stordev, "storage.drive_type"),
"cdrom") == 0) {
cdrom_check (stordev, device_file);
}
hal_device_property_set_string (stordev, "info.category", "storage");
hal_device_add_capability (stordev, "storage");
hal_device_property_set_bool (stordev, "storage.hotpluggable",
is_hotpluggable);
/* FINALLY, merge information derived from a .fdi file, from the
* physical device that is backing this block device.
*
* - physdev is either NULL or the physical device (ide,
* usb, iee1394 etc.) of which we are the offspring
*
* - scsidev is either NULL or the SCSI device inbetween
* us and the physical device
*/
/* Merge storage.lun%d.* to storage.* from physical device for the
* appropriate LUN */
if (physdev != NULL) {
/* Merge storage.* from physdev to stordev */
hal_device_merge_with_rewrite (stordev, physdev,
"storage.", "storage.");
/* If there's a scsi device inbetween merge all
* storage.lun%d.* properties */
if (scsidev != NULL) {
int lun;
char propname[64];
lun = hal_device_property_get_int (
scsidev, "scsi_device.lun");
/* See 6in1-card-reader.fdi for an example */
snprintf (propname, sizeof (propname),
"storage_lun%d.", lun);
hal_device_merge_with_rewrite (stordev, physdev,
"storage.", propname);
}
}
/* check for media on the device */
detect_media (d);
/* Check the mtab to see if the device is mounted */
etc_mtab_process_all_block_devices (TRUE);
}
static char *
block_class_compute_udi (HalDevice * d, int append_num)
{
char *format;
static char buf[256];
if (append_num == -1)
format = "/org/freedesktop/Hal/devices/block_%d_%d";
else
format = "/org/freedesktop/Hal/devices/block_%d_%d-%d";
snprintf (buf, 256, format,
hal_device_property_get_int (d, "block.major"),
hal_device_property_get_int (d, "block.minor"), append_num);
return buf;
}
#define MOUNT_POINT_MAX 256
#define MOUNT_POINT_STRING_SIZE 128
/** Structure for holding mount point information */
struct mount_point_s {
int major; /**< Major device number */
int minor; /**< Minor device number */
char device[MOUNT_POINT_STRING_SIZE]; /**< Device filename */
char mount_point[MOUNT_POINT_STRING_SIZE];
/**< Mount point */
char fs_type[MOUNT_POINT_STRING_SIZE]; /**< Filesystem type */
};
/** Array holding (valid) mount points from /etc/mtab. */
static struct mount_point_s mount_points[MOUNT_POINT_MAX];
/** Number of elements in #mount_points array */
static int num_mount_points;
static int etc_fd = -1;
/** Process a line in /etc/mtab. The given string will be modifed by
* this function.
*
* @param s Line of /etc/mtab
*/
static void
etc_mtab_process_line (char *s)
{
int i;
char *p;
char *delim = " \t\n";
char buf[256];
char *bufp = buf;
struct stat stat_buf;
int major = 0;
int minor = 0;
char *device = NULL;
char *mount_point = NULL;
char *fs_type = NULL;
i = 0;
p = strtok_r (s, delim, &bufp);
while (p != NULL) {
/*printf("token: '%s'\n", p); */
switch (i) {
case 0:
if (strcmp (p, "none") == 0)
return;
if (p[0] != '/')
return;
device = p;
/* Find major/minor for this device */
if (stat (p, &stat_buf) != 0) {
return;
}
major = MAJOR (stat_buf.st_rdev);
minor = MINOR (stat_buf.st_rdev);
break;
case 1:
mount_point = p;
break;
case 2:
fs_type = p;
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
p = strtok_r (NULL, delim, &bufp);
i++;
}
/** @todo FIXME: Use a linked list or something that doesn't restrict
* us like this
*/
if (num_mount_points == MOUNT_POINT_MAX)
return;
mount_points[num_mount_points].major = major;
mount_points[num_mount_points].minor = minor;
strncpy (mount_points[num_mount_points].device, device,
MOUNT_POINT_STRING_SIZE);
strncpy (mount_points[num_mount_points].mount_point, mount_point,
MOUNT_POINT_STRING_SIZE);
strncpy (mount_points[num_mount_points].fs_type, fs_type,
MOUNT_POINT_STRING_SIZE);
num_mount_points++;
}
/** Last mtime when /etc/mtab was processed */
static time_t etc_mtab_mtime = 0;
/** Reads /etc/mtab and fill out #mount_points and #num_mount_points
* variables accordingly
*
* This function holds the file open for further access
*
* @param force Force reading of mtab
* @return FALSE if there was no changes to /etc/mtab
* since last invocation or an error occured
*/
static dbus_bool_t
read_etc_mtab (dbus_bool_t force)
{
int fd;
char buf[256];
FILE *f;
struct stat stat_buf;
num_mount_points = 0;
fd = open ("/etc/mtab", O_RDONLY);
if (fd == -1) {
HAL_ERROR (("Cannot open /etc/mtab"));
return FALSE;
}
if (fstat (fd, &stat_buf) != 0) {
HAL_ERROR (("Cannot fstat /etc/mtab fd, errno=%d", errno));
close(fd);
return FALSE;
}
if (!force && etc_mtab_mtime == stat_buf.st_mtime) {
/*printf("No modification, etc_mtab_mtime=%d\n", etc_mtab_mtime); */
close(fd);
return FALSE;
}
etc_mtab_mtime = stat_buf.st_mtime;
/*printf("Modification, etc_mtab_mtime=%d\n", etc_mtab_mtime); */
f = fdopen (fd, "r");
if (f == NULL) {
HAL_ERROR (("Cannot fdopen /etc/mtab fd"));
return FALSE;
}
while (!feof (f)) {
if (fgets (buf, 256, f) == NULL)
break;
/*printf("got line: '%s'\n", buf); */
etc_mtab_process_line (buf);
}
fclose (f);
return TRUE;
}
static void sigio_handler (int sig);
/** Global to see if we have setup the watcher on /etc */
static dbus_bool_t have_setup_watcher = FALSE;
static gboolean
foreach_block_device (HalDeviceStore *store, HalDevice *d,
gpointer user_data)
{
const char *bus;
int major, minor;
dbus_bool_t found_mount_point;
struct mount_point_s *mp;
int i;
bus = hal_device_property_get_string (d, "info.bus");
if (bus == NULL ||
strncmp (bus, "block", 5) != 0 ||
!hal_device_property_get_bool (d, "block.is_volume"))
return TRUE;
major = hal_device_property_get_int (d, "block.major");
minor = hal_device_property_get_int (d, "block.minor");
/* Search all mount points */
found_mount_point = FALSE;
for (i = 0; i < num_mount_points; i++) {
mp = &mount_points[i];
if (mp->major == major && mp->minor == minor) {
const char *existing_block_device;
dbus_bool_t was_mounted;
HAL_INFO (("%s mounted at %s, major:minor=%d:%d, fstype=%s, udi=%s", mp->device, mp->mount_point, mp->major, mp->minor, mp->fs_type, d->udi));
device_property_atomic_update_begin ();
existing_block_device =
hal_device_property_get_string (d,
"block.device");
was_mounted =
hal_device_property_get_bool (d,
"volume.is_mounted");
/* Yay! Found a mount point; set properties accordingly */
hal_device_property_set_string (d,
"volume.mount_point",
mp->mount_point);
hal_device_property_set_string (d, "volume.fstype",
mp->fs_type);
hal_device_property_set_bool (d,
"volume.is_mounted",
TRUE);
/* only overwrite block.device if it's not set */
if (existing_block_device == NULL ||
(existing_block_device != NULL &&
strcmp (existing_block_device,
"") == 0)) {
hal_device_property_set_string (d,
"block.device",
mp->
device);
}
device_property_atomic_update_end ();
if (!was_mounted) {
device_send_signal_condition (
d,
"BlockMountEvent",
DBUS_TYPE_STRING,
hal_device_property_get_string (
d,
"block.device"),
DBUS_TYPE_STRING,
mp->mount_point,
DBUS_TYPE_STRING,
mp->fs_type,
DBUS_TYPE_INVALID);
}
found_mount_point = TRUE;
break;
}
}
/* No mount point found; (possibly) remove all information */
if (!found_mount_point) {
dbus_bool_t was_mounted;
device_property_atomic_update_begin ();
was_mounted =
hal_device_property_get_bool (d, "volume.is_mounted");
hal_device_property_set_bool (d, "volume.is_mounted",
FALSE);
hal_device_property_set_string (d, "volume.mount_point",
"");
hal_device_property_set_string (d, "volume.fstype", "");
device_property_atomic_update_end ();
if (was_mounted) {
device_send_signal_condition (
d, "BlockUnmountEvent",
DBUS_TYPE_STRING,
hal_device_property_get_string (
d,
"block.device"),
DBUS_TYPE_INVALID);
}
}
return TRUE;
}
/** Load /etc/mtab and process all HAL block devices and set properties
* according to mount status. Also, optionally, sets up a watcher to do
* this whenever /etc/mtab changes
*
* @param force Force reading of mtab
*/
static void
etc_mtab_process_all_block_devices (dbus_bool_t force)
{
/* Start or continue watching /etc */
if (!have_setup_watcher) {
have_setup_watcher = TRUE;
signal (SIGIO, sigio_handler);
etc_fd = open ("/etc", O_RDONLY);
fcntl (etc_fd, F_NOTIFY, DN_MODIFY | DN_MULTISHOT);
}
/* Just return if /etc/mtab wasn't modified */
if (!read_etc_mtab (force))
return;
if (!force)
HAL_INFO (("/etc/mtab changed, processing all block devices"));
else
HAL_INFO (("processing /etc/mtab"));
hal_device_store_foreach (hald_get_gdl (), foreach_block_device, NULL);
}
/** Will be set to true by the SIGIO handler */
static dbus_bool_t sigio_etc_changed = FALSE;
/** Signal handler for watching /etc
*
* @param sig Signal number
*/
static void
sigio_handler (int sig)
{
/* Set a variable instead of handling it now - this is *much* safer
* since this handler must be very careful - man signal for more
* information
*/
sigio_etc_changed = TRUE;
}
static void
block_class_removed (ClassDeviceHandler* self,
const char *sysfs_path,
HalDevice *d)
{
if (hal_device_has_property (d, "block.is_volume")) {
if (hal_device_property_get_bool (d, "block.is_volume")) {
force_unmount (d);
} else {
force_unmount_of_all_childs (d);
}
}
}
static gboolean
foreach_detect_media (HalDeviceStore *store, HalDevice *device,
gpointer user_data)
{
/** @todo FIXME GDL was modifed so we have to break here because we
* are iterating over devices and this will break it
*/
if (detect_media (device))
return FALSE;
else
return TRUE;
}
static void
block_class_tick (ClassDeviceHandler *self)
{
/*HAL_INFO(("entering")); */
hal_device_store_foreach (hald_get_gdl (), foreach_detect_media, NULL);
/* check if the SIGIO signal handler delivered something to us */
if (sigio_etc_changed) {
/* acknowledge we got it */
sigio_etc_changed = FALSE;
HAL_INFO (("Directory /etc changed"));
/* don't force reloading of /etc/mtab */
etc_mtab_process_all_block_devices (FALSE);
}
/* HAL_INFO (("exiting")); */
}
/** Method specialisations for block device class */
ClassDeviceHandler block_class_handler = {
class_device_init, /**< init function */
class_device_shutdown, /**< shutdown function */
block_class_tick, /**< timer function */
class_device_accept, /**< accept function */
block_class_visit, /**< visitor function */
block_class_removed, /**< class device is removed */
class_device_udev_event, /**< handle udev event */
class_device_get_device_file_target,/**< where to store devfile name */
block_class_pre_process, /**< add more properties */
class_device_post_merge, /**< post merge function */
block_class_got_udi, /**< got UDI */
block_class_compute_udi, /**< UDI computation */
"block", /**< sysfs class name */
"block", /**< hal class name */
TRUE, /**< require device file */
FALSE /**< merge onto sysdevice */
};
/** @} */
|