~singpolyma/asterisk

ref: 51af79b42e4ae7d1831603633d73bf0df179720e asterisk/main/loader.c -rw-r--r-- 64.6 KiB
51af79b4Christopher Vollick Content Created By Initiator 2: Electric Boogaloo 10 months ago
                                                                                
130ba7ae Bernhard Schmidt
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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 1999 - 2006, Digium, Inc.
 *
 * Mark Spencer <markster@digium.com>
 * Kevin P. Fleming <kpfleming@digium.com>
 * Luigi Rizzo <rizzo@icir.org>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Module Loader
 * \author Mark Spencer <markster@digium.com>
 * \author Kevin P. Fleming <kpfleming@digium.com>
 * \author Luigi Rizzo <rizzo@icir.org>
 * - See ModMngMnt
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include "asterisk/_private.h"
#include "asterisk/paths.h"	/* use ast_config_AST_MODULE_DIR */
#include <dirent.h>

#include "asterisk/dlinkedlists.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/channel.h"
#include "asterisk/term.h"
#include "asterisk/manager.h"
#include "asterisk/io.h"
#include "asterisk/lock.h"
#include "asterisk/vector.h"
#include "asterisk/app.h"
#include "asterisk/test.h"
#include "asterisk/cli.h"

#include <dlfcn.h>

#include "asterisk/md5.h"
#include "asterisk/utils.h"

/*** DOCUMENTATION
	<managerEvent language="en_US" name="Reload">
		<managerEventInstance class="EVENT_FLAG_SYSTEM">
			<synopsis>Raised when a module has been reloaded in Asterisk.</synopsis>
			<syntax>
				<parameter name="Module">
					<para>The name of the module that was reloaded, or
					<literal>All</literal> if all modules were reloaded</para>
				</parameter>
				<parameter name="Status">
					<para>The numeric status code denoting the success or failure
					of the reload request.</para>
					<enumlist>
						<enum name="0"><para>Success</para></enum>
						<enum name="1"><para>Request queued</para></enum>
						<enum name="2"><para>Module not found</para></enum>
						<enum name="3"><para>Error</para></enum>
						<enum name="4"><para>Reload already in progress</para></enum>
						<enum name="5"><para>Module uninitialized</para></enum>
						<enum name="6"><para>Reload not supported</para></enum>
					</enumlist>
				</parameter>
			</syntax>
		</managerEventInstance>
	</managerEvent>
	<managerEvent language="en_US" name="Load">
		<managerEventInstance class="EVENT_FLAG_SYSTEM">
			<synopsis>Raised when a module has been loaded in Asterisk.</synopsis>
			<syntax>
				<parameter name="Module">
					<para>The name of the module that was loaded</para>
				</parameter>
				<parameter name="Status">
					<para>The result of the load request.</para>
					<enumlist>
						<enum name="Failure"><para>Module could not be loaded properly</para></enum>
						<enum name="Success"><para>Module loaded and configured</para></enum>
						<enum name="Decline"><para>Module is not configured</para></enum>
					</enumlist>
				</parameter>
			</syntax>
		</managerEventInstance>
	</managerEvent>
	<managerEvent language="en_US" name="Unload">
		<managerEventInstance class="EVENT_FLAG_SYSTEM">
			<synopsis>Raised when a module has been unloaded in Asterisk.</synopsis>
			<syntax>
				<parameter name="Module">
					<para>The name of the module that was unloaded</para>
				</parameter>
				<parameter name="Status">
					<para>The result of the unload request.</para>
					<enumlist>
						<enum name="Success"><para>Module unloaded successfully</para></enum>
					</enumlist>
				</parameter>
			</syntax>
		</managerEventInstance>
	</managerEvent>
 ***/

#ifndef RTLD_NOW
#define RTLD_NOW 0
#endif

#ifndef RTLD_LOCAL
#define RTLD_LOCAL 0
#endif

struct ast_module_user {
	struct ast_channel *chan;
	AST_LIST_ENTRY(ast_module_user) entry;
};

AST_DLLIST_HEAD(module_user_list, ast_module_user);

static const unsigned char expected_key[] =
{ 0x87, 0x76, 0x79, 0x35, 0x23, 0xea, 0x3a, 0xd3,
  0x25, 0x2a, 0xbb, 0x35, 0x87, 0xe4, 0x22, 0x24 };

static char buildopt_sum[33] = AST_BUILDOPT_SUM;

AST_VECTOR(module_vector, struct ast_module *);

/*! Used with AST_VECTOR_CALLBACK_VOID to create a
 * comma separated list of module names for error messages. */
#define STR_APPEND_TEXT(txt, str) \
	ast_str_append(str, 0, "%s%s", \
		ast_str_strlen(*(str)) > 0 ? ", " : "", \
		txt)

/* Built-in module registrations need special handling at startup */
static unsigned int loader_ready;

/*! String container for deferring output of startup errors. */
static struct ast_vector_string startup_errors;
static struct ast_str *startup_error_builder;

static __attribute__((format(printf, 1, 2))) void module_load_error(const char *fmt, ...)
{
	char *copy = NULL;
	va_list ap;

	va_start(ap, fmt);
	if (startup_error_builder) {
		ast_str_set_va(&startup_error_builder, 0, fmt, ap);
		copy = ast_strdup(ast_str_buffer(startup_error_builder));
		if (!copy || AST_VECTOR_APPEND(&startup_errors, copy)) {
			ast_log(LOG_ERROR, "%s", ast_str_buffer(startup_error_builder));
			ast_free(copy);
		}
	} else {
		ast_log_ap(LOG_ERROR, fmt, ap);
	}
	va_end(ap);
}

/*!
 * \brief Internal flag to indicate all modules have been initially loaded.
 */
static int modules_loaded;

struct ast_module {
	const struct ast_module_info *info;
	/*! Used to get module references into refs log */
	void *ref_debug;
	/*! The shared lib. */
	void *lib;
	/*! Number of 'users' and other references currently holding the module. */
	int usecount;
	/*! List of users holding the module. */
	struct module_user_list users;

	/*! List of required module names. */
	struct ast_vector_string requires;
	/*! List of optional api modules. */
	struct ast_vector_string optional_modules;
	/*! List of modules this enhances. */
	struct ast_vector_string enhances;

	/*!
	 * \brief Vector holding pointers to modules we have a reference to.
	 *
	 * When one module requires another, the required module gets added
	 * to this list with a reference.
	 */
	struct module_vector reffed_deps;
	struct {
		/*! The module running and ready to accept requests. */
		unsigned int running:1;
		/*! The module has declined to start. */
		unsigned int declined:1;
		/*! This module is being held open until it's time to shutdown. */
		unsigned int keepuntilshutdown:1;
		/*! The module is built-in. */
		unsigned int builtin:1;
		/*! The admin has declared this module is required. */
		unsigned int required:1;
		/*! This module is marked for preload. */
		unsigned int preload:1;
	} flags;
	AST_DLLIST_ENTRY(ast_module) entry;
	char resource[0];
};

static AST_DLLIST_HEAD_STATIC(module_list, ast_module);


struct load_results_map {
	int result;
	const char *name;
};

static const struct load_results_map load_results[] = {
	{ AST_MODULE_LOAD_SUCCESS, "Success" },
	{ AST_MODULE_LOAD_DECLINE, "Decline" },
	{ AST_MODULE_LOAD_SKIP, "Skip" },
	{ AST_MODULE_LOAD_PRIORITY, "Priority" },
	{ AST_MODULE_LOAD_FAILURE, "Failure" },
};
#define AST_MODULE_LOAD_UNKNOWN_STRING		"Unknown"		/* Status string for unknown load status */

static void publish_load_message_type(const char* type, const char *name, const char *status);
static void publish_reload_message(const char *name, enum ast_module_reload_result result);
static void publish_load_message(const char *name, enum ast_module_load_result result);
static void publish_unload_message(const char *name, const char* status);


/*
 * module_list is cleared by its constructor possibly after
 * we start accumulating built-in modules, so we need to
 * use another list (without the lock) to accumulate them.
 */
static struct module_list builtin_module_list;

static int module_vector_strcasecmp(struct ast_module *a, struct ast_module *b)
{
	return strcasecmp(a->resource, b->resource);
}

static int module_vector_cmp(struct ast_module *a, struct ast_module *b)
{
	int preload_diff = (int)b->flags.preload - (int)a->flags.preload;
	/* if load_pri is not set, default is 128.  Lower is better */
	int a_pri = ast_test_flag(a->info, AST_MODFLAG_LOAD_ORDER)
		? a->info->load_pri : AST_MODPRI_DEFAULT;
	int b_pri = ast_test_flag(b->info, AST_MODFLAG_LOAD_ORDER)
		? b->info->load_pri : AST_MODPRI_DEFAULT;

	if (preload_diff) {
		/* -1 preload a but not b */
		/*  0 preload both or neither */
		/*  1 preload b but not a */
		return preload_diff;
	}

	/*
	 * Returns comparison values for a vector sorted by priority.
	 * <0 a_pri < b_pri
	 * =0 a_pri == b_pri
	 * >0 a_pri > b_pri
	 */
	return a_pri - b_pri;
}

static struct ast_module *find_resource(const char *resource, int do_lock);

