summaryrefslogtreecommitdiff
path: root/kernel.c
blob: 082eba29aff58b02cdb790fe3d9d58dbd94f72d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
/*
 * nvidia-installer: A tool for installing NVIDIA software packages on
 * Unix and Linux systems.
 *
 * Copyright (C) 2003-2009 NVIDIA Corporation
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions 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, see <http://www.gnu.org/licenses>.
 */

#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <ctype.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <limits.h>
#include <fts.h>
#include <dlfcn.h>
#include <libkmod.h>

#include "nvidia-installer.h"
#include "kernel.h"
#include "user-interface.h"
#include "files.h"
#include "misc.h"
#include "precompiled.h"
#include "snarf.h"
#include "crc.h"

/* local prototypes */

static char *default_kernel_module_installation_path(Options *op);
static char *default_kernel_source_path(Options *op);
static char *find_module_substring(char *string, const char *substring);
static int check_for_loaded_kernel_module(Options *op, const char *);
static void check_for_warning_messages(Options *op);
static int rmmod_kernel_module(Options *op, const char *);
static PrecompiledInfo *download_updated_kernel_interface(Options*, Package*,
                                                          const char*, 
                                                          char* const*);
static int fbdev_check(Options *op, Package *p);
static int xen_check(Options *op, Package *p);
static int preempt_rt_check(Options *op, Package *p);

static PrecompiledInfo *scan_dir(Options *op, Package *p,
                                 const char *directory_name,
                                 const char *proc_version_string,
                                 char *const *search_filelist);

static char *build_distro_precompiled_kernel_interface_dir(Options *op);
static char *convert_include_path_to_source_path(const char *inc);
static char *get_machine_arch(Options *op);
static int init_libkmod(void);
static void close_libkmod(void);
static int run_conftest(Options *op, Package *p, const char *args,
                        char **result);
static void replace_zero(char* filename, int i);

/* libkmod handle and function pointers */
static void *libkmod = NULL;
static struct kmod_ctx* (*lkmod_new)(const char*, const char* const*) = NULL;
static struct kmod_ctx* (*lkmod_unref)(struct kmod_ctx*) = NULL;
static struct kmod_module* (*lkmod_module_unref)(struct kmod_module *) = NULL;
static int (*lkmod_module_new_from_path)(struct kmod_ctx*, const char*,
             struct kmod_module**) = NULL;
static int (*lkmod_module_insert_module)(struct kmod_module*, unsigned int,
             const char*) = NULL;
static void free_search_filelist(char **);

/*
 * Message text that is used by several error messages.
 */

static const char install_your_kernel_source[] = 
"Please make sure you have installed the kernel source files for "
"your kernel and that they are properly configured; on Red Hat "
"Linux systems, for example, be sure you have the 'kernel-source' "
"or 'kernel-devel' RPM installed.  If you know the correct kernel "
"source files are installed, you may specify the kernel source "
"path with the '--kernel-source-path' command line option.";

 


/*
 * determine_kernel_module_installation_path() - get the installation
 * path for the kernel module.  The order is:
 *
 * - if op->kernel_module_installation_path is non-NULL, then it must
 *   have been initialized by the commandline parser, and therefore we
 *   should obey that (so just return).
 *
 * - get the default installation path
 *
 * - if in expert mode, ask the user, and use what they gave, if
 *   non-NULL
 */

int determine_kernel_module_installation_path(Options *op)
{
    char *result;
    int count = 0;
    
    if (op->kernel_module_installation_path) return TRUE;
    
    op->kernel_module_installation_path =
        default_kernel_module_installation_path(op);
    
    if (!op->kernel_module_installation_path) return FALSE;

    if (op->expert) {

    ask_for_kernel_install_path:

        result = ui_get_input(op, op->kernel_module_installation_path,
                              "Kernel module installation path");
        if (result && result[0]) {
            free(op->kernel_module_installation_path);
            op->kernel_module_installation_path = result;
            if (!confirm_path(op, op->kernel_module_installation_path)) {
                return FALSE;
            }
        } else {
            if (result) free(result);
            
            if (++count < NUM_TIMES_QUESTIONS_ASKED) {
                ui_warn(op, "Invalid kernel module installation path.");
                goto ask_for_kernel_install_path;
            } else {
                ui_error(op, "Unable to determine kernel module "
                         "installation path.");

                return FALSE;
            }
        }
    }

    if (!mkdir_recursive(op, op->kernel_module_installation_path, 0755))
        return FALSE;

    ui_expert(op, "Kernel module installation path: %s",
              op->kernel_module_installation_path);

    return TRUE;
    
} /* determine_kernel_module_installation_path() */



/*
 * run_conftest() - run conftest.sh with the given additional arguments; pass
 * the result back to the caller. Returns TRUE on success, or FALSE on failure.
 */

static int run_conftest(Options *op, Package *p, const char *args, char **result)
{
    char *cmd, *arch;
    int ret;

    if (result)
        *result = NULL;

    arch = get_machine_arch(op);
    if (!arch)
        return FALSE;

    cmd = nvstrcat("sh \"", p->kernel_module_build_directory,
                   "/conftest.sh\" \"", op->utils[CC], "\" \"", op->utils[CC],
                   "\" \"", arch, "\" \"", op->kernel_source_path, "\" \"",
                   op->kernel_output_path, "\" ", args, NULL);

    ret = run_command(op, cmd, result, FALSE, 0, TRUE);
    nvfree(cmd);

    return ret == 0;

} /* run_conftest() */



/*
 * determine_kernel_source_path() - find the qualified path to the
 * kernel source tree.  This is called from install_from_cwd() if we
 * need to compile the kernel interface files.  Assigns
 * op->kernel_source_path and returns TRUE if successful.  Returns
 * FALSE if no kernel source tree was found.
 */

int determine_kernel_source_path(Options *op, Package *p)
{
    char *result;
    char *source_files[2], *source_path;
    int ret, count = 0;
    
    /* determine the kernel source path */
    
    op->kernel_source_path = default_kernel_source_path(op);
    
    if (op->expert) {
        
    ask_for_kernel_source_path:
        
        result = ui_get_input(op, op->kernel_source_path,
                              "Kernel source path");
        if (result && result[0]) {
            if (!directory_exists(op, result)) {
                ui_warn(op, "Kernel source path '%s' does not exist.",
                        result);
                free(result);
                
                if (++count < NUM_TIMES_QUESTIONS_ASKED) {
                    goto ask_for_kernel_source_path;
                } else {
                    op->kernel_source_path = NULL;
                }
            } else {
                op->kernel_source_path = result;
            }
        } else {
            ui_warn(op, "Invalid kernel source path.");
            if (result) free(result);
            
            if (++count < NUM_TIMES_QUESTIONS_ASKED) {
                goto ask_for_kernel_source_path;
            } else {
                op->kernel_source_path = NULL;
            }
        }
    }

    /* if we STILL don't have a kernel source path, give up */
    
    if (!op->kernel_source_path) {
        ui_error(op, "Unable to find the kernel source tree for the "
                 "currently running kernel.  %s", install_your_kernel_source);
        
        /*
         * I suppose we could ask them here for the kernel source
         * path, but we've already given users multiple methods of
         * specifying their kernel source tree.
         */
        
        return FALSE;
    }

    /* reject /usr as an invalid kernel source path */

    if (!strcmp(op->kernel_source_path, "/usr") ||
            !strcmp(op->kernel_source_path, "/usr/")) {
        ui_error (op, "The kernel source path '%s' is invalid.  %s",
                  op->kernel_source_path, install_your_kernel_source);
        op->kernel_source_path = NULL;
        return FALSE;
    }

    /* check that the kernel source path exists */

    if (!directory_exists(op, op->kernel_source_path)) {
        ui_error (op, "The kernel source path '%s' does not exist.  %s",
                  op->kernel_source_path, install_your_kernel_source);
        op->kernel_source_path = NULL;
        return FALSE;
    }

    /* check that <path>/include/linux/kernel.h exists */

    result = nvstrcat(op->kernel_source_path, "/include/linux/kernel.h", NULL);
    if (access(result, F_OK) == -1) {
        ui_error(op, "The kernel header file '%s' does not exist.  "
                 "The most likely reason for this is that the kernel source "
                 "path '%s' is incorrect.  %s", result,
                 op->kernel_source_path, install_your_kernel_source);
        free(result);
        return FALSE;
    }
    free(result);

    if (!determine_kernel_output_path(op)) return FALSE;

    ret = run_conftest(op, p, "get_uname", &result);

    if (!ret) {
        ui_error(op, "Unable to determine the version of the kernel "
                 "sources located in '%s'.  %s",
                 op->kernel_source_path, install_your_kernel_source);
        free(result);
        return FALSE;
    }

    if (strncmp(result, "2.4", 3) == 0) {
        source_files[0] = nvstrcat(op->kernel_source_path,
                                   "/include/linux/version.h", NULL);
        source_files[1] = NULL;
        source_path = op->kernel_source_path;
    } else {
        source_files[0] = nvstrcat(op->kernel_output_path,
                                   "/include/linux/version.h", NULL);
        source_files[1] = nvstrcat(op->kernel_output_path,
                                   "/include/generated/uapi/linux/version.h",
                                   NULL);
        source_path = op->kernel_output_path;
    }
    free(result);

    if (access(source_files[0], F_OK) != 0) {
        if (!source_files[1]) {
            ui_error(op, "The kernel header file '%s' does not exist.  "
                     "The most likely reason for this is that the kernel "
                     "source files in '%s' have not been configured.",
                     source_files[0], source_path);
            return FALSE;
        } else if (access(source_files[1], F_OK) != 0) {
            ui_error(op, "Neither the '%s' nor the '%s' kernel header "
                     "file exists.  The most likely reason for this "
                     "is that the kernel source files in '%s' have not been "
                     "configured.",
                     source_files[0], source_files[1], source_path);
            return FALSE;
        }
    }

    /* OK, we seem to have a path to a configured kernel source tree */
    
    ui_log(op, "Kernel source path: '%s'\n", op->kernel_source_path);
    ui_log(op, "Kernel output path: '%s'\n", op->kernel_output_path);
    
    return TRUE;
    
} /* determine_kernel_source_path() */


/*
 * determine_kernel_output_path() - determine the kernel output
 * path; unless specified, the kernel output path is assumed to be
 * the same as the kernel source path.
 */

int determine_kernel_output_path(Options *op)
{
    char *str, *tmp;
    int len;

    /* check --kernel-output-path */

    if (op->kernel_output_path) {
        ui_log(op, "Using the kernel output path '%s' as specified by the "
               "'--kernel-output-path' commandline option.",
               op->kernel_output_path);

        if (!directory_exists(op, op->kernel_output_path)) {
            ui_error(op, "The kernel output path '%s' does not exist.",
                     op->kernel_output_path);
            op->kernel_output_path = NULL;
            return FALSE;
        }

        return TRUE;
    }

    /* check SYSOUT */

    str = getenv("SYSOUT");
    if (str) {
        ui_log(op, "Using the kernel output path '%s', as specified by the "
               "SYSOUT environment variable.", str);
        op->kernel_output_path = str;

        if (!directory_exists(op, op->kernel_output_path)) {
            ui_error(op, "The kernel output path '%s' does not exist.",
                     op->kernel_output_path);
            op->kernel_output_path = NULL;
            return FALSE;
        }

        return TRUE;
    }

    /* check /lib/modules/`uname -r`/{source,build} */

    tmp = get_kernel_name(op);

    if (tmp) {
        str = nvstrcat("/lib/modules/", tmp, "/source", NULL);
        len = strlen(str);

        if (!strncmp(op->kernel_source_path, str, len)) {
            nvfree(str);
            str = nvstrcat("/lib/modules/", tmp, "/build", NULL);

            if (directory_exists(op, str)) {
                op->kernel_output_path = str;
                return TRUE;
            }
        }

        nvfree(str);
    }

    op->kernel_output_path = op->kernel_source_path;
    return TRUE;
}


/*
 * attach_signature() - If we have a detached signature, verify the checksum of
 * the linked module and append the signature.
 */
static int attach_signature(Options *op, Package *p,
                            const PrecompiledFileInfo *fileInfo,
                            const char *module_name) {
    uint32 actual_crc;
    char *module_path;
    int ret = FALSE, command_ret;

    const char *choices[2] = {
        "Install unsigned kernel module",
        "Abort installation"
    };

    ui_log(op, "Attaching module signature to linked kernel module.");

    module_path = nvstrcat(p->kernel_module_build_directory, "/",
                           fileInfo->target_directory, "/", module_name, NULL);

    command_ret = verify_crc(op, module_path, fileInfo->linked_module_crc,
                             &actual_crc);

    if (command_ret) {
        FILE *module_file;

        module_file = fopen(module_path, "a+");

        if (module_file && fileInfo->signature_size) {
            command_ret = fwrite(fileInfo->signature, 1,
                                 fileInfo->signature_size, module_file);
            if (command_ret != fileInfo->signature_size) {
                goto attach_done;
            }

            op->kernel_module_signed = ret = !ferror(module_file);
attach_done:
            fclose(module_file);
        } else {
            ret = (ui_multiple_choice(op, choices, 2, 1,
                                      "A detached signature was included with "
                                      "the precompiled interface, but opening "
                                      "the linked kernel module and/or the "
                                      "signature file failed.\n\nThe detached "
                                      "signature will not be added; would you "
                                      "still like to install the unsigned "
                                      "kernel module?") == 0);
        }
    } else {
        ret = (ui_multiple_choice(op, choices, 2, 1,
                                  "A detached signature was included with the "
                                  "precompiled interface, but the checksum of "
                                  "the linked kernel module (%d) did not match "
                                  "the checksum of the the kernel module for "
                                  "which the detached signature was generated "
                                  "(%d).\n\nThis can happen if the linker on "
                                  "the installation target system is not the "
                                  "same as the linker on the system that built "
                                  "the precompiled interface.\n\nThe detached "
                                  "signature will not be added; would you "
                                  "still like to install the unsigned kernel "
                                  "module?", actual_crc,
                                  fileInfo->linked_module_crc) == 0);
    }

    if (ret) {
        if(op->kernel_module_signed) {
            ui_log(op, "Signature attached successfully.");
        } else {
            ui_log(op, "Signature not attached.");
        }
    } else {
        ui_error(op, "Failed to attach signature.");
    }

    nvfree(module_path);
    return ret;
} /* attach_signature() */


/*
 * Look for the presence of char '0' in filename and replace
 * it with the integer value passed as input. This is used
 * especially for multiple kernel module builds.
 */

static void replace_zero(char *filename, int i)
{
    char *name;

    if (i < 0 || i > 9) return;

    name = strrchr(filename, '0');
    if (name) *name = *name + i;
}


/*
 * link_kernel_module() - link the prebuilt kernel interface against
 * the binary-only core of the kernel module.  This results in a
 * complete kernel module, ready for installation.
 *
 *
 * ld -r -o nvidia.o nv-linux.o nv-kernel.o
 */

int link_kernel_module(Options *op, Package *p, const char *build_directory,
                       const PrecompiledFileInfo *fileInfo)
{
    char *cmd, *result;
    int ret;
    uint32 attrmask;

    if (fileInfo->type != PRECOMPILED_FILE_TYPE_INTERFACE) {
        ui_error(op, "The file does not appear to be a valid precompiled "
                 "kernel interface.");
        return FALSE;
    }

    ret = precompiled_file_unpack(op, fileInfo, build_directory);
    if (!ret) {
        ui_error(op, "Failed to unpack the precompiled interface.");
        return FALSE;
    }

    cmd = nvstrcat("cd ", build_directory, "/", fileInfo->target_directory,
                   "; ", op->utils[LD], " ", LD_OPTIONS, " -o ",
                   fileInfo->linked_module_name, " ",
                   fileInfo->name, " ", fileInfo->core_object_name, NULL);

    ret = run_command(op, cmd, &result, TRUE, 0, TRUE);

    free(cmd);

    if (ret != 0) {
        ui_error(op, "Unable to link kernel module.");
        return FALSE;
    }

    ui_log(op, "Kernel module linked successfully.");

    attrmask = PRECOMPILED_ATTR(DETACHED_SIGNATURE) |
               PRECOMPILED_ATTR(LINKED_MODULE_CRC);

    if ((fileInfo->attributes & attrmask) == attrmask) {
        return attach_signature(op, p, fileInfo,
                                fileInfo->linked_module_name);
    }

    return TRUE;
   
} /* link_kernel_module() */



static int build_kernel_module_helper(Options *op, const char *dir,
                                      const char *module, int num_instances)
{
    int ret;
    char *instances = NULL, *cmd, *tmp;

    tmp = op->multiple_kernel_modules && num_instances ?
              nvasprintf("%d", num_instances) : NULL;
    instances = nvstrcat(" NV_BUILD_MODULE_INSTANCES=", tmp, NULL);
    nvfree(tmp);

    tmp = nvasprintf("Building %s kernel module:", module);
    ui_status_begin(op, tmp, "Building");
    nvfree(tmp);

    cmd = nvstrcat("cd ", dir, "; ", op->utils[MAKE], " module",
                   " SYSSRC=", op->kernel_source_path,
                   " SYSOUT=", op->kernel_output_path,
                   instances, NULL);
    nvfree(instances);

    ret = run_command(op, cmd, NULL, TRUE, 25, TRUE);

    nvfree(cmd);

    if (ret != 0) {
        ui_status_end(op, "Error.");
        ui_error(op, "Unable to build the %s kernel module.", module);
        /* XXX need more descriptive error message */

        return FALSE;
    }

    ui_status_end(op, "done.");

    return TRUE;
}


static int check_file(Options *op, const char *dir, const char *filename,
                      const char *modname)
{
    int ret;
    char *path;

    path = nvstrcat(dir, "/", filename, NULL);
    ret = access(path, F_OK);
    nvfree(path);

    if (ret == -1) {
        ui_error(op, "The NVIDIA %s module was not created.", modname);
    }

    return ret != -1;
}


/*
 * build_kernel_module() - determine the kernel include directory,
 * copy the kernel module source files into a temporary directory, and
 * compile nvidia.o.
 */

int build_kernel_module(Options *op, Package *p)
{
    char *result, *cmd;
    int ret;

    /*
     * touch all the files in the build directory to avoid make time
     * skew messages
     */
    
    touch_directory(op, p->kernel_module_build_directory);
    
    /*
     * Check if conftest.sh can determine the Makefile, there's
     * no hope for the make rules if this fails.
     */
    ret = run_conftest(op, p, "select_makefile just_msg", &result);

    if (!ret) {
        if (result)
            ui_error(op, "%s", result); /* display conftest.sh's error message */
        nvfree(result);
        return FALSE;
    }

    if (!fbdev_check(op, p)) return FALSE;
    if (!xen_check(op, p)) return FALSE;
    if (!preempt_rt_check(op, p)) return FALSE;

    ui_log(op, "Cleaning kernel module build directory.");
    
    cmd = nvstrcat("cd ", p->kernel_module_build_directory, "; ",
                   op->utils[MAKE], " clean", NULL);

    ret = run_command(op, cmd, &result, TRUE, 0, TRUE);
    free(result);
    free(cmd);

    ret = build_kernel_module_helper(op, p->kernel_module_build_directory,
                                     "NVIDIA", op->num_kernel_modules);

    if (!ret) {
        return FALSE;
    }
    
    /* check that the frontend file actually exists */
    if (op->multiple_kernel_modules) {
        if (!check_file(op, p->kernel_module_build_directory,
                        p->kernel_frontend_module_filename, "frontend")) {
            return FALSE;
        }
    }

    /* check that the file actually exists */
    if (!check_file(op, p->kernel_module_build_directory,
                    p->kernel_module_filename, "kernel")) {
        return FALSE;
    }

    /*
     * Build the UVM kernel module. The build directory from the previous kernel
     * module build must not be cleaned first, as the UVM kernel module will
     * depend on the Module.symvers file produced by that build.
     */

    if (op->install_uvm) {
        ret = build_kernel_module_helper(op, p->uvm_module_build_directory,
                                         "Unified Memory", 0);

        ret = ret && check_file(op, p->uvm_module_build_directory,
                                p->uvm_kernel_module_filename, "Unified Memory");

        if (!ret) {
            ui_error(op, "The Unified Memory kernel module failed to build. "
                     "This kernel module is required for the proper operation "
                     "of CUDA. If you do not need to use CUDA, you can try to "
                     "install this driver package again with the "
                     "'--no-unified-memory' option.");
            return FALSE;
        }
    }

    ui_log(op, "Kernel module compilation complete.");

    return TRUE;
    
} /* build_kernel_module() */



/*
 * sign_kernel_module() - sign the kernel module. The caller is responsible
 * for ensuring that the kernel module is already built successfully and that
 * op->module_signing_{secret,public}_key are set.
 */
int sign_kernel_module(Options *op, const char *build_directory, 
                       const char *module_suffix, int status) {
    char *cmd, *mod_sign_cmd, *mod_sign_hash;
    int ret, success;
    char *build_module_instances_parameter;

    /* if module_signing_script isn't set, then set mod_sign_cmd below to end
     * the nvstrcat() that builds cmd early. */
    mod_sign_cmd = op->module_signing_script ?
                   nvstrcat(" mod_sign_cmd=", op->module_signing_script, NULL) :
                   NULL;

    mod_sign_hash = op->module_signing_hash ?
                    nvstrcat(" CONFIG_MODULE_SIG_HASH=",
                             op->module_signing_hash, NULL) :
                    NULL;

    if (status) {
        ui_status_begin(op, "Signing kernel module:", "Signing");
    }

    if (op->multiple_kernel_modules) {
        build_module_instances_parameter = 
                                 nvasprintf(" NV_BUILD_MODULE_INSTANCES=%d",
                                            NV_MAX_MODULE_INSTANCES);
    }
    else {
        build_module_instances_parameter = nvstrdup("");
    }

    cmd = nvstrcat("cd ", build_directory, "; ", op->utils[MAKE], " module-sign"
                   " SYSSRC=", op->kernel_source_path,
                   " SYSOUT=", op->kernel_output_path,
                   " MODSECKEY=", op->module_signing_secret_key,
                   " MODPUBKEY=", op->module_signing_public_key,
                   " NV_MODULE_SUFFIX=", module_suffix,
                   build_module_instances_parameter,
                   mod_sign_cmd ? mod_sign_cmd : "",
                   mod_sign_hash ? mod_sign_hash : "", NULL);

    ret = run_command(op, cmd, NULL, TRUE, 20 /* XXX */, TRUE);
    success = ret == 0;

    nvfree(mod_sign_hash);
    nvfree(mod_sign_cmd);
    nvfree(cmd);
    nvfree(build_module_instances_parameter);

    if (status) {
        ui_status_end(op, success ? "done." : "Failed to sign kernel module.");
    } else {
        ui_log(op, success ? "Signed kernel module." : "Module signing failed");
    }
    op->kernel_module_signed = success;
    return success;
} /* sign_kernel_module */



/*
 * create_detached_signature() - Link a precompiled interface into a module,
 * sign the resulting linked module, and store a CRC for the linked, unsigned
 * module and the detached signature in the provided PrecompiledFileInfo record.
 */
static int create_detached_signature(Options *op, Package *p,
                                     const char *build_dir,
                                     PrecompiledFileInfo *fileInfo,
                                     const char *module_suffix,
                                     const char *module_filename)
{
    int ret, command_ret;
    struct stat st;
    char *module_path = NULL, *error = NULL, *target_dir = NULL;

    ui_status_begin(op, "Creating a detached signature for the linked "
                    "kernel module:", "Linking module");

    ret = link_kernel_module(op, p, build_dir, fileInfo);

    if (!ret) {
        ui_error(op, "Failed to link a kernel module for signing.");
        goto done;
    }

    target_dir = nvstrcat(build_dir, "/", fileInfo->target_directory, NULL);
    module_path = nvstrcat(target_dir, "/", module_filename, NULL);
    command_ret = stat(module_path, &st);

    if (command_ret != 0) {
        ret = FALSE;
        error = "Unable to determine size of linked module.";
        goto done;
    }

    ui_status_update(op, .25, "Generating module checksum");

    fileInfo->linked_module_crc = compute_crc(op, module_path);
    fileInfo->attributes |= PRECOMPILED_ATTR(LINKED_MODULE_CRC);

    ui_status_update(op, .50, "Signing linked module");

    ret = sign_kernel_module(op, target_dir, module_suffix, FALSE);

    if (!ret) {
        error = "Failed to sign the linked kernel module.";
        goto done;
    }

    ui_status_update(op, .75, "Detaching module signature");

    fileInfo->signature_size = byte_tail(module_path, st.st_size,
                                         &(fileInfo->signature));

    if (!(fileInfo->signature) || fileInfo->signature_size == 0) {
        error = "Failed to detach the module signature";
        goto done;
    }

    fileInfo->attributes |= PRECOMPILED_ATTR(DETACHED_SIGNATURE);

done:
    if (ret) {
        ui_status_end(op, "done.");
    } else {
        ui_status_end(op, "Error.");
        if (error) {
            ui_error(op, "%s", error);
        }
    }

    nvfree(module_path);
    nvfree(target_dir);
    return ret;
} /* create_detached_signature() */


/*
 * build_kernel_interface_file() - build the kernel interface(s).
 * Returns true if the build was successful, or false on error.
 *
 * This is done by copying the sources to a temporary working
 * directory and building the kernel interface in that directory.
 */

static int build_kernel_interface_file(Options *op, const char *tmpdir,
                                       PrecompiledFileInfo *fileInfo,
                                       const char *kernel_interface_filename,
                                       const char *module_suffix)
{
    char *cmd;
    char *kernel_interface, *build_module_instances_parameter = NULL;
    int ret;

    if (op->multiple_kernel_modules) {
        build_module_instances_parameter =
            nvasprintf(" NV_BUILD_MODULE_INSTANCES=%d", NV_MAX_MODULE_INSTANCES);
    }

    cmd = nvstrcat("cd ", tmpdir, "; ", op->utils[MAKE], " ",
                   kernel_interface_filename,
                   " SYSSRC=", op->kernel_source_path,
                   " SYSOUT=", op->kernel_output_path,
                   " NV_MODULE_SUFFIX=", module_suffix,
                   build_module_instances_parameter, NULL);

    nvfree(build_module_instances_parameter);

    ret = run_command(op, cmd, NULL, TRUE, 25 /* XXX */, TRUE);

    free(cmd);

    if (ret != 0) {
        ui_status_end(op, "Error.");
        ui_error(op, "Unable to build the NVIDIA kernel module interface.");
        /* XXX need more descriptive error message */
        return FALSE;
    }

    /* check that the file exists */

    kernel_interface = nvstrcat(tmpdir, "/",
                                kernel_interface_filename, NULL);
    ret = access(kernel_interface, F_OK);
    nvfree(kernel_interface);

    if (ret == -1) {
        ui_status_end(op, "Error.");
        ui_error(op, "The NVIDIA kernel module interface was not created.");
        return FALSE;
    }

    return TRUE;
}

/*
 * pack_kernel_interface() - Store the input built interfaces in the
 * PrecompiledFileInfo array.
 *
 * Returns true if the packing was successful, or false on error.
 */

static int pack_kernel_interface(Options *op, Package *p,
                                 const char *build_dir,
                                 PrecompiledFileInfo *fileInfo,
                                 const char *module_suffix,
                                 const char *kernel_interface,
                                 const char *module_filename,
                                 const char *core_file,
                                 const char *target_directory)
{
    int command_ret;

    command_ret = precompiled_read_interface(fileInfo, kernel_interface,
                                             module_filename,
                                             core_file, target_directory);

    if (command_ret) {
        if (op->module_signing_secret_key && op->module_signing_public_key) {
            if (!create_detached_signature(op, p, build_dir, fileInfo,
                                           module_suffix,
                                           module_filename)) {
                return FALSE;
            }
        }
        return TRUE;
    }

    return FALSE;
}



static int build_and_pack_interface(Options *op, Package *p, const char *tmpdir,
                                    const char *subdir,
                                    PrecompiledFileInfo *fileInfo,
                                    const char *interface, const char *module,
                                    const char *suffix, const char *core)
{
    int ret;
    char *filename = NULL;
    char *dir = NULL;

    ui_status_begin(op, "Building kernel module interface: ", "Building %s",
                    interface);

    dir = nvstrcat(tmpdir, "/", subdir, NULL);
    ret = build_kernel_interface_file(op, dir, fileInfo, interface, suffix);

    if (!ret) {
        goto done;
    }

    ui_status_end(op, "done.");

    ui_log(op, "Kernel module interface compilation complete.");

    filename = nvstrcat(dir, "/", interface, NULL);

    /* add the kernel interface to the list of files to be packaged */
    ret = pack_kernel_interface(op, p, tmpdir, fileInfo, suffix, filename,
                                module, core, subdir);

done:
    nvfree(dir);
    nvfree(filename);

    return ret;
}



/*
 * build_kernel_interface() - build the kernel interface(s), and store any
 * built interfaces in a newly allocated PrecompiledFileInfo array. Return
 * the number of packaged interface files, or 0 on error.
 *
 * The tmpdir created to build the kernel interface is removed before exit.
 *
 * For multi-RM, the frontend module interface should be compiled and
 * packaged too.
 *
 * XXX this and build_kernel_module() should be merged.
 */

int build_kernel_interface(Options *op, Package *p,
                           PrecompiledFileInfo ** fileInfos)
{
    char *tmpdir = NULL;
    char *dstfile = NULL;
    int files_packaged = 0, i;
    int num_files = 1, ret = FALSE;
    char *uvmdir = NULL;

    *fileInfos = NULL;

    /* create a temporary directory */

    tmpdir = make_tmpdir(op);

    if (!tmpdir) {
        ui_error(op, "Unable to create a temporary build directory.");
        return FALSE;
    }
    
    /* copy the kernel module sources to it */
    
    ui_log(op, "Copying kernel module sources to temporary directory.");

    if (!copy_directory_contents
        (op, p->kernel_module_build_directory, tmpdir)) {
        ui_error(op, "Unable to copy the kernel module sources to temporary "
                 "directory '%s'.", tmpdir);
        goto failed;
    }

    uvmdir = nvstrcat(tmpdir, "/" UVM_SUBDIR, NULL);

    if (op->install_uvm) {
        if (!mkdir_recursive(op, uvmdir, 0655)) {
            ui_error(op, "Unable to create a temporary subdirectory for the "
                     "Unified Memory kernel module build.");
            goto failed;
        }

        if (!copy_directory_contents(op,
                                     p->uvm_module_build_directory, uvmdir)) {
            ui_error(op, "Unable to copy the Unified Memory kernel module "
                     "sources to temporary directory '%s'.", uvmdir);
            goto failed;
        }
    }
    
    /*
     * touch the contents of the build directory, to avoid make time
     * skew error messages
     */

    touch_directory(op, p->kernel_module_build_directory);

    if (op->multiple_kernel_modules) {
        num_files = op->num_kernel_modules;
    }

    *fileInfos = nvalloc(sizeof(PrecompiledFileInfo) * (num_files + 2));

    for (i = 0; i < num_files; i++) {
        char *kernel_module_filename;
        char *kernel_interface_filename;
        char *module_instance_str = NULL;

        if (op->multiple_kernel_modules) {
            module_instance_str = nvasprintf("%d", i);
        } else {
            module_instance_str = nvstrdup("");
        }

        kernel_interface_filename = nvstrdup(p->kernel_interface_filename);
        replace_zero(kernel_interface_filename, i);

        kernel_module_filename = nvstrdup(p->kernel_module_filename);
        replace_zero(kernel_module_filename, i);

        ret = build_and_pack_interface(op, p, tmpdir, "", *fileInfos + i,
                                       kernel_interface_filename,
                                       kernel_module_filename,
                                       module_instance_str, "nv-kernel.o");

        if (!ret) {
            goto interface_done;
        }

interface_done:

        nvfree(kernel_interface_filename);
        nvfree(kernel_module_filename);
        nvfree(module_instance_str);

        if (!ret)
            goto failed;

        files_packaged++;
    }

    if (op->multiple_kernel_modules) {
        ret = build_and_pack_interface(op, p, tmpdir, "", *fileInfos + num_files,
                                       p->kernel_frontend_interface_filename,
                                       p->kernel_frontend_module_filename,
                                       "frontend", "");
         if (!ret) {
            goto failed;
         }

        files_packaged++;
    }

    if (op->install_uvm) {
        ret = build_and_pack_interface(op, p, tmpdir, UVM_SUBDIR,
                                       *fileInfos + files_packaged,
                                       p->uvm_interface_filename,
                                       p->uvm_kernel_module_filename, "uvm", "");

        if (!ret) {
            goto failed;
        }

        files_packaged++;
    }

failed:
    nvfree(uvmdir);

    if (files_packaged == 0) {
        nvfree(*fileInfos);
        *fileInfos = NULL;
    }

    if (tmpdir) {
        remove_directory(op, tmpdir);
        nvfree(tmpdir);
    }

    if (dstfile) nvfree(dstfile);

    return files_packaged;

} /* build_kernel_interface() */




/*
 * check_for_warning_messages() - check if the kernel module detected
 * problems with the target system and registered warning messages
 * for us with the Linux /proc interface. If yes, show these messages
 * to the user.
 */

void check_for_warning_messages(Options *op)
{
    char *paths[2] = { "/proc/driver/nvidia/warnings", NULL };
    FTS *fts;
    FTSENT *ent;
    char *buf = NULL;

    fts = fts_open(paths, FTS_LOGICAL, NULL);
    if (!fts) return;

    while ((ent = fts_read(fts)) != NULL) {
        switch (ent->fts_info) {
            case FTS_F:
                if ((strlen(ent->fts_name) == 6) &&
                    !strncmp("README", ent->fts_name, 6))
                    break;
                if (read_text_file(ent->fts_path, &buf)) {
                    ui_warn(op, "%s", buf);
                    nvfree(buf);
                }
                break;
            default:
                /* ignore this file entry */
                break;
        }
    }

    fts_close(fts);

} /* check_for_warning_messages() */



/*
 * init_libkmod() - Attempt to dlopen() libkmod and the function symbols we
 * need from it. Set the global libkmod handle and function pointers on
 * success. Return TRUE if loading all symbols succeeded; FALSE otherwise.
 */
static int init_libkmod(void)
{
    if (!libkmod) {
        libkmod = dlopen("libkmod.so.2", RTLD_LAZY);
        if(!libkmod) {
            return FALSE;
        }

        lkmod_new = dlsym(libkmod, "kmod_new");
        lkmod_unref = dlsym(libkmod, "kmod_unref");
        lkmod_module_unref = dlsym(libkmod, "kmod_module_unref");
        lkmod_module_new_from_path = dlsym(libkmod, "kmod_module_new_from_path");
        lkmod_module_insert_module = dlsym(libkmod, "kmod_module_insert_module");
    }

    if (libkmod) {
        /* libkmod was already open, or was just successfully dlopen()ed:
         * check to make sure all of the symbols are set */
        if (lkmod_new && lkmod_unref && lkmod_module_unref &&
            lkmod_module_new_from_path && lkmod_module_insert_module) {
            return TRUE;
        } else {
            /* One or more symbols missing; abort */
            close_libkmod();
            return FALSE;
        }
    }
    return FALSE;
} /* init_libkmod() */



/*
 * close_libkmod() - clear all libkmod function pointers and dlclose() libkmod
 */
static void close_libkmod(void)
{
    if (libkmod) {
        dlclose(libkmod);
    }

    libkmod = NULL;
    lkmod_new = NULL;
    lkmod_unref = NULL;
    lkmod_module_unref = NULL;
    lkmod_module_new_from_path = NULL;
    lkmod_module_insert_module = NULL;
} /* close_libkmod() */



/*
 * do_insmod() - load the kernel module using libkmod if available; fall back
 * to insmod otherwise. Returns the result of kmod_module_insert_module() if
 * available, or of insmod otherwise. Pass the result of module loading up
 * through the data argument, regardless of whether we used libkmod or insmod.
 */
static int do_insmod(Options *op, const char *module, char **data)
{
    int ret = 0, libkmod_failed = FALSE;
    *data = NULL;

    if (init_libkmod()) {
        struct kmod_ctx *ctx = NULL;
        struct kmod_module *mod = NULL;
        const char *config_paths = NULL;

        ctx = lkmod_new(NULL, &config_paths);
        if (!ctx) {
            libkmod_failed = TRUE;
            goto kmod_done;
        }

        ret = lkmod_module_new_from_path(ctx, module, &mod);
        if (ret < 0) {
            libkmod_failed = TRUE;
            goto kmod_done;
        }

        ret = lkmod_module_insert_module(mod, 0, "");
        if (ret < 0) {
            /* insmod ignores > 0 return codes of kmod_module_insert_module(),
             * so we should do it too. On failure, strdup() the error string to
             * *data to ensure that it can be freed later. */
            *data = nvstrdup(strerror(-ret));
        }

kmod_done:
        if (mod) {
            lkmod_module_unref(mod);
        }
        if (ctx) {
            lkmod_unref(ctx);
        }
    } else {
        if (op->expert) {
            ui_log(op, "Unable to load module with libkmod; "
                   "falling back to insmod.");
        }
        libkmod_failed = TRUE;
    }

    if (!libkmod || libkmod_failed) {
        char *cmd;

        /* Fall back to insmod */

        cmd = nvstrcat(op->utils[INSMOD], " ", module, NULL);

        /* only output the result of the test if in expert mode */

        ret = run_command(op, cmd, data, op->expert, 0, TRUE);
        nvfree(cmd);
    }

    close_libkmod();

    return ret;
} /* do_insmod() */


