source: tests/meta/dumpable.cfa@ e48aca8

Last change on this file since e48aca8 was 2a90639, checked in by Peter A. Buhr <pabuhr@…>, 9 months ago

small updates to dumpable.cfa

  • Property mode set to 100644
File size: 3.7 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
[2a90639]12// Last Modified On : Sun Mar 2 20:56:32 2025
13// Update Count : 18
[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];
[2a90639]46
47 #define exe "/proc/self/exe"
48 ssize_t n = readlink( exe, myExe, sizeof(myExe));
[3ee4a53]49 if ( n < 0 ) {
[2a90639]50 perror( "readlink( " exe " ) error" );
[3ee4a53]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
[10b3fc3]63
[3ee4a53]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";
[10b3fc3]67}
68
69void check_free_space() {
70 struct statvfs buf;
[3ee4a53]71 if ( statvfs( ".", &buf ) != 0 ) {
72 perror( "statvfs() error" );
[10b3fc3]73 return;
[3ee4a53]74 } // if
[10b3fc3]75
[f0567a8]76 uint64_t avail = buf.f_bavail;
77 avail *= buf.f_bsize;
[d7b399f]78 if ( avail < 536870912_l64u ) {
[f0567a8]79 serr | "Available diskspace is less than ~500Mb: " | avail;
[3ee4a53]80 } // if
[10b3fc3]81
[d7b399f]82 if ( buf.f_favail < 10 ) {
[10b3fc3]83 serr | "Available inodes is less than 10: " | buf.f_favail;
[3ee4a53]84 } // if
[10b3fc3]85
[d7b399f]86 if ( buf.f_flag & ST_RDONLY ) {
[10b3fc3]87 serr | "Filesystem is read only";
[3ee4a53]88 } // if
[10b3fc3]89}
90
91void check_noconflict() {
[3ee4a53]92 const char * name = "./core";
93 if ( access( name, F_OK ) == 0 ) serr | "File \"" | name | "\" already exists";
[10b3fc3]94}
95
96void check_dumpflag() {
[3ee4a53]97 int r = prctl( PR_GET_DUMPABLE, 0, 0, 0, 0 );
[d7b399f]98 if ( r < 0 ) {
[3ee4a53]99 perror( "prctl( PR_GET_DUMPABLE ) error" );
[10b3fc3]100 return;
[3ee4a53]101 } // if
[10b3fc3]102
[3ee4a53]103 if ( r != 1 ) serr | "dumpable attribute not set to 1 \"( SUID_DUMP_USER, process is dumpable )\", was" | r;
[10b3fc3]104}
105
[a8e9e9d]106void check_core_pattern() {
[2a90639]107 #define core_pattern "/proc/sys/kernel/core_pattern"
[a8e9e9d]108 int ret;
[2a90639]109 int cp = open( core_pattern, 0, O_RDONLY );
[d7b399f]110
111 if ( cp < 0 ) {
[2a90639]112 perror( "open( " core_pattern ", 0, O_RDONLY ) error" );
[a8e9e9d]113 return;
[d7b399f]114 } // if
[a8e9e9d]115
116 try {
[0521a1a]117 const char * expected = "core\n";
[3ee4a53]118 const int sz = sizeof( "core\n" );
[a8e9e9d]119 char buf[512];
[d7b399f]120 ret = read( cp, buf, 512 );
121 if ( ret < 0 ) {
122 perror( "core pattern read error" );
[a8e9e9d]123 return;
[3ee4a53]124 } // if
125
[d7b399f]126 ret = strncmp( expected, buf, sz - 1 );
127 if ( ret != 0 ) {
[3ee4a53]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
[d7b399f]131 } finally {
[3ee4a53]132 ret = close( cp );
[2a90639]133 if ( ret < 0 ) perror( "close( " core_pattern " ) error" );
[3ee4a53]134 } // try
[a8e9e9d]135}
136
[10b3fc3]137int main() {
138 check_ulimit();
139 check_permission();
140 check_free_space();
141 check_noconflict();
142 check_dumpflag();
[a8e9e9d]143 check_core_pattern();
[10b3fc3]144 sout | "Done";
[d7b399f]145}
Note: See TracBrowser for help on using the repository browser.