/*!
 * \internal
 * \brief Add a reference from mod to dep.
 *
 * \param mod Owner of the new reference.
 * \param dep Module to reference
 * \param missing Vector to store name of \a dep if it is not running.
 *
 * This function returns failure if \a dep is not running and \a missing
 * is NULL.  If \a missing is not NULL errors will only be returned for
 * allocation failures.
 *
 * \retval 0 Success
 * \retval -1 Failure
 *
 * \note Adding a second reference to the same dep will return success
 *       without doing anything.
 */
static int module_reffed_deps_add(struct ast_module *mod, struct ast_module *dep,
	struct ast_vector_const_string *missing)
{
	if (!dep->flags.running) {
		return !missing ? -1 : AST_VECTOR_APPEND(missing, dep->info->name);
	}

	if (AST_VECTOR_GET_CMP(&mod->reffed_deps, dep, AST_VECTOR_ELEM_DEFAULT_CMP)) {
		/* Skip duplicate. */
		return 0;
	}

	if (AST_VECTOR_APPEND(&mod->reffed_deps, dep)) {
		return -1;
	}

	ast_module_ref(dep);

	return 0;
}

/*!
 * \internal
 * \brief Add references for modules that enhance a dependency.
 *
 * \param mod Owner of the new references.
 * \param dep Module to check for enhancers.
 * \param missing Vector to store name of any enhancer that is not running or declined.
 *
 * \retval 0 Success
 * \retval -1 Failure
 */
static int module_reffed_deps_add_dep_enhancers(struct ast_module *mod,
	struct ast_module *dep, struct ast_vector_const_string *missing)
{
	struct ast_module *cur;

	AST_DLLIST_TRAVERSE(&module_list, cur, entry) {
		if (cur->flags.declined) {
			continue;
		}

		if (!AST_VECTOR_GET_CMP(&cur->enhances, dep->info->name, !strcasecmp)) {
			/* dep is not enhanced by cur. */
			continue;
		}

		/* dep is enhanced by cur, therefore mod requires cur. */
		if (module_reffed_deps_add(mod, cur, missing)) {
			return -1;
		}
	}

	return 0;
}

/*!
 * \internal
 * \brief Add references to a list of dependencies.
 *
 * \param mod Owner of the new references.
 * \param vec List of required modules to process
 * \param missing Vector to store names of modules that are not running.
 * \param ref_enhancers Reference all enhancers of each required module.
 * \param isoptional Modules that are not loaded can be ignored.
 *
 * \retval 0 Success
 * \retval -1 Failure
 */
static int module_deps_process_reqlist(struct ast_module *mod,
	struct ast_vector_string *vec, struct ast_vector_const_string *missing,
	int ref_enhancers, int isoptional)
{
	int idx;

	for (idx = 0; idx < AST_VECTOR_SIZE(vec); idx++) {
		const char *depname = AST_VECTOR_GET(vec, idx);
		struct ast_module *dep = find_resource(depname, 0);

		if (!dep || !dep->flags.running) {
			if (isoptional && !dep) {
				continue;
			}

			if (missing && !AST_VECTOR_APPEND(missing, depname)) {
				continue;
			}

			return -1;
		}

		if (module_reffed_deps_add(mod, dep, missing)) {
			return -1;
		}

		if (ref_enhancers && module_reffed_deps_add_dep_enhancers(mod, dep, missing)) {
			return -1;
		}
	}

	return 0;
}

/*!
 * \internal
 * \brief Grab all references required to start the module.
 *
 * \param mod The module we're trying to start.
 * \param missing Vector to store a list of missing dependencies.
 *
 * \retval 0 Success
 * \retval -1 Failure
 *
 * \note module_list must be locked.
 *
 * \note Caller is responsible for initializing and freeing the vector.
 *       Elements are safely read only while module_list remains locked.
 */
static int module_deps_reference(struct ast_module *mod, struct ast_vector_const_string *missing)
{
	int res = 0;

	/* Grab references to modules we enhance but not other enhancements. */
	res |= module_deps_process_reqlist(mod, &mod->enhances, missing, 0, 0);

	/* Grab references to modules we require plus enhancements. */
	res |= module_deps_process_reqlist(mod, &mod->requires, missing, 1, 0);

	/* Grab references to optional modules including enhancements. */
	res |= module_deps_process_reqlist(mod, &mod->optional_modules, missing, 1, 1);

	return res;
}

/*!
 * \brief Recursively find required dependencies that are not running.
 *
 * \param mod Module to scan for dependencies.
 * \param missingdeps Vector listing modules that must be started first.
 *
 * \retval 0 All dependencies resolved.
 * \retval -1 Failed to resolve some dependencies.
 *
 * An error from this function usually means a required module is not even
 * loaded.  This function is safe from infinite recursion, but dependency
 * loops are not reported as an error from here.  On success missingdeps
 * will contain a list of every module that needs to be running before this
 * module can start.  missingdeps is sorted by load priority so any missing
 * dependencies can be started if needed.
 */
static int module_deps_missing_recursive(struct ast_module *mod, struct module_vector *missingdeps)
{
	int i = 0;
	int res = -1;
	struct ast_vector_const_string localdeps;
	struct ast_module *dep;

	/*
	 * localdeps stores a copy of all dependencies that mod could not reference.
	 * First we discard modules that we've already found. We add all newly found
	 * modules to the missingdeps vector then scan them recursively.  This will
	 * ensure we quickly run out of stuff to do.
	 */
	AST_VECTOR_INIT(&localdeps, 0);
	if (module_deps_reference(mod, &localdeps)) {
		goto clean_return;
	}

	while (i < AST_VECTOR_SIZE(&localdeps)) {
		dep = find_resource(AST_VECTOR_GET(&localdeps, i), 0);
		if (!dep) {
			goto clean_return;
		}

		if (AST_VECTOR_GET_CMP(missingdeps, dep, AST_VECTOR_ELEM_DEFAULT_CMP)) {
			/* Skip common dependency.  We have already searched it. */
			AST_VECTOR_REMOVE(&localdeps, i, 0);
		} else {
			/* missingdeps is the real list so keep it sorted. */
			if (AST_VECTOR_ADD_SORTED(missingdeps, dep, module_vector_cmp)) {
				goto clean_return;
			}
			i++;
		}
	}

	res = 0;
	for (i = 0; !res && i < AST_VECTOR_SIZE(&localdeps); i++) {
		dep = find_resource(AST_VECTOR_GET(&localdeps, i), 0);
		/* We've already confirmed dep is loaded in the first loop. */
		res = module_deps_missing_recursive(dep, missingdeps);
	}

clean_return:
	AST_VECTOR_FREE(&localdeps);

	return res;
}

const char *ast_module_name(const struct ast_module *mod)
{
	if (!mod || !mod->info) {
		return NULL;
	}

	return mod->info->name;
}

struct loadupdate {
	int (*updater)(void);
	AST_LIST_ENTRY(loadupdate) entry;
};

static AST_DLLIST_HEAD_STATIC(updaters, loadupdate);

AST_MUTEX_DEFINE_STATIC(reloadlock);

struct reload_queue_item {
	AST_LIST_ENTRY(reload_queue_item) entry;
	char module[0];
};

static int do_full_reload = 0;

static AST_DLLIST_HEAD_STATIC(reload_queue, reload_queue_item);

/*!
 * \internal
 *
 * This variable is set by load_dynamic_module so ast_module_register
 * can know what pointer is being registered.
 *
 * This is protected by the module_list lock.
 */
static struct ast_module * volatile resource_being_loaded;

/*!
 * \internal
 * \brief Used by AST_MODULE_INFO to register with the module loader.
 *
 * This function is automatically called when each module is opened.
 * It must never be used from outside AST_MODULE_INFO.
 */
void ast_module_register(const struct ast_module_info *info)
{
	struct ast_module *mod;

	if (!loader_ready) {
		mod = ast_std_calloc(1, sizeof(*mod) + strlen(info->name) + 1);
		if (!mod) {
			/* We haven't even reached main() yet, if we can't
			 * allocate memory at this point just give up. */
			fprintf(stderr, "Allocation failure during startup.\n");
			exit(2);
		}
		strcpy(mod->resource, info->name); /* safe */
		mod->info = info;
		mod->flags.builtin = 1;
		AST_DLLIST_INSERT_TAIL(&builtin_module_list, mod, entry);

		/* ast_module_register for built-in modules is run again during module preload. */
		return;
	}

	/*
	 * This lock protects resource_being_loaded as well as the module
	 * list.  Normally we already have a lock on module_list when we
	 * begin the load but locking again from here prevents corruption
	 * if an asterisk module is dlopen'ed from outside the module loader.
	 */
	AST_DLLIST_LOCK(&module_list);
	mod = resource_being_loaded;
	if (!mod) {
		AST_DLLIST_UNLOCK(&module_list);
		return;
	}

	ast_debug(5, "Registering module %s\n", info->name);

	/* This tells load_dynamic_module that we're registered. */
	resource_being_loaded = NULL;

	mod->info = info;
	if (ast_opt_ref_debug) {
		mod->ref_debug = ao2_t_alloc_options(0, NULL, AO2_ALLOC_OPT_LOCK_NOLOCK, info->name);
	}
	AST_LIST_HEAD_INIT(&mod->users);
	AST_VECTOR_INIT(&mod->requires, 0);
	AST_VECTOR_INIT(&mod->optional_modules, 0);
	AST_VECTOR_INIT(&mod->enhances, 0);
	AST_VECTOR_INIT(&mod->reffed_deps, 0);

	AST_DLLIST_INSERT_TAIL(&module_list, mod, entry);
	AST_DLLIST_UNLOCK(&module_list);

	/* give the module a copy of its own handle, for later use in registrations and the like */
	*((struct ast_module **) &(info->self)) = mod;
}

