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 //-----------------------------------------------------------------------------
77 int StartScriptExecuter(char * procName, int msgKey, int * msgID)
79 int StartScriptExecuter(char *, int msgKey, int * msgID)
82 STG_LOGGER & WriteServLog = GetStgLogger();
84 if (*msgID == -11) // If msgID == -11 - first call. Create queue
86 for (int i = 0; i < 2; i++)
88 *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
92 *msgID = msgget(msgKey, 0);
95 WriteServLog("Message queue not created.");
100 msgctl(*msgID, IPC_RMID, NULL);
105 WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
111 pid_t executerPid = fork();
116 WriteServLog("Fork error!");
125 Executer(*msgID, executerPid, procName);
127 Executer(*msgID, executerPid);
132 if (executersPid.empty())
134 Executer(*msgID, executerPid, NULL);
136 Executer(*msgID, executerPid);
138 executersPid.insert(executerPid);
142 //-----------------------------------------------------------------------------
143 void StopScriptExecuter(int msgID)
145 STG_LOGGER & WriteServLog = GetStgLogger();
147 for (int i = 0; i < 5; ++i)
149 struct msqid_ds data;
150 if (msgctl(msgID, IPC_STAT, &data))
153 printfd(__FILE__, "StopScriptExecuter() - msgctl for IPC_STAT failed: '%s'\n", strerror(e));
154 WriteServLog( "Failed to check queue emptiness: '%s'", strerror(e));
158 WriteServLog("Messages in queue: %d", data.msg_qnum);
160 if (data.msg_qnum == 0)
163 struct timespec ts = {1, 0};
164 nanosleep(&ts, NULL);
167 if (msgctl(msgID, IPC_RMID, NULL))
170 printfd(__FILE__, "StopScriptExecuter() - msgctl for IPC_STAT failed: '%s'\n", strerror(e));
171 WriteServLog("Failed to remove queue: '%s'", strerror(e));
175 WriteServLog("Queue removed successfully.");
180 //-----------------------------------------------------------------------------
182 int ForkAndWait(const string &)
184 int ForkAndWait(const string & confDir)
188 pid_t childPid = fork();
210 //-----------------------------------------------------------------------------
211 int main(int argc, char * argv[])
213 CONFIGFILE * cfg = NULL;
214 LISTENER * listener = NULL;
229 printf("You must be root. Exit.\n");
234 cfg = new CONFIGFILE(argv[1]);
236 cfg = new CONFIGFILE("/etc/rscriptd/rscriptd.conf");
240 STG_LOGGER & WriteServLog = GetStgLogger();
241 WriteServLog.SetLogFileName("/var/log/rscriptd.log");
242 WriteServLog("Error reading config file!");
247 cfg->ReadString("LogFileName", &logFileName, "/var/log/rscriptd.log");
248 cfg->ReadInt("ExecutersNum", &execNum, 1);
249 cfg->ReadInt("ExecMsgKey", &execMsgKey, 5555);
250 cfg->ReadString("ConfigDir", &confDir, "/etc/rscriptd");
251 cfg->ReadString("Password", &password, "");
252 cfg->ReadInt("Port", &port, 5555);
253 cfg->ReadInt("UserTimeout", &userTimeout, 60);
254 cfg->ReadString("ScriptOnConnect", &onConnect, "/etc/rscriptd/OnConnect");
255 cfg->ReadString("ScriptOnDisconnect", &onDisconnect, "/etc/rscriptd/OnDisconnect");
257 if (ForkAndWait(confDir) < 0)
259 STG_LOGGER & WriteServLog = GetStgLogger();
260 WriteServLog("Fork error!");
265 STG_LOGGER & WriteServLog = GetStgLogger();
266 PIDFile pidFile("/var/run/rscriptd.pid");
267 WriteServLog.SetLogFileName(logFileName);
268 WriteServLog("rscriptd v. %s", SERVER_VERSION);
270 for (int i = 0; i < execNum; i++)
272 int ret = StartScriptExecuter(argv[0], execMsgKey, &msgID);
275 STG_LOGGER & WriteServLog = GetStgLogger();
276 WriteServLog("Start Script Executer error!");
287 listener = new LISTENER();
288 listener->SetPort(port);
289 listener->SetPassword(password);
290 listener->SetUserTimeout(userTimeout);
291 listener->SetScriptOnConnect(onConnect);
292 listener->SetScriptOnDisconnect(onDisconnect);
296 WriteServLog("rscriptd started successfully.");
297 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
300 sigfillset(&signalSet);
301 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
305 sigfillset(&signalSet);
307 printfd(__FILE__, "Before sigwait\n");
308 sigwait(&signalSet, &sig);
309 printfd(__FILE__, "After sigwait. Signal: %d\n", sig);
320 WriteServLog("Ignore signel %d", sig);
329 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
331 StopScriptExecuter(msgID);
333 WriteServLog("rscriptd stopped successfully.");
334 WriteServLog("---------------------------------------------");
340 //-----------------------------------------------------------------------------