make specifying hdd names more flexible
[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 <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <netdb.h>
36 #include <sys/select.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39
40 #define BUFLEN 512
41 #define PORT 7634
42
43 char buf[BUFLEN];
44
45 int scan_hddtemp(const char *arg, char **dev, char **addr, int *port)
46 {
47         char buf1[32], buf2[64];
48         int n, ret;
49
50         ret = sscanf(arg, "%31s %63s %d", buf1, buf2, &n);
51
52         if (ret < 1) {
53                 return -1;
54         }
55
56         if (strncmp(buf1, "/dev/", 5)) {
57                 strncpy(buf1 + 5, buf1, 32 - 5);
58                 strncpy(buf1, "/dev/", 5);
59         }
60         *dev = strdup(buf1);
61
62         if (ret >= 2) {
63                 *addr = strdup(buf2);
64         } else {
65                 *addr = strdup("127.0.0.1");
66         }
67
68         if (ret == 3) {
69                 *port = n;
70         } else {
71                 *port = PORT;
72         }
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;
81         struct sockaddr_in addr;
82         struct timeval tv;
83         fd_set rfds;
84         int len, i, devlen = strlen(dev);
85         char sep;
86         char *p, *out, *r = NULL;
87
88         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
89                 perror("socket");
90                 return NULL;
91         }
92
93         he = gethostbyname(hostaddr);
94         if (!he) {
95                 perror("gethostbyname");
96                 goto out;
97         }
98
99         addr.sin_family = AF_INET;
100         addr.sin_port = htons(port);
101         addr.sin_addr = *((struct in_addr *) he->h_addr);
102         memset(&(addr.sin_zero), 0, 8);
103
104         if (connect(sockfd, (struct sockaddr *) &addr,
105                         sizeof(struct sockaddr)) == -1) {
106                 perror("connect");
107                 goto out;
108         }
109
110         FD_ZERO(&rfds);
111         FD_SET(sockfd, &rfds);
112
113         /* We're going to wait up to a quarter a second to see whether there's
114          * any data available. Polling with timeout set to 0 doesn't seem to work
115          * with hddtemp. */
116         tv.tv_sec = 0;
117         tv.tv_usec = 250000;
118
119         i = select(sockfd + 1, &rfds, NULL, NULL, &tv);
120         if (i == -1) {
121                 if (errno == EINTR) {   /* silently ignore interrupted system call */
122                         goto out;
123                 } else {
124                         perror("select");
125                 }
126         }
127
128         /* No data available */
129         if (i <= 0) {
130                 goto out;
131         }
132
133         p = buf;
134         len = 0;
135         do {
136                 i = recv(sockfd, p, BUFLEN - (p - buf), 0);
137                 if (i < 0) {
138                         perror("recv");
139                         goto out;
140                 }
141                 len += i;
142                 p += i;
143         } while (i > 0 && p < buf + BUFLEN - 1);
144
145         if (len < 2) {
146                 goto out;
147         }
148
149         buf[len] = 0;
150
151         /* The first character read is the separator. */
152         sep = buf[0];
153         p = buf + 1;
154
155         while (*p) {
156                 if (!strncmp(p, dev, devlen)) {
157                         p += devlen + 1;
158                         p = strchr(p, sep);
159                         if (!p) {
160                                 goto out;
161                         }
162                         p++;
163                         out = p;
164                         p = strchr(p, sep);
165                         if (!p) {
166                                 goto out;
167                         }
168                         *p = '\0';
169                         p++;
170                         *unit = *p;
171                         if (!strncmp(out, "NA", 2)) {
172                                 strcpy(buf, "N/A");
173                                 r = buf;
174                         } else {
175                                 r = out;
176                         }
177                         goto out;
178                 } else {
179                         for (i = 0; i < 5; i++) {
180                                 p = strchr(p, sep);
181                                 if (!p) {
182                                         goto out;
183                                 }
184                                 p++;
185                         }
186                 }
187         }
188 out:
189         close(sockfd);
190         return r;
191 }