blob: b7e4de3f63842f01ef1085922b06434b9333d33e [file] [log] [blame]
Zeng Linggang73932d92014-04-23 11:52:56 +08001/*
2 * Copyright (c) 2014 Fujitsu Ltd.
3 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17/*
18 * DESCRIPTION
19 * Check sbrk() with error condition that should produce ENOMEM.
20 */
21
22#include <errno.h>
23#include <unistd.h>
24#include "test.h"
Zeng Linggang73932d92014-04-23 11:52:56 +080025
26#define INC 16*1024*1024
27
28char *TCID = "sbrk02";
29int TST_TOTAL = 1;
30
31static void setup(void);
32static void sbrk_verify(void);
33static void cleanup(void);
Zeng Linggang73932d92014-04-23 11:52:56 +080034
Jan Stancek54cee112014-08-22 16:05:31 +020035static long increment = INC;
Zeng Linggang73932d92014-04-23 11:52:56 +080036
37int main(int argc, char *argv[])
38{
39 int lc;
40 int i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020041 const char *msg;
Zeng Linggang73932d92014-04-23 11:52:56 +080042
43 msg = parse_opts(argc, argv, NULL, NULL);
44 if (msg != NULL)
45 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
46
47 setup();
48
49 for (lc = 0; TEST_LOOPING(lc); lc++) {
50 tst_count = 0;
51 for (i = 0; i < TST_TOTAL; i++)
52 sbrk_verify();
53 }
54
55 cleanup();
56 tst_exit();
57}
58
59static void setup(void)
60{
Jan Stancek54cee112014-08-22 16:05:31 +020061 void *ret = NULL;
62
Zeng Linggang73932d92014-04-23 11:52:56 +080063 tst_sig(NOFORK, DEF_HANDLER, cleanup);
64
65 TEST_PAUSE;
66
Jan Stancek54cee112014-08-22 16:05:31 +020067 /* call sbrk until it fails or increment overflows */
68 while (ret != (void *)-1 && increment > 0) {
69 ret = sbrk(increment);
70 increment += INC;
71 }
72 tst_resm(TINFO | TERRNO, "setup() bailing inc: %ld, ret: %p, sbrk: %p",
73 increment, ret, sbrk(0));
Zeng Linggang73932d92014-04-23 11:52:56 +080074
75 errno = 0;
Zeng Linggang73932d92014-04-23 11:52:56 +080076}
77
78static void sbrk_verify(void)
79{
80 void *tret;
81
82 tret = sbrk(increment);
83 TEST_ERRNO = errno;
84
85 if (tret != (void *)-1) {
86 tst_resm(TFAIL,
87 "sbrk(%ld) returned %p, expected (void *)-1, errno=%d",
88 increment, tret, ENOMEM);
89 return;
90 }
91
Zeng Linggang73932d92014-04-23 11:52:56 +080092 if (TEST_ERRNO == ENOMEM) {
93 tst_resm(TPASS | TTERRNO, "sbrk(%ld) failed as expected",
94 increment);
95 } else {
96 tst_resm(TFAIL | TTERRNO,
97 "sbrk(%ld) failed unexpectedly; expected: %d - %s",
98 increment, ENOMEM, strerror(ENOMEM));
99 }
100}
101
102static void cleanup(void)
103{
Zeng Linggang73932d92014-04-23 11:52:56 +0800104}