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 //-----------------------------------------------------------------------------
182 pid_t childPid = fork();
204 //-----------------------------------------------------------------------------
205 int main(int argc, char * argv[])
207 CONFIGFILE * cfg = NULL;
208 LISTENER * listener = NULL;
213 std::string logFileName;
215 std::string password;
216 std::string onConnect;
217 std::string onDisconnect;
223 printf("You must be root. Exit.\n");
228 cfg = new CONFIGFILE(argv[1]);
230 cfg = new CONFIGFILE("/etc/rscriptd/rscriptd.conf");
234 auto & WriteServLog = STG::Logger::get();
235 WriteServLog.setFileName("/var/log/rscriptd.log");
236 WriteServLog("Error reading config file!");
241 cfg->ReadString("LogFileName", &logFileName, "/var/log/rscriptd.log");
242 cfg->ReadInt("ExecutersNum", &execNum, 1);
243 cfg->ReadInt("ExecMsgKey", &execMsgKey, 5555);
244 cfg->ReadString("ConfigDir", &confDir, "/etc/rscriptd");
245 cfg->ReadString("Password", &password, "");
246 cfg->ReadInt("Port", &port, 5555);
247 cfg->ReadInt("UserTimeout", &userTimeout, 60);
248 cfg->ReadString("ScriptOnConnect", &onConnect, "/etc/rscriptd/OnConnect");
249 cfg->ReadString("ScriptOnDisconnect", &onDisconnect, "/etc/rscriptd/OnDisconnect");
251 if (ForkAndWait() < 0)
253 auto & WriteServLog = STG::Logger::get();
254 WriteServLog("Fork error!");
259 auto & WriteServLog = STG::Logger::get();
260 PIDFile pidFile("/var/run/rscriptd.pid");
261 WriteServLog.setFileName(logFileName);
262 WriteServLog("rscriptd v. %s", SERVER_VERSION);
264 for (int i = 0; i < execNum; i++)
266 int ret = StartScriptExecuter(argv[0], execMsgKey, &msgID);
269 STG::Logger::get()("Start Script Executer error!");
280 listener = new LISTENER();
281 listener->SetPort(port);
282 listener->SetPassword(password);
283 listener->SetUserTimeout(userTimeout);
284 listener->SetScriptOnConnect(onConnect);
285 listener->SetScriptOnDisconnect(onDisconnect);
289 WriteServLog("rscriptd started successfully.");
290 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
293 sigfillset(&signalSet);
294 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
298 sigfillset(&signalSet);
300 printfd(__FILE__, "Before sigwait\n");
301 sigwait(&signalSet, &sig);
302 printfd(__FILE__, "After sigwait. Signal: %d\n", sig);
313 WriteServLog("Ignore signel %d", sig);
322 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
324 StopScriptExecuter(msgID);
326 WriteServLog("rscriptd stopped successfully.");
327 WriteServLog("---------------------------------------------");
333 //-----------------------------------------------------------------------------