-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesys.c
4167 lines (3579 loc) · 108 KB
/
filesys.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
/* filesys.c - core analysis suite
*
* Copyright (C) 1999, 2000, 2001, 2002 Mission Critical Linux, Inc.
* Copyright (C) 2002-2015 David Anderson
* Copyright (C) 2002-2015 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*/
#include "defs.h"
#include <linux/major.h>
#include <regex.h>
#include <sys/utsname.h>
static void show_mounts(ulong, int, struct task_context *);
static int find_booted_kernel(void);
static int find_booted_system_map(void);
static int verify_utsname(char *);
static char **build_searchdirs(int, int *);
static int build_kernel_directory(char *);
static int redhat_kernel_directory_v1(char *);
static int redhat_kernel_directory_v2(char *);
static int redhat_debug_directory(char *);
static ulong *create_dentry_array(ulong, int *);
static ulong *create_dentry_array_percpu(ulong, int *);
static void show_fuser(char *, char *);
static int mount_point(char *);
static int open_file_reference(struct reference *);
static void memory_source_init(void);
static int get_pathname_component(ulong, ulong, int, char *, char *);
static ulong *get_mount_list(int *, struct task_context *);
char *inode_type(char *, char *);
static void match_proc_version(void);
static void get_live_memory_source(void);
static int memory_driver_module_loaded(int *);
static int insmod_memory_driver_module(void);
static int get_memory_driver_dev(dev_t *);
static int memory_driver_init(void);
static int create_memory_device(dev_t);
static void *radix_tree_lookup(ulong, ulong, int);
static int match_file_string(char *, char *, char *);
static ulong get_root_vfsmount(char *);
static void check_live_arch_mismatch(void);
#define DENTRY_CACHE (20)
#define INODE_CACHE (20)
#define FILE_CACHE (20)
static struct filesys_table {
char *dentry_cache;
ulong cached_dentry[DENTRY_CACHE];
ulong cached_dentry_hits[DENTRY_CACHE];
int dentry_cache_index;
ulong dentry_cache_fills;
char *inode_cache;
ulong cached_inode[INODE_CACHE];
ulong cached_inode_hits[INODE_CACHE];
int inode_cache_index;
ulong inode_cache_fills;
char *file_cache;
ulong cached_file[FILE_CACHE];
ulong cached_file_hits[FILE_CACHE];
int file_cache_index;
ulong file_cache_fills;
} filesys_table = { 0 };
static struct filesys_table *ft = &filesys_table;
/*
* Open the namelist, dumpfile and output devices.
*/
void
fd_init(void)
{
pc->nfd = pc->kfd = pc->mfd = pc->dfd = -1;
if ((pc->nullfp = fopen("/dev/null", "w+")) == NULL)
error(INFO, "cannot open /dev/null (for extraneous output)");
if (REMOTE())
remote_fd_init();
else {
if (pc->namelist && pc->namelist_debug && pc->system_map) {
error(INFO,
"too many namelist options:\n %s\n %s\n %s\n",
pc->namelist, pc->namelist_debug,
pc->system_map);
program_usage(SHORT_FORM);
}
if (pc->namelist) {
if (XEN_HYPER_MODE() && !pc->dumpfile)
error(FATAL,
"Xen hypervisor mode requires a dumpfile\n");
if (!pc->dumpfile && !get_proc_version())
error(INFO, "/proc/version: %s\n",
strerror(errno));
} else {
if (pc->dumpfile) {
error(INFO, "namelist argument required\n");
program_usage(SHORT_FORM);
}
if (!pc->dumpfile)
check_live_arch_mismatch();
if (!find_booted_kernel())
program_usage(SHORT_FORM);
}
if (!pc->dumpfile) {
pc->flags |= LIVE_SYSTEM;
get_live_memory_source();
}
if ((pc->nfd = open(pc->namelist, O_RDONLY)) < 0)
error(FATAL, "%s: %s\n", pc->namelist, strerror(errno));
else {
close(pc->nfd);
pc->nfd = -1;
}
if (ACTIVE() && !(pc->namelist_debug || pc->system_map)) {
memory_source_init();
match_proc_version();
}
}
memory_source_init();
if (CRASHDEBUG(1)) {
fprintf(fp, "readmem: %s() ", readmem_function_name());
if (ACTIVE()) {
fprintf(fp, "-> %s ", pc->live_memsrc);
if (pc->flags & MEMMOD)
fprintf(fp, "(module)");
else if (pc->flags & CRASHBUILTIN)
fprintf(fp, "(built-in)");
}
fprintf(fp, "\n");
}
}
/*
* Do whatever's necessary to handle the memory source.
*/
static void
memory_source_init(void)
{
if (REMOTE() && !(pc->flags & MEMSRC_LOCAL))
return;
if (pc->flags & KERNEL_DEBUG_QUERY)
return;
if (ACTIVE()) {
if (pc->mfd != -1) /* already been here */
return;
if (!STREQ(pc->live_memsrc, "/dev/mem") &&
STREQ(pc->live_memsrc, pc->memory_device)) {
if (memory_driver_init())
return;
error(INFO, "cannot initialize crash memory driver\n");
error(INFO, "using /dev/mem\n\n");
pc->flags &= ~MEMMOD;
pc->flags |= DEVMEM;
pc->readmem = read_dev_mem;
pc->writemem = write_dev_mem;
pc->live_memsrc = "/dev/mem";
}
if (STREQ(pc->live_memsrc, "/dev/mem")) {
if ((pc->mfd = open("/dev/mem", O_RDWR)) < 0) {
if ((pc->mfd = open("/dev/mem", O_RDONLY)) < 0)
error(FATAL, "/dev/mem: %s\n",
strerror(errno));
} else
pc->flags |= MFD_RDWR;
} else if (STREQ(pc->live_memsrc, "/proc/kcore")) {
if ((pc->mfd = open("/proc/kcore", O_RDONLY)) < 0)
error(FATAL, "/proc/kcore: %s\n",
strerror(errno));
if (!proc_kcore_init(fp))
error(FATAL,
"/proc/kcore: initialization failed\n");
} else
error(FATAL, "unknown memory device: %s\n",
pc->live_memsrc);
return;
}
if (pc->dumpfile) {
if (!file_exists(pc->dumpfile, NULL))
error(FATAL, "%s: %s\n", pc->dumpfile,
strerror(ENOENT));
if (!(pc->flags & DUMPFILE_TYPES))
error(FATAL, "%s: dump format not supported!\n",
pc->dumpfile);
if (pc->flags & NETDUMP) {
if (!netdump_init(pc->dumpfile, fp))
error(FATAL, "%s: initialization failed\n",
pc->dumpfile);
} else if (pc->flags & KDUMP) {
if (!kdump_init(pc->dumpfile, fp))
error(FATAL, "%s: initialization failed\n",
pc->dumpfile);
} else if (pc->flags & XENDUMP) {
if (!xendump_init(pc->dumpfile, fp))
error(FATAL, "%s: initialization failed\n",
pc->dumpfile);
} else if (pc->flags & KVMDUMP) {
if (!kvmdump_init(pc->dumpfile, fp))
error(FATAL, "%s: initialization failed\n",
pc->dumpfile);
} else if (pc->flags & DISKDUMP) {
if (!diskdump_init(pc->dumpfile, fp))
error(FATAL, "%s: initialization failed\n",
pc->dumpfile);
} else if (pc->flags & LKCD) {
if ((pc->dfd = open(pc->dumpfile, O_RDONLY)) < 0)
error(FATAL, "%s: %s\n", pc->dumpfile,
strerror(errno));
if (!lkcd_dump_init(fp, pc->dfd, pc->dumpfile))
error(FATAL, "%s: initialization failed\n",
pc->dumpfile);
} else if (pc->flags & S390D) {
if (!s390_dump_init(pc->dumpfile))
error(FATAL, "%s: initialization failed\n",
pc->dumpfile);
} else if (pc->flags & VMWARE_VMSS) {
if (!vmware_vmss_init(pc->dumpfile, fp))
error(FATAL, "%s: initialization failed\n",
pc->dumpfile);
}
}
}
/*
* If only a namelist argument is entered for a live system, and the
* version string doesn't match /proc/version, try to avert a failure
* by assigning it to a matching System.map.
*/
static void
match_proc_version(void)
{
char buffer[BUFSIZE], *p1, *p2;
if (pc->flags & KERNEL_DEBUG_QUERY)
return;
if (!strlen(kt->proc_version))
return;
if (match_file_string(pc->namelist, kt->proc_version, buffer)) {
if (CRASHDEBUG(1)) {
fprintf(fp, "/proc/version:\n%s\n", kt->proc_version);
fprintf(fp, "%s:\n%s", pc->namelist, buffer);
}
return;
}
error(WARNING, "%s%sand /proc/version do not match!\n\n",
pc->namelist,
strlen(pc->namelist) > 39 ? "\n " : " ");
/*
* find_booted_system_map() requires VTOP(), which used to be a
* hardwired masking of the kernel address. But some architectures
* may not know what their physical base address is at this point,
* and others may have different machdep->kvbase values, so for all
* but the 0-based kernel virtual address architectures, bail out
* here with a relevant error message.
*/
if (!machine_type("S390") && !machine_type("S390X")) {
p1 = &kt->proc_version[strlen("Linux version ")];
p2 = strstr(p1, " ");
*p2 = NULLCHAR;
error(WARNING, "/proc/version indicates kernel version: %s\n", p1);
error(FATAL, "please use the vmlinux file for that kernel version, or try using\n"
" the System.map for that kernel version as an additional argument.\n", p1);
clean_exit(1);
}
if (find_booted_system_map())
pc->flags |= SYSMAP;
}
#define CREATE 1
#define DESTROY 0
#define DEFAULT_SEARCHDIRS 5
static char **
build_searchdirs(int create, int *preferred)
{
int i;
int cnt, start;
DIR *dirp;
struct dirent *dp;
char dirbuf[BUFSIZE];
static char **searchdirs = { 0 };
static char *default_searchdirs[DEFAULT_SEARCHDIRS+1] = {
"/usr/src/linux/",
"/boot/",
"/boot/efi/redhat",
"/boot/efi/EFI/redhat",
"/",
NULL
};
if (!create) {
if (searchdirs) {
for (i = DEFAULT_SEARCHDIRS; searchdirs[i]; i++)
free(searchdirs[i]);
free(searchdirs);
}
return NULL;
}
if (preferred)
*preferred = 0;
/*
* Allow, at a minimum, the defaults plus an extra three directories
* for the two possible /usr/src/redhat/BUILD/kernel-xxx locations
* plus the Red Hat debug directory.
*/
cnt = DEFAULT_SEARCHDIRS + 3;
if ((dirp = opendir("/usr/src"))) {
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
cnt++;
if ((searchdirs = calloc(cnt, sizeof(char *))) == NULL) {
error(INFO, "/usr/src/ directory list malloc: %s\n",
strerror(errno));
closedir(dirp);
return default_searchdirs;
}
for (i = 0; i < DEFAULT_SEARCHDIRS; i++)
searchdirs[i] = default_searchdirs[i];
cnt = DEFAULT_SEARCHDIRS;
rewinddir(dirp);
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
if (STREQ(dp->d_name, "linux") ||
STREQ(dp->d_name, "redhat") ||
STREQ(dp->d_name, ".") ||
STREQ(dp->d_name, ".."))
continue;
sprintf(dirbuf, "/usr/src/%s", dp->d_name);
if (mount_point(dirbuf))
continue;
if (!is_directory(dirbuf))
continue;
if ((searchdirs[cnt] = (char *)
malloc(strlen(dirbuf)+2)) == NULL) {
error(INFO,
"/usr/src/ directory entry malloc: %s\n",
strerror(errno));
break;
}
sprintf(searchdirs[cnt], "%s/", dirbuf);
cnt++;
}
closedir(dirp);
searchdirs[cnt] = NULL;
} else {
if ((searchdirs = calloc(cnt, sizeof(char *))) == NULL) {
error(INFO, "search directory list malloc: %s\n",
strerror(errno));
closedir(dirp);
return default_searchdirs;
}
for (i = 0; i < DEFAULT_SEARCHDIRS; i++)
searchdirs[i] = default_searchdirs[i];
cnt = DEFAULT_SEARCHDIRS;
}
if (build_kernel_directory(dirbuf)) {
if ((searchdirs[cnt] = (char *)
malloc(strlen(dirbuf)+2)) == NULL) {
error(INFO,
"/lib/modules/ directory entry malloc: %s\n",
strerror(errno));
} else {
sprintf(searchdirs[cnt], "%s/", dirbuf);
cnt++;
}
}
if (redhat_kernel_directory_v1(dirbuf)) {
if ((searchdirs[cnt] = (char *)
malloc(strlen(dirbuf)+2)) == NULL) {
error(INFO,
"/usr/src/redhat directory entry malloc: %s\n",
strerror(errno));
} else {
sprintf(searchdirs[cnt], "%s/", dirbuf);
cnt++;
}
}
if (redhat_kernel_directory_v2(dirbuf)) {
if ((searchdirs[cnt] = (char *)
malloc(strlen(dirbuf)+2)) == NULL) {
error(INFO,
"/usr/src/redhat directory entry malloc: %s\n",
strerror(errno));
} else {
sprintf(searchdirs[cnt], "%s/", dirbuf);
cnt++;
}
}
if (redhat_debug_directory(dirbuf)) {
if ((searchdirs[cnt] = (char *)
malloc(strlen(dirbuf)+2)) == NULL) {
error(INFO, "%s directory entry malloc: %s\n",
dirbuf, strerror(errno));
} else {
sprintf(searchdirs[cnt], "%s/", dirbuf);
if (preferred)
*preferred = cnt;
cnt++;
}
}
searchdirs[cnt] = NULL;
if (CRASHDEBUG(1)) {
i = start = preferred ? *preferred : 0;
do {
fprintf(fp, "searchdirs[%d]: %s\n",
i, searchdirs[i]);
if (++i == cnt) {
if (start != 0)
i = 0;
else
break;
}
} while (i != start);
}
return searchdirs;
}
static int
build_kernel_directory(char *buf)
{
char *p1, *p2;
if (!strstr(kt->proc_version, "Linux version "))
return FALSE;
BZERO(buf, BUFSIZE);
sprintf(buf, "/lib/modules/");
p1 = &kt->proc_version[strlen("Linux version ")];
p2 = &buf[strlen(buf)];
while (*p1 != ' ')
*p2++ = *p1++;
strcat(buf, "/build");
return TRUE;
}
static int
redhat_kernel_directory_v1(char *buf)
{
char *p1, *p2;
if (!strstr(kt->proc_version, "Linux version "))
return FALSE;
BZERO(buf, BUFSIZE);
sprintf(buf, "/usr/src/redhat/BUILD/kernel-");
p1 = &kt->proc_version[strlen("Linux version ")];
p2 = &buf[strlen(buf)];
while (((*p1 >= '0') && (*p1 <= '9')) || (*p1 == '.'))
*p2++ = *p1++;
strcat(buf, "/linux");
return TRUE;
}
static int
redhat_kernel_directory_v2(char *buf)
{
char *p1, *p2;
if (!strstr(kt->proc_version, "Linux version "))
return FALSE;
BZERO(buf, BUFSIZE);
sprintf(buf, "/usr/src/redhat/BUILD/kernel-");
p1 = &kt->proc_version[strlen("Linux version ")];
p2 = &buf[strlen(buf)];
while (((*p1 >= '0') && (*p1 <= '9')) || (*p1 == '.'))
*p2++ = *p1++;
strcat(buf, "/linux-");
p1 = &kt->proc_version[strlen("Linux version ")];
p2 = &buf[strlen(buf)];
while (((*p1 >= '0') && (*p1 <= '9')) || (*p1 == '.'))
*p2++ = *p1++;
return TRUE;
}
static int
redhat_debug_directory(char *buf)
{
char *p1, *p2;
if (!strstr(kt->proc_version, "Linux version "))
return FALSE;
BZERO(buf, BUFSIZE);
sprintf(buf, "%s/", pc->redhat_debug_loc);
p1 = &kt->proc_version[strlen("Linux version ")];
p2 = &buf[strlen(buf)];
while (*p1 != ' ')
*p2++ = *p1++;
return TRUE;
}
/*
* If a namelist was not entered, presume we're using the currently-running
* kernel. Read its version string from /proc/version, and then look in
* the search directories for a kernel with the same version string embedded
* in it.
*/
static int
find_booted_kernel(void)
{
char kernel[BUFSIZE];
char buffer[BUFSIZE];
char **searchdirs;
int i, preferred, wrapped;
DIR *dirp;
struct dirent *dp;
int found;
pc->flags |= FINDKERNEL;
fflush(fp);
if (!file_exists("/proc/version", NULL)) {
error(INFO,
"/proc/version: %s: cannot determine booted kernel\n",
strerror(ENOENT));
return FALSE;
}
if (!get_proc_version()) {
error(INFO, "/proc/version: %s\n", strerror(errno));
return FALSE;
}
if (CRASHDEBUG(1))
fprintf(fp, "\nfind_booted_kernel: search for [%s]\n",
kt->proc_version);
searchdirs = build_searchdirs(CREATE, &preferred);
for (i = preferred, wrapped = found = FALSE; !found; i++) {
if (!searchdirs[i]) {
if (preferred && !wrapped) {
wrapped = TRUE;
i = 0;
} else
break;
} else if (wrapped && (preferred == i))
break;
dirp = opendir(searchdirs[i]);
if (!dirp)
continue;
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
if (dp->d_name[0] == '.')
continue;
sprintf(kernel, "%s%s", searchdirs[i], dp->d_name);
if (mount_point(kernel) ||
!file_readable(kernel) ||
!is_elf_file(kernel))
continue;
if (CRASHDEBUG(1))
fprintf(fp, "find_booted_kernel: check: %s\n",
kernel);
found = match_file_string(kernel, kt->proc_version, buffer);
if (found)
break;
}
closedir(dirp);
}
mount_point(DESTROY);
build_searchdirs(DESTROY, NULL);
if (found) {
if ((pc->namelist = (char *)malloc
(strlen(kernel)+1)) == NULL)
error(FATAL, "booted kernel name malloc: %s\n",
strerror(errno));
else {
strcpy(pc->namelist, kernel);
if (CRASHDEBUG(1))
fprintf(fp, "find_booted_kernel: found: %s\n",
pc->namelist);
return TRUE;
}
}
error(INFO,
"cannot find booted kernel -- please enter namelist argument\n\n");
return FALSE;
}
/*
* Determine whether a file is a mount point, without the benefit of stat().
* This horrendous kludge is necessary to avoid uninterruptible stat() or
* fstat() calls on nfs mount-points where the remote directory is no longer
* available.
*/
static int
mount_point(char *name)
{
int i;
static int mount_points_gathered = -1;
static char **mount_points;
char *arglist[MAXARGS];
char buf[BUFSIZE];
char mntfile[BUFSIZE];
int argc, found;
FILE *mp;
/*
* The first time through, stash a list of mount points.
*/
if (mount_points_gathered < 0) {
found = mount_points_gathered = 0;
if (file_exists("/proc/mounts", NULL))
sprintf(mntfile, "/proc/mounts");
else if (file_exists("/etc/mtab", NULL))
sprintf(mntfile, "/etc/mtab");
else
return FALSE;
if ((mp = fopen(mntfile, "r")) == NULL)
return FALSE;
while (fgets(buf, BUFSIZE, mp)) {
argc = parse_line(buf, arglist);
if (argc < 2)
continue;
found++;
}
pclose(mp);
if (!(mount_points = (char **)malloc(sizeof(char *) * found)))
return FALSE;
if ((mp = fopen(mntfile, "r")) == NULL)
return FALSE;
i = 0;
while (fgets(buf, BUFSIZE, mp) &&
(mount_points_gathered < found)) {
argc = parse_line(buf, arglist);
if (argc < 2)
continue;
if ((mount_points[i] = (char *)
malloc(strlen(arglist[1])*2))) {
strcpy(mount_points[i], arglist[1]);
mount_points_gathered++, i++;
}
}
pclose(mp);
if (CRASHDEBUG(2))
for (i = 0; i < mount_points_gathered; i++)
fprintf(fp, "mount_points[%d]: %s (%lx)\n",
i, mount_points[i],
(ulong)mount_points[i]);
}
/*
* A null name string means we're done with this routine forever,
* so the malloc'd memory can be freed.
*/
if (!name) {
for (i = 0; i < mount_points_gathered; i++)
free(mount_points[i]);
free(mount_points);
return FALSE;
}
for (i = 0; i < mount_points_gathered; i++) {
if (STREQ(name, mount_points[i]))
return TRUE;
}
return FALSE;
}
/*
* If /proc/version exists, get it for verification purposes later.
*/
int
get_proc_version(void)
{
FILE *version;
if (strlen(kt->proc_version)) /* been here, done that... */
return TRUE;
if (!file_exists("/proc/version", NULL))
return FALSE;
if ((version = fopen("/proc/version", "r")) == NULL)
return FALSE;
if (fread(&kt->proc_version, sizeof(char),
BUFSIZE-1, version) <= 0) {
fclose(version);
return FALSE;
}
fclose(version);
strip_linefeeds(kt->proc_version);
return TRUE;
}
/*
* Given a non-matching kernel namelist, try to find a System.map file
* that has a system_utsname whose contents match /proc/version.
*/
static int
find_booted_system_map(void)
{
char system_map[BUFSIZE];
char **searchdirs;
int i;
DIR *dirp;
struct dirent *dp;
int found;
fflush(fp);
if (!file_exists("/proc/version", NULL)) {
error(INFO,
"/proc/version: %s: cannot determine booted System.map\n",
strerror(ENOENT));
return FALSE;
}
if (!get_proc_version()) {
error(INFO, "/proc/version: %s\n", strerror(errno));
return FALSE;
}
found = FALSE;
/*
* To avoid a search, try the obvious first.
*/
sprintf(system_map, "/boot/System.map");
if (file_readable(system_map) && verify_utsname(system_map)) {
found = TRUE;
} else {
searchdirs = build_searchdirs(CREATE, NULL);
for (i = 0; !found && searchdirs[i]; i++) {
dirp = opendir(searchdirs[i]);
if (!dirp)
continue;
for (dp = readdir(dirp); dp != NULL;
dp = readdir(dirp)) {
if (!strstr(dp->d_name, "System.map"))
continue;
sprintf(system_map, "%s%s", searchdirs[i],
dp->d_name);
if (mount_point(system_map) ||
!file_readable(system_map) ||
!is_system_map(system_map))
continue;
if (verify_utsname(system_map)) {
found = TRUE;
break;
}
}
closedir(dirp);
}
mount_point(DESTROY);
build_searchdirs(DESTROY, NULL);
}
if (found) {
if ((pc->system_map = (char *)malloc
(strlen(system_map)+1)) == NULL)
error(FATAL, "booted system map name malloc: %s\n",
strerror(errno));
strcpy(pc->system_map, system_map);
if (CRASHDEBUG(1))
fprintf(fp, "find_booted_system_map: found: %s\n",
pc->system_map);
return TRUE;
}
error(INFO,
"cannot find booted system map -- please enter namelist or system map\n\n");
return FALSE;
}
/*
* Read the system_utsname from /dev/mem, based upon the address found
* in the passed-in System.map file, and compare it to /proc/version.
*/
static int
verify_utsname(char *system_map)
{
char buffer[BUFSIZE];
ulong value;
struct new_utsname new_utsname;
if (CRASHDEBUG(1))
fprintf(fp, "verify_utsname: check: %s\n", system_map);
if (!match_file_string(system_map, "D system_utsname", buffer))
return FALSE;
if (extract_hex(buffer, &value, NULLCHAR, TRUE) &&
(READMEM(pc->mfd, &new_utsname,
sizeof(struct new_utsname), value,
VTOP(value)) > 0) &&
ascii_string(new_utsname.release) &&
ascii_string(new_utsname.version) &&
STRNEQ(new_utsname.release, "2.") &&
(strlen(new_utsname.release) > 4) &&
(strlen(new_utsname.version) > 27)) {
if (CRASHDEBUG(1)) {
fprintf(fp, "release: [%s]\n", new_utsname.release);
fprintf(fp, "version: [%s]\n", new_utsname.version);
}
if (strstr(kt->proc_version, new_utsname.release) &&
strstr(kt->proc_version, new_utsname.version)) {
return TRUE;
}
}
return FALSE;
}
/*
* Determine whether a file exists, using the caller's stat structure if
* one was passed in.
*/
int
file_exists(char *file, struct stat *sp)
{
struct stat sbuf;
if (stat(file, sp ? sp : &sbuf) == 0)
return TRUE;
return FALSE;
}
/*
* Determine whether a file exists, and if so, if it's readable.
*/
int
file_readable(char *file)
{
char tmp;
int fd;
if (!file_exists(file, NULL))
return FALSE;
if ((fd = open(file, O_RDONLY)) < 0)
return FALSE;
if (read(fd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
close(fd);
return FALSE;
}
close(fd);
return TRUE;
}
/*
* Quick file checksummer.
*/
int
file_checksum(char *file, long *retsum)
{
int i;
int fd;
ssize_t cnt;
char buf[MIN_PAGE_SIZE];
long csum;
if ((fd = open(file, O_RDONLY)) < 0)
return FALSE;
csum = 0;
BZERO(buf, MIN_PAGE_SIZE);
while ((cnt = read(fd, buf, MIN_PAGE_SIZE)) > 0) {
for (i = 0; i < cnt; i++)
csum += buf[i];
BZERO(buf, MIN_PAGE_SIZE);
}
close(fd);
*retsum = csum;
return TRUE;
}
int
is_directory(char *file)
{
struct stat sbuf;
if (!file || !strlen(file))
return(FALSE);
if (stat(file, &sbuf) == -1)
return(FALSE); /* This file doesn't exist. */
return((sbuf.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
}
/*
* Search a directory tree for filename, and if found, return a temporarily
* allocated buffer containing the full pathname. The "done" business is
* protection against fgets() prematurely returning NULL before the find
* command completes. (I thought this was impossible until I saw it happen...)
* When time permits, rewrite this doing the search by hand.
*/
char *
search_directory_tree(char *directory, char *file, int follow_links)