ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
Revision: 1.39
Committed: Mon Apr 5 00:17:40 2004 UTC (20 years, 1 month ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.38: +2 -2 lines
Log Message:
Fix a couple of warnings on Solaris.

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 tdb 1.39 * $Id: tools.c,v 1.38 2004/04/05 00:01:48 ats 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 tdb 1.29 #endif
37 tdb 1.34 #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
38 ats 1.8 #include <kvm.h>
39 tdb 1.35 #include <paths.h>
40 ats 1.8 #endif
41 ats 1.28 #if defined(NETBSD) || defined(OPENBSD)
42 ats 1.9 #include <uvm/uvm_extern.h>
43 ats 1.28 #include <sys/param.h>
44     #include <sys/sysctl.h>
45 ats 1.9 #endif
46 pajs 1.1
47 ats 1.7 #include "tools.h"
48    
49 pajs 1.14 #ifdef SOLARIS
50 pajs 1.27 #ifdef HAVE_LIBDEVINFO_H
51 pajs 1.14 #include <libdevinfo.h>
52 pajs 1.27 #endif
53 pajs 1.14 #include <kstat.h>
54     #include <unistd.h>
55     #include <ctype.h>
56     #include <sys/types.h>
57     #include <sys/dkio.h>
58     #include <sys/stat.h>
59     #include <fcntl.h>
60     #include <sys/fcntl.h>
61     #include <dirent.h>
62     #endif
63    
64 pajs 1.26 #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
65 pajs 1.14 struct map{
66     char *bsd;
67     char *svr;
68    
69     struct map *next;
70     };
71     typedef struct map mapping_t;
72    
73     static mapping_t *mapping = NULL;
74 pajs 1.26 #endif
75 pajs 1.14
76 pajs 1.26 #ifdef SOLARIS
77 ats 1.38 const char *get_svr_from_bsd(const char *bsd){
78 pajs 1.26 #ifdef HAVE_LIBDEVINFO_H
79 pajs 1.14 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 pajs 1.26 #endif
83 pajs 1.14 return bsd;
84     }
85 pajs 1.26 #endif
86 pajs 1.14
87 pajs 1.26 #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
88 pajs 1.14 void add_mapping(char *bsd, char *svr){
89     mapping_t *map_ptr;
90     mapping_t *map_end_ptr;
91    
92     bsd = strdup(bsd);
93     svr = strdup(svr);
94    
95     if (mapping == NULL){
96     mapping = malloc(sizeof(mapping_t));
97     if (mapping == NULL) return;
98     map_ptr = mapping;
99     }else{
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)) ){
103     return;
104     }
105     map_end_ptr = map_ptr;
106     }
107    
108     /* We've reached end of list and not found the entry.. So we need to malloc
109     * new mapping_t
110     */
111     map_end_ptr->next = malloc(sizeof(mapping_t));
112     if (map_end_ptr->next == NULL) return;
113     map_ptr = map_end_ptr->next;
114     }
115    
116     map_ptr->next = NULL;
117     map_ptr->bsd = bsd;
118     map_ptr->svr = svr;
119    
120     return;
121     }
122    
123     char *read_dir(char *disk_path){
124     DIR *dirp;
125 tdb 1.15 struct dirent *dp;
126     struct stat stbuf;
127 pajs 1.14 char *svr_name;
128 tdb 1.15 char current_dir[MAXPATHLEN];
129     char file_name[MAXPATHLEN];
130     char temp_name[MAXPATHLEN];
131     char dir_dname[MAXPATHLEN];
132 pajs 1.14 char *dsk_dir;
133     int x;
134    
135     dsk_dir = "/dev/osa/dev/dsk";
136 tdb 1.15 strncpy(current_dir, dsk_dir, sizeof current_dir);
137     if ((dirp = opendir(current_dir)) == NULL){
138 pajs 1.14 dsk_dir = "/dev/dsk";
139     snprintf(current_dir, sizeof current_dir, "%s", dsk_dir);
140     if ((dirp = opendir(current_dir)) == NULL){
141     return NULL;
142     }
143     }
144    
145 tdb 1.15 while ((dp = readdir(dirp)) != NULL){
146 pajs 1.14 snprintf(temp_name, sizeof temp_name, "../..%s", disk_path);
147     snprintf(dir_dname, sizeof dir_dname, "%s/%s", dsk_dir, dp->d_name);
148     stat(dir_dname,&stbuf);
149    
150     if (S_ISBLK(stbuf.st_mode)){
151     x = readlink(dir_dname, file_name, sizeof(file_name));
152     file_name[x] = '\0';
153     if (strcmp(file_name, temp_name) == 0) {
154     svr_name = strdup(dp->d_name);
155     closedir(dirp);
156     return svr_name;
157     }
158     }
159     }
160 tdb 1.16 closedir(dirp);
161 pajs 1.14 return NULL;
162     }
163    
164    
165    
166     int get_alias(char *alias){
167     char file[MAXPATHLEN];
168     di_node_t root_node;
169     di_node_t node;
170     di_minor_t minor = DI_MINOR_NIL;
171     char tmpnode[MAXPATHLEN];
172     char *phys_path;
173     char *minor_name;
174     char *value;
175     int instance;
176     if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
177 pajs 1.17 return 1;
178 pajs 1.14 }
179     node = di_drv_first_node(alias, root_node);
180     while (node != DI_NODE_NIL) {
181     if ((minor = di_minor_next(node, DI_MINOR_NIL)) != DI_MINOR_NIL) {
182     instance = di_instance(node);
183     phys_path = di_devfs_path(node);
184     minor_name = di_minor_name(minor);
185     strcpy(tmpnode, alias);
186     sprintf(tmpnode, "%s%d", tmpnode, instance);
187 pajs 1.17 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 pajs 1.14 value = read_dir(file);
192     if (value != NULL){
193     add_mapping(tmpnode, value);
194     }
195     di_devfs_path_free(phys_path);
196     node = di_drv_next_node(node);
197     }else{
198     node = di_drv_next_node(node);
199     }
200 tdb 1.15 }
201 pajs 1.14 di_fini(root_node);
202 pajs 1.17 return 0;
203 pajs 1.14 }
204    
205 pajs 1.22
206     #define BIG_ENOUGH 512
207 pajs 1.17 int build_mapping(){
208 pajs 1.22 char device_name[BIG_ENOUGH];
209 pajs 1.14 int x;
210 tdb 1.15 kstat_ctl_t *kc;
211     kstat_t *ksp;
212     kstat_io_t kios;
213 pajs 1.14
214 pajs 1.22 char driver_list[BIG_ENOUGH][BIG_ENOUGH];
215     int list_entries = 0;
216     int found;
217    
218 pajs 1.14 if ((kc = kstat_open()) == NULL) {
219 tdb 1.39 return 1;
220 tdb 1.15 }
221 pajs 1.14
222 tdb 1.15 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
223     if (!strcmp(ksp->ks_class, "disk")) {
224     if(ksp->ks_type != KSTAT_TYPE_IO) continue;
225     /* We dont want metadevices appearing as num_diskio */
226     if(strcmp(ksp->ks_module, "md")==0) continue;
227     if((kstat_read(kc, ksp, &kios))==-1) continue;
228 pajs 1.14 strncpy(device_name, ksp->ks_name, sizeof device_name);
229     for(x=0;x<(sizeof device_name);x++){
230     if( isdigit((int)device_name[x]) ) break;
231     }
232     if(x == sizeof device_name) x--;
233     device_name[x] = '\0';
234 pajs 1.22
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 pajs 1.17 }
256 tdb 1.15 }
257 pajs 1.14 }
258 tdb 1.15
259 pajs 1.17 return 0;
260 pajs 1.14 }
261    
262     #endif
263    
264    
265    
266 pajs 1.1 char *f_read_line(FILE *f, const char *string){
267     /* Max line length. 8k should be more than enough */
268     static char line[8192];
269    
270 pajs 1.2 while((fgets(line, sizeof(line), f))!=NULL){
271 pajs 1.1 if(strncmp(string, line, strlen(string))==0){
272     return line;
273     }
274     }
275    
276     return NULL;
277 pajs 1.3 }
278    
279     char *get_string_match(char *line, regmatch_t *match){
280     int len=match->rm_eo - match->rm_so;
281     char *match_string=malloc(len+1);
282    
283     match_string=strncpy(match_string, line+match->rm_so, len);
284     match_string[len]='\0';
285    
286     return match_string;
287     }
288 ats 1.7
289 pajs 1.14
290    
291 ats 1.13 #ifndef HAVE_ATOLL
292     static long long atoll(const char *s) {
293     long long value = 0;
294     int isneg = 0;
295    
296     while (*s == ' ' || *s == '\t') {
297     s++;
298     }
299     if (*s == '-') {
300     isneg = 1;
301     s++;
302     }
303     while (*s >= '0' && *s <= '9') {
304     value = (10 * value) + (*s - '0');
305     s++;
306     }
307     return (isneg ? -value : value);
308     }
309     #endif
310    
311 pajs 1.19 #ifndef HAVE_STRLCPY
312 ats 1.21 /* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */
313 pajs 1.19
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 ats 1.37
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 pajs 1.19
427 pajs 1.3 long long get_ll_match(char *line, regmatch_t *match){
428     char *ptr;
429     long long num;
430    
431     ptr=line+match->rm_so;
432     num=atoll(ptr);
433    
434     return num;
435 ats 1.8 }
436    
437 tdb 1.34 #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
438 ats 1.8 kvm_t *get_kvm() {
439     static kvm_t *kvmd = NULL;
440    
441     if (kvmd != NULL) {
442     return kvmd;
443     }
444    
445     kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
446     return kvmd;
447 pajs 1.1 }
448 tdb 1.34
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 pajs 1.4 #endif
461 ats 1.9
462 ats 1.28 #if defined(NETBSD) || defined(OPENBSD)
463 ats 1.9 struct uvmexp *get_uvmexp() {
464 ats 1.28 int mib[2];
465     size_t size;
466     static struct uvmexp *uvm = NULL;
467 ats 1.36 struct uvmexp *new;
468 ats 1.9
469 ats 1.28 mib[0] = CTL_VM;
470     mib[1] = VM_UVMEXP;
471    
472     if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) {
473 ats 1.9 return NULL;
474     }
475    
476 ats 1.36 new = realloc(uvm, size);
477     if (new == NULL) {
478 ats 1.28 return NULL;
479 ats 1.9 }
480 ats 1.36 uvm = new;
481 ats 1.9
482 ats 1.28 if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) {
483 ats 1.9 return NULL;
484     }
485 ats 1.28
486     return uvm;
487 ats 1.9 }
488     #endif
489    
490 pajs 1.10 int statgrab_init(){
491 tdb 1.33 #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
492 pajs 1.11 {
493     kvm_t *kvmd = get_kvm();
494 tdb 1.15 if (kvmd == NULL) return 1;
495 tdb 1.34 }
496     {
497     kvm_t *kvmd2 = get_kvm2();
498     if (kvmd2 == NULL) return 1;
499 ats 1.32 }
500     #endif
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 pajs 1.11 }
510 pajs 1.14 #endif
511     #ifdef SOLARIS
512 pajs 1.20 /* 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 pajs 1.26 #ifdef HAVE_LIBDEVINFO_H
517 pajs 1.20 build_mapping();
518 pajs 1.25 #endif
519 pajs 1.10 #endif
520     return 0;
521     }
522 ats 1.18
523     int statgrab_drop_privileges() {
524     if (setegid(getgid()) != 0) return -1;
525     if (seteuid(getuid()) != 0) return -1;
526     return 0;
527     }
528