1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org |
4 |
* Copyright (C) 2000-2003 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. |
10 |
* |
11 |
* This program 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. |
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. |
19 |
*/ |
20 |
|
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include "config.h" |
23 |
#endif |
24 |
|
25 |
#include <stdio.h> |
26 |
#include <string.h> |
27 |
#include <stdlib.h> |
28 |
#include <sys/types.h> |
29 |
#include <regex.h> |
30 |
#ifdef ALLBSD |
31 |
#include <fcntl.h> |
32 |
#include <kvm.h> |
33 |
#endif |
34 |
#ifdef NETBSD |
35 |
#include <uvm/uvm_extern.h> |
36 |
#endif |
37 |
|
38 |
#include "tools.h" |
39 |
|
40 |
char *f_read_line(FILE *f, const char *string){ |
41 |
/* Max line length. 8k should be more than enough */ |
42 |
static char line[8192]; |
43 |
|
44 |
while((fgets(line, sizeof(line), f))!=NULL){ |
45 |
if(strncmp(string, line, strlen(string))==0){ |
46 |
return line; |
47 |
} |
48 |
} |
49 |
|
50 |
return NULL; |
51 |
} |
52 |
|
53 |
char *get_string_match(char *line, regmatch_t *match){ |
54 |
int len=match->rm_eo - match->rm_so; |
55 |
char *match_string=malloc(len+1); |
56 |
|
57 |
match_string=strncpy(match_string, line+match->rm_so, len); |
58 |
match_string[len]='\0'; |
59 |
|
60 |
return match_string; |
61 |
} |
62 |
|
63 |
long long get_ll_match(char *line, regmatch_t *match){ |
64 |
char *ptr; |
65 |
long long num; |
66 |
|
67 |
ptr=line+match->rm_so; |
68 |
#ifdef HAVE_ATOLL |
69 |
num=atoll(ptr); |
70 |
#else |
71 |
/* Don't have atoll, so use this bodge instead */ |
72 |
{ |
73 |
long numl; |
74 |
numl=atol(ptr); |
75 |
num=numl; |
76 |
} |
77 |
#endif |
78 |
|
79 |
return num; |
80 |
} |
81 |
|
82 |
#ifdef ALLBSD |
83 |
kvm_t *get_kvm() { |
84 |
static kvm_t *kvmd = NULL; |
85 |
|
86 |
if (kvmd != NULL) { |
87 |
return kvmd; |
88 |
} |
89 |
|
90 |
kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); |
91 |
return kvmd; |
92 |
} |
93 |
#endif |
94 |
|
95 |
#ifdef NETBSD |
96 |
struct uvmexp *get_uvmexp() { |
97 |
static u_long addr = 0; |
98 |
static struct uvmexp uvm; |
99 |
kvm_t *kvmd = get_kvm(); |
100 |
|
101 |
if (kvmd == NULL) { |
102 |
return NULL; |
103 |
} |
104 |
|
105 |
if (addr == 0) { |
106 |
struct nlist symbols[] = { |
107 |
{ "_uvmexp" }, |
108 |
{ NULL } |
109 |
}; |
110 |
|
111 |
if (kvm_nlist(kvmd, symbols) != 0) { |
112 |
return NULL; |
113 |
} |
114 |
addr = symbols[0].n_value; |
115 |
} |
116 |
|
117 |
if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) { |
118 |
return NULL; |
119 |
} |
120 |
return &uvm; |
121 |
} |
122 |
#endif |
123 |
|
124 |
int statgrab_init(){ |
125 |
#ifdef ALLBSD |
126 |
{ |
127 |
kvm_t *kvmd = get_kvm(); |
128 |
if (kvmd == NULL) return 1; |
129 |
} |
130 |
#endif |
131 |
#ifdef NETBSD |
132 |
{ |
133 |
struct uvmexp *uvm = get_uvmexp(); |
134 |
if (uvm == NULL) return 1; |
135 |
} |
136 |
#endif |
137 |
return 0; |
138 |
} |