Changeset 353 in MondoRescue
- Timestamp:
- Jan 28, 2006, 6:42:59 PM (19 years ago)
- Location:
- branches/stable/monitas
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/monitas/client.c
r352 r353 62 62 #include "structs.h" 63 63 //#define LOG_THESE_AND_HIGHER debug 64 #define LOGFILE "/var/log/monitas-client.log"64 //#define LOGFILE "/var/log/monitas-client.log" 65 65 66 66 … … 74 74 pthread_t g_mondo_thread=0; 75 75 char g_command_fifo[MAX_STR_LEN+1]; 76 char g_logfile[MAX_STR_LEN+1] = "/var/log/monitas-client.log";77 76 78 77 /* externs */ … … 92 91 extern void register_pid(pid_t, char*); 93 92 extern void set_signals(bool); 93 extern int parse_options(int argc, char *argv[]); 94 94 95 95 /* prototypes */ … … 202 202 return(1); 203 203 } 204 strcpy(mondoparams_str, get_param_from_rcfile( CLIENT_RCFILE, "mondoarchive_params"));204 strcpy(mondoparams_str, get_param_from_rcfile(g->client_rcfile, "mondoarchive_params")); 205 205 sprintf(tmp, "mondoarchive_params --> '%s'", mondoparams_str); 206 206 log_it(debug, tmp); … … 342 342 return(1); 343 343 } 344 strcpy(mondoparams_str, get_param_from_rcfile( CLIENT_RCFILE, "mondoarchive_params"));344 strcpy(mondoparams_str, get_param_from_rcfile(g->client_rcfile, "mondoarchive_params")); 345 345 sprintf(tmp, "mondoarchive_params --> '%s'", mondoparams_str); 346 346 log_it(debug, tmp); … … 434 434 char tmp[MAX_STR_LEN+1]; 435 435 436 for(server_port = 8700; server_port < 8710; server_port++)437 {438 sin->sin_port = htons(server_port);439 436 if ((*p_s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) 440 437 { 441 perror("socket"); 438 // perror("socket"); 439 log_it(info, "Creating socket failed: %s", strerror(errno)); 442 440 return(-1); 443 441 } 442 for(server_port = 8700; server_port < 8710; server_port++) 443 { 444 sin->sin_port = htons(server_port); 444 445 if (connect(*p_s, (struct sockaddr*)sin, sizeof(struct sockaddr_in)) < 0) 445 446 { … … 450 451 return(server_port); 451 452 } 453 close(*p_s); 452 454 return(-1); 453 455 } … … 1249 1251 // pthread_t thread; 1250 1252 1253 parse_options(argc, argv); 1254 1251 1255 log_it(info, "---------- Monitas (client) by Hugo Rabson ----------"); 1252 1256 register_pid(getpid(), "client"); … … 1284 1288 } 1285 1289 1286 // set_param_in_rcfile( CLIENT_RCFILE, "mondoarchive_params", "-1 -L");1290 // set_param_in_rcfile(g->client_rcfile, "mondoarchive_params", "-1 -L"); 1287 1291 log_it(debug, "Awaiting commands from FIFO"); 1288 create_and_watch_fifo_for_commands( CLIENT_COMDEV);1292 create_and_watch_fifo_for_commands(g->client_comdev); 1289 1293 logout_and_exit(g_server_name); 1290 1294 exit(0); -
branches/stable/monitas/common.c
r352 r353 27 27 28 28 #include "structs.h" 29 #include <stdarg.h> 30 31 #define CLIENT_RCFILE "/root/.monitas-client.rc" 32 #define CLIENT_COMDEV "/var/spool/monitas/client-input.dev" 33 #define CLIENT_LOGFILE "/var/log/monitas-client.log" 34 #define SERVER_COMDEV "/var/spool/monitas/server-input.dev" 35 #define SERVER_STATUS_FILE "/var/spool/monitas/server-status.txt" 36 #define SERVER_LOGFILE "/var/log/monitas-server.log" 29 37 30 38 … … 43 51 int create_and_watch_fifo_for_commands(char*); 44 52 int get_bkgd_prog_result(pthread_t*); 45 void log_it_SUB(char*, t_loglevel, char*);53 //void log_it_SUB(char*, t_loglevel, char*); 46 54 bool program_still_running(char*); 47 55 int receive_file_from_socket(FILE*fout, int socket_fd); … … 52 60 void termination_in_progress(int sig); 53 61 int transmit_file_to_socket(FILE*fin, int socket_fd); 62 char *my_basename(char *path); 63 int parse_options(int argc, char *argv[]); 64 65 /* global vars */ 66 67 /* global (semi-) static data. 68 * Always use following pointer to access this struct!! 69 * Struct is initialized by parse_options() */ 70 static struct s_globaldata _global_data; 71 /* Global Pointer to access the (semi-) static data */ 72 struct s_globaldata *g = & _global_data; 54 73 55 74 … … 73 92 74 93 res = pthread_create(thread, NULL, call_prog_in_bkgd_SUB, (void*)command); 75 if (res) { log_it(fatal, "Unable to call program in background - thread creation failure "); }94 if (res) { log_it(fatal, "Unable to call program in background - thread creation failure %s", command); } 76 95 log_it(debug, "Done creating pthread. Will continue now."); 77 96 } … … 90 109 Return: None 91 110 */ 92 char*command;93 111 int res; 94 char tmp[MAX_STR_LEN+1]; 95 static char result_str[MAX_STR_LEN+1]; 96 97 command=(char*)cmd; 98 sprintf(tmp, "Calling '%s' in background pthread", command); 99 log_it(debug, tmp); 100 res = system(command); 101 sprintf(tmp, "'%s' is returning from bkgd process - res=%d", command, res); 102 log_it(debug, tmp); 103 sprintf(result_str, "%d", res); 104 pthread_exit((void*)result_str); 112 113 log_it(debug, "Calling '%s' in background pthread", cmd); 114 res = system(cmd); 115 log_it(debug, "'%s' is returning from bkgd process - res=%d", cmd, res); 116 pthread_exit((void*)res); 105 117 } 106 118 … … 119 131 */ 120 132 { 121 char tmp[MAX_STR_LEN+1],incoming[MAX_STR_LEN+1];133 char incoming[MAX_STR_LEN+1]; 122 134 int res=0, fd, len, pos=0; 123 135 124 136 res = make_hole_for_file(devpath) + mkfifo(devpath, 700); 125 if (res) { log_it(error, "Unable to prepare command FIFO "); return(res); }137 if (res) { log_it(error, "Unable to prepare command FIFO %s", devpath); return(res); } 126 138 strncpy(g_command_fifo, devpath, MAX_STR_LEN); 127 if (!(fd = open(g_command_fifo, O_RDONLY))) { log_it(error, "Unable to openin command FIFO "); return(1); }139 if (!(fd = open(g_command_fifo, O_RDONLY))) { log_it(error, "Unable to openin command FIFO %s", g_command_fifo); return(1); } 128 140 // log_it(info, "Awaiting commands from FIFO"); 129 141 for(;;) … … 140 152 if (res) 141 153 { 142 sprintf(tmp, "%s <-- errors occurred during processing", incoming); 143 log_it(error, tmp); 154 log_it(error, "%s <-- errors occurred during processing", incoming); 144 155 } 145 156 else 146 157 { 147 sprintf(tmp, "%s <-- command received OK", incoming); 148 log_it(info, tmp); 158 log_it(info, "%s <-- command received OK", incoming); 149 159 } 150 160 } … … 168 178 */ 169 179 { 170 void* vp;171 void**pvp;180 void* vp; 181 // void**pvp; 172 182 int res; 173 char tmp[MAX_STR_LEN+1],*p, result_str[MAX_STR_LEN+1];174 175 pvp=&vp;183 // char *p, result_str[MAX_STR_LEN+1]; 184 185 /* pvp=&vp; 176 186 vp=(void*)result_str; 177 187 strcpy(result_str, "(uninitialized)"); … … 181 191 p = (char*)vp; 182 192 res = atoi(p); 183 sprintf(tmp, "pthread res = %d ('%s')", res, p); 184 log_it(debug, tmp); 193 log_it(debug, "pthread res = %d ('%s')", res, p); 194 */ 195 log_it(debug, "Waiting for bkgd prog to finish, so that we can get its result"); 196 if ((res = pthread_join(*thread, &vp)) != 0) 197 { 198 log_it(fatal, "pthread_join failed with error %s", 199 ((res==ESRCH) ? "ESRCH - No thread could be found corresponding to that specified by 'thread'" : 200 ((res==EINVAL) ? "EINVAL - The thread has been detached OR Another thread is already waiting on termination of it." : 201 ((res==EDEADLK) ? "EDEADLK - The 'tread' argument refers to the calling thread." : 202 "UNSPECIFIED")))); 203 } 185 204 *thread = 0; 205 res = (int) vp; 206 log_it(debug, "pthread res = %d ('%s')", res, (res>256 || res<-256) ? (char*)vp : ""); 186 207 return(res); 187 208 } … … 201 222 */ 202 223 { 203 char sz_outstr[MAX_STR_LEN], sz_level[MAX_STR_LEN], sz_time[MAX_STR_LEN];224 char *sz_level = ""; 204 225 time_t time_rec; 226 struct tm tm; 205 227 FILE*fout; 206 228 207 229 time(&time_rec); 230 localtime_r(&time_rec, &tm); 208 231 switch(level) 209 232 { 210 case debug: strcpy(sz_level, "DEBUG"); break; 211 case info: strcpy(sz_level, "INFO "); break; 212 case warn: strcpy(sz_level, "WARN "); break; 213 case error: strcpy(sz_level, "ERROR"); break; 214 case fatal: strcpy(sz_level, "FATAL"); break; 215 default: strcpy(sz_level, "UNKWN"); break; 216 } 217 sprintf(sz_time, ctime(&time_rec)); 218 sz_time[strlen(sz_time)-6] = '\0'; 219 sprintf(sz_outstr, "%s %s: %s", sz_time+11, sz_level, sz_message); 233 case debug: sz_level = "DEBUG"; break; 234 case info: sz_level = "INFO "; break; 235 case warn: sz_level = "WARN "; break; 236 case error: sz_level = "ERROR"; break; 237 case fatal: sz_level = "FATAL"; break; 238 default: sz_level = "UNKWN"; break; 239 } 220 240 if ((int)level >= LOG_THESE_AND_HIGHER) 221 241 { 222 while (!(fout=fopen(logfile,"a"))) { usleep((int)(random()%32768)); } 223 fprintf(fout, "%s\n",sz_outstr); 242 while (!(fout=fopen(logfile,"a"))) 243 { usleep((int)(random()%32768)); } 244 fprintf(fout, "%02d:%02d:%02d %s: %s\n", tm.tm_hour, tm.tm_min, tm.tm_sec, sz_level, sz_message); 224 245 fclose(fout); 225 246 } 226 247 if (level==fatal) 227 248 { 228 fprintf(stderr, "Aborting now. See %s for more information.\n", g_logfile); 249 fprintf(stderr, "Aborting now. See %s for more information.\n", logfile); 250 terminate_daemon(0); 251 exit(1); 252 } 253 } 254 255 256 257 /*-----------------------------------------------------------*/ 258 259 260 261 void logToFile(char *logfile, t_loglevel level, char *filename, char *lineno, char *funcname, char *sz_message, ...) 262 /* 263 Purpose: Log <sz_message> and its <level> of severity to <logfile> 264 Params: logfile name of logfile 265 level level of severity (debug/info/warn/error/fatal) 266 sz_message message/event [string] 267 for <level>==debug the location of the event is specified by: 268 filename name of the file, where <sz_message> was created 269 lineno number of the line [string], where <sz_message> was created 270 funcname name of the function, that created the event 271 Return: None 272 */ 273 { 274 char *sz_level = ""; 275 time_t time_rec; 276 struct tm tm; 277 FILE *fout; 278 va_list ap; 279 280 281 time(&time_rec); 282 va_start(ap, sz_message); // initialize the variable arguments 283 localtime_r(&time_rec, &tm); 284 switch(level) 285 { 286 case debug: 287 { 288 // sz_level = "DEBUG"; break; 289 // at debug level we add filename, lineno and functionname instead of "DEBUG" 290 // i.e. "file.c:123, subfunction()" 291 char *fmt = "%s:%s, %s()"; 292 // calculate length of resulting string: 293 size_t size = strlen(fmt) -(3*2) + 1 // length of fmt-string minus 3x 2 chars ("%s") plus 1 for terminating \0' 294 + strlen(filename) // plus length of filename 295 + strlen(lineno) // plus length of linenumber 296 + strlen(funcname); // plus length of functionname 297 sz_level = alloca(size); // memory is automatically free'd when this function returns 298 snprintf(sz_level, size, "%s:%s, %s()", filename, lineno, funcname); 299 } break; 300 case info: sz_level = "INFO "; break; 301 case warn: sz_level = "WARN "; break; 302 case error: sz_level = "ERROR"; break; 303 case fatal: sz_level = "FATAL"; break; 304 default: sz_level = "UNKWN"; break; 305 } 306 if ((int)level >= LOG_THESE_AND_HIGHER) 307 { 308 while (!(fout=fopen(logfile,"a"))) 309 { usleep((int)(random()%32768)); } 310 fprintf(fout, "%02d:%02d:%02d %s: ", tm.tm_hour, tm.tm_min, tm.tm_sec, sz_level); // print time and level 311 vfprintf(fout, sz_message, ap); // print message (and further args) 312 fprintf(fout, "\n"); 313 fclose(fout); 314 } 315 va_end(ap); // finalize the variable arguments 316 if (level==fatal) 317 { 318 fprintf(stderr, "Aborting now. See %s for more information.\n", logfile); 229 319 terminate_daemon(0); 230 320 exit(1); … … 428 518 log_it(debug, "Termination in progress"); 429 519 usleep(1000); 430 pthread_exit( 0);520 pthread_exit(NULL); 431 521 } 432 522 … … 539 629 540 630 541 542 543 544 545 631 /*-----------------------------------------------------------*/ 632 633 634 /* 635 Purpose: strip directory from filenames 636 Params: path like: "dir1/dir2/file" 637 Return: result pointer to the first character behind the last '/' 638 */ 639 char *my_basename(char *path) 640 { 641 char *name; 642 if (path == NULL) 643 return (NULL); 644 name = strrchr(path, '/'); 645 if (name == NULL) 646 return (path); /* only a filename was given */ 647 else 648 return (++name); /* skip the '/' and return pointer to next char */ 649 } 650 651 652 653 654 /*-----------------------------------------------------------*/ 655 656 657 /* 658 Purpose: analyse command line arguments and write them to global struct 659 Params: argc number of arguments 660 argv arguments as array of strings 661 Return: result (0=success, nonzero=failure) 662 */ 663 int parse_options(int argc, char *argv[]) 664 { 665 // initialize the global data structure 666 memset( g, 0, sizeof(struct s_globaldata)); 667 668 // we use malloc() here for default strings, as we wanna use free() later 669 // when changing the value. free()ing mem in the text segment would be a 670 // bad idea... 671 672 /* Initialize default values */ 673 #define INIT_DEFAULT_STRING(var, string) \ 674 if ( ((var) = (char*) malloc(strlen(string)+1)) != NULL ) \ 675 strcpy((var), string) 676 677 INIT_DEFAULT_STRING( g->client_rcfile, CLIENT_RCFILE ); 678 INIT_DEFAULT_STRING( g->client_comdev, CLIENT_COMDEV ); 679 INIT_DEFAULT_STRING( g->server_comdev, SERVER_COMDEV ); 680 INIT_DEFAULT_STRING( g->server_status_file, SERVER_STATUS_FILE ); 681 682 g->loglevel = LOG_THESE_AND_HIGHER; // log messages with level >= loglevel to logfile 683 684 // evaluate if we're server or client: 685 // We decide it by looking at the name of the running program 686 if (strstr(my_basename(argv[0]), "server") != NULL) 687 { // the server may be named 'server', 'monitos-server', 'monserver_start', ... 688 INIT_DEFAULT_STRING( g->logfile, SERVER_LOGFILE ); 689 } 690 else if (strstr(my_basename(argv[0]), "client") != NULL) 691 { // the client may be named 'client', 'monitos-client', 'monclient_start', ... 692 INIT_DEFAULT_STRING( g->logfile, CLIENT_LOGFILE ); 693 } 694 else /* this should never happen */ 695 { 696 fprintf(stderr, "Don't know if we're running as server or client, when started as %s", my_basename(argv[0])); 697 INIT_DEFAULT_STRING( g->logfile, "/var/log/monitas.log" ); 698 } 699 700 #undef INIT_DEFAULT_STRING 701 return 0; 702 } 703 704 705 706 707 708 709 -
branches/stable/monitas/from-mondo.c
r352 r353 8 8 9 9 10 extern void log_it_SUB(char*, t_loglevel, char*);10 //extern void log_it_SUB(char*, t_loglevel, char*); 11 11 char *call_program_and_get_last_line_of_output(char*); 12 12 int call_program_and_log_output (char *); … … 17 17 18 18 19 extern char g_logfile[MAX_STR_LEN+1];20 19 21 20 … … 215 214 /************************************************************************* 216 215 * strip_spaces() -- Hugo Rabson * 217 * * 218 * Purpose: * 216 * rewitten Stefan Huebner * 217 * * 218 * Purpose: remove leading whitespaces and control characters, replace * 219 * TABs by SPACES, remove previous char when followed with BS,* 220 * skip control chars, terminate string with '\0' when CR or * 221 * NL is detected * 222 * afterwards remove trailing whitespace * 219 223 * Called by: * 220 224 * Returns: * … … 223 227 strip_spaces (char *in_out) 224 228 { 229 char *r, *w; 230 r = w = in_out; // initialize read and write pointer 231 232 while ( *r && *r <= ' ' ) 233 ++r; // skip leading whitespace and control chars 234 235 for ( ; *r ; ++r ) // until end of existing string do: 236 { 237 if (*r == '\t') // TAB 238 { *w++ = ' '; // write SPACE instead 239 continue; } 240 if (*r == (char) 8) // BACKSPACE 241 { if (w > in_out) --w; // remove previous character (if any ;-) 242 continue; } 243 if (*r == '\n' || *r == '\r') // CR and LF 244 break; // terminate copying 245 if (*r < ' ') // Control chars 246 continue; // are ignored (skipped) 247 *w++ = *r; // all other chars are copied 248 } 249 // all characters are copied 250 *w = '\0'; // terminate the copied string 251 for (--w; w>in_out && *w == ' '; --w) 252 *w = '\0'; // remove trailing whitespaces 253 return; 225 254 /** buffers ******************************************************/ 226 char tmp[MAX_STR_LEN+1];255 // char tmp[MAX_STR_LEN+1]; 227 256 228 257 /** pointers *****************************************************/ 229 char *p;258 // char *p; 230 259 231 260 /** int *********************************************************/ 232 int i;261 // int i; 233 262 234 263 /** end vars ****************************************************/ 235 for (i = 0; in_out[i] <= ' ' && i < strlen (in_out); i++);264 /* for (i = 0; in_out[i] <= ' ' && i < strlen (in_out); i++); 236 265 strcpy (tmp, in_out + i); 237 266 for (i = strlen (tmp); i>0 && tmp[i - 1] <= 32; i--); … … 277 306 } 278 307 in_out[i] = '\0'; 308 */ 279 309 /* for(i=strlen(in_out); i>0 && in_out[i-1]<=32; i--) {in_out[i-1]='\0';} */ 280 310 } -
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); -
branches/stable/monitas/structs.h
r352 r353 1 1 /* structs.h 2 2 3 07/17 4 - add central global struct 3 5 4 6 06/16 5 - added CLIENT_RCFILE 7 - added CLIENT_RCFILE [moved to common.c on 07/17] 6 8 - added .bodyAux to msg record structure 7 9 8 10 06/14 9 - added SERVER_COMDEV and CLIENT_COMDEV 11 - added SERVER_COMDEV and CLIENT_COMDEV [moved to common.c on 07/17] 10 12 11 13 06/11 … … 45 47 46 48 47 #define log_it(x,y) { log_it_SUB(g_logfile,x,y); } 49 /* moved to common.c : 48 50 #define CLIENT_RCFILE "/root/.monitas-client.rc" 49 51 #define CLIENT_COMDEV "/var/spool/monitas/client-input.dev" 50 52 #define SERVER_COMDEV "/var/spool/monitas/server-input.dev" 51 53 #define SERVER_STATUS_FILE "/var/spool/monitas/server-status.txt" 54 */ 52 55 #define MAX_STR_LEN 510 53 56 #define MAX_PENDING 8 /* silently limited to 5 by BSD, says a Linux manual */ … … 56 59 #define XFER_BUF_SIZE 16384 /* for sending archives between client and server */ 57 60 /* NB: 32768 doesn't work; don't ask me why */ 61 58 62 typedef enum { false=0, true=1} bool; 59 63 typedef enum { unused=0, login, logout, ping, pong, login_ok, logout_ok, … … 63 67 begin_stream, end_stream, user_req, progress_rpt } t_msg; 64 68 typedef enum { debug, info, warn, error, fatal } t_loglevel; 69 70 71 //#define log_it(x,y) { log_it_SUB(g_logfile,x,y); } 72 #ifndef __XSTR 73 # define __XSTR(x) _XSTR(x) 74 # define _XSTR(x) #x 75 #endif 76 #define log_it(lvl,msg,args...) { logToFile(g->logfile,lvl,__FILE__,__XSTR(__LINE__),__FUNCTION__,msg , ## args); } 77 extern void logToFile(char *logfile, t_loglevel level, char *filename, char *lineno, char *funcname, char *sz_message, ...); 78 79 80 /* 81 * Global structure for all data that doesn't change at runtime. 82 * The data may be modified at program start via command line options, 83 * or -not implemented yet- via environment variables or server-client 84 * communication. 85 */ 86 struct s_globaldata 87 { 88 char *client_rcfile; // path+name of client's rc file 89 char *client_comdev; // path+name of client's 90 char *server_comdev; // path+name of server's 91 char *server_status_file; // path+name of server's 92 93 char *logfile; // path+name of logfile 94 t_loglevel loglevel; // lowest level of msg written in the logfile 95 }; 96 /* 97 * there is one (1) global instance of above struct [defined in common.c] and 98 * one global pointer [also in common.c] to address the struct inside all functions. 99 * The pointer is declared here as external to permit the global access. 100 */ 101 extern struct s_globaldata *g; 102 103 65 104 66 105
Note:
See TracChangeset
for help on using the changeset viewer.