-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyATASmart.c
266 lines (166 loc) · 6.78 KB
/
pyATASmart.c
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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <atasmart.h>
#include <Python.h>
static PyObject *pyATASmart_diskOpen(PyObject *self, PyObject *args)//Incompatible with python memory manager
{
int ret;
SkDisk *d;
const char *device;
if (!PyArg_ParseTuple(args, "s",&device))
{
return NULL;
}
if ((ret = sk_disk_open(device, &d)) < 0) {
PyErr_SetString(PyExc_OSError, "Failed to open disk");
return NULL;
}
return Py_BuildValue("O", d);
}
/*static PyObject *pyATASmart_diskFree(PyObject *self, PyObject *args) //Reimplemented original C function to be compatible with python memory manager
{
SkDisk *d;
if (!PyArg_ParseTuple(args, "O",&d))
{
return NULL;
}
//sk_disk_free(d); Reimplimentation:
if (d->fd >= 0);
// close(d->fd);
// PyMem_Free(d->name);
//PyMem_Free(d->blob);
//PyMem_Free(d);
}
*/
//Get the power-on time
static PyObject *pyATASmart_getPowerOn(PyObject *self, PyObject *args)
{
int ret;
uint64_t ms;
SkDisk *d;
if (!PyArg_ParseTuple(args, "O", &d))
{
return NULL;
}
if ((ret = sk_disk_smart_read_data(d)) < 0) {
PyErr_SetString(PyExc_OSError, "Failed to read SMART data");
return NULL;
}
if ((ret = sk_disk_smart_get_power_on(d, &ms)) < 0) {
PyErr_SetString(PyExc_OSError, "Failed to get power on time");
return NULL;
}
return Py_BuildValue("K", (unsigned long long) ms);
}
//Get number of power cycles
static PyObject *pyATASmart_getPowerCycle(PyObject *self, PyObject *args)
{
int ret;
uint64_t count;
SkDisk *d;
if (!PyArg_ParseTuple(args, "O", &d))
{
return NULL;
}
if ((ret = sk_disk_smart_read_data(d)) < 0) {
PyErr_SetString(PyExc_OSError, "Failed to read SMART data");
return NULL;
}
if ((ret = sk_disk_smart_get_power_cycle(d, &count)) < 0) {
PyErr_SetString(PyExc_OSError, "Failed to get number of power cycles");
return NULL;
}
return Py_BuildValue("K", (unsigned long long) count);
}
//Get the number of bad sectors (i.e. pending and reallocated)
static PyObject *pyATASmart_getBad(PyObject *self, PyObject *args)
{
int ret;
uint64_t sectors;
SkDisk *d;
if (!PyArg_ParseTuple(args, "O", &d))
{
return NULL;
}
if ((ret = sk_disk_smart_read_data(d)) < 0) {
PyErr_SetString(PyExc_OSError, "Failed to read SMART data");
return NULL;
}
if ((ret = sk_disk_smart_get_bad(d, §ors)) < 0) {
PyErr_SetString(PyExc_OSError, "Failed to get number of bad sectors");
return NULL;
}
return Py_BuildValue("K", (unsigned long long) sectors);
}
static PyObject *pyATASmart_getTemperature(PyObject *self, PyObject *args)
{
int ret;
SkDisk *d;
uint64_t mkelvin;
if (!PyArg_ParseTuple(args, "O", &d))
{
return NULL;
}
if ((ret = sk_disk_smart_read_data(d)) < 0) {
PyErr_SetString(PyExc_OSError, "Failed to read SMART data");
return NULL;
}
if ((ret = sk_disk_smart_get_temperature(d, &mkelvin)) < 0) {
PyErr_SetString(PyExc_OSError, "Failed to get disk temperature");
return NULL;
}
return Py_BuildValue("K", (unsigned long long) mkelvin);
}
static PyObject *pyATASmart_smartIsAvailable(PyObject *self, PyObject *args)
{
int ret;
SkDisk *d;
SkBool available;
if (!PyArg_ParseTuple(args, "O", &d))
{
return NULL;
}
if ((ret = sk_disk_smart_is_available(d, &available)) < 0) {
PyErr_SetString(PyExc_OSError, "Unable to check if SMART is available.");
return NULL;
}
return Py_BuildValue("i", (int) available);
}
static PyObject *pyATASmart_smartStatus(PyObject *self, PyObject *args)
{
int ret;
SkDisk *d;
SkBool statusGood;
if (!PyArg_ParseTuple(args, "O", &d))
{
return NULL;
}
if ((ret = sk_disk_smart_read_data(d)) < 0) {
PyErr_SetString(PyExc_OSError, "Failed to read SMART data");
return NULL;
}
if ((ret = sk_disk_smart_status(d, &statusGood)) < 0) {
PyErr_SetString(PyExc_OSError, "Failed to get SMART status");
return NULL;
}
return Py_BuildValue("i", (int) statusGood);
}
static PyMethodDef pyATASmart_methods[] = {
{ "diskOpen", (PyCFunction)pyATASmart_diskOpen, METH_VARARGS, "Open a disk" },
{ "getPowerOn", (PyCFunction)pyATASmart_getPowerOn, METH_VARARGS, "Get the disk power-on time"},
{ "getTemperature", (PyCFunction)pyATASmart_getTemperature, METH_VARARGS, "Get the disk temperature" },
{ "getBad", (PyCFunction)pyATASmart_getBad, METH_VARARGS, "Get number of bad sectors" },
{ "getPowerCycle", (PyCFunction)pyATASmart_getPowerCycle, METH_VARARGS, "Get number of power cycles" },
{ "smartStatus", (PyCFunction)pyATASmart_smartStatus, METH_VARARGS, "Get smart status" },
{ "smartIsAvailable", (PyCFunction)pyATASmart_smartIsAvailable, METH_VARARGS, "Check if SMART is available" },
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC initpyATASmart()
{
Py_InitModule3("pyATASmart", pyATASmart_methods, "Python bindings for the libatasmart library");
}