~jamesodhunt/ubuntu/oneiric/upstart/fixes-for-user-sessions

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
/* upstart
 *
 * Copyright © 2010 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 <dbus/dbus.h>

#include <sys/types.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fnmatch.h>

#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/string.h>
#include <nih/main.h>
#include <nih/option.h>
#include <nih/command.h>
#include <nih/logging.h>
#include <nih/error.h>
#include <nih/hash.h>
#include <nih/tree.h>

#include <nih-dbus/dbus_error.h>
#include <nih-dbus/dbus_proxy.h>
#include <nih-dbus/errors.h>
#include <nih-dbus/dbus_connection.h>

#include "dbus/upstart.h"

#include "com.ubuntu.Upstart.h"
#include "com.ubuntu.Upstart.Job.h"
#include "com.ubuntu.Upstart.Instance.h"

#include "../init/events.h"
#include "initctl.h"


/* Prototypes for local functions */
NihDBusProxy *upstart_open (const void *parent)
	__attribute__ ((warn_unused_result, malloc));
char *        job_status   (const void *parent,
			    NihDBusProxy *job_class, NihDBusProxy *job)
	__attribute__ ((warn_unused_result, malloc));

/* Prototypes for static functions */
static void   start_reply_handler (char **job_path, NihDBusMessage *message,
				   const char *instance);
static void   reply_handler       (int *ret, NihDBusMessage *message);
static void   error_handler       (void *data, NihDBusMessage *message);

static void   job_class_condition_handler (void *data,
		NihDBusMessage *message,
		char ** const *value);

static void   job_class_condition_err_handler (void *data,
		NihDBusMessage *message);

static void   job_class_parse_events (const ConditionHandlerData *data,
		char ** const *variant_array);

static void   job_class_show_emits (const void *parent,
		NihDBusProxy *job_class_proxy, const char *job_class_name);

static void   job_class_show_conditions (NihDBusProxy *job_class_proxy,
		const char *job_class_name);

static void   eval_expr_tree (const char *expr, NihList **stack);

static int    check_condition (const char *job_class,
		const char *condition, NihList *condition_list,
		int *job_class_displayed)
	__attribute__ ((warn_unused_result));

static int    tree_filter (void *data, NihTree *node);

static void   display_check_errors (const char *job_class,
		const char *condition, NihTree *node);

static int    allow_job (const char *job);
static int    allow_event (const char *event);

#ifndef TEST

static int    dbus_bus_type_setter  (NihOption *option, const char *arg);
static int    ignored_events_setter (NihOption *option, const char *arg);

#endif

/* Prototypes for option and command functions */
int start_action                (NihCommand *command, char * const *args);
int stop_action                 (NihCommand *command, char * const *args);
int restart_action              (NihCommand *command, char * const *args);
int reload_action               (NihCommand *command, char * const *args);
int status_action               (NihCommand *command, char * const *args);
int list_action                 (NihCommand *command, char * const *args);
int emit_action                 (NihCommand *command, char * const *args);
int reload_configuration_action (NihCommand *command, char * const *args);
int version_action              (NihCommand *command, char * const *args);
int log_priority_action         (NihCommand *command, char * const *args);
int show_config_action          (NihCommand *command, char * const *args);
int check_config_action         (NihCommand *command, char * const *args);


/**
 * use_dbus:
 *
 * If  1, connect using a D-Bus bus.
 * If  0, connect using private connection.
 * If -1, determine appropriate connection based on UID.
 */
int use_dbus = -1;

/**
 * dbus_bus_type:
 *
 * D-Bus bus to connect to (DBUS_BUS_SYSTEM or DBUS_BUS_SESSION), or -1
 * to have an appropriate bus selected.
 */
int dbus_bus_type = -1;

/**
 * dest_name:
 *
 * Name on the D-Bus system bus that the message should be sent to when
 * system is TRUE.
 **/
char *dest_name = NULL;

/**
 * dest_address:
 *
 * Address for private D-Bus connection.
 **/
const char *dest_address = DBUS_ADDRESS_UPSTART;

/**
 * no_wait:
 *
 * Whether to wait for a job or event to be finished before existing or not.
 **/
int no_wait = FALSE;

/**
 * enumerate_events:
 *
 * If TRUE, list out all events/jobs that a particular job *may require* to
 * be run: essentially any event/job mentioned in a job configuration files
 * "start on" / "stop on" condition. Used for showing dependencies
 * between jobs and events.
 **/
int enumerate_events = FALSE;

/**
 * check_config_mode:
 *
 * If TRUE, parse all job configuration files looking for unreachable
 * jobs/events.
 **/
int check_config_mode = FALSE;

/**
 * check_config_warn:
 *
 * If TRUE, check-config will generate a warning for *any* unreachable
 * events/jobs.
 **/
int check_config_warn = FALSE;

/**
 * check_config_data:
 *
 * Used to record details of all known jobs and events.
 **/
CheckConfigData check_config_data;

/**
 * NihOption setter function to handle selection of appropriate D-Bus
 * bus.
 *
 * Always returns 1 denoting success.
 **/
int
dbus_bus_type_setter (NihOption *option, const char *arg)
{
	nih_assert (option);

	if (! strcmp (option->long_option, "system")) {
		use_dbus      = TRUE;
		dbus_bus_type = DBUS_BUS_SYSTEM;
	}
	else if (! strcmp (option->long_option, "session")) {
		use_dbus      = TRUE;
		dbus_bus_type = DBUS_BUS_SESSION;
	}

	return 1;
}


/**
 * NihOption setter function to handle specification of events to
 * ignore.
 *
 * Returns 1 on success, else 0.
 **/
int
ignored_events_setter (NihOption *option, const char *arg)
{
	NihError     *err;
	char        **events;
	char        **event;
	NihListEntry *entry;

	nih_assert (option);
	nih_assert (arg);

	if (! check_config_data.ignored_events_hash)
		check_config_data.ignored_events_hash = NIH_MUST (nih_hash_string_new (NULL, 0));

	events = nih_str_split (NULL, arg, ",", TRUE);

	if (!events) {
		goto error;
	}

	for (event = events; event && *event; ++event) {
		entry = NIH_MUST (nih_list_entry_new (check_config_data.ignored_events_hash));
		entry->str = NIH_MUST (nih_strdup (entry, *event));
		nih_hash_add (check_config_data.ignored_events_hash, &entry->entry);
	}

	nih_free (events);

	return 1;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);

	return 0;
}


/**
 * upstart_open:
 * @parent: parent object for new proxy.
 *
 * Opens a connection to the init daemon and returns a proxy to the manager
 * object.  If @dest_name is not NULL, a connection is instead opened to
 * the system bus and the proxy linked to the well-known name given.
 *
 * Error messages are output to standard error.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned proxy.  When all parents
 * of the returned proxy are freed, the returned proxy will also be
 * freed.
 *
 * Returns: newly allocated D-Bus proxy or NULL on error.
 **/
NihDBusProxy *
upstart_open (const void *parent)
{
	DBusError       dbus_error;
	DBusConnection *connection;
	NihDBusProxy *  upstart;

	if (use_dbus < 0)
		use_dbus = getuid () ? TRUE : FALSE;
	if (use_dbus >= 0 && dbus_bus_type < 0)
		dbus_bus_type = DBUS_BUS_SYSTEM;

	dbus_error_init (&dbus_error);
	if (use_dbus) {
		if (! dest_name)
			dest_name = DBUS_SERVICE_UPSTART;

		connection = dbus_bus_get (dbus_bus_type, &dbus_error);
		if (! connection) {
			nih_error ("%s: %s",
				dbus_bus_type == DBUS_BUS_SYSTEM
				? _("Unable to connect to system bus")
				: _("Unable to connect to session bus"),
				   dbus_error.message);
			dbus_error_free (&dbus_error);
			return NULL;
		}

		dbus_connection_set_exit_on_disconnect (connection, FALSE);
	} else {
		if (dest_name) {
			fprintf (stderr, _("%s: --dest given without --system\n"),
				 program_name);
			nih_main_suggest_help ();
			return NULL;
		}

		connection = dbus_connection_open (dest_address, &dbus_error);
		if (! connection) {
			nih_error ("%s: %s", _("Unable to connect to Upstart"),
				   dbus_error.message);
			dbus_error_free (&dbus_error);
			return NULL;
		}
	}
	dbus_error_free (&dbus_error);

	upstart = nih_dbus_proxy_new (parent, connection,
				      dest_name,
				      DBUS_PATH_UPSTART,
				      NULL, NULL);
	if (! upstart) {
		NihError *err;

		err = nih_error_get ();
		nih_error ("%s", err->message);
		nih_free (err);

		dbus_connection_unref (connection);
		return NULL;
	}

	upstart->auto_start = FALSE;

	/* Drop initial reference now the proxy holds one */
	dbus_connection_unref (connection);

	return upstart;
}


