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