/*
 * Determine if the load error be ignored or not. Also print detailed 
 * error messages corresponding to the return status from do_install(). 
 *
 * Returns true if user chooses to ignore the load error, else, false.
 */

static int ignore_load_error(Options *op, Package *p, 
                             const char *module_filename,
                             const char* data, int insmod_status)
{
    int ignore_error = FALSE, secureboot, module_sig_force, enokey;
    const char *probable_reason, *signature_related;

    enokey = (-insmod_status == ENOKEY);
    secureboot = (secure_boot_enabled() == 1);
    module_sig_force =
        (test_kernel_config_option(op, p, "CONFIG_MODULE_SIG_FORCE") ==
         KERNEL_CONFIG_OPTION_DEFINED);

    if (enokey) {
        probable_reason = ",";
        signature_related = "";
    } else if (module_sig_force) {
        probable_reason = ". CONFIG_MODULE_SIG_FORCE is set on the target "
                          "kernel, so this is likely";
    } else if (secureboot) {
        probable_reason = ". Secure boot is enabled on this system, so "
                          "this is likely";
    } else {
        probable_reason = ", possibly";
    }

    if (!enokey) {
        signature_related = "if this module loading failure is due to the "
                            "lack of a trusted signature, ";
    }

    if (enokey || secureboot || module_sig_force || op->expert) {
        if (op->kernel_module_signed) {

            const char *choices[2] = {
                "Install signed kernel module",
                "Abort installation"
            };

            ignore_error = (ui_multiple_choice(op, choices, 2, 0,
                                               "The signed kernel module failed "
                                               "to load%s because the kernel "
                                               "does not trust any key which is "
                                               "capable of verifying the module "
                                               "signature. Would you like to "
                                               "install the signed kernel module "
                                               "anyway?\n\nNote that %syou "
                                               "will not be able to load the "
                                               "installed module until after a "
                                               "key that can verify the module "
                                               "signature is added to a key "
                                               "database that is trusted by the "
                                               "kernel. This will likely "
                                               "require rebooting your computer.",
                                               probable_reason,
                                               signature_related) == 0);
        } else {
            const char *secureboot_message, *dkms_message;

            secureboot_message = secureboot == 1 ?
                                     "and sign the kernel module when "
                                     "prompted to do so." :
                                     "and set the --module-signing-secret-"
                                     "key and --module-signing-public-key "
                                     "options on the command line, or run "
                                     "the installer in expert mode to "
                                     "enable the interactive module "
                                     "signing prompts.";

            dkms_message = op->dkms ? " Module signing is incompatible "
                                      "with DKMS, so please select the "
                                      "non-DKMS option when building the "
                                      "kernel module to be signed." : "";
            ui_error(op, "The kernel module failed to load%s because it "
                     "was not signed by a key that is trusted by the "
                     "kernel. Please try installing the driver again, %s%s",
                     probable_reason, secureboot_message, dkms_message);
        }
    }

    if (ignore_error) {
        ui_log(op, "An error was encountered when loading the kernel "
               "module, but that error was ignored, and the kernel module "
               "will be installed, anyway. The error was: %s", data);
    } else {
        ui_error(op, "Unable to load the kernel module '%s'.  This "
                 "happens most frequently when this kernel module was "
                 "built against the wrong or improperly configured "
                 "kernel sources, with a version of gcc that differs "
                 "from the one used to build the target kernel, or "
                 "if a driver such as rivafb, nvidiafb, or nouveau is "
                 "present and prevents the NVIDIA kernel module from "
                 "obtaining ownership of the NVIDIA graphics device(s), "
                 "or no NVIDIA GPU installed in this system is supported "
                 "by this NVIDIA Linux graphics driver release.\n\n"
                 "Please see the log entries 'Kernel module load "
                 "error' and 'Kernel messages' at the end of the file "
                 "'%s' for more information.",
                 module_filename, op->log_file_name);

        /*
         * if in expert mode, run_command() would have caused this to
         * be written to the log file; so if not in expert mode, print
         * the output now.
         */

        if (!op->expert) ui_log(op, "Kernel module load error: %s", data);
    }

    return ignore_error; 
} /* ignore_load_error() */


/*
 * test_kernel_module() - attempt to insmod the kernel module and then
 * rmmod it.  Return TRUE if the insmod succeeded, or FALSE otherwise.
 */

int test_kernel_module(Options *op, Package *p)
{
    char *cmd = NULL, *data = NULL, *module_path;
    int old_loglevel = 0, new_loglevel = 0;
    int fd, ret, name[] = { CTL_KERN, KERN_PRINTK }, i;
    size_t len = sizeof(int);
    const char *depmods[] = { "i2c-core", "drm" };

    /* 
     * If we're building/installing for a different kernel, then we
     * can't test the module now.
     */

    if (op->kernel_name) return TRUE;

    /*
     * Temporarily disable most console messages to keep the curses
     * interface from being clobbered when the module is loaded.
     * Save the original console loglevel to allow restoring it once
     * we're done.
     */
    fd = open("/proc/sys/kernel/printk", O_RDWR);
    if (fd >= 0) {
        if (read(fd, &old_loglevel, 1) == 1) {
            new_loglevel = '2'; /* KERN_CRIT */
            write(fd, &new_loglevel, 1);
        }
    } else {
        if (!sysctl(name, 2, &old_loglevel, &len, NULL, 0)) {
            new_loglevel = 2; /* KERN_CRIT */
            sysctl(name, 2, NULL, 0, &new_loglevel, len);
        }
    }

    /*
     * Attempt to load modules that nvidia.ko might depend on.  Silently ignore
     * failures: if nvidia.ko doesn't depend on the module that failed, the test
     * load below will succeed and it doesn't matter that the load here failed.
     */
    for (i = 0; i < ARRAY_LEN(depmods); i++) {
        cmd = nvstrcat(op->utils[MODPROBE], " -q ", depmods[i], NULL);
        run_command(op, cmd, NULL, FALSE, 0, TRUE);
        nvfree(cmd);
    }

    if (op->multiple_kernel_modules) {
        /* Load nvidia-frontend.ko */
        
        module_path = nvstrcat(p->kernel_module_build_directory, "/",
                               p->kernel_frontend_module_filename, NULL);
        ret = do_insmod(op, module_path, &data);
        nvfree(module_path);

        if (ret != 0) {
            ret = ignore_load_error(op, p, p->kernel_frontend_module_filename, 
                                    data, ret);
            goto test_exit;
        }

        nvfree(data);
    }

    /* 
     * Load nvidia0.ko while building multiple kernel modules or
     * load nvidia.ko for non-multiple-kernel-module/simple builds.
     */

    module_path = nvstrcat(p->kernel_module_build_directory, "/",
                           p->kernel_module_filename, NULL);
    ret = do_insmod(op, module_path, &data);
    nvfree(module_path);

    if (ret != 0) {
        ret = ignore_load_error(op, p, p->kernel_module_filename, data, ret);
        goto test_exit;
    }
    if (op->install_uvm) {
        module_path = nvstrcat(p->uvm_module_build_directory, "/",
                               p->uvm_kernel_module_filename, NULL);
        ret = do_insmod(op, module_path, &data);
        nvfree(module_path);

        if (ret != 0) {
            ret = ignore_load_error(op, p, p->uvm_kernel_module_filename, data,
                                    ret);
            if (ret) {
                ui_warn(op, "The NVIDIA Unified Memory module failed to load, "
                        "and the load failure was ignored. This module is "
                        "required in order for the CUDA driver to function; if "
                        "the load failure cannot be resolved, then this system "
                        "will be unable to run CUDA applications.");
            }
            goto test_exit;
        }
    }

    /*
     * check if the kernel module detected problems with this
     * system's kernel and display any warning messages it may
     * have prepared for us.
     */

    check_for_warning_messages(op);

    /*
     * attempt to unload the kernel module, but don't abort if
     * this fails: the kernel may not have been configured with
     * support for module unloading (Linux 2.6).
     */

    if (op->install_uvm) {
        cmd = nvstrcat(op->utils[RMMOD], " ", p->uvm_kernel_module_name, NULL);
        run_command(op, cmd, NULL, FALSE, 0, TRUE);
        nvfree(cmd);
    }

    cmd = nvstrcat(op->utils[RMMOD], " ", p->kernel_module_name, NULL);
    run_command(op, cmd, NULL, FALSE, 0, TRUE);
    nvfree(cmd);

    if (op->multiple_kernel_modules) {
        cmd = nvstrcat(op->utils[RMMOD], " ",
                       p->kernel_frontend_module_name, NULL);
        run_command(op, cmd, NULL, FALSE, 0, TRUE);
        nvfree(cmd);
    }

    ret = TRUE;

test_exit:
    nvfree(data);
    
    /*
     * display/log the last few lines of the kernel ring buffer
     * to provide further details in case of a load failure or
     * to capture NVRM warning messages, if any.
     */
    cmd = nvstrcat(op->utils[DMESG], " | ",
                   op->utils[TAIL], " -n 25", NULL);

    if (!run_command(op, cmd, &data, FALSE, 0, TRUE))
        ui_log(op, "Kernel messages:\n%s", data);

    nvfree(cmd);
    nvfree(data);

    if (fd >= 0) {
        if (new_loglevel != 0)
            write(fd, &old_loglevel, 1);
        close(fd);
    } else {
        if (new_loglevel != 0)
            sysctl(name, 2, NULL, 0, &old_loglevel, len);
    }

    /*
     * Unload dependencies that might have been loaded earlier.
     */
    for (i = 0; i < ARRAY_LEN(depmods); i++) {
        cmd = nvstrcat(op->utils[MODPROBE], " -qr ", depmods[i], NULL);
        run_command(op, cmd, NULL, FALSE, 0, TRUE);
        nvfree(cmd);
    }

    return ret;
    
} /* test_kernel_module() */



/*
 * load_kernel_module() - modprobe the kernel module
 */

int load_kernel_module(Options *op, Package *p)
{
    char *cmd, *data;
    int len, ret;

    len = strlen(op->utils[MODPROBE]) + strlen(p->kernel_module_name) + 2;

    cmd = (char *) nvalloc(len);
    
    snprintf(cmd, len, "%s %s", op->utils[MODPROBE], p->kernel_module_name);
    
    ret = run_command(op, cmd, &data, FALSE, 0, TRUE);

    if (ret != 0) {
        if (op->expert) {
            ui_error(op, "Unable to load the kernel module: '%s'", data);
        } else {
            ui_error(op, "Unable to load the kernel module.");
        }
        ret = FALSE;
    } else {
        ret = TRUE;
    }

    nvfree(cmd);
    nvfree(data);
    
    return ret;

} /* load_kernel_module() */



/*
 * check_for_unloaded_kernel_module() - test if any of the "bad"
 * kernel modules are loaded; if they are, then try to unload it.  If
 * we can't unload it, then report an error and return FALSE;
 */

int check_for_unloaded_kernel_module(Options *op, Package *p)
{
    int n = 0;
    int loaded = FALSE;
    unsigned int bits = 0;
    
    /*
     * We can skip this check if we are installing for a non-running
     * kernel and only installing a kernel module.
     */
    
    if (op->kernel_module_only && op->kernel_name) {
        ui_log(op, "Only installing a kernel module for a non-running "
               "kernel; skipping the \"is an NVIDIA kernel module loaded?\" "
               "test.");
        return TRUE;
    }

    /*
     * We can also skip this check if we aren't installing a kernel
     * module at all.
     */

    if (op->no_kernel_module) {
        ui_log(op, "Not installing a kernel module; skipping the \"is an "
               "NVIDIA kernel module loaded?\" test.");
        return TRUE;
    }

    while (p->bad_modules[n]) {
        if (check_for_loaded_kernel_module(op, p->bad_modules[n])) {
            loaded = TRUE;
            bits |= (1 << n);
        }
        n++;
    }

    if (!loaded) return TRUE;
    
    /* one or more kernel modules is loaded... try to unload them */

    n = 0;
    while (p->bad_modules[n]) {
        if (!(bits & (1 << n))) {
            n++;
            continue;
        }
        
        rmmod_kernel_module(op, p->bad_modules[n]);

        /* check again */
        
        if (check_for_loaded_kernel_module(op, p->bad_modules[n])) {
            ui_error(op,  "An NVIDIA kernel module '%s' appears to already "
                     "be loaded in your kernel.  This may be because it is "
                     "in use (for example, by the X server), but may also "
                     "happen if your kernel was configured without support "
                     "for module unloading.  Please be sure you have exited "
                     "X before attempting to upgrade your driver.  If you "
                     "have exited X, know that your kernel supports module "
                     "unloading, and still receive this message, then an "
                     "error may have occured that has corrupted the NVIDIA "
                     "kernel module's usage count; the simplest remedy is "
                     "to reboot your computer.",
                     p->bad_modules[n]);
    
            return FALSE;
        }
        n++;
    }

    return TRUE;

} /* check_for_unloaded_kernel_module() */


/*
 * add_file_to_search_filelist() - Add file to build a list
 * of files expected to be unpacked.
 */

static void add_file_to_search_filelist(char **search_filelist, char *filename)
{
    int index = 0;

    while(search_filelist[index]) {
        index++;
    }

    if (index == SEARCH_FILELIST_MAX_ENTRIES)
        return;
 
    search_filelist[index] = nvstrdup(filename);

} /* add_file_to_search_filelist() */


/* 
 * free_search_filelist() - frees the list of files expected
 * to unpack
 */

static void free_search_filelist(char **search_filelist)
{
    int index = 0;

    while (search_filelist[index]) {
        free(search_filelist[index]);
        index++;
    }

} /*free_search_filelist() */


/*
 * find_precompiled_kernel_interface() - do assorted black magic to
 * determine if the given package contains a precompiled kernel interface
 * for the kernel on this system.
 *
 * XXX it would be nice to extend this so that a kernel module could
 * be installed for a kernel other than the currently running one.
 */

PrecompiledInfo *find_precompiled_kernel_interface(Options *op, Package *p)
{
    char *proc_version_string, *tmp;
    PrecompiledInfo *info = NULL;
    char *search_filelist[SEARCH_FILELIST_MAX_ENTRIES+1];
    int index;
  
    /* allow the user to completely skip this search */
    
    if (op->no_precompiled_interface) {
        ui_log(op, "Not probing for precompiled kernel interfaces.");
        return NULL;
    }
    
    /* retrieve the proc version string for the running kernel */

    proc_version_string = read_proc_version(op, op->proc_mount_point);
    
    if (!proc_version_string) goto failed;
    
    /* make sure the target directory exists */
    
    if (!mkdir_recursive(op, p->kernel_module_build_directory, 0755))
        goto failed;

    memset(search_filelist, 0, sizeof(search_filelist));

    if (op->multiple_kernel_modules) {
        add_file_to_search_filelist(search_filelist, 
                                    p->kernel_frontend_interface_filename);
    }

    for (index = 0; index < op->num_kernel_modules; index++) {
        char *tmp;
        tmp = nvstrdup(p->kernel_interface_filename);
        replace_zero(tmp, index);
        add_file_to_search_filelist(search_filelist, tmp);
        free(tmp);
    }

    /*
     * if the --precompiled-kernel-interfaces-path option was
     * specified, search that directory, first
     */
    
    if (op->precompiled_kernel_interfaces_path) {
        info = scan_dir(op, p, op->precompiled_kernel_interfaces_path,
                        proc_version_string, search_filelist);
    }
    
    /*
     * If we didn't find a match, search for distro-provided
     * precompiled kernel interfaces
     */

    if (!info) {
        tmp = build_distro_precompiled_kernel_interface_dir(op);
        if (tmp) {
            info = scan_dir(op, p, tmp, proc_version_string, search_filelist);
            nvfree(tmp);
        }
    }

    /*
     * if we still haven't found a match, search in
     * p->precompiled_kernel_interface_directory (the directory
     * containing the precompiled kernel interfaces shipped with the
     * package)
     */

    if (!info) {
        info = scan_dir(op, p, p->precompiled_kernel_interface_directory,
                        proc_version_string, search_filelist);
    }
    
    /*
     * If we didn't find a matching precompiled kernel interface, ask
     * if we should try to download one.
     */

    if (!info && !op->no_network && op->precompiled_kernel_interfaces_url) {
        info = download_updated_kernel_interface(op, p,
                                                 proc_version_string,
                                                 search_filelist);
        if (!info) {
            ui_message(op, "No matching precompiled kernel interface was "
                       "found at '%s'; this means that the installer will need "
                       "to compile a kernel interface for your kernel.",
                       op->precompiled_kernel_interfaces_url);
            free_search_filelist(search_filelist);
            return NULL;
        }
    }

    free_search_filelist(search_filelist);

    /* If we found one, ask expert users if they really want to use it */

    if (info && op->expert) {
        const char *choices[2] = {
            "Use the precompiled interface",
            "Compile the interface"
        };

        if (ui_multiple_choice(op, choices, 2, 0, "A precompiled kernel "
                               "interface for the kernel '%s' has been found.  "
                               "Would you like use the precompiled interface, "
                               "or would you like to compile the interface "
                               "instead?", info->description) == 1) {
            free_precompiled(info);
            info = NULL;
        }
    }

    if (info) {
        return info;
    }

 failed:

    if (op->expert) {
        ui_message(op, "No precompiled kernel interface was found to match "
                   "your kernel; this means that the installer will need to "
                   "compile a new kernel interface.");
    }

    return NULL;
    
} /* find_precompiled_kernel_interface() */



/*
 * get_kernel_name() - get the kernel name: this is either what
 * the user specified via the --kernel-name option, or `name -r`.
 */

char __kernel_name[256];

char *get_kernel_name(Options *op)
{
    struct utsname uname_buf;

    if (op->kernel_name) {
        return op->kernel_name;
    } else {
        if (uname(&uname_buf) == -1) {
            ui_warn(op, "Unable to determine kernel version (%s).",
                    strerror(errno));
            return NULL;
        } else {
            strncpy(__kernel_name, uname_buf.release, 256);
            return __kernel_name;
        }
    }
} /* get_kernel_name() */



/*
 * test_kernel_config_option() - test to see if the given option is defined
 * in the target kernel's configuration.
 */

KernelConfigOptionStatus test_kernel_config_option(Options* op, Package *p,
                                                   const char *option)
{
    if (op->kernel_source_path && op->kernel_output_path) {
        int ret;
        char *conftest_cmd;

        conftest_cmd = nvstrcat("test_configuration_option ", option, NULL);
        ret = run_conftest(op, p, conftest_cmd, NULL);
        nvfree(conftest_cmd);

        return ret ? KERNEL_CONFIG_OPTION_DEFINED :
                     KERNEL_CONFIG_OPTION_NOT_DEFINED;
    }

    return KERNEL_CONFIG_OPTION_UNKNOWN;
}



/*
 * guess_module_signing_hash() - return the hash algorithm used for
 * signing kernel modules, or NULL if it can't be determined.
 */

