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.37 by ats, Sun Apr 4 23:55:48 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 + #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 396 | 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 406 | 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 417 | 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" },
435 <                        { NULL }
436 <                };
437 <
438 <                if (kvm_nlist(kvmd, symbols) != 0) {
439 <                        return NULL;
440 <                }
441 <                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          }
# Line 466 | Line 513 | int statgrab_init(){
513           * will still work, just no disk mappings. So we will ignore the exit
514           * status of this, and carry on merrily.
515           */
516 + #ifdef HAVE_LIBDEVINFO_H
517          build_mapping();
518 + #endif
519   #endif
520          return 0;
521   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines