- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gpidfile.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // gpidfile.cpp
19 //
20
21 #include "gdef.h"
22 #include "gpidfile.h"
23 #include "gprocess.h"
24 #include "groot.h"
25 #include "gcleanup.h"
26 #include "gfile.h"
27 #include "gdebug.h"
28 #include <fstream>
29 #include <string>
30
31 G::PidFile::PidFile()
32 {
33 }
34
35 G::PidFile::~PidFile()
36 {
37 if( valid() )
38 cleanup( SignalSafe() , m_path.str().c_str() ) ;
39 }
40
41 G::PidFile::PidFile( const Path & path ) :
42 m_path(path)
43 {
44 }
45
46 void G::PidFile::init( const Path & path )
47 {
48 m_path = path ;
49 }
50
51 void G::PidFile::create( const Path & pid_file )
52 {
53 if( pid_file != Path() )
54 {
55 G_DEBUG( "G::PidFile::create: \"" << pid_file << "\"" ) ;
56
57 Process::Umask umask(Process::Umask::Readable) ;
58 std::ofstream file( pid_file.str().c_str() ) ;
59 Process::Id pid ;
60 file << pid.str() << std::endl ;
61 if( !file.good() )
62 throw Error(std::string("cannot create file: ")+pid_file.str()) ;
63 Cleanup::add( cleanup , (new std::string(pid_file.str()))->c_str() ) ; // (leak)
64 }
65 }
66
67 bool G::PidFile::mine( SignalSafe safe , const char * path )
68 {
69 // signal-safe, reentrant implementation...
70 Process::Id this_pid ;
71 Process::Id file_pid( safe , path ) ;
72 return this_pid == file_pid ;
73 }
74
75 void G::PidFile::cleanup( SignalSafe safe , const char * path )
76 {
77 // signal-safe, reentrant implementation...
78 try
79 {
80 Identity id = Root::start( safe ) ;
81 if( path && *path && mine(safe,path) )
82 {
83 std::remove( path ) ;
84 }
85 Root::stop( safe , id ) ;
86 }
87 catch(...)
88 {
89 }
90 }
91
92 void G::PidFile::check()
93 {
94 if( valid() && ! m_path.isAbsolute() )
95 throw Error(std::string("must be an absolute path: ")+m_path.str()) ;
96 }
97
98 void G::PidFile::commit()
99 {
100 if( valid() )
101 create( m_path ) ;
102 }
103
104 G::Path G::PidFile::path() const
105 {
106 return m_path ;
107 }
108
109 bool G::PidFile::valid() const
110 {
111 return m_path != Path() ;
112 }
113
G::PidFile::init
void init(const Path &pid_file_path)
Used after default construction.
Definition: gpidfile.cpp:46
G::Path::str
std::string str() const
Returns the path string.
Definition: gpath.cpp:135
G::PidFile::check
void check()
Throws an exception if the path is not absolute.
Definition: gpidfile.cpp:92
G::SignalSafe
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:35
G::PidFile::path
Path path() const
Returns the path as supplied to the constructor or init().
Definition: gpidfile.cpp:104
G::Process::Id::str
std::string str() const
Definition: gprocess_unix.cpp:236
G::Identity
A very low-level interface to getpwnam() and the get/set/e/uid/gid functions.
Definition: gidentity.h:41
G::PidFile::~PidFile
~PidFile()
Destructor.
Definition: gpidfile.cpp:35
G::Root::start
static Identity start(SignalSafe)
A signal-safe alternative to construction.
Definition: groot.cpp:62
G::Process::Umask
Used to temporarily modify the process umask.
Definition: gprocess.h:64
groot.h
G::Root::stop
static void stop(SignalSafe, Identity)
A signal-safe alternative to destruction.
Definition: groot.cpp:68
G::PidFile::PidFile
PidFile()
Default constructor.
Definition: gpidfile.cpp:31
gcleanup.h
G::Process::Umask::Readable
Definition: gprocess.h:66
G::PidFile::cleanup
static void cleanup(SignalSafe, const char *path)
Deletes the specified pid file if it contains this process's id.
Definition: gpidfile.cpp:75
gpidfile.h
gfile.h
G::PidFile::commit
void commit()
Creates the file.
Definition: gpidfile.cpp:98
gprocess.h
G_DEBUG
#define G_DEBUG(expr)
Definition: glog.h:95
G::Process::Id
Process-id class.
Definition: gprocess.h:52
G::Path
A Path object represents a file system path.
Definition: gpath.h:44
G::Cleanup::add
static void add(void(*fn)(SignalSafe, const char *), const char *arg)
Adds the given handler to the list which are to be called when the process terminates abnormally...
Definition: gcleanup_unix.cpp:78
gdebug.h