Whoops, I'm a bit late with my comments, but all the same...

You can't safely use C++ stream functions in a signal handler.

Why does terrasync need to be protected from interruption like this? Does
the SVN update really get corrupted if it is interrupted?

Tim

On Sun, Feb 28, 2010 at 6:50 AM, Alex Perry <alex.pe...@ieee.org> wrote:

> Index: terrasync.cxx
> ===================================================================
> RCS file: /var/cvs/FlightGear-0.9/source/utils/TerraSync/terrasync.cxx,v
> retrieving revision 1.28
> diff -u -r1.28 terrasync.cxx
> --- terrasync.cxx       23 Jan 2010 22:27:38 -0000      1.28
> +++ terrasync.cxx       28 Feb 2010 05:49:01 -0000
> @@ -35,6 +35,7 @@
>  #endif
>
>  #include <stdlib.h>             // atoi() atof() abs() system()
> +#include <signal.h>             // signal()
>
>  #include <simgear/compiler.h>
>
> @@ -270,8 +271,23 @@
>  }
>
>
> +bool terminating = false;
> +sighandler_t prior_signal_handlers[32];
> +int termination_triggering_signals[] =
> +  {SIGHUP, SIGINT, SIGQUIT, SIGKILL, 0};  // zero terminated
> +
> +void terminate_request_handler(int param) {
> +    cout << "\nReceived signal " << param << ", "
> +         << "intend to terminate soon, "
> +         << "repeat to force an immediate effect.\n";
> +    terminating = true;
> +    signal(param, prior_signal_handlers[param]);
> +}
> +
> +
>  const int nowhere = -9999;
>
> +
>  // parse message
>  static void parse_message( const string &msg, int *lat, int *lon ) {
>     double dlat, dlon;
> @@ -281,8 +297,8 @@
>     string::size_type pos = text.find( "$GPGGA" );
>     if ( pos == string::npos )
>     {
> -       *lat = -9999.0;
> -       *lon = -9999.0;
> +       *lat = nowhere;
> +       *lon = nowhere;
>        return;
>     }
>     string tmp = text.substr( pos + 7 );
> @@ -408,7 +424,8 @@
>
>  void getWaitingTile() {
>     while ( !waitingTiles.empty() ) {
> -       CompletedTiles::iterator ii = completedTiles.find(
> waitingTiles.front() );
> +       CompletedTiles::iterator ii =
> +            completedTiles.find( waitingTiles.front() );
>        time_t now = time(0);
>        if ( ii == completedTiles.end() || ii->second + 600 < now ) {
>            sync_tree(waitingTiles.front().c_str());
> @@ -422,7 +439,7 @@
>
>  int main( int argc, char **argv ) {
>     int port = 5501;
> -    char host[256] = "";        // accept messages from anyone
> +    char host[256] = "localhost";
>     bool testing = false;
>     bool do_checkout(true);
>     int verbose(0);
> @@ -485,7 +502,6 @@
>
>     // Must call this before any other net stuff
>     netInit( &argc,argv );
> -
>     netSocket s;
>
>     if ( ! s.open( false ) ) {  // open a UDP socket
> @@ -524,7 +540,14 @@
>     }
>
>
> -    while ( true ) {                    // main loop
> +    for (int* sigp=termination_triggering_signals; *sigp; sigp++) {
> +        prior_signal_handlers[*sigp] =
> +            signal(*sigp, *terminate_request_handler);
> +        if (verbose) cout << "Intercepted signal " << *sigp << endl;
> +    }
> +
> +    while ( !terminating ) {
> +        // main loop
>         recv_msg = false;
>         if ( testing ) {
>             // No FGFS communications
> @@ -532,6 +555,9 @@
>             lon = -123;
>             recv_msg = (lat != last_lat) || (lon != last_lon);
>         } else {
> +            if (verbose && waitingTiles.empty()) {
> +                cout << "Idle; waiting for FlightGear position\n";
> +            }
>             s.setBlocking(waitingTiles.empty());
>             len = s.recv(msg, maxlen, 0);
>             if (len >= 0) {
> @@ -584,11 +610,11 @@
>         }
>
>        else if ( testing ) {
> -           exit( 0 );
> +           terminating = true;
>        } else
>
>         ulSleep( 1 );
> -    } // while true
> +    } // while !terminating
>
>     return 0;
>  }
>
>
> ------------------------------------------------------------------------------
> Download Intel&#174; Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> _______________________________________________
> Flightgear-devel mailing list
> Flightgear-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/flightgear-devel
>
------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to