ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/tools.c (file contents):
Revision 1.5 by tdb, Wed Apr 9 21:01:53 2003 UTC vs.
Revision 1.56 by ats, Sun Jul 18 21:30:12 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines