E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gpidfile.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 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 m_committed(false)
33 {
34 }
35
36 G::PidFile::~PidFile()
37 {
38 if( valid() )
39 cleanup( SignalSafe() , m_path.str().c_str() ) ;
40 }
41
42 G::PidFile::PidFile( const Path & path ) :
43 m_path(path) ,
44 m_committed(false)
45 {
46 }
47
48 void G::PidFile::init( const Path & path )
49 {
50 m_path = path ;
51 }
52
53 void G::PidFile::create( const Path & pid_file )
54 {
55 if( pid_file != Path() )
56 {
57 G_DEBUG( "G::PidFile::create: \"" << pid_file << "\"" ) ;
58
59 std::ofstream file ;
60 {
61 //Process::Umask umask(Process::Umask::Readable) ; // let the caller do this now
62 file.open( pid_file.str().c_str() , std::ios_base::out | std::ios_base::trunc ) ;
63 }
64 Process::Id pid ;
65 file << pid.str() << std::endl ;
66 file.close() ;
67 if( file.fail() )
68 throw Error(std::string("cannot create file: ")+pid_file.str()) ;
69 Cleanup::add( cleanup , new_string_ignore_leak(pid_file.str())->c_str() ) ;
70 }
71 }
72
73 bool G::PidFile::mine( SignalSafe safe , const char * path )
74 {
75 // signal-safe, reentrant implementation...
76 Process::Id this_pid ;
77 Process::Id file_pid( safe , path ) ;
78 return this_pid == file_pid ;
79 }
80
81 void G::PidFile::cleanup( SignalSafe safe , const char * path )
82 {
83 // signal-safe, reentrant implementation...
84 try
85 {
86 Identity id = Root::start( safe ) ; // claim_root
87 if( path && *path && mine(safe,path) )
88 std::remove( path ) ;
89 Root::stop( safe , id ) ;
90 }
91 catch(...)
92 {
93 }
94 }
95
96 void G::PidFile::check()
97 {
98 if( valid() && ! m_path.isAbsolute() )
99 throw Error(std::string("must be an absolute path: ")+m_path.str()) ;
100 }
101
102 void G::PidFile::commit()
103 {
104 if( valid() )
105 {
106 create( m_path ) ;
107 m_committed = true ;
108 }
109 }
110
111 bool G::PidFile::committed() const
112 {
113 return m_committed ;
114 }
115
116 G::Path G::PidFile::path() const
117 {
118 return m_path ;
119 }
120
121 bool G::PidFile::valid() const
122 {
123 return m_path != Path() ;
124 }
125
126 std::string * G::PidFile::new_string_ignore_leak( const std::string & s )
127 {
128 return new std::string( s ) ; // (ignore leak)
129 }
130
131 /// \file gpidfile.cpp
G::PidFile::init
void init(const Path &pid_file_path)
Used after default construction to make the object active.
Definition: gpidfile.cpp:48
G::Path::str
std::string str() const
Returns the path string.
Definition: gpath.cpp:290
G::PidFile::check
void check()
Throws an exception if the path is not absolute.
Definition: gpidfile.cpp:96
G::SignalSafe
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:36
G::PidFile::path
Path path() const
Returns the path as supplied to the constructor or init().
Definition: gpidfile.cpp:116
G::Identity
A combination of user-id and group-id, with a very low-level interface to the get/set/e/uid/gid funct...
Definition: gidentity.h:42
G::PidFile::~PidFile
~PidFile()
Destructor. Calls cleanup() to delete the file.
Definition: gpidfile.cpp:36
G::Root::start
static Identity start(SignalSafe)
A signal-safe alternative to construction.
Definition: groot.cpp:80
groot.h
G::Root::stop
static void stop(SignalSafe, Identity)
A signal-safe alternative to destruction.
Definition: groot.cpp:86
G::PidFile::PidFile
PidFile()
Default constructor.
Definition: gpidfile.cpp:31
gcleanup.h
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:81
gpidfile.h
G::PidFile::committed
bool committed() const
Returns true if commit() has been called with a valid path().
Definition: gpidfile.cpp:111
gfile.h
G::PidFile::commit
void commit()
Creates the file and installs signal handlers to cleanup() the file on abnormal process termination...
Definition: gpidfile.cpp:102
gprocess.h
G::Path
A Path object represents a file system path.
Definition: gpath.h:72
G::Cleanup::add
static void add(void(*fn)(SignalSafe, const char *), const char *arg)
Adds the given handler to the list of handlers that are to be called when the process terminates abno...
Definition: gcleanup_unix.cpp:103
gdebug.h