summaryrefslogtreecommitdiffstats
path: root/misc-progs/asynctest.c
diff options
context:
space:
mode:
authorJavier Martinez Canillas <martinez.javier@gmail.com>2010-11-27 07:49:17 +0100
committerJavier Martinez Canillas <martinez.javier@gmail.com>2010-11-27 07:49:17 +0100
commitab121f379a3cff458c90e6f480ba4bb68c8733dd (patch)
treea9851af109ee83646d108bc247d03b131461b764 /misc-progs/asynctest.c
downloadldd3-ab121f379a3cff458c90e6f480ba4bb68c8733dd.tar.gz
Linux Device Drivers 3 examples
Diffstat (limited to 'misc-progs/asynctest.c')
-rw-r--r--misc-progs/asynctest.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/misc-progs/asynctest.c b/misc-progs/asynctest.c
new file mode 100644
index 0000000..8dcb458
--- /dev/null
+++ b/misc-progs/asynctest.c
@@ -0,0 +1,57 @@
+/*
+ * asynctest.c: use async notification to read stdin
+ *
+ * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
+ * Copyright (C) 2001 O'Reilly & Associates
+ *
+ * The source code in this file can be freely used, adapted,
+ * and redistributed in source or binary form, so long as an
+ * acknowledgment appears in derived source files. The citation
+ * should list that the code comes from the book "Linux Device
+ * Drivers" by Alessandro Rubini and Jonathan Corbet, published
+ * by O'Reilly & Associates. No warranty is attached;
+ * we cannot take responsibility for errors or fitness for use.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <fcntl.h>
+
+int gotdata=0;
+void sighandler(int signo)
+{
+ if (signo==SIGIO)
+ gotdata++;
+ return;
+}
+
+char buffer[4096];
+
+int main(int argc, char **argv)
+{
+ int count;
+ struct sigaction action;
+
+ memset(&action, 0, sizeof(action));
+ action.sa_handler = sighandler;
+ action.sa_flags = 0;
+
+ sigaction(SIGIO, &action, NULL);
+
+ fcntl(STDIN_FILENO, F_SETOWN, getpid());
+ fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | FASYNC);
+
+ while(1) {
+ /* this only returns if a signal arrives */
+ sleep(86400); /* one day */
+ if (!gotdata)
+ continue;
+ count=read(0, buffer, 4096);
+ /* buggy: if avail data is more than 4kbytes... */
+ write(1,buffer,count);
+ gotdata=0;
+ }
+}