File: stat.c

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (47 lines) | stat: -rw-r--r-- 1,308 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

#if defined(__x86_64__) || defined(__aarch64__)
static const char * badAddress = (char *)0xffffffffff000000;
#else
static const char * badAddress = (char *)0xff000000;
#endif

int main() {
  struct stat buf;
  int rc;
  while (1) {
    char procFd[100];
    snprintf(procFd, sizeof(procFd), "/proc/%d/fd", getpid());
    procFd[sizeof(procFd)-1] = '\0';

    rc = stat("/dev/tty", &buf);
    if (rc == -1) return 1;
    rc = stat("/etc/passwd", &buf);
    if (rc == -1) return 2;
    rc = stat(procFd, &buf);
    if (rc == -1) return 3;
    rc = stat("/etc/there_is_no_such_file_and_so_rc_is_error", &buf);
    if (rc != -1 || errno != ENOENT) return 4;
    rc = stat(badAddress, &buf);
    if (rc != -1 || errno != EFAULT) return 5;

    rc = lstat("/dev/tty", &buf);
    if (rc == -1) return 1;
    rc = lstat("/etc/passwd", &buf);
    if (rc == -1) return 2;
    rc = lstat(procFd, &buf);
    if (rc == -1) return 3;
    rc = lstat("/etc/there_is_no_such_file_and_so_rc_is_error", &buf);
    if (rc != -1 || errno != ENOENT) return 4;
    rc = lstat(badAddress, &buf);
    if (rc != -1 || errno != EFAULT) return 5;

    printf(". "); fflush(stdout);
    sleep(1);
  }
  return 1;  /* NOTREACHED */
}