1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org |
4 |
* Copyright (C) 2000-2004 i-scream |
5 |
* |
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 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 GNU |
14 |
* Lesser General Public License for more details. |
15 |
* |
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 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include "config.h" |
24 |
#endif |
25 |
|
26 |
#include <stdio.h> |
27 |
#include <string.h> |
28 |
#include <stdlib.h> |
29 |
#include <unistd.h> |
30 |
#include <sys/types.h> |
31 |
#include <regex.h> |
32 |
#ifdef ALLBSD |
33 |
#include <fcntl.h> |
34 |
#include <kvm.h> |
35 |
#endif |
36 |
#ifdef NETBSD |
37 |
#include <uvm/uvm_extern.h> |
38 |
#endif |
39 |
|
40 |
#include "tools.h" |
41 |
|
42 |
#ifdef SOLARIS |
43 |
#include <libdevinfo.h> |
44 |
#include <kstat.h> |
45 |
#include <unistd.h> |
46 |
#include <ctype.h> |
47 |
#include <sys/types.h> |
48 |
#include <sys/dkio.h> |
49 |
#include <sys/stat.h> |
50 |
#include <fcntl.h> |
51 |
#include <sys/fcntl.h> |
52 |
#include <dirent.h> |
53 |
#endif |
54 |
|
55 |
#ifdef SOLARIS |
56 |
struct map{ |
57 |
char *bsd; |
58 |
char *svr; |
59 |
|
60 |
struct map *next; |
61 |
}; |
62 |
typedef struct map mapping_t; |
63 |
|
64 |
static mapping_t *mapping = NULL; |
65 |
|
66 |
char *get_svr_from_bsd(char *bsd){ |
67 |
mapping_t *map_ptr; |
68 |
for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next) |
69 |
if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr; |
70 |
|
71 |
return bsd; |
72 |
} |
73 |
|
74 |
void add_mapping(char *bsd, char *svr){ |
75 |
mapping_t *map_ptr; |
76 |
mapping_t *map_end_ptr; |
77 |
|
78 |
bsd = strdup(bsd); |
79 |
svr = strdup(svr); |
80 |
|
81 |
if (mapping == NULL){ |
82 |
mapping = malloc(sizeof(mapping_t)); |
83 |
if (mapping == NULL) return; |
84 |
map_ptr = mapping; |
85 |
}else{ |
86 |
/* See if its already been added */ |
87 |
for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next){ |
88 |
if( (!strcmp(map_ptr->bsd, bsd)) || (!strcmp(map_ptr->svr, svr)) ){ |
89 |
return; |
90 |
} |
91 |
map_end_ptr = map_ptr; |
92 |
} |
93 |
|
94 |
/* We've reached end of list and not found the entry.. So we need to malloc |
95 |
* new mapping_t |
96 |
*/ |
97 |
map_end_ptr->next = malloc(sizeof(mapping_t)); |
98 |
if (map_end_ptr->next == NULL) return; |
99 |
map_ptr = map_end_ptr->next; |
100 |
} |
101 |
|
102 |
map_ptr->next = NULL; |
103 |
map_ptr->bsd = bsd; |
104 |
map_ptr->svr = svr; |
105 |
|
106 |
return; |
107 |
} |
108 |
|
109 |
char *read_dir(char *disk_path){ |
110 |
DIR *dirp; |
111 |
struct dirent *dp; |
112 |
struct stat stbuf; |
113 |
char *svr_name; |
114 |
char current_dir[MAXPATHLEN]; |
115 |
char file_name[MAXPATHLEN]; |
116 |
char temp_name[MAXPATHLEN]; |
117 |
char dir_dname[MAXPATHLEN]; |
118 |
char *dsk_dir; |
119 |
int x; |
120 |
|
121 |
dsk_dir = "/dev/osa/dev/dsk"; |
122 |
strncpy(current_dir, dsk_dir, sizeof current_dir); |
123 |
if ((dirp = opendir(current_dir)) == NULL){ |
124 |
dsk_dir = "/dev/dsk"; |
125 |
snprintf(current_dir, sizeof current_dir, "%s", dsk_dir); |
126 |
if ((dirp = opendir(current_dir)) == NULL){ |
127 |
return NULL; |
128 |
} |
129 |
} |
130 |
|
131 |
while ((dp = readdir(dirp)) != NULL){ |
132 |
snprintf(temp_name, sizeof temp_name, "../..%s", disk_path); |
133 |
snprintf(dir_dname, sizeof dir_dname, "%s/%s", dsk_dir, dp->d_name); |
134 |
stat(dir_dname,&stbuf); |
135 |
|
136 |
if (S_ISBLK(stbuf.st_mode)){ |
137 |
x = readlink(dir_dname, file_name, sizeof(file_name)); |
138 |
file_name[x] = '\0'; |
139 |
if (strcmp(file_name, temp_name) == 0) { |
140 |
svr_name = strdup(dp->d_name); |
141 |
closedir(dirp); |
142 |
return svr_name; |
143 |
} |
144 |
} |
145 |
} |
146 |
closedir(dirp); |
147 |
return NULL; |
148 |
} |
149 |
|
150 |
|
151 |
|
152 |
int get_alias(char *alias){ |
153 |
char file[MAXPATHLEN]; |
154 |
di_node_t root_node; |
155 |
di_node_t node; |
156 |
di_minor_t minor = DI_MINOR_NIL; |
157 |
char tmpnode[MAXPATHLEN]; |
158 |
char *phys_path; |
159 |
char *minor_name; |
160 |
char *value; |
161 |
int instance; |
162 |
if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) { |
163 |
return 1; |
164 |
} |
165 |
node = di_drv_first_node(alias, root_node); |
166 |
while (node != DI_NODE_NIL) { |
167 |
if ((minor = di_minor_next(node, DI_MINOR_NIL)) != DI_MINOR_NIL) { |
168 |
instance = di_instance(node); |
169 |
phys_path = di_devfs_path(node); |
170 |
minor_name = di_minor_name(minor); |
171 |
strcpy(tmpnode, alias); |
172 |
sprintf(tmpnode, "%s%d", tmpnode, instance); |
173 |
strlcpy(file, "/devices", sizeof file); |
174 |
strlcat(file, phys_path, sizeof file); |
175 |
strlcat(file, ":", sizeof file); |
176 |
strlcat(file, minor_name, sizeof file); |
177 |
value = read_dir(file); |
178 |
if (value != NULL){ |
179 |
add_mapping(tmpnode, value); |
180 |
} |
181 |
di_devfs_path_free(phys_path); |
182 |
node = di_drv_next_node(node); |
183 |
}else{ |
184 |
node = di_drv_next_node(node); |
185 |
} |
186 |
} |
187 |
di_fini(root_node); |
188 |
return 0; |
189 |
} |
190 |
|
191 |
|
192 |
#define BIG_ENOUGH 512 |
193 |
int build_mapping(){ |
194 |
char device_name[BIG_ENOUGH]; |
195 |
int x; |
196 |
kstat_ctl_t *kc; |
197 |
kstat_t *ksp; |
198 |
kstat_io_t kios; |
199 |
|
200 |
char driver_list[BIG_ENOUGH][BIG_ENOUGH]; |
201 |
int list_entries = 0; |
202 |
int found; |
203 |
|
204 |
if ((kc = kstat_open()) == NULL) { |
205 |
return; |
206 |
} |
207 |
|
208 |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
209 |
if (!strcmp(ksp->ks_class, "disk")) { |
210 |
if(ksp->ks_type != KSTAT_TYPE_IO) continue; |
211 |
/* We dont want metadevices appearing as num_diskio */ |
212 |
if(strcmp(ksp->ks_module, "md")==0) continue; |
213 |
if((kstat_read(kc, ksp, &kios))==-1) continue; |
214 |
strncpy(device_name, ksp->ks_name, sizeof device_name); |
215 |
for(x=0;x<(sizeof device_name);x++){ |
216 |
if( isdigit((int)device_name[x]) ) break; |
217 |
} |
218 |
if(x == sizeof device_name) x--; |
219 |
device_name[x] = '\0'; |
220 |
|
221 |
/* Check if we've not already looked it up */ |
222 |
found = 0; |
223 |
for(x=0;x<list_entries;x++){ |
224 |
if (x>=BIG_ENOUGH){ |
225 |
/* We've got bigger than we thought was massive */ |
226 |
/* If we hit this.. Make big enough bigger */ |
227 |
return 1; |
228 |
} |
229 |
if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){ |
230 |
found = 1; |
231 |
break; |
232 |
} |
233 |
} |
234 |
|
235 |
if(!found){ |
236 |
if((get_alias(device_name)) != 0){ |
237 |
return 1; |
238 |
} |
239 |
strncpy(driver_list[x], device_name, BIG_ENOUGH); |
240 |
list_entries++; |
241 |
} |
242 |
} |
243 |
} |
244 |
|
245 |
return 0; |
246 |
} |
247 |
|
248 |
#endif |
249 |
|
250 |
|
251 |
|
252 |
char *f_read_line(FILE *f, const char *string){ |
253 |
/* Max line length. 8k should be more than enough */ |
254 |
static char line[8192]; |
255 |
|
256 |
while((fgets(line, sizeof(line), f))!=NULL){ |
257 |
if(strncmp(string, line, strlen(string))==0){ |
258 |
return line; |
259 |
} |
260 |
} |
261 |
|
262 |
return NULL; |
263 |
} |
264 |
|
265 |
char *get_string_match(char *line, regmatch_t *match){ |
266 |
int len=match->rm_eo - match->rm_so; |
267 |
char *match_string=malloc(len+1); |
268 |
|
269 |
match_string=strncpy(match_string, line+match->rm_so, len); |
270 |
match_string[len]='\0'; |
271 |
|
272 |
return match_string; |
273 |
} |
274 |
|
275 |
|
276 |
|
277 |
#ifndef HAVE_ATOLL |
278 |
static long long atoll(const char *s) { |
279 |
long long value = 0; |
280 |
int isneg = 0; |
281 |
|
282 |
while (*s == ' ' || *s == '\t') { |
283 |
s++; |
284 |
} |
285 |
if (*s == '-') { |
286 |
isneg = 1; |
287 |
s++; |
288 |
} |
289 |
while (*s >= '0' && *s <= '9') { |
290 |
value = (10 * value) + (*s - '0'); |
291 |
s++; |
292 |
} |
293 |
return (isneg ? -value : value); |
294 |
} |
295 |
#endif |
296 |
|
297 |
#ifndef HAVE_STRLCPY |
298 |
/* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */ |
299 |
|
300 |
/* |
301 |
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
302 |
* |
303 |
* Permission to use, copy, modify, and distribute this software for any |
304 |
* purpose with or without fee is hereby granted, provided that the above |
305 |
* copyright notice and this permission notice appear in all copies. |
306 |
* |
307 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
308 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
309 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
310 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
311 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
312 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
313 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
314 |
*/ |
315 |
|
316 |
/* |
317 |
* Copy src to string dst of size siz. At most siz-1 characters |
318 |
* will be copied. Always NUL terminates (unless siz == 0). |
319 |
* Returns strlen(src); if retval >= siz, truncation occurred. |
320 |
*/ |
321 |
size_t strlcpy(char *dst, const char *src, size_t siz){ |
322 |
register char *d = dst; |
323 |
register const char *s = src; |
324 |
register size_t n = siz; |
325 |
|
326 |
/* Copy as many bytes as will fit */ |
327 |
if (n != 0 && --n != 0) { |
328 |
do { |
329 |
if ((*d++ = *s++) == 0) |
330 |
break; |
331 |
} while (--n != 0); |
332 |
} |
333 |
|
334 |
/* Not enough room in dst, add NUL and traverse rest of src */ |
335 |
if (n == 0) { |
336 |
if (siz != 0) |
337 |
*d = '\0'; /* NUL-terminate dst */ |
338 |
while (*s++) |
339 |
; |
340 |
} |
341 |
|
342 |
return(s - src - 1); /* count does not include NUL */ |
343 |
} |
344 |
#endif |
345 |
|
346 |
#ifndef HAVE_STRLCAT |
347 |
/* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */ |
348 |
|
349 |
/* |
350 |
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
351 |
* |
352 |
* Permission to use, copy, modify, and distribute this software for any |
353 |
* purpose with or without fee is hereby granted, provided that the above |
354 |
* copyright notice and this permission notice appear in all copies. |
355 |
* |
356 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
357 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
358 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
359 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
360 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
361 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
362 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
363 |
*/ |
364 |
|
365 |
/* |
366 |
* Appends src to string dst of size siz (unlike strncat, siz is the |
367 |
* full size of dst, not space left). At most siz-1 characters |
368 |
* will be copied. Always NUL terminates (unless siz <= strlen(dst)). |
369 |
* Returns strlen(src) + MIN(siz, strlen(initial dst)). |
370 |
* If retval >= siz, truncation occurred. |
371 |
*/ |
372 |
size_t strlcat(char *dst, const char *src, size_t siz){ |
373 |
register char *d = dst; |
374 |
register const char *s = src; |
375 |
register size_t n = siz; |
376 |
size_t dlen; |
377 |
|
378 |
/* Find the end of dst and adjust bytes left but don't go past end */ |
379 |
while (n-- != 0 && *d != '\0') |
380 |
d++; |
381 |
dlen = d - dst; |
382 |
n = siz - dlen; |
383 |
|
384 |
if (n == 0) |
385 |
return(dlen + strlen(s)); |
386 |
while (*s != '\0') { |
387 |
if (n != 1) { |
388 |
*d++ = *s; |
389 |
n--; |
390 |
} |
391 |
s++; |
392 |
} |
393 |
*d = '\0'; |
394 |
|
395 |
return(dlen + (s - src)); /* count does not include NUL */ |
396 |
} |
397 |
|
398 |
#endif |
399 |
|
400 |
long long get_ll_match(char *line, regmatch_t *match){ |
401 |
char *ptr; |
402 |
long long num; |
403 |
|
404 |
ptr=line+match->rm_so; |
405 |
num=atoll(ptr); |
406 |
|
407 |
return num; |
408 |
} |
409 |
|
410 |
#ifdef ALLBSD |
411 |
kvm_t *get_kvm() { |
412 |
static kvm_t *kvmd = NULL; |
413 |
|
414 |
if (kvmd != NULL) { |
415 |
return kvmd; |
416 |
} |
417 |
|
418 |
kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); |
419 |
return kvmd; |
420 |
} |
421 |
#endif |
422 |
|
423 |
#ifdef NETBSD |
424 |
struct uvmexp *get_uvmexp() { |
425 |
static u_long addr = 0; |
426 |
static struct uvmexp uvm; |
427 |
kvm_t *kvmd = get_kvm(); |
428 |
|
429 |
if (kvmd == NULL) { |
430 |
return NULL; |
431 |
} |
432 |
|
433 |
if (addr == 0) { |
434 |
struct nlist symbols[] = { |
435 |
{ "_uvmexp" }, |
436 |
{ NULL } |
437 |
}; |
438 |
|
439 |
if (kvm_nlist(kvmd, symbols) != 0) { |
440 |
return NULL; |
441 |
} |
442 |
addr = symbols[0].n_value; |
443 |
} |
444 |
|
445 |
if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) { |
446 |
return NULL; |
447 |
} |
448 |
return &uvm; |
449 |
} |
450 |
#endif |
451 |
|
452 |
int statgrab_init(){ |
453 |
#ifdef ALLBSD |
454 |
{ |
455 |
kvm_t *kvmd = get_kvm(); |
456 |
if (kvmd == NULL) return 1; |
457 |
} |
458 |
#endif |
459 |
#ifdef NETBSD |
460 |
{ |
461 |
struct uvmexp *uvm = get_uvmexp(); |
462 |
if (uvm == NULL) return 1; |
463 |
} |
464 |
#endif |
465 |
#ifdef SOLARIS |
466 |
/* On solaris 7, this will fail if you are not root. But, everything |
467 |
* will still work, just no disk mappings. So we will ignore the exit |
468 |
* status of this, and carry on merrily. |
469 |
*/ |
470 |
build_mapping(); |
471 |
#endif |
472 |
return 0; |
473 |
} |
474 |
|
475 |
int statgrab_drop_privileges() { |
476 |
if (setegid(getgid()) != 0) return -1; |
477 |
if (seteuid(getuid()) != 0) return -1; |
478 |
return 0; |
479 |
} |
480 |
|