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.6 by tdb, Sun Aug 24 20:24:09 2003 UTC vs.
Revision 1.22 by pajs, Thu Jan 15 22:21:37 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines