NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: PR/53998 CVS commit: src/sys/kern



The following reply was made to PR kern/53998; it has been noted by GNATS.

From: =?UTF-8?Q?BERTRAND_Jo=c3=abl?= <joel.bertrand%systella.fr@localhost>
To: Christos Zoulas <christos%zoulas.com@localhost>,
        "gnats-bugs%netbsd.org@localhost" <gnats-bugs%NetBSD.org@localhost>
Cc: kern-bug-people%netbsd.org@localhost, gnats-admin%netbsd.org@localhost, netbsd-bugs%netbsd.org@localhost
Subject: Re: PR/53998 CVS commit: src/sys/kern
Date: Sat, 23 Feb 2019 19:51:10 +0100

 This is a multi-part message in MIME format.
 --------------B994DFAEB0534B5A216F6793
 Content-Type: text/plain; charset=UTF-8
 Content-Transfer-Encoding: 8bit
 
 Christos Zoulas a écrit :
 > The original program was incorrect; here's a fixed version that works as expected.
 
 	Oops, sorry, I don't understand why I have forgotten a pointer.
 That's being said, in my original program, I use sem_t * and this
 program fails.
 
 	I have tried to reproduce in a simple program that raises issue I can
 see in RPL/2 (daemon.c in attachment).
 
 schwarz# gcc daemon.c -lrt
 schwarz# ./a.out
 > /TEST-23204-1
 > /TEST-23204-2
 Parent process
 < /TEST-23204-1
 After sem_close()
 < /TEST-23204-2
 After sem_close()
 End of parent process
 schwarz# End of second process
 Daemonized process
 > /TEST-7787-1
 sem_open(): Unknown error: 4294967295
 > /TEST-7787-2
 sem_open(): Unknown error: 4294967295
 Closing semaphore
 < /TEST-7787-1
 
 and generates core file.
 
 	Oops ;-)
 
 	To trigger this issue, you have to define more than one semaphore. With
 only ONE semaphore, program runs fine. With more than one, I suppose
 sem_close() does some memory corruptions.
 
 	sem_close() is called from child process as sem_unlink() is not enough
 to free ressources.
 
 	Best regards,
 
 	JKB
 
 --------------B994DFAEB0534B5A216F6793
 Content-Type: text/x-csrc;
  name="daemon.c"
 Content-Transfer-Encoding: 7bit
 Content-Disposition: attachment;
  filename="daemon.c"
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <semaphore.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 
 static void
 sem_create(sem_t **semaphore, int s)
 {
 	char	name[15];
 
 	sprintf(name, "/TEST-%d-%d", getpid(), s);
 	printf("> %s\n", name);
 	fflush(NULL);
 
 	if ((*semaphore = sem_open(name, O_RDWR | O_CREAT | O_EXCL,
 			S_IRUSR | S_IWUSR, 0)) == SEM_FAILED)
 	{
 		perror("sem_open()");
 		fflush(NULL);
 	}
 
 	return;
 }
 
 static void
 sem_delete(sem_t **semaphore, int s)
 {
 	char	name[15];
 
 	sprintf(name, "/TEST-%d-%d", getpid(), s);
 	printf("< %s\n", name);
 	fflush(NULL);
 
 	if (sem_close(*semaphore) != 0)
 	{
 		perror("sem_close");
 		fflush(NULL);
 	}
 
 	printf("After sem_close()\n");
 	sem_unlink(name);
 
 	return;
 }
 
 int
 main()
 {
 	int		pid;
 	sem_t	*semaphore1;
 	sem_t	*semaphore2;
 
 	sem_create(&semaphore1, 1);
 	sem_create(&semaphore2, 2);
 
 	pid = fork();
 
 	if (pid > 0)
 	{
 		printf("Parent process\n");
 		sem_delete(&semaphore1, 1);
 		sem_delete(&semaphore2, 2);
 		printf("End of parent process\n");
 		_exit(EXIT_SUCCESS);
 	}
 
 	sleep(1);
 
 	setsid();
 	pid = fork();
 
 	if (pid > 0)
 	{
 		sem_close(semaphore1);
 		sem_close(semaphore2);
 		printf("End of second process\n");
 		fflush(NULL);
 		_exit(EXIT_SUCCESS);
 	}
 
 	sem_close(semaphore1);
 	sem_close(semaphore2);
 
 	printf("Daemonized process\n");
 	fflush(NULL);
 	sem_create(&semaphore1, 1);
 	sem_create(&semaphore2, 2);
 	printf("Closing semaphore\n");
 	fflush(NULL);
 	sem_delete(&semaphore1, 1);
 	sem_delete(&semaphore2, 2);
 	printf("End\n");
 	fflush(NULL);
 
 	sleep(1);
 
 	exit(EXIT_SUCCESS);
 }
 
 --------------B994DFAEB0534B5A216F6793--
 


Home | Main Index | Thread Index | Old Index