/**
 * job_status:
 * @parent: parent object for new string,
 * @job_class: proxy for remote job class object,
 * @job: proxy for remote instance object.
 *
 * Queries the job object @job and contructs a string defining the status
 * of that instance, containing the name of the @job_class and @job,
 * the goal, state and any running processes.
 *
 * @job may be NULL in which case a non-running job is assumed, the
 * function also catches the remote object no longer existing.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the returned string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL on raised error.
 **/
char *
job_status (const void *  parent,
	    NihDBusProxy *job_class,
	    NihDBusProxy *job)
{
	nih_local char *         job_class_name = NULL;
	nih_local JobProperties *props = NULL;
	char *                   str = NULL;

	nih_assert (job_class != NULL);

	/* Get the job name */
	if (job_class_get_name_sync (NULL, job_class, &job_class_name) < 0)
		return NULL;

	/* Get the instance properties, catching the instance going away
	 * between the time we got it and this call.
	 */
	if (job) {
		if (job_get_all_sync (NULL, job, &props) < 0) {
			NihDBusError *dbus_err;

			dbus_err = (NihDBusError *)nih_error_get ();
			if ((dbus_err->number != NIH_DBUS_ERROR)
			    || strcmp (dbus_err->name, DBUS_ERROR_UNKNOWN_METHOD)) {
				return NULL;
			} else {
				nih_free (dbus_err);
			}
		}
	}

	if (props && *props->name) {
		str = nih_sprintf (parent, "%s (%s)",
				   job_class_name, props->name);
		if (! str)
			nih_return_no_memory_error (NULL);
	} else {
		str = nih_strdup (parent, job_class_name);
		if (! str)
			nih_return_no_memory_error (NULL);
	}

	if (props) {
		if (! nih_strcat_sprintf (&str, parent, " %s/%s",
					  props->goal, props->state)) {
			nih_error_raise_no_memory ();
			nih_free (str);
			return NULL;
		}

		/* The first process returned is always the main process,
		 * which is the process we always want to display alongside
		 * the state if there is one.  Prefix if it's not one of
		 * the standard processes.
		 */
		if (props->processes[0]) {
			if (strcmp (props->processes[0]->item0, "main")
			    && strcmp (props->processes[0]->item0, "pre-start")
			    && strcmp (props->processes[0]->item0, "post-stop")) {
				if (! nih_strcat_sprintf (&str, parent, ", (%s) process %d",
							  props->processes[0]->item0,
							  props->processes[0]->item1)) {
					nih_error_raise_no_memory ();
					nih_free (str);
					return NULL;
				}
			} else {
				if (! nih_strcat_sprintf (&str, parent, ", process %d",
							  props->processes[0]->item1)) {
					nih_error_raise_no_memory ();
					nih_free (str);
					return NULL;
				}
			}

			/* Append a line for each additional process */
			for (JobProcessesElement **p = &props->processes[1];
			     p && *p; p++) {
				if (! nih_strcat_sprintf (&str, parent, "\n\t%s process %d",
							  (*p)->item0,
							  (*p)->item1)) {
					nih_error_raise_no_memory ();
					nih_free (str);
					return NULL;
				}
			}
		}
	} else {
		if (! nih_strcat (&str, parent, " stop/waiting")) {
			nih_error_raise_no_memory ();
			nih_free (str);
			return NULL;
		}
	}

	return str;
}


/**
 * start_action:
 * @command: NihCommand invoked,
 * @args: command-line arguments.
 *
 * This function is called for the "start" command.
 *
 * Returns: command exit status.
 **/
int
start_action (NihCommand *  command,
	      char * const *args)
{
	nih_local NihDBusProxy *upstart = NULL;
	const char *            upstart_job = NULL;
	const char *            upstart_instance = NULL;
	nih_local char *        job_class_path = NULL;
	nih_local NihDBusProxy *job_class = NULL;
	nih_local char *        job_path = NULL;
	nih_local NihDBusProxy *job = NULL;
	DBusPendingCall *       pending_call;
	int                     ret = 1;
	NihError *              err;

	nih_assert (command != NULL);
	nih_assert (args != NULL);

	if (args[0]) {
		upstart_job = args[0];
	} else {
		upstart_job = getenv ("UPSTART_JOB");
		upstart_instance = getenv ("UPSTART_INSTANCE");

		if (! (upstart_job && upstart_instance)) {
			fprintf (stderr, _("%s: missing job name\n"), program_name);
			nih_main_suggest_help ();
			return 1;
		}
	}

	upstart = upstart_open (NULL);
	if (! upstart)
		return 1;

	/* Obtain a proxy to the job */
	if (upstart_get_job_by_name_sync (NULL, upstart, upstart_job,
					  &job_class_path) < 0)
		goto error;

	job_class = nih_dbus_proxy_new (NULL, upstart->connection,
					upstart->name, job_class_path,
					NULL, NULL);
	if (! job_class)
		goto error;

	job_class->auto_start = FALSE;

	/* When called from a job handler, we directly change the goal
	 * so we need the instance.  These calls are always made without
	 * waiting, since otherwise we'd block the job we're called from.
	 */
	if (upstart_instance) {
		if (job_class_get_instance_by_name_sync (NULL, job_class,
							 upstart_instance,
							 &job_path) < 0)
			goto error;

		job = nih_dbus_proxy_new (NULL, upstart->connection,
					  upstart->name, job_path,
					  NULL, NULL);
		if (! job)
			goto error;

		job->auto_start = FALSE;

		pending_call = job_start (job, FALSE,
					  (JobStartReply)reply_handler,
					  error_handler, &ret,
					  NIH_DBUS_TIMEOUT_NEVER);
		if (! pending_call)
			goto error;
	} else {
		/* job_path is nih_local, so whatever gets filled in will
		 * be automatically freed.
		 */
		pending_call = job_class_start (job_class, &args[1], (! no_wait),
						(JobClassStartReply)start_reply_handler,
						error_handler, &job_path,
						NIH_DBUS_TIMEOUT_NEVER);
		if (! pending_call)
			goto error;
	}

	dbus_pending_call_block (pending_call);
	dbus_pending_call_unref (pending_call);

	/* Make sure we got a valid job path, and in the case of the instance
	 * path, no error message.  Then display the current job status.
	 */
	if (job_path && ((job == NULL) || (ret == 0))) {
		nih_local char *status = NULL;

		if (! job) {
			job = NIH_SHOULD (nih_dbus_proxy_new (
						  NULL, upstart->connection,
						  upstart->name, job_path,
						  NULL, NULL));
			if (! job)
				goto error;

			job->auto_start = FALSE;
		}

		status = NIH_SHOULD (job_status (NULL, job_class, job));
		if (! status)
			goto error;

		nih_message ("%s", status);

		ret = 0;
	} else {
		ret = 1;
	}

	return ret;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);

	return 1;
}


/**
 * stop_action:
 * @command: NihCommand invoked,
 * @args: command-line arguments.
 *
 * This function is called for the "stop" command.
 *
 * Returns: command exit status.
 **/
