1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2019 Petr Vorel <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker #ifndef SETDOMAINNAME_H__
7*49cdfc7eSAndroid Build Coastguard Worker #define SETDOMAINNAME_H__
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
11*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/utsname.h"
12*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker #define TST_VALID_DOMAIN_NAME "test_dom"
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #ifdef TEST_SETHOSTNAME
17*49cdfc7eSAndroid Build Coastguard Worker # define GET_SYSCALL gethostname
18*49cdfc7eSAndroid Build Coastguard Worker # define SET_SYSCALL sethostname
19*49cdfc7eSAndroid Build Coastguard Worker # define SYSCALL_NAME "hostname"
20*49cdfc7eSAndroid Build Coastguard Worker # define SYSCALL_NR __NR_sethostname
21*49cdfc7eSAndroid Build Coastguard Worker #else
22*49cdfc7eSAndroid Build Coastguard Worker # define GET_SYSCALL getdomainname
23*49cdfc7eSAndroid Build Coastguard Worker # define SET_SYSCALL setdomainname
24*49cdfc7eSAndroid Build Coastguard Worker # define SYSCALL_NAME "domainname"
25*49cdfc7eSAndroid Build Coastguard Worker # define SYSCALL_NR __NR_setdomainname
26*49cdfc7eSAndroid Build Coastguard Worker #endif
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker static char backup[_UTSNAME_DOMAIN_LENGTH];
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker #define TEST_VARIANTS 2
31*49cdfc7eSAndroid Build Coastguard Worker
setdomainname_info(void)32*49cdfc7eSAndroid Build Coastguard Worker static void setdomainname_info(void)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker switch (tst_variant) {
35*49cdfc7eSAndroid Build Coastguard Worker case 0:
36*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Testing libc set" SYSCALL_NAME "()");
37*49cdfc7eSAndroid Build Coastguard Worker break;
38*49cdfc7eSAndroid Build Coastguard Worker case 1:
39*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Testing __NR_set" SYSCALL_NAME " syscall");
40*49cdfc7eSAndroid Build Coastguard Worker break;
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker
do_setdomainname(char * new,size_t len)44*49cdfc7eSAndroid Build Coastguard Worker static int do_setdomainname(char *new, size_t len)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker switch (tst_variant) {
47*49cdfc7eSAndroid Build Coastguard Worker case 0:
48*49cdfc7eSAndroid Build Coastguard Worker return SET_SYSCALL(new, len);
49*49cdfc7eSAndroid Build Coastguard Worker break;
50*49cdfc7eSAndroid Build Coastguard Worker case 1:
51*49cdfc7eSAndroid Build Coastguard Worker return tst_syscall(SYSCALL_NR, new, len);
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker return -1;
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
setup(void)57*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker setdomainname_info();
60*49cdfc7eSAndroid Build Coastguard Worker if ((GET_SYSCALL(backup, sizeof(backup))) < 0)
61*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "get" SYSCALL_NAME "() failed");
62*49cdfc7eSAndroid Build Coastguard Worker }
63*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)64*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
65*49cdfc7eSAndroid Build Coastguard Worker {
66*49cdfc7eSAndroid Build Coastguard Worker if ((SET_SYSCALL(backup, strlen(backup))) < 0)
67*49cdfc7eSAndroid Build Coastguard Worker tst_res(TWARN | TERRNO, "set" SYSCALL_NAME "() failed ('%s')", backup);
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker #endif /* SETDOMAINNAME_H__ */
71