1 /*
2 * Check decoding of readlinkat syscall.
3 *
4 * Copyright (c) 2015 Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
5 * Copyright (c) 2015-2018 Dmitry V. Levin <ldv@strace.io>
6 * Copyright (c) 2015-2021 The strace developers.
7 * All rights reserved.
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12 #include "tests.h"
13 #include "scno.h"
14
15 #include <stdio.h>
16 #include <unistd.h>
17
18 #define PREFIX "test.readlinkat"
19 #define TARGET (PREFIX ".target")
20 #define LINKPATH (PREFIX ".link")
21
22 int
23 main(void)
24 {
25 const char * const fname = tail_memdup(LINKPATH, sizeof(LINKPATH));
26 const char * const hex_fname =
27 hexquote_strndup(fname, sizeof(LINKPATH) - 1);
28
29 const unsigned int size = sizeof(TARGET) - 1;
30 char * const buf = tail_alloc(size);
31
32 (void) unlink(fname);
33
34 long rc = syscall(__NR_readlinkat, -100, fname, buf, size);
35 printf("readlinkat(AT_FDCWD, \"%s\", %p, %u) = -1 ENOENT (%m)\n",
36 hex_fname, buf, size);
37
38 if (symlink(TARGET, fname))
39 perror_msg_and_fail("symlink");
40
41 rc = syscall(__NR_readlinkat, -100, fname, buf, size);
42 if (rc < 0) {
43 perror("readlinkat");
44 (void) unlink(fname);
45 return 77;
46 }
47 const char * const hex_buf = hexquote_strndup(buf, size);
48 printf("readlinkat(AT_FDCWD, \"%s\", \"%s\", %u) = %u\n",
49 hex_fname, hex_buf, size, size);
50
51 if (unlink(fname))
52 perror_msg_and_fail("unlink");
53
54 puts("+++ exited with 0 +++");
55 return 0;
56 }