| 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 : Sun Mar 2 20:56:32 2025
|
|---|
| 13 | // Update Count : 18
|
|---|
| 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 |
|
|---|
| 47 | #define exe "/proc/self/exe"
|
|---|
| 48 | ssize_t n = readlink( exe, myExe, sizeof(myExe));
|
|---|
| 49 | if ( n < 0 ) {
|
|---|
| 50 | perror( "readlink( " exe " ) error" );
|
|---|
| 51 | return 1;
|
|---|
| 52 | } // if
|
|---|
| 53 | myExe[n] = '\0';
|
|---|
| 54 |
|
|---|
| 55 | if ( int r = access( myExe, F_OK ); r != 0 ) serr | "Expected current executable does not exist!" | r | errno;
|
|---|
| 56 | if ( int r = access( myExe, R_OK ); r != 0 ) serr | "No read access for current executable" | r | errno;
|
|---|
| 57 |
|
|---|
| 58 | char myCwd[PATH_MAX];
|
|---|
| 59 | if ( getcwd( myCwd, sizeof(myCwd ) ) == 0p ) {
|
|---|
| 60 | perror( "getcwd() error" );
|
|---|
| 61 | return;
|
|---|
| 62 | } // if
|
|---|
| 63 |
|
|---|
| 64 | if ( access( myCwd, F_OK ) != 0 ) serr | "Expected current working directory does not exist!";
|
|---|
| 65 | if ( access( myCwd, R_OK ) != 0 ) serr | "No read access for current working directory";
|
|---|
| 66 | if ( access( myCwd, W_OK ) != 0 ) serr | "No write access for current working directory";
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | void check_free_space() {
|
|---|
| 70 | struct statvfs buf;
|
|---|
| 71 | if ( statvfs( ".", &buf ) != 0 ) {
|
|---|
| 72 | perror( "statvfs() error" );
|
|---|
| 73 | return;
|
|---|
| 74 | } // if
|
|---|
| 75 |
|
|---|
| 76 | uint64_t avail = buf.f_bavail;
|
|---|
| 77 | avail *= buf.f_bsize;
|
|---|
| 78 | if ( avail < 536870912_l64u ) {
|
|---|
| 79 | serr | "Available diskspace is less than ~500Mb: " | avail;
|
|---|
| 80 | } // if
|
|---|
| 81 |
|
|---|
| 82 | if ( buf.f_favail < 10 ) {
|
|---|
| 83 | serr | "Available inodes is less than 10: " | buf.f_favail;
|
|---|
| 84 | } // if
|
|---|
| 85 |
|
|---|
| 86 | if ( buf.f_flag & ST_RDONLY ) {
|
|---|
| 87 | serr | "Filesystem is read only";
|
|---|
| 88 | } // if
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | void check_noconflict() {
|
|---|
| 92 | const char * name = "./core";
|
|---|
| 93 | if ( access( name, F_OK ) == 0 ) serr | "File \"" | name | "\" already exists";
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | void check_dumpflag() {
|
|---|
| 97 | int r = prctl( PR_GET_DUMPABLE, 0, 0, 0, 0 );
|
|---|
| 98 | if ( r < 0 ) {
|
|---|
| 99 | perror( "prctl( PR_GET_DUMPABLE ) error" );
|
|---|
| 100 | return;
|
|---|
| 101 | } // if
|
|---|
| 102 |
|
|---|
| 103 | if ( r != 1 ) serr | "dumpable attribute not set to 1 \"( SUID_DUMP_USER, process is dumpable )\", was" | r;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void check_core_pattern() {
|
|---|
| 107 | #define core_pattern "/proc/sys/kernel/core_pattern"
|
|---|
| 108 | int ret;
|
|---|
| 109 | int cp = open( core_pattern, 0, O_RDONLY );
|
|---|
| 110 |
|
|---|
| 111 | if ( cp < 0 ) {
|
|---|
| 112 | perror( "open( " core_pattern ", 0, O_RDONLY ) error" );
|
|---|
| 113 | return;
|
|---|
| 114 | } // if
|
|---|
| 115 |
|
|---|
| 116 | try {
|
|---|
| 117 | const char * expected = "core\n";
|
|---|
| 118 | const int sz = sizeof( "core\n" );
|
|---|
| 119 | char buf[512];
|
|---|
| 120 | ret = read( cp, buf, 512 );
|
|---|
| 121 | if ( ret < 0 ) {
|
|---|
| 122 | perror( "core pattern read error" );
|
|---|
| 123 | return;
|
|---|
| 124 | } // if
|
|---|
| 125 |
|
|---|
| 126 | ret = strncmp( expected, buf, sz - 1 );
|
|---|
| 127 | if ( ret != 0 ) {
|
|---|
| 128 | serr | "Apport is supported on your system, which means the test-suite core-dump feature does not work." | nl
|
|---|
| 129 | | "This is not a test failure, just a limitation on debugging output should a test fail.";
|
|---|
| 130 | } // if
|
|---|
| 131 | } finally {
|
|---|
| 132 | ret = close( cp );
|
|---|
| 133 | if ( ret < 0 ) perror( "close( " core_pattern " ) error" );
|
|---|
| 134 | } // try
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | int main() {
|
|---|
| 138 | check_ulimit();
|
|---|
| 139 | check_permission();
|
|---|
| 140 | check_free_space();
|
|---|
| 141 | check_noconflict();
|
|---|
| 142 | check_dumpflag();
|
|---|
| 143 | check_core_pattern();
|
|---|
| 144 | sout | "Done";
|
|---|
| 145 | }
|
|---|