]> git.stg.codes - stg.git/blob - projects/stargazer/script_executer.cpp
Добавление исходников
[stg.git] / projects / stargazer / script_executer.cpp
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <string>
4 #include <string.h>
5 #include <errno.h>
6 #include <signal.h>
7
8 #include <sys/types.h>
9 #include <sys/ipc.h>
10 #include <sys/msg.h>
11
12 #include "common.h"
13
14 using namespace std;
15
16 #define MAX_SCRIPT_LEN  (256)
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 //printfd(__FILE__, "CatchUSR1Executer\n");
31 exit(0);
32 nonstop = false;
33 }
34 //-----------------------------------------------------------------------------
35 int ScriptExec(const string & str)
36 {
37 if (str.length() >= MAX_SCRIPT_LEN)
38     return -1;
39
40 /*printfd(__FILE__, "2 Script schedule: %s\n", str.c_str());
41
42 if (access(str.c_str(), X_OK))
43     return -1;*/
44
45 struct msgbuf * mbuf;
46 int ret;
47 strncpy(sd.script, str.c_str(), MAX_SCRIPT_LEN);
48 sd.mtype = 1;
49 mbuf = (struct msgbuf * )&sd;
50 ret = msgsnd(msgid, mbuf, MAX_SCRIPT_LEN, 0);
51 if (ret < 0)
52     {
53     printfd(__FILE__, "snd ERROR!\n");
54     return -1;
55     }
56 return 0;
57 }
58 //-----------------------------------------------------------------------------
59 void Executer(int msgKey, int msgID, pid_t pid, char * procName)
60 {
61 msgid = msgID;
62 if (pid)
63     return;
64 nonstop = true;
65 //printfd(__FILE__, "close(pipeOut) %d pid=%d\n", pipeOut, getpid());
66
67 //printfd(__FILE__, "Executer started %d\n", getpid());
68 #ifdef LINUX
69 memset(procName, 0, strlen(procName));
70 strcpy(procName, "stg-exec");
71 #else
72 setproctitle("stg-exec");
73 #endif
74
75 struct sigaction newsa, oldsa;
76 sigset_t sigmask;
77
78 sigemptyset(&sigmask);
79 sigaddset(&sigmask, SIGTERM);
80 newsa.sa_handler = SIG_IGN;
81 newsa.sa_mask = sigmask;
82 newsa.sa_flags = 0;
83 sigaction(SIGTERM, &newsa, &oldsa);
84
85 sigemptyset(&sigmask);
86 sigaddset(&sigmask, SIGINT);
87 newsa.sa_handler = SIG_IGN;
88 newsa.sa_mask = sigmask;
89 newsa.sa_flags = 0;
90 sigaction(SIGINT, &newsa, &oldsa);
91
92 sigemptyset(&sigmask);
93 sigaddset(&sigmask, SIGUSR1);
94 newsa.sa_handler = CatchUSR1Executer;
95 newsa.sa_mask = sigmask;
96 newsa.sa_flags = 0;
97 sigaction(SIGUSR1, &newsa, &oldsa);
98
99 int ret;
100
101 SCRIPT_DATA sd;
102
103 while (nonstop)
104     {
105     sd.mtype = 1;
106     //printfd(__FILE__, "Waiting for msgrcv... msgid=%d pid=%d\n", msgid, getpid());
107     ret = msgrcv(msgid, &sd, MAX_SCRIPT_LEN, 0, 0);
108
109     if (ret < 0)
110         {
111         usleep(20000);
112         continue;
113         }
114     //printfd(__FILE__, "Script execute: %s\n", sd.script);
115     system(sd.script);
116     }
117 exit(0);
118 }
119 //-----------------------------------------------------------------------------
120
121