From: Harald Anlauf <anlauf@gmx.de>
To: GFortran mailing list <fortran@gcc.gnu.org>,
stanislav.maslovski@gmail.com
Subject: Re: Segfaults in SIGINT signal handler
Date: Sun, 15 Oct 2023 20:14:29 +0200 [thread overview]
Message-ID: <0aa83e43-7f05-494e-9ef3-01e5b86300c9@gmx.de> (raw)
In-Reply-To: <ZSvmjLGlOSnPtBDX@zephyrus.lan>
Hi Stanislav,
Am 15.10.23 um 15:18 schrieb Stanislav Maslovski:
> Hi,
>
> First of all, many thanks to everyone involved in developing and
> maintaining gfortran!
>
> I have been trying to use gfortran's function signal() to add a SIGINT
> signal handler to my program. Gfortran's documentation for this function
> says that, when the signal handler is called, it is given an argument of
> integer type. However, when I try to access this argument in the
> signal handler, my program segfaults.
>
> Here are two examples:
>
> ================= this code segfaults ===================
>
> program test_signal
> implicit none
> integer, parameter :: SIGINT = 2, SIG_ERR = -1
> logical :: sig_received = .false.
> common /signal/ sig_received
> external sig_handler
> if (signal(SIGINT, sig_handler) /= SIG_ERR) then
> print *, "Signal handler installed"
> else
> print *, "Error in signal()"
> endif
> do while (.true.)
> print *, "Waiting for signal..."
> do while (.not. sig_received)
> continue
> enddo
> print *, "Signal received!"
> sig_received = .false.
> enddo
> end program
>
> subroutine sig_handler(s)
> implicit none
> integer, parameter :: SIGINT = 2
> integer, intent(in) :: s
> logical sig_received
> common /signal/ sig_received
> if (s == SIGINT) sig_received = .true.
> end subroutine
>
> =========================================================
>
> $ gfortran test_signal.f90
> $ ./a.out
> Signal handler installed
> Waiting for signal...
> ^C
> Program received signal SIGSEGV: Segmentation fault - invalid memory
> reference.
>
> Backtrace for this error:
> #0 0x7f9c312218c2 in ???
> #1 0x7f9c31220a55 in ???
> #2 0x7f9c3105afcf in ???
> at ./signal/../sysdeps/unix/sysv/linux/x86_64/libc_sigaction.c:0
> #3 0x562929f153bc in ???
> #4 0x7f9c3105afcf in ???
> at ./signal/../sysdeps/unix/sysv/linux/x86_64/libc_sigaction.c:0
> #5 0x562929f152f4 in ???
> #6 0x562929f153a8 in ???
> #7 0x7f9c310461c9 in __libc_start_call_main
> at ../sysdeps/nptl/libc_start_call_main.h:58
> #8 0x7f9c31046284 in __libc_start_main_impl
> at ../csu/libc-start.c:360
> #9 0x562929f150c0 in ???
> #10 0xffffffffffffffff in ???
> zsh: segmentation fault ./a.out
>
> ============= this code does not segfault ===============
>
> program test_signal
> implicit none
> integer, parameter :: SIGINT = 2, SIG_ERR = -1
> logical :: sig_received = .false.
> common /signal/ sig_received
> external sig_handler
> if (signal(SIGINT, sig_handler) /= SIG_ERR) then
> print *, "Signal handler installed"
> else
> print *, "Error in signal()"
> endif
> do while (.true.)
> print *, "Waiting for signal..."
> do while (.not. sig_received)
> continue
> enddo
> print *, "Signal received!"
> sig_received = .false.
> enddo
> end program
>
> subroutine sig_handler(s)
> implicit none
> integer, intent(in) :: s
> logical sig_received
> common /signal/ sig_received
> sig_received = .true.
> end subroutine
>
> =========================================================
>
> What am I doing wrong?
the man page for signal states that the interface of the signal handler is:
void (*sighandler_t)(int);
which means that the signal number is passed by value, not by reference.
If you replace
integer, intent(in) :: s
by
integer, value :: s
you'll get the intended behavior.
> With best regards,
>
prev parent reply other threads:[~2023-10-15 18:14 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-15 13:18 Stanislav Maslovski
2023-10-15 18:14 ` Harald Anlauf [this message]
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0aa83e43-7f05-494e-9ef3-01e5b86300c9@gmx.de \
--to=anlauf@gmx.de \
--cc=fortran@gcc.gnu.org \
--cc=stanislav.maslovski@gmail.com \
/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).