-
Notifications
You must be signed in to change notification settings - Fork 29
/
SelfDetect.h
55 lines (47 loc) · 1.59 KB
/
SelfDetect.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* Copyright 2013, 2014 Range Networks, Inc.
* All Rights Reserved.
*
* This software is distributed under multiple licenses;
* see the COPYING file in the main directory for licensing
* information for this specific distribution.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef SELFDETECT_H
#define SELFDETECT_H
#include <list>
#include <string>
#include <cstdlib>
#include <cstdio>
/** A C++ wrapper for preventing 2 instances of a program from running. */
class SelfDetect
{
private:
const char *mProg; // program name, which will map to /tmp/%s.pid
const char *mFile; // file in /tmp for pid
std::list<std::string> mListFiles; // list of files to be removed at shutdown
public:
SelfDetect(void)
{
mProg = NULL;
mFile = NULL;
mListFiles.clear();
}
~SelfDetect(void)
{
if (mProg) { free((void *)mProg); mProg = NULL; }
if (mFile) { free((void *)mFile); mFile = NULL; }
mListFiles.clear();
}
void RegisterProgram(const char *argv0); // register the program and validate
void RegisterFile(const char *file); // register a file to be removed at shutdown / exit
void Exit(int); // atexit() registration function -- called internally by real atexit() function that isn't in a class
};
extern SelfDetect gSelf;
#endif // SELFDETECT_H
// vim: ts=4 sw=4