int
stop_action (NihCommand *  command,
	     char * const *args)
{
	nih_local NihDBusProxy *upstart = NULL;
	const char *            upstart_job = NULL;
	const char *            upstart_instance = NULL;
	nih_local char *        job_class_path = NULL;
	nih_local NihDBusProxy *job_class = NULL;
	nih_local char *        job_path = NULL;
	nih_local NihDBusProxy *job = NULL;
	DBusPendingCall *       pending_call;
	int                     ret = 1;
	NihError *              err;

	nih_assert (command != NULL);
	nih_assert (args != NULL);

	if (args[0]) {
		upstart_job = args[0];
	} else {
		upstart_job = getenv ("UPSTART_JOB");
		upstart_instance = getenv ("UPSTART_INSTANCE");

		if (! (upstart_job && upstart_instance)) {
			fprintf (stderr, _("%s: missing job name\n"), program_name);
			nih_main_suggest_help ();
			return 1;
		}
	}

	upstart = upstart_open (NULL);
	if (! upstart)
		return 1;

	/* Obtain a proxy to the job */
	if (upstart_get_job_by_name_sync (NULL, upstart, upstart_job,
					  &job_class_path) < 0)
		goto error;

	job_class = nih_dbus_proxy_new (NULL, upstart->connection,
					upstart->name, job_class_path,
					NULL, NULL);
	if (! job_class)
		goto error;

	job_class->auto_start = FALSE;

	/* When called from a job handler, we directly change the goal
	 * so need the instance.  These calls are always made without
	 * waiting, since otherwise we'd block the job we're called from.
	 */
	if (upstart_instance) {
		if (job_class_get_instance_by_name_sync (NULL, job_class,
							 upstart_instance,
							 &job_path) < 0)
			goto error;

		job = nih_dbus_proxy_new (NULL, upstart->connection,
					  upstart->name, job_path,
					  NULL, NULL);
		if (! job)
			goto error;

		job->auto_start = FALSE;

		pending_call = job_stop (job, FALSE,
					 (JobStopReply)reply_handler,
					 error_handler, &ret,
					 NIH_DBUS_TIMEOUT_NEVER);
		if (! pending_call)
			goto error;
	} else {
		/* Lookup the instance that we'll end up stopping */
		if (job_class_get_instance_sync (NULL, job_class, &args[1],
						 &job_path) < 0)
			goto error;

		job = nih_dbus_proxy_new (NULL, upstart->connection,
					  upstart->name, job_path,
					  NULL, NULL);
		if (! job)
			goto error;

		job->auto_start = FALSE;

		pending_call = job_class_stop (job_class, &args[1], (! no_wait),
					       (JobClassStopReply)reply_handler,
					       error_handler, &ret,
					       NIH_DBUS_TIMEOUT_NEVER);
		if (! pending_call)
			goto error;
	}

	dbus_pending_call_block (pending_call);
	dbus_pending_call_unref (pending_call);

	/* Display the current job status */
	if (ret == 0) {
		nih_local char *status = NULL;

		status = NIH_SHOULD (job_status (NULL, job_class, job));
		if (! status)
			goto error;

		nih_message ("%s", status);
	}

	return ret;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);

	return 1;
}


/**
 * restart_action:
 * @command: NihCommand invoked,
 * @args: command-line arguments.
 *
 * This function is called for the "restart" command.
 *
 * Returns: command exit status.
 **/
int
restart_action (NihCommand *  command,
		char * const *args)
{
	nih_local NihDBusProxy *upstart = NULL;
	const char *            upstart_job = NULL;
	const char *            upstart_instance = NULL;
	nih_local char *        job_class_path = NULL;
	nih_local NihDBusProxy *job_class = NULL;
	nih_local char *        job_path = NULL;
	nih_local NihDBusProxy *job = NULL;
	DBusPendingCall *       pending_call;
	int                     ret = 1;
	NihError *              err;

	nih_assert (command != NULL);
	nih_assert (args != NULL);

	if (args[0]) {
		upstart_job = args[0];
	} else {
		upstart_job = getenv ("UPSTART_JOB");
		upstart_instance = getenv ("UPSTART_INSTANCE");

		if (! (upstart_job && upstart_instance)) {
			fprintf (stderr, _("%s: missing job name\n"), program_name);
			nih_main_suggest_help ();
			return 1;
		}
	}

	upstart = upstart_open (NULL);
	if (! upstart)
		return 1;

	/* Obtain a proxy to the job */
	if (upstart_get_job_by_name_sync (NULL, upstart, upstart_job,
					  &job_class_path) < 0)
		goto error;

	job_class = nih_dbus_proxy_new (NULL, upstart->connection,
					upstart->name, job_class_path,
					NULL, NULL);
	if (! job_class)
		goto error;

	job_class->auto_start = FALSE;

	/* When called from a job handler, we directly toggle the goal
	 * so we need the instance.  These calls are always made without
	 * waiting, since otherwise we'd block the job we're called from.
	 */
	if (upstart_instance) {
		if (job_class_get_instance_by_name_sync (NULL, job_class,
							 upstart_instance,
							 &job_path) < 0)
			goto error;

		job = nih_dbus_proxy_new (NULL, upstart->connection,
					  upstart->name, job_path,
					  NULL, NULL);
		if (! job)
			goto error;

		job->auto_start = FALSE;

		pending_call = job_restart (job, FALSE,
					    (JobRestartReply)reply_handler,
					    error_handler, &ret,
					    NIH_DBUS_TIMEOUT_NEVER);
		if (! pending_call)
			goto error;
	} else {
		/* job_path is nih_local, so whatever gets filled in will
		 * be automatically freed.
		 */
		pending_call = job_class_restart (job_class, &args[1], (! no_wait),
						  (JobClassRestartReply)start_reply_handler,
						  error_handler, &job_path,
						  NIH_DBUS_TIMEOUT_NEVER);

		if (! pending_call)
			goto error;
	}

	dbus_pending_call_block (pending_call);
	dbus_pending_call_unref (pending_call);

	/* Make sure we got a valid job path, and in the case of the instance
	 * path, no error message.  Then display the current job status.
	 */
	if (job_path && ((job == NULL) || (ret == 0))) {
		nih_local char *status = NULL;

		if (! job) {
			job = NIH_SHOULD (nih_dbus_proxy_new (
						  NULL, upstart->connection,
						  upstart->name, job_path,
						  NULL, NULL));
			if (! job)
				goto error;

			job->auto_start = FALSE;
		}

		status = NIH_SHOULD (job_status (NULL, job_class, job));
		if (! status)
			goto error;

		nih_message ("%s", status);

		ret = 0;
	} else {
		ret = 1;
	}

	return ret;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);

	return 1;
}


/**
 * reload_action:
 * @command: NihCommand invoked,
 * @args: command-line arguments.
 *
 * This function is called for the "reload" command.
 *
 * Returns: command exit status.
 **/
int
reload_action (NihCommand *  command,
	       char * const *args)
{
	nih_local NihDBusProxy *upstart = NULL;
	const char *            upstart_job = NULL;
	const char *            upstart_instance = NULL;
	nih_local char *        job_class_path = NULL;
	nih_local NihDBusProxy *job_class = NULL;
	nih_local char *        job_path = NULL;
	nih_local NihDBusProxy *job = NULL;
	nih_local JobProcessesElement **processes = NULL;
	NihError *              err;

	nih_assert (command != NULL);
	nih_assert (args != NULL);

	if (args[0]) {
		upstart_job = args[0];
	} else {
		upstart_job = getenv ("UPSTART_JOB");
		upstart_instance = getenv ("UPSTART_INSTANCE");

		if (! (upstart_job && upstart_instance)) {
			fprintf (stderr, _("%s: missing job name\n"), program_name);
			nih_main_suggest_help ();
			return 1;
		}
	}

	upstart = upstart_open (NULL);
	if (! upstart)
		return 1;

	/* Obtain a proxy to the job */
	if (upstart_get_job_by_name_sync (NULL, upstart, upstart_job,
					  &job_class_path) < 0)
		goto error;

	job_class = nih_dbus_proxy_new (NULL, upstart->connection,
					upstart->name, job_class_path,
					NULL, NULL);
	if (! job_class)
		goto error;

	job_class->auto_start = FALSE;

	/* Obtain a proxy to the specific instance.  Catch the case where
	 * we were just given a job name, and there was no single instance
	 * running.
	 */
	if (upstart_instance) {
		if (job_class_get_instance_by_name_sync (NULL, job_class,
							 upstart_instance,
							 &job_path) < 0)
			goto error;
	} else {
		if (job_class_get_instance_sync (NULL, job_class,
						 &args[1], &job_path) < 0)
			goto error;
	}

	job = nih_dbus_proxy_new (NULL, upstart->connection,
				  upstart->name, job_path,
				  NULL, NULL);
	if (! job)
		goto error;

	job->auto_start = FALSE;

	/* Get the process list */
	if (job_get_processes_sync (NULL, job, &processes) < 0)
		goto error;

	if ((! processes[0]) || strcmp (processes[0]->item0, "main")) {
		nih_error (_("Not running"));
		return 1;
	}

	if (kill (processes[0]->item1, SIGHUP) < 0) {
		nih_error_raise_system ();
		goto error;
	}

	return 0;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);

	return 1;
}


/**
 * status_action:
 * @command: NihCommand invoked,
 * @args: command-line arguments.
 *
 * This function is called for the "status" command.
 *
 * Returns: command exit status.
 **/
int
status_action (NihCommand *  command,
	       char * const *args)
{
	nih_local NihDBusProxy *upstart = NULL;
	const char *            upstart_job = NULL;
	const char *            upstart_instance = NULL;
	nih_local char *        job_class_path = NULL;
	nih_local NihDBusProxy *job_class = NULL;
	nih_local char *        job_path = NULL;
	nih_local NihDBusProxy *job = NULL;
	nih_local char *        status = NULL;
	NihError *              err;
	NihDBusError *          dbus_err;

	nih_assert (command != NULL);
	nih_assert (args != NULL);

	if (args[0]) {
		upstart_job = args[0];
	} else {
		upstart_job = getenv ("UPSTART_JOB");
		upstart_instance = getenv ("UPSTART_INSTANCE");

		if (! (upstart_job && upstart_instance)) {
			fprintf (stderr, _("%s: missing job name\n"), program_name);
			nih_main_suggest_help ();
			return 1;
		}
	}

	upstart = upstart_open (NULL);
	if (! upstart)
		return 1;

	/* Obtain a proxy to the job */
	if (upstart_get_job_by_name_sync (NULL, upstart, upstart_job,
					  &job_class_path) < 0)
		goto error;

	job_class = nih_dbus_proxy_new (NULL, upstart->connection,
					upstart->name, job_class_path,
					NULL, NULL);
	if (! job_class)
		goto error;

	job_class->auto_start = FALSE;

	/* Obtain a proxy to the specific instance.  Catch the case where
	 * we were just given a job name, and there was no single instance
	 * running.
	 */
	if (upstart_instance) {
		if (job_class_get_instance_by_name_sync (NULL, job_class,
							 upstart_instance,
							 &job_path) < 0)
			goto error;
	} else {
		if (job_class_get_instance_sync (NULL, job_class,
						 &args[1], &job_path) < 0) {
			dbus_err = (NihDBusError *)nih_error_get ();
			if (args[1]
			    || (dbus_err->number != NIH_DBUS_ERROR)
			    || strcmp (dbus_err->name, DBUS_INTERFACE_UPSTART ".Error.UnknownInstance"))
				goto error;

			nih_free (dbus_err);
		}
	}

	if (job_path) {
		job = nih_dbus_proxy_new (NULL, upstart->connection,
					  upstart->name, job_path,
					  NULL, NULL);
		if (! job)
			goto error;

		job->auto_start = FALSE;
	} else {
		job = NULL;
	}

	status = job_status (NULL, job_class, job);
	if (! status)
		goto error;

	nih_message ("%s", status);

	return 0;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);

	return 1;
}


/**
 * list_action:
 * @command: NihCommand invoked,
 * @args: command-line arguments.
 *
 * This function is called for the "list" command.
 *
 * Returns: command exit status.
 **/
int
list_action (NihCommand *  command,
	     char * const *args)
{
	nih_local NihDBusProxy *upstart = NULL;
	nih_local char **       job_class_paths = NULL;
	NihError *              err;
	NihDBusError *          dbus_err;

	nih_assert (command != NULL);
	nih_assert (args != NULL);

	upstart = upstart_open (NULL);
	if (! upstart)
		return 1;

	/* Obtain a list of jobs */
	if (upstart_get_all_jobs_sync (NULL, upstart, &job_class_paths) < 0)
		goto error;

	for (char **job_class_path = job_class_paths;
	     job_class_path && *job_class_path; job_class_path++) {
		nih_local NihDBusProxy *job_class = NULL;
		nih_local char **       job_paths = NULL;

		job_class = nih_dbus_proxy_new (NULL, upstart->connection,
						upstart->name, *job_class_path,
						NULL, NULL);
		if (! job_class)
			goto error;

		job_class->auto_start = FALSE;

		/* Obtain a list of instances, catch an error from the
		 * command and assume the job just went away.
		 */
		if (job_class_get_all_instances_sync (NULL, job_class,
						      &job_paths) < 0) {
			dbus_err = (NihDBusError *)nih_error_get ();
			if ((dbus_err->number != NIH_DBUS_ERROR)
			    || strcmp (dbus_err->name, DBUS_ERROR_UNKNOWN_METHOD))
				goto error;

			nih_free (dbus_err);
			continue;
		}

		/* Handle no running instances by assuming that the
		 * job is just stopped, again catch an error and assume
		 * the job went away.
		 */
		if (! *job_paths) {
			nih_local char *status = NULL;

			status = job_status (NULL, job_class, NULL);
			if (! status) {
				dbus_err = (NihDBusError *)nih_error_get ();
				if ((dbus_err->number != NIH_DBUS_ERROR)
				    || strcmp (dbus_err->name, DBUS_ERROR_UNKNOWN_METHOD))
					goto error;

				nih_free (dbus_err);
				continue;
			}

			nih_message ("%s", status);
		}

		/* Otherwise iterate the instances and output the status
		 * of each.
		 */
		for (char **job_path = job_paths; job_path && *job_path;
		     job_path++) {
			nih_local NihDBusProxy *job = NULL;
			nih_local char *        status = NULL;

			job = nih_dbus_proxy_new (NULL, upstart->connection,
						  upstart->name, *job_path,
						  NULL, NULL);
			if (! job)
				goto error;

			job->auto_start = FALSE;

			status = job_status (NULL, job_class, job);
			if (! status) {
				dbus_err = (NihDBusError *)nih_error_get ();
				if ((dbus_err->number != NIH_DBUS_ERROR)
				    || strcmp (dbus_err->name, DBUS_ERROR_UNKNOWN_METHOD))
					goto error;

				nih_free (dbus_err);
				continue;
			}

			nih_message ("%s", status);
		}
	}

	return 0;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);

	return 1;
}

/**
 * show_config_action:
 * @command: NihCommand invoked,
 * @args: command-line arguments.
 *
 * This function is called for the "show-config" command.
 *
 * Returns: command exit status.
 **/
