bug#70628: segmentation fault reading socketpair after fork

Jakov Tutavac <[email protected]>
Newsgroups gmane.lisp.guile.bugs
Message-ID <CAA2KzJdpT=nYwLcHRqpVGaQs_8K9e-xFP-vfnS7B0bdR8KdOjg@mail.gmail.com>
Dear Sir/Madam,

The 62 lines of code in the attachment result in a segmentation fault when
run.
I hope you can help suggest a fix or a workaround.

user@pcxy:~/$ guile --version
guile (GNU Guile) 3.0.7
Packaged by Debian (3.0.7-deb+3.0.7-1)

user@pcxy:~/$ make
gcc -I/usr/include/guile/3.0 -g -c -o main.o main.c
gcc -o a.out main.o -lguile-3.0 -lgc -lpthread -ldl
user@pcxy:~/$ ./a.out
Parent sv:3
Child sv:4
Segmentation fault (core dumped)

(gdb) r
Starting program: a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[Detaching after fork from child process 100100]

Parent sv:3
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7be68f2 in GC_find_limit_with_bound () from
/lib/x86_64-linux-gnu/libgc.so.1
main.c (text/x-csrc, 1.2 KB)
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <libguile.h>

static void *main_parent(void *arg)
{
	int sv = *((int *) arg);
	SCM port;
	char *buf = "Write from parent\n";

	printf("Parent sv:%d\n", sv);

	port = scm_fdopen(scm_from_int(dup(sv)), scm_from_utf8_stringn("r+", 2));
	scm_c_write(port, buf, sizeof(buf));
}

static void *main_child(void *arg)
{
	int sv = *((int *) arg);
	SCM port;
	char *buf = "                 \n";

	printf("Child sv:%d\n", sv);

	port = scm_fdopen(scm_from_int(dup(sv)), scm_from_utf8_stringn("r+", 2));
	scm_c_read(port, buf, sizeof(buf));
	printf("%s\n", buf);
}

int main(int argc, char *argv[])
{
	pid_t id;
	int sv[2];

	if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sv) != 0) {
		fprintf(stderr, "%s", strerror(errno));
		return EXIT_FAILURE;
	}

	id = fork();
	if (id == -1) {
		fprintf(stderr, "%s", strerror(errno));
		return EXIT_FAILURE;
	}

	if (id == 0) {
		/* parent process */
		scm_with_guile(main_parent, &sv[0]);
	} else {
		/* child process */
		scm_with_guile(main_child, &sv[1]);
	}
	wait(NULL);
	return EXIT_SUCCESS;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.