113546Sjulian/*
235509Sjb * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
313546Sjulian * All rights reserved.
413546Sjulian *
513546Sjulian * Redistribution and use in source and binary forms, with or without
613546Sjulian * modification, are permitted provided that the following conditions
713546Sjulian * are met:
813546Sjulian * 1. Redistributions of source code must retain the above copyright
913546Sjulian *    notice, this list of conditions and the following disclaimer.
1013546Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1113546Sjulian *    notice, this list of conditions and the following disclaimer in the
1213546Sjulian *    documentation and/or other materials provided with the distribution.
13165967Simp * 3. Neither the name of the author nor the names of any co-contributors
1413546Sjulian *    may be used to endorse or promote products derived from this software
1513546Sjulian *    without specific prior written permission.
1613546Sjulian *
1713546Sjulian * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
1813546Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1913546Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2049439Sdeischen * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2113546Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2213546Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2313546Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2413546Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2513546Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2613546Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2713546Sjulian * SUCH DAMAGE.
2813546Sjulian *
2950476Speter * $FreeBSD$
3013546Sjulian */
31174112Sdeischen
32174112Sdeischen#include "namespace.h"
3313546Sjulian#include <signal.h>
3413546Sjulian#include <errno.h>
3513546Sjulian#include <pthread.h>
36174112Sdeischen#include "un-namespace.h"
37103388Smini#include "thr_private.h"
3813546Sjulian
3975369Sdeischen__weak_reference(_sigaction, sigaction);
4071581Sdeischen
4113546Sjulianint
4256698Sjasone_sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
4313546Sjulian{
4435509Sjb	int ret = 0;
45120429Sdavidxu	int err = 0;
46117300Sdavidxu	struct sigaction newact, oldact;
47116977Sdavidxu	struct pthread *curthread;
48117300Sdavidxu	kse_critical_t crit;
4913546Sjulian
5013546Sjulian	/* Check if the signal number is out of range: */
51116977Sdavidxu	if (sig < 1 || sig > _SIG_MAXSIG) {
5213546Sjulian		/* Return an invalid argument: */
5322315Sjulian		errno = EINVAL;
5413546Sjulian		ret = -1;
5513546Sjulian	} else {
56117300Sdavidxu		if (act)
57117300Sdavidxu			newact = *act;
58117300Sdavidxu
59116977Sdavidxu		crit = _kse_critical_enter();
60116977Sdavidxu		curthread = _get_curthread();
61116977Sdavidxu		KSE_LOCK_ACQUIRE(curthread->kse, &_thread_signal_lock);
6235509Sjb
63120429Sdavidxu		oldact = _thread_sigact[sig - 1];
64120429Sdavidxu
6513546Sjulian		/* Check if a signal action was supplied: */
6613546Sjulian		if (act != NULL) {
6713546Sjulian			/* Set the new signal handler: */
68117300Sdavidxu			_thread_sigact[sig - 1] = newact;
6935509Sjb		}
7013546Sjulian
7135509Sjb		/*
7235509Sjb		 * Check if the kernel needs to be advised of a change
7335509Sjb		 * in signal action:
7435509Sjb		 */
75159462Smaxim		if (act != NULL) {
7635509Sjb
77117300Sdavidxu			newact.sa_flags |= SA_SIGINFO;
78117300Sdavidxu
7913546Sjulian			/*
8035509Sjb			 * Check if the signal handler is being set to
8135509Sjb			 * the default or ignore handlers:
8213546Sjulian			 */
83117300Sdavidxu			if (newact.sa_handler != SIG_DFL &&
84117300Sdavidxu			    newact.sa_handler != SIG_IGN) {
8535509Sjb				/*
8635509Sjb				 * Specify the thread kernel signal
8735509Sjb				 * handler:
8835509Sjb				 */
89174112Sdeischen				newact.sa_sigaction = _thr_sig_handler;
90117300Sdavidxu			}
91159462Smaxim			/*
92159462Smaxim			 * Install libpthread signal handler wrapper
93159462Smaxim			 * for SIGINFO signal if threads dump enabled
94159462Smaxim			 * even if a user set the signal handler to
95159462Smaxim			 * SIG_DFL or SIG_IGN.
96159462Smaxim			 */
97159462Smaxim			if (sig == SIGINFO && _thr_dump_enabled()) {
98174112Sdeischen				newact.sa_sigaction = _thr_sig_handler;
99159462Smaxim			}
10035509Sjb			/* Change the signal action in the kernel: */
101120429Sdavidxu			if (__sys_sigaction(sig, &newact, NULL) != 0) {
102120429Sdavidxu				_thread_sigact[sig - 1] = oldact;
103120429Sdavidxu				/* errno is in kse, will copy it to thread */
104120429Sdavidxu				err = errno;
10535509Sjb				ret = -1;
106120429Sdavidxu			}
10713546Sjulian		}
108116977Sdavidxu		KSE_LOCK_RELEASE(curthread->kse, &_thread_signal_lock);
109116977Sdavidxu		_kse_critical_leave(crit);
110120429Sdavidxu		/*
111120429Sdavidxu		 * Check if the existing signal action structure contents are
112120429Sdavidxu		 * to be returned:
113120429Sdavidxu		*/
114120429Sdavidxu		if (oact != NULL) {
115120429Sdavidxu			/* Return the existing signal action contents: */
116117300Sdavidxu			*oact = oldact;
117120429Sdavidxu		}
118120429Sdavidxu		if (ret != 0) {
119120429Sdavidxu			/* Return errno to thread */
120120429Sdavidxu			errno = err;
121120429Sdavidxu		}
12213546Sjulian	}
12313546Sjulian
12413546Sjulian	/* Return the completion status: */
12513546Sjulian	return (ret);
12613546Sjulian}
127