initial check-in, version 0.5.0

This commit is contained in:
Rich Felker
2011-02-12 00:22:29 -05:00
commit 0b44a0315b
1021 changed files with 45711 additions and 0 deletions

6
src/signal/bsd_signal.c Normal file
View File

@ -0,0 +1,6 @@
#include <signal.h>
void (*bsd_signal(int sig, void (*func)(int)))(int)
{
return signal(sig, func);
}

12
src/signal/getitimer.c Normal file
View File

@ -0,0 +1,12 @@
#include <sys/time.h>
#include "syscall.h"
int getitimer(int which, struct itimerval *old)
{
int ret;
long kold[4];
if (!(ret = syscall2(__NR_getitimer, which, (long)&kold)))
*old = (struct itimerval){ { kold[0], kold[1] }, { kold[2], kold[3] } };
return ret;
}

View File

@ -0,0 +1,13 @@
.global sigsetjmp
sigsetjmp:
mov 4(%esp),%eax
mov 8(%esp),%ecx
mov %ecx,24(%eax)
jecxz 1f
add $28,%eax
push %eax
push $0
push $2
call sigprocmask
add $12,%esp
1: jmp setjmp

7
src/signal/kill.c Normal file
View File

@ -0,0 +1,7 @@
#include <signal.h>
#include "syscall.h"
int kill(pid_t pid, int sig)
{
return syscall2(__NR_kill, pid, sig);
}

11
src/signal/killpg.c Normal file
View File

@ -0,0 +1,11 @@
#include <signal.h>
#include <errno.h>
int killpg(pid_t pgid, int sig)
{
if (pgid < 0) {
errno = EINVAL;
return -1;
}
return kill(-pgid, sig);
}

7
src/signal/raise.c Normal file
View File

@ -0,0 +1,7 @@
#include <signal.h>
#include "syscall.h"
int raise(int sig)
{
return __syscall_kill(__syscall_getpid(), sig);
}

15
src/signal/setitimer.c Normal file
View File

@ -0,0 +1,15 @@
#include <sys/time.h>
#include "syscall.h"
int setitimer(int which, const struct itimerval *new, struct itimerval *old)
{
int ret;
long knew[4] = {
new->it_interval.tv_sec, new->it_interval.tv_usec,
new->it_value.tv_sec, new->it_value.tv_usec
}, kold[4];
if (!(ret = syscall3(__NR_setitimer, which, (long)&knew, old ? (long)&kold : 0)) && old)
*old = (struct itimerval){ { kold[0], kold[1] }, { kold[2], kold[3] } };
return ret;
}

48
src/signal/sigaction.c Normal file
View File

@ -0,0 +1,48 @@
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include "syscall.h"
#include "pthread_impl.h"
static void restorer()
{
syscall0(__NR_rt_sigreturn);
}
int __libc_sigaction(int sig, const struct sigaction *sa, struct sigaction *old)
{
struct {
void *handler;
unsigned long flags;
void (*restorer)(void);
sigset_t mask;
} ksa, kold;
long pksa=0, pkold=0;
if (sa) {
ksa.handler = sa->sa_handler;
ksa.flags = sa->sa_flags;
ksa.restorer = restorer;
ksa.mask = sa->sa_mask;
pksa = (long)&ksa;
}
if (old) pkold = (long)&kold;
if (syscall4(__NR_rt_sigaction, sig, pksa, pkold, 8))
return -1;
if (old) {
old->sa_handler = kold.handler;
old->sa_flags = kold.flags;
old->sa_mask = kold.mask;
}
return 0;
}
int __sigaction(int sig, const struct sigaction *sa, struct sigaction *old)
{
if (sig == SIGCANCEL || sig == SIGSYSCALL) {
errno = EINVAL;
return -1;
}
return __libc_sigaction(sig, sa, old);
}
weak_alias(__sigaction, sigaction);

13
src/signal/sigaddset.c Normal file
View File

@ -0,0 +1,13 @@
#include <signal.h>
#include <errno.h>
int sigaddset(sigset_t *set, int sig)
{
unsigned s = sig-1;
if (s >= 8*sizeof(sigset_t)) {
errno = EINVAL;
return -1;
}
set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
return 0;
}

8
src/signal/sigaltstack.c Normal file
View File

@ -0,0 +1,8 @@
#include <signal.h>
#include "syscall.h"
int sigaltstack(const stack_t *ss, stack_t *old)
{
/* depends on kernel struct matching */
return syscall2(__NR_sigaltstack, (long)ss, (long)old);
}

