source: tests/meta/dumpable.cfa @ 884f3f67

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 884f3f67 was f0567a8, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Fix meta/dumpable which had integer overflow errors.

  • Property mode set to 100644
File size: 2.6 KB
Line 
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
21extern "C" {
22        #include <sys/prctl.h>
23        #include <sys/resource.h>
24        #include <sys/statvfs.h>
25        #include <unistd.h>
26}
27
28void 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
40void 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
67void check_free_space() {
68        struct statvfs buf;
69        if(statvfs(".", &buf) != 0) {
70                perror("statvfs() error");
71                return;
72        }
73
74        uint64_t avail = buf.f_bavail;
75        avail *= buf.f_bsize;
76        if(avail < 536870912_l64u) {
77                serr | "Available diskspace is less than ~500Mb: " | avail;
78        }
79
80        if(buf.f_favail < 10) {
81                serr | "Available inodes is less than 10: " | buf.f_favail;
82        }
83
84        if(buf.f_flag & ST_RDONLY) {
85                serr | "Filesystem is read only";
86        }
87}
88
89void check_noconflict() {
90        char * name = "./core";
91        if(access("./core", F_OK) == 0) serr | "A file of the core name ('" | name | "') already exists";
92}
93
94void check_dumpflag() {
95        int r = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
96        if(r < 0) {
97                perror("prctl(PR_GET_DUMPABLE) error");
98                return;
99        }
100
101        if(r != 1) serr | "dumpable attribute not set to 1 \"(SUID_DUMP_USER, process is dumpable)\", was" | r;
102}
103
104int main() {
105        check_ulimit();
106
107        check_permission();
108
109        check_free_space();
110
111        check_noconflict();
112
113        check_dumpflag();
114
115        sout | "Done";
116}
Note: See TracBrowser for help on using the repository browser.