]> granicus.if.org Git - strace/blob - tests/syslog.c
Remove XLAT_END
[strace] / tests / syslog.c
1 /*
2  * Copyright (c) 2016-2019 The strace developers.
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  */
7
8 #include "tests.h"
9 #include "scno.h"
10
11 #ifdef __NR_syslog
12
13 # include <stdio.h>
14 # include <unistd.h>
15
16 # ifdef RETVAL_INJECTED
17 #  define RET_SFX " (INJECTED)"
18 # else
19 #  define RET_SFX ""
20 # endif
21
22 bool
23 valid_cmd(int cmd)
24 {
25         return cmd >= 0 && cmd <= 10;
26 }
27
28 void
29 printstr(const char *s, int cmd, long size)
30 {
31         if (size < 0 || !valid_cmd(cmd))
32                 printf("%p", s);
33         else if (size == 0)
34                 printf("\"\"");
35         else if (size <= DEFAULT_STRLEN)
36                 print_quoted_memory(s, size);
37         else {
38                 print_quoted_memory(s, DEFAULT_STRLEN);
39                 printf("...");
40         }
41 }
42
43 int
44 main(void)
45 {
46         static const struct cmd_str {
47                 unsigned int cmd;
48                 const char *str;
49         } no_args[] = {
50                 { 0,  "0 /* SYSLOG_ACTION_CLOSE */" },
51                 { 1,  "1 /* SYSLOG_ACTION_OPEN */" },
52                 { 5,  "5 /* SYSLOG_ACTION_CLEAR */" },
53                 { 6,  "6 /* SYSLOG_ACTION_CONSOLE_OFF */" },
54                 { 7,  "7 /* SYSLOG_ACTION_CONSOLE_ON */" },
55                 { 9,  "9 /* SYSLOG_ACTION_SIZE_UNREAD */" },
56                 { 10, "10 /* SYSLOG_ACTION_SIZE_BUFFER */" },
57         };
58         static const struct cmd_str two_args[] = {
59                 { 0xfeedbeef, "-17973521 /* SYSLOG_ACTION_??? */" },
60                 { -1U, "-1 /* SYSLOG_ACTION_??? */" },
61                 { 2,  "2 /* SYSLOG_ACTION_READ */" },
62                 { 3,  "3 /* SYSLOG_ACTION_READ_ALL */" },
63                 { 4,  "4 /* SYSLOG_ACTION_READ_CLEAR */" },
64                 { 11, "11 /* SYSLOG_ACTION_??? */" },
65                 { (1U << 31) - 1, "2147483647 /* SYSLOG_ACTION_??? */" },
66         };
67         static const struct cmd_str levels[] = {
68                 { 0xfeedbeef, "-17973521 /* LOGLEVEL_??? */" },
69                 { -1U, "-1 /* LOGLEVEL_??? */" },
70                 { 0, "0 /* LOGLEVEL_EMERG */" },
71                 { 7, "7 /* LOGLEVEL_DEBUG */" },
72                 { 8, "8 /* LOGLEVEL_DEBUG+1 */" },
73                 { 9, "9 /* LOGLEVEL_??? */" },
74                 { (1U << 31) - 1, "2147483647 /* LOGLEVEL_??? */" },
75         };
76         static const kernel_ulong_t high =
77                 (kernel_ulong_t) 0xbadc0ded00000000ULL;
78         static const size_t buf_size = 64;
79
80         const kernel_ulong_t addr = (kernel_ulong_t) 0xfacefeeddeadbeefULL;
81         int rc;
82         char *buf = tail_alloc(buf_size);
83
84         fill_memory(buf, buf_size);
85
86         for (size_t i = 0; i < ARRAY_SIZE(no_args); i++) {
87                 rc = syscall(__NR_syslog, high | no_args[i].cmd, addr, -1);
88                 printf("syslog(%s) = %s" RET_SFX "\n",
89                 no_args[i].str, sprintrc(rc));
90         }
91
92         for (size_t i = 0; i < ARRAY_SIZE(two_args); i++) {
93                 rc = syscall(__NR_syslog, high | two_args[i].cmd, NULL, -1);
94                 printf("syslog(%s, NULL, -1) = %s" RET_SFX "\n",
95                        two_args[i].str, sprintrc(rc));
96
97 # ifdef RETVAL_INJECTED
98                 /* Avoid valid commands with a bogus address */
99                 if (!valid_cmd(two_args[i].cmd))
100 # endif
101                 {
102                         rc = syscall(__NR_syslog, high | two_args[i].cmd, addr,
103                                      -1);
104                         printf("syslog(%s, %#llx, -1) = %s" RET_SFX "\n",
105                                two_args[i].str, (unsigned long long) addr,
106                                sprintrc(rc));
107
108                         rc = syscall(__NR_syslog, two_args[i].cmd, addr, 0);
109
110                         printf("syslog(%s, %s, 0) = %s" RET_SFX "\n",
111                                two_args[i].str,
112                                !rc && valid_cmd(two_args[i].cmd)
113                                    && (sizeof(kernel_ulong_t) == sizeof(void *))
114                                       ? "\"\""
115                                       : (sizeof(addr) == 8)
116                                         ? "0xfacefeeddeadbeef" : "0xdeadbeef",
117                                sprintrc(rc));
118                 }
119
120                 rc = syscall(__NR_syslog, two_args[i].cmd, buf, buf_size);
121                 const char *errstr = sprintrc(rc);
122
123                 printf("syslog(%s, ", two_args[i].str);
124                 if (rc >= 0 && valid_cmd(two_args[i].cmd))
125                         printstr(buf, two_args[i].cmd, rc);
126                 else
127                         printf("%p", buf);
128                 printf(", %zu) = %s" RET_SFX "\n", buf_size, errstr);
129         }
130
131         for (size_t i = 0; i < ARRAY_SIZE(levels); i++) {
132                 rc = syscall(__NR_syslog, high | 8, addr, levels[i].cmd);
133                 printf("syslog(8 /* SYSLOG_ACTION_CONSOLE_LEVEL */, %#llx, %s)"
134                        " = %s" RET_SFX "\n",
135                        (unsigned long long) addr, levels[i].str, sprintrc(rc));
136         }
137
138         puts("+++ exited with 0 +++");
139         return 0;
140 }
141
142 #else
143
144 SKIP_MAIN_UNDEFINED("__NR_syslog")
145
146 #endif