All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH][openfile] file descriptors not cleaned up
@ 2009-09-30  2:06 Henry Yei
  2009-10-04 22:18 ` Jiri Palecek
  0 siblings, 1 reply; 12+ messages in thread
From: Henry Yei @ 2009-09-30  2:06 UTC (permalink / raw)
  To: LTP Mailing List

[-- Attachment #1: Type: text/plain, Size: 486 bytes --]

Hello,

This patch for openfile contains the following changes:
- test output to use tst_resm functions
- sets ups and cleans up tmp dir properly
- closes all opened file descriptors before thread exit(fixes nfs issues on removing tmp dir)

Signed-off-by: Henry Yei <hyei@mvista.com>


This test opens multiple file descriptors to the same file. Perhaps the author meant to open file handles for separate files?



Henry Yei
MontaVista Software, Inc.
hyei@mvista.com


[-- Attachment #2: openfile.patch --]
[-- Type: application/octet-stream, Size: 9821 bytes --]

diff -Nurp -x CVS ltp-20090531/testcases/kernel/fs/openfile/Makefile ltp-wdir/testcases/kernel/fs/openfile/Makefile
--- ltp-20090531/testcases/kernel/fs/openfile/Makefile	2003-03-04 08:13:52.000000000 -0800
+++ ltp-wdir/testcases/kernel/fs/openfile/Makefile	2009-09-29 18:37:25.704096806 -0700
@@ -1,5 +1,5 @@
-CFLAGS += -Wall
-LOADLIBES += -lpthread
+CFLAGS+= -I../../../../include -Wall
+LDLIBS+= -L../../../../lib -lltp -lpthread
 
 SRCS=$(wildcard *.c)
 TARGETS=$(patsubst %.c,%,$(SRCS))
diff -Nurp -x CVS ltp-20090531/testcases/kernel/fs/openfile/openfile.c ltp-wdir/testcases/kernel/fs/openfile/openfile.c
--- ltp-20090531/testcases/kernel/fs/openfile/openfile.c	2009-02-26 04:14:52.000000000 -0800
+++ ltp-wdir/testcases/kernel/fs/openfile/openfile.c	2009-09-29 18:53:04.900096315 -0700
@@ -29,94 +29,107 @@
  *
  */
 
-
 #include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <unistd.h>
 
+#include "test.h"
+#include "usctest.h"
+
+char *TCID = "openfile01";	/* Test program identifier.    */
+int TST_TOTAL = 1;
 
 #define MAXFILES        32768
 #define MAXTHREADS      10
 
-
 /* Control Structure */
 struct cb {
 	pthread_mutex_t m;
-	pthread_cond_t  init_cv;
-	pthread_cond_t  thr_cv;
-	int		thr_sleeping;
+	pthread_cond_t init_cv;
+	pthread_cond_t thr_cv;
+	int thr_sleeping;
 } c;
 
-
 /* Global Variables */
-int     numthreads=10, numfiles=10;
-int     debug=0;
-char *  filename="FILETOOPEN";
+int numthreads = 10, numfiles = 10;
+int debug = 0;
+char * filename = "FILETOOPEN";
 
+void setup(void)
+{
+	tst_tmpdir();
+}
 
-/* Procedures */
-void *threads(void* thread_id);
+void cleanup(void)
+{
+	tst_rmdir();
+	tst_exit();
+}
 
 
+/* Procedures */
+void *threads(void* thread_id);
 
 /* **************************************************************************
-   *                              MAIN PROCEDURE                            *
-   ************************************************************************** */
+ *                              MAIN PROCEDURE                            *
+ ************************************************************************** */
 
 int main(int argc, char *argv[])
 {
-	int 	   i,opt,badopts=0;
-        FILE 	   *fd;
-	pthread_t  th_id;
-	char       msg[80]="";
-        extern char *optarg;
-
-        while((opt=getopt(argc, argv, "df:t:h")) != EOF) {
-          switch((char) opt) {
-            case 'd':
-              debug=1;
-              break;
-            case 'f':
-              numfiles=atoi(optarg);
-              if(numfiles <= 0)
-                badopts=1;
-              break;
-            case 't':
-              numthreads=atoi(optarg);
-              if(numthreads <= 0)
-                badopts=1;
-              break;
-            case 'h':
-            default:
-              printf("Usage: openfile [-d] -f FILES -t THREADS\n");
-       	      _exit(1);
-          }
-        }
-        if(badopts) {
-          printf("Usage: openfile [-d] -f FILES -t THREADS\n");
-       	  _exit(1);
-        }
-
-    	/* Check if numthreads is less than MAXFILES */
-    	if ( numfiles > MAXFILES ){
-          	sprintf(msg,"%s\nCannot use %d files", msg, numfiles);
-          	sprintf(msg,"%s, used %d files instead\n", msg, MAXFILES);
-          	numfiles = MAXFILES;
-    	}
-
-    	/* Check if numthreads is less than MAXTHREADS */
-    	if ( numthreads > MAXTHREADS ){
-          	sprintf(msg,"%s\nCannot use %d threads", msg, numthreads);
-          	sprintf(msg,"%s, used %d threads instead\n", msg, MAXTHREADS);
-          	numthreads = MAXTHREADS;
+	int i, opt, badopts = 0;
+	FILE *fd;
+	pthread_t th_id;
+	char msg[80] = "";
+	extern char *optarg;
+
+	while ((opt = getopt(argc, argv, "df:t:h")) != EOF) {
+		switch ((char) opt) {
+		case 'd':
+			debug = 1;
+			break;
+		case 'f':
+			numfiles = atoi(optarg);
+			if (numfiles <= 0)
+				badopts = 1;
+			break;
+		case 't':
+			numthreads = atoi(optarg);
+			if (numthreads <= 0)
+				badopts = 1;
+			break;
+		case 'h':
+		default:
+			printf("Usage: openfile [-d] -f FILES -t THREADS\n");
+			_exit(1);
+		}
+	}
+	if (badopts) {
+		printf("Usage: openfile [-d] -f FILES -t THREADS\n");
+		_exit(1);
+	}
+
+	setup();
+
+	/* Check if numthreads is less than MAXFILES */
+	if (numfiles > MAXFILES) {
+		sprintf(msg, "%s\nCannot use %d files", msg, numfiles);
+		sprintf(msg, "%s, used %d files instead\n", msg, MAXFILES);
+		numfiles = MAXFILES;
+	}
+
+	/* Check if numthreads is less than MAXTHREADS */
+	if (numthreads > MAXTHREADS) {
+		sprintf(msg, "%s\nCannot use %d threads", msg, numthreads);
+		sprintf(msg, "%s, used %d threads instead\n", msg, MAXTHREADS);
+		numthreads = MAXTHREADS;
 	}
 
 	/* Create files */
-	if ((fd=fopen(filename,"w")) == NULL){
-		perror ("FAIL - Could not create file");
-		_exit(1);
+	if ((fd = fopen(filename, "w")) == NULL) {
+		tst_resm(TFAIL, "Could not create file");
+		cleanup();
 	}
 
 	/* Initialize thread control variables, lock & condition */
@@ -126,120 +139,122 @@ int main(int argc, char *argv[])
 	c.thr_sleeping = 0;
 
 	/* Grab mutex lock */
-	if (pthread_mutex_lock(&c.m)){
-		perror("FAIL - failed to grab mutex lock");
+	if (pthread_mutex_lock(&c.m)) {
+		tst_resm(TFAIL, "failed to grab mutex lock");
 		fclose(fd);
 		unlink(filename);
-		_exit(1);
+		cleanup();
 	}
 
 	printf("Creating Reading Threads\n");
 
 	/* Create threads */
-	for (i=0; i<numthreads; i++)
-		if (pthread_create(&th_id, (pthread_attr_t *)NULL, threads,
-				(void *)(uintptr_t)i)) {
-			perror("FAIL - failed creating a pthread; increase limits");
+	for (i = 0; i < numthreads; i++)
+		if (pthread_create(&th_id, (pthread_attr_t *) NULL, threads,
+				(void *) (uintptr_t) i)) {
+			tst_resm(TFAIL, "failed creating a pthread; increase limits");
 			fclose(fd);
 			unlink(filename);
-			_exit(1);
+			cleanup();
 		}
 
 	/* Sleep until all threads are created */
 	while (c.thr_sleeping != numthreads)
-		if (pthread_cond_wait(&c.init_cv, &c.m)){
-			perror("FAIL - error while waiting for reading threads");
+		if (pthread_cond_wait(&c.init_cv, &c.m)) {
+			tst_resm(TFAIL, "error while waiting for reading threads");
 			fclose(fd);
 			unlink(filename);
-			_exit(1);
+			cleanup();
 		}
 
 	/* Wake up all threads */
-	if (pthread_cond_broadcast(&c.thr_cv)){
-		perror("FAIL - failed trying to wake up reading threads");
+	if (pthread_cond_broadcast(&c.thr_cv)) {
+		tst_resm(TFAIL, "failed trying to wake up reading threads");
 		fclose(fd);
 		unlink(filename);
-		_exit(1);
+		cleanup();
 	}
 	/* Release mutex lock */
 	if (pthread_mutex_unlock(&c.m)) {
-		perror("FAIL - failed to release mutex lock");
+		tst_resm(TFAIL, "failed to release mutex lock");
 		fclose(fd);
 		unlink(filename);
-		_exit(1);
+		cleanup();
 	}
 
-        printf("PASS - Threads are done reading\n");
+	tst_resm(TPASS, "Threads are done reading");
 
-    	printf("%s", msg);
 	fclose(fd);
 	unlink(filename);
-	_exit(0);
+	cleanup();
 }
 
-
-
 /* **************************************************************************
-   *				OTHER PROCEDURES			    *
-   ************************************************************************** */
+ *				OTHER PROCEDURES			    *
+ ************************************************************************** */
+
+void close_files(FILE *fd_list[]) {
+	int i;
+	for (i = 0; i < numfiles; i++) {
+		fclose(fd_list[i]);
+	}
+}
 
 /* threads: Each thread opens the files specified */
-void * threads(void* thread_id_)
-{
-  int thread_id=(uintptr_t)thread_id_;
-    	char  errmsg[80];
-        FILE  *fd;
-	int   i;
-
-    	/* Open files */
-    	for (i=0; i<numfiles; i++) {
-                if(debug)
-       		  printf("Thread  %d : Opening file number %d \n", thread_id, i);
-       		if ((fd = fopen(filename,"rw")) == NULL) {
-          		sprintf(errmsg,"FAIL - Couldn't open file #%d",i);
-          		perror(errmsg);
+void * threads(void* thread_id_) {
+	int thread_id = (uintptr_t) thread_id_;
+	char errmsg[80];
+	FILE *fd_list[MAXFILES];
+	int i;
+
+	/* Open files */
+	for (i = 0; i < numfiles; i++) {
+		if (debug)
+			printf("Thread  %d : Opening file number %d \n", thread_id, i);
+		if ((fd_list[i] = fopen(filename, "rw")) == NULL) {
+			sprintf(errmsg, "FAIL - Couldn't open file #%d", i);
+			perror(errmsg);
 			unlink(filename);
-			pthread_exit((void*)1);
-       		}
-    	}
+			pthread_exit((void*) 1);
+		}
+	}
 
 	/* Grab mutex lock */
 	if (pthread_mutex_lock(&c.m)) {
 		perror("FAIL - failed to grab mutex lock");
-       		fclose(fd);
+		close_files(fd_list);
 		unlink(filename);
-		pthread_exit((void*)1);
+		pthread_exit((void*) 1);
 	}
 
 	/* Check if you should wake up main thread */
-        if (++c.thr_sleeping == numthreads)
-                if (pthread_cond_signal(&c.init_cv)){
+	if (++c.thr_sleeping == numthreads)
+		if (pthread_cond_signal(&c.init_cv)) {
 			perror("FAIL - failed to signal main thread");
-       			fclose(fd);
+			close_files(fd_list);
 			unlink(filename);
-			pthread_exit((void*)1);
+			pthread_exit((void*) 1);
 		}
 
 	/* Sleep until woken up */
-        if (pthread_cond_wait(&c.thr_cv, &c.m)){
+	if (pthread_cond_wait(&c.thr_cv, &c.m)) {
 		perror("FAIL - failed to wake up correctly");
-       		fclose(fd);
+		close_files(fd_list);
 		unlink(filename);
-		pthread_exit((void*)1);
+		pthread_exit((void*) 1);
 	}
 
 	/* Release mutex lock */
-        if (pthread_mutex_unlock(&c.m)){
+	if (pthread_mutex_unlock(&c.m)) {
 		perror("FAIL - failed to release mutex lock");
-       		fclose(fd);
+		close_files(fd_list);
 		unlink(filename);
-		pthread_exit((void*)1);
+		pthread_exit((void*) 1);
 	}
 
-	/* Close file and exit */
-       	fclose(fd);
+	/* Close file handles and exit */
+	close_files(fd_list);
 	unlink(filename);
-        pthread_exit((void*)0);
+	pthread_exit((void*) 0);
 }
 
-

[-- Attachment #3: Type: text/plain, Size: 401 bytes --]

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf

[-- Attachment #4: Type: text/plain, Size: 155 bytes --]

_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2009-10-15 22:04 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-09-30  2:06 [LTP] [PATCH][openfile] file descriptors not cleaned up Henry Yei
2009-10-04 22:18 ` Jiri Palecek
2009-10-05  2:51   ` [LTP] GPL License in CrackerJack hisashi.hashimoto.wh
2009-10-13 14:43     ` Subrata Modak
2009-10-05 21:33   ` [LTP] [PATCH][openfile] file descriptors not cleaned up Henry Yei
2009-10-13 14:43     ` Subrata Modak
2009-10-13 19:20       ` Garrett Cooper
2009-10-13 19:21         ` Garrett Cooper
2009-10-14  2:25           ` Henry Yei
2009-10-14  2:30             ` Garrett Cooper
     [not found]               ` <8368651DACC1964480F951191B94A0780153D6A2@svexch01.mvista.com>
2009-10-15 22:03                 ` [LTP] autoconf files Garrett Cooper
2009-10-14 20:28             ` [LTP] [PATCH][openfile] file descriptors not cleaned up Subrata Modak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.