int
show_config_action (NihCommand *  command,
	     char * const *args)
{
	nih_local NihDBusProxy  *upstart = NULL;
	nih_local char         **job_class_paths = NULL;
	const char              *upstart_job_class = NULL;
	NihError                *err;

	nih_assert (command != NULL);
	nih_assert (args != NULL);

	upstart = upstart_open (NULL);
	if (! upstart)
		return 1;

	if (args[0]) {
		/* Single job specified */
		upstart_job_class = args[0];
		job_class_paths = NIH_MUST (nih_alloc (NULL, 2*sizeof (char *)));
		job_class_paths[1] = NULL;

		if (upstart_get_job_by_name_sync (NULL, upstart, upstart_job_class,
					job_class_paths) < 0)
			goto error;
	} else {
		/* Obtain a list of jobs */
		if (upstart_get_all_jobs_sync (NULL, upstart, &job_class_paths) < 0)
			goto error;
	}

	for (char **job_class_path = job_class_paths;
	     job_class_path && *job_class_path; job_class_path++) {
		nih_local NihDBusProxy *job_class      = NULL;
		nih_local char         *job_class_name = NULL;

		job_class = nih_dbus_proxy_new (NULL, upstart->connection,
						upstart->name, *job_class_path,
						NULL, NULL);
		if (! job_class)
			goto error;

		job_class->auto_start = FALSE;

		if (job_class_get_name_sync (NULL, job_class, &job_class_name) < 0)
			goto error;

		if (! check_config_mode)
			nih_message ("%s", job_class_name);

		job_class_show_emits (NULL, job_class, job_class_name);
		job_class_show_conditions (job_class, job_class_name);

		/* Add any jobs *without* "start on"/"stop on" conditions
		 * to ensure we have a complete list of jobs for check-config to work with.
		 */
		if (check_config_mode) {
			JobCondition *entry;

			entry = (JobCondition *)nih_hash_lookup (check_config_data.job_class_hash,
					job_class_name);
			if (!entry) {
				MAKE_JOB_CONDITION (check_config_data.job_class_hash,
						entry, job_class_name);
				nih_hash_add (check_config_data.job_class_hash, &entry->list);
			}
		}
	}

	return 0;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);

	return 1;
}

/**
 * emit_action:
 * @command: NihCommand invoked,
 * @args: command-line arguments.
 *
 * This function is called for the "emit" command.
 *
 * Returns: command exit status.
 **/
int
emit_action (NihCommand *  command,
	     char * const *args)
{
	nih_local NihDBusProxy *upstart = NULL;
	DBusPendingCall *       pending_call;
	int                     ret = 1;
	NihError *              err;

	nih_assert (command != NULL);
	nih_assert (args != NULL);

	if (! args[0]) {
		fprintf (stderr, _("%s: missing event name\n"), program_name);
		nih_main_suggest_help ();
		return 1;
	}

	upstart = upstart_open (NULL);
	if (! upstart)
		return 1;

	pending_call = upstart_emit_event (upstart, args[0], &args[1], (! no_wait),
					   (UpstartEmitEventReply)reply_handler,
					   error_handler, &ret,
					   NIH_DBUS_TIMEOUT_NEVER);
	if (! pending_call)
		goto error;

	dbus_pending_call_block (pending_call);
	dbus_pending_call_unref (pending_call);

	return ret;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);

	return 1;
}


/**
 * reload_configuration_action:
 * @command: NihCommand invoked,
 * @args: command-line arguments.
 *
 * This function is called for the "reload-configuration" command.
 *
 * Returns: command exit status.
 **/
int
reload_configuration_action (NihCommand *  command,
			     char * const *args)
{
	nih_local NihDBusProxy *upstart = NULL;
	NihError *              err;

	nih_assert (command != NULL);
	nih_assert (args != NULL);

	upstart = upstart_open (NULL);
	if (! upstart)
		return 1;

	if (upstart_reload_configuration_sync (NULL, upstart) < 0)
		goto error;

	return 0;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);

	return 1;
}


/**
 * version_action:
 * @command: NihCommand invoked,
 * @args: command-line arguments.
 *
 * This function is called for the "version" command.
 *
 * Returns: command exit status.
 **/
int
version_action (NihCommand *  command,
		char * const *args)
{
	nih_local NihDBusProxy *upstart = NULL;
	nih_local char *        version = NULL;
	NihError *              err;

	nih_assert (command != NULL);
	nih_assert (args != NULL);

	upstart = upstart_open (NULL);
	if (! upstart)
		return 1;

	if (upstart_get_version_sync (NULL, upstart, &version) < 0)
		goto error;

	nih_message ("%s", version);

	return 0;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);

	return 1;
}


/**
 * log_priority_action:
 * @command: NihCommand invoked,
 * @args: command-line arguments.
 *
 * This function is called for the "log-priority" command.
 *
 * Returns: command exit status.
 **/
int
log_priority_action (NihCommand *  command,
		     char * const *args)
{
	nih_local NihDBusProxy *upstart = NULL;
	NihError *              err;

	nih_assert (command != NULL);
	nih_assert (args != NULL);

	upstart = upstart_open (NULL);
	if (! upstart)
		return 1;

	if (args[0]) {
		if (upstart_set_log_priority_sync (NULL, upstart, args[0]) < 0)
			goto error;
	} else {
		nih_local char *log_priority = NULL;

		if (upstart_get_log_priority_sync (NULL, upstart,
						   &log_priority) < 0)
			goto error;

		nih_message ("%s", log_priority);
	}

	return 0;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);

	return 1;
}


/**
 * check_config_action:
 * @command: NihCommand invoked,
 * @args: command-line arguments.
 *
 * This function is called for the "check-config" command.
 *
 * Returns: command exit status.
 **/
int
check_config_action (NihCommand *command,
		char * const *args)
{
	int              ret;
	char            *no_args[1] = { NULL };
	char            *job_class  = NULL;

	check_config_data.job_class_hash = NIH_MUST (nih_hash_string_new (NULL, 0));
	check_config_data.event_hash     = NIH_MUST (nih_hash_string_new (NULL, 0));

	if (! check_config_data.ignored_events_hash)
		check_config_data.ignored_events_hash =
			NIH_MUST (nih_hash_string_new (NULL, 0));

	/* Tell other functions we are running in a special mode */
	check_config_mode = TRUE;

	/* Obtain emits, start on and stop on data.
	 *
	 * Note: we pass null args since we always want details of all jobs.
	 */
	ret = show_config_action (command, no_args);

	if (ret)
		return 0;

	if (args[0]) {
		NihList *entry;
		job_class = args[0];

		entry = nih_hash_lookup (check_config_data.job_class_hash, job_class);

		if (! entry) {
			nih_error ("%s: %s", _("Invalid job class"), job_class);
			return 1;
		}
	}

	NIH_HASH_FOREACH (check_config_data.job_class_hash, iter) {
		JobCondition *j = (JobCondition *)iter;
		int job_class_displayed = FALSE;

		if (job_class && strcmp (job_class, j->job_class) != 0) {
			/* user specified a job on the command-line,
			 * so only show that one.
			 */
			continue;
		}

		ret += check_condition (j->job_class, "start on", j->start_on,
				&job_class_displayed);

		ret += check_condition (j->job_class, "stop on" , j->stop_on,
				&job_class_displayed);
	}

	nih_free (check_config_data.job_class_hash);
	nih_free (check_config_data.event_hash);
	if (check_config_data.ignored_events_hash)
		nih_free (check_config_data.ignored_events_hash);

	return ret ? 1 : 0;
}

static void
start_reply_handler (char **         job_path,
		     NihDBusMessage *message,
		     const char *    instance)
{
	nih_assert (message != NULL);
	nih_assert (instance != NULL);

	/* Return the job path */
	*job_path = NIH_MUST (nih_strdup (NULL, instance));
}

static void
reply_handler (int *           ret,
	       NihDBusMessage *message)
{
	nih_assert (message != NULL);

	*ret = 0;
}

static void
error_handler (void *          data,
	       NihDBusMessage *message)
{
	NihError *err;

	nih_assert (message != NULL);

	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);
}

/**
 * job_class_parse_events:
 * @condition_data: type of condition we are parsing (used as an indicator to
 * user) and name of job,
 * @variant_array: pointer to array of variants.
 *
 * The array of variants encodes the event operator tree in 
 * Reverse Polish Notation (RPN).
 *
 * Each variant is itself an array. There are two types:
 *
 * - An "Operator" (array length == 1).
 *
 *   Operators are: "/AND" and "/OR".
 *
 * - An "Event" (array length >= 1).
 *
 *   Each Event comprises a name (array element zero), followed by zero or
 *   more "Event Matches". If the first Event Match is of the form "JOB=name",
 *   or is a single token "name" (crucially not containining "="), then
 *   'name' refers to the job which emitted the event.
 **/
void
job_class_parse_events (const ConditionHandlerData *data, char ** const *variant_array)
{
	char          ** const *variant;
	char                  **arg;
	char                   *token;
	nih_local NihList      *rpn_stack = NULL;
	char                   *name = NULL;
	const char             *stanza_name;
	const char             *job_class_name;

	nih_assert (data);

	stanza_name    = ((ConditionHandlerData *)data)->condition_name;
	job_class_name = ((ConditionHandlerData *)data)->job_class_name;

	if (! variant_array || ! *variant_array || ! **variant_array)
		return;

	STACK_CREATE (rpn_stack);
	STACK_SHOW (rpn_stack);

	for (variant = variant_array; variant && *variant && **variant; variant++, name = NULL) {

		/* token is either the first token beyond the stanza name (ie the event name),
		 * or an operator.
		 */
		token = **variant;

		if (IS_OPERATOR (token)) {
			/* Used to hold result of combining top two stack elements. */
			nih_local char *new_token = NULL;

			nih_local NihList *first  = NULL;
			nih_local NihList *second = NULL;

			if (enumerate_events) {
				/* We only care about operands in this mode. */
				continue;
			}

			if (check_config_mode) {
				/* Save token verbatim. */
				new_token = NIH_MUST (nih_strdup (NULL, token));
				STACK_PUSH_NEW_ELEM (rpn_stack, new_token);
				continue;
			}

			first  = NIH_MUST (nih_list_new (NULL));
			second = NIH_MUST (nih_list_new (NULL));

			/* Found an operator, so pop 2 values off stack,
			 * combine them and push back onto stack.
			 */
			STACK_POP (rpn_stack, first);
			STACK_POP (rpn_stack, second);

			new_token = NIH_MUST (nih_strdup (NULL, ""));
			new_token = NIH_MUST (nih_strcat_sprintf (&new_token,
					NULL,
					"(%s %s %s)",
					((NihListEntry *)second)->str,
					IS_OP_AND (token) ? "and" : "or",
					((NihListEntry *)first)->str));

			STACK_PUSH_NEW_ELEM (rpn_stack, new_token);
		} else {
			/* Save operand token (event or job), add
			 * arguments (job names and env vars) and push
			 * onto stack. If we are enumerating_events,
			 * this records the environment only.
			 */
			nih_local char *element = NULL;
			int i;

			element = NIH_MUST (nih_strdup (NULL,
						enumerate_events ? "" : token));

			/* Handle arguments (job names and env vars). */
			arg = (*variant)+1;

			for (i=0; arg[i] && *arg[i]; i++) {
				if ((enumerate_events || check_config_mode) && IS_JOB_EVENT (token)) {
					if (!name) {
						GET_JOB_NAME (name, i, arg[i]);
						if (name)
							continue;
					}
				}

				if (! check_config_mode) {
					element = NIH_MUST (nih_strcat (&element, NULL, " "));
					element = NIH_MUST (nih_strcat (&element, NULL, arg[i]));
				}
			}

			if (enumerate_events) {
				nih_message ("  %s %s (job:%s%s, env:%s)",
						stanza_name,
						token,
						name ? " " : "",
						name ? name : "",
						element);
			} else {
				if (check_config_mode) {
					element = NIH_MUST (nih_sprintf (NULL, "%s%s%s",
								token,
								name ? " " : "",
								name ? name : ""));
				}
				STACK_PUSH_NEW_ELEM (rpn_stack, element);
			}

		}
	}

	if (enumerate_events)
		return;

	if (check_config_mode) {
		int add = 0;
		JobCondition *entry;

		/* Create job class entry if necessary */
		entry = (JobCondition *)nih_hash_lookup (check_config_data.job_class_hash, job_class_name);

		if (!entry) {
			add = 1;
			MAKE_JOB_CONDITION (check_config_data.job_class_hash, entry, job_class_name);
		}

		/* Unstitch the conditions from the stack and stash them
		 * in the appropriate list for the job in question.
		 */
		NIH_LIST_FOREACH_SAFE (rpn_stack, iter) {
			NihListEntry *node = (NihListEntry *)iter; 
			NihList *l = !strcmp (stanza_name, "start on")
				? entry->start_on
				: entry->stop_on;
			nih_ref (node, l);
			nih_unref (node, rpn_stack);
			nih_list_add_after (l, &node->entry);
		}

		if (add)
			nih_hash_add (check_config_data.job_class_hash, &entry->list);

		return;
	}

	/* Handle case where a single event was specified (there
	 * was no operator to pop the entry off the stack).
	 */
	if (! STACK_EMPTY (rpn_stack)) {
		if (! enumerate_events) {
			/* Our job is done: show the user what we found. */
			nih_message ("  %s %s", stanza_name,
					STACK_PEEK (rpn_stack));
		}
	}
}

/**
 * job_class_show_conditions:
 * @job_class_proxy: D-Bus proxy for job class.
 * @job_class_name: Name of config whose conditions we wish to display.
 *
 * Register D-Bus call-backs to display job classes start on and stop on
 * conditions.
 **/
void
job_class_show_conditions (NihDBusProxy *job_class_proxy, const char *job_class_name)
{
	DBusPendingCall  *pending_call;
	NihError         *err;
	ConditionHandlerData start_data, stop_data;

	nih_assert (job_class_proxy);
	nih_assert (job_class_name);
	
	start_data.condition_name = "start on";
	start_data.job_class_name = job_class_name;

	stop_data.condition_name  = "stop on";
	stop_data.job_class_name  = job_class_name;

	pending_call = job_class_get_start_on (job_class_proxy,
			job_class_condition_handler,
			job_class_condition_err_handler, 
			&start_data,
			NIH_DBUS_TIMEOUT_NEVER);

	if (!pending_call)
		goto error;

	/* wait for completion */
	dbus_pending_call_block (pending_call);
	dbus_pending_call_unref (pending_call);

	pending_call = job_class_get_stop_on (job_class_proxy,
			job_class_condition_handler,
			job_class_condition_err_handler, 
			&stop_data,
			NIH_DBUS_TIMEOUT_NEVER);

	if (!pending_call)
		goto error;

	/* wait for completion */
	dbus_pending_call_block (pending_call);
	dbus_pending_call_unref (pending_call);

	return;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);
}

/**
 * job_class_show_emits:
 * @parent: parent object,
 * @job_class_proxy: D-Bus proxy for job class,
 * @job_class_name: Name of job class that emits an event.
 *
 * Display events job class emits to user.
 **/
void
job_class_show_emits (const void *parent, NihDBusProxy *job_class_proxy, const char *job_class_name)
{
	NihError         *err;
	nih_local char **job_emits = NULL;

	nih_assert (job_class_proxy);

	if (job_class_get_emits_sync (parent, job_class_proxy, &job_emits) < 0) {
		goto error;
	}

	if (job_emits && *job_emits) {
		char **p = job_emits;
		while (*p) {
			if (check_config_mode) {
				/* Record event for later */
				NihListEntry *node = NIH_MUST (nih_list_entry_new (check_config_data.event_hash));
				node->str = NIH_MUST (nih_strdup (node, *p));
				nih_hash_add_unique (check_config_data.event_hash, &node->entry);
			}
			else {
				nih_message ("  emits %s", *p);
			}
			p++;
		}
	}

	return;

error:
	err = nih_error_get ();
	nih_error ("%s", err->message);
	nih_free (err);
}

/**
 * job_class_condition_handler:
 * @data: data passed via job_class_get_start_on() or job_class_get_stop_on(),
 * @message: D-Bus message received,
 * @value: array of variants generated by D-Bus call we are registering
 * with. 
 *
 * Handler for D-Bus message encoding a job classes "start on" or
 * "stop on" condtions.
 **/
void
job_class_condition_handler (void *data, NihDBusMessage *message, char ** const *value)
{
	job_class_parse_events ((const ConditionHandlerData *)data, value);
}

