Merge branch 'upstream/OpenFOAM' into master
[freefoam.git] / src / OSspecific / POSIX / signals / sigSegvImpl.C
blobb2f1cf003a90ab45a3f9f91a692ce8fb61d7d575
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 <OpenFOAM/error.H>
28 #include "sigSegvImpl.H"
29 #include <OpenFOAM/JobInfo.H>
30 #include <OpenFOAM/IOstreams.H>
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 struct sigaction Foam::sigSegvImpl::oldAction_;
36 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
38 void Foam::sigSegvImpl::sigSegvHandler(int)
40     // Reset old handling
41     if (sigaction(SIGSEGV, &oldAction_, NULL) < 0)
42     {
43         FatalErrorIn
44         (
45             "Foam::sigSegvImpl::sigSegvHandler()"
46         )   << "Cannot reset SIGSEGV trapping"
47             << abort(FatalError);
48     }
50     // Update jobInfo file
51     jobInfo.signalEnd();
53     Perr << "\n"
54             "************************************************************\n"
55             "* FreeFOAM  crashed. See  below  for a  backtrace to  help *\n"
56             "* locating the problem.                                    *\n"
57             "************************************************************\n";
58     error::printStack(Perr);
59     Perr << "************************************************************\n"
60             "\n";
62     // Throw signal (to old handler)
63     raise(SIGSEGV);
67 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
69 Foam::sigSegvImpl::sigSegvImpl()
71     oldAction_.sa_handler = NULL;
75 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
77 Foam::sigSegvImpl::~sigSegvImpl()
79     // Reset old handling
80     if (sigaction(SIGSEGV, &oldAction_, NULL) < 0)
81     {
82         FatalErrorIn
83         (
84             "Foam::sigSegvImpl::~sigSegvImpl()"
85         )   << "Cannot reset SIGSEGV trapping"
86             << abort(FatalError);
87     }
91 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
93 void Foam::sigSegvImpl::set(const bool verbose)
95     if (oldAction_.sa_handler)
96     {
97         FatalErrorIn
98         (
99             "Foam::sigSegvImpl::set()"
100         )   << "Cannot call sigSegvImpl::set() more than once"
101             << abort(FatalError);
102     }
104     struct sigaction newAction;
105     newAction.sa_handler = sigSegvHandler;
106     newAction.sa_flags = SA_NODEFER;
107     sigemptyset(&newAction.sa_mask);
108     if (sigaction(SIGSEGV, &newAction, &oldAction_) < 0)
109     {
110         FatalErrorIn
111         (
112             "Foam::sigSegvImpl::set()"
113         )   << "Cannot set SIGSEGV trapping"
114             << abort(FatalError);
115     }
119 // ************************ vim: set sw=4 sts=4 et: ************************ //