[Stgt-devel] [Patch] Small problem in the logging code

Tomas Henzl thenzl
Wed Jul 30 14:31:28 CEST 2008


Hi,

I have found what I think is a small problem in the log.c. I think that
it is caused by the fact that we are using the same sembuf struct 
in the semop function for acquiring and releasing the semaphore and this 
structure is shared by all threads. 

I believe that this can lead to either stopping the mechanism 
(semop fails every time) or segfault when two threads are accessing 
the log_enqueue at the same time.

The attached patch should solve this.

Tomas

Signed-off-by: Tomas Henzl <thenzl at redhat.com>
---
diff -Naurp tgt/usr/log.c tgt2/usr/log.c
--- tgt/usr/log.c	2008-07-28 12:37:20.000000000 +0200
+++ tgt2/usr/log.c	2008-07-30 13:48:25.000000000 +0200
@@ -108,8 +108,13 @@ static int logarea_init (int size)
 		return 1;
 	}
 
-	la->ops[0].sem_num = 0;
-	la->ops[0].sem_flg = 0;
+	la->acquire.sem_num = 0;
+	la->acquire.sem_flg = 0;
+	la->acquire.sem_op  = -1;
+
+	la->release.sem_num = 0;
+	la->release.sem_flg = 0;
+	la->release.sem_op  = 1;
 
 	return 0;
 }
@@ -242,16 +248,14 @@ static void dolog(int prio, const char *
 		ts.tv_sec = 0;
 		ts.tv_nsec = 10000;
 
-		la->ops[0].sem_op = -1;
-		if (semtimedop(la->semid, la->ops, 1, &ts) < 0) {
+		if (semtimedop(la->semid, &la->acquire, 1, &ts) < 0) {
 			syslog(LOG_ERR, "semop up failed");
 			return;
 		}
 
 		log_enqueue(prio, fmt, ap);
 
-		la->ops[0].sem_op = 1;
-		if (semop(la->semid, la->ops, 1) < 0) {
+		if (semop(la->semid, &la->release, 1) < 0) {
 			syslog(LOG_ERR, "semop down failed");
 			return;
 		}
@@ -292,14 +296,12 @@ void log_debug(const char *fmt, ...)
 static void log_flush(void)
 {
 	while (!la->empty) {
-		la->ops[0].sem_op = -1;
-		if (semop(la->semid, la->ops, 1) < 0) {
+		if (semop(la->semid, &la->acquire, 1) < 0) {
 			syslog(LOG_ERR, "semop up failed");
 			exit(1);
 		}
 		log_dequeue(la->buff);
-		la->ops[0].sem_op = 1;
-		if (semop(la->semid, la->ops, 1) < 0) {
+		if (semop(la->semid, &la->release, 1) < 0) {
 			syslog(LOG_ERR, "semop down failed");
 			exit(1);
 		}
diff -Naurp tgt/usr/log.h tgt2/usr/log.h
--- tgt/usr/log.h	2008-07-28 12:37:20.000000000 +0200
+++ tgt2/usr/log.h	2008-07-30 13:46:38.000000000 +0200
@@ -55,7 +55,7 @@ struct logarea {
 	void *start;
 	void *end;
 	char *buff;
-	struct sembuf ops[1];
+	struct sembuf acquire, release;
 	int semid;
 	union semun semarg;
 };
--






More information about the stgt mailing list