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
41 #include "stg_logger.h"
42 #include "script_executer.h"
43 #include "conffiles.h"
55 #define START_FILE "/._ST_ART_ED_"
57 static bool childExited = false;
58 set<pid_t> executersPid;
59 static pid_t stgChildPid;
60 volatile time_t stgTime = time(NULL);
65 STG_STOPPER() { nonstop = true; }
66 bool GetStatus() const { return nonstop; };
67 void Stop(const char * __file__, int __line__)
70 printfd(__FILE__, "rscriptd stopped at %s:%d\n", __file__, __line__);
77 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
88 void CatchTERM(int sig)
91 *Function Name:CatchINT
92 *Parameters: sig_num - signal number
93 *Description: INT signal handler
96 STG_LOGGER & WriteServLog = GetStgLogger();
97 WriteServLog("Shutting down... %d", sig);
99 nonstop.Stop(__FILE__, __LINE__);
101 struct sigaction newsa, oldsa;
104 sigemptyset(&sigmask);
105 sigaddset(&sigmask, SIGTERM);
106 newsa.sa_handler = SIG_IGN;
107 newsa.sa_mask = sigmask;
109 sigaction(SIGTERM, &newsa, &oldsa);
111 sigemptyset(&sigmask);
112 sigaddset(&sigmask, SIGINT);
113 newsa.sa_handler = SIG_IGN;
114 newsa.sa_mask = sigmask;
116 sigaction(SIGINT, &newsa, &oldsa);
118 //-----------------------------------------------------------------------------
121 STG_LOGGER & WriteServLog = GetStgLogger();
122 WriteServLog("Broken pipe!");
124 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
133 childPid = waitpid(-1, &status, WNOHANG);
135 set<pid_t>::iterator pid;
136 pid = executersPid.find(childPid);
137 if (pid != executersPid.end())
139 executersPid.erase(pid);
141 if (childPid == stgChildPid)
146 //-----------------------------------------------------------------------------
147 void SetSignalHandlers()
149 struct sigaction newsa, oldsa;
152 sigemptyset(&sigmask);
153 sigaddset(&sigmask, SIGTERM);
154 newsa.sa_handler = CatchTERM;
155 newsa.sa_mask = sigmask;
157 sigaction(SIGTERM, &newsa, &oldsa);
159 sigemptyset(&sigmask);
160 sigaddset(&sigmask, SIGUSR1);
161 newsa.sa_handler = CatchUSR1;
162 newsa.sa_mask = sigmask;
164 sigaction(SIGUSR1, &newsa, &oldsa);
166 sigemptyset(&sigmask);
167 sigaddset(&sigmask, SIGINT);
168 newsa.sa_handler = CatchTERM;
169 newsa.sa_mask = sigmask;
171 sigaction(SIGINT, &newsa, &oldsa);
173 sigemptyset(&sigmask);
174 sigaddset(&sigmask, SIGPIPE);
175 newsa.sa_handler = CatchPIPE;
176 newsa.sa_mask = sigmask;
178 sigaction(SIGPIPE, &newsa, &oldsa);
180 sigemptyset(&sigmask);
181 sigaddset(&sigmask, SIGHUP);
182 newsa.sa_handler = CatchHUP;
183 newsa.sa_mask = sigmask;
185 sigaction(SIGHUP, &newsa, &oldsa);
187 sigemptyset(&sigmask);
188 sigaddset(&sigmask, SIGCHLD);
189 newsa.sa_handler = CatchCHLD;
190 newsa.sa_mask = sigmask;
192 sigaction(SIGCHLD, &newsa, &oldsa);
194 //-----------------------------------------------------------------------------
195 int StartScriptExecuter(char * procName, int msgKey, int * msgID)
197 STG_LOGGER & WriteServLog = GetStgLogger();
199 if (*msgID == -11) // If msgID == -11 - first call. Create queue
201 for (int i = 0; i < 2; i++)
203 *msgID = msgget(msgKey, IPC_CREAT | IPC_EXCL | 0600);
207 *msgID = msgget(msgKey, 0);
210 WriteServLog("Message queue not created.");
215 msgctl(*msgID, IPC_RMID, NULL);
220 WriteServLog("Message queue created successfully. msgKey=%d msgID=%d", msgKey, *msgID);
226 pid_t executerPid = fork();
231 WriteServLog("Fork error!");
239 Executer(msgKey, *msgID, executerPid, procName);
243 if (executersPid.empty())
244 Executer(msgKey, *msgID, executerPid, NULL);
245 executersPid.insert(executerPid);
249 //-----------------------------------------------------------------------------
251 int ForkAndWait(const string &)
253 int ForkAndWait(const string & confDir)
257 stgChildPid = fork();
258 string startFile = confDir + START_FILE;
259 unlink(startFile.c_str());
275 for (int i = 0; i < 120 * 5; i++)
277 if (access(startFile.c_str(), F_OK) == 0)
279 //printf("Fork successfull. Exit.\n");
280 unlink(startFile.c_str());
286 unlink(startFile.c_str());
291 unlink(startFile.c_str());
298 //-----------------------------------------------------------------------------
301 set<pid_t>::iterator pid;
302 pid = executersPid.begin();
303 while (pid != executersPid.end())
305 printfd(__FILE__, "KillExecuters pid=%d\n", *pid);
310 //-----------------------------------------------------------------------------
311 int main(int argc, char * argv[])
315 Initialization order:
318 - Set signal nandlers
322 CONFIGFILE * cfg = NULL;
323 LISTENER * listener = NULL;
338 printf("You must be root. Exit.\n");
343 cfg = new CONFIGFILE(argv[1]);
345 cfg = new CONFIGFILE("/etc/rscriptd/rscriptd.conf");
349 STG_LOGGER & WriteServLog = GetStgLogger();
350 WriteServLog.SetLogFileName("/var/log/rscriptd.log");
351 WriteServLog("Error reading config file!");
356 cfg->ReadString("LogFileName", &logFileName, "/var/log/rscriptd.log");
357 cfg->ReadInt("ExecutersNum", &execNum, 1);
358 cfg->ReadInt("ExecMsgKey", &execMsgKey, 5555);
359 cfg->ReadString("ConfigDir", &confDir, "/etc/rscriptd");
360 cfg->ReadString("Password", &password, "");
361 cfg->ReadInt("Port", &port, 5555);
362 cfg->ReadInt("UserTimeout", &userTimeout, 60);
363 cfg->ReadString("ScriptOnConnect", &onConnect, "/etc/rscriptd/OnConnect");
364 cfg->ReadString("ScriptOnDisconnect", &onDisconnect, "/etc/rscriptd/OnDisconnect");
367 if (ForkAndWait(confDir) < 0)
369 STG_LOGGER & WriteServLog = GetStgLogger();
370 WriteServLog("Fork error!");
375 STG_LOGGER & WriteServLog = GetStgLogger();
376 PIDFile pidFile("/var/run/rscriptd.pid");
377 WriteServLog.SetLogFileName(logFileName);
378 WriteServLog("rscriptd v. %s", SERVER_VERSION);
380 for (int i = 0; i < execNum; i++)
382 int ret = StartScriptExecuter(argv[0], execMsgKey, &msgID);
385 STG_LOGGER & WriteServLog = GetStgLogger();
386 WriteServLog("Start Script Executer error!");
397 listener = new LISTENER();
398 listener->SetPort(port);
399 listener->SetPassword(password);
400 listener->SetUserTimeout(userTimeout);
401 listener->SetScriptOnConnect(onConnect);
402 listener->SetScriptOnDisconnect(onDisconnect);
406 WriteServLog("rscriptd started successfully.");
407 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
410 string startFile(confDir + START_FILE);
411 creat(startFile.c_str(), S_IRUSR);
414 while (nonstop.GetStatus())
421 WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
423 int res = msgctl(msgID, IPC_RMID, NULL);
425 WriteServLog("Queue was not removed. id=%d", msgID);
427 WriteServLog("Queue removed successfully.");
431 WriteServLog("rscriptd stopped successfully.");
432 WriteServLog("---------------------------------------------");
438 //-----------------------------------------------------------------------------