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



	Hello,

	I have found another strange behavior with POSIX semaphores that could
be related to this PR. I have attached a little test program. This
program only does two fork() to daemonize itself.

	Please note that it creates two named semaphores, one in main program
and another one in daemonized process.

schwarz# gcc daemon.c -g -lrt
schwarz# ./a.out
> /TEST-29002
Parent process
< /TEST-29002
[1]   Segmentation fault (core dumped) ./a.out
schwarz# End of second process
Daemonized process
> /TEST-19064
Closing semaphore
< /TEST-19064

	sem_close() raises a segfault:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000759346a017b8 in sem_close () from /usr/lib/librt.so.1
(gdb) bt
#0  0x0000759346a017b8 in sem_close () from /usr/lib/librt.so.1
#1  0x0000000000400c1a in sem_delete (semaphore=0x7f7fff6b44f0) at
daemon.c:36
#2  0x0000000000400d1c in main () at daemon.c:84

	To be clear, each segfault triggers a segfault and a.out generates two
a.out.core (one for main program, second one for daemonized process).

	Same program perfetcly runs on a Linux workstation :

hilbert:[~] > ./a.out
> /TEST-26345
Parent process
< /TEST-26345
sem_close: Invalid argument
After sem_close()
End of parent process
hilbert:[~] > End of second process
Daemonized process
> /TEST-26347
Closing semaphore
< /TEST-26347
sem_close: Invalid argument
After sem_close()
End

	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)
{
	char	name[15];

	sprintf(name, "/TEST-%d", getpid());
	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)
{
	char	name[15];

	sprintf(name, "/TEST-%d", getpid());
	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	semaphore;

	sem_create(&semaphore);

	pid = fork();

	if (pid > 0)
	{
		printf("Parent process\n");
		sem_delete(&semaphore);
		printf("End of parent process\n");
		fflush(NULL);
		_exit(EXIT_SUCCESS);
	}

	sleep(1);

	setsid();
	pid = fork();

	if (pid > 0)
	{
		printf("End of second process\n");
		fflush(NULL);
		_exit(EXIT_SUCCESS);
	}

	printf("Daemonized process\n");
	fflush(NULL);
	sem_create(&semaphore);
	printf("Closing semaphore\n");
	fflush(NULL);
	sem_delete(&semaphore);
	printf("End\n");
	fflush(NULL);

	sleep(1);

	exit(EXIT_SUCCESS);
}


Home | Main Index | Thread Index | Old Index