ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/os_info.c
Revision: 1.20
Committed: Mon Nov 1 18:30:17 2004 UTC (19 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.19: +24 -2 lines
Log Message:
Merge in patch to provide support for HP-UX 11.11.

Contributed by Roy Keene - thanks Roy!

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 tdb 1.20 * $Id: os_info.c,v 1.19 2004/07/18 21:30:11 ats 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 tdb 1.20 #ifdef HPUX
50     #include <sys/param.h>
51     #include <sys/pstat.h>
52     #include <time.h>
53     #endif
54 pajs 1.1
55 ats 1.15 sg_host_info *sg_get_host_info(){
56 pajs 1.1
57 tdb 1.20 static sg_host_info general_stat;
58 pajs 1.4 static struct utsname os;
59 pajs 1.1
60 tdb 1.20 #ifdef HPUX
61     struct pst_static pstat_static;
62     time_t currtime;
63     long boottime;
64     #endif
65 pajs 1.4 #ifdef SOLARIS
66 pajs 1.1 time_t boottime,curtime;
67     kstat_ctl_t *kc;
68     kstat_t *ksp;
69     kstat_named_t *kn;
70 pajs 1.4 #endif
71 ats 1.11 #if defined(LINUX) || defined(CYGWIN)
72 pajs 1.4 FILE *f;
73     #endif
74 ats 1.9 #ifdef ALLBSD
75     int mib[2];
76 pajs 1.5 struct timeval boottime;
77     time_t curtime;
78     size_t size;
79     #endif
80 pajs 1.1
81     if((uname(&os)) < 0){
82 ats 1.19 sg_set_error_with_errno(SG_ERROR_UNAME, NULL);
83 pajs 1.1 return NULL;
84     }
85    
86     general_stat.os_name = os.sysname;
87 tdb 1.17 general_stat.os_release = os.release;
88     general_stat.os_version = os.version;
89     general_stat.platform = os.machine;
90     general_stat.hostname = os.nodename;
91 pajs 1.1
92     /* get uptime */
93 tdb 1.20 #ifdef HPUX
94     if (pstat_getstatic(&pstat_static, sizeof(pstat_static), 1, 0) == -1) {
95     sg_set_error_with_errno(SG_ERROR_PSTAT, "pstat_static");
96     return NULL;
97     }
98    
99     currtime = time(NULL);
100    
101     boottime = pstat_static.boot_time;
102    
103     general_stat.uptime = currtime - boottime;
104     #endif
105 pajs 1.4 #ifdef SOLARIS
106 pajs 1.1 if ((kc = kstat_open()) == NULL) {
107 tdb 1.18 sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
108 pajs 1.1 return NULL;
109     }
110     if((ksp=kstat_lookup(kc, "unix", -1, "system_misc"))==NULL){
111 tdb 1.18 sg_set_error(SG_ERROR_KSTAT_LOOKUP, "unix,-1,system_misc");
112 pajs 1.1 return NULL;
113     }
114     if (kstat_read(kc, ksp, 0) == -1) {
115 tdb 1.18 sg_set_error(SG_ERROR_KSTAT_READ, NULL);
116 pajs 1.1 return NULL;
117     }
118     if((kn=kstat_data_lookup(ksp, "boot_time")) == NULL){
119 tdb 1.18 sg_set_error(SG_ERROR_KSTAT_DATA_LOOKUP, "boot_time");
120 pajs 1.1 return NULL;
121     }
122     boottime=(kn->value.ui32);
123 pajs 1.2
124     kstat_close(kc);
125 pajs 1.1
126     time(&curtime);
127     general_stat.uptime = curtime - boottime;
128 pajs 1.4 #endif
129 ats 1.11 #if defined(LINUX) || defined(CYGWIN)
130 pajs 1.4 if ((f=fopen("/proc/uptime", "r")) == NULL) {
131 ats 1.19 sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/uptime");
132 pajs 1.4 return NULL;
133     }
134     if((fscanf(f,"%lu %*d",&general_stat.uptime)) != 1){
135 tdb 1.18 sg_set_error(SG_ERROR_PARSE, NULL);
136 pajs 1.4 return NULL;
137     }
138     fclose(f);
139 pajs 1.5 #endif
140 ats 1.9 #ifdef ALLBSD
141     mib[0] = CTL_KERN;
142     mib[1] = KERN_BOOTTIME;
143 ats 1.8 size = sizeof boottime;
144 ats 1.9 if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0){
145 ats 1.19 sg_set_error_with_errno(SG_ERROR_SYSCTL,
146     "CTL_KERN.KERN_BOOTTIME");
147 pajs 1.5 return NULL;
148     }
149     time(&curtime);
150     general_stat.uptime=curtime-boottime.tv_sec;
151 pajs 1.4 #endif
152 pajs 1.1
153     return &general_stat;
154    
155     }