2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-04-30 22:22:54 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2004-07-13 13:54:09 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2004-07-13 13:54:09 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2004-07-13 13:54:09 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2003-10-16 19:03:49 +00:00
|
|
|
#include "cpp.h"
|
2002-07-22 23:07:14 +00:00
|
|
|
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <ctime>
|
2002-07-03 22:37:24 +00:00
|
|
|
|
2002-10-14 05:52:48 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
2002-07-03 22:37:24 +00:00
|
|
|
#if HAVE_SYS_TYPES_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <sys/types.h>
|
2002-07-03 22:37:24 +00:00
|
|
|
#endif
|
2002-10-14 05:52:48 +00:00
|
|
|
#ifdef HAVE_SYS_MOUNT_H
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_VFS_H
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
#endif
|
2002-10-14 05:58:33 +00:00
|
|
|
#ifdef HAVE_SYS_VMMETER_H
|
2002-10-14 05:52:48 +00:00
|
|
|
#include <sys/vmmeter.h>
|
2002-10-14 05:58:33 +00:00
|
|
|
#endif
|
2002-10-14 05:52:48 +00:00
|
|
|
|
2003-04-08 18:36:03 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#if HAVE_SYS_SWAP_H
|
|
|
|
#include <sys/swap.h>
|
|
|
|
#endif
|
2002-10-14 05:52:48 +00:00
|
|
|
#if HAVE_SYS_SYSCTL_H
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
2002-06-10 06:14:18 +00:00
|
|
|
#if HAVE_SYS_SYSTEMINFO_H
|
2002-06-06 22:39:21 +00:00
|
|
|
#include <sys/systeminfo.h>
|
2002-06-10 06:14:18 +00:00
|
|
|
#endif
|
2002-07-03 22:37:24 +00:00
|
|
|
#if HAVE_SYS_UTSNAME_H
|
2002-06-06 18:50:12 +00:00
|
|
|
#include <sys/utsname.h>
|
2002-07-03 22:37:24 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_UNISTD_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <unistd.h>
|
2002-07-03 22:37:24 +00:00
|
|
|
#endif
|
2004-06-15 00:46:59 +00:00
|
|
|
#if HAVE_UTMP_H
|
|
|
|
#include <utmp.h>
|
|
|
|
#endif
|
2002-07-03 22:37:24 +00:00
|
|
|
#if HAVE_NETINET_IN_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <netinet/in.h>
|
2002-06-06 18:50:12 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-06-01 20:26:21 +00:00
|
|
|
#include "client_types.h"
|
2003-06-14 20:24:29 +00:00
|
|
|
#include "filesys.h"
|
2002-07-11 01:09:53 +00:00
|
|
|
#include "error_numbers.h"
|
2003-03-09 01:02:20 +00:00
|
|
|
#include "util.h"
|
2004-04-05 20:08:20 +00:00
|
|
|
#include "client_state.h"
|
2004-06-16 19:59:07 +00:00
|
|
|
#include "hostinfo_network.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-07-02 02:58:43 +00:00
|
|
|
using std::string;
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
// functions to get name/addr of local host
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Converts a int ip address to a string representation (i.e. "66.218.71.198")
|
|
|
|
//
|
2003-01-13 23:37:56 +00:00
|
|
|
#if 0
|
|
|
|
char* ip_addr_string(int ip_addr) {
|
2002-04-30 22:22:54 +00:00
|
|
|
in_addr ia;
|
|
|
|
|
|
|
|
ia.s_addr = ip_addr;
|
|
|
|
return inet_ntoa(ia);
|
2003-01-13 23:37:56 +00:00
|
|
|
}
|
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-07-18 21:14:32 +00:00
|
|
|
// Returns the number of seconds difference from UTC
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2003-06-14 20:24:29 +00:00
|
|
|
int get_timezone() {
|
2002-07-18 21:21:37 +00:00
|
|
|
tzset();
|
|
|
|
// TODO: take daylight savings time into account
|
2003-08-13 21:35:26 +00:00
|
|
|
#ifdef HAVE_STRUCT_TM_TM_ZONE
|
2003-01-13 23:37:56 +00:00
|
|
|
time_t cur_time;
|
|
|
|
struct tm *time_data;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-01-13 23:37:56 +00:00
|
|
|
cur_time = time(NULL);
|
|
|
|
time_data = localtime( &cur_time );
|
|
|
|
return time_data->tm_gmtoff;
|
2003-05-06 21:43:26 +00:00
|
|
|
#elif defined(linux)
|
2003-01-13 23:37:56 +00:00
|
|
|
return __timezone;
|
2003-12-01 22:53:09 +00:00
|
|
|
#elif defined(__CYGWIN32__)
|
|
|
|
return _timezone;
|
2003-05-06 21:43:26 +00:00
|
|
|
#elif defined(unix)
|
2003-01-13 23:37:56 +00:00
|
|
|
return timezone;
|
2003-04-30 20:10:37 +00:00
|
|
|
#else
|
|
|
|
#error timezone
|
2002-07-18 21:21:37 +00:00
|
|
|
#endif
|
2002-07-18 23:05:34 +00:00
|
|
|
return 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Returns true if the host is currently running off battery power
|
2004-03-31 19:43:15 +00:00
|
|
|
// If you can't figure out, return false
|
|
|
|
//
|
2002-07-15 23:21:20 +00:00
|
|
|
// TODO: port this to other platforms (Windows, Mac OS X, others?)
|
|
|
|
//
|
2004-03-31 19:43:15 +00:00
|
|
|
bool HOST_INFO::host_is_running_on_batteries() {
|
2004-04-04 05:31:27 +00:00
|
|
|
#ifdef linux
|
2004-04-22 19:14:56 +00:00
|
|
|
bool retval = false;
|
2004-04-04 05:31:27 +00:00
|
|
|
|
|
|
|
FILE* fapm = fopen("/proc/apm", "r");
|
2004-04-05 06:32:28 +00:00
|
|
|
if (fapm) { // Then we're using APM! Yay.
|
2004-04-22 19:14:56 +00:00
|
|
|
|
|
|
|
char apm_driver_version[10];
|
|
|
|
int apm_major_version;
|
|
|
|
int apm_minor_version;
|
|
|
|
int apm_flags;
|
|
|
|
int apm_ac_line_status=1;
|
|
|
|
|
|
|
|
// Supposedly we're on batteries if the 5th entry is zero.
|
|
|
|
fscanf(fapm, "%10s %d.%d %x %x",
|
|
|
|
apm_driver_version,
|
|
|
|
&apm_major_version,
|
|
|
|
&apm_minor_version,
|
|
|
|
&apm_flags,
|
|
|
|
&apm_ac_line_status
|
|
|
|
);
|
|
|
|
retval = (apm_ac_line_status == 0);
|
2004-04-28 21:10:14 +00:00
|
|
|
fclose(fapm);
|
2004-04-22 19:14:56 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// we try ACPI
|
|
|
|
char buf[128];
|
|
|
|
char ac_state[64];
|
2004-07-02 04:49:17 +00:00
|
|
|
std::string ac_name;
|
2004-04-22 19:14:56 +00:00
|
|
|
FILE* facpi;
|
2004-07-13 13:54:09 +00:00
|
|
|
|
|
|
|
// we need to find the right ac adapter first
|
2004-04-22 19:14:56 +00:00
|
|
|
DirScanner dir("/proc/acpi/ac_adapter/");
|
|
|
|
while (dir.scan(ac_name)) {
|
|
|
|
if ((ac_name.c_str()==".")||(ac_name.c_str()=="..")) continue;
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2004-04-22 19:14:56 +00:00
|
|
|
// newer ACPI versions use "state" as filename
|
|
|
|
sprintf(ac_state, "/proc/acpi/ac_adapter/%s/state", ac_name.c_str());
|
|
|
|
facpi = fopen(ac_state, "r");
|
2004-07-13 13:54:09 +00:00
|
|
|
if (!facpi) {
|
2004-04-22 19:14:56 +00:00
|
|
|
// older ACPI versions use "status" instead
|
|
|
|
sprintf(ac_state, "/proc/acpi/ac_adapter/%s/status", ac_name.c_str());
|
|
|
|
facpi = fopen(ac_state, "r");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (facpi) {
|
|
|
|
fgets(buf, 128, facpi);
|
2004-04-30 02:26:23 +00:00
|
|
|
fclose(facpi);
|
2004-04-22 19:14:56 +00:00
|
|
|
|
|
|
|
// only valid state if it contains "state" (newer) or "Status" (older)
|
2004-07-13 13:54:09 +00:00
|
|
|
if ((strstr(buf, "state:") != NULL) || (strstr(buf, "Status:") != NULL)) {
|
2004-04-22 19:14:56 +00:00
|
|
|
// on batteries if ac adapter is "off-line" (or maybe "offline")
|
|
|
|
retval = (strstr(buf, "off") != NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-03-31 06:25:40 +00:00
|
|
|
}
|
2004-04-22 19:14:56 +00:00
|
|
|
|
|
|
|
return retval;
|
2004-04-04 05:31:27 +00:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef linux
|
|
|
|
|
2004-03-12 23:09:48 +00:00
|
|
|
#ifdef __mips__
|
|
|
|
|
|
|
|
void parse_cpuinfo(HOST_INFO& host) {
|
|
|
|
char buf[256];
|
|
|
|
char buf2[256];
|
2004-03-17 02:06:09 +00:00
|
|
|
int system_found=0,model_found=0;
|
2004-03-12 23:09:48 +00:00
|
|
|
|
|
|
|
strcpy(host.p_model, "MIPS ");
|
|
|
|
|
|
|
|
FILE* f = fopen("/proc/cpuinfo", "r");
|
|
|
|
if (!f) return;
|
|
|
|
|
|
|
|
while (fgets(buf, 256, f)) {
|
2004-03-17 02:06:09 +00:00
|
|
|
if ( (strstr(buf, "system type\t\t: ") == buf) &&
|
|
|
|
(system_found == 0) ) {
|
|
|
|
system_found = 1;
|
|
|
|
strncpy(host.p_vendor, strchr(buf, ':') + 2, sizeof(host.p_vendor)-1);
|
|
|
|
char * p = strchr(host.p_vendor, '\n');
|
|
|
|
if (p) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2004-03-12 23:09:48 +00:00
|
|
|
}
|
2004-03-17 02:06:09 +00:00
|
|
|
if ( (strstr(buf, "cpu model\t\t: ") == buf) &&
|
|
|
|
(model_found == 0) ) {
|
|
|
|
model_found = 1;
|
|
|
|
strncpy(buf2, strchr(buf, ':') + 2, sizeof(host.p_model) - strlen(host.p_model) - 1);
|
|
|
|
strcat(host.p_model, buf2);
|
|
|
|
char * p = strchr(host.p_model, '\n');
|
|
|
|
if (p) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2004-03-12 23:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2004-04-04 05:31:27 +00:00
|
|
|
#else // mips
|
2004-03-12 23:09:48 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
// Unfortunately the format of /proc/cpuinfo is not standardized.
|
|
|
|
// See http://people.nl.linux.org/~hch/cpuinfo/ for some examples.
|
|
|
|
// The following is for Redhat Linux 2.2.14.
|
2002-07-15 23:21:20 +00:00
|
|
|
// TODO: get this to work on all platforms
|
2003-11-25 15:51:19 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
void parse_cpuinfo(HOST_INFO& host) {
|
|
|
|
char buf[256];
|
2004-03-17 02:06:09 +00:00
|
|
|
int system_found=0,model_found=0,cache_found=0;
|
2002-04-30 22:22:54 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
FILE* f = fopen("/proc/cpuinfo", "r");
|
|
|
|
if (!f) return;
|
2004-03-12 23:09:48 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
while (fgets(buf, 256, f)) {
|
2004-03-17 02:06:09 +00:00
|
|
|
if ( (strstr(buf, "vendor_id\t: ") == buf) &&
|
|
|
|
(system_found == 0) ) {
|
|
|
|
system_found = 1;
|
2004-03-12 23:09:48 +00:00
|
|
|
strncpy(host.p_vendor, strchr(buf, ':') + 2, sizeof(host.p_vendor) - 1);
|
|
|
|
char * p = strchr(host.p_vendor, '\n');
|
|
|
|
if (p) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2004-03-17 02:06:09 +00:00
|
|
|
if ( (strstr(buf, "model name\t: ") == buf) &&
|
|
|
|
(model_found == 0) ) {
|
|
|
|
model_found = 1;
|
2004-03-12 23:09:48 +00:00
|
|
|
strncpy(host.p_model, strchr(buf, ':') + 2, sizeof(host.p_model) - 1);
|
|
|
|
char * p = strchr(host.p_model, '\n');
|
|
|
|
if (p) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2004-03-17 02:06:09 +00:00
|
|
|
if ( (strstr(buf, "cache size\t: ") == buf) &&
|
|
|
|
(cache_found == 0) ) {
|
|
|
|
cache_found = 1;
|
2002-04-30 22:22:54 +00:00
|
|
|
sscanf(buf, "cache size\t: %d", &n);
|
|
|
|
host.m_cache = n*1024;
|
|
|
|
}
|
|
|
|
}
|
2004-03-12 23:09:48 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
2004-03-12 23:09:48 +00:00
|
|
|
|
|
|
|
#endif // MIPS
|
|
|
|
|
2004-04-04 05:31:27 +00:00
|
|
|
#endif // linux
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-06-14 20:24:29 +00:00
|
|
|
// get all relevant host information
|
2002-10-14 05:52:48 +00:00
|
|
|
//
|
2004-03-31 19:43:15 +00:00
|
|
|
int HOST_INFO::get_host_info() {
|
|
|
|
get_filesystem_info(d_total, d_free);
|
2003-05-06 21:43:26 +00:00
|
|
|
|
2003-05-10 00:52:36 +00:00
|
|
|
#ifdef linux
|
2004-03-31 19:43:15 +00:00
|
|
|
parse_cpuinfo(*this);
|
2003-05-10 00:52:36 +00:00
|
|
|
#else
|
2003-04-30 20:10:37 +00:00
|
|
|
#if HAVE_SYS_SYSCTL_H
|
2004-03-17 01:26:44 +00:00
|
|
|
int mib[2];
|
|
|
|
unsigned int mem_size;
|
2003-04-30 20:10:37 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
// Get machine
|
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_MACHINE;
|
2004-03-31 19:43:15 +00:00
|
|
|
len = sizeof(p_vendor);
|
|
|
|
sysctl(mib, 2, &p_vendor, &len, NULL, 0);
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2003-04-30 20:10:37 +00:00
|
|
|
// Get model
|
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_MODEL;
|
2004-04-03 00:43:55 +00:00
|
|
|
len = sizeof(p_model);
|
2004-03-31 19:43:15 +00:00
|
|
|
sysctl(mib, 2, &p_model, &len, NULL, 0);
|
2003-04-30 20:10:37 +00:00
|
|
|
#endif
|
2003-04-08 07:05:22 +00:00
|
|
|
#endif
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2003-04-08 07:05:22 +00:00
|
|
|
|
2003-04-08 18:36:03 +00:00
|
|
|
#if defined(_SC_NPROCESSORS_ONLN)
|
2004-03-31 19:43:15 +00:00
|
|
|
p_ncpus = sysconf(_SC_NPROCESSORS_ONLN);
|
2003-04-30 20:10:37 +00:00
|
|
|
#elif defined(HAVE_SYS_SYSCTL_H) && defined(CTL_HW) && defined(HW_NCPU)
|
|
|
|
// Get number of CPUs
|
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_NCPU;
|
2004-03-31 19:43:15 +00:00
|
|
|
len = sizeof(p_ncpus);
|
|
|
|
sysctl(mib, 2, &p_ncpus, &len, NULL, 0);
|
2003-04-08 18:36:03 +00:00
|
|
|
#else
|
|
|
|
#error Need to specify a sysconf() define to obtain number of processors
|
|
|
|
#endif
|
2002-11-04 04:06:06 +00:00
|
|
|
|
2003-06-06 22:08:46 +00:00
|
|
|
/* There can be a variety of methods to obtain amount of
|
|
|
|
* usable memory. You will have to check your sysconf()
|
|
|
|
* defines, probably in unistd.h
|
|
|
|
* - 2002-11-03 hiram@users.sourceforge.net
|
2002-11-04 04:06:06 +00:00
|
|
|
*/
|
|
|
|
#if defined(_SC_USEABLE_MEMORY)
|
2004-03-31 19:43:15 +00:00
|
|
|
m_nbytes = (double)sysconf(_SC_PAGESIZE)
|
2003-06-06 22:08:46 +00:00
|
|
|
* (double)sysconf(_SC_USEABLE_MEMORY); /* UnixWare */
|
2002-11-04 04:06:06 +00:00
|
|
|
#elif defined(_SC_PHYS_PAGES)
|
2004-03-31 19:43:15 +00:00
|
|
|
m_nbytes = (double)sysconf(_SC_PAGESIZE)
|
2003-06-06 22:08:46 +00:00
|
|
|
* (double)sysconf(_SC_PHYS_PAGES); /* Linux */
|
2003-04-30 20:10:37 +00:00
|
|
|
#elif defined(HAVE_SYS_SYSCTL_H) && defined(CTL_HW) && defined(HW_PHYSMEM)
|
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_PHYSMEM;
|
|
|
|
len = sizeof(mem_size);
|
2003-06-06 22:08:46 +00:00
|
|
|
sysctl(mib, 2, &mem_size, &len, NULL, 0); // Mac OS X
|
2004-03-31 19:43:15 +00:00
|
|
|
m_nbytes = mem_size;
|
2002-11-04 04:06:06 +00:00
|
|
|
#else
|
|
|
|
#error Need to specify a sysconf() define to obtain memory size
|
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-04-08 18:36:03 +00:00
|
|
|
#if defined(HAVE_SYS_SWAP_H) && defined(SC_GETNSWP)
|
|
|
|
char buf[256];
|
2002-04-30 22:22:54 +00:00
|
|
|
swaptbl_t* s;
|
2003-04-08 18:36:03 +00:00
|
|
|
int i, n;
|
2002-04-30 22:22:54 +00:00
|
|
|
n = swapctl(SC_GETNSWP, 0);
|
|
|
|
s = (swaptbl_t*)malloc(n*sizeof(swapent_t) + sizeof(struct swaptable));
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
s->swt_ent[i].ste_path = buf;
|
|
|
|
}
|
|
|
|
s->swt_n = n;
|
|
|
|
n = swapctl(SC_LIST, s);
|
|
|
|
for (i=0; i<n; i++) {
|
2004-03-31 19:43:15 +00:00
|
|
|
m_swap += 512.*(double)s->swt_ent[i].ste_length;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2003-04-30 20:10:37 +00:00
|
|
|
#elif defined(HAVE_SYS_SYSCTL_H) && defined(CTL_VM) && defined(VM_METER)
|
|
|
|
// TODO: figure this out
|
|
|
|
/*vmtotal vm_info;
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2003-04-30 20:10:37 +00:00
|
|
|
mib[0] = CTL_VM;
|
2002-10-14 05:52:48 +00:00
|
|
|
mib[1] = VM_METER;
|
|
|
|
len = sizeof(vm_info);
|
|
|
|
sysctl(mib, 2, &vm_info, &len, NULL, 0);
|
2004-03-31 19:43:15 +00:00
|
|
|
m_swap = vm_info.t_vm;
|
2003-04-30 20:10:37 +00:00
|
|
|
*/
|
|
|
|
#else
|
|
|
|
#endif
|
|
|
|
|
2004-07-13 13:54:09 +00:00
|
|
|
#if defined(HAVE_SYS_SYSTEMINFO_H)
|
2003-10-28 23:45:31 +00:00
|
|
|
#if defined(SI_HW_SERIAL)
|
2004-03-31 19:43:15 +00:00
|
|
|
sysinfo(SI_HW_SERIAL, serialnum, sizeof(serialnum));
|
2003-04-30 20:10:37 +00:00
|
|
|
#else
|
|
|
|
//#error Need to specify a method to obtain serial num
|
2002-10-14 05:52:48 +00:00
|
|
|
#endif
|
2003-10-28 23:45:31 +00:00
|
|
|
#ifdef SI_PLATFORM
|
2004-03-31 19:43:15 +00:00
|
|
|
sysinfo(SI_PLATFORM, p_vendor, sizeof(p_vendor));
|
2003-10-28 23:45:31 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SI_ISALIST
|
2004-03-31 19:43:15 +00:00
|
|
|
sysinfo(SI_ISALIST, p_model, sizeof(p_model));
|
|
|
|
for (unsigned int i=0; i<sizeof(p_model); i++) {
|
|
|
|
if (p_model[i]==' ') {
|
|
|
|
p_model[i]=0;
|
2003-11-25 15:51:19 +00:00
|
|
|
}
|
2004-03-31 19:43:15 +00:00
|
|
|
if (p_model[i]==0) {
|
|
|
|
i=sizeof(p_model);
|
2003-11-25 15:51:19 +00:00
|
|
|
}
|
2003-10-28 23:45:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2004-06-17 17:00:14 +00:00
|
|
|
get_local_network_info(
|
|
|
|
domain_name, sizeof(domain_name), ip_addr, sizeof(ip_addr)
|
|
|
|
);
|
|
|
|
|
2004-03-31 19:43:15 +00:00
|
|
|
timezone = get_timezone();
|
2002-07-03 22:37:24 +00:00
|
|
|
#ifdef HAVE_SYS_UTSNAME_H
|
2003-04-08 18:36:03 +00:00
|
|
|
struct utsname u;
|
|
|
|
uname(&u);
|
2004-03-31 19:43:15 +00:00
|
|
|
safe_strcpy(os_name, u.sysname);
|
|
|
|
safe_strcpy(os_version, u.release);
|
2003-04-30 20:10:37 +00:00
|
|
|
#elif defined(HAVE_SYS_SYSCTL_H) && defined(CTL_KERN) && defined(KERN_OSTYPE) && defined(KERN_OSRELEASE)
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_OSTYPE;
|
2004-03-31 19:43:15 +00:00
|
|
|
len = sizeof(os_name);
|
|
|
|
sysctl(mib, 2, &os_name, &len, NULL, 0);
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2003-04-30 20:10:37 +00:00
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_OSRELEASE;
|
2004-03-31 19:43:15 +00:00
|
|
|
len = sizeof(os_version);
|
|
|
|
sysctl(mib, 2, &os_version, &len, NULL, 0);
|
2003-04-30 20:10:37 +00:00
|
|
|
#elif HAVE_SYS_SYSTEMINFO_H
|
2004-03-31 19:43:15 +00:00
|
|
|
sysinfo(SI_SYSNAME, os_name, sizeof(os_name));
|
|
|
|
sysinfo(SI_RELEASE, os_version, sizeof(os_version));
|
2003-04-30 20:10:37 +00:00
|
|
|
#else
|
|
|
|
#error Need to specify a method to obtain OS name/version
|
2002-06-06 22:39:21 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2004-06-12 05:44:28 +00:00
|
|
|
|
|
|
|
// returns true iff device was last accessed before t
|
|
|
|
// or if an error occurred looking at the device.
|
|
|
|
inline bool device_idle(time_t t, char *device) {
|
|
|
|
struct stat sbuf;
|
|
|
|
return stat(device, &sbuf) || (sbuf.st_atime < t);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool all_tty_idle(time_t t, char *device, char first_char, int num_tty) {
|
|
|
|
struct stat sbuf;
|
|
|
|
char *tty_index = device + strlen(device) - 1;
|
|
|
|
*tty_index = first_char;
|
|
|
|
for (int i = 0; i < num_tty; i++, (*tty_index)++) {
|
|
|
|
if (stat(device, &sbuf)) {
|
|
|
|
// error looking at device; don't try any more
|
|
|
|
return true;
|
|
|
|
} else if (sbuf.st_atime >= t) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-06-16 21:18:40 +00:00
|
|
|
#ifdef HAVE_UTMP_H
|
2004-06-15 00:46:59 +00:00
|
|
|
inline bool user_idle(time_t t, struct utmp* u) {
|
|
|
|
char tty[5 + sizeof u->ut_line + 1] = "/dev/";
|
|
|
|
unsigned int i;
|
|
|
|
|
2004-06-29 04:34:55 +00:00
|
|
|
for (i=0; i < sizeof(u->ut_line); i++) {
|
|
|
|
// clean up tty if garbled
|
|
|
|
if (isalnum((int) u->ut_line[i]) || (u->ut_line[i]=='/')) {
|
2004-06-15 00:46:59 +00:00
|
|
|
tty[i+5] = u->ut_line[i];
|
2004-06-29 04:34:55 +00:00
|
|
|
} else {
|
2004-06-15 00:46:59 +00:00
|
|
|
tty[i+5] = '\0';
|
2004-06-29 04:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
2004-06-15 00:46:59 +00:00
|
|
|
return device_idle(t, tty);
|
|
|
|
}
|
|
|
|
|
2004-06-16 21:18:40 +00:00
|
|
|
#if !defined(HAVE_SETUTENT) || !defined(HAVE_GETUTENT)
|
|
|
|
static FILE *ufp = NULL;
|
|
|
|
static struct utmp ut;
|
|
|
|
|
2004-06-18 05:08:57 +00:00
|
|
|
// get next user login record
|
|
|
|
// (this is defined on everything except BSD)
|
|
|
|
//
|
2004-06-16 21:18:40 +00:00
|
|
|
struct utmp *getutent() {
|
|
|
|
if (ufp == NULL) {
|
2004-06-17 23:10:52 +00:00
|
|
|
#if defined(UTMP_LOCATION)
|
|
|
|
if ((ufp = fopen(UTMP_LOCATION, "r")) == NULL) {
|
|
|
|
#elif defined(UTMP_FILE)
|
2004-06-16 21:18:40 +00:00
|
|
|
if ((ufp = fopen(UTMP_FILE, "r")) == NULL) {
|
2004-06-17 23:10:52 +00:00
|
|
|
#elif defined(_PATH_UTMP)
|
|
|
|
if ((ufp = fopen(_PATH_UTMP, "r")) == NULL) {
|
|
|
|
#else
|
|
|
|
if ((ufp = fopen("/etc/utmp", "r")) == NULL) {
|
2004-06-17 21:52:11 +00:00
|
|
|
#endif
|
2004-06-16 21:18:40 +00:00
|
|
|
return((struct utmp *)NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
if (fread((char *)&ut, sizeof(ut), 1, ufp) != 1) {
|
|
|
|
return((struct utmp *)NULL);
|
|
|
|
}
|
|
|
|
} while (ut.ut_name[0] == 0);
|
|
|
|
return(&ut);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setutent() {
|
|
|
|
if (ufp != NULL) rewind(ufp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-18 05:08:57 +00:00
|
|
|
// scan list of logged-in users, and see if they're all idle
|
|
|
|
//
|
2004-06-15 00:46:59 +00:00
|
|
|
inline bool all_logins_idle(time_t t) {
|
|
|
|
struct utmp* u;
|
|
|
|
setutent();
|
|
|
|
|
|
|
|
while ((u = getutent()) != NULL) {
|
|
|
|
if (!user_idle(t, u)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2004-06-15 23:56:17 +00:00
|
|
|
#endif
|
2004-06-15 00:46:59 +00:00
|
|
|
|
2004-06-29 19:54:50 +00:00
|
|
|
bool HOST_INFO::users_idle(bool check_all_logins, double idle_time_to_run) {
|
2004-06-15 00:46:59 +00:00
|
|
|
#ifdef HAVE__DEV_TTY1
|
2004-06-12 05:44:28 +00:00
|
|
|
char device_tty[] = "/dev/tty1";
|
2004-06-15 00:46:59 +00:00
|
|
|
#endif
|
2004-06-29 19:54:50 +00:00
|
|
|
time_t idle_time = time(NULL) - (long) (60 * idle_time_to_run);
|
|
|
|
return true
|
2004-06-15 23:56:17 +00:00
|
|
|
#ifdef HAVE_UTMP_H
|
2004-06-15 00:46:59 +00:00
|
|
|
&& (!check_all_logins || all_logins_idle(idle_time))
|
2004-06-15 23:56:17 +00:00
|
|
|
#endif
|
2004-06-12 05:44:28 +00:00
|
|
|
#ifdef HAVE__DEV_MOUSE
|
|
|
|
&& device_idle(idle_time, "/dev/mouse") // solaris, linux
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE__DEV_KBD
|
|
|
|
&& device_idle(idle_time, "/dev/kbd") // solaris
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE__DEV_TTY1
|
2004-06-15 00:46:59 +00:00
|
|
|
&& (check_all_logins || all_tty_idle(idle_time, device_tty, '1', 7))
|
2004-06-12 05:44:28 +00:00
|
|
|
#endif
|
|
|
|
;
|
|
|
|
}
|
2004-06-15 00:46:59 +00:00
|
|
|
|