Changeset 353 in MondoRescue for branches/stable/monitas/server.c
- Timestamp:
- Jan 28, 2006, 6:42:59 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/monitas/server.c
r352 r353 80 80 81 81 #define NOOF_THREADS 10 82 #define LOGFILE "/var/log/monitas-server.log"83 #define log_it(x,y) { log_it_SUB(g_logfile,x,y); }82 //#define LOGFILE "/var/log/monitas-server.log" 83 //#define log_it(x,y) { log_it_SUB(g_logfile,x,y); } 84 84 85 85 … … 100 100 extern char *tmsg_to_string(t_msg); 101 101 extern int transmit_file_to_socket(FILE*,int); 102 102 extern int parse_options(int argc, char *argv[]); 103 103 104 104 … … 108 108 char g_command_fifo[MAX_STR_LEN+1]; // Device which server will monitor for incoming commands 109 109 pthread_t g_threadinfo[NOOF_THREADS]; // One thread per port, to watch for requests from clients 110 char g_logfile[MAX_STR_LEN+1] = "/var/log/monitas-server.log";111 110 char g_server_status_file[MAX_STR_LEN+1]; 112 111 … … 156 155 FILE*fout; 157 156 158 sprintf(tmp, "%s - backup of %s commencing", ipaddr, clientpath); 159 log_it(info, tmp); 160 sprintf(outfile, "/var/spool/monitas/%s/%s.dat", ipaddr, call_program_and_get_last_line_of_output("date +%s")); 161 if (does_file_exist(outfile)) { log_it(error, "Backup storage location exists already. That should be impossible."); return(1); } 157 log_it(info, "%s - backup of %s commencing", ipaddr, clientpath); 158 sprintf(outfile, "/var/spool/monitas/%s/%lu.dat", ipaddr, time(NULL)); 159 if (does_file_exist(outfile)) { log_it(error, "Backup storage location '%s' exists already. That should be impossible.", outfile); return(1); } 162 160 if (make_hole_for_file(outfile)) 163 { res++; log_it(error, "Cannot write archive to spool dir "); }161 { res++; log_it(error, "Cannot write archive to spool dir '%s'", outfile); } 164 162 else if (!(fout=fopen(outfile, "w"))) 165 { res++; log_it(fatal, "Failed to openout temp data file "); }163 { res++; log_it(fatal, "Failed to openout temp data file '%s'", outfile); } 166 164 else 167 165 { 168 sprintf(tmp, "Backing up %s - archive=%s", ipaddr, outfile); 169 log_it(debug, tmp); 166 log_it(debug, "Backing up %s - archive=%s", ipaddr, outfile); 170 167 rec_to_client.msg_type = trigger_backup; 171 168 strncpy(rec_to_client.body, clientpath, sizeof(rec_to_client.body)); … … 193 190 if (res>0) 194 191 { 195 sprintf(tmp, "%s - error(s) occurred while backing up %s", ipaddr, clientpath); 196 log_it(error, tmp); 192 log_it(error, "%s - error(s) occurred while backing up %s", ipaddr, clientpath); 197 193 rec_to_client.msg_type = backup_fail; 198 194 sprintf(rec_to_client.body, "Failed to backup %s", clientpath); … … 202 198 else 203 199 { 204 sprintf(tmp, "%s - backed up %s ok", ipaddr, clientpath); 205 log_it(info, tmp); 200 log_it(info, "%s - backed up %s ok", ipaddr, clientpath); 206 201 rec_to_client.msg_type = backup_ok; 207 202 sprintf(rec_to_client.body, "%s - backed up ok", clientpath); … … 211 206 { 212 207 res++; 213 sprintf(tmp, "Unable to notify %s of backup success/failure", ipaddr); 214 log_it(error, tmp); 208 log_it(error, "Unable to notify %s of backup success/failure", ipaddr); 215 209 i = find_client_in_clientlist(ipaddr); 216 210 if (i>=0) { forcibly_logout_client(i); } … … 241 235 FILE*fin; 242 236 243 sprintf(tmp, "%s - comparison of %s commencing", ipaddr, clientpath); 244 log_it(info, tmp); 237 log_it(info, "%s - comparison of %s commencing", ipaddr, clientpath); 245 238 // FIXME - don't assume the latest backup contains the files we want ;) 246 239 sprintf(tmp, "find /var/spool/monitas/%s -type f | sort | tail -n1", ipaddr); 247 240 strcpy(infile, call_program_and_get_last_line_of_output(tmp)); 248 sprintf(tmp, "Comparing to data file '%s'", infile); 249 log_it(debug, tmp); 250 if (!does_file_exist(infile)) { log_it(error, "Backup not found. That should be impossible."); return(1); } 251 sprintf(tmp, "Comparing %s - archive=%s", ipaddr, infile); 252 log_it(debug, tmp); 241 log_it(debug, "Comparing to data file '%s'", infile); 242 if (!does_file_exist(infile)) { log_it(error, "Backup '%s' not found. That should be impossible.", infile); return(1); } 243 log_it(debug, "Comparing %s - archive=%s", ipaddr, infile); 253 244 rec_to_client.msg_type = trigger_compare; 254 245 strncpy(rec_to_client.body, clientpath, sizeof(rec_to_client.body)); … … 256 247 { log_it(error, "compare_client - failed to send msg to client"); return(1); } 257 248 if (!(fin=fopen(infile, "r"))) 258 { log_it(fatal, "Failed to openin temp data file "); }249 { log_it(fatal, "Failed to openin temp data file '%s'", infile); } 259 250 res += transmit_file_to_socket(fin, socket_fd); 260 251 len=read(socket_fd, (char*)&i, sizeof(i)); … … 267 258 if (res>0) 268 259 { 269 sprintf(tmp, "%s - error(s) occurred while comparing %s", ipaddr, clientpath); 270 log_it(error, tmp); 260 log_it(error, "%s - error(s) occurred while comparing %s", ipaddr, clientpath); 271 261 rec_to_client.msg_type = compare_fail; 272 262 sprintf(rec_to_client.body, "Failed to compare %s", clientpath); … … 275 265 else 276 266 { 277 sprintf(tmp, "%s - compared %s ok", ipaddr, clientpath); 278 log_it(info, tmp); 267 log_it(info, "%s - compared %s ok", ipaddr, clientpath); 279 268 rec_to_client.msg_type = compare_ok; 280 269 sprintf(rec_to_client.body, "%s - compared ok", clientpath); … … 283 272 if (send_msg_to_client(&rec_to_client, ipaddr, port, &socket_fd)) 284 273 { 285 sprintf(tmp, "Unable to notify %s of compare success/failure", ipaddr); 286 log_it(error, tmp); 274 log_it(error, "Unable to notify %s of compare success/failure", ipaddr); 287 275 i = find_client_in_clientlist(ipaddr); 288 276 if (i>=0) { forcibly_logout_client(i); } … … 307 295 { 308 296 int i; 309 char tmp[MAX_STR_LEN+1];310 297 311 298 for(i = 0; i < g_clientlist.items; i++) … … 313 300 if (!strcmp(clientIP, g_clientlist.el[i].ipaddr)) 314 301 { return(i); } 315 sprintf(tmp, "find_client_in_clientlist: Compared %s to clientlist[%d]=%s; failed\n", clientIP, i, g_clientlist.el[i].ipaddr); 316 log_it(debug, tmp); 302 log_it(debug, "find_client_in_clientlist: Compared %s to clientlist[%d]=%s; failed\n", clientIP, i, g_clientlist.el[i].ipaddr); 317 303 } 318 304 return(-1); … … 359 345 int res=0; 360 346 361 sprintf(tmp, "Forcibly logging %s out", g_clientlist.el[clientno].ipaddr); 362 log_it(info, tmp); 347 log_it(info, "Forcibly logging %s out", g_clientlist.el[clientno].ipaddr); 363 348 rec_to_client.msg_type = logout_ok; /* to confirm logout */ 364 349 strcpy(rec_to_client.body, "Server is shutting down. You are forced to logout"); … … 366 351 if (--g_clientlist.items > 0) 367 352 { 368 sprintf(tmp, "Moving clientlist[%d] to clientlist[%d]", clientno, g_clientlist.items); 369 log_it(debug, tmp); 353 log_it(debug, "Moving clientlist[%d] to clientlist[%d]", clientno, g_clientlist.items); 370 354 sprintf(tmp, "Was ipaddr=%s; now is ipaddr=", g_clientlist.el[clientno].ipaddr); 371 355 memcpy((void*)&g_clientlist.el[clientno], (void*)&g_clientlist.el[g_clientlist.items], sizeof(struct s_registered_client_record)); 372 356 strcat(tmp, g_clientlist.el[clientno].ipaddr); 357 /* FIXME: tmp must never contain '%'-sequences */ 373 358 log_it(debug, tmp); 374 359 } … … 419 404 */ 420 405 { 421 char clientIP[MAX_STR_LEN+1] , tmp[MAX_STR_LEN+1];406 char clientIP[MAX_STR_LEN+1]; 422 407 unsigned char *ptr; 423 408 int res=0; … … 426 411 ptr = (unsigned char*)(&sin->sin_addr); 427 412 sprintf(clientIP, "%d.%d.%d.%d", ptr[0], ptr[1], ptr[2], ptr[3]); 428 sprintf(tmp, "clientIP = %s", clientIP); 429 log_it(debug, tmp); 430 sprintf(tmp, "%s message from %s [%s] (port %d)", tmsg_to_string(rec->msg_type), clientIP, rec->body, rec->port); 431 log_it(debug, tmp); 413 log_it(debug, "clientIP = %s", clientIP); 414 log_it(debug, "%s message from %s [%s] (port %d)", tmsg_to_string(rec->msg_type), clientIP, rec->body, rec->port); 432 415 switch(rec->msg_type) 433 416 { … … 471 454 struct s_server2client_msg_record rec_to_client; 472 455 int clientno; 473 char tmp[MAX_STR_LEN+1];474 456 475 457 //FIXME - lock g_clientlist[] … … 479 461 rec_to_client.msg_type = login_fail; 480 462 sprintf(rec_to_client.body, "Sorry, you're already logged in!"); 481 sprintf(tmp, "Ignoring login rq from %s: he's already logged in.", clientIP); 482 log_it(error, tmp); 463 log_it(error, "Ignoring login rq from %s: he's already logged in.", clientIP); 483 464 /* FIXME - ping client (which will have a child watching for incoming 484 465 packets by now - you didn't forget to do that, did you? :)) - to find out … … 497 478 g_clientlist.el[clientno].busy = false; 498 479 g_clientlist.items ++; 499 sprintf(tmp, "Login request from %s ACCEPTED", clientIP); 500 log_it(info, tmp); 480 log_it(info, "Login request from %s ACCEPTED", clientIP); 501 481 strcpy(g_clientlist.el[clientno].last_progress_rpt, "Logged in"); 502 482 } … … 523 503 struct s_server2client_msg_record rec_to_client; 524 504 int i, res=0; 525 char tmp[MAX_STR_LEN+1];526 505 527 506 i = find_client_in_clientlist(clientIP); … … 550 529 g_clientlist.items--; 551 530 rec_to_client.msg_type = logout_ok; /* to confirm logout */ 552 sprintf(tmp, "Logout request from %s ACCEPTED", clientIP); 553 log_it(info, tmp); 531 log_it(info, "Logout request from %s ACCEPTED", clientIP); 554 532 } 555 533 send_msg_to_client(&rec_to_client, clientIP, rec_from_client->port, NULL); … … 575 553 struct s_server2client_msg_record rec_to_client; 576 554 int i; 577 char tmp[MAX_STR_LEN+1];578 555 579 556 i = find_client_in_clientlist(clientIP); 580 557 if (i < 0) 581 558 { 582 sprintf(tmp, "Hey, %s isn't logged in. I'm not going to pong him.", clientIP); 583 log_it(error, tmp); 559 log_it(error, "Hey, %s isn't logged in. I'm not going to pong him.", clientIP); 584 560 } 585 561 else … … 611 587 // struct s_server2client_msg_record rec_to_client; 612 588 int i, res=0; 613 char tmp[MAX_STR_LEN+1];614 589 615 590 i = find_client_in_clientlist(clientIP); 616 591 if (i < 0) 617 592 { 618 sprintf(tmp, "Hey, %s isn't logged in. I'm not going to deal with his progress_rpt.", clientIP); 619 log_it(error, tmp); 593 log_it(error, "Hey, %s isn't logged in. I'm not going to deal with his progress_rpt.", clientIP); 620 594 res++; 621 595 } … … 645 619 // struct s_server2client_msg_record rec_to_client; 646 620 int i, res=0; 647 char tmp[MAX_STR_LEN+1],command[MAX_STR_LEN+1], first_half[MAX_STR_LEN+1], second_half[MAX_STR_LEN+1], *p;621 char command[MAX_STR_LEN+1], first_half[MAX_STR_LEN+1], second_half[MAX_STR_LEN+1], *p; 648 622 649 623 i = find_client_in_clientlist(clientIP); 650 624 if (i < 0) 651 625 { 652 sprintf(tmp, "Hey, %s isn't logged in. I'm not going to deal with his request.", clientIP); 653 log_it(error, tmp); 626 log_it(error, "Hey, %s isn't logged in. I'm not going to deal with his request.", clientIP); 654 627 res++; 655 628 } … … 659 632 p = strchr(first_half, ' '); 660 633 if (!p) { second_half[0]='\0'; } else { strcpy(second_half, p); *p='\0'; } 661 sprintf(command, "echo \"%s %s%s\" > %s", first_half, clientIP, second_half, SERVER_COMDEV);634 sprintf(command, "echo \"%s %s%s\" > %s", first_half, clientIP, second_half, g->server_comdev); 662 635 log_it(debug, command); 663 636 i = system(command); … … 683 656 int res=0, port; 684 657 int clientno; 685 char tmp[MAX_STR_LEN+1];686 658 int pos; 687 659 char command[MAX_STR_LEN+1], ipaddr[MAX_STR_LEN+1], … … 698 670 699 671 // for(i=0; i<strlen(command); i++) { command[i]=command[i]|0x60; } 700 sprintf(tmp, "cmd=%s ipaddr=%s path=%s", command, ipaddr, path); 701 log_it(debug, tmp); 702 sprintf(tmp, "%s of %s on %s <-- command received", command, path, ipaddr); 703 log_it(info, tmp); 672 log_it(debug, "cmd=%s ipaddr=%s path=%s", command, ipaddr, path); 673 log_it(info, "%s of %s on %s <-- command received", command, path, ipaddr); 704 674 if ((clientno = find_client_in_clientlist(ipaddr)) < 0) 705 675 { 706 sprintf(tmp, "%s not found in clientlist; so, %s failed.", ipaddr, command); 707 log_it(error, tmp); 676 log_it(error, "%s not found in clientlist; so, %s failed.", ipaddr, command); 708 677 } 709 678 else if (g_clientlist.el[clientno].busy == true) 710 679 { 711 sprintf(tmp, "%s is busy; so, %s failed.", ipaddr, command); 712 log_it(error, tmp); 680 log_it(error, "%s is busy; so, %s failed.", ipaddr, command); 713 681 } 714 682 else … … 724 692 else 725 693 { 726 sprintf(tmp, "%s - cannot '%s'. Command unknown.", ipaddr, command); 727 log_it(error, tmp); 694 log_it(error, "%s - cannot '%s'. Command unknown.", ipaddr, command); 728 695 res=1; 729 696 } … … 753 720 FILE*fin; 754 721 755 sprintf(tmp, "%s - restoration of %s commencing", ipaddr, clientpath); 756 log_it(info, tmp); 722 log_it(info, "%s - restoration of %s commencing", ipaddr, clientpath); 757 723 // FIXME - don't assume the latest backup contains the files we want ;) 758 724 sprintf(tmp, "find /var/spool/monitas/%s -type f | sort | tail -n1", ipaddr); 759 725 strcpy(infile, call_program_and_get_last_line_of_output(tmp)); 760 sprintf(tmp, "Restoring from data file '%s'", infile); 761 log_it(debug, tmp); 762 if (!does_file_exist(infile)) { log_it(error, "Backup not found. That should be impossible."); return(1); } 763 sprintf(tmp, "Restoring %s - archive=%s", ipaddr, infile); 764 log_it(debug, tmp); 726 log_it(debug, "Restoring from data file '%s'", infile); 727 if (!does_file_exist(infile)) { log_it(error, "Backup '%s' not found. That should be impossible.", infile); return(1); } 728 log_it(debug, "Restoring %s - archive=%s", ipaddr, infile); 765 729 rec_to_client.msg_type = trigger_restore; 766 730 strncpy(rec_to_client.body, clientpath, sizeof(rec_to_client.body)); … … 782 746 if (res>0) 783 747 { 784 sprintf(tmp, "%s - error(s) occurred while restoring %s", ipaddr, clientpath); 785 log_it(error, tmp); 748 log_it(error, "%s - error(s) occurred while restoring %s", ipaddr, clientpath); 786 749 rec_to_client.msg_type = restore_fail; 787 750 sprintf(rec_to_client.body, "Failed to restore %s", clientpath); … … 790 753 else 791 754 { 792 sprintf(tmp, "%s - restored %s ok", ipaddr, clientpath); 793 log_it(info, tmp); 755 log_it(info, "%s - restored %s ok", ipaddr, clientpath); 794 756 rec_to_client.msg_type = restore_ok; 795 757 sprintf(rec_to_client.body, "%s - restored ok", clientpath); … … 798 760 if (send_msg_to_client(&rec_to_client, ipaddr, port, &socket_fd)) 799 761 { 800 sprintf(tmp, "Unable to notify %s of restore success/failure", ipaddr); 801 log_it(error, tmp); 762 log_it(error, "Unable to notify %s of restore success/failure", ipaddr); 802 763 i = find_client_in_clientlist(ipaddr); 803 764 if (i>=0) { forcibly_logout_client(i); } … … 830 791 struct sockaddr_in sin; 831 792 int s; 832 char tmp[MAX_STR_LEN+1];833 793 if ((hp = gethostbyname(clientIP)) == NULL) 834 794 { 835 sprintf(tmp, "send_msg_to_client: %s: unknown host", clientIP); 836 log_it(error, tmp); 795 log_it(error, "send_msg_to_client: %s: unknown host", clientIP); 837 796 return(1); 838 797 } … … 845 804 { perror("socket"); log_it(error, "send_msg_to_client: SOCKET error"); return(1); } 846 805 if (connect(s, (struct sockaddr*)&sin, sizeof(struct sockaddr_in)) < 0) 847 { sprintf(tmp, "Failed to connect to client %s on port %d", clientIP, port); log_it(error, tmp); return(1); }806 { log_it(error, "Failed to connect to client %s on port %d", clientIP, port); return(1); } 848 807 send(s, (char*)rec, sizeof(struct s_server2client_msg_record), 0); 849 808 if (psocket) { *psocket=s; } else { close(s); } 850 sprintf(tmp, "Sent %s msg [%s] to %s (port %d)", tmsg_to_string(rec->msg_type), rec->body, clientIP, port); 851 log_it(debug, tmp); 809 log_it(debug, "Sent %s msg [%s] to %s (port %d)", tmsg_to_string(rec->msg_type), rec->body, clientIP, port); 852 810 return(0); 853 811 } … … 883 841 usleep(50000); 884 842 } 885 sprintf(tmp, "Now monitoring ports %d thru %d for requests from clients.", 8700, 8700+NOOF_THREADS-1); 886 log_it(info, tmp); 843 log_it(info, "Now monitoring ports %d thru %d for requests from clients.", 8700, 8700+NOOF_THREADS-1); 887 844 } 888 845 … … 950 907 int watch_port; 951 908 struct sockaddr_in sin; 952 char buf[MAX_STR_LEN+1] , tmp[MAX_STR_LEN+1];909 char buf[MAX_STR_LEN+1]; 953 910 int len, s, new_s; 954 911 struct s_client2server_msg_record rec; … … 962 919 if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) 963 920 { 964 sprintf(tmp, "Unable to open socket on port #%d", watch_port); 965 log_it(error, tmp); 921 log_it(error, "Unable to open socket on port #%d", watch_port); 966 922 return((void*)-1); 967 923 } 968 924 if (bind(s, (struct sockaddr*)&sin, sizeof(sin)) < 0) 969 925 { 970 sprintf(tmp, "Cannot bind %d - %s\n", watch_port, strerror(errno)); 971 log_it(error, tmp); 926 log_it(error, "Cannot bind %d - %s\n", watch_port, strerror(errno)); 972 927 return((void*)-1); 973 928 } 974 929 if (listen(s, MAX_PENDING) < 0) 975 930 { 976 sprintf(tmp, "Cannot setup listen (%d) - %s\n", watch_port, strerror(errno)); 977 log_it(error, tmp); 931 log_it(error, "Cannot setup listen (%d) - %s\n", watch_port, strerror(errno)); 978 932 return((void*)-1); 979 933 } 980 934 /* service incoming connections */ 981 sprintf(tmp, "Bound port #%d OK", watch_port); 982 log_it(debug, tmp); 935 log_it(debug, "Bound port #%d OK", watch_port); 983 936 while(true) 984 937 { … … 1016 969 pthread_t server_status_thread; 1017 970 971 parse_options(argc, argv); 972 1018 973 log_it(info, "---------- Monitas (server) by Hugo Rabson ----------"); 1019 974 register_pid(getpid(), "server"); 1020 975 set_signals(true); 1021 976 start_threads_to_watch_ports_for_requests(); 1022 pthread_create(&server_status_thread, NULL, generate_server_status_file_regularly, (void*) SERVER_STATUS_FILE);1023 create_and_watch_fifo_for_commands( SERVER_COMDEV);977 pthread_create(&server_status_thread, NULL, generate_server_status_file_regularly, (void*)g->server_status_file); 978 create_and_watch_fifo_for_commands(g->server_comdev); 1024 979 log_it(warn, "Execution should never reach this point"); 1025 980 exit(0);
Note:
See TracChangeset
for help on using the changeset viewer.