ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
Revision: 1.9
Committed: Sun Oct 19 02:03:02 2003 UTC (20 years, 6 months ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.8: +35 -2 lines
Log Message:
Initial support for NetBSD. This adds NetBSD support for everything
except diskio stats (since they're even more disturbingly complex to get
at on NetBSD than the three OSs we already support). Tested against
NetBSD 1.6 on i386.

File Contents

# Content
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 #ifdef HAVE_ATOLL
64 long long get_ll_match(char *line, regmatch_t *match){
65 char *ptr;
66 long long num;
67
68 ptr=line+match->rm_so;
69 num=atoll(ptr);
70
71 return num;
72 }
73 #endif
74
75 #ifdef ALLBSD
76 kvm_t *get_kvm() {
77 static kvm_t *kvmd = NULL;
78
79 if (kvmd != NULL) {
80 return kvmd;
81 }
82
83 kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
84 return kvmd;
85 }
86 #endif
87
88 #ifdef NETBSD
89 struct uvmexp *get_uvmexp() {
90 static u_long addr = 0;
91 static struct uvmexp uvm;
92 kvm_t *kvmd = get_kvm();
93
94 if (kvmd == NULL) {
95 return NULL;
96 }
97
98 if (addr == 0) {
99 struct nlist symbols[] = {
100 { "_uvmexp" },
101 { NULL }
102 };
103
104 if (kvm_nlist(kvmd, symbols) != 0) {
105 return NULL;
106 }
107 addr = symbols[0].n_value;
108 }
109
110 if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
111 return NULL;
112 }
113 return &uvm;
114 }
115 #endif
116