/[monit]/monit/util.c
ViewVC logotype

Diff of /monit/util.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.42 by chopp, Tue Nov 5 09:04:02 2002 UTC revision 1.43 by hauk, Tue Nov 5 12:24:35 2002 UTC
# Line 27  Line 27 
27  #include <time.h>  #include <time.h>
28  #include <stdlib.h>  #include <stdlib.h>
29  #include <sys/socket.h>  #include <sys/socket.h>
30    #include <fcntl.h>
31    
32  #ifdef HAVE_UNISTD_H  #ifdef HAVE_UNISTD_H
33  #include <unistd.h>  #include <unistd.h>
# Line 93  void error(const char *format, ...) { Line 94  void error(const char *format, ...) {
94    vsnprintf(msg, 1024, format, ap);    vsnprintf(msg, 1024, format, ap);
95    va_end(ap);    va_end(ap);
96        
97    if (have_tty) {    if(have_tty) {
98            
99      fprintf(stderr,"%s", msg);      fprintf(stderr,"%s", msg);
100      fflush(stderr);      fflush(stderr);
# Line 113  void error(const char *format, ...) { Line 114  void error(const char *format, ...) {
114   */   */
115  int is_strdefined(char *p) {  int is_strdefined(char *p) {
116        
117    return (p && p[0] != '\0' && strlen(p) > 0);    return(p && p[0] != '\0' && strlen(p) > 0);
118        
119  }  }
120    
# Line 131  char *stripfilename(char* path) { Line 132  char *stripfilename(char* path) {
132    
133    fname= strrchr(path, '/');    fname= strrchr(path, '/');
134        
135    return (fname ? ++fname : path);    return(fname ? ++fname : path);
136        
137  }  }
138    
# Line 146  void chomp(char *string) { Line 147  void chomp(char *string) {
147    
148    ASSERT(string);    ASSERT(string);
149    
150    if ((p= strchr(string, '\r'))   ||    if((p= strchr(string, '\r')) || (p= strchr(string, '\n'))) *p= 0;
        (p= strchr(string, '\n'))) {  
       
     *p= '\0';  
       
   }  
151        
152  }  }
153    
# Line 184  char *ltrim(char *s) { Line 180  char *ltrim(char *s) {
180    
181    ASSERT(s);    ASSERT(s);
182    
183    while (*t==' ' || *t=='\t' || *t=='\r' || *t=='\n') t++;    while(*t==' ' || *t=='\t' || *t=='\r' || *t=='\n') t++;
184    
185    return strcpy(s, t);    return strcpy(s, t);
186    
# Line 202  char *rtrim(char *s) { Line 198  char *rtrim(char *s) {
198    
199    ASSERT(s);    ASSERT(s);
200    
201    while (*s) s++;    while(*s) s++;
202    while (*--s==' ' || *s=='\t' || *s=='\r' || *s=='\n') *s= '\0';    while(*--s==' ' || *s=='\t' || *s=='\r' || *s=='\n') *s= '\0';
203    
204    return t;    return t;
205        
# Line 223  char *trim_quotes(char *s) { Line 219  char *trim_quotes(char *s) {
219    
220    ASSERT(s);    ASSERT(s);
221    
222    while (*t==39 || *t==34 ) {    while(*t==39 || *t==34) {
223    
224      tmp=*t;      tmp=*t;
225      t++;      t++;
# Line 231  char *trim_quotes(char *s) { Line 227  char *trim_quotes(char *s) {
227    
228    }      }  
229    
230    if (t[strlen(t)-1]==tmp) t[strlen(t)-1]= '\0';    if(t[strlen(t)-1]==tmp) t[strlen(t)-1]= '\0';
231    
232    return strcpy(s, t);    return strcpy(s, t);
233    
# Line 275  void handle_string_escapes(char *buf) { Line 271  void handle_string_escapes(char *buf) {
271    
272      if(*(buf+editpos) == '\\' ) {      if(*(buf+editpos) == '\\' ) {
273                
274        switch (*(buf+editpos+1)) {        switch(*(buf+editpos+1)) {
275    
276        case 'n':        case 'n':
277          *(buf+insertpos)='\n';          *(buf+insertpos)='\n';
# Line 329  Process_T get_process(char *name) { Line 325  Process_T get_process(char *name) {
325    
326    ASSERT(name);    ASSERT(name);
327    
328    for (p= processlist; p; p= p->next)    for(p= processlist; p; p= p->next)
329        if (is(p->name, name)) return p;        if(is(p->name, name))
330              return p;
331    
332    return NULL;    return NULL;
333    
# Line 348  int exist_process(char *name) { Line 345  int exist_process(char *name) {
345    
346    ASSERT(name);    ASSERT(name);
347    
348    for (p= processlist; p; p= p->next)    for(p= processlist; p; p= p->next)
349        if (is(p->name, name)) return TRUE;        if(is(p->name, name))
350              return TRUE;
351    
352    return FALSE;    return FALSE;
353    
# Line 384  void printrunlist() { Line 382  void printrunlist() {
382    
383    
384        
385    if (Run.dohttpd) {    if(Run.dohttpd) {
386            
387      printf(" %-18s = %d\n", "httpd portnumber", Run.httpdport);      printf(" %-18s = %d\n", "httpd portnumber", Run.httpdport);
388      printf(" %-18s = %s\n", "httpd bind address",      printf(" %-18s = %s\n", "httpd bind address",
389             Run.bind_addr?Run.bind_addr:"Any/All");             Run.bind_addr?Run.bind_addr:"Any/All");
390      printf(" %-18s = %s\n", "Use ssl encryption", Run.httpdssl?"True":"False");      printf(" %-18s = %s\n", "Use ssl encryption", Run.httpdssl?"True":"False");
391    
392      if (Run.httpdssl) {      if(Run.httpdssl) {
393    
394        printf(" %-18s = %s\n", "PEM key/cert file", Run.httpsslpem);        printf(" %-18s = %s\n", "PEM key/cert file", Run.httpsslpem);
395    
396        if (Run.httpsslclientpem!=NULL) {        if(Run.httpsslclientpem!=NULL) {
397          printf(" %-18s = %s\n", "Client cert file", Run.httpsslclientpem);          printf(" %-18s = %s\n", "Client cert file", Run.httpsslclientpem);
398        } else {        } else {
399          printf(" %-18s = %s\n", "Client cert file", "None");          printf(" %-18s = %s\n", "Client cert file", "None");
# Line 441  void printprocess(Process_T p) { Line 439  void printprocess(Process_T p) {
439    if(p->stop)    if(p->stop)
440        printf(" %-20s = %s\n", "Stop program", is_str_defined(p->stop->arg[0]));        printf(" %-20s = %s\n", "Stop program", is_str_defined(p->stop->arg[0]));
441    
442    for (c= p->checksumlist; c; c= c->next) {    for(c= p->checksumlist; c; c= c->next) {
443            
444      printf(" %-20s = %s %s\n", "Checksum", c->md5, c->file);      printf(" %-20s = %s %s\n", "Checksum", c->md5, c->file);
445            
446    }    }
447    
448    if (! p->checksumlist) {    if(! p->checksumlist) {
449            
450      printf(" %-20s = (not defined)\n", "Other checksum");      printf(" %-20s = (not defined)\n", "Other checksum");
451            
452    }    }
453    
454    if (! p->portlist) {    if(! p->portlist) {
455            
456      printf(" %-20s = (not defined)\n", "Host:Port");      printf(" %-20s = (not defined)\n", "Host:Port");
457            
458    } else {    } else {
459        
460      for (n= p->portlist; n; n= n->next) {      for(n= p->portlist; n; n= n->next) {
461            
462        if ( n->family == AF_INET ) {        if(n->family == AF_INET) {
463    
464          if ( n->ssl != NULL ) {          if(n->ssl != NULL) {
465    
466            printf(" %-20s = %s:%d%s [protocol %s via SSL]\n", "Host:Port",            printf(" %-20s = %s:%d%s [protocol %s via SSL]\n", "Host:Port",
467                   n->hostname, n->port, n->request?n->request:"",                   n->hostname, n->port, n->request?n->request:"",
468                   n->protocol->name);                   n->protocol->name);
469    
470            if ( n->certmd5 != NULL ) {            if(n->certmd5 != NULL) {
471    
472              printf(" %-20s = %s\n",              printf(" %-20s = %s\n",
473                     "Server cert md5 sum",                     "Server cert md5 sum",
# Line 485  void printprocess(Process_T p) { Line 483  void printprocess(Process_T p) {
483    
484          }          }
485    
486        } else if ( n->family == AF_UNIX ) {        } else if(n->family == AF_UNIX) {
487    
488          printf(" %-20s = %s [protocol %s]\n", "Unix Socket",          printf(" %-20s = %s [protocol %s]\n", "Unix Socket",
489                 n->pathname, n->protocol->name);                 n->pathname, n->protocol->name);
# Line 496  void printprocess(Process_T p) { Line 494  void printprocess(Process_T p) {
494    
495    }    }
496        
497    if (p->def_timeout) {    if(p->def_timeout) {
498            
499      printf(" %-20s = Do timeout if %d restart within %d cycles\n",      printf(" %-20s = Do timeout if %d restart within %d cycles\n",
500             "Timeout", p->to_start, p->to_cycle);             "Timeout", p->to_start, p->to_cycle);
# Line 507  void printprocess(Process_T p) { Line 505  void printprocess(Process_T p) {
505            
506    }    }
507    
508    for (r= p->maillist; r; r= r->next) {    for(r= p->maillist; r; r= r->next) {
509            
510      printf(" %-20s = %s\n", "Alert mail to", is_str_defined(r->to));      printf(" %-20s = %s\n", "Alert mail to", is_str_defined(r->to));
511      printf("   %-18s = %s\n", "alert from", is_str_defined_default(r->from));      printf("   %-18s = %s\n", "alert from", is_str_defined_default(r->from));
# Line 529  void printprocess(Process_T p) { Line 527  void printprocess(Process_T p) {
527    
528    }    }
529    
530    if ( ! p->resourcelist ) {    if(! p->resourcelist) {
531    
532      printf(" %-20s = (not defined)\n", "Resource Limits");      printf(" %-20s = (not defined)\n", "Resource Limits");
533    
534    }    }
535            
536    for (q= p->resourcelist; q; q= q->next) {    for(q= p->resourcelist; q; q= q->next) {
537    
538      switch (q->resource_id) {      switch(q->resource_id) {
539    
540      case RESOURCE_ID_CPU_PERCENT:      case RESOURCE_ID_CPU_PERCENT:
541    
# Line 590  void printprocess(Process_T p) { Line 588  void printprocess(Process_T p) {
588      }          }    
589    }    }
590    
591    if (p->def_every)    if(p->def_every)
592                
593      printf(" %-20s = Check process every %d cycles\n", "Every", p->every);      printf(" %-20s = Check process every %d cycles\n", "Every", p->every);
594        
# Line 616  void printprocesslist() { Line 614  void printprocesslist() {
614        
615    printf("The process list contains the following entries:\n\n");    printf("The process list contains the following entries:\n\n");
616        
617    for (p= processlist; p; p= p->next) {    for(p= processlist; p; p= p->next) {
618            
619      printprocess(p);      printprocess(p);
620            
# Line 641  pid_t get_pid(char *pidfile) { Line 639  pid_t get_pid(char *pidfile) {
639    
640    ASSERT(pidfile);    ASSERT(pidfile);
641    
642    if (! exist_file(pidfile)) {    if(! exist_file(pidfile)) {
643            
644      return(FALSE);      return(FALSE);
645            
646    }    }
647    
648    if (! isreg_file(pidfile)) {    if(! isreg_file(pidfile)) {
649            
650      log("%s: pidfile '%s' is not a regular file\n",prog, pidfile);      log("%s: pidfile '%s' is not a regular file\n",prog, pidfile);
651      return(FALSE);      return(FALSE);
652            
653    }    }
654        
655    if ((file= fopen(pidfile,"r")) == (FILE *)NULL) {    if((file= fopen(pidfile,"r")) == (FILE *)NULL) {
656            
657      log("%s: Error opening the pidfile '%s' -- %s\n",      log("%s: Error opening the pidfile '%s' -- %s\n",
658          prog, pidfile, STRERROR);          prog, pidfile, STRERROR);
# Line 665  pid_t get_pid(char *pidfile) { Line 663  pid_t get_pid(char *pidfile) {
663    fscanf(file, "%d", &pid);    fscanf(file, "%d", &pid);
664    fclose(file);    fclose(file);
665    
666    if (pid == -1) {    if(pid == -1) {
667            
668      log("%s: pidfile `%s' does not contain a valid pidnumber\n",      log("%s: pidfile `%s' does not contain a valid pidnumber\n",
669          prog, pidfile);          prog, pidfile);
# Line 692  int is_process_running(Process_T p) { Line 690  int is_process_running(Process_T p) {
690        
691    errno= 0;    errno= 0;
692    
693    if ((pid= get_pid(p->pidfile))) {    if((pid= get_pid(p->pidfile))) {
694            
695      if((kill_return= getpgid(pid)) > 0 || errno == EPERM)      if((kill_return= getpgid(pid)) > 0 || errno == EPERM)
696                    
# Line 734  char *get_RFC1123date(long *date) { Line 732  char *get_RFC1123date(long *date) {
732    
733    datelen=strftime(D, STRLEN-7, "%a, %d %b %Y %H:%M:%S", tm_now);    datelen=strftime(D, STRLEN-7, "%a, %d %b %Y %H:%M:%S", tm_now);
734    
735    if ( ! datelen ) {    if(! datelen) {
736    
737      memset(D, 0, STRLEN);      memset(D, 0, STRLEN);
738                                
# Line 818  char *get_uptime(time_t delta) { Line 816  char *get_uptime(time_t delta) {
816      p+= sprintf(p, "%ldd ", rest_d);      p+= sprintf(p, "%ldd ", rest_d);
817      delta-= rest_d*day;      delta-= rest_d*day;
818    }    }
819    if((rest_h= delta/hour)>0 || ( rest_d > 0 )) {    if((rest_h= delta/hour)>0 || (rest_d > 0)) {
820      p+= sprintf(p, "%ldh ", rest_h);      p+= sprintf(p, "%ldh ", rest_h);
821      delta-= rest_h*hour;      delta-= rest_h*hour;
822    }    }
# Line 845  int set_md5sum(char **dest, char *file) Line 843  int set_md5sum(char **dest, char *file)
843    ASSERT(dest);    ASSERT(dest);
844    ASSERT(file);    ASSERT(file);
845    
846    if (! (*dest= get_md5sum(file))) {    if(! (*dest= get_md5sum(file)))
847                  return FALSE;
     return FALSE;  
           
   }  
848        
849    return TRUE;    return TRUE;
850    
# Line 863  char *get_md5sum(char *file) { Line 858  char *get_md5sum(char *file) {
858    
859    ASSERT(file);    ASSERT(file);
860    
861    if (isreg_file(file)) {    if(isreg_file(file)) {
862            
863      FILE *f= fopen(file, "r");      FILE *f= fopen(file, "r");
864            
865      if (f) {      if(f) {
866                
867        int i;        int i;
868        unsigned char md5buf[16];        unsigned char md5buf[16];
869        char result[STRLEN];        char result[STRLEN];
870        char *r= result;        char *r= result;
871                
872        if (md5_stream(f, md5buf)) {        if(md5_stream(f, md5buf)) {
873                    
874          fclose(f);          fclose(f);
875                    
# Line 884  char *get_md5sum(char *file) { Line 879  char *get_md5sum(char *file) {
879                
880        fclose(f);        fclose(f);
881                
882        for (i= 0; i < 16; ++i) {        for(i= 0; i < 16; ++i) {
883                    
884          r+= sprintf(r, "%02x", md5buf[i]);          r+= sprintf(r, "%02x", md5buf[i]);
885                    
# Line 917  int check_md5(char *file, char *sum) { Line 912  int check_md5(char *file, char *sum) {
912    
913    newSum= get_md5sum(file);    newSum= get_md5sum(file);
914    
915    if (newSum) {    if(newSum) {
916            
917      int rv;      int rv;
918            
# Line 949  char *url_encode(char *uri) { Line 944  char *url_encode(char *uri) {
944    
945    str= (unsigned char *)xmalloc(3 * strlen(uri) + 1);    str= (unsigned char *)xmalloc(3 * strlen(uri) + 1);
946    
947    for (x = 0, y = 0; uri[x]; x++, y++) {    for(x = 0, y = 0; uri[x]; x++, y++) {
948    
949      if (is_unsafe(&uri[x])) {      if(is_unsafe(&uri[x])) {
950        str[y++] = '%';        str[y++] = '%';
951        str[y++] = hexchars[(unsigned char) uri[x] >> 4];        str[y++] = hexchars[(unsigned char) uri[x] >> 4];
952        str[y] = hexchars[(unsigned char) uri[x] & 0xf];        str[y] = hexchars[(unsigned char) uri[x] & 0xf];
# Line 973  char *url_encode(char *uri) { Line 968  char *url_encode(char *uri) {
968   */   */
969  char *get_basic_authentication_header() {  char *get_basic_authentication_header() {
970    
971    if (Run.Auth.defined) {    if(Run.Auth.defined) {
972            
973      char *b64;      char *b64;
974      char buf[STRLEN];      char buf[STRLEN];
# Line 994  char *get_basic_authentication_header() Line 989  char *get_basic_authentication_header()
989    
990    
991  /**  /**
992   * Does printf style format line parsing   * Do printf style format line parsing
993   * @param s format string   * @param s format string
994   * @param ap variable argument list   * @param ap variable argument list
995   * @return buffer with parsed string   * @return buffer with parsed string
# Line 1028  char *format(const char *s, va_list ap) Line 1023  char *format(const char *s, va_list ap)
1023  }  }
1024    
1025    
1026    /**
1027     * Redirect the standard file descriptors to /dev/null and route any
1028     * error messages to the log file.
1029     */
1030    void redirect_stdfd() {
1031    
1032      int i;
1033      
1034      have_tty= FALSE;
1035      for(i= 0; i < 3; i++)
1036          if(close(i) == -1 || open("/dev/null", O_RDWR) != i)
1037          error("Cannot reopen standard file descriptor (%d) -- %s\n", i, STRERROR);
1038      
1039    }
1040    
1041    
1042  /* ----------------------------------------------------------------- Private */  /* ----------------------------------------------------------------- Private */
1043    
1044    

Legend:
Removed from v.1.42  
changed lines
  Added in v.1.43

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26