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.40 by ats, Mon Apr 5 01:06:02 2004 UTC vs.
Revision 1.54 by tdb, Fri Jul 16 15:07:14 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 45 | Line 44
44   #endif
45  
46   #include "tools.h"
47 + #include "statgrab.h"
48  
49   #ifdef SOLARIS
50   #ifdef HAVE_LIBDEVINFO_H
# Line 74 | Line 74 | static mapping_t *mapping = NULL;
74   #endif
75  
76   #ifdef SOLARIS
77 < const char *get_svr_from_bsd(const char *bsd){
77 > const char *sg_get_svr_from_bsd(const char *bsd){
78   #ifdef HAVE_LIBDEVINFO_H
79          mapping_t *map_ptr;
80          for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next)
# Line 85 | Line 85 | const char *get_svr_from_bsd(const char *bsd){
85   #endif
86  
87   #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
88 < void add_mapping(char *bsd, char *svr){
88 > static void add_mapping(char *bsd, char *svr){
89          mapping_t *map_ptr;
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 | 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   }
123  
124 < char *read_dir(char *disk_path){
124 >
125 > 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 148 | Line 153 | 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 159 | Line 167 | char *read_dir(char *disk_path){
167   }
168  
169  
170 <
163 < int get_alias(char *alias){
170 > static int get_alias(char *alias){
171          char file[MAXPATHLEN];
172          di_node_t root_node;
173          di_node_t node;
# Line 171 | Line 178 | int get_alias(char *alias){
178          char *value;
179          int instance;
180          if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
181 <                return 1;
181 >                return -1;
182          }
183          node = di_drv_first_node(alias, root_node);
184          while (node != DI_NODE_NIL) {
# Line 181 | Line 188 | int get_alias(char *alias){
188                          minor_name = di_minor_name(minor);
189                          strcpy(tmpnode, alias);
190                          sprintf(tmpnode, "%s%d", tmpnode, instance);
191 <                        strlcpy(file, "/devices", sizeof file);
192 <                        strlcat(file, phys_path, sizeof file);
193 <                        strlcat(file, ":", sizeof file);
194 <                        strlcat(file, minor_name, sizeof file);
191 >                        sg_strlcpy(file, "/devices", sizeof file);
192 >                        sg_strlcat(file, phys_path, sizeof file);
193 >                        sg_strlcat(file, ":", sizeof file);
194 >                        sg_strlcat(file, minor_name, sizeof file);
195                          value = read_dir(file);
196                          if (value != NULL){
197                                  add_mapping(tmpnode, value);
# Line 201 | Line 208 | int get_alias(char *alias){
208  
209  
210   #define BIG_ENOUGH 512
211 < int build_mapping(){
211 > static int build_mapping(){
212          char device_name[BIG_ENOUGH];
213          int x;
214          kstat_ctl_t *kc;
# Line 213 | Line 220 | int build_mapping(){
220          int found;
221  
222          if ((kc = kstat_open()) == NULL) {
223 <                return 1;
223 >                return -1;
224          }
225  
226          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
# Line 235 | Line 242 | int build_mapping(){
242                                  if (x>=BIG_ENOUGH){
243                                          /* We've got bigger than we thought was massive */
244                                          /* If we hit this.. Make big enough bigger */
245 <                                        return 1;
245 >                                        return -1;
246                                  }
247                                  if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){
248                                          found = 1;
# Line 245 | Line 252 | int build_mapping(){
252  
253                          if(!found){
254                                  if((get_alias(device_name)) != 0){
255 <                                        return 1;
255 >                                        return -1;
256                                  }
257                                  strncpy(driver_list[x], device_name, BIG_ENOUGH);
258                                  list_entries++;
# Line 258 | Line 265 | int build_mapping(){
265  
266   #endif
267  
268 <
269 <
263 < char *f_read_line(FILE *f, const char *string){
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];
272  
# Line 270 | Line 276 | char *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 *get_string_match(char *line, regmatch_t *match){
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 *get_string_match(char *line, regmatch_t *match){
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 305 | Line 321 | static long long atoll(const char *s) {
321   }
322   #endif
323  
308 #ifndef HAVE_STRLCPY
324   /*      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
325  
326   /*
# Line 329 | Line 344 | static long long atoll(const char *s) {
344   * will be copied.  Always NUL terminates (unless siz == 0).
345   * Returns strlen(src); if retval >= siz, truncation occurred.
346   */
347 < size_t 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;
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;
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   }
355 #endif
370  
357 #ifndef HAVE_STRLCAT
371   /*      $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $    */
372  
373   /*
# Line 380 | Line 393 | size_t strlcpy(char *dst, const char *src, size_t siz)
393   * Returns strlen(src) + MIN(siz, strlen(initial dst)).
394   * If retval >= siz, truncation occurred.
395   */
396 < size_t 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;
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;
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 < #endif
410 <
411 < char *update_string(char **dest, const char *src) {
422 > int sg_update_string(char **dest, const char *src) {
423          char *new;
424  
425 <        new = realloc(*dest, strlen(src) + 1);
425 >        if (src == NULL) {
426 >                /* We're being told to set it to NULL. */
427 >                free(*dest);
428 >                *dest = NULL;
429 >                return 0;
430 >        }
431 >
432 >        new = sg_realloc(*dest, strlen(src) + 1);
433          if (new == NULL) {
434 <                return NULL;
434 >                return -1;
435          }
436  
437          strcpy(new, src);
438          *dest = new;
439 <        return new;
439 >        return 0;
440   }
441  
424 long long get_ll_match(char *line, regmatch_t *match){
425        char *ptr;
426        long long num;
427
428        ptr=line+match->rm_so;
429        num=atoll(ptr);
430
431        return num;
432 }
433
442   #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
443 < kvm_t *get_kvm() {
443 > kvm_t *sg_get_kvm() {
444          static kvm_t *kvmd = NULL;
445  
446          if (kvmd != NULL) {
# Line 440 | Line 448 | kvm_t *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  
457   /* Can't think of a better name for this function */
458 < kvm_t *get_kvm2() {
458 > kvm_t *sg_get_kvm2() {
459          static kvm_t *kvmd2 = NULL;
460  
461          if (kvmd2 != NULL) {
# Line 452 | Line 463 | kvm_t *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
472  
473   #if defined(NETBSD) || defined(OPENBSD)
474 < struct uvmexp *get_uvmexp() {
474 > struct uvmexp *sg_get_uvmexp() {
475          int mib[2];
476 <        size_t size;
477 <        static struct uvmexp *uvm = NULL;
476 >        size_t size = sizeof(struct uvmexp);
477 >        static struct uvmexp uvm;
478          struct uvmexp *new;
479  
480          mib[0] = CTL_VM;
481          mib[1] = VM_UVMEXP;
482  
483 <        if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) {
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  
488 <        new = realloc(uvm, size);
474 <        if (new == NULL) {
475 <                return NULL;
476 <        }
477 <        uvm = new;
478 <
479 <        if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) {
480 <                return NULL;
481 <        }
482 <
483 <        return uvm;
488 >        return &uvm;
489   }
490   #endif
491  
492 < int statgrab_init(){
492 > int sg_init(){
493 >        sg_set_error_with_errno(SG_ERROR_NONE, NULL, 0);
494 >
495   #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
496 <        {
497 <                kvm_t *kvmd = get_kvm();
491 <                if (kvmd == NULL) return 1;
496 >        if (sg_get_kvm() == NULL) {
497 >                return -1;
498          }
499 <        {
500 <                kvm_t *kvmd2 = get_kvm2();
495 <                if (kvmd2 == NULL) return 1;
499 >        if (sg_get_kvm2() == NULL) {
500 >                return -1;
501          }
502   #endif
498 #if defined(NETBSD) || defined(OPENBSD)
499        {
500                /* This should always succeed, but it seems that on some
501                 * versions of NetBSD the first call to get_uvmexp will return
502                 * a non-filled-in structure; this is a workaround for that.
503                 */
504                struct uvmexp *uvm = get_uvmexp();
505                if (uvm == NULL) return 1;
506        }
507 #endif
503   #ifdef SOLARIS
504          /* On solaris 7, this will fail if you are not root. But, everything
505           * will still work, just no disk mappings. So we will ignore the exit
# Line 517 | Line 512 | int statgrab_init(){
512          return 0;
513   }
514  
515 < int statgrab_drop_privileges() {
516 <        if (setegid(getgid()) != 0) return -1;
517 <        if (seteuid(getuid()) != 0) return -1;
515 > int sg_drop_privileges() {
516 >        if (setegid(getgid()) != 0) {
517 >                sg_set_error(SG_ERROR_SETEGID, NULL);
518 >                return -1;
519 >        }
520 >        if (seteuid(getuid()) != 0) {
521 >                sg_set_error(SG_ERROR_SETEUID, NULL);
522 >                return -1;
523 >        }
524          return 0;
525 + }
526 +
527 + void *sg_realloc(void *ptr, size_t size) {
528 +        void *tmp = NULL;
529 +        tmp = realloc(ptr, size);
530 +        if(tmp == NULL) {
531 +                sg_set_error(SG_ERROR_MALLOC, NULL);
532 +        }
533 +        return tmp;
534   }
535  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines