/
Unique.cpp
144 lines (130 loc) · 3.4 KB
/
Unique.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <signal.h>
#include <sys/sem.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include "Unique.hpp"
#include "config.h"
Unique::Unique(const char *const path)
{
char pathname[PATH_MAX + 1];
if (path) {
getPathnameFromFile(path, *&pathname);
} else {
getPathnameFromExe(*&pathname);
}
// Get unique key for semaphore.
semaphore_key = ftok(pathname, 1);
if (semaphore_key == -1) {
throw new UniqueException("ftok failed");
}
}
Unique::~Unique()
{
}
bool Unique::isRunning()
{
int semid = semget(semaphore_key, 0, 0);
if (semid == -1) {
return false;
}
return true;
}
void Unique::start()
{
if (!isRunning()) {
// Create semaphore; fail if already exists.
int semid = semget(semaphore_key, 1, IPC_CREAT | IPC_EXCL | 0660);
if (semid == -1) {
throw UniqueException("semget failed while creating semaphore");
}
// Perform an operation on semaphore so that semctl(GETPID) returns
// current PID.
struct sembuf ops = {0, 1, 0};
if (semop(semid, &ops, 1) == -1) {
throw UniqueException("semop failed");
}
}
}
void Unique::signal(int signal_id)
{
kill(signal_id);
}
void Unique::kill()
{
kill(SIGTERM);
}
void Unique::kill(int signal_id)
{
if (isRunning()) {
// Get semaphore; fail if not present.
int semid = semget(semaphore_key, 1, 0660);
if (semid == -1) {
throw UniqueException("semget failed while trying to kill");
}
// Get the pid from semaphore value (stored before) to kill the process.
int pid = semctl(semid, 0, GETPID, 0);
if (pid <= 0) {
throw UniqueException("semctl(GETPID) failed");
}
if (::kill(pid, signal_id)) {
throw UniqueException("kill failed");
}
}
}
void Unique::stop()
{
// Clean up semaphore, if exists.
int semid = semget(semaphore_key, 1, 0660);
if (semid != -1) {
semctl(semid, 0, IPC_RMID, 0);
}
}
void Unique::getPathnameFromFile(const char *const path, char* pathname)
{
#if HAVE_STRLCPY
strlcpy(pathname, path, PATH_MAX + 1);
#else
strncpy(pathname, path, PATH_MAX);
pathname[PATH_MAX] = '\0';
#endif
}
void Unique::getPathnameFromExe(char* pathname)
{
#if HAVE_GETEXECNAME && HAVE_STRLCPY
// Try getexecname (Solaris).
const char* execname = getexecname();
strlcpy(pathname, execname, PATH_MAX + 1);
#elif HAVE_SYSCTL && defined(CTL_KERN) && defined(KERN_PROC) && defined(KERN_PROC_PATHNAME)
// Try sysctl call (FreeBSD).
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
size_t cb = PATH_MAX;
sysctl(mib, 4, pathname, &cb, NULL, 0);
#else
// Try reading procfs links.
const char *const paths[] = {
"/proc/self/exe", // Linux
"/proc/curproc/exe", // NetBSD
"/proc/curproc/file", // FreeBSD with procfs
NULL
};
for (const char *const *path = paths; *path; path++) {
pathname[0] = 0;
int bytes = readlink(*path, pathname, sizeof(*pathname) * PATH_MAX);
if (bytes > 0) {
if (bytes > PATH_MAX - 1) {
bytes = PATH_MAX;
}
pathname[bytes] = '\0';
break;
}
}
#endif
}