/**
 * job_class_condition_err_handler:
 *
 * @data data passed via job_class_get_start_on() or job_class_get_stop_on(),
 * @message: D-Bus message received.
 *
 * Error handler for D-Bus message encoding a job classes "start on" or
 * "stop on" conditions.
 **/
void
job_class_condition_err_handler (void *data, NihDBusMessage *message)
{
	/* no remedial action possible */
}


/**
 * eval_expr_tree:
 *
 * @expr: expression to consider (see ExprNode),
 * @stack: stack used to hold evaluated expressions.
 *
 * Evaluate @expr, in the context of @stack, creating ExprNode
 * nodes as necessary.
 *
 * See ExprNode for details of @token.
 **/
void
eval_expr_tree (const char *expr, NihList **stack)
{
	NihList *s;

	nih_assert (stack);

	s = *stack;

	if (IS_OPERATOR (expr)) {
		ExprNode *node, *first, *second;
		NihListEntry *tmp = NULL;
		NihListEntry *le;

		/* make node for operator */
		MAKE_EXPR_NODE (NULL, node, expr);

		/* pop */
		nih_assert (! NIH_LIST_EMPTY (s));
		tmp = (NihListEntry *)nih_list_remove (s->next);

		/* re-parent */
		nih_ref (tmp, node);
		nih_unref (tmp, s);

		first = (ExprNode *)tmp->data;
		nih_assert (first->value != -1);

		/* attach to operator node */
		nih_tree_add (&node->node, &first->node, NIH_TREE_LEFT);


		/* pop */
		nih_assert (! NIH_LIST_EMPTY (s));
		tmp = (NihListEntry *)nih_list_remove (s->next);

		/* re-parent */
		nih_ref (tmp, node);
		nih_unref (tmp, s);

		second = (ExprNode *)tmp->data;
		nih_assert (second->value != -1);

		/* attach to operator node */
		nih_tree_add (&node->node, &second->node, NIH_TREE_RIGHT);


		/* Determine truth value for
		 * this node based on children
		 * and type of operator.
		 */
		if (IS_OP_AND (expr) || check_config_warn)
			node->value = first->value && second->value;
		else
			node->value = first->value || second->value;

		/* Create list entry and hook
		 * node onto it.
		 */
		le = NIH_MUST (nih_new (s, NihListEntry));
		nih_list_init (&le->entry);
		le->data = node;

		/* push operator node */
		nih_list_add_after (s, &le->entry);

	} else {
		char *event = NULL;
		char *job   = NULL;
		NihListEntry *le;
		ExprNode *en;
		int errors = 0;

		le = NIH_MUST (nih_new (s, NihListEntry));
		nih_list_init (&le->entry);

		MAKE_EXPR_NODE (le, en, expr);
		le->data = en;

		event = en->expr;

		/* Determine the type of operand node we
		 * have.
		 */
		job = strchr (en->expr, ' ');

		/* found a job */
		if (job) {
			job++;
			*(job-1) = '\0';

			if (! allow_job (job)) {
				errors++;

				/* remember the error for later */
				en->job_in_error = job;
			}
		}

		/* handle event */
		if (! allow_event (event)) {
			errors++;
			/* remember the error for later */
			en->event_in_error = en->expr;
		}

		/* Determine if this this node is in error (0 means yes) */
		en->value = errors ? 0 : 1;

		nih_list_add_after (s, &le->entry);
	}
}


/**
 * check_condition:
 *
 * @job_class: name of job class,
 * @condition: name of condition,
 * @condition_list: conditions to check.
 * @job_class_displayed: Will be set to TRUE when an error node is found
 * and the job class has been displayed to the user.
 *
 * Evaluate all expression tree nodes in @condition_list looking for
 * errors.
 *
 * Returns error count.
 **/
int
check_condition (const char *job_class, const char *condition, NihList *condition_list,
		int *job_class_displayed)
{
	nih_local NihList *stack  = NULL;
	NihTree           *root   = NULL;
	int                errors = 0;

	nih_assert (job_class);
	nih_assert (condition);

	if (! condition_list || NIH_LIST_EMPTY (condition_list))
		return 0;

	STACK_CREATE (stack);
	STACK_SHOW (stack);

	NIH_LIST_FOREACH (condition_list, iter) {
		NihListEntry *e = (NihListEntry *)iter; 

		eval_expr_tree (e->str, &stack);
	}

	root = &(((ExprNode *)((NihListEntry *)stack->next)->data)->node);

	if (! root)
		/* no conditions found */
		return 0;

	/* Look through the expression tree for nodes in error and
	 * display them.
	 */
	NIH_TREE_FOREACH_PRE_FULL (root, iter, tree_filter, root) {
		ExprNode *e = (ExprNode *)iter;

		if ( e->value != 1 ) {
			errors++;
			if ( *job_class_displayed == FALSE) {
				nih_message ("%s", job_class);
				*job_class_displayed = TRUE;
			}

			display_check_errors (job_class, condition, iter);
			break;
		}
	}
	return errors;
}

/**
 * display_check_errors:
 *
 * @job_class: name of job class,
 * @condition: name of condition,
 * @node: tree node that is in error.
 *
 * Display error details from expression tree nodes
 * that are in error.
 *
 * Note that this function should only be passed operand nodes or the
 * root node.
 **/
void
display_check_errors (const char *job_class, const char *condition, NihTree *node)
{
	nih_assert (job_class);
	nih_assert (node);

	NIH_TREE_FOREACH_POST (node, iter) {
		ExprNode   *expr  = (ExprNode *)iter;
		const char *event = expr->event_in_error;
		const char *job   = expr->job_in_error;

		if (event)
			nih_message ("  %s: %s %s", condition,
				_("unknown event"), event);

		if (job)
			nih_message ("  %s: %s %s", condition,
				_("unknown job"), job);
	}
}


/**
 * tree_filter:
 *
 * @data: node representing root of tree,
 * @node: node to consider.
 *
 * Node filter for NIH_TREE_FOREACH_PRE_FULL that ensures only 
 * operator nodes and the root node are to be considered.
 *
 * Returns FALSE if node should be considered, else TRUE.
 *
 **/
int
tree_filter (void *data, NihTree *node)
{
	NihTree  *root = (NihTree *)data;
	ExprNode *e    = (ExprNode *)node;

	nih_assert (root);
	nih_assert (e);

	if (IS_OPERATOR (e->expr) || node == root)
		return FALSE;

	/* ignore */
	return TRUE;
}


/**
 * allow_job:
 *
 * @job: name of job to check.
 *
 * Returns TRUE if @job is recognized or can be ignored,
 * else FALSE.
 **/
int
allow_job (const char *job)
{
	NihList *found;

	nih_assert (job);

	found = nih_hash_lookup (check_config_data.job_class_hash, job);

	/* The second part of this test ensures we ignore the (unusual)
	 * situation whereby the condition references a
	 * variable (for example an instance).
	 */
	if (!found && job[0] != '$')
		return FALSE;

	return TRUE;
}


/**
 * allow_event:
 *
 * @event: name of event to check.
 *
 * Returns TRUE if @event is recognized or can be ignored,
 * else FALSE.
 **/
int
allow_event (const char *event)
{
	nih_assert (event);

	NIH_HASH_FOREACH (check_config_data.event_hash, iter) {
		NihListEntry *entry = (NihListEntry *)iter;

		/* handles expansion of any globs */
		if (fnmatch (entry->str, event, 0) == 0)
			goto out;
	}

	if (IS_INIT_EVENT (event) ||
		nih_hash_lookup (check_config_data.ignored_events_hash, event))
		goto out;

	return FALSE;

out:
	return TRUE;
}


#ifndef TEST
/**
 * options:
 *
 * Command-line options accepted for all arguments.
 **/
static NihOption options[] = {
	{ 0, "session", N_("use D-Bus session bus to connect to init daemon (for testing)"),
	  NULL, NULL, NULL, dbus_bus_type_setter },
	{ 0, "system", N_("use D-Bus system bus to connect to init daemon"),
	  NULL, NULL, NULL, dbus_bus_type_setter },
	{ 0, "dest", N_("destination well-known name on D-Bus bus"),
	  NULL, "NAME", &dest_name, NULL },

	NIH_OPTION_LAST
};


