initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OSspecific / POSIX / signals / sigInt.C
blob1f3555ec8746d86f7fc06a09ed0823185c006969
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "error.H"
28 #include "sigInt.H"
29 #include "JobInfo.H"
30 #include "IOstreams.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 struct sigaction Foam::sigInt::oldAction_;
36 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
38 void Foam::sigInt::sigIntHandler(int)
40     // Reset old handling
41     if (sigaction(SIGINT, &oldAction_, NULL) < 0)
42     {
43         FatalErrorIn
44         (
45             "Foam::sigInt::sigIntHandler()"
46         )   << "Cannot reset SIGINT trapping"
47             << abort(FatalError);    
48     }
50     // Update jobInfo file
51     jobInfo.signalEnd();
53     // Throw signal (to old handler)
54     raise(SIGINT);
58 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
60 Foam::sigInt::sigInt()
62     oldAction_.sa_handler = NULL;
66 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
68 Foam::sigInt::~sigInt()
70     // Reset old handling
71     if (sigaction(SIGINT, &oldAction_, NULL) < 0)
72     {
73         FatalErrorIn
74         (
75             "Foam::sigInt::~sigInt()"
76         )   << "Cannot reset SIGINT trapping"
77             << abort(FatalError);    
78     }
82 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
84 void Foam::sigInt::set(const bool verbose)
86     if (oldAction_.sa_handler)
87     {
88         FatalErrorIn
89         (
90             "Foam::sigInt::set()"
91         )   << "Cannot call sigInt::set() more than once"
92             << abort(FatalError);
93     }
95     struct sigaction newAction;
96     newAction.sa_handler = sigIntHandler;
97     newAction.sa_flags = SA_NODEFER;
98     sigemptyset(&newAction.sa_mask);
99     if (sigaction(SIGINT, &newAction, &oldAction_) < 0)
100     {
101         FatalErrorIn
102         (
103             "Foam::sigInt::set()"
104         )   << "Cannot set SIGINT trapping"
105             << abort(FatalError);    
106     }
110 // ************************************************************************* //