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 <limits.h>
|
---|
17 | #include <errno.h>
|
---|
18 |
|
---|
19 | #include <fstream.hfa>
|
---|
20 |
|
---|
21 | extern "C" {
|
---|
22 | #include <sys/prctl.h>
|
---|
23 | #include <sys/resource.h>
|
---|
24 | #include <sys/statvfs.h>
|
---|
25 | #include <unistd.h>
|
---|
26 | }
|
---|
27 |
|
---|
28 | void check_ulimit() {
|
---|
29 | struct rlimit rlp;
|
---|
30 | getrlimit(RLIMIT_CORE, &rlp);
|
---|
31 | if(rlp.rlim_cur < 536870912) {
|
---|
32 | serr | "Soft core limit is less than ~500Mb: " | rlp.rlim_cur;
|
---|
33 | }
|
---|
34 |
|
---|
35 | if(rlp.rlim_max < 536870912) {
|
---|
36 | serr | "Hard core limit is less than ~500Mb: " | rlp.rlim_max;
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | void check_permission() {
|
---|
41 | {
|
---|
42 | char myExe[PATH_MAX];
|
---|
43 | ssize_t n = readlink("/proc/self/exe", myExe, sizeof(myExe));
|
---|
44 | if (n < 0) {
|
---|
45 | perror("readlink(/proc/self/exe) error");
|
---|
46 | return 1;
|
---|
47 | }
|
---|
48 | myExe[n] = '\0';
|
---|
49 |
|
---|
50 | if(int r = access(myExe, F_OK); r != 0) serr | "Expected current executable does not exist!" | r | errno;
|
---|
51 | if(int r = access(myExe, R_OK); r != 0) serr | "No read access for current executable" | r | errno;
|
---|
52 | }
|
---|
53 |
|
---|
54 | {
|
---|
55 | char myCwd[PATH_MAX];
|
---|
56 | if (getcwd(myCwd, sizeof(myCwd)) == 0p) {
|
---|
57 | perror("getcwd() error");
|
---|
58 | return;
|
---|
59 | }
|
---|
60 |
|
---|
61 | if(access(myCwd, F_OK) != 0) serr | "Expected current working directory does not exist!";
|
---|
62 | if(access(myCwd, R_OK) != 0) serr | "No read access for current working directory";
|
---|
63 | if(access(myCwd, W_OK) != 0) serr | "No write access for current working directory";
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | void check_free_space() {
|
---|
68 | struct statvfs buf;
|
---|
69 | if(statvfs(".", &buf) != 0) {
|
---|
70 | perror("statvfs() error");
|
---|
71 | return;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if((buf.f_bsize * buf.f_bavail) < 536870912) {
|
---|
75 | serr | "Available diskspace is less than ~500Mb: " | (buf.f_bsize * buf.f_bavail);
|
---|
76 | }
|
---|
77 |
|
---|
78 | if(buf.f_favail < 10) {
|
---|
79 | serr | "Available inodes is less than 10: " | buf.f_favail;
|
---|
80 | }
|
---|
81 |
|
---|
82 | if(buf.f_flag & ST_RDONLY) {
|
---|
83 | serr | "Filesystem is read only";
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | void check_noconflict() {
|
---|
88 | char * name = "./core";
|
---|
89 | if(access("./core", F_OK) == 0) serr | "A file of the core name ('" | name | "') already exists";
|
---|
90 | }
|
---|
91 |
|
---|
92 | void check_dumpflag() {
|
---|
93 | int r = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
|
---|
94 | if(r < 0) {
|
---|
95 | perror("prctl(PR_GET_DUMPABLE) error");
|
---|
96 | return;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if(r != 1) serr | "dumpable attribute not set to 1 \"(SUID_DUMP_USER, process is dumpable)\", was" | r;
|
---|
100 | }
|
---|
101 |
|
---|
102 | int main() {
|
---|
103 | check_ulimit();
|
---|
104 |
|
---|
105 | check_permission();
|
---|
106 |
|
---|
107 | check_free_space();
|
---|
108 |
|
---|
109 | check_noconflict();
|
---|
110 |
|
---|
111 | check_dumpflag();
|
---|
112 |
|
---|
113 | sout | "Done";
|
---|
114 | }
|
---|