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.1 by pajs, Tue Mar 4 18:07:42 2003 UTC vs.
Revision 1.46 by tdb, Wed Apr 7 09:44:08 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines