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