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.20 by pajs, Fri Jan 9 16:37:08 2004 UTC vs.
Revision 1.40 by ats, Mon Apr 5 01:06:02 2004 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * i-scream central monitoring system
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  
49   #ifdef SOLARIS
50 + #ifdef HAVE_LIBDEVINFO_H
51   #include <libdevinfo.h>
52 + #endif
53   #include <kstat.h>
54   #include <unistd.h>
55   #include <ctype.h>
# Line 51 | Line 61
61   #include <dirent.h>
62   #endif
63  
64 < #ifdef SOLARIS
64 > #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
65   struct map{
66          char *bsd;
67          char *svr;
# Line 61 | Line 71 | struct map{
71   typedef struct map mapping_t;
72  
73   static mapping_t *mapping = NULL;
74 + #endif
75  
76 < char *get_svr_from_bsd(char *bsd){
76 > #ifdef SOLARIS
77 > const char *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)
81                  if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr;
82 <
82 > #endif
83          return bsd;
84   }
85 + #endif
86  
87 + #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
88   void add_mapping(char *bsd, char *svr){
89          mapping_t *map_ptr;
90          mapping_t *map_end_ptr;
91  
77        bsd = strdup(bsd);
78        svr = strdup(svr);
79
92          if (mapping == NULL){
93                  mapping = malloc(sizeof(mapping_t));
94                  if (mapping == NULL) return;
# Line 99 | Line 111 | void add_mapping(char *bsd, char *svr){
111          }
112  
113          map_ptr->next = NULL;
114 <        map_ptr->bsd = bsd;
115 <        map_ptr->svr = svr;
114 >        map_ptr->bsd = strdup(bsd);
115 >        map_ptr->svr = strdup(svr);
116  
117          return;
118   }
# Line 187 | Line 199 | int get_alias(char *alias){
199          return 0;
200   }
201  
202 +
203 + #define BIG_ENOUGH 512
204   int build_mapping(){
205 <        char device_name[512];
205 >        char device_name[BIG_ENOUGH];
206          int x;
207          kstat_ctl_t *kc;
208          kstat_t *ksp;
209          kstat_io_t kios;
210  
211 +        char driver_list[BIG_ENOUGH][BIG_ENOUGH];
212 +        int list_entries = 0;
213 +        int found;
214 +
215          if ((kc = kstat_open()) == NULL) {
216 <                return;
216 >                return 1;
217          }
218  
219          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
# Line 210 | Line 228 | int build_mapping(){
228                          }
229                          if(x == sizeof device_name) x--;
230                          device_name[x] = '\0';
231 <                        if((get_alias(device_name)) != 0){
232 <                                return 1;
231 >
232 >                        /* Check if we've not already looked it up */
233 >                        found = 0;
234 >                        for(x=0;x<list_entries;x++){
235 >                                if (x>=BIG_ENOUGH){
236 >                                        /* We've got bigger than we thought was massive */
237 >                                        /* If we hit this.. Make big enough bigger */
238 >                                        return 1;
239 >                                }
240 >                                if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){
241 >                                        found = 1;
242 >                                        break;
243 >                                }
244                          }
245 +
246 +                        if(!found){
247 +                                if((get_alias(device_name)) != 0){
248 +                                        return 1;
249 +                                }
250 +                                strncpy(driver_list[x], device_name, BIG_ENOUGH);
251 +                                list_entries++;
252 +                        }
253                  }
254          }
255  
# Line 269 | Line 306 | static long long atoll(const char *s) {
306   #endif
307  
308   #ifndef HAVE_STRLCPY
309 < *      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
309 > /*      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
310  
311   /*
312   * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
# Line 371 | Line 408 | size_t strlcat(char *dst, const char *src, size_t siz)
408  
409   #endif
410  
411 + char *update_string(char **dest, const char *src) {
412 +        char *new;
413 +
414 +        new = realloc(*dest, strlen(src) + 1);
415 +        if (new == NULL) {
416 +                return NULL;
417 +        }
418 +
419 +        strcpy(new, src);
420 +        *dest = new;
421 +        return new;
422 + }
423 +
424   long long get_ll_match(char *line, regmatch_t *match){
425          char *ptr;
426          long long num;
# Line 381 | Line 431 | long long get_ll_match(char *line, regmatch_t *match){
431          return num;
432   }
433  
434 < #ifdef ALLBSD
434 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
435   kvm_t *get_kvm() {
436          static kvm_t *kvmd = NULL;
437  
# Line 392 | Line 442 | kvm_t *get_kvm() {
442          kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
443          return kvmd;
444   }
445 +
446 + /* Can't think of a better name for this function */
447 + kvm_t *get_kvm2() {
448 +        static kvm_t *kvmd2 = NULL;
449 +
450 +        if (kvmd2 != NULL) {
451 +                return kvmd2;
452 +        }
453 +
454 +        kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
455 +        return kvmd2;
456 + }
457   #endif
458  
459 < #ifdef NETBSD
459 > #if defined(NETBSD) || defined(OPENBSD)
460   struct uvmexp *get_uvmexp() {
461 <        static u_long addr = 0;
462 <        static struct uvmexp uvm;
463 <        kvm_t *kvmd = get_kvm();
461 >        int mib[2];
462 >        size_t size;
463 >        static struct uvmexp *uvm = NULL;
464 >        struct uvmexp *new;
465  
466 <        if (kvmd == NULL) {
466 >        mib[0] = CTL_VM;
467 >        mib[1] = VM_UVMEXP;
468 >
469 >        if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) {
470                  return NULL;
471          }
472  
473 <        if (addr == 0) {
474 <                struct nlist symbols[] = {
475 <                        { "_uvmexp" },
410 <                        { NULL }
411 <                };
412 <
413 <                if (kvm_nlist(kvmd, symbols) != 0) {
414 <                        return NULL;
415 <                }
416 <                addr = symbols[0].n_value;
473 >        new = realloc(uvm, size);
474 >        if (new == NULL) {
475 >                return NULL;
476          }
477 +        uvm = new;
478  
479 <        if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
479 >        if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) {
480                  return NULL;
481          }
482 <        return &uvm;
482 >
483 >        return uvm;
484   }
485   #endif
486  
487   int statgrab_init(){
488 < #ifdef ALLBSD
488 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
489          {
490                  kvm_t *kvmd = get_kvm();
491                  if (kvmd == NULL) return 1;
492          }
493 +        {
494 +                kvm_t *kvmd2 = get_kvm2();
495 +                if (kvmd2 == NULL) return 1;
496 +        }
497   #endif
498 < #ifdef NETBSD
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          }
# Line 441 | Line 510 | int statgrab_init(){
510           * will still work, just no disk mappings. So we will ignore the exit
511           * status of this, and carry on merrily.
512           */
513 + #ifdef HAVE_LIBDEVINFO_H
514          build_mapping();
515 + #endif
516   #endif
517          return 0;
518   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines