Debian Codegraph @

src/SysV.cpp

raw
1 // For all support, instructions and copyright go to:
2 // http://e2guardian.org/
3 // Released under the GPL v2, with the OpenSSL exception described in the README file.
4
5 // INCLUDES
6
7 #ifdef HAVE_CONFIG_H
8 #include "e2config.h"
9 #endif
10 #include "SysV.hpp"
11
12 #include <cstdio>
13 #include <unistd.h>
14 #include <cstdlib>
15 #include <fcntl.h>
16 #include <csignal>
17 #include <climits>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <cerrno>
21
22 // GLOBALS
23
24 extern OptionContainer o;
25
26 // DECLARATIONS
27
28 // get the PID from the given file, or by looking for "e2guardian" process by name - returns -1 if process not running
29 pid_t getpid(std::string pidfile);
30 // read process number from file (straight file read, no is-it-running check)
31 pid_t getpidfromfile(std::string pidfile);
32 // get PID from processes matching this command name (UNIMPLEMENTED)
33 pid_t getpidfromcommand(const char *command);
34 // confirm pid corresponds to a currently running process
35 bool confirmname(pid_t p);
36
37 // IMPLEMENTATION
38
39 // grab the PID from the file & check it's running (returns -1 on failure)
40 // (also checks process names if file method fails, but this is unimplemented)
41 pid_t getpid(std::string pidfile)
42 {
43 pid_t p = getpidfromfile(pidfile);
44 if (p > 1) {
45 if (confirmname(p)) { // is that pid really E2 and running?
46 return p; // it is so return it
47 }
48 }
49 pid_t t = getpidfromcommand("e2guardian"); // pid file method failed
50 // so try a search from the
51 // command
52 return t; // if it failed t will be -1
53 }
54
55 // grab process number from file (no run check)
56 pid_t getpidfromfile(std::string pidfile)
57 {
58 int handle = open(pidfile.c_str(), O_RDONLY);
59 if (handle < 0) { // Unable to open the pid file.
60 return -1;
61 }
62 char pidbuff[32];
63 int rc = read(handle, pidbuff, sizeof(pidbuff) - 1);
64 if (rc < 1) { // pid file must be at least 1 byte long
65 close(handle);
66 return -1;
67 }
68 pidbuff[rc] = '\0';
69 close(handle);
70 return atoi(pidbuff); // convert the string to a pid
71 }
72
73 // grab process number from processes matching the given command
74 pid_t getpidfromcommand(const char *command)
75 {
76 return -1; // ******** NOT IMPLEMENTED YET ************
77 // only needed if the pid file gets deleted.
78
79 //I KNOW HOW TO DO THIS IN A PORTABLE LINUX/BSD WAY
80 //BUT I HAVE NOT HAD THE TIME TO ADD IT YET AS THIS
81 //IS FUNCTIONAL ENOUGH TO WORK
82 }
83
84 // check the given PID is alive and running
85 bool confirmname(pid_t p)
86 {
87 int rc = ::kill(p, 0); // just a test
88 if (rc != 0) {
89 if (errno == EPERM) {
90 return true; // we got no perms to test it but it must be there
91 }
92 return false; // no process running at that pid
93 }
94 // ******** NOT FULLY IMPLEMENTED YET ************
95
96 //I KNOW HOW TO DO THIS IN A PORTABLE LINUX/BSD WAY
97 //BUT I HAVE NOT HAD THE TIME TO ADD IT YET AS THIS
98 //IS FUNCTIONAL ENOUGH TO WORK
99
100 return true;
101 }
102
103 // kill process in the pidfile, optionally deleting the pidfile & URL cache/logger IPC sockets
104 int sysv_kill(std::string pidfile, bool dounlink)
105 {
106 pid_t p = getpid(pidfile);
107 if (p > 1) {
108 int rc = ::kill(p, SIGTERM);
109 if (rc == -1) {
110 std::cerr << "Error trying to kill pid:" << p << std::endl;
111 if (errno == EPERM) {
112 std::cerr << "Permission denied." << std::endl;
113 }
114 return 1;
115 }
116 if (dounlink) {
117 unlink(pidfile.c_str());
118 }
119 return 0;
120 }
121 std::cerr << "No e2guardian process found." << std::endl;
122 return 1;
123 }
124
125 // send HUP to process
126 int sysv_hup(std::string pidfile)
127 {
128 pid_t p = getpid(pidfile);
129 if (p > 1) {
130 int rc = ::kill(p, SIGHUP);
131 if (rc == -1) {
132 std::cerr << "Error trying to hup pid:" << p << std::endl;
133 if (errno == EPERM) {
134 std::cerr << "Permission denied." << std::endl;
135 }
136 return 1;
137 }
138 return 0;
139 }
140 std::cerr << "No e2guardian process found." << std::endl;
141 return 1;
142 }
143
144 // send USR1 to process
145 int sysv_usr1(std::string pidfile)
146 {
147 pid_t p = getpid(pidfile);
148 if (p > 1) {
149 int rc = ::kill(p, SIGUSR1);
150 if (rc == -1) {
151 std::cerr << "Error trying to sig1 pid:" << p << std::endl;
152 if (errno == EPERM) {
153 std::cerr << "Permission denied." << std::endl;
154 }
155 return 1;
156 }
157 return 0;
158 }
159 std::cerr << "No e2guardian process found." << std::endl;
160 return 1;
161 }
162
163 // show PID of running E2 process
164 int sysv_showpid(std::string pidfile)
165 {
166 pid_t p = getpid(pidfile);
167 if (p > 1) {
168 std::cout << "Parent e2guardian pid:" << p << std::endl;
169 return 0;
170 }
171 std::cerr << "No e2guardian process found." << std::endl;
172 return 1;
173 }
174
175 // create a new pidfile
176 int sysv_openpidfile(std::string pidfile)
177 {
178 unlink(pidfile.c_str());
179 return open(pidfile.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
180 }
181
182 // write pid to file & close it
183 int sysv_writepidfile(int pidfilefd, pid_t pid = 0)
184 {
185 if(pid == 0) pid = getpid();
186 char pidbuff[32];
187 sprintf(pidbuff, "%d", (int)pid); // Messy, but it works!
188 int len = strlen(pidbuff) + 1;
189 pidbuff[len - 1] = '\n';
190 int rc = write(pidfilefd, pidbuff, len);
191 if (rc < len) { // failed to write
192 close(pidfilefd);
193 return 1;
194 }
195 close(pidfilefd);
196 return 0;
197 }
198
199 // check process in pidfile is running
200 bool sysv_amirunning(std::string pidfile)
201 {
202 if (getpid(pidfile) > 1) {
203 return true;
204 }
205 return false;
206 }