Fix segfault in i8k (sf.net #3007167)
[monky] / src / i8k.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2007 Toni Spets
14  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15  *      (see AUTHORS)
16  * All rights reserved.
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  * You should have received a copy of the GNU General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  *
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "text_object.h"
36 #include "temphelper.h"
37 #include "logging.h"
38
39 struct {
40         char *version;
41         char *bios;
42         char *serial;
43         char *cpu_temp;
44         char *left_fan_status;
45         char *right_fan_status;
46         char *left_fan_rpm;
47         char *right_fan_rpm;
48         char *ac_status;
49         char *buttons_status;
50 } i8k;
51
52 /* FIXME: there should be an ioctl interface to request specific data */
53 #define PROC_I8K "/proc/i8k"
54 #define I8K_DELIM " "
55 static char *i8k_procbuf = NULL;
56 int update_i8k(void)
57 {
58         FILE *fp;
59
60         if (!i8k_procbuf) {
61                 i8k_procbuf = (char *) malloc(128 * sizeof(char));
62         }
63         if ((fp = fopen(PROC_I8K, "r")) == NULL) {
64                 free(i8k_procbuf);
65                 i8k_procbuf = NULL;
66                 NORM_ERR("/proc/i8k doesn't exist! use insmod to make sure the kernel "
67                         "driver is loaded...");
68                 clean_up_without_threads(NULL, NULL);
69                 free(current_mail_spool);
70                 return 1;
71         }
72
73         memset(&i8k_procbuf[0], 0, 128);
74         if (fread(&i8k_procbuf[0], sizeof(char), 128, fp) == 0) {
75                 NORM_ERR("something wrong with /proc/i8k...");
76         }
77
78         fclose(fp);
79
80         DBG("read `%s' from /proc/i8k\n", i8k_procbuf);
81
82         i8k.version = strtok(&i8k_procbuf[0], I8K_DELIM);
83         i8k.bios = strtok(NULL, I8K_DELIM);
84         i8k.serial = strtok(NULL, I8K_DELIM);
85         i8k.cpu_temp = strtok(NULL, I8K_DELIM);
86         i8k.left_fan_status = strtok(NULL, I8K_DELIM);
87         i8k.right_fan_status = strtok(NULL, I8K_DELIM);
88         i8k.left_fan_rpm = strtok(NULL, I8K_DELIM);
89         i8k.right_fan_rpm = strtok(NULL, I8K_DELIM);
90         i8k.ac_status = strtok(NULL, I8K_DELIM);
91         i8k.buttons_status = strtok(NULL, I8K_DELIM);
92         return 0;
93 }
94
95 static void print_i8k_fan_status(char *p, int p_max_size, const char *status)
96 {
97         static char *status_arr[] = { "off", "low", "high", "error" };
98
99         int i = status ? atoi(status) : 3;
100         if(i < 0 || i > 3)
101                 i = 3;
102
103         snprintf(p, p_max_size, "%s", status_arr[i]);
104 }
105
106 void print_i8k_left_fan_status(struct text_object *obj, char *p, int p_max_size)
107 {
108         (void)obj;
109         print_i8k_fan_status(p, p_max_size, i8k.left_fan_status);
110 }
111
112 void print_i8k_cpu_temp(struct text_object *obj, char *p, int p_max_size)
113 {
114         int cpu_temp;
115
116         (void)obj;
117
118         sscanf(i8k.cpu_temp, "%d", &cpu_temp);
119         temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
120 }
121
122 void print_i8k_right_fan_status(struct text_object *obj, char *p, int p_max_size)
123 {
124         (void)obj;
125         print_i8k_fan_status(p, p_max_size, i8k.right_fan_status);
126 }
127
128 void print_i8k_ac_status(struct text_object *obj, char *p, int p_max_size)
129 {
130         int ac_status;
131
132         (void)obj;
133
134         sscanf(i8k.ac_status, "%d", &ac_status);
135         if (ac_status == -1) {
136                 snprintf(p, p_max_size, "disabled (read i8k docs)");
137         }
138         if (ac_status == 0) {
139                 snprintf(p, p_max_size, "off");
140         }
141         if (ac_status == 1) {
142                 snprintf(p, p_max_size, "on");
143         }
144 }
145
146 #define I8K_PRINT_GENERATOR(name) \
147 void print_i8k_##name(struct text_object *obj, char *p, int p_max_size) \
148 { \
149         (void)obj; \
150         snprintf(p, p_max_size, "%s", i8k.name); \
151 }
152
153 I8K_PRINT_GENERATOR(version)
154 I8K_PRINT_GENERATOR(bios)
155 I8K_PRINT_GENERATOR(serial)
156 I8K_PRINT_GENERATOR(left_fan_rpm)
157 I8K_PRINT_GENERATOR(right_fan_rpm)
158 I8K_PRINT_GENERATOR(buttons_status)
159
160 #undef I8K_PRINT_GENERATOR