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