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
|
/*
* Copyright (C) 2006 Jean-Yves Lefort <jylefort@FreeBSD.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <time.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include "smartctl-sensors-interface.h"
#include "sensors-applet.h"
/* be gentle */
#define POLL_INTERVAL 30
typedef struct
{
time_t last_poll;
double value;
} SensorInfo;
static GHashTable *sensors;
static gboolean
smartctl_sensors_interface_run (const char *command,
const char *device,
char **output)
{
char *argv[4];
GSpawnFlags flags = G_SPAWN_STDERR_TO_DEV_NULL;
char *_output = NULL;
int exit_status;
g_return_val_if_fail(command != NULL, FALSE);
g_return_val_if_fail(device != NULL, FALSE);
argv[0] = SMARTCTL_HELPER;
argv[1] = (char *) command;
argv[2] = (char *) device;
argv[3] = NULL;
if (! output)
flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
if (g_spawn_sync(NULL, argv, NULL, flags, NULL, NULL, output ? &_output : NULL, NULL, &exit_status, NULL))
{
if (exit_status == 0)
{
if (output)
*output = _output;
return TRUE;
}
g_free(_output);
}
return FALSE;
}
static gboolean
smartctl_sensors_interface_get_temperature (const char *device, double *temp)
{
char *output;
char **lines;
int i;
gboolean status = FALSE;
g_return_val_if_fail(device != NULL, FALSE);
if (! smartctl_sensors_interface_run("attributes", device, &output))
return FALSE;
lines = g_strsplit(output, "\n", 0);
g_free(output);
for (i = 0; lines[i]; i++)
if (g_str_has_prefix(lines[i], "194 Temperature_Celsius"))
{
char *p;
p = strrchr(lines[i], ' ');
if (p)
{
double _temp;
char *end;
_temp = strtod(p + 1, &end);
if (*end == 0)
{
status = TRUE;
if (temp)
*temp = _temp;
}
}
break;
}
g_strfreev(lines);
return status;
}
static void
smartctl_sensors_interface_disk_init (SensorsApplet *sensors_applet,
const char *disk,
int unit)
{
char *device;
g_return_if_fail(sensors_applet != NULL);
g_return_if_fail(disk != NULL);
device = g_strdup_printf("/dev/%s%i", disk, unit);
if (g_file_test(device, G_FILE_TEST_EXISTS)
&& smartctl_sensors_interface_run("enable", device, NULL)
&& smartctl_sensors_interface_get_temperature(device, NULL))
{
char *path;
char *label;
path = g_strdup_printf("/smartctl%s", device);
label = g_strdup_printf("%s%i", disk, unit);
sensors_applet_add_sensor(sensors_applet,
path,
device,
label,
SMARTCTL,
TRUE,
TEMP_SENSOR,
HDD_ICON);
g_free(path);
g_free(label);
}
g_free(device);
}
void
smartctl_sensors_interface_init (SensorsApplet *sensors_applet)
{
int i;
sensors = g_hash_table_new(g_str_hash, g_str_equal);
sensors_applet_register_sensors_interface(sensors_applet,
SMARTCTL,
smartctl_sensors_interface_get_sensor_value);
/* smartctl supports ad(4) and da(4) disks */
for (i = 0; i < 10; i++)
{
smartctl_sensors_interface_disk_init(sensors_applet, "ad", i);
smartctl_sensors_interface_disk_init(sensors_applet, "da", i);
}
}
double
smartctl_sensors_interface_get_sensor_value (const gchar *path,
const gchar *id,
SensorType type,
GError **error)
{
SensorInfo *info;
time_t now;
info = g_hash_table_lookup(sensors, id);
if (! info)
{
info = g_new0(SensorInfo, 1);
g_hash_table_insert(sensors, g_strdup(id), info);
}
now = time(NULL);
if (now == -1 || now - info->last_poll >= POLL_INTERVAL)
{
info->last_poll = now;
smartctl_sensors_interface_get_temperature(id, &info->value);
}
return info->value;
}
|