Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch Ctrl-C and enable Ctrl-D #77

Merged
merged 2 commits into from Oct 24, 2022

Conversation

tnull
Copy link
Contributor

@tnull tnull commented Oct 16, 2022

Fixes #76.

We catch Ctrl-C/SIGINT via a dummy signal handler and enable the use of Ctrl-D. For convenience, we introduce "exit" and "quit" cli commands.

@TheBlueMatt
Copy link
Contributor

Will this fail to build on Windows?

@TheBlueMatt
Copy link
Contributor

man page says WARNING: the behavior of signal() varies across UNIX versions, and has also varied historically across different versions of Linux. Avoid its use: use sigaction(2) instead. See Portability below.

@tnull
Copy link
Contributor Author

tnull commented Oct 18, 2022

Will this fail to build on Windows?

I honestly don't know and don't have a VM ready to test it out. Now taking no chances and simply disable catching ^C on Windows with 7bb75cc.

@tnull
Copy link
Contributor Author

tnull commented Oct 18, 2022

man page says WARNING: the behavior of signal() varies across UNIX versions, and has also varied historically across different versions of Linux. Avoid its use: use sigaction(2) instead. See Portability below.

Interesting, this warning is not present in the macOS man page. IIUC there wouldn't be a big behavior difference in the dummy case, I'll nonetheless switch over to use sigaction.

@TheBlueMatt
Copy link
Contributor

Should we just add a CI job to build on windows? Feel free to squash.

@tnull tnull force-pushed the 2022-10-catch-ctrlc branch 5 times, most recently from d0387d3 to 5252753 Compare October 18, 2022 17:28
@tnull
Copy link
Contributor Author

tnull commented Oct 18, 2022

Should we just add a CI job to build on windows? Feel free to squash.

Added CI jobs for windows-latest and macos-latest, checked that building would indeed fail on Windows, and squashed fixup commits.

src/main.rs Outdated
let mut new_action: libc::sigaction = core::mem::zeroed();
let mut old_action: libc::sigaction = core::mem::zeroed();

new_action.sa_sigaction = libc::SIG_IGN;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

man page seems to think you should be applying this to sa_handler, not sa_sigaction.

Copy link
Contributor Author

@tnull tnull Oct 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, only thing is, sa_sigaction seems to be the only handler field available in libc?: https://docs.rs/libc/0.2.135/libc/struct.sigaction.html

Also seems to work fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, probably because On some architectures a union is involved: do not assign to both sa_handler and sa_sigaction.

But also If SA_SIGINFO is specified in sa_flags, then sa_sigaction (instead of sa_handler) specifies the signal-handling function for signum. This function receives three arguments, as described below.

If we can't assign to sa_handler we should set SA_SIGINFO, assign to sa_sigaction, and use our own function rather than SIG_IGN.

Copy link
Contributor Author

@tnull tnull Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mh, good point, I didn't consider that those two might not always be a union. I'm however a bit confused by what the current state of sigaction in libc is, since there are some parts of the code that suggest the previous way would work fine. Then again there is rust-lang/libc#2107, which seems to be blocked until the next major release (and somehow still calls the field sa_sigaction, even though also introducing a union with sa_handler).

TLDR: Just to be safe I now went the dummy handler route (and squashed again).

We ignore Ctrl-C/SIGINT via a dummy signal handler and enable the use of
Ctrl-D. For convenience, we introduce "exit" and "quit" cli commands.
@TheBlueMatt TheBlueMatt merged commit 76c26ba into lightningdevkit:main Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Catch ^C
3 participants