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.22 by pajs, Thu Jan 15 22:21:37 2004 UTC vs.
Revision 1.61 by tdb, Sat Nov 6 23:54:12 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 27 | Line 30
30   #include <stdlib.h>
31   #include <unistd.h>
32   #include <sys/types.h>
30 #include <regex.h>
33   #ifdef ALLBSD
34   #include <fcntl.h>
35 + #endif
36 + #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
37   #include <kvm.h>
38 + #include <paths.h>
39   #endif
40 < #ifdef NETBSD
40 > #if defined(NETBSD) || defined(OPENBSD)
41   #include <uvm/uvm_extern.h>
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
55   #include <libdevinfo.h>
56 + #endif
57   #include <kstat.h>
58   #include <unistd.h>
59   #include <ctype.h>
# Line 51 | Line 65
65   #include <dirent.h>
66   #endif
67  
68 < #ifdef SOLARIS
68 > #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
69   struct map{
70          char *bsd;
71          char *svr;
# Line 61 | Line 75 | struct map{
75   typedef struct map mapping_t;
76  
77   static mapping_t *mapping = NULL;
78 + #endif
79  
80 < char *get_svr_from_bsd(char *bsd){
80 > #ifdef SOLARIS
81 > const char *sg_get_svr_from_bsd(const char *bsd){
82 > #ifdef HAVE_LIBDEVINFO_H
83          mapping_t *map_ptr;
84          for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next)
85                  if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr;
86 <
86 > #endif
87          return bsd;
88   }
89 + #endif
90  
91 < void add_mapping(char *bsd, char *svr){
91 > #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
92 > static void add_mapping(char *bsd, char *svr){
93          mapping_t *map_ptr;
94          mapping_t *map_end_ptr;
95  
77        bsd = strdup(bsd);
78        svr = strdup(svr);
79
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 93 | Line 109 | 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 = bsd;
119 <        map_ptr->svr = 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   }
127  
128 < char *read_dir(char *disk_path){
128 >
129 > 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 136 | Line 157 | 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 147 | Line 171 | char *read_dir(char *disk_path){
171   }
172  
173  
174 <
151 < int get_alias(char *alias){
174 > static int get_alias(char *alias){
175          char file[MAXPATHLEN];
176          di_node_t root_node;
177          di_node_t node;
# Line 159 | Line 182 | 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 169 | Line 192 | int get_alias(char *alias){
192                          minor_name = di_minor_name(minor);
193                          strcpy(tmpnode, alias);
194                          sprintf(tmpnode, "%s%d", tmpnode, instance);
195 <                        strlcpy(file, "/devices", sizeof file);
196 <                        strlcat(file, phys_path, sizeof file);
197 <                        strlcat(file, ":", sizeof file);
198 <                        strlcat(file, minor_name, sizeof file);
195 >                        sg_strlcpy(file, "/devices", sizeof file);
196 >                        sg_strlcat(file, phys_path, sizeof file);
197 >                        sg_strlcat(file, ":", sizeof file);
198 >                        sg_strlcat(file, minor_name, sizeof file);
199                          value = read_dir(file);
200                          if (value != NULL){
201                                  add_mapping(tmpnode, value);
# Line 189 | Line 212 | int get_alias(char *alias){
212  
213  
214   #define BIG_ENOUGH 512
215 < int build_mapping(){
215 > static int build_mapping(){
216          char device_name[BIG_ENOUGH];
217          int x;
218          kstat_ctl_t *kc;
# Line 201 | Line 224 | int build_mapping(){
224          int found;
225  
226          if ((kc = kstat_open()) == NULL) {
227 <                return;
227 >                return -1;
228          }
229  
230          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
# Line 211 | Line 234 | int build_mapping(){
234                          if(strcmp(ksp->ks_module, "md")==0) continue;
235                          if((kstat_read(kc, ksp, &kios))==-1) continue;
236                          strncpy(device_name, ksp->ks_name, sizeof device_name);
237 <                        for(x=0;x<(sizeof device_name);x++){
237 >                        for(x=0;x<(int)(sizeof device_name);x++){
238                                  if( isdigit((int)device_name[x]) ) break;
239                          }
240                          if(x == sizeof device_name) x--;
# Line 223 | Line 246 | 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 233 | Line 256 | 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 246 | Line 269 | int build_mapping(){
269  
270   #endif
271  
272 <
273 <
251 < char *f_read_line(FILE *f, const char *string){
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];
276  
# Line 258 | Line 280 | char *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 *get_string_match(char *line, regmatch_t *match){
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 271 | Line 294 | char *get_string_match(char *line, regmatch_t *match){
294          return match_string;
295   }
296  
297 <
275 <
297 > /* FIXME do Linux and Cygwin always have atoll? */
298   #ifndef HAVE_ATOLL
299   static long long atoll(const char *s) {
300          long long value = 0;
# Line 293 | Line 315 | static long long atoll(const char *s) {
315   }
316   #endif
317  
318 < #ifndef HAVE_STRLCPY
318 > long long sg_get_ll_match(char *line, regmatch_t *match){
319 >        char *ptr;
320 >        long long num;
321 >
322 >        ptr=line+match->rm_so;
323 >        num=atoll(ptr);
324 >
325 >        return num;
326 > }
327 > #endif
328 >
329   /*      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
330  
331   /*
# Line 317 | Line 349 | static long long atoll(const char *s) {
349   * will be copied.  Always NUL terminates (unless siz == 0).
350   * Returns strlen(src); if retval >= siz, truncation occurred.
351   */
352 < size_t strlcpy(char *dst, const char *src, size_t siz){
353 <        register char *d = dst;
354 <        register const char *s = src;
355 <        register size_t n = siz;
352 > size_t sg_strlcpy(char *dst, const char *src, size_t siz){
353 >        register char *d = dst;
354 >        register const char *s = src;
355 >        register size_t n = siz;
356  
357 <        /* Copy as many bytes as will fit */
358 <        if (n != 0 && --n != 0) {
359 <                do {
360 <                        if ((*d++ = *s++) == 0)
361 <                                break;
362 <                } while (--n != 0);
363 <        }
357 >        /* Copy as many bytes as will fit */
358 >        if (n != 0 && --n != 0) {
359 >                do {
360 >                        if ((*d++ = *s++) == 0)
361 >                                break;
362 >                } while (--n != 0);
363 >        }
364  
365 <        /* Not enough room in dst, add NUL and traverse rest of src */
366 <        if (n == 0) {
367 <                if (siz != 0)
368 <                        *d = '\0';              /* NUL-terminate dst */
369 <                while (*s++)
370 <                        ;
371 <        }
365 >        /* Not enough room in dst, add NUL and traverse rest of src */
366 >        if (n == 0) {
367 >                if (siz != 0)
368 >                        *d = '\0';            /* NUL-terminate dst */
369 >                while (*s++)
370 >                        ;
371 >        }
372  
373 <        return(s - src - 1);    /* count does not include NUL */
373 >        return(s - src - 1);    /* count does not include NUL */
374   }
343 #endif
375  
345 #ifndef HAVE_STRLCAT
376   /*      $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $    */
377  
378   /*
# Line 368 | Line 398 | size_t strlcpy(char *dst, const char *src, size_t siz)
398   * Returns strlen(src) + MIN(siz, strlen(initial dst)).
399   * If retval >= siz, truncation occurred.
400   */
401 < size_t strlcat(char *dst, const char *src, size_t siz){
402 <        register char *d = dst;
403 <        register const char *s = src;
404 <        register size_t n = siz;
405 <        size_t dlen;
401 > size_t sg_strlcat(char *dst, const char *src, size_t siz){
402 >        register char *d = dst;
403 >        register const char *s = src;
404 >        register size_t n = siz;
405 >        size_t dlen;
406  
407 <        /* Find the end of dst and adjust bytes left but don't go past end */
408 <        while (n-- != 0 && *d != '\0')
409 <                d++;
410 <        dlen = d - dst;
411 <        n = siz - dlen;
407 >        /* Find the end of dst and adjust bytes left but don't go past end */
408 >        while (n-- != 0 && *d != '\0')
409 >                d++;
410 >        dlen = d - dst;
411 >        n = siz - dlen;
412  
413 <        if (n == 0)
414 <                return(dlen + strlen(s));
415 <        while (*s != '\0') {
416 <                if (n != 1) {
417 <                        *d++ = *s;
418 <                        n--;
419 <                }
420 <                s++;
421 <        }
422 <        *d = '\0';
413 >        if (n == 0)
414 >                return(dlen + strlen(s));
415 >        while (*s != '\0') {
416 >                if (n != 1) {
417 >                        *d++ = *s;
418 >                        n--;
419 >                }
420 >                s++;
421 >        }
422 >        *d = '\0';
423  
424 <        return(dlen + (s - src));       /* count does not include NUL */
424 >        return(dlen + (s - src));       /* count does not include NUL */
425   }
426  
427 < #endif
427 > int sg_update_string(char **dest, const char *src) {
428 >        char *new;
429  
430 < long long get_ll_match(char *line, regmatch_t *match){
431 <        char *ptr;
432 <        long long num;
430 >        if (src == NULL) {
431 >                /* We're being told to set it to NULL. */
432 >                free(*dest);
433 >                *dest = NULL;
434 >                return 0;
435 >        }
436  
437 <        ptr=line+match->rm_so;
438 <        num=atoll(ptr);
437 >        new = sg_realloc(*dest, strlen(src) + 1);
438 >        if (new == NULL) {
439 >                return -1;
440 >        }
441  
442 <        return num;
442 >        strcpy(new, src);
443 >        *dest = new;
444 >        return 0;
445   }
446  
447 < #ifdef ALLBSD
448 < kvm_t *get_kvm() {
447 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
448 > kvm_t *sg_get_kvm() {
449          static kvm_t *kvmd = NULL;
450  
451          if (kvmd != NULL) {
# Line 415 | Line 453 | kvm_t *get_kvm() {
453          }
454  
455          kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
456 +        if(kvmd == NULL) {
457 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
458 +        }
459          return kvmd;
460   }
461 +
462 + /* Can't think of a better name for this function */
463 + kvm_t *sg_get_kvm2() {
464 +        static kvm_t *kvmd2 = NULL;
465 +
466 +        if (kvmd2 != NULL) {
467 +                return kvmd2;
468 +        }
469 +
470 +        kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
471 +        if(kvmd2 == NULL) {
472 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
473 +        }
474 +        return kvmd2;
475 + }
476   #endif
477  
478 < #ifdef NETBSD
479 < struct uvmexp *get_uvmexp() {
480 <        static u_long addr = 0;
478 > #if defined(NETBSD) || defined(OPENBSD)
479 > struct uvmexp *sg_get_uvmexp() {
480 >        int mib[2];
481 >        size_t size = sizeof(struct uvmexp);
482          static struct uvmexp uvm;
483 <        kvm_t *kvmd = get_kvm();
483 >        struct uvmexp *new;
484  
485 <        if (kvmd == NULL) {
485 >        mib[0] = CTL_VM;
486 >        mib[1] = VM_UVMEXP;
487 >
488 >        if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) {
489 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP");
490                  return NULL;
491          }
492  
493 <        if (addr == 0) {
494 <                struct nlist symbols[] = {
495 <                        { "_uvmexp" },
435 <                        { NULL }
436 <                };
493 >        return &uvm;
494 > }
495 > #endif
496  
497 <                if (kvm_nlist(kvmd, symbols) != 0) {
497 > #ifdef HPUX
498 > struct pst_static *sg_get_pstat_static() {
499 >        static int got = 0;
500 >        static struct pst_static pst;
501 >
502 >        if (!got) {
503 >                if (pstat_getstatic(&pst, sizeof pst, 1, 0) == -1) {
504 >                        sg_set_error_with_errno(SG_ERROR_PSTAT,
505 >                                                "pstat_static");
506                          return NULL;
507                  }
508 <                addr = symbols[0].n_value;
508 >                got = 1;
509          }
510 <
444 <        if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
445 <                return NULL;
446 <        }
447 <        return &uvm;
510 >        return &pst;
511   }
512   #endif
513  
514 < int statgrab_init(){
515 < #ifdef ALLBSD
516 <        {
517 <                kvm_t *kvmd = get_kvm();
518 <                if (kvmd == NULL) return 1;
514 > int sg_init(){
515 >        sg_set_error(SG_ERROR_NONE, NULL);
516 >
517 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
518 >        if (sg_get_kvm() == NULL) {
519 >                return -1;
520          }
521 < #endif
522 < #ifdef NETBSD
459 <        {
460 <                struct uvmexp *uvm = get_uvmexp();
461 <                if (uvm == NULL) return 1;
521 >        if (sg_get_kvm2() == NULL) {
522 >                return -1;
523          }
524   #endif
525   #ifdef SOLARIS
# Line 466 | Line 527 | int statgrab_init(){
527           * will still work, just no disk mappings. So we will ignore the exit
528           * status of this, and carry on merrily.
529           */
530 + #ifdef HAVE_LIBDEVINFO_H
531          build_mapping();
532   #endif
533 + #endif
534          return 0;
535   }
536  
537 < int statgrab_drop_privileges() {
538 <        if (setegid(getgid()) != 0) return -1;
539 <        if (seteuid(getuid()) != 0) return -1;
537 > int sg_drop_privileges() {
538 > #ifdef HAVE_SETEGID
539 >        if (setegid(getgid()) != 0) {
540 > #elif defined(HAVE_SETRESGID)
541 >        if (setresgid(getgid(), getgid(), getgid()) != 0) {
542 > #else
543 >        {
544 > #endif
545 >                sg_set_error_with_errno(SG_ERROR_SETEGID, NULL);
546 >                return -1;
547 >        }
548 > #ifdef HAVE_SETEUID
549 >        if (seteuid(getuid()) != 0) {
550 > #elif defined(HAVE_SETRESUID)
551 >        if (setresuid(getuid(), getuid(), getuid()) != 0) {
552 > #else
553 >        {
554 > #endif
555 >                sg_set_error_with_errno(SG_ERROR_SETEUID, NULL);
556 >                return -1;
557 >        }
558          return 0;
559   }
560  
561 + void *sg_realloc(void *ptr, size_t size) {
562 +        void *tmp = NULL;
563 +        tmp = realloc(ptr, size);
564 +        if(tmp == NULL) {
565 +                sg_set_error_with_errno(SG_ERROR_MALLOC, NULL);
566 +        }
567 +        return tmp;
568 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines