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 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
|
/* upstart
*
* job_class.c - job class definition handling
*
* Copyright 2011 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 <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/string.h>
#include <nih/list.h>
#include <nih/hash.h>
#include <nih/tree.h>
#include <nih/logging.h>
#include <nih-dbus/dbus_error.h>
#include <nih-dbus/dbus_message.h>
#include <nih-dbus/dbus_object.h>
#include <nih-dbus/dbus_util.h>
#include "dbus/upstart.h"
#include "environ.h"
#include "process.h"
#include "session.h"
#include "job_class.h"
#include "job.h"
#include "event_operator.h"
#include "blocked.h"
#include "conf.h"
#include "control.h"
#include "parse_job.h"
#include "com.ubuntu.Upstart.h"
#include "com.ubuntu.Upstart.Job.h"
#include <json.h>
extern json_object *json_classes;
extern int user_mode;
extern int no_inherit_env;
extern char **environ;
/* Prototypes for static functions */
static void job_class_add (JobClass *class);
static int job_class_remove (JobClass *class, const Session *session);
/**
* default_console:
*
* If a job does not specify a value for the 'console' stanza, use this value.
*
* Only used if value is >= 0;
**/
int default_console = -1;
/**
* job_classes:
*
* This hash table holds the list of known job classes indexed by their name.
* Each entry is a JobClass structure; multiple entries with the same name
* are not permitted.
**/
NihHash *job_classes = NULL;
/**
* job_environ:
*
* Array of environment variables that will be set in the jobs
* environment.
**/
static char **job_environ = NULL;
/**
* initial_umask:
*
* Value of umask at startup.
**/
mode_t initial_umask;
/**
* job_class_init:
*
* Initialise the job classes hash table.
**/
void
job_class_init (void)
{
if (! job_classes)
job_classes = NIH_MUST (nih_hash_string_new (NULL, 0));
}
/**
* job_class_environ_init:
*
* Initialise the job_environ array.
**/
void
job_class_environment_init (void)
{
char * const default_environ[] = { JOB_DEFAULT_ENVIRONMENT, NULL };
if (job_environ)
return;
job_environ = NIH_MUST (nih_str_array_new (NULL));
NIH_MUST (environ_append (&job_environ, NULL, 0, TRUE, default_environ));
if (user_mode && ! no_inherit_env)
NIH_MUST(environ_append (&job_environ, NULL, 0, TRUE, environ));
}
/**
* job_class_environment_reset:
*
* Reset the environment back to defaults.
*
* Note: not applied to running job instances.
**/
void
job_class_environment_reset (void)
{
job_class_environment_clear ();
job_class_environment_init ();
}
/**
* job_class_environment_clear:
*
* Clear the environment table.
**/
void
job_class_environment_clear (void)
{
if (job_environ) {
nih_free (job_environ);
job_environ = NULL;
}
}
/**
* job_class_environment_set:
*
* @var: environment variable to set in form 'name[=value]',
* @replace: TRUE if @name should be overwritten if already set, else
* FALSE.
*
* Set specified variable in job environment.
*
* Returns: 0 on success, -1 on error.
**/
int
job_class_environment_set (const char *var, int replace)
{
nih_assert (var);
nih_assert (job_environ);
if (! environ_add (&job_environ, NULL, NULL, replace, var))
return -1;
/* Update all running jobs */
NIH_HASH_FOREACH (job_classes, iter) {
JobClass *class = (JobClass *)iter;
NIH_HASH_FOREACH (class->instances, job_iter) {
Job *job = (Job *)job_iter;
if (! environ_add (&job->env, job, NULL, replace, var))
return -1;
}
}
return 0;
}
/**
* job_class_environment_unset:
*
* @var: name of environment variable to unset.
*
* Remove specified variable from job environment array.
*
* Returns: 0 on success, -1 on error.
**/
int
job_class_environment_unset (const char *name)
{
nih_assert (name);
nih_assert (job_environ);
if (! environ_remove (&job_environ, NULL, NULL, name))
return -1;
/* Update all running jobs */
NIH_HASH_FOREACH (job_classes, iter) {
JobClass *class = (JobClass *)iter;
NIH_HASH_FOREACH (class->instances, job_iter) {
Job *job = (Job *)job_iter;
if ( ! environ_remove (&job->env, job, NULL, name))
return -1;
}
}
return 0;
}
/**
* job_class_environment_get_all:
*
* @parent: parent for new environment array.
*
* Obtain a copy of the entire environment a job will be provided with.
*
* Returns: Newly-allocated copy of the job environment array,
* or NULL on error.
**/
char **
job_class_environment_get_all (const void *parent)
{
nih_assert (job_environ);
return nih_str_array_copy (parent, NULL, job_environ);
}
/**
* job_class_environment_get:
*
* @name: name of variable to query.
*
* Determine value of variable @name in job environment.
*
* XXX: The returned value must not be freed.
*
* Returns: pointer to static storage value of @name, or NULL if @name
* does not exist in job environment.
**/
const char *
job_class_environment_get (const char *name)
{
nih_assert (name);
nih_assert (job_environ);
return environ_get (job_environ, name);
}
/**
* job_class_new:
*
* @parent: parent for new job class,
* @name: name of new job class,
* @session: session.
*
* Allocates and returns a new JobClass structure with the given @name
* and @session. It will not be automatically added to the job classes
* table, it is up to the caller to ensure this is done using
* job_class_register() once the class has been set up.
*
* If @parent is not NULL, it should be a pointer to another object which
* will be used as a parent for the returned job class. When all parents
* of the returned job class are freed, the returned job class will also be
* freed.
*
* Returns: newly allocated JobClass structure or NULL if insufficient memory.
**/
JobClass *
job_class_new (const void *parent,
const char *name,
Session *session)
{
JobClass *class;
int i;
nih_assert (name != NULL);
nih_assert (strlen (name) > 0);
class = nih_new (parent, JobClass);
if (! class)
return NULL;
nih_list_init (&class->entry);
nih_alloc_set_destructor (class, nih_list_destroy);
class->name = nih_strdup (class, name);
if (! class->name)
goto error;
class->session = session;
if (class->session && class->session->chroot) {
class->path = nih_dbus_path (class, DBUS_PATH_UPSTART, "jobs",
session->chroot,
class->name, NULL);
} else {
class->path = nih_dbus_path (class, DBUS_PATH_UPSTART, "jobs",
class->name, NULL);
}
if (! class->path)
goto error;
class->instance = nih_strdup (class, "");
if (! class->instance)
goto error;
class->instances = nih_hash_string_new (class, 0);
if (! class->instances)
goto error;
class->description = NULL;
class->author = NULL;
class->version = NULL;
class->env = NULL;
class->export = NULL;
class->start_on = NULL;
class->stop_on = NULL;
class->emits = NULL;
class->process = nih_alloc (class, sizeof (Process *) * PROCESS_LAST);
if (! class->process)
goto error;
for (i = 0; i < PROCESS_LAST; i++)
class->process[i] = NULL;
class->expect = EXPECT_NONE;
class->task = FALSE;
class->kill_timeout = JOB_DEFAULT_KILL_TIMEOUT;
class->kill_signal = SIGTERM;
class->reload_signal = SIGHUP;
class->respawn = FALSE;
class->respawn_limit = JOB_DEFAULT_RESPAWN_LIMIT;
class->respawn_interval = JOB_DEFAULT_RESPAWN_INTERVAL;
class->normalexit = NULL;
class->normalexit_len = 0;
class->console = default_console >= 0 ? default_console : CONSOLE_LOG;
class->umask = (user_mode && ! no_inherit_env) ? initial_umask : JOB_DEFAULT_UMASK;
class->nice = JOB_NICE_INVALID;
class->oom_score_adj = JOB_DEFAULT_OOM_SCORE_ADJ;
for (i = 0; i < RLIMIT_NLIMITS; i++)
class->limits[i] = NULL;
class->chroot = NULL;
class->chdir = NULL;
class->setuid = NULL;
class->setgid = NULL;
class->deleted = FALSE;
class->debug = FALSE;
class->usage = NULL;
class->apparmor_switch = NULL;
return class;
error:
nih_free (class);
return NULL;
}
/**
* job_class_get_registered:
*
* @name: name of JobClass to search for,
* @session: Session of @class.
*
* Determine the currently registered JobClass with name @name for
* session @session.
*
* Returns: JobClass or NULL if no JobClass with name @name and
* session @session is registered.
**/
JobClass *
job_class_get_registered (const char *name, const Session *session)
{
JobClass *registered = NULL;
nih_assert (name);
job_class_init ();
/* If we found an entry, ensure we only consider the appropriate session */
do {
registered = (JobClass *)nih_hash_search (job_classes,
name, registered ? ®istered->entry : NULL);
} while (registered && registered->session != session);
return registered;
}
/**
* job_class_consider:
* @class: job class to consider.
*
* Considers adding @class to the job classes hash table as the best
* available class, if there is no existing class with the name or the
* existing class can be replaced.
*
* Returns: TRUE if @class is now the registered class, FALSE otherwise.
**/
int
job_class_consider (JobClass *class)
{
JobClass *registered = NULL;
JobClass *best = NULL;
nih_assert (class != NULL);
job_class_init ();
best = conf_select_job (class->name, class->session);
nih_assert (best != NULL);
nih_assert (best->session == class->session);
registered = job_class_get_registered (class->name, class->session);
if (registered != best) {
if (registered) {
job_class_event_block (NULL, registered, best);
if (! job_class_remove (registered, class->session)) {
/* Couldn't deregister, so undo */
if (best->start_on)
event_operator_reset (best->start_on);
return FALSE;
}
}
job_class_add (best);
}
return (class == best ? TRUE : FALSE);
}
/**
* job_class_reconsider:
* @class: job class to reconsider.
*
* Reconsiders whether @class should be the best available class in the
* job classes hash table, if it is the existing class and can be
* replaced by a better then it will be.
*
* Note that the best class may be itself unless you have first removed
* @class from any configuration sources before calling.
*
* Returns: FALSE if @class is still the hash table member, TRUE otherwise.
**/
int
job_class_reconsider (JobClass *class)
{
JobClass *registered = NULL;
JobClass *best = NULL;
nih_assert (class != NULL);
job_class_init ();
best = conf_select_job (class->name, class->session);
registered = job_class_get_registered (class->name, class->session);
if (registered == class) {
if (class != best) {
if (! job_class_remove (class, class->session))
return FALSE;
job_class_add (best);
return TRUE;
} else {
return FALSE;
}
}
return TRUE;
}
/**
* job_class_event_block:
*
* @parent: parent object for list,
* @old: original JobClass currently registered in job_classes,
* @new: new "best" JobClass that is not yet present in job_classes.
*
* Compare @old and @new start on EventOperator trees looking for
* matching events that occur in both (_and_ which implicitly still exist
* in the global events list). Events that satisfy these criteria will have
* their reference count elevated to allow @new to replace @old in job_classes
* without the destruction of @old freeing the events in question.
*
* Note that the reference count never needs to be decremented back
* again since this function effectively passes "ownership" of the event
* block from @old to @new, since @old will be replaced by @new but @new
* should replicate the EventOperator state of @old.
**/
void
job_class_event_block (void *parent, JobClass *old, JobClass *new)
{
EventOperator *old_root;
EventOperator *new_root;
if (! old || ! new)
return;
old_root = old->start_on;
new_root = new->start_on;
/* If either @old or @new are NULL, or have no start_on
* condition, there is no need to modify any events.
*/
if (! old_root || ! new_root)
return;
/* The old JobClass has associated instances meaning it
* will not be possible for job_class_remove() to replace it, so
* we don't need to manipulate any event reference counts.
*/
NIH_HASH_FOREACH (old->instances, iter)
return;
NIH_TREE_FOREACH_POST (&old_root->node, iter) {
EventOperator *old_oper = (EventOperator *)iter;
Event *event;
if (old_oper->type != EVENT_MATCH)
continue;
/* Ignore nodes that are not blocking events */
if (! old_oper->event)
continue;
/* Since the JobClass is blocking an event,
* that event must be valid.
*/
event = old_oper->event;
NIH_TREE_FOREACH_POST (&new_root->node, niter) {
EventOperator *new_oper = (EventOperator *)niter;
if (new_oper->type != EVENT_MATCH)
continue;
/* ignore the return - we just want to ensure
* that any events in @new that match those in
* @old have identical nodes.
*/
(void)event_operator_handle (new_oper, event, NULL);
}
}
}
/**
* job_class_add:
* @class: new class to select.
*
* Adds @class to the hash table and registers it with all current D-Bus
* connections. @class may be NULL.
**/
static void
job_class_add (JobClass *class)
{
control_init ();
if (! class)
return;
nih_hash_add (job_classes, &class->entry);
NIH_LIST_FOREACH (control_conns, iter) {
NihListEntry *entry = (NihListEntry *)iter;
DBusConnection *conn = (DBusConnection *)entry->data;
job_class_register (class, conn, TRUE);
}
}
/**
* job_class_add_safe:
* @class: new class to select.
*
* Adds @class to the hash table iff no existing entry of the
* same name exists for the same session.
**/
void
job_class_add_safe (JobClass *class)
{
JobClass *registered = NULL;
nih_assert (class);
nih_assert (class->name);
control_init ();
registered = job_class_get_registered (class->name, class->session);
nih_assert (! registered);
job_class_add (class);
}
/**
* job_class_remove:
* @class: class to remove,
* @session: Session of @class.
*
* Removes @class from the hash table and unregisters it from all current
* D-Bus connections.
*
* Returns: TRUE if class could be unregistered, FALSE if there are
* active instances that prevent unregistration, or if @session
* does not match the session associated with @class.
**/
static int
job_class_remove (JobClass *class, const Session *session)
{
nih_assert (class != NULL);
if (class->session != session)
return FALSE;
control_init ();
/* Return if we have any active instances */
NIH_HASH_FOREACH (class->instances, iter)
return FALSE;
nih_list_remove (&class->entry);
NIH_LIST_FOREACH (control_conns, iter) {
NihListEntry *entry = (NihListEntry *)iter;
DBusConnection *conn = (DBusConnection *)entry->data;
job_class_unregister (class, conn);
}
return TRUE;
}
/**
* job_class_register:
* @class: class to register,
* @conn: connection to register for
* @signal: emit the JobAdded signal.
*
* Register the job @class with the D-Bus connection @conn, using the
* path set when the class was created. Since multiple classes with the
* same name may exist, this should only ever be called with the current
* class of that name, and job_class_unregister() should be used before
* registering a new one with the same name.
**/
void
job_class_register (JobClass *class,
DBusConnection *conn,
int signal)
{
nih_assert (class != NULL);
nih_assert (conn != NULL);
NIH_MUST (nih_dbus_object_new (class, conn, class->path,
job_class_interfaces, class));
nih_debug ("Registered job %s", class->path);
if (signal)
NIH_ZERO (control_emit_job_added (conn, DBUS_PATH_UPSTART,
class->path));
NIH_HASH_FOREACH (class->instances, iter) {
Job *job = (Job *)iter;
job_register (job, conn, signal);
}
}
/**
* job_class_unregister:
* @class: class to unregistered,
* @conn: connection to unregister from.
*
* Unregister the job @class from the D-Bus connection @conn, which must
* have already been registered with job_class_register().
**/
void
job_class_unregister (JobClass *class,
DBusConnection *conn)
{
nih_assert (class != NULL);
nih_assert (conn != NULL);
NIH_HASH_FOREACH (class->instances, iter)
nih_assert_not_reached ();
NIH_MUST (dbus_connection_unregister_object_path (conn, class->path));
nih_debug ("Unregistered job %s", class->path);
NIH_ZERO (control_emit_job_removed (conn, DBUS_PATH_UPSTART,
class->path));
}
/**
* job_class_environment:
* @parent: parent object for new table,
* @class: job class,
* @len: pointer to variable to store table length.
*
* Constructs an environment table containing the standard environment
* variables and defined in the job's @class.
*
* This table is suitable for storing in @job's env member so that it is
* used for all processes spawned by the job.
*
* If @len is not NULL it will be updated to contain the new array length.
*
* If @parent is not NULL, it should be a pointer to another object which
* will be used as a parent for the returned array. When all parents
* of the returned array are freed, the returned array will also be
* freed.
*
* Returns: new environment table or NULL if insufficient memory.
**/
char **
job_class_environment (const void *parent,
JobClass *class,
size_t *len)
{
char **env;
nih_assert (class != NULL);
nih_assert (job_environ);
env = nih_str_array_new (parent);
if (! env)
return NULL;
if (len)
*len = 0;
/* Copy the set of environment variables, usually these just
* pick up the values from init's own environment.
*/
if (! environ_append (&env, parent, len, TRUE, job_environ))
goto error;
/* Copy the set of environment variables from the job configuration,
* these often have values but also often don't and we want them to
* override the builtins.
*/
if (! environ_append (&env, parent, len, TRUE, class->env))
goto error;
return env;
error:
nih_free (env);
return NULL;
}
/**
* job_class_get_instance:
* @class: job class to be query,
* @message: D-Bus connection and message received,
* @env: NULL-terminated array of environment variables,
* @instance: pointer for instance name.
*
* Implements the GetInstance method of the com.ubuntu.Upstart.Job
* interface.
*
* Called to obtain the path of an instance based on @env, which is used
* to locate the instance in the same way that Start, Stop and Restart do.
*
* If no such instance is found, the com.ubuntu.Upstart.Error.UnknownInstance
* D-Bus error will be returned immediately.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_get_instance (JobClass *class,
NihDBusMessage *message,
char * const *env,
char **instance)
{
Job *job;
nih_local char **instance_env = NULL;
nih_local char *name = NULL;
size_t len;
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (env != NULL);
/* Verify that the environment is valid */
if (! environ_all_valid (env)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
_("Env must be KEY=VALUE pairs"));
return -1;
}
/* Construct the full environment for the instance based on the class
* and that provided.
*/
instance_env = job_class_environment (NULL, class, &len);
if (! instance_env)
nih_return_system_error (-1);
if (! environ_append (&instance_env, NULL, &len, TRUE, env))
nih_return_system_error (-1);
/* Use the environment to expand the instance name and look it up
* in the job.
*/
name = environ_expand (NULL, class->instance, instance_env);
if (! name) {
NihError *error;
nih_local char *error_message = NULL;
error = nih_error_get ();
if (error->number != ENOMEM) {
error = nih_error_steal ();
error_message = nih_strdup (NULL, error->message);
if (! error_message)
nih_return_system_error (-1);
if (class->usage) {
if (! nih_strcat_sprintf (&error_message, NULL,
"\n%s: %s", _("Usage"), class->usage)) {
nih_return_system_error (-1);
}
}
nih_dbus_error_raise (DBUS_ERROR_INVALID_ARGS,
error_message);
nih_free (error);
}
return -1;
}
job = (Job *)nih_hash_lookup (class->instances, name);
if (! job) {
nih_dbus_error_raise_printf (
DBUS_INTERFACE_UPSTART ".Error.UnknownInstance",
_("Unknown instance: %s"), name);
return -1;
}
*instance = nih_strdup (message, job->path);
if (! *instance)
nih_return_system_error (-1);
return 0;
}
/**
* job_class_get_instance_by_name:
* @class: class to obtain instance from,
* @message: D-Bus connection and message received,
* @name: name of instance to get,
* @instance: pointer for object path reply.
*
* Implements the GetInstanceByName method of the com.ubuntu.Upstart.Job
* interface.
*
* Called to obtain the path to a D-Bus object for the instance named
* @name of this job which will be stored in @job. If no instance with
* that name exists, the com.ubuntu.Upstart.Error.UnknownInstance D-Bus
* error will be raised.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_get_instance_by_name (JobClass *class,
NihDBusMessage *message,
const char *name,
char **instance)
{
Job *job;
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (name != NULL);
nih_assert (instance != NULL);
job = (Job *)nih_hash_lookup (class->instances, name);
if (! job) {
nih_dbus_error_raise_printf (
DBUS_INTERFACE_UPSTART ".Error.UnknownInstance",
_("Unknown instance: %s"), name);
return -1;
}
*instance = nih_strdup (message, job->path);
if (! *instance)
nih_return_system_error (-1);
return 0;
}
/**
* job_class_get_all_instances:
* @class: class to obtain instance from,
* @message: D-Bus connection and message received,
* @instances: pointer for array of object paths reply.
*
* Implements the GetAllInstances method of the com.ubuntu.Upstart.Job
* interface.
*
* Called to obtain the paths of all instances for the given @class, which
* will be stored in @instances. If no instances exist, @instances will
* point to an empty array.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_get_all_instances (JobClass *class,
NihDBusMessage *message,
char ***instances)
{
char **list;
size_t len;
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (instances != NULL);
len = 0;
list = nih_str_array_new (message);
if (! list)
nih_return_system_error (-1);
NIH_HASH_FOREACH (class->instances, iter) {
Job *job = (Job *)iter;
if (! nih_str_array_add (&list, message, &len, job->path)) {
nih_error_raise_system ();
nih_free (list);
return -1;
}
}
*instances = list;
return 0;
}
/**
* job_class_start:
* @class: job class to be started,
* @message: D-Bus connection and message received,
* @env: NULL-terminated array of environment variables,
* @wait: whether to wait for command to finish before returning.
*
* Implements the top half of the Start method of the com.ubuntu.Upstart.Job
* interface, the bottom half may be found in job_finished().
*
* This is the primary method to start new instances of jobs. The given
* @env will be used to locate an existing instance, or create a new one
* if necessary; in either case, the instance will be set to be started
* (or restarted if it is currently stopping) with @env as its new
* environment.
*
* If the instance goal is already start,
* the com.ubuntu.Upstart.Error.AlreadyStarted D-Bus error will be returned
* immediately. If the instance fails to start, the
* com.ubuntu.Upstart.Error.JobFailed D-Bus error will be returned when the
* problem occurs.
*
* When @wait is TRUE the method call will not return until the job has
* finished starting (running for tasks); when @wait is FALSE, the method
* call returns once the command has been processed and the goal changed.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_start (JobClass *class,
NihDBusMessage *message,
char * const *env,
int wait)
{
Session *session;
Blocked *blocked = NULL;
Job *job;
nih_local char **start_env = NULL;
nih_local char *name = NULL;
size_t len;
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (env != NULL);
/* Don't permit out-of-session modification */
session = session_from_dbus (NULL, message);
if (session != class->session) {
nih_dbus_error_raise_printf (
DBUS_INTERFACE_UPSTART ".Error.PermissionDenied",
_("You do not have permission to modify job: %s"),
class->name);
return -1;
}
/* Verify that the environment is valid */
if (! environ_all_valid (env)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
_("Env must be KEY=VALUE pairs"));
return -1;
}
/* Construct the full environment for the instance based on the class
* and that provided.
*/
start_env = job_class_environment (NULL, class, &len);
if (! start_env)
nih_return_system_error (-1);
if (! environ_append (&start_env, NULL, &len, TRUE, env))
nih_return_system_error (-1);
/* Use the environment to expand the instance name and look it up
* in the job.
*/
name = environ_expand (NULL, class->instance, start_env);
if (! name) {
NihError *error;
nih_local char *error_message = NULL;
error = nih_error_get ();
if (error->number != ENOMEM) {
error = nih_error_steal ();
error_message = nih_strdup (NULL, error->message);
if (! error_message)
nih_return_system_error (-1);
if (class->usage) {
if (! nih_strcat_sprintf (&error_message, NULL,
"\n%s: %s", _("Usage"), class->usage)) {
nih_return_system_error (-1);
}
}
nih_dbus_error_raise (DBUS_ERROR_INVALID_ARGS,
error_message);
nih_free (error);
}
return -1;
}
job = (Job *)nih_hash_lookup (class->instances, name);
/* If no instance exists with the expanded name, create a new
* instance.
*/
if (! job) {
job = job_new (class, name);
if (! job)
nih_return_system_error (-1);
}
if (job->goal == JOB_START) {
nih_dbus_error_raise_printf (
DBUS_INTERFACE_UPSTART ".Error.AlreadyStarted",
_("Job is already running: %s"),
job_name (job));
return -1;
}
if (wait)
blocked = NIH_MUST (blocked_new (job, BLOCKED_JOB_START_METHOD,
message));
if (job->start_env)
nih_unref (job->start_env, job);
job->start_env = start_env;
nih_ref (job->start_env, job);
job_finished (job, FALSE);
if (blocked)
nih_list_add (&job->blocking, &blocked->entry);
job_change_goal (job, JOB_START);
if (! wait)
NIH_ZERO (job_class_start_reply (message, job->path));
return 0;
}
/**
* job_class_stop:
* @class: job class to be stopped,
* @message: D-Bus connection and message received,
* @env: NULL-terminated array of environment variables,
* @wait: whether to wait for command to finish before returning.
*
* Implements the top half of the Stop method of the com.ubuntu.Upstart.Job
* interface, the bottom half may be found in job_finished().
*
* This is the primary method to stop instances of jobs. The given @env
* will be used to locate an existing instance which will be set to be
* stopped with @env as the environment passed to the pre-stop script.
*
* If no such instance is found, the com.ubuntu.Upstart.Error.UnknownInstance
* D-Bus error will be returned immediately. If the instance goal is already
* stop, the com.ubuntu.Upstart.Error.AlreadyStopped D-Bus error will be
* returned immediately. If the instance fails to stop, the
* com.ubuntu.Upstart.Error.JobFailed D-Bus error will be returned when the
* problem occurs.
*
* When @wait is TRUE the method call will not return until the job has
* finished stopping; when @wait is FALSE, the method call returns once
* the command has been processed and the goal changed.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_stop (JobClass *class,
NihDBusMessage *message,
char * const *env,
int wait)
{
Session *session;
Blocked *blocked = NULL;
Job *job;
nih_local char **stop_env = NULL;
nih_local char *name = NULL;
size_t len;
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (env != NULL);
/* Don't permit out-of-session modification */
session = session_from_dbus (NULL, message);
if (session != class->session) {
nih_dbus_error_raise_printf (
DBUS_INTERFACE_UPSTART ".Error.PermissionDenied",
_("You do not have permission to modify job: %s"),
class->name);
return -1;
}
/* Verify that the environment is valid */
if (! environ_all_valid (env)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
_("Env must be KEY=VALUE pairs"));
return -1;
}
/* Construct the full environment for the instance based on the class
* and that provided; while we don't pass this to the instance itself,
* we need this to look up the instance in the first place.
*/
stop_env = job_class_environment (NULL, class, &len);
if (! stop_env)
nih_return_system_error (-1);
if (! environ_append (&stop_env, NULL, &len, TRUE, env))
nih_return_system_error (-1);
/* Use the environment to expand the instance name and look it up
* in the job.
*/
name = environ_expand (NULL, class->instance, stop_env);
if (! name) {
NihError *error;
error = nih_error_get ();
if (error->number != ENOMEM) {
error = nih_error_steal ();
nih_dbus_error_raise (DBUS_ERROR_INVALID_ARGS,
error->message);
nih_free (error);
}
return -1;
}
job = (Job *)nih_hash_lookup (class->instances, name);
if (! job) {
nih_dbus_error_raise_printf (
DBUS_INTERFACE_UPSTART ".Error.UnknownInstance",
_("Unknown instance: %s"), name);
return -1;
}
if (job->goal == JOB_STOP) {
nih_dbus_error_raise_printf (
DBUS_INTERFACE_UPSTART ".Error.AlreadyStopped",
_("Job has already been stopped: %s"),
job_name (job));
return -1;
}
if (wait)
blocked = NIH_MUST (blocked_new (job, BLOCKED_JOB_STOP_METHOD,
message));
if (job->stop_env)
nih_unref (job->stop_env, job);
job->stop_env = (char **)env;
nih_ref (job->stop_env, job);
job_finished (job, FALSE);
if (blocked)
nih_list_add (&job->blocking, &blocked->entry);
job_change_goal (job, JOB_STOP);
if (! wait)
NIH_ZERO (job_class_stop_reply (message));
return 0;
}
/**
* job_restart:
* @class: job class to be restarted,
* @message: D-Bus connection and message received,
* @env: NULL-terminated array of environment variables,
* @wait: whether to wait for command to finish before returning.
*
* Implements the top half of the Restart method of the com.ubuntu.Upstart.Job
* interface, the bottom half may be found in job_finished().
*
* This is the primary method to restart existing instances of jobs; while
* calling both "Stop" and "Start" may have the same effect, there is no
* guarantee of atomicity.
*
* The given @env will be used to locate the existing instance, which will
* be stopped and then restarted with @env as its new environment.
*
* If no such instance is found, the com.ubuntu.Upstart.Error.UnknownInstance
* D-Bus error will be returned immediately. If the instance goal is already
* stop, the com.ubuntu.Upstart.Error.AlreadyStopped D-Bus error will be
* returned immediately. If the instance fails to restart, the
* com.ubuntu.Upstart.Error.JobFailed D-Bus error will be returned when the
* problem occurs.
*
* When @wait is TRUE the method call will not return until the job has
* finished starting again (running for tasks); when @wait is FALSE, the
* method call returns once the command has been processed and the goal
* changed.
* Returns: zero on success, negative value on raised error.
**/
int
job_class_restart (JobClass *class,
NihDBusMessage *message,
char * const *env,
int wait)
{
Session *session;
Blocked *blocked = NULL;
Job *job;
nih_local char **restart_env = NULL;
nih_local char *name = NULL;
size_t len;
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (env != NULL);
/* Don't permit out-of-session modification */
session = session_from_dbus (NULL, message);
if (session != class->session) {
nih_dbus_error_raise_printf (
DBUS_INTERFACE_UPSTART ".Error.PermissionDenied",
_("You do not have permission to modify job: %s"),
class->name);
return -1;
}
/* Verify that the environment is valid */
if (! environ_all_valid (env)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
_("Env must be KEY=VALUE pairs"));
return -1;
}
/* Construct the full environment for the instance based on the class
* and that provided.
*/
restart_env = job_class_environment (NULL, class, &len);
if (! restart_env)
nih_return_system_error (-1);
if (! environ_append (&restart_env, NULL, &len, TRUE, env))
nih_return_system_error (-1);
/* Use the environment to expand the instance name and look it up
* in the job.
*/
name = environ_expand (NULL, class->instance, restart_env);
if (! name) {
NihError *error;
error = nih_error_get ();
if (error->number != ENOMEM) {
error = nih_error_steal ();
nih_dbus_error_raise (DBUS_ERROR_INVALID_ARGS,
error->message);
nih_free (error);
}
return -1;
}
job = (Job *)nih_hash_lookup (class->instances, name);
if (! job) {
nih_dbus_error_raise_printf (
DBUS_INTERFACE_UPSTART ".Error.UnknownInstance",
_("Unknown instance: %s"), name);
return -1;
}
if (job->goal == JOB_STOP) {
nih_dbus_error_raise_printf (
DBUS_INTERFACE_UPSTART ".Error.AlreadyStopped",
_("Job has already been stopped: %s"), job->name);
return -1;
}
if (wait)
blocked = NIH_MUST (blocked_new (job,
BLOCKED_JOB_RESTART_METHOD,
message));
if (job->start_env)
nih_unref (job->start_env, job);
job->start_env = restart_env;
nih_ref (job->start_env, job);
if (job->stop_env)
nih_unref (job->stop_env, job);
job->stop_env = NULL;
job_finished (job, FALSE);
if (blocked)
nih_list_add (&job->blocking, &blocked->entry);
job_change_goal (job, JOB_STOP);
job_change_goal (job, JOB_START);
if (! wait)
NIH_ZERO (job_class_restart_reply (message, job->path));
return 0;
}
/**
* job_class_get:
*
* @name: name of job class,
* @session: session of job class.
*
* Obtain JobClass with name @name and session @session.
*
* Returns: JobClass, or NULL if no matching job class found.
**/
JobClass *
job_class_get (const char *name, Session *session)
{
JobClass *class = NULL;
NihList *prev = NULL;
nih_assert (name);
job_class_init ();
do {
class = (JobClass *)nih_hash_search (job_classes, name, prev);
if (! class)
return NULL;
if (class && class->session == session)
return class;
nih_assert (class);
prev = (NihList *)class;
} while (TRUE);
nih_assert_not_reached ();
}
/**
* job_class_get_name:
* @class: class to obtain name from,
* @message: D-Bus connection and message received,
* @name: pointer for reply string.
*
* Implements the get method for the name property of the
* com.ubuntu.Upstart.Job interface.
*
* Called to obtain the name of the given @class, which will be stored in
* @name.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_get_name (JobClass *class,
NihDBusMessage *message,
char **name)
{
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (name != NULL);
*name = class->name;
nih_ref (*name, message);
return 0;
}
/**
* job_class_get_description:
* @class: class to obtain name from,
* @message: D-Bus connection and message received,
* @description: pointer for reply string.
*
* Implements the get method for the description property of the
* com.ubuntu.Upstart.Job interface.
*
* Called to obtain the description of the given @class, which will be stored
* in @description.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_get_description (JobClass *class,
NihDBusMessage *message,
char **description)
{
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (description != NULL);
if (class->description) {
*description = class->description;
nih_ref (*description, message);
} else {
*description = nih_strdup (message, "");
if (! *description)
nih_return_no_memory_error (-1);
}
return 0;
}
/**
* job_class_get_author:
* @class: class to obtain name from,
* @message: D-Bus connection and message received,
* @author: pointer for reply string.
*
* Implements the get method for the author property of the
* com.ubuntu.Upstart.Job interface.
*
* Called to obtain the author of the given @class, which will be stored
* in @author.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_get_author (JobClass *class,
NihDBusMessage *message,
char **author)
{
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (author != NULL);
if (class->author) {
*author = class->author;
nih_ref (*author, message);
} else {
*author = nih_strdup (message, "");
if (! *author)
nih_return_no_memory_error (-1);
}
return 0;
}
/**
* job_class_get_version:
* @class: class to obtain name from,
* @message: D-Bus connection and message received,
* @version: pointer for reply string.
*
* Implements the get method for the version property of the
* com.ubuntu.Upstart.Job interface.
*
* Called to obtain the version of the given @class, which will be stored
* in @version.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_get_version (JobClass *class,
NihDBusMessage *message,
char **version)
{
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (version != NULL);
if (class->version) {
*version = class->version;
nih_ref (*version, message);
} else {
*version = nih_strdup (message, "");
if (! *version)
nih_return_no_memory_error (-1);
}
return 0;
}
/**
* job_class_get_start_on:
* @class: class to obtain events from,
* @message: D-Bus connection and message received,
* @start_on: pointer for reply array.
*
* Implements the get method for the start_on property of the
* com.ubuntu.Upstart.Job interface.
*
* Called to obtain the set of events that will start jobs of the given
* @class, this is returned as an array of the event tree flattened into
* reverse polish form.
*
* Each array element is an array of strings representing the events,
* or a single element containing "/OR" or "/AND" to represent the
* operators.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_get_start_on (JobClass * class,
NihDBusMessage *message,
char **** start_on)
{
size_t len = 0;
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (start_on != NULL);
*start_on = nih_alloc (message, sizeof (char ***));
if (! *start_on)
nih_return_no_memory_error (-1);
len = 0;
(*start_on)[len] = NULL;
if (class->start_on) {
NIH_TREE_FOREACH_POST (&class->start_on->node, iter) {
EventOperator *oper = (EventOperator *)iter;
*start_on = nih_realloc (*start_on, message,
sizeof (char ***) * (len + 2));
if (! *start_on)
nih_return_no_memory_error (-1);
(*start_on)[len] = nih_str_array_new (*start_on);
if (! (*start_on)[len])
nih_return_no_memory_error (-1);
switch (oper->type) {
case EVENT_OR:
if (! nih_str_array_add (&(*start_on)[len], *start_on,
NULL, "/OR"))
nih_return_no_memory_error (-1);
break;
case EVENT_AND:
if (! nih_str_array_add (&(*start_on)[len], *start_on,
NULL, "/AND"))
nih_return_no_memory_error (-1);
break;
case EVENT_MATCH:
if (! nih_str_array_add (&(*start_on)[len], *start_on,
NULL, oper->name))
nih_return_no_memory_error (-1);
if (oper->env)
if (! nih_str_array_append (&(*start_on)[len], *start_on,
NULL, oper->env))
nih_return_no_memory_error (-1);
break;
}
(*start_on)[++len] = NULL;
}
}
return 0;
}
/**
* job_class_get_stop_on:
* @class: class to obtain events from,
* @message: D-Bus connection and message received,
* @stop_on: pointer for reply array.
*
* Implements the get method for the stop_on property of the
* com.ubuntu.Upstart.Job interface.
*
* Called to obtain the set of events that will stop jobs of the given
* @class, this is returned as an array of the event tree flattened into
* reverse polish form.
*
* Each array element is an array of strings representing the events,
* or a single element containing "/OR" or "/AND" to represent the
* operators.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_get_stop_on (JobClass * class,
NihDBusMessage *message,
char **** stop_on)
{
size_t len = 0;
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (stop_on != NULL);
*stop_on = nih_alloc (message, sizeof (char ***));
if (! *stop_on)
nih_return_no_memory_error (-1);
len = 0;
(*stop_on)[len] = NULL;
if (class->stop_on) {
NIH_TREE_FOREACH_POST (&class->stop_on->node, iter) {
EventOperator *oper = (EventOperator *)iter;
*stop_on = nih_realloc (*stop_on, message,
sizeof (char ***) * (len + 2));
if (! *stop_on)
nih_return_no_memory_error (-1);
(*stop_on)[len] = nih_str_array_new (*stop_on);
if (! (*stop_on)[len])
nih_return_no_memory_error (-1);
switch (oper->type) {
case EVENT_OR:
if (! nih_str_array_add (&(*stop_on)[len], *stop_on,
NULL, "/OR"))
nih_return_no_memory_error (-1);
break;
case EVENT_AND:
if (! nih_str_array_add (&(*stop_on)[len], *stop_on,
NULL, "/AND"))
nih_return_no_memory_error (-1);
break;
case EVENT_MATCH:
if (! nih_str_array_add (&(*stop_on)[len], *stop_on,
NULL, oper->name))
nih_return_no_memory_error (-1);
if (oper->env)
if (! nih_str_array_append (&(*stop_on)[len], *stop_on,
NULL, oper->env))
nih_return_no_memory_error (-1);
break;
}
(*stop_on)[++len] = NULL;
}
}
return 0;
}
/**
* job_class_get_emits:
* @class: class to obtain events from,
* @message: D-Bus connection and message received,
* @emits: pointer for reply array.
*
* Implements the get method for the emits property of the
* com.ubuntu.Upstart.Job interface.
*
* Called to obtain the list of additional events of the given @class
* which will be stored as an array in @emits.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_get_emits (JobClass * class,
NihDBusMessage *message,
char *** emits)
{
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (emits != NULL);
if (class->emits) {
*emits = nih_str_array_copy (message, NULL, class->emits);
if (! *emits)
nih_return_no_memory_error (-1);
} else {
*emits = nih_str_array_new (message);
if (! *emits)
nih_return_no_memory_error (-1);
}
return 0;
}
/**
* job_class_console_type:
* @console: string representing console type.
*
* Returns: ConsoleType equivalent of @string, or -1 on invalid @console.
**/
ConsoleType
job_class_console_type (const char *console)
{
if (! strcmp (console, "none")) {
return CONSOLE_NONE;
} else if (! strcmp (console, "output")) {
return CONSOLE_OUTPUT;
} else if (! strcmp (console, "owner")) {
return CONSOLE_OWNER;
} else if (! strcmp (console, "log")) {
return CONSOLE_LOG;
}
return (ConsoleType)-1;
}
/**
* job_class_get_usage:
* @class: class to obtain usage from,
* @message: D-Bus connection and message received,
* @usage: pointer for reply string.
*
* Implements the get method for the usage property of the
* com.ubuntu.Upstart.Job interface.
*
* Called to obtain the usage of the given @class
* which will be stored as an string in @usage.
*
* Returns: zero on success, negative value on raised error.
**/
int
job_class_get_usage (JobClass * class,
NihDBusMessage *message,
char ** usage)
{
nih_assert (class != NULL);
nih_assert (message != NULL);
nih_assert (usage != NULL);
if (class->usage) {
*usage = nih_strdup (message, class->usage);
}
else {
*usage = nih_strdup (message, "");
}
if (! *usage) {
nih_return_no_memory_error (-1);
}
return 0;
}
/**
* job_class_serialise_job_environ:
*
* Serialise the global job environment table.
*
* Returns: JSON-serialised global job environment table, or NULL on error.
**/
json_object *
job_class_serialise_job_environ (void)
{
json_object *json;
job_class_environment_init ();
json = state_serialise_str_array (job_environ);
if (! json)
goto error;
return json;
error:
json_object_put (json);
return NULL;
}
/**
* job_class_deserialise_job_environ
* @json: JSON-serialised global job environment table to deserialise.
*
* Create the global job environment table from provided JSON.
*
* Returns: 0 on success, < 0 on error.
**/
int
job_class_deserialise_job_environ (json_object *json)
{
nih_assert (json);
nih_assert (! job_environ);
if (! state_check_json_type (json, array))
goto error;
if (! state_deserialise_str_array (NULL, json, &job_environ))
goto error;
return 0;
error:
return -1;
}
/**
* job_class_serialise:
* @class: job class to serialise.
*
* Convert @class into a JSON representation for serialisation.
* Caller must free returned value using json_object_put().
*
* Returns: JSON-serialised JobClass object, or NULL on error.
**/
json_object *
job_class_serialise (const JobClass *class)
{
json_object *json;
json_object *json_export;
json_object *json_emits;
json_object *json_processes;
json_object *json_normalexit;
json_object *json_limits;
json_object *json_jobs;
json_object *json_start_on;
json_object *json_stop_on;
int session_index;
nih_assert (class);
nih_assert (job_classes);
json = json_object_new_object ();
if (! json)
return NULL;
session_index = session_get_index (class->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, class, name))
goto error;
if (! state_set_json_string_var_from_obj (json, class, path))
goto error;
if (! state_set_json_string_var_from_obj (json, class, instance))
goto error;
json_jobs = job_serialise_all (class->instances);
if (! json_jobs)
goto error;
json_object_object_add (json, "jobs", json_jobs);
if (! state_set_json_string_var_from_obj (json, class, description))
goto error;
if (! state_set_json_string_var_from_obj (json, class, author))
goto error;
if (! state_set_json_string_var_from_obj (json, class, version))
goto error;
if (! state_set_json_str_array_from_obj (json, class, env))
goto error;
json_export = class->export
? state_serialise_str_array (class->export)
: json_object_new_array ();
if (! json_export)
goto error;
json_object_object_add (json, "export", json_export);
if (class->start_on) {
json_start_on = event_operator_serialise_all (class->start_on);
if (! json_start_on)
goto error;
json_object_object_add (json, "start_on", json_start_on);
}
if (class->stop_on) {
json_stop_on = event_operator_serialise_all (class->stop_on);
if (! json_stop_on)
goto error;
json_object_object_add (json, "stop_on", json_stop_on);
}
json_emits = class->emits
? state_serialise_str_array (class->emits)
: json_object_new_array ();
if (! json_emits)
goto error;
json_object_object_add (json, "emits", json_emits);
json_processes = process_serialise_all (
(const Process const * const * const)class->process);
if (! json_processes)
goto error;
json_object_object_add (json, "process", json_processes);
if (! state_set_json_enum_var (json,
job_class_expect_type_enum_to_str,
"expect", class->expect))
goto error;
if (! state_set_json_int_var_from_obj (json, class, task))
goto error;
if (! state_set_json_int_var_from_obj (json, class, kill_timeout))
goto error;
if (! state_set_json_int_var_from_obj (json, class, kill_signal))
goto error;
if (! state_set_json_int_var_from_obj (json, class, reload_signal))
goto error;
if (! state_set_json_int_var_from_obj (json, class, respawn))
goto error;
if (! state_set_json_int_var_from_obj (json, class, respawn_limit))
goto error;
if (! state_set_json_int_var_from_obj (json, class, respawn_interval))
goto error;
json_normalexit = state_serialise_int_array (int, class->normalexit,
class->normalexit_len);
if (! json_normalexit)
goto error;
json_object_object_add (json, "normalexit", json_normalexit);
if (! state_set_json_enum_var (json,
job_class_console_type_enum_to_str,
"console", class->console))
goto error;
if (! state_set_json_int_var_from_obj (json, class, umask))
goto error;
if (! state_set_json_int_var_from_obj (json, class, nice))
goto error;
if (! state_set_json_int_var_from_obj (json, class, oom_score_adj))
goto error;
json_limits = state_rlimit_serialise_all (class->limits);
if (! json_limits)
goto error;
json_object_object_add (json, "limits", json_limits);
if (! state_set_json_string_var_from_obj (json, class, chroot))
goto error;
if (! state_set_json_string_var_from_obj (json, class, chdir))
goto error;
if (! state_set_json_string_var_from_obj (json, class, setuid))
goto error;
if (! state_set_json_string_var_from_obj (json, class, setgid))
goto error;
if (! state_set_json_int_var_from_obj (json, class, deleted))
goto error;
if (! state_set_json_int_var_from_obj (json, class, debug))
goto error;
if (! state_set_json_string_var_from_obj (json, class, usage))
goto error;
if (! state_set_json_string_var_from_obj (json, class, apparmor_switch))
goto error;
return json;
error:
json_object_put (json);
return NULL;
}
/**
* job_class_serialise_all:
*
* Convert existing JobClass objects in job classes hash to JSON
* representation.
*
* NOTE: despite its name, this function does not _necessarily_
* serialise all JobClasses - there may be "best" (ie newer) JobClasses
* associated with ConfFiles that have not yet replaced the existing
* entries in the job classes hash if the JobClass has running instances.
*
* However, this is academic since although such data is not serialised,
* after the re-exec conf_reload() is called to recreate these "best"
* JobClasses. This also has the nice side-effect of ensuring that
* should jobs get created in the window when Upstart is statefully
* re-exec'ing, it will always see the newest versions of on-disk files
* (which is what the user expects).
*
* Returns: JSON object containing array of JobClass objects,
* or NULL on error.
**/
json_object *
job_class_serialise_all (void)
{
json_object *json;
job_class_init ();
json = json_object_new_array ();
if (! json)
return NULL;
NIH_HASH_FOREACH (job_classes, iter) {
json_object *json_class;
JobClass *class = (JobClass *)iter;
json_class = job_class_serialise (class);
if (! json_class)
goto error;
json_object_array_add (json, json_class);
}
return json;
error:
json_object_put (json);
return NULL;
}
/**
* job_class_deserialise:
* @json: JSON-serialised JobClass object to deserialise.
*
* Create JobClass from provided JSON and add to the
* job classes table.
*
* Returns: JobClass object, or NULL on error.
**/
JobClass *
job_class_deserialise (json_object *json)
{
json_object *json_normalexit;
JobClass *class = NULL;
ConfFile *file = NULL;
Session *session;
int session_index = -1;
int ret;
nih_local char *name = NULL;
nih_local char *path = NULL;
json_object *json_start_on = NULL;
json_object *json_stop_on = NULL;
nih_assert (json);
nih_assert (job_classes);
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);
/* XXX: chroot and old user session jobs not currently supported */
if (session) {
nih_info ("WARNING: deserialisation of user/chroot "
"sessions not currently supported");
goto error;
}
if (! state_get_json_string_var_strict (json, "name", NULL, name))
goto error;
/* Create the class and associate it with the ConfFile */
class = job_class_new (NULL, name, session);
if (! class)
goto error;
/* Lookup the ConfFile associated with this class.
*
* Don't error if this fails since previous serialisation data
* formats did not encode ConfSources and ConfFiles.
*/
file = conf_file_find (name, session);
if (file)
file->job = class;
/* job_class_new() sets path */
if (! state_get_json_string_var_strict (json, "path", NULL, path))
goto error;
nih_assert (! strcmp (class->path, path));
/* Discard default instance as we're about to be handed a fresh
* string from the JSON.
*/
nih_free (class->instance);
if (! state_get_json_string_var_to_obj (json, class, instance))
goto error;
if (! state_get_json_string_var_to_obj (json, class, description))
goto error;
if (! state_get_json_string_var_to_obj (json, class, author))
goto error;
if (! state_get_json_string_var_to_obj (json, class, version))
goto error;
if (! state_get_json_env_array_to_obj (json, class, env))
goto error;
if (! state_get_json_env_array_to_obj (json, class, export))
goto error;
/* start and stop conditions are optional */
if (json_object_object_get_ex (json, "start_on", &json_start_on)) {
if (state_check_json_type (json_start_on, array)) {
class->start_on = event_operator_deserialise_all (class, json_start_on);
if (! class->start_on)
goto error;
} else {
nih_local char *start_on = NULL;
/* Old format (string).
*
* Note that we re-search for the JSON key here
* (json, rather than json_start_on) to allow
* the use of the convenience macro. This is
* of course slower, but its a legacy scenario.
*/
if (! state_get_json_string_var_strict (json, "start_on", NULL, start_on))
goto error;
if (*start_on) {
class->start_on = parse_on_simple (class, "start", start_on);
if (! class->start_on) {
NihError *err;
err = nih_error_get ();
nih_error ("%s %s: %s",
_("BUG"),
_("'start on' parse error"),
err->message);
nih_free (err);
goto error;
}
}
}
}
if (json_object_object_get_ex (json, "stop_on", &json_stop_on)) {
if (state_check_json_type (json_stop_on, array)) {
class->stop_on = event_operator_deserialise_all (class, json_stop_on);
if (! class->stop_on)
goto error;
} else {
nih_local char *stop_on = NULL;
/* Old format (string) - re-search as above */
if (! state_get_json_string_var_strict (json, "stop_on", NULL, stop_on))
goto error;
if (*stop_on) {
class->stop_on = parse_on_simple (class, "stop", stop_on);
if (! class->stop_on) {
NihError *err;
err = nih_error_get ();
nih_error ("%s %s: %s",
_("BUG"),
_("'stop on' parse error"),
err->message);
nih_free (err);
goto error;
}
}
}
}
if (! state_get_json_str_array_to_obj (json, class, emits))
goto error;
if (! state_get_json_enum_var (json,
job_class_expect_type_str_to_enum,
"expect", class->expect))
goto error;
if (! state_get_json_int_var_to_obj (json, class, task))
goto error;
if (! state_get_json_int_var_to_obj (json, class, kill_timeout))
goto error;
if (! state_get_json_int_var_to_obj (json, class, kill_signal))
goto error;
/* reload_signal is new in upstart 1.10+ */
if (json_object_object_get (json, "reload_signal")) {
if (! state_get_json_int_var_to_obj (json, class, reload_signal))
goto error;
}
if (! state_get_json_int_var_to_obj (json, class, respawn))
goto error;
if (! state_get_json_int_var_to_obj (json, class, respawn_limit))
goto error;
if (! state_get_json_int_var_to_obj (json, class, respawn_interval))
goto error;
if (! state_get_json_enum_var (json,
job_class_console_type_str_to_enum,
"console", class->console))
goto error;
if (! state_get_json_int_var_to_obj (json, class, umask))
goto error;
if (! state_get_json_int_var_to_obj (json, class, nice))
goto error;
if (! state_get_json_int_var_to_obj (json, class, oom_score_adj))
goto error;
if (! state_get_json_string_var_to_obj (json, class, chroot))
goto error;
if (! state_get_json_string_var_to_obj (json, class, chdir))
goto error;
if (! state_get_json_string_var_to_obj (json, class, setuid))
goto error;
if (! state_get_json_string_var_to_obj (json, class, setgid))
goto error;
if (! state_get_json_int_var_to_obj (json, class, deleted))
goto error;
if (! state_get_json_int_var_to_obj (json, class, debug))
goto error;
if (! state_get_json_string_var_to_obj (json, class, usage))
goto error;
/* If we are missing this, we're probably importing from a
* previous version that didn't include PROCESS_SECURITY.
*/
if (json_object_object_get (json, "apparmor_switch")) {
if (! state_get_json_string_var_to_obj (json, class, apparmor_switch))
goto error;
}
json_normalexit = json_object_object_get (json, "normalexit");
if (! json_normalexit)
goto error;
ret = state_deserialise_int_array (class, json_normalexit,
int, &class->normalexit, &class->normalexit_len);
if (ret < 0)
goto error;
if (state_rlimit_deserialise_all (json, class, &class->limits) < 0)
goto error;
if (process_deserialise_all (json, class->process, class->process) < 0)
goto error;
if (file) {
/* Add the class to the job_classes hash if ConfFiles were
* available in the serialisation data.
*/
job_class_consider (class);
} else {
/* No ConfSources and ConfFiles were available in the
* serialisation data, so special-case the insertion.
*/
job_class_add_safe (class);
}
/* Any jobs must be added after the class is registered
* (since you cannot add a job to a partially-created
* class).
*/
if (job_deserialise_all (class, json) < 0)
goto error;
return class;
error:
if (class)
nih_free (class);
return NULL;
}
/**
* job_class_deserialise_all:
*
* @json: root of JSON-serialised state.
*
* Convert JSON representation of JobClasses back into JobClass objects.
*
* Returns: 0 on success, -1 on error.
**/
int
job_class_deserialise_all (json_object *json)
{
JobClass *class = NULL;
nih_assert (json);
job_class_init ();
json_classes = json_object_object_get (json, "job_classes");
if (! json_classes)
goto error;
if (! state_check_json_type (json_classes, array))
goto error;
for (int i = 0; i < json_object_array_length (json_classes); i++) {
json_object *json_class;
json_class = json_object_array_get_idx (json_classes, i);
if (! json_class)
goto error;
if (! state_check_json_type (json_class, object))
goto error;
class = job_class_deserialise (json_class);
/* Either memory is low or -- more likely -- a JobClass
* with a session was encountered, so keep going.
*/
if (! class) {
int session_index = -1;
if (state_get_json_int_var (json_class, "session", session_index)
&& session_index > 0) {
/* Although ConfSources are now serialised, ignore
* JobClasses with associated user/chroot sessions to avoid
* behavioural changes for the time being.
*/
continue;
} else {
goto error;
}
}
}
return 0;
error:
if (class)
nih_free (class);
return -1;
}
/**
* job_class_expect_type_enum_to_str:
*
* @expect: ExpectType.
*
* Convert ExpectType to a string representation.
*
* Returns: string representation of @expect, or NULL if not known.
**/
const char *
job_class_expect_type_enum_to_str (ExpectType expect)
{
state_enum_to_str (EXPECT_NONE, expect);
state_enum_to_str (EXPECT_STOP, expect);
state_enum_to_str (EXPECT_DAEMON, expect);
state_enum_to_str (EXPECT_FORK, expect);
return NULL;
}
/**
* job_class_expect_type_str_to_enum:
*
* @expect: string ExpectType value.
*
* Convert @expect back into an enum value.
*
* Returns: ExpectType representing @expect, or -1 if not known.
**/
ExpectType
job_class_expect_type_str_to_enum (const char *expect)
{
nih_assert (expect);
state_str_to_enum (EXPECT_NONE, expect);
state_str_to_enum (EXPECT_STOP, expect);
state_str_to_enum (EXPECT_DAEMON, expect);
state_str_to_enum (EXPECT_FORK, expect);
return -1;
}
/**
* job_class_console_type_enum_to_str:
*
* @console: ConsoleType.
*
* Convert ConsoleType to a string representation.
*
* Returns: string representation of @console, or NULL if not known.
**/
const char *
job_class_console_type_enum_to_str (ConsoleType console)
{
state_enum_to_str (CONSOLE_NONE, console);
state_enum_to_str (CONSOLE_OUTPUT, console);
state_enum_to_str (CONSOLE_OWNER, console);
state_enum_to_str (CONSOLE_LOG, console);
return NULL;
}
/**
* job_class_console_type_str_to_enum:
*
* @console: string ConsoleType value.
*
* Convert @console back into enum value.
*
* Returns: ExpectType representing @console, or -1 if not known.
**/
ConsoleType
job_class_console_type_str_to_enum (const char *console)
{
if (! console)
goto error;
state_str_to_enum (CONSOLE_NONE, console);
state_str_to_enum (CONSOLE_OUTPUT, console);
state_str_to_enum (CONSOLE_OWNER, console);
state_str_to_enum (CONSOLE_LOG, console);
error:
return -1;
}
/**
* job_class_prepare_reexec:
*
* Prepare for a re-exec by clearing the CLOEXEC bit on all log object
* file descriptors associated with their parent jobs.
**/
void
job_class_prepare_reexec (void)
{
job_class_init ();
NIH_HASH_FOREACH (job_classes, iter) {
JobClass *class = (JobClass *)iter;
NIH_HASH_FOREACH (class->instances, job_iter) {
Job *job = (Job *)job_iter;
nih_assert (job->log);
for (int process = 0; process < PROCESS_LAST; process++) {
int fd;
Log *log;
log = job->log[process];
/* No associated job process or logger has detected
* remote end of pty has closed.
*/
if (! log || ! log->io)
continue;
nih_assert (log->io->watch);
fd = log->io->watch->fd;
if (fd < 0)
continue;
if (state_toggle_cloexec (fd, FALSE) < 0)
goto error;
fd = log->fd;
if (fd < 0)
continue;
if (state_toggle_cloexec (fd, FALSE) < 0)
goto error;
}
}
}
return;
error:
nih_warn (_("unable to clear CLOEXEC bit on log fd"));
}
/**
* job_class_max_kill_timeout:
*
* Determine maximum kill timeout for all running jobs.
*
* Returns: Maximum kill timeout (seconds).
**/
time_t
job_class_max_kill_timeout (void)
{
time_t kill_timeout = JOB_DEFAULT_KILL_TIMEOUT;
job_class_init ();
NIH_HASH_FOREACH (job_classes, iter) {
JobClass *class = (JobClass *)iter;
NIH_HASH_FOREACH (class->instances, job_iter) {
Job *job = (Job *)job_iter;
if (job->class->kill_timeout > kill_timeout) {
kill_timeout = job->class->kill_timeout;
break;
}
}
}
return kill_timeout;
}
/**
* job_class_get_index:
* @class: JobClass to search for.
*
* Returns: index of @class in the job classes hash,
* or -1 if not found.
**/
ssize_t
job_class_get_index (const JobClass *class)
{
ssize_t i = 0;
nih_assert (class);
NIH_HASH_FOREACH (job_classes, iter) {
JobClass *c = (JobClass *)iter;
if (! strcmp (c->name, class->name)
&& c->session == class->session)
return i;
i++;
}
return -1;
}
|