added common alarm(2) implementation

This commit is contained in:
Enrico Scholz 2011-01-14 21:29:01 +01:00
commit 6e9794c707
2 changed files with 131 additions and 6 deletions

View file

@ -193,6 +193,25 @@ index 143113b..a4f0c0c 100644
+GIT_CVSIMPORT=git cvsimport
+cvsimport:
+ $(GIT_CVSIMPORT) -k -p '--cvs-direct' -d :pserver:cvs@cvs.fefe.de:/cvs dietlibc
diff --git a/alpha/__alarm.c b/alpha/__alarm.c
deleted file mode 100644
index 7ca35cb..0000000
--- a/alpha/__alarm.c
+++ /dev/null
@@ -1,13 +0,0 @@
-#include <unistd.h>
-#include <sys/time.h>
-
-unsigned int alarm(unsigned int seconds) {
- struct itimerval old, new;
- unsigned int ret;
- new.it_interval.tv_usec=0;
- new.it_interval.tv_sec=0;
- new.it_value.tv_usec =0;
- new.it_value.tv_sec =(long)seconds;
- if (setitimer(ITIMER_REAL,&new,&old)==-1) return 0;
- return old.it_value.tv_sec+(old.it_value.tv_usec?1:0);
-}
diff --git a/alpha/__time.c b/alpha/__time.c
deleted file mode 100644
index 07275e0..0000000
@ -623,6 +642,13 @@ index 94a4f73..9ad41ff 100644
#ifdef PROFILING
pushl $_etext
pushl $.text
diff --git a/ia64/__alarm.c b/ia64/__alarm.c
deleted file mode 100644
index e2c499f..0000000
--- a/ia64/__alarm.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "alpha/__alarm.c"
diff --git a/ia64/__time.c b/ia64/__time.c
deleted file mode 100644
index 7547acb..0000000
@ -724,6 +750,28 @@ index 9b2d04d..70bb17e 100644
extern int shmget(key_t key, int size, int shmflg) __THROW;
extern void *shmat(int shmid, const void *shmaddr, int shmflg) __THROW;
extern int shmdt (const void *shmaddr) __THROW;
diff --git a/lib/__alarm.c b/lib/__alarm.c
new file mode 100644
index 0000000..9b4bc30
--- /dev/null
+++ b/lib/__alarm.c
@@ -0,0 +1,16 @@
+#include <unistd.h>
+#include <sys/time.h>
+#include <syscalls.h>
+
+#ifndef __NR_alarm
+unsigned int alarm(unsigned int seconds) {
+ struct itimerval old, new;
+ unsigned int ret;
+ new.it_interval.tv_usec=0;
+ new.it_interval.tv_sec=0;
+ new.it_value.tv_usec =0;
+ new.it_value.tv_sec =(long)seconds;
+ if (setitimer(ITIMER_REAL,&new,&old)==-1) return 0;
+ return old.it_value.tv_sec+(old.it_value.tv_usec?1:0);
+}
+#endif
diff --git a/lib/__dtostr.c b/lib/__dtostr.c
index 1d082e3..bc61200 100644
--- a/lib/__dtostr.c
@ -1660,11 +1708,15 @@ index 2e57fbb..486b531 100644
+
+#endif
diff --git a/test/Makefile b/test/Makefile
index eea0075..537b32f 100644
index eea0075..69454e5 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -10,11 +10,11 @@ LCOMPAT=-lcompat
TESTPROGRAMS=adjtime argv asprintf atexit bsearch byteswap calloc confstr cycles empty flush fnmatch \
@@ -7,14 +7,14 @@ CFLAGS=-nostdinc -Wall
LCOMPAT=-lcompat
-TESTPROGRAMS=adjtime argv asprintf atexit bsearch byteswap calloc confstr cycles empty flush fnmatch \
+TESTPROGRAMS=adjtime alarm argv asprintf atexit bsearch byteswap calloc confstr cycles empty flush fnmatch \
fputc ftw fwrite getaddrinfo getenv getgrnam gethostbyaddr gethostbyname \
gethostbyname_r getmntent getopt getpass getpwnam getservbyname getservbyport getusershell \
-glob grent hasmntopt hello iconv if_nameindex ltostr malloc-debugger md5_testharness \
@ -1677,6 +1729,76 @@ index eea0075..537b32f 100644
test: $(TESTPROGRAMS)
diff --git a/test/alarm.c b/test/alarm.c
new file mode 100644
index 0000000..d1e13ce
--- /dev/null
+++ b/test/alarm.c
@@ -0,0 +1,64 @@
+#include <stdlib.h>
+#include <assert.h>
+
+#include <time.h>
+#include <unistd.h>
+#include <signal.h>
+
+static volatile int alrm_triggered;
+
+static void sig_alrm(int s)
+{
+ alrm_triggered = 1;
+}
+
+int main()
+{
+ int rc;
+ time_t end;
+ sighandler_t old_sig;
+
+ alarm(50);
+
+ old_sig = signal(SIGALRM, &sig_alrm);
+ assert(old_sig != SIG_ERR);
+
+ /* check whether alarm() returns correct number of remaining
+ * seconds */
+ rc = alarm(2);
+ assert(rc > 40 && rc <= 50);
+
+ /* check whether SIGALRM is triggered within the set time */
+ end = time(NULL) + 5;
+ while (!alrm_triggered && time(NULL) < end) {
+ /* noop */
+ }
+ assert(alrm_triggered);
+
+ /* there should be no pending alarm */
+ rc = alarm(0);
+ assert(rc == 0);
+
+ alrm_triggered = 0;
+
+ /* test whether alarm can be canceled */
+ rc = alarm(2);
+ assert(rc == 0);
+
+ rc = alarm(0);
+ assert(rc > 0 && rc < 4);
+ assert(!alrm_triggered);
+
+ /* there should not happen an alarm */
+ end = time(NULL) + 5;
+ while (!alrm_triggered && time(NULL) < end) {
+ /* noop */
+ }
+ assert(!alrm_triggered);
+
+ /* there should be no pending alarm */
+ rc = alarm(0);
+ assert(rc == 0);
+
+ return EXIT_SUCCESS;
+}
diff --git a/test/asprintf.c b/test/asprintf.c
index 996a5aa..0d4f2eb 100644
--- a/test/asprintf.c
@ -1995,14 +2117,14 @@ index 4f5b08f..1e67632 100644
return 0;
}
diff --git a/test/runtests.sh b/test/runtests.sh
index 6d89efb..fcd3202 100644
index 6d89efb..3896fbf 100644
--- a/test/runtests.sh
+++ b/test/runtests.sh
@@ -1,6 +1,6 @@
SUBDIRS="dirent inet stdio string stdlib time"
-TESTPROGRAMS="adjtime argv atexit bsearch byteswap calloc confstr empty flush fputc ffs fnmatch ftw fwrite getaddrinfo getenv getdelim getgrnam gethostbyaddr gethostbyname gethostbyname_r getmntent getopt getpwnam getservbyname getservbyport getusershell glob grent hasmntopt hello iconv if_nameindex ltostr malloc-debugger md5_testharness memccpy memchr memcmp memrchr memusage mktime mmap_test pipe printf printftest protoent prototypes putenv pwent rand48 readdir regex select sendfile servent siglist speed spent sprintf sscanf stdarg strcasecmp strcmp strncat strncpy strptime strrchr strstr strtol sysenter ungetc waitpid"
+TESTPROGRAMS="adjtime argv atexit bsearch byteswap calloc confstr empty flush fputc ffs fnmatch ftw fwrite getaddrinfo getenv getdelim getgrnam gethostbyaddr gethostbyname gethostbyname_r getmntent getopt getpwnam getservbyname getservbyport getusershell glob grent hasmntopt hello iconv if_nameindex ltostr malloc-debugger math md5_testharness memccpy memchr memcmp memrchr memusage mktime mmap_test pipe printf printftest protoent prototypes putenv pwent rand48 readdir regex select sendfile servent siglist speed spent sprintf sscanf stdarg strcasecmp strcmp strncat strncpy strptime strrchr strstr strtol sysconf sysenter ungetc waitpid"
+TESTPROGRAMS="adjtime alarm argv atexit bsearch byteswap calloc confstr empty flush fputc ffs fnmatch ftw fwrite getaddrinfo getenv getdelim getgrnam gethostbyaddr gethostbyname gethostbyname_r getmntent getopt getpwnam getservbyname getservbyport getusershell glob grent hasmntopt hello iconv if_nameindex ltostr malloc-debugger math md5_testharness memccpy memchr memcmp memrchr memusage mktime mmap_test pipe printf printftest protoent prototypes putenv pwent rand48 readdir regex select sendfile servent siglist speed spent sprintf sscanf stdarg strcasecmp strcmp strncat strncpy strptime strrchr strstr strtol sysconf sysenter ungetc waitpid"
STDIN="read1"
PASS="getpass"