Changeset 353


Ignore:
Timestamp:
Jan 28, 2006, 6:42:59 PM (13 years ago)
Author:
bcornec
Message:

monitas latest version

Location:
branches/stable/monitas
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • branches/stable/monitas/client.c

    r352 r353  
    6262#include "structs.h"
    6363//#define LOG_THESE_AND_HIGHER debug
    64 #define LOGFILE "/var/log/monitas-client.log"
     64//#define LOGFILE "/var/log/monitas-client.log"
    6565
    6666
     
    7474pthread_t g_mondo_thread=0;
    7575char g_command_fifo[MAX_STR_LEN+1];
    76 char g_logfile[MAX_STR_LEN+1] = "/var/log/monitas-client.log";
    7776
    7877/* externs */
     
    9291extern void register_pid(pid_t, char*);
    9392extern void set_signals(bool);
     93extern int parse_options(int argc, char *argv[]);
    9494
    9595/* prototypes */
     
    202202      return(1);
    203203    }
    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"));
    205205  sprintf(tmp, "mondoarchive_params --> '%s'", mondoparams_str);
    206206  log_it(debug, tmp);
     
    342342      return(1);
    343343    }
    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"));
    345345  sprintf(tmp, "mondoarchive_params --> '%s'", mondoparams_str);
    346346  log_it(debug, tmp);
     
    434434  char tmp[MAX_STR_LEN+1];
    435435
    436   for(server_port = 8700; server_port < 8710; server_port++)
    437     {
    438       sin->sin_port = htons(server_port);
    439436      if ((*p_s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    440437    {
    441       perror("socket");
     438//    perror("socket");
     439      log_it(info, "Creating socket failed: %s", strerror(errno));
    442440      return(-1);
    443441    }
     442  for(server_port = 8700; server_port < 8710; server_port++)
     443    {
     444      sin->sin_port = htons(server_port);
    444445      if (connect(*p_s, (struct sockaddr*)sin, sizeof(struct sockaddr_in)) < 0)
    445446    {
     
    450451      return(server_port);
    451452    }
     453  close(*p_s);
    452454  return(-1);
    453455}
     
    12491251//  pthread_t thread;
    12501252
     1253  parse_options(argc, argv);
     1254
    12511255  log_it(info, "---------- Monitas (client) by Hugo Rabson ----------");
    12521256  register_pid(getpid(), "client");
     
    12841288    }
    12851289
    1286 //  set_param_in_rcfile(CLIENT_RCFILE, "mondoarchive_params", "-1 -L");
     1290//  set_param_in_rcfile(g->client_rcfile, "mondoarchive_params", "-1 -L");
    12871291  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);
    12891293  logout_and_exit(g_server_name);
    12901294  exit(0);
  • branches/stable/monitas/common.c

    r352 r353  
    2727
    2828#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"
    2937
    3038
     
    4351int create_and_watch_fifo_for_commands(char*);
    4452int get_bkgd_prog_result(pthread_t*);
    45 void log_it_SUB(char*, t_loglevel, char*);
     53//void log_it_SUB(char*, t_loglevel, char*);
    4654bool program_still_running(char*);
    4755int receive_file_from_socket(FILE*fout, int socket_fd);
     
    5260void termination_in_progress(int sig);
    5361int transmit_file_to_socket(FILE*fin, int socket_fd);
     62char *my_basename(char *path);
     63int 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() */
     70static struct s_globaldata  _global_data;
     71/* Global Pointer to access the (semi-) static data */
     72struct s_globaldata *g = & _global_data;
    5473
    5574
     
    7392
    7493  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); }
    7695  log_it(debug, "Done creating pthread. Will continue now.");
    7796}
     
    90109Return: None
    91110*/
    92   char*command;
    93111  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);
    105117}
    106118
     
    119131*/
    120132{
    121   char tmp[MAX_STR_LEN+1], incoming[MAX_STR_LEN+1];
     133  char incoming[MAX_STR_LEN+1];
    122134  int res=0, fd, len, pos=0;
    123135
    124136  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); }
    126138  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); }
    128140//  log_it(info, "Awaiting commands from FIFO");
    129141  for(;;)
     
    140152          if (res)
    141153            {
    142               sprintf(tmp, "%s <-- errors occurred during processing", incoming);
    143               log_it(error, tmp);
     154              log_it(error,  "%s <-- errors occurred during processing", incoming);
    144155            }
    145156          else
    146157            {
    147               sprintf(tmp, "%s <-- command received OK", incoming);
    148               log_it(info, tmp);
     158              log_it(info, "%s <-- command received OK", incoming);
    149159            }
    150160        }
     
    168178*/
    169179{
    170   void*vp;
    171   void**pvp;
     180  void* vp;
     181//  void**pvp;
    172182  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;
    176186  vp=(void*)result_str;
    177187  strcpy(result_str, "(uninitialized)");
     
    181191  p = (char*)vp;
    182192  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    }
    185204  *thread = 0;
     205  res = (int) vp;
     206  log_it(debug, "pthread res = %d ('%s')", res, (res>256 || res<-256) ? (char*)vp : "");
    186207  return(res);
    187208}
     
    201222*/
    202223{
    203   char sz_outstr[MAX_STR_LEN], sz_level[MAX_STR_LEN], sz_time[MAX_STR_LEN];
     224  char      *sz_level = "";
    204225  time_t time_rec;
     226  struct tm  tm;
    205227  FILE*fout;
    206228
    207229  time(&time_rec);
     230  localtime_r(&time_rec, &tm);
    208231  switch(level)
    209232    {
    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    }
    220240  if ((int)level >= LOG_THESE_AND_HIGHER)
    221241    {
    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);
    224245      fclose(fout);
    225246    }
    226247  if (level==fatal)
    227248    {
    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
     261void logToFile(char *logfile, t_loglevel level, char *filename, char *lineno, char *funcname, char *sz_message, ...)
     262/*
     263Purpose: Log <sz_message> and its <level> of severity to <logfile>
     264Params:  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
     271Return:  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);
    229319      terminate_daemon(0);
    230320      exit(1);
     
    428518  log_it(debug, "Termination in progress");
    429519  usleep(1000);
    430   pthread_exit(0);
     520  pthread_exit(NULL);
    431521}
    432522
     
    539629
    540630
    541 
    542 
    543 
    544 
    545 
     631/*-----------------------------------------------------------*/
     632
     633
     634/*
     635Purpose: strip directory from filenames
     636Params:  path    like: "dir1/dir2/file"
     637Return:  result  pointer to the first character behind the last '/'
     638*/
     639char *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/*
     658Purpose: analyse command line arguments and write them to global struct
     659Params:  argc   number of arguments
     660         argv   arguments as array of strings
     661Return: result (0=success, nonzero=failure)
     662*/
     663int 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  
    88
    99
    10 extern void log_it_SUB(char*, t_loglevel, char*);
     10//extern void log_it_SUB(char*, t_loglevel, char*);
    1111char *call_program_and_get_last_line_of_output(char*);
    1212int call_program_and_log_output (char *);
     
    1717
    1818
    19 extern char g_logfile[MAX_STR_LEN+1];
    2019
    2120
     
    215214/*************************************************************************
    216215 * 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                      *
    219223 * Called by:                                                            *
    220224 * Returns:                                                              *
     
    223227strip_spaces (char *in_out)
    224228{
     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;
    225254    /** buffers ******************************************************/
    226   char tmp[MAX_STR_LEN+1];
     255//  char tmp[MAX_STR_LEN+1];
    227256
    228257    /** pointers *****************************************************/
    229   char *p;
     258//  char *p;
    230259
    231260    /** int *********************************************************/
    232   int i;
     261//  int i;
    233262
    234263    /** 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++);
    236265  strcpy (tmp, in_out + i);
    237266  for (i = strlen (tmp); i>0 && tmp[i - 1] <= 32; i--);
     
    277306    }
    278307  in_out[i] = '\0';
     308*/
    279309/*  for(i=strlen(in_out); i>0 && in_out[i-1]<=32; i--) {in_out[i-1]='\0';} */
    280310}
  • branches/stable/monitas/server.c

    r352 r353  
    8080
    8181#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); }
    8484
    8585
     
    100100extern char *tmsg_to_string(t_msg);
    101101extern int transmit_file_to_socket(FILE*,int);
    102 
     102extern int parse_options(int argc, char *argv[]);
    103103
    104104
     
    108108char g_command_fifo[MAX_STR_LEN+1]; // Device which server will monitor for incoming commands
    109109pthread_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";
    111110char g_server_status_file[MAX_STR_LEN+1];
    112111
     
    156155  FILE*fout;
    157156
    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); }
    162160  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); }
    164162  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); }
    166164  else
    167165    {
    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);
    170167      rec_to_client.msg_type = trigger_backup;
    171168      strncpy(rec_to_client.body, clientpath, sizeof(rec_to_client.body));
     
    193190  if (res>0)
    194191    {
    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);
    197193      rec_to_client.msg_type = backup_fail;
    198194      sprintf(rec_to_client.body, "Failed to backup %s", clientpath);
     
    202198  else
    203199    {
    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);
    206201      rec_to_client.msg_type = backup_ok;
    207202      sprintf(rec_to_client.body, "%s - backed up ok", clientpath);
     
    211206    {
    212207      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);
    215209      i = find_client_in_clientlist(ipaddr);
    216210      if (i>=0) { forcibly_logout_client(i); }
     
    241235  FILE*fin;
    242236
    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);
    245238// FIXME - don't assume the latest backup contains the files we want ;)
    246239  sprintf(tmp, "find /var/spool/monitas/%s -type f | sort | tail -n1", ipaddr);
    247240  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);
    253244  rec_to_client.msg_type = trigger_compare;
    254245  strncpy(rec_to_client.body, clientpath, sizeof(rec_to_client.body));
     
    256247    { log_it(error, "compare_client - failed to send msg to client"); return(1); }
    257248  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); }
    259250  res += transmit_file_to_socket(fin, socket_fd);
    260251  len=read(socket_fd, (char*)&i, sizeof(i));
     
    267258  if (res>0)
    268259    {
    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);
    271261      rec_to_client.msg_type = compare_fail;
    272262      sprintf(rec_to_client.body, "Failed to compare %s", clientpath);
     
    275265  else
    276266    {
    277       sprintf(tmp, "%s - compared %s ok", ipaddr, clientpath);
    278       log_it(info, tmp);
     267      log_it(info, "%s - compared %s ok", ipaddr, clientpath);
    279268      rec_to_client.msg_type = compare_ok;
    280269      sprintf(rec_to_client.body, "%s - compared ok", clientpath);
     
    283272  if (send_msg_to_client(&rec_to_client, ipaddr, port, &socket_fd))
    284273    {
    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);
    287275      i = find_client_in_clientlist(ipaddr);
    288276      if (i>=0) { forcibly_logout_client(i); }
     
    307295{
    308296  int i;
    309   char tmp[MAX_STR_LEN+1];
    310297
    311298  for(i = 0; i < g_clientlist.items; i++)
     
    313300      if (!strcmp(clientIP, g_clientlist.el[i].ipaddr))
    314301    { 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);
    317303    }
    318304  return(-1);
     
    359345  int res=0;
    360346
    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);
    363348  rec_to_client.msg_type = logout_ok; /* to confirm logout */
    364349  strcpy(rec_to_client.body, "Server is shutting down. You are forced to logout");
     
    366351  if (--g_clientlist.items > 0)
    367352    {
    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);
    370354      sprintf(tmp, "Was ipaddr=%s; now is ipaddr=", g_clientlist.el[clientno].ipaddr);
    371355      memcpy((void*)&g_clientlist.el[clientno], (void*)&g_clientlist.el[g_clientlist.items], sizeof(struct s_registered_client_record));
    372356      strcat(tmp, g_clientlist.el[clientno].ipaddr);
     357      /* FIXME: tmp must never contain '%'-sequences */
    373358      log_it(debug, tmp);
    374359    }
     
    419404*/
    420405{
    421   char clientIP[MAX_STR_LEN+1], tmp[MAX_STR_LEN+1];
     406  char clientIP[MAX_STR_LEN+1];
    422407  unsigned char *ptr;
    423408  int res=0;
     
    426411  ptr = (unsigned char*)(&sin->sin_addr);
    427412  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);
    432415  switch(rec->msg_type)
    433416    {
     
    471454  struct s_server2client_msg_record rec_to_client;
    472455  int clientno;
    473   char tmp[MAX_STR_LEN+1];
    474456
    475457//FIXME - lock g_clientlist[]
     
    479461      rec_to_client.msg_type = login_fail;
    480462      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);
    483464/* FIXME - ping client (which will have a child watching for incoming
    484465packets by now - you didn't forget to do that, did you? :)) - to find out
     
    497478      g_clientlist.el[clientno].busy = false;
    498479      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);
    501481      strcpy(g_clientlist.el[clientno].last_progress_rpt, "Logged in");
    502482    }
     
    523503  struct s_server2client_msg_record rec_to_client;
    524504  int i, res=0;
    525   char tmp[MAX_STR_LEN+1];
    526505
    527506  i = find_client_in_clientlist(clientIP);
     
    550529      g_clientlist.items--;
    551530      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);
    554532    }
    555533  send_msg_to_client(&rec_to_client, clientIP, rec_from_client->port, NULL);
     
    575553  struct s_server2client_msg_record rec_to_client;
    576554  int i;
    577   char tmp[MAX_STR_LEN+1];
    578555
    579556  i = find_client_in_clientlist(clientIP);
    580557  if (i < 0)
    581558    {
    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);
    584560    }
    585561  else
     
    611587//  struct s_server2client_msg_record rec_to_client;
    612588  int i, res=0;
    613   char tmp[MAX_STR_LEN+1];
    614589
    615590  i = find_client_in_clientlist(clientIP);
    616591  if (i < 0)
    617592    {
    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);
    620594      res++;
    621595    }
     
    645619//  struct s_server2client_msg_record rec_to_client;
    646620  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;
    648622
    649623  i = find_client_in_clientlist(clientIP);
    650624  if (i < 0)
    651625    {
    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);
    654627      res++;
    655628    }
     
    659632      p = strchr(first_half, ' ');
    660633      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);
    662635      log_it(debug, command);
    663636      i = system(command);
     
    683656int res=0, port;
    684657int clientno;
    685 char tmp[MAX_STR_LEN+1];
    686658int pos;
    687659char command[MAX_STR_LEN+1], ipaddr[MAX_STR_LEN+1],
     
    698670
    699671//          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);
    704674          if ((clientno = find_client_in_clientlist(ipaddr)) < 0)
    705675            {
    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);
    708677            }
    709678          else if (g_clientlist.el[clientno].busy == true)
    710679            {
    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);
    713681            }
    714682          else
     
    724692              else
    725693                {
    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);
    728695                  res=1;
    729696                }
     
    753720  FILE*fin;
    754721
    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);
    757723// FIXME - don't assume the latest backup contains the files we want ;)
    758724  sprintf(tmp, "find /var/spool/monitas/%s -type f | sort | tail -n1", ipaddr);
    759725  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);
    765729  rec_to_client.msg_type = trigger_restore;
    766730  strncpy(rec_to_client.body, clientpath, sizeof(rec_to_client.body));
     
    782746  if (res>0)
    783747    {
    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);
    786749      rec_to_client.msg_type = restore_fail;
    787750      sprintf(rec_to_client.body, "Failed to restore %s", clientpath);
     
    790753  else
    791754    {
    792       sprintf(tmp, "%s - restored %s ok", ipaddr, clientpath);
    793       log_it(info, tmp);
     755      log_it(info, "%s - restored %s ok", ipaddr, clientpath);
    794756      rec_to_client.msg_type = restore_ok;
    795757      sprintf(rec_to_client.body, "%s - restored ok", clientpath);
     
    798760  if (send_msg_to_client(&rec_to_client, ipaddr, port, &socket_fd))
    799761    {
    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);
    802763      i = find_client_in_clientlist(ipaddr);
    803764      if (i>=0) { forcibly_logout_client(i); }
     
    830791struct sockaddr_in sin;
    831792int  s;
    832 char tmp[MAX_STR_LEN+1];
    833793if ((hp = gethostbyname(clientIP)) == NULL)
    834794{
    835 sprintf(tmp, "send_msg_to_client: %s: unknown host", clientIP);
    836 log_it(error, tmp);
     795log_it(error, "send_msg_to_client: %s: unknown host", clientIP);
    837796return(1);
    838797}
     
    845804{ perror("socket"); log_it(error, "send_msg_to_client: SOCKET error"); return(1); }
    846805if (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); }
    848807send(s, (char*)rec, sizeof(struct s_server2client_msg_record), 0);
    849808if (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);
     809log_it(debug, "Sent %s msg [%s] to %s (port %d)", tmsg_to_string(rec->msg_type), rec->body, clientIP, port);
    852810return(0);
    853811}
     
    883841      usleep(50000);
    884842    }
    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);
    887844}
    888845
     
    950907  int watch_port;
    951908  struct sockaddr_in sin;
    952   char buf[MAX_STR_LEN+1], tmp[MAX_STR_LEN+1];
     909  char buf[MAX_STR_LEN+1];
    953910  int len, s, new_s;
    954911  struct s_client2server_msg_record rec;
     
    962919  if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    963920    {
    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);
    966922      return((void*)-1);
    967923    }
    968924  if (bind(s, (struct sockaddr*)&sin, sizeof(sin)) < 0)
    969925    {
    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));
    972927      return((void*)-1);
    973928    }
    974929  if (listen(s, MAX_PENDING) < 0)
    975930    {
    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));
    978932      return((void*)-1);
    979933    }
    980934  /* 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);
    983936  while(true)
    984937    {
     
    1016969  pthread_t server_status_thread;
    1017970
     971  parse_options(argc, argv);
     972
    1018973  log_it(info, "---------- Monitas (server) by Hugo Rabson ----------");
    1019974  register_pid(getpid(), "server");
    1020975  set_signals(true);
    1021976  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);
    1024979  log_it(warn, "Execution should never reach this point");
    1025980  exit(0);
  • branches/stable/monitas/structs.h

    r352 r353  
    11/* structs.h
    22
     307/17
     4- add central global struct
    35
    4606/16
    5 - added CLIENT_RCFILE
     7- added CLIENT_RCFILE [moved to common.c on 07/17]
    68- added .bodyAux to msg record structure
    79
    81006/14
    9 - added SERVER_COMDEV and CLIENT_COMDEV
     11- added SERVER_COMDEV and CLIENT_COMDEV [moved to common.c on 07/17]
    1012
    111306/11
     
    4547
    4648
    47 #define log_it(x,y) { log_it_SUB(g_logfile,x,y); }
     49/* moved to common.c :
    4850#define CLIENT_RCFILE "/root/.monitas-client.rc"
    4951#define CLIENT_COMDEV "/var/spool/monitas/client-input.dev"
    5052#define SERVER_COMDEV "/var/spool/monitas/server-input.dev"
    5153#define SERVER_STATUS_FILE "/var/spool/monitas/server-status.txt"
     54*/
    5255#define MAX_STR_LEN 510
    5356#define MAX_PENDING 8 /* silently limited to 5 by BSD, says a Linux manual */
     
    5659#define XFER_BUF_SIZE 16384 /* for sending archives between client and server */
    5760                            /* NB: 32768 doesn't work; don't ask me why */
     61
    5862typedef enum { false=0, true=1} bool;
    5963typedef enum { unused=0, login, logout, ping, pong, login_ok, logout_ok,
     
    6367                begin_stream, end_stream, user_req, progress_rpt } t_msg;
    6468typedef 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); }
     77extern 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 */
     86struct 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 */
     101extern struct s_globaldata *g;
     102
     103
    65104
    66105
Note: See TracChangeset for help on using the changeset viewer.