static int module_post_register(struct ast_module *mod)
{
	int res;

	/* Split lists from mod->info. */
	res  = ast_vector_string_split(&mod->requires, mod->info->requires, ",", 0, strcasecmp);
	res |= ast_vector_string_split(&mod->optional_modules, mod->info->optional_modules, ",", 0, strcasecmp);
	res |= ast_vector_string_split(&mod->enhances, mod->info->enhances, ",", 0, strcasecmp);

	return res;
}

static void module_destroy(struct ast_module *mod)
{
	AST_VECTOR_CALLBACK_VOID(&mod->requires, ast_free);
	AST_VECTOR_FREE(&mod->requires);

	AST_VECTOR_CALLBACK_VOID(&mod->optional_modules, ast_free);
	AST_VECTOR_FREE(&mod->optional_modules);

	AST_VECTOR_CALLBACK_VOID(&mod->enhances, ast_free);
	AST_VECTOR_FREE(&mod->enhances);

	/* Release references to all dependencies. */
	AST_VECTOR_CALLBACK_VOID(&mod->reffed_deps, ast_module_unref);
	AST_VECTOR_FREE(&mod->reffed_deps);

	AST_LIST_HEAD_DESTROY(&mod->users);
	ao2_cleanup(mod->ref_debug);
	if (mod->flags.builtin) {
		ast_std_free(mod);
	} else {
		ast_free(mod);
	}
}

void ast_module_unregister(const struct ast_module_info *info)
{
	struct ast_module *mod = NULL;

	/* it is assumed that the users list in the module structure
	   will already be empty, or we cannot have gotten to this
	   point
	*/
	AST_DLLIST_LOCK(&module_list);
	AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&module_list, mod, entry) {
		if (mod->info == info) {
			AST_DLLIST_REMOVE_CURRENT(entry);
			break;
		}
	}
	AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END;
	AST_DLLIST_UNLOCK(&module_list);

	if (mod && !mod->usecount) {
		/*
		 * We are intentionally leaking mod if usecount is not zero.
		 * This is necessary if the module is being forcefully unloaded.
		 * In addition module_destroy is not safe to run after exit()
		 * is called.  ast_module_unregister is run during cleanup of
		 * the process when libc releases each module's shared object
		 * library.
		 */
		ast_debug(5, "Unregistering module %s\n", info->name);
		module_destroy(mod);
	}
}

struct ast_module_user *__ast_module_user_add(struct ast_module *mod, struct ast_channel *chan)
{
	struct ast_module_user *u;

	u = ast_calloc(1, sizeof(*u));
	if (!u) {
		return NULL;
	}

	u->chan = chan;

	AST_LIST_LOCK(&mod->users);
	AST_LIST_INSERT_HEAD(&mod->users, u, entry);
	AST_LIST_UNLOCK(&mod->users);

	if (mod->ref_debug) {
		ao2_ref(mod->ref_debug, +1);
	}

	ast_atomic_fetchadd_int(&mod->usecount, +1);

	ast_update_use_count();

	return u;
}

void __ast_module_user_remove(struct ast_module *mod, struct ast_module_user *u)
{
	if (!u) {
		return;
	}

	AST_LIST_LOCK(&mod->users);
	u = AST_LIST_REMOVE(&mod->users, u, entry);
	AST_LIST_UNLOCK(&mod->users);
	if (!u) {
		/*
		 * Was not in the list.  Either a bad pointer or
		 * __ast_module_user_hangup_all() has been called.
		 */
		return;
	}

	if (mod->ref_debug) {
		ao2_ref(mod->ref_debug, -1);
	}

	ast_atomic_fetchadd_int(&mod->usecount, -1);
	ast_free(u);

	ast_update_use_count();
}

void __ast_module_user_hangup_all(struct ast_module *mod)
{
	struct ast_module_user *u;

	AST_LIST_LOCK(&mod->users);
	while ((u = AST_LIST_REMOVE_HEAD(&mod->users, entry))) {
		if (u->chan) {
			ast_softhangup(u->chan, AST_SOFTHANGUP_APPUNLOAD);
		}

		if (mod->ref_debug) {
			ao2_ref(mod->ref_debug, -1);
		}

		ast_atomic_fetchadd_int(&mod->usecount, -1);
		ast_free(u);
	}
	AST_LIST_UNLOCK(&mod->users);

	ast_update_use_count();
}

static int printdigest(const unsigned char *d)
{
	int x, pos;
	char buf[256]; /* large enough so we don't have to worry */

	for (pos = 0, x = 0; x < 16; x++)
		pos += sprintf(buf + pos, " %02hhx", *d++);

	ast_debug(1, "Unexpected signature:%s\n", buf);

	return 0;
}

static int key_matches(const unsigned char *key1, const unsigned char *key2)
{
	int x;

	for (x = 0; x < 16; x++) {
		if (key1[x] != key2[x])
			return 0;
	}

	return 1;
}

static int verify_key(const unsigned char *key)
{
	struct MD5Context c;
	unsigned char digest[16];

	MD5Init(&c);
	MD5Update(&c, key, strlen((char *)key));
	MD5Final(digest, &c);

	if (key_matches(expected_key, digest))
		return 0;

	printdigest(digest);

	return -1;
}

static size_t resource_name_baselen(const char *name)
{
	size_t len = strlen(name);

	if (len > 3 && !strcasecmp(name + len - 3, ".so")) {
		return len - 3;
	}

	return len;
}

static int resource_name_match(const char *name1, size_t baselen1, const char *name2)
{
	if (baselen1 != resource_name_baselen(name2)) {
		return -1;
	}

	return strncasecmp(name1, name2, baselen1);
}

static struct ast_module *find_resource(const char *resource, int do_lock)
{
	struct ast_module *cur;
	size_t resource_baselen = resource_name_baselen(resource);

	if (do_lock) {
		AST_DLLIST_LOCK(&module_list);
	}

	AST_DLLIST_TRAVERSE(&module_list, cur, entry) {
		if (!resource_name_match(resource, resource_baselen, cur->resource)) {
			break;
		}
	}

	if (do_lock) {
		AST_DLLIST_UNLOCK(&module_list);
	}

	return cur;
}

/*!
 * \brief dlclose(), with failure logging.
 */
static void logged_dlclose(const char *name, void *lib)
{
	char *error;

	if (!lib) {
		return;
	}

	/* Clear any existing error */
	dlerror();
	if (dlclose(lib)) {
		error = dlerror();
		ast_log(AST_LOG_ERROR, "Failure in dlclose for module '%s': %s\n",
			S_OR(name, "unknown"), S_OR(error, "Unknown error"));
	}
}

#if defined(HAVE_RTLD_NOLOAD)
/*!
 * \brief Check to see if the given resource is loaded.
 *
 * \param resource_name Name of the resource, including .so suffix.
 * \return False (0) if module is not loaded.
 * \return True (non-zero) if module is loaded.
 */
static int is_module_loaded(const char *resource_name)
{
	char fn[PATH_MAX] = "";
	void *lib;

	snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_MODULE_DIR,
		resource_name);

	lib = dlopen(fn, RTLD_LAZY | RTLD_NOLOAD);

	if (lib) {
		logged_dlclose(resource_name, lib);
		return 1;
	}

	return 0;
}
#endif

static void unload_dynamic_module(struct ast_module *mod)
{
#if defined(HAVE_RTLD_NOLOAD)
	char *name = ast_strdupa(ast_module_name(mod));
#endif
	void *lib = mod->lib;

	/* WARNING: the structure pointed to by mod is going to
	   disappear when this operation succeeds, so we can't
	   dereference it */
	logged_dlclose(ast_module_name(mod), lib);

	/* There are several situations where the module might still be resident
	 * in memory.
	 *
	 * If somehow there was another dlopen() on the same module (unlikely,
	 * since that all is supposed to happen in loader.c).
	 *
	 * Avoid the temptation of repeating the dlclose(). The other code that
	 * dlopened the module still has its module reference, and should close
	 * it itself. In other situations, dlclose() will happily return success
	 * for as many times as you wish to call it.
	 */
#if defined(HAVE_RTLD_NOLOAD)
	if (is_module_loaded(name)) {
		ast_log(LOG_WARNING, "Module '%s' could not be completely unloaded\n", name);
	}
#endif
}

static int load_dlopen_missing(struct ast_str **list, struct ast_vector_string *deps)
{
	int i;
	int c = 0;

	for (i = 0; i < AST_VECTOR_SIZE(deps); i++) {
		const char *dep = AST_VECTOR_GET(deps, i);
		if (!find_resource(dep, 0)) {
			STR_APPEND_TEXT(dep, list);
			c++;
		}
	}

	return c;
}

/*!
 * \internal
 * \brief Attempt to dlopen a module.
 *
 * \param resource_in The module name to load.
 * \param so_ext ".so" or blank if ".so" is already part of resource_in.
 * \param filename Passed directly to dlopen.
 * \param flags Passed directly to dlopen.
 * \param suppress_logging Do not log any error from dlopen.
 *
 * \return Pointer to opened module, NULL on error.
 *
 * \warning module_list must be locked before calling this function.
 */
static struct ast_module *load_dlopen(const char *resource_in, const char *so_ext,
	const char *filename, int flags, unsigned int suppress_logging)
{
	struct ast_module *mod;

	ast_assert(!resource_being_loaded);

	mod = ast_calloc(1, sizeof(*mod) + strlen(resource_in) + strlen(so_ext) + 1);
	if (!mod) {
		return NULL;
	}

	sprintf(mod->resource, "%s%s", resource_in, so_ext); /* safe */

	resource_being_loaded = mod;
	mod->lib = dlopen(filename, flags);
	if (resource_being_loaded) {
		struct ast_str *list;
		int c = 0;
		const char *dlerror_msg = ast_strdupa(dlerror());

		resource_being_loaded = NULL;
		if (mod->lib) {
			module_load_error("Module '%s' did not register itself during load\n", resource_in);
			logged_dlclose(resource_in, mod->lib);

			goto error_return;
		}

		if (suppress_logging) {
			goto error_return;
		}

		resource_being_loaded = mod;
		mod->lib = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
		if (resource_being_loaded) {
			resource_being_loaded = NULL;

			module_load_error("Error loading module '%s': %s\n", resource_in, dlerror_msg);
			logged_dlclose(resource_in, mod->lib);

			goto error_return;
		}

		list = ast_str_create(64);
		if (list) {
			if (module_post_register(mod)) {
				goto loaded_error;
			}

			c = load_dlopen_missing(&list, &mod->requires);
			c += load_dlopen_missing(&list, &mod->enhances);
#ifndef OPTIONAL_API
			c += load_dlopen_missing(&list, &mod->optional_modules);
#endif
		}

		if (list && ast_str_strlen(list)) {
			module_load_error("Error loading module '%s', missing %s: %s\n",
				resource_in, c == 1 ? "dependency" : "dependencies", ast_str_buffer(list));
		} else {
			module_load_error("Error loading module '%s': %s\n", resource_in, dlerror_msg);
		}

loaded_error:
		ast_free(list);
		unload_dynamic_module(mod);

		return NULL;

error_return:
		ast_free(mod);

		return NULL;
	}

	return mod;
}

static struct ast_module *load_dynamic_module(const char *resource_in, unsigned int suppress_logging)
{
	char fn[PATH_MAX];
	struct ast_module *mod;
	size_t resource_in_len = strlen(resource_in);
	const char *so_ext = "";

	if (resource_in_len < 4 || strcasecmp(resource_in + resource_in_len - 3, ".so")) {
		so_ext = ".so";
	}

	snprintf(fn, sizeof(fn), "%s/%s%s", ast_config_AST_MODULE_DIR, resource_in, so_ext);

	/* Try loading in quiet mode first with RTLD_LOCAL.  The majority of modules do not
	 * export symbols so this allows the least number of calls to dlopen. */
	mod = load_dlopen(resource_in, so_ext, fn, RTLD_NOW | RTLD_LOCAL, suppress_logging);

	if (!mod || !ast_test_flag(mod->info, AST_MODFLAG_GLOBAL_SYMBOLS)) {
		return mod;
	}

	/* Close the module so we can reopen with correct flags. */
	logged_dlclose(resource_in, mod->lib);

	return load_dlopen(resource_in, so_ext, fn, RTLD_NOW | RTLD_GLOBAL, 0);
}

int modules_shutdown(void)
{
	struct ast_module *mod;
	int somethingchanged;
	int res;

	AST_DLLIST_LOCK(&module_list);

	/*!\note Some resources, like timers, are started up dynamically, and thus
	 * may be still in use, even if all channels are dead.  We must therefore
	 * check the usecount before asking modules to unload. */
	do {
		/* Reset flag before traversing the list */
		somethingchanged = 0;

		AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&module_list, mod, entry) {
			if (mod->usecount) {
				ast_debug(1, "Passing on %s: its use count is %d\n",
					mod->resource, mod->usecount);
				continue;
			}
			AST_DLLIST_REMOVE_CURRENT(entry);
			if (mod->flags.running && !mod->flags.declined && mod->info->unload) {
				ast_verb(1, "Unloading %s\n", mod->resource);
				mod->info->unload();
			}
			module_destroy(mod);
			somethingchanged = 1;
		}
		AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END;
		if (!somethingchanged) {
			AST_DLLIST_TRAVERSE(&module_list, mod, entry) {
				if (mod->flags.keepuntilshutdown) {
					ast_module_unref(mod);
					mod->flags.keepuntilshutdown = 0;
					somethingchanged = 1;
				}
			}
		}
	} while (somethingchanged);

	res = AST_DLLIST_EMPTY(&module_list);
	AST_DLLIST_UNLOCK(&module_list);

	return !res;
}

int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode force)
{
	struct ast_module *mod;
	int res = -1;
	int error = 0;

	AST_DLLIST_LOCK(&module_list);

	if (!(mod = find_resource(resource_name, 0))) {
		AST_DLLIST_UNLOCK(&module_list);
		ast_log(LOG_WARNING, "Unload failed, '%s' could not be found\n", resource_name);
		return -1;
	}

	if (!mod->flags.running || mod->flags.declined) {
		ast_log(LOG_WARNING, "Unload failed, '%s' is not loaded.\n", resource_name);
		error = 1;
	}

	if (!error && (mod->usecount > 0)) {
		if (force)
			ast_log(LOG_WARNING, "Warning:  Forcing removal of module '%s' with use count %d\n",
				resource_name, mod->usecount);
		else {
			ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name,
				mod->usecount);
			error = 1;
		}
	}

	if (!error) {
		/* Request any channels attached to the module to hangup. */
		__ast_module_user_hangup_all(mod);

		ast_verb(1, "Unloading %s\n", mod->resource);
		res = mod->info->unload();
		if (res) {
			ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
			if (force <= AST_FORCE_FIRM) {
				error = 1;
			} else {
				ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
			}
		}

		if (!error) {
			/*
			 * Request hangup on any channels that managed to get attached
			 * while we called the module unload function.
			 */
			__ast_module_user_hangup_all(mod);
			sched_yield();
		}
	}

	if (!error)
		mod->flags.running = mod->flags.declined = 0;

	AST_DLLIST_UNLOCK(&module_list);

	if (!error) {
		unload_dynamic_module(mod);
		ast_test_suite_event_notify("MODULE_UNLOAD", "Message: %s", resource_name);
		ast_update_use_count();
		publish_unload_message(resource_name, "Success");
	}

	return res;
}

static int module_matches_helper_type(struct ast_module *mod, enum ast_module_helper_type type)
{
	switch (type) {
	case AST_MODULE_HELPER_UNLOAD:
		return !mod->usecount && mod->flags.running && !mod->flags.declined;

	case AST_MODULE_HELPER_RELOAD:
		return mod->flags.running && mod->info->reload;

	case AST_MODULE_HELPER_RUNNING:
		return mod->flags.running;

	case AST_MODULE_HELPER_LOADED:
		/* if we have a 'struct ast_module' then we're loaded. */
		return 1;
	default:
		/* This function is not called for AST_MODULE_HELPER_LOAD. */
		/* Unknown ast_module_helper_type. Assume it doesn't match. */
		ast_assert(0);

		return 0;
	}
}

struct module_load_word {
	const char *word;
	size_t len;
	size_t moddir_len;
};

static int module_load_helper_on_file(const char *dir_name, const char *filename, void *obj)
{
	struct module_load_word *word = obj;
	struct ast_module *mod;
	char *filename_merged = NULL;

	/* dir_name will never be shorter than word->moddir_len. */
	dir_name += word->moddir_len;
	if (!ast_strlen_zero(dir_name)) {
		ast_assert(dir_name[0] == '/');

		dir_name += 1;
		if (ast_asprintf(&filename_merged, "%s/%s", dir_name, filename) < 0) {
			/* If we can't allocate the string just give up! */
			return -1;
		}
		filename = filename_merged;
	}

	if (!strncasecmp(filename, word->word, word->len)) {
		/* Don't list files that are already loaded! */
		mod = find_resource(filename, 0);
		if (!mod || !mod->flags.running) {
			ast_cli_completion_add(ast_strdup(filename));
		}
	}

	ast_free(filename_merged);

	return 0;
}

static void module_load_helper(const char *word)
{
	struct module_load_word word_l = {
		.word = word,
		.len = strlen(word),
		.moddir_len = strlen(ast_config_AST_MODULE_DIR),
	};

	AST_DLLIST_LOCK(&module_list);
	ast_file_read_dirs(ast_config_AST_MODULE_DIR, module_load_helper_on_file, &word_l, -1);
	AST_DLLIST_UNLOCK(&module_list);
}

char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, enum ast_module_helper_type type)
{
	struct ast_module *mod;
	int which = 0;
	int wordlen = strlen(word);
	char *ret = NULL;

	if (pos != rpos) {
		return NULL;
	}

	if (type == AST_MODULE_HELPER_LOAD) {
		module_load_helper(word);

		return NULL;
	}

	AST_DLLIST_LOCK(&module_list);
	AST_DLLIST_TRAVERSE(&module_list, mod, entry) {
		if (!module_matches_helper_type(mod, type)) {
			continue;
		}

		if (!strncasecmp(word, mod->resource, wordlen) && ++which > state) {
			ret = ast_strdup(mod->resource);
			break;
		}
	}
	AST_DLLIST_UNLOCK(&module_list);

	return ret;
}

