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!");
120 Executer(*msgID, executerPid, procName);
124 if (executersPid.empty())
125 Executer(*msgID, executerPid, NULL);
126 executersPid.insert(executerPid);
130 //-----------------------------------------------------------------------------
131 void StopScriptExecuter(int msgID)
133 STG_LOGGER & WriteServLog = GetStgLogger();
135 for (int i = 0; i < 5; ++i)
137 struct msqid_ds data;
138 if (msgctl(msgID, IPC_STAT, &data))
141 printfd(__FILE__, "StopScriptExecuter() - msgctl for IPC_STAT failed: '%s'\n", strerror(e));
142 WriteServLog( "Failed to check queue emptiness: '%s'", strerror(e));
146 WriteServLog("Messages in queue: %d", data.msg_qnum);
148 if (data.msg_qnum == 0)
151 struct timespec ts = {1, 0};
152 nanosleep(&ts, NULL);
155 if (msgctl(msgID, IPC_RMID, NULL))
158 printfd(__FILE__, "StopScriptExecuter() - msgctl for IPC_STAT failed: '%s'\n", strerror(e));
159 WriteServLog("Failed to remove queue: '%s'", strerror(e));
163 WriteServLog("Queue removed successfully.");
168 //-----------------------------------------------------------------------------
170 int ForkAndWait(const string &)
172 int ForkAndWait(const string & confDir)
176 pid_t childPid = fork();
198 //-----------------------------------------------------------------------------
199 int main(int argc, char * argv[])
201 CONFIGFILE * cfg = NULL;
202 LISTENER * listener = NULL;
217 printf("You must be root. Exit.\n");
222 cfg = new CONFIGFILE(argv[1]);
224 cfg = new CONFIGFILE("/etc/rscriptd/rscriptd.conf");
228 STG_LOGGER & WriteServLog = GetStgLogger();
229 WriteServLog.SetLogFileName("/var/log/rscriptd.log");
230 WriteServLog("Error reading config file!");
235 cfg->ReadString("LogFileName", &logFileName, "/var/log/rscriptd.log");
236 cfg->ReadInt("ExecutersNum", &execNum, 1);
237 cfg->ReadInt("ExecMsgKey", &execMsgKey, 5555);
238 cfg->ReadString("ConfigDir", &confDir, "/etc/rscriptd");
239 cfg->ReadString("Password", &password, "");
240 cfg->ReadInt("Port", &port, 5555);
241 cfg->ReadInt("UserTimeout", &userTimeout, 60);
242 cfg->ReadString("ScriptOnConnect", &onConnect, "/etc/rscriptd/OnConnect");
243 cfg->ReadString("ScriptOnDisconnect", &onDisconnect, "/etc/rscriptd/OnDisconnect");
245 if (ForkAndWait(confDir) < 0)
247 STG_LOGGER & WriteServLog = GetStgLogger();
248 WriteServLog("Fork error!");
253 STG_LOGGER & WriteServLog = GetStgLogger();
254 PIDFile pidFile("/var/run/rscriptd.pid");
255 WriteServLog.SetLogFileName(logFileName);
256 WriteServLog("rscriptd v. %s", SERVER_VERSION);
258 for (int i = 0; i < execNum; i++)
260 int ret = StartScriptExecuter(argv[0], execMsgKey, &msgID);
263 STG_LOGGER & WriteServLog = GetStgLogger();
264 WriteServLog("Start Script Executer error!");
275 listener = new LISTENER();
276 listener->SetPort(port);
277 listener->SetPassword(password);
278 listener->SetUserTimeout(userTimeout);
279 listener->SetScriptOnConnect(onConnect);
280 listener->SetScriptOnDisconnect(onDisconnect);
284 WriteServLog("rscriptd started successfully.");
285 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
288 sigfillset(&signalSet);
289 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
293 sigfillset(&signalSet);
295 printfd(__FILE__, "Before sigwait\n");
296 sigwait(&signalSet, &sig);
297 printfd(__FILE__, "After sigwait. Signal: %d\n", sig);
308 WriteServLog("Ignore signel %d", sig);
317 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
319 StopScriptExecuter(msgID);
321 WriteServLog("rscriptd stopped successfully.");
322 WriteServLog("---------------------------------------------");
328 //-----------------------------------------------------------------------------