]> git.sur5r.net Git - i3/i3status/blob - src/print_cpu_temperature.c
fix: use SYSCONFDIR in error message
[i3/i3status] / src / print_cpu_temperature.c
1 // vim:ts=4:sw=4:expandtab
2 #include <config.h>
3 #include <stdlib.h>
4 #include <limits.h>
5 #include <glob.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <yajl/yajl_gen.h>
9 #include <yajl/yajl_version.h>
10
11 #include "i3status.h"
12
13 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
14 #include <err.h>
15 #include <sys/types.h>
16 #include <sys/sysctl.h>
17 #define TZ_ZEROC 2731
18 #define TZ_KELVTOC(x) (((x)-TZ_ZEROC) / 10), abs(((x)-TZ_ZEROC) % 10)
19 #define TZ_AVG(x) ((x)-TZ_ZEROC) / 10
20 #endif
21
22 #if defined(__DragonFly__)
23 #include <sys/sysctl.h>
24 #include <sys/types.h>
25 #include <sys/sensors.h>
26 #define MUKTOC(v) ((v - 273150000) / 1000000.0)
27 #endif
28
29 #if defined(__OpenBSD__)
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/sysctl.h>
33 #include <sys/sensors.h>
34 #include <errno.h>
35 #include <err.h>
36
37 #define MUKTOC(v) ((v - 273150000) / 1000000.0)
38 #endif
39
40 #if defined(__NetBSD__)
41 #include <fcntl.h>
42 #include <prop/proplib.h>
43 #include <sys/envsys.h>
44
45 #define MUKTOC(v) ((v - 273150000) / 1000000.0)
46 #endif
47
48 typedef struct temperature_s {
49     double raw_value;
50     char formatted_value[20];
51 } temperature_t;
52
53 #define ERROR_CODE 1
54
55 static int read_temperature(char *thermal_zone, temperature_t *temperature) {
56 #if defined(__linux__)
57     static char buf[16];
58     long int temp;
59
60     if (!slurp(thermal_zone, buf, sizeof(buf)))
61         return ERROR_CODE;
62
63     temp = strtol(buf, NULL, 10);
64     temperature->raw_value = temp / 1000;
65
66     if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
67         strcpy(temperature->formatted_value, "?");
68     else
69         sprintf(temperature->formatted_value, "%ld", (temp / 1000));
70
71 #elif defined(__DragonFly__)
72     struct sensor th_sensor;
73     size_t th_sensorlen;
74
75     th_sensorlen = sizeof(th_sensor);
76
77     if (sysctlbyname(thermal_zone, &th_sensor, &th_sensorlen, NULL, 0) == -1) {
78         perror("sysctlbyname");
79         return ERROR_CODE;
80     }
81
82     temperature->raw_value = MUKTOC(th_sensor.value);
83     sprintf(temperature->formatted_value, "%.2f", MUKTOC(th_sensor.value));
84
85 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
86     int sysctl_rslt;
87     size_t sysctl_size = sizeof(sysctl_rslt);
88
89     if (sysctlbyname(thermal_zone, &sysctl_rslt, &sysctl_size, NULL, 0))
90         return ERROR_CODE;
91
92     temperature->raw_value = TZ_AVG(sysctl_rslt);
93     sprintf(temperature->formatted_value, "%d.%d", TZ_KELVTOC(sysctl_rslt));
94
95 #elif defined(__OpenBSD__)
96     struct sensordev sensordev;
97     struct sensor sensor;
98     size_t sdlen, slen;
99     int dev, numt, mib[5] = {CTL_HW, HW_SENSORS, 0, 0, 0};
100
101     sdlen = sizeof(sensordev);
102     slen = sizeof(sensor);
103
104     for (dev = 0;; dev++) {
105         mib[2] = dev;
106         if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
107             if (errno == ENXIO)
108                 continue;
109             if (errno == ENOENT)
110                 break;
111             return ERROR_CODE;
112         }
113         /* 'path' is the node within the full path (defaults to acpitz0). */
114         if (BEGINS_WITH(sensordev.xname, thermal_zone)) {
115             mib[3] = SENSOR_TEMP;
116             /* Limit to temp0, but should retrieve from a full path... */
117             for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) {
118                 mib[4] = numt;
119                 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
120                     if (errno != ENOENT) {
121                         warn("sysctl");
122                         continue;
123                     }
124                 }
125                 temperature->raw_value = MUKTOC(sensor.value);
126                 sprintf(temperature->formatted_value, "%.2f", MUKTOC(sensor.value));
127             }
128         }
129     }
130 #elif defined(__NetBSD__)
131     int fd, rval;
132     bool err = false;
133     prop_dictionary_t dict;
134     prop_array_t array;
135     prop_object_iterator_t iter;
136     prop_object_iterator_t iter2;
137     prop_object_t obj, obj2, obj3;
138
139     fd = open("/dev/sysmon", O_RDONLY);
140     if (fd == -1)
141         return ERROR_CODE;
142
143     rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
144     if (rval == -1) {
145         err = true;
146         goto error_netbsd1;
147     }
148
149     /* No drivers registered? */
150     if (prop_dictionary_count(dict) == 0) {
151         err = true;
152         goto error_netbsd2;
153     }
154
155     iter = prop_dictionary_iterator(dict);
156     if (iter == NULL) {
157         err = true;
158         goto error_netbsd2;
159     }
160
161     /* iterate over the dictionary returned by the kernel */
162     while ((obj = prop_object_iterator_next(iter)) != NULL) {
163         /* skip this dict if it's not what we're looking for */
164         if ((strlen(prop_dictionary_keysym_cstring_nocopy(obj)) != strlen(thermal_zone)) ||
165             (strncmp(thermal_zone,
166                      prop_dictionary_keysym_cstring_nocopy(obj),
167                      strlen(thermal_zone)) != 0))
168             continue;
169
170         array = prop_dictionary_get_keysym(dict, obj);
171         if (prop_object_type(array) != PROP_TYPE_ARRAY) {
172             err = true;
173             goto error_netbsd3;
174         }
175
176         iter2 = prop_array_iterator(array);
177         if (!iter2) {
178             err = true;
179             goto error_netbsd3;
180         }
181
182         /* iterate over array of dicts specific to target sensor */
183         while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
184             obj3 = prop_dictionary_get(obj2, "cur-value");
185
186             float temp = MUKTOC(prop_number_integer_value(obj3));
187             temperature->raw_value = temp;
188             sprintf(temperature->formatted_value, "%.2f", temp);
189
190             break;
191         }
192         prop_object_iterator_release(iter2);
193     }
194 error_netbsd3:
195     prop_object_iterator_release(iter);
196 error_netbsd2:
197     prop_object_release(dict);
198 error_netbsd1:
199     close(fd);
200     if (err)
201         return ERROR_CODE;
202
203 #endif
204     return 0;
205 }
206
207 /*
208  * Reads the CPU temperature from /sys/class/thermal/thermal_zone%d/temp (or
209  * the user provided path) and returns the temperature in degree celsius.
210  *
211  */
212 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, const char *format_above_threshold, int max_threshold) {
213     char *outwalk = buffer;
214 #ifdef THERMAL_ZONE
215     const char *selected_format = format;
216     const char *walk;
217     bool colorful_output = false;
218     char *thermal_zone;
219     temperature_t temperature;
220     temperature.raw_value = 0;
221     sprintf(temperature.formatted_value, "%.2f", 0.0);
222
223     if (path == NULL)
224         asprintf(&thermal_zone, THERMAL_ZONE, zone);
225     else {
226         static glob_t globbuf;
227         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) != 0)
228             die("glob() failed\n");
229         if (globbuf.gl_pathc == 0) {
230             /* No glob matches, the specified path does not contain a wildcard. */
231             asprintf(&thermal_zone, path, zone);
232         } else {
233             /* glob matched, we take the first match and ignore the others */
234             asprintf(&thermal_zone, "%s", globbuf.gl_pathv[0]);
235         }
236         globfree(&globbuf);
237     }
238
239     INSTANCE(thermal_zone);
240
241     if (read_temperature(thermal_zone, &temperature) != 0)
242         goto error;
243
244     if (temperature.raw_value >= max_threshold) {
245         START_COLOR("color_bad");
246         colorful_output = true;
247         if (format_above_threshold != NULL)
248             selected_format = format_above_threshold;
249     }
250
251     for (walk = selected_format; *walk != '\0'; walk++) {
252         if (*walk != '%') {
253             *(outwalk++) = *walk;
254
255         } else if (BEGINS_WITH(walk + 1, "degrees")) {
256             outwalk += sprintf(outwalk, "%s", temperature.formatted_value);
257             walk += strlen("degrees");
258
259         } else {
260             *(outwalk++) = '%';
261         }
262     }
263
264     if (colorful_output) {
265         END_COLOR;
266         colorful_output = false;
267     }
268
269     free(thermal_zone);
270
271     OUTPUT_FULL_TEXT(buffer);
272     return;
273 error:
274     free(thermal_zone);
275 #endif
276
277     OUTPUT_FULL_TEXT("can't read temp");
278     (void)fputs("i3status: Cannot read temperature. Verify that you have a thermal zone in /sys/class/thermal or disable the cpu_temperature module in your i3status config.\n", stderr);
279 }