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 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
|
/***************************************************************************
* CVSID: $Id$
*
* blockdev.c : Handling of block devices
*
* Copyright (C) 2005 David Zeuthen, <david@fubar.dk>
* Copyright (C) 2005,2006 Kay Sievers, <kay.sievers@vrfy.org>
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <ctype.h>
#include <limits.h>
#include <linux/kdev_t.h>
#include <mntent.h>
#include <stdint.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <syslog.h>
#include <unistd.h>
#include <errno.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include "../device_info.h"
#include "../hald.h"
#include "../hald_dbus.h"
#include "../hald_runner.h"
#include "../logger.h"
#include "../osspec.h"
#include "../util.h"
#include "coldplug.h"
#include "hotplug.h"
#include "hotplug_helper.h"
#include "osspec_linux.h"
#include "blockdev.h"
static gchar *
strdup_valid_utf8 (const char *str)
{
char *endchar;
char *newstr;
unsigned int fixes;
if (str == NULL)
return NULL;
newstr = g_strdup (str);
fixes = 0;
while (!g_utf8_validate (newstr, -1, (const char **) &endchar)) {
*endchar = '_';
++fixes;
}
/* If we had to fix more than 20% of the characters, give up */
if (fixes > 0 && g_utf8_strlen (newstr, -1) / fixes < 5) {
g_free (newstr);
newstr = g_strdup("");
}
return newstr;
}
/*--------------------------------------------------------------------------------------------------------------*/
static gboolean
blockdev_compute_udi (HalDevice *d)
{
gchar udi[256];
if (hal_device_property_get_bool (d, "block.is_volume")) {
const char *label;
const char *uuid;
char *volumelabel;
label = hal_device_property_get_string (d, "volume.label");
/* replace '/' to avoid trouble if the string get part of the UDI see fd.o #11401 */
volumelabel = g_strdup(label);
volumelabel = g_strdelimit (volumelabel, "/", '_');
uuid = hal_device_property_get_string (d, "volume.uuid");
if (uuid != NULL && strlen (uuid) > 0) {
hald_compute_udi (udi, sizeof (udi),
"/org/freedesktop/Hal/devices/volume_uuid_%s", uuid);
} else if (volumelabel != NULL && strlen (volumelabel) > 0) {
hald_compute_udi (udi, sizeof (udi),
"/org/freedesktop/Hal/devices/volume_label_%s", volumelabel);
} else if (hal_device_property_get_bool(d, "volume.is_disc") &&
hal_device_property_get_bool(d, "volume.disc.is_blank")) {
/* this should be a empty CD/DVD */
hald_compute_udi (udi, sizeof (udi),
"/org/freedesktop/Hal/devices/volume_empty_%s",
hal_device_property_get_string (d, "volume.disc.type"));
} else {
/* fallback to partition number, size */
hald_compute_udi (udi, sizeof (udi),
"/org/freedesktop/Hal/devices/volume_part%d_size_%lld",
hal_device_property_get_int (d, "volume.partition.number"),
hal_device_property_get_uint64 (d, "volume.size"));
}
g_free(volumelabel);
} else {
const char *model;
const char *serial;
const char *bus;
const char *type;
model = hal_device_property_get_string (d, "storage.model");
serial = hal_device_property_get_string (d, "storage.serial");
bus = hal_device_property_get_string (d, "storage.bus");
type = hal_device_property_get_string (d, "storage.drive_type");
if (serial != NULL) {
hald_compute_udi (udi, sizeof (udi),
"/org/freedesktop/Hal/devices/storage_serial_%s",
serial);
} else if ((model != NULL) && (strlen(model) != 0) ) {
hald_compute_udi (udi, sizeof (udi),
"/org/freedesktop/Hal/devices/storage_model_%s",
model);
} else if ((bus != NULL) && (type != NULL)){
hald_compute_udi (udi, sizeof (udi),
"%s_storage_%s_%s",
hal_device_property_get_string (d, "storage.originating_device"),
bus, type);
} else {
hald_compute_udi (udi, sizeof (udi),
"%s_storage",
hal_device_property_get_string (d, "storage.originating_device"));
}
}
hal_device_set_udi (d, udi);
return TRUE;
}
static void
blockdev_callouts_add_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
{
void *end_token = (void *) userdata1;
HAL_INFO (("Add callouts completed udi=%s", hal_device_get_udi (d)));
/* Move from temporary to global device store */
hal_device_store_remove (hald_get_tdl (), d);
hal_device_store_add (hald_get_gdl (), d);
hotplug_event_end (end_token);
}
static void
blockdev_callouts_remove_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
{
void *end_token = (void *) userdata1;
HAL_INFO (("Remove callouts completed udi=%s", hal_device_get_udi (d)));
if (!hal_device_store_remove (hald_get_gdl (), d)) {
HAL_WARNING (("Error removing device"));
}
g_object_unref (d);
hotplug_event_end (end_token);
}
static void
cleanup_mountpoint_cb (HalDevice *d, guint32 exit_type,
gint return_code, gchar **error,
gpointer data1, gpointer data2)
{
char *mount_point = (char *) data1;
HAL_INFO (("In cleanup_mountpoint_cb for '%s'", mount_point));
g_free (mount_point);
}
void
blockdev_refresh_mount_state (HalDevice *d)
{
FILE *f;
struct mntent mnt;
char buf[1024];
GSList *volumes = NULL;
GSList *volume;
/* open /proc/mounts */
g_snprintf (buf, sizeof (buf), "%s/mounts", "/proc");
if ((f = setmntent (buf, "r")) == NULL) {
HAL_ERROR (("Could not open /proc/mounts"));
return;
}
if (d)
volumes = g_slist_append (NULL, d);
else
volumes = hal_device_store_match_multiple_key_value_string (hald_get_gdl (), "info.category", "volume");
if (!volumes)
goto exit;
/* loop over /proc/mounts */
while (getmntent_r (f, &mnt, buf, sizeof(buf)) != NULL) {
struct stat statbuf;
dev_t devt;
/* HAL_INFO ((" * /proc/mounts contain dev %s - type %s", mnt.mnt_fsname, mnt.mnt_type)); */
/* We don't handle nfs mounts in HAL and stat() on mountpoints,
* and we would block on 'stale nfs handle'.
*/
if (strcmp(mnt.mnt_type, "nfs") == 0)
continue;
/* skip plain names, we look for device nodes */
if (mnt.mnt_fsname[0] != '/')
continue;
/*
* We can't just stat() the mountpoint, because it breaks all sorts
* non-disk filesystems. So assume, that the names in /proc/mounts
* are existing device-files used to mount the filesystem.
*/
devt = makedev(0, 0);
if (stat (mnt.mnt_fsname, &statbuf) == 0) {
/* not a device node */
if (major (statbuf.st_rdev) == 0)
continue;
/* found major/minor */
devt = statbuf.st_rdev;
} else {
/* The root filesystem may be mounted by a device name that doesn't
* exist in the real root, like /dev/root, which the kernel uses
* internally, when no initramfs image is used. For "/", it is safe
* to get the major/minor by stat()'ing the mount-point.
*/
if (strcmp (mnt.mnt_dir, "/") == 0 && stat ("/", &statbuf) == 0)
devt = statbuf.st_dev;
/* DING DING DING... the device-node may not exist, or is
* already deleted, but the device may be still mounted.
*
* We will fall back to looking up the device-name, instead
* of using major/minor.
*/
}
/* HAL_INFO (("* found mounts dev %s (%i:%i)", mnt.mnt_fsname,
major (devt), minor (devt))); */
for (volume = volumes; volume != NULL; volume = g_slist_next (volume)) {
HalDevice *dev;
gboolean is_match;
is_match = FALSE;
dev = HAL_DEVICE (volume->data);
/* lookup dev_t or devname of known hal devices */
if (major (devt) == 0) {
const char *device_name;
device_name = hal_device_property_get_string (dev, "block.device");
if (device_name == NULL)
continue;
if (strcmp (device_name, mnt.mnt_fsname) == 0)
is_match = TRUE;
} else {
unsigned int majornum;
unsigned int minornum;
majornum = hal_device_property_get_int (dev, "block.major");
if (majornum == 0)
continue;
minornum = hal_device_property_get_int (dev, "block.minor");
/* HAL_INFO ((" match %s (%i:%i)", hal_device_get_udi (dev), majornum, minornum)); */
if (majornum == major (devt) && minornum == minor (devt))
is_match = TRUE;
}
if (is_match) {
/* found entry for this device in /proc/mounts */
device_property_atomic_update_begin ();
hal_device_property_set_bool (dev, "volume.is_mounted", TRUE);
hal_device_property_set_bool (dev, "volume.is_mounted_read_only",
hasmntopt (&mnt, MNTOPT_RO) ? TRUE : FALSE);
hal_device_property_set_string (dev, "volume.mount_point", mnt.mnt_dir);
device_property_atomic_update_end ();
/* HAL_INFO ((" set %s to be mounted at %s (%s)", hal_device_get_udi (dev),
mnt.mnt_dir, hasmntopt (&mnt, MNTOPT_RO) ? "ro" : "rw")); */
volumes = g_slist_delete_link (volumes, volume);
break;
}
}
}
/* all remaining volumes are not mounted */
for (volume = volumes; volume != NULL; volume = g_slist_next (volume)) {
HalDevice *dev;
dev = HAL_DEVICE (volume->data);
/* do nothing if we have a Unmount() method running on the object. This is
* is because on Linux /proc/mounts is changed immediately while umount(8)
* doesn't return until the block cache is flushed. Note that when Unmount()
* terminates we'll be checking /proc/mounts again so this event is not
* lost... it is merely delayed...
*/
if (device_is_executing_method (dev, "org.freedesktop.Hal.Device.Volume", "Unmount")) {
HAL_INFO (("/proc/mounts tells that %s is unmounted - waiting for Unmount() to complete to change mount state", hal_device_get_udi (dev)));
} else {
char *mount_point;
mount_point = g_strdup (hal_device_property_get_string (dev, "volume.mount_point"));
device_property_atomic_update_begin ();
hal_device_property_set_bool (dev, "volume.is_mounted", FALSE);
hal_device_property_set_bool (dev, "volume.is_mounted_read_only", FALSE);
hal_device_property_set_string (dev, "volume.mount_point", "");
device_property_atomic_update_end ();
/*HAL_INFO (("set %s to unmounted", hal_device_get_udi (dev)));*/
if (mount_point != NULL && strlen (mount_point) > 0 &&
hal_util_is_mounted_by_hald (mount_point)) {
char *cleanup_stdin;
char *extra_env[2];
HAL_INFO (("Cleaning up directory '%s' since it was created by HAL's Mount()", mount_point));
extra_env[0] = g_strdup_printf ("HALD_CLEANUP=%s", mount_point);
extra_env[1] = NULL;
cleanup_stdin = "\n";
hald_runner_run_method (dev,
"hal-storage-cleanup-mountpoint",
extra_env,
cleanup_stdin, TRUE,
0,
cleanup_mountpoint_cb,
g_strdup (mount_point), NULL);
}
g_free (mount_point);
}
}
g_slist_free (volumes);
exit:
endmntent (f);
}
static void
generate_fakevolume_hotplug_event_add_for_storage_device (HalDevice *d)
{
const char *sysfs_path;
const char *device_file;
HotplugEvent *hotplug_event;
char fake_sysfs_path[HAL_PATH_MAX];
sysfs_path = hal_device_property_get_string (d, "linux.sysfs_path");
device_file = hal_device_property_get_string (d, "block.device");
snprintf (fake_sysfs_path, sizeof(fake_sysfs_path), "%s/fakevolume", sysfs_path);
hotplug_event = g_slice_new0 (HotplugEvent);
hotplug_event->action = HOTPLUG_ACTION_ADD;
hotplug_event->type = HOTPLUG_EVENT_SYSFS_BLOCK;
g_strlcpy (hotplug_event->sysfs.subsystem, "block", sizeof (hotplug_event->sysfs.subsystem));
g_strlcpy (hotplug_event->sysfs.sysfs_path, fake_sysfs_path, sizeof (hotplug_event->sysfs.sysfs_path));
if (device_file != NULL)
g_strlcpy (hotplug_event->sysfs.device_file, device_file, sizeof (hotplug_event->sysfs.device_file));
else
hotplug_event->sysfs.device_file[0] = '\0';
hotplug_event->sysfs.net_ifindex = -1;
hotplug_event_enqueue (hotplug_event);
}
static void
add_blockdev_probing_helper_done (HalDevice *d, guint32 exit_type,
gint return_code, char **error,
gpointer data1, gpointer data2)
{
void *end_token = (void *) data1;
gboolean is_volume;
/* helper_data may be null if probing is skipped */
HAL_INFO (("entering; exit_type=%d, return_code=%d", exit_type, return_code));
if (d == NULL) {
HAL_INFO (("Device object already removed"));
hotplug_event_end (end_token);
goto out;
}
is_volume = hal_device_property_get_bool (d, "block.is_volume");
/* Discard device if probing reports failure
*
* (return code 2 means fs found on main block device (for non-volumes))
*/
if (exit_type != HALD_RUN_SUCCESS
|| !(return_code == 0 || (!is_volume && return_code == 2))) {
hal_device_store_remove (hald_get_tdl (), d);
g_object_unref (d);
hotplug_event_end (end_token);
goto out;
}
if (!blockdev_compute_udi (d)) {
hal_device_store_remove (hald_get_tdl (), d);
g_object_unref (d);
hotplug_event_end (end_token);
goto out;
}
/* set block.storage_device for storage devices since only now we know the UDI */
if (!is_volume) {
hal_device_copy_property (d, "info.udi", d, "block.storage_device");
} else {
/* check for mount point */
blockdev_refresh_mount_state (d);
}
/* Merge properties from .fdi files */
di_search_and_merge (d, DEVICE_INFO_TYPE_INFORMATION);
di_search_and_merge (d, DEVICE_INFO_TYPE_POLICY);
/* TODO: Merge persistent properties */
/* Run callouts */
hal_util_callout_device_add (d, blockdev_callouts_add_done, end_token, NULL);
/* Yay, got a file system on the main block device...
*
* Generate a fake hotplug event to get this added
*/
if (!is_volume && return_code == 2) {
generate_fakevolume_hotplug_event_add_for_storage_device (d);
}
out:
return;
}
static void
blockdev_callouts_preprobing_storage_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
{
void *end_token = (void *) userdata1;
if (hal_device_property_get_bool (d, "info.ignore")) {
/* Leave the device here with info.ignore==TRUE so we won't pick up children
* Also remove category and all capabilities
*/
hal_device_property_remove (d, "info.category");
hal_device_property_remove (d, "info.capabilities");
hal_device_property_set_string (d, "info.udi", "/org/freedesktop/Hal/devices/ignored-device");
hal_device_property_set_string (d, "info.product", "Ignored Device");
HAL_INFO (("Preprobing merged info.ignore==TRUE"));
/* Move from temporary to global device store */
hal_device_store_remove (hald_get_tdl (), d);
hal_device_store_add (hald_get_gdl (), d);
hotplug_event_end (end_token);
goto out;
}
if (!hal_device_property_get_bool (d, "storage.media_check_enabled") &&
hal_device_property_get_bool (d, "storage.no_partitions_hint")) {
/* special probe for PC floppy drives */
if (strcmp (hal_device_property_get_string (d, "storage.bus"), "platform") == 0 &&
strcmp (hal_device_property_get_string (d, "storage.drive_type"), "floppy") == 0) {
HAL_INFO (("Probing PC floppy %s to see if it is present",
hal_device_property_get_string (d, "block.device")));
hald_runner_run(d,
"hald-probe-pc-floppy", NULL,
HAL_HELPER_TIMEOUT,
add_blockdev_probing_helper_done,
(gpointer) end_token, NULL);
goto out;
} else {
char *synerror[1] = {NULL};
HAL_INFO (("Not probing storage device %s",
hal_device_property_get_string (d, "block.device")));
add_blockdev_probing_helper_done (d, FALSE, 0, synerror, (gpointer) end_token, NULL);
goto out;
}
}
/* run prober for
*
* - cdrom drive properties
* - non-partitioned filesystem on main block device
*/
HAL_INFO (("Probing storage device %s", hal_device_property_get_string (d, "block.device")));
/* probe the device */
hald_runner_run(d,
"hald-probe-storage", NULL,
HAL_HELPER_TIMEOUT,
add_blockdev_probing_helper_done,
(gpointer) end_token, NULL);
out:
return;
}
static void
blockdev_callouts_preprobing_volume_done (HalDevice *d, gpointer userdata1, gpointer userdata2)
{
void *end_token = (void *) userdata1;
if (hal_device_property_get_bool (d, "info.ignore")) {
/* Leave the device here with info.ignore==TRUE so we won't pick up children
* Also remove category and all capabilities
*/
hal_device_property_remove (d, "info.category");
hal_device_property_remove (d, "info.capabilities");
hal_device_property_set_string (d, "info.udi", "/org/freedesktop/Hal/devices/ignored-device");
hal_device_property_set_string (d, "info.product", "Ignored Device");
HAL_INFO (("Preprobing merged info.ignore==TRUE"));
/* Move from temporary to global device store */
hal_device_store_remove (hald_get_tdl (), d);
hal_device_store_add (hald_get_gdl (), d);
hotplug_event_end (end_token);
goto out;
}
/* probe the device */
hald_runner_run (d,
"hald-probe-volume", NULL,
HAL_HELPER_TIMEOUT,
add_blockdev_probing_helper_done,
(gpointer) end_token, NULL);
out:
return;
}
/* borrowed from gtk/gtkfilesystemunix.c in GTK+ on 02/23/2006
* (which is LGPL, so can't go into hald/utils.[ch] until it's relicensed)
*/
static void
canonicalize_filename (gchar *filename)
{
gchar *p, *q;
gboolean last_was_slash = FALSE;
p = filename;
q = filename;
while (*p)
{
if (*p == G_DIR_SEPARATOR)
{
if (!last_was_slash)
*q++ = G_DIR_SEPARATOR;
last_was_slash = TRUE;
}
else
{
if (last_was_slash && *p == '.')
{
if (*(p + 1) == G_DIR_SEPARATOR ||
*(p + 1) == '\0')
{
if (*(p + 1) == '\0')
break;
p += 1;
}
else if (*(p + 1) == '.' &&
(*(p + 2) == G_DIR_SEPARATOR ||
*(p + 2) == '\0'))
{
if (q > filename + 1)
{
q--;
while (q > filename + 1 &&
*(q - 1) != G_DIR_SEPARATOR)
q--;
}
if (*(p + 2) == '\0')
break;
p += 2;
}
else
{
*q++ = *p;
last_was_slash = FALSE;
}
}
else
{
*q++ = *p;
last_was_slash = FALSE;
}
}
p++;
}
if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
q--;
*q = '\0';
}
static char *
resolve_symlink (const char *file)
{
char *dir;
gchar link[HAL_PATH_MAX];
char *f;
char *f1;
f = g_strdup (file);
memset(link, 0, HAL_PATH_MAX);
while (g_file_test (f, G_FILE_TEST_IS_SYMLINK)) {
if(readlink(f, link, HAL_PATH_MAX - 1) < 0) {
g_warning ("Cannot resolve symlink %s: %s", f, strerror(errno));
g_free (f);
f = NULL;
goto out;
}
dir = g_path_get_dirname (f);
f1 = g_strdup_printf ("%s/%s", dir, link);
g_free (dir);
g_free (f);
f = f1;
}
out:
if (f != NULL)
canonicalize_filename (f);
return f;
}
static gboolean
refresh_md_state (HalDevice *d);
static gboolean
md_check_sync_timeout (gpointer user_data)
{
HalDevice *d;
char *sysfs_path = (char *) user_data;
HAL_INFO (("In md_check_sync_timeout for sysfs path %s", sysfs_path));
d = hal_device_store_match_key_value_string (hald_get_gdl (),
"storage.linux_raid.sysfs_path",
sysfs_path);
if (d == NULL)
d = hal_device_store_match_key_value_string (hald_get_tdl (),
"storage.linux_raid.sysfs_path",
sysfs_path);
if (d == NULL) {
HAL_WARNING (("Cannot find md device with sysfs path '%s'", sysfs_path));
goto out;
}
refresh_md_state (d);
out:
g_free (sysfs_path);
return FALSE;
}
static gboolean
refresh_md_state (HalDevice *d)
{
int n;
char *sync_action;
int num_components;
gboolean ret;
const char *sysfs_path;
const char *level;
ret = FALSE;
sysfs_path = hal_device_property_get_string (d, "storage.linux_raid.sysfs_path");
if (sysfs_path == NULL) {
HAL_WARNING (("Cannot get sysfs_path for udi %s", hal_device_get_udi (d)));
goto error;
}
HAL_INFO (("In refresh_md_state() for '%s'", sysfs_path));
level = hal_device_property_get_string (d, "storage.linux_raid.level");
HAL_INFO ((" MD Level is '%s'", level));
/* MD linear device are not syncable */
if (strcmp (level, "linear") != 0) {
sync_action = hal_util_get_string_from_file (sysfs_path, "md/sync_action");
if (sync_action == NULL) {
HAL_WARNING (("Cannot get sync_action for %s", sysfs_path));
goto error;
}
if (strcmp (sync_action, "idle") == 0) {
hal_device_property_set_bool (d, "storage.linux_raid.is_syncing", FALSE);
hal_device_property_remove (d, "storage.linux_raid.sync.action");
hal_device_property_remove (d, "storage.linux_raid.sync.speed");
hal_device_property_remove (d, "storage.linux_raid.sync.progress");
} else {
int speed;
char *str_completed;
hal_device_property_set_bool (d, "storage.linux_raid.is_syncing", TRUE);
hal_device_property_set_string (d, "storage.linux_raid.sync.action", sync_action);
if (!hal_util_get_int_from_file (sysfs_path, "md/sync_speed", &speed, 10)) {
HAL_WARNING (("Cannot get sync_speed for %s", sysfs_path));
} else {
hal_device_property_set_uint64 (d, "storage.linux_raid.sync.speed", speed);
}
if ((str_completed = hal_util_get_string_from_file (sysfs_path, "md/sync_completed")) == NULL) {
HAL_WARNING (("Cannot get sync_completed for %s", sysfs_path));
} else {
long long int sync_pos, sync_total;
if (sscanf (str_completed, "%lld / %lld", &sync_pos, &sync_total) != 2) {
HAL_WARNING (("Malformed sync_completed '%s'", str_completed));
} else {
double sync_progress;
sync_progress = ((double) sync_pos) / ((double) sync_total);
hal_device_property_set_double (d, "storage.linux_raid.sync.progress", sync_progress);
}
}
/* check again in two seconds */
#ifdef HAVE_GLIB_2_14
g_timeout_add_seconds (2, md_check_sync_timeout, g_strdup (sysfs_path));
#else
g_timeout_add (2000, md_check_sync_timeout, g_strdup (sysfs_path));
#endif
}
} else
hal_device_property_set_bool (d, "storage.linux_raid.is_syncing", FALSE);
if (!hal_util_get_int_from_file (sysfs_path, "md/raid_disks", &num_components, 0)) {
HAL_WARNING (("Cannot get number of RAID components"));
goto error;
}
hal_device_property_set_int (d, "storage.linux_raid.num_components", num_components);
/* add all components */
for (n = 0; n < num_components; n++) {
char *s;
char *link;
char *target;
HalDevice *slave_volume;
const char *slave_volume_stordev_udi;
HalDevice *slave_volume_stordev;
s = g_strdup_printf ("%s/md/rd%d", sysfs_path, n);
if (!g_file_test (s, G_FILE_TEST_IS_SYMLINK)) {
g_free (s);
break;
}
g_free (s);
link = g_strdup_printf ("%s/md/rd%d/block", sysfs_path, n);
target = resolve_symlink (link);
if (target == NULL) {
HAL_WARNING (("Cannot resolve %s", link));
g_free (link);
goto error;
}
HAL_INFO (("link->target: '%s' -> '%s'", link, target));
slave_volume = hal_device_store_match_key_value_string (hald_get_gdl (), "linux.sysfs_path", target);
if (slave_volume == NULL) {
HAL_WARNING (("No volume for sysfs path %s", target));
g_free (target);
g_free (link);
goto error;
}
slave_volume_stordev_udi = hal_device_property_get_string (slave_volume, "block.storage_device");
if (slave_volume_stordev_udi == NULL) {
HAL_WARNING (("No storage device for slave"));
g_free (target);
g_free (link);
goto error;
}
slave_volume_stordev = hal_device_store_find (hald_get_gdl (), slave_volume_stordev_udi);
if (slave_volume_stordev == NULL) {
HAL_WARNING (("No storage device for slave"));
g_free (target);
g_free (link);
goto error;
}
hal_device_property_strlist_add (d, "storage.linux_raid.components", hal_device_get_udi (slave_volume));
/* derive
*
* - hotpluggability (is that a word?)
* - array uuid
*
* Hmm.. every raid member (PV) get the array UUID. That's
* probably.. wrong. TODO: check with Kay.
*
* from component 0.
*/
if (n == 0) {
const char *uuid;
hal_device_property_set_bool (
d, "storage.hotpluggable",
hal_device_property_get_bool (slave_volume_stordev, "storage.hotpluggable"));
uuid = hal_device_property_get_string (
slave_volume, "volume.uuid");
if (uuid != NULL) {
hal_device_property_set_string (
d, "storage.serial", uuid);
}
}
g_free (target);
g_free (link);
} /* for all components */
hal_device_property_set_int (d, "storage.linux_raid.num_components_active", n);
/* TODO: add more state here */
ret = TRUE;
error:
return ret;
}
void
hotplug_event_begin_add_blockdev (const gchar *sysfs_path, const gchar *device_file, gboolean is_partition,
HalDevice *parent, void *end_token)
{
HotplugEvent *hotplug_event = (HotplugEvent *) end_token;
gchar *major_minor;
HalDevice *d;
unsigned int major, minor;
gboolean is_fakevolume;
char *sysfs_path_real = NULL;
int floppy_num;
gboolean is_device_mapper;
gboolean is_md_device;
gboolean is_cciss_device;
int md_number;
char tc;
is_device_mapper = FALSE;
is_fakevolume = FALSE;
is_md_device = FALSE;
is_cciss_device = FALSE;
HAL_INFO (("block_add: sysfs_path=%s dev=%s is_part=%d, parent=0x%08x",
sysfs_path, device_file, is_partition, parent));
if (parent != NULL && hal_device_property_get_bool (parent, "info.ignore")) {
HAL_INFO (("Ignoring block_add since parent has info.ignore==TRUE"));
goto out;
}
if (strcmp (hal_util_get_last_element (sysfs_path), "fakevolume") == 0) {
HAL_INFO (("Handling %s as fakevolume - sysfs_path_real=%s", device_file, sysfs_path_real));
is_fakevolume = TRUE;
sysfs_path_real = hal_util_get_parent_path (sysfs_path);
} else if (sscanf (hal_util_get_last_element (sysfs_path), "md%d%c", &md_number, &tc) == 1) {
HAL_INFO (("Handling %s as MD device", device_file));
is_md_device = TRUE;
sysfs_path_real = g_strdup (sysfs_path);
/* set parent to root computer device object */
parent = hal_device_store_find (hald_get_gdl (), "/org/freedesktop/Hal/devices/computer");
if (parent == NULL)
parent = hal_device_store_find (hald_get_tdl (), "/org/freedesktop/Hal/devices/computer");
} else {
sysfs_path_real = g_strdup (sysfs_path);
}
/* See if we already have device (which we may have as we're ignoring rem/add
* for certain classes of devices - see hotplug_event_begin_remove_blockdev)
*/
d = hal_device_store_match_key_value_string (hald_get_gdl (), "linux.sysfs_path", sysfs_path);
if (d != NULL) {
HAL_INFO (("Ignoring hotplug event - device is already added"));
goto out;
}
d = hal_device_new ();
/* OK, no parent... */
if (parent == NULL && !is_partition && !is_fakevolume && !hotplug_event->reposted) {
char path[HAL_PATH_MAX];
g_snprintf (path, HAL_PATH_MAX, "%s/slaves", sysfs_path);
if (device_file && (strstr(device_file, "/dev/cciss/") != NULL)) {
/* ... it's a cciss (HP Smart Array CCISS) device */
if (g_file_test (path, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
HAL_DEBUG(("block_add: found cciss a base device"));
is_cciss_device = TRUE;
parent = hal_device_store_find (hald_get_gdl (), "/org/freedesktop/Hal/devices/computer");
if (parent == NULL)
parent = hal_device_store_find (hald_get_tdl (), "/org/freedesktop/Hal/devices/computer");
}
} else {
/* ... it might a device-mapper device => check slaves/ subdir in sysfs */
DIR * dir;
struct dirent *dp;
HAL_INFO (("Looking in %s for Device Mapper", path));
if ((dir = opendir (path)) == NULL) {
HAL_WARNING (("Unable to open %s: %s", path, strerror(errno)));
} else {
while (((dp = readdir (dir)) != NULL) && (parent == NULL)) {
char *link;
char *target;
link = g_strdup_printf ("%s/%s", path, dp->d_name);
target = resolve_symlink (link);
HAL_INFO ((" %s -> %s", link, target));
g_free (link);
if (target != NULL) {
HalDevice *slave_volume;
slave_volume = hal_device_store_match_key_value_string (hald_get_gdl (),
"linux.sysfs_path",
target);
if (slave_volume != NULL) {
const char *slave_volume_stordev_udi;
const char *slave_volume_fstype;
slave_volume_stordev_udi = hal_device_property_get_string (slave_volume, "block.storage_device");
slave_volume_fstype = hal_device_property_get_string (slave_volume, "volume.fstype");
/* Yup, we only support crypto_LUKS right now.
*
* In the future we can support other device-mapper mappings
* such as LVM etc.
*/
if (slave_volume_stordev_udi != NULL &&
slave_volume_fstype != NULL &&
(strcmp (slave_volume_fstype, "crypto_LUKS") == 0)) {
HAL_INFO ((" slave_volume_stordev_udi='%s'!", slave_volume_stordev_udi));
parent = hal_device_store_find (hald_get_gdl (), slave_volume_stordev_udi);
if (parent != NULL) {
HAL_INFO ((" parent='%s'!", hal_device_get_udi (parent)));
hal_device_property_set_string (d, "volume.crypto_luks.clear.backing_volume",
hal_device_get_udi (slave_volume));
is_device_mapper = TRUE;
}
}
} else {
HAL_INFO(("Couldn't find slave volume in devices"));
}
}
g_free (target);
}
closedir(dir);
HAL_INFO (("Done looking in %s", path));
}
}
}
if (parent == NULL) {
HAL_INFO (("Ignoring hotplug event - no parent"));
goto error;
}
if (!is_fakevolume && hal_device_property_get_bool (parent, "storage.no_partitions_hint")) {
HAL_INFO (("Ignoring blockdev since not a fakevolume and parent has "
"storage.no_partitions_hint==TRUE"));
goto error;
}
hal_device_property_set_string (d, "linux.sysfs_path", sysfs_path);
hal_device_property_set_string (d, "info.parent", hal_device_get_udi (parent));
hal_device_property_set_int (d, "linux.hotplug_type", HOTPLUG_EVENT_SYSFS_BLOCK);
hal_device_property_set_string (d, "block.device", device_file);
if ((major_minor = hal_util_get_string_from_file (sysfs_path_real, "dev")) == NULL ||
sscanf (major_minor, "%d:%d", &major, &minor) != 2) {
HAL_INFO (("Ignoring hotplug event - cannot read major:minor"));
goto error;
}
hal_device_property_set_int (d, "block.major", major);
hal_device_property_set_int (d, "block.minor", minor);
hal_device_property_set_bool (d, "block.is_volume", is_partition || is_device_mapper || is_fakevolume);
if (hal_device_has_property(parent, "info.subsystem") &&
(strcmp(hal_device_property_get_string(parent, "info.subsystem"), "platform") == 0) &&
(sscanf(hal_device_property_get_string(parent, "platform.id"), "floppy.%d", &floppy_num) == 1)) {
/* for now, just cheat here for floppy drives */
HAL_INFO (("doing floppy drive hack for floppy %d", floppy_num));
hal_device_property_set_string (d, "storage.bus", "platform");
hal_device_property_set_bool (d, "storage.no_partitions_hint", TRUE);
hal_device_property_set_bool (d, "storage.media_check_enabled", FALSE);
hal_device_property_set_bool (d, "storage.automount_enabled_hint", TRUE);
hal_device_property_set_string (d, "storage.model", "");
hal_device_property_set_string (d, "storage.vendor", "PC Floppy Drive");
hal_device_property_set_string (d, "info.vendor", "");
hal_device_property_set_string (d, "info.product", "PC Floppy Drive");
hal_device_property_set_string (d, "storage.drive_type", "floppy");
hal_device_property_set_string (d, "storage.originating_device", hal_device_get_udi (parent));
hal_device_property_set_bool (d, "storage.removable", TRUE);
hal_device_property_set_bool (d, "storage.removable.media_available", FALSE);
hal_device_property_set_bool (d, "storage.hotpluggable", FALSE);
hal_device_property_set_bool (d, "storage.requires_eject", FALSE);
hal_device_property_set_uint64 (d, "storage.size", 0);
hal_device_property_set_string (d, "info.category", "storage");
hal_device_add_capability (d, "storage");
hal_device_add_capability (d, "block");
/* add to TDL so preprobing callouts and prober can access it */
hal_device_store_add (hald_get_tdl (), d);
/* Process preprobe fdi files */
di_search_and_merge (d, DEVICE_INFO_TYPE_PREPROBE);
/* Run preprobe callouts */
hal_util_callout_device_preprobe (d, blockdev_callouts_preprobing_storage_done, end_token, NULL);
goto out2;
}
if (!is_partition && !is_device_mapper && !is_fakevolume) {
const char *udi_it;
const char *physdev_udi;
HalDevice *scsidev;
HalDevice *physdev;
gboolean is_hotpluggable;
gboolean is_removable;
gboolean requires_eject;
gboolean no_partitions_hint;
const gchar *bus;
const gchar *parent_bus;
/********************************
* storage devices
*******************************/
scsidev = NULL;
physdev = NULL;
physdev_udi = NULL;
is_removable = FALSE;
is_hotpluggable = FALSE;
requires_eject = FALSE;
no_partitions_hint = FALSE;
/* defaults */
hal_device_property_set_string (d, "storage.bus", "unknown");
hal_device_property_set_bool (d, "storage.no_partitions_hint", TRUE);
hal_device_property_set_bool (d, "storage.media_check_enabled", TRUE);
hal_device_property_set_bool (d, "storage.automount_enabled_hint", TRUE);
hal_device_property_set_string (d, "storage.drive_type", "disk");
/* persistent properties from udev (may be empty) */
hal_device_property_set_string (d, "storage.model", hotplug_event->sysfs.model);
hal_device_property_set_string (d, "storage.vendor", hotplug_event->sysfs.vendor);
if (hotplug_event->sysfs.serial[0] != '\0')
hal_device_property_set_string (d, "storage.serial", hotplug_event->sysfs.serial);
if (hotplug_event->sysfs.revision[0] != '\0')
hal_device_property_set_string (d, "storage.firmware_version", hotplug_event->sysfs.revision);
/* walk up the device chain to find the physical device,
* start with our parent. On the way, optionally pick up
* the scsi if it exists */
udi_it = hal_device_get_udi (parent);
while (udi_it != NULL) {
HalDevice *d_it;
/*************************
*
* STORAGE
*
************************/
/* Find device */
d_it = hal_device_store_find (hald_get_gdl (), udi_it);
if (d_it == NULL) {
d_it = hal_device_store_find (hald_get_tdl (), udi_it);
if (d_it == NULL) {
HAL_WARNING (("Could not get device '%s' from gdl or tdl.", udi_it));
goto error;
}
}
if (strcmp (udi_it, "/org/freedesktop/Hal/devices/computer") == 0) {
physdev = d_it;
physdev_udi = udi_it;
if (is_md_device) {
char *level;
char *model_name;
hal_device_property_set_string (d, "storage.bus", "linux_raid");
level = hal_util_get_string_from_file (sysfs_path_real, "md/level");
if (level == NULL)
goto error;
hal_device_property_set_string (d, "storage.linux_raid.level", level);
hal_device_property_set_string (d, "storage.linux_raid.sysfs_path", sysfs_path_real);
hal_device_property_set_string (d, "storage.vendor", "Linux");
if (strcmp (level, "linear") == 0) {
model_name = g_strdup ("Software RAID (Linear)");
} else if (strcmp (level, "raid0") == 0) {
model_name = g_strdup ("Software RAID-0 (Stripe)");
} else if (strcmp (level, "raid1") == 0) {
model_name = g_strdup ("Software RAID-1 (Mirror)");
} else if (strcmp (level, "raid5") == 0) {
model_name = g_strdup ("Software RAID-5");
} else {
model_name = g_strdup_printf ("Software RAID (%s)", level);
}
hal_device_property_set_string (d, "storage.model", model_name);
g_free (model_name);
hal_util_set_string_from_file (
d, "storage.firmware_version",
sysfs_path_real, "md/metadata_version");
hal_device_add_capability (d, "storage.linux_raid");
if (!refresh_md_state (d))
goto error;
is_hotpluggable = hal_device_property_get_bool (
d, "storage.hotpluggable");
} else if (is_cciss_device) {
HAL_DEBUG (("block_add: parent=/org/freedesktop/Hal/devices/computer, is_cciss_device=true"));
hal_device_property_set_string (d, "storage.bus", "cciss");
}
break;
}
/* Check info.subsystem */
if ((bus = hal_device_property_get_string (d_it, "info.subsystem")) != NULL) {
HAL_DEBUG(("block_add: info.subsystem='%s'", bus));
if (strcmp (bus, "scsi") == 0) {
scsidev = d_it;
physdev = d_it;
physdev_udi = udi_it;
hal_device_property_set_string (d, "storage.bus", "scsi");
hal_device_copy_property (scsidev, "scsi.lun", d, "storage.lun");
/* want to continue here, because it may be USB or IEEE1394 */
}
if (strcmp (bus, "usb") == 0) {
physdev = d_it;
physdev_udi = udi_it;
is_hotpluggable = TRUE;
hal_device_property_set_string (d, "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 (d, "storage.bus", "ieee1394");
break;
} else if (strcmp (bus, "ide") == 0) {
physdev = d_it;
physdev_udi = udi_it;
hal_device_property_set_string (d, "storage.bus", "ide");
/* want to continue here, because it may be pcmcia */
} else if (strcmp (bus, "pcmcia") == 0) {
physdev = d_it;
physdev_udi = udi_it;
is_hotpluggable = TRUE;
hal_device_property_set_string (d, "storage.bus", "pcmcia");
break;
} else if (strcmp (bus, "mmc") == 0) {
physdev = d_it;
physdev_udi = udi_it;
is_hotpluggable = TRUE;
hal_device_property_set_string (d, "storage.bus", "mmc");
break;
} else if (strcmp (bus, "memstick") == 0) {
physdev = d_it;
physdev_udi = udi_it;
is_hotpluggable = TRUE;
hal_device_property_set_string (d, "storage.bus", "memstick");
break;
} else if (strcmp (bus, "ccw") == 0) {
physdev = d_it;
physdev_udi = udi_it;
is_hotpluggable = TRUE;
hal_device_property_set_string (d, "storage.bus", "ccw");
} else if (strcmp (bus, "vio") == 0) {
physdev = d_it;
physdev_udi = udi_it;
hal_device_property_set_string (d, "storage.bus", "vio");
} else if (strcmp (bus, "pci") == 0) {
physdev = d_it;
physdev_udi = udi_it;
hal_device_property_set_string (d, "storage.bus", "pci");
}
}
/* Go to parent */
udi_it = hal_device_property_get_string (d_it, "info.parent");
}
/* needs physical device */
if (physdev_udi == NULL) {
HAL_WARNING (("No physical device?"));
goto error;
}
hal_device_property_set_string (d, "storage.originating_device", physdev_udi);
if (!hal_util_get_int_from_file (sysfs_path_real, "removable", (gint *) &is_removable, 10)) {
HAL_WARNING (("Cannot get 'removable' file"));
is_removable = FALSE;
}
hal_device_property_set_bool (d, "storage.removable.media_available", FALSE);
hal_device_property_set_bool (d, "storage.removable", is_removable);
/* set storage.size only if we have fixed media */
if (!is_removable) {
guint64 num_blocks;
if (hal_util_get_uint64_from_file (sysfs_path_real, "size", &num_blocks, 0)) {
/* TODO: sane to assume this is always 512 for non-removable?
* I think genhd.c guarantees this... */
hal_device_property_set_uint64 (d, "storage.size", num_blocks * 512);
}
} else {
hal_device_property_set_uint64 (d, "storage.size", 0);
}
if (is_removable) {
int sysfs_capability;
gboolean support_an;
support_an =
hal_util_get_int_from_file (sysfs_path, "capability", &sysfs_capability, 16) &&
(sysfs_capability&4) != 0;
hal_device_property_set_bool (d, "storage.removable.support_async_notification", support_an);
}
/* by default, do checks for media if, and only if, the removable file is set to 1
*
* Problematic buses, like IDE, may override this.
*/
hal_device_property_set_bool (d, "storage.media_check_enabled", is_removable);
parent_bus = hal_device_property_get_string (parent, "info.subsystem");
if (parent_bus == NULL) {
HAL_INFO (("parent_bus is NULL - wrong parent?"));
goto error;
}
HAL_INFO (("parent_bus is %s", parent_bus));
/* per-bus specific properties */
if (strcmp (parent_bus, "ide") == 0) {
char buf[256];
gchar *media;
gchar *model;
struct stat st;
/* Be conservative and don't poll IDE drives at all (except CD-ROM's, see below) */
hal_device_property_set_bool (d, "storage.media_check_enabled", FALSE);
/* according to kernel source, media can assume the following values:
*
* "disk", "cdrom", "tape", "floppy", "UNKNOWN"
*/
snprintf (buf, sizeof (buf), "/proc/ide/%s", hal_util_get_last_element (sysfs_path_real));
if (stat(buf, &st)) {
/*
* /proc/ide does not exist; try with sysfs
*/
snprintf (buf, sizeof (buf), "%s/%s", sysfs_path_real, "device");
}
if ((media = hal_util_get_string_from_file (buf, "media")) != NULL) {
if (strcmp (media, "disk") == 0 ||
strcmp (media, "cdrom") == 0 ||
strcmp (media, "floppy") == 0) {
hal_device_property_set_string (d, "storage.drive_type", media);
} else {
HAL_WARNING (("Cannot determine IDE drive type from file %s/media", buf));
goto error;
}
if (strcmp (media, "cdrom") == 0) {
/* only optical drives are the only IDE devices that can safely be polled */
hal_device_property_set_bool (d, "storage.media_check_enabled", TRUE);
}
}
if ((model = hal_util_get_string_from_file (buf, "model")) != NULL) {
hal_device_property_set_string (d, "storage.model", model);
hal_device_property_set_string (d, "info.product", model);
}
} else if (strcmp (parent_bus, "scsi") == 0) {
if (strcmp (hal_device_property_get_string (parent, "scsi.type"), "unknown") == 0) {
HAL_WARNING (("scsi.type is unknown"));
goto error;
}
hal_device_copy_property (parent, "scsi.type", d, "storage.drive_type");
hal_device_copy_property (parent, "scsi.vendor", d, "storage.vendor");
hal_device_copy_property (parent, "scsi.model", d, "storage.model");
hal_device_copy_property (d, "storage.vendor", d, "info.vendor");
hal_device_copy_property (d, "storage.model", d, "info.product");
/* Check for USB floppy drive by looking at USB Mass Storage interface class
* instead of Protocol: Uniform Floppy Interface (UFI) in /proc as we did before.
*
* (should fix RH bug 133834)
*/
if (physdev != NULL) {
if (hal_device_property_get_int (physdev, "usb.interface.class") == 8 &&
hal_device_property_get_int (physdev, "usb.interface.subclass") == 4 ) {
hal_device_property_set_string (d, "storage.drive_type", "floppy");
/* My experiments with my USB LaCie Floppy disk
* drive is that polling indeed work (Yay!), so
* we don't set storage.media_check_enabled to
* FALSE - for devices where this doesn't work,
* we can override it with .fdi files
*/
}
}
} else if (strcmp (parent_bus, "mmc") == 0) {
hal_device_property_set_string (d, "storage.drive_type", "sd_mmc");
} else if (strcmp (parent_bus, "memstick") == 0) {
hal_device_property_set_string (d, "storage.drive_type", "memstick");
} else if (strcmp (parent_bus, "vio") == 0) {
char buf[256];
const gchar *prop;
snprintf(buf, sizeof(buf), "%s/device", sysfs_path_real);
prop = hal_util_get_string_from_file (buf, "name");
if (prop) {
if (strcmp (prop, "viocd") == 0) {
hal_device_property_set_string (d, "info.product", "Vio Virtual CD");
hal_device_property_set_string (d, "storage.drive_type", "cdrom");
} else if (strcmp (prop, "viodasd") == 0) {
hal_device_property_set_string (d, "info.product", "Vio Virtual Disk");
hal_device_property_set_string (d, "storage.drive_type", "disk");
} else if (strcmp (prop, "viotape") == 0) {
hal_device_property_set_string (d, "info.product", "Vio Virtual Tape");
hal_device_property_set_string (d, "storage.drive_type", "tape");
}
}
}
else if (strcmp (parent_bus, "xen") == 0) {
char buf[256];
gchar *media;
snprintf (buf, sizeof (buf), "%s/%s", sysfs_path_real, "device");
if ((media = hal_util_get_string_from_file (buf, "media")) != NULL) {
if (strcmp (media, "cdrom") == 0) {
hal_device_property_set_string (d, "info.product", "Xen Virtual CD");
hal_device_property_set_string (d, "storage.drive_type", media);
hal_device_property_set_bool (d, "storage.media_check_enabled", FALSE);
} else if (strcmp (media, "floppy") == 0) {
hal_device_property_set_string (d, "info.product", "Xen Virtual Floppy");
hal_device_property_set_string (d, "storage.drive_type", media);
hal_device_property_set_bool (d, "storage.media_check_enabled", FALSE);
}
else {
hal_device_property_set_string (d, "info.product", "Xen Virtual Disk");
hal_device_property_set_string (d, "storage.drive_type", "disk");
}
}
}
hal_device_property_set_string (d, "info.category", "storage");
hal_device_add_capability (d, "storage");
hal_device_add_capability (d, "block");
if (strcmp (hal_device_property_get_string (d, "storage.drive_type"), "cdrom") == 0) {
hal_device_add_capability (d, "storage.cdrom");
no_partitions_hint = TRUE;
requires_eject = TRUE;
}
if (strcmp (hal_device_property_get_string (d, "storage.drive_type"), "floppy") == 0) {
no_partitions_hint = TRUE;
}
hal_device_property_set_bool (d, "storage.hotpluggable", is_hotpluggable);
hal_device_property_set_bool (d, "storage.requires_eject", requires_eject);
hal_device_property_set_bool (d, "storage.no_partitions_hint", no_partitions_hint);
/* add to TDL so preprobing callouts and prober can access it */
hal_device_store_add (hald_get_tdl (), d);
/* Process preprobe fdi files */
di_search_and_merge (d, DEVICE_INFO_TYPE_PREPROBE);
/* Run preprobe callouts */
hal_util_callout_device_preprobe (d, blockdev_callouts_preprobing_storage_done, end_token, NULL);
} else {
guint sysfs_path_len;
gboolean is_physical_partition;
char *volume_label;
char buf[64];
/*************************
*
* VOLUMES
*
************************/
hal_device_property_set_string (d, "block.storage_device", hal_device_get_udi (parent));
/* defaults */
hal_device_property_set_string (d, "volume.fstype", "");
hal_device_property_set_string (d, "volume.fsusage", "");
hal_device_property_set_string (d, "volume.fsversion", "");
hal_device_property_set_string (d, "volume.uuid", "");
hal_device_property_set_string (d, "volume.label", "");
hal_device_property_set_string (d, "volume.mount_point", "");
/* persistent properties from udev (may be empty) */
hal_device_property_set_string (d, "volume.fsusage", hotplug_event->sysfs.fsusage);
hal_device_property_set_string (d, "volume.fsversion", hotplug_event->sysfs.fsversion);
hal_device_property_set_string (d, "volume.uuid", hotplug_event->sysfs.fsuuid);
hal_device_property_set_string (d, "volume.fstype", hotplug_event->sysfs.fstype);
if (hotplug_event->sysfs.fstype[0] != '\0') {
snprintf (buf, sizeof (buf), "Volume (%s)", hotplug_event->sysfs.fstype);
} else {
snprintf (buf, sizeof (buf), "Volume");
}
hal_device_property_set_string (d, "info.product", buf);
volume_label = strdup_valid_utf8 (hotplug_event->sysfs.fslabel);
if (volume_label) {
hal_device_property_set_string (d, "volume.label", volume_label);
if (volume_label[0] != '\0') {
hal_device_property_set_string (d, "info.product", volume_label);
}
g_free(volume_label);
}
hal_device_property_set_bool (d, "volume.is_mounted", FALSE);
hal_device_property_set_bool (d, "volume.is_mounted_read_only", FALSE);
hal_device_property_set_bool (d, "volume.linux.is_device_mapper", is_device_mapper);
/* Don't assume that the parent has storage capability, eg
* if we are an MD partition then this is the case as we were
* re-parented to the root computer device object earlier.
*/
if (hal_device_has_property(parent, "storage.drive_type")) {
hal_device_property_set_bool (d, "volume.is_disc", strcmp (hal_device_property_get_string (parent, "storage.drive_type"), "cdrom") == 0);
} else {
hal_device_property_set_bool (d, "volume.is_disc", FALSE);
}
is_physical_partition = TRUE;
if (is_fakevolume || is_device_mapper)
is_physical_partition = FALSE;
hal_device_property_set_bool (d, "volume.is_partition", is_physical_partition);
hal_device_property_set_string (d, "info.category", "volume");
if (hal_device_has_property(parent, "storage.drive_type")) {
if (strcmp(hal_device_property_get_string (parent, "storage.drive_type"), "cdrom") == 0) {
hal_device_add_capability (d, "volume.disc");
}
}
hal_device_add_capability (d, "volume");
hal_device_add_capability (d, "block");
/* determine partition number */
sysfs_path_len = strlen (sysfs_path);
if (is_physical_partition) {
if (sysfs_path_len > 0 && isdigit (sysfs_path[sysfs_path_len - 1])) {
guint i;
for (i = sysfs_path_len - 1; isdigit (sysfs_path[i]); --i)
;
if (isdigit (sysfs_path[i+1])) {
guint partition_number;
partition_number = atoi (&sysfs_path[i+1]);
hal_device_property_set_int (d, "volume.partition.number", partition_number);
} else {
HAL_WARNING (("Cannot determine partition number?"));
goto error;
}
} else {
HAL_WARNING (("Cannot determine partition number"));
goto error;
}
}
/* first estimate - prober may override this...
*
* (block size requires opening the device file)
*/
hal_device_property_set_int (d, "volume.block_size", 512);
if (!hal_util_set_uint64_from_file (d, "volume.num_blocks", sysfs_path_real, "size", 0)) {
HAL_INFO (("Ignoring hotplug event - cannot read 'size'"));
goto error;
}
hal_device_property_set_uint64 (
d, "volume.size",
((dbus_uint64_t)(512)) * ((dbus_uint64_t)(hal_device_property_get_uint64 (d, "volume.num_blocks"))));
/* TODO: move to prober? */
if (is_physical_partition) {
guint64 start_block;
guint64 parent_size;
if (hal_util_get_uint64_from_file (sysfs_path, "start", &start_block, 0)) {
hal_device_property_set_uint64 (d, "volume.partition.start", start_block * 512);
}
if (hal_util_get_uint64_from_file (sysfs_path, "../size", &parent_size, 0)) {
hal_device_property_set_uint64 (d, "volume.partition.media_size", parent_size * 512);
}
}
/* add to TDL so preprobing callouts and prober can access it */
hal_device_store_add (hald_get_tdl (), d);
/* Process preprobe fdi files */
di_search_and_merge (d, DEVICE_INFO_TYPE_PREPROBE);
/* Run preprobe callouts */
hal_util_callout_device_preprobe (d, blockdev_callouts_preprobing_volume_done, end_token, NULL);
}
out2:
g_free (sysfs_path_real);
return;
error:
HAL_WARNING (("Not adding device object"));
if (d != NULL)
g_object_unref (d);
out:
hotplug_event_end (end_token);
g_free (sysfs_path_real);
return;
}
static void
force_unmount_cb (HalDevice *d, guint32 exit_type,
gint return_code, gchar **error,
gpointer data1, gpointer data2)
{
void *end_token = (void *) data1;
HAL_INFO (("force_unmount_cb for udi='%s', exit_type=%d, return_code=%d", hal_device_get_udi (d), exit_type, return_code));
if (exit_type == HALD_RUN_SUCCESS && error != NULL &&
error[0] != NULL && error[1] != NULL) {
char *exp_name = NULL;
char *exp_detail = NULL;
exp_name = error[0];
if (error[0] != NULL) {
exp_detail = error[1];
}
HAL_INFO (("failed with '%s' '%s'", exp_name, exp_detail));
}
hal_util_callout_device_remove (d, blockdev_callouts_remove_done, end_token, NULL);
}
static gboolean
force_unmount (HalDevice *d, void *end_token)
{
const char *device_file;
const char *mount_point;
device_file = hal_device_property_get_string (d, "block.device");
mount_point = hal_device_property_get_string (d, "volume.mount_point");
/* look up in /media/.hal-mtab to see if we mounted this one */
if (mount_point != NULL && strlen (mount_point) > 0 && hal_util_is_mounted_by_hald (mount_point)) {
char *unmount_stdin;
char *extra_env[2];
extra_env[0] = "HAL_METHOD_INVOKED_BY_UID=0";
extra_env[1] = NULL;
HAL_INFO (("force_unmount for udi='%s'", hal_device_get_udi (d)));
syslog (LOG_NOTICE, "forcibly attempting to lazy unmount %s as enclosing drive was disconnected", device_file);
unmount_stdin = "lazy\n";
/* so, yea, calling the Unmount methods handler.. is cheating a bit :-) */
hald_runner_run_method (d,
"hal-storage-unmount",
extra_env,
unmount_stdin, TRUE,
0,
force_unmount_cb,
end_token, NULL);
return TRUE;
} else {
return FALSE;
}
}
void
hotplug_event_begin_remove_blockdev (const gchar *sysfs_path, void *end_token)
{
HalDevice *d;
HAL_INFO (("block_rem: sysfs_path=%s", sysfs_path));
d = hal_device_store_match_key_value_string (hald_get_gdl (), "linux.sysfs_path", sysfs_path);
if (d == NULL) {
HAL_WARNING (("Device is not in the HAL database"));
hotplug_event_end (end_token);
} else {
HalDevice *fakevolume;
char fake_sysfs_path[HAL_PATH_MAX];
/* if we're a storage device synthesize hotplug rem event
* for the one potential fakevolume we've got
*/
snprintf (fake_sysfs_path, sizeof(fake_sysfs_path), "%s/fakevolume", sysfs_path);
fakevolume = hal_device_store_match_key_value_string (hald_get_gdl (), "linux.sysfs_path", fake_sysfs_path);
if (fakevolume != NULL) {
HotplugEvent *hotplug_event;
HAL_INFO (("Storage device with a fakevolume is going away; "
"synthesizing hotplug rem for fakevolume %s", hal_device_get_udi (fakevolume)));
hotplug_event = blockdev_generate_remove_hotplug_event (fakevolume);
if (hotplug_event != NULL) {
/* push synthesized event at front of queue and repost this event... this is such that
* the fakevolume event is processed before this one... because if we didn't make the
* events to be processed in this order, the "lazy unmount" of the fakevolume would
* fail...
*/
hotplug_event_enqueue_at_front ((HotplugEvent *) end_token);
hotplug_event_enqueue_at_front (hotplug_event);
hotplug_event_reposted (end_token);
goto out;
}
}
/* if we're mounted, then do a lazy unmount so the system can gracefully recover */
if (hal_device_property_get_bool (d, "volume.is_mounted")) {
if (!force_unmount (d, end_token)) {
/* this wasn't mounted by us... carry on */
HAL_INFO (("device at sysfs_path %s is mounted, but not by HAL", sysfs_path));
hal_util_callout_device_remove (d, blockdev_callouts_remove_done, end_token, NULL);
}
} else {
HAL_INFO (("device at sysfs_path %s is not mounted", sysfs_path));
hal_util_callout_device_remove (d, blockdev_callouts_remove_done, end_token, NULL);
}
}
out:
;
}
void
hotplug_event_refresh_blockdev (gchar *sysfs_path, HalDevice *d, void *end_token)
{
HAL_INFO (("block_change: sysfs_path=%s", sysfs_path));
if (hal_device_property_get_bool (d, "storage.removable.support_async_notification")) {
blockdev_rescan_device (d);
}
/* done with change event */
hotplug_event_end (end_token);
}
static void
block_rescan_storage_done (HalDevice *d, guint32 exit_type,
gint return_code, gchar **error,
gpointer data1, gpointer data2)
{
const char *sysfs_path;
HalDevice *fakevolume;
char fake_sysfs_path[HAL_PATH_MAX];
HAL_INFO (("hald-probe-storage --only-check-for-media returned %d (exit_type=%d)", return_code, exit_type));
if (d == NULL) {
HAL_INFO (("Device object already removed"));
goto out;
}
sysfs_path = hal_device_property_get_string (d, "linux.sysfs_path");
/* see if we already got a fake volume */
snprintf (fake_sysfs_path, sizeof(fake_sysfs_path), "%s/fakevolume", sysfs_path);
fakevolume = hal_device_store_match_key_value_string (hald_get_gdl (), "linux.sysfs_path", fake_sysfs_path);
if (return_code == 2) {
/* we've got something on the main block device - add fakevolume if we haven't got one already */
if (fakevolume == NULL) {
HAL_INFO (("Media insertion detected with file system on main block device; synthesizing hotplug add"));
generate_fakevolume_hotplug_event_add_for_storage_device (d);
}
} else {
/* no fs on the main block device - remove fakevolume if we have one */
if (fakevolume != NULL) {
/* generate hotplug event to remove the fakevolume */
HotplugEvent *hotplug_event;
HAL_INFO (("Media removal detected; synthesizing hotplug rem for fakevolume %s",
hal_device_get_udi (fakevolume)));
hotplug_event = blockdev_generate_remove_hotplug_event (fakevolume);
if (hotplug_event != NULL) {
hotplug_event_enqueue (hotplug_event);
}
}
}
out:
;
}
gboolean
blockdev_rescan_device (HalDevice *d)
{
gboolean ret;
ret = FALSE;
HAL_INFO (("blockdev_rescan_device: udi=%s", hal_device_get_udi (d)));
/* This only makes sense on storage devices */
if (hal_device_property_get_bool (d, "block.is_volume")) {
HAL_INFO (("No action on volumes", hal_device_get_udi (d)));
goto out;
}
/* now see if we got a file system on the main block device */
hald_runner_run (d,
"hald-probe-storage --only-check-for-media", NULL,
HAL_HELPER_TIMEOUT,
block_rescan_storage_done,
NULL, NULL);
ret = TRUE;
out:
return ret;
}
HotplugEvent *
blockdev_generate_add_hotplug_event (HalDevice *d)
{
const char *sysfs_path;
const char *device_file;
const char *model;
const char *vendor;
const char *serial;
const char *revision;
HotplugEvent *hotplug_event;
const char *nul;
nul = "\0";
sysfs_path = hal_device_property_get_string (d, "linux.sysfs_path");
device_file = hal_device_property_get_string (d, "block.device");
model = hal_device_property_get_string (d, "storage.model");
vendor = hal_device_property_get_string (d, "storage.vendor");
serial = hal_device_property_get_string (d, "storage.serial");
revision = hal_device_property_get_string (d, "storage.firmware_revision");
hotplug_event = g_slice_new0 (HotplugEvent);
hotplug_event->action = HOTPLUG_ACTION_ADD;
hotplug_event->type = HOTPLUG_EVENT_SYSFS;
g_strlcpy (hotplug_event->sysfs.subsystem, "block", sizeof (hotplug_event->sysfs.subsystem));
g_strlcpy (hotplug_event->sysfs.sysfs_path, sysfs_path, sizeof (hotplug_event->sysfs.sysfs_path));
g_strlcpy (hotplug_event->sysfs.device_file, device_file != NULL ? device_file : nul, HAL_NAME_MAX);
g_strlcpy (hotplug_event->sysfs.vendor, vendor != NULL ? vendor : nul, HAL_NAME_MAX);
g_strlcpy (hotplug_event->sysfs.model, model != NULL ? model : nul, HAL_NAME_MAX);
g_strlcpy (hotplug_event->sysfs.serial, serial != NULL ? serial : nul, HAL_NAME_MAX);
g_strlcpy (hotplug_event->sysfs.revision, revision != NULL ? revision : nul, HAL_NAME_MAX);
hotplug_event->sysfs.net_ifindex = -1;
return hotplug_event;
}
HotplugEvent *
blockdev_generate_remove_hotplug_event (HalDevice *d)
{
const char *sysfs_path;
HotplugEvent *hotplug_event;
sysfs_path = hal_device_property_get_string (d, "linux.sysfs_path");
hotplug_event = g_slice_new0 (HotplugEvent);
hotplug_event->action = HOTPLUG_ACTION_REMOVE;
hotplug_event->type = HOTPLUG_EVENT_SYSFS;
g_strlcpy (hotplug_event->sysfs.subsystem, "block", sizeof (hotplug_event->sysfs.subsystem));
g_strlcpy (hotplug_event->sysfs.sysfs_path, sysfs_path, sizeof (hotplug_event->sysfs.sysfs_path));
hotplug_event->sysfs.device_file[0] = '\0';
hotplug_event->sysfs.net_ifindex = -1;
return hotplug_event;
}
static GSList *md_devs = NULL;
static char *
udev_get_device_file_for_sysfs_path (const char *sysfs_path)
{
char *ret;
char *u_stdout;
int u_exit_status;
const char *argv[] = {"/sbin/udevadm", "info", "--root", "--query", "name", "--path", NULL, NULL};
GError *g_error;
ret = NULL;
argv[6] = sysfs_path;
g_error = NULL;
if (!g_spawn_sync("/",
(char **) argv,
NULL, /* envp */
G_SPAWN_LEAVE_DESCRIPTORS_OPEN, /* flags */
NULL, /* child_setup */
NULL, /* user_data */
&u_stdout,
NULL, /* stderr */
&u_exit_status,
&g_error)) {
HAL_ERROR (("Error spawning udevinfo: %s", g_error->message));
g_error_free (g_error);
goto out;
}
if (u_exit_status != 0) {
HAL_ERROR (("udevinfo returned exit code %d", u_exit_status));
g_free (u_stdout);
goto out;
}
ret = u_stdout;
g_strchomp (ret);
HAL_INFO (("Got '%s'", ret));
out:
return ret;
}
void
blockdev_process_mdstat (void)
{
HotplugEvent *hotplug_event;
GIOChannel *channel;
GSList *read_md_devs;
GSList *i;
GSList *j;
GSList *k;
GError *gerror = NULL;
channel = get_mdstat_channel ();
if (channel == NULL)
goto error;
if (g_io_channel_seek_position (channel, 0, G_SEEK_SET, &gerror) == G_IO_STATUS_ERROR) {
HAL_ERROR (("Cannot seek in /proc/mdstat"));
if (gerror != NULL)
g_error_free(gerror);
goto error;
}
read_md_devs = NULL;
while (TRUE) {
int num;
char *line;
if (g_io_channel_read_line (channel, &line, NULL, NULL, NULL) != G_IO_STATUS_NORMAL)
break;
if (sscanf (line, "md%d : ", &num) == 1) {
char *sysfs_path;
sysfs_path = g_strdup_printf ("/sys/block/md%d", num);
read_md_devs = g_slist_prepend (read_md_devs, sysfs_path);
}
g_free (line);
}
/* now compute the delta */
/* add devices */
for (i = read_md_devs; i != NULL; i = i->next) {
gboolean should_add = TRUE;
for (j = md_devs; j != NULL && should_add; j = j->next) {
if (strcmp (i->data, j->data) == 0) {
should_add = FALSE;
}
}
if (should_add) {
char *sysfs_path = i->data;
char *device_file;
int num_tries;
num_tries = 0;
retry_add:
device_file = udev_get_device_file_for_sysfs_path (sysfs_path);
if (device_file == NULL) {
if (num_tries <= 6) {
int num_ms;
num_ms = 10 * (1<<num_tries);
HAL_INFO (("spinning %d ms waiting for device file for sysfs path %s",
num_ms, sysfs_path));
usleep (1000 * num_ms);
num_tries++;
goto retry_add;
} else {
HAL_ERROR (("Cannot get device file for sysfs path %s", sysfs_path));
}
} else {
HAL_INFO (("Adding md device at '%s' ('%s')", sysfs_path, device_file));
hotplug_event = g_slice_new0 (HotplugEvent);
hotplug_event->action = HOTPLUG_ACTION_ADD;
hotplug_event->type = HOTPLUG_EVENT_SYSFS_BLOCK;
g_strlcpy (hotplug_event->sysfs.subsystem, "block", sizeof (hotplug_event->sysfs.subsystem));
g_strlcpy (hotplug_event->sysfs.sysfs_path, sysfs_path, sizeof (hotplug_event->sysfs.sysfs_path));
g_strlcpy (hotplug_event->sysfs.device_file, device_file, sizeof (hotplug_event->sysfs.device_file));
hotplug_event->sysfs.net_ifindex = -1;
hotplug_event_enqueue (hotplug_event);
md_devs = g_slist_prepend (md_devs, g_strdup (sysfs_path));
g_free (device_file);
}
}
}
/* remove devices */
for (i = md_devs; i != NULL; i = k) {
gboolean should_remove = TRUE;
k = i->next;
for (j = read_md_devs; j != NULL && should_remove; j = j->next) {
if (strcmp (i->data, j->data) == 0) {
should_remove = FALSE;
}
}
if (should_remove) {
char *sysfs_path = i->data;
char *device_file;
int num_tries;
num_tries = 0;
retry_rem:
device_file = udev_get_device_file_for_sysfs_path (sysfs_path);
if (device_file == NULL) {
if (num_tries <= 6) {
int num_ms;
num_ms = 10 * (1<<num_tries);
HAL_INFO (("spinning %d ms waiting for device file for sysfs path %s",
num_ms, sysfs_path));
usleep (1000 * num_ms);
num_tries++;
goto retry_rem;
} else {
HAL_ERROR (("Cannot get device file for sysfs path %s", sysfs_path));
}
} else {
HAL_INFO (("Removing md device at '%s' ('%s')", sysfs_path, device_file));
hotplug_event = g_slice_new0 (HotplugEvent);
hotplug_event->action = HOTPLUG_ACTION_REMOVE;
hotplug_event->type = HOTPLUG_EVENT_SYSFS_BLOCK;
g_strlcpy (hotplug_event->sysfs.subsystem, "block", sizeof (hotplug_event->sysfs.subsystem));
g_strlcpy (hotplug_event->sysfs.sysfs_path, sysfs_path, sizeof (hotplug_event->sysfs.sysfs_path));
g_strlcpy (hotplug_event->sysfs.device_file, device_file, sizeof (hotplug_event->sysfs.device_file));
hotplug_event->sysfs.net_ifindex = -1;
hotplug_event_enqueue (hotplug_event);
md_devs = g_slist_remove_link (md_devs, i);
g_free (i->data);
g_slist_free (i);
g_free (device_file);
}
}
}
g_slist_foreach (read_md_devs, (GFunc) g_free, NULL);
g_slist_free (read_md_devs);
/* finally, refresh all md devices */
for (i = md_devs; i != NULL; i = i->next) {
char *sysfs_path = i->data;
HalDevice *d;
d = hal_device_store_match_key_value_string (hald_get_gdl (),
"storage.linux_raid.sysfs_path",
sysfs_path);
if (d == NULL)
d = hal_device_store_match_key_value_string (hald_get_tdl (),
"storage.linux_raid.sysfs_path",
sysfs_path);
if (d != NULL)
refresh_md_state (d);
}
hotplug_event_process_queue ();
error:
;
}
|