2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
25 $Date: 2010/09/10 06:37:45 $
28 #include <sys/types.h>
33 #include <fcntl.h> // creat
40 #include <cstring> // strerror
43 #include "stg/common.h"
44 #include "stg/logger.h"
45 #include "stg/scriptexecuter.h"
46 #include "stg/conffiles.h"
47 #include "stg/version.h"
56 #define START_FILE "/._ST_ART_ED_"
58 std::set<pid_t> executersPid;
59 volatile time_t stgTime = time(NULL);
61 //-----------------------------------------------------------------------------
64 std::set<pid_t>::iterator pid;
65 pid = executersPid.begin();
66 while (pid != executersPid.end())
68 printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
73 //-----------------------------------------------------------------------------
74 #if defined(LINUX) || defined(DARWIN)
75 int StartScriptExecuter(char * procName, int msgKey, int * msgID)
77 int StartScriptExecuter(char *, int msgKey, int * msgID)
80 auto & WriteServLog = STG::Logger::get();
82 if (*msgID == -11) // If msgID == -11 - first call. Create queue
84 for (int i = 0; i < 2; i++)
86 *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
90 *msgID = msgget(msgKey, 0);
93 WriteServLog("Message queue not created.");
98 msgctl(*msgID, IPC_RMID, NULL);
103 WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
109 pid_t executerPid = fork();
114 WriteServLog("Fork error!");
122 #if defined(LINUX) || defined(DARWIN)
123 Executer(*msgID, executerPid, procName);
125 Executer(*msgID, executerPid);
130 if (executersPid.empty())
131 #if defined(LINUX) || defined(DARWIN)
132 Executer(*msgID, executerPid, NULL);
134 Executer(*msgID, executerPid);
136 executersPid.insert(executerPid);
140 //-----------------------------------------------------------------------------
141 void StopScriptExecuter(int msgID)
143 auto & WriteServLog = STG::Logger::get();
145 for (int i = 0; i < 5; ++i)
147 struct msqid_ds data;
148 if (msgctl(msgID, IPC_STAT, &data))
151 printfd(__FILE__, "StopScriptExecuter() - msgctl for IPC_STAT failed: '%s'\n", strerror(e));
152 WriteServLog( "Failed to check queue emptiness: '%s'", strerror(e));
156 WriteServLog("Messages in queue: %d", data.msg_qnum);
158 if (data.msg_qnum == 0)
161 struct timespec ts = {1, 0};
162 nanosleep(&ts, NULL);
165 if (msgctl(msgID, IPC_RMID, NULL))
168 printfd(__FILE__, "StopScriptExecuter() - msgctl for IPC_STAT failed: '%s'\n", strerror(e));
169 WriteServLog("Failed to remove queue: '%s'", strerror(e));
173 WriteServLog("Queue removed successfully.");
178 //-----------------------------------------------------------------------------
180 int ForkAndWait(const std::string &)
182 int ForkAndWait(const std::string & confDir)
186 pid_t childPid = fork();
208 //-----------------------------------------------------------------------------
209 int main(int argc, char * argv[])
211 CONFIGFILE * cfg = NULL;
212 LISTENER * listener = NULL;
217 std::string logFileName;
219 std::string password;
220 std::string onConnect;
221 std::string onDisconnect;
227 printf("You must be root. Exit.\n");
232 cfg = new CONFIGFILE(argv[1]);
234 cfg = new CONFIGFILE("/etc/rscriptd/rscriptd.conf");
238 auto & WriteServLog = STG::Logger::get();
239 WriteServLog.setFileName("/var/log/rscriptd.log");
240 WriteServLog("Error reading config file!");
245 cfg->ReadString("LogFileName", &logFileName, "/var/log/rscriptd.log");
246 cfg->ReadInt("ExecutersNum", &execNum, 1);
247 cfg->ReadInt("ExecMsgKey", &execMsgKey, 5555);
248 cfg->ReadString("ConfigDir", &confDir, "/etc/rscriptd");
249 cfg->ReadString("Password", &password, "");
250 cfg->ReadInt("Port", &port, 5555);
251 cfg->ReadInt("UserTimeout", &userTimeout, 60);
252 cfg->ReadString("ScriptOnConnect", &onConnect, "/etc/rscriptd/OnConnect");
253 cfg->ReadString("ScriptOnDisconnect", &onDisconnect, "/etc/rscriptd/OnDisconnect");
255 if (ForkAndWait(confDir) < 0)
257 auto & WriteServLog = STG::Logger::get();
258 WriteServLog("Fork error!");
263 auto & WriteServLog = STG::Logger::get();
264 PIDFile pidFile("/var/run/rscriptd.pid");
265 WriteServLog.setFileName(logFileName);
266 WriteServLog("rscriptd v. %s", SERVER_VERSION);
268 for (int i = 0; i < execNum; i++)
270 int ret = StartScriptExecuter(argv[0], execMsgKey, &msgID);
273 STG::Logger::get()("Start Script Executer error!");
284 listener = new LISTENER();
285 listener->SetPort(port);
286 listener->SetPassword(password);
287 listener->SetUserTimeout(userTimeout);
288 listener->SetScriptOnConnect(onConnect);
289 listener->SetScriptOnDisconnect(onDisconnect);
293 WriteServLog("rscriptd started successfully.");
294 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
297 sigfillset(&signalSet);
298 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
302 sigfillset(&signalSet);
304 printfd(__FILE__, "Before sigwait\n");
305 sigwait(&signalSet, &sig);
306 printfd(__FILE__, "After sigwait. Signal: %d\n", sig);
317 WriteServLog("Ignore signel %d", sig);
326 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
328 StopScriptExecuter(msgID);
330 WriteServLog("rscriptd stopped successfully.");
331 WriteServLog("---------------------------------------------");
337 //-----------------------------------------------------------------------------