-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplatform.c
More file actions
276 lines (223 loc) · 6.98 KB
/
Copy pathplatform.c
File metadata and controls
276 lines (223 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Gophernicus - Copyright (c) 2009-2014 Kim Holviala <kim@holviala.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ugopherserver.h"
/*
* Get OS name, version & architecture we're running on
*/
void platform(state *st)
{
#ifdef HAVE_UNAME
#if defined(_AIX) || defined(__linux) || defined(__APPLE__)
FILE *fp;
#endif
#if defined(__arm__) || defined(__mips__) || defined(__APPLE__)
char buf[BUFSIZE];
#endif
#ifdef __linux
struct stat file;
#endif
struct utsname name;
char sysname[64];
char release[64];
char machine[64];
char *c;
/* Fetch system information */
uname(&name);
strclear(sysname);
strclear(release);
strclear(machine);
/* AIX-specific */
#ifdef _AIX
/* Fix uname() results */
sstrlcpy(machine, "powerpc");
snprintf(release, sizeof(release), "%s.%s",
name.version,
name.release);
/* Get CPU type */
if ((fp = popen("/usr/sbin/getsystype -i", "r"))) {
fgets(machine, sizeof(machine), fp);
pclose(fp);
strreplace(machine, ' ', '_');
}
/* Get hardware name using shell uname */
if (!*st->server_description &&
(fp = popen("/usr/bin/uname -M", "r"))) {
fgets(st->server_description,
sizeof(st->server_description), fp);
pclose(fp);
strreplace(st->server_description, ',', ' ');
chomp(st->server_description);
}
#endif
/* Mac OS X, just like Unix but totally different... */
#ifdef __APPLE__
/* Hardcode OS name */
sstrlcpy(sysname, "MacOSX");
/* Get OS X version */
if ((fp = popen("/usr/bin/sw_vers -productVersion", "r"))) {
fgets(release, sizeof(release), fp);
pclose(fp);
}
/* Get hardware name */
if (!*st->server_description &&
(fp = popen("/usr/sbin/sysctl -n hw.model", "r"))) {
/* Read hardware name */
fgets(buf, sizeof(buf), fp);
pclose(fp);
/* Clones are gone now so we'll hardcode the manufacturer */
sstrlcpy(st->server_description, "Apple ");
sstrlcat(st->server_description, buf);
/* Remove hardware revision */
for (c = st->server_description; *c; c++)
if (*c >= '0' && *c <= '9') { *c = '\0'; break; }
}
#endif
/* Linux uname() just says Linux/2.6 - let's dig deeper... */
#ifdef __linux
/* Most Linux ARM/MIPS boards have hardware name in /proc/cpuinfo */
#if defined(__arm__) || defined(__mips__)
if (!*st->server_description && (fp = fopen("/proc/cpuinfo" , "r"))) {
while (fgets(buf, sizeof(buf), fp)) {
#ifdef __arm__
if ((c = strkey(buf, "Hardware"))) {
#else
if ((c = strkey(buf, "machine"))) {
#endif
sstrlcpy(st->server_description, c);
chomp(st->server_description);
break;
}
}
fclose(fp);
}
#endif
/* Identify RedHat */
if (!*sysname && (fp = fopen("/etc/redhat-release", "r"))) {
fgets(sysname, sizeof(sysname), fp);
fclose(fp);
if ((c = strstr(sysname, "release "))) sstrlcpy(release, c + 8);
if ((c = strchr(release, ' '))) *c = '\0';
if ((c = strchr(sysname, ' '))) *c = '\0';
if (strcmp(sysname, "Red") == MATCH) sstrlcpy(sysname, "RedHat");
}
/* Identify Slackware */
if (!*sysname && (fp = fopen("/etc/slackware-version", "r"))) {
fgets(sysname, sizeof(sysname), fp);
fclose(fp);
if ((c = strchr(sysname, ' '))) {
sstrlcpy(release, c + 1);
*c = '\0';
}
}
/* Uh-oh.... how about a standard Linux with lsb_release? */
if (stat("/usr/bin/lsb_release", &file) == OK && (file.st_mode & S_IXOTH)) {
if (!*sysname && (fp = popen("/usr/bin/lsb_release -i -s", "r"))) {
fgets(sysname, sizeof(sysname), fp);
pclose(fp);
}
if (!*release && (fp = popen("/usr/bin/lsb_release -r -s", "r"))) {
fgets(release, sizeof(release), fp);
pclose(fp);
}
}
/* OK, nothing worked - let's try /etc/issue for sysname */
if (!*sysname && (fp = fopen("/etc/issue", "r"))) {
fgets(sysname, sizeof(sysname), fp);
fclose(fp);
if ((c = strchr(sysname, ' '))) *c = '\0';
if ((c = strchr(sysname, '\\'))) *c = '\0';
}
/* Debian version should be in /etc/debian_version */
if (!*release && (fp = fopen("/etc/debian_version", "r"))) {
fgets (release, sizeof(release), fp);
fclose(fp);
if ((c = strchr(release, '/'))) *c = '\0';
}
#endif
/* Haiku OS */
#ifdef __HAIKU__
/* Fix release name */
snprintf(release, sizeof(release), "R%s", name.release);
#endif
/* Fill in the blanks using uname() data */
if (!*sysname) sstrlcpy(sysname, name.sysname);
if (!*release) sstrlcpy(release, name.release);
if (!*machine) sstrlcpy(machine, name.machine);
/* I always liked weird Perl-only functions */
chomp(sysname);
chomp(release);
chomp(machine);
/* We're only interested in major.minor version */
if ((c = strchr(release, '.'))) if ((c = strchr(c + 1, '.'))) *c = '\0';
if ((c = strchr(release, '-'))) *c = '\0';
if ((c = strchr(release, '/'))) *c = '\0';
/* Create a nicely formatted platform string */
snprintf(st->server_platform, sizeof(st->server_platform), "%s/%s %s",
sysname,
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
machine,
release);
#else
release,
machine);
#endif
/* Debug */
if (st->debug) {
syslog(LOG_INFO, "generated platform string \"%s\"",
st->server_platform);
}
#else
/* Fallback reply */
sstrlcpy(st->server_platform, "Unknown computer-like system");
#endif
}
/*
* Return current CPU load
*/
float loadavg(void)
{
FILE *fp;
char buf[BUFSIZE];
/* Faster Linux version */
#ifdef __linux
buf[0] = '\0';
if ((fp = fopen("/proc/loadavg" , "r")) == NULL) return 0;
fgets(buf, sizeof(buf), fp);
fclose(fp);
return (float) atof(buf);
/* Generic slow version - parse the output of uptime */
#else
#ifdef HAVE_POPEN
char *c;
if ((fp = popen("/usr/bin/uptime", "r"))) {
fgets(buf, sizeof(buf), fp);
pclose(fp);
if ((c = strstr(buf, "average: ")) || (c = strstr(buf, "averages: ")))
return (float) atof(c + 10);
}
#endif
/* Fallback reply */
return 0;
#endif
}