Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / OSspecific / POSIX / signals / sigInt.C
blob16cdfb464c7077bea0ddc7be71fba82bc12ff315
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "error.H"
27 #include "sigInt.H"
28 #include "JobInfo.H"
29 #include "IOstreams.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 struct sigaction Foam::sigInt::oldAction_;
35 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
37 void Foam::sigInt::sigIntHandler(int)
39     // Reset old handling
40     if (sigaction(SIGINT, &oldAction_, NULL) < 0)
41     {
42         FatalErrorIn
43         (
44             "Foam::sigInt::sigIntHandler()"
45         )   << "Cannot reset SIGINT trapping"
46             << abort(FatalError);
47     }
49     // Update jobInfo file
50     jobInfo.signalEnd();
52     // Throw signal (to old handler)
53     raise(SIGINT);
57 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
59 Foam::sigInt::sigInt()
61     oldAction_.sa_handler = NULL;
65 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
67 Foam::sigInt::~sigInt()
69     // Reset old handling
70     if (sigaction(SIGINT, &oldAction_, NULL) < 0)
71     {
72         FatalErrorIn
73         (
74             "Foam::sigInt::~sigInt()"
75         )   << "Cannot reset SIGINT trapping"
76             << abort(FatalError);
77     }
81 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
83 void Foam::sigInt::set(const bool verbose)
85     if (oldAction_.sa_handler)
86     {
87         FatalErrorIn
88         (
89             "Foam::sigInt::set()"
90         )   << "Cannot call sigInt::set() more than once"
91             << abort(FatalError);
92     }
94     struct sigaction newAction;
95     newAction.sa_handler = sigIntHandler;
96     newAction.sa_flags = SA_NODEFER;
97     sigemptyset(&newAction.sa_mask);
98     if (sigaction(SIGINT, &newAction, &oldAction_) < 0)
99     {
100         FatalErrorIn
101         (
102             "Foam::sigInt::set()"
103         )   << "Cannot set SIGINT trapping"
104             << abort(FatalError);
105     }
109 // ************************************************************************* //