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.23 by tdb, Fri Jan 16 15:54:54 2004 UTC vs.
Revision 1.36 by ats, Sun Apr 4 23:48:26 2004 UTC

# Line 17 | Line 17
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 31 | 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 52 | 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 62 | Line 71 | struct map{
71   typedef struct map mapping_t;
72  
73   static mapping_t *mapping = NULL;
74 + #endif
75  
76 + #ifdef SOLARIS
77   char *get_svr_from_bsd(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;
# Line 407 | Line 421 | long long get_ll_match(char *line, regmatch_t *match){
421          return num;
422   }
423  
424 < #ifdef ALLBSD
424 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
425   kvm_t *get_kvm() {
426          static kvm_t *kvmd = NULL;
427  
# Line 418 | Line 432 | kvm_t *get_kvm() {
432          kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
433          return kvmd;
434   }
435 +
436 + /* Can't think of a better name for this function */
437 + kvm_t *get_kvm2() {
438 +        static kvm_t *kvmd2 = NULL;
439 +
440 +        if (kvmd2 != NULL) {
441 +                return kvmd2;
442 +        }
443 +
444 +        kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
445 +        return kvmd2;
446 + }
447   #endif
448  
449 < #ifdef NETBSD
449 > #if defined(NETBSD) || defined(OPENBSD)
450   struct uvmexp *get_uvmexp() {
451 <        static u_long addr = 0;
452 <        static struct uvmexp uvm;
453 <        kvm_t *kvmd = get_kvm();
451 >        int mib[2];
452 >        size_t size;
453 >        static struct uvmexp *uvm = NULL;
454 >        struct uvmexp *new;
455  
456 <        if (kvmd == NULL) {
456 >        mib[0] = CTL_VM;
457 >        mib[1] = VM_UVMEXP;
458 >
459 >        if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) {
460                  return NULL;
461          }
462  
463 <        if (addr == 0) {
464 <                struct nlist symbols[] = {
465 <                        { "_uvmexp" },
436 <                        { NULL }
437 <                };
438 <
439 <                if (kvm_nlist(kvmd, symbols) != 0) {
440 <                        return NULL;
441 <                }
442 <                addr = symbols[0].n_value;
463 >        new = realloc(uvm, size);
464 >        if (new == NULL) {
465 >                return NULL;
466          }
467 +        uvm = new;
468  
469 <        if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
469 >        if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) {
470                  return NULL;
471          }
472 <        return &uvm;
472 >
473 >        return uvm;
474   }
475   #endif
476  
477   int statgrab_init(){
478 < #ifdef ALLBSD
478 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
479          {
480                  kvm_t *kvmd = get_kvm();
481                  if (kvmd == NULL) return 1;
482          }
483 +        {
484 +                kvm_t *kvmd2 = get_kvm2();
485 +                if (kvmd2 == NULL) return 1;
486 +        }
487   #endif
488 < #ifdef NETBSD
488 > #if defined(NETBSD) || defined(OPENBSD)
489          {
490 +                /* This should always succeed, but it seems that on some
491 +                 * versions of NetBSD the first call to get_uvmexp will return
492 +                 * a non-filled-in structure; this is a workaround for that.
493 +                 */
494                  struct uvmexp *uvm = get_uvmexp();
495                  if (uvm == NULL) return 1;
496          }
# Line 467 | Line 500 | int statgrab_init(){
500           * will still work, just no disk mappings. So we will ignore the exit
501           * status of this, and carry on merrily.
502           */
503 + #ifdef HAVE_LIBDEVINFO_H
504          build_mapping();
505 + #endif
506   #endif
507          return 0;
508   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines