File: | daemon.c |
Location: | line 82, column 14 |
Description: | Call to 'malloc' has an allocation size of 0 bytes |
1 | /* | |||
2 | * Copyright (C) 2002 Emmanuel VARAGNAT <hddtemp@guzu.net> | |||
3 | * | |||
4 | * This program is free software; you can redistribute it and/or modify | |||
5 | * it under the terms of the GNU General Public License as published by | |||
6 | * the Free Software Foundation; either version 2 of the License, or | |||
7 | * (at your option) any later version. | |||
8 | * | |||
9 | * This program is distributed in the hope that it will be useful, | |||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
12 | * GNU General Public License for more details. | |||
13 | * | |||
14 | * You should have received a copy of the GNU General Public License | |||
15 | * along with this program; if not, write to the Free Software | |||
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |||
17 | */ | |||
18 | ||||
19 | // Include file generated by ./configure | |||
20 | #ifdef HAVE_CONFIG_H1 | |||
21 | #include <config.h> | |||
22 | #endif | |||
23 | ||||
24 | // Gettext includes | |||
25 | #if ENABLE_NLS1 | |||
26 | #include <libintl.h> | |||
27 | #define _(String)dcgettext (((void*)0), String, 5) gettext (String)dcgettext (((void*)0), String, 5) | |||
28 | #else | |||
29 | #define _(String)dcgettext (((void*)0), String, 5) (String) | |||
30 | #endif | |||
31 | ||||
32 | // Standard includes | |||
33 | #include <unistd.h> | |||
34 | #include <stdlib.h> | |||
35 | #include <stdio.h> | |||
36 | #include <string.h> | |||
37 | #include <sys/ioctl.h> | |||
38 | #include <sys/types.h> | |||
39 | #include <sys/stat.h> | |||
40 | #include <sys/socket.h> | |||
41 | #include <arpa/inet.h> | |||
42 | #include <netdb.h> | |||
43 | #include <signal.h> | |||
44 | #include <netinet/in.h> | |||
45 | #include <syslog.h> | |||
46 | ||||
47 | // Application specific includes | |||
48 | #include "hddtemp.h" | |||
49 | ||||
50 | #define DELAY60.0 60.0 | |||
51 | ||||
52 | int sks_serv_num = 0; | |||
53 | int * sks_serv; | |||
54 | int stop_daemon = 0; | |||
55 | ||||
56 | /******************************************************* | |||
57 | *******************************************************/ | |||
58 | ||||
59 | void daemon_open_sockets(void) | |||
60 | { | |||
61 | struct addrinfo* all_ai; | |||
62 | struct addrinfo hints; | |||
63 | struct addrinfo* resp; | |||
64 | char portbuf[10]; | |||
65 | int on = 1; | |||
66 | int ret; | |||
67 | ||||
68 | memset(&hints, 0, sizeof hints); | |||
69 | hints.ai_family = af_hint; | |||
70 | hints.ai_socktype = SOCK_STREAMSOCK_STREAM; | |||
71 | hints.ai_flags = AI_PASSIVE0x0001; | |||
72 | snprintf(portbuf, sizeof(portbuf), "%ld", portnum); | |||
73 | ret = getaddrinfo(listen_addr, portbuf, &hints, &all_ai); | |||
74 | if (ret != 0) { | |||
75 | fprintf(stderrstderr, "getaddrinfo: %s\n", gai_strerror(ret)); | |||
76 | exit(1); | |||
77 | } | |||
78 | ||||
79 | /* Count max number of sockets we might open. */ | |||
80 | for (sks_serv_num = 0, resp = all_ai ; resp ; resp = resp->ai_next) | |||
81 | sks_serv_num++; | |||
82 | sks_serv = malloc(sizeof(int) * sks_serv_num); | |||
| ||||
83 | if (!sks_serv) { | |||
84 | perror("malloc"); | |||
85 | freeaddrinfo(all_ai); | |||
86 | exit(1); | |||
87 | } | |||
88 | ||||
89 | /* We may not be able to create the socket, if for example the | |||
90 | * machine knows about IPv6 in the C library, but not in the | |||
91 | * kernel. */ | |||
92 | for (sks_serv_num = 0, resp = all_ai; resp; resp = resp->ai_next) { | |||
93 | sks_serv[sks_serv_num] = socket(resp->ai_family, resp->ai_socktype, resp->ai_protocol); | |||
94 | if (sks_serv[sks_serv_num] == -1) | |||
95 | /* See if there's another address that will work... */ | |||
96 | continue; | |||
97 | ||||
98 | /* Allow local port reuse in TIME_WAIT */ | |||
99 | setsockopt(sks_serv[sks_serv_num], SOL_SOCKET1, SO_REUSEADDR2, &on, sizeof(on)); | |||
100 | ||||
101 | /* Allow binding to a listen address that doesn't exist yet */ | |||
102 | setsockopt(sks_serv[sks_serv_num], SOL_IP0, IP_FREEBIND15, &on, sizeof(on)); | |||
103 | ||||
104 | /* Now we've got a socket - we need to bind it. */ | |||
105 | if (bind(sks_serv[sks_serv_num], resp->ai_addr, resp->ai_addrlen) < 0) { | |||
106 | /* Nope, try another */ | |||
107 | close(sks_serv[sks_serv_num]); | |||
108 | continue; | |||
109 | } | |||
110 | ||||
111 | /* Ready to listen */ | |||
112 | if (listen(sks_serv[sks_serv_num], 5) == -1) { | |||
113 | perror("listen"); | |||
114 | for (sks_serv_num-- ; sks_serv_num > 0 ; sks_serv_num--) | |||
115 | close(sks_serv[sks_serv_num]); | |||
116 | freeaddrinfo(all_ai); | |||
117 | free(sks_serv); | |||
118 | exit(1); | |||
119 | } | |||
120 | ||||
121 | sks_serv_num++; | |||
122 | } | |||
123 | ||||
124 | if (sks_serv_num == 0) { | |||
125 | perror("socket"); | |||
126 | free(sks_serv); | |||
127 | freeaddrinfo(all_ai); | |||
128 | exit(1); | |||
129 | } | |||
130 | ||||
131 | freeaddrinfo(all_ai); | |||
132 | } | |||
133 | ||||
134 | void daemon_update(struct disk *ldisks, int nocache) { | |||
135 | struct disk * dsk; | |||
136 | ||||
137 | for(dsk = ldisks; dsk; dsk = dsk->next) { | |||
138 | if(nocache || (difftime(time(NULL((void*)0)), dsk->last_time) > DELAY60.0)) { | |||
139 | dsk->value = -1; | |||
140 | ||||
141 | if(dsk->type == ERROR) | |||
142 | dsk->ret = GETTEMP_ERROR; | |||
143 | else | |||
144 | dsk->ret = bus[dsk->type]->get_temperature(dsk); | |||
145 | ||||
146 | time(&dsk->last_time); | |||
147 | } | |||
148 | } | |||
149 | } | |||
150 | ||||
151 | void daemon_close_sockets(void) { | |||
152 | int i; | |||
153 | ||||
154 | for (i = 0 ; i < sks_serv_num; i++) | |||
155 | close(sks_serv[i]); | |||
156 | } | |||
157 | ||||
158 | void daemon_send_msg(struct disk *ldisks, int cfd) { | |||
159 | struct disk * dsk; | |||
160 | ||||
161 | daemon_update(ldisks, 0); | |||
162 | ||||
163 | for(dsk = ldisks; dsk; dsk = dsk->next) { | |||
164 | char msg[128]; | |||
165 | int n; | |||
166 | ||||
167 | switch(dsk->ret) { | |||
168 | case GETTEMP_NOT_APPLICABLE: | |||
169 | n = snprintf(msg, sizeof(msg), "%s%c%s%cNA%c*", | |||
170 | dsk->drive, separator, | |||
171 | dsk->model, separator, | |||
172 | separator); | |||
173 | break; | |||
174 | case GETTEMP_UNKNOWN: | |||
175 | n = snprintf(msg, sizeof(msg), "%s%c%s%cUNK%c*", | |||
176 | dsk->drive, separator, | |||
177 | dsk->model, separator, | |||
178 | separator); | |||
179 | break; | |||
180 | case GETTEMP_KNOWN: | |||
181 | n = snprintf(msg, sizeof(msg), "%s%c%s%c%d%c%c", | |||
182 | dsk->drive, separator, | |||
183 | dsk->model, separator, | |||
184 | value_to_unit(dsk), separator, | |||
185 | get_unit(dsk)); | |||
186 | break; | |||
187 | case GETTEMP_NOSENSOR: | |||
188 | n = snprintf(msg, sizeof(msg), "%s%c%s%cNOS%c*", | |||
189 | dsk->drive, separator, | |||
190 | dsk->model, separator, | |||
191 | separator); | |||
192 | break; | |||
193 | case GETTEMP_DRIVE_SLEEP: | |||
194 | n = snprintf(msg, sizeof(msg), "%s%c%s%cSLP%c*", | |||
195 | dsk->drive, separator, | |||
196 | dsk->model, separator, | |||
197 | separator); | |||
198 | break; | |||
199 | case GETTEMP_ERROR: | |||
200 | default: | |||
201 | n = snprintf(msg, sizeof(msg), "%s%c%s%cERR%c*", | |||
202 | dsk->drive, separator, | |||
203 | (dsk->model) ? dsk->model : "???", separator, | |||
204 | separator); | |||
205 | break; | |||
206 | } | |||
207 | write(cfd,&separator, 1); | |||
208 | write(cfd, &msg, n); | |||
209 | write(cfd,&separator, 1); | |||
210 | } | |||
211 | } | |||
212 | ||||
213 | ||||
214 | void daemon_syslog(struct disk *ldisks) { | |||
215 | struct disk * dsk; | |||
216 | ||||
217 | daemon_update(ldisks, 1); | |||
218 | ||||
219 | for(dsk = ldisks; dsk; dsk = dsk->next) { | |||
220 | switch(dsk->ret) { | |||
221 | case GETTEMP_KNOWN: | |||
222 | syslog(LOG_INFO6, "%s: %s: %d %c", | |||
223 | dsk->drive, | |||
224 | dsk->model, | |||
225 | value_to_unit(dsk), | |||
226 | get_unit(dsk)); | |||
227 | break; | |||
228 | case GETTEMP_DRIVE_SLEEP: | |||
229 | syslog(LOG_WARNING4, _("%s: %s: drive is sleeping")dcgettext (((void*)0), "%s: %s: drive is sleeping", 5), | |||
230 | dsk->drive, | |||
231 | dsk->model); | |||
232 | break; | |||
233 | case GETTEMP_NOSENSOR: | |||
234 | case GETTEMP_UNKNOWN: | |||
235 | syslog(LOG_WARNING4, _("%s: %s: no sensor")dcgettext (((void*)0), "%s: %s: no sensor", 5), | |||
236 | dsk->drive, | |||
237 | dsk->model); | |||
238 | break; | |||
239 | case GETTEMP_NOT_APPLICABLE: | |||
240 | syslog(LOG_ERR3, "%s: %s: %s", | |||
241 | dsk->drive, | |||
242 | dsk->model, | |||
243 | dsk->errormsg); | |||
244 | break; | |||
245 | default: | |||
246 | case GETTEMP_ERROR: | |||
247 | syslog(LOG_ERR3, "%s: %s", | |||
248 | dsk->drive, | |||
249 | dsk->errormsg); | |||
250 | break; | |||
251 | } | |||
252 | } | |||
253 | } | |||
254 | ||||
255 | void daemon_stop(int n) { | |||
256 | stop_daemon = 1; | |||
257 | } | |||
258 | ||||
259 | void do_daemon_mode(struct disk *ldisks) { | |||
260 | struct disk * dsk; | |||
261 | int cfd; | |||
262 | int i, ret, maxfd; | |||
263 | struct tm * time_st; | |||
264 | fd_set deffds; | |||
265 | time_t next_time; | |||
266 | ||||
267 | if (!foreground) { | |||
| ||||
268 | switch(fork()) { | |||
269 | case -1: | |||
270 | perror("fork"); | |||
271 | exit(2); | |||
272 | break; | |||
273 | case 0: | |||
274 | break; | |||
275 | default: | |||
276 | exit(0); | |||
277 | } | |||
278 | ||||
279 | setsid(); | |||
280 | ||||
281 | switch(fork()) { | |||
282 | case -1: | |||
283 | perror("fork"); | |||
284 | exit(2); | |||
285 | break; | |||
286 | case 0: | |||
287 | break; | |||
288 | default: | |||
289 | exit(0); | |||
290 | } | |||
291 | } | |||
292 | chdir("/"); | |||
293 | umask(0); | |||
294 | ||||
295 | /* close standard input and output */ | |||
296 | close(0); | |||
297 | close(1); | |||
298 | close(2); | |||
299 | ||||
300 | if (tcp_daemon) | |||
301 | daemon_open_sockets(); | |||
302 | ||||
303 | if (syslog_interval > 0) | |||
304 | openlog("hddtemp", LOG_PID0x01, LOG_DAEMON(3<<3)); | |||
305 | ||||
306 | /* redirect signals */ | |||
307 | for(i = 0; i <= _NSIG65; i++) { | |||
308 | switch(i) { | |||
309 | case SIGSEGV11: /* still done */ | |||
310 | case SIGBUS7: | |||
311 | case SIGILL4: | |||
312 | break; | |||
313 | case SIGPIPE13: | |||
314 | signal(SIGPIPE13, SIG_IGN((__sighandler_t) 1)); | |||
315 | break; | |||
316 | default: | |||
317 | signal(i, daemon_stop); | |||
318 | break; | |||
319 | } | |||
320 | } | |||
321 | ||||
322 | /* timers initialization */ | |||
323 | next_time = time(NULL((void*)0)); | |||
324 | for(dsk = ldisks; dsk; dsk = dsk->next) { | |||
325 | time(&dsk->last_time); | |||
326 | time_st = gmtime(&dsk->last_time); | |||
327 | time_st->tm_year -= 1; | |||
328 | dsk->last_time = mktime(time_st); | |||
329 | } | |||
330 | ||||
331 | /* initialize file descriptors and compute maxfd */ | |||
332 | FD_ZERO(&deffds)do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&deffds)->__fds_bits )[0]) : "memory"); } while (0); | |||
333 | maxfd = -1; | |||
334 | ||||
335 | for (i = 0 ; i < sks_serv_num; i++) { | |||
336 | FD_SET(sks_serv[i], &deffds)((void) (((&deffds)->__fds_bits)[((sks_serv[i]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) 1 << ((sks_serv [i]) % (8 * (int) sizeof (__fd_mask)))))); | |||
337 | ||||
338 | if (maxfd < sks_serv[i]) | |||
339 | maxfd = sks_serv[i]; | |||
340 | } | |||
341 | ||||
342 | /* start daemon */ | |||
343 | while(stop_daemon == 0) { | |||
344 | fd_set fds; | |||
345 | fds = deffds; | |||
346 | ||||
347 | if (syslog_interval > 0) | |||
348 | { | |||
349 | struct timeval tv; | |||
350 | time_t current_time; | |||
351 | ||||
352 | current_time = time(NULL((void*)0)); | |||
353 | if (next_time > current_time) | |||
354 | tv.tv_sec = next_time - current_time; | |||
355 | else | |||
356 | tv.tv_sec = 0; | |||
357 | tv.tv_usec = 0; | |||
358 | ret = select(maxfd + 1, &fds, NULL((void*)0), NULL((void*)0), &tv); | |||
359 | } | |||
360 | else | |||
361 | ret = select(maxfd + 1, &fds, NULL((void*)0), NULL((void*)0), NULL((void*)0)); | |||
362 | ||||
363 | if (ret == -1) | |||
364 | break; | |||
365 | else if (ret == 0 && syslog_interval > 0) { | |||
366 | daemon_syslog(ldisks); | |||
367 | next_time = time(NULL((void*)0)) + syslog_interval; | |||
368 | } | |||
369 | else if (tcp_daemon) { | |||
370 | struct sockaddr_storage caddr; | |||
371 | socklen_t sz_caddr; | |||
372 | sz_caddr = sizeof(struct sockaddr_storage); | |||
373 | ||||
374 | for (i = 0 ; i < sks_serv_num; i++) { | |||
375 | if (FD_ISSET(sks_serv[i], &fds)((((&fds)->__fds_bits)[((sks_serv[i]) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) 1 << ((sks_serv[i]) % (8 * (int) sizeof (__fd_mask))))) != 0)) | |||
376 | break; | |||
377 | } | |||
378 | ||||
379 | if (i == sks_serv_num) | |||
380 | continue; | |||
381 | ||||
382 | if ((cfd = accept(sks_serv[i], (struct sockaddr *)&caddr, &sz_caddr)) == -1) | |||
383 | continue; | |||
384 | ||||
385 | daemon_send_msg(ldisks, cfd); | |||
386 | ||||
387 | close(cfd); | |||
388 | } | |||
389 | } | |||
390 | ||||
391 | if (tcp_daemon) | |||
392 | daemon_close_sockets(); | |||
393 | ||||
394 | if (syslog_interval > 0) | |||
395 | closelog(); | |||
396 | } | |||
397 |