ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/tools.c (file contents):
Revision 1.41 by ats, Mon Apr 5 15:40:15 2004 UTC vs.
Revision 1.59 by ats, Sat Nov 6 14:55:53 2004 UTC

# Line 1 | Line 1
1   /*
2 < * i-scream central monitoring system
2 > * i-scream libstatgrab
3   * http://www.i-scream.org
4   * Copyright (C) 2000-2004 i-scream
5   *
# Line 30 | Line 30
30   #include <stdlib.h>
31   #include <unistd.h>
32   #include <sys/types.h>
33 #include <regex.h>
33   #ifdef ALLBSD
34   #include <fcntl.h>
35   #endif
# Line 43 | Line 42
42   #include <sys/param.h>
43   #include <sys/sysctl.h>
44   #endif
45 + #ifdef HPUX
46 + #include <sys/param.h>
47 + #include <sys/pstat.h>
48 + #endif
49  
50   #include "tools.h"
51 + #include "statgrab.h"
52  
53   #ifdef SOLARIS
54   #ifdef HAVE_LIBDEVINFO_H
# Line 90 | Line 94 | static void add_mapping(char *bsd, char *svr){
94          mapping_t *map_end_ptr;
95  
96          if (mapping == NULL){
97 <                mapping = malloc(sizeof(mapping_t));
97 >                mapping = sg_malloc(sizeof(mapping_t));
98                  if (mapping == NULL) return;
99                  map_ptr = mapping;
100          }else{
# Line 105 | Line 109 | static void add_mapping(char *bsd, char *svr){
109                  /* We've reached end of list and not found the entry.. So we need to malloc
110                   * new mapping_t
111                   */
112 <                map_end_ptr->next = malloc(sizeof(mapping_t));
112 >                map_end_ptr->next = sg_malloc(sizeof(mapping_t));
113                  if (map_end_ptr->next == NULL) return;
114                  map_ptr = map_end_ptr->next;
115          }
116  
117          map_ptr->next = NULL;
118 <        map_ptr->bsd = strdup(bsd);
119 <        map_ptr->svr = strdup(svr);
118 >        map_ptr->bsd = NULL;
119 >        map_ptr->svr = NULL;
120 >        if (sg_update_string(&map_ptr->bsd, bsd) < 0
121 >            || sg_update_string(&map_ptr->svr, svr) < 0) {
122 >                return;
123 >        }
124  
125          return;
126   }
# Line 122 | Line 130 | static char *read_dir(char *disk_path){
130          DIR *dirp;
131          struct dirent *dp;
132          struct stat stbuf;
133 <        char *svr_name;
133 >        char *svr_name = NULL;
134          char current_dir[MAXPATHLEN];
135          char file_name[MAXPATHLEN];
136          char temp_name[MAXPATHLEN];
# Line 149 | Line 157 | static char *read_dir(char *disk_path){
157                          x = readlink(dir_dname, file_name, sizeof(file_name));
158                          file_name[x] = '\0';
159                          if (strcmp(file_name, temp_name) == 0) {
160 <                                svr_name = strdup(dp->d_name);
160 >                                if (sg_update_string(&svr_name,
161 >                                                     dp->d_name) < 0) {
162 >                                        return NULL;
163 >                                }
164                                  closedir(dirp);
165                                  return svr_name;
166                          }
# Line 171 | Line 182 | static int get_alias(char *alias){
182          char *value;
183          int instance;
184          if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
185 <                return 1;
185 >                return -1;
186          }
187          node = di_drv_first_node(alias, root_node);
188          while (node != DI_NODE_NIL) {
# Line 213 | Line 224 | static int build_mapping(){
224          int found;
225  
226          if ((kc = kstat_open()) == NULL) {
227 <                return 1;
227 >                return -1;
228          }
229  
230          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
# Line 235 | Line 246 | static int build_mapping(){
246                                  if (x>=BIG_ENOUGH){
247                                          /* We've got bigger than we thought was massive */
248                                          /* If we hit this.. Make big enough bigger */
249 <                                        return 1;
249 >                                        return -1;
250                                  }
251                                  if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){
252                                          found = 1;
# Line 245 | Line 256 | static int build_mapping(){
256  
257                          if(!found){
258                                  if((get_alias(device_name)) != 0){
259 <                                        return 1;
259 >                                        return -1;
260                                  }
261                                  strncpy(driver_list[x], device_name, BIG_ENOUGH);
262                                  list_entries++;
# Line 258 | Line 269 | static int build_mapping(){
269  
270   #endif
271  
272 <
262 <
272 > #if defined(LINUX) || defined(CYGWIN)
273   char *sg_f_read_line(FILE *f, const char *string){
274          /* Max line length. 8k should be more than enough */
275          static char line[8192];
# Line 270 | Line 280 | char *sg_f_read_line(FILE *f, const char *string){
280                  }
281          }
282  
283 +        sg_set_error(SG_ERROR_PARSE, NULL);
284          return NULL;
285   }
286  
287   char *sg_get_string_match(char *line, regmatch_t *match){
288          int len=match->rm_eo - match->rm_so;
289 <        char *match_string=malloc(len+1);
289 >        char *match_string=sg_malloc(len+1);
290  
291          match_string=strncpy(match_string, line+match->rm_so, len);
292          match_string[len]='\0';
# Line 283 | Line 294 | char *sg_get_string_match(char *line, regmatch_t *matc
294          return match_string;
295   }
296  
297 + long long sg_get_ll_match(char *line, regmatch_t *match){
298 +        char *ptr;
299 +        long long num;
300  
301 +        ptr=line+match->rm_so;
302 +        num=atoll(ptr);
303  
304 +        return num;
305 + }
306 + #endif
307 +
308   #ifndef HAVE_ATOLL
309   static long long atoll(const char *s) {
310          long long value = 0;
# Line 329 | Line 349 | static long long atoll(const char *s) {
349   * Returns strlen(src); if retval >= siz, truncation occurred.
350   */
351   size_t sg_strlcpy(char *dst, const char *src, size_t siz){
352 <        register char *d = dst;
353 <        register const char *s = src;
354 <        register size_t n = siz;
352 >        register char *d = dst;
353 >        register const char *s = src;
354 >        register size_t n = siz;
355  
356 <        /* Copy as many bytes as will fit */
357 <        if (n != 0 && --n != 0) {
358 <                do {
359 <                        if ((*d++ = *s++) == 0)
360 <                                break;
361 <                } while (--n != 0);
362 <        }
356 >        /* Copy as many bytes as will fit */
357 >        if (n != 0 && --n != 0) {
358 >                do {
359 >                        if ((*d++ = *s++) == 0)
360 >                                break;
361 >                } while (--n != 0);
362 >        }
363  
364 <        /* Not enough room in dst, add NUL and traverse rest of src */
365 <        if (n == 0) {
366 <                if (siz != 0)
367 <                        *d = '\0';              /* NUL-terminate dst */
368 <                while (*s++)
369 <                        ;
370 <        }
364 >        /* Not enough room in dst, add NUL and traverse rest of src */
365 >        if (n == 0) {
366 >                if (siz != 0)
367 >                        *d = '\0';            /* NUL-terminate dst */
368 >                while (*s++)
369 >                        ;
370 >        }
371  
372 <        return(s - src - 1);    /* count does not include NUL */
372 >        return(s - src - 1);    /* count does not include NUL */
373   }
374  
375   /*      $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $    */
# Line 378 | Line 398 | size_t sg_strlcpy(char *dst, const char *src, size_t s
398   * If retval >= siz, truncation occurred.
399   */
400   size_t sg_strlcat(char *dst, const char *src, size_t siz){
401 <        register char *d = dst;
402 <        register const char *s = src;
403 <        register size_t n = siz;
404 <        size_t dlen;
401 >        register char *d = dst;
402 >        register const char *s = src;
403 >        register size_t n = siz;
404 >        size_t dlen;
405  
406 <        /* Find the end of dst and adjust bytes left but don't go past end */
407 <        while (n-- != 0 && *d != '\0')
408 <                d++;
409 <        dlen = d - dst;
410 <        n = siz - dlen;
406 >        /* Find the end of dst and adjust bytes left but don't go past end */
407 >        while (n-- != 0 && *d != '\0')
408 >                d++;
409 >        dlen = d - dst;
410 >        n = siz - dlen;
411  
412 <        if (n == 0)
413 <                return(dlen + strlen(s));
414 <        while (*s != '\0') {
415 <                if (n != 1) {
416 <                        *d++ = *s;
417 <                        n--;
418 <                }
419 <                s++;
420 <        }
421 <        *d = '\0';
412 >        if (n == 0)
413 >                return(dlen + strlen(s));
414 >        while (*s != '\0') {
415 >                if (n != 1) {
416 >                        *d++ = *s;
417 >                        n--;
418 >                }
419 >                s++;
420 >        }
421 >        *d = '\0';
422  
423 <        return(dlen + (s - src));       /* count does not include NUL */
423 >        return(dlen + (s - src));       /* count does not include NUL */
424   }
425  
426 < char *sg_update_string(char **dest, const char *src) {
426 > int sg_update_string(char **dest, const char *src) {
427          char *new;
428  
429 <        new = realloc(*dest, strlen(src) + 1);
429 >        if (src == NULL) {
430 >                /* We're being told to set it to NULL. */
431 >                free(*dest);
432 >                *dest = NULL;
433 >                return 0;
434 >        }
435 >
436 >        new = sg_realloc(*dest, strlen(src) + 1);
437          if (new == NULL) {
438 <                return NULL;
438 >                return -1;
439          }
440  
441          strcpy(new, src);
442          *dest = new;
443 <        return new;
443 >        return 0;
444   }
445  
419 long long sg_get_ll_match(char *line, regmatch_t *match){
420        char *ptr;
421        long long num;
422
423        ptr=line+match->rm_so;
424        num=atoll(ptr);
425
426        return num;
427 }
428
446   #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
447   kvm_t *sg_get_kvm() {
448          static kvm_t *kvmd = NULL;
# Line 435 | Line 452 | kvm_t *sg_get_kvm() {
452          }
453  
454          kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
455 +        if(kvmd == NULL) {
456 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
457 +        }
458          return kvmd;
459   }
460  
# Line 447 | Line 467 | kvm_t *sg_get_kvm2() {
467          }
468  
469          kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
470 +        if(kvmd2 == NULL) {
471 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
472 +        }
473          return kvmd2;
474   }
475   #endif
# Line 454 | Line 477 | kvm_t *sg_get_kvm2() {
477   #if defined(NETBSD) || defined(OPENBSD)
478   struct uvmexp *sg_get_uvmexp() {
479          int mib[2];
480 <        size_t size;
481 <        static struct uvmexp *uvm = NULL;
480 >        size_t size = sizeof(struct uvmexp);
481 >        static struct uvmexp uvm;
482          struct uvmexp *new;
483  
484          mib[0] = CTL_VM;
485          mib[1] = VM_UVMEXP;
486  
487 <        if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) {
487 >        if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) {
488 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP");
489                  return NULL;
490          }
491  
492 <        new = realloc(uvm, size);
493 <        if (new == NULL) {
494 <                return NULL;
471 <        }
472 <        uvm = new;
492 >        return &uvm;
493 > }
494 > #endif
495  
496 <        if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) {
497 <                return NULL;
498 <        }
496 > #ifdef HPUX
497 > struct pst_static *sg_get_pstat_static() {
498 >        static int got = 0;
499 >        static struct pst_static pst;
500  
501 <        return uvm;
501 >        if (!got) {
502 >                if (pstat_getstatic(&pst, sizeof pst, 1, 0) == -1) {
503 >                        sg_set_error_with_errno(SG_ERROR_PSTAT,
504 >                                                "pstat_static");
505 >                        return NULL;
506 >                }
507 >                got = 1;
508 >        }
509 >        return &pst;
510   }
511   #endif
512  
513   int sg_init(){
514 +        sg_set_error(SG_ERROR_NONE, NULL);
515 +
516   #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
517          if (sg_get_kvm() == NULL) {
518 <                return 1;
518 >                return -1;
519          }
520          if (sg_get_kvm2() == NULL) {
521 <                return 1;
521 >                return -1;
522          }
523   #endif
491 #if defined(NETBSD) || defined(OPENBSD)
492        /* This should always succeed, but it seems that on some
493         * versions of NetBSD the first call to get_uvmexp will return
494         * a non-filled-in structure; this is a workaround for that.
495         */
496        if (sg_get_uvmexp() == NULL) return 1;
497 #endif
524   #ifdef SOLARIS
525          /* On solaris 7, this will fail if you are not root. But, everything
526           * will still work, just no disk mappings. So we will ignore the exit
# Line 508 | Line 534 | int sg_init(){
534   }
535  
536   int sg_drop_privileges() {
537 <        if (setegid(getgid()) != 0) return -1;
538 <        if (seteuid(getuid()) != 0) return -1;
537 > #ifdef HAVE_SETEGID
538 >        if (setegid(getgid()) != 0) {
539 > #elif defined(HAVE_SETRESGID)
540 >        if (setresgid(getgid(), getgid(), getgid()) != 0) {
541 > #else
542 >        {
543 > #endif
544 >                sg_set_error_with_errno(SG_ERROR_SETEGID, NULL);
545 >                return -1;
546 >        }
547 > #ifdef HAVE_SETEUID
548 >        if (seteuid(getuid()) != 0) {
549 > #elif defined(HAVE_SETRESUID)
550 >        if (setresuid(getuid(), getuid(), getuid()) != 0) {
551 > #else
552 >        {
553 > #endif
554 >                sg_set_error_with_errno(SG_ERROR_SETEUID, NULL);
555 >                return -1;
556 >        }
557          return 0;
558   }
559  
560 + void *sg_realloc(void *ptr, size_t size) {
561 +        void *tmp = NULL;
562 +        tmp = realloc(ptr, size);
563 +        if(tmp == NULL) {
564 +                sg_set_error_with_errno(SG_ERROR_MALLOC, NULL);
565 +        }
566 +        return tmp;
567 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines