-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmisc.c
3348 lines (2686 loc) · 101 KB
/
misc.c
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
/*
* nvidia-installer: A tool for installing NVIDIA software packages on
* Unix and Linux systems.
*
* Copyright (C) 2003 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>.
*
*
* misc.c - this source file contains miscellaneous routines for use
* by the nvidia-installer.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <dirent.h>
#include <libgen.h>
#include <pciaccess.h>
#include <elf.h>
#include <link.h>
#include "nvidia-installer.h"
#include "user-interface.h"
#include "kernel.h"
#include "files.h"
#include "misc.h"
#include "crc.h"
#include "nvGpus.h"
#include "manifest.h"
#include "nvpci-utils.h"
#include "conflicting-kernel-modules.h"
#include "initramfs.h"
#include "detect-self-hosted.h"
static int check_symlink(Options*, const char*, const char*, const char*);
/*
* read_next_word() - given a string buf, skip any whitespace, and
* then copy the next set of characters until more white space is
* encountered. A new string containing this next word is returned.
* The passed-by-reference parameter e, if not NULL, is set to point
* at the where the end of the word was, to facilitate multiple calls
* of read_next_word().
*/
char *read_next_word (char *buf, char **e)
{
char *c = buf;
char *start, *ret;
int len;
while ((*c) && (isspace (*c)) && (*c != '\n')) c++;
start = c;
while ((*c) && (!isspace (*c)) && (*c != '\n')) c++;
len = c - start;
if (len == 0) return NULL;
ret = (char *) nvalloc (len + 1);
strncpy (ret, start, len);
ret[len] = '\0';
if (e) *e = c;
return ret;
} /* read_next_word() */
/*
* check_euid() - this function checks that the effective uid of this
* application is root, and calls the ui to print an error if it's not
* root.
*/
int check_euid(Options *op)
{
uid_t euid;
euid = geteuid();
if (euid != 0) {
ui_error(op, "nvidia-installer must be run as root");
return FALSE;
}
return TRUE;
} /* check_euid() */
/*
* adjust_cwd() - this function scans through program_name (ie
* argv[0]) for any possible relative paths, and chdirs into the
* relative path it finds. The point of all this is to make the
* directory with the executed binary the cwd.
*
* It is assumed that the user interface has not yet been initialized
* by the time this function is called.
*/
int adjust_cwd(Options *op, const char *program_name)
{
char *c;
int success = TRUE;
/*
* extract any pathname portion out of the program_name and chdir
* to it
*/
c = strrchr(program_name, '/');
if (c) {
int len;
char *path;
len = c - program_name + 1;
path = (char *) nvalloc(len + 1);
strncpy(path, program_name, len);
path[len] = '\0';
if (op->expert) log_printf(op, NULL, "chdir(\"%s\")", path);
if (chdir(path)) {
fprintf(stderr, "Unable to chdir to %s (%s)",
path, strerror(errno));
success = FALSE;
}
free(path);
}
return success;
}
/*
* get_next_line() - this function scans for the next newline,
* carriage return, NUL terminator, or EOF in buf. If non-NULL, the
* passed-by-reference parameter 'end' is set to point to the next
* printable character in the buffer, or NULL if EOF is encountered.
*
* If the parameter 'start' is non-NULL, then that is interpreted as
* the start of the buffer string, and we check that we never walk
* 'length' bytes past 'start'.
*
* On success, a newly allocated buffer is allocated containing the
* next line of text (with a NULL terminator in place of the
* newline/carriage return).
*
* On error, NULL is returned.
*/
char *get_next_line(char *buf, char **end, char *start, int length)
{
char *c, *retbuf;
int len;
if (start && (length < 1)) return NULL;
#define __AT_END(_start, _current, _length) \
((_start) && (((_current) - (_start)) >= (_length)))
if (end) *end = NULL;
// Cast all char comparisons to EOF to signed char in order to
// allow proper sign extension on platforms like GCC ARM where
// char is unsigned char
if ((!buf) ||
__AT_END(start, buf, length) ||
(*buf == '\0') ||
(((signed char)*buf) == EOF)) return NULL;
c = buf;
while ((!__AT_END(start, c, length)) &&
(*c != '\0') &&
(((signed char)*c) != EOF) &&
(*c != '\n') &&
(*c != '\r')) c++;
len = c - buf;
retbuf = nvalloc(len + 1);
strncpy(retbuf, buf, len);
retbuf[len] = '\0';
if (end) {
while ((!__AT_END(start, c, length)) &&
(*c != '\0') &&
(((signed char)*c) != EOF) &&
(!isprint(*c))) c++;
if (__AT_END(start, c, length) ||
(*c == '\0') ||
(((signed char)*c) == EOF)) *end = NULL;
else *end = c;
}
return retbuf;
} /* get_next_line() */
/*
* run_command() - this function runs the given command and assigns
* the data parameter to a malloced buffer containing the command's
* output, if any. The caller of this function should free the data
* string. The return value of the command is returned from this
* function.
*
* The output parameter controls whether command output is sent to the
* ui; if this is TRUE, then everyline of output that is read is sent
* to the ui.
*
* If the output_match parameter is set to a { 0 }-terminated array of
* RunCommandOutputMatch records, it is interpreted as a rough estimate of how
* many lines of output (optionally requiring an initial substring match) will
* be generated by the command. This is used to compute the value that should
* be passed to ui_status_update() as lines of output are received. This may be
* set to NULL to skip the ui_status_update() updates.
*
* The redirect argument tells run_command() to redirect stderr to
* stdout so that all output is collected, or just stdout.
*
* XXX maybe we should do something to cap the time we allow the
* command to run?
*/
int run_command(Options *op, char **data, int output,
const RunCommandOutputMatch *output_match, int redirect,
const char *cmd_start, ...)
{
int n = 0; /* output line counter */
int len = 0; /* length of what has actually been read */
int buflen = 0; /* length of destination buffer */
int ret, total_lines;
char *cmd, *buf = NULL;
FILE *stream = NULL;
struct sigaction act, old_act;
float percent;
int *match_sizes = NULL;
va_list ap;
va_start(ap, cmd_start);
cmd = nvvstrcat(cmd_start, ap);
va_end(ap);
if (!cmd) {
return 1;
}
if (data) *data = NULL;
/*
* if command output is requested, print the command that we will
* execute
*/
if (output) ui_command_output (op, "executing: '%s'...", cmd);
/* redirect stderr to stdout */
if (redirect) {
char *tmp = cmd;
cmd = nvstrcat(cmd, " 2>&1", NULL);
nvfree(tmp);
}
/*
* XXX: temporarily ignore SIGWINCH; our child process inherits
* this disposition and will likewise ignore it (by default).
* This fixes cases where child processes abort after receiving
* SIGWINCH when its caught in the parent process.
*/
if (op->sigwinch_workaround) {
act.sa_handler = SIG_IGN;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
if (sigaction(SIGWINCH, &act, &old_act) < 0)
old_act.sa_handler = NULL;
}
/*
* Clear LANG and LC_ALL before running the command, to make sure
* command output that might need to be parsed doesn't vary based
* on system locale settings.
*/
unsetenv("LANG");
unsetenv("LC_ALL");
/*
* Open a process by creating a pipe, forking, and invoking the
* command.
*/
stream = popen(cmd, "r");
if (stream == NULL) {
ret = errno;
ui_error(op, "Failure executing command '%s' (%s).",
cmd, strerror(errno));
}
nvfree(cmd);
if (stream == NULL) {
return ret;
}
/*
* read from the stream, filling and growing buf, until we hit
* EOF. Send each line to the ui as it is read.
*/
if (output_match) {
int match_count, i;
/* Determine the total number of lines to match */
for (total_lines = i = 0; output_match[i].lines; i++) {
total_lines += output_match[i].lines;
}
match_count = i;
/* Cache the lengths of the .initial_match strings */
match_sizes = nvalloc(match_count * sizeof(*match_sizes));
for (i = 0; i < match_count; i++) {
if (output_match[i].initial_match) {
match_sizes[i] = strlen(output_match[i].initial_match);
}
}
/* Don't divide by zero */
if (total_lines == 0) total_lines = 1;
/*
* Note: 'match_sizes' and 'total_lines' must only be used when
* output_match != NULL; otherwise, 'match_sizes' cannot be safely
* dereferenced, and 'total_lines' is uninitialized.
*/
}
while (1) {
if ((buflen - len) < NV_MIN_LINE_LEN) {
buflen += NV_LINE_LEN;
buf = nvrealloc(buf, buflen);
}
if (fgets(buf + len, buflen - len, stream) == NULL) break;
if (output) ui_command_output(op, "%s", buf + len);
if (output_match) {
int i;
for (i = 0; output_match[i].lines; i++) {
const char *s = output_match[i].initial_match;
if (s == NULL || strncmp(buf + len, s, match_sizes[i]) == 0) {
n++;
break;
}
}
if (n > total_lines) n = total_lines;
percent = (float) n / (float) total_lines;
/*
* XXX: manually call the SIGWINCH handler, if set, to
* handle window resizes while we ignore the signal.
*/
if (op->sigwinch_workaround) {
/* Only call into the handler if it isn't one of the special
* pointer values from bits/signum-generic.h */
if (old_act.sa_handler != NULL &&
old_act.sa_handler != SIG_DFL &&
old_act.sa_handler != SIG_IGN &&
old_act.sa_handler != SIG_ERR) {
old_act.sa_handler(SIGWINCH);
}
}
ui_status_update(op, percent, NULL);
}
len += strlen(buf + len);
} /* while (1) */
nvfree(match_sizes);
/* Close the popen()'ed stream. */
ret = pclose(stream);
/*
* Restore the SIGWINCH signal disposition and handler, if any,
* to their original values.
*/
if (op->sigwinch_workaround)
sigaction(SIGWINCH, &old_act, NULL);
/* if the last character in the buffer is a newline, null it */
if ((len > 0) && (buf[len-1] == '\n')) buf[len-1] = '\0';
if (data) *data = buf;
else free(buf);
return ret;
} /* run_command() */
/*
* read_text_file() - open a text file, read its contents and return
* them to the caller in a newly allocated buffer. Returns TRUE on
* success and FALSE on failure.
*/
int read_text_file(const char *filename, char **buf)
{
FILE *fp;
int index = 0, buflen = 0;
int eof = FALSE;
char *line, *tmpbuf;
*buf = NULL;
fp = fopen(filename, "r");
if (!fp)
return FALSE;
while (((line = fget_next_line(fp, &eof)) != NULL)) {
if ((index + strlen(line) + 2) > buflen) {
buflen = 2 * (index + strlen(line) + 2);
tmpbuf = (char *)nvalloc(buflen);
if (!tmpbuf) {
if (*buf) nvfree(*buf);
fclose(fp);
return FALSE;
}
if (*buf) {
memcpy(tmpbuf, *buf, index);
nvfree(*buf);
}
*buf = tmpbuf;
}
index += sprintf(*buf + index, "%s\n", line);
nvfree(line);
if (eof) {
break;
}
}
fclose(fp);
return TRUE;
} /* read_text_file() */
/*
* find_system_utils() - search the $PATH (as well as some common
* additional directories) for the utilities that the installer will
* need to use. Returns TRUE on success and assigns the util fields
* in the option struct; it returns FALSE on failure.
*/
#define EXTRA_PATH "/bin:/usr/bin:/sbin:/usr/sbin:/usr/X11R6/bin:/usr/bin/X11"
/*
* Utils list; keep in sync with SystemUtils, SystemOptionalUtils, ModuleUtils
* and DevelopUtils enum types
*/
typedef struct {
const char *util;
const char *package;
} Util;
static const Util __utils[] = {
/* SystemUtils */
[LDCONFIG] = { "ldconfig", "glibc" },
[GREP] = { "grep", "grep" },
[DMESG] = { "dmesg", "util-linux" },
[TAIL] = { "tail", "coreutils" },
/* SystemOptionalUtils */
[OBJCOPY] = { "objcopy", "binutils" },
[CHCON] = { "chcon", "selinux" },
[SELINUX_ENABLED] = { "selinuxenabled", "selinux" },
[GETENFORCE] = { "getenforce", "selinux" },
[EXECSTACK] = { "execstack", "selinux" },
[PKG_CONFIG] = { "pkg-config", "pkg-config" },
[XSERVER] = { "X", "xserver" },
[OPENSSL] = { "openssl", "openssl" },
[DKMS] = { "dkms", "dkms" },
[SYSTEMCTL] = { "systemctl", "systemd" },
[TAR] = { "tar", "tar" },
/* ModuleUtils */
[MODPROBE] = { "modprobe", "module-init-tools' or 'kmod" },
[RMMOD] = { "rmmod", "module-init-tools' or 'kmod" },
[LSMOD] = { "lsmod", "module-init-tools' or 'kmod" },
[DEPMOD] = { "depmod", "module-init-tools' or 'kmod" },
/* DevelopUtils */
[CC] = { "cc", "gcc" },
[MAKE] = { "make", "make" },
[LD] = { "ld", "binutils" },
[TR] = { "tr", "coreutils" },
[SED] = { "sed", "sed" },
};
int find_system_utils(Options *op)
{
int i;
ui_expert(op, "Searching for system utilities:");
/* search the PATH for each utility */
for (i = MIN_SYSTEM_UTILS; i < MAX_SYSTEM_UTILS; i++) {
op->utils[i] = find_system_util(__utils[i].util);
if (!op->utils[i]) {
ui_error(op, "Unable to find the system utility `%s`; please "
"make sure you have the package '%s' installed. If "
"you do have %s installed, then please check that "
"`%s` is in your PATH.",
__utils[i].util, __utils[i].package,
__utils[i].package, __utils[i].util);
return FALSE;
}
ui_expert(op, "found `%s` : `%s`", __utils[i].util, op->utils[i]);
}
for (i = MIN_SYSTEM_OPTIONAL_UTILS; i < MAX_SYSTEM_OPTIONAL_UTILS; i++) {
op->utils[i] = find_system_util(__utils[i].util);
if (op->utils[i]) {
ui_expert(op, "found `%s` : `%s`", __utils[i].util, op->utils[i]);
}
}
/* If no program called `X` is found; try searching for Xorg */
if (op->utils[XSERVER] == NULL) {
op->utils[XSERVER] = find_system_util("Xorg");
if (op->utils[XSERVER]) {
ui_expert(op, "found `%s` : `%s`",
"Xorg", op->utils[XSERVER]);
}
}
return TRUE;
} /* find_system_utils() */
/*
* find_module_utils() - search the $PATH (as well as some common
* additional directories) for the utilities that the installer will
* need to use. Returns TRUE on success and assigns the util fields
* in the option struct; it returns FALSE on failures.
*/
int find_module_utils(Options *op)
{
int i;
ui_expert(op, "Searching for module utilities:");
/* search the PATH for each utility */
for (i = MIN_MODULE_UTILS; i < MAX_MODULE_UTILS; i++) {
op->utils[i] = find_system_util(__utils[i].util);
if (!op->utils[i]) {
ui_error(op, "Unable to find the module utility `%s`; please "
"make sure you have the package '%s' installed. If "
"you do have '%s' installed, then please check that "
"`%s` is in your PATH.",
__utils[i].util, __utils[i].package,
__utils[i].package, __utils[i].util);
return FALSE;
}
ui_expert(op, "found `%s` : `%s`", __utils[i].util, op->utils[i]);
};
return TRUE;
} /* find_module_utils() */
/*
* check_proc_modprobe_path() - check if the modprobe path reported
* via /proc matches the one determined earlier; also check if it can
* be accessed/executed.
*/
#define PROC_MODPROBE_PATH_FILE "/proc/sys/kernel/modprobe"
#define DEFAULT_MODPROBE "/sbin/modprobe"
int check_proc_modprobe_path(Options *op)
{
FILE *fp;
char *proc_modprobe = NULL, *found_modprobe;
struct stat st;
int ret, success = FALSE;
found_modprobe = op->utils[MODPROBE];
fp = fopen(PROC_MODPROBE_PATH_FILE, "r");
if (fp) {
proc_modprobe = fget_next_line(fp, NULL);
fclose(fp);
}
/* If the modprobe found by find_system_utils() is a symlink, resolve it */
ret = lstat(found_modprobe, &st);
if (ret == 0 && S_ISLNK(st.st_mode)) {
char *target = get_resolved_symlink_target(op, found_modprobe);
if (target && access(target, F_OK | X_OK) == 0) {
found_modprobe = target;
} else {
nvfree(target);
}
}
if (proc_modprobe) {
/* If the modprobe reported by the kernel is a symlink, resolve it */
ret = lstat(proc_modprobe, &st);
if (ret == 0 && S_ISLNK(st.st_mode)) {
char *target = get_resolved_symlink_target(op, proc_modprobe);
if (target && access(target, F_OK | X_OK) == 0) {
nvfree(proc_modprobe);
proc_modprobe = target;
} else {
nvfree(target);
}
}
/* Check to see if the modprobe reported by the kernel and the
* modprobe found by nvidia-installer match. */
if (strcmp(proc_modprobe, found_modprobe) == 0) {
success = TRUE;
} else {
if (access(proc_modprobe, F_OK | X_OK) == 0) {
ui_warn(op, "The path to the `modprobe` utility reported by "
"'%s', `%s`, differs from the path determined by "
"`nvidia-installer`, `%s`. Please verify that `%s` "
"works correctly and correct the path in '%s' if "
"it does not.",
PROC_MODPROBE_PATH_FILE, proc_modprobe, found_modprobe,
proc_modprobe, PROC_MODPROBE_PATH_FILE);
success = TRUE;
} else {
ui_error(op, "The path to the `modprobe` utility reported by "
"'%s', `%s`, differs from the path determined by "
"`nvidia-installer`, `%s`, and does not appear to "
"point to a valid `modprobe` binary. Please correct "
"the path in '%s'.",
PROC_MODPROBE_PATH_FILE, proc_modprobe, found_modprobe,
PROC_MODPROBE_PATH_FILE);
}
}
} else {
/* We failed to read from /proc/sys/kernel/modprobe, possibly because
* it doesn't exist or /proc isn't mounted. Assume a default modprobe
* path of /sbin/modprobe. */
char * found_mismatch;
if (strcmp(DEFAULT_MODPROBE, found_modprobe) == 0) {
found_mismatch = nvstrdup("");
} else {
found_mismatch = nvstrcat("This path differs from the one "
"determined by `nvidia-installer`, ",
found_modprobe, ". ", NULL);
}
if (access(DEFAULT_MODPROBE, F_OK | X_OK) == 0) {
ui_warn(op, "The file '%s' is unavailable; the X server will "
"use `" DEFAULT_MODPROBE "` as the path to the `modprobe` "
"utility. %sPlease verify that `" DEFAULT_MODPROBE
"` works correctly or mount the /proc file system and "
"verify that '%s' reports the correct path.",
PROC_MODPROBE_PATH_FILE, found_mismatch,
PROC_MODPROBE_PATH_FILE);
success = TRUE;
} else {
ui_error(op, "The file '%s' is unavailable; the X server will "
"use `" DEFAULT_MODPROBE "` as the path to the `modprobe` "
"utility. %s`" DEFAULT_MODPROBE "` does not appear to "
"point to a valid `modprobe` binary. Please create a "
"symbolic link from `" DEFAULT_MODPROBE "` to `%s` or "
"mount the /proc file system and verify that '%s' reports "
"the correct path.",
PROC_MODPROBE_PATH_FILE, found_mismatch,
found_modprobe, PROC_MODPROBE_PATH_FILE);
}
nvfree(found_mismatch);
}
nvfree(proc_modprobe);
if (found_modprobe != op->utils[MODPROBE]) {
nvfree(found_modprobe);
}
return success;
} /* check_proc_modprobe_path() */
/*
* check_development_tools() - check if the development tools needed
* to build custom kernel interfaces are available.
*/
static int check_development_tool(Options *op, int idx)
{
if (!op->utils[idx]) {
ui_error(op, "Unable to find the development tool `%s` in "
"your path; please make sure that you have the "
"package '%s' installed. If %s is installed on your "
"system, then please check that `%s` is in your "
"PATH.",
__utils[idx].util, __utils[idx].package,
__utils[idx].package, __utils[idx].util);
return FALSE;
}
ui_expert(op, "found `%s` : `%s`", __utils[idx].util, op->utils[idx]);
return TRUE;
}
int check_development_tools(Options *op, Package *p)
{
int i, ret;
op->utils[CC] = getenv("CC");
ui_expert(op, "Checking development tools:");
/*
* Check if the required toolchain components are installed on
* the system. Note that we skip the check for `cc` if the
* user specified the CC environment variable; we do this because
* `cc` may not be present in the path, nor the compiler named
* in $CC, but the installation may still succeed. $CC is sanity
* checked below.
*/
for (i = (op->utils[CC] != NULL) ? MIN_DEVELOP_UTILS + 1 : MIN_DEVELOP_UTILS;
i < MAX_DEVELOP_UTILS; i++) {
op->utils[i] = find_system_util(__utils[i].util);
if (!check_development_tool(op, i)) {
return FALSE;
}
}
/*
* Check if the libc development headers are installed; we need
* these to build the CC version check utility.
*/
if (access("/usr/include/stdio.h", F_OK) == -1) {
ui_error(op, "You do not appear to have libc header files "
"installed on your system. Please install your "
"distribution's libc development package.");
return FALSE;
}
if (!op->utils[CC]) op->utils[CC] = "cc";
ui_log(op, "Performing CC sanity check with CC=\"%s\".", op->utils[CC]);
ret = conftest_sanity_check(op, p->kernel_module_build_directory,
"CC", "cc_sanity_check");
if (ret) return TRUE;
return FALSE;
} /* check_development_tools() */
/*
* check_precompiled_kernel_interface_tools() - check if the development tools
* needed to link precompiled kernel interfaces are available.
*/
int check_precompiled_kernel_interface_tools(Options *op)
{
/*
* If precompiled info has been found we only need to check for
* a linker
*/
op->utils[LD] = find_system_util(__utils[LD].util);
return check_development_tool(op, LD);
} /* check_precompiled_kernel_interface_tools() */
/*
* find_system_util() - build a search path and search for the named
* utility. If the utility is found, the fully qualified path to the
* utility is returned. On failure NULL is returned.
*/
char *find_system_util(const char *util)
{
char *buf, *path, *file, *x, *y, c;
/* build the search path */
buf = getenv("PATH");
if (buf) {
path = nvstrcat(buf, ":", EXTRA_PATH, NULL);
} else {
path = nvstrdup(EXTRA_PATH);
}
/* search the PATH for the utility */
for (x = y = path; ; x++) {
if (*x == ':' || *x == '\0') {
struct stat st;
c = *x;
*x = '\0';
file = nvstrcat(y, "/", util, NULL);
*x = c;
if (stat(file, &st) == 0 && S_ISREG(st.st_mode) &&
(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0) {
/* If the path points to a regular file (or a symbolic link to a
* regular file), and it is executable by at least one of user,
* group, or other, use this path for the relevant utility. */
nvfree(path);
return file;
}
nvfree(file);
y = x + 1;
if (*x == '\0') break;
}
}
nvfree(path);
return NULL;
} /* find_system_util() */
/*
* continue_after_error() - tell the user that an error has occurred,
* and ask them if they would like to continue.
*
* Returns TRUE if the installer should continue.
*/
int continue_after_error(Options *op, const char *fmt, ...)
{
char *msg;
int ret;
NV_VSNPRINTF(msg, fmt);
ret = (ui_multiple_choice(op, CONTINUE_ABORT_CHOICES,
NUM_CONTINUE_ABORT_CHOICES,
CONTINUE_CHOICE, /* Default choice */
"The installer has encountered the following "
"error during installation: '%s'. Would you "
"like to continue installation anyway?",
msg) == CONTINUE_CHOICE);
nvfree(msg);
return ret;
} /* continue_after_error() */
/*
* do_install()
*/
int do_install(Options *op, Package *p, CommandList *c)
{
char *msg;
int len, ret;
len = strlen(p->description) + strlen(p->version) + 64;
msg = (char *) nvalloc(len);
snprintf(msg, len, "Installing '%s' (%s):",
p->description, p->version);
ret = execute_command_list(op, c, msg, "Installing");
free(msg);
if (!ret) return FALSE;
ui_log(op, "Driver file installation is complete.");
return TRUE;
} /* do_install() */
/*
* extract_version_string() - extract the NVIDIA driver version string
* from the given string. On failure, return NULL; on success, return
* a malloced string containing just the version string.
*
* The version string can have one of two forms: either the old
* "X.Y.ZZZZ" format (e.g., "1.0-9742"), or the new format where it is
* just a collection of period-separated numbers (e.g., "105.17.2").
* The length and number of periods in the newer format is arbitrary.
*
* Furthermore, we expect the new version format to be enclosed either
* in parenthesis or whitespace (or be at the start or end of the
* input string) and be at least 5 characters long. This allows us to
* distinguish the version string from other numbers such as the year
* or the old version format in input strings like this:
*
* "NVIDIA UNIX x86 Kernel Module 105.17.2 Fri Dec 15 09:54:45 PST 2006"
* "1.0-105917 (105.9.17)"
*/
char *extract_version_string(const char *str)
{
char c, *copiedString, *start, *end, *x, *version = NULL;
int state;
if (!str) return NULL;
copiedString = strdup(str);
x = copiedString;
/*
* look for a block of only numbers and periods; the version
* string must be surrounded by either whitespace, or the
* start/end of the string; we use a small state machine to parse
* the string
*/
start = NULL;
end = NULL;
#define STATE_IN_VERSION 0
#define STATE_NOT_IN_VERSION 1
#define STATE_LOOKING_FOR_VERSION 2
#define STATE_FOUND_VERSION 3