void ast_process_pending_reloads(void)
{
	struct reload_queue_item *item;

	modules_loaded = 1;

	AST_LIST_LOCK(&reload_queue);

	if (do_full_reload) {
		do_full_reload = 0;
		AST_LIST_UNLOCK(&reload_queue);
		ast_log(LOG_NOTICE, "Executing deferred reload request.\n");
		ast_module_reload(NULL);
		return;
	}

	while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
		ast_log(LOG_NOTICE, "Executing deferred reload request for module '%s'.\n", item->module);
		ast_module_reload(item->module);
		ast_free(item);
	}

	AST_LIST_UNLOCK(&reload_queue);
}

static void queue_reload_request(const char *module)
{
	struct reload_queue_item *item;

	AST_LIST_LOCK(&reload_queue);

	if (do_full_reload) {
		AST_LIST_UNLOCK(&reload_queue);
		return;
	}

	if (ast_strlen_zero(module)) {
		/* A full reload request (when module is NULL) wipes out any previous
		   reload requests and causes the queue to ignore future ones */
		while ((item = AST_LIST_REMOVE_HEAD(&reload_queue, entry))) {
			ast_free(item);
		}
		do_full_reload = 1;
	} else {
		/* No reason to add the same module twice */
		AST_LIST_TRAVERSE(&reload_queue, item, entry) {
			if (!strcasecmp(item->module, module)) {
				AST_LIST_UNLOCK(&reload_queue);
				return;
			}
		}
		item = ast_calloc(1, sizeof(*item) + strlen(module) + 1);
		if (!item) {
			ast_log(LOG_ERROR, "Failed to allocate reload queue item.\n");
			AST_LIST_UNLOCK(&reload_queue);
			return;
		}
		strcpy(item->module, module);
		AST_LIST_INSERT_TAIL(&reload_queue, item, entry);
	}
	AST_LIST_UNLOCK(&reload_queue);
}

/*!
 * \since 12
 * \internal
 * \brief Publish a \ref stasis message regarding the type.
 */
static void publish_load_message_type(const char* type, const char *name, const char *status)
{
	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
	RAII_VAR(struct ast_json_payload *, payload, NULL, ao2_cleanup);
	RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
	RAII_VAR(struct ast_json *, event_object, NULL, ast_json_unref);

	ast_assert(type != NULL);
	ast_assert(!ast_strlen_zero(name));
	ast_assert(!ast_strlen_zero(status));

	if (!ast_manager_get_generic_type()) {
		return;
	}

	event_object = ast_json_pack("{s:s, s:s}",
			"Module", name,
			"Status", status);
	json_object = ast_json_pack("{s:s, s:i, s:o}",
			"type", type,
			"class_type", EVENT_FLAG_SYSTEM,
			"event", ast_json_ref(event_object));
	if (!json_object) {
		return;
	}

	payload = ast_json_payload_create(json_object);
	if (!payload) {
		return;
	}

	message = stasis_message_create(ast_manager_get_generic_type(), payload);
	if (!message) {
		return;
	}

	stasis_publish(ast_manager_get_topic(), message);
}

static const char* loadresult2str(enum ast_module_load_result result)
{
	int i;
	for (i = 0; i < ARRAY_LEN(load_results); i++) {
		if (load_results[i].result == result) {
			return load_results[i].name;
		}
	}

	ast_log(LOG_WARNING, "Failed to find correct load result status. result %d\n", result);
	return AST_MODULE_LOAD_UNKNOWN_STRING;
}

/*!
 * \internal
 * \brief Publish a \ref stasis message regarding the load result
 */
static void publish_load_message(const char *name, enum ast_module_load_result result)
{
	const char *status;

	status = loadresult2str(result);

	publish_load_message_type("Load", name, status);
}

/*!
 * \internal
 * \brief Publish a \ref stasis message regarding the unload result
 */
static void publish_unload_message(const char *name, const char* status)
{
	publish_load_message_type("Unload", name, status);
}

/*!
 * \since 12
 * \internal
 * \brief Publish a \ref stasis message regarding the reload result
 */
static void publish_reload_message(const char *name, enum ast_module_reload_result result)
{
	char res_buffer[8];

	snprintf(res_buffer, sizeof(res_buffer), "%u", result);
	publish_load_message_type("Reload", S_OR(name, "All"), res_buffer);
}

enum ast_module_reload_result ast_module_reload(const char *name)
{
	struct ast_module *cur;
	enum ast_module_reload_result res = AST_MODULE_RELOAD_NOT_FOUND;
	size_t name_baselen = name ? resource_name_baselen(name) : 0;

	/* If we aren't fully booted, we just pretend we reloaded but we queue this
	   up to run once we are booted up. */
	if (!modules_loaded) {
		queue_reload_request(name);
		res = AST_MODULE_RELOAD_QUEUED;
		goto module_reload_exit;
	}

	if (ast_mutex_trylock(&reloadlock)) {
		ast_verb(3, "The previous reload command didn't finish yet\n");
		res = AST_MODULE_RELOAD_IN_PROGRESS;
		goto module_reload_exit;
	}
	ast_sd_notify("RELOAD=1");
	ast_lastreloadtime = ast_tvnow();

	if (ast_opt_lock_confdir) {
		int try;
		int lockres;
		for (try = 1, lockres = AST_LOCK_TIMEOUT; try < 6 && (lockres == AST_LOCK_TIMEOUT); try++) {
			lockres = ast_lock_path(ast_config_AST_CONFIG_DIR);
			if (lockres == AST_LOCK_TIMEOUT) {
				ast_log(LOG_WARNING, "Failed to grab lock on %s, try %d\n", ast_config_AST_CONFIG_DIR, try);
			}
		}
		if (lockres != AST_LOCK_SUCCESS) {
			ast_log(AST_LOG_WARNING, "Cannot grab lock on %s\n", ast_config_AST_CONFIG_DIR);
			res = AST_MODULE_RELOAD_ERROR;
			goto module_reload_done;
		}
	}

	AST_DLLIST_LOCK(&module_list);
	AST_DLLIST_TRAVERSE(&module_list, cur, entry) {
		const struct ast_module_info *info = cur->info;

		if (name && resource_name_match(name, name_baselen, cur->resource)) {
			continue;
		}

		if (!cur->flags.running || cur->flags.declined) {
			if (res == AST_MODULE_RELOAD_NOT_FOUND) {
				res = AST_MODULE_RELOAD_UNINITIALIZED;
			}
			if (!name) {
				continue;
			}
			break;
		}

		if (!info->reload) {	/* cannot be reloaded */
			if (res == AST_MODULE_RELOAD_NOT_FOUND) {
				res = AST_MODULE_RELOAD_NOT_IMPLEMENTED;
			}
			if (!name) {
				continue;
			}
			break;
		}
		ast_verb(3, "Reloading module '%s' (%s)\n", cur->resource, info->description);
		if (info->reload() == AST_MODULE_LOAD_SUCCESS) {
			res = AST_MODULE_RELOAD_SUCCESS;
		} else if (res == AST_MODULE_RELOAD_NOT_FOUND) {
			res = AST_MODULE_RELOAD_ERROR;
		}
		if (name) {
			break;
		}
	}
	AST_DLLIST_UNLOCK(&module_list);

	if (ast_opt_lock_confdir) {
		ast_unlock_path(ast_config_AST_CONFIG_DIR);
	}
module_reload_done:
	ast_mutex_unlock(&reloadlock);
	ast_sd_notify("READY=1");

module_reload_exit:
	publish_reload_message(name, res);
	return res;
}

static unsigned int inspect_module(const struct ast_module *mod)
{
	if (!mod->info->description) {
		module_load_error("Module '%s' does not provide a description.\n", mod->resource);
		return 1;
	}

	if (!mod->info->key) {
		module_load_error("Module '%s' does not provide a license key.\n", mod->resource);
		return 1;
	}

	if (verify_key((unsigned char *) mod->info->key)) {
		module_load_error("Module '%s' did not provide a valid license key.\n", mod->resource);
		return 1;
	}

	if (!ast_strlen_zero(mod->info->buildopt_sum) &&
	    strcmp(buildopt_sum, mod->info->buildopt_sum)) {
		module_load_error("Module '%s' was not compiled with the same compile-time options as this version of Asterisk.\n", mod->resource);
		module_load_error("Module '%s' will not be initialized as it may cause instability.\n", mod->resource);
		return 1;
	}

	return 0;
}

static enum ast_module_load_result start_resource(struct ast_module *mod)
{
	char tmp[256];
	enum ast_module_load_result res;

	if (mod->flags.running) {
		return AST_MODULE_LOAD_SUCCESS;
	}

	if (!mod->info->load) {
		mod->flags.declined = 1;

		return mod->flags.required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
	}

	if (module_deps_reference(mod, NULL)) {
		struct module_vector missing;
		int i;

		AST_VECTOR_INIT(&missing, 0);
		if (module_deps_missing_recursive(mod, &missing)) {
			module_load_error("%s has one or more unknown dependencies.\n", mod->info->name);
		}
		for (i = 0; i < AST_VECTOR_SIZE(&missing); i++) {
			module_load_error("%s loaded before dependency %s!\n", mod->info->name,
				AST_VECTOR_GET(&missing, i)->info->name);
		}
		AST_VECTOR_FREE(&missing);

		return AST_MODULE_LOAD_DECLINE;
	}

	if (!ast_fully_booted) {
		ast_verb(1, "Loading %s.\n", mod->resource);
	}
	res = mod->info->load();

	switch (res) {
	case AST_MODULE_LOAD_SUCCESS:
		if (!ast_fully_booted) {
			ast_verb(2, "%s => (%s)\n", mod->resource, term_color(tmp, mod->info->description, COLOR_BROWN, COLOR_BLACK, sizeof(tmp)));
		} else {
			ast_verb(1, "Loaded %s => (%s)\n", mod->resource, mod->info->description);
		}

		mod->flags.running = 1;
		if (mod->flags.builtin) {
			/* Built-in modules cannot be unloaded. */
			ast_module_shutdown_ref(mod);
		}

		ast_update_use_count();
		break;
	case AST_MODULE_LOAD_DECLINE:
		mod->flags.declined = 1;
		if (mod->flags.required) {
			res = AST_MODULE_LOAD_FAILURE;
		}
		break;
	case AST_MODULE_LOAD_FAILURE:
		mod->flags.declined = 1;
		break;
	case AST_MODULE_LOAD_SKIP: /* modules should never return this value */
	case AST_MODULE_LOAD_PRIORITY:
		break;
	}

	/* Make sure the newly started module is at the end of the list */
	AST_DLLIST_LOCK(&module_list);
	AST_DLLIST_REMOVE(&module_list, mod, entry);
	AST_DLLIST_INSERT_TAIL(&module_list, mod, entry);
	AST_DLLIST_UNLOCK(&module_list);

	return res;
}

/*! loads a resource based upon resource_name. If global_symbols_only is set
 *  only modules with global symbols will be loaded.
 *
 *  If the module_vector is provided (not NULL) the module is found and added to the
 *  vector without running the module's load() function.  By doing this, modules
 *  can be initialized later in order by priority and dependencies.
 *
 *  If the module_vector is not provided, the module's load function will be executed
 *  immediately */
static enum ast_module_load_result load_resource(const char *resource_name, unsigned int suppress_logging,
	struct module_vector *module_priorities, int required, int preload)
{
	struct ast_module *mod;
	enum ast_module_load_result res = AST_MODULE_LOAD_SUCCESS;

	if ((mod = find_resource(resource_name, 0))) {
		if (mod->flags.running) {
			ast_log(LOG_WARNING, "Module '%s' already loaded and running.\n", resource_name);
			return AST_MODULE_LOAD_DECLINE;
		}
	} else {
		mod = load_dynamic_module(resource_name, suppress_logging);
		if (!mod) {
			return required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
		}

		if (module_post_register(mod)) {
			goto prestart_error;
		}
	}

	mod->flags.required |= required;
	mod->flags.preload |= preload;

	if (inspect_module(mod)) {
		goto prestart_error;
	}

	mod->flags.declined = 0;

	if (module_priorities) {
		if (AST_VECTOR_ADD_SORTED(module_priorities, mod, module_vector_cmp)) {
			goto prestart_error;
		}
		res = AST_MODULE_LOAD_PRIORITY;
	} else {
		res = start_resource(mod);
	}

	if (ast_fully_booted && !ast_shutdown_final()) {
		publish_load_message(resource_name, res);
	}

	return res;

prestart_error:
	module_load_error("Module '%s' could not be loaded.\n", resource_name);
	unload_dynamic_module(mod);
	res = required ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_DECLINE;
	if (ast_fully_booted && !ast_shutdown_final()) {
		publish_load_message(resource_name, res);
	}
	return res;
}

int ast_load_resource(const char *resource_name)
{
	int res;
	AST_DLLIST_LOCK(&module_list);
	res = load_resource(resource_name, 0, NULL, 0, 0);
	if (!res) {
		ast_test_suite_event_notify("MODULE_LOAD", "Message: %s", resource_name);
	}
	AST_DLLIST_UNLOCK(&module_list);

	return res;
}

struct load_order_entry {
	char *resource;
	int required;
	int preload;
	int builtin;
	AST_LIST_ENTRY(load_order_entry) entry;
};

AST_LIST_HEAD_NOLOCK(load_order, load_order_entry);

static struct load_order_entry *add_to_load_order(const char *resource, struct load_order *load_order, int required, int preload, int builtin)
{
	struct load_order_entry *order;
	size_t resource_baselen = resource_name_baselen(resource);

	AST_LIST_TRAVERSE(load_order, order, entry) {
		if (!resource_name_match(resource, resource_baselen, order->resource)) {
			/* Make sure we have the proper setting for the required field
			   (we might have both load= and required= lines in modules.conf) */
			order->required |= required;
			order->preload |= preload;
			return order;
		}
	}

	order = ast_calloc(1, sizeof(*order));
	if (!order) {
		return NULL;
	}

	order->resource = ast_strdup(resource);
	if (!order->resource) {
		ast_free(order);

		return NULL;
	}
	order->required = required;
	order->preload = preload;
	order->builtin = builtin;
	AST_LIST_INSERT_TAIL(load_order, order, entry);

	return order;
}

AST_LIST_HEAD_NOLOCK(load_retries, load_order_entry);

static enum ast_module_load_result start_resource_attempt(struct ast_module *mod, int *count)
{
	enum ast_module_load_result lres;

	/* Try to grab required references. */
	if (module_deps_reference(mod, NULL)) {
		/* We're likely to retry so not an error. */
		ast_debug(1, "Module %s is missing dependencies\n", mod->resource);
		return AST_MODULE_LOAD_SKIP;
	}

	lres = start_resource(mod);
	ast_debug(3, "START: %-46s[%d] %d\n",
		mod->resource,
		ast_test_flag(mod->info, AST_MODFLAG_LOAD_ORDER) ? mod->info->load_pri : AST_MODPRI_DEFAULT,
		lres);

	if (lres == AST_MODULE_LOAD_SUCCESS) {
		(*count)++;
	} else if (lres == AST_MODULE_LOAD_FAILURE) {
		module_load_error("*** Failed to load %smodule %s\n",
			mod->flags.required ? "required " : "",
			mod->resource);
	}

	return lres;
}

static int resource_list_recursive_decline(struct module_vector *resources, struct ast_module *mod,
	struct ast_str **printmissing)
{
	struct module_vector missingdeps;
	struct ast_vector_const_string localdeps;
	int i = 0;
	int res = -1;

	mod->flags.declined = 1;
	if (mod->flags.required) {
		module_load_error("Required module %s declined to load.\n", ast_module_name(mod));

		return -2;
	}

	module_load_error("%s declined to load.\n", ast_module_name(mod));

	if (!*printmissing) {
		*printmissing = ast_str_create(64);
		if (!*printmissing) {
			return -1;
		}
	} else {
		ast_str_reset(*printmissing);
	}

	AST_VECTOR_INIT(&missingdeps, 0);
	AST_VECTOR_INIT(&localdeps, 0);

	/* Decline everything that depends on 'mod' from resources so we can
	 * print a concise list. */
	while (res != -2 && i < AST_VECTOR_SIZE(resources)) {
		struct ast_module *dep = AST_VECTOR_GET(resources, i);
		i++;

		AST_VECTOR_RESET(&missingdeps, AST_VECTOR_ELEM_CLEANUP_NOOP);
		if (dep->flags.declined || module_deps_missing_recursive(dep, &missingdeps)) {
			continue;
		}

		if (AST_VECTOR_GET_CMP(&missingdeps, mod, AST_VECTOR_ELEM_DEFAULT_CMP)) {
			dep->flags.declined = 1;
			if (dep->flags.required) {
				module_load_error("Cannot load required module %s that depends on %s\n",
					ast_module_name(dep), ast_module_name(mod));
				res = -2;
			} else {
				AST_VECTOR_APPEND(&localdeps, ast_module_name(dep));
			}
		}
	}
	AST_VECTOR_FREE(&missingdeps);

	if (res != -2 && AST_VECTOR_SIZE(&localdeps)) {
		AST_VECTOR_CALLBACK_VOID(&localdeps, STR_APPEND_TEXT, printmissing);
		module_load_error("Declined modules which depend on %s: %s\n",
			ast_module_name(mod), ast_str_buffer(*printmissing));
	}
	AST_VECTOR_FREE(&localdeps);

	return res;
}

static int start_resource_list(struct module_vector *resources, int *mod_count)
{
	struct module_vector missingdeps;
	int res = 0;
	struct ast_str *printmissing = NULL;

	AST_VECTOR_INIT(&missingdeps, 0);
	while (res != -2 && AST_VECTOR_SIZE(resources)) {
		struct ast_module *mod = AST_VECTOR_REMOVE(resources, 0, 1);
		enum ast_module_load_result lres;

		if (mod->flags.declined) {
			ast_debug(1, "%s is already declined, skipping\n", ast_module_name(mod));
			continue;
		}

retry_load:
		lres = start_resource_attempt(mod, mod_count);
		if (lres == AST_MODULE_LOAD_SUCCESS) {
			/* No missing dependencies, successful. */
			continue;
		}

		if (lres == AST_MODULE_LOAD_FAILURE) {
			res = -2;
			break;
		}

		if (lres == AST_MODULE_LOAD_DECLINE) {
			res = resource_list_recursive_decline(resources, mod, &printmissing);
			continue;
		}

		if (module_deps_missing_recursive(mod, &missingdeps)) {
			AST_VECTOR_RESET(&missingdeps, AST_VECTOR_ELEM_CLEANUP_NOOP);
			module_load_error("Failed to resolve dependencies for %s\n", ast_module_name(mod));
			res = resource_list_recursive_decline(resources, mod, &printmissing);
			continue;
		}

		if (!AST_VECTOR_SIZE(&missingdeps)) {
			module_load_error("%s load function returned an invalid result. "
				"This is a bug in the module.\n", ast_module_name(mod));
			/* Dependencies were met but the module failed to start and the result
			 * code was not AST_MODULE_LOAD_FAILURE or AST_MODULE_LOAD_DECLINE. */
			res = resource_list_recursive_decline(resources, mod, &printmissing);
			continue;
		}

		ast_debug(1, "%s has %d dependencies\n",
			ast_module_name(mod), (int)AST_VECTOR_SIZE(&missingdeps));
		while (AST_VECTOR_SIZE(&missingdeps)) {
			int didwork = 0;
			int i = 0;

			while (i < AST_VECTOR_SIZE(&missingdeps)) {
				struct ast_module *dep = AST_VECTOR_GET(&missingdeps, i);

				if (dep->flags.declined) {
					ast_debug(1, "%s tried to start %s but it's already declined\n",
						ast_module_name(mod), ast_module_name(dep));
					i++;
					continue;
				}

				ast_debug(1, "%s trying to start %s\n", ast_module_name(mod), ast_module_name(dep));
				lres = start_resource_attempt(dep, mod_count);
				if (lres == AST_MODULE_LOAD_SUCCESS) {
					ast_debug(1, "%s started %s\n", ast_module_name(mod), ast_module_name(dep));
					AST_VECTOR_REMOVE(&missingdeps, i, 1);
					AST_VECTOR_REMOVE_CMP_ORDERED(resources, dep,
						AST_VECTOR_ELEM_DEFAULT_CMP, AST_VECTOR_ELEM_CLEANUP_NOOP);
					didwork++;
					continue;
				}

				if (lres == AST_MODULE_LOAD_FAILURE) {
					module_load_error("Failed to load %s.\n", ast_module_name(dep));
					res = -2;
					goto exitpoint;
				}

				ast_debug(1, "%s failed to start %s\n", ast_module_name(mod), ast_module_name(dep));
				i++;
			}

			if (!didwork) {
				break;
			}
		}

		if (AST_VECTOR_SIZE(&missingdeps)) {
			if (!printmissing) {
				printmissing = ast_str_create(64);
			} else {
				ast_str_reset(printmissing);
			}

			if (printmissing) {
				struct ast_vector_const_string localdeps;

				AST_VECTOR_INIT(&localdeps, 0);
				module_deps_reference(mod, &localdeps);
				AST_VECTOR_CALLBACK_VOID(&localdeps, STR_APPEND_TEXT, &printmissing);
				AST_VECTOR_FREE(&localdeps);
			}

			module_load_error("Failed to load %s due to dependencies: %s.\n",
				ast_module_name(mod),
				printmissing ? ast_str_buffer(printmissing) : "allocation failure creating list");
			res = resource_list_recursive_decline(resources, mod, &printmissing);

			AST_VECTOR_RESET(&missingdeps, AST_VECTOR_ELEM_CLEANUP_NOOP);

			continue;
		}

		/* If we're here it means that we started with missingdeps and they're all loaded
		 * now.  It's impossible to reach this point a second time for the same module. */
		goto retry_load;
	}

exitpoint:
	ast_free(printmissing);
	AST_VECTOR_FREE(&missingdeps);

	return res;
}

/*! loads modules in order by load_pri, updates mod_count
	\return -1 on failure to load module, -2 on failure to load required module, otherwise 0
*/
static int load_resource_list(struct load_order *load_order, int *mod_count)
{
	struct module_vector module_priorities;
	struct load_order_entry *order;
	int attempt = 0;
	int count = 0;
	int res = 0;
	int didwork;
	int lasttry = 0;

	if (AST_VECTOR_INIT(&module_priorities, 500)) {
		ast_log(LOG_ERROR, "Failed to initialize module loader.\n");

		return -1;
	}

	while (res != -2) {
		didwork = 0;

		AST_LIST_TRAVERSE_SAFE_BEGIN(load_order, order, entry) {
			enum ast_module_load_result lres;

			/* Suppress log messages unless this is the last pass */
			lres = load_resource(order->resource, !lasttry, &module_priorities, order->required, order->preload);
			ast_debug(3, "PASS %d: %-46s %d\n", attempt, order->resource, lres);
			switch (lres) {
			case AST_MODULE_LOAD_SUCCESS:
			case AST_MODULE_LOAD_SKIP:
				/* We're supplying module_priorities so SUCCESS isn't possible but we
				 * still have to test for it.  SKIP is only used when we try to start a
				 * module that is missing dependencies. */
				break;
			case AST_MODULE_LOAD_DECLINE:
				res = -1;
				break;
			case AST_MODULE_LOAD_FAILURE:
				/* LOAD_FAILURE only happens for required modules */
				if (lasttry) {
					/* This run is just to print errors. */
					module_load_error("*** Failed to load module %s - Required\n", order->resource);
					fprintf(stderr, "*** Failed to load module %s - Required\n", order->resource);
					res =  -2;
				}
				break;
			case AST_MODULE_LOAD_PRIORITY:
				/* load_resource worked and the module was added to module_priorities */
				AST_LIST_REMOVE_CURRENT(entry);
				ast_free(order->resource);
				ast_free(order);
				didwork = 1;
				break;
			}
		}
		AST_LIST_TRAVERSE_SAFE_END;

		if (!didwork) {
			if (lasttry) {
				break;
			}
			/* We know the next try is going to fail, it's only being performed
			 * so we can print errors. */
			lasttry = 1;
		}
		attempt++;
	}

	if (res != -2) {
		res = start_resource_list(&module_priorities, &count);
	}

	if (mod_count) {
		*mod_count += count;
	}
	AST_VECTOR_FREE(&module_priorities);

	return res;
}

static int loader_builtin_init(struct load_order *load_order)
{
	struct ast_module *mod;

	/*
	 * All built-in modules have registered the first time, now it's time to complete
	 * the registration and add them to the priority list.
	 */
	loader_ready = 1;

	while ((resource_being_loaded = AST_DLLIST_REMOVE_HEAD(&builtin_module_list, entry))) {
		/* ast_module_register doesn't finish when first run by built-in modules. */
		ast_module_register(resource_being_loaded->info);
	}

	/* Add all built-in modules to the load order. */
	AST_DLLIST_TRAVERSE(&module_list, mod, entry) {
		if (!mod->flags.builtin) {
			continue;
		}

		/* Parse dependendencies from mod->info. */
		if (module_post_register(mod)) {
			return -1;
		}

		/* Built-in modules are not preloaded, most have an early load priority. */
		if (!add_to_load_order(mod->resource, load_order, 0, 0, 1)) {
			return -1;
		}
	}

	return 0;
}

static int loader_config_init(struct load_order *load_order)
{
	int res = -1;
	struct load_order_entry *order;
	struct ast_config *cfg;
	struct ast_variable *v;
	struct ast_flags config_flags = { 0 };

	cfg = ast_config_load2(AST_MODULE_CONFIG, "" /* core, can't reload */, config_flags);
	if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
		ast_log(LOG_WARNING, "'%s' invalid or missing.\n", AST_MODULE_CONFIG);

		return -1;
	}

	/* first, find all the modules we have been explicitly requested to load */
	for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
		int required;
		int preload = 0;

		if (!strncasecmp(v->name, "preload", strlen("preload"))) {
			preload = 1;
			if (!strcasecmp(v->name, "preload")) {
				required = 0;
			} else if (!strcasecmp(v->name, "preload-require")) {
				required = 1;
			} else {
				ast_log(LOG_ERROR, "Unknown configuration option '%s'", v->name);
				goto done;
			}
		} else if (!strcasecmp(v->name, "load")) {
			required = 0;
		} else if (!strcasecmp(v->name, "require")) {
			required = 1;
		} else if (!strcasecmp(v->name, "noload") || !strcasecmp(v->name, "autoload")) {
			continue;
		} else {
			ast_log(LOG_ERROR, "Unknown configuration option '%s'", v->name);
			goto done;
		}

		if (required) {
			ast_debug(2, "Adding module to required list: %s (%s)\n", v->value, v->name);
		}

		if (!add_to_load_order(v->value, load_order, required, preload, 0)) {
			goto done;
		}
	}

	/* check if 'autoload' is on */
	if (ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
		/* if we are allowed to load dynamic modules, scan the directory for
		   for all available modules and add them as well */
		DIR *dir = opendir(ast_config_AST_MODULE_DIR);
		struct dirent *dirent;

		if (dir) {
			while ((dirent = readdir(dir))) {
				int ld = strlen(dirent->d_name);

				/* Must end in .so to load it.  */

				if (ld < 4)
					continue;

				if (strcasecmp(dirent->d_name + ld - 3, ".so"))
					continue;

				/* if there is already a module by this name in the module_list,
				   skip this file */
				if (find_resource(dirent->d_name, 0))
					continue;

				if (!add_to_load_order(dirent->d_name, load_order, 0, 0, 0)) {
					closedir(dir);
					goto done;
				}
			}

			closedir(dir);
		} else {
			ast_log(LOG_ERROR, "Unable to open modules directory '%s'.\n", ast_config_AST_MODULE_DIR);
			goto done;
		}
	}

	/* now scan the config for any modules we are prohibited from loading and
	   remove them from the load order */
	for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
		size_t baselen;

		if (strcasecmp(v->name, "noload")) {
			continue;
		}

		baselen = resource_name_baselen(v->value);
		AST_LIST_TRAVERSE_SAFE_BEGIN(load_order, order, entry) {
			if (!resource_name_match(v->value, baselen, order->resource)) {
				if (order->builtin) {
					ast_log(LOG_ERROR, "%s is a built-in module, you cannot specify 'noload'.\n", v->value);
					goto done;
				}

				if (order->required) {
					ast_log(LOG_ERROR, "%s is configured with '%s' and 'noload', this is impossible.\n",
						v->value, order->preload ? "preload-require" : "require");
					goto done;
				}
				AST_LIST_REMOVE_CURRENT(entry);
				ast_free(order->resource);
				ast_free(order);
			}
		}
		AST_LIST_TRAVERSE_SAFE_END;
	}

	res = 0;
done:
	ast_config_destroy(cfg);

	return res;
}

int load_modules(void)
{
	struct load_order_entry *order;
	unsigned int load_count;
	struct load_order load_order;
	int res = 0;
	int modulecount = 0;
	int i;

	ast_verb(1, "Asterisk Dynamic Loader Starting:\n");

	AST_LIST_HEAD_INIT_NOLOCK(&load_order);
	AST_DLLIST_LOCK(&module_list);

	AST_VECTOR_INIT(&startup_errors, 0);
	startup_error_builder = ast_str_create(64);

	res = loader_builtin_init(&load_order);
	if (res) {
		goto done;
	}

	res = loader_config_init(&load_order);
	if (res) {
		goto done;
	}

	load_count = 0;
	AST_LIST_TRAVERSE(&load_order, order, entry)
		load_count++;

	if (load_count)
		ast_log(LOG_NOTICE, "%u modules will be loaded.\n", load_count);

	res = load_resource_list(&load_order, &modulecount);
	if (res == -1) {
		ast_log(LOG_WARNING, "Some non-required modules failed to load.\n");
		res = 0;
	}

done:
	while ((order = AST_LIST_REMOVE_HEAD(&load_order, entry))) {
		ast_free(order->resource);
		ast_free(order);
	}

	AST_DLLIST_UNLOCK(&module_list);

	for (i = 0; i < AST_VECTOR_SIZE(&startup_errors); i++) {
		char *str = AST_VECTOR_GET(&startup_errors, i);

		ast_log(LOG_ERROR, "%s", str);
		ast_free(str);
	}
	AST_VECTOR_FREE(&startup_errors);

	ast_free(startup_error_builder);
	startup_error_builder = NULL;

	return res;
}

void ast_update_use_count(void)
{
	/* Notify any module monitors that the use count for a
	   resource has changed */
	struct loadupdate *m;

	AST_LIST_LOCK(&updaters);
	AST_LIST_TRAVERSE(&updaters, m, entry)
		m->updater();
	AST_LIST_UNLOCK(&updaters);
}

/*!
 * \internal
 * \brief Build an alpha sorted list of modules.
 *
 * \param alpha_module_list Pointer to uninitialized module_vector.
 *
 * This function always initializes alpha_module_list.
 *
 * \pre module_list must be locked.
 */
static int alpha_module_list_create(struct module_vector *alpha_module_list)
{
	struct ast_module *cur;

	if (AST_VECTOR_INIT(alpha_module_list, 32)) {
		return -1;
	}

	AST_DLLIST_TRAVERSE(&module_list, cur, entry) {
		if (AST_VECTOR_ADD_SORTED(alpha_module_list, cur, module_vector_strcasecmp)) {
			return -1;
		}
	}

	return 0;
}

int ast_update_module_list(int (*modentry)(const char *module, const char *description,
                                           int usecnt, const char *status, const char *like,
										   enum ast_module_support_level support_level),
                           const char *like)
{
	int total_mod_loaded = 0;
	struct module_vector alpha_module_list;

	AST_DLLIST_LOCK(&module_list);

	if (!alpha_module_list_create(&alpha_module_list)) {
		int idx;

		for (idx = 0; idx < AST_VECTOR_SIZE(&alpha_module_list); idx++) {
			struct ast_module *cur = AST_VECTOR_GET(&alpha_module_list, idx);

			total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount,
				cur->flags.running ? "Running" : "Not Running", like, cur->info->support_level);
		}
	}

	AST_DLLIST_UNLOCK(&module_list);
	AST_VECTOR_FREE(&alpha_module_list);

	return total_mod_loaded;
}

int ast_update_module_list_data(int (*modentry)(const char *module, const char *description,
                                                int usecnt, const char *status, const char *like,
                                                enum ast_module_support_level support_level,
                                                void *data),
                                const char *like, void *data)
{
	int total_mod_loaded = 0;
	struct module_vector alpha_module_list;

	AST_DLLIST_LOCK(&module_list);

	if (!alpha_module_list_create(&alpha_module_list)) {
		int idx;

		for (idx = 0; idx < AST_VECTOR_SIZE(&alpha_module_list); idx++) {
			struct ast_module *cur = AST_VECTOR_GET(&alpha_module_list, idx);

			total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount,
				cur->flags.running? "Running" : "Not Running", like, cur->info->support_level, data);
		}
	}

	AST_DLLIST_UNLOCK(&module_list);
	AST_VECTOR_FREE(&alpha_module_list);

	return total_mod_loaded;
}

int ast_update_module_list_condition(int (*modentry)(const char *module, const char *description,
                                                     int usecnt, const char *status,
                                                     const char *like,
                                                     enum ast_module_support_level support_level,
                                                     void *data, const char *condition),
                                     const char *like, void *data, const char *condition)
{
	int conditions_met = 0;
	struct module_vector alpha_module_list;

	AST_DLLIST_LOCK(&module_list);

	if (!alpha_module_list_create(&alpha_module_list)) {
		int idx;

		for (idx = 0; idx < AST_VECTOR_SIZE(&alpha_module_list); idx++) {
			struct ast_module *cur = AST_VECTOR_GET(&alpha_module_list, idx);

			conditions_met += modentry(cur->resource, cur->info->description, cur->usecount,
				cur->flags.running? "Running" : "Not Running", like, cur->info->support_level, data,
				condition);
		}
	}

	AST_DLLIST_UNLOCK(&module_list);
	AST_VECTOR_FREE(&alpha_module_list);

	return conditions_met;
}

/*! \brief Check if module exists */
int ast_module_check(const char *name)
{
	struct ast_module *cur;

	if (ast_strlen_zero(name))
		return 0;       /* FALSE */

	cur = find_resource(name, 1);

	return (cur != NULL);
}


int ast_loader_register(int (*v)(void))
{
	struct loadupdate *tmp;

	if (!(tmp = ast_malloc(sizeof(*tmp))))
		return -1;

	tmp->updater = v;
	AST_LIST_LOCK(&updaters);
	AST_LIST_INSERT_HEAD(&updaters, tmp, entry);
	AST_LIST_UNLOCK(&updaters);

	return 0;
}

int ast_loader_unregister(int (*v)(void))
{
	struct loadupdate *cur;

	AST_LIST_LOCK(&updaters);
	AST_LIST_TRAVERSE_SAFE_BEGIN(&updaters, cur, entry) {
		if (cur->updater == v)	{
			AST_LIST_REMOVE_CURRENT(entry);
			break;
		}
	}
	AST_LIST_TRAVERSE_SAFE_END;
	AST_LIST_UNLOCK(&updaters);

	return cur ? 0 : -1;
}

struct ast_module *__ast_module_ref(struct ast_module *mod, const char *file, int line, const char *func)
{
	if (!mod) {
		return NULL;
	}

	if (mod->ref_debug) {
		__ao2_ref(mod->ref_debug, +1, "", file, line, func);
	}

	ast_atomic_fetchadd_int(&mod->usecount, +1);
	ast_update_use_count();

	return mod;
}

struct ast_module *__ast_module_running_ref(struct ast_module *mod,
	const char *file, int line, const char *func)
{
	if (!mod || !mod->flags.running) {
		return NULL;
	}

	return __ast_module_ref(mod, file, line, func);
}

void __ast_module_shutdown_ref(struct ast_module *mod, const char *file, int line, const char *func)
{
	if (!mod || mod->flags.keepuntilshutdown) {
		return;
	}

	__ast_module_ref(mod, file, line, func);
	mod->flags.keepuntilshutdown = 1;
}

void __ast_module_unref(struct ast_module *mod, const char *file, int line, const char *func)
{
	if (!mod) {
		return;
	}

	if (mod->ref_debug) {
		__ao2_ref(mod->ref_debug, -1, "", file, line, func);
	}

	ast_atomic_fetchadd_int(&mod->usecount, -1);
	ast_update_use_count();
}

const char *support_level_map [] = {
	[AST_MODULE_SUPPORT_UNKNOWN] = "unknown",
	[AST_MODULE_SUPPORT_CORE] = "core",
	[AST_MODULE_SUPPORT_EXTENDED] = "extended",
	[AST_MODULE_SUPPORT_DEPRECATED] = "deprecated",
};

const char *ast_module_support_level_to_string(enum ast_module_support_level support_level)
{
	return support_level_map[support_level];
}