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 : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : |
---|
14 | // |
---|
15 | |
---|
16 | #include <errno.h> |
---|
17 | #include <limits.h> |
---|
18 | #include <string.h> |
---|
19 | |
---|
20 | #include <fstream.hfa> |
---|
21 | |
---|
22 | extern "C" { |
---|
23 | #include <fcntl.h> |
---|
24 | #include <unistd.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 | } |
---|
38 | |
---|
39 | if ( rlp.rlim_max < 536870912 ) { |
---|
40 | serr | "Hard core limit is less than ~500Mb: " | rlp.rlim_max; |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | void check_permission() { |
---|
45 | { |
---|
46 | char myExe[PATH_MAX]; |
---|
47 | ssize_t n = readlink("/proc/self/exe", myExe, sizeof(myExe)); |
---|
48 | if (n < 0 ) { |
---|
49 | perror("readlink(/proc/self/exe ) error"); |
---|
50 | return 1; |
---|
51 | } |
---|
52 | myExe[n] = '\0'; |
---|
53 | |
---|
54 | if ( int r = access(myExe, F_OK ); r != 0 ) serr | "Expected current executable does not exist!" | r | errno; |
---|
55 | if ( int r = access(myExe, R_OK ); r != 0 ) serr | "No read access for current executable" | r | errno; |
---|
56 | } |
---|
57 | |
---|
58 | { |
---|
59 | char myCwd[PATH_MAX]; |
---|
60 | if (getcwd(myCwd, sizeof(myCwd )) == 0p ) { |
---|
61 | perror("getcwd() error"); |
---|
62 | return; |
---|
63 | } |
---|
64 | |
---|
65 | if ( access(myCwd, F_OK ) != 0 ) serr | "Expected current working directory does not exist!"; |
---|
66 | if ( access(myCwd, R_OK ) != 0 ) serr | "No read access for current working directory"; |
---|
67 | if ( access(myCwd, W_OK ) != 0 ) serr | "No write access for current working directory"; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | void check_free_space() { |
---|
72 | struct statvfs buf; |
---|
73 | if ( statvfs(".", &buf ) != 0 ) { |
---|
74 | perror("statvfs() error"); |
---|
75 | return; |
---|
76 | } |
---|
77 | |
---|
78 | uint64_t avail = buf.f_bavail; |
---|
79 | avail *= buf.f_bsize; |
---|
80 | if ( avail < 536870912_l64u ) { |
---|
81 | serr | "Available diskspace is less than ~500Mb: " | avail; |
---|
82 | } |
---|
83 | |
---|
84 | if ( buf.f_favail < 10 ) { |
---|
85 | serr | "Available inodes is less than 10: " | buf.f_favail; |
---|
86 | } |
---|
87 | |
---|
88 | if ( buf.f_flag & ST_RDONLY ) { |
---|
89 | serr | "Filesystem is read only"; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | void check_noconflict() { |
---|
94 | char * name = "./core"; |
---|
95 | if ( access("./core", F_OK ) == 0 ) serr | "A file of the core name ('" | name | "') already exists"; |
---|
96 | } |
---|
97 | |
---|
98 | void check_dumpflag() { |
---|
99 | int r = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0 ); |
---|
100 | if ( r < 0 ) { |
---|
101 | perror("prctl(PR_GET_DUMPABLE ) error"); |
---|
102 | return; |
---|
103 | } |
---|
104 | |
---|
105 | if ( r != 1 ) serr | "dumpable attribute not set to 1 \"(SUID_DUMP_USER, process is dumpable )\", was" | r; |
---|
106 | } |
---|
107 | |
---|
108 | void check_core_pattern() { |
---|
109 | int ret; |
---|
110 | int cp = open("/proc/sys/kernel/core_pattern", 0, O_RDONLY ); |
---|
111 | |
---|
112 | if ( cp < 0 ) { |
---|
113 | perror( "open(/proc/sys/kernel/core_pattern, O_RDONLY ) error" ); |
---|
114 | return; |
---|
115 | } // if |
---|
116 | |
---|
117 | try { |
---|
118 | const char * expected = "core\n"; |
---|
119 | const int sz = sizeof("core\n"); |
---|
120 | char buf[512]; |
---|
121 | ret = read( cp, buf, 512 ); |
---|
122 | if ( ret < 0 ) { |
---|
123 | perror( "core pattern read error" ); |
---|
124 | return; |
---|
125 | } |
---|
126 | ret = strncmp( expected, buf, sz - 1 ); |
---|
127 | if ( ret != 0 ) { |
---|
128 | serr | "/proc/sys/kernel/core_pattern does not contain 'core', was:" | nl | nl | buf | nl |
---|
129 | | "Test script expect cores files to be dumped with name 'core' in current working directory." | nl |
---|
130 | | "Apport is not supported, it should be deactivated in /etc/default/apport for the test suite to work with core dumps."; |
---|
131 | } |
---|
132 | } finally { |
---|
133 | ret = close(cp ); |
---|
134 | if ( ret < 0 ) perror("close(/proc/sys/kernel/core_pattern ) error"); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | int main() { |
---|
139 | check_ulimit(); |
---|
140 | check_permission(); |
---|
141 | check_free_space(); |
---|
142 | check_noconflict(); |
---|
143 | check_dumpflag(); |
---|
144 | check_core_pattern(); |
---|
145 | sout | "Done"; |
---|
146 | } |
---|