source: tests/meta/dumpable.cfa @ 2980ccb8

Last change on this file since 2980ccb8 was 3ee4a53, checked in by Peter A. Buhr <pabuhr@…>, 5 months ago

formatting

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[10b3fc3]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
[3ee4a53]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Jul 19 07:58:45 2024
13// Update Count     : 10
[10b3fc3]14//
15
16#include <errno.h>
[a8e9e9d]17#include <limits.h>
18#include <string.h>
[3ee4a53]19#include <unistd.h>
[10b3fc3]20
21#include <fstream.hfa>
22
23extern "C" {
[a8e9e9d]24        #include <fcntl.h>
[10b3fc3]25        #include <sys/prctl.h>
26        #include <sys/resource.h>
27        #include <sys/statvfs.h>
[a8e9e9d]28        #include <sys/stat.h>
29        #include <sys/types.h>
[10b3fc3]30}
31
32void check_ulimit() {
33        struct rlimit rlp;
[3ee4a53]34        getrlimit( RLIMIT_CORE, &rlp );
[d7b399f]35        if ( rlp.rlim_cur < 536870912 ) {
[10b3fc3]36                serr | "Soft core limit is less than ~500Mb: " | rlp.rlim_cur;
[3ee4a53]37        } // if
[10b3fc3]38
[d7b399f]39        if ( rlp.rlim_max < 536870912 ) {
[10b3fc3]40                serr | "Hard core limit is less than ~500Mb: " | rlp.rlim_max;
[3ee4a53]41        } // if
[10b3fc3]42}
43
44void check_permission() {
[3ee4a53]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
[10b3fc3]61
[3ee4a53]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";
[10b3fc3]65}
66
67void check_free_space() {
68        struct statvfs buf;
[3ee4a53]69        if ( statvfs( ".", &buf ) != 0 ) {
70                perror( "statvfs() error" );
[10b3fc3]71                return;
[3ee4a53]72        } // if
[10b3fc3]73
[f0567a8]74        uint64_t avail = buf.f_bavail;
75        avail *= buf.f_bsize;
[d7b399f]76        if ( avail < 536870912_l64u ) {
[f0567a8]77                serr | "Available diskspace is less than ~500Mb: " | avail;
[3ee4a53]78        } // if
[10b3fc3]79
[d7b399f]80        if ( buf.f_favail < 10 ) {
[10b3fc3]81                serr | "Available inodes is less than 10: " | buf.f_favail;
[3ee4a53]82        } // if
[10b3fc3]83
[d7b399f]84        if ( buf.f_flag & ST_RDONLY ) {
[10b3fc3]85                serr | "Filesystem is read only";
[3ee4a53]86        } // if
[10b3fc3]87}
88
89void check_noconflict() {
[3ee4a53]90        const char * name = "./core";
91        if ( access( name, F_OK ) == 0 ) serr | "File \"" | name | "\" already exists";
[10b3fc3]92}
93
94void check_dumpflag() {
[3ee4a53]95        int r = prctl( PR_GET_DUMPABLE, 0, 0, 0, 0 );
[d7b399f]96        if ( r < 0 ) {
[3ee4a53]97                perror( "prctl( PR_GET_DUMPABLE ) error" );
[10b3fc3]98                return;
[3ee4a53]99        } // if
[10b3fc3]100
[3ee4a53]101        if ( r != 1 ) serr | "dumpable attribute not set to 1 \"( SUID_DUMP_USER, process is dumpable )\", was" | r;
[10b3fc3]102}
103
[a8e9e9d]104void check_core_pattern() {
105        int ret;
[3ee4a53]106        int cp = open( "/proc/sys/kernel/core_pattern", 0, O_RDONLY );
[d7b399f]107
108        if ( cp < 0 ) {
109                perror( "open(/proc/sys/kernel/core_pattern, O_RDONLY ) error" );
[a8e9e9d]110                return;
[d7b399f]111        } // if
[a8e9e9d]112
113        try {
[0521a1a]114                const char * expected = "core\n";
[3ee4a53]115                const int sz = sizeof( "core\n" );
[a8e9e9d]116                char buf[512];
[d7b399f]117                ret = read( cp, buf, 512 );
118                if ( ret < 0 ) {
119                        perror( "core pattern read error" );
[a8e9e9d]120                        return;
[3ee4a53]121                } // if
122
[d7b399f]123                ret = strncmp( expected, buf, sz - 1 );
124                if ( ret != 0 ) {
[3ee4a53]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
[d7b399f]128        } finally {
[3ee4a53]129                ret = close( cp );
130                if ( ret < 0 ) perror( "close( /proc/sys/kernel/core_pattern ) error" );
131        } // try
[a8e9e9d]132}
133
[10b3fc3]134int main() {
135        check_ulimit();
136        check_permission();
137        check_free_space();
138        check_noconflict();
139        check_dumpflag();
[a8e9e9d]140        check_core_pattern();
[10b3fc3]141        sout | "Done";
[d7b399f]142}
Note: See TracBrowser for help on using the repository browser.