ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
Revision: 1.42
Committed: Mon Apr 5 18:10:11 2004 UTC (20 years, 1 month ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.41: +11 -4 lines
Log Message:
Make sg_update_string return an int to indicate success, so that it can
be used to set a string to NULL.

Make Linux process_stats code set proctitle to NULL if it's empty rather
than "".

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.42 * $Id: tools.c,v 1.41 2004/04/05 15:40:15 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.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     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 ats 1.40 map_ptr->bsd = strdup(bsd);
115     map_ptr->svr = strdup(svr);
116 pajs 1.14
117     return;
118     }
119    
120 ats 1.41
121     static char *read_dir(char *disk_path){
122 pajs 1.14 DIR *dirp;
123 tdb 1.15 struct dirent *dp;
124     struct stat stbuf;
125 pajs 1.14 char *svr_name;
126 tdb 1.15 char current_dir[MAXPATHLEN];
127     char file_name[MAXPATHLEN];
128     char temp_name[MAXPATHLEN];
129     char dir_dname[MAXPATHLEN];
130 pajs 1.14 char *dsk_dir;
131     int x;
132    
133     dsk_dir = "/dev/osa/dev/dsk";
134 tdb 1.15 strncpy(current_dir, dsk_dir, sizeof current_dir);
135     if ((dirp = opendir(current_dir)) == NULL){
136 pajs 1.14 dsk_dir = "/dev/dsk";
137     snprintf(current_dir, sizeof current_dir, "%s", dsk_dir);
138     if ((dirp = opendir(current_dir)) == NULL){
139     return NULL;
140     }
141     }
142    
143 tdb 1.15 while ((dp = readdir(dirp)) != NULL){
144 pajs 1.14 snprintf(temp_name, sizeof temp_name, "../..%s", disk_path);
145     snprintf(dir_dname, sizeof dir_dname, "%s/%s", dsk_dir, dp->d_name);
146     stat(dir_dname,&stbuf);
147    
148     if (S_ISBLK(stbuf.st_mode)){
149     x = readlink(dir_dname, file_name, sizeof(file_name));
150     file_name[x] = '\0';
151     if (strcmp(file_name, temp_name) == 0) {
152     svr_name = strdup(dp->d_name);
153     closedir(dirp);
154     return svr_name;
155     }
156     }
157     }
158 tdb 1.16 closedir(dirp);
159 pajs 1.14 return NULL;
160     }
161    
162    
163 ats 1.41 static int get_alias(char *alias){
164 pajs 1.14 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 ats 1.41 sg_strlcpy(file, "/devices", sizeof file);
185     sg_strlcat(file, phys_path, sizeof file);
186     sg_strlcat(file, ":", sizeof file);
187     sg_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 ats 1.41 static 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.39 return 1;
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 ats 1.41 char *sg_f_read_line(FILE *f, const char *string){
264 pajs 1.1 /* 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 ats 1.41 char *sg_get_string_match(char *line, regmatch_t *match){
277 pajs 1.3 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 ats 1.21 /* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */
309 pajs 1.19
310     /*
311     * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
312     *
313     * Permission to use, copy, modify, and distribute this software for any
314     * purpose with or without fee is hereby granted, provided that the above
315     * copyright notice and this permission notice appear in all copies.
316     *
317     * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
318     * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
319     * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
320     * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
321     * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
322     * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
323     * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
324     */
325    
326     /*
327     * Copy src to string dst of size siz. At most siz-1 characters
328     * will be copied. Always NUL terminates (unless siz == 0).
329     * Returns strlen(src); if retval >= siz, truncation occurred.
330     */
331 ats 1.41 size_t sg_strlcpy(char *dst, const char *src, size_t siz){
332 pajs 1.19 register char *d = dst;
333     register const char *s = src;
334     register size_t n = siz;
335    
336     /* Copy as many bytes as will fit */
337     if (n != 0 && --n != 0) {
338     do {
339     if ((*d++ = *s++) == 0)
340     break;
341     } while (--n != 0);
342     }
343    
344     /* Not enough room in dst, add NUL and traverse rest of src */
345     if (n == 0) {
346     if (siz != 0)
347     *d = '\0'; /* NUL-terminate dst */
348     while (*s++)
349     ;
350     }
351    
352     return(s - src - 1); /* count does not include NUL */
353     }
354    
355     /* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */
356    
357     /*
358     * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
359     *
360     * Permission to use, copy, modify, and distribute this software for any
361     * purpose with or without fee is hereby granted, provided that the above
362     * copyright notice and this permission notice appear in all copies.
363     *
364     * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
365     * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
366     * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
367     * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
368     * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
369     * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
370     * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
371     */
372    
373     /*
374     * Appends src to string dst of size siz (unlike strncat, siz is the
375     * full size of dst, not space left). At most siz-1 characters
376     * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
377     * Returns strlen(src) + MIN(siz, strlen(initial dst)).
378     * If retval >= siz, truncation occurred.
379     */
380 ats 1.41 size_t sg_strlcat(char *dst, const char *src, size_t siz){
381 pajs 1.19 register char *d = dst;
382     register const char *s = src;
383     register size_t n = siz;
384     size_t dlen;
385    
386     /* Find the end of dst and adjust bytes left but don't go past end */
387     while (n-- != 0 && *d != '\0')
388     d++;
389     dlen = d - dst;
390     n = siz - dlen;
391    
392     if (n == 0)
393     return(dlen + strlen(s));
394     while (*s != '\0') {
395     if (n != 1) {
396     *d++ = *s;
397     n--;
398     }
399     s++;
400     }
401     *d = '\0';
402    
403     return(dlen + (s - src)); /* count does not include NUL */
404     }
405    
406 ats 1.42 int sg_update_string(char **dest, const char *src) {
407 ats 1.37 char *new;
408    
409 ats 1.42 if (src == NULL) {
410     /* We're being told to set it to NULL. */
411     free(*dest);
412     *dest = NULL;
413     return 0;
414     }
415    
416 ats 1.37 new = realloc(*dest, strlen(src) + 1);
417     if (new == NULL) {
418 ats 1.42 return -1;
419 ats 1.37 }
420    
421     strcpy(new, src);
422     *dest = new;
423 ats 1.42 return 0;
424 ats 1.37 }
425 pajs 1.19
426 ats 1.41 long long sg_get_ll_match(char *line, regmatch_t *match){
427 pajs 1.3 char *ptr;
428     long long num;
429    
430     ptr=line+match->rm_so;
431     num=atoll(ptr);
432    
433     return num;
434 ats 1.8 }
435    
436 tdb 1.34 #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
437 ats 1.41 kvm_t *sg_get_kvm() {
438 ats 1.8 static kvm_t *kvmd = NULL;
439    
440     if (kvmd != NULL) {
441     return kvmd;
442     }
443    
444     kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
445     return kvmd;
446 pajs 1.1 }
447 tdb 1.34
448     /* Can't think of a better name for this function */
449 ats 1.41 kvm_t *sg_get_kvm2() {
450 tdb 1.34 static kvm_t *kvmd2 = NULL;
451    
452     if (kvmd2 != NULL) {
453     return kvmd2;
454     }
455    
456     kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
457     return kvmd2;
458     }
459 pajs 1.4 #endif
460 ats 1.9
461 ats 1.28 #if defined(NETBSD) || defined(OPENBSD)
462 ats 1.41 struct uvmexp *sg_get_uvmexp() {
463 ats 1.28 int mib[2];
464     size_t size;
465     static struct uvmexp *uvm = NULL;
466 ats 1.36 struct uvmexp *new;
467 ats 1.9
468 ats 1.28 mib[0] = CTL_VM;
469     mib[1] = VM_UVMEXP;
470    
471     if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) {
472 ats 1.9 return NULL;
473     }
474    
475 ats 1.36 new = realloc(uvm, size);
476     if (new == NULL) {
477 ats 1.28 return NULL;
478 ats 1.9 }
479 ats 1.36 uvm = new;
480 ats 1.9
481 ats 1.28 if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) {
482 ats 1.9 return NULL;
483     }
484 ats 1.28
485     return uvm;
486 ats 1.9 }
487     #endif
488    
489 ats 1.41 int sg_init(){
490 tdb 1.33 #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
491 ats 1.41 if (sg_get_kvm() == NULL) {
492     return 1;
493     }
494     if (sg_get_kvm2() == NULL) {
495     return 1;
496 ats 1.32 }
497     #endif
498     #if defined(NETBSD) || defined(OPENBSD)
499 ats 1.41 /* This should always succeed, but it seems that on some
500     * versions of NetBSD the first call to get_uvmexp will return
501     * a non-filled-in structure; this is a workaround for that.
502     */
503     if (sg_get_uvmexp() == NULL) return 1;
504 pajs 1.14 #endif
505     #ifdef SOLARIS
506 pajs 1.20 /* On solaris 7, this will fail if you are not root. But, everything
507     * will still work, just no disk mappings. So we will ignore the exit
508     * status of this, and carry on merrily.
509     */
510 pajs 1.26 #ifdef HAVE_LIBDEVINFO_H
511 pajs 1.20 build_mapping();
512 pajs 1.25 #endif
513 pajs 1.10 #endif
514     return 0;
515     }
516 ats 1.18
517 ats 1.41 int sg_drop_privileges() {
518 ats 1.18 if (setegid(getgid()) != 0) return -1;
519     if (seteuid(getuid()) != 0) return -1;
520     return 0;
521     }
522