1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // dumpable.cfa -- Check if everything looks correctly set to dump core |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Wed Jan 05 13:53:22 2022 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Fri Jul 19 07:58:45 2024 |
---|
13 | // Update Count : 10 |
---|
14 | // |
---|
15 | |
---|
16 | #include <errno.h> |
---|
17 | #include <limits.h> |
---|
18 | #include <string.h> |
---|
19 | #include <unistd.h> |
---|
20 | |
---|
21 | #include <fstream.hfa> |
---|
22 | |
---|
23 | extern "C" { |
---|
24 | #include <fcntl.h> |
---|
25 | #include <sys/prctl.h> |
---|
26 | #include <sys/resource.h> |
---|
27 | #include <sys/statvfs.h> |
---|
28 | #include <sys/stat.h> |
---|
29 | #include <sys/types.h> |
---|
30 | } |
---|
31 | |
---|
32 | void check_ulimit() { |
---|
33 | struct rlimit rlp; |
---|
34 | getrlimit( RLIMIT_CORE, &rlp ); |
---|
35 | if ( rlp.rlim_cur < 536870912 ) { |
---|
36 | serr | "Soft core limit is less than ~500Mb: " | rlp.rlim_cur; |
---|
37 | } // if |
---|
38 | |
---|
39 | if ( rlp.rlim_max < 536870912 ) { |
---|
40 | serr | "Hard core limit is less than ~500Mb: " | rlp.rlim_max; |
---|
41 | } // if |
---|
42 | } |
---|
43 | |
---|
44 | void check_permission() { |
---|
45 | char myExe[PATH_MAX]; |
---|
46 | ssize_t n = readlink( "/proc/self/exe", myExe, sizeof(myExe)); |
---|
47 | if ( n < 0 ) { |
---|
48 | perror( "readlink(/proc/self/exe ) error" ); |
---|
49 | return 1; |
---|
50 | } // if |
---|
51 | myExe[n] = '\0'; |
---|
52 | |
---|
53 | if ( int r = access( myExe, F_OK ); r != 0 ) serr | "Expected current executable does not exist!" | r | errno; |
---|
54 | if ( int r = access( myExe, R_OK ); r != 0 ) serr | "No read access for current executable" | r | errno; |
---|
55 | |
---|
56 | char myCwd[PATH_MAX]; |
---|
57 | if ( getcwd( myCwd, sizeof(myCwd ) ) == 0p ) { |
---|
58 | perror( "getcwd() error" ); |
---|
59 | return; |
---|
60 | } // if |
---|
61 | |
---|
62 | if ( access( myCwd, F_OK ) != 0 ) serr | "Expected current working directory does not exist!"; |
---|
63 | if ( access( myCwd, R_OK ) != 0 ) serr | "No read access for current working directory"; |
---|
64 | if ( access( myCwd, W_OK ) != 0 ) serr | "No write access for current working directory"; |
---|
65 | } |
---|
66 | |
---|
67 | void check_free_space() { |
---|
68 | struct statvfs buf; |
---|
69 | if ( statvfs( ".", &buf ) != 0 ) { |
---|
70 | perror( "statvfs() error" ); |
---|
71 | return; |
---|
72 | } // if |
---|
73 | |
---|
74 | uint64_t avail = buf.f_bavail; |
---|
75 | avail *= buf.f_bsize; |
---|
76 | if ( avail < 536870912_l64u ) { |
---|
77 | serr | "Available diskspace is less than ~500Mb: " | avail; |
---|
78 | } // if |
---|
79 | |
---|
80 | if ( buf.f_favail < 10 ) { |
---|
81 | serr | "Available inodes is less than 10: " | buf.f_favail; |
---|
82 | } // if |
---|
83 | |
---|
84 | if ( buf.f_flag & ST_RDONLY ) { |
---|
85 | serr | "Filesystem is read only"; |
---|
86 | } // if |
---|
87 | } |
---|
88 | |
---|
89 | void check_noconflict() { |
---|
90 | const char * name = "./core"; |
---|
91 | if ( access( name, F_OK ) == 0 ) serr | "File \"" | name | "\" already exists"; |
---|
92 | } |
---|
93 | |
---|
94 | void check_dumpflag() { |
---|
95 | int r = prctl( PR_GET_DUMPABLE, 0, 0, 0, 0 ); |
---|
96 | if ( r < 0 ) { |
---|
97 | perror( "prctl( PR_GET_DUMPABLE ) error" ); |
---|
98 | return; |
---|
99 | } // if |
---|
100 | |
---|
101 | if ( r != 1 ) serr | "dumpable attribute not set to 1 \"( SUID_DUMP_USER, process is dumpable )\", was" | r; |
---|
102 | } |
---|
103 | |
---|
104 | void check_core_pattern() { |
---|
105 | int ret; |
---|
106 | int cp = open( "/proc/sys/kernel/core_pattern", 0, O_RDONLY ); |
---|
107 | |
---|
108 | if ( cp < 0 ) { |
---|
109 | perror( "open(/proc/sys/kernel/core_pattern, O_RDONLY ) error" ); |
---|
110 | return; |
---|
111 | } // if |
---|
112 | |
---|
113 | try { |
---|
114 | const char * expected = "core\n"; |
---|
115 | const int sz = sizeof( "core\n" ); |
---|
116 | char buf[512]; |
---|
117 | ret = read( cp, buf, 512 ); |
---|
118 | if ( ret < 0 ) { |
---|
119 | perror( "core pattern read error" ); |
---|
120 | return; |
---|
121 | } // if |
---|
122 | |
---|
123 | ret = strncmp( expected, buf, sz - 1 ); |
---|
124 | if ( ret != 0 ) { |
---|
125 | serr | "Apport is supported on your system, which means the test-suite core-dump feature does not work." | nl |
---|
126 | | "This is not a test failure, just a limitation on debugging output should a test fail."; |
---|
127 | } // if |
---|
128 | } finally { |
---|
129 | ret = close( cp ); |
---|
130 | if ( ret < 0 ) perror( "close( /proc/sys/kernel/core_pattern ) error" ); |
---|
131 | } // try |
---|
132 | } |
---|
133 | |
---|
134 | int main() { |
---|
135 | check_ulimit(); |
---|
136 | check_permission(); |
---|
137 | check_free_space(); |
---|
138 | check_noconflict(); |
---|
139 | check_dumpflag(); |
---|
140 | check_core_pattern(); |
---|
141 | sout | "Done"; |
---|
142 | } |
---|