]> granicus.if.org Git - strace/blob - tests/prctl-name.c
tests: change the license to GPL-2.0-or-later
[strace] / tests / prctl-name.c
1 /*
2  * Check decoding of prctl PR_GET_NAME/PR_SET_NAME operations.
3  *
4  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
6  * All rights reserved.
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include "tests.h"
12
13 #ifdef HAVE_PRCTL
14 # include <sys/prctl.h>
15 #endif
16
17 #if defined HAVE_PRCTL && defined PR_GET_NAME && defined PR_SET_NAME
18
19 # include <stdio.h>
20 # include <string.h>
21 # include <unistd.h>
22
23 int
24 main(void)
25 {
26         static const char str[] = "0123456789abcdef";
27         static const int len = sizeof(str) - 1;
28         char *name = tail_memdup(str, sizeof(str));
29         int i;
30         int rc;
31
32         rc = prctl(PR_SET_NAME, NULL);
33         printf("prctl(PR_SET_NAME, NULL) = %s\n", sprintrc(rc));
34
35         for (i = 0; i <= len; ++i) {
36                 rc = prctl(PR_SET_NAME, name + len - i);
37                 printf("prctl(PR_SET_NAME, \"%.*s\"%s) = %s\n",
38                        i < len - 1 ? i : len - 1,
39                        str + len - i,
40                        i < len - 1 ? "" : "...",
41                        sprintrc(rc));
42         }
43
44         *name = -1;
45         ++name;
46         memcpy(name, str, len);
47
48         for (i = 0; i <= len; ++i) {
49                 rc = prctl(PR_SET_NAME, name + len - i);
50                 if (i < len - 1)
51                         printf("prctl(PR_SET_NAME, %p) = %s\n",
52                                name + len - i, sprintrc(rc));
53                 else
54                         printf("prctl(PR_SET_NAME, \"%.*s\"...) = %s\n",
55                                len - 1, str + len - i, sprintrc(rc));
56         }
57
58         rc = prctl(PR_GET_NAME, NULL);
59         printf("prctl(PR_GET_NAME, NULL) = %s\n", sprintrc(rc));
60
61         for (i = 0; i < len; ++i) {
62                 rc = prctl(PR_GET_NAME, name + len - i);
63                 printf("prctl(PR_GET_NAME, %p) = %s\n",
64                        name + len - i, sprintrc(rc));
65         }
66
67         rc = prctl(PR_GET_NAME, name);
68         if (rc)
69                 printf("prctl(PR_GET_NAME, %p) = %s\n",
70                        name, sprintrc(rc));
71         else
72                 printf("prctl(PR_GET_NAME, \"%.*s\") = %s\n",
73                        len - 1, name, sprintrc(rc));
74
75         puts("+++ exited with 0 +++");
76         return 0;
77 }
78
79 #else
80
81 SKIP_MAIN_UNDEFINED("HAVE_PRCTL && PR_GET_NAME && PR_SET_NAME")
82
83 #endif