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



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
#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);
}


Home | Main Index | Thread Index | Old Index