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.15 by tdb, Wed Nov 19 21:56:21 2003 UTC vs.
Revision 1.37 by ats, Sun Apr 4 23:55:48 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 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>
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 50 | 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 60 | Line 71 | struct map{
71   typedef struct map mapping_t;
72  
73   static mapping_t *mapping = NULL;
74 + #endif
75  
76 + #ifdef SOLARIS
77   char *get_svr_from_bsd(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 + #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;
# Line 77 | Line 93 | void add_mapping(char *bsd, char *svr){
93          svr = strdup(svr);
94  
95          if (mapping == NULL){
80                printf("New malloc\n");
96                  mapping = malloc(sizeof(mapping_t));
97                  if (mapping == NULL) return;
98                  map_ptr = mapping;
# Line 85 | Line 100 | void add_mapping(char *bsd, char *svr){
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)) ){
88                                printf("%s matches %s\n", map_ptr->bsd, bsd);
103                                  return;
104                          }
105                          map_end_ptr = map_ptr;
# Line 95 | Line 109 | void add_mapping(char *bsd, char *svr){
109                   * new mapping_t
110                   */
111                  map_end_ptr->next = malloc(sizeof(mapping_t));
98                printf("Second malloc\n");
112                  if (map_end_ptr->next == NULL) return;
113                  map_ptr = map_end_ptr->next;
114          }
115  
103        printf("Adding %s\n", bsd);
116          map_ptr->next = NULL;
117          map_ptr->bsd = bsd;
118          map_ptr->svr = svr;
# Line 144 | Line 156 | char *read_dir(char *disk_path){
156                                  return svr_name;
157                          }
158                  }
147                closedir(dirp);
159          }
160 +        closedir(dirp);
161          return NULL;
162   }
163  
# Line 162 | Line 174 | int get_alias(char *alias){
174          char *value;
175          int instance;
176          if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
177 <                fprintf(stderr, "di_init() failed\n");
166 <                exit(1);
177 >                return 1;
178          }
179          node = di_drv_first_node(alias, root_node);
180          while (node != DI_NODE_NIL) {
# Line 173 | Line 184 | int get_alias(char *alias){
184                          minor_name = di_minor_name(minor);
185                          strcpy(tmpnode, alias);
186                          sprintf(tmpnode, "%s%d", tmpnode, instance);
187 <                        strcpy(file, "/devices");
188 <                        strcat(file, phys_path);
189 <                        strcat(file, ":");
190 <                        strcat(file, minor_name);
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);
# Line 188 | Line 199 | int get_alias(char *alias){
199                  }
200          }
201          di_fini(root_node);
202 <        return (-1);
202 >        return 0;
203   }
204  
205 < void build_mapping(){
206 <        char device_name[512];
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 NULL;
219 >                return;
220          }
221  
222          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
# Line 214 | Line 231 | void build_mapping(){
231                          }
232                          if(x == sizeof device_name) x--;
233                          device_name[x] = '\0';
234 <                        get_alias(device_name);
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;
259 >        return 0;
260   }
261  
262   #endif
# Line 270 | Line 308 | static long long atoll(const char *s) {
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;
# Line 280 | Line 434 | long long get_ll_match(char *line, regmatch_t *match){
434          return num;
435   }
436  
437 < #ifdef ALLBSD
437 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
438   kvm_t *get_kvm() {
439          static kvm_t *kvmd = NULL;
440  
# Line 291 | Line 445 | kvm_t *get_kvm() {
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 < #ifdef NETBSD
462 > #if defined(NETBSD) || defined(OPENBSD)
463   struct uvmexp *get_uvmexp() {
464 <        static u_long addr = 0;
465 <        static struct uvmexp uvm;
466 <        kvm_t *kvmd = get_kvm();
464 >        int mib[2];
465 >        size_t size;
466 >        static struct uvmexp *uvm = NULL;
467 >        struct uvmexp *new;
468  
469 <        if (kvmd == NULL) {
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 <        if (addr == 0) {
477 <                struct nlist symbols[] = {
478 <                        { "_uvmexp" },
309 <                        { NULL }
310 <                };
311 <
312 <                if (kvm_nlist(kvmd, symbols) != 0) {
313 <                        return NULL;
314 <                }
315 <                addr = symbols[0].n_value;
476 >        new = realloc(uvm, size);
477 >        if (new == NULL) {
478 >                return NULL;
479          }
480 +        uvm = new;
481  
482 <        if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
482 >        if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) {
483                  return NULL;
484          }
485 <        return &uvm;
485 >
486 >        return uvm;
487   }
488   #endif
489  
490   int statgrab_init(){
491 < #ifdef ALLBSD
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 < #ifdef NETBSD
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 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines