doc cleanup
[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         *dev = strdup(buf1);
57         if (ret >= 2) {
58                 *addr = strdup(buf2);
59         } else {
60                 *addr = strdup("127.0.0.1");
61         }
62
63         if (ret == 3) {
64                 *port = n;
65         } else {
66                 *port = PORT;
67         }
68
69         return 0;
70 }
71
72 char *get_hddtemp_info(char *dev, char *hostaddr, int port, char *unit)
73 {
74         int sockfd = 0;
75         struct hostent *he;
76         struct sockaddr_in addr;
77         struct timeval tv;
78         fd_set rfds;
79         int len, i, devlen = strlen(dev);
80         char sep;
81         char *p, *out, *r = NULL;
82
83         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
84                 perror("socket");
85                 return NULL;
86         }
87
88         he = gethostbyname(hostaddr);
89         if (!he) {
90                 perror("gethostbyname");
91                 goto out;
92         }
93
94         addr.sin_family = AF_INET;
95         addr.sin_port = htons(port);
96         addr.sin_addr = *((struct in_addr *) he->h_addr);
97         memset(&(addr.sin_zero), 0, 8);
98
99         if (connect(sockfd, (struct sockaddr *) &addr,
100                         sizeof(struct sockaddr)) == -1) {
101                 perror("connect");
102                 goto out;
103         }
104
105         FD_ZERO(&rfds);
106         FD_SET(sockfd, &rfds);
107
108         /* We're going to wait up to a quarter a second to see whether there's
109          * any data available. Polling with timeout set to 0 doesn't seem to work
110          * with hddtemp. */
111         tv.tv_sec = 0;
112         tv.tv_usec = 250000;
113
114         i = select(sockfd + 1, &rfds, NULL, NULL, &tv);
115         if (i == -1) {
116                 if (errno == EINTR) {   /* silently ignore interrupted system call */
117                         goto out;
118                 } else {
119                         perror("select");
120                 }
121         }
122
123         /* No data available */
124         if (i <= 0) {
125                 goto out;
126         }
127
128         p = buf;
129         len = 0;
130         do {
131                 i = recv(sockfd, p, BUFLEN - (p - buf), 0);
132                 if (i < 0) {
133                         perror("recv");
134                         goto out;
135                 }
136                 len += i;
137                 p += i;
138         } while (i > 0 && p < buf + BUFLEN - 1);
139
140         if (len < 2) {
141                 goto out;
142         }
143
144         buf[len] = 0;
145
146         /* The first character read is the separator. */
147         sep = buf[0];
148         p = buf + 1;
149
150         while (*p) {
151                 if (!strncmp(p, dev, devlen)) {
152                         p += devlen + 1;
153                         p = strchr(p, sep);
154                         if (!p) {
155                                 goto out;
156                         }
157                         p++;
158                         out = p;
159                         p = strchr(p, sep);
160                         if (!p) {
161                                 goto out;
162                         }
163                         *p = '\0';
164                         p++;
165                         *unit = *p;
166                         if (!strncmp(out, "NA", 2)) {
167                                 strcpy(buf, "N/A");
168                                 r = buf;
169                         } else {
170                                 r = out;
171                         }
172                         goto out;
173                 } else {
174                         for (i = 0; i < 5; i++) {
175                                 p = strchr(p, sep);
176                                 if (!p) {
177                                         goto out;
178                                 }
179                                 p++;
180                         }
181                 }
182         }
183 out:
184         close(sockfd);
185         return r;
186 }