ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
Revision: 1.38
Committed: Mon Apr 5 00:01:48 2004 UTC (20 years, 1 month ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.37: +2 -2 lines
Log Message:
Add prototype for get_svr_from_bsd, and make it take and return const
char *.

File Contents

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