|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
// ------------------------------------------------------------ //
// JK2 Autorestarter by ouned //
// Version: 1.1 //
// Supported operating systems: //
// Windows and Linux //
// //
// //
// Compile: ( gcc windows 3.4.5 and linux 4.1.2 ) //
// Windows: gcc -o jk2restarter.exe jk2_restarter.c -lws2_32 //
// Linux: gcc -o jk2restarter jk2_restarter.c //
// ------------------------------------------------------------ //
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef WIN32
//#include <ctype.h>
#include <winsock.h>
#include <winbase.h>
#include <Tlhelp32.h>
#include "winerr.h"
#define close closesocket
#define sleep Sleep
#else
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#endif
#define BUFFSZ 2048
#define TIMEOUT 1
#define INFO "\xff\xff\xff\xff" "getstatus\n"
int firsttime = 1;
// functions...
int send_recv(int sd, u_char *in, int inlen, u_char *out, int outlen, int bug);
u_int resolv(char *host);
int timeout(int sock);
void std_err(void);
void writelog(char *file);
struct sockaddr_in peer;
int main(int argc, char *argv[])
{
int sd;
u_char buff[BUFFSZ + 1];
int checks = 0;
u_short port;
#ifdef WIN32
WSADATA wsadata;
WSAStartup(MAKEWORD(1,0), &wsadata);
#endif
if ( argc != 7 )
{
#ifdef WIN32
printf("\n--- JK2 Autorestarter v1.0 by ouned ---"
"\n"
"\n"
"Usage: jk2ar.exe <IP / Hostname> <Port> <Interval> <startscript> <logfile> <jk2ded name>"
"\n"
"\n"
"Example: jk2ar.exe 127.0.0.1 28070 120 \"C:\\jk2\\startserver.bat\" \"C:\\jk2\\autorestart.log\" jk2Ded.exe"
"\n"
"\n"
"The Interval must be indicated in seconds!"
"\n"
"You should rename the jk2ded for each server you run!"
"\n"
);
#else
printf("\n--- JK2 Autorestarter v1.0 by ouned ---"
"\n"
"\n"
"Usage: ./jk2ar <IP / Hostname> <Port> <Interval> <startscript> <logfile> <jk2ded name>"
"\n"
"\n"
"Example: ./jk2ar 127.0.0.1 28070 120 \"/home/jk2/startserver.sh\" \"/home/jk2/autorestart.log\" jk2Ded"
"\n"
"\n"
"The Interval must be indicated in seconds!"
"\n"
"You should rename the jk2ded for each server you run!"
"\n"
);
#endif
return(1);
}
if ( ExistFile(argv[4]) == 0 )
{
printf("\nError: File (%s) doesn't exist.\n", argv[4]);
return(1);
}
if ( atoi(argv[3]) < 5 )
{
printf("\nWarning: The Interval should not be < 5!\n");
return(1);
}
// All checks passed! Start the Timer!
port = atoi(argv[2]);
peer.sin_addr.s_addr = resolv(argv[1]);
peer.sin_port = htons(port);
peer.sin_family = AF_INET;
sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
while(1)
{
strcpy(buff, "");
send_recv(sd, INFO, sizeof(INFO) - 1, buff, BUFFSZ, 0);
if (strstr(buff, "sv_hostname"))
{
checks = 0;
}
else
{
if ( checks < 3 )
{
checks++;
// Sleep!
#ifdef WIN32
Sleep(2000);
#else
sleep(2);
#endif
continue;
}
else
{
// Server is down, restart it!
checks = 0;
#ifndef WIN32
// Kill existing process
char kill[80];
strcpy(kill, "");
strcat(kill, "killall ");
strcat(kill, argv[6]);
system(kill);
// Start the Server
char start[600];
strcpy(start, "");
strcat(start, "screen -d -m ");
strcat(start, "\"");
strcat(start, argv[4]);
strcat(start, "\"");
system(start);
// Write the log
if ( firsttime != 1 )
{
writelog(argv[5]);
}
firsttime = 0;
#else
// Kill existing process
int hi;
hi = KILL_PROC_BY_NAME(argv[6]);
// Start the Server
char start[600];
strcpy(start, "");
strcat(start, "\"");
strcat(start, argv[4]);
strcat(start, "\"");
system(start);
// Write the log
if ( firsttime != 1 )
{
writelog(argv[5]);
}
firsttime = 0;
#endif
}
}
// Sleep!
#ifdef WIN32
Sleep(atoi(argv[3]) * 1000);
#else
sleep(atoi(argv[3]));
#endif
}
return(1);
}
// ---------------------------------------------------- //
// WriteLog: //
// Nothing special here. Just the log function. //
// ---------------------------------------------------- //
void writelog(char *file)
{
FILE *h;
time_t rawtime;
time ( &rawtime );
char kill[600];
strcpy(kill, "");
strcat(kill, "---------------------------------------------------\nServer restarted at ");
strcat(kill, ctime (&rawtime));
h = fopen(file,"a+");
fputs(kill ,h);
fclose(h);
return;
}
// ---------------------------------------------------- //
// ExistFile: //
// This function does check if the given startscript //
// exist. //
// ---------------------------------------------------- //
int ExistFile (char *fnam)
{
FILE * f= fopen (fnam,"r");
if (!f)
{
return 0;
}
else
{
fclose(f);
return 1;
}
}
// ---------------------------------------------------- //
// send_recv, timeout, std_err: //
// This functions try to get the Serverinfos. //
// ---------------------------------------------------- //
int send_recv(int sd, u_char *in, int inlen, u_char *out, int outlen, int bug)
{
int retry;
for(retry = 3; retry; retry--)
{
sendto(sd, in, inlen, 0, (struct sockaddr *)&peer, sizeof(peer));
if(!timeout(sd)) break;
}
if(!retry)
{
if(bug) return(-1);
return;
}
outlen = recvfrom(sd, out, outlen, 0, NULL, NULL);
if(outlen < 0) std_err();
out[outlen] = 0;
return(outlen);
}
// Of course, Socket Timeout is needed.
int timeout(int sock)
{
struct timeval tout;
fd_set fd_read;
int err;
tout.tv_sec = TIMEOUT;
tout.tv_usec = 0;
FD_ZERO(&fd_read);
FD_SET(sock, &fd_read);
err = select(sock + 1, &fd_read, NULL, NULL, &tout);
if(err < 0) std_err();
if(!err) return(-1);
return(0);
}
// Windows needs that for the Socket Timouts, Linux not!
#ifndef WIN32
void std_err(void)
{
perror("\nError");
return;
}
#endif
// ---------------------------------------------------- //
// Resolv hostname: //
// This is needed if the user type something for the IP //
// like "localhost" or "example.dyndns.org". //
// ---------------------------------------------------- //
u_int resolv(char *host)
{
struct hostent *hp;
u_int host_ip;
host_ip = inet_addr(host);
if(host_ip == INADDR_NONE)
{
hp = gethostbyname(host);
if(!hp)
{
printf("\nError: Unable to resolv hostname (%s)\n", host);
exit(1);
}
else
{
host_ip = *(u_int *)hp->h_addr;
}
}
return(host_ip);
}
// ---------------------------------------------------- //
// KILL PROCESS BY NAME //
// I found that on the Internet. //
// Only for Windows needed. //
// ---------------------------------------------------- //
#ifdef WIN32
int KILL_PROC_BY_NAME(const char *szToTerminate)
{
BOOL bResult,bResultm;
DWORD aiPID[1000],iCb=1000,iNumProc,iV2000=0;
DWORD iCbneeded,i,iFound=0;
char szName[MAX_PATH],szToTermUpper[MAX_PATH];
HANDLE hProc,hSnapShot,hSnapShotm;
OSVERSIONINFO osvi;
HINSTANCE hInstLib;
int iLen,iLenP,indx;
HMODULE hMod;
PROCESSENTRY32 procentry;
MODULEENTRY32 modentry;
iLenP=strlen(szToTerminate);
if(iLenP<1 || iLenP>MAX_PATH) return 632;
for(indx=0;indx<iLenP;indx++)
szToTermUpper[indx]=toupper(szToTerminate[indx]);
szToTermUpper[iLenP]=0;
BOOL (WINAPI *lpfEnumProcesses)( DWORD *, DWORD cb, DWORD * );
BOOL (WINAPI *lpfEnumProcessModules)( HANDLE, HMODULE *,
DWORD, LPDWORD );
DWORD (WINAPI *lpfGetModuleBaseName)( HANDLE, HMODULE,
LPTSTR, DWORD );
HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD,DWORD) ;
BOOL (WINAPI *lpfProcess32First)(HANDLE,LPPROCESSENTRY32) ;
BOOL (WINAPI *lpfProcess32Next)(HANDLE,LPPROCESSENTRY32) ;
BOOL (WINAPI *lpfModule32First)(HANDLE,LPMODULEENTRY32) ;
BOOL (WINAPI *lpfModule32Next)(HANDLE,LPMODULEENTRY32) ;
// First check what version of Windows we're in
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
bResult=GetVersionEx(&osvi);
if(!bResult) // Unable to identify system version
return 606;
// At Present we only support Win/NT/2000/XP or Win/9x/ME
if((osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) &&
(osvi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS))
return 607;
if(osvi.dwPlatformId==VER_PLATFORM_WIN32_NT)
{
// Win/NT or 2000 or XP
// Load library and get the procedures explicitly. We do
// this so that we don't have to worry about modules using
// this code failing to load under Windows 9x, because
// it can't resolve references to the PSAPI.DLL.
hInstLib = LoadLibraryA("PSAPI.DLL");
if(hInstLib == NULL)
return 605;
// Get procedure addresses.
lpfEnumProcesses = (BOOL(WINAPI *)(DWORD *,DWORD,DWORD*))
GetProcAddress( hInstLib, "EnumProcesses" ) ;
lpfEnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *,
DWORD, LPDWORD)) GetProcAddress( hInstLib,
"EnumProcessModules" ) ;
lpfGetModuleBaseName =(DWORD (WINAPI *)(HANDLE, HMODULE,
LPTSTR, DWORD )) GetProcAddress( hInstLib,
"GetModuleBaseNameA" ) ;
if(lpfEnumProcesses == NULL ||
lpfEnumProcessModules == NULL ||
lpfGetModuleBaseName == NULL)
{
FreeLibrary(hInstLib);
return 700;
}
bResult=lpfEnumProcesses(aiPID,iCb,&iCbneeded);
if(!bResult)
{
// Unable to get process list, EnumProcesses failed
FreeLibrary(hInstLib);
return 701;
}
// How many processes are there?
iNumProc=iCbneeded/sizeof(DWORD);
// Get and match the name of each process
for(i=0;i<iNumProc;i++)
{
// Get the (module) name for this process
strcpy(szName,"Unknown");
// First, get a handle to the process
hProc=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,
aiPID[i]);
// Now, get the process name
if(hProc)
{
if(lpfEnumProcessModules(hProc,&hMod,sizeof(hMod),&iCbneeded) )
{
iLen=lpfGetModuleBaseName(hProc,hMod,szName,MAX_PATH);
}
}
CloseHandle(hProc);
// We will match regardless of lower or upper case
#ifdef BORLANDC
if(strcmp(strupr(szName),szToTermUpper)==0)
#else
if(strcmp(_strupr(szName),szToTermUpper)==0)
#endif
{
// Process found, now terminate it
iFound=1;
// First open for termination
hProc=OpenProcess(PROCESS_TERMINATE,FALSE,aiPID[i]);
if(hProc)
{
if(TerminateProcess(hProc,0))
{
// process terminated
CloseHandle(hProc);
FreeLibrary(hInstLib);
return 0;
}
else
{
// Unable to terminate process
CloseHandle(hProc);
FreeLibrary(hInstLib);
return 602;
}
}
else
{
// Unable to open process for termination
FreeLibrary(hInstLib);
return 604;
}
}
}
}
if(osvi.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
{
// Win/95 or 98 or ME
hInstLib = LoadLibraryA("Kernel32.DLL");
if( hInstLib == NULL )
return 702;
// Get procedure addresses.
// We are linking to these functions of Kernel32
// explicitly, because otherwise a module using
// this code would fail to load under Windows NT,
// which does not have the Toolhelp32
// functions in the Kernel 32.
lpfCreateToolhelp32Snapshot=
(HANDLE(WINAPI *)(DWORD,DWORD))
GetProcAddress( hInstLib,
"CreateToolhelp32Snapshot" ) ;
lpfProcess32First=
(BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
GetProcAddress( hInstLib, "Process32First" ) ;
lpfProcess32Next=
(BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
GetProcAddress( hInstLib, "Process32Next" ) ;
lpfModule32First=
(BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32))
GetProcAddress( hInstLib, "Module32First" ) ;
lpfModule32Next=
(BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32))
GetProcAddress( hInstLib, "Module32Next" ) ;
if( lpfProcess32Next == NULL ||
lpfProcess32First == NULL ||
lpfModule32Next == NULL ||
lpfModule32First == NULL ||
lpfCreateToolhelp32Snapshot == NULL )
{
FreeLibrary(hInstLib);
return 703;
}
// The Process32.. and Module32.. routines return names in all uppercase
// Get a handle to a Toolhelp snapshot of all the systems processes.
hSnapShot = lpfCreateToolhelp32Snapshot(
TH32CS_SNAPPROCESS, 0 ) ;
if( hSnapShot == INVALID_HANDLE_VALUE )
{
FreeLibrary(hInstLib);
return 704;
}
// Get the first process' information.
procentry.dwSize = sizeof(PROCESSENTRY32);
bResult=lpfProcess32First(hSnapShot,&procentry);
// While there are processes, keep looping and checking.
while(bResult)
{
// Get a handle to a Toolhelp snapshot of this process.
hSnapShotm = lpfCreateToolhelp32Snapshot(
TH32CS_SNAPMODULE, procentry.th32ProcessID) ;
if( hSnapShotm == INVALID_HANDLE_VALUE )
{
CloseHandle(hSnapShot);
FreeLibrary(hInstLib);
return 704;
}
// Get the module list for this process
modentry.dwSize=sizeof(MODULEENTRY32);
bResultm=lpfModule32First(hSnapShotm,&modentry);
// While there are modules, keep looping and checking
while(bResultm)
{
if(strcmp(modentry.szModule,szToTermUpper)==0)
{
// Process found, now terminate it
iFound=1;
// First open for termination
hProc=OpenProcess(PROCESS_TERMINATE,FALSE,procentry.th32ProcessID);
if(hProc)
{
if(TerminateProcess(hProc,0))
{
// process terminated
CloseHandle(hSnapShotm);
CloseHandle(hSnapShot);
CloseHandle(hProc);
FreeLibrary(hInstLib);
return 0;
}
else
{
// Unable to terminate process
CloseHandle(hSnapShotm);
CloseHandle(hSnapShot);
CloseHandle(hProc);
FreeLibrary(hInstLib);
return 602;
}
}
else
{
// Unable to open process for termination
CloseHandle(hSnapShotm);
CloseHandle(hSnapShot);
FreeLibrary(hInstLib);
return 604;
}
}
else
{ // Look for next modules for this process
modentry.dwSize=sizeof(MODULEENTRY32);
bResultm=lpfModule32Next(hSnapShotm,&modentry);
}
}
//Keep looking
CloseHandle(hSnapShotm);
procentry.dwSize = sizeof(PROCESSENTRY32);
bResult = lpfProcess32Next(hSnapShot,&procentry);
}
CloseHandle(hSnapShot);
}
if(iFound==0)
{
FreeLibrary(hInstLib);
return 603;
}
FreeLibrary(hInstLib);
return 0;
}
#endif
|
Forum Software: Burning Board® 3.1.6, developed by WoltLab® GmbH