Changeset 353 in MondoRescue for branches/stable/monitas/common.c
- Timestamp:
- Jan 28, 2006, 6:42:59 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.