Zeng Linggang | 73932d9 | 2014-04-23 11:52:56 +0800 | [diff] [blame] | 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" |
Zeng Linggang | 73932d9 | 2014-04-23 11:52:56 +0800 | [diff] [blame] | 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); |
Zeng Linggang | 73932d9 | 2014-04-23 11:52:56 +0800 | [diff] [blame] | 34 | |
Jan Stancek | 54cee11 | 2014-08-22 16:05:31 +0200 | [diff] [blame] | 35 | static long increment = INC; |
Zeng Linggang | 73932d9 | 2014-04-23 11:52:56 +0800 | [diff] [blame] | 36 | |
| 37 | int main(int argc, char *argv[]) |
| 38 | { |
| 39 | int lc; |
| 40 | int i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 41 | const char *msg; |
Zeng Linggang | 73932d9 | 2014-04-23 11:52:56 +0800 | [diff] [blame] | 42 | |
| 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 | |
| 59 | static void setup(void) |
| 60 | { |
Jan Stancek | 54cee11 | 2014-08-22 16:05:31 +0200 | [diff] [blame] | 61 | void *ret = NULL; |
| 62 | |
Zeng Linggang | 73932d9 | 2014-04-23 11:52:56 +0800 | [diff] [blame] | 63 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 64 | |
| 65 | TEST_PAUSE; |
| 66 | |
Jan Stancek | 54cee11 | 2014-08-22 16:05:31 +0200 | [diff] [blame] | 67 | /* 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 Linggang | 73932d9 | 2014-04-23 11:52:56 +0800 | [diff] [blame] | 74 | |
| 75 | errno = 0; |
Zeng Linggang | 73932d9 | 2014-04-23 11:52:56 +0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static 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 Linggang | 73932d9 | 2014-04-23 11:52:56 +0800 | [diff] [blame] | 92 | 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 | |
| 102 | static void cleanup(void) |
| 103 | { |
Zeng Linggang | 73932d9 | 2014-04-23 11:52:56 +0800 | [diff] [blame] | 104 | } |