Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/tests/lib/libc/sys Rewrite the limit-test.
details: https://anonhg.NetBSD.org/src/rev/a304a662feb9
branches: trunk
changeset: 767047:a304a662feb9
user: jruoho <jruoho%NetBSD.org@localhost>
date: Thu Jul 07 10:27:31 2011 +0000
description:
Rewrite the limit-test.
diffstat:
tests/lib/libc/sys/t_dup.c | 93 ++++++++++++++++++++++++++-------------------
1 files changed, 53 insertions(+), 40 deletions(-)
diffs (139 lines):
diff -r dfa6dacb56ba -r a304a662feb9 tests/lib/libc/sys/t_dup.c
--- a/tests/lib/libc/sys/t_dup.c Thu Jul 07 10:26:00 2011 +0000
+++ b/tests/lib/libc/sys/t_dup.c Thu Jul 07 10:27:31 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: t_dup.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
+/* $NetBSD: t_dup.c,v 1.2 2011/07/07 10:27:31 jruoho Exp $ */
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -29,18 +29,20 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: t_dup.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
+__RCSID("$NetBSD: t_dup.c,v 1.2 2011/07/07 10:27:31 jruoho Exp $");
#include <sys/resource.h>
#include <sys/stat.h>
+#include <sys/wait.h>
+#include <atf-c.h>
#include <errno.h>
#include <fcntl.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-
-#include <atf-c.h>
+#include <sysexits.h>
static char path[] = "dup";
@@ -63,58 +65,69 @@
ATF_TC_BODY(dup_max, tc)
{
- int current, fd, *buf, serrno;
struct rlimit res;
- long i, maxfd;
+ int *buf, fd, sta;
+ size_t i, n;
+ pid_t pid;
+
+ pid = fork();
+ ATF_REQUIRE(pid >= 0);
+
+ if (pid == 0) {
- /*
- * Open a temporary file until the
- * maximum number of open files is
- * reached. Ater that dup(2) should
- * fail with EMFILE.
- */
- (void)memset(&res, 0, sizeof(struct rlimit));
+ /*
+ * Open a temporary file until the
+ * maximum number of open files is
+ * reached. Ater that dup(2) should
+ * fail with EMFILE.
+ */
+ (void)closefrom(0);
+ (void)memset(&res, 0, sizeof(struct rlimit));
- ATF_REQUIRE(getrlimit(RLIMIT_NOFILE, &res) == 0);
+ if (getrlimit(RLIMIT_NOFILE, &res) != 0)
+ _exit(EX_OSERR);
- ATF_REQUIRE(res.rlim_cur > 0);
- ATF_REQUIRE(res.rlim_max > 0);
+ if (res.rlim_cur == 0 || res.rlim_max == 0)
+ _exit(EX_OSERR);
- maxfd = res.rlim_cur;
- buf = calloc(maxfd, sizeof(int));
+ n = res.rlim_cur;
+ buf = calloc(n, sizeof(int));
+
+ if (buf == NULL)
+ _exit(EX_OSERR);
- if (buf == NULL)
- return;
+ buf[0] = mkstemp(path);
- buf[0] = mkstemp(path);
- ATF_REQUIRE(buf[0] != -1);
+ if (buf[0] < 0)
+ _exit(EX_OSERR);
+
+ for (i = 1; i < n; i++) {
- current = fcntl(0, F_MAXFD);
- ATF_REQUIRE(current != -1);
+ buf[i] = open(path, O_RDONLY);
- fd = -1;
- serrno = EMFILE;
+ if (buf[i] < 0)
+ _exit(EX_OSERR);
+ }
- for (i = current; i <= maxfd; i++) {
+ errno = 0;
+ fd = dup(buf[0]);
- buf[i] = open(path, O_RDONLY);
+ if (fd != -1 || errno != EMFILE)
+ _exit(EX_DATAERR);
- if (buf[i] < 0)
- goto out;
+ _exit(EXIT_SUCCESS);
}
- errno = 0;
- fd = dup(buf[0]);
- serrno = errno;
+ (void)wait(&sta);
+
+ if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
-out:
- for (i = 0; i <= maxfd; i++)
- (void)close(buf[i]);
+ if (WEXITSTATUS(sta) == EX_OSERR)
+ atf_tc_fail("unknown error");
- free(buf);
-
- if (fd != -1 || serrno != EMFILE)
- atf_tc_fail("dup(2) dupped more than RLIMIT_NOFILE");
+ if (WEXITSTATUS(sta) == EX_DATAERR)
+ atf_tc_fail("dup(2) dupped more than RLIMIT_NOFILE");
+ }
}
ATF_TC_CLEANUP(dup_max, tc)
Home |
Main Index |
Thread Index |
Old Index