1 /*
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"
25
26 #define INC 16*1024*1024
27
28 char *TCID = "sbrk02";
29 int TST_TOTAL = 1;
30
31 static void setup(void);
32 static void sbrk_verify(void);
33 static void cleanup(void);
34
35 static long increment = INC;
36
main(int argc,char * argv[])37 int main(int argc, char *argv[])
38 {
39 int lc;
40 int i;
41
42 tst_parse_opts(argc, argv, NULL, NULL);
43
44 setup();
45
46 for (lc = 0; TEST_LOOPING(lc); lc++) {
47 tst_count = 0;
48 for (i = 0; i < TST_TOTAL; i++)
49 sbrk_verify();
50 }
51
52 cleanup();
53 tst_exit();
54 }
55
setup(void)56 static void setup(void)
57 {
58 void *ret = NULL;
59
60 tst_sig(NOFORK, DEF_HANDLER, cleanup);
61
62 TEST_PAUSE;
63
64 /* call sbrk until it fails or increment overflows */
65 while (ret != (void *)-1 && increment > 0) {
66 ret = sbrk(increment);
67 increment += INC;
68 }
69 tst_resm(TINFO | TERRNO, "setup() bailing inc: %ld, ret: %p, sbrk: %p",
70 increment, ret, sbrk(0));
71
72 errno = 0;
73 }
74
sbrk_verify(void)75 static void sbrk_verify(void)
76 {
77 void *tret;
78
79 tret = sbrk(increment);
80 TEST_ERRNO = errno;
81
82 if (tret != (void *)-1) {
83 tst_resm(TFAIL,
84 "sbrk(%ld) returned %p, expected (void *)-1, errno=%d",
85 increment, tret, ENOMEM);
86 return;
87 }
88
89 if (TEST_ERRNO == ENOMEM) {
90 tst_resm(TPASS | TTERRNO, "sbrk(%ld) failed as expected",
91 increment);
92 } else {
93 tst_resm(TFAIL | TTERRNO,
94 "sbrk(%ld) failed unexpectedly; expected: %d - %s",
95 increment, ENOMEM, strerror(ENOMEM));
96 }
97 }
98
cleanup(void)99 static void cleanup(void)
100 {
101 }
102