]> git.sur5r.net Git - i3/i3status/blob - src/print_cpu_temperature.c
print_cpu_temperature: fix memory leak (Thanks kuba)
[i3/i3status] / src / print_cpu_temperature.c
1 // vim:ts=8:expandtab
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
8
9 #include "i3status.h"
10
11 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
12 #include <err.h>
13 #include <sys/types.h>
14 #include <sys/sysctl.h>
15 #define TZ_ZEROC 2732
16 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
17 #define TZ_AVG(x) ((x) - TZ_ZEROC) / 10
18 #endif
19
20 #if defined(__OpenBSD__)
21 #include <sys/param.h>
22 #include <sys/types.h>
23 #include <sys/sysctl.h>
24 #include <sys/sensors.h>
25 #include <errno.h>
26 #include <err.h>
27
28 #define MUKTOC(v) ((v - 273150000) / 1000000.0)
29 #endif
30
31 #if defined(__NetBSD__)
32 #include <fcntl.h>
33 #include <prop/proplib.h>
34 #include <sys/envsys.h>
35
36 #define MUKTOC(v) ((v - 273150000) / 1000000.0)
37 #endif
38
39
40 static char *thermal_zone;
41
42 /*
43  * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
44  * returns the temperature in degree celcius.
45  *
46  */
47 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int max_threshold) {
48         char *outwalk = buffer;
49 #ifdef THERMAL_ZONE
50         const char *walk;
51         bool colorful_output = false;
52
53         if (thermal_zone == NULL) {
54                 if (path == NULL)
55                         asprintf(&thermal_zone, THERMAL_ZONE, zone);
56                 else
57                         asprintf(&thermal_zone, path, zone);
58         }
59         path = thermal_zone;
60
61         INSTANCE(path);
62
63         for (walk = format; *walk != '\0'; walk++) {
64                 if (*walk != '%') {
65                         *(outwalk++) = *walk;
66                         continue;
67                 }
68
69                 if (BEGINS_WITH(walk+1, "degrees")) {
70 #if defined(LINUX)
71                         static char buf[16];
72                         long int temp;
73                         if (!slurp(path, buf, sizeof(buf)))
74                                 goto error;
75                         temp = strtol(buf, NULL, 10);
76                         if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
77                                 *(outwalk++) = '?';
78                         else {
79                                 if ((temp/1000) >= max_threshold) {
80                                         START_COLOR("color_bad");
81                                         colorful_output = true;
82                                 }
83                                 outwalk += sprintf(outwalk, "%ld", (temp/1000));
84                                 if (colorful_output) {
85                                         END_COLOR;
86                                         colorful_output = false;
87                                 }
88                         }
89 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
90                         int sysctl_rslt;
91                         size_t sysctl_size = sizeof(sysctl_rslt);
92                         if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0))
93                                 goto error;
94
95                         if (TZ_AVG(sysctl_rslt) >= max_threshold) {
96                                 START_COLOR("color_bad");
97                                 colorful_output = true;
98                         }
99                         outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt));
100                         if (colorful_output) {
101                                 END_COLOR;
102                                 colorful_output = false;
103                         }
104
105 #elif defined(__OpenBSD__)
106         struct sensordev sensordev;
107         struct sensor sensor;
108         size_t sdlen, slen;
109         int dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
110
111         sdlen = sizeof(sensordev);
112         slen = sizeof(sensor);
113
114         for (dev = 0; ; dev++) {
115                 mib[2] = dev;
116                 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
117                         if (errno == ENXIO)
118                                 continue;
119                         if (errno == ENOENT)
120                                 break;
121                         goto error;
122                 }
123                 /* 'path' is the node within the full path (defaults to acpitz0). */
124                 if (strncmp(sensordev.xname, path, strlen(path)) == 0) {
125                         mib[3] = SENSOR_TEMP;
126                         /* Limit to temo0, but should retrieve from a full path... */
127                         for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) {
128                                 mib[4] = numt;
129                                 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
130                                         if (errno != ENOENT) {
131                                                 warn("sysctl");
132                                                 continue;
133                                         }
134                                 }
135                                 if ((int)MUKTOC(sensor.value) >= max_threshold) {
136                                         START_COLOR("color_bad");
137                                         colorful_output = true;
138                                 }
139
140                                 outwalk += sprintf(outwalk, "%.2f", MUKTOC(sensor.value));
141
142                                 if (colorful_output) {
143                                         END_COLOR;
144                                         colorful_output = false;
145                                 }
146                         }
147                 }
148         }
149 #elif defined(__NetBSD__)
150         int fd, rval;
151         bool err = false;
152         prop_dictionary_t dict;
153         prop_array_t array;
154         prop_object_iterator_t iter;
155         prop_object_iterator_t iter2;
156         prop_object_t obj, obj2, obj3;
157
158         fd = open("/dev/sysmon", O_RDONLY);
159         if (fd == -1)
160                 goto error;
161
162         rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
163         if (rval == -1) {
164             err = true;
165             goto error_netbsd1;
166         }
167
168         /* No drivers registered? */
169         if (prop_dictionary_count(dict) == 0) {
170             err = true;
171             goto error_netbsd2;
172         }
173
174         /* print sensors for all devices registered */
175         iter = prop_dictionary_iterator(dict);
176         if (iter == NULL) {
177             err = true;
178             goto error_netbsd2;
179         }
180
181         /* iterate over the dictionary returned by the kernel */
182         while ((obj = prop_object_iterator_next(iter)) != NULL) {
183                 array = prop_dictionary_get_keysym(dict, obj);
184                 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
185                     err = true;
186                     goto error_netbsd3;
187                 }
188                 iter2 = prop_array_iterator(array);
189                 if (!iter2) {
190                     err = true;
191                     goto error_netbsd3;
192                 }
193
194                 /* iterate over the array of dictionaries */
195                 while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
196                         obj3 = prop_dictionary_get(obj2, "description");
197                         if (obj3 &&
198                             strcmp(path, prop_string_cstring_nocopy(obj3)) == 0)
199                         {
200                                 obj3 = prop_dictionary_get(obj2, "cur-value");
201                                 float temp = MUKTOC(prop_number_integer_value(obj3));
202                                 if ((int)temp >= max_threshold) {
203                                         START_COLOR("color_bad");
204                                         colorful_output = true;
205                                 }
206
207                                 outwalk += sprintf(outwalk, "%.2f", temp);
208
209                                 if (colorful_output) {
210                                         END_COLOR;
211                                         colorful_output = false;
212                                 }
213                                 break;
214                         }
215
216                 }
217                 prop_object_iterator_release(iter2);
218         }
219 error_netbsd3:
220         prop_object_iterator_release(iter);
221 error_netbsd2:
222         prop_object_release(dict);
223 error_netbsd1:
224         close(fd);
225         if (err) goto error;
226
227 #endif
228
229
230                         walk += strlen("degrees");
231                 }
232         }
233         OUTPUT_FULL_TEXT(buffer);
234         return;
235 error:
236 #endif
237         OUTPUT_FULL_TEXT("cant read temp");
238         (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);
239 }