unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Jakov Tutavac <jakov.tutavac@gmail.com>
To: 70628@debbugs.gnu.org
Subject: bug#70628: segmentation fault reading socketpair after fork
Date: Sun, 28 Apr 2024 14:19:52 +0200	[thread overview]
Message-ID: <CAA2KzJdpT=nYwLcHRqpVGaQs_8K9e-xFP-vfnS7B0bdR8KdOjg@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 801 bytes --]

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

[-- Attachment #1.2: Type: text/html, Size: 1001 bytes --]

[-- Attachment #2: main.c --]
[-- Type: text/x-csrc, Size: 1196 bytes --]

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

                 reply	other threads:[~2024-04-28 12:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAA2KzJdpT=nYwLcHRqpVGaQs_8K9e-xFP-vfnS7B0bdR8KdOjg@mail.gmail.com' \
    --to=jakov.tutavac@gmail.com \
    --cc=70628@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).