File: access_extended.c

package info (click to toggle)
valgrind 1%3A3.12.0~svn20160714-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 120,428 kB
  • ctags: 70,855
  • sloc: ansic: 674,645; exp: 26,134; xml: 21,574; asm: 7,570; cpp: 7,567; makefile: 7,380; sh: 6,188; perl: 5,855; haskell: 195
file content (48 lines) | stat: -rw-r--r-- 1,628 bytes parent folder | download | duplicates (10)
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
48
// This is a test for access_extended(), one of the more ridiculous syscalls
// ever devised.  For bug 200760.  See the comments on the wrapper in
// syswrap-darwin.c to understand what is going on here.

#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
   char* name1 = "access_extended.c";
   char* name2 = "no_such_file";
   // Space for three descriptors and the two strings (and NUL chars for them).
   size_t entries_szB =
      sizeof(struct accessx_descriptor) * 3 +
      strlen(name1) + 1 +
      strlen(name2) + 1;
   struct accessx_descriptor* entries = malloc(entries_szB);
   char* string1 = (char*)&entries[3];
   char* string2 = string1 + strlen(name1) + 1;
   int results[3];
   int retval;

   entries[0].ad_name_offset = string1 - (char*)entries;
   entries[1].ad_name_offset = 0;   // reuse the previous entry's string
   entries[2].ad_name_offset = string2 - (char*)entries;
   entries[0].ad_flags       = F_OK;      // succeeds
   entries[1].ad_flags       = X_OK;      // fails
   entries[2].ad_flags       = F_OK;      // fails
   strcpy(string1, name1);
   strcpy(string2, name2);

   retval = syscall(SYS_access_extended, entries, entries_szB, results,
                    /*uid--unused?*/0);

   fprintf(stderr, "retval = %d\n", retval);
   fprintf(stderr, "%s(F_OK) = %d (%s)\n",
      name1, results[0], strerror(results[0]));
   fprintf(stderr, "%s(X_OK) = %d (%s)\n",
      name1, results[1], strerror(results[1]));
   fprintf(stderr, "%s(F_OK) = %d (%s)\n",
      name2, results[2], strerror(results[2]));

   return 0;
}