]> git.stg.codes - stg.git/blob - stglibs/script_executer.lib/script_executer.cpp
Remove const qualifier from function definitions
[stg.git] / stglibs / script_executer.lib / script_executer.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 "script_executer.h"
12
13 using namespace std;
14
15 #define MAX_SCRIPT_LEN  (1100)
16
17 static int msgid;
18 static bool nonstop;
19
20 //-----------------------------------------------------------------------------
21 struct SCRIPT_DATA
22 {
23     long    mtype;
24     char    script[MAX_SCRIPT_LEN];
25 } sd;
26 //-----------------------------------------------------------------------------
27 static void CatchUSR1Executer(int)
28 {
29 nonstop = false;
30 }
31 //-----------------------------------------------------------------------------
32 int ScriptExec(const string & str)
33 {
34 if (str.length() >= MAX_SCRIPT_LEN)
35     return -1;
36
37 int ret;
38 strncpy(sd.script, str.c_str(), MAX_SCRIPT_LEN);
39 sd.mtype = 1;
40 ret = msgsnd(msgid, (void *)&sd, MAX_SCRIPT_LEN, 0);
41 if (ret < 0)
42     {
43     return -1;
44     }
45 return 0;
46 }
47 //-----------------------------------------------------------------------------
48 #ifdef LINUX
49 void Executer(int, int msgID, pid_t pid, char * procName)
50 #else
51 void Executer(int, int msgID, pid_t pid, char *)
52 #endif
53 {
54 msgid = msgID;
55 if (pid)
56     return;
57 nonstop = true;
58
59 #ifdef LINUX
60 memset(procName, 0, strlen(procName));
61 strcpy(procName, "stg-exec");
62 #else
63 setproctitle("stg-exec");
64 #endif
65
66 struct sigaction newsa, oldsa;
67 sigset_t sigmask;
68
69 sigemptyset(&sigmask);
70 sigaddset(&sigmask, SIGTERM);
71 newsa.sa_handler = SIG_IGN;
72 newsa.sa_mask = sigmask;
73 newsa.sa_flags = 0;
74 sigaction(SIGTERM, &newsa, &oldsa);
75
76 sigemptyset(&sigmask);
77 sigaddset(&sigmask, SIGINT);
78 newsa.sa_handler = SIG_IGN;
79 newsa.sa_mask = sigmask;
80 newsa.sa_flags = 0;
81 sigaction(SIGINT, &newsa, &oldsa);
82
83 sigemptyset(&sigmask);
84 sigaddset(&sigmask, SIGHUP);
85 newsa.sa_handler = SIG_IGN;
86 newsa.sa_mask = sigmask;
87 newsa.sa_flags = 0;
88 sigaction(SIGHUP, &newsa, &oldsa);
89
90 sigemptyset(&sigmask);
91 sigaddset(&sigmask, SIGUSR1);
92 newsa.sa_handler = CatchUSR1Executer;
93 newsa.sa_mask = sigmask;
94 newsa.sa_flags = 0;
95 sigaction(SIGUSR1, &newsa, &oldsa);
96
97 int ret;
98
99 SCRIPT_DATA sd;
100
101 while (nonstop)
102     {
103     sd.mtype = 1;
104     ret = msgrcv(msgid, &sd, MAX_SCRIPT_LEN, 0, 0);
105
106     if (ret < 0)
107         {
108         usleep(20000);
109         continue;
110         }
111     int ret = system(sd.script);
112     if (ret == -1)
113         {
114         // Fork failed
115         }
116     }
117 }
118 //-----------------------------------------------------------------------------
119
120