]> git.stg.codes - stg.git/blob - stglibs/scriptexecuter.lib/scriptexecuter.cpp
Logging added for scriptexecuter
[stg.git] / stglibs / scriptexecuter.lib / scriptexecuter.cpp
1 #include <sys/types.h>
2 #include <sys/ipc.h>
3 #include <sys/msg.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include <cstring>
8 #include <cerrno>
9 #include <csignal>
10
11 #include "stg/common.h"
12 #include "scriptexecuter.h"
13
14 using namespace std;
15
16 #define MAX_SCRIPT_LEN  (1100)
17
18 static int msgid;
19 static bool nonstop;
20
21 //-----------------------------------------------------------------------------
22 struct SCRIPT_DATA
23 {
24     long    mtype;
25     char    script[MAX_SCRIPT_LEN];
26 } sd;
27 //-----------------------------------------------------------------------------
28 static void CatchUSR1Executer(int)
29 {
30 nonstop = false;
31 }
32 //-----------------------------------------------------------------------------
33 int ScriptExec(const string & str)
34 {
35 if (str.length() >= MAX_SCRIPT_LEN)
36     {
37     printfd(__FILE__, "ScriptExec() - script params exceeds MAX_SCRIPT_LENGTH (%d > %d)\n", str.length(), MAX_SCRIPT_LEN);
38     return -1;
39     }
40
41 strncpy(sd.script, str.c_str(), MAX_SCRIPT_LEN);
42 sd.mtype = 1;
43 if (msgsnd(msgid, (void *)&sd, MAX_SCRIPT_LEN, 0) < 0)
44     {
45     printfd(__FILE__, "ScriptExec() - failed to send message to the IPC queue: '%s'\n", strerror(errno));
46     return -1;
47     }
48 return 0;
49 }
50 //-----------------------------------------------------------------------------
51 #ifdef LINUX
52 void Executer(int, int msgID, pid_t pid, char * procName)
53 #else
54 void Executer(int, int msgID, pid_t pid, char *)
55 #endif
56 {
57 msgid = msgID;
58 if (pid)
59     return;
60 nonstop = true;
61
62 #ifdef LINUX
63 memset(procName, 0, strlen(procName));
64 strcpy(procName, "stg-exec");
65 #else
66 setproctitle("stg-exec");
67 #endif
68
69 struct sigaction newsa, oldsa;
70 sigset_t sigmask;
71
72 sigemptyset(&sigmask);
73 sigaddset(&sigmask, SIGTERM);
74 newsa.sa_handler = SIG_IGN;
75 newsa.sa_mask = sigmask;
76 newsa.sa_flags = 0;
77 sigaction(SIGTERM, &newsa, &oldsa);
78
79 sigemptyset(&sigmask);
80 sigaddset(&sigmask, SIGINT);
81 newsa.sa_handler = SIG_IGN;
82 newsa.sa_mask = sigmask;
83 newsa.sa_flags = 0;
84 sigaction(SIGINT, &newsa, &oldsa);
85
86 sigemptyset(&sigmask);
87 sigaddset(&sigmask, SIGHUP);
88 newsa.sa_handler = SIG_IGN;
89 newsa.sa_mask = sigmask;
90 newsa.sa_flags = 0;
91 sigaction(SIGHUP, &newsa, &oldsa);
92
93 sigemptyset(&sigmask);
94 sigaddset(&sigmask, SIGUSR1);
95 newsa.sa_handler = CatchUSR1Executer;
96 newsa.sa_mask = sigmask;
97 newsa.sa_flags = 0;
98 sigaction(SIGUSR1, &newsa, &oldsa);
99
100 int ret;
101
102 SCRIPT_DATA sd;
103
104 while (nonstop)
105     {
106     sd.mtype = 1;
107     ret = msgrcv(msgid, &sd, MAX_SCRIPT_LEN, 0, 0);
108
109     if (ret < 0)
110         {
111         usleep(20000);
112         continue;
113         }
114     int ret = system(sd.script);
115     if (ret == -1)
116         {
117         // Fork failed
118         }
119     }
120 }
121 //-----------------------------------------------------------------------------