13
src/signal/sigdelset.c Normal file
View File

@ -0,0 +1,13 @@
#include <signal.h>
#include <errno.h>
int sigdelset(sigset_t *set, int sig)
{
unsigned s = sig-1;
if (s >= 8*sizeof(sigset_t)) {
errno = EINVAL;
return -1;
}
set->__bits[s/8/sizeof *set->__bits] &=~(1UL<<(s&8*sizeof *set->__bits-1));
return 0;
}

8
src/signal/sigemptyset.c Normal file
View File

@ -0,0 +1,8 @@
#include <signal.h>
#include <string.h>
int sigemptyset(sigset_t *set)
{
memset(set, 0, sizeof *set);
return 0;
}

8
src/signal/sigfillset.c Normal file
View File

@ -0,0 +1,8 @@
#include <signal.h>
#include <string.h>
int sigfillset(sigset_t *set)
{
memset(set, -1, sizeof *set);
return 0;
}

11
src/signal/sighold.c Normal file
View File

@ -0,0 +1,11 @@
#include <signal.h>
#include <stdlib.h>
int sighold(int sig)
{
sigset_t mask;
sigemptyset(&mask);
if (sigaddset(&mask, sig) < 0) return -1;
return sigprocmask(SIG_BLOCK, &mask, NULL);
}

12
src/signal/sigignore.c Normal file
View File

@ -0,0 +1,12 @@
#include <signal.h>
#include <stdlib.h>
int sigignore(int sig)
{
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
return sigaction(sig, &sa, NULL);
}

13
src/signal/siginterrupt.c Normal file
View File

@ -0,0 +1,13 @@
#include <stdlib.h>
#include <signal.h>
int siginterrupt(int sig, int flag)
{
struct sigaction sa;
sigaction(sig, NULL, &sa);
if (flag) sa.sa_flags &= ~SA_RESTART;
else sa.sa_flags |= SA_RESTART;
return sigaction(sig, &sa, NULL);
}

12
src/signal/sigismember.c Normal file
View File

@ -0,0 +1,12 @@
#include <signal.h>
#include <errno.h>
int sigismember(const sigset_t *set, int sig)
{
unsigned s = sig-1;
if (s >= 8*sizeof(sigset_t)) {
errno = EINVAL;
return -1;
}
return !!(set->__bits[s/8/sizeof *set->__bits] & 1UL<<(s&8*sizeof *set->__bits-1));
}

12
src/signal/siglongjmp.c Normal file
View File

@ -0,0 +1,12 @@
#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
void siglongjmp(sigjmp_buf buf, int ret)
{
long *flag = buf + sizeof(jmp_buf)/sizeof(long);
sigset_t *mask = (void *)(flag + 1);
if (*flag)
sigprocmask (SIG_SETMASK, mask, NULL);
longjmp((void *)buf, ret);
}

13
src/signal/signal.c Normal file
View File

@ -0,0 +1,13 @@
#include <signal.h>
#include <stddef.h>
#include "syscall.h"
int __sigaction(int, const struct sigaction *, struct sigaction *);
void (*signal(int sig, void (*func)(int)))(int)
{
struct sigaction sa = { .sa_handler = func, .sa_flags = SA_RESTART };
if (__sigaction(sig, &sa, &sa) < 0)
return SIG_ERR;
return sa.sa_handler;
}

11
src/signal/sigpause.c Normal file
View File

@ -0,0 +1,11 @@
#include <signal.h>
#include <stdlib.h>
int sigpause(int sig)
{
sigset_t mask;
if (sigprocmask(0, NULL, &mask) < 0 || sigdelset(&mask, sig) < 0)
return -1;
return sigsuspend(&mask);
}

7
src/signal/sigpending.c Normal file
View File

@ -0,0 +1,7 @@
#include <signal.h>
#include "syscall.h"
int sigpending(sigset_t *set)
{
return syscall2(__NR_rt_sigpending, (long)set, SYSCALL_SIGSET_SIZE);
}

23
src/signal/sigprocmask.c Normal file
View File

@ -0,0 +1,23 @@
#include <signal.h>
#include "syscall.h"
#include "libc.h"
int __libc_sigprocmask(int how, const sigset_t *set, sigset_t *old)
{
return syscall4(__NR_rt_sigprocmask, how, (long)set, (long)old, 8);
}
int __sigprocmask(int how, const sigset_t *set, sigset_t *old)
{
sigset_t tmp;
/* Quickly mask out bits 32 and 33 (thread control signals) */
if (0 && how != SIG_UNBLOCK && (set->__bits[4/sizeof *set->__bits] & 3UL<<(32&8*sizeof *set->__bits-1))) {
tmp = *set;
set = &tmp;
tmp.__bits[4/sizeof *set->__bits] &= ~(3UL<<(32&8*sizeof *set->__bits-1));
}
return __libc_sigprocmask(how, set, old);
}
weak_alias(__sigprocmask, sigprocmask);
weak_alias(__sigprocmask, pthread_sigmask);

14
src/signal/sigqueue.c Normal file
View File

@ -0,0 +1,14 @@
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include "syscall.h"
int sigqueue(pid_t pid, int sig, const union sigval value)
{
siginfo_t si = {
.si_signo = sig,
.si_code = -1,
.si_value = value,
};
return syscall3(__NR_rt_sigqueueinfo, pid, sig, (long)&si);
}

11
src/signal/sigrelse.c Normal file
View File

@ -0,0 +1,11 @@
#include <signal.h>
#include <stdlib.h>
int sigrelse(int sig)
{
sigset_t mask;
sigemptyset(&mask);
if (sigaddset(&mask, sig) < 0) return -1;
return sigprocmask(SIG_UNBLOCK, &mask, NULL);
}

4
src/signal/sigrtmax.c Normal file
View File

@ -0,0 +1,4 @@
int __libc_current_sigrtmax()
{
return 64;
}

4
src/signal/sigrtmin.c Normal file
View File

@ -0,0 +1,4 @@
int __libc_current_sigrtmin()
{
return 34;
}

28
src/signal/sigset.c Normal file
View File

@ -0,0 +1,28 @@
#include <signal.h>
#include <stdlib.h>
void (*sigset(int sig, void (*handler)(int)))(int)
{
struct sigaction sa, sa_old;
sigset_t mask;
sigemptyset(&mask);
if (sigaddset(&mask, sig) < 0)
return SIG_ERR;
if (handler == SIG_HOLD) {
if (sigaction(sig, NULL, &sa_old) < 0)
return SIG_ERR;
if (sigprocmask(SIG_BLOCK, &mask, &mask) < 0)
return SIG_ERR;
} else {
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if (sigaction(sig, &sa, &sa_old) < 0)
return SIG_ERR;
if (sigprocmask(SIG_UNBLOCK, &mask, &mask) < 0)
return SIG_ERR;
}
return sigismember(&mask, sig) ? SIG_HOLD : sa_old.sa_handler;
}

17
src/signal/sigsetjmp.c Normal file
View File

@ -0,0 +1,17 @@
#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
/* !!! This function will not work unless the compiler performs
* tail call optimization. Machine-specific asm versions should
* be created instead even though the workaround (tail call)
* is entirely non-machine-specific... */
int sigsetjmp(sigjmp_buf buf, int save)
{
long *flag = buf + sizeof(jmp_buf)/sizeof(long);
sigset_t *mask = (void *)(flag + 1);
if ((*flag = save))
sigprocmask (SIG_SETMASK, NULL, mask);
return setjmp((void *)buf);
}

7
src/signal/sigsuspend.c Normal file
View File

@ -0,0 +1,7 @@
#include <signal.h>
#include "syscall.h"
int sigsuspend(const sigset_t *mask)
{
return syscall2(__NR_rt_sigsuspend, (long)mask, sizeof(sigset_t));
}

12
src/signal/sigtimedwait.c Normal file
View File

@ -0,0 +1,12 @@
#include <signal.h>
#include "syscall.h"
int sigtimedwait(const sigset_t *mask, siginfo_t *si, const struct timespec *timeout)
{
long k_timeout[2];
if (timeout) {
k_timeout[0] = timeout->tv_sec;
k_timeout[1] = timeout->tv_nsec;
}
return syscall4(__NR_rt_sigtimedwait, (long)mask, (long)si, timeout ? (long)k_timeout : 0, SYSCALL_SIGSET_SIZE);
}

11
src/signal/sigwait.c Normal file
View File

@ -0,0 +1,11 @@
#include <signal.h>
#include <stddef.h>
int sigwait(const sigset_t *mask, int *sig)
{
siginfo_t si;
if (sigtimedwait(mask, &si, NULL) < 0)
return -1;
*sig = si.si_signo;
return 0;
}

7
src/signal/sigwaitinfo.c Normal file
View File

@ -0,0 +1,7 @@
#include <signal.h>
#include <stddef.h>
int sigwaitinfo(const sigset_t *mask, siginfo_t *si)
{
return sigtimedwait(mask, si, NULL);
}