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.50 by pajs, Wed Apr 7 15:46:34 2004 UTC vs.
Revision 1.63 by tdb, Sat Sep 24 13:29:23 2005 UTC

# Line 30 | Line 30
30   #include <stdlib.h>
31   #include <unistd.h>
32   #include <sys/types.h>
33 #include <regex.h>
33   #ifdef ALLBSD
34   #include <fcntl.h>
35   #endif
# Line 43 | Line 42
42   #include <sys/param.h>
43   #include <sys/sysctl.h>
44   #endif
45 + #ifdef HPUX
46 + #include <sys/param.h>
47 + #include <sys/pstat.h>
48 + #endif
49  
50   #include "tools.h"
51   #include "statgrab.h"
# Line 61 | Line 64
64   #include <sys/fcntl.h>
65   #include <dirent.h>
66   #endif
67 + #ifdef WIN32
68 + #include "win32.h"
69 + #endif
70  
71   #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
72   struct map{
# Line 231 | Line 237 | static int build_mapping(){
237                          if(strcmp(ksp->ks_module, "md")==0) continue;
238                          if((kstat_read(kc, ksp, &kios))==-1) continue;
239                          strncpy(device_name, ksp->ks_name, sizeof device_name);
240 <                        for(x=0;x<(sizeof device_name);x++){
240 >                        for(x=0;x<(int)(sizeof device_name);x++){
241                                  if( isdigit((int)device_name[x]) ) break;
242                          }
243                          if(x == sizeof device_name) x--;
# Line 266 | Line 272 | static int build_mapping(){
272  
273   #endif
274  
275 <
270 <
275 > #if defined(LINUX) || defined(CYGWIN)
276   char *sg_f_read_line(FILE *f, const char *string){
277          /* Max line length. 8k should be more than enough */
278          static char line[8192];
# Line 278 | Line 283 | char *sg_f_read_line(FILE *f, const char *string){
283                  }
284          }
285  
286 +        sg_set_error(SG_ERROR_PARSE, NULL);
287          return NULL;
288   }
289  
# Line 291 | Line 297 | char *sg_get_string_match(char *line, regmatch_t *matc
297          return match_string;
298   }
299  
300 <
295 <
300 > /* Cygwin (without a recent newlib) doesn't have atoll */
301   #ifndef HAVE_ATOLL
302   static long long atoll(const char *s) {
303          long long value = 0;
# Line 313 | Line 318 | static long long atoll(const char *s) {
318   }
319   #endif
320  
321 + long long sg_get_ll_match(char *line, regmatch_t *match){
322 +        char *ptr;
323 +        long long num;
324 +
325 +        ptr=line+match->rm_so;
326 +        num=atoll(ptr);
327 +
328 +        return num;
329 + }
330 + #endif
331 +
332   /*      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
333  
334   /*
# Line 431 | Line 447 | int sg_update_string(char **dest, const char *src) {
447          return 0;
448   }
449  
450 < long long sg_get_ll_match(char *line, regmatch_t *match){
451 <        char *ptr;
452 <        long long num;
450 > /* join two strings together */
451 > int sg_concat_string(char **dest, const char *src) {
452 >        char *new;
453 >        int len = strlen(*dest) + strlen(src) + 1;
454  
455 <        ptr=line+match->rm_so;
456 <        num=atoll(ptr);
455 >        new = sg_realloc(*dest, len);
456 >        if (new == NULL) {
457 >                return -1;
458 >        }
459  
460 <        return num;
460 >        *dest = new;
461 >        strcat(*dest, src);
462 >        return 0;
463   }
464  
465   #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
# Line 450 | Line 471 | kvm_t *sg_get_kvm() {
471          }
472  
473          kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
474 +        if(kvmd == NULL) {
475 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
476 +        }
477          return kvmd;
478   }
479  
# Line 462 | Line 486 | kvm_t *sg_get_kvm2() {
486          }
487  
488          kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
489 +        if(kvmd2 == NULL) {
490 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
491 +        }
492          return kvmd2;
493   }
494   #endif
# Line 477 | Line 504 | struct uvmexp *sg_get_uvmexp() {
504          mib[1] = VM_UVMEXP;
505  
506          if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) {
507 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP");
508                  return NULL;
509          }
510  
# Line 484 | Line 512 | struct uvmexp *sg_get_uvmexp() {
512   }
513   #endif
514  
515 + #ifdef HPUX
516 + struct pst_static *sg_get_pstat_static() {
517 +        static int got = 0;
518 +        static struct pst_static pst;
519 +
520 +        if (!got) {
521 +                if (pstat_getstatic(&pst, sizeof pst, 1, 0) == -1) {
522 +                        sg_set_error_with_errno(SG_ERROR_PSTAT,
523 +                                                "pstat_static");
524 +                        return NULL;
525 +                }
526 +                got = 1;
527 +        }
528 +        return &pst;
529 + }
530 + #endif
531 +
532   int sg_init(){
533 +        sg_set_error(SG_ERROR_NONE, NULL);
534 +
535   #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
536          if (sg_get_kvm() == NULL) {
537                  return -1;
# Line 502 | Line 549 | int sg_init(){
549          build_mapping();
550   #endif
551   #endif
552 + #ifdef WIN32
553 +        return sg_win32_start_capture();
554 + #endif
555          return 0;
556   }
557  
558 + int sg_shutdown() {
559 + #ifdef WIN32
560 +        sg_win32_end_capture();
561 + #endif
562 +        return 0;
563 + }
564 +
565 + int sg_snapshot() {
566 + #ifdef WIN32
567 +        return sg_win32_snapshot();
568 + #else
569 +        return 0;
570 + #endif
571 + }
572 +
573   int sg_drop_privileges() {
574 <        if (setegid(getgid()) != 0) return -1;
575 <        if (seteuid(getuid()) != 0) return -1;
574 > #ifndef WIN32
575 > #ifdef HAVE_SETEGID
576 >        if (setegid(getgid()) != 0) {
577 > #elif defined(HAVE_SETRESGID)
578 >        if (setresgid(getgid(), getgid(), getgid()) != 0) {
579 > #else
580 >        {
581 > #endif
582 >                sg_set_error_with_errno(SG_ERROR_SETEGID, NULL);
583 >                return -1;
584 >        }
585 > #ifdef HAVE_SETEUID
586 >        if (seteuid(getuid()) != 0) {
587 > #elif defined(HAVE_SETRESUID)
588 >        if (setresuid(getuid(), getuid(), getuid()) != 0) {
589 > #else
590 >        {
591 > #endif
592 >                sg_set_error_with_errno(SG_ERROR_SETEUID, NULL);
593 >                return -1;
594 >        }
595 > #endif /* WIN32 */
596          return 0;
597   }
598  
# Line 515 | Line 600 | void *sg_realloc(void *ptr, size_t size) {
600          void *tmp = NULL;
601          tmp = realloc(ptr, size);
602          if(tmp == NULL) {
603 <                sg_set_error(SG_ERROR_MALLOC, NULL);
603 >                sg_set_error_with_errno(SG_ERROR_MALLOC, NULL);
604          }
605          return tmp;
606   }
522

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines