ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/os_info.c
Revision: 1.19
Committed: Sun Jul 18 21:30:11 2004 UTC (19 years, 10 months ago) by ats
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_10_3, LIBSTATGRAB_0_10_2, LIBSTATGRAB_0_10_1
Changes since 1.18: +5 -4 lines
Log Message:
Use sg_set_error_with_errno whenever errno's valid.
Change the one user of SG_ERROR_ENOENT to SG_ERROR_SYSCTL instead (since
that's what it should have been).

File Contents

# User Rev Content
1 tdb 1.12 /*
2 tdb 1.16 * i-scream libstatgrab
3 tdb 1.6 * http://www.i-scream.org
4 tdb 1.12 * Copyright (C) 2000-2004 i-scream
5 pajs 1.1 *
6 tdb 1.12 * 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 pajs 1.1 *
11 tdb 1.12 * This library is distributed in the hope that it will be useful,
12 pajs 1.1 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 tdb 1.12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * Lesser General Public License for more details.
15 pajs 1.1 *
16 tdb 1.12 * 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.13 *
21 ats 1.19 * $Id: os_info.c,v 1.18 2004/04/07 21:08:40 tdb Exp $
22 pajs 1.1 */
23    
24     #ifdef HAVE_CONFIG_H
25     #include "config.h"
26     #endif
27    
28 tdb 1.3 #include <sys/utsname.h>
29 pajs 1.1 #include "statgrab.h"
30 ats 1.9 #include <stdlib.h>
31 pajs 1.1 #ifdef SOLARIS
32     #include <kstat.h>
33     #include <time.h>
34     #endif
35 ats 1.11 #if defined(LINUX) || defined(CYGWIN)
36 pajs 1.4 #include <stdio.h>
37     #endif
38 ats 1.9 #ifdef ALLBSD
39 tdb 1.14 #if defined(FREEBSD) || defined(DFBSD)
40 pajs 1.5 #include <sys/types.h>
41     #include <sys/sysctl.h>
42 ats 1.9 #else
43     #include <sys/param.h>
44     #include <sys/sysctl.h>
45     #endif
46 pajs 1.5 #include <time.h>
47     #include <sys/time.h>
48     #endif
49 pajs 1.1
50 ats 1.15 sg_host_info *sg_get_host_info(){
51 pajs 1.1
52 ats 1.15 static sg_host_info general_stat;
53 pajs 1.4 static struct utsname os;
54 pajs 1.1
55 pajs 1.4 #ifdef SOLARIS
56 pajs 1.1 time_t boottime,curtime;
57     kstat_ctl_t *kc;
58     kstat_t *ksp;
59     kstat_named_t *kn;
60 pajs 1.4 #endif
61 ats 1.11 #if defined(LINUX) || defined(CYGWIN)
62 pajs 1.4 FILE *f;
63     #endif
64 ats 1.9 #ifdef ALLBSD
65     int mib[2];
66 pajs 1.5 struct timeval boottime;
67     time_t curtime;
68     size_t size;
69     #endif
70 pajs 1.1
71     if((uname(&os)) < 0){
72 ats 1.19 sg_set_error_with_errno(SG_ERROR_UNAME, NULL);
73 pajs 1.1 return NULL;
74     }
75    
76     general_stat.os_name = os.sysname;
77 tdb 1.17 general_stat.os_release = os.release;
78     general_stat.os_version = os.version;
79     general_stat.platform = os.machine;
80     general_stat.hostname = os.nodename;
81 pajs 1.1
82     /* get uptime */
83 pajs 1.4 #ifdef SOLARIS
84 pajs 1.1 if ((kc = kstat_open()) == NULL) {
85 tdb 1.18 sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
86 pajs 1.1 return NULL;
87     }
88     if((ksp=kstat_lookup(kc, "unix", -1, "system_misc"))==NULL){
89 tdb 1.18 sg_set_error(SG_ERROR_KSTAT_LOOKUP, "unix,-1,system_misc");
90 pajs 1.1 return NULL;
91     }
92     if (kstat_read(kc, ksp, 0) == -1) {
93 tdb 1.18 sg_set_error(SG_ERROR_KSTAT_READ, NULL);
94 pajs 1.1 return NULL;
95     }
96     if((kn=kstat_data_lookup(ksp, "boot_time")) == NULL){
97 tdb 1.18 sg_set_error(SG_ERROR_KSTAT_DATA_LOOKUP, "boot_time");
98 pajs 1.1 return NULL;
99     }
100     boottime=(kn->value.ui32);
101 pajs 1.2
102     kstat_close(kc);
103 pajs 1.1
104     time(&curtime);
105     general_stat.uptime = curtime - boottime;
106 pajs 1.4 #endif
107 ats 1.11 #if defined(LINUX) || defined(CYGWIN)
108 pajs 1.4 if ((f=fopen("/proc/uptime", "r")) == NULL) {
109 ats 1.19 sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/uptime");
110 pajs 1.4 return NULL;
111     }
112     if((fscanf(f,"%lu %*d",&general_stat.uptime)) != 1){
113 tdb 1.18 sg_set_error(SG_ERROR_PARSE, NULL);
114 pajs 1.4 return NULL;
115     }
116     fclose(f);
117 pajs 1.5 #endif
118 ats 1.9 #ifdef ALLBSD
119     mib[0] = CTL_KERN;
120     mib[1] = KERN_BOOTTIME;
121 ats 1.8 size = sizeof boottime;
122 ats 1.9 if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0){
123 ats 1.19 sg_set_error_with_errno(SG_ERROR_SYSCTL,
124     "CTL_KERN.KERN_BOOTTIME");
125 pajs 1.5 return NULL;
126     }
127     time(&curtime);
128     general_stat.uptime=curtime-boottime.tv_sec;
129 pajs 1.4 #endif
130 pajs 1.1
131     return &general_stat;
132    
133     }