[LTP] [PATCH] open04.c: convert to new LTP API

Petr Vorel pvorel@suse.cz
Thu Sep 1 08:32:55 CEST 2022


Hi Avinesh,

I suggest to merge with these fixes:

* added free (if needed - that was in the original source)
* fixed cleanup (don't run SAFE_CLOSE() if previous SAFE_OPEN() or SAFE_MALLOC()
  failed)
* use TST_EXP_FAIL() - the same result as TST_EXP_FAIL2() with shorter code
* allocate memory needed (it's actually -2)
* #define FNAME "open04"

If it's ok, I'll merge it.

Kind regards,
Petr

diff --git testcases/kernel/syscalls/open/open04.c testcases/kernel/syscalls/open/open04.c
index e7cb533fe..16477e459 100644
--- testcases/kernel/syscalls/open/open04.c
+++ testcases/kernel/syscalls/open/open04.c
@@ -1,19 +1,22 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *   Copyright (c) International Business Machines  Corp., 2001
- *   Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
+ * Copyright (c) International Business Machines  Corp., 2001
+ * Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
  */
 
 /*\
  * [Description]
  *
- * Verify that open(2) fails with EMFILE when
- * per-process limit on the number of open file descriptors has been reached.
+ * Verify that open(2) fails with EMFILE when per-process limit on the number
+ * of open file descriptors has been reached.
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include "tst_test.h"
 
+#define FNAME "open04"
+
 static int fds_limit, first, i;
 static int *fds;
 static char fname[20];
@@ -23,13 +26,13 @@ static void setup(void)
 	int fd;
 
 	fds_limit = getdtablesize();
-	first = SAFE_OPEN("open04", O_RDWR | O_CREAT, 0777);
+	first = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0777);
 
-	fds = SAFE_MALLOC(sizeof(int) * (fds_limit - first));
+	fds = SAFE_MALLOC(sizeof(int) * (fds_limit - first - 2));
 	fds[0] = first;
 
 	for (i = first + 1; i < fds_limit; i++) {
-		sprintf(fname, "open04.%d", i);
+		sprintf(fname, FNAME ".%d", i);
 		fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0777);
 		fds[i - first] = fd;
 	}
@@ -37,16 +40,20 @@ static void setup(void)
 
 static void run(void)
 {
-	sprintf(fname, "open04.%d", fds_limit);
-	TST_EXP_FAIL2(open(fname, O_RDWR | O_CREAT, 0777),
-				EMFILE,
-				"open(%s, O_RDWR | O_CREAT, 0777)", fname);
+	sprintf(fname, FNAME ".%d", fds_limit);
+	TST_EXP_FAIL(open(fname, O_RDWR | O_CREAT, 0777), EMFILE);
 }
 
 static void cleanup(void)
 {
+	if (!first || !fds)
+		return;
+
 	for (i = first; i < fds_limit; i++)
 		SAFE_CLOSE(fds[i - first]);
+
+	if (fds)
+		free(fds);
 }
 
 static struct tst_test test = {


More information about the ltp mailing list