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.28 by ats, Fri Feb 13 15:22:49 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   #include <kvm.h>
37   #endif
38 < #ifdef NETBSD
38 > #if defined(NETBSD) || defined(OPENBSD)
39   #include <uvm/uvm_extern.h>
40 + #include <sys/param.h>
41 + #include <sys/sysctl.h>
42   #endif
43  
44   #include "tools.h"
45  
46   #ifdef SOLARIS
47 + #ifdef HAVE_LIBDEVINFO_H
48   #include <libdevinfo.h>
49 + #endif
50   #include <kstat.h>
51   #include <unistd.h>
52   #include <ctype.h>
# Line 50 | Line 58
58   #include <dirent.h>
59   #endif
60  
61 < #ifdef SOLARIS
61 > #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
62   struct map{
63          char *bsd;
64          char *svr;
# Line 60 | Line 68 | struct map{
68   typedef struct map mapping_t;
69  
70   static mapping_t *mapping = NULL;
71 + #endif
72  
73 + #ifdef SOLARIS
74   char *get_svr_from_bsd(char *bsd){
75 + #ifdef HAVE_LIBDEVINFO_H
76          mapping_t *map_ptr;
77          for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next)
78                  if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr;
79 <
79 > #endif
80          return bsd;
81   }
82 + #endif
83  
84 + #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
85   void add_mapping(char *bsd, char *svr){
86          mapping_t *map_ptr;
87          mapping_t *map_end_ptr;
# Line 77 | Line 90 | void add_mapping(char *bsd, char *svr){
90          svr = strdup(svr);
91  
92          if (mapping == NULL){
80                printf("New malloc\n");
93                  mapping = malloc(sizeof(mapping_t));
94                  if (mapping == NULL) return;
95                  map_ptr = mapping;
# Line 85 | Line 97 | void add_mapping(char *bsd, char *svr){
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)) ){
88                                printf("%s matches %s\n", map_ptr->bsd, bsd);
100                                  return;
101                          }
102                          map_end_ptr = map_ptr;
# Line 95 | Line 106 | void add_mapping(char *bsd, char *svr){
106                   * new mapping_t
107                   */
108                  map_end_ptr->next = malloc(sizeof(mapping_t));
98                printf("Second malloc\n");
109                  if (map_end_ptr->next == NULL) return;
110                  map_ptr = map_end_ptr->next;
111          }
112  
103        printf("Adding %s\n", bsd);
113          map_ptr->next = NULL;
114          map_ptr->bsd = bsd;
115          map_ptr->svr = svr;
# Line 144 | Line 153 | char *read_dir(char *disk_path){
153                                  return svr_name;
154                          }
155                  }
147                closedir(dirp);
156          }
157 +        closedir(dirp);
158          return NULL;
159   }
160  
# Line 162 | Line 171 | int get_alias(char *alias){
171          char *value;
172          int instance;
173          if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
174 <                fprintf(stderr, "di_init() failed\n");
166 <                exit(1);
174 >                return 1;
175          }
176          node = di_drv_first_node(alias, root_node);
177          while (node != DI_NODE_NIL) {
# Line 173 | 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 <                        strcpy(file, "/devices");
185 <                        strcat(file, phys_path);
186 <                        strcat(file, ":");
187 <                        strcat(file, minor_name);
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);
188                          value = read_dir(file);
189                          if (value != NULL){
190                                  add_mapping(tmpnode, value);
# Line 188 | Line 196 | int get_alias(char *alias){
196                  }
197          }
198          di_fini(root_node);
199 <        return (-1);
199 >        return 0;
200   }
201  
202 < void build_mapping(){
203 <        char device_name[512];
202 >
203 > #define BIG_ENOUGH 512
204 > 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 NULL;
216 >                return;
217          }
218  
219          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
# Line 214 | Line 228 | void build_mapping(){
228                          }
229                          if(x == sizeof device_name) x--;
230                          device_name[x] = '\0';
231 <                        get_alias(device_name);
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  
256 <        return;
256 >        return 0;
257   }
258  
259   #endif
# Line 270 | Line 305 | static long long atoll(const char *s) {
305   }
306   #endif
307  
308 + #ifndef HAVE_STRLCPY
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 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 + #endif
356 +
357 + #ifndef HAVE_STRLCAT
358 + /*      $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $    */
359 +
360 + /*
361 + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
362 + *
363 + * Permission to use, copy, modify, and distribute this software for any
364 + * purpose with or without fee is hereby granted, provided that the above
365 + * copyright notice and this permission notice appear in all copies.
366 + *
367 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
368 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
369 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
370 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
371 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
372 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
373 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
374 + */
375 +
376 + /*
377 + * Appends src to string dst of size siz (unlike strncat, siz is the
378 + * full size of dst, not space left).  At most siz-1 characters
379 + * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
380 + * Returns strlen(src) + MIN(siz, strlen(initial dst)).
381 + * If retval >= siz, truncation occurred.
382 + */
383 + size_t strlcat(char *dst, const char *src, size_t siz){
384 +        register char *d = dst;
385 +        register const char *s = src;
386 +        register size_t n = siz;
387 +        size_t dlen;
388 +
389 +        /* Find the end of dst and adjust bytes left but don't go past end */
390 +        while (n-- != 0 && *d != '\0')
391 +                d++;
392 +        dlen = d - dst;
393 +        n = siz - dlen;
394 +
395 +        if (n == 0)
396 +                return(dlen + strlen(s));
397 +        while (*s != '\0') {
398 +                if (n != 1) {
399 +                        *d++ = *s;
400 +                        n--;
401 +                }
402 +                s++;
403 +        }
404 +        *d = '\0';
405 +
406 +        return(dlen + (s - src));       /* count does not include NUL */
407 + }
408 +
409 + #endif
410 +
411   long long get_ll_match(char *line, regmatch_t *match){
412          char *ptr;
413          long long num;
# Line 293 | Line 431 | kvm_t *get_kvm() {
431   }
432   #endif
433  
434 < #ifdef NETBSD
434 > #if defined(NETBSD) || defined(OPENBSD)
435   struct uvmexp *get_uvmexp() {
436 <        static u_long addr = 0;
437 <        static struct uvmexp uvm;
438 <        kvm_t *kvmd = get_kvm();
436 >        int mib[2];
437 >        size_t size;
438 >        static struct uvmexp *uvm = NULL;
439  
440 <        if (kvmd == NULL) {
440 >        mib[0] = CTL_VM;
441 >        mib[1] = VM_UVMEXP;
442 >
443 >        if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) {
444                  return NULL;
445          }
446  
447 <        if (addr == 0) {
448 <                struct nlist symbols[] = {
449 <                        { "_uvmexp" },
309 <                        { NULL }
310 <                };
311 <
312 <                if (kvm_nlist(kvmd, symbols) != 0) {
313 <                        return NULL;
314 <                }
315 <                addr = symbols[0].n_value;
447 >        uvm = realloc(uvm, size);
448 >        if (uvm == NULL) {
449 >                return NULL;
450          }
451  
452 <        if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
452 >        if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) {
453                  return NULL;
454          }
455 <        return &uvm;
455 >
456 >        return uvm;
457   }
458   #endif
459  
# Line 336 | Line 471 | int statgrab_init(){
471          }
472   #endif
473   #ifdef SOLARIS
474 +        /* On solaris 7, this will fail if you are not root. But, everything
475 +         * will still work, just no disk mappings. So we will ignore the exit
476 +         * status of this, and carry on merrily.
477 +         */
478 + #ifdef HAVE_LIBDEVINFO_H
479          build_mapping();
480   #endif
481 + #endif
482          return 0;
483   }
484 +
485 + int statgrab_drop_privileges() {
486 +        if (setegid(getgid()) != 0) return -1;
487 +        if (seteuid(getuid()) != 0) return -1;
488 +        return 0;
489 + }
490 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines