Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add missing POSIX sig2str(3) & str2sig(3) calls #1696

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

Closed
wants to merge 7 commits into from

Conversation

ricardobranco777
Copy link
Contributor

@ricardobranco777 ricardobranco777 commented May 10, 2025

Add sig2str(3) & str2sig(3) calls defined in POSIX 1003.1-2024:

Also add needed SIG2STR_MAX definition:

Alternative implementations:

Notes:

  • Except for manpage date, this patch applies cleanly on FreeBSD 14
  • Had to remove a smaller definition for SIG2STR_MAX here to use 32 as defined by Illumos

Next steps could be:

  • Modifying kill, fuser and other tools that need translation between signal names & numbers.
  • Add POSIX definition for NSIG_MAX in <limits.h>

Fixes https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=286714

Copy link

github-actions bot commented May 10, 2025

Thank you for taking the time to contribute to FreeBSD!
There is an issue that needs to be fixed:

@ricardobranco777
Copy link
Contributor Author

Tested with:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>

static void
test(const char *arg)
{
	char buf[SIG2STR_MAX];
	int signum;
	char *end;

	if (isdigit((unsigned char)arg[0])) {
		signum = strtol(arg, &end, 10);
		if (sig2str(signum, buf) == 0)
			printf("sig2str(%d) = \"%s\"\n", signum, buf);
		else
			printf("sig2str(%d) = error\n", signum);
	} else {
		if (str2sig(arg, &signum) == 0)
			printf("str2sig(\"%s\") = %d\n", arg, signum);
		else
			printf("str2sig(\"%s\") = error\n", arg);
	}
}

int
main(int argc, char *argv[])
{
	for (int i = 1; i < argc; i++)
		test(argv[i]);

	return (0);
}
# Shall work
./str2sig HUP 1 USR2 31 RTMIN 65 RTMIN+1 66 RTMAX 126 RTMAX-1 125 RTMIN+30 95 RTMAX-30 96
str2sig("HUP") = 1
sig2str(1) = "HUP"
str2sig("USR2") = 31
sig2str(31) = "USR2"
str2sig("RTMIN") = 65
sig2str(65) = "RTMIN"
str2sig("RTMIN+1") = 66
sig2str(66) = "RTMIN+1"
str2sig("RTMAX") = 126
sig2str(126) = "RTMAX"
str2sig("RTMAX-1") = 125
sig2str(125) = "RTMAX-1"
str2sig("RTMIN+30") = 95
sig2str(95) = "RTMIN+30"
str2sig("RTMAX-30") = 96
sig2str(96) = "RTMAX-30"

# Shall fail
./str2sig XXX 0 -1 SIGXXX RTMIN-1 RTMAX+1
str2sig("XXX") = error
sig2str(0) = error
str2sig("-1") = error
str2sig("SIGXXX") = error
str2sig("RTMIN-1") = error
str2sig("RTMAX+1") = error

sys/sys/signal.h Outdated Show resolved Hide resolved
lib/libc/gen/psignal.c Outdated Show resolved Hide resolved
lib/libc/gen/psignal.c Outdated Show resolved Hide resolved
lib/libc/gen/psignal.c Outdated Show resolved Hide resolved
@ricardobranco777 ricardobranco777 force-pushed the sig2str branch 4 times, most recently from e1b5058 to 4b5d6cd Compare May 11, 2025 15:44
Copy link
Member

@kostikbel kostikbel left a comment

Choose a reason for hiding this comment

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

I think we should put the new functions into a new source file, in particular due to stdio use, as you noted.

sys/sys/signal.h Outdated Show resolved Hide resolved
lib/libc/gen/psignal.c Outdated Show resolved Hide resolved
lib/libc/gen/psignal.c Outdated Show resolved Hide resolved
lib/libc/gen/psignal.c Outdated Show resolved Hide resolved
@ricardobranco777 ricardobranco777 force-pushed the sig2str branch 5 times, most recently from f19e29b to 1e77d5a Compare May 11, 2025 21:27
lib/libc/gen/sig2str.c Outdated Show resolved Hide resolved
Copy link
Member

@kostikbel kostikbel left a comment

Choose a reason for hiding this comment

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

@bsdimp please review and push

@bsdimp bsdimp self-assigned this May 13, 2025
@bsdimp bsdimp added needs-review Someone should look at this before proceeding ready Seems ready to go, subject to final sanity check labels May 13, 2025
@dag-erling
Copy link
Member

