"Fossies" - the Fresh Open Source Software Archive

Member "hatools-2.14/printsignal.c" (16 Mar 2010, 2781 Bytes) of package /linux/privat/old/hatools-2.14.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "printsignal.c" see the Fossies "Dox" file reference documentation.

    1 /*********************************************************************
    2 hatools (c) 2001,2003,2005-2010 by Markus Winand <mws@fatalmind.com>
    3 
    4 This program is free software; you can redistribute it and/or modify
    5 it under the terms of the GNU General Public License as published by
    6 the Free Software Foundation; either version 2 of the License, or
    7 (at your option) any later version.
    8 
    9 This program is distributed in the hope that it will be useful,
   10 but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12 GNU General Public License for more details.
   13 
   14 You should have received a copy of the GNU General Public License
   15 along with this program; if not, write to the Free Software
   16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   17 *********************************************************************/
   18 
   19 #include <errno.h>
   20 #include <signal.h>
   21 #include <stdio.h>
   22 #include <stdlib.h>
   23 #include <unistd.h>
   24 #include <sys/time.h>
   25 #include <time.h>
   26 
   27 void mygettimeofday(struct timeval *tv) {
   28     if (gettimeofday(tv, NULL)) {
   29         printf("gettimeofday: errno: %d\n", errno);
   30         abort();
   31     }
   32 }
   33 
   34 struct timeval starttime;
   35 
   36 void printsignal_sighand(int no) {
   37     /* directly calling an "unsafe" fuction, however, i know */
   38     /* this process isn't in any library function except sleep */
   39     /* right now... */
   40     
   41     struct timeval nowtime;
   42         double diff;
   43     
   44     mygettimeofday(&nowtime);   
   45     diff  = nowtime.tv_sec + (((double)nowtime.tv_usec)/1000000);
   46     diff -= starttime.tv_sec + (((double)starttime.tv_usec)/1000000);
   47 
   48     printf("Caught signal %d after %8.0f (%8.3f) seconds\n", no, diff, diff);
   49 }
   50 
   51 
   52 int main(int argc, char *argv[]) {
   53     struct sigaction alrt;
   54     int hlp;
   55     char dummy;
   56     struct timespec slp;
   57     
   58     if (argc != 2) {
   59         printf("you need to specify the number of seconds to sleep\n");
   60         exit(1);
   61     }
   62 
   63     if (sscanf(argv[1], "%d%c", &hlp, &dummy) != 1) {
   64         printf("Specified argument is not numeric\n");
   65         exit(1);
   66     }
   67     if (hlp <= 0) {
   68         printf("Specified argument has to be a positive integer\n");
   69         exit(1);
   70     }
   71     
   72     mygettimeofday(&starttime);
   73 
   74     printf("This program is intended for testing hatimerun.\n");
   75     printf("Waiting %d seconds and printing all signals received\n", hlp);
   76 
   77     alrt.sa_handler = printsignal_sighand;
   78     sigemptyset(&alrt.sa_mask);
   79     alrt.sa_flags = 0;
   80 
   81     sigaction(SIGTERM, &alrt, NULL);
   82     sigaction(SIGHUP,  &alrt, NULL);
   83     sigaction(SIGINT,  &alrt, NULL);
   84     sigaction(SIGPIPE, &alrt, NULL);
   85     sigaction(SIGQUIT, &alrt, NULL);
   86     sigaction(SIGUSR1, &alrt, NULL);
   87     sigaction(SIGUSR2, &alrt, NULL);
   88     sigaction(SIGALRM, &alrt, NULL);
   89 
   90     /* This is a very strange program, it is not intended to exit... */
   91     slp.tv_sec  = hlp;
   92     slp.tv_nsec = 0;
   93     while (nanosleep(&slp, &slp) == -1 && errno == EINTR) {
   94     }
   95     return 0;
   96 }