1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
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.
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/>.
34 #include <sys/select.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
43 int scan_hddtemp(const char *arg, char **dev, char **addr, int *port)
45 char buf1[32], buf2[64];
51 if ((ret = sscanf(arg, "%31s %63s %d", buf1, buf2, &n)) < 1)
54 if (strncmp(buf1, "/dev/", 5)) {
55 strncpy(buf1 + 5, buf1, 32 - 5);
56 strncpy(buf1, "/dev/", 5);
58 *dev = strndup(buf1, text_buffer_size);
61 *addr = strndup(buf2, text_buffer_size);
63 *addr = strndup("127.0.0.1", text_buffer_size);
75 /* this is an iterator:
76 * set line to NULL in consecutive calls to get the next field
77 * returns "<dev><unit><val>" or NULL on error
79 static char *read_hdd_val(const char *line)
81 static char line_s[512] = "\0";
83 char *dev, *val, unit;
87 snprintf(line_s, 512, "%s", line);
94 if (!(p = strchr(p, line_s[0])))
97 /* jump over the devname */
98 if (!(p = strchr(p, line_s[0])))
102 if (!(p = strchr(p, line_s[0])))
106 /* preset p for next call */
107 p = strchr(p + 1, line_s[0]);
109 if (dev && *dev && val && *val) {
110 asprintf(&ret, "%s%c%s", dev, unit, val);
115 /* returns <unit><val> or NULL on error or N/A */
116 char *get_hddtemp_info(char *dev, char *hostaddr, int port)
119 struct hostent he, *he_res = 0;
122 struct sockaddr_in addr;
128 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
133 #ifdef HAVE_GETHOSTBYNAME_R
134 if (gethostbyname_r(hostaddr, &he, hostbuff,
135 sizeof(hostbuff), &he_res, &he_errno)) {
136 ERR("hddtemp gethostbyname_r: %s", hstrerror(h_errno));
137 #else /* HAVE_GETHOSTBYNAME_R */
138 if (!(he_res = gethostbyname(hostaddr))) {
139 perror("gethostbyname()");
140 #endif /* HAVE_GETHOSTBYNAME_R */
144 addr.sin_family = AF_INET;
145 addr.sin_port = htons(port);
146 addr.sin_addr = *((struct in_addr *) he_res->h_addr);
147 memset(&(addr.sin_zero), 0, 8);
149 if (connect(sockfd, (struct sockaddr *) &addr,
150 sizeof(struct sockaddr)) == -1) {
156 FD_SET(sockfd, &rfds);
158 /* We're going to wait up to a half second to see whether there's any
159 * data available. Polling with timeout set to 0 doesn't seem to work
165 i = select(sockfd + 1, &rfds, NULL, NULL, &tv);
166 if (i == -1) { /* select() failed */
167 if (errno == EINTR) {
168 /* silently ignore interrupted system call */
173 } else if (i == 0) { /* select() timeouted */
174 ERR("hddtemp had nothing for us");
181 i = recv(sockfd, p, BUFLEN - (p - buf), 0);
188 } while (i > 0 && p < buf + BUFLEN - 1);
191 ERR("hddtemp returned nada");
197 if ((p = read_hdd_val(buf)) == NULL)
200 if (!strncmp(dev, p, strlen(dev)))
201 asprintf(&r, "%s", p + strlen(dev));
203 } while(!r && (p = read_hdd_val(NULL)) != NULL);