Line data Source code
1 : // Snap Websites Server -- manage a PID file for a snap service
2 : // Copyright (c) 2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // https://snapwebsites.org/
5 : // contact@m2osw.com
6 : //
7 : // This program is free software; you can redistribute it and/or modify
8 : // it under the terms of the GNU General Public License as published by
9 : // the Free Software Foundation; either version 2 of the License, or
10 : // (at your option) any later version.
11 : //
12 : // This program is distributed in the hope that it will be useful,
13 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : // GNU General Public License for more details.
16 : //
17 : // You should have received a copy of the GNU General Public License
18 : // along with this program; if not, write to the Free Software
19 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 : #pragma once
21 :
22 :
23 : // self
24 : //
25 : #include "snapwebsites/snap_exception.h"
26 :
27 :
28 : // snapdev lib
29 : //
30 : #include <snapdev/raii_generic_deleter.h>
31 :
32 :
33 :
34 : /** \file
35 : * \brief Declaration of the snap_pid class to manage a PID file.
36 : *
37 : * This header defines the snap_pid class which we use to manage a PID
38 : * file. This is quite practical to make sure only one instance of a
39 : * service is running, it also allows us to let systemd know once our
40 : * child process is ready and the parent is about to quit (i.e. when
41 : * we use detached processes.)
42 : *
43 : * See the .cpp file for more details.
44 : */
45 :
46 : namespace snap
47 : {
48 :
49 :
50 :
51 0 : class snap_pid_exception : public snap_exception
52 : {
53 : public:
54 : explicit snap_pid_exception(char const * whatmsg) : snap_exception("snap_pid", whatmsg) {}
55 0 : explicit snap_pid_exception(std::string const & whatmsg) : snap_exception("snap_pid", whatmsg) {}
56 : explicit snap_pid_exception(QString const & whatmsg) : snap_exception("snap_pid", whatmsg) {}
57 : };
58 :
59 0 : class snap_pid_exception_io_error : public snap_pid_exception
60 : {
61 : public:
62 : explicit snap_pid_exception_io_error(char const * whatmsg) : snap_pid_exception(whatmsg) {}
63 0 : explicit snap_pid_exception_io_error(std::string const & whatmsg) : snap_pid_exception(whatmsg) {}
64 : explicit snap_pid_exception_io_error(QString const & whatmsg) : snap_pid_exception(whatmsg) {}
65 : };
66 :
67 0 : class snap_pid_exception_invalid_parameter : public snap_pid_exception
68 : {
69 : public:
70 : explicit snap_pid_exception_invalid_parameter(char const * whatmsg) : snap_pid_exception(whatmsg) {}
71 0 : explicit snap_pid_exception_invalid_parameter(std::string const & whatmsg) : snap_pid_exception(whatmsg) {}
72 : explicit snap_pid_exception_invalid_parameter(QString const & whatmsg) : snap_pid_exception(whatmsg) {}
73 : };
74 :
75 :
76 :
77 :
78 :
79 :
80 : class snap_pid
81 : {
82 : public:
83 : typedef std::shared_ptr<snap_pid> pointer_t;
84 :
85 : snap_pid(std::string const & service_name);
86 : ~snap_pid();
87 :
88 : void create_pid_file();
89 : bool wait_signal();
90 :
91 : private:
92 : void generate_filename(std::string const & service_name);
93 : void unlink_pid_file();
94 : void close_pipes();
95 : void send_signal(bool result);
96 :
97 : std::string f_service_name = std::string();
98 : int f_pipes[2];
99 : std::string f_pid_filename = std::string();
100 : raii_fd_t f_safe_fd = raii_fd_t();
101 : bool f_child_process = false;
102 : char f_result = static_cast<char>(false);
103 : };
104 :
105 : } // namespace snap
106 : // vim: ts=4 sw=4 et
|