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
os/proc-kill now accepts an optional signal to send #1137
Conversation
A better approach would be to have a null-terminated struct mapping the keywords to the signals, which would make it less repetitive and easier to add new signals. struct keyword_signal {
const char *keyword;
int signal;
};
static const struct keyword_signal signal_keywords[] = {
{"abrt", SIGINT},
{"alrm", SIGALRM},
{"fpe", SIGFPE},
{"hup, SIGHUP},
{"ill", SIGILL},
{"int", SIGINT},
{"kill", SIGKILL},
// ... continue on like this, also consider using ifdefs to make sure all of the signals are supported on given platforms
{NULL, 0},
};
// inside of os_proc_kill
const struct keyword_signal *ptr = signal_keywords;
while (ptr->keyword) {
if (janet_cstrcmp(signal_kw, ptr->keyword)) {
signal = ptr->signal;
break;
}
ptr++;
} |
Thanks for your Input! Your suggestion is easier to extend, but I'm not sure if that's important, as the signals are unlikely to change anytime soon. |
A lot of your signals are actually already extensions, and as such not strictly guaranteed. Thankfully, ISO C99 also defines the signals in question to be macros (rather than enums).
Yes. In C, array access is actually pointer math - which is why C arrays are zero-indexed.
My code isn't strictly checked, sorry ^^ |
Also, I usually do a |
Given that |
@CosmicToast Thanks for the detailed explanation, I really appreciate it! I'll push a patch with your suggestions later. |
OK, I'm still not sure how to write a test for the feature, but the code is now cleaner, throws an error when the signal is not defined, and it's working as expected on my machine |
May be the |
I could do that, but a test checking for other signals would be even better. But that has to wait for native signal catching support in janet I guess. |
Ok, added the simple test. If someone tackles the signal catching feature at some time, a more advanced test can be added. |
Is there a specific reason for not having the signal definitions on windows? |
Are those these? Update: seems we can link to individual pages in pdfs these days, so we can make use of CosmicToast's info about section 7.14 in the form of a link to a relevant page in a draft of ISO/IEC 9899 from 2005. |
Right.
|
But so they have any effect on windows? |
They absolutely have an effect on Windows. There are absolutely weirdnesses to the Windows implementation of things. In short, there is no reason to handle Windows separately here, since all of the features are based on ISO C99. |
src/core/os.c
Outdated
@@ -624,26 +624,133 @@ JANET_CORE_FN(os_proc_wait, | |||
#endif | |||
} | |||
|
|||
#ifndef JANET_WINDOWS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still needs removing for windows support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah just saw your comment, I'm currently trying to get a dev environment running on a windows VM to properly test the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But thanks. I removed this ifdef now I just gotta test it all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And fix all the compile error...
It looks like
On a Linux box here a Would |
Please notice CI failed for windows. Afaik Honestly, I still think this functionality has a better place in a POSIX-specific function in spork because |
That are some good points. Before closing this completely, I'd like to hear @bakpakin's opinion on this. |
Also regarding windows: I know about the failed tests, the code doesn't compile as is on Windows, as I don't know how to send signals on Windows. The previously working code simply ignored signals on Windows. |
That's accurate. I think keeping the code as an
That makes sense.
I don't personally think non-portable Janet programs are strictly an issue, just like non-portable Python programs are an issue, for instance. |
I think we're likely all on the same page here. I took the original comment to suggest there would be a tendency toward writing programs that would unintentionally be non-portable for no real good reason. |
So as far as windows implementations, I think it is useful to look to python or other languages and their analogs. Windows does not have support for signals, so perhaps the second argument could be ignored on windows, or if provided, needs to be some default value |
I reverted to the old behaviour of ignoring signals on windows, will look at some other cross-platform languages and how they handle this |
Oh and it seems that I still have to fix windows compile |
Minor comment -- may be you can |
@sogaiu Sure |
Pythons signal handling seems very simplistic: |
Relevant documentation in python with a mention of windows: Perhaps we could accept three signals for windows: terminate, ctrl-c-event and ctrl-break-event |
I don't know if the info is up-to-date, but there's a comment for this SO answer that says:
@tionis The link for "Relevant documentation in python with a mention of windows:" looks the same to me as the one for "Pythons signal handling seems very simplistic:". Is that intentional? May be this file is relevant? Ah, may be this is the receiving end... |
Thanks for the updated link. |
Oh no, it's not, I updated it, thanks. Regarding the SO link: To introduce some quick, working, inelegant code into core that is difficult to change later would be foolish, I think. |
I added an optional keyword parameter to os/proc-kill that supports sending a custom signal instead of SIGKILL.
I ain't got much experience in C, so the implementation is quite simple and just converts the keywords to the relevant constants with an if-else structure and
janet_cstrcmp
.Please let me know if there's a better way.
The option is ignored on Windows, as there are no signals on Windows as far as I know.
There is no test yet, as janet code can't catch signals as far as I can tell, but I tested it on my machine with a bash script:
and the following janet invocation:
$ build/janet -e '(def proc (os/spawn ["./test.sh"] :p))(os/sleep 1)(os/proc-kill proc true :int)' sleeping bye