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.18 by ats, Mon Jan 5 17:20:29 2004 UTC vs.
Revision 1.41 by ats, Mon Apr 5 15:40:15 2004 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * i-scream central monitoring system
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
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 30 | Line 33
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 < #ifdef NETBSD
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>
# Line 51 | Line 61
61   #include <dirent.h>
62   #endif
63  
64 < #ifdef SOLARIS
64 > #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
65   struct map{
66          char *bsd;
67          char *svr;
# Line 61 | Line 71 | struct map{
71   typedef struct map mapping_t;
72  
73   static mapping_t *mapping = NULL;
74 + #endif
75  
76 < char *get_svr_from_bsd(char *bsd){
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 <
82 > #endif
83          return bsd;
84   }
85 + #endif
86  
87 < void add_mapping(char *bsd, char *svr){
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  
77        bsd = strdup(bsd);
78        svr = strdup(svr);
79
92          if (mapping == NULL){
93                  mapping = malloc(sizeof(mapping_t));
94                  if (mapping == NULL) return;
# Line 99 | Line 111 | void add_mapping(char *bsd, char *svr){
111          }
112  
113          map_ptr->next = NULL;
114 <        map_ptr->bsd = bsd;
115 <        map_ptr->svr = svr;
114 >        map_ptr->bsd = strdup(bsd);
115 >        map_ptr->svr = strdup(svr);
116  
117          return;
118   }
119  
120 < char *read_dir(char *disk_path){
120 >
121 > static char *read_dir(char *disk_path){
122          DIR *dirp;
123          struct dirent *dp;
124          struct stat stbuf;
# Line 147 | Line 160 | char *read_dir(char *disk_path){
160   }
161  
162  
163 <
151 < int get_alias(char *alias){
163 > static int get_alias(char *alias){
164          char file[MAXPATHLEN];
165          di_node_t root_node;
166          di_node_t node;
# Line 169 | Line 181 | int get_alias(char *alias){
181                          minor_name = di_minor_name(minor);
182                          strcpy(tmpnode, alias);
183                          sprintf(tmpnode, "%s%d", tmpnode, instance);
184 <                        strlcpy(file, "/devices", sizeof file);
185 <                        strlcat(file, phys_path, sizeof file);
186 <                        strlcat(file, ":", sizeof file);
187 <                        strlcat(file, minor_name, sizeof file);
184 >                        sg_strlcpy(file, "/devices", sizeof file);
185 >                        sg_strlcat(file, phys_path, sizeof file);
186 >                        sg_strlcat(file, ":", sizeof file);
187 >                        sg_strlcat(file, minor_name, sizeof file);
188                          value = read_dir(file);
189                          if (value != NULL){
190                                  add_mapping(tmpnode, value);
# Line 187 | Line 199 | int get_alias(char *alias){
199          return 0;
200   }
201  
202 < int build_mapping(){
203 <        char device_name[512];
202 >
203 > #define BIG_ENOUGH 512
204 > static int build_mapping(){
205 >        char device_name[BIG_ENOUGH];
206          int x;
207          kstat_ctl_t *kc;
208          kstat_t *ksp;
209          kstat_io_t kios;
210  
211 +        char driver_list[BIG_ENOUGH][BIG_ENOUGH];
212 +        int list_entries = 0;
213 +        int found;
214 +
215          if ((kc = kstat_open()) == NULL) {
216 <                return;
216 >                return 1;
217          }
218  
219          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
# Line 210 | Line 228 | int build_mapping(){
228                          }
229                          if(x == sizeof device_name) x--;
230                          device_name[x] = '\0';
231 <                        if((get_alias(device_name)) != 0){
232 <                                return 1;
231 >
232 >                        /* Check if we've not already looked it up */
233 >                        found = 0;
234 >                        for(x=0;x<list_entries;x++){
235 >                                if (x>=BIG_ENOUGH){
236 >                                        /* We've got bigger than we thought was massive */
237 >                                        /* If we hit this.. Make big enough bigger */
238 >                                        return 1;
239 >                                }
240 >                                if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){
241 >                                        found = 1;
242 >                                        break;
243 >                                }
244                          }
245 +
246 +                        if(!found){
247 +                                if((get_alias(device_name)) != 0){
248 +                                        return 1;
249 +                                }
250 +                                strncpy(driver_list[x], device_name, BIG_ENOUGH);
251 +                                list_entries++;
252 +                        }
253                  }
254          }
255  
# Line 223 | Line 260 | int build_mapping(){
260  
261  
262  
263 < char *f_read_line(FILE *f, const char *string){
263 > char *sg_f_read_line(FILE *f, const char *string){
264          /* Max line length. 8k should be more than enough */
265          static char line[8192];
266  
# Line 236 | Line 273 | char *f_read_line(FILE *f, const char *string){
273          return NULL;
274   }
275  
276 < char *get_string_match(char *line, regmatch_t *match){
276 > char *sg_get_string_match(char *line, regmatch_t *match){
277          int len=match->rm_eo - match->rm_so;
278          char *match_string=malloc(len+1);
279  
# Line 268 | Line 305 | static long long atoll(const char *s) {
305   }
306   #endif
307  
308 < long long get_ll_match(char *line, regmatch_t *match){
308 > /*      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
309 >
310 > /*
311 > * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
312 > *
313 > * Permission to use, copy, modify, and distribute this software for any
314 > * purpose with or without fee is hereby granted, provided that the above
315 > * copyright notice and this permission notice appear in all copies.
316 > *
317 > * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
318 > * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
319 > * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
320 > * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
321 > * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
322 > * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
323 > * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
324 > */
325 >
326 > /*
327 > * Copy src to string dst of size siz.  At most siz-1 characters
328 > * will be copied.  Always NUL terminates (unless siz == 0).
329 > * Returns strlen(src); if retval >= siz, truncation occurred.
330 > */
331 > size_t sg_strlcpy(char *dst, const char *src, size_t siz){
332 >        register char *d = dst;
333 >        register const char *s = src;
334 >        register size_t n = siz;
335 >
336 >        /* Copy as many bytes as will fit */
337 >        if (n != 0 && --n != 0) {
338 >                do {
339 >                        if ((*d++ = *s++) == 0)
340 >                                break;
341 >                } while (--n != 0);
342 >        }
343 >
344 >        /* Not enough room in dst, add NUL and traverse rest of src */
345 >        if (n == 0) {
346 >                if (siz != 0)
347 >                        *d = '\0';              /* NUL-terminate dst */
348 >                while (*s++)
349 >                        ;
350 >        }
351 >
352 >        return(s - src - 1);    /* count does not include NUL */
353 > }
354 >
355 > /*      $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $    */
356 >
357 > /*
358 > * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
359 > *
360 > * Permission to use, copy, modify, and distribute this software for any
361 > * purpose with or without fee is hereby granted, provided that the above
362 > * copyright notice and this permission notice appear in all copies.
363 > *
364 > * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
365 > * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
366 > * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
367 > * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
368 > * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
369 > * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
370 > * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
371 > */
372 >
373 > /*
374 > * Appends src to string dst of size siz (unlike strncat, siz is the
375 > * full size of dst, not space left).  At most siz-1 characters
376 > * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
377 > * Returns strlen(src) + MIN(siz, strlen(initial dst)).
378 > * If retval >= siz, truncation occurred.
379 > */
380 > size_t sg_strlcat(char *dst, const char *src, size_t siz){
381 >        register char *d = dst;
382 >        register const char *s = src;
383 >        register size_t n = siz;
384 >        size_t dlen;
385 >
386 >        /* Find the end of dst and adjust bytes left but don't go past end */
387 >        while (n-- != 0 && *d != '\0')
388 >                d++;
389 >        dlen = d - dst;
390 >        n = siz - dlen;
391 >
392 >        if (n == 0)
393 >                return(dlen + strlen(s));
394 >        while (*s != '\0') {
395 >                if (n != 1) {
396 >                        *d++ = *s;
397 >                        n--;
398 >                }
399 >                s++;
400 >        }
401 >        *d = '\0';
402 >
403 >        return(dlen + (s - src));       /* count does not include NUL */
404 > }
405 >
406 > char *sg_update_string(char **dest, const char *src) {
407 >        char *new;
408 >
409 >        new = realloc(*dest, strlen(src) + 1);
410 >        if (new == NULL) {
411 >                return NULL;
412 >        }
413 >
414 >        strcpy(new, src);
415 >        *dest = new;
416 >        return new;
417 > }
418 >
419 > long long sg_get_ll_match(char *line, regmatch_t *match){
420          char *ptr;
421          long long num;
422  
# Line 278 | Line 426 | long long get_ll_match(char *line, regmatch_t *match){
426          return num;
427   }
428  
429 < #ifdef ALLBSD
430 < kvm_t *get_kvm() {
429 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
430 > kvm_t *sg_get_kvm() {
431          static kvm_t *kvmd = NULL;
432  
433          if (kvmd != NULL) {
# Line 289 | Line 437 | kvm_t *get_kvm() {
437          kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
438          return kvmd;
439   }
440 +
441 + /* Can't think of a better name for this function */
442 + kvm_t *sg_get_kvm2() {
443 +        static kvm_t *kvmd2 = NULL;
444 +
445 +        if (kvmd2 != NULL) {
446 +                return kvmd2;
447 +        }
448 +
449 +        kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
450 +        return kvmd2;
451 + }
452   #endif
453  
454 < #ifdef NETBSD
455 < struct uvmexp *get_uvmexp() {
456 <        static u_long addr = 0;
457 <        static struct uvmexp uvm;
458 <        kvm_t *kvmd = get_kvm();
454 > #if defined(NETBSD) || defined(OPENBSD)
455 > struct uvmexp *sg_get_uvmexp() {
456 >        int mib[2];
457 >        size_t size;
458 >        static struct uvmexp *uvm = NULL;
459 >        struct uvmexp *new;
460  
461 <        if (kvmd == NULL) {
461 >        mib[0] = CTL_VM;
462 >        mib[1] = VM_UVMEXP;
463 >
464 >        if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) {
465                  return NULL;
466          }
467  
468 <        if (addr == 0) {
469 <                struct nlist symbols[] = {
470 <                        { "_uvmexp" },
307 <                        { NULL }
308 <                };
309 <
310 <                if (kvm_nlist(kvmd, symbols) != 0) {
311 <                        return NULL;
312 <                }
313 <                addr = symbols[0].n_value;
468 >        new = realloc(uvm, size);
469 >        if (new == NULL) {
470 >                return NULL;
471          }
472 +        uvm = new;
473  
474 <        if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
474 >        if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) {
475                  return NULL;
476          }
477 <        return &uvm;
477 >
478 >        return uvm;
479   }
480   #endif
481  
482 < int statgrab_init(){
483 < #ifdef ALLBSD
484 <        {
485 <                kvm_t *kvmd = get_kvm();
327 <                if (kvmd == NULL) return 1;
482 > int sg_init(){
483 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
484 >        if (sg_get_kvm() == NULL) {
485 >                return 1;
486          }
487 < #endif
488 < #ifdef NETBSD
331 <        {
332 <                struct uvmexp *uvm = get_uvmexp();
333 <                if (uvm == NULL) return 1;
487 >        if (sg_get_kvm2() == NULL) {
488 >                return 1;
489          }
490   #endif
491 + #if defined(NETBSD) || defined(OPENBSD)
492 +        /* This should always succeed, but it seems that on some
493 +         * versions of NetBSD the first call to get_uvmexp will return
494 +         * a non-filled-in structure; this is a workaround for that.
495 +         */
496 +        if (sg_get_uvmexp() == NULL) return 1;
497 + #endif
498   #ifdef SOLARIS
499 <        if((build_mapping()) != 0) return 1;
499 >        /* On solaris 7, this will fail if you are not root. But, everything
500 >         * will still work, just no disk mappings. So we will ignore the exit
501 >         * status of this, and carry on merrily.
502 >         */
503 > #ifdef HAVE_LIBDEVINFO_H
504 >        build_mapping();
505   #endif
506 + #endif
507          return 0;
508   }
509  
510 < int statgrab_drop_privileges() {
510 > int sg_drop_privileges() {
511          if (setegid(getgid()) != 0) return -1;
512          if (seteuid(getuid()) != 0) return -1;
513          return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines