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.19 by pajs, Fri Jan 9 16:19:58 2004 UTC vs.
Revision 1.51 by tdb, Wed Apr 7 21:08:40 2004 UTC

# Line 1 | Line 1
1 < /*
2 < * i-scream central monitoring system
1 > /*
2 > * i-scream libstatgrab
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
4 > * Copyright (C) 2000-2004 i-scream
5   *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU General Public License
8 < * as published by the Free Software Foundation; either version 2
9 < * of the License, or (at your option) any later version.
6 > * This library is free software; you can redistribute it and/or
7 > * modify it under the terms of the GNU Lesser General Public
8 > * License as published by the Free Software Foundation; either
9 > * version 2.1 of the License, or (at your option) any later version.
10   *
11 < * This program is distributed in the hope that it will be useful,
11 > * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 < * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 < * GNU General Public License for more details.
13 > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 > * Lesser General Public License for more details.
15   *
16 < * You should have received a copy of the GNU General Public License
17 < * along with this program; if not, write to the Free Software
18 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 > * You should have received a copy of the GNU Lesser General Public
17 > * License along with this library; if not, write to the Free Software
18 > * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 > * 02111-1307 USA
20 > *
21 > * $Id$
22   */
23  
24   #ifdef HAVE_CONFIG_H
# Line 30 | Line 33
33   #include <regex.h>
34   #ifdef ALLBSD
35   #include <fcntl.h>
36 + #endif
37 + #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
38   #include <kvm.h>
39 + #include <paths.h>
40   #endif
41 < #ifdef NETBSD
41 > #if defined(NETBSD) || defined(OPENBSD)
42   #include <uvm/uvm_extern.h>
43 + #include <sys/param.h>
44 + #include <sys/sysctl.h>
45   #endif
46  
47   #include "tools.h"
48 + #include "statgrab.h"
49  
50   #ifdef SOLARIS
51 + #ifdef HAVE_LIBDEVINFO_H
52   #include <libdevinfo.h>
53 + #endif
54   #include <kstat.h>
55   #include <unistd.h>
56   #include <ctype.h>
# Line 51 | Line 62
62   #include <dirent.h>
63   #endif
64  
65 < #ifdef SOLARIS
65 > #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
66   struct map{
67          char *bsd;
68          char *svr;
# Line 61 | Line 72 | struct map{
72   typedef struct map mapping_t;
73  
74   static mapping_t *mapping = NULL;
75 + #endif
76  
77 < char *get_svr_from_bsd(char *bsd){
77 > #ifdef SOLARIS
78 > const char *sg_get_svr_from_bsd(const char *bsd){
79 > #ifdef HAVE_LIBDEVINFO_H
80          mapping_t *map_ptr;
81          for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next)
82                  if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr;
83 <
83 > #endif
84          return bsd;
85   }
86 + #endif
87  
88 < void add_mapping(char *bsd, char *svr){
88 > #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
89 > static void add_mapping(char *bsd, char *svr){
90          mapping_t *map_ptr;
91          mapping_t *map_end_ptr;
92  
77        bsd = strdup(bsd);
78        svr = strdup(svr);
79
93          if (mapping == NULL){
94 <                mapping = malloc(sizeof(mapping_t));
94 >                mapping = sg_malloc(sizeof(mapping_t));
95                  if (mapping == NULL) return;
96                  map_ptr = mapping;
97          }else{
# Line 93 | Line 106 | void add_mapping(char *bsd, char *svr){
106                  /* We've reached end of list and not found the entry.. So we need to malloc
107                   * new mapping_t
108                   */
109 <                map_end_ptr->next = malloc(sizeof(mapping_t));
109 >                map_end_ptr->next = sg_malloc(sizeof(mapping_t));
110                  if (map_end_ptr->next == NULL) return;
111                  map_ptr = map_end_ptr->next;
112          }
113  
114          map_ptr->next = NULL;
115 <        map_ptr->bsd = bsd;
116 <        map_ptr->svr = svr;
115 >        map_ptr->bsd = NULL;
116 >        map_ptr->svr = NULL;
117 >        if (sg_update_string(&map_ptr->bsd, bsd) < 0
118 >            || sg_update_string(&map_ptr->svr, svr) < 0) {
119 >                return;
120 >        }
121  
122          return;
123   }
124  
125 < char *read_dir(char *disk_path){
125 >
126 > static char *read_dir(char *disk_path){
127          DIR *dirp;
128          struct dirent *dp;
129          struct stat stbuf;
130 <        char *svr_name;
130 >        char *svr_name = NULL;
131          char current_dir[MAXPATHLEN];
132          char file_name[MAXPATHLEN];
133          char temp_name[MAXPATHLEN];
# Line 136 | Line 154 | char *read_dir(char *disk_path){
154                          x = readlink(dir_dname, file_name, sizeof(file_name));
155                          file_name[x] = '\0';
156                          if (strcmp(file_name, temp_name) == 0) {
157 <                                svr_name = strdup(dp->d_name);
157 >                                if (sg_update_string(&svr_name,
158 >                                                     dp->d_name) < 0) {
159 >                                        return NULL;
160 >                                }
161                                  closedir(dirp);
162                                  return svr_name;
163                          }
# Line 147 | Line 168 | char *read_dir(char *disk_path){
168   }
169  
170  
171 <
151 < int get_alias(char *alias){
171 > static int get_alias(char *alias){
172          char file[MAXPATHLEN];
173          di_node_t root_node;
174          di_node_t node;
# Line 159 | Line 179 | int get_alias(char *alias){
179          char *value;
180          int instance;
181          if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
182 <                return 1;
182 >                return -1;
183          }
184          node = di_drv_first_node(alias, root_node);
185          while (node != DI_NODE_NIL) {
# Line 169 | Line 189 | int get_alias(char *alias){
189                          minor_name = di_minor_name(minor);
190                          strcpy(tmpnode, alias);
191                          sprintf(tmpnode, "%s%d", tmpnode, instance);
192 <                        strlcpy(file, "/devices", sizeof file);
193 <                        strlcat(file, phys_path, sizeof file);
194 <                        strlcat(file, ":", sizeof file);
195 <                        strlcat(file, minor_name, sizeof file);
192 >                        sg_strlcpy(file, "/devices", sizeof file);
193 >                        sg_strlcat(file, phys_path, sizeof file);
194 >                        sg_strlcat(file, ":", sizeof file);
195 >                        sg_strlcat(file, minor_name, sizeof file);
196                          value = read_dir(file);
197                          if (value != NULL){
198                                  add_mapping(tmpnode, value);
# Line 187 | Line 207 | int get_alias(char *alias){
207          return 0;
208   }
209  
210 < int build_mapping(){
211 <        char device_name[512];
210 >
211 > #define BIG_ENOUGH 512
212 > static int build_mapping(){
213 >        char device_name[BIG_ENOUGH];
214          int x;
215          kstat_ctl_t *kc;
216          kstat_t *ksp;
217          kstat_io_t kios;
218  
219 +        char driver_list[BIG_ENOUGH][BIG_ENOUGH];
220 +        int list_entries = 0;
221 +        int found;
222 +
223          if ((kc = kstat_open()) == NULL) {
224 <                return;
224 >                return -1;
225          }
226  
227          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
# Line 210 | Line 236 | int build_mapping(){
236                          }
237                          if(x == sizeof device_name) x--;
238                          device_name[x] = '\0';
239 <                        if((get_alias(device_name)) != 0){
240 <                                return 1;
239 >
240 >                        /* Check if we've not already looked it up */
241 >                        found = 0;
242 >                        for(x=0;x<list_entries;x++){
243 >                                if (x>=BIG_ENOUGH){
244 >                                        /* We've got bigger than we thought was massive */
245 >                                        /* If we hit this.. Make big enough bigger */
246 >                                        return -1;
247 >                                }
248 >                                if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){
249 >                                        found = 1;
250 >                                        break;
251 >                                }
252                          }
253 +
254 +                        if(!found){
255 +                                if((get_alias(device_name)) != 0){
256 +                                        return -1;
257 +                                }
258 +                                strncpy(driver_list[x], device_name, BIG_ENOUGH);
259 +                                list_entries++;
260 +                        }
261                  }
262          }
263  
# Line 222 | Line 267 | int build_mapping(){
267   #endif
268  
269  
270 <
226 < char *f_read_line(FILE *f, const char *string){
270 > char *sg_f_read_line(FILE *f, const char *string){
271          /* Max line length. 8k should be more than enough */
272          static char line[8192];
273  
# Line 233 | Line 277 | char *f_read_line(FILE *f, const char *string){
277                  }
278          }
279  
280 +        sg_set_error(SG_ERROR_PARSE, NULL);
281          return NULL;
282   }
283  
284 < char *get_string_match(char *line, regmatch_t *match){
284 > char *sg_get_string_match(char *line, regmatch_t *match){
285          int len=match->rm_eo - match->rm_so;
286 <        char *match_string=malloc(len+1);
286 >        char *match_string=sg_malloc(len+1);
287  
288          match_string=strncpy(match_string, line+match->rm_so, len);
289          match_string[len]='\0';
# Line 247 | Line 292 | char *get_string_match(char *line, regmatch_t *match){
292   }
293  
294  
250
295   #ifndef HAVE_ATOLL
296   static long long atoll(const char *s) {
297          long long value = 0;
# Line 268 | Line 312 | static long long atoll(const char *s) {
312   }
313   #endif
314  
315 < #ifndef HAVE_STRLCPY
272 < *      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
315 > /*      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
316  
317   /*
318   * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
# Line 292 | Line 335 | static long long atoll(const char *s) {
335   * will be copied.  Always NUL terminates (unless siz == 0).
336   * Returns strlen(src); if retval >= siz, truncation occurred.
337   */
338 < size_t strlcpy(char *dst, const char *src, size_t siz){
339 <        register char *d = dst;
340 <        register const char *s = src;
341 <        register size_t n = siz;
338 > size_t sg_strlcpy(char *dst, const char *src, size_t siz){
339 >        register char *d = dst;
340 >        register const char *s = src;
341 >        register size_t n = siz;
342  
343 <        /* Copy as many bytes as will fit */
344 <        if (n != 0 && --n != 0) {
345 <                do {
346 <                        if ((*d++ = *s++) == 0)
347 <                                break;
348 <                } while (--n != 0);
349 <        }
343 >        /* Copy as many bytes as will fit */
344 >        if (n != 0 && --n != 0) {
345 >                do {
346 >                        if ((*d++ = *s++) == 0)
347 >                                break;
348 >                } while (--n != 0);
349 >        }
350  
351 <        /* Not enough room in dst, add NUL and traverse rest of src */
352 <        if (n == 0) {
353 <                if (siz != 0)
354 <                        *d = '\0';              /* NUL-terminate dst */
355 <                while (*s++)
356 <                        ;
357 <        }
351 >        /* Not enough room in dst, add NUL and traverse rest of src */
352 >        if (n == 0) {
353 >                if (siz != 0)
354 >                        *d = '\0';            /* NUL-terminate dst */
355 >                while (*s++)
356 >                        ;
357 >        }
358  
359 <        return(s - src - 1);    /* count does not include NUL */
359 >        return(s - src - 1);    /* count does not include NUL */
360   }
318 #endif
361  
320 #ifndef HAVE_STRLCAT
362   /*      $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $    */
363  
364   /*
# Line 343 | Line 384 | size_t strlcpy(char *dst, const char *src, size_t siz)
384   * Returns strlen(src) + MIN(siz, strlen(initial dst)).
385   * If retval >= siz, truncation occurred.
386   */
387 < size_t strlcat(char *dst, const char *src, size_t siz){
388 <        register char *d = dst;
389 <        register const char *s = src;
390 <        register size_t n = siz;
391 <        size_t dlen;
387 > size_t sg_strlcat(char *dst, const char *src, size_t siz){
388 >        register char *d = dst;
389 >        register const char *s = src;
390 >        register size_t n = siz;
391 >        size_t dlen;
392  
393 <        /* Find the end of dst and adjust bytes left but don't go past end */
394 <        while (n-- != 0 && *d != '\0')
395 <                d++;
396 <        dlen = d - dst;
397 <        n = siz - dlen;
393 >        /* Find the end of dst and adjust bytes left but don't go past end */
394 >        while (n-- != 0 && *d != '\0')
395 >                d++;
396 >        dlen = d - dst;
397 >        n = siz - dlen;
398  
399 <        if (n == 0)
400 <                return(dlen + strlen(s));
401 <        while (*s != '\0') {
402 <                if (n != 1) {
403 <                        *d++ = *s;
404 <                        n--;
405 <                }
406 <                s++;
407 <        }
408 <        *d = '\0';
399 >        if (n == 0)
400 >                return(dlen + strlen(s));
401 >        while (*s != '\0') {
402 >                if (n != 1) {
403 >                        *d++ = *s;
404 >                        n--;
405 >                }
406 >                s++;
407 >        }
408 >        *d = '\0';
409  
410 <        return(dlen + (s - src));       /* count does not include NUL */
410 >        return(dlen + (s - src));       /* count does not include NUL */
411   }
412  
413 < #endif
413 > int sg_update_string(char **dest, const char *src) {
414 >        char *new;
415  
416 < long long get_ll_match(char *line, regmatch_t *match){
416 >        if (src == NULL) {
417 >                /* We're being told to set it to NULL. */
418 >                free(*dest);
419 >                *dest = NULL;
420 >                return 0;
421 >        }
422 >
423 >        new = sg_realloc(*dest, strlen(src) + 1);
424 >        if (new == NULL) {
425 >                return -1;
426 >        }
427 >
428 >        strcpy(new, src);
429 >        *dest = new;
430 >        return 0;
431 > }
432 >
433 > long long sg_get_ll_match(char *line, regmatch_t *match){
434          char *ptr;
435          long long num;
436  
# Line 381 | Line 440 | long long get_ll_match(char *line, regmatch_t *match){
440          return num;
441   }
442  
443 < #ifdef ALLBSD
444 < kvm_t *get_kvm() {
443 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
444 > kvm_t *sg_get_kvm() {
445          static kvm_t *kvmd = NULL;
446  
447          if (kvmd != NULL) {
# Line 390 | Line 449 | kvm_t *get_kvm() {
449          }
450  
451          kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
452 +        if(kvmd == NULL) {
453 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
454 +        }
455          return kvmd;
456   }
395 #endif
457  
458 < #ifdef NETBSD
459 < struct uvmexp *get_uvmexp() {
460 <        static u_long addr = 0;
400 <        static struct uvmexp uvm;
401 <        kvm_t *kvmd = get_kvm();
458 > /* Can't think of a better name for this function */
459 > kvm_t *sg_get_kvm2() {
460 >        static kvm_t *kvmd2 = NULL;
461  
462 <        if (kvmd == NULL) {
463 <                return NULL;
462 >        if (kvmd2 != NULL) {
463 >                return kvmd2;
464          }
465  
466 <        if (addr == 0) {
467 <                struct nlist symbols[] = {
468 <                        { "_uvmexp" },
410 <                        { NULL }
411 <                };
412 <
413 <                if (kvm_nlist(kvmd, symbols) != 0) {
414 <                        return NULL;
415 <                }
416 <                addr = symbols[0].n_value;
466 >        kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
467 >        if(kvmd2 == NULL) {
468 >                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
469          }
470 +        return kvmd2;
471 + }
472 + #endif
473  
474 <        if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
474 > #if defined(NETBSD) || defined(OPENBSD)
475 > struct uvmexp *sg_get_uvmexp() {
476 >        int mib[2];
477 >        size_t size = sizeof(struct uvmexp);
478 >        static struct uvmexp uvm;
479 >        struct uvmexp *new;
480 >
481 >        mib[0] = CTL_VM;
482 >        mib[1] = VM_UVMEXP;
483 >
484 >        if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) {
485 >                sg_set_error(SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP");
486                  return NULL;
487          }
488 +
489          return &uvm;
490   }
491   #endif
492  
493 < int statgrab_init(){
494 < #ifdef ALLBSD
495 <        {
496 <                kvm_t *kvmd = get_kvm();
430 <                if (kvmd == NULL) return 1;
493 > int sg_init(){
494 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
495 >        if (sg_get_kvm() == NULL) {
496 >                return -1;
497          }
498 < #endif
499 < #ifdef NETBSD
434 <        {
435 <                struct uvmexp *uvm = get_uvmexp();
436 <                if (uvm == NULL) return 1;
498 >        if (sg_get_kvm2() == NULL) {
499 >                return -1;
500          }
501   #endif
502   #ifdef SOLARIS
503 <        if((build_mapping()) != 0) return 1;
503 >        /* On solaris 7, this will fail if you are not root. But, everything
504 >         * will still work, just no disk mappings. So we will ignore the exit
505 >         * status of this, and carry on merrily.
506 >         */
507 > #ifdef HAVE_LIBDEVINFO_H
508 >        build_mapping();
509   #endif
510 + #endif
511          return 0;
512   }
513  
514 < int statgrab_drop_privileges() {
515 <        if (setegid(getgid()) != 0) return -1;
516 <        if (seteuid(getuid()) != 0) return -1;
514 > int sg_drop_privileges() {
515 >        if (setegid(getgid()) != 0) {
516 >                sg_set_error(SG_ERROR_SETEGID, NULL);
517 >                return -1;
518 >        }
519 >        if (seteuid(getuid()) != 0) {
520 >                sg_set_error(SG_ERROR_SETEUID, NULL);
521 >                return -1;
522 >        }
523          return 0;
524 + }
525 +
526 + void *sg_realloc(void *ptr, size_t size) {
527 +        void *tmp = NULL;
528 +        tmp = realloc(ptr, size);
529 +        if(tmp == NULL) {
530 +                sg_set_error(SG_ERROR_MALLOC, NULL);
531 +        }
532 +        return tmp;
533   }
534  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines