ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
Revision: 1.54
Committed: Fri Jul 16 15:07:14 2004 UTC (19 years, 10 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.53: +2 -2 lines
Log Message:
Add function to allow grabbing of the current errno. Should we store errno
too maybe?

File Contents

# User Rev Content
1 tdb 1.23 /*
2 tdb 1.43 * i-scream libstatgrab
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.54 * $Id: tools.c,v 1.53 2004/05/30 17:07:53 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 ats 1.9 #ifdef ALLBSD
34 ats 1.8 #include <fcntl.h>
35 tdb 1.29 #endif
36 tdb 1.34 #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
37 ats 1.8 #include <kvm.h>
38 tdb 1.35 #include <paths.h>
39 ats 1.8 #endif
40 ats 1.28 #if defined(NETBSD) || defined(OPENBSD)
41 ats 1.9 #include <uvm/uvm_extern.h>
42 ats 1.28 #include <sys/param.h>
43     #include <sys/sysctl.h>
44 ats 1.9 #endif
45 pajs 1.1
46 ats 1.7 #include "tools.h"
47 tdb 1.46 #include "statgrab.h"
48 ats 1.7
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.41 const char *sg_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 ats 1.41 static void add_mapping(char *bsd, char *svr){
89 pajs 1.14 mapping_t *map_ptr;
90     mapping_t *map_end_ptr;
91    
92     if (mapping == NULL){
93 ats 1.48 mapping = sg_malloc(sizeof(mapping_t));
94 pajs 1.14 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 ats 1.48 map_end_ptr->next = sg_malloc(sizeof(mapping_t));
109 pajs 1.14 if (map_end_ptr->next == NULL) return;
110     map_ptr = map_end_ptr->next;
111     }
112    
113     map_ptr->next = NULL;
114 ats 1.47 map_ptr->bsd = NULL;
115     map_ptr->svr = NULL;
116     if (sg_update_string(&map_ptr->bsd, bsd) < 0
117     || sg_update_string(&map_ptr->svr, svr) < 0) {
118     return;
119     }
120 pajs 1.14
121     return;
122     }
123    
124 ats 1.41
125     static char *read_dir(char *disk_path){
126 pajs 1.14 DIR *dirp;
127 tdb 1.15 struct dirent *dp;
128     struct stat stbuf;
129 ats 1.47 char *svr_name = NULL;
130 tdb 1.15 char current_dir[MAXPATHLEN];
131     char file_name[MAXPATHLEN];
132     char temp_name[MAXPATHLEN];
133     char dir_dname[MAXPATHLEN];
134 pajs 1.14 char *dsk_dir;
135     int x;
136    
137     dsk_dir = "/dev/osa/dev/dsk";
138 tdb 1.15 strncpy(current_dir, dsk_dir, sizeof current_dir);
139     if ((dirp = opendir(current_dir)) == NULL){
140 pajs 1.14 dsk_dir = "/dev/dsk";
141     snprintf(current_dir, sizeof current_dir, "%s", dsk_dir);
142     if ((dirp = opendir(current_dir)) == NULL){
143     return NULL;
144     }
145     }
146    
147 tdb 1.15 while ((dp = readdir(dirp)) != NULL){
148 pajs 1.14 snprintf(temp_name, sizeof temp_name, "../..%s", disk_path);
149     snprintf(dir_dname, sizeof dir_dname, "%s/%s", dsk_dir, dp->d_name);
150     stat(dir_dname,&stbuf);
151    
152     if (S_ISBLK(stbuf.st_mode)){
153     x = readlink(dir_dname, file_name, sizeof(file_name));
154     file_name[x] = '\0';
155     if (strcmp(file_name, temp_name) == 0) {
156 ats 1.47 if (sg_update_string(&svr_name,
157 tdb 1.49 dp->d_name) < 0) {
158 ats 1.47 return NULL;
159     }
160 pajs 1.14 closedir(dirp);
161     return svr_name;
162     }
163     }
164     }
165 tdb 1.16 closedir(dirp);
166 pajs 1.14 return NULL;
167     }
168    
169    
170 ats 1.41 static int get_alias(char *alias){
171 pajs 1.14 char file[MAXPATHLEN];
172     di_node_t root_node;
173     di_node_t node;
174     di_minor_t minor = DI_MINOR_NIL;
175     char tmpnode[MAXPATHLEN];
176     char *phys_path;
177     char *minor_name;
178     char *value;
179     int instance;
180     if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
181 ats 1.45 return -1;
182 pajs 1.14 }
183     node = di_drv_first_node(alias, root_node);
184     while (node != DI_NODE_NIL) {
185     if ((minor = di_minor_next(node, DI_MINOR_NIL)) != DI_MINOR_NIL) {
186     instance = di_instance(node);
187     phys_path = di_devfs_path(node);
188     minor_name = di_minor_name(minor);
189     strcpy(tmpnode, alias);
190     sprintf(tmpnode, "%s%d", tmpnode, instance);
191 ats 1.41 sg_strlcpy(file, "/devices", sizeof file);
192     sg_strlcat(file, phys_path, sizeof file);
193     sg_strlcat(file, ":", sizeof file);
194     sg_strlcat(file, minor_name, sizeof file);
195 pajs 1.14 value = read_dir(file);
196     if (value != NULL){
197     add_mapping(tmpnode, value);
198     }
199     di_devfs_path_free(phys_path);
200     node = di_drv_next_node(node);
201     }else{
202     node = di_drv_next_node(node);
203     }
204 tdb 1.15 }
205 pajs 1.14 di_fini(root_node);
206 pajs 1.17 return 0;
207 pajs 1.14 }
208    
209 pajs 1.22
210     #define BIG_ENOUGH 512
211 ats 1.41 static int build_mapping(){
212 pajs 1.22 char device_name[BIG_ENOUGH];
213 pajs 1.14 int x;
214 tdb 1.15 kstat_ctl_t *kc;
215     kstat_t *ksp;
216     kstat_io_t kios;
217 pajs 1.14
218 pajs 1.22 char driver_list[BIG_ENOUGH][BIG_ENOUGH];
219     int list_entries = 0;
220     int found;
221    
222 pajs 1.14 if ((kc = kstat_open()) == NULL) {
223 ats 1.45 return -1;
224 tdb 1.15 }
225 pajs 1.14
226 tdb 1.15 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
227     if (!strcmp(ksp->ks_class, "disk")) {
228     if(ksp->ks_type != KSTAT_TYPE_IO) continue;
229     /* We dont want metadevices appearing as num_diskio */
230     if(strcmp(ksp->ks_module, "md")==0) continue;
231     if((kstat_read(kc, ksp, &kios))==-1) continue;
232 pajs 1.14 strncpy(device_name, ksp->ks_name, sizeof device_name);
233     for(x=0;x<(sizeof device_name);x++){
234     if( isdigit((int)device_name[x]) ) break;
235     }
236     if(x == sizeof device_name) x--;
237     device_name[x] = '\0';
238 pajs 1.22
239     /* Check if we've not already looked it up */
240     found = 0;
241     for(x=0;x<list_entries;x++){
242     if (x>=BIG_ENOUGH){
243     /* We've got bigger than we thought was massive */
244     /* If we hit this.. Make big enough bigger */
245 ats 1.45 return -1;
246 pajs 1.22 }
247     if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){
248     found = 1;
249     break;
250     }
251     }
252    
253     if(!found){
254     if((get_alias(device_name)) != 0){
255 ats 1.45 return -1;
256 pajs 1.22 }
257     strncpy(driver_list[x], device_name, BIG_ENOUGH);
258     list_entries++;
259 pajs 1.17 }
260 tdb 1.15 }
261 pajs 1.14 }
262 tdb 1.15
263 pajs 1.17 return 0;
264 pajs 1.14 }
265    
266     #endif
267    
268 tdb 1.52 #if defined(LINUX) || defined(CYGWIN)
269 ats 1.41 char *sg_f_read_line(FILE *f, const char *string){
270 pajs 1.1 /* Max line length. 8k should be more than enough */
271     static char line[8192];
272    
273 pajs 1.2 while((fgets(line, sizeof(line), f))!=NULL){
274 pajs 1.1 if(strncmp(string, line, strlen(string))==0){
275     return line;
276     }
277     }
278    
279 tdb 1.51 sg_set_error(SG_ERROR_PARSE, NULL);
280 pajs 1.1 return NULL;
281 pajs 1.3 }
282    
283 ats 1.41 char *sg_get_string_match(char *line, regmatch_t *match){
284 pajs 1.3 int len=match->rm_eo - match->rm_so;
285 ats 1.48 char *match_string=sg_malloc(len+1);
286 pajs 1.3
287     match_string=strncpy(match_string, line+match->rm_so, len);
288     match_string[len]='\0';
289    
290     return match_string;
291     }
292 ats 1.7
293 tdb 1.52 long long sg_get_ll_match(char *line, regmatch_t *match){
294     char *ptr;
295     long long num;
296    
297     ptr=line+match->rm_so;
298     num=atoll(ptr);
299    
300     return num;
301     }
302     #endif
303 pajs 1.14
304 ats 1.13 #ifndef HAVE_ATOLL
305     static long long atoll(const char *s) {
306     long long value = 0;
307     int isneg = 0;
308    
309     while (*s == ' ' || *s == '\t') {
310     s++;
311     }
312     if (*s == '-') {
313     isneg = 1;
314     s++;
315     }
316     while (*s >= '0' && *s <= '9') {
317     value = (10 * value) + (*s - '0');
318     s++;
319     }
320     return (isneg ? -value : value);
321     }
322     #endif
323    
324 ats 1.21 /* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */
325 pajs 1.19
326     /*
327     * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
328     *
329     * Permission to use, copy, modify, and distribute this software for any
330     * purpose with or without fee is hereby granted, provided that the above
331     * copyright notice and this permission notice appear in all copies.
332     *
333     * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
334     * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
335     * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
336     * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
337     * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
338     * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
339     * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
340     */
341    
342     /*
343     * Copy src to string dst of size siz. At most siz-1 characters
344     * will be copied. Always NUL terminates (unless siz == 0).
345     * Returns strlen(src); if retval >= siz, truncation occurred.
346     */
347 ats 1.41 size_t sg_strlcpy(char *dst, const char *src, size_t siz){
348 tdb 1.49 register char *d = dst;
349     register const char *s = src;
350     register size_t n = siz;
351    
352     /* Copy as many bytes as will fit */
353     if (n != 0 && --n != 0) {
354     do {
355     if ((*d++ = *s++) == 0)
356     break;
357     } while (--n != 0);
358     }
359    
360     /* Not enough room in dst, add NUL and traverse rest of src */
361     if (n == 0) {
362     if (siz != 0)
363     *d = '\0'; /* NUL-terminate dst */
364     while (*s++)
365     ;
366     }
367 pajs 1.19
368 tdb 1.49 return(s - src - 1); /* count does not include NUL */
369 pajs 1.19 }
370    
371     /* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */
372    
373     /*
374     * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
375     *
376     * Permission to use, copy, modify, and distribute this software for any
377     * purpose with or without fee is hereby granted, provided that the above
378     * copyright notice and this permission notice appear in all copies.
379     *
380     * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
381     * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
382     * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
383     * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
384     * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
385     * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
386     * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
387     */
388    
389     /*
390     * Appends src to string dst of size siz (unlike strncat, siz is the
391     * full size of dst, not space left). At most siz-1 characters
392     * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
393     * Returns strlen(src) + MIN(siz, strlen(initial dst)).
394     * If retval >= siz, truncation occurred.
395     */
396 ats 1.41 size_t sg_strlcat(char *dst, const char *src, size_t siz){
397 tdb 1.49 register char *d = dst;
398     register const char *s = src;
399     register size_t n = siz;
400     size_t dlen;
401    
402     /* Find the end of dst and adjust bytes left but don't go past end */
403     while (n-- != 0 && *d != '\0')
404     d++;
405     dlen = d - dst;
406     n = siz - dlen;
407    
408     if (n == 0)
409     return(dlen + strlen(s));
410     while (*s != '\0') {
411     if (n != 1) {
412     *d++ = *s;
413     n--;
414     }
415     s++;
416     }
417     *d = '\0';
418 pajs 1.19
419 tdb 1.49 return(dlen + (s - src)); /* count does not include NUL */
420 pajs 1.19 }
421    
422 ats 1.42 int sg_update_string(char **dest, const char *src) {
423 ats 1.37 char *new;
424    
425 ats 1.42 if (src == NULL) {
426     /* We're being told to set it to NULL. */
427     free(*dest);
428     *dest = NULL;
429     return 0;
430     }
431    
432 ats 1.48 new = sg_realloc(*dest, strlen(src) + 1);
433 ats 1.37 if (new == NULL) {
434 ats 1.42 return -1;
435 ats 1.37 }
436    
437     strcpy(new, src);
438     *dest = new;
439 ats 1.42 return 0;
440 ats 1.8 }
441    
442 tdb 1.34 #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
443 ats 1.41 kvm_t *sg_get_kvm() {
444 ats 1.8 static kvm_t *kvmd = NULL;
445    
446     if (kvmd != NULL) {
447     return kvmd;
448     }
449    
450     kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
451 tdb 1.51 if(kvmd == NULL) {
452     sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
453     }
454 ats 1.8 return kvmd;
455 pajs 1.1 }
456 tdb 1.34
457     /* Can't think of a better name for this function */
458 ats 1.41 kvm_t *sg_get_kvm2() {
459 tdb 1.34 static kvm_t *kvmd2 = NULL;
460    
461     if (kvmd2 != NULL) {
462     return kvmd2;
463     }
464    
465     kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
466 tdb 1.51 if(kvmd2 == NULL) {
467     sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
468     }
469 tdb 1.34 return kvmd2;
470     }
471 pajs 1.4 #endif
472 ats 1.9
473 ats 1.28 #if defined(NETBSD) || defined(OPENBSD)
474 ats 1.41 struct uvmexp *sg_get_uvmexp() {
475 ats 1.28 int mib[2];
476 ats 1.44 size_t size = sizeof(struct uvmexp);
477     static struct uvmexp uvm;
478 ats 1.36 struct uvmexp *new;
479 ats 1.9
480 ats 1.28 mib[0] = CTL_VM;
481     mib[1] = VM_UVMEXP;
482    
483 ats 1.44 if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) {
484 tdb 1.51 sg_set_error(SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP");
485 ats 1.9 return NULL;
486     }
487    
488 ats 1.44 return &uvm;
489 ats 1.9 }
490     #endif
491    
492 ats 1.41 int sg_init(){
493 tdb 1.54 sg_set_error_with_errno(SG_ERROR_NONE, NULL, 0);
494 ats 1.53
495 tdb 1.33 #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
496 ats 1.41 if (sg_get_kvm() == NULL) {
497 ats 1.45 return -1;
498 ats 1.41 }
499     if (sg_get_kvm2() == NULL) {
500 ats 1.45 return -1;
501 ats 1.32 }
502 pajs 1.14 #endif
503     #ifdef SOLARIS
504 pajs 1.20 /* On solaris 7, this will fail if you are not root. But, everything
505     * will still work, just no disk mappings. So we will ignore the exit
506     * status of this, and carry on merrily.
507     */
508 pajs 1.26 #ifdef HAVE_LIBDEVINFO_H
509 pajs 1.20 build_mapping();
510 pajs 1.25 #endif
511 pajs 1.10 #endif
512     return 0;
513     }
514 ats 1.18
515 ats 1.41 int sg_drop_privileges() {
516 tdb 1.51 if (setegid(getgid()) != 0) {
517     sg_set_error(SG_ERROR_SETEGID, NULL);
518     return -1;
519     }
520     if (seteuid(getuid()) != 0) {
521     sg_set_error(SG_ERROR_SETEUID, NULL);
522     return -1;
523     }
524 ats 1.18 return 0;
525 tdb 1.46 }
526    
527     void *sg_realloc(void *ptr, size_t size) {
528     void *tmp = NULL;
529     tmp = realloc(ptr, size);
530     if(tmp == NULL) {
531 pajs 1.50 sg_set_error(SG_ERROR_MALLOC, NULL);
532 tdb 1.46 }
533     return tmp;
534 ats 1.18 }
535