Don't put unit with hddtemp values for consistency (bug 2133107)
[monky] / src / hddtemp.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  * $Id$ */
27
28 #include "conky.h"
29 #include <errno.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <netdb.h>
33 #include <sys/select.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36
37 #define BUFLEN 512
38 #define PORT 7634
39
40 char buf[BUFLEN];
41
42 int scan_hddtemp(const char *arg, char **dev, char **addr, int *port, char** temp)
43 {
44         char buf1[32], buf2[64];
45         int n, ret;
46
47         ret = sscanf(arg, "%31s %63s %d", buf1, buf2, &n);
48
49         if (ret < 1) {
50                 return -1;
51         }
52
53         if (strncmp(buf1, "/dev/", 5)) {
54                 strncpy(buf1 + 5, buf1, 32 - 5);
55                 strncpy(buf1, "/dev/", 5);
56         }
57         *dev = strndup(buf1, text_buffer_size);
58
59         if (ret >= 2) {
60                 *addr = strndup(buf2, text_buffer_size);
61         } else {
62                 *addr = strndup("127.0.0.1", text_buffer_size);
63         }
64
65         if (ret == 3) {
66                 *port = n;
67         } else {
68                 *port = PORT;
69         }
70
71         *temp = malloc(text_buffer_size);
72         memset(*temp, 0, text_buffer_size);
73
74         return 0;
75 }
76
77 char *get_hddtemp_info(char *dev, char *hostaddr, int port/*, char *unit*/)
78 {
79         int sockfd = 0;
80         struct hostent he, *he_res = 0;
81         int he_errno;
82         char hostbuff[2048];
83         struct sockaddr_in addr;
84         struct timeval tv;
85         fd_set rfds;
86         int len, i, devlen = strlen(dev);
87         char sep;
88         char *p, *out, *r = NULL;
89
90         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
91                 perror("socket");
92                 return NULL;
93         }
94
95         do {
96 #ifdef HAVE_GETHOSTBYNAME_R
97                 if (gethostbyname_r(hostaddr, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) {   // get the host info
98                         ERR("hddtemp gethostbyname_r: %s", hstrerror(h_errno));
99                         break;
100                 }
101 #else /* HAVE_GETHOSTBYNAME_R */
102                 he_res = gethostbyname(hostaddr);
103                 if (!he_res) {
104                         perror("gethostbyname");
105                         break;
106                 }
107 #endif /* HAVE_GETHOSTBYNAME_R */
108
109                 addr.sin_family = AF_INET;
110                 addr.sin_port = htons(port);
111                 addr.sin_addr = *((struct in_addr *) he_res->h_addr);
112                 memset(&(addr.sin_zero), 0, 8);
113
114                 if (connect(sockfd, (struct sockaddr *) &addr,
115                                         sizeof(struct sockaddr)) == -1) {
116                         perror("connect");
117                         break;
118                 }
119
120                 FD_ZERO(&rfds);
121                 FD_SET(sockfd, &rfds);
122
123                 /* We're going to wait up to a half second to see whether there's any
124                  * data available. Polling with timeout set to 0 doesn't seem to work
125                  * with hddtemp.
126                  */
127                 tv.tv_sec = 0;
128                 tv.tv_usec = 500000;
129
130                 i = select(sockfd + 1, &rfds, NULL, NULL, &tv);
131                 if (i == -1) {
132                         if (errno == EINTR) {   /* silently ignore interrupted system call */
133                                 break;
134                         } else {
135                                 perror("select");
136                         }
137                 }
138
139                 /* No data available */
140                 if (i <= 0) {
141                         ERR("hddtemp had nothing for us");
142                         break;
143                 }
144
145                 p = buf;
146                 len = 0;
147                 do {
148                         i = recv(sockfd, p, BUFLEN - (p - buf), 0);
149                         if (i < 0) {
150                                 perror("recv");
151                                 break;
152                         }
153                         len += i;
154                         p += i;
155                 } while (i > 0 && p < buf + BUFLEN - 1);
156
157                 if (len < 2) {
158                         ERR("hddtemp returned nada");
159                         break;
160                 }
161
162                 buf[len] = 0;
163 //              printf("read: '%s'\n", buf);
164
165                 /* The first character read is the separator. */
166                 sep = buf[0];
167                 p = buf + 1;
168
169                 while (*p) {
170                         if (!strncmp(p, dev, devlen)) {
171                                 p += devlen + 1;
172                                 p = strchr(p, sep);
173                                 if (!p) {
174                                         break;
175                                 }
176                                 p++;
177                                 out = p;
178                                 p = strchr(p, sep);
179                                 if (!p) {
180                                         break;
181                                 }
182                                 *p = '\0';
183                                 p++;
184 //                              *unit = *p;
185                                 if (!strncmp(out, "NA", 2)) {
186                                         strncpy(buf, "N/A", BUFLEN);
187                                         r = buf;
188                                 } else {
189                                         r = out;
190                                 }
191                                 break;
192                         } else {
193                                 for (i = 0; i < 5; i++) {
194                                         p = strchr(p, sep);
195                                         if (!p) {
196                                                 break;
197                                         }
198                                         p++;
199                                 }
200                                 if (!p && i < 5) {
201                                         break;
202                                 }
203                         }
204                 }
205         } while (0);
206         close(sockfd);
207 //      printf("got: '%s'\n", r);
208         return r;
209 }