]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/main.cc
added signal handling
[mikachu/openbox.git] / util / epist / main.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // main.cc for Epistory - a key handler for NETWM/EWMH window managers.
3 // Copyright (c) 2002 - 2002 Ben Jansens <ben at orodu.net>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22
23 #ifdef    HAVE_CONFIG_H
24 #  include "../../config.h"
25 #endif // HAVE_CONFIG_H
26
27 extern "C" {
28 #ifdef    HAVE_UNISTD_H
29 #  include <sys/types.h>
30 #  include <unistd.h>
31 #endif // HAVE_UNISTD_H
32
33 #ifdef    HAVE_SIGNAL_H
34 #  include <signal.h>
35 #endif // HAVE_SIGNAL_H
36
37 #ifdef    HAVE_SYS_SIGNAL_H
38 #  include <sys/signal.h>
39 #endif // HAVE_SYS_SIGNAL_H
40
41 #ifdef    HAVE_LIBGEN_H
42 #  include <libgen.h>
43 #endif // HAVE_LIBGEN_H
44 }
45
46 #include <iostream>
47
48 using std::cout;
49 using std::endl;
50
51 bool _shutdown = false;
52 char **_argv;
53
54 #ifdef   HAVE_SIGACTION
55 static void signalhandler(int sig)
56 #else //  HAVE_SIGACTION
57 static RETSIGTYPE signalhandler(int sig)
58 #endif // HAVE_SIGACTION
59 {
60   switch (sig) {
61   case SIGSEGV:
62     cout << "Segmentation fault. Aborting and dumping core.\n";
63     abort();
64   case SIGHUP:
65     cout << "Restarting on request.\n";
66     execvp(_argv[0], _argv);
67     execvp(basename(_argv[0]), _argv);
68   }
69   _shutdown = true;
70
71 #ifndef   HAVE_SIGACTION
72   // assume broken, braindead sysv signal semantics
73   signal(sig, (RETSIGTYPE (*)(int)) signalhandler);
74 #endif // HAVE_SIGACTION
75 }
76
77 int main(int, char **argv) {
78   _argv = argv;
79
80 #ifdef    HAVE_SIGACTION
81   struct sigaction action;
82
83   action.sa_handler = signalhandler;
84   action.sa_mask = sigset_t();
85   action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
86
87   sigaction(SIGPIPE, &action, NULL);
88   sigaction(SIGSEGV, &action, NULL);
89   sigaction(SIGFPE, &action, NULL);
90   sigaction(SIGTERM, &action, NULL);
91   sigaction(SIGINT, &action, NULL);
92   sigaction(SIGHUP, &action, NULL);
93 #else // !HAVE_SIGACTION
94   signal(SIGPIPE, (RETSIGTYPE (*)(int)) signalhandler);
95   signal(SIGSEGV, (RETSIGTYPE (*)(int)) signalhandler);
96   signal(SIGFPE, (RETSIGTYPE (*)(int)) signalhandler);
97   signal(SIGTERM, (RETSIGTYPE (*)(int)) signalhandler);
98   signal(SIGINT, (RETSIGTYPE (*)(int)) signalhandler);
99   signal(SIGHUP, (RETSIGTYPE (*)(int)) signalhandler);
100 #endif // HAVE_SIGACTION
101   
102   while (! _shutdown) {
103     usleep(500);
104   }
105   return 0;
106 }