char *guess_module_signing_hash(Options *op, Package *p)
{
    char *ret;

    if (run_conftest(op, p, "guess_module_signing_hash", &ret)) {
        return ret;
    }

    return NULL;
}

/*
 ***************************************************************************
 * local static routines
 ***************************************************************************
 */



/*
 * default_kernel_module_installation_path() - do the equivalent of:
 *
 * SYSSRC = /lib/modules/$(shell uname -r)
 *
 * ifeq ($(shell if test -d $(SYSSRC)/kernel; then echo yes; fi),yes)
 *   INSTALLDIR = $(SYSSRC)/kernel/drivers/video
 * else
 *   INSTALLDIR = $(SYSSRC)/video
 * endif
 */

static char *default_kernel_module_installation_path(Options *op)
{
    char *str, *tmp;
    
    tmp = get_kernel_name(op);
    if (!tmp) return NULL;

    str = nvstrcat("/lib/modules/", tmp, "/kernel", NULL);
    
    if (directory_exists(op, str)) {
        free(str);
        str = nvstrcat("/lib/modules/", tmp, "/kernel/drivers/video", NULL);
        return str;
    }

    free(str);
    
    str = nvstrcat("/lib/modules/", tmp, "/video", NULL);

    return str;

} /* default_kernel_module_installation_path() */



/*
 * default_kernel_source_path() - determine the default kernel
 * source path, if possible.  Return NULL if no default kernel path
 * is found.
 *
 * Here is the logic:
 *
 * if --kernel-source-path was set, use that
 * 
 * if --kernel-include-path was set, use that (converting it to the
 * source path); also print a warning that --kernel-include-path is
 * deprecated.
 *
 * else if SYSSRC is set, use that
 *
 * else if /lib/modules/`uname -r`/build exists use that
 *
 * else if /usr/src/linux exists use that
 *
 * else return NULL
 *
 * One thing to note is that for the first two methods
 * (--kernel-source-path and $SYSSRC) we don't check for directory
 * existence before returning.  This is intentional: if the user set
 * one of these, then they're trying to set a particular path.  If
 * that directory doesn't exist, then better to abort installation with
 * an appropriate error message in determine_kernel_source_path().
 * Whereas, for the later two (/lib/modules/`uname -r`/build
 * and /usr/src/linux), these are not explicitly requested by
 * the user, so it makes sense to only use them if they exist.
 */ 

static char *default_kernel_source_path(Options *op)
{
    char *str, *tmp;
    
    str = tmp = NULL;
    
    /* check --kernel-source-path */

    if (op->kernel_source_path) {
        ui_log(op, "Using the kernel source path '%s' as specified by the "
               "'--kernel-source-path' commandline option.",
               op->kernel_source_path);
        return op->kernel_source_path;
    }

    /* check --kernel-include-path */

    if (op->kernel_include_path) {
        ui_warn(op, "The \"--kernel-include-path\" option is deprecated "
                "(as part of reorganization to support Linux 2.6); please use "
                "\"--kernel-source-path\" instead.");
        str = convert_include_path_to_source_path(op->kernel_include_path);
        ui_log(op, "Using the kernel source path '%s' (inferred from the "
               "'--kernel-include-path' commandline option '%s').",
               str, op->kernel_include_path);
        return str;
    }

    /* check SYSSRC */
    
    str = getenv("SYSSRC");
    if (str) {
        ui_log(op, "Using the kernel source path '%s', as specified by the "
               "SYSSRC environment variable.", str);
        return str;
    }
    
    /* check /lib/modules/`uname -r`/build and /usr/src/linux-`uname -r` */
    
    tmp = get_kernel_name(op);

    if (tmp) {
        str = nvstrcat("/lib/modules/", tmp, "/source", NULL);

        if (directory_exists(op, str)) {
            return str;
        }

        nvfree(str);

        str = nvstrcat("/lib/modules/", tmp, "/build", NULL);
    
        if (directory_exists(op, str)) {
            return str;
        }

        nvfree(str);

        /*
         * check "/usr/src/linux-`uname -r`", too; patch suggested by
         * Peter Berg Larsen <pebl@math.ku.dk>
         */

        str = nvstrcat("/usr/src/linux-", tmp, NULL);
        if (directory_exists(op, str)) {
            return str;
        }

        free(str);
    }

    /* finally, try /usr/src/linux */

    if (directory_exists(op, "/usr/src/linux")) {
        return "/usr/src/linux";
    }
    
    return NULL;
    
} /* default_kernel_source_path() */


/*
 * find_module_substring() - find substring in a given string where differences
 * between hyphens and underscores are ignored. Returns a pointer to the
 * beginning of the substring, or NULL if the string/substring is NULL, or if
 * length of substring is greater than length of string, or substring is not
 * found.
 */

static char *find_module_substring(char *string, const char *substring)
{
    int string_len, substring_len, len;
    char *tstr;
    const char *tsubstr;

    if ((string == NULL) || (substring == NULL))
        return NULL;

    string_len = strlen(string);
    substring_len = strlen(substring);

    for (len = 0; len <= string_len - substring_len; len++, string++) {
        if (*string != *substring) {
            continue;
        }

        for (tstr = string, tsubstr = substring;
             *tsubstr != '\0';
             tstr++, tsubstr++) {
            if (*tstr != *tsubstr) {
                if (((*tstr == '-') || (*tstr == '_')) &&
                    ((*tsubstr == '-') || (*tsubstr == '_')))
                    continue;
                break;
            }
        }

        if (*tsubstr == '\0')
            return string;
    }

    return NULL;
} /* find_module_substring */


/*
 * substring_is_isolated() - given the string 'substring' with length 'len',
 * which points to a location inside the string 'string', check to see if
 * 'substring' is surrounded by either whitespace or the start/end of 'string'
 * on both ends.
 */

static int substring_is_isolated(const char *substring, const char *string,
                                 int len)
{
    if (substring != string) {
        if (!isspace(substring[-1])) {
            return FALSE;
        }
    }

    if (substring[len] && !isspace(substring[len])) {
        return FALSE;
    }

    return TRUE;
}


/*
 * check_for_loaded_kernel_module() - check if the specified kernel
 * module is currently loaded using `lsmod`.  Returns TRUE if the
 * kernel module is loaded; FALSE if it is not.
 *
 * Be sure to check that the character following the kernel module
 * name is a space (to avoid getting false positivies when the given
 * kernel module name is contained within another kernel module name.
 */

static int check_for_loaded_kernel_module(Options *op, const char *module_name)
{
    char *result = NULL;
    int ret, found = FALSE;

    ret = run_command(op, op->utils[LSMOD], &result, FALSE, 0, TRUE);
    
    if ((ret == 0) && (result) && (result[0] != '\0')) {
        char *ptr;
        int len = strlen(module_name);

        for (ptr = result;
             (ptr = find_module_substring(ptr, module_name));
             ptr += len) {
            if (substring_is_isolated(ptr, result, len)) {
                found = TRUE;
                break;
            }
        }
    }
    
    if (result) free(result);
    
    return found;
    
} /* check_for_loaded_kernel_module() */


/*
 * rmmod_kernel_module() - run `rmmod nvidia`
 */

static int rmmod_kernel_module(Options *op, const char *module_name)
{
    int len, ret;
    char *cmd;
    
    len = strlen(op->utils[RMMOD]) + strlen(module_name) + 2;
    
    cmd = (char *) nvalloc(len);
    
    snprintf(cmd, len, "%s %s", op->utils[RMMOD], module_name);
    
    ret = run_command(op, cmd, NULL, FALSE, 0, TRUE);
    
    free(cmd);
    
    return ret ? FALSE : TRUE;
    
} /* rmmod_kernel_module() */



/*
 * get_updated_kernel_interfaces() - 
 */

static PrecompiledInfo *
download_updated_kernel_interface(Options *op, Package *p,
                                  const char *proc_version_string,
                                  char *const *search_filelist)
{
    int fd = -1;
    int dst_fd = -1;
    int length, i;
    char *url = NULL;
    char *tmpfile = NULL;
    char *dstfile = NULL;
    char *buf = NULL;
    char *str = (void *) -1;
    char *ptr, *s;
    struct stat stat_buf;
    PrecompiledInfo *info = NULL;
    
    /* initialize the tmpfile and url strings */
    
    tmpfile = nvstrcat(op->tmpdir, "/nv-updates-XXXXXX", NULL);
    url = nvstrcat(op->precompiled_kernel_interfaces_url, "/", INSTALLER_OS,
                   "-", INSTALLER_ARCH, "/", p->version, "/updates/updates.txt",
                   NULL);

    /*
     * create a temporary file in which to write the list of available
     * updates
     */

    if ((fd = mkstemp(tmpfile)) == -1) {
        ui_error(op, "Unable to create temporary file (%s)", strerror(errno));
        goto done;
    }

    /* download the updates list */

    if (!snarf(op, url, fd, SNARF_FLAGS_DOWNLOAD_SILENT)) goto done;
    
    /* get the length of the file */

    if (fstat(fd, &stat_buf) == -1) goto done;

    length = stat_buf.st_size;
    
    /* map the file into memory for easier reading */
    
    str = mmap(0, length, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
    if (str == (void *) -1) goto done;
    
    /*
     * loop over each line of the updates file: each line should be of
     * the format: "[filename]:::[proc version string]"
     */

    ptr = str;

    while (TRUE) {
        buf = get_next_line(ptr, &ptr, str, length);
        if ((!buf) || (buf[0] == '\0')) goto done;

        s = strstr(buf, ":::");
        if (!s) {
            ui_error(op, "Invalid updates.txt list.");
            goto done;
        }
        
        s += 3; /* skip past the ":::" separator */
        
        if (strcmp(proc_version_string, s) == 0) {

            /* proc versions strings match */
            
            /*
             * terminate the string at the start of the ":::"
             * separator so that buf is the filename
             */
            
            s -= 3;
            *s = '\0';

            /* build the new url and dstfile strings */

            nvfree(url);
            url = nvstrcat(op->precompiled_kernel_interfaces_url, "/",
                           INSTALLER_OS, "-", INSTALLER_ARCH, "/", p->version,
                           "/updates/", buf, NULL);

            dstfile = nvstrcat(p->precompiled_kernel_interface_directory,
                               "/", buf, NULL);
            
            /* create dstfile */
            
            dst_fd = creat(dstfile, S_IRUSR | S_IWUSR);
            if (dst_fd == -1) {
                ui_error(op, "Unable to create file '%s' (%s).",
                         dstfile, strerror(errno));
                goto done;
            }
            
            /* download the file */
            
            if (!snarf(op, url, dst_fd, SNARF_FLAGS_STATUS_BAR)) goto done;
            
            close(dst_fd);
            dst_fd = -1;
            
            /* XXX once we have gpg setup, should check the file here */

            info = get_precompiled_info(op, dstfile, proc_version_string,
                                        p->version, search_filelist);

            if (!info) {
                ui_error(op, "The format of the downloaded precompiled package "
                         "is invalid!");
                free_precompiled(info);
                info = NULL;
            }
            
            /* compare checksums */

            for (i = 0; info && i < info->num_files; i++) {
                uint32 crc = compute_crc_from_buffer(info->files[i].data,
                                                     info->files[i].size);
                if (info->files[i].crc != crc) {
                    ui_error(op, "The embedded checksum of the file %s in the "
                             "downloaded precompiled pacakge '%s' (%" PRIu32
                             ") does not match the computed checksum (%"
                             PRIu32 "); not using.", info->files[i].name,
                             buf, info->files[i].crc, crc);
                    free_precompiled(info);
                    info = NULL;
                }
            }

            goto done;

        }

        nvfree(buf);
    }
    

 done:

    if (dstfile) nvfree(dstfile);
    if (buf) nvfree(buf);
    if (str != (void *) -1) munmap(str, stat_buf.st_size);
    if (dst_fd > 0) close(dst_fd);
    if (fd > 0) close(fd);

    unlink(tmpfile);
    if (tmpfile) nvfree(tmpfile);
    if (url) nvfree(url);

    return info;

} /* get_updated_kernel_interfaces() */



/*
 * check_cc_version() - check if the selected or default system
 * compiler is compatible with the one that was used to build the
 * currently running kernel.
 */

int check_cc_version(Options *op, Package *p)
{
    char *result;
    int ret;
    Options dummyop;

    const char *choices[2] = {
        "Ignore CC version check",
        "Abort installation"
    };

    /* 
     * If we're building/installing for a different kernel, then we
     * can't do the gcc version check (we don't have a /proc/version
     * string from which to get the kernel's gcc version).
     * If the user passes the option no-cc-version-check, then we also
     * shouldn't perform the cc version check.
     */

    if (op->ignore_cc_version_check) {
        setenv("IGNORE_CC_MISMATCH", "1", 1);
        return TRUE;
    }

    /* Kernel source/output paths may not be set yet; we don't need them
     * for this test, anyway. */
    dummyop = *op;
    dummyop.kernel_source_path = "DUMMY_SOURCE_PATH";
    dummyop.kernel_output_path = "DUMMY_OUTPUT_PATH";

    ret = run_conftest(&dummyop, p, "cc_version_check just_msg", &result);

    if (ret) return TRUE;

    ret = (ui_multiple_choice(op, choices, 2, 1, "The CC version check failed:"
                              "\n\n%s\n\nIf you know what you are doing you "
                              "can either ignore the CC version check and "
                              "continue installation, or abort installation, "
                              "set the CC environment variable to the name of "
                              "the compiler used to compile your kernel, and "
                              "restart installation.", result) == 1);
    nvfree(result);
    
    if (!ret) setenv("IGNORE_CC_MISMATCH", "1", 1);
    
    return !ret;
             
} /* check_cc_version() */


/*
 * fbdev_check() - run the rivafb_sanity_check and the nvidiafb_sanity_check
 * conftests; if either test fails, print the error message from the test
 * and abort the driver installation.
 */

static int fbdev_check(Options *op, Package *p)
{
    char *result;
    int ret;
    
    ui_log(op, "Performing rivafb check.");
    
    ret = run_conftest(op, p,"rivafb_sanity_check just_msg", &result);
    
    if (!ret) {
        if (result)
            ui_error(op, "%s", result);
        nvfree(result);

        return FALSE;
    }

    ui_log(op, "Performing nvidiafb check.");
    
    ret = run_conftest(op, p,"nvidiafb_sanity_check just_msg", &result);
    
    if (!ret) {
        if (result)
            ui_error(op, "%s", result);
        nvfree(result);

        return FALSE;
    }

    return TRUE;
    
} /* fbdev_check() */



/*
 * xen_check() - run the xen_sanity_check conftest; if this test fails, print
 * the test's error message and abort the driver installation.
 */

static int xen_check(Options *op, Package *p)
{
    char *result;
    int ret;
    
    ui_log(op, "Performing Xen check.");
    
    ret = run_conftest(op, p,"xen_sanity_check just_msg", &result);
    
    if (!ret) {
        if (result)
            ui_error(op, "%s", result);
        nvfree(result);

        return FALSE;
    }

    return TRUE;
    
} /* xen_check() */



/*
 * preempt_rt_check() - run the preempt_rt_sanity_check conftest; if this
 * test fails, print the test's error message and abort the driver
 * installation.
 */

static int preempt_rt_check(Options *op, Package *p)
{
    char *result;
    int ret;

    ui_log(op, "Performing PREEMPT_RT check.");

    ret = run_conftest(op, p, "preempt_rt_sanity_check just_msg", &result);

    if (!ret) {
        if (result)
            ui_error(op, "%s", result);
        nvfree(result);

        return FALSE;
    }

    return TRUE;

} /* preempt_rt_check() */



/*
 * scan_dir() - scan through the specified directory for a matching
 * precompiled kernel interface.
 */

static PrecompiledInfo *scan_dir(Options *op, Package *p,
                                 const char *directory_name,
                                 const char *proc_version_string,
                                 char *const *search_filelist)
{
    DIR *dir;
    struct dirent *ent;
    PrecompiledInfo *info = NULL;
    char *filename;
    
    if (!directory_name) return NULL;
    
    dir = opendir(directory_name);
    if (!dir) return NULL;

    /*
     * loop over all contents of the directory, looking for a
     * precompiled kernel interface that matches the running kernel
     */

    while ((ent = readdir(dir)) != NULL) {

        if (((strcmp(ent->d_name, ".")) == 0) ||
            ((strcmp(ent->d_name, "..")) == 0)) continue;
            
        filename = nvstrcat(directory_name, "/", ent->d_name, NULL);
        
        info = get_precompiled_info(op, filename, proc_version_string,
                                    p->version, search_filelist);

        free(filename);

        if (info) break;
    }
        
    if (closedir(dir) != 0) {
        ui_error(op, "Failure while closing directory '%s' (%s).",
                 directory_name,
                 strerror(errno));
    }
    
    return info;
    
} /* scan_dir() */



/*
 * build_distro_precompiled_kernel_interface_dir() - construct this
 * path:
 *
 *  /lib/modules/precompiled/`uname -r`/nvidia/gfx/
 */

static char *build_distro_precompiled_kernel_interface_dir(Options *op)
{
    struct utsname uname_buf;
    char *str;
    
    if (uname(&uname_buf) == -1) {
        ui_error(op, "Unable to determine kernel version (%s)",
                 strerror(errno));
        return NULL;
    }
    
    str = nvstrcat("/lib/modules/precompiled/", uname_buf.release,
                   "/nvidia/gfx/", NULL);
    
    return str;

} /* build_distro_precompiled_kernel_interface_dir() */



/*
 * convert_include_path_to_source_path() - given input to
 * "--kernel-include-path", convert it to "--kernel-source-path" by
 * scanning from the end to the previous "/".
 */

static char *convert_include_path_to_source_path(const char *inc)
{
    char *c, *str;

    str = nvstrdup(inc);

    /* go to the end of the string */

    for (c = str; *c; c++);
    
    /* move to the last printable character */

    c--;

    /* if the string ends in '/'; backup one more */

    if (*c == '/') c--;

    /* now back up to the next '/' */

    while ((c >= str) && (*c != '/')) c--;

    if (*c == '/') *c = '\0';

    return str;

} /* convert_include_path_to_source_path() */


/*
 * get_machine_arch() - get the machine architecture, substituting
 * i386 for i586 and i686 or arm for arm7l.
 */

static char __machine_arch[16];

char *get_machine_arch(Options *op)
{
    struct utsname uname_buf;

    if (uname(&uname_buf) == -1) {
        ui_warn(op, "Unable to determine machine architecture (%s).",
                strerror(errno));
        return NULL;
    } else {
        if ((strncmp(uname_buf.machine, "i586", 4) == 0) ||
            (strncmp(uname_buf.machine, "i686", 4) == 0)) {
            strcpy(__machine_arch, "i386");
        } else if ((strncmp(uname_buf.machine, "armv", 4) == 0)) {
            strcpy(__machine_arch, "arm");
        } else {
            strncpy(__machine_arch, uname_buf.machine,
                    sizeof(__machine_arch));
        }
        return __machine_arch;
    }

} /* get_machine_arch() */