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.14 by pajs, Thu Nov 13 17:02:46 2003 UTC vs.
Revision 1.29 by tdb, Sat Feb 14 12:38:18 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 + #ifdef FREEBSD
38   #include <kvm.h>
39   #endif
40 < #ifdef NETBSD
40 > #if defined(NETBSD) || defined(OPENBSD)
41   #include <uvm/uvm_extern.h>
42 + #include <sys/param.h>
43 + #include <sys/sysctl.h>
44   #endif
45  
46   #include "tools.h"
47  
48   #ifdef SOLARIS
49 + #ifdef HAVE_LIBDEVINFO_H
50   #include <libdevinfo.h>
51 + #endif
52   #include <kstat.h>
53   #include <unistd.h>
54   #include <ctype.h>
# Line 50 | Line 60
60   #include <dirent.h>
61   #endif
62  
63 < #ifdef SOLARIS
63 > #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
64   struct map{
65          char *bsd;
66          char *svr;
# Line 60 | Line 70 | struct map{
70   typedef struct map mapping_t;
71  
72   static mapping_t *mapping = NULL;
73 + #endif
74  
75 + #ifdef SOLARIS
76   char *get_svr_from_bsd(char *bsd){
77 + #ifdef HAVE_LIBDEVINFO_H
78          mapping_t *map_ptr;
79          for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next)
80                  if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr;
81 <
81 > #endif
82          return bsd;
83   }
84 + #endif
85  
86 + #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
87   void add_mapping(char *bsd, char *svr){
88          mapping_t *map_ptr;
89          mapping_t *map_end_ptr;
# Line 77 | Line 92 | void add_mapping(char *bsd, char *svr){
92          svr = strdup(svr);
93  
94          if (mapping == NULL){
80                printf("New malloc\n");
95                  mapping = malloc(sizeof(mapping_t));
96                  if (mapping == NULL) return;
97                  map_ptr = mapping;
# Line 85 | Line 99 | void add_mapping(char *bsd, char *svr){
99                  /* See if its already been added */
100                  for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next){
101                          if( (!strcmp(map_ptr->bsd, bsd)) || (!strcmp(map_ptr->svr, svr)) ){
88                                printf("%s matches %s\n", map_ptr->bsd, bsd);
102                                  return;
103                          }
104                          map_end_ptr = map_ptr;
# Line 95 | Line 108 | void add_mapping(char *bsd, char *svr){
108                   * new mapping_t
109                   */
110                  map_end_ptr->next = malloc(sizeof(mapping_t));
98                printf("Second malloc\n");
111                  if (map_end_ptr->next == NULL) return;
112                  map_ptr = map_end_ptr->next;
113          }
114  
103        printf("Adding %s\n", bsd);
115          map_ptr->next = NULL;
116          map_ptr->bsd = bsd;
117          map_ptr->svr = svr;
# Line 110 | Line 121 | void add_mapping(char *bsd, char *svr){
121  
122   char *read_dir(char *disk_path){
123          DIR *dirp;
124 <        struct dirent *dp;
125 <        struct stat stbuf;
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];
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){
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){
# Line 130 | Line 141 | char *read_dir(char *disk_path){
141                  }
142          }
143  
144 <        while ((dp = readdir(dirp)) != NULL){
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);
# Line 144 | Line 155 | char *read_dir(char *disk_path){
155                                  return svr_name;
156                          }
157                  }
147                closedir(dirp);
158          }
159 +        closedir(dirp);
160          return NULL;
161   }
162  
# Line 162 | Line 173 | int get_alias(char *alias){
173          char *value;
174          int instance;
175          if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
176 <                fprintf(stderr, "di_init() failed\n");
166 <                exit(1);
176 >                return 1;
177          }
178          node = di_drv_first_node(alias, root_node);
179          while (node != DI_NODE_NIL) {
# Line 173 | Line 183 | int get_alias(char *alias){
183                          minor_name = di_minor_name(minor);
184                          strcpy(tmpnode, alias);
185                          sprintf(tmpnode, "%s%d", tmpnode, instance);
186 <                        strcpy(file, "/devices");
187 <                        strcat(file, phys_path);
188 <                        strcat(file, ":");
189 <                        strcat(file, minor_name);
186 >                        strlcpy(file, "/devices", sizeof file);
187 >                        strlcat(file, phys_path, sizeof file);
188 >                        strlcat(file, ":", sizeof file);
189 >                        strlcat(file, minor_name, sizeof file);
190                          value = read_dir(file);
191                          if (value != NULL){
192                                  add_mapping(tmpnode, value);
# Line 186 | Line 196 | int get_alias(char *alias){
196                  }else{
197                          node = di_drv_next_node(node);
198                  }
199 <        }                           /* End of the while loop */
199 >        }
200          di_fini(root_node);
201 <        return (-1);
201 >        return 0;
202   }
203  
204 < void build_mapping(){
205 <        char device_name[512];
204 >
205 > #define BIG_ENOUGH 512
206 > int build_mapping(){
207 >        char device_name[BIG_ENOUGH];
208          int x;
209 <        kstat_ctl_t *kc;
210 <        kstat_t *ksp;
211 <        kstat_io_t kios;
209 >        kstat_ctl_t *kc;
210 >        kstat_t *ksp;
211 >        kstat_io_t kios;
212  
213 +        char driver_list[BIG_ENOUGH][BIG_ENOUGH];
214 +        int list_entries = 0;
215 +        int found;
216 +
217          if ((kc = kstat_open()) == NULL) {
218 <                return NULL;
219 <        }
218 >                return;
219 >        }
220  
221 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
222 <                if (!strcmp(ksp->ks_class, "disk")) {
223 <
224 <                        if(ksp->ks_type != KSTAT_TYPE_IO) continue;
225 <                        /* We dont want metadevices appearins as num_diskio */
226 <                        if(strcmp(ksp->ks_module, "md")==0) continue;
211 <                        if((kstat_read(kc, ksp, &kios))==-1) continue;
221 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
222 >                if (!strcmp(ksp->ks_class, "disk")) {
223 >                        if(ksp->ks_type != KSTAT_TYPE_IO) continue;
224 >                        /* We dont want metadevices appearing as num_diskio */
225 >                        if(strcmp(ksp->ks_module, "md")==0) continue;
226 >                        if((kstat_read(kc, ksp, &kios))==-1) continue;
227                          strncpy(device_name, ksp->ks_name, sizeof device_name);
228                          for(x=0;x<(sizeof device_name);x++){
229                                  if( isdigit((int)device_name[x]) ) break;
230                          }
231                          if(x == sizeof device_name) x--;
232                          device_name[x] = '\0';
233 <                        get_alias(device_name);
234 <                 }
233 >
234 >                        /* Check if we've not already looked it up */
235 >                        found = 0;
236 >                        for(x=0;x<list_entries;x++){
237 >                                if (x>=BIG_ENOUGH){
238 >                                        /* We've got bigger than we thought was massive */
239 >                                        /* If we hit this.. Make big enough bigger */
240 >                                        return 1;
241 >                                }
242 >                                if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){
243 >                                        found = 1;
244 >                                        break;
245 >                                }
246 >                        }
247 >
248 >                        if(!found){
249 >                                if((get_alias(device_name)) != 0){
250 >                                        return 1;
251 >                                }
252 >                                strncpy(driver_list[x], device_name, BIG_ENOUGH);
253 >                                list_entries++;
254 >                        }
255 >                }
256          }
257 <        
258 <        return;
257 >
258 >        return 0;
259   }
260  
261   #endif
# Line 271 | Line 307 | static long long atoll(const char *s) {
307   }
308   #endif
309  
310 + #ifndef HAVE_STRLCPY
311 + /*      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
312 +
313 + /*
314 + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
315 + *
316 + * Permission to use, copy, modify, and distribute this software for any
317 + * purpose with or without fee is hereby granted, provided that the above
318 + * copyright notice and this permission notice appear in all copies.
319 + *
320 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
321 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
322 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
323 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
324 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
325 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
326 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
327 + */
328 +
329 + /*
330 + * Copy src to string dst of size siz.  At most siz-1 characters
331 + * will be copied.  Always NUL terminates (unless siz == 0).
332 + * Returns strlen(src); if retval >= siz, truncation occurred.
333 + */
334 + size_t strlcpy(char *dst, const char *src, size_t siz){
335 +        register char *d = dst;
336 +        register const char *s = src;
337 +        register size_t n = siz;
338 +
339 +        /* Copy as many bytes as will fit */
340 +        if (n != 0 && --n != 0) {
341 +                do {
342 +                        if ((*d++ = *s++) == 0)
343 +                                break;
344 +                } while (--n != 0);
345 +        }
346 +
347 +        /* Not enough room in dst, add NUL and traverse rest of src */
348 +        if (n == 0) {
349 +                if (siz != 0)
350 +                        *d = '\0';              /* NUL-terminate dst */
351 +                while (*s++)
352 +                        ;
353 +        }
354 +
355 +        return(s - src - 1);    /* count does not include NUL */
356 + }
357 + #endif
358 +
359 + #ifndef HAVE_STRLCAT
360 + /*      $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $    */
361 +
362 + /*
363 + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
364 + *
365 + * Permission to use, copy, modify, and distribute this software for any
366 + * purpose with or without fee is hereby granted, provided that the above
367 + * copyright notice and this permission notice appear in all copies.
368 + *
369 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
370 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
371 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
372 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
373 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
374 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
375 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
376 + */
377 +
378 + /*
379 + * Appends src to string dst of size siz (unlike strncat, siz is the
380 + * full size of dst, not space left).  At most siz-1 characters
381 + * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
382 + * Returns strlen(src) + MIN(siz, strlen(initial dst)).
383 + * If retval >= siz, truncation occurred.
384 + */
385 + size_t strlcat(char *dst, const char *src, size_t siz){
386 +        register char *d = dst;
387 +        register const char *s = src;
388 +        register size_t n = siz;
389 +        size_t dlen;
390 +
391 +        /* Find the end of dst and adjust bytes left but don't go past end */
392 +        while (n-- != 0 && *d != '\0')
393 +                d++;
394 +        dlen = d - dst;
395 +        n = siz - dlen;
396 +
397 +        if (n == 0)
398 +                return(dlen + strlen(s));
399 +        while (*s != '\0') {
400 +                if (n != 1) {
401 +                        *d++ = *s;
402 +                        n--;
403 +                }
404 +                s++;
405 +        }
406 +        *d = '\0';
407 +
408 +        return(dlen + (s - src));       /* count does not include NUL */
409 + }
410 +
411 + #endif
412 +
413   long long get_ll_match(char *line, regmatch_t *match){
414          char *ptr;
415          long long num;
# Line 281 | Line 420 | long long get_ll_match(char *line, regmatch_t *match){
420          return num;
421   }
422  
423 < #ifdef ALLBSD
423 > #ifdef FREEBSD
424   kvm_t *get_kvm() {
425          static kvm_t *kvmd = NULL;
426  
# Line 294 | Line 433 | kvm_t *get_kvm() {
433   }
434   #endif
435  
436 < #ifdef NETBSD
436 > #if defined(NETBSD) || defined(OPENBSD)
437   struct uvmexp *get_uvmexp() {
438 <        static u_long addr = 0;
439 <        static struct uvmexp uvm;
440 <        kvm_t *kvmd = get_kvm();
438 >        int mib[2];
439 >        size_t size;
440 >        static struct uvmexp *uvm = NULL;
441  
442 <        if (kvmd == NULL) {
442 >        mib[0] = CTL_VM;
443 >        mib[1] = VM_UVMEXP;
444 >
445 >        if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) {
446                  return NULL;
447          }
448  
449 <        if (addr == 0) {
450 <                struct nlist symbols[] = {
451 <                        { "_uvmexp" },
310 <                        { NULL }
311 <                };
312 <
313 <                if (kvm_nlist(kvmd, symbols) != 0) {
314 <                        return NULL;
315 <                }
316 <                addr = symbols[0].n_value;
449 >        uvm = realloc(uvm, size);
450 >        if (uvm == NULL) {
451 >                return NULL;
452          }
453  
454 <        if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
454 >        if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) {
455                  return NULL;
456          }
457 <        return &uvm;
457 >
458 >        return uvm;
459   }
460   #endif
461  
462   int statgrab_init(){
463 < #ifdef ALLBSD
463 > #ifdef FREEBSD
464          {
465                  kvm_t *kvmd = get_kvm();
466 <                if (kvmd == NULL) return 1;
466 >                if (kvmd == NULL) return 1;
467          }
468   #endif
469   #ifdef NETBSD
# Line 337 | Line 473 | int statgrab_init(){
473          }
474   #endif
475   #ifdef SOLARIS
476 +        /* On solaris 7, this will fail if you are not root. But, everything
477 +         * will still work, just no disk mappings. So we will ignore the exit
478 +         * status of this, and carry on merrily.
479 +         */
480 + #ifdef HAVE_LIBDEVINFO_H
481          build_mapping();
482   #endif
483 + #endif
484          return 0;
485   }
486 +
487 + int statgrab_drop_privileges() {
488 +        if (setegid(getgid()) != 0) return -1;
489 +        if (seteuid(getuid()) != 0) return -1;
490 +        return 0;
491 + }
492 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines