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.45 by ats, Tue Apr 6 17:55:16 2004 UTC vs.
Revision 1.52 by tdb, Thu Apr 8 10:56:13 2004 UTC

# 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 45 | Line 44
44   #endif
45  
46   #include "tools.h"
47 + #include "statgrab.h"
48  
49   #ifdef SOLARIS
50   #ifdef HAVE_LIBDEVINFO_H
# Line 90 | Line 90 | static void add_mapping(char *bsd, char *svr){
90          mapping_t *map_end_ptr;
91  
92          if (mapping == NULL){
93 <                mapping = malloc(sizeof(mapping_t));
93 >                mapping = sg_malloc(sizeof(mapping_t));
94                  if (mapping == NULL) return;
95                  map_ptr = mapping;
96          }else{
# Line 105 | Line 105 | static void add_mapping(char *bsd, char *svr){
105                  /* We've reached end of list and not found the entry.. So we need to malloc
106                   * new mapping_t
107                   */
108 <                map_end_ptr->next = malloc(sizeof(mapping_t));
108 >                map_end_ptr->next = sg_malloc(sizeof(mapping_t));
109                  if (map_end_ptr->next == NULL) return;
110                  map_ptr = map_end_ptr->next;
111          }
112  
113          map_ptr->next = NULL;
114 <        map_ptr->bsd = strdup(bsd);
115 <        map_ptr->svr = strdup(svr);
114 >        map_ptr->bsd = NULL;
115 >        map_ptr->svr = NULL;
116 >        if (sg_update_string(&map_ptr->bsd, bsd) < 0
117 >            || sg_update_string(&map_ptr->svr, svr) < 0) {
118 >                return;
119 >        }
120  
121          return;
122   }
# Line 122 | Line 126 | static char *read_dir(char *disk_path){
126          DIR *dirp;
127          struct dirent *dp;
128          struct stat stbuf;
129 <        char *svr_name;
129 >        char *svr_name = NULL;
130          char current_dir[MAXPATHLEN];
131          char file_name[MAXPATHLEN];
132          char temp_name[MAXPATHLEN];
# Line 149 | Line 153 | static char *read_dir(char *disk_path){
153                          x = readlink(dir_dname, file_name, sizeof(file_name));
154                          file_name[x] = '\0';
155                          if (strcmp(file_name, temp_name) == 0) {
156 <                                svr_name = strdup(dp->d_name);
156 >                                if (sg_update_string(&svr_name,
157 >                                                     dp->d_name) < 0) {
158 >                                        return NULL;
159 >                                }
160                                  closedir(dirp);
161                                  return svr_name;
162                          }
# Line 258 | Line 265 | static int build_mapping(){
265  
266   #endif
267  
268 <
262 <
268 > #if defined(LINUX) || defined(CYGWIN)
269   char *sg_f_read_line(FILE *f, const char *string){
270          /* Max line length. 8k should be more than enough */
271          static char line[8192];
# Line 270 | Line 276 | char *sg_f_read_line(FILE *f, const char *string){
276                  }
277          }
278  
279 +        sg_set_error(SG_ERROR_PARSE, NULL);
280          return NULL;
281   }
282  
283   char *sg_get_string_match(char *line, regmatch_t *match){
284          int len=match->rm_eo - match->rm_so;
285 <        char *match_string=malloc(len+1);
285 >        char *match_string=sg_malloc(len+1);
286  
287          match_string=strncpy(match_string, line+match->rm_so, len);
288          match_string[len]='\0';
# Line 283 | Line 290 | char *sg_get_string_match(char *line, regmatch_t *matc
290          return match_string;
291   }
292  
293 + long long sg_get_ll_match(char *line, regmatch_t *match){
294 +        char *ptr;
295 +        long long num;
296  
297 +        ptr=line+match->rm_so;
298 +        num=atoll(ptr);
299  
300 +        return num;
301 + }
302 + #endif
303 +
304   #ifndef HAVE_ATOLL
305   static long long atoll(const char *s) {
306          long long value = 0;
# Line 329 | Line 345 | static long long atoll(const char *s) {
345   * Returns strlen(src); if retval >= siz, truncation occurred.
346   */
347   size_t sg_strlcpy(char *dst, const char *src, size_t siz){
348 <        register char *d = dst;
349 <        register const char *s = src;
350 <        register size_t n = siz;
348 >        register char *d = dst;
349 >        register const char *s = src;
350 >        register size_t n = siz;
351  
352 <        /* Copy as many bytes as will fit */
353 <        if (n != 0 && --n != 0) {
354 <                do {
355 <                        if ((*d++ = *s++) == 0)
356 <                                break;
357 <                } while (--n != 0);
358 <        }
352 >        /* Copy as many bytes as will fit */
353 >        if (n != 0 && --n != 0) {
354 >                do {
355 >                        if ((*d++ = *s++) == 0)
356 >                                break;
357 >                } while (--n != 0);
358 >        }
359  
360 <        /* Not enough room in dst, add NUL and traverse rest of src */
361 <        if (n == 0) {
362 <                if (siz != 0)
363 <                        *d = '\0';              /* NUL-terminate dst */
364 <                while (*s++)
365 <                        ;
366 <        }
360 >        /* Not enough room in dst, add NUL and traverse rest of src */
361 >        if (n == 0) {
362 >                if (siz != 0)
363 >                        *d = '\0';            /* NUL-terminate dst */
364 >                while (*s++)
365 >                        ;
366 >        }
367  
368 <        return(s - src - 1);    /* count does not include NUL */
368 >        return(s - src - 1);    /* count does not include NUL */
369   }
370  
371   /*      $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $    */
# Line 378 | Line 394 | size_t sg_strlcpy(char *dst, const char *src, size_t s
394   * If retval >= siz, truncation occurred.
395   */
396   size_t sg_strlcat(char *dst, const char *src, size_t siz){
397 <        register char *d = dst;
398 <        register const char *s = src;
399 <        register size_t n = siz;
400 <        size_t dlen;
397 >        register char *d = dst;
398 >        register const char *s = src;
399 >        register size_t n = siz;
400 >        size_t dlen;
401  
402 <        /* Find the end of dst and adjust bytes left but don't go past end */
403 <        while (n-- != 0 && *d != '\0')
404 <                d++;
405 <        dlen = d - dst;
406 <        n = siz - dlen;
402 >        /* Find the end of dst and adjust bytes left but don't go past end */
403 >        while (n-- != 0 && *d != '\0')
404 >                d++;
405 >        dlen = d - dst;
406 >        n = siz - dlen;
407  
408 <        if (n == 0)
409 <                return(dlen + strlen(s));
410 <        while (*s != '\0') {
411 <                if (n != 1) {
412 <                        *d++ = *s;
413 <                        n--;
414 <                }
415 <                s++;
416 <        }
417 <        *d = '\0';
408 >        if (n == 0)
409 >                return(dlen + strlen(s));
410 >        while (*s != '\0') {
411 >                if (n != 1) {
412 >                        *d++ = *s;
413 >                        n--;
414 >                }
415 >                s++;
416 >        }
417 >        *d = '\0';
418  
419 <        return(dlen + (s - src));       /* count does not include NUL */
419 >        return(dlen + (s - src));       /* count does not include NUL */
420   }
421  
422   int sg_update_string(char **dest, const char *src) {
# Line 413 | Line 429 | int sg_update_string(char **dest, const char *src) {
429                  return 0;
430          }
431  
432 <        new = realloc(*dest, strlen(src) + 1);
432 >        new = sg_realloc(*dest, strlen(src) + 1);
433          if (new == NULL) {
434                  return -1;
435          }
# Line 423 | Line 439 | int sg_update_string(char **dest, const char *src) {
439          return 0;
440   }
441  
426 long long sg_get_ll_match(char *line, regmatch_t *match){
427        char *ptr;
428        long long num;
429
430        ptr=line+match->rm_so;
431        num=atoll(ptr);
432
433        return num;
434 }
435
442   #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
443   kvm_t *sg_get_kvm() {
444          static kvm_t *kvmd = NULL;
# Line 442 | Line 448 | kvm_t *sg_get_kvm() {
448          }
449  
450          kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
451 +        if(kvmd == NULL) {
452 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
453 +        }
454          return kvmd;
455   }
456  
# Line 454 | Line 463 | kvm_t *sg_get_kvm2() {
463          }
464  
465          kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
466 +        if(kvmd2 == NULL) {
467 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
468 +        }
469          return kvmd2;
470   }
471   #endif
# Line 469 | Line 481 | struct uvmexp *sg_get_uvmexp() {
481          mib[1] = VM_UVMEXP;
482  
483          if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) {
484 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP");
485                  return NULL;
486          }
487  
# Line 498 | Line 511 | int sg_init(){
511   }
512  
513   int sg_drop_privileges() {
514 <        if (setegid(getgid()) != 0) return -1;
515 <        if (seteuid(getuid()) != 0) return -1;
514 >        if (setegid(getgid()) != 0) {
515 >                sg_set_error(SG_ERROR_SETEGID, NULL);
516 >                return -1;
517 >        }
518 >        if (seteuid(getuid()) != 0) {
519 >                sg_set_error(SG_ERROR_SETEUID, NULL);
520 >                return -1;
521 >        }
522          return 0;
523 + }
524 +
525 + void *sg_realloc(void *ptr, size_t size) {
526 +        void *tmp = NULL;
527 +        tmp = realloc(ptr, size);
528 +        if(tmp == NULL) {
529 +                sg_set_error(SG_ERROR_MALLOC, NULL);
530 +        }
531 +        return tmp;
532   }
533  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines