Skip to content

posix: signal: implement sigprocmask() #67008

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

Merged
merged 4 commits into from
Jan 11, 2024

Conversation

ycsin
Copy link
Member

@ycsin ycsin commented Dec 26, 2023

Implement sigprocmask() by simply redirecting call to the pthread_sigmask() as they are identical in single threaded application.

Fixes #66928

@ycsin ycsin requested a review from cfriedt December 26, 2023 10:33
@ycsin ycsin marked this pull request as ready for review December 26, 2023 12:00
@zephyrbot zephyrbot added the area: POSIX POSIX API Library label Dec 26, 2023
@moonlight83340
Copy link
Contributor

moonlight83340 commented Dec 27, 2023

Just want to understand well that part :

/* this should probably go into signal.c but we need access to the lock */
int pthread_sigmask(int how, const sigset_t *ZRESTRICT set, sigset_t *ZRESTRICT oset)
{
	struct posix_thread *t;

	if (!(how == SIG_BLOCK || how == SIG_SETMASK || how == SIG_UNBLOCK)) {
		return EINVAL;
	}

	t = to_posix_thread(pthread_self());
	if (t == NULL) {
		return ESRCH;
	}

	K_SPINLOCK(&pthread_pool_lock) {
		if (oset != NULL) {
			*oset = t->sigset;
		}

		if (set == NULL) {
			K_SPINLOCK_BREAK;
		}

		switch (how) {
		case SIG_BLOCK:
			for (size_t i = 0; i < ARRAY_SIZE(set->sig); ++i) {
				t->sigset.sig[i] |= set->sig[i];
			}
			break;
		case SIG_SETMASK:
			t->sigset = *set;
			break;
		case SIG_UNBLOCK:
			for (size_t i = 0; i < ARRAY_SIZE(set->sig); ++i) {
				t->sigset.sig[i] &= ~set->sig[i];
			}
			break;
		}
	}

	return 0;
}

Could we move the nonlock part on the signal file and apply the block part from thread fonction ?

/* this should probably go into signal.c but we need access to the lock */
int sigmask(int how, const sigset_t *ZRESTRICT set, sigset_t *ZRESTRICT oset)
{
	struct posix_thread *t;

	if (!(how == SIG_BLOCK || how == SIG_SETMASK || how == SIG_UNBLOCK)) {
		return EINVAL;
	}

	t = to_posix_thread(pthread_self());
	if (t == NULL) {
		return ESRCH;
	}

       pthread_lockPartFunction(struct posix_thread *t,int how, const sigset_t *ZRESTRICT set, sigset_t *ZRESTRICT oset);

	return 0;
}

I want to write it to understand and now I writed it, it sounds so bad 😂

Copy link
Member

@cfriedt cfriedt left a comment

Choose a reason for hiding this comment

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

A temporary nak for the moment because sigprocmask() should only be used with a single-threaded process.

In Zephyr, even with the approximation that we are running in a single process environment, it is (normally) a multi-threaded environment.

This might need to check a Kconfig variable CONFIG_MULTITHREADED before calling into pthread_sigmask().

Likely if CONFIG_MULTITHREADED is chosen, and this function is called, it might need to assert, since it's "unspecified" according to the spec.

https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigprocmask.html

@ycsin ycsin force-pushed the pr/posix_sigprocmask branch from 40aeebe to ff7f9e7 Compare January 4, 2024 02:45
Implement `sigprocmask()` by simply redirecting call to the
`pthread_sigmask()` as they are identical.

Signed-off-by: Yong Cong Sin <[email protected]>
@ycsin ycsin force-pushed the pr/posix_sigprocmask branch from ff7f9e7 to 2e44777 Compare January 4, 2024 02:52
ycsin added 2 commits January 4, 2024 11:21
Refactor the existing `pthread_sigmask` test function to
get its function-under-test via arg and test `sigprocmask` with
that.

Signed-off-by: Yong Cong Sin <[email protected]>
`sigprocmask()` is now implemented, mark it so.

Signed-off-by: Yong Cong Sin <[email protected]>
@ycsin ycsin force-pushed the pr/posix_sigprocmask branch from 2e44777 to 695ade0 Compare January 4, 2024 03:22
The `sigsuspend` is currently `igsuspend`, fix that.

Signed-off-by: Yong Cong Sin <[email protected]>
@@ -230,68 +231,67 @@ static void *test_pthread_sigmask_entry(void *arg)
};
static sigset_t set[2];
const int invalid_how = 0x9a2ba9e;

ARG_UNUSED(arg);
sigmask_fn sigmask = arg;
Copy link
Member

Choose a reason for hiding this comment

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

This is cool 👍

@cfriedt cfriedt merged commit 768ed26 into zephyrproject-rtos:main Jan 11, 2024
@ycsin ycsin deleted the pr/posix_sigprocmask branch January 17, 2024 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: POSIX POSIX API Library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

posix: implement sigprocmask()
6 participants