Could I ask you to please rewrite your test to use libatf-c, expand it to cover as many cases (including errors) as possible, and include it in the PR? (give it a name that ends in _test, put it in lib/libc/tests/gen/, don't forget to update the Makefile)

@ricardobranco777
Copy link
Contributor Author

Could I ask you to please rewrite your test to use libatf-c, expand it to cover as many cases (including errors) as possible, and include it in the PR? (give it a name that ends in _test, put it in lib/libc/tests/gen/, don't forget to update the Makefile)

Sure, I could do that. I have some experience with ATF on NetBSD.

Copy link
Member

@dag-erling dag-erling left a comment

Choose a reason for hiding this comment

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

Please also consider adding SSP instrumentation, see for instance d0b7445 which added instrumentation for realpath() which, like sig2str(), has an implied buffer size.

lib/libc/gen/psignal.3 Outdated Show resolved Hide resolved
lib/libc/gen/psignal.3 Outdated Show resolved Hide resolved
lib/libc/gen/psignal.3 Outdated Show resolved Hide resolved
lib/libc/gen/psignal.3 Outdated Show resolved Hide resolved
lib/libc/gen/psignal.3 Outdated Show resolved Hide resolved
lib/libc/gen/sig2str.c Outdated Show resolved Hide resolved
include/signal.h Outdated Show resolved Hide resolved
lib/libproc/libproc.h Show resolved Hide resolved
lib/libc/gen/sig2str.c Outdated Show resolved Hide resolved
usr.bin/fstat/fuser.c Outdated Show resolved Hide resolved
@dag-erling
Copy link
Member

Additionally, in bin/kill/kill.c, signame_to_signum() can be replaced with str2sig() and the -l logic can use sig2str() instead of accessing sys_signame[] directly.

BTW, both times strtol() is used in that file to parse a signal number, it assigns a long to an int without a range check, and the subsequent error check is incorrect. It would probably be better to use strtonum() instead. Also, it is bad form to use “illegal” in an error message; it should be replaced with something more neutral like “invalid” or “unrecognized”.

@dag-erling
Copy link
Member

In bin/sh/trap.c, sigstring_to_signum() can be replaced with str2sig(). It still needs to check for EXIT, but since the function is only used once, that check can be moved to the caller. The other uses of sys_signame in bin/sh can be left as-is.

@ricardobranco777 ricardobranco777 force-pushed the sig2str branch 2 times, most recently from 6c878f2 to 733c7e4 Compare May 14, 2025 19:16
lib/libc/gen/sig2str.c Outdated Show resolved Hide resolved
@ricardobranco777 ricardobranco777 force-pushed the sig2str branch 2 times, most recently from a63aa06 to 5360d85 Compare May 14, 2025 20:52
lib/libc/gen/psignal.3 Outdated Show resolved Hide resolved
lib/libc/gen/psignal.3 Outdated Show resolved Hide resolved
lib/libc/gen/psignal.3 Outdated Show resolved Hide resolved
lib/libproc/libproc.h Show resolved Hide resolved
usr.bin/fstat/fuser.c Outdated Show resolved Hide resolved
@ricardobranco777 ricardobranco777 force-pushed the sig2str branch 7 times, most recently from d31b9dc to a840ae5 Compare May 19, 2025 12:30
@ricardobranco777
Copy link
Contributor Author

$ cd /usr/tests/lib/libc/secure
$ kyua test fortify_signal_test
fortify_signal_test:sig2str_before_end  ->  passed  [0.002s]
fortify_signal_test:sig2str_end  ->  passed  [0.002s]
fortify_signal_test:sig2str_heap_after_end  ->  passed  [0.003s]
fortify_signal_test:sig2str_heap_before_end  ->  passed  [0.001s]
fortify_signal_test:sig2str_heap_end  ->  passed  [0.002s]
...

$ cd /usr/tests/lib/libc/gen
$ kyua test sig2str_test
sig2str_test:sig2str_invalid  ->  passed  [0.001s]
sig2str_test:sig2str_valid  ->  passed  [0.002s]
sig2str_test:str2sig_fullname  ->  passed  [0.002s]
sig2str_test:str2sig_invalid  ->  passed  [0.002s]
sig2str_test:str2sig_invalid_rt  ->  passed  [0.002s]
sig2str_test:str2sig_lowercase  ->  passed  [0.002s]
sig2str_test:str2sig_numeric  ->  passed  [0.002s]
sig2str_test:str2sig_rtmin_rtmax  ->  passed  [0.001s]
...

$ cd /usr/tests/bin/sh/builtins
$ kyua test functional_test:kill1
functional_test:kill1  ->  passed  [0.019s]
...
$ kyua test functional_test:kill2
functional_test:kill2  ->  passed  [0.017s]
...

@dag-erling dag-erling self-requested a review May 24, 2025 10:00
@bsdimp
Copy link
Member

bsdimp commented Jun 5, 2025

I think this is ready to go and all the feedback has been addressed. Is that assessment correct?

@bsdimp bsdimp added the merged Closed commit that's been merged label Jun 11, 2025
@bsdimp
Copy link
Member

bsdimp commented Jun 11, 2025

Automated message from ghpr: Thank you for your submission. This PR has been merged to FreeBSD's branch. These changes will appear shortly on our GitHub mirror.

@bsdimp bsdimp closed this Jun 11, 2025
freebsd-git pushed a commit that referenced this pull request Jun 11, 2025
Reviewed by: imp, kib, des, jilles
Pull Request: #1696
freebsd-git pushed a commit that referenced this pull request Jun 11, 2025
Signed-off-by: Ricardo Branco <rbranco@suse.de>
Reviewed by: imp, kib, des, jilles
Pull Request: #1696
freebsd-git pushed a commit that referenced this pull request Jun 11, 2025
Reviewed by: imp, kib, des, jilles
Pull Request: #1696
freebsd-git pushed a commit that referenced this pull request Jun 11, 2025
Reviewed by: imp, kib, des, jilles
Pull Request: #1696
freebsd-git pushed a commit that referenced this pull request Jun 11, 2025
Reviewed by: imp, kib, des, jilles
Pull Request: #1696
freebsd-git pushed a commit that referenced this pull request Jun 11, 2025
Reviewed by: imp, kib, des, jilles
Pull Request: #1696
freebsd-git pushed a commit that referenced this pull request Jun 11, 2025
sig2str(3)

Reviewed by: imp, kib, des, jilles
Pull Request: #1696
Closes: #1696
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged Closed commit that's been merged needs-review Someone should look at this before proceeding ready Seems ready to go, subject to final sanity check
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.