]> git.stg.codes - stg.git/blob - stglibs/scriptexecuter.lib/scriptexecuter.c
Merge branch 'stg-2.409-radius'
[stg.git] / stglibs / scriptexecuter.lib / scriptexecuter.c
1 #include <sys/types.h>
2 #include <sys/ipc.h>
3 #include <sys/msg.h>
4
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <signal.h>
10
11 #include "stg/scriptexecuter.h"
12
13 #define MAX_SCRIPT_LEN  (1100)
14
15 static int msgid;
16 static int nonstop;
17
18 //-----------------------------------------------------------------------------
19 struct SCRIPT_DATA
20 {
21     long    mtype;
22     char    script[MAX_SCRIPT_LEN];
23 } sd;
24 //-----------------------------------------------------------------------------
25 static void CatchUSR1Executer()
26 {
27 nonstop = 0;
28 }
29 //-----------------------------------------------------------------------------
30 int ScriptExec(const char * str)
31 {
32 if (strlen(str) >= MAX_SCRIPT_LEN)
33     return -1;
34
35 strncpy(sd.script, str, MAX_SCRIPT_LEN);
36 sd.mtype = 1;
37 if (msgsnd(msgid, (void *)&sd, MAX_SCRIPT_LEN, 0) < 0)
38     return -1;
39
40 return 0;
41 }
42 //-----------------------------------------------------------------------------
43 #if defined(LINUX) || defined(DARWIN)
44 void Executer(int msgID, pid_t pid, char * procName)
45 #else
46 void Executer(int msgID, pid_t pid)
47 #endif
48 {
49 struct SCRIPT_DATA sd;
50 struct sigaction newsa, oldsa;
51 sigset_t sigmask;
52
53 msgid = msgID;
54 if (pid)
55     return;
56 nonstop = 1;
57
58 #if defined(LINUX) || defined(DARWIN)
59 memset(procName, 0, strlen(procName));
60 strcpy(procName, "stg-exec");
61 #else
62 setproctitle("stg-exec");
63 #endif
64
65 sigemptyset(&sigmask);
66 sigaddset(&sigmask, SIGTERM);
67 newsa.sa_handler = SIG_IGN;
68 newsa.sa_mask = sigmask;
69 newsa.sa_flags = 0;
70 sigaction(SIGTERM, &newsa, &oldsa);
71
72 sigemptyset(&sigmask);
73 sigaddset(&sigmask, SIGINT);
74 newsa.sa_handler = SIG_IGN;
75 newsa.sa_mask = sigmask;
76 newsa.sa_flags = 0;
77 sigaction(SIGINT, &newsa, &oldsa);
78
79 sigemptyset(&sigmask);
80 sigaddset(&sigmask, SIGHUP);
81 newsa.sa_handler = SIG_IGN;
82 newsa.sa_mask = sigmask;
83 newsa.sa_flags = 0;
84 sigaction(SIGHUP, &newsa, &oldsa);
85
86 sigemptyset(&sigmask);
87 sigaddset(&sigmask, SIGUSR1);
88 newsa.sa_handler = CatchUSR1Executer;
89 newsa.sa_mask = sigmask;
90 newsa.sa_flags = 0;
91 sigaction(SIGUSR1, &newsa, &oldsa);
92
93 while (nonstop)
94     {
95     sd.mtype = 1;
96     int ret = msgrcv(msgid, &sd, MAX_SCRIPT_LEN, 0, 0);
97
98     if (ret < 0)
99         {
100         usleep(20000);
101         continue;
102         }
103     ret = system(sd.script);
104     if (ret == -1)
105         {
106         // Fork failed
107         }
108     }
109 }
110 //-----------------------------------------------------------------------------