forked from compulab/i3m-linux-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdd-info.c
183 lines (149 loc) · 3.4 KB
/
hdd-info.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
/*
* Copyright (C) 2016, CompuLab ltd.
* Author: Andrey Gelman <andrey.gelman@compulab.co.il>
* License: GNU GPLv2 or later, at your option
*
* Gather some HDD-related information using the S.M.A.R.T. technology.
* This code relies on libatasmart.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <atasmart.h>
#include "common.h"
#include "dlist.h"
#include "hdd-info.h"
#define SYS_BLOCK_PATH "/sys/block"
#define DEV_BLOCK_PATH "/dev"
DList *smart_devices = NULL;
/*
* SMARTinfo freelist-based memory management
*/
DList *SMARTinfo_freelist = NULL;
SMARTinfo *new_SMARTinfo(void)
{
SMARTinfo *si;
si = dlist_pop_front(SMARTinfo_freelist);
if (si == NULL)
si = (SMARTinfo *)malloc(sizeof(SMARTinfo));
memset(si, 0, sizeof(SMARTinfo));
return si;
}
void delete_SMARTinfo(SMARTinfo *si)
{
if (si == NULL)
return;
dlist_push_back(SMARTinfo_freelist, si);
}
static void hdd_info_cleanup(void)
{
SMARTinfo *si;
while ((si = dlist_pop_front(SMARTinfo_freelist)) != NULL)
free(si);
while ((si = dlist_pop_front(smart_devices)) != NULL)
free(si);
dlist_destroy(SMARTinfo_freelist);
dlist_destroy(smart_devices);
}
static void hdd_info_init(void)
{
smart_devices = dlist_create(NULL);
SMARTinfo_freelist = dlist_create(NULL);
atexit(hdd_info_cleanup);
}
static unsigned int to_celsius(uint64_t millikelvin)
{
return (millikelvin - 273150) / 1000;
}
/*
* Implement visitor pattern on each directory under 'path'.
*/
static void scan_dirs(const char *path, int (*visitor)(const char *ata_dev, void *arg), void *varg)
{
DIR *root;
struct dirent *d;
char buffer[128];
bool keep_searching;
int n;
root = opendir(path);
if (root == NULL)
{
sloge("%s: could not open directory: %m", path);
return;
}
keep_searching = true;
while (((d = readdir(root)) != NULL) && keep_searching)
{
if (!strcmp(".", d->d_name) || !strcmp("..", d->d_name))
continue;
n = readlinkat(dirfd(root), d->d_name, buffer, sizeof(buffer));
if (n < 0)
continue;
buffer[n] = '\0';
if (!strstr(buffer, "/ata"))
continue;
keep_searching = visitor(d->d_name, varg);
}
closedir(root);
}
static int atasmart_get_info(const char *devname, void *arg)
{
int err;
char device[64];
SkDisk *d;
SMARTinfo *si;
uint64_t mkelvin;
uint64_t size_B;
DList *hdd_list = (DList *)arg;
snprintf(device, sizeof(device), "%s/%s", DEV_BLOCK_PATH, devname);
err = sk_disk_open(device, &d);
if (err < 0)
{
sloge("%s: could not open: %m", device);
goto gettemp_out0;
}
err = sk_disk_smart_read_data(d);
if (err < 0)
{
slogi("%s: could not read SMART data: %m", device);
goto gettemp_out1;
}
si = new_SMARTinfo();
memcpy(si->devname, devname, HDD_DEVNAME_SIZE);
err = sk_disk_smart_get_temperature(d, &mkelvin);
if (err < 0)
{
slogi("%s: SMART: temperature is not available", device);
}
else
{
si->temp = to_celsius(mkelvin);
si->temp_valid = true;
}
err = sk_disk_get_size(d, &size_B);
if (err < 0)
{
slogi("%s: SMART: size is not available", device);
}
else
{
si->size_GB = (unsigned int)(size_B >> 30);
si->size_valid = true;
}
dlist_push_back(hdd_list, si);
gettemp_out1:
sk_disk_free(d);
gettemp_out0:
return 1;
}
void hdd_get_temperature(DList **sd)
{
if (smart_devices == NULL)
hdd_info_init();
scan_dirs(SYS_BLOCK_PATH, atasmart_get_info, smart_devices);
*sd = smart_devices;
}