Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * asynctest.c: use async notification to read stdin
  3  *
  4  * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
  5  * Copyright (C) 2001 O'Reilly & Associates
  6  *
  7  * The source code in this file can be freely used, adapted,
  8  * and redistributed in source or binary form, so long as an
  9  * acknowledgment appears in derived source files.  The citation
 10  * should list that the code comes from the book "Linux Device
 11  * Drivers" by Alessandro Rubini and Jonathan Corbet, published
 12  * by O'Reilly & Associates.   No warranty is attached;
 13  * we cannot take responsibility for errors or fitness for use.
 14  */
 15 
 16 #include <stdio.h>
 17 #include <stdlib.h>
 18 #include <string.h>
 19 #include <unistd.h>
 20 #include <signal.h>
 21 #include <fcntl.h>
 22 
 23 int gotdata=0;
 24 void sighandler(int signo)
 25 {
 26     if (signo==SIGIO)
 27         gotdata++;
 28     return;
 29 }
 30 
 31 char buffer[4096];
 32 
 33 int main(int argc, char **argv)
 34 {
 35     int count;
 36     struct sigaction action;
 37 
 38     memset(&action, 0, sizeof(action));
 39     action.sa_handler = sighandler;
 40     action.sa_flags = 0;
 41 
 42     sigaction(SIGIO, &action, NULL);
 43 
 44     fcntl(STDIN_FILENO, F_SETOWN, getpid());
 45     fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | FASYNC);
 46 
 47     while(1) {
 48         /* this only returns if a signal arrives */
 49         sleep(86400); /* one day */
 50         if (!gotdata)
 51             continue;
 52         count=read(0, buffer, 4096);
 53         /* buggy: if avail data is more than 4kbytes... */
 54         write(1,buffer,count);
 55         gotdata=0;
 56     }
 57 }
 58 
  This page was automatically generated by the LXR engine.