/**
 * start_options:
 *
 * Command-line options accepted for the start command.
 **/
NihOption start_options[] = {
	{ 'n', "no-wait", N_("do not wait for job to start before exiting"),
	  NULL, NULL, &no_wait, NULL },

	NIH_OPTION_LAST
};

/**
 * stop_options:
 *
 * Command-line options accepted for the stop command.
 **/
NihOption stop_options[] = {
	{ 'n', "no-wait", N_("do not wait for job to stop before exiting"),
	  NULL, NULL, &no_wait, NULL },

	NIH_OPTION_LAST
};

/**
 * restart_options:
 *
 * Command-line options accepted for the restart command.
 **/
NihOption restart_options[] = {
	{ 'n', "no-wait", N_("do not wait for job to restart before exiting"),
	  NULL, NULL, &no_wait, NULL },

	NIH_OPTION_LAST
};

/**
 * reload_options:
 *
 * Command-line options accepted for the reload command.
 **/
NihOption reload_options[] = {
	NIH_OPTION_LAST
};

/**
 * status_options:
 *
 * Command-line options accepted for the status command.
 **/
NihOption status_options[] = {
	NIH_OPTION_LAST
};

/**
 * list_options:
 *
 * Command-line options accepted for the list command.
 **/
NihOption list_options[] = {
	NIH_OPTION_LAST
};

/**
 * emit_options:
 *
 * Command-line options accepted for the emit command.
 **/
NihOption emit_options[] = {
	{ 'n', "no-wait", N_("do not wait for event to finish before exiting"),
	  NULL, NULL, &no_wait, NULL },

	NIH_OPTION_LAST
};

/**
 * reload_configuration_options:
 *
 * Command-line options accepted for the reload-configuration command.
 **/
NihOption reload_configuration_options[] = {
	NIH_OPTION_LAST
};

/**
 * version_options:
 *
 * Command-line options accepted for the version command.
 **/
NihOption version_options[] = {
	NIH_OPTION_LAST
};

/**
 * log_priority_options:
 *
 * Command-line options accepted for the log-priority command.
 **/
NihOption log_priority_options[] = {
	NIH_OPTION_LAST
};


/**
 * show_config_options:
 *
 * Command-line options accepted for the show-config command.
 **/
NihOption show_config_options[] = {
	{ 'e', "enumerate",
		N_("enumerate list of events and jobs causing job "
		   "created from job config to start/stop"),
	  NULL, NULL, &enumerate_events, NULL },

	NIH_OPTION_LAST
};

/**
 * check_config_options:
 *
 * Command-line options accepted for the check-config command.
 **/
NihOption check_config_options[] = {
	{ 'i', "ignore-events", N_("ignore specified list of events (comma-separated)"),
	  NULL, "EVENT_LIST", NULL, ignored_events_setter },
	{ 'w', "warn", N_("Generate warning for any unreachable events/jobs"),
	  NULL, NULL, &check_config_warn, NULL },
	NIH_OPTION_LAST
};
/**
 * job_group:
 *
 * Group of commands related to jobs.
 **/
static NihCommandGroup job_commands = { N_("Job") };

/**
 * event_group:
 *
 * Group of commands related to events.
 **/
static NihCommandGroup event_commands = { N_("Event") };

/**
 * commands:
 *
 * Commands accepts as the first non-option argument, or program name.
 **/
static NihCommand commands[] = {
	{ "start", N_("JOB [KEY=VALUE]..."),
	  N_("Start job."),
	  N_("JOB is the name of the job that is to be started, this may "
	     "be followed by zero or more environment variables to be "
	     "defined in the new job.\n"
	     "\n"
	     "The environment may also serve to distinguish between job "
	     "instances, and thus decide whether a new instance will be "
	     "started or an error returned if an existing instance is "
	     "already running."),
	  &job_commands, start_options, start_action },

	{ "stop", N_("JOB [KEY=VALUE]..."),
	  N_("Stop job."),
	  N_("JOB is the name of the job that is to be stopped, this may "
	     "be followed by zero or more environment variables to be "
	     "passed to the job's pre-stop and post-stop processes.\n"
	     "\n"
	     "The environment also serves to distinguish between job "
	     "instances, and thus decide which of multiple instances will "
	     "be stopped."),
	  &job_commands, stop_options, stop_action },

	{ "restart", N_("JOB [KEY=VALUE]..."),
	  N_("Restart job."),
	  N_("JOB is the name of the job that is to be restarted, this may "
	     "be followed by zero or more environment variables to be "
	     "defined in the job after restarting.\n"
	     "\n"
	     "The environment also serves to distinguish between job "
	     "instances, and thus decide which of multiple instances will "
	     "be restarted."),
	  &job_commands, restart_options, restart_action },

	{ "reload", N_("JOB [KEY=VALUE]..."),
	  N_("Send HUP signal to job."),
	  N_("JOB is the name of the job that is to be sent the signal, "
	     "this may be followed by zero or more environment variables "
	     "to distinguish between job instances.\n"),
	  &job_commands, reload_options, reload_action },

	{ "status", N_("JOB [KEY=VALUE]..."),
	  N_("Query status of job."),
	  N_("JOB is the name of the job that is to be queried, this may "
	     "be followed by zero or more environment variables to "
	     "distguish between job instances.\n" ),
	  &job_commands, status_options, status_action },

	{ "list", NULL,
	  N_("List known jobs."),
	  N_("The known jobs and their current status will be output."),
	  &job_commands, list_options, list_action },

	{ "emit", N_("EVENT [KEY=VALUE]..."),
	  N_("Emit an event."),
	  N_("EVENT is the name of an event the init daemon should emit, "
	     "this may be followed by zero or more environment variables "
	     "to be included in the event.\n"),
	  &event_commands, emit_options, emit_action },

	{ "reload-configuration", NULL,
	  N_("Reload the configuration of the init daemon."),
	  NULL,
	  NULL, reload_configuration_options, reload_configuration_action },
	{ "version", NULL,
	  N_("Request the version of the init daemon."),
	  NULL,
	  NULL, version_options, version_action },
	{ "log-priority", N_("[PRIORITY]"),
	  N_("Change the minimum priority of log messages from the init "
	     "daemon"),
	  N_("PRIORITY may be one of:\n"
	     "  `debug' (messages useful for debugging upstart are logged, "
	     "equivalent to --debug on kernel command-line);\n"
	     "  `info' (messages about job goal and state changes, as well "
	     "as event emissions are logged, equivalent to --verbose on the "
	     "kernel command-line);\n"
	     "  `message' (informational and debugging messages are suppressed, "
	     "the default); "
	     "  `warn' (ordinary messages are suppressed whilst still "
	     "logging warnings and errors);\n"
	     "  `error' (only errors are logged, equivalent to --quiet on "
	     "the kernel command-line) or\n"
	     "  `fatal' (only fatal errors are logged).\n"
	     "\n"
	     "Without arguments, this outputs the current log priority."),
	  NULL, log_priority_options, log_priority_action },

	{ "show-config", N_("[CONF]"),
	  N_("Show emits, start on and stop on details for job configurations."),
	  N_("If CONF specified, show configuration details for single job "
	     "configuration, else show details for all jobs configurations.\n"),
	  NULL, show_config_options, show_config_action },

	{ "check-config", N_("[CONF]"),
	  N_("Check for unreachable jobs/event conditions."),
	  N_("List all jobs and events which cannot be satisfied by "
	     "currently available job configuration files"),
	  NULL, check_config_options, check_config_action },


	NIH_COMMAND_LAST
};






int
main (int   argc,
      char *argv[])
{
	int ret;

	nih_main_init (argv[0]);

	ret = nih_command_parser (NULL, argc, argv, options, commands);
	if (ret < 0)
		exit (1);

	dbus_shutdown ();

	return ret;
}
#endif