VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gpidfile.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 
32 {
33 }
34 
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  std::ofstream file ;
58  {
59  //Process::Umask umask(Process::Umask::Readable) ; // let the caller do this now
60  file.open( pid_file.str().c_str() , std::ios_base::out | std::ios_base::trunc ) ;
61  }
62  Process::Id pid ;
63  file << pid.str() << std::endl ;
64  file.close() ;
65  if( file.fail() )
66  throw Error(std::string("cannot create file: ")+pid_file.str()) ;
67  Cleanup::add( cleanup , (new std::string(pid_file.str()))->c_str() ) ; // (leak)
68  }
69 }
70 
71 bool G::PidFile::mine( SignalSafe safe , const char * path )
72 {
73  // signal-safe, reentrant implementation...
74  Process::Id this_pid ;
75  Process::Id file_pid( safe , path ) ;
76  return this_pid == file_pid ;
77 }
78 
79 void G::PidFile::cleanup( SignalSafe safe , const char * path )
80 {
81  // signal-safe, reentrant implementation...
82  try
83  {
84  Identity id = Root::start( safe ) ; // claim_root
85  if( path && *path && mine(safe,path) )
86  std::remove( path ) ;
87  Root::stop( safe , id ) ;
88  }
89  catch(...)
90  {
91  }
92 }
93 
95 {
96  if( valid() && ! m_path.isAbsolute() )
97  throw Error(std::string("must be an absolute path: ")+m_path.str()) ;
98 }
99 
101 {
102  if( valid() )
103  create( m_path ) ;
104 }
105 
107 {
108  return m_path ;
109 }
110 
111 bool G::PidFile::valid() const
112 {
113  return m_path != Path() ;
114 }
115 
116 /// \file gpidfile.cpp
void init(const Path &pid_file_path)
Used after default construction to make the object active.
Definition: gpidfile.cpp:46
std::string str() const
Returns the path string.
Definition: gpath.cpp:290
void check()
Throws an exception if the path is not absolute.
Definition: gpidfile.cpp:94
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:36
Path path() const
Returns the path as supplied to the constructor or init().
Definition: gpidfile.cpp:106
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
~PidFile()
Destructor. Calls cleanup() to delete the file.
Definition: gpidfile.cpp:35
static Identity start(SignalSafe)
A signal-safe alternative to construction.
Definition: groot.cpp:80
static void stop(SignalSafe, Identity)
A signal-safe alternative to destruction.
Definition: groot.cpp:86
PidFile()
Default constructor.
Definition: gpidfile.cpp:31
static void cleanup(SignalSafe, const char *path)
Deletes the specified pid file if it contains this process's id.
Definition: gpidfile.cpp:79
void commit()
Creates the file and installs signal handlers to cleanup() the file on abnormal process termination...
Definition: gpidfile.cpp:100
A Path object represents a file system path.
Definition: gpath.h:72
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...