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.16 by tdb, Thu Nov 20 12:13:12 2003 UTC vs.
Revision 1.24 by tdb, Mon Jan 19 16:49:21 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
# Line 158 | Line 162 | int get_alias(char *alias){
162          char *value;
163          int instance;
164          if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
165 <                fprintf(stderr, "di_init() failed\n");
162 <                exit(1);
165 >                return 1;
166          }
167          node = di_drv_first_node(alias, root_node);
168          while (node != DI_NODE_NIL) {
# Line 169 | Line 172 | int get_alias(char *alias){
172                          minor_name = di_minor_name(minor);
173                          strcpy(tmpnode, alias);
174                          sprintf(tmpnode, "%s%d", tmpnode, instance);
175 <                        strcpy(file, "/devices");
176 <                        strcat(file, phys_path);
177 <                        strcat(file, ":");
178 <                        strcat(file, minor_name);
175 >                        strlcpy(file, "/devices", sizeof file);
176 >                        strlcat(file, phys_path, sizeof file);
177 >                        strlcat(file, ":", sizeof file);
178 >                        strlcat(file, minor_name, sizeof file);
179                          value = read_dir(file);
180                          if (value != NULL){
181                                  add_mapping(tmpnode, value);
# Line 184 | Line 187 | int get_alias(char *alias){
187                  }
188          }
189          di_fini(root_node);
190 <        return (-1);
190 >        return 0;
191   }
192  
193 < void build_mapping(){
194 <        char device_name[512];
193 >
194 > #define BIG_ENOUGH 512
195 > int build_mapping(){
196 >        char device_name[BIG_ENOUGH];
197          int x;
198          kstat_ctl_t *kc;
199          kstat_t *ksp;
200          kstat_io_t kios;
201  
202 +        char driver_list[BIG_ENOUGH][BIG_ENOUGH];
203 +        int list_entries = 0;
204 +        int found;
205 +
206          if ((kc = kstat_open()) == NULL) {
207                  return;
208          }
# Line 210 | Line 219 | void build_mapping(){
219                          }
220                          if(x == sizeof device_name) x--;
221                          device_name[x] = '\0';
222 <                        get_alias(device_name);
222 >
223 >                        /* Check if we've not already looked it up */
224 >                        found = 0;
225 >                        for(x=0;x<list_entries;x++){
226 >                                if (x>=BIG_ENOUGH){
227 >                                        /* We've got bigger than we thought was massive */
228 >                                        /* If we hit this.. Make big enough bigger */
229 >                                        return 1;
230 >                                }
231 >                                if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){
232 >                                        found = 1;
233 >                                        break;
234 >                                }
235 >                        }
236 >
237 >                        if(!found){
238 >                                if((get_alias(device_name)) != 0){
239 >                                        return 1;
240 >                                }
241 >                                strncpy(driver_list[x], device_name, BIG_ENOUGH);
242 >                                list_entries++;
243 >                        }
244                  }
245          }
246  
247 <        return;
247 >        return 0;
248   }
249  
250   #endif
# Line 266 | Line 296 | static long long atoll(const char *s) {
296   }
297   #endif
298  
299 + #ifndef HAVE_STRLCPY
300 + /*      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
301 +
302 + /*
303 + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
304 + *
305 + * Permission to use, copy, modify, and distribute this software for any
306 + * purpose with or without fee is hereby granted, provided that the above
307 + * copyright notice and this permission notice appear in all copies.
308 + *
309 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
310 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
311 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
312 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
313 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
314 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
315 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
316 + */
317 +
318 + /*
319 + * Copy src to string dst of size siz.  At most siz-1 characters
320 + * will be copied.  Always NUL terminates (unless siz == 0).
321 + * Returns strlen(src); if retval >= siz, truncation occurred.
322 + */
323 + size_t strlcpy(char *dst, const char *src, size_t siz){
324 +        register char *d = dst;
325 +        register const char *s = src;
326 +        register size_t n = siz;
327 +
328 +        /* Copy as many bytes as will fit */
329 +        if (n != 0 && --n != 0) {
330 +                do {
331 +                        if ((*d++ = *s++) == 0)
332 +                                break;
333 +                } while (--n != 0);
334 +        }
335 +
336 +        /* Not enough room in dst, add NUL and traverse rest of src */
337 +        if (n == 0) {
338 +                if (siz != 0)
339 +                        *d = '\0';              /* NUL-terminate dst */
340 +                while (*s++)
341 +                        ;
342 +        }
343 +
344 +        return(s - src - 1);    /* count does not include NUL */
345 + }
346 + #endif
347 +
348 + #ifndef HAVE_STRLCAT
349 + /*      $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $    */
350 +
351 + /*
352 + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
353 + *
354 + * Permission to use, copy, modify, and distribute this software for any
355 + * purpose with or without fee is hereby granted, provided that the above
356 + * copyright notice and this permission notice appear in all copies.
357 + *
358 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
359 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
360 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
361 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
362 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
363 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
364 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
365 + */
366 +
367 + /*
368 + * Appends src to string dst of size siz (unlike strncat, siz is the
369 + * full size of dst, not space left).  At most siz-1 characters
370 + * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
371 + * Returns strlen(src) + MIN(siz, strlen(initial dst)).
372 + * If retval >= siz, truncation occurred.
373 + */
374 + size_t strlcat(char *dst, const char *src, size_t siz){
375 +        register char *d = dst;
376 +        register const char *s = src;
377 +        register size_t n = siz;
378 +        size_t dlen;
379 +
380 +        /* Find the end of dst and adjust bytes left but don't go past end */
381 +        while (n-- != 0 && *d != '\0')
382 +                d++;
383 +        dlen = d - dst;
384 +        n = siz - dlen;
385 +
386 +        if (n == 0)
387 +                return(dlen + strlen(s));
388 +        while (*s != '\0') {
389 +                if (n != 1) {
390 +                        *d++ = *s;
391 +                        n--;
392 +                }
393 +                s++;
394 +        }
395 +        *d = '\0';
396 +
397 +        return(dlen + (s - src));       /* count does not include NUL */
398 + }
399 +
400 + #endif
401 +
402   long long get_ll_match(char *line, regmatch_t *match){
403          char *ptr;
404          long long num;
# Line 332 | Line 465 | int statgrab_init(){
465          }
466   #endif
467   #ifdef SOLARIS
468 +        /* On solaris 7, this will fail if you are not root. But, everything
469 +         * will still work, just no disk mappings. So we will ignore the exit
470 +         * status of this, and carry on merrily.
471 +         */
472          build_mapping();
473   #endif
474          return 0;
475   }
476 +
477 + int statgrab_drop_privileges() {
478 +        if (setegid(getgid()) != 0) return -1;
479 +        if (seteuid(getuid()) != 0) return -1;
480 +        return 0;
481 + }
482 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines