ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
Revision: 1.28
Committed: Fri Feb 13 15:22:49 2004 UTC (20 years, 3 months ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.27: +18 -19 lines
Log Message:
Use sysctl to read struct uvmexp, since this works on both NetBSD and OpenBSD.

File Contents

# User Rev Content
1 tdb 1.23 /*
2 tdb 1.5 * i-scream central monitoring system
3 tdb 1.6 * http://www.i-scream.org
4 tdb 1.23 * Copyright (C) 2000-2004 i-scream
5 tdb 1.5 *
6 tdb 1.23 * 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 tdb 1.5 *
11 tdb 1.23 * This library is distributed in the hope that it will be useful,
12 tdb 1.5 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 tdb 1.23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * Lesser General Public License for more details.
15 tdb 1.5 *
16 tdb 1.23 * 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 tdb 1.24 *
21 ats 1.28 * $Id: tools.c,v 1.27 2004/02/10 16:23:07 pajs Exp $
22 tdb 1.5 */
23    
24     #ifdef HAVE_CONFIG_H
25     #include "config.h"
26     #endif
27    
28 pajs 1.1 #include <stdio.h>
29     #include <string.h>
30 pajs 1.3 #include <stdlib.h>
31 ats 1.18 #include <unistd.h>
32 pajs 1.3 #include <sys/types.h>
33     #include <regex.h>
34 ats 1.9 #ifdef ALLBSD
35 ats 1.8 #include <fcntl.h>
36     #include <kvm.h>
37     #endif
38 ats 1.28 #if defined(NETBSD) || defined(OPENBSD)
39 ats 1.9 #include <uvm/uvm_extern.h>
40 ats 1.28 #include <sys/param.h>
41     #include <sys/sysctl.h>
42 ats 1.9 #endif
43 pajs 1.1
44 ats 1.7 #include "tools.h"
45    
46 pajs 1.14 #ifdef SOLARIS
47 pajs 1.27 #ifdef HAVE_LIBDEVINFO_H
48 pajs 1.14 #include <libdevinfo.h>
49 pajs 1.27 #endif
50 pajs 1.14 #include <kstat.h>
51     #include <unistd.h>
52     #include <ctype.h>
53     #include <sys/types.h>
54     #include <sys/dkio.h>
55     #include <sys/stat.h>
56     #include <fcntl.h>
57     #include <sys/fcntl.h>
58     #include <dirent.h>
59     #endif
60    
61 pajs 1.26 #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
62 pajs 1.14 struct map{
63     char *bsd;
64     char *svr;
65    
66     struct map *next;
67     };
68     typedef struct map mapping_t;
69    
70     static mapping_t *mapping = NULL;
71 pajs 1.26 #endif
72 pajs 1.14
73 pajs 1.26 #ifdef SOLARIS
74 pajs 1.14 char *get_svr_from_bsd(char *bsd){
75 pajs 1.26 #ifdef HAVE_LIBDEVINFO_H
76 pajs 1.14 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 pajs 1.26 #endif
80 pajs 1.14 return bsd;
81     }
82 pajs 1.26 #endif
83 pajs 1.14
84 pajs 1.26 #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
85 pajs 1.14 void add_mapping(char *bsd, char *svr){
86     mapping_t *map_ptr;
87     mapping_t *map_end_ptr;
88    
89     bsd = strdup(bsd);
90     svr = strdup(svr);
91    
92     if (mapping == NULL){
93     mapping = malloc(sizeof(mapping_t));
94     if (mapping == NULL) return;
95     map_ptr = mapping;
96     }else{
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)) ){
100     return;
101     }
102     map_end_ptr = map_ptr;
103     }
104    
105     /* We've reached end of list and not found the entry.. So we need to malloc
106     * new mapping_t
107     */
108     map_end_ptr->next = malloc(sizeof(mapping_t));
109     if (map_end_ptr->next == NULL) return;
110     map_ptr = map_end_ptr->next;
111     }
112    
113     map_ptr->next = NULL;
114     map_ptr->bsd = bsd;
115     map_ptr->svr = svr;
116    
117     return;
118     }
119    
120     char *read_dir(char *disk_path){
121     DIR *dirp;
122 tdb 1.15 struct dirent *dp;
123     struct stat stbuf;
124 pajs 1.14 char *svr_name;
125 tdb 1.15 char current_dir[MAXPATHLEN];
126     char file_name[MAXPATHLEN];
127     char temp_name[MAXPATHLEN];
128     char dir_dname[MAXPATHLEN];
129 pajs 1.14 char *dsk_dir;
130     int x;
131    
132     dsk_dir = "/dev/osa/dev/dsk";
133 tdb 1.15 strncpy(current_dir, dsk_dir, sizeof current_dir);
134     if ((dirp = opendir(current_dir)) == NULL){
135 pajs 1.14 dsk_dir = "/dev/dsk";
136     snprintf(current_dir, sizeof current_dir, "%s", dsk_dir);
137     if ((dirp = opendir(current_dir)) == NULL){
138     return NULL;
139     }
140     }
141    
142 tdb 1.15 while ((dp = readdir(dirp)) != NULL){
143 pajs 1.14 snprintf(temp_name, sizeof temp_name, "../..%s", disk_path);
144     snprintf(dir_dname, sizeof dir_dname, "%s/%s", dsk_dir, dp->d_name);
145     stat(dir_dname,&stbuf);
146    
147     if (S_ISBLK(stbuf.st_mode)){
148     x = readlink(dir_dname, file_name, sizeof(file_name));
149     file_name[x] = '\0';
150     if (strcmp(file_name, temp_name) == 0) {
151     svr_name = strdup(dp->d_name);
152     closedir(dirp);
153     return svr_name;
154     }
155     }
156     }
157 tdb 1.16 closedir(dirp);
158 pajs 1.14 return NULL;
159     }
160    
161    
162    
163     int get_alias(char *alias){
164     char file[MAXPATHLEN];
165     di_node_t root_node;
166     di_node_t node;
167     di_minor_t minor = DI_MINOR_NIL;
168     char tmpnode[MAXPATHLEN];
169     char *phys_path;
170     char *minor_name;
171     char *value;
172     int instance;
173     if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
174 pajs 1.17 return 1;
175 pajs 1.14 }
176     node = di_drv_first_node(alias, root_node);
177     while (node != DI_NODE_NIL) {
178     if ((minor = di_minor_next(node, DI_MINOR_NIL)) != DI_MINOR_NIL) {
179     instance = di_instance(node);
180     phys_path = di_devfs_path(node);
181     minor_name = di_minor_name(minor);
182     strcpy(tmpnode, alias);
183     sprintf(tmpnode, "%s%d", tmpnode, instance);
184 pajs 1.17 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 pajs 1.14 value = read_dir(file);
189     if (value != NULL){
190     add_mapping(tmpnode, value);
191     }
192     di_devfs_path_free(phys_path);
193     node = di_drv_next_node(node);
194     }else{
195     node = di_drv_next_node(node);
196     }
197 tdb 1.15 }
198 pajs 1.14 di_fini(root_node);
199 pajs 1.17 return 0;
200 pajs 1.14 }
201    
202 pajs 1.22
203     #define BIG_ENOUGH 512
204 pajs 1.17 int build_mapping(){
205 pajs 1.22 char device_name[BIG_ENOUGH];
206 pajs 1.14 int x;
207 tdb 1.15 kstat_ctl_t *kc;
208     kstat_t *ksp;
209     kstat_io_t kios;
210 pajs 1.14
211 pajs 1.22 char driver_list[BIG_ENOUGH][BIG_ENOUGH];
212     int list_entries = 0;
213     int found;
214    
215 pajs 1.14 if ((kc = kstat_open()) == NULL) {
216 tdb 1.16 return;
217 tdb 1.15 }
218 pajs 1.14
219 tdb 1.15 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
220     if (!strcmp(ksp->ks_class, "disk")) {
221     if(ksp->ks_type != KSTAT_TYPE_IO) continue;
222     /* We dont want metadevices appearing as num_diskio */
223     if(strcmp(ksp->ks_module, "md")==0) continue;
224     if((kstat_read(kc, ksp, &kios))==-1) continue;
225 pajs 1.14 strncpy(device_name, ksp->ks_name, sizeof device_name);
226     for(x=0;x<(sizeof device_name);x++){
227     if( isdigit((int)device_name[x]) ) break;
228     }
229     if(x == sizeof device_name) x--;
230     device_name[x] = '\0';
231 pajs 1.22
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 pajs 1.17 }
253 tdb 1.15 }
254 pajs 1.14 }
255 tdb 1.15
256 pajs 1.17 return 0;
257 pajs 1.14 }
258    
259     #endif
260    
261    
262    
263 pajs 1.1 char *f_read_line(FILE *f, const char *string){
264     /* Max line length. 8k should be more than enough */
265     static char line[8192];
266    
267 pajs 1.2 while((fgets(line, sizeof(line), f))!=NULL){
268 pajs 1.1 if(strncmp(string, line, strlen(string))==0){
269     return line;
270     }
271     }
272    
273     return NULL;
274 pajs 1.3 }
275    
276     char *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    
280     match_string=strncpy(match_string, line+match->rm_so, len);
281     match_string[len]='\0';
282    
283     return match_string;
284     }
285 ats 1.7
286 pajs 1.14
287    
288 ats 1.13 #ifndef HAVE_ATOLL
289     static long long atoll(const char *s) {
290     long long value = 0;
291     int isneg = 0;
292    
293     while (*s == ' ' || *s == '\t') {
294     s++;
295     }
296     if (*s == '-') {
297     isneg = 1;
298     s++;
299     }
300     while (*s >= '0' && *s <= '9') {
301     value = (10 * value) + (*s - '0');
302     s++;
303     }
304     return (isneg ? -value : value);
305     }
306     #endif
307    
308 pajs 1.19 #ifndef HAVE_STRLCPY
309 ats 1.21 /* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */
310 pajs 1.19
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 pajs 1.3 long long get_ll_match(char *line, regmatch_t *match){
412     char *ptr;
413     long long num;
414    
415     ptr=line+match->rm_so;
416     num=atoll(ptr);
417    
418     return num;
419 ats 1.8 }
420    
421 ats 1.9 #ifdef ALLBSD
422 ats 1.8 kvm_t *get_kvm() {
423     static kvm_t *kvmd = NULL;
424    
425     if (kvmd != NULL) {
426     return kvmd;
427     }
428    
429     kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
430     return kvmd;
431 pajs 1.1 }
432 pajs 1.4 #endif
433 ats 1.9
434 ats 1.28 #if defined(NETBSD) || defined(OPENBSD)
435 ats 1.9 struct uvmexp *get_uvmexp() {
436 ats 1.28 int mib[2];
437     size_t size;
438     static struct uvmexp *uvm = NULL;
439 ats 1.9
440 ats 1.28 mib[0] = CTL_VM;
441     mib[1] = VM_UVMEXP;
442    
443     if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) {
444 ats 1.9 return NULL;
445     }
446    
447 ats 1.28 uvm = realloc(uvm, size);
448     if (uvm == NULL) {
449     return NULL;
450 ats 1.9 }
451    
452 ats 1.28 if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) {
453 ats 1.9 return NULL;
454     }
455 ats 1.28
456     return uvm;
457 ats 1.9 }
458     #endif
459    
460 pajs 1.10 int statgrab_init(){
461 pajs 1.11 #ifdef ALLBSD
462     {
463     kvm_t *kvmd = get_kvm();
464 tdb 1.15 if (kvmd == NULL) return 1;
465 pajs 1.11 }
466 pajs 1.10 #endif
467     #ifdef NETBSD
468 pajs 1.11 {
469     struct uvmexp *uvm = get_uvmexp();
470     if (uvm == NULL) return 1;
471     }
472 pajs 1.14 #endif
473     #ifdef SOLARIS
474 pajs 1.20 /* 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 pajs 1.26 #ifdef HAVE_LIBDEVINFO_H
479 pajs 1.20 build_mapping();
480 pajs 1.25 #endif
481 pajs 1.10 #endif
482     return 0;
483     }
484 ats 1.18
485     int statgrab_drop_privileges() {
486     if (setegid(getgid()) != 0) return -1;
487     if (seteuid(getuid()) != 0) return -1;
488     return 0;
489     }
490