sig.c
Go to the documentation of this file.
1 /*
2  * $Id: sig.c,v 1.6 2003/12/01 09:10:16 troth Exp $
3  *
4  ****************************************************************************
5  *
6  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
7  * Copyright (C) 2001, 2002, 2003 Theodore A. Roth
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  ****************************************************************************
24  */
25 
26 /**
27  * \file sig.c
28  * \brief Public interface to signal handlers.
29  *
30  * This module provides a way for the simulator to process signals generated
31  * by the native host system. Note that these signals in this context have
32  * nothing to do with signals or interrupts as far as a program running in the
33  * simulator is concerned. */
34 
35 #include <config.h>
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <signal.h>
42 
43 #include "avrerror.h"
44 #include "sig.h"
45 
46 static volatile int global_got_sigint = 0;
47 
48 /*
49  * Private.
50  *
51  * Handler for SIGINT signals.
52  */
53 static void
54 signal_handle_sigint (int signo)
55 {
56  global_got_sigint = 1;
57 }
58 
59 /**
60  * \brief Start watching for the occurrance of the given signal.
61  *
62  * This function will install a signal handler which will set a flag when the
63  * signal occurs. Once the watch has been started, periodically call
64  * signal_has_occurred() to check if the signal was raised.
65  */
66 void
67 signal_watch_start (int signo)
68 {
69  struct sigaction act, oact;
70 
71  sigemptyset (&act.sa_mask);
72  act.sa_flags = 0;
73 
74  switch (signo)
75  {
76  case SIGINT:
77  global_got_sigint = 0;
78  act.sa_handler = signal_handle_sigint;
79  break;
80  default:
81  avr_warning ("Invalid signal: %d\n", signo);
82  return;
83  }
84 
85  if (sigaction (signo, &act, &oact) < 0)
86  avr_warning ("Failed to install signal handler: sig=%d: %s\n", signo,
87  strerror (errno));
88 }
89 
90 /**
91  * \brief Stop watching signal.
92  *
93  * Restores the default signal handler for the given signal and resets the
94  * signal flag.
95  */
96 void
97 signal_watch_stop (int signo)
98 {
99  struct sigaction act, oact;
100 
101  sigemptyset (&act.sa_mask);
102  act.sa_flags = 0;
103  act.sa_handler = SIG_DFL;
104 
105  signal_reset (signo);
106 
107  if (sigaction (signo, &act, &oact) < 0)
108  avr_warning ("Failed to restore default signal handler: sig=%d: %s\n",
109  signo, strerror (errno));
110 }
111 
112 /**
113  * \brief Check to see if a signal has occurred.
114  *
115  * \return Non-zero if signal has occurred. The flag will always be reset
116  * automatically.
117  */
118 int
120 {
121  int res = 0;
122 
123  switch (signo)
124  {
125  case SIGINT:
126  res = global_got_sigint;
127  global_got_sigint = 0;
128  break;
129  default:
130  avr_warning ("Invalid signal: %d", signo);
131  }
132 
133  return res;
134 }
135 
136 /**
137  * \brief Clear the flag which indicates that a signal has ocurred.
138  *
139  * Use signal_reset to manually reset (i.e. clear) the flag.
140  */
141 void
142 signal_reset (int signo)
143 {
144  signal_has_occurred (signo);
145 }

Automatically generated by Doxygen 1.8.2 on Tue Feb 26 2013.