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.26 by pajs, Tue Feb 10 16:16:22 2004 UTC vs.
Revision 1.37 by ats, Sun Apr 4 23:55:48 2004 UTC

# Line 33 | 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 404 | Line 411 | size_t strlcat(char *dst, const char *src, size_t siz)
411  
412   #endif
413  
414 + char *update_string(char **dest, const char *src) {
415 +        char *new;
416 +
417 +        new = realloc(*dest, strlen(src) + 1);
418 +        if (new == NULL) {
419 +                return NULL;
420 +        }
421 +
422 +        strcpy(new, src);
423 +        *dest = new;
424 +        return new;
425 + }
426 +
427   long long get_ll_match(char *line, regmatch_t *match){
428          char *ptr;
429          long long num;
# Line 414 | Line 434 | long long get_ll_match(char *line, regmatch_t *match){
434          return num;
435   }
436  
437 < #ifdef ALLBSD
437 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
438   kvm_t *get_kvm() {
439          static kvm_t *kvmd = NULL;
440  
# Line 425 | Line 445 | kvm_t *get_kvm() {
445          kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
446          return kvmd;
447   }
448 +
449 + /* Can't think of a better name for this function */
450 + kvm_t *get_kvm2() {
451 +        static kvm_t *kvmd2 = NULL;
452 +
453 +        if (kvmd2 != NULL) {
454 +                return kvmd2;
455 +        }
456 +
457 +        kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
458 +        return kvmd2;
459 + }
460   #endif
461  
462 < #ifdef NETBSD
462 > #if defined(NETBSD) || defined(OPENBSD)
463   struct uvmexp *get_uvmexp() {
464 <        static u_long addr = 0;
465 <        static struct uvmexp uvm;
466 <        kvm_t *kvmd = get_kvm();
464 >        int mib[2];
465 >        size_t size;
466 >        static struct uvmexp *uvm = NULL;
467 >        struct uvmexp *new;
468  
469 <        if (kvmd == NULL) {
469 >        mib[0] = CTL_VM;
470 >        mib[1] = VM_UVMEXP;
471 >
472 >        if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) {
473                  return NULL;
474          }
475  
476 <        if (addr == 0) {
477 <                struct nlist symbols[] = {
478 <                        { "_uvmexp" },
443 <                        { NULL }
444 <                };
445 <
446 <                if (kvm_nlist(kvmd, symbols) != 0) {
447 <                        return NULL;
448 <                }
449 <                addr = symbols[0].n_value;
476 >        new = realloc(uvm, size);
477 >        if (new == NULL) {
478 >                return NULL;
479          }
480 +        uvm = new;
481  
482 <        if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
482 >        if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) {
483                  return NULL;
484          }
485 <        return &uvm;
485 >
486 >        return uvm;
487   }
488   #endif
489  
490   int statgrab_init(){
491 < #ifdef ALLBSD
491 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
492          {
493                  kvm_t *kvmd = get_kvm();
494                  if (kvmd == NULL) return 1;
495          }
496 +        {
497 +                kvm_t *kvmd2 = get_kvm2();
498 +                if (kvmd2 == NULL) return 1;
499 +        }
500   #endif
501 < #ifdef NETBSD
501 > #if defined(NETBSD) || defined(OPENBSD)
502          {
503 +                /* This should always succeed, but it seems that on some
504 +                 * versions of NetBSD the first call to get_uvmexp will return
505 +                 * a non-filled-in structure; this is a workaround for that.
506 +                 */
507                  struct uvmexp *uvm = get_uvmexp();
508                  if (uvm == NULL) return 1;
509          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines