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
|
/* upstart
*
* conf.c - configuration management
*
* Copyright 2009,2010,2011,2012,2013 Canonical Ltd.
* Author: Scott James Remnant <scott@netsplit.com>.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <libgen.h>
#include <string.h>
#include <unistd.h>
#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/list.h>
#include <nih/hash.h>
#include <nih/string.h>
#include <nih/io.h>
#include <nih/file.h>
#include <nih/watch.h>
#include <nih/logging.h>
#include <nih/error.h>
#include <nih/errors.h>
#include "parse_job.h"
#include "parse_conf.h"
#include "conf.h"
#include "errors.h"
#include "paths.h"
#include "environ.h"
/* Prototypes for static functions */
static int conf_source_reload_file (ConfSource *source)
__attribute__ ((warn_unused_result));
static int conf_source_reload_dir (ConfSource *source)
__attribute__ ((warn_unused_result));
static int conf_file_filter (ConfSource *source, const char *path,
int is_dir);
static int conf_dir_filter (ConfSource *source, const char *path,
int is_dir);
static void conf_create_modify_handler (ConfSource *source, NihWatch *watch,
const char *path,
struct stat *statbuf);
static void conf_delete_handler (ConfSource *source, NihWatch *watch,
const char *path);
static int conf_file_visitor (ConfSource *source,
const char *dirname, const char *path,
struct stat *statbuf)
__attribute__ ((warn_unused_result));
static int conf_reload_path (ConfSource *source, const char *path,
const char *override_path)
__attribute__ ((warn_unused_result));
static inline int is_conf_file (const char *path)
__attribute__ ((warn_unused_result));
static inline int is_conf_file_std (const char *path)
__attribute__ ((warn_unused_result));
static inline int is_conf_file_override(const char *path)
__attribute__ ((warn_unused_result));
static inline char *toggle_conf_name (const void *parent, const char *path)
__attribute__ ((warn_unused_result));
static inline char * conf_to_job_name (const char *source_path,
const char *conf_path)
__attribute__ ((warn_unused_result));
static char * conf_get_best_override (const char *name,
const ConfSource *last_source)
__attribute__ ((warn_unused_result));
/**
* conf_sources:
*
* This list holds the list of known sources of configuration; each item
* is a ConfSource structure. The order of this list dictates the priority
* of the sources, with the first one having the highest priority.
**/
NihList *conf_sources = NULL;
extern json_object *json_conf_sources;
/**
* is_conf_file_std:
* @path: path to check.
*
* Determine if specified path contains a legitimate
* configuration file name.
*
* Returns: TRUE if @path contains a valid configuration file name,
* else FALSE.
*
**/
static inline int
is_conf_file_std (const char *path)
{
nih_assert (path != NULL);
char *ptr = strrchr (path, '.');
if (ptr && IS_CONF_EXT_STD (ptr))
return TRUE;
return FALSE;
}
/**
* is_conf_file_override:
* @path: path to check.
*
* Determine if specified path contains a legitimate
* override file name.
*
* Returns: TRUE if @path contains a valid override file name,
* else FALSE.
*
**/
static inline int
is_conf_file_override (const char *path)
{
nih_assert (path != NULL);
char *ptr = strrchr (path, '.');
if (ptr && IS_CONF_EXT_OVERRIDE (ptr))
return TRUE;
return FALSE;
}
/**
* is_conf_file:
* @path: path to check.
*
* Determine if specified path contains a legitimate
* configuration file or override file name.
*
* Returns: TRUE if @path contains a valid configuration
* file or override file name, else FALSE.
*
**/
static inline int
is_conf_file (const char *path)
{
nih_assert (path != NULL);
char *ptr = strrchr (path, '.');
if (ptr && (ptr > path) && (ptr[-1] != '/') && IS_CONF_EXT (ptr))
return TRUE;
return FALSE;
}
/**
* conf_to_job_name:
* @source_path: path to ConfSource
* @conf_path: path to configuration file
*
* Constructs the job name for a given @conf_path. Removes
* @source_path directory name from the front of @conf_path and
* extension from the end.
*
* Returns: newly-allocated name.
*
**/
static inline char *
conf_to_job_name (const char * source_path, const char * conf_path)
{
const char *start, *end;
char *name = NULL;
int source_len;
nih_assert (source_path != NULL);
nih_assert (conf_path != NULL);
start = conf_path;
source_len = strlen (source_path);
if (! strncmp (start, source_path, source_len))
start += source_len;
while (*start == '/')
start++;
end = strrchr (start, '.');
if (end && IS_CONF_EXT (end))
name = NIH_MUST (nih_strndup (NULL, start, end - start));
else
name = NIH_MUST (nih_strdup (NULL, start));
return name;
}
/**
* conf_get_best_override:
* @conf_file: conf_file object
*
* Given a @conf_file iterate over all config sources & find the
* first applicable override file. It will not look for override files
* beyond the @conf_file's ConfSource.
*
* Returns: newly allocated path to override file or NULL.
**/
static char *
conf_get_best_override (const char *name, const ConfSource *last_source)
{
char *try_path=NULL;
struct stat statbuf;
nih_assert (name != NULL);
nih_assert (last_source != NULL);
NIH_LIST_FOREACH (conf_sources, iter) {
ConfSource *source = (ConfSource *)iter;
/* Look at directories only */
if (source->type == CONF_FILE)
continue;
/* Reclaim memory */
if (try_path)
nih_free (try_path);
/* construct path */
try_path = NIH_MUST (nih_sprintf (NULL, "%s/%s%s", source->path, name, CONF_EXT_OVERRIDE));
/* Found it! */
if (lstat (try_path, &statbuf) == 0 && S_ISREG (statbuf.st_mode))
return try_path;
/* Warning, you have reached the end of the conveyor!
* Ignore overrides beyond .conf itself.
*/
if (source == last_source)
break;
}
if (try_path)
nih_free (try_path);
return NULL;
}
/**
* Convert a configuration file name to an override file name and vice
* versa.
*
* For example, if @path is "foo.conf", this function will return
* "foo.override", whereas if @path is "foo.override", it will return
* "foo.conf".
*
* @parent: parent of returned path,
* @path: path to a configuration file.
*
* Returns: newly allocated toggled path, or NULL on error.
**/
static inline char *
toggle_conf_name (const void *parent,
const char *path)
{
char *new_path;
char *ext;
char *new_ext;
size_t len;
ext = strrchr (path, '.');
if (!ext)
return NULL;
new_ext = IS_CONF_EXT_STD (ext)
? CONF_EXT_OVERRIDE
: CONF_EXT_STD;
len = strlen (new_ext);
new_path = NIH_MUST (nih_strndup (parent, path, (ext - path) + len));
memcpy (new_path + (ext - path), new_ext, len);
return new_path;
}
/**
* conf_init:
*
* Initialise the conf_sources list.
**/
void
conf_init (void)
{
if (! conf_sources)
conf_sources = NIH_MUST (nih_list_new (NULL));
}
/**
* conf_destroy:
*
* Clear: the conf_sources list.
**/
void
conf_destroy (void)
{
if (conf_sources)
nih_free (conf_sources);
}
/**
* conf_source_new:
* @parent: parent of new block,
* @path: path to source,
* @type: type of source.
*
* Allocates and returns a new ConfSource structure for the given @path;
* @type indicates whether this @path is a file or directory and what type
* of files are within the directory.
*
* The returned structure is automatically added to the conf_sources list.
*
* Configuration is not parsed immediately, instead you must call
* conf_source_reload() on this source to set up any watches and load the
* current configuration. Normally you would set up all of the sources and
* then call conf_reload() which will load them all.
*
* If @parent is not NULL, it should be a pointer to another allocated
* block which will be used as the parent for this block. When @parent
* is freed, the returned block will be freed too.
*
* Returns: newly allocated ConfSource structure or NULL if
* insufficient memory.
**/
ConfSource *
conf_source_new (const void *parent,
const char *path,
ConfSourceType type)
{
ConfSource *source;
nih_assert (path != NULL);
conf_init ();
source = nih_new (parent, ConfSource);
if (! source)
return NULL;
nih_list_init (&source->entry);
source->path = nih_strdup (source, path);
if (! source->path) {
nih_free (source);
return NULL;
}
source->type = type;
source->watch = NULL;
source->session = NULL;
source->flag = FALSE;
source->files = nih_hash_string_new (source, 0);
if (! source->files) {
nih_free (source);
return NULL;
}
nih_alloc_set_destructor (source, nih_list_destroy);
nih_list_add (conf_sources, &source->entry);
return source;
}
/**
* conf_file_new:
* @source: configuration source,
* @path: path to file.
*
* Allocates and returns a new ConfFile structure for the given @source,
* with @path indicating which file it is.
*
* The returned structure is automatically placed in the @source's files hash
* and the flag of the returned ConfFile will be set to that of the @source.
*
* Returns: newly allocated ConfFile structure or NULL if insufficient memory.
**/
ConfFile *
conf_file_new (ConfSource *source,
const char *path)
{
ConfFile *file;
nih_assert (source != NULL);
nih_assert (path != NULL);
file = nih_new (source, ConfFile);
if (! file)
return NULL;
nih_list_init (&file->entry);
file->path = nih_strdup (file, path);
if (! file->path) {
nih_free (file);
return NULL;
}
file->source = source;
file->flag = source->flag;
file->data = NULL;
nih_alloc_set_destructor (file, conf_file_destroy);
nih_hash_add (source->files, &file->entry);
return file;
}
/**
* conf_reload:
*
* Reloads configuration sources.
*
* Watches on new configuration sources are established so that future
* changes will be automatically detected with inotify. Then for both
* new and existing sources, the current state is parsed.
*
* All ConfFiles are recreated as part of the reload. If the JobClass
* associated with a ConfSource has no Job instances, the JobClass is
* recreated and added to the job_classes hash.
*
* However, if a JobClass has running instances at reload time, although
* a new ConfSource *and* a new JobClass are created, the new JobClass
* (called the "best") will *NOT* be added to the job_classes hash until
* the last running instance has finished. At this point, the "registered"
* JobClass will be removed from the hash (when job_class_reconsider()
* is called by job_change_state()) and replaced by the "best" (newest)
* JobClass.
*
* Any errors are logged through the usual mechanism, and not returned,
* since some configuration may have been parsed; and it's possible to
* parse no configuration without error.
**/
void
conf_reload (void)
{
conf_init ();
NIH_LIST_FOREACH (conf_sources, iter) {
ConfSource *source = (ConfSource *)iter;
if (conf_source_reload (source) < 0) {
NihError *err;
err = nih_error_get ();
if (err->number != ENOENT)
nih_error ("%s: %s: %s", source->path,
_("Unable to load configuration"),
err->message);
nih_free (err);
}
}
}
/**
* conf_source_reload:
* @source: configuration source to reload.
*
* Reloads the given configuration @source.
*
* If not already established, an inotify watch is created so that future
* changes to this source are automatically detected and parsed. For files,
* this watch is actually on the parent directory, since we need to watch
* out for editors that rename over the top, etc.
*
* We then parse the current state of the source. The flag member is
* toggled first, and this is propagated to all new and modified files and
* items that we find as a result of parsing. Once done, we scan for
* anything with the wrong flag, and delete them.
*
* Returns: zero on success, negative value on raised error.
**/
int
conf_source_reload (ConfSource *source)
{
NihList deleted;
int ret;
nih_assert (source != NULL);
nih_info (_("Loading configuration from %s"), source->path);
/* Toggle the flag so we can detect deleted files and items. */
source->flag = (! source->flag);
/* Reload the source itself. */
switch (source->type) {
case CONF_FILE:
ret = conf_source_reload_file (source);
break;
case CONF_DIR:
case CONF_JOB_DIR:
ret = conf_source_reload_dir (source);
break;
default:
nih_assert_not_reached ();
}
/* Scan for files that have been deleted since the last time we
* reloaded; these are simple to detect, as they will have the wrong
* flag.
*
* We take them out of the files list and then we can delete the
* attached jobs and free the file. We can't just do this from
* the one loop because to delete the jobs, we need to be able
* to iterate the sources and files.
*/
nih_list_init (&deleted);
NIH_HASH_FOREACH_SAFE (source->files, iter) {
ConfFile *file = (ConfFile *)iter;
if (file->flag != source->flag)
nih_list_add (&deleted, &file->entry);
}
NIH_LIST_FOREACH_SAFE (&deleted, iter) {
ConfFile *file = (ConfFile *)iter;
nih_info (_("Handling deletion of %s"), file->path);
nih_unref (file, source);
}
return ret;
}
/**
* conf_source_reload_file:
* @source: configuration source to reload.
*
* Reloads the configuration file specified by @source.
*
* If not already established, an inotify watch is created on the parent
* directory so that future changes to the file are automatically detected
* and parsed. It is the parent directory because we need to watch out for
* editors that rename over the top, etc.
*
* Returns: zero on success, negative value on raised error.
**/
static int
conf_source_reload_file (ConfSource *source)
{
NihError *err = NULL;
nih_local char *override_path = NULL;
struct stat statbuf;
nih_assert (source != NULL);
nih_assert (source->type == CONF_FILE);
/* this function should only be called for standard
* configuration files.
*/
if (! source->watch) {
nih_local char *dpath = NULL;
char *dname;
dpath = NIH_MUST (nih_strdup (NULL, source->path));
dname = dirname (dpath);
source->watch = nih_watch_new (source, dname, FALSE, FALSE,
(NihFileFilter)conf_file_filter,
(NihCreateHandler)conf_create_modify_handler,
(NihModifyHandler)conf_create_modify_handler,
(NihDeleteHandler)conf_delete_handler,
source);
/* If successful mark the file descriptor close-on-exec,
* otherwise stash the error for comparison with a later
* failure to parse the file.
*/
if (source->watch) {
nih_io_set_cloexec (source->watch->fd);
} else {
err = nih_error_steal ();
}
}
/* Parse the file itself. If this fails, then we can discard the
* inotify error, since this one will be better.
*/
if (conf_reload_path (source, source->path, NULL) < 0) {
if (err)
nih_free (err);
return -1;
}
/* We were able to parse the file, but were not able to set up an
* inotify watch. This isn't critical, so we just warn about it,
* unless this is simply that inotify isn't supported, in which case
* we do nothing.
*/
if (err) {
if (err->number != ENOSYS)
nih_warn ("%s: %s: %s", source->path,
_("Unable to watch configuration file"),
err->message);
nih_free (err);
}
if (! is_conf_file_std (source->path))
return 0;
override_path = toggle_conf_name (NULL, source->path);
if (lstat (override_path, &statbuf) != 0)
return 0;
nih_debug ("Updating configuration for %s from %s",
source->path, override_path);
if (conf_reload_path (source, source->path, override_path) < 0) {
if (err)
nih_free (err);
return -1;
}
return 0;
}
/**
* conf_source_reload_dir:
* @source: configuration source to reload.
*
* Reloads the configuration directory specified by @source.
*
* If not already established, an inotify watch is created on the directory
* so that future changes to the structure or files within it are
* automatically parsed. This has the side-effect of parsing the current
* tree.
*
* Otherwise we walk the tree ourselves and parse all files that we find,
* propagating the value of the flag member to all files so that deletion
* can be detected by the calling function.
*
* Returns: zero on success, negative value on raised error.
**/
static int
conf_source_reload_dir (ConfSource *source)
{
NihError *err = NULL;
nih_assert (source != NULL);
nih_assert (source->type != CONF_FILE);
if (! source->watch) {
source->watch = nih_watch_new (source, source->path,
TRUE, TRUE,
(NihFileFilter)conf_dir_filter,
(NihCreateHandler)conf_create_modify_handler,
(NihModifyHandler)conf_create_modify_handler,
(NihDeleteHandler)conf_delete_handler,
source);
/* If successful, the directory tree will have been walked
* already; so just mark the file descriptor close-on-exec
* and return; otherwise we'll try and walk ourselves, so
* stash the error for comparison.
*/
if (source->watch) {
nih_io_set_cloexec (source->watch->fd);
return 0;
} else {
err = nih_error_steal ();
}
}
/* We're either performing a mandatory reload, or we failed to set
* up an inotify watch; walk the directory tree the old fashioned
* way. If this fails too, then we can discard the inotify error
* since this one will be better.
*/
if (nih_dir_walk (source->path, (NihFileFilter)conf_dir_filter,
(NihFileVisitor)conf_file_visitor, NULL,
source) < 0) {
if (err)
nih_free (err);
return -1;
}
/* We were able to walk the directory, but were not able to set up
* an inotify watch. This isn't critical, so we just warn about it,
* unless this is simply that inotify isn't supported, in which case
* we do nothing.
*/
if (err) {
if (err->number != ENOSYS)
nih_warn ("%s: %s: %s", source->path,
_("Unable to watch configuration directory"),
err->message);
nih_free (err);
}
return 0;
}
/**
* conf_file_filter:
* @source: configuration source,
* @path: path to check,
* @is_dir: TRUE if @path is a directory.
*
* When we watch the parent directory of a file for changes, we receive
* notification about all changes to that directory. We only care about
* those that affect the path in @source, and the path that we're watching,
* so we use this function to filter out all others.
*
* Returns: FALSE if @path matches @source, TRUE otherwise.
**/
static int
conf_file_filter (ConfSource *source,
const char *path,
int is_dir)
{
nih_assert (source != NULL);
nih_assert (path != NULL);
if (! strcmp (source->path, path))
return FALSE;
if (! strcmp (source->watch->path, path))
return FALSE;
return TRUE;
}
/**
* conf_dir_filter:
* @source: configuration source,
* @path: path to check,
* @is_dir: TRUE of @path is a directory.
*
* This is the file filter used for the jobs directory, we only care
* about paths with particular extensions (see IS_CONF_EXT).
*
* Directories that match the nih_file_ignore() function are also ignored.
*
* Returns: FALSE if @path ends in ".conf" or ".override",
* or is the original source, TRUE otherwise.
**/
static int
conf_dir_filter (ConfSource *source,
const char *path,
int is_dir)
{
nih_assert (source != NULL);
nih_assert (path != NULL);
if (! strcmp (source->path, path))
return FALSE;
if (is_dir)
return nih_file_ignore (NULL, path);
if (is_conf_file (path))
return FALSE;
return TRUE;
}
/**
* conf_load_path_with_override:
* @source: configuration source
* @conf_path: path to config file
*
* Loads given @conf_path as a config file in a given @source. Then it
* finds an override file. If an override file is found it applies it
* as well.
**/
static void
conf_load_path_with_override (ConfSource *source,
const char *conf_path)
{
int ret = 0;
const char *error_path = NULL;
char *override_path = NULL;
nih_local char *job_name = NULL;
nih_assert (source != NULL);
nih_assert (conf_path != NULL);
/* reload conf file */
nih_debug ("Loading configuration file %s", conf_path);
ret = conf_reload_path (source, conf_path, NULL);
if (ret < 0) {
error_path = conf_path;
goto error;
}
job_name = conf_to_job_name (source->path, conf_path);
override_path = conf_get_best_override (job_name, source);
if (! override_path)
return;
/* overlay override settings */
nih_debug ("Loading override file %s for %s", conf_path, override_path);
ret = conf_reload_path (source, conf_path, override_path);
if (ret < 0) {
error_path = override_path;
goto error;
}
nih_free (override_path);
return;
error:
{
NihError *err;
err = nih_error_get ();
nih_error ("%s: %s: %s", error_path,
_("Error while loading configuration file"),
err->message);
nih_free (err);
if (override_path)
nih_free (override_path);
return;
}
}
/**
* conf_create_modify_handler:
* @source: configuration source,
* @watch: NihWatch for source,
* @path: full path to modified file,
* @statbuf: stat of @path.
*
* This function will be called whenever a file is created in a directory
* that we're watching, moved into the directory we're watching, or is
* modified. This works for both directory and file sources, since the
* watch for the latter is on the parent and filtered to only return the
* path that we're interested in.
*
* After checking that it was a regular file that was changed, we reload it;
* we expect this to fail sometimes since the file may be only partially
* written.
**/
static void
conf_create_modify_handler (ConfSource *source,
NihWatch *watch,
const char *path,
struct stat *statbuf)
{
ConfFile *file = NULL;
char *config_path = NULL;
nih_local char *job_name = NULL;
nih_assert (source != NULL);
nih_assert (watch != NULL);
nih_assert (path != NULL);
/* note that symbolic links are ignored */
if (statbuf && ! S_ISREG (statbuf->st_mode))
return;
/* ignore non-config file changes */
if (! is_conf_file (path))
return;
/* For config file, load it and it's override file */
if (is_conf_file_std (path)) {
conf_load_path_with_override (source, path);
return;
}
/* For override files, reload all matching conf+override combos */
job_name = conf_to_job_name (source->path, path);
NIH_LIST_FOREACH (conf_sources, iter) {
ConfSource *source = (ConfSource *)iter;
if (source->type == CONF_FILE)
continue;
config_path = NIH_MUST (nih_sprintf (NULL, "%s/%s%s", source->path, job_name, CONF_EXT_STD));
file = (ConfFile *)nih_hash_lookup (source->files, config_path);
if (file) {
/* Find its override file and reload both */
conf_load_path_with_override (source, config_path);
}
nih_free (config_path);
}
return;
}
/**
* conf_delete_handler:
* @source: configuration source,
* @watch: NihWatch for source,
* @path: full path to deleted file.
*
* This function will be called whenever a file is removed or moved out
* of a directory that we're watching. This works for both directory and
* file sources, since the watch for the latter is on the parent and
* filtered to only return the path that we're interested in.
*
* We lookup the file in our hash table, and if we can find it, perform
* the usual deletion of it.
**/
static void
conf_delete_handler (ConfSource *source,
NihWatch *watch,
const char *path)
{
ConfFile *file;
nih_local char *new_path = NULL;
nih_assert (source != NULL);
nih_assert (watch != NULL);
nih_assert (path != NULL);
/* Lookup the file in the source. If we haven't parsed it, this
* could actually mean that it was the top-level directory itself
* that was deleted, in which case we free the watch, otherwise
* it's probably a directory or something, so just ignore it.
*/
file = (ConfFile *)nih_hash_lookup (source->files, path);
/* Note we have to be careful to consider deletion of directories too.
* This is handled implicitly by the override check which will return
* false if passed a directory in this case.
*/
if (! file && ! is_conf_file_override (path)) {
if (! strcmp (watch->path, path)) {
nih_warn ("%s: %s", source->path,
_("Configuration directory deleted"));
nih_unref (source->watch, source);
source->watch = NULL;
}
return;
}
/* non-override files (and directories) are the simple case, so handle
* them and leave.
*/
if (! is_conf_file_override (path)) {
nih_unref (file, source);
return;
}
/* Deleting override file is about the same as changing one.
* We need to iterate across all matching jobs and reload them
* with new "best" override file, if any.
*/
nih_debug ("Reloading configuration for matching configs on deletion of override (%s)",
path);
conf_create_modify_handler (source, watch, path, NULL);
}
/**
* conf_file_visitor:
* @source: configuration source,
* @dirname: top-level directory being walked,
* @path: path found in directory,
* @statbuf: stat of @path.
*
* This function is called when walking a directory tree for each file
* found within it.
*
* After checking that it's a regular file, we reload it.
*
* Returns: always zero.
**/
static int
conf_file_visitor (ConfSource *source,
const char *dirname,
const char *path,
struct stat *statbuf)
{
nih_assert (source != NULL);
nih_assert (dirname != NULL);
nih_assert (path != NULL);
nih_assert (statbuf != NULL);
if (! S_ISREG (statbuf->st_mode))
return 0;
if (is_conf_file_std (path))
conf_load_path_with_override (source, path);
return 0;
}
/**
* conf_reload_path:
* @source: configuration source,
* @path: path of conf file to be reloaded.
* @override_path: if not NULL and @path refers to a path associated with @source,
* overlay the contents of @path into the existing @source entry for
* @path. If FALSE, discard any existing knowledge of @path.
*
* This function is used to parse the file at @path (or @override_path) in the
* context of the given configuration @source. Necessary ConfFile structures
* are allocated and attached to @source as appropriate. CONF_FILE sources
* always have a single ConfFile when the file exists.
*
* If the file has been parsed before, then the existing item is deleted and
* freed if the file fails to load, or after the new item has been parsed.
* Items are only reused between reloads if @override_path is
* non-NULL.
*
* Physical errors are returned, parse errors are not.
*
* Returns: zero on success, negative value on raised error.
**/
static int
conf_reload_path (ConfSource *source,
const char *path,
const char *override_path)
{
ConfFile *file = NULL;
ConfFile *orig = NULL;
nih_local char *buf = NULL;
nih_local char *name = NULL;
size_t len, pos, lineno;
NihError *err = NULL;
const char *path_to_load;
nih_assert (source != NULL);
nih_assert (path != NULL);
path_to_load = (override_path ? override_path : path);
/* If there is no corresponding override file, look up the old
* conf file in memory, and then free it. In cases of failure,
* we discard it anyway, so there's no particular reason
* to keep it around anymore.
*
* Notes:
*
* - If @override_path has been specified, do not free the file
* if found, since we want to _update_ the existing entry.
* - Freeing a ConfFile does _not_ necessarily free its associated
* JobClass.
*/
orig = (ConfFile *)nih_hash_lookup (source->files, path);
if (! override_path && orig) {
/* Found an existing ConfFile. We will free this, but
* just not yet since iff that ConfFiles associated JobClass
* does not have any running instances, freeing the
* ConfFile will cause the original JobClass associated
* with this ConfFile to be destroyed. But if the JobClass
* had referenced any events via it's 'start on' EventOperator tree,
* the JobClasses destruction could lead to the Events
* being destroyed _before_ the about-to-be-created
* replacement JobClass gets a chance to reference those
* same events (assuming its 'start on' EventOperator tree
* contains nodes specifying the same event names as
* those in the original JobClasses).
*
* As such, we simply remove the ConfFile from its
* parent ConfSources hash, create the new ConfFile and
* JobClass, give the new JobClass a chance to be the
* registered JobClass, and finally allow the original
* ConfFile to be destroyed.
*
* If this is not done, reloading a configuration
* mid-way through the boot sequence could lead to a
* hung system as the new JobClasses will wait forever
* for events to be emitted that have already been
* destroyed.
*/
nih_list_remove (&orig->entry);
}
/* Read the file into memory for parsing, if this fails we don't
* bother creating a new ConfFile structure for it and bail out
* now.
*/
buf = nih_file_read (NULL, path_to_load, &len);
if (! buf) {
if (! override_path && orig) {
/* Failed to reload the file from disk in all
* likelihood because the configuration file was
* deleted.
*
* Allow the ConfFile to be cleaned up taking
* its JobClass (and possibly events that
* JobClass was referencing) with it.
*/
nih_unref (orig, source);
}
return -1;
}
/* Create a new ConfFile structure (if no @override_path specified) */
file = (ConfFile *)nih_hash_lookup (source->files, path);
if (! file)
file = NIH_MUST (conf_file_new (source, path));
pos = 0;
lineno = 1;
switch (source->type) {
case CONF_FILE:
case CONF_DIR:
/* Simple file of options; usually no item attached to it. */
if (override_path) {
nih_debug ("Updating configuration for %s from %s",
path, override_path);
} else {
nih_debug ("Loading configuration from %s %s",
(source->type == CONF_DIR ? "directory" : "file"), path);
}
if (parse_conf (file, buf, len, &pos, &lineno) < 0)
err = nih_error_get ();
break;
case CONF_JOB_DIR:
name = conf_to_job_name (source->path, path);
/* Create a new job item and parse the buffer to produce
* the job definition.
*/
if (override_path) {
nih_debug ("Updating %s (%s) with %s",
name, path, override_path);
} else {
nih_debug ("Loading %s from %s", name, path);
}
file->job = parse_job (NULL, source->session, file->job,
name, buf, len, &pos, &lineno);
/* Allow the original ConfFile which has now been replaced to be
* destroyed which will also cause the original JobClass to be
* freed.
*/
if (file->job) {
job_class_consider (file->job);
} else {
err = nih_error_get ();
}
break;
default:
nih_assert_not_reached ();
}
/* Finally, allow the original ConfFile to be destroyed without
* affecting the new JobClass.
*/
if (! override_path && orig)
nih_unref (orig, source);
/* Deal with any parsing errors that occurred; we don't consider
* these to be hard failures, which means we can warn about them
* here and give the path and line number along with the warning.
*/
if (err) {
switch (err->number) {
case NIH_CONFIG_EXPECTED_TOKEN:
case NIH_CONFIG_UNEXPECTED_TOKEN:
case NIH_CONFIG_TRAILING_SLASH:
case NIH_CONFIG_UNTERMINATED_QUOTE:
case NIH_CONFIG_UNTERMINATED_BLOCK:
case NIH_CONFIG_UNKNOWN_STANZA:
case PARSE_ILLEGAL_INTERVAL:
case PARSE_ILLEGAL_EXIT:
case PARSE_ILLEGAL_UMASK:
case PARSE_ILLEGAL_NICE:
case PARSE_ILLEGAL_OOM:
case PARSE_ILLEGAL_LIMIT:
case PARSE_EXPECTED_EVENT:
case PARSE_EXPECTED_OPERATOR:
case PARSE_EXPECTED_VARIABLE:
case PARSE_MISMATCHED_PARENS:
nih_error ("%s:%zi: %s", path_to_load, lineno, err->message);
nih_free (err);
err = NULL;
break;
}
}
/* If we had any unknown error from parsing the file, raise it again
* and return an error condition.
*/
if (err)
return -1;
return 0;
}
/**
* conf_file_destroy:
* @file: configuration file to be destroyed.
*
* Handles the replacement and deletion of a configuration file, ensuring
* that @file is removed from the containing linked list and that the item
* attached to it is destroyed if not currently in use.
*
* Normally used or called from an nih_alloc() destructor so that the list
* item is automatically removed from its containing list when freed.
*
* Returns: zero.
**/
int
conf_file_destroy (ConfFile *file)
{
nih_assert (file != NULL);
nih_list_destroy (&file->entry);
switch (file->source->type) {
case CONF_FILE:
case CONF_DIR:
break;
case CONF_JOB_DIR:
if (! file->job)
break;
/* Mark the job to be deleted when it stops, in case
* it cannot be deleted here.
*/
file->job->deleted = TRUE;
/* Check whether the job is the current one with that name;
* if it is, try and replace it. If it wasn't the current
* job, or isn't after replacement, we can free it now.
*/
if (job_class_reconsider (file->job)) {
nih_debug ("Destroyed unused job %s", file->job->name);
nih_free (file->job);
}
break;
default:
nih_assert_not_reached ();
}
return 0;
}
/**
* conf_select_job:
* @name: name of job class to locate,
* @session: session class name belongs to.
*
* Select the best available class of a job named @name from the registered
* configuration sources.
*
* Returns: Best available job class or NULL if none available.
**/
JobClass *
conf_select_job (const char *name, const Session *session)
{
nih_assert (name != NULL);
conf_init ();
NIH_LIST_FOREACH (conf_sources, iter) {
ConfSource *source = (ConfSource *)iter;
if (source->type != CONF_JOB_DIR)
continue;
if (source->session != session)
continue;
NIH_HASH_FOREACH (source->files, file_iter) {
ConfFile *file = (ConfFile *)file_iter;
if (! file->job)
continue;
if (! strcmp (file->job->name, name))
return file->job;
}
}
return NULL;
}
/**
* conf_source_serialise:
* @source: ConfSource to serialise.
*
* Convert @source into a JSON representation for serialisation.
* Caller must free returned value using json_object_put().
*
* Returns: JSON-serialised ConfSource object, or NULL on error.
**/
json_object *
conf_source_serialise (const ConfSource *source)
{
json_object *json;
json_object *json_files;
int session_index;
nih_assert (source);
nih_assert (conf_sources);
json = json_object_new_object ();
if (! json)
return NULL;
json_files = json_object_new_array ();
if (! json_files)
goto error;
session_index = session_get_index (source->session);
if (session_index < 0)
goto error;
if (! state_set_json_int_var (json, "session", session_index))
goto error;
if (! state_set_json_string_var_from_obj (json, source, path))
goto error;
if (! state_set_json_enum_var (json,
conf_source_type_enum_to_str,
"type", source->type))
goto error;
/* 'watch' does not need to be serialised - it gets recreated
* when conf_source_new() is called on deserialisation.
*/
if (! state_set_json_int_var_from_obj (json, source, flag))
goto error;
/* Add array of ConfFile names to represent the files hash */
NIH_HASH_FOREACH (source->files, file_iter) {
ConfFile *file = (ConfFile *)file_iter;
json_object *json_conf_file;
json_conf_file = conf_file_serialise (file);
if (! json_conf_file)
goto error;
json_object_array_add (json_files, json_conf_file);
}
json_object_object_add (json, "conf_files", json_files);
return json;
error:
json_object_put (json);
return NULL;
}
/**
* conf_source_serialise_all:
*
* Convert existing ConfSource objects to JSON representation.
*
* Returns: JSON object containing array of ConfSource objects,
* or NULL on error.
**/
json_object *
conf_source_serialise_all (void)
{
json_object *json;
conf_init ();
json = json_object_new_array ();
if (! json)
return NULL;
NIH_LIST_FOREACH (conf_sources, iter) {
json_object *json_source;
ConfSource *source = (ConfSource *)iter;
json_source = conf_source_serialise (source);
if (! json_source)
goto error;
json_object_array_add (json, json_source);
}
return json;
error:
json_object_put (json);
return NULL;
}
/**
* conf_source_deserialise:
* @parent: parent,
* @json: JSON-serialised ConfSource object to deserialise.
*
* Create ConfSource from provided JSON and add to the
* conf sources list.
*
* Returns: ConfSource object, or NULL on error.
**/
ConfSource *
conf_source_deserialise (void *parent, json_object *json)
{
ConfSource *source = NULL;
ConfSourceType type = -1;
Session *session;
int session_index = -1;
nih_local char *path = NULL;
nih_assert (json);
if (! state_check_json_type (json, object))
goto error;
if (! state_get_json_int_var (json, "session", session_index))
goto error;
if (session_index < 0)
goto error;
session = session_from_index (session_index);
if (! state_get_json_string_var_strict (json, "path", NULL, path))
goto error;
if (! state_get_json_enum_var (json,
conf_source_type_str_to_enum,
"type", type))
goto error;
source = conf_source_new (parent, path, type);
if (! source)
goto error;
source->session = session;
if (! state_get_json_int_var_to_obj (json, source, flag))
goto error;
if (conf_file_deserialise_all (source, json) < 0)
goto error;
return source;
error:
if (source)
nih_free (source);
return NULL;
}
/**
* conf_source_deserialise_all:
*
* @json: root of JSON-serialised state.
*
* Convert JSON representation of ConfSources back into ConfSource objects.
*
* Returns: 0 on success, -1 on error.
**/
int
conf_source_deserialise_all (json_object *json)
{
ConfSource *source = NULL;
nih_assert (json);
conf_init ();
nih_assert (NIH_LIST_EMPTY (conf_sources));
json_conf_sources = json_object_object_get (json, "conf_sources");
if (! json_conf_sources)
goto error;
if (! state_check_json_type (json_conf_sources, array))
goto error;
for (int i = 0; i < json_object_array_length (json_conf_sources); i++) {
json_object *json_source;
json_source = json_object_array_get_idx (json_conf_sources, i);
if (! json_source)
goto error;
if (! state_check_json_type (json_source, object))
goto error;
source = conf_source_deserialise (NULL, json_source);
if (! source)
continue;
}
return 0;
error:
if (source)
nih_free (source);
return -1;
}
/**
* conf_source_type_enum_to_str:
*
* @type: ConfSourceType.
*
* Convert ConfSourceType to a string representation.
*
* Returns: string representation of @type, or NULL if not known.
**/
const char *
conf_source_type_enum_to_str (ConfSourceType type)
{
state_enum_to_str (CONF_FILE, type);
state_enum_to_str (CONF_DIR, type);
state_enum_to_str (CONF_JOB_DIR, type);
return NULL;
}
/**
* conf_source_type_str_to_enum:
*
* @type: string ConfSourceType value.
*
* Convert @expect back into an enum value.
*
* Returns: ConfSourceType representing @type, or -1 if not known.
**/
ConfSourceType
conf_source_type_str_to_enum (const char *type)
{
nih_assert (type);
state_str_to_enum (CONF_FILE, type);
state_str_to_enum (CONF_DIR, type);
state_str_to_enum (CONF_JOB_DIR, type);
return -1;
}
/**
* conf_file_serialise:
* @file: ConfFile to serialise.
*
* Convert @file into a JSON representation for serialisation.
* Caller must free returned value using json_object_put().
*
* Returns: JSON-serialised ConfFile object, or NULL on error.
**/
json_object *
conf_file_serialise (const ConfFile *file)
{
json_object *json;
json_object *json_job_class;
JobClass *registered;
int session_index;
ssize_t conf_source_index;
nih_assert (file);
json = json_object_new_object ();
if (! json)
return NULL;
if (! state_set_json_string_var_from_obj (json, file, path))
goto error;
conf_source_index = conf_source_get_index (file->source);
if (conf_source_index < 0)
goto error;
if (! state_set_json_int_var (json, "conf_source", conf_source_index))
goto error;
if (! state_set_json_int_var_from_obj (json, file, flag))
goto error;
/*
* Ignore the "best" JobClass associated with this ConfFile
* (file->job) since it won't be serialised.
*
* Instead look up the currently registered JobClass and
* reference that. This ensures that best == registered for the
* re-exec. This may change though immediately after re-exec
* when conf_reload() gets called.
*
* See job_class_serialise_all() for further details.
*/
registered = job_class_get_registered (file->job->name,
file->job->session);
if (! registered)
goto error;
/* Create a reference to the registered job class in the JSON by
* encoding the name and session index. We do this rather than
* simply encoding an index number for the JobClass since
* job_classes is a hash and it is safer should a re-exec
* result from an upgrade to NIH, say, where its hashing
* algorithm changed meaning the index may be unreliable once
* the job_classes hash was created post-re-exec.
*/
json_job_class = json_object_new_object ();
if (! json_job_class)
goto error;
if (! state_set_json_string_var (json_job_class,
"name",
registered->name))
goto error;
session_index = session_get_index (registered->session);
if (session_index < 0)
goto error;
if (! state_set_json_int_var (json_job_class,
"session",
session_index))
goto error;
json_object_object_add (json, "job_class", json_job_class);
return json;
error:
json_object_put (json);
return NULL;
}
/**
* conf_file_deserialise:
* @source: ConfSource,
* @json: JSON-serialised ConfFile object to deserialise.
*
* Create ConfFile from provided JSON and add to the
* conf sources list.
*
* Returns: ConfFile object, or NULL on error.
**/
ConfFile *
conf_file_deserialise (ConfSource *source, json_object *json)
{
ConfFile *file = NULL;
nih_local char *path = NULL;
nih_assert (json);
if (! state_check_json_type (json, object))
goto error;
if (! state_get_json_string_var_strict (json, "path", NULL, path))
goto error;
file = conf_file_new (source, path);
if (! file)
goto error;
/* Note that the associated JobClass is not handled at this
* stage: it can't be the JobClasses haven't been deserialised
* yet. As such, the ConfFile->JobClass link is dealt with by
* job_class_deserialise_all().
*/
file->job = NULL;
if (! state_get_json_int_var_to_obj (json, file, flag))
goto error;
return file;
error:
if (file)
nih_free (file);
return NULL;
}
/**
* conf_file_deserialise_all:
*
* @source: ConfSource,
* @json: root of JSON-serialised state.
*
* Convert JSON representation of ConfFiles back into ConfFile objects.
*
* Returns: 0 on success, -1 on error.
**/
int
conf_file_deserialise_all (ConfSource *source, json_object *json)
{
json_object *json_conf_files;
ConfFile *file = NULL;
nih_assert (source);
nih_assert (json);
conf_init ();
json_conf_files = json_object_object_get (json, "conf_files");
if (! json_conf_files)
goto error;
if (! state_check_json_type (json_conf_files, array))
goto error;
for (int i = 0; i < json_object_array_length (json_conf_files); i++) {
json_object *json_file;
json_file = json_object_array_get_idx (json_conf_files, i);
if (! json_file)
goto error;
if (! state_check_json_type (json_file, object))
goto error;
file = conf_file_deserialise (source, json_file);
if (! file)
goto error;
}
return 0;
error:
if (file)
nih_free (file);
return -1;
}
/**
* conf_source_get_index:
*
* @source: ConfSource to search for.
*
* Returns: index of @source in the conf sources list,
* or -1 if not found.
**/
ssize_t
conf_source_get_index (const ConfSource *source)
{
ssize_t i = 0;
nih_assert (source);
conf_init ();
NIH_LIST_FOREACH (conf_sources, iter) {
ConfSource *s = (ConfSource *)iter;
if (! strcmp (source->path, s->path)
&& source->session == s->session)
return i;
i++;
}
return -1;
}
/**
* conf_file_find:
*
* @name: name of ConfFile (without dirname and extension),
* @session: session ConfFile belongs to.
*
* Find the ConfFile with name @name in session @session.
*
* Returns: ConfFile or NULL if not found.
**/
ConfFile *
conf_file_find (const char *name, const Session *session)
{
nih_local char *basename = NULL;
nih_assert (name);
conf_init ();
/* There can only be one ConfFile per session with the same
* basename.
*/
basename = NIH_MUST (nih_sprintf (NULL, "/%s%s",
name, CONF_EXT_STD));
NIH_LIST_FOREACH (conf_sources, iter) {
ConfSource *source = (ConfSource *)iter;
if (source->session != session)
continue;
NIH_HASH_FOREACH (source->files, file_iter) {
ConfFile *file = (ConfFile *)file_iter;
char *match;
char *slash;
match = strstr (file->path, basename);
slash = strrchr (file->path, '/');
if (match && match == slash)
return file;
}
}
return NULL;
}
#ifdef DEBUG
void
debug_show_event_operator (EventOperator *oper)
{
nih_local char *env = NULL;
nih_assert (oper);
if (oper->env) env = state_collapse_env ((const char **)oper->env);
nih_debug ("EventOperator %p: type='%s', value=%d, name='%s', event='%s', env='%s'",
oper,
oper->type == EVENT_MATCH ? "EVENT_MATCH"
: oper->type == EVENT_AND ? "EVENT_AND"
: "EVENT_OR",
oper->value,
oper->name,
oper->event ? oper->event->name : "",
env ? env : "");
}
void
debug_show_event_operators (EventOperator *root)
{
nih_assert (root);
NIH_TREE_FOREACH_POST (&root->node, iter) {
EventOperator *oper = (EventOperator *)iter;
debug_show_event_operator (oper);
}
}
size_t
debug_count_list_entries (const NihList *list)
{
size_t i = 0;
NIH_LIST_FOREACH (list, iter) {
i++;
}
return i;
}
size_t
debug_count_hash_entries (const NihHash *hash)
{
size_t i = 0;
NIH_HASH_FOREACH_SAFE (hash, iter) {
i++;
}
return i;
}
void
debug_show_job_class (const JobClass *class)
{
nih_local char *start_on = NULL;
nih_local char *stop_on = NULL;
nih_assert (class);
nih_debug ("JobClass %p: name='%s', path='%s', task=%d, "
"respawn=%d, console=%x, deleted=%d, debug=%d",
class, class->name, class->path, class->task,
class->respawn, class->console, class->deleted, class->debug);
if (class->start_on) start_on = event_operator_collapse (class->start_on);
if (class->stop_on) stop_on = event_operator_collapse (class->stop_on);
nih_debug ("\tstart_on=%p (%s), stop_on=%p (%s), emits=%p, process=%p",
class->start_on, start_on ? start_on : "",
class->stop_on, stop_on ? stop_on : "",
class->emits, class->process);
for (int i = 0; i < PROCESS_LAST; i++) {
if (class->process[i]) {
nih_debug ("Process[%d]=%p: script=%d, cmd='%s'",
i, class->process[i],
class->process[i]->script,
class->process[i]->command);
} else {
nih_debug ("Process[%d]=%p",
i, class->process[i]);
}
}
nih_debug ("\tauthor='%s', description='%s'",
class->author, class->description);
if (class->env && *class->env) {
nih_local char *env = state_collapse_env ((const char **)class->env);
nih_debug ("\tenv:%s", env);
} else {
nih_debug ("\tenv: none.");
}
if (class->export && *class->export) {
nih_local char *export = state_collapse_env ((const char **)class->export);
nih_debug ("\texport:%s", export);
} else {
nih_debug ("\texport: none");
}
debug_show_jobs (class->instances);
}
void
debug_show_job_classes (void)
{
nih_debug ("job_classes:");
NIH_HASH_FOREACH_SAFE (job_classes, iter) {
JobClass *job = (JobClass *)iter;
debug_show_job_class (job);
}
}
void
debug_show_job (const Job *job)
{
nih_local char *env = NULL;
nih_local char *start_env = NULL;
nih_local char *stop_env = NULL;
nih_local char *stop_on = NULL;
nih_assert (job);
if (job->env)
env = state_collapse_env ((const char **)job->env);
if (job->start_env)
start_env = state_collapse_env ((const char **)job->start_env);
if (job->stop_env)
stop_env = state_collapse_env ((const char **)job->stop_env);
if (job->stop_on)
stop_on = event_operator_collapse (job->stop_on);
nih_debug ("Job %p: name=%s, class=%p (%s), path=%s, env='%s'"
"start_env='%s', stop_env='%s', stop_on='%s', "
"goal=%d, state=%d, failed=%d, exit_status=%d",
job,
job->name ? job->name : "",
job->class, job->class->name,
job->path,
env ? env : "",
start_env ? start_env : "",
stop_env ? stop_env : "",
stop_on ? stop_on : "",
job->goal,
job->state,
job->failed,
job->exit_status);
}
void
debug_show_jobs (const NihHash *instances)
{
if (! instances)
return;
nih_debug ("jobs:");
NIH_HASH_FOREACH (instances, iter) {
Job *job = (Job *)iter;
debug_show_job (job);
}
}
void
debug_show_event (const Event *event)
{
nih_assert (event);
nih_debug ("Event %p: name='%s', progress=%x, failed=%d, "
"blockers=%d, blocking=%p",
event, event->name, event->progress, event->failed,
event->blockers, (void *)&event->blocking);
}
void
debug_show_events (void)
{
nih_assert (events);
NIH_LIST_FOREACH (events, iter) {
Event *event = (Event *)iter;
debug_show_event (event);
}
}
void
debug_show_conf_file (const ConfFile *file)
{
nih_assert (file);
nih_debug ("ConfFile %p: path='%s', source=%p, flag=%x, job=%p",
file, file->path, file->source, file->flag, file->job);
/* Some ConfFile objects won't have any JobClass details, for example,
* the ConfFile object associated with "/etc/init.conf".
*/
if (! file->job) {
nih_debug ("ConfFile %p: job: no JobClass object.", file);
return;
}
nih_debug ("ConfFile %p: job:", file);
debug_show_job_class (file->job);
}
void
debug_show_conf_source (const ConfSource *source)
{
nih_assert (source);
nih_debug ("ConfSource %p: path='%s', type=%x, flag=%x",
source, source->path, source->type, source->flag);
nih_debug ("ConfSource %p files (%d):", source,
(int)debug_count_hash_entries (source->files));
NIH_HASH_FOREACH (source->files, file_iter) {
ConfFile *file = (ConfFile *)file_iter;
debug_show_conf_file (file);
}
}
void
debug_show_conf_sources (void)
{
nih_assert (conf_sources);
nih_debug ("conf_sources:");
NIH_LIST_FOREACH (conf_sources, iter) {
ConfSource *source = (ConfSource *)iter;
debug_show_conf_source (source);
}
}
#endif /* DEBUG */
|