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 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2007 David Zeuthen <david@fubar.dk>
*
* 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 <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <locale.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <glib-object.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <atasmart.h>
#include "udisks-daemon-glue.h"
#include "udisks-device-glue.h"
#include "udisks-marshal.h"
static DBusGConnection *bus = NULL;
static DBusGProxy *disks_proxy = NULL;
static GMainLoop *loop;
static gboolean opt_dump = FALSE;
static gboolean opt_enumerate = FALSE;
static gboolean opt_enumerate_device_files = FALSE;
static gboolean opt_monitor = FALSE;
static gboolean opt_monitor_detail = FALSE;
static char *opt_show_info = NULL;
static char *opt_inhibit_polling = NULL;
static char *opt_poll_for_media = NULL;
static gboolean opt_inhibit = FALSE;
static gboolean opt_inhibit_all_polling = FALSE;
static char *opt_drive_spindown = NULL;
static gboolean opt_drive_spindown_all = FALSE;
static gint opt_spindown_seconds = 0;
static char *opt_mount = NULL;
static char *opt_mount_fstype = NULL;
static char *opt_mount_options = NULL;
static char *opt_unmount = NULL;
static char *opt_unmount_options = NULL;
static char *opt_detach = NULL;
static char *opt_detach_options = NULL;
static char *opt_eject = NULL;
static char *opt_eject_options = NULL;
static char *opt_ata_smart_refresh = NULL;
static gboolean opt_ata_smart_wakeup = FALSE;
static char *opt_ata_smart_simulate = NULL;
static gboolean do_monitor (void);
static void do_show_info (const char *object_path);
static void
do_ata_smart_refresh (const gchar *object_path,
gboolean wakeup,
const gchar *simulate_path)
{
DBusGProxy *proxy;
GError *error;
GPtrArray *options;
options = g_ptr_array_new ();
if (!wakeup)
g_ptr_array_add (options, g_strdup ("nowakeup"));
if (simulate_path != NULL)
g_ptr_array_add (options, g_strdup_printf ("simulate=%s", simulate_path));
g_ptr_array_add (options, NULL);
proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", object_path, "org.freedesktop.UDisks.Device");
error = NULL;
if (!org_freedesktop_UDisks_Device_drive_ata_smart_refresh_data (proxy, (const char **) options->pdata, &error))
{
g_print ("Refreshing ATA SMART data failed: %s\n", error->message);
g_error_free (error);
}
else
{
do_show_info (object_path);
}
g_ptr_array_foreach (options, (GFunc) g_free, NULL);
g_ptr_array_free (options, TRUE);
}
static void
do_mount (const char *object_path,
const char *filesystem_type,
const char *options)
{
char *mount_path;
DBusGProxy *proxy;
GError *error;
char **mount_options;
mount_options = NULL;
if (options != NULL)
mount_options = g_strsplit (options, ",", 0);
proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", object_path, "org.freedesktop.UDisks.Device");
error = NULL;
if (!org_freedesktop_UDisks_Device_filesystem_mount (proxy,
filesystem_type,
(const char **) mount_options,
&mount_path,
&error))
{
g_print ("Mount failed: %s\n", error->message);
g_error_free (error);
goto out;
}
g_print ("Mounted %s at %s\n", object_path, mount_path);
g_free (mount_path);
out:
g_strfreev (mount_options);
}
static void
do_unmount (const char *object_path,
const char *options)
{
DBusGProxy *proxy;
GError *error;
char **unmount_options;
unmount_options = NULL;
if (options != NULL)
unmount_options = g_strsplit (options, ",", 0);
proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", object_path, "org.freedesktop.UDisks.Device");
error = NULL;
if (!org_freedesktop_UDisks_Device_filesystem_unmount (proxy, (const char **) unmount_options, &error))
{
g_print ("Unmount failed: %s\n", error->message);
g_error_free (error);
}
g_strfreev (unmount_options);
}
static void
do_detach (const char *object_path,
const char *options)
{
DBusGProxy *proxy;
GError *error;
char **unmount_options;
unmount_options = NULL;
if (options != NULL)
unmount_options = g_strsplit (options, ",", 0);
proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", object_path, "org.freedesktop.UDisks.Device");
error = NULL;
if (!org_freedesktop_UDisks_Device_drive_detach (proxy, (const char **) options, &error))
{
g_print ("Detach failed: %s\n", error->message);
g_error_free (error);
}
g_strfreev (unmount_options);
}
static void
do_eject (const char *object_path,
const char *options)
{
DBusGProxy *proxy;
GError *error;
char **eject_options;
eject_options = NULL;
if (options != NULL)
eject_options = g_strsplit (options, ",", 0);
proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", object_path, "org.freedesktop.UDisks.Device");
error = NULL;
if (!org_freedesktop_UDisks_Device_drive_eject (proxy, (const char **) eject_options, &error))
{
g_print ("Eject failed: %s\n", error->message);
g_error_free (error);
}
g_strfreev (eject_options);
}
static void
device_added_signal_handler (DBusGProxy *proxy,
const char *object_path,
gpointer user_data)
{
g_print ("added: %s\n", object_path);
if (opt_monitor_detail)
{
do_show_info (object_path);
g_print ("\n");
}
}
static void
device_changed_signal_handler (DBusGProxy *proxy,
const char *object_path,
gpointer user_data)
{
g_print ("changed: %s\n", object_path);
if (opt_monitor_detail)
{
/* TODO: would be nice to just show the diff */
do_show_info (object_path);
g_print ("\n");
}
}
static void
print_job (gboolean job_in_progress,
const char *job_id,
uid_t job_initiated_by_uid,
gboolean job_is_cancellable,
double job_percentage)
{
if (job_in_progress)
{
g_print (" job underway: %s", job_id);
if (job_percentage >= 0)
g_print (", %3.0lf%% complete", job_percentage);
if (job_is_cancellable)
g_print (", cancellable");
g_print (", initiated by uid %d", job_initiated_by_uid);
g_print ("\n");
}
else
{
g_print (" job underway: no\n");
}
}
static void
device_job_changed_signal_handler (DBusGProxy *proxy,
const char *object_path,
gboolean job_in_progress,
const char *job_id,
guint32 job_initiated_by_uid,
gboolean job_is_cancellable,
double job_percentage,
gpointer user_data)
{
g_print ("job-changed: %s\n", object_path);
if (opt_monitor_detail)
{
print_job (job_in_progress, job_id, job_initiated_by_uid, job_is_cancellable, job_percentage);
}
}
static void
device_removed_signal_handler (DBusGProxy *proxy,
const char *object_path,
gpointer user_data)
{
g_print ("removed: %s\n", object_path);
}
#define LSOF_DATA_STRUCT_TYPE (dbus_g_type_get_struct ("GValueArray", \
G_TYPE_UINT, \
G_TYPE_UINT, \
G_TYPE_STRING, \
G_TYPE_INVALID))
/* --- SUCKY CODE BEGIN --- */
/* This totally sucks; dbus-bindings-tool and dbus-glib should be able
* to do this for us.
*
* TODO: keep in sync with code in tools/udisks in udisks.
*/
typedef struct
{
char *native_path;
guint64 device_detection_time;
guint64 device_media_detection_time;
gint64 device_major;
gint64 device_minor;
char *device_file;
char *device_file_presentation;
char **device_file_by_id;
char **device_file_by_path;
gboolean device_is_system_internal;
gboolean device_is_partition;
gboolean device_is_partition_table;
gboolean device_is_removable;
gboolean device_is_media_available;
gboolean device_is_media_change_detected;
gboolean device_is_media_change_detection_polling;
gboolean device_is_media_change_detection_inhibitable;
gboolean device_is_media_change_detection_inhibited;
gboolean device_is_read_only;
gboolean device_is_drive;
gboolean device_is_optical_disc;
gboolean device_is_luks;
gboolean device_is_luks_cleartext;
gboolean device_is_mounted;
gboolean device_is_linux_md_component;
gboolean device_is_linux_md;
gboolean device_is_linux_lvm2_lv;
gboolean device_is_linux_lvm2_pv;
gboolean device_is_linux_dmmp;
gboolean device_is_linux_dmmp_component;
gboolean device_is_linux_loop;
char **device_mount_paths;
uid_t device_mounted_by_uid;
gboolean device_presentation_hide;
gboolean device_presentation_nopolicy;
char *device_presentation_name;
char *device_presentation_icon_name;
char *device_automount_hint;
guint64 device_size;
guint64 device_block_size;
gboolean job_in_progress;
char *job_id;
uid_t job_initiated_by_uid;
gboolean job_is_cancellable;
double job_percentage;
char *id_usage;
char *id_type;
char *id_version;
char *id_uuid;
char *id_label;
char *partition_slave;
char *partition_scheme;
int partition_number;
char *partition_type;
char *partition_label;
char *partition_uuid;
char **partition_flags;
guint64 partition_offset;
guint64 partition_size;
guint64 partition_alignment_offset;
char *partition_table_scheme;
int partition_table_count;
char *luks_holder;
char *luks_cleartext_slave;
uid_t luks_cleartext_unlocked_by_uid;
char *drive_vendor;
char *drive_model;
char *drive_revision;
char *drive_serial;
char *drive_wwn;
char *drive_connection_interface;
guint64 drive_connection_speed;
char **drive_media_compatibility;
char *drive_media;
gboolean drive_is_media_ejectable;
gboolean drive_can_detach;
gboolean drive_can_spindown;
gboolean drive_is_rotational;
guint drive_rotation_rate;
char *drive_write_cache;
char *drive_adapter;
char **drive_ports;
char **drive_similar_devices;
gboolean optical_disc_is_blank;
gboolean optical_disc_is_appendable;
gboolean optical_disc_is_closed;
guint optical_disc_num_tracks;
guint optical_disc_num_audio_tracks;
guint optical_disc_num_sessions;
gboolean drive_ata_smart_is_available;
guint64 drive_ata_smart_time_collected;
gchar *drive_ata_smart_status;
gchar *drive_ata_smart_blob;
gsize drive_ata_smart_blob_size;
char *linux_md_component_level;
int linux_md_component_position;
int linux_md_component_num_raid_devices;
char *linux_md_component_uuid;
char *linux_md_component_home_host;
char *linux_md_component_name;
char *linux_md_component_version;
char *linux_md_component_holder;
char **linux_md_component_state;
char *linux_md_state;
char *linux_md_level;
int linux_md_num_raid_devices;
char *linux_md_uuid;
char *linux_md_home_host;
char *linux_md_name;
char *linux_md_version;
char **linux_md_slaves;
gboolean linux_md_is_degraded;
char *linux_md_sync_action;
double linux_md_sync_percentage;
guint64 linux_md_sync_speed;
gchar *linux_lvm2_lv_name;
gchar *linux_lvm2_lv_uuid;
gchar *linux_lvm2_lv_group_name;
gchar *linux_lvm2_lv_group_uuid;
gchar *linux_lvm2_pv_uuid;
guint linux_lvm2_pv_num_metadata_areas;
gchar *linux_lvm2_pv_group_name;
gchar *linux_lvm2_pv_group_uuid;
guint64 linux_lvm2_pv_group_size;
guint64 linux_lvm2_pv_group_unallocated_size;
guint64 linux_lvm2_pv_group_sequence_number;
guint64 linux_lvm2_pv_group_extent_size;
char **linux_lvm2_pv_group_physical_volumes;
char **linux_lvm2_pv_group_logical_volumes;
gchar *linux_dmmp_component_holder;
gchar *linux_dmmp_name;
gchar **linux_dmmp_slaves;
gchar *linux_dmmp_parameters;
gchar *linux_loop_filename;
} DeviceProperties;
static void
collect_props (const char *key,
const GValue *value,
DeviceProperties *props)
{
gboolean handled = TRUE;
if (strcmp (key, "NativePath") == 0)
props->native_path = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DeviceDetectionTime") == 0)
props->device_detection_time = g_value_get_uint64 (value);
else if (strcmp (key, "DeviceMediaDetectionTime") == 0)
props->device_media_detection_time = g_value_get_uint64 (value);
else if (strcmp (key, "DeviceMajor") == 0)
props->device_major = g_value_get_int64 (value);
else if (strcmp (key, "DeviceMinor") == 0)
props->device_minor = g_value_get_int64 (value);
else if (strcmp (key, "DeviceFile") == 0)
props->device_file = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DeviceFilePresentation") == 0)
props->device_file_presentation = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DeviceFileById") == 0)
props->device_file_by_id = g_strdupv (g_value_get_boxed (value));
else if (strcmp (key, "DeviceFileByPath") == 0)
props->device_file_by_path = g_strdupv (g_value_get_boxed (value));
else if (strcmp (key, "DeviceIsSystemInternal") == 0)
props->device_is_system_internal = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsPartition") == 0)
props->device_is_partition = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsPartitionTable") == 0)
props->device_is_partition_table = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsRemovable") == 0)
props->device_is_removable = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsMediaAvailable") == 0)
props->device_is_media_available = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsMediaChangeDetected") == 0)
props->device_is_media_change_detected = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsMediaChangeDetectionPolling") == 0)
props->device_is_media_change_detection_polling = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsMediaChangeDetectionInhibitable") == 0)
props->device_is_media_change_detection_inhibitable = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsMediaChangeDetectionInhibited") == 0)
props->device_is_media_change_detection_inhibited = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsReadOnly") == 0)
props->device_is_read_only = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsDrive") == 0)
props->device_is_drive = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsOpticalDisc") == 0)
props->device_is_optical_disc = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsLuks") == 0)
props->device_is_luks = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsLuksCleartext") == 0)
props->device_is_luks_cleartext = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsLinuxMdComponent") == 0)
props->device_is_linux_md_component = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsLinuxMd") == 0)
props->device_is_linux_md = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsLinuxLvm2LV") == 0)
props->device_is_linux_lvm2_lv = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsLinuxLvm2PV") == 0)
props->device_is_linux_lvm2_pv = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsLinuxDmmp") == 0)
props->device_is_linux_dmmp = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsLinuxDmmpComponent") == 0)
props->device_is_linux_dmmp_component = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsLinuxLoop") == 0)
props->device_is_linux_loop = g_value_get_boolean (value);
else if (strcmp (key, "DeviceIsMounted") == 0)
props->device_is_mounted = g_value_get_boolean (value);
else if (strcmp (key, "DeviceMountPaths") == 0)
props->device_mount_paths = g_strdupv (g_value_get_boxed (value));
else if (strcmp (key, "DeviceMountedByUid") == 0)
props->device_mounted_by_uid = g_value_get_uint (value);
else if (strcmp (key, "DevicePresentationHide") == 0)
props->device_presentation_hide = g_value_get_boolean (value);
else if (strcmp (key, "DevicePresentationNopolicy") == 0)
props->device_presentation_nopolicy = g_value_get_boolean (value);
else if (strcmp (key, "DevicePresentationName") == 0)
props->device_presentation_name = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DevicePresentationIconName") == 0)
props->device_presentation_icon_name = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DeviceAutomountHint") == 0)
props->device_automount_hint = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DeviceSize") == 0)
props->device_size = g_value_get_uint64 (value);
else if (strcmp (key, "DeviceBlockSize") == 0)
props->device_block_size = g_value_get_uint64 (value);
else if (strcmp (key, "JobInProgress") == 0)
props->job_in_progress = g_value_get_boolean (value);
else if (strcmp (key, "JobId") == 0)
props->job_id = g_strdup (g_value_get_string (value));
else if (strcmp (key, "JobInitiatedByUid") == 0)
props->job_initiated_by_uid = g_value_get_uint (value);
else if (strcmp (key, "JobIsCancellable") == 0)
props->job_is_cancellable = g_value_get_boolean (value);
else if (strcmp (key, "JobPercentage") == 0)
props->job_percentage = g_value_get_double (value);
else if (strcmp (key, "IdUsage") == 0)
props->id_usage = g_strdup (g_value_get_string (value));
else if (strcmp (key, "IdType") == 0)
props->id_type = g_strdup (g_value_get_string (value));
else if (strcmp (key, "IdVersion") == 0)
props->id_version = g_strdup (g_value_get_string (value));
else if (strcmp (key, "IdUuid") == 0)
props->id_uuid = g_strdup (g_value_get_string (value));
else if (strcmp (key, "IdLabel") == 0)
props->id_label = g_strdup (g_value_get_string (value));
else if (strcmp (key, "PartitionSlave") == 0)
props->partition_slave = g_strdup (g_value_get_boxed (value));
else if (strcmp (key, "PartitionScheme") == 0)
props->partition_scheme = g_strdup (g_value_get_string (value));
else if (strcmp (key, "PartitionNumber") == 0)
props->partition_number = g_value_get_int (value);
else if (strcmp (key, "PartitionType") == 0)
props->partition_type = g_strdup (g_value_get_string (value));
else if (strcmp (key, "PartitionLabel") == 0)
props->partition_label = g_strdup (g_value_get_string (value));
else if (strcmp (key, "PartitionUuid") == 0)
props->partition_uuid = g_strdup (g_value_get_string (value));
else if (strcmp (key, "PartitionFlags") == 0)
props->partition_flags = g_strdupv (g_value_get_boxed (value));
else if (strcmp (key, "PartitionOffset") == 0)
props->partition_offset = g_value_get_uint64 (value);
else if (strcmp (key, "PartitionSize") == 0)
props->partition_size = g_value_get_uint64 (value);
else if (strcmp (key, "PartitionAlignmentOffset") == 0)
props->partition_alignment_offset = g_value_get_uint64 (value);
else if (strcmp (key, "PartitionTableScheme") == 0)
props->partition_table_scheme = g_strdup (g_value_get_string (value));
else if (strcmp (key, "PartitionTableCount") == 0)
props->partition_table_count = g_value_get_int (value);
else if (strcmp (key, "LuksHolder") == 0)
props->luks_holder = g_strdup (g_value_get_boxed (value));
else if (strcmp (key, "LuksCleartextSlave") == 0)
props->luks_cleartext_slave = g_strdup (g_value_get_boxed (value));
else if (strcmp (key, "LuksCleartextUnlockedByUid") == 0)
props->luks_cleartext_unlocked_by_uid = g_value_get_uint (value);
else if (strcmp (key, "DriveVendor") == 0)
props->drive_vendor = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DriveModel") == 0)
props->drive_model = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DriveRevision") == 0)
props->drive_revision = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DriveSerial") == 0)
props->drive_serial = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DriveWwn") == 0)
props->drive_wwn = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DriveConnectionInterface") == 0)
props->drive_connection_interface = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DriveConnectionSpeed") == 0)
props->drive_connection_speed = g_value_get_uint64 (value);
else if (strcmp (key, "DriveMediaCompatibility") == 0)
props->drive_media_compatibility = g_strdupv (g_value_get_boxed (value));
else if (strcmp (key, "DriveMedia") == 0)
props->drive_media = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DriveIsMediaEjectable") == 0)
props->drive_is_media_ejectable = g_value_get_boolean (value);
else if (strcmp (key, "DriveCanDetach") == 0)
props->drive_can_detach = g_value_get_boolean (value);
else if (strcmp (key, "DriveCanSpindown") == 0)
props->drive_can_spindown = g_value_get_boolean (value);
else if (strcmp (key, "DriveIsRotational") == 0)
props->drive_is_rotational = g_value_get_boolean (value);
else if (strcmp (key, "DriveRotationRate") == 0)
props->drive_rotation_rate = g_value_get_uint (value);
else if (strcmp (key, "DriveWriteCache") == 0)
props->drive_write_cache = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DriveAdapter") == 0)
props->drive_adapter = g_strdup (g_value_get_boxed (value));
else if (strcmp (key, "DrivePorts") == 0)
{
guint n;
GPtrArray *object_paths;
object_paths = g_value_get_boxed (value);
props->drive_ports = g_new0 (char *, object_paths->len + 1);
for (n = 0; n < object_paths->len; n++)
props->drive_ports[n] = g_strdup (object_paths->pdata[n]);
props->drive_ports[n] = NULL;
}
else if (strcmp (key, "DriveSimilarDevices") == 0)
{
guint n;
GPtrArray *object_paths;
object_paths = g_value_get_boxed (value);
props->drive_similar_devices = g_new0 (char *, object_paths->len + 1);
for (n = 0; n < object_paths->len; n++)
props->drive_similar_devices[n] = g_strdup (object_paths->pdata[n]);
props->drive_similar_devices[n] = NULL;
}
else if (strcmp (key, "OpticalDiscIsBlank") == 0)
props->optical_disc_is_blank = g_value_get_boolean (value);
else if (strcmp (key, "OpticalDiscIsAppendable") == 0)
props->optical_disc_is_appendable = g_value_get_boolean (value);
else if (strcmp (key, "OpticalDiscIsClosed") == 0)
props->optical_disc_is_closed = g_value_get_boolean (value);
else if (strcmp (key, "OpticalDiscNumTracks") == 0)
props->optical_disc_num_tracks = g_value_get_uint (value);
else if (strcmp (key, "OpticalDiscNumAudioTracks") == 0)
props->optical_disc_num_audio_tracks = g_value_get_uint (value);
else if (strcmp (key, "OpticalDiscNumSessions") == 0)
props->optical_disc_num_sessions = g_value_get_uint (value);
else if (strcmp (key, "DriveAtaSmartIsAvailable") == 0)
props->drive_ata_smart_is_available = g_value_get_boolean (value);
else if (strcmp (key, "DriveAtaSmartTimeCollected") == 0)
props->drive_ata_smart_time_collected = g_value_get_uint64 (value);
else if (strcmp (key, "DriveAtaSmartStatus") == 0)
props->drive_ata_smart_status = g_strdup (g_value_get_string (value));
else if (strcmp (key, "DriveAtaSmartBlob") == 0)
{
GArray *a = g_value_get_boxed (value);
g_free (props->drive_ata_smart_blob);
props->drive_ata_smart_blob = g_memdup (a->data, a->len);
props->drive_ata_smart_blob_size = a->len;
}
else if (strcmp (key, "LinuxMdComponentLevel") == 0)
props->linux_md_component_level = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxMdComponentPosition") == 0)
props->linux_md_component_position = g_value_get_int (value);
else if (strcmp (key, "LinuxMdComponentNumRaidDevices") == 0)
props->linux_md_component_num_raid_devices = g_value_get_int (value);
else if (strcmp (key, "LinuxMdComponentUuid") == 0)
props->linux_md_component_uuid = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxMdComponentHomeHost") == 0)
props->linux_md_component_home_host = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxMdComponentName") == 0)
props->linux_md_component_name = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxMdComponentVersion") == 0)
props->linux_md_component_version = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxMdComponentHolder") == 0)
props->linux_md_component_holder = g_strdup (g_value_get_boxed (value));
else if (strcmp (key, "LinuxMdComponentState") == 0)
props->linux_md_component_state = g_strdupv (g_value_get_boxed (value));
else if (strcmp (key, "LinuxMdState") == 0)
props->linux_md_state = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxMdLevel") == 0)
props->linux_md_level = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxMdNumRaidDevices") == 0)
props->linux_md_num_raid_devices = g_value_get_int (value);
else if (strcmp (key, "LinuxMdUuid") == 0)
props->linux_md_uuid = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxMdHomeHost") == 0)
props->linux_md_home_host = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxMdName") == 0)
props->linux_md_name = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxMdVersion") == 0)
props->linux_md_version = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxMdSlaves") == 0)
{
guint n;
GPtrArray *object_paths;
object_paths = g_value_get_boxed (value);
props->linux_md_slaves = g_new0 (char *, object_paths->len + 1);
for (n = 0; n < object_paths->len; n++)
props->linux_md_slaves[n] = g_strdup (object_paths->pdata[n]);
props->linux_md_slaves[n] = NULL;
}
else if (strcmp (key, "LinuxMdIsDegraded") == 0)
props->linux_md_is_degraded = g_value_get_boolean (value);
else if (strcmp (key, "LinuxMdSyncAction") == 0)
props->linux_md_sync_action = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxMdSyncPercentage") == 0)
props->linux_md_sync_percentage = g_value_get_double (value);
else if (strcmp (key, "LinuxMdSyncSpeed") == 0)
props->linux_md_sync_speed = g_value_get_uint64 (value);
else if (strcmp (key, "LinuxLvm2LVName") == 0)
props->linux_lvm2_lv_name = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxLvm2LVUuid") == 0)
props->linux_lvm2_lv_uuid = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxLvm2LVGroupName") == 0)
props->linux_lvm2_lv_group_name = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxLvm2LVGroupUuid") == 0)
props->linux_lvm2_lv_group_uuid = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxLvm2PVUuid") == 0)
props->linux_lvm2_pv_uuid = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxLvm2PVNumMetadataAreas") == 0)
props->linux_lvm2_pv_num_metadata_areas = g_value_get_uint (value);
else if (strcmp (key, "LinuxLvm2PVGroupName") == 0)
props->linux_lvm2_pv_group_name = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxLvm2PVGroupUuid") == 0)
props->linux_lvm2_pv_group_uuid = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxLvm2PVGroupSize") == 0)
props->linux_lvm2_pv_group_size = g_value_get_uint64 (value);
else if (strcmp (key, "LinuxLvm2PVGroupUnallocatedSize") == 0)
props->linux_lvm2_pv_group_unallocated_size = g_value_get_uint64 (value);
else if (strcmp (key, "LinuxLvm2PVGroupSequenceNumber") == 0)
props->linux_lvm2_pv_group_sequence_number = g_value_get_uint64 (value);
else if (strcmp (key, "LinuxLvm2PVGroupExtentSize") == 0)
props->linux_lvm2_pv_group_extent_size = g_value_get_uint64 (value);
else if (strcmp (key, "LinuxLvm2PVGroupPhysicalVolumes") == 0)
props->linux_lvm2_pv_group_physical_volumes = g_strdupv (g_value_get_boxed (value));
else if (strcmp (key, "LinuxLvm2PVGroupLogicalVolumes") == 0)
props->linux_lvm2_pv_group_logical_volumes = g_strdupv (g_value_get_boxed (value));
else if (strcmp (key, "LinuxDmmpComponentHolder") == 0)
props->linux_dmmp_component_holder = g_strdup (g_value_get_boxed (value));
else if (strcmp (key, "LinuxDmmpName") == 0)
props->linux_dmmp_name = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxDmmpSlaves") == 0)
{
guint n;
GPtrArray *object_paths;
object_paths = g_value_get_boxed (value);
props->linux_dmmp_slaves = g_new0 (char *, object_paths->len + 1);
for (n = 0; n < object_paths->len; n++)
props->linux_dmmp_slaves[n] = g_strdup (object_paths->pdata[n]);
props->linux_dmmp_slaves[n] = NULL;
}
else if (strcmp (key, "LinuxDmmpParameters") == 0)
props->linux_dmmp_parameters = g_strdup (g_value_get_string (value));
else if (strcmp (key, "LinuxLoopFilename") == 0)
props->linux_loop_filename = g_strdup (g_value_get_string (value));
else
handled = FALSE;
if (!handled)
g_warning ("unhandled property '%s'", key);
}
static void
device_properties_free (DeviceProperties *props)
{
g_free (props->native_path);
g_free (props->device_file);
g_free (props->device_file_presentation);
g_strfreev (props->device_file_by_id);
g_strfreev (props->device_file_by_path);
g_strfreev (props->device_mount_paths);
g_free (props->device_presentation_name);
g_free (props->device_presentation_icon_name);
g_free (props->device_automount_hint);
g_free (props->job_id);
g_free (props->id_usage);
g_free (props->id_type);
g_free (props->id_version);
g_free (props->id_uuid);
g_free (props->id_label);
g_free (props->partition_slave);
g_free (props->partition_type);
g_free (props->partition_label);
g_free (props->partition_uuid);
g_strfreev (props->partition_flags);
g_free (props->partition_table_scheme);
g_free (props->luks_holder);
g_free (props->luks_cleartext_slave);
g_free (props->drive_model);
g_free (props->drive_vendor);
g_free (props->drive_revision);
g_free (props->drive_serial);
g_free (props->drive_wwn);
g_free (props->drive_connection_interface);
g_strfreev (props->drive_media_compatibility);
g_free (props->drive_media);
g_free (props->drive_write_cache);
g_free (props->drive_adapter);
g_strfreev (props->drive_ports);
g_strfreev (props->drive_similar_devices);
g_free (props->drive_ata_smart_status);
g_free (props->drive_ata_smart_blob);
g_free (props->linux_md_component_level);
g_free (props->linux_md_component_uuid);
g_free (props->linux_md_component_home_host);
g_free (props->linux_md_component_name);
g_free (props->linux_md_component_version);
g_free (props->linux_md_component_holder);
g_strfreev (props->linux_md_component_state);
g_free (props->linux_md_state);
g_free (props->linux_md_level);
g_free (props->linux_md_uuid);
g_free (props->linux_md_home_host);
g_free (props->linux_md_name);
g_free (props->linux_md_version);
g_strfreev (props->linux_md_slaves);
g_free (props->linux_md_sync_action);
g_free (props->linux_lvm2_lv_name);
g_free (props->linux_lvm2_lv_uuid);
g_free (props->linux_lvm2_lv_group_name);
g_free (props->linux_lvm2_lv_group_uuid);
g_free (props->linux_lvm2_pv_uuid);
g_free (props->linux_lvm2_pv_group_name);
g_free (props->linux_lvm2_pv_group_uuid);
g_strfreev (props->linux_lvm2_pv_group_physical_volumes);
g_strfreev (props->linux_lvm2_pv_group_logical_volumes);
g_free (props->linux_dmmp_component_holder);
g_free (props->linux_dmmp_name);
g_strfreev (props->linux_dmmp_slaves);
g_free (props->linux_dmmp_parameters);
g_free (props->linux_loop_filename);
g_free (props);
}
static DeviceProperties *
device_properties_get (DBusGConnection *bus,
const char *object_path)
{
DeviceProperties *props;
GError *error;
GHashTable *hash_table;
DBusGProxy *prop_proxy;
const char *ifname = "org.freedesktop.UDisks.Device";
props = g_new0 (DeviceProperties, 1);
prop_proxy
= dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", object_path, "org.freedesktop.DBus.Properties");
error = NULL;
if (!dbus_g_proxy_call (prop_proxy,
"GetAll",
&error,
G_TYPE_STRING,
ifname,
G_TYPE_INVALID,
dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
&hash_table,
G_TYPE_INVALID))
{
g_warning ("Couldn't call GetAll() to get properties for %s: %s", object_path, error->message);
g_error_free (error);
device_properties_free (props);
props = NULL;
goto out;
}
g_hash_table_foreach (hash_table, (GHFunc) collect_props, props);
g_hash_table_unref (hash_table);
out:
g_object_unref (prop_proxy);
return props;
}
/* --- SUCKY CODE END --- */
static gboolean
do_monitor (void)
{
g_print ("Monitoring activity from the disks daemon. Press Ctrl+C to cancel.\n");
dbus_g_proxy_connect_signal (disks_proxy, "DeviceAdded", G_CALLBACK (device_added_signal_handler), NULL, NULL);
dbus_g_proxy_connect_signal (disks_proxy, "DeviceRemoved", G_CALLBACK (device_removed_signal_handler), NULL, NULL);
dbus_g_proxy_connect_signal (disks_proxy, "DeviceChanged", G_CALLBACK (device_changed_signal_handler), NULL, NULL);
dbus_g_proxy_connect_signal (disks_proxy,
"DeviceJobChanged",
G_CALLBACK (device_job_changed_signal_handler),
NULL,
NULL);
g_main_loop_run (loop);
return FALSE;
}
static gboolean
has_colors (void)
{
static gboolean ret = FALSE;
static gboolean checked = FALSE;
if (checked)
return ret;
if (isatty (STDOUT_FILENO))
{
ret = TRUE;
}
checked = TRUE;
return ret;
}
static void
begin_highlight (void)
{
if (has_colors ())
g_print ("\x1B[1;31m");
}
static void
end_highlight (void)
{
if (has_colors ())
g_print ("\x1B[0m");
}
static const gchar *
ata_smart_status_to_desc (const gchar *status,
gboolean *out_highlight)
{
const gchar *desc;
gboolean highlight;
highlight = FALSE;
if (g_strcmp0 (status, "GOOD") == 0)
{
desc = "Good";
}
else if (g_strcmp0 (status, "BAD_ATTRIBUTE_IN_THE_PAST") == 0)
{
desc = "Disk was used outside of design parameters in the past";
}
else if (g_strcmp0 (status, "BAD_SECTOR") == 0)
{
desc = "Disk has a few bad sectors";
}
else if (g_strcmp0 (status, "BAD_ATTRIBUTE_NOW") == 0)
{
desc = "Disk is being used outside of design parameters";
highlight = TRUE;
}
else if (g_strcmp0 (status, "BAD_SECTOR_MANY") == 0)
{
desc = "Disk reports many bad sectors";
highlight = TRUE;
}
else if (g_strcmp0 (status, "BAD_STATUS") == 0)
{
desc = "Disk failure is imminent";
highlight = TRUE;
}
else
{
desc = status;
}
if (out_highlight != NULL)
*out_highlight = highlight;
return desc;
}
static gchar *
get_ata_smart_unit (guint unit,
guint64 pretty_value)
{
gchar *ret;
switch (unit)
{
default:
case SK_SMART_ATTRIBUTE_UNIT_UNKNOWN:
case SK_SMART_ATTRIBUTE_UNIT_NONE:
ret = g_strdup_printf ("%" G_GUINT64_FORMAT, pretty_value);
break;
case SK_SMART_ATTRIBUTE_UNIT_MSECONDS:
if (pretty_value > 1000 * 60 * 60 * 24)
{
ret = g_strdup_printf ("%3.1f days", pretty_value / 1000.0 / 60.0 / 60.0 / 24.0);
}
else if (pretty_value > 1000 * 60 * 60)
{
ret = g_strdup_printf ("%3.1f hours", pretty_value / 1000.0 / 60.0 / 60.0);
}
else if (pretty_value > 1000 * 60)
{
ret = g_strdup_printf ("%3.1f mins", pretty_value / 1000.0 / 60.0);
}
else if (pretty_value > 1000)
{
ret = g_strdup_printf ("%3.1f secs", pretty_value / 1000.0);
}
else
{
ret = g_strdup_printf ("%d msec", (gint) pretty_value);
}
break;
case SK_SMART_ATTRIBUTE_UNIT_SECTORS:
ret = g_strdup_printf ("%" G_GUINT64_FORMAT " sectors", pretty_value);
break;
case SK_SMART_ATTRIBUTE_UNIT_MKELVIN:
ret = g_strdup_printf ("%.3gC / %.3gF", pretty_value / 1000.0 - 273.15, (pretty_value / 1000.0 - 273.15) * 9.0
/ 5.0 + 32.0);
break;
}
return ret;
}
static void
print_ata_smart_attr (SkDisk *d,
const SkSmartAttributeParsedData *a,
void *user_data)
{
const gchar *assessment;
const gchar *type;
const gchar *updates;
gchar *current_str;
gchar *worst_str;
gchar *threshold_str;
gchar *pretty;
pretty = get_ata_smart_unit (a->pretty_unit, a->pretty_value);
if (!a->good_now_valid)
{
assessment = " n/a ";
}
else
{
if (!a->good_now)
{
assessment = " FAIL ";
}
else
{
if (a->good_in_the_past_valid && !a->good_in_the_past)
{
assessment = "FAIL_PAST";
}
else
{
assessment = " good ";
}
}
}
if (a->online)
updates = "Online ";
else
updates = "Offline";
if (a->prefailure)
type = "Pre-fail";
else
type = "Old-age ";
if (a->current_value_valid)
current_str = g_strdup_printf ("%3d", a->current_value);
else
current_str = g_strdup ("n/a");
if (a->worst_value_valid)
worst_str = g_strdup_printf ("%3d", a->worst_value);
else
worst_str = g_strdup ("n/a");
if (a->threshold_valid)
threshold_str = g_strdup_printf ("%3d", a->threshold);
else
threshold_str = g_strdup ("n/a");
if (a->warn)
begin_highlight ();
g_print (" %-27s %s|%s|%s %s %-11s %s %s\n",
a->name,
current_str,
worst_str,
threshold_str,
assessment,
pretty,
type,
updates);
if (a->warn)
end_highlight ();
g_free (current_str);
g_free (worst_str);
g_free (threshold_str);
g_free (pretty);
}
static void
do_show_info (const char *object_path)
{
guint n;
DeviceProperties *props;
struct tm *time_tm;
time_t time;
char time_buf[256];
props = device_properties_get (bus, object_path);
if (props == NULL)
return;
time = (time_t) props->device_detection_time;
time_tm = localtime (&time);
strftime (time_buf, sizeof time_buf, "%c", time_tm);
g_print ("Showing information for %s\n", object_path);
g_print (" native-path: %s\n", props->native_path);
g_print (" device: %" G_GINT64_MODIFIER "d:%" G_GINT64_MODIFIER "d\n",
props->device_major,
props->device_minor);
g_print (" device-file: %s\n", props->device_file);
g_print (" presentation: %s\n",
(props->device_file_presentation != NULL && strlen (props->device_file_presentation) > 0) ?
props->device_file_presentation : "(not set)");
for (n = 0; props->device_file_by_id[n] != NULL; n++)
g_print (" by-id: %s\n", (char *) props->device_file_by_id[n]);
for (n = 0; props->device_file_by_path[n] != NULL; n++)
g_print (" by-path: %s\n", (char *) props->device_file_by_path[n]);
g_print (" detected at: %s\n", time_buf);
g_print (" system internal: %d\n", props->device_is_system_internal);
g_print (" removable: %d\n", props->device_is_removable);
g_print (" has media: %d", props->device_is_media_available);
if (props->device_media_detection_time != 0)
{
time = (time_t) props->device_media_detection_time;
time_tm = localtime (&time);
strftime (time_buf, sizeof time_buf, "%c", time_tm);
g_print (" (detected at %s)", time_buf);
}
g_print ("\n");
g_print (" detects change: %d\n", props->device_is_media_change_detected);
g_print (" detection by polling: %d\n", props->device_is_media_change_detection_polling);
g_print (" detection inhibitable: %d\n", props->device_is_media_change_detection_inhibitable);
g_print (" detection inhibited: %d\n", props->device_is_media_change_detection_inhibited);
g_print (" is read only: %d\n", props->device_is_read_only);
g_print (" is mounted: %d\n", props->device_is_mounted);
g_print (" mount paths: ");
for (n = 0; props->device_mount_paths != NULL && props->device_mount_paths[n] != NULL; n++)
{
if (n != 0)
g_print (", ");
g_print ("%s", props->device_mount_paths[n]);
}
g_print ("\n");
g_print (" mounted by uid: %d\n", props->device_mounted_by_uid);
g_print (" presentation hide: %d\n", props->device_presentation_hide);
g_print (" presentation nopolicy: %d\n", props->device_presentation_nopolicy);
g_print (" presentation name: %s\n", props->device_presentation_name);
g_print (" presentation icon: %s\n", props->device_presentation_icon_name);
g_print (" automount hint: %s\n", props->device_automount_hint);
g_print (" size: %" G_GUINT64_FORMAT "\n", props->device_size);
g_print (" block size: %" G_GUINT64_FORMAT "\n", props->device_block_size);
print_job (props->job_in_progress,
props->job_id,
props->job_initiated_by_uid,
props->job_is_cancellable,
props->job_percentage);
g_print (" usage: %s\n", props->id_usage);
g_print (" type: %s\n", props->id_type);
g_print (" version: %s\n", props->id_version);
g_print (" uuid: %s\n", props->id_uuid);
g_print (" label: %s\n", props->id_label);
if (props->device_is_linux_md_component)
{
g_print (" linux md component:\n");
g_print (" RAID level: %s\n", props->linux_md_component_level);
g_print (" position: %d\n", props->linux_md_component_position);
g_print (" num components: %d\n", props->linux_md_component_num_raid_devices);
g_print (" uuid: %s\n", props->linux_md_component_uuid);
g_print (" home host: %s\n", props->linux_md_component_home_host);
g_print (" name: %s\n", props->linux_md_component_name);
g_print (" version: %s\n", props->linux_md_component_version);
g_print (" holder: %s\n", g_strcmp0 (props->linux_md_component_holder, "/") == 0 ? "(none)"
: props->linux_md_component_holder);
g_print (" state: ");
for (n = 0; props->linux_md_component_state != NULL && props->linux_md_component_state[n] != NULL; n++)
{
if (n > 0)
g_print (", ");
g_print ("%s", props->linux_md_component_state[n]);
}
g_print ("\n");
}
if (props->device_is_linux_md)
{
g_print (" linux md:\n");
g_print (" state: %s\n", props->linux_md_state);
g_print (" RAID level: %s\n", props->linux_md_level);
g_print (" uuid: %s\n", props->linux_md_uuid);
g_print (" home host: %s\n", props->linux_md_home_host);
g_print (" name: %s\n", props->linux_md_name);
g_print (" num comp: %d\n", props->linux_md_num_raid_devices);
g_print (" version: %s\n", props->linux_md_version);
g_print (" degraded: %d\n", props->linux_md_is_degraded);
g_print (" sync action: %s\n", props->linux_md_sync_action);
if (strcmp (props->linux_md_sync_action, "idle") != 0)
{
g_print (" complete: %3.01f%%\n", props->linux_md_sync_percentage);
g_print (" speed: %" G_GINT64_FORMAT " bytes/sec\n", props->linux_md_sync_speed);
}
g_print (" slaves:\n");
for (n = 0; props->linux_md_slaves[n] != NULL; n++)
g_print (" %s\n", props->linux_md_slaves[n]);
}
if (props->device_is_linux_lvm2_lv)
{
g_print (" LVM2 Logical Volume:\n");
g_print (" LV name: %s\n", props->linux_lvm2_lv_name);
g_print (" LV uuid: %s\n", props->linux_lvm2_lv_uuid);
g_print (" VG name: %s\n", props->linux_lvm2_lv_group_name);
g_print (" VG uuid: %s\n", props->linux_lvm2_lv_group_uuid);
}
if (props->device_is_linux_lvm2_pv)
{
g_print (" LVM2 Physical Volume:\n");
g_print (" PV uuid: %s\n", props->linux_lvm2_pv_uuid);
g_print (" PV num mda: %d\n", props->linux_lvm2_pv_num_metadata_areas);
g_print (" VG name: %s\n", props->linux_lvm2_pv_group_name);
g_print (" VG uuid: %s\n", props->linux_lvm2_pv_group_uuid);
g_print (" VG size: %" G_GUINT64_FORMAT "\n", props->linux_lvm2_pv_group_size);
g_print (" VG unallocated size: %" G_GUINT64_FORMAT "\n", props->linux_lvm2_pv_group_unallocated_size);
g_print (" VG extent size: %" G_GUINT64_FORMAT "\n", props->linux_lvm2_pv_group_extent_size);
g_print (" VG sequence number: %" G_GUINT64_FORMAT "\n", props->linux_lvm2_pv_group_sequence_number);
g_print (" Physical Volumes bound to the VG:\n");
for (n = 0; props->linux_lvm2_pv_group_physical_volumes[n] != NULL; n++)
g_print (" %s\n", props->linux_lvm2_pv_group_physical_volumes[n]);
g_print (" Logical Volumes that are part of the VG:\n");
for (n = 0; props->linux_lvm2_pv_group_logical_volumes[n] != NULL; n++)
g_print (" %s\n", props->linux_lvm2_pv_group_logical_volumes[n]);
}
if (props->device_is_linux_dmmp)
{
g_print (" dm-multipath:\n");
g_print (" name: %s\n", props->linux_dmmp_name);
g_print (" parameters: %s\n", props->linux_dmmp_parameters);
g_print (" components:\n");
for (n = 0; props->linux_dmmp_slaves[n] != NULL; n++)
g_print (" %s\n", props->linux_dmmp_slaves[n]);
}
if (props->device_is_linux_dmmp_component)
{
g_print (" dm-multipath component:\n");
g_print (" multipath device: %s\n", props->linux_dmmp_component_holder);
}
if (props->device_is_linux_loop)
{
g_print (" loop:\n");
g_print (" filename: %s\n", props->linux_loop_filename);
}
if (props->device_is_luks)
{
g_print (" luks device:\n");
g_print (" holder: %s\n", props->luks_holder);
}
if (props->device_is_luks_cleartext)
{
g_print (" cleartext luks device:\n");
g_print (" backed by: %s\n", props->luks_cleartext_slave);
g_print (" unlocked by: uid %d\n", props->luks_cleartext_unlocked_by_uid);
}
if (props->device_is_partition_table)
{
g_print (" partition table:\n");
g_print (" scheme: %s\n", props->partition_table_scheme);
g_print (" count: %d\n", props->partition_table_count);
}
if (props->device_is_partition)
{
g_print (" partition:\n");
g_print (" part of: %s\n", props->partition_slave);
g_print (" scheme: %s\n", props->partition_scheme);
g_print (" number: %d\n", props->partition_number);
g_print (" type: %s\n", props->partition_type);
g_print (" flags: ");
for (n = 0; props->partition_flags[n] != NULL; n++)
g_print (" %s", (char *) props->partition_flags[n]);
g_print ("\n");
g_print (" offset: %" G_GINT64_FORMAT "\n", props->partition_offset);
if (props->partition_alignment_offset != 0)
begin_highlight ();
g_print (" alignment offset: %" G_GINT64_FORMAT "\n", props->partition_alignment_offset);
if (props->partition_alignment_offset != 0)
end_highlight ();
g_print (" size: %" G_GINT64_FORMAT "\n", props->partition_size);
g_print (" label: %s\n", props->partition_label);
g_print (" uuid: %s\n", props->partition_uuid);
}
if (props->device_is_optical_disc)
{
g_print (" optical disc:\n");
g_print (" blank: %d\n", props->optical_disc_is_blank);
g_print (" appendable: %d\n", props->optical_disc_is_appendable);
g_print (" closed: %d\n", props->optical_disc_is_closed);
g_print (" num tracks: %d\n", props->optical_disc_num_tracks);
g_print (" num audio tracks: %d\n", props->optical_disc_num_audio_tracks);
g_print (" num sessions: %d\n", props->optical_disc_num_sessions);
}
if (props->device_is_drive)
{
g_print (" drive:\n");
g_print (" vendor: %s\n", props->drive_vendor);
g_print (" model: %s\n", props->drive_model);
g_print (" revision: %s\n", props->drive_revision);
g_print (" serial: %s\n", props->drive_serial);
g_print (" WWN: %s\n", props->drive_wwn);
g_print (" detachable: %d\n", props->drive_can_detach);
g_print (" can spindown: %d\n", props->drive_can_spindown);
if (props->drive_is_rotational)
{
if (props->drive_rotation_rate > 0)
{
g_print (" rotational media: Yes, at %d RPM\n", props->drive_rotation_rate);
}
else
{
g_print (" rotational media: Yes, unknown rate\n");
}
}
else
{
g_print (" rotational media: No\n");
}
if (props->drive_write_cache == NULL || strlen (props->drive_write_cache) == 0)
{
g_print (" write-cache: unknown\n");
}
else
{
g_print (" write-cache: %s\n", props->drive_write_cache);
}
g_print (" ejectable: %d\n", props->drive_is_media_ejectable);
g_print (" adapter: %s\n", strlen (props->drive_adapter) > 1 ? props->drive_adapter : "Unknown");
g_print (" ports:\n");
for (n = 0; props->drive_ports != NULL && props->drive_ports[n] != NULL; n++)
{
g_print (" %s\n", props->drive_ports[n]);
}
g_print (" similar devices:\n");
for (n = 0; props->drive_similar_devices != NULL && props->drive_similar_devices[n] != NULL; n++)
{
g_print (" %s\n", props->drive_similar_devices[n]);
}
if (props->drive_similar_devices != NULL && g_strv_length (props->drive_similar_devices) > 0)
{
if (!props->device_is_linux_dmmp_component && !props->device_is_linux_dmmp)
{
begin_highlight ();
g_print (" WARNING: Multiple devices with this serial and/or WWN has been detected\n"
" but dm-multipath is not active for these devices.\n");
end_highlight ();
}
}
g_print (" media: %s\n", props->drive_media);
g_print (" compat: ");
for (n = 0; props->drive_media_compatibility[n] != NULL; n++)
g_print (" %s", (char *) props->drive_media_compatibility[n]);
g_print ("\n");
if (props->drive_connection_interface == NULL || strlen (props->drive_connection_interface) == 0)
g_print (" interface: (unknown)\n");
else
g_print (" interface: %s\n", props->drive_connection_interface);
if (props->drive_connection_speed == 0)
g_print (" if speed: (unknown)\n");
else
g_print (" if speed: %" G_GINT64_FORMAT " bits/s\n", props->drive_connection_speed);
/* ------------------------------------------------------------------------------------------------- */
if (!props->drive_ata_smart_is_available)
{
g_print (" ATA SMART: not available\n");
}
else if (props->drive_ata_smart_time_collected == 0)
{
g_print (" ATA SMART: Data not collected\n");
}
else
{
SkDisk *d;
time = (time_t) props->drive_ata_smart_time_collected;
time_tm = localtime (&time);
strftime (time_buf, sizeof time_buf, "%c", time_tm);
g_print (" ATA SMART: Updated at %s\n", time_buf);
if (props->drive_ata_smart_status == NULL || strlen (props->drive_ata_smart_status) == 0)
{
g_print (" overall assessment: UNKNOWN\n");
}
else
{
const gchar *status_desc;
gboolean do_highlight;
status_desc = ata_smart_status_to_desc (props->drive_ata_smart_status, &do_highlight);
if (do_highlight)
begin_highlight ();
g_print (" overall assessment: %s\n", status_desc);
if (do_highlight)
end_highlight ();
}
if (sk_disk_open (NULL, &d) == 0)
{
if (sk_disk_set_blob (d,
props->drive_ata_smart_blob,
props->drive_ata_smart_blob_size) == 0)
{
g_print ("===============================================================================\n");
g_print (" Attribute Current|Worst|Threshold Status Value Type Updates\n");
g_print ("===============================================================================\n");
sk_disk_smart_parse_attributes (d, print_ata_smart_attr, NULL);
}
sk_disk_free (d);
}
}
/* ------------------------------------------------------------------------------------------------- */
}
device_properties_free (props);
}
/* ---------------------------------------------------------------------------------------------------- */
static gint
do_poll_for_media (const char *object_path)
{
DBusGProxy *proxy;
GError *error;
gint ret;
ret = 1;
proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", object_path, "org.freedesktop.UDisks.Device");
error = NULL;
if (!org_freedesktop_UDisks_Device_drive_poll_media (proxy, &error))
{
g_print ("Poll for media failed: %s\n", error->message);
g_error_free (error);
goto out;
}
ret = 0;
out:
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
static gint
do_inhibit_polling (const char *object_path,
gint argc,
gchar *argv[])
{
char *cookie;
DBusGProxy *proxy;
GError *error;
char **options;
gint ret;
options = NULL;
cookie = NULL;
ret = 127;
if (argc > 0 && strcmp (argv[0], "--") == 0)
{
argv++;
argc--;
}
proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", object_path, "org.freedesktop.UDisks.Device");
error = NULL;
if (!org_freedesktop_UDisks_Device_drive_inhibit_polling (proxy, (const char **) options, &cookie, &error))
{
g_print ("Inhibit polling failed: %s\n", error->message);
g_error_free (error);
goto out;
}
if (argc == 0)
{
g_print ("Inhibiting polling on %s. Press Ctrl+C to exit.\n", object_path);
while (TRUE)
sleep (100000000);
}
else
{
GError * error;
gint exit_status;
error = NULL;
if (!g_spawn_sync (NULL, /* working dir */
argv, NULL, /* envp */
G_SPAWN_SEARCH_PATH, NULL, /* child_setup */
NULL, /* user_data */
NULL, /* standard_output */
NULL, /* standard_error */
&exit_status, /* exit_status */
&error))
{
g_printerr ("Error launching program: %s\n", error->message);
g_error_free (error);
ret = 126;
goto out;
}
if (WIFEXITED (exit_status))
ret = WEXITSTATUS (exit_status);
else
ret = 125;
}
out:
g_free (cookie);
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
static gint
do_inhibit_all_polling (gint argc,
gchar *argv[])
{
char *cookie;
DBusGProxy *proxy;
GError *error;
char **options;
gint ret;
options = NULL;
cookie = NULL;
ret = 127;
if (argc > 0 && strcmp (argv[0], "--") == 0)
{
argv++;
argc--;
}
proxy
= dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", "/org/freedesktop/UDisks", "org.freedesktop.UDisks");
error = NULL;
if (!org_freedesktop_UDisks_drive_inhibit_all_polling (proxy, (const char **) options, &cookie, &error))
{
g_print ("Inhibit all polling failed: %s\n", error->message);
g_error_free (error);
goto out;
}
if (argc == 0)
{
g_print ("Inhibiting polling on all devices. Press Ctrl+C to exit.\n");
while (TRUE)
sleep (100000000);
}
else
{
GError * error;
gint exit_status;
error = NULL;
if (!g_spawn_sync (NULL, /* working dir */
argv, NULL, /* envp */
G_SPAWN_SEARCH_PATH, NULL, /* child_setup */
NULL, /* user_data */
NULL, /* standard_output */
NULL, /* standard_error */
&exit_status, /* exit_status */
&error))
{
g_printerr ("Error launching program: %s\n", error->message);
g_error_free (error);
ret = 126;
goto out;
}
if (WIFEXITED (exit_status))
ret = WEXITSTATUS (exit_status);
else
ret = 125;
}
out:
g_free (cookie);
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
static gint
do_set_spindown (const char *object_path,
gint argc,
gchar *argv[])
{
char *cookie;
DBusGProxy *proxy;
GError *error;
char **options;
gint ret;
options = NULL;
cookie = NULL;
ret = 127;
if (argc > 0 && strcmp (argv[0], "--") == 0)
{
argv++;
argc--;
}
proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", object_path, "org.freedesktop.UDisks.Device");
error = NULL;
if (!org_freedesktop_UDisks_Device_drive_set_spindown_timeout (proxy,
opt_spindown_seconds,
(const char **) options,
&cookie,
&error))
{
g_print ("Setting spindown failed: %s\n", error->message);
g_error_free (error);
goto out;
}
if (argc == 0)
{
g_print ("Set spindown on %s to %d seconds. Press Ctrl+C to exit.\n", object_path, opt_spindown_seconds);
while (TRUE)
sleep (100000000);
}
else
{
GError * error;
gint exit_status;
error = NULL;
if (!g_spawn_sync (NULL, /* working dir */
argv, NULL, /* envp */
G_SPAWN_SEARCH_PATH, NULL, /* child_setup */
NULL, /* user_data */
NULL, /* standard_output */
NULL, /* standard_error */
&exit_status, /* exit_status */
&error))
{
g_printerr ("Error launching program: %s\n", error->message);
g_error_free (error);
ret = 126;
goto out;
}
if (WIFEXITED (exit_status))
ret = WEXITSTATUS (exit_status);
else
ret = 125;
}
out:
g_free (cookie);
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
static gint
do_set_spindown_all (gint argc,
gchar *argv[])
{
char *cookie;
DBusGProxy *proxy;
GError *error;
char **options;
gint ret;
options = NULL;
cookie = NULL;
ret = 127;
if (argc > 0 && strcmp (argv[0], "--") == 0)
{
argv++;
argc--;
}
proxy
= dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", "/org/freedesktop/UDisks", "org.freedesktop.UDisks");
error = NULL;
if (!org_freedesktop_UDisks_drive_set_all_spindown_timeouts (proxy,
opt_spindown_seconds,
(const char **) options,
&cookie,
&error))
{
g_print ("Setting spindown failed: %s\n", error->message);
g_error_free (error);
goto out;
}
if (argc == 0)
{
g_print ("Set spindown for all drives to %d seconds. Press Ctrl+C to exit.\n", opt_spindown_seconds);
while (TRUE)
sleep (100000000);
}
else
{
GError * error;
gint exit_status;
error = NULL;
if (!g_spawn_sync (NULL, /* working dir */
argv, NULL, /* envp */
G_SPAWN_SEARCH_PATH, NULL, /* child_setup */
NULL, /* user_data */
NULL, /* standard_output */
NULL, /* standard_error */
&exit_status, /* exit_status */
&error))
{
g_printerr ("Error launching program: %s\n", error->message);
g_error_free (error);
ret = 126;
goto out;
}
if (WIFEXITED (exit_status))
ret = WEXITSTATUS (exit_status);
else
ret = 125;
}
out:
g_free (cookie);
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
static gint
do_inhibit (gint argc,
gchar *argv[])
{
char *cookie;
DBusGProxy *proxy;
GError *error;
gint ret;
cookie = NULL;
ret = 127;
if (argc > 0 && strcmp (argv[0], "--") == 0)
{
argv++;
argc--;
}
proxy
= dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", "/org/freedesktop/UDisks", "org.freedesktop.UDisks");
error = NULL;
if (!org_freedesktop_UDisks_inhibit (proxy, &cookie, &error))
{
g_print ("Inhibit all polling failed: %s\n", error->message);
g_error_free (error);
goto out;
}
if (argc == 0)
{
g_print ("Inhibiting the daemon. Press Ctrl+C to exit.\n");
while (TRUE)
sleep (100000000);
}
else
{
GError * error;
gint exit_status;
error = NULL;
if (!g_spawn_sync (NULL, /* working dir */
argv, NULL, /* envp */
G_SPAWN_SEARCH_PATH, NULL, /* child_setup */
NULL, /* user_data */
NULL, /* standard_output */
NULL, /* standard_error */
&exit_status, /* exit_status */
&error))
{
g_printerr ("Error launching program: %s\n", error->message);
g_error_free (error);
ret = 126;
goto out;
}
if (WIFEXITED (exit_status))
ret = WEXITSTATUS (exit_status);
else
ret = 125;
}
out:
g_free (cookie);
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
static gint
ptr_str_array_compare (const gchar **a,
const gchar **b)
{
return g_strcmp0 (*a, *b);
}
static gchar *
device_file_to_object_path (const gchar *device_file)
{
gchar *object_path;
DBusGProxy *proxy;
GError *error;
struct stat statbuf;
object_path = NULL;
error = NULL;
if (stat (device_file, &statbuf) != 0)
{
g_print ("Cannot stat device file %s: %m\n", device_file);
goto out;
}
if (!S_ISBLK (statbuf.st_mode))
{
g_print ("Device file %s is not a block device: %m\n", device_file);
goto out;
}
proxy
= dbus_g_proxy_new_for_name (bus, "org.freedesktop.UDisks", "/org/freedesktop/UDisks", "org.freedesktop.UDisks");
error = NULL;
if (!org_freedesktop_UDisks_find_device_by_major_minor (proxy,
major (statbuf.st_rdev),
minor (statbuf.st_rdev),
&object_path,
&error))
{
g_print ("Cannot find device with major:minor %d:%d: %s\n",
major (statbuf.st_rdev),
minor (statbuf.st_rdev),
error->message);
g_error_free (error);
goto out;
}
out:
return object_path;
}
int
main (int argc,
char **argv)
{
int ret;
GOptionContext *context;
GError *error = NULL;
unsigned int n;
gchar *device_file;
static GOptionEntry
entries[] =
{
{ "enumerate", 0, 0, G_OPTION_ARG_NONE, &opt_enumerate, "Enumerate objects paths for devices", NULL },
{ "enumerate-device-files", 0, 0, G_OPTION_ARG_NONE, &opt_enumerate_device_files,
"Enumerate device files for devices", NULL },
{ "dump", 0, 0, G_OPTION_ARG_NONE, &opt_dump, "Dump all information about all devices", NULL },
{ "monitor", 0, 0, G_OPTION_ARG_NONE, &opt_monitor, "Monitor activity from the disk daemon", NULL },
{ "monitor-detail", 0, 0, G_OPTION_ARG_NONE, &opt_monitor_detail, "Monitor with detail", NULL },
{ "show-info", 0, 0, G_OPTION_ARG_STRING, &opt_show_info, "Show information about a device file", NULL },
{ "inhibit-polling", 0, 0, G_OPTION_ARG_STRING, &opt_inhibit_polling, "Inhibit polling", NULL },
{ "inhibit-all-polling", 0, 0, G_OPTION_ARG_NONE, &opt_inhibit_all_polling, "Inhibit all polling", NULL },
{ "poll-for-media", 0, 0, G_OPTION_ARG_STRING, &opt_poll_for_media, "Poll for media", NULL },
{ "set-spindown", 0, 0, G_OPTION_ARG_STRING, &opt_drive_spindown, "Set spindown timeout for drive", NULL },
{ "set-spindown-all", 0, 0, G_OPTION_ARG_NONE, &opt_drive_spindown_all,
"Set spindown timeout for all drives", NULL },
{ "spindown-timeout", 0, 0, G_OPTION_ARG_INT, &opt_spindown_seconds, "Spindown timeout in seconds", NULL },
{ "inhibit", 0, 0, G_OPTION_ARG_NONE, &opt_inhibit, "Inhibit the daemon", NULL },
{ "mount", 0, 0, G_OPTION_ARG_STRING, &opt_mount, "Mount the given device", NULL },
{ "mount-fstype", 0, 0, G_OPTION_ARG_STRING, &opt_mount_fstype, "Specify file system type", NULL },
{ "mount-options", 0, 0, G_OPTION_ARG_STRING, &opt_mount_options, "Mount options separated by comma",
NULL },
{ "unmount", 0, 0, G_OPTION_ARG_STRING, &opt_unmount, "Unmount the given device", NULL },
{ "unmount-options", 0, 0, G_OPTION_ARG_STRING, &opt_unmount_options,
"Unmount options separated by comma", NULL },
{ "detach", 0, 0, G_OPTION_ARG_STRING, &opt_detach, "Detach the given device", NULL },
{ "detach-options", 0, 0, G_OPTION_ARG_STRING, &opt_detach_options, "Detach options separated by comma",
NULL },
{ "eject", 0, 0, G_OPTION_ARG_STRING, &opt_eject, "Eject the given device", NULL },
{ "eject-options", 0, 0, G_OPTION_ARG_STRING, &opt_eject_options, "Eject options separated by comma", NULL },
{ "ata-smart-refresh", 0, 0, G_OPTION_ARG_STRING, &opt_ata_smart_refresh, "Refresh ATA SMART data", NULL },
{ "ata-smart-wakeup", 0, 0, G_OPTION_ARG_NONE, &opt_ata_smart_wakeup,
"Wake up the disk if it is not awake", NULL },
{ "ata-smart-simulate", 0, 0, G_OPTION_ARG_STRING, &opt_ata_smart_simulate,
"Inject libatasmart BLOB for testing", NULL },
{ NULL } };
setlocale (LC_ALL, "");
ret = 1;
device_file = NULL;
g_type_init ();
context = g_option_context_new ("udisks commandline tool");
g_option_context_set_description (context, "See the udisks man page for details.");
g_option_context_add_main_entries (context, entries, NULL);
g_option_context_parse (context, &argc, &argv, NULL);
loop = g_main_loop_new (NULL, FALSE);
bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
if (bus == NULL)
{
g_warning ("Couldn't connect to system bus: %s", error->message);
g_error_free (error);
goto out;
}
dbus_g_object_register_marshaller (udisks_marshal_VOID__BOXED_BOOLEAN_STRING_UINT_BOOLEAN_DOUBLE,
G_TYPE_NONE,
DBUS_TYPE_G_OBJECT_PATH,
G_TYPE_BOOLEAN,
G_TYPE_STRING,
G_TYPE_UINT,
G_TYPE_BOOLEAN,
G_TYPE_DOUBLE,
G_TYPE_INVALID);
disks_proxy = dbus_g_proxy_new_for_name (bus,
"org.freedesktop.UDisks",
"/org/freedesktop/UDisks",
"org.freedesktop.UDisks");
dbus_g_proxy_add_signal (disks_proxy, "DeviceAdded", DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID);
dbus_g_proxy_add_signal (disks_proxy, "DeviceRemoved", DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID);
dbus_g_proxy_add_signal (disks_proxy, "DeviceChanged", DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID);
dbus_g_proxy_add_signal (disks_proxy,
"DeviceJobChanged",
DBUS_TYPE_G_OBJECT_PATH,
G_TYPE_BOOLEAN,
G_TYPE_STRING,
G_TYPE_UINT,
G_TYPE_BOOLEAN,
G_TYPE_DOUBLE,
G_TYPE_INVALID);
if (opt_dump)
{
GPtrArray *devices;
if (!org_freedesktop_UDisks_enumerate_devices (disks_proxy, &devices, &error))
{
g_warning ("Couldn't enumerate devices: %s", error->message);
g_error_free (error);
goto out;
}
g_ptr_array_sort (devices, (GCompareFunc) ptr_str_array_compare);
g_print ("========================================================================\n");
for (n = 0; n < devices->len; n++)
{
char *object_path = devices->pdata[n];
do_show_info (object_path);
g_print ("\n"
"========================================================================\n");
}
g_ptr_array_foreach (devices, (GFunc) g_free, NULL);
g_ptr_array_free (devices, TRUE);
}
else if (opt_enumerate)
{
GPtrArray *devices;
if (!org_freedesktop_UDisks_enumerate_devices (disks_proxy, &devices, &error))
{
g_warning ("Couldn't enumerate devices: %s", error->message);
g_error_free (error);
goto out;
}
for (n = 0; n < devices->len; n++)
{
char *object_path = devices->pdata[n];
g_print ("%s\n", object_path);
}
g_ptr_array_foreach (devices, (GFunc) g_free, NULL);
g_ptr_array_free (devices, TRUE);
}
else if (opt_enumerate_device_files)
{
gchar **device_files;
if (!org_freedesktop_UDisks_enumerate_device_files (disks_proxy, &device_files, &error))
{
g_warning ("Couldn't enumerate device files: %s", error->message);
g_error_free (error);
goto out;
}
for (n = 0; device_files != NULL && device_files[n] != NULL; n++)
{
g_print ("%s\n", device_files[n]);
}
g_strfreev (device_files);
}
else if (opt_monitor || opt_monitor_detail)
{
if (!do_monitor ())
goto out;
}
else if (opt_show_info != NULL)
{
device_file = device_file_to_object_path (opt_show_info);
if (device_file == NULL)
goto out;
do_show_info (device_file);
}
else if (opt_inhibit_polling != NULL)
{
device_file = device_file_to_object_path (opt_inhibit_polling);
if (device_file == NULL)
goto out;
ret = do_inhibit_polling (device_file, argc - 1, argv + 1);
goto out;
}
else if (opt_poll_for_media != NULL)
{
device_file = device_file_to_object_path (opt_poll_for_media);
if (device_file == NULL)
goto out;
ret = do_poll_for_media (device_file);
goto out;
}
else if (opt_inhibit_all_polling)
{
ret = do_inhibit_all_polling (argc - 1, argv + 1);
goto out;
}
else if (opt_drive_spindown != NULL)
{
device_file = device_file_to_object_path (opt_drive_spindown);
if (device_file == NULL)
goto out;
ret = do_set_spindown (device_file, argc - 1, argv + 1);
goto out;
}
else if (opt_drive_spindown_all)
{
ret = do_set_spindown_all (argc - 1, argv + 1);
goto out;
}
else if (opt_inhibit)
{
ret = do_inhibit (argc - 1, argv + 1);
goto out;
}
else if (opt_mount != NULL)
{
device_file = device_file_to_object_path (opt_mount);
if (device_file == NULL)
goto out;
do_mount (device_file, opt_mount_fstype, opt_mount_options);
}
else if (opt_unmount != NULL)
{
device_file = device_file_to_object_path (opt_unmount);
if (device_file == NULL)
goto out;
do_unmount (device_file, opt_unmount_options);
}
else if (opt_detach != NULL)
{
device_file = device_file_to_object_path (opt_detach);
if (device_file == NULL)
goto out;
do_detach (device_file, opt_detach_options);
}
else if (opt_eject != NULL)
{
device_file = device_file_to_object_path (opt_eject);
if (device_file == NULL)
goto out;
do_eject (device_file, opt_eject_options);
}
else if (opt_ata_smart_refresh != NULL)
{
device_file = device_file_to_object_path (opt_ata_smart_refresh);
if (device_file == NULL)
goto out;
do_ata_smart_refresh (device_file, opt_ata_smart_wakeup, opt_ata_smart_simulate);
}
else
{
gchar *usage;
usage = g_option_context_get_help (context, FALSE, NULL);
g_printerr ("%s", usage);
g_free (usage);
ret = 1;
goto out;
}
ret = 0;
out:
g_free (device_file);
if (disks_proxy != NULL)
g_object_unref (disks_proxy);
if (bus != NULL)
dbus_g_connection_unref (bus);
g_option_context_free (context);
return ret;
}
|