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"
58 #define START_FILE "/._ST_ART_ED_"
60 set<pid_t> executersPid;
61 volatile time_t stgTime = time(NULL);
63 //-----------------------------------------------------------------------------
66 set<pid_t>::iterator pid;
67 pid = executersPid.begin();
68 while (pid != executersPid.end())
70 printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
75 //-----------------------------------------------------------------------------
76 int StartScriptExecuter(char * procName, int msgKey, int * msgID)
78 STG_LOGGER & WriteServLog = GetStgLogger();
80 if (*msgID == -11) // If msgID == -11 - first call. Create queue
82 for (int i = 0; i < 2; i++)
84 *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
88 *msgID = msgget(msgKey, 0);
91 WriteServLog("Message queue not created.");
96 msgctl(*msgID, IPC_RMID, NULL);
101 WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
107 pid_t executerPid = fork();
112 WriteServLog("Fork error!");
121 Executer(*msgID, executerPid, procName);
123 Executer(*msgID, executerPid);
128 if (executersPid.empty())
130 Executer(*msgID, executerPid, NULL);
132 Executer(*msgID, executerPid);
134 executersPid.insert(executerPid);
138 //-----------------------------------------------------------------------------
139 void StopScriptExecuter(int msgID)
141 STG_LOGGER & WriteServLog = GetStgLogger();
143 for (int i = 0; i < 5; ++i)
145 struct msqid_ds data;
146 if (msgctl(msgID, IPC_STAT, &data))
149 printfd(__FILE__, "StopScriptExecuter() - msgctl for IPC_STAT failed: '%s'\n", strerror(e));
150 WriteServLog( "Failed to check queue emptiness: '%s'", strerror(e));
154 WriteServLog("Messages in queue: %d", data.msg_qnum);
156 if (data.msg_qnum == 0)
159 struct timespec ts = {1, 0};
160 nanosleep(&ts, NULL);
163 if (msgctl(msgID, IPC_RMID, NULL))
166 printfd(__FILE__, "StopScriptExecuter() - msgctl for IPC_STAT failed: '%s'\n", strerror(e));
167 WriteServLog("Failed to remove queue: '%s'", strerror(e));
171 WriteServLog("Queue removed successfully.");
176 //-----------------------------------------------------------------------------
178 int ForkAndWait(const string &)
180 int ForkAndWait(const string & confDir)
184 pid_t childPid = fork();
206 //-----------------------------------------------------------------------------
207 int main(int argc, char * argv[])
209 CONFIGFILE * cfg = NULL;
210 LISTENER * listener = NULL;
225 printf("You must be root. Exit.\n");
230 cfg = new CONFIGFILE(argv[1]);
232 cfg = new CONFIGFILE("/etc/rscriptd/rscriptd.conf");
236 STG_LOGGER & WriteServLog = GetStgLogger();
237 WriteServLog.SetLogFileName("/var/log/rscriptd.log");
238 WriteServLog("Error reading config file!");
243 cfg->ReadString("LogFileName", &logFileName, "/var/log/rscriptd.log");
244 cfg->ReadInt("ExecutersNum", &execNum, 1);
245 cfg->ReadInt("ExecMsgKey", &execMsgKey, 5555);
246 cfg->ReadString("ConfigDir", &confDir, "/etc/rscriptd");
247 cfg->ReadString("Password", &password, "");
248 cfg->ReadInt("Port", &port, 5555);
249 cfg->ReadInt("UserTimeout", &userTimeout, 60);
250 cfg->ReadString("ScriptOnConnect", &onConnect, "/etc/rscriptd/OnConnect");
251 cfg->ReadString("ScriptOnDisconnect", &onDisconnect, "/etc/rscriptd/OnDisconnect");
253 if (ForkAndWait(confDir) < 0)
255 STG_LOGGER & WriteServLog = GetStgLogger();
256 WriteServLog("Fork error!");
261 STG_LOGGER & WriteServLog = GetStgLogger();
262 PIDFile pidFile("/var/run/rscriptd.pid");
263 WriteServLog.SetLogFileName(logFileName);
264 WriteServLog("rscriptd v. %s", SERVER_VERSION);
266 for (int i = 0; i < execNum; i++)
268 int ret = StartScriptExecuter(argv[0], execMsgKey, &msgID);
271 STG_LOGGER & WriteServLog = GetStgLogger();
272 WriteServLog("Start Script Executer error!");
283 listener = new LISTENER();
284 listener->SetPort(port);
285 listener->SetPassword(password);
286 listener->SetUserTimeout(userTimeout);
287 listener->SetScriptOnConnect(onConnect);
288 listener->SetScriptOnDisconnect(onDisconnect);
292 WriteServLog("rscriptd started successfully.");
293 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
296 sigfillset(&signalSet);
297 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
301 sigfillset(&signalSet);
303 printfd(__FILE__, "Before sigwait\n");
304 sigwait(&signalSet, &sig);
305 printfd(__FILE__, "After sigwait. Signal: %d\n", sig);
316 WriteServLog("Ignore signel %d", sig);
325 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
327 StopScriptExecuter(msgID);
329 WriteServLog("rscriptd stopped successfully.");
330 WriteServLog("---------------------------------------------");
336 //-----------------------------------------------------------------------------