[352] | 1 | /* common.c - functions common to server.c and client.c
|
---|
| 2 |
|
---|
| 3 |
|
---|
| 4 |
|
---|
| 5 | 06/19
|
---|
| 6 | - fixed silly bug in create_and_watch_fifo_for_commands()
|
---|
| 7 |
|
---|
| 8 | 06/16
|
---|
| 9 | - added program_still_running()
|
---|
| 10 |
|
---|
| 11 | 06/14
|
---|
| 12 | - added create_and_watch_fifo_for_commands() from server.c
|
---|
| 13 | - if log_it(fatal,...) then call terminate_daemon()
|
---|
| 14 |
|
---|
| 15 | 06/11
|
---|
| 16 | - added register_pid(), set_signals()
|
---|
| 17 | - commented code a bit
|
---|
| 18 |
|
---|
| 19 | 06/10
|
---|
| 20 | - create function to call external executable in background
|
---|
| 21 | - create function to wait for it to terminate & to grab its result
|
---|
| 22 | - put them in common.c
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 | #include "structs.h"
|
---|
[353] | 29 | #include <stdarg.h>
|
---|
[352] | 30 |
|
---|
[353] | 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"
|
---|
[352] | 37 |
|
---|
[353] | 38 |
|
---|
[352] | 39 | extern char g_command_fifo[MAX_STR_LEN+1];
|
---|
| 40 | extern char g_logfile[MAX_STR_LEN+1];
|
---|
| 41 | extern char *call_program_and_get_last_line_of_output(char*);
|
---|
| 42 | extern int call_program_and_log_output (char *);
|
---|
| 43 | extern bool does_file_exist (char *);
|
---|
| 44 | extern int make_hole_for_file (char *outfile_fname);
|
---|
| 45 | extern int process_incoming_command(char*);
|
---|
| 46 | extern void terminate_daemon(int);
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | void call_program_in_background(pthread_t*, char*);
|
---|
| 50 | void *call_prog_in_bkgd_SUB(void*);
|
---|
| 51 | int create_and_watch_fifo_for_commands(char*);
|
---|
| 52 | int get_bkgd_prog_result(pthread_t*);
|
---|
[353] | 53 | //void log_it_SUB(char*, t_loglevel, char*);
|
---|
[352] | 54 | bool program_still_running(char*);
|
---|
| 55 | int receive_file_from_socket(FILE*fout, int socket_fd);
|
---|
| 56 | void register_pid(pid_t, char*);
|
---|
| 57 | void set_signals(bool);
|
---|
| 58 | char *tmsg_to_string(t_msg msg_type);
|
---|
| 59 | int read_block_from_fd(int, char*, int);
|
---|
| 60 | void termination_in_progress(int sig);
|
---|
| 61 | int transmit_file_to_socket(FILE*fin, int socket_fd);
|
---|
[353] | 62 | char *my_basename(char *path);
|
---|
| 63 | int parse_options(int argc, char *argv[]);
|
---|
[352] | 64 |
|
---|
[353] | 65 | /* global vars */
|
---|
[352] | 66 |
|
---|
[353] | 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;
|
---|
[352] | 73 |
|
---|
[353] | 74 |
|
---|
| 75 |
|
---|
[352] | 76 | /*-----------------------------------------------------------*/
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | void call_program_in_background(pthread_t *thread, char*command)
|
---|
| 81 | /*
|
---|
| 82 | Purpose:Execute external binary in a background thread created
|
---|
| 83 | specially for this purpose. Return. Leave binary running.
|
---|
| 84 | Params: thread - [returned] thread running in background
|
---|
| 85 | command - binary call to be executed
|
---|
| 86 | Return: 0 if prog started ok; nonzero if failure
|
---|
| 87 | NB: Use get_bkgd_prog_result() to wait for binary to terminate
|
---|
| 88 | and to get binary's result (zero or nonzero)
|
---|
| 89 | */
|
---|
| 90 | {
|
---|
| 91 | int res;
|
---|
| 92 |
|
---|
| 93 | res = pthread_create(thread, NULL, call_prog_in_bkgd_SUB, (void*)command);
|
---|
[353] | 94 | if (res) { log_it(fatal, "Unable to call program in background - thread creation failure %s", command); }
|
---|
[352] | 95 | log_it(debug, "Done creating pthread. Will continue now.");
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 | /*-----------------------------------------------------------*/
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | void *call_prog_in_bkgd_SUB(void*cmd)
|
---|
| 105 | {
|
---|
| 106 | /*
|
---|
| 107 | Purpose:Subroutine of call_program_in_background()
|
---|
| 108 | Params: cmd - binary call to be executed
|
---|
| 109 | Return: None
|
---|
| 110 | */
|
---|
| 111 | int res;
|
---|
| 112 |
|
---|
[353] | 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);
|
---|
[352] | 117 | }
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | /*-----------------------------------------------------------*/
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | int create_and_watch_fifo_for_commands(char*devpath)
|
---|
| 126 | /*
|
---|
| 127 | Purpose:Create a FIFO (device). Open it. Read commands from it
|
---|
| 128 | if/when they're echoed to it (e.g. backup, restore, ...)
|
---|
| 129 | Params: devpath - path+filename of FIFO to create
|
---|
| 130 | Return: result (nonzero=failure; else, infinite loop & notional 0)
|
---|
| 131 | */
|
---|
| 132 | {
|
---|
[353] | 133 | char incoming[MAX_STR_LEN+1];
|
---|
[352] | 134 | int res=0, fd, len, pos=0;
|
---|
| 135 |
|
---|
| 136 | res = make_hole_for_file(devpath) + mkfifo(devpath, 700);
|
---|
[353] | 137 | if (res) { log_it(error, "Unable to prepare command FIFO %s", devpath); return(res); }
|
---|
[352] | 138 | strncpy(g_command_fifo, devpath, MAX_STR_LEN);
|
---|
[353] | 139 | if (!(fd = open(g_command_fifo, O_RDONLY))) { log_it(error, "Unable to openin command FIFO %s", g_command_fifo); return(1); }
|
---|
[352] | 140 | // log_it(info, "Awaiting commands from FIFO");
|
---|
| 141 | for(;;)
|
---|
| 142 | {
|
---|
| 143 | len=read(fd, incoming+pos, 1);
|
---|
| 144 | if (len==0) { usleep(1000); continue; }
|
---|
| 145 | pos+=len;
|
---|
| 146 | incoming[pos]='\0';
|
---|
| 147 | if (incoming[pos-1]=='\n' || pos>=MAX_STR_LEN)
|
---|
| 148 | {
|
---|
| 149 | incoming[pos-1]='\0';
|
---|
| 150 | len=pos=0;
|
---|
| 151 | res=process_incoming_command(incoming);
|
---|
| 152 | if (res)
|
---|
| 153 | {
|
---|
[353] | 154 | log_it(error, "%s <-- errors occurred during processing", incoming);
|
---|
[352] | 155 | }
|
---|
| 156 | else
|
---|
| 157 | {
|
---|
[353] | 158 | log_it(info, "%s <-- command received OK", incoming);
|
---|
[352] | 159 | }
|
---|
| 160 | }
|
---|
| 161 | } // forever
|
---|
| 162 | return(0);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 |
|
---|
| 166 | /*-----------------------------------------------------------*/
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 |
|
---|
| 170 | int get_bkgd_prog_result(pthread_t *thread)
|
---|
| 171 | /*
|
---|
| 172 | Purpose:Wait for external binary (running in background) to terminate.
|
---|
| 173 | Discover how it ended (w/success or w/failure?).
|
---|
| 174 | Params: thread - thread running in background
|
---|
| 175 | Return: result (0=success, nonzero=failure)
|
---|
| 176 | NB: Binary was executed by call_program_in_background(). This
|
---|
| 177 | subroutine should be called sometime after that call.
|
---|
| 178 | */
|
---|
| 179 | {
|
---|
[353] | 180 | void* vp;
|
---|
| 181 | // void**pvp;
|
---|
[352] | 182 | int res;
|
---|
[353] | 183 | // char *p, result_str[MAX_STR_LEN+1];
|
---|
[352] | 184 |
|
---|
[353] | 185 | /* pvp=&vp;
|
---|
[352] | 186 | vp=(void*)result_str;
|
---|
| 187 | strcpy(result_str, "(uninitialized)");
|
---|
| 188 | log_it(debug, "Waiting for bkgd prog to finish, so that we can get its result");
|
---|
| 189 | if (pthread_join(*thread, pvp)) { log_it(fatal, "pthread_join failed"); }
|
---|
| 190 | p = result_str;
|
---|
| 191 | p = (char*)vp;
|
---|
| 192 | res = atoi(p);
|
---|
[353] | 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 | }
|
---|
[352] | 204 | *thread = 0;
|
---|
[353] | 205 | res = (int) vp;
|
---|
| 206 | log_it(debug, "pthread res = %d ('%s')", res, (res>256 || res<-256) ? (char*)vp : "");
|
---|
[352] | 207 | return(res);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 |
|
---|
| 211 |
|
---|
| 212 | /*-----------------------------------------------------------*/
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 |
|
---|
| 216 | void log_it_SUB(char *logfile, t_loglevel level, char *sz_message)
|
---|
| 217 | /*
|
---|
| 218 | Purpose:Log event and its level of severity.
|
---|
| 219 | Params: level - level of severity (debug/info/warn/error/fatal)
|
---|
| 220 | sz_message - message/event [string]
|
---|
| 221 | Return: None
|
---|
| 222 | */
|
---|
| 223 | {
|
---|
[353] | 224 | char *sz_level = "";
|
---|
[352] | 225 | time_t time_rec;
|
---|
[353] | 226 | struct tm tm;
|
---|
[352] | 227 | FILE*fout;
|
---|
| 228 |
|
---|
| 229 | time(&time_rec);
|
---|
[353] | 230 | localtime_r(&time_rec, &tm);
|
---|
[352] | 231 | switch(level)
|
---|
| 232 | {
|
---|
[353] | 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;
|
---|
[352] | 239 | }
|
---|
| 240 | if ((int)level >= LOG_THESE_AND_HIGHER)
|
---|
| 241 | {
|
---|
[353] | 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);
|
---|
[352] | 245 | fclose(fout);
|
---|
| 246 | }
|
---|
| 247 | if (level==fatal)
|
---|
| 248 | {
|
---|
[353] | 249 | fprintf(stderr, "Aborting now. See %s for more information.\n", logfile);
|
---|
[352] | 250 | terminate_daemon(0);
|
---|
| 251 | exit(1);
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 |
|
---|
| 256 |
|
---|
| 257 | /*-----------------------------------------------------------*/
|
---|
| 258 |
|
---|
| 259 |
|
---|
| 260 |
|
---|
[353] | 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);
|
---|
| 319 | terminate_daemon(0);
|
---|
| 320 | exit(1);
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 |
|
---|
| 325 |
|
---|
| 326 | /*-----------------------------------------------------------*/
|
---|
| 327 |
|
---|
| 328 |
|
---|
| 329 |
|
---|
[352] | 330 | bool program_still_running(char*command)
|
---|
| 331 | {
|
---|
| 332 | char comm_sub[MAX_STR_LEN+1],
|
---|
| 333 | tmp[MAX_STR_LEN+1];
|
---|
| 334 | strncpy(comm_sub, command, 40);
|
---|
| 335 | comm_sub[40] = '\0';
|
---|
| 336 | sprintf(tmp, "ps ax | grep \"%s\" | grep -v \"ps ax\" | grep -v \"grep \"", comm_sub);
|
---|
| 337 | if (call_program_and_log_output(tmp))
|
---|
| 338 | { return(false); }
|
---|
| 339 | else
|
---|
| 340 | { return(true); }
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 |
|
---|
| 344 |
|
---|
| 345 | /*-----------------------------------------------------------*/
|
---|
| 346 |
|
---|
| 347 |
|
---|
| 348 |
|
---|
| 349 | int read_block_from_fd(int socket_fd, char*buf, int len_to_read)
|
---|
| 350 | /*
|
---|
| 351 | Purpose:Read N bytes from socket, into buffer
|
---|
| 352 | Params: socket_fd - socket to be read from
|
---|
| 353 | buf - buffer where incoming data will be stored
|
---|
| 354 | len_to_read - bytes to be read from socket
|
---|
| 355 | Return: bytes read
|
---|
| 356 | NB: if bytes read < len_to_read then there is
|
---|
| 357 | probably something wrong (e.g. EOF of pipe)
|
---|
| 358 | */
|
---|
| 359 | {
|
---|
| 360 | int length, noof_tries, i=0;
|
---|
| 361 | char tmp[MAX_STR_LEN+1];
|
---|
| 362 |
|
---|
| 363 | // log_it(debug, "Reading from pipe");
|
---|
| 364 | for(length=noof_tries=0; length<len_to_read && noof_tries<10; length+=i)
|
---|
| 365 | {
|
---|
| 366 | i = read(socket_fd, buf+length, len_to_read-length);
|
---|
| 367 | if (i==0) { noof_tries++; }
|
---|
| 368 | if (i<0) { log_it(error, "Error while reading from fd"); return(-1); }
|
---|
| 369 | }
|
---|
| 370 | if (length != len_to_read) { log_it(error, "Unable to continue reading from fd"); }
|
---|
| 371 | sprintf(tmp, "%d of %d bytes read from fd", length, len_to_read);
|
---|
| 372 | return(length);
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 |
|
---|
| 376 |
|
---|
| 377 | /*-----------------------------------------------------------*/
|
---|
| 378 |
|
---|
| 379 |
|
---|
| 380 |
|
---|
| 381 | int receive_file_from_socket(FILE*fout, int socket_fd)
|
---|
| 382 | /*
|
---|
| 383 | Purpose:Receive file from socket and save to disk / stream.
|
---|
| 384 | Params: socket_fd - file descriptor of socket
|
---|
| 385 | fout - file descriptor of output file
|
---|
| 386 | Return: result (0=success, nonzero=failure)
|
---|
| 387 | NB: socket_fd was created with open() but
|
---|
| 388 | fopen was created with fopen(). Different, see? Nyah.
|
---|
| 389 | */
|
---|
| 390 | {
|
---|
| 391 | int res=0, length, rotor=0, incoming_len;
|
---|
| 392 | char tmp[MAX_STR_LEN+1], buf[XFER_BUF_SIZE];
|
---|
| 393 | bool done;
|
---|
| 394 | long long total_length=0;
|
---|
| 395 |
|
---|
| 396 | signal(SIGPIPE, SIG_IGN);
|
---|
| 397 |
|
---|
| 398 | length=99999;
|
---|
| 399 | // sleep(1);
|
---|
| 400 | for(done=false; !done; )
|
---|
| 401 | {
|
---|
| 402 | // usleep(1000);
|
---|
| 403 | length = read_block_from_fd(socket_fd, buf, 3);
|
---|
| 404 | if (length<3) { log_it(debug, "Failed to read header block from fd"); res++; done=true; continue; }
|
---|
| 405 | rotor=(rotor%127)+1;
|
---|
| 406 | sprintf(tmp, "Rotor is %d (I was expecting %d)", buf[0], rotor);
|
---|
| 407 | if (buf[0]==0 && buf[1]==0x7B && buf[2]==0x21) { log_it(debug, "OK, end of stream. Cool."); done=true; continue; }
|
---|
| 408 | // log_it(debug, tmp);
|
---|
| 409 | if (buf[0]!=rotor) { log_it(debug, "Rotors do not match"); res++; break; }
|
---|
| 410 | incoming_len = buf[2];
|
---|
| 411 | if (incoming_len < 0) { incoming_len += 256; }
|
---|
| 412 | incoming_len += buf[1] * 256;
|
---|
| 413 | // memcpy((char*)&incoming_len, buf+1, 2);
|
---|
| 414 | sprintf(tmp, "Expecting %d bytes in block #%d", incoming_len, rotor);
|
---|
| 415 | // log_it(debug, tmp);
|
---|
| 416 | length = read_block_from_fd(socket_fd, buf, incoming_len);
|
---|
| 417 | /*
|
---|
| 418 | for(length=0,i=0; length<incoming_len; length+=i)
|
---|
| 419 | {
|
---|
| 420 | i = read(socket_fd, buf+length, incoming_len-length);
|
---|
| 421 | if (length<0) { log_it(debug, "Unable to read block in from socket_fd"); done=true; }
|
---|
| 422 | else if (length==0) { log_it(debug, "Zero-length block arrived from socket"); }
|
---|
| 423 | sprintf(tmp, "Read %d bytes from socket", i);
|
---|
| 424 | // log_it(debug, tmp);
|
---|
| 425 | }
|
---|
| 426 | */
|
---|
| 427 | if (length != incoming_len) { log_it(debug, "Error reading from socket"); res++; }
|
---|
| 428 | if (fwrite(buf, 1, length, fout)!=length) { log_it(debug, "Error writing data to output file"); res++; }
|
---|
| 429 | sprintf(tmp, "Written %d bytes to output stream", length);
|
---|
| 430 | total_length += length;
|
---|
| 431 | // log_it(debug, tmp);
|
---|
| 432 | }
|
---|
| 433 | if (total_length==0) { res++; log_it(debug, "Zero-length file received. Silly. I say that's an error."); }
|
---|
| 434 |
|
---|
| 435 | signal(SIGPIPE, terminate_daemon);
|
---|
| 436 |
|
---|
| 437 | return(res);
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 |
|
---|
| 441 |
|
---|
| 442 |
|
---|
| 443 | /*-----------------------------------------------------------*/
|
---|
| 444 |
|
---|
| 445 |
|
---|
| 446 |
|
---|
| 447 | void register_pid(pid_t pid, char*name_str)
|
---|
| 448 | /*
|
---|
| 449 | Purpose:Register executable's PID in /var/run/monitas-[name_str].pid;
|
---|
| 450 | store [pid] in data file
|
---|
| 451 | Params: pid - process ID to be stored in data file
|
---|
| 452 | name_str - name (e.g. "client" or "server") to be included
|
---|
| 453 | in the data file name of the PID locking file
|
---|
| 454 | Return: None
|
---|
| 455 | NB: Use pid=0 to delete the lock file and unregister the PID
|
---|
| 456 | */
|
---|
| 457 | {
|
---|
| 458 | char tmp[MAX_STR_LEN+1], lockfile_fname[MAX_STR_LEN+1];
|
---|
| 459 | int res;
|
---|
| 460 | FILE*fin;
|
---|
| 461 |
|
---|
| 462 | sprintf(lockfile_fname, "/var/run/monitas-%s.pid", name_str);
|
---|
| 463 | if (!pid)
|
---|
| 464 | {
|
---|
| 465 | log_it(debug, "Unregistering PID");
|
---|
| 466 | if (unlink(lockfile_fname)) { log_it(error, "Error unregistering PID"); }
|
---|
| 467 | return;
|
---|
| 468 | }
|
---|
| 469 | if (does_file_exist(lockfile_fname))
|
---|
| 470 | {
|
---|
| 471 | tmp[0]='\0';
|
---|
| 472 | if ((fin=fopen(lockfile_fname,"r"))) { fgets(tmp, MAX_STR_LEN, fin); fclose(fin); }
|
---|
| 473 | pid = atol(tmp);
|
---|
| 474 | sprintf(tmp, "ps %ld", (long int)pid);
|
---|
| 475 | res = call_program_and_log_output(tmp);
|
---|
| 476 | if (!res)
|
---|
| 477 | {
|
---|
| 478 | sprintf(tmp, "I believe the daemon is already running. If it isn't, please delete %s and try again.", lockfile_fname);
|
---|
| 479 | log_it(fatal, tmp);
|
---|
| 480 | }
|
---|
| 481 | }
|
---|
| 482 | sprintf(tmp, "echo %ld > %s", (long int)getpid(), lockfile_fname);
|
---|
| 483 | if (system(tmp)) { log_it(fatal, "Cannot register PID"); }
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 |
|
---|
| 487 |
|
---|
| 488 | /*-----------------------------------------------------------*/
|
---|
| 489 |
|
---|
| 490 |
|
---|
| 491 |
|
---|
| 492 | void set_signals(bool on)
|
---|
| 493 | /*
|
---|
| 494 | Purpose:Turn on/off signal-trapping
|
---|
| 495 | Params: on - turn on or off (true=on, false=off)
|
---|
| 496 | Return: None
|
---|
| 497 | */
|
---|
| 498 | {
|
---|
| 499 | int signals[]= { SIGKILL, SIGPIPE, SIGTERM, SIGHUP, SIGTRAP, SIGABRT, SIGINT, SIGSTOP, 0 };
|
---|
| 500 | int i;
|
---|
| 501 | for (i=0; signals[i]; i++)
|
---|
| 502 | {
|
---|
| 503 | if (on)
|
---|
| 504 | { signal(signals[i], terminate_daemon); }
|
---|
| 505 | else
|
---|
| 506 | { signal(signals[i], termination_in_progress); }
|
---|
| 507 | }
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 |
|
---|
| 511 |
|
---|
| 512 | /*-----------------------------------------------------------*/
|
---|
| 513 |
|
---|
| 514 |
|
---|
| 515 |
|
---|
| 516 | void termination_in_progress(int sig)
|
---|
| 517 | {
|
---|
| 518 | log_it(debug, "Termination in progress");
|
---|
| 519 | usleep(1000);
|
---|
[353] | 520 | pthread_exit(NULL);
|
---|
[352] | 521 | }
|
---|
| 522 |
|
---|
| 523 |
|
---|
| 524 |
|
---|
| 525 | /*-----------------------------------------------------------*/
|
---|
| 526 |
|
---|
| 527 |
|
---|
| 528 |
|
---|
| 529 | char *tmsg_to_string(t_msg msg_type)
|
---|
| 530 | /*
|
---|
| 531 | Purpose:turn msg_type struct variable into a string
|
---|
| 532 | Params: msg_type - type of message
|
---|
| 533 | Return: pointer to static string containing text version of msg_type
|
---|
| 534 | */
|
---|
| 535 | {
|
---|
| 536 | static char sz_msg[MAX_STR_LEN];
|
---|
| 537 | switch(msg_type)
|
---|
| 538 | {
|
---|
| 539 | case unused : strcpy(sz_msg, "unused"); break;
|
---|
| 540 | case login : strcpy(sz_msg, "login"); break;
|
---|
| 541 | case logout : strcpy(sz_msg, "logout"); break;
|
---|
| 542 | case login_ok:strcpy(sz_msg, "login_ok"); break;
|
---|
| 543 | case logout_ok:strcpy(sz_msg, "logout_ok"); break;
|
---|
| 544 | case ping : strcpy(sz_msg, "ping"); break;
|
---|
| 545 | case pong : strcpy(sz_msg, "pong"); break;
|
---|
| 546 | case login_fail : strcpy(sz_msg, "login_fail"); break;
|
---|
| 547 | case logout_fail: strcpy(sz_msg, "logout_fail"); break;
|
---|
| 548 | case trigger_backup: strcpy(sz_msg, "trigger_backup"); break;
|
---|
| 549 | case trigger_compare:strcpy(sz_msg, "trigger_compare"); break;
|
---|
| 550 | case trigger_restore:strcpy(sz_msg, "trigger_restore"); break;
|
---|
| 551 | case begin_stream: strcpy(sz_msg, "begin_stream"); break;
|
---|
| 552 | case end_stream: strcpy(sz_msg, "end_stream"); break;
|
---|
| 553 | case backup_ok: strcpy(sz_msg, "backup_ok"); break;
|
---|
| 554 | case backup_fail: strcpy(sz_msg, "backup_fail"); break;
|
---|
| 555 | case compare_ok: strcpy(sz_msg, "compare_ok"); break;
|
---|
| 556 | case compare_fail: strcpy(sz_msg, "compare_fail"); break;
|
---|
| 557 | case restore_ok: strcpy(sz_msg, "restore_ok"); break;
|
---|
| 558 | case restore_fail: strcpy(sz_msg, "restore_fail"); break;
|
---|
| 559 | case user_req: strcpy(sz_msg, "user_req"); break;
|
---|
| 560 | case progress_rpt: strcpy(sz_msg, "progress_rpt"); break;
|
---|
| 561 | default: strcpy(sz_msg, "(Fix tmsg_to_string please)"); break;
|
---|
| 562 | }
|
---|
| 563 | return(sz_msg);
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 |
|
---|
| 567 |
|
---|
| 568 | /*-----------------------------------------------------------*/
|
---|
| 569 |
|
---|
| 570 |
|
---|
| 571 |
|
---|
| 572 | int transmit_file_to_socket(FILE*fin, int socket_fd)
|
---|
| 573 | /*
|
---|
| 574 | Purpose:Transmit file to socket, from disk / stream.
|
---|
| 575 | Params: socket_fd - file descriptor of socket
|
---|
| 576 | fin - file descriptor of input file
|
---|
| 577 | Return: result (0=success, nonzero=failure)
|
---|
| 578 | NB: socket_fd was created with open() but
|
---|
| 579 | fopen was created with fopen(). Different, see? Nyah.
|
---|
| 580 | */
|
---|
| 581 | {
|
---|
| 582 | char tmp[MAX_STR_LEN+1], buf[XFER_BUF_SIZE];
|
---|
| 583 | int length, retval=0, rotor=0;
|
---|
| 584 | bool done;
|
---|
| 585 |
|
---|
| 586 | signal(SIGPIPE, SIG_IGN);
|
---|
| 587 | for(done=false; !done; )
|
---|
| 588 | {
|
---|
| 589 | if (feof(fin))
|
---|
| 590 | {
|
---|
| 591 | log_it(debug, "eof(fin) - OK, stopped reading from mondoarchive");
|
---|
| 592 | done=true;
|
---|
| 593 | continue;
|
---|
| 594 | }
|
---|
| 595 | // log_it(debug, "Reading from mondoarchive");
|
---|
| 596 | length = fread(buf, 1, XFER_BUF_SIZE, fin); /* read from fifo (i.e. mondoarchive) */
|
---|
| 597 | // sprintf(tmp, "%d bytes read from mondoarchive", length); log_it(debug, tmp);
|
---|
| 598 | if (length > 0)
|
---|
| 599 | {
|
---|
| 600 | tmp[0] = (char)(rotor=(rotor%127)+1);
|
---|
| 601 | tmp[1] = (char)(length / 256);
|
---|
| 602 | tmp[2] = (char)(length % 256);
|
---|
| 603 | if (write(socket_fd, tmp, 3)!=3) { log_it(debug, "Failed to write rotor and block_len to socket"); retval++; done=true; continue; }
|
---|
| 604 | // else { log_it(debug, "rotor and block_len written to socket"); }
|
---|
| 605 | if (write(socket_fd, buf, length)!=length) { log_it(debug, "Failed to write block read from mondoarchive to socket"); retval++; done=true; continue; }
|
---|
| 606 | else
|
---|
| 607 | {
|
---|
| 608 | sprintf(tmp, "Block #%d (%d bytes) written to socket", rotor, length);
|
---|
| 609 | // log_it(debug, tmp);
|
---|
| 610 | }
|
---|
| 611 | }
|
---|
| 612 | else if (length < 0)
|
---|
| 613 | { log_it(warn, "I had trouble reading from FIFO while calling mondoarchive"); }
|
---|
| 614 | }
|
---|
| 615 | if (retval==0)
|
---|
| 616 | {
|
---|
| 617 | // log_it(debug, "Writing final rotor (the 'end of stream' one) to socket");
|
---|
| 618 | tmp[0]=0;
|
---|
| 619 | tmp[1]=0x7B;
|
---|
| 620 | tmp[2]=0x21;
|
---|
| 621 | if (write(socket_fd, tmp, 3)!=3) { log_it(debug, "Failed to write end-of-stream rotor to socket"); retval++; }
|
---|
| 622 | }
|
---|
| 623 | log_it(debug, "Closed fopen() to tempdev");
|
---|
| 624 | // fclose(fin);
|
---|
| 625 | signal(SIGPIPE, terminate_daemon);
|
---|
| 626 | return(retval);
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 |
|
---|
| 630 |
|
---|
[353] | 631 | /*-----------------------------------------------------------*/
|
---|
[352] | 632 |
|
---|
| 633 |
|
---|
[353] | 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 | }
|
---|
[352] | 650 |
|
---|
| 651 |
|
---|
| 652 |
|
---|
[353] | 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 |
|
---|