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