]> git.stg.codes - stg.git/blob - projects/sgauth/main.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / sgauth / main.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21  /*
22  $Revision: 1.13 $
23  $Date: 2010/04/14 09:01:29 $
24  $Author: faust $
25  */
26
27 #include <unistd.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31
32 #include <csignal>
33 #include <cstdio>
34 #include <cstring>
35 #include <iostream>
36 #include <vector>
37
38 #include "stg/ia.h"
39 #include "stg/common.h"
40 #include "web.h"
41 #include "settings_impl.h"
42
43 int mes;
44 char infoText[256];
45 char messageText[256];
46
47 const int winKOI = 0;
48
49 IA_CLIENT_PROT * clnp;
50 WEB * web = NULL;
51
52 time_t stgTime;
53
54 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 void Usage()
58 {
59 printf("sgauth <path_to_config>\n");
60 }
61 //-----------------------------------------------------------------------------
62 void SetDirName(const std::vector<std::string> & dn, void *)
63 {
64 for (int j = 0; j < DIR_NUM; j++)
65     {
66     if (winKOI)
67         {
68         std::string dir;
69         KOIToWin(dn[j], &dir);
70         if (web)
71             web->SetDirName(dir, j);
72         }
73     else
74         {
75         if (web)
76             web->SetDirName(dn[j], j);
77         }
78     }
79 }
80 //-----------------------------------------------------------------------------
81 void StatUpdate(const LOADSTAT & ls, void *)
82 {
83 if (web)
84     web->UpdateStat(ls);
85 }
86 //-----------------------------------------------------------------------------
87 void StatusChanged(int, void *)
88 {
89 }
90 //-----------------------------------------------------------------------------
91 void ShowMessage(const std::string & message, int i, int, int, void *)
92 {
93 if (web)
94     web->AddMessage(message, i);
95 }
96 //-----------------------------------------------------------------------------
97 void ShowError(const std::string & message, int, void *)
98 {
99 if (web)
100      web->AddMessage(message, 0);
101 }
102 //-----------------------------------------------------------------------------
103 void CatchUSR1(int)
104 {
105 if (clnp->GetAuthorized())
106     {
107     std::cout << "Connect" << std::endl;
108     clnp->Connect();
109     }
110 }
111 //-----------------------------------------------------------------------------
112 void CatchUSR2(int)
113 {
114 std::cout << "Disconnect" << std::endl;
115 clnp->Disconnect();
116 }
117 //-----------------------------------------------------------------------------
118 void CatchTERM(int)
119 {
120 std::cout << "Terminated" << std::endl;
121 clnp->Disconnect();
122 sleep(2);
123 exit(0);
124 }
125 //-----------------------------------------------------------------------------
126 static void SetSignalHandlers()
127 {
128 struct sigaction newsa, oldsa;
129 sigset_t sigmask;
130
131 sigemptyset(&sigmask);
132 sigaddset(&sigmask, SIGTERM);
133 newsa.sa_handler = CatchTERM;
134 newsa.sa_mask = sigmask;
135 newsa.sa_flags = 0;
136 sigaction(SIGTERM, &newsa, &oldsa);
137
138 sigemptyset(&sigmask);
139 sigaddset(&sigmask, SIGINT);
140 newsa.sa_handler = CatchTERM;
141 newsa.sa_mask = sigmask;
142 newsa.sa_flags = 0;
143 sigaction(SIGINT, &newsa, &oldsa);
144
145 sigemptyset(&sigmask);
146 sigaddset(&sigmask, SIGUSR1);
147 newsa.sa_handler = CatchUSR1;
148 newsa.sa_mask = sigmask;
149 newsa.sa_flags = 0;
150 sigaction(SIGUSR1, &newsa, &oldsa);
151
152 sigemptyset(&sigmask);
153 sigaddset(&sigmask, SIGUSR2);
154 newsa.sa_handler = CatchUSR2;
155 newsa.sa_mask = sigmask;
156 newsa.sa_flags = 0;
157 sigaction(SIGUSR2, &newsa, &oldsa);
158
159 return;
160 }
161 //-----------------------------------------------------------------------------
162 int main(int argc, char *argv[])
163 {
164 SETTINGS_IMPL settings;
165
166 if (argc == 2)
167     {
168     settings.SetConfFile(argv[1]);
169     }
170 else
171     {
172     // Usage
173     }
174
175 if (settings.ReadSettings())
176     {
177     printf("ReadSettingsError\n");
178     printf("%s\n", settings.GetStrError().c_str());
179     exit(-1);
180     }
181 settings.Print();
182
183 if (settings.GetDaemon())
184     {
185     switch (fork())
186         {
187         case -1:
188             exit(1);
189             break;
190
191         case 0:
192             setsid();
193             break;
194
195         default:
196             exit(0);
197             break;
198         }
199     }
200
201 clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalName(), settings.GetLocalPort());
202
203 if (!settings.GetNoWeb())
204     {
205     web = new WEB();
206     web->SetRefreshPagePeriod(settings.GetRefreshPeriod());
207     web->SetListenAddr(settings.GetListenWebIP());
208     web->Start();
209     }
210
211 clnp->SetLogin(settings.GetLogin());
212 clnp->SetPassword(settings.GetPassword());
213
214 clnp->SetStatusChangedCb(StatusChanged, NULL);
215 clnp->SetInfoCb(ShowMessage, NULL);
216 clnp->SetErrorCb(ShowError, NULL);
217 clnp->SetDirNameCb(SetDirName, NULL);
218 clnp->SetStatChangedCb(StatUpdate, NULL);
219 clnp->SetReconnect(settings.GetReconnect());
220
221 clnp->Start();
222
223 SetSignalHandlers();
224
225 #ifdef LINUX
226 for (int i = 1; i < argc; i++)
227     memset(argv[i], 0, strlen(argv[i]));
228
229 if(argc > 1)
230     strcpy(argv[1], "Connecting...");
231 #endif
232
233 #ifdef FREEBSD
234 setproctitle("Connecting...");
235 #endif
236 clnp->Connect();
237
238 while (1)
239     {
240     struct timespec ts = {0, 200000000};
241     nanosleep(&ts, NULL);
242
243     char state[20];
244
245     if (clnp->GetAuthorized())
246         {
247         if (settings.GetShowPid())
248             sprintf(state, "On %d", getpid());
249         else
250             strcpy(state, "Online");
251         }
252     else
253         {
254         if (settings.GetShowPid())
255             sprintf(state, "Off %d", getpid());
256         else
257             strcpy(state, "Offline");
258         }
259
260     #ifdef LINUX
261     for (int i = 1; i < argc; i++)
262         memset(argv[i], 0, strlen(argv[i]));
263     if(argc > 1)
264         strcpy(argv[1], state);
265     #endif
266
267     #ifdef FREEBSD
268     setproctitle(state);
269     #endif
270
271     #ifdef FREEBSD_5
272     setproctitle(state);
273     #endif
274     }
275
276 return 0;
277 }
278 //-----------------------------------------------------------------------------