1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2014 Fujitsu Ltd.
4f08c3bdfSopenharmony_ci * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
5f08c3bdfSopenharmony_ci * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Verify that sbrk() on failure sets errno to ENOMEM.
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include "tst_test.h"
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#define INC (16*1024*1024)
18f08c3bdfSopenharmony_cistatic long increment = INC;
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic void run(void)
21f08c3bdfSopenharmony_ci{
22f08c3bdfSopenharmony_ci	TESTPTR(sbrk(increment));
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci	if (TST_RET_PTR != (void *)-1) {
25f08c3bdfSopenharmony_ci		tst_res(TFAIL, "sbrk(%ld) unexpectedly passed and returned %p, "
26f08c3bdfSopenharmony_ci						"expected (void *)-1 with errno=%d",
27f08c3bdfSopenharmony_ci						increment, TST_RET_PTR, ENOMEM);
28f08c3bdfSopenharmony_ci		return;
29f08c3bdfSopenharmony_ci	}
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	if (TST_ERR == ENOMEM)
32f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "sbrk(%ld) failed as expected", increment);
33f08c3bdfSopenharmony_ci	else
34f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "sbrk(%ld) failed but unexpected errno, "
35f08c3bdfSopenharmony_ci								"expected errno=%d - %s",
36f08c3bdfSopenharmony_ci								increment, ENOMEM, strerror(ENOMEM));
37f08c3bdfSopenharmony_ci}
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic void setup(void)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	void *ret = NULL;
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	while (ret != (void *)-1 && increment > 0) {
44f08c3bdfSopenharmony_ci		ret = sbrk(increment);
45f08c3bdfSopenharmony_ci		increment += INC;
46f08c3bdfSopenharmony_ci	}
47f08c3bdfSopenharmony_ci}
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_cistatic struct tst_test test = {
50f08c3bdfSopenharmony_ci	.test_all = run,
51f08c3bdfSopenharmony_ci	.setup = setup
52f08c3bdfSopenharmony_ci};
53