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.64 by tdb, Mon Oct 9 14:09:38 2006 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 50 | Line 64
64   #include <sys/fcntl.h>
65   #include <dirent.h>
66   #endif
67 + #ifdef WIN32
68 + #include "win32.h"
69 + #endif
70  
71 < #ifdef SOLARIS
71 > #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
72   struct map{
73          char *bsd;
74          char *svr;
# Line 61 | Line 78 | struct map{
78   typedef struct map mapping_t;
79  
80   static mapping_t *mapping = NULL;
81 + #endif
82  
83 < char *get_svr_from_bsd(char *bsd){
83 > #ifdef SOLARIS
84 > const char *sg_get_svr_from_bsd(const char *bsd){
85 > #ifdef HAVE_LIBDEVINFO_H
86          mapping_t *map_ptr;
87          for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next)
88                  if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr;
89 <
89 > #endif
90          return bsd;
91   }
92 + #endif
93  
94 < void add_mapping(char *bsd, char *svr){
94 > #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
95 > static void add_mapping(char *bsd, char *svr){
96          mapping_t *map_ptr;
97          mapping_t *map_end_ptr;
98  
77        bsd = strdup(bsd);
78        svr = strdup(svr);
79
99          if (mapping == NULL){
100 <                mapping = malloc(sizeof(mapping_t));
100 >                mapping = sg_malloc(sizeof(mapping_t));
101                  if (mapping == NULL) return;
102                  map_ptr = mapping;
103          }else{
# Line 93 | Line 112 | void add_mapping(char *bsd, char *svr){
112                  /* We've reached end of list and not found the entry.. So we need to malloc
113                   * new mapping_t
114                   */
115 <                map_end_ptr->next = malloc(sizeof(mapping_t));
115 >                map_end_ptr->next = sg_malloc(sizeof(mapping_t));
116                  if (map_end_ptr->next == NULL) return;
117                  map_ptr = map_end_ptr->next;
118          }
119  
120          map_ptr->next = NULL;
121 <        map_ptr->bsd = bsd;
122 <        map_ptr->svr = svr;
121 >        map_ptr->bsd = NULL;
122 >        map_ptr->svr = NULL;
123 >        if (sg_update_string(&map_ptr->bsd, bsd) < 0
124 >            || sg_update_string(&map_ptr->svr, svr) < 0) {
125 >                return;
126 >        }
127  
128          return;
129   }
130  
131 < char *read_dir(char *disk_path){
131 >
132 > static char *read_dir(char *disk_path){
133          DIR *dirp;
134          struct dirent *dp;
135          struct stat stbuf;
136 <        char *svr_name;
136 >        char *svr_name = NULL;
137          char current_dir[MAXPATHLEN];
138          char file_name[MAXPATHLEN];
139          char temp_name[MAXPATHLEN];
# Line 136 | Line 160 | char *read_dir(char *disk_path){
160                          x = readlink(dir_dname, file_name, sizeof(file_name));
161                          file_name[x] = '\0';
162                          if (strcmp(file_name, temp_name) == 0) {
163 <                                svr_name = strdup(dp->d_name);
163 >                                if (sg_update_string(&svr_name,
164 >                                                     dp->d_name) < 0) {
165 >                                        return NULL;
166 >                                }
167                                  closedir(dirp);
168                                  return svr_name;
169                          }
# Line 147 | Line 174 | char *read_dir(char *disk_path){
174   }
175  
176  
177 <
151 < int get_alias(char *alias){
177 > static int get_alias(char *alias){
178          char file[MAXPATHLEN];
179          di_node_t root_node;
180          di_node_t node;
# Line 159 | Line 185 | int get_alias(char *alias){
185          char *value;
186          int instance;
187          if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
188 <                return 1;
188 >                return -1;
189          }
190          node = di_drv_first_node(alias, root_node);
191          while (node != DI_NODE_NIL) {
# Line 169 | Line 195 | int get_alias(char *alias){
195                          minor_name = di_minor_name(minor);
196                          strcpy(tmpnode, alias);
197                          sprintf(tmpnode, "%s%d", tmpnode, instance);
198 <                        strlcpy(file, "/devices", sizeof file);
199 <                        strlcat(file, phys_path, sizeof file);
200 <                        strlcat(file, ":", sizeof file);
201 <                        strlcat(file, minor_name, sizeof file);
198 >                        sg_strlcpy(file, "/devices", sizeof file);
199 >                        sg_strlcat(file, phys_path, sizeof file);
200 >                        sg_strlcat(file, ":", sizeof file);
201 >                        sg_strlcat(file, minor_name, sizeof file);
202                          value = read_dir(file);
203                          if (value != NULL){
204                                  add_mapping(tmpnode, value);
# Line 187 | Line 213 | int get_alias(char *alias){
213          return 0;
214   }
215  
216 < int build_mapping(){
217 <        char device_name[512];
216 >
217 > #define BIG_ENOUGH 512
218 > static int build_mapping(){
219 >        char device_name[BIG_ENOUGH];
220          int x;
221          kstat_ctl_t *kc;
222          kstat_t *ksp;
223          kstat_io_t kios;
224  
225 +        char driver_list[BIG_ENOUGH][BIG_ENOUGH];
226 +        int list_entries = 0;
227 +        int found;
228 +
229          if ((kc = kstat_open()) == NULL) {
230 <                return;
230 >                return -1;
231          }
232  
233          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
# Line 205 | Line 237 | int build_mapping(){
237                          if(strcmp(ksp->ks_module, "md")==0) continue;
238                          if((kstat_read(kc, ksp, &kios))==-1) continue;
239                          strncpy(device_name, ksp->ks_name, sizeof device_name);
240 <                        for(x=0;x<(sizeof device_name);x++){
240 >                        for(x=0;x<(int)(sizeof device_name);x++){
241                                  if( isdigit((int)device_name[x]) ) break;
242                          }
243                          if(x == sizeof device_name) x--;
244                          device_name[x] = '\0';
245 <                        if((get_alias(device_name)) != 0){
246 <                                return 1;
245 >
246 >                        /* Check if we've not already looked it up */
247 >                        found = 0;
248 >                        for(x=0;x<list_entries;x++){
249 >                                if (x>=BIG_ENOUGH){
250 >                                        /* We've got bigger than we thought was massive */
251 >                                        /* If we hit this.. Make big enough bigger */
252 >                                        kstat_close(kc);
253 >                                        return -1;
254 >                                }
255 >                                if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){
256 >                                        found = 1;
257 >                                        break;
258 >                                }
259                          }
260 +
261 +                        if(!found){
262 +                                if((get_alias(device_name)) != 0){
263 +                                        kstat_close(kc);
264 +                                        return -1;
265 +                                }
266 +                                strncpy(driver_list[x], device_name, BIG_ENOUGH);
267 +                                list_entries++;
268 +                        }
269                  }
270          }
271  
272 +        kstat_close(kc);
273 +
274          return 0;
275   }
276  
277   #endif
278  
279 <
280 <
226 < char *f_read_line(FILE *f, const char *string){
279 > #if defined(LINUX) || defined(CYGWIN)
280 > char *sg_f_read_line(FILE *f, const char *string){
281          /* Max line length. 8k should be more than enough */
282          static char line[8192];
283  
# Line 233 | Line 287 | char *f_read_line(FILE *f, const char *string){
287                  }
288          }
289  
290 +        sg_set_error(SG_ERROR_PARSE, NULL);
291          return NULL;
292   }
293  
294 < char *get_string_match(char *line, regmatch_t *match){
294 > char *sg_get_string_match(char *line, regmatch_t *match){
295          int len=match->rm_eo - match->rm_so;
296 <        char *match_string=malloc(len+1);
296 >        char *match_string=sg_malloc(len+1);
297  
298          match_string=strncpy(match_string, line+match->rm_so, len);
299          match_string[len]='\0';
# Line 246 | Line 301 | char *get_string_match(char *line, regmatch_t *match){
301          return match_string;
302   }
303  
304 <
250 <
304 > /* Cygwin (without a recent newlib) doesn't have atoll */
305   #ifndef HAVE_ATOLL
306   static long long atoll(const char *s) {
307          long long value = 0;
# Line 268 | Line 322 | static long long atoll(const char *s) {
322   }
323   #endif
324  
325 < #ifndef HAVE_STRLCPY
326 < *      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
325 > long long sg_get_ll_match(char *line, regmatch_t *match){
326 >        char *ptr;
327 >        long long num;
328  
329 +        ptr=line+match->rm_so;
330 +        num=atoll(ptr);
331 +
332 +        return num;
333 + }
334 + #endif
335 +
336 + /*      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
337 +
338   /*
339   * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
340   *
# Line 292 | Line 356 | static long long atoll(const char *s) {
356   * will be copied.  Always NUL terminates (unless siz == 0).
357   * Returns strlen(src); if retval >= siz, truncation occurred.
358   */
359 < size_t strlcpy(char *dst, const char *src, size_t siz){
360 <        register char *d = dst;
361 <        register const char *s = src;
362 <        register size_t n = siz;
359 > size_t sg_strlcpy(char *dst, const char *src, size_t siz){
360 >        register char *d = dst;
361 >        register const char *s = src;
362 >        register size_t n = siz;
363  
364 <        /* Copy as many bytes as will fit */
365 <        if (n != 0 && --n != 0) {
366 <                do {
367 <                        if ((*d++ = *s++) == 0)
368 <                                break;
369 <                } while (--n != 0);
370 <        }
364 >        /* Copy as many bytes as will fit */
365 >        if (n != 0 && --n != 0) {
366 >                do {
367 >                        if ((*d++ = *s++) == 0)
368 >                                break;
369 >                } while (--n != 0);
370 >        }
371  
372 <        /* Not enough room in dst, add NUL and traverse rest of src */
373 <        if (n == 0) {
374 <                if (siz != 0)
375 <                        *d = '\0';              /* NUL-terminate dst */
376 <                while (*s++)
377 <                        ;
378 <        }
372 >        /* Not enough room in dst, add NUL and traverse rest of src */
373 >        if (n == 0) {
374 >                if (siz != 0)
375 >                        *d = '\0';            /* NUL-terminate dst */
376 >                while (*s++)
377 >                        ;
378 >        }
379  
380 <        return(s - src - 1);    /* count does not include NUL */
380 >        return(s - src - 1);    /* count does not include NUL */
381   }
318 #endif
382  
320 #ifndef HAVE_STRLCAT
383   /*      $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $    */
384  
385   /*
# Line 343 | Line 405 | size_t strlcpy(char *dst, const char *src, size_t siz)
405   * Returns strlen(src) + MIN(siz, strlen(initial dst)).
406   * If retval >= siz, truncation occurred.
407   */
408 < size_t strlcat(char *dst, const char *src, size_t siz){
409 <        register char *d = dst;
410 <        register const char *s = src;
411 <        register size_t n = siz;
412 <        size_t dlen;
408 > size_t sg_strlcat(char *dst, const char *src, size_t siz){
409 >        register char *d = dst;
410 >        register const char *s = src;
411 >        register size_t n = siz;
412 >        size_t dlen;
413  
414 <        /* Find the end of dst and adjust bytes left but don't go past end */
415 <        while (n-- != 0 && *d != '\0')
416 <                d++;
417 <        dlen = d - dst;
418 <        n = siz - dlen;
414 >        /* Find the end of dst and adjust bytes left but don't go past end */
415 >        while (n-- != 0 && *d != '\0')
416 >                d++;
417 >        dlen = d - dst;
418 >        n = siz - dlen;
419  
420 <        if (n == 0)
421 <                return(dlen + strlen(s));
422 <        while (*s != '\0') {
423 <                if (n != 1) {
424 <                        *d++ = *s;
425 <                        n--;
426 <                }
427 <                s++;
428 <        }
429 <        *d = '\0';
420 >        if (n == 0)
421 >                return(dlen + strlen(s));
422 >        while (*s != '\0') {
423 >                if (n != 1) {
424 >                        *d++ = *s;
425 >                        n--;
426 >                }
427 >                s++;
428 >        }
429 >        *d = '\0';
430  
431 <        return(dlen + (s - src));       /* count does not include NUL */
431 >        return(dlen + (s - src));       /* count does not include NUL */
432   }
433  
434 < #endif
434 > int sg_update_string(char **dest, const char *src) {
435 >        char *new;
436  
437 < long long get_ll_match(char *line, regmatch_t *match){
438 <        char *ptr;
439 <        long long num;
437 >        if (src == NULL) {
438 >                /* We're being told to set it to NULL. */
439 >                free(*dest);
440 >                *dest = NULL;
441 >                return 0;
442 >        }
443  
444 <        ptr=line+match->rm_so;
445 <        num=atoll(ptr);
444 >        new = sg_realloc(*dest, strlen(src) + 1);
445 >        if (new == NULL) {
446 >                return -1;
447 >        }
448  
449 <        return num;
449 >        strcpy(new, src);
450 >        *dest = new;
451 >        return 0;
452   }
453  
454 < #ifdef ALLBSD
455 < kvm_t *get_kvm() {
454 > /* join two strings together */
455 > int sg_concat_string(char **dest, const char *src) {
456 >        char *new;
457 >        int len = strlen(*dest) + strlen(src) + 1;
458 >
459 >        new = sg_realloc(*dest, len);
460 >        if (new == NULL) {
461 >                return -1;
462 >        }
463 >
464 >        *dest = new;
465 >        strcat(*dest, src);
466 >        return 0;
467 > }
468 >
469 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
470 > kvm_t *sg_get_kvm() {
471          static kvm_t *kvmd = NULL;
472  
473          if (kvmd != NULL) {
# Line 390 | Line 475 | kvm_t *get_kvm() {
475          }
476  
477          kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
478 +        if(kvmd == NULL) {
479 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
480 +        }
481          return kvmd;
482   }
483 +
484 + /* Can't think of a better name for this function */
485 + kvm_t *sg_get_kvm2() {
486 +        static kvm_t *kvmd2 = NULL;
487 +
488 +        if (kvmd2 != NULL) {
489 +                return kvmd2;
490 +        }
491 +
492 +        kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
493 +        if(kvmd2 == NULL) {
494 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
495 +        }
496 +        return kvmd2;
497 + }
498   #endif
499  
500 < #ifdef NETBSD
501 < struct uvmexp *get_uvmexp() {
502 <        static u_long addr = 0;
500 > #if defined(NETBSD) || defined(OPENBSD)
501 > struct uvmexp *sg_get_uvmexp() {
502 >        int mib[2];
503 >        size_t size = sizeof(struct uvmexp);
504          static struct uvmexp uvm;
505 <        kvm_t *kvmd = get_kvm();
505 >        struct uvmexp *new;
506  
507 <        if (kvmd == NULL) {
507 >        mib[0] = CTL_VM;
508 >        mib[1] = VM_UVMEXP;
509 >
510 >        if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) {
511 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP");
512                  return NULL;
513          }
514  
515 <        if (addr == 0) {
516 <                struct nlist symbols[] = {
517 <                        { "_uvmexp" },
410 <                        { NULL }
411 <                };
515 >        return &uvm;
516 > }
517 > #endif
518  
519 <                if (kvm_nlist(kvmd, symbols) != 0) {
519 > #ifdef HPUX
520 > struct pst_static *sg_get_pstat_static() {
521 >        static int got = 0;
522 >        static struct pst_static pst;
523 >
524 >        if (!got) {
525 >                if (pstat_getstatic(&pst, sizeof pst, 1, 0) == -1) {
526 >                        sg_set_error_with_errno(SG_ERROR_PSTAT,
527 >                                                "pstat_static");
528                          return NULL;
529                  }
530 <                addr = symbols[0].n_value;
530 >                got = 1;
531          }
532 <
419 <        if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
420 <                return NULL;
421 <        }
422 <        return &uvm;
532 >        return &pst;
533   }
534   #endif
535  
536 < int statgrab_init(){
537 < #ifdef ALLBSD
538 <        {
539 <                kvm_t *kvmd = get_kvm();
540 <                if (kvmd == NULL) return 1;
536 > int sg_init(){
537 >        sg_set_error(SG_ERROR_NONE, NULL);
538 >
539 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
540 >        if (sg_get_kvm() == NULL) {
541 >                return -1;
542          }
543 < #endif
544 < #ifdef NETBSD
434 <        {
435 <                struct uvmexp *uvm = get_uvmexp();
436 <                if (uvm == NULL) return 1;
543 >        if (sg_get_kvm2() == NULL) {
544 >                return -1;
545          }
546   #endif
547   #ifdef SOLARIS
548 <        if((build_mapping()) != 0) return 1;
548 >        /* On solaris 7, this will fail if you are not root. But, everything
549 >         * will still work, just no disk mappings. So we will ignore the exit
550 >         * status of this, and carry on merrily.
551 >         */
552 > #ifdef HAVE_LIBDEVINFO_H
553 >        build_mapping();
554   #endif
555 + #endif
556 + #ifdef WIN32
557 +        return sg_win32_start_capture();
558 + #endif
559          return 0;
560   }
561  
562 < int statgrab_drop_privileges() {
563 <        if (setegid(getgid()) != 0) return -1;
564 <        if (seteuid(getuid()) != 0) return -1;
562 > int sg_shutdown() {
563 > #ifdef WIN32
564 >        sg_win32_end_capture();
565 > #endif
566          return 0;
567   }
568  
569 + int sg_snapshot() {
570 + #ifdef WIN32
571 +        return sg_win32_snapshot();
572 + #else
573 +        return 0;
574 + #endif
575 + }
576 +
577 + int sg_drop_privileges() {
578 + #ifndef WIN32
579 + #ifdef HAVE_SETEGID
580 +        if (setegid(getgid()) != 0) {
581 + #elif defined(HAVE_SETRESGID)
582 +        if (setresgid(getgid(), getgid(), getgid()) != 0) {
583 + #else
584 +        {
585 + #endif
586 +                sg_set_error_with_errno(SG_ERROR_SETEGID, NULL);
587 +                return -1;
588 +        }
589 + #ifdef HAVE_SETEUID
590 +        if (seteuid(getuid()) != 0) {
591 + #elif defined(HAVE_SETRESUID)
592 +        if (setresuid(getuid(), getuid(), getuid()) != 0) {
593 + #else
594 +        {
595 + #endif
596 +                sg_set_error_with_errno(SG_ERROR_SETEUID, NULL);
597 +                return -1;
598 +        }
599 + #endif /* WIN32 */
600 +        return 0;
601 + }
602 +
603 + void *sg_realloc(void *ptr, size_t size) {
604 +        void *tmp = NULL;
605 +        tmp = realloc(ptr, size);
606 +        if(tmp == NULL) {
607 +                sg_set_error_with_errno(SG_ERROR_MALLOC, NULL);
608 +        }
609 +        return tmp;
610 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines