~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/ksysguardd/Linux/uptime.c

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
 
 
4
    Copyright (c) 2006 Greg Martyn <greg.martyn@gmail.com>
 
5
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
18
 
 
19
*/
 
20
 
 
21
/*
 
22
 * This file will read from /proc/uptime.
 
23
*/
 
24
 
 
25
#include <string.h> /* for strcmp */
 
26
#include <stdlib.h> /* for malloc */
 
27
 
 
28
#include <sys/types.h>
 
29
#include <sys/stat.h>
 
30
#include <fcntl.h>
 
31
#include <unistd.h>
 
32
 
 
33
#include "Command.h"
 
34
#include "ksysguardd.h"
 
35
 
 
36
#include "uptime.h"
 
37
 
 
38
#define UPTIMEBUFSIZE 64
 
39
 
 
40
static char UptimeBuf[ UPTIMEBUFSIZE ]; /* Buffer for /proc/uptime */
 
41
 
 
42
static struct SensorModul* StatSM;
 
43
 
 
44
void printUptime( const char* cmd );
 
45
void printUptimeInfo( const char* cmd );
 
46
static void openUptimeFile();
 
47
 
 
48
void initUptime( struct SensorModul* sm ) {
 
49
        char format[ 32 ];
 
50
        char buf[ 1024 ];
 
51
        char* uptimeBufP;
 
52
 
 
53
        StatSM = sm;
 
54
 
 
55
        openUptimeFile();
 
56
 
 
57
        uptimeBufP = UptimeBuf;
 
58
        sprintf( format, "%%%d[^\n]\n", (int)sizeof( buf ) - 1 );
 
59
 
 
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 );
 
64
        }
 
65
}
 
66
 
 
67
void exitUptime( void ) {
 
68
 
 
69
}
 
70
 
 
71
void printUptime( const char* cmd ) {
 
72
        /* Process values from /proc/uptime */
 
73
        (void)cmd;
 
74
        
 
75
        char format[ 32 ];
 
76
        char buf[ 1024 ];
 
77
        char* uptimeBufP = UptimeBuf;
 
78
        float uptime;
 
79
        
 
80
        sprintf( format, "%%%d[^\n]\n", (int)sizeof( buf ) - 1 );
 
81
        
 
82
        openUptimeFile();
 
83
        
 
84
        if (sscanf(uptimeBufP, format, buf) == 1)
 
85
        {
 
86
                buf[sizeof(buf) - 1] = '\0';
 
87
                sscanf( buf, "%f", &uptime );
 
88
                output( "%f\n", uptime );
 
89
        }
 
90
}
 
91
 
 
92
void printUptimeInfo( const char* cmd ) {
 
93
        (void)cmd;
 
94
        
 
95
        output( "System uptime\t0\t0\ts\n" );
 
96
}
 
97
 
 
98
static void openUptimeFile() {
 
99
        size_t n;
 
100
        int fd;
 
101
 
 
102
        UptimeBuf[ 0 ] = '\0';
 
103
        if ( ( fd = open( "/proc/uptime", O_RDONLY ) ) < 0 )
 
104
                return; /* couldn't open /proc/uptime */
 
105
        
 
106
        n = read( fd, UptimeBuf, UPTIMEBUFSIZE - 1 );
 
107
        close( fd );
 
108
 
 
109
        if ( n == UPTIMEBUFSIZE - 1 || n <= 0 ) {
 
110
                log_error( "Internal buffer too small to read \'/proc/uptime\'" );
 
111
 
 
112
                return;
 
113
        }
 
114
        
 
115
        UptimeBuf[ n ] = '\0';
 
116
}