xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/gethostid/gethostid01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2021 Xie Ziyao <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  * AUTHOR: William Roske
7*49cdfc7eSAndroid Build Coastguard Worker  * CO-PILOT: Dave Fenner
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  *   12/2002 Paul Larson: Add functional test to compare output from hostid
10*49cdfc7eSAndroid Build Coastguard Worker  *   command and gethostid().
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  *   01/2003 Robbie Williamson: Add code to handle distros that add "0x" to
13*49cdfc7eSAndroid Build Coastguard Worker  *   beginning of `hostid` output.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   01/2006  Marty Ridgeway: Correct 64 bit check so the second 64 bit check
16*49cdfc7eSAndroid Build Coastguard Worker  *   doesn't clobber the first 64 bit check.
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  *   07/2021 Xie Ziyao: Rewrite with newlib and use/test sethostid.
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker /*\
22*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
23*49cdfc7eSAndroid Build Coastguard Worker  *
24*49cdfc7eSAndroid Build Coastguard Worker  * Test the basic functionality of the sethostid() and gethostid() system call.
25*49cdfc7eSAndroid Build Coastguard Worker  */
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SETHOSTID
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker static long origin;
33*49cdfc7eSAndroid Build Coastguard Worker static long tc[] = {0x00000000, 0x0000ffff};
34*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int i)35*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(sethostid(tc[i]), "set hostid to %ld", tc[i]);
38*49cdfc7eSAndroid Build Coastguard Worker 	TEST(gethostid());
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
41*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "gethostid failed");
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	if (tc[i] == TST_RET)
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "hostid is %ld, expected %ld", TST_RET, tc[i]);
45*49cdfc7eSAndroid Build Coastguard Worker 	else
46*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "hostid is %ld, expected %ld", TST_RET, tc[i]);
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)49*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker 	TEST(gethostid());
52*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
53*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TFAIL | TTERRNO, "gethostid failed");
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "get original hostid: %ld", origin = TST_RET);
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)58*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(sethostid(origin), "set hostid to %ld", origin);
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
64*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
65*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
66*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
67*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
68*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tc),
69*49cdfc7eSAndroid Build Coastguard Worker };
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker #else
72*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("sethostid is undefined.");
73*49cdfc7eSAndroid Build Coastguard Worker #endif
74