ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
Revision: 1.17
Committed: Mon Jan 5 14:21:07 2004 UTC (20 years, 4 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.16: +12 -11 lines
Log Message:
Minor tiding

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 #ifdef SOLARIS
41 #include <libdevinfo.h>
42 #include <kstat.h>
43 #include <unistd.h>
44 #include <ctype.h>
45 #include <sys/types.h>
46 #include <sys/dkio.h>
47 #include <sys/stat.h>
48 #include <fcntl.h>
49 #include <sys/fcntl.h>
50 #include <dirent.h>
51 #endif
52
53 #ifdef SOLARIS
54 struct map{
55 char *bsd;
56 char *svr;
57
58 struct map *next;
59 };
60 typedef struct map mapping_t;
61
62 static mapping_t *mapping = NULL;
63
64 char *get_svr_from_bsd(char *bsd){
65 mapping_t *map_ptr;
66 for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next)
67 if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr;
68
69 return bsd;
70 }
71
72 void add_mapping(char *bsd, char *svr){
73 mapping_t *map_ptr;
74 mapping_t *map_end_ptr;
75
76 bsd = strdup(bsd);
77 svr = strdup(svr);
78
79 if (mapping == NULL){
80 mapping = malloc(sizeof(mapping_t));
81 if (mapping == NULL) return;
82 map_ptr = mapping;
83 }else{
84 /* See if its already been added */
85 for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next){
86 if( (!strcmp(map_ptr->bsd, bsd)) || (!strcmp(map_ptr->svr, svr)) ){
87 return;
88 }
89 map_end_ptr = map_ptr;
90 }
91
92 /* We've reached end of list and not found the entry.. So we need to malloc
93 * new mapping_t
94 */
95 map_end_ptr->next = malloc(sizeof(mapping_t));
96 if (map_end_ptr->next == NULL) return;
97 map_ptr = map_end_ptr->next;
98 }
99
100 map_ptr->next = NULL;
101 map_ptr->bsd = bsd;
102 map_ptr->svr = svr;
103
104 return;
105 }
106
107 char *read_dir(char *disk_path){
108 DIR *dirp;
109 struct dirent *dp;
110 struct stat stbuf;
111 char *svr_name;
112 char current_dir[MAXPATHLEN];
113 char file_name[MAXPATHLEN];
114 char temp_name[MAXPATHLEN];
115 char dir_dname[MAXPATHLEN];
116 char *dsk_dir;
117 int x;
118
119 dsk_dir = "/dev/osa/dev/dsk";
120 strncpy(current_dir, dsk_dir, sizeof current_dir);
121 if ((dirp = opendir(current_dir)) == NULL){
122 dsk_dir = "/dev/dsk";
123 snprintf(current_dir, sizeof current_dir, "%s", dsk_dir);
124 if ((dirp = opendir(current_dir)) == NULL){
125 return NULL;
126 }
127 }
128
129 while ((dp = readdir(dirp)) != NULL){
130 snprintf(temp_name, sizeof temp_name, "../..%s", disk_path);
131 snprintf(dir_dname, sizeof dir_dname, "%s/%s", dsk_dir, dp->d_name);
132 stat(dir_dname,&stbuf);
133
134 if (S_ISBLK(stbuf.st_mode)){
135 x = readlink(dir_dname, file_name, sizeof(file_name));
136 file_name[x] = '\0';
137 if (strcmp(file_name, temp_name) == 0) {
138 svr_name = strdup(dp->d_name);
139 closedir(dirp);
140 return svr_name;
141 }
142 }
143 }
144 closedir(dirp);
145 return NULL;
146 }
147
148
149
150 int get_alias(char *alias){
151 char file[MAXPATHLEN];
152 di_node_t root_node;
153 di_node_t node;
154 di_minor_t minor = DI_MINOR_NIL;
155 char tmpnode[MAXPATHLEN];
156 char *phys_path;
157 char *minor_name;
158 char *value;
159 int instance;
160 if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
161 return 1;
162 }
163 node = di_drv_first_node(alias, root_node);
164 while (node != DI_NODE_NIL) {
165 if ((minor = di_minor_next(node, DI_MINOR_NIL)) != DI_MINOR_NIL) {
166 instance = di_instance(node);
167 phys_path = di_devfs_path(node);
168 minor_name = di_minor_name(minor);
169 strcpy(tmpnode, alias);
170 sprintf(tmpnode, "%s%d", tmpnode, instance);
171 strlcpy(file, "/devices", sizeof file);
172 strlcat(file, phys_path, sizeof file);
173 strlcat(file, ":", sizeof file);
174 strlcat(file, minor_name, sizeof file);
175 value = read_dir(file);
176 if (value != NULL){
177 add_mapping(tmpnode, value);
178 }
179 di_devfs_path_free(phys_path);
180 node = di_drv_next_node(node);
181 }else{
182 node = di_drv_next_node(node);
183 }
184 }
185 di_fini(root_node);
186 return 0;
187 }
188
189 int build_mapping(){
190 char device_name[512];
191 int x;
192 kstat_ctl_t *kc;
193 kstat_t *ksp;
194 kstat_io_t kios;
195
196 if ((kc = kstat_open()) == NULL) {
197 return;
198 }
199
200 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
201 if (!strcmp(ksp->ks_class, "disk")) {
202 if(ksp->ks_type != KSTAT_TYPE_IO) continue;
203 /* We dont want metadevices appearing as num_diskio */
204 if(strcmp(ksp->ks_module, "md")==0) continue;
205 if((kstat_read(kc, ksp, &kios))==-1) continue;
206 strncpy(device_name, ksp->ks_name, sizeof device_name);
207 for(x=0;x<(sizeof device_name);x++){
208 if( isdigit((int)device_name[x]) ) break;
209 }
210 if(x == sizeof device_name) x--;
211 device_name[x] = '\0';
212 if((get_alias(device_name)) != 0){
213 return 1;
214 }
215 }
216 }
217
218 return 0;
219 }
220
221 #endif
222
223
224
225 char *f_read_line(FILE *f, const char *string){
226 /* Max line length. 8k should be more than enough */
227 static char line[8192];
228
229 while((fgets(line, sizeof(line), f))!=NULL){
230 if(strncmp(string, line, strlen(string))==0){
231 return line;
232 }
233 }
234
235 return NULL;
236 }
237
238 char *get_string_match(char *line, regmatch_t *match){
239 int len=match->rm_eo - match->rm_so;
240 char *match_string=malloc(len+1);
241
242 match_string=strncpy(match_string, line+match->rm_so, len);
243 match_string[len]='\0';
244
245 return match_string;
246 }
247
248
249
250 #ifndef HAVE_ATOLL
251 static long long atoll(const char *s) {
252 long long value = 0;
253 int isneg = 0;
254
255 while (*s == ' ' || *s == '\t') {
256 s++;
257 }
258 if (*s == '-') {
259 isneg = 1;
260 s++;
261 }
262 while (*s >= '0' && *s <= '9') {
263 value = (10 * value) + (*s - '0');
264 s++;
265 }
266 return (isneg ? -value : value);
267 }
268 #endif
269
270 long long get_ll_match(char *line, regmatch_t *match){
271 char *ptr;
272 long long num;
273
274 ptr=line+match->rm_so;
275 num=atoll(ptr);
276
277 return num;
278 }
279
280 #ifdef ALLBSD
281 kvm_t *get_kvm() {
282 static kvm_t *kvmd = NULL;
283
284 if (kvmd != NULL) {
285 return kvmd;
286 }
287
288 kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
289 return kvmd;
290 }
291 #endif
292
293 #ifdef NETBSD
294 struct uvmexp *get_uvmexp() {
295 static u_long addr = 0;
296 static struct uvmexp uvm;
297 kvm_t *kvmd = get_kvm();
298
299 if (kvmd == NULL) {
300 return NULL;
301 }
302
303 if (addr == 0) {
304 struct nlist symbols[] = {
305 { "_uvmexp" },
306 { NULL }
307 };
308
309 if (kvm_nlist(kvmd, symbols) != 0) {
310 return NULL;
311 }
312 addr = symbols[0].n_value;
313 }
314
315 if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
316 return NULL;
317 }
318 return &uvm;
319 }
320 #endif
321
322 int statgrab_init(){
323 #ifdef ALLBSD
324 {
325 kvm_t *kvmd = get_kvm();
326 if (kvmd == NULL) return 1;
327 }
328 #endif
329 #ifdef NETBSD
330 {
331 struct uvmexp *uvm = get_uvmexp();
332 if (uvm == NULL) return 1;
333 }
334 #endif
335 #ifdef SOLARIS
336 if((build_mapping()) != 0) return 1;
337 #endif
338 return 0;
339 }