Use getaddrinfo instead of gethostbyname
[monky] / src / apcupsd.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * apcupsd.c:  conky module for APC UPS daemon monitoring
5  *
6  * Copyright (C) 2009 Jaromir Smrcek <jaromir.smrcek@zoner.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
21  * USA.
22  *
23  */
24
25 #include "conky.h"
26 #include "apcupsd.h"
27 #include "logging.h"
28
29 #include <errno.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34
35 //
36 // encapsulated recv()
37 //
38 static int net_recv_ex(int sock, void *buf, int size, struct timeval *tv)
39 {
40         
41         fd_set  fds;
42         int             res;
43
44         // wait for some data to be read
45         do {
46                 errno = 0;
47                 FD_ZERO(&fds);
48                 FD_SET(sock, &fds);
49                 res = select(sock + 1, &fds, NULL, NULL, tv);
50         } while (res < 0 && errno == EINTR);
51         if (res < 0) return 0;
52         if (res == 0) {
53                 // timeout
54                 errno = ETIMEDOUT;  // select was succesfull, errno is now 0
55                 return 0;
56         }
57
58         // socket ready, read the data
59         do {
60                 errno = 0;
61                 res = recv(sock, (char*)buf, size, 0);
62         } while (res < 0 && errno == EINTR);
63         if (res < 0) return 0;
64         if (res == 0) {
65                 // orderly shutdown
66                 errno = ENOTCONN;
67                 return 0;
68         }
69
70         return res;
71 }
72
73 //
74 // read whole buffer or fail
75 //
76 static int net_recv(int sock, void* buf, int size) {
77
78         int todo = size;
79         int off = 0;
80         int len;
81         struct timeval tv = { 0, 250000 };
82
83         while (todo) {
84                 len = net_recv_ex(sock, (char*)buf + off, todo, &tv);
85                 if (!len) return 0;
86                 todo -= len;
87                 off  += len;
88         }
89         return 1;
90 }
91
92 //
93 // get one response line
94 //
95 static int get_line(int sock, char line[], short linesize) {
96
97         // get the line length
98         short sz;
99         if (!net_recv(sock, &sz, sizeof(sz))) return -1;
100         sz = ntohs(sz);
101         if (!sz) return 0;
102
103         // get the line
104         while (sz > linesize) {
105                 // this is just a hack (being lazy), this should not happen anyway
106                 net_recv(sock, line, linesize);
107                 sz -= sizeof(line);
108         }
109         if (!net_recv(sock, line, sz)) return 0;
110         line[sz] = 0;
111         return sz;
112 }
113
114 #define FILL(NAME,FIELD,FIRST)                                                                                                          \
115         if (!strncmp(NAME, line, sizeof(NAME)-1)) {                                                                             \
116                 strncpy(apc->items[FIELD], line+11, APCUPSD_MAXSTR);                                            \
117                 /* remove trailing newline and assure termination */                                            \
118                 apc->items[FIELD][len-11 > APCUPSD_MAXSTR ? APCUPSD_MAXSTR : len-12] = 0;       \
119                 if (FIRST) {                                                                                                                            \
120                         char* c;                                                                                                                                \
121                         for (c = apc->items[FIELD]; *c; ++c)                                                                    \
122                                 if (*c == ' ' && c > apc->items[FIELD]+2) {                                                     \
123                                         *c = 0;                                                                                                                 \
124                                         break;                                                                                                                  \
125                                 }                                                                                                                                       \
126                 }                                                                                                                                                       \
127         }
128
129 //
130 // fills in the data received from a socket
131 //
132 static int fill_items(int sock, PAPCUPSD_S apc) {
133
134         char line[512];
135         int len;
136         while ((len = get_line(sock, line, sizeof(line)))) {
137                 // fill the right types in
138                 FILL("UPSNAME",         APCUPSD_NAME,           FALSE);
139                 FILL("MODEL",           APCUPSD_MODEL,          FALSE);
140                 FILL("UPSMODE",         APCUPSD_UPSMODE,        FALSE);
141                 FILL("CABLE",           APCUPSD_CABLE,          FALSE);
142                 FILL("STATUS",          APCUPSD_STATUS,         FALSE);
143                 FILL("LINEV",           APCUPSD_LINEV,          TRUE);
144                 FILL("LOADPCT",         APCUPSD_LOAD,           TRUE);
145                 FILL("BCHARGE",         APCUPSD_CHARGE,         TRUE);
146                 FILL("TIMELEFT",        APCUPSD_TIMELEFT,       TRUE);
147                 FILL("ITEMP",           APCUPSD_TEMP,           TRUE);
148                 FILL("LASTXFER",        APCUPSD_LASTXFER,       FALSE);
149         }
150         
151         return len == 0;
152 }
153
154 //
155 // Conky update function for apcupsd data
156 //
157 int update_apcupsd(void) {
158
159         int i;
160         APCUPSD_S apc;
161         int sock;
162
163         for (i = 0; i < _APCUPSD_COUNT; ++i)
164                 memcpy(apc.items[i], "N/A", 4); // including \0
165
166         do {
167                 struct addrinfo hints;
168                 struct addrinfo *ai, *rp;
169                 int res;
170                 short sz = 0;
171                 char portbuf[8];
172                 //
173                 // connect to apcupsd daemon
174                 //
175                 memset(&hints, 0, sizeof(struct addrinfo));
176                 hints.ai_family = AF_UNSPEC;
177                 hints.ai_socktype = SOCK_STREAM;
178                 hints.ai_flags = 0;
179                 hints.ai_protocol = 0;
180                 snprintf(portbuf, 8, "%d", info.apcupsd.port);
181                 res = getaddrinfo(info.apcupsd.host, portbuf, &hints, &ai);
182                 if (res != 0) {
183                         NORM_ERR("APCUPSD getaddrinfo: %s", gai_strerror(res));
184                         break;
185                 }
186                 for (rp = ai; rp != NULL; rp = rp->ai_next) {
187                         sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
188                         if (sock == -1) {
189                                 continue;
190                         }
191                         if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) {
192                                 break;
193                         }
194                         close(sock);
195                 }
196                 freeaddrinfo(ai);
197                 if (rp == NULL) {
198                         // no error reporting, the daemon is probably not running
199                         break;
200                 }
201
202                 //
203                 // send status request - "status" - 6B
204                 //
205                 sz = htons(6);
206                 // no waiting to become writeable is really needed
207                 if (send(sock, &sz, sizeof(sz), 0) != sizeof(sz) || send(sock, "status", 6, 0) != 6) {
208                         perror("send");
209                         break;
210                 }
211         
212                 //
213                 // read the lines of output and put them into the info structure
214                 //
215                 if (!fill_items(sock, &apc)) break;
216
217         } while (0);
218
219         close(sock);
220
221         //
222         // "atomically" copy the data into working set
223         //
224         memcpy(info.apcupsd.items, apc.items, sizeof(info.apcupsd.items));
225         return 0;
226 }