2
KSysGuard, the KDE System Guard
4
Copyright (c) 2006 Greg Martyn <greg.martyn@gmail.com>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of version 2 or later of the GNU General Public
8
License as published by the Free Software Foundation.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
* This file will read from /proc/uptime.
25
#include <string.h> /* for strcmp */
26
#include <stdlib.h> /* for malloc */
28
#include <sys/types.h>
34
#include "ksysguardd.h"
38
#define UPTIMEBUFSIZE 64
40
static char UptimeBuf[ UPTIMEBUFSIZE ]; /* Buffer for /proc/uptime */
42
static struct SensorModul* StatSM;
44
void printUptime( const char* cmd );
45
void printUptimeInfo( const char* cmd );
46
static void openUptimeFile();
48
void initUptime( struct SensorModul* sm ) {
57
uptimeBufP = UptimeBuf;
58
sprintf( format, "%%%d[^\n]\n", (int)sizeof( buf ) - 1 );
60
/* Process values from /proc/uptime */
61
if (sscanf(uptimeBufP, format, buf) == 1) {
62
buf[sizeof(buf) - 1] = '\0';
63
registerMonitor( "system/uptime", "float", printUptime, printUptimeInfo, StatSM );
67
void exitUptime( void ) {
71
void printUptime( const char* cmd ) {
72
/* Process values from /proc/uptime */
77
char* uptimeBufP = UptimeBuf;
80
sprintf( format, "%%%d[^\n]\n", (int)sizeof( buf ) - 1 );
84
if (sscanf(uptimeBufP, format, buf) == 1)
86
buf[sizeof(buf) - 1] = '\0';
87
sscanf( buf, "%f", &uptime );
88
output( "%f\n", uptime );
92
void printUptimeInfo( const char* cmd ) {
95
output( "System uptime\t0\t0\ts\n" );
98
static void openUptimeFile() {
102
UptimeBuf[ 0 ] = '\0';
103
if ( ( fd = open( "/proc/uptime", O_RDONLY ) ) < 0 )
104
return; /* couldn't open /proc/uptime */
106
n = read( fd, UptimeBuf, UPTIMEBUFSIZE - 1 );
109
if ( n == UPTIMEBUFSIZE - 1 || n <= 0 ) {
110
log_error( "Internal buffer too small to read \'/proc/uptime\'" );
115
UptimeBuf[ n ] = '\0';