Line data Source code
1 : /*
2 : * Copyright (C) 2011 Andrea Mazzoleni
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 3 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, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "portable.h"
19 :
20 : #include "support.h"
21 :
22 : /****************************************************************************/
23 : /* print */
24 :
25 : int msg_level = 0;
26 :
27 : /**
28 : * Mutex used for printf.
29 : *
30 : * In Windows printf() is not atomic, and multiple threads
31 : * will have output interleaved.
32 : *
33 : * Note that even defining __USE_MINGW_ANSI_STDIO the problem persists.
34 : *
35 : * See for example:
36 : *
37 : * Weird output when I use pthread and printf.
38 : * http://stackoverflow.com/questions/13190254/weird-output-when-i-use-pthread-and-printf
39 : *
40 : * This is also required in other OS because we split output in stdlog in
41 : * two fprintf calls.
42 : */
43 : static pthread_mutex_t msg_lock = PTHREAD_MUTEX_INITIALIZER;
44 :
45 : /*
46 : * Note that in the following functions we always flush both
47 : * stdout and stderr, because we want to ensure that they mixes
48 : * well when redirected to files
49 : *
50 : * The buffering is similar at the "line buffered" one, that
51 : * is not available on Windows, so we emulate it in this way.
52 : *
53 : * For stdlog flushing is limited. To ensure flushing the
54 : * caller should use log_flush().
55 : */
56 :
57 804 : void log_fatal(const char* format, ...)
58 : {
59 : va_list ap;
60 :
61 804 : pthread_mutex_lock(&msg_lock);
62 :
63 804 : if (stdlog) {
64 103 : va_start(ap, format);
65 103 : fprintf(stdlog, "msg:fatal: ");
66 103 : vfprintf(stdlog, format, ap);
67 103 : fflush(stdlog);
68 103 : va_end(ap);
69 : }
70 :
71 804 : va_start(ap, format);
72 804 : vfprintf(stderr, format, ap);
73 804 : fflush(stderr);
74 804 : va_end(ap);
75 :
76 804 : pthread_mutex_unlock(&msg_lock);
77 804 : }
78 :
79 614821 : void log_error(const char* format, ...)
80 : {
81 : va_list ap;
82 :
83 614821 : pthread_mutex_lock(&msg_lock);
84 :
85 614821 : if (stdlog) {
86 612910 : va_start(ap, format);
87 612910 : fprintf(stdlog, "msg:error: ");
88 612910 : vfprintf(stdlog, format, ap);
89 612910 : fflush(stdlog);
90 612910 : va_end(ap);
91 : } else {
92 1911 : va_start(ap, format);
93 1911 : vfprintf(stderr, format, ap);
94 1911 : fflush(stderr);
95 1911 : va_end(ap);
96 : }
97 :
98 614821 : pthread_mutex_unlock(&msg_lock);
99 614821 : }
100 :
101 11751 : void log_expected(const char* format, ...)
102 : {
103 : va_list ap;
104 :
105 11751 : pthread_mutex_lock(&msg_lock);
106 :
107 11751 : if (stdlog) {
108 11751 : va_start(ap, format);
109 11751 : fprintf(stdlog, "msg:expected: ");
110 11751 : vfprintf(stdlog, format, ap);
111 11751 : fflush(stdlog);
112 11751 : va_end(ap);
113 : }
114 :
115 11751 : pthread_mutex_unlock(&msg_lock);
116 11751 : }
117 :
118 4043608 : void log_tag(const char* format, ...)
119 : {
120 : va_list ap;
121 :
122 4043608 : pthread_mutex_lock(&msg_lock);
123 :
124 4043608 : if (stdlog) {
125 2964603 : va_start(ap, format);
126 2964603 : vfprintf(stdlog, format, ap);
127 : /* here we intentionally don't flush */
128 : /* to make the output faster */
129 2964603 : va_end(ap);
130 : }
131 :
132 4043608 : pthread_mutex_unlock(&msg_lock);
133 4043608 : }
134 :
135 1108 : void log_flush(void)
136 : {
137 1108 : pthread_mutex_lock(&msg_lock);
138 :
139 1108 : if (stdlog)
140 326 : fflush(stdlog);
141 1108 : fflush(stdout);
142 1108 : fflush(stderr);
143 :
144 1108 : pthread_mutex_unlock(&msg_lock);
145 1108 : }
146 :
147 568 : void msg_status(const char* format, ...)
148 : {
149 : va_list ap;
150 :
151 568 : pthread_mutex_lock(&msg_lock);
152 :
153 569 : if (stdlog) {
154 276 : va_start(ap, format);
155 276 : fprintf(stdlog, "msg:status: ");
156 276 : vfprintf(stdlog, format, ap);
157 276 : fflush(stdlog);
158 276 : va_end(ap);
159 : }
160 :
161 569 : if (msg_level >= MSG_STATUS) {
162 569 : va_start(ap, format);
163 569 : vfprintf(stdout, format, ap);
164 569 : fflush(stdout);
165 569 : va_end(ap);
166 : }
167 :
168 569 : pthread_mutex_unlock(&msg_lock);
169 569 : }
170 :
171 296170 : void msg_info(const char* format, ...)
172 : {
173 : va_list ap;
174 :
175 296170 : pthread_mutex_lock(&msg_lock);
176 :
177 : /* don't output in stdlog as these messages */
178 : /* are always paired with a msg_tag() call */
179 :
180 296170 : if (msg_level >= MSG_INFO) {
181 28833 : va_start(ap, format);
182 28833 : vfprintf(stdout, format, ap);
183 28833 : fflush(stdout);
184 28833 : va_end(ap);
185 : }
186 :
187 296170 : pthread_mutex_unlock(&msg_lock);
188 296170 : }
189 :
190 3911 : void msg_progress(const char* format, ...)
191 : {
192 : va_list ap;
193 :
194 3911 : pthread_mutex_lock(&msg_lock);
195 :
196 3911 : if (stdlog) {
197 775 : va_start(ap, format);
198 775 : fprintf(stdlog, "msg:progress: ");
199 775 : vfprintf(stdlog, format, ap);
200 775 : fflush(stdlog);
201 775 : va_end(ap);
202 : }
203 :
204 3911 : if (msg_level >= MSG_PROGRESS) {
205 153 : va_start(ap, format);
206 153 : vfprintf(stdout, format, ap);
207 153 : fflush(stdout);
208 153 : va_end(ap);
209 : }
210 :
211 3911 : pthread_mutex_unlock(&msg_lock);
212 3911 : }
213 :
214 954 : void msg_bar(const char* format, ...)
215 : {
216 : va_list ap;
217 :
218 954 : pthread_mutex_lock(&msg_lock);
219 :
220 : /* don't output in stdlog as these messages */
221 : /* are intended for screen only */
222 : /* also don't flush stdout as they are intended to be partial messages */
223 :
224 954 : if (msg_level >= MSG_BAR) {
225 0 : va_start(ap, format);
226 0 : vfprintf(stdout, format, ap);
227 0 : va_end(ap);
228 : }
229 :
230 954 : pthread_mutex_unlock(&msg_lock);
231 954 : }
232 :
233 127078 : void msg_verbose(const char* format, ...)
234 : {
235 : va_list ap;
236 :
237 127078 : pthread_mutex_lock(&msg_lock);
238 :
239 127078 : if (stdlog) {
240 125434 : va_start(ap, format);
241 125434 : fprintf(stdlog, "msg:verbose: ");
242 125434 : vfprintf(stdlog, format, ap);
243 125434 : fflush(stdlog);
244 125434 : va_end(ap);
245 : }
246 :
247 127078 : if (msg_level >= MSG_VERBOSE) {
248 79 : va_start(ap, format);
249 79 : vfprintf(stdout, format, ap);
250 79 : fflush(stdout);
251 79 : va_end(ap);
252 : }
253 :
254 127078 : pthread_mutex_unlock(&msg_lock);
255 127078 : }
256 :
257 440 : void msg_flush(void)
258 : {
259 440 : pthread_mutex_lock(&msg_lock);
260 :
261 440 : fflush(stdout);
262 440 : fflush(stderr);
263 :
264 440 : pthread_mutex_unlock(&msg_lock);
265 440 : }
266 :
267 : /**
268 : * Number of buffers for esc().
269 : */
270 : #define ESC_MAX 4
271 :
272 : /**
273 : * Buffer table to allow multiple calls to esc().
274 : */
275 : static char esc_table[ESC_MAX][PATH_MAX*2+1];
276 :
277 : /**
278 : * Next buffer to use.
279 : */
280 : static unsigned esc_index = 0;
281 :
282 : #define ESCAPE(from,to) \
283 : case from : \
284 : if (p == end) \
285 : goto bail; \
286 : *p++ = '\\'; \
287 : if (p == end) \
288 : goto bail; \
289 : *p++ = to; \
290 : break
291 :
292 3594259 : const char* esc(const char* str)
293 : {
294 3594259 : char* begin = esc_table[esc_index];
295 3594259 : char* end = begin + PATH_MAX;
296 3594259 : char* p = begin;
297 :
298 : /* next call uses the next buffer */
299 3594259 : esc_index = (esc_index + 1) % ESC_MAX;
300 :
301 : /* copy string with escaping */
302 53734097 : while (*str) {
303 46545579 : char c = *str;
304 :
305 46545579 : switch (c) {
306 :
307 0 : ESCAPE('\n', 'n');
308 0 : ESCAPE('\r', 'r');
309 0 : ESCAPE(':', 'd');
310 0 : ESCAPE('\\', '\\');
311 :
312 : default:
313 46545579 : if (p == end)
314 0 : goto bail;
315 46545579 : *p++ = c;
316 46545579 : break;
317 : }
318 :
319 46545579 : ++str;
320 : }
321 :
322 : /* put final 0 */
323 3594259 : if (p == end)
324 0 : goto bail;
325 3594259 : *p = 0;
326 :
327 7188518 : return begin;
328 :
329 : bail:
330 : /* LCOV_EXCL_START */
331 : log_fatal("Escape too long\n");
332 : exit(EXIT_FAILURE);
333 : /* LCOV_EXCL_STOP */
334 : }
335 :
336 0 : char* polish(char* s)
337 : {
338 0 : char* i = s;
339 :
340 0 : while (*i) {
341 0 : if (isspace(*i) || !isprint(*i))
342 0 : *i = ' ';
343 0 : ++i;
344 : }
345 :
346 0 : return s;
347 : }
348 :
349 : /****************************************************************************/
350 : /* path */
351 :
352 6858280 : void pathcpy(char* dst, size_t size, const char* src)
353 : {
354 6858280 : size_t len = strlen(src);
355 :
356 6858280 : if (len + 1 > size) {
357 : /* LCOV_EXCL_START */
358 : log_fatal("Path too long\n");
359 : exit(EXIT_FAILURE);
360 : /* LCOV_EXCL_STOP */
361 : }
362 :
363 6858280 : memcpy(dst, src, len + 1);
364 6858280 : }
365 :
366 248 : void pathcat(char* dst, size_t size, const char* src)
367 : {
368 248 : size_t dst_len = strlen(dst);
369 248 : size_t src_len = strlen(src);
370 :
371 248 : if (dst_len + src_len + 1 > size) {
372 : /* LCOV_EXCL_START */
373 : log_fatal("Path too long\n");
374 : exit(EXIT_FAILURE);
375 : /* LCOV_EXCL_STOP */
376 : }
377 :
378 248 : memcpy(dst + dst_len, src, src_len + 1);
379 248 : }
380 :
381 788 : void pathcatc(char* dst, size_t size, char c)
382 : {
383 788 : size_t dst_len = strlen(dst);
384 :
385 788 : if (dst_len + 2 > size) {
386 : /* LCOV_EXCL_START */
387 : log_fatal("Path too long\n");
388 : exit(EXIT_FAILURE);
389 : /* LCOV_EXCL_STOP */
390 : }
391 :
392 788 : dst[dst_len] = c;
393 788 : dst[dst_len + 1] = 0;
394 788 : }
395 :
396 8852 : void pathimport(char* dst, size_t size, const char* src)
397 : {
398 8852 : pathcpy(dst, size, src);
399 :
400 : #ifdef _WIN32
401 : /* convert all Windows '\' to C '/' */
402 : while (*dst) {
403 : if (*dst == '\\')
404 : *dst = '/';
405 : ++dst;
406 : }
407 : #endif
408 8852 : }
409 :
410 17567 : void pathexport(char* dst, size_t size, const char* src)
411 : {
412 17567 : pathcpy(dst, size, src);
413 :
414 : #ifdef _WIN32
415 : /* convert all C '/' to Windows '\' */
416 : while (*dst) {
417 : if (*dst == '/')
418 : *dst = '\\';
419 : ++dst;
420 : }
421 : #endif
422 17567 : }
423 :
424 50027975 : void pathprint(char* dst, size_t size, const char* format, ...)
425 : {
426 : size_t len;
427 : va_list ap;
428 :
429 50027975 : va_start(ap, format);
430 50027975 : len = vsnprintf(dst, size, format, ap);
431 50027975 : va_end(ap);
432 :
433 50027975 : if (len >= size) {
434 : /* LCOV_EXCL_START */
435 : log_fatal("Path too long\n");
436 : exit(EXIT_FAILURE);
437 : /* LCOV_EXCL_STOP */
438 : }
439 50027975 : }
440 :
441 7319 : void pathslash(char* dst, size_t size)
442 : {
443 7319 : size_t len = strlen(dst);
444 :
445 7319 : if (len > 0 && dst[len - 1] != '/') {
446 5842 : if (len + 2 >= size) {
447 : /* LCOV_EXCL_START */
448 : log_fatal("Path too long\n");
449 : exit(EXIT_FAILURE);
450 : /* LCOV_EXCL_STOP */
451 : }
452 :
453 5842 : dst[len] = '/';
454 5842 : dst[len + 1] = 0;
455 : }
456 7319 : }
457 :
458 30 : void pathcut(char* dst)
459 : {
460 30 : char* slash = strrchr(dst, '/');
461 30 : if (slash)
462 30 : slash[1] = 0;
463 : else
464 0 : dst[0] = 0;
465 30 : }
466 :
467 50334016 : int pathcmp(const char* a, const char* b)
468 : {
469 : #ifdef _WIN32
470 : char ai[PATH_MAX];
471 : char bi[PATH_MAX];
472 :
473 : /* import to convert \ to / */
474 : pathimport(ai, sizeof(ai), a);
475 : pathimport(bi, sizeof(bi), b);
476 :
477 : /* case insensitive compare in Windows */
478 : return stricmp(ai, bi);
479 : #else
480 50334016 : return strcmp(a, b);
481 : #endif
482 : }
483 :
484 : /****************************************************************************/
485 : /* filesystem */
486 :
487 793004 : int mkancestor(const char* file)
488 : {
489 : char dir[PATH_MAX];
490 : struct stat st;
491 : char* c;
492 :
493 793004 : pathcpy(dir, sizeof(dir), file);
494 :
495 793004 : c = strrchr(dir, '/');
496 793004 : if (!c) {
497 : /* no ancestor */
498 0 : return 0;
499 : }
500 :
501 : /* clear the file */
502 793004 : *c = 0;
503 :
504 : /* if it's the root dir */
505 793004 : if (*dir == 0) {
506 : /* nothing more to do */
507 0 : return 0;
508 : }
509 :
510 : #ifdef _WIN32
511 : /* if it's a drive specificaion like "C:" */
512 : if (isalpha(dir[0]) && dir[1] == ':' && dir[2] == 0) {
513 : /* nothing more to do */
514 : return 0;
515 : }
516 : #endif
517 :
518 : /*
519 : * Check if the dir already exists using lstat().
520 : *
521 : * Note that in Windows when dealing with read-only media
522 : * you cannot try to create the directory, and expecting
523 : * the EEXIST error because the call will fail with ERROR_WRITE_PROTECTED.
524 : *
525 : * Also in Windows it's better to use lstat() than stat() because it
526 : * doen't need to open the dir with CreateFile().
527 : */
528 793004 : if (lstat(dir, &st) == 0) {
529 : /* it already exists */
530 792924 : return 0;
531 : }
532 :
533 : /* recursively create them all */
534 80 : if (mkancestor(dir) != 0) {
535 : /* LCOV_EXCL_START */
536 : return -1;
537 : /* LCOV_EXCL_STOP */
538 : }
539 :
540 : /* create it */
541 80 : if (mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
542 : /* LCOV_EXCL_START */
543 : log_fatal("Error creating directory '%s'. %s.\n", dir, strerror(errno));
544 : return -1;
545 : /* LCOV_EXCL_STOP */
546 : }
547 :
548 80 : return 0;
549 : }
550 :
551 118002 : int fmtime(int f, int64_t mtime_sec, int mtime_nsec)
552 : {
553 : #if HAVE_FUTIMENS
554 : struct timespec tv[2];
555 : #else
556 : struct timeval tv[2];
557 : #endif
558 : int ret;
559 :
560 : #if HAVE_FUTIMENS /* futimens() is preferred because it gives nanosecond precision */
561 118002 : tv[0].tv_sec = mtime_sec;
562 118002 : if (mtime_nsec != STAT_NSEC_INVALID)
563 118002 : tv[0].tv_nsec = mtime_nsec;
564 : else
565 0 : tv[0].tv_nsec = 0;
566 118002 : tv[1].tv_sec = tv[0].tv_sec;
567 118002 : tv[1].tv_nsec = tv[0].tv_nsec;
568 :
569 118002 : ret = futimens(f, tv);
570 : #elif HAVE_FUTIMES /* fallback to futimes() if nanosecond precision is not available */
571 : tv[0].tv_sec = mtime_sec;
572 : if (mtime_nsec != STAT_NSEC_INVALID)
573 : tv[0].tv_usec = mtime_nsec / 1000;
574 : else
575 : tv[0].tv_usec = 0;
576 : tv[1].tv_sec = tv[0].tv_sec;
577 : tv[1].tv_usec = tv[0].tv_usec;
578 :
579 : ret = futimes(f, tv);
580 : #elif HAVE_FUTIMESAT /* fallback to futimesat() for Solaris, it only has futimesat() */
581 : tv[0].tv_sec = mtime_sec;
582 : if (mtime_nsec != STAT_NSEC_INVALID)
583 : tv[0].tv_usec = mtime_nsec / 1000;
584 : else
585 : tv[0].tv_usec = 0;
586 : tv[1].tv_sec = tv[0].tv_sec;
587 : tv[1].tv_usec = tv[0].tv_usec;
588 :
589 : ret = futimesat(f, 0, tv);
590 : #else
591 : #error No function available to set file timestamps with sub-second precision
592 : #endif
593 :
594 118002 : return ret;
595 : }
596 :
597 : /****************************************************************************/
598 : /* memory */
599 :
600 : /**
601 : * Total amount of memory allocated.
602 : */
603 : static size_t mcounter;
604 :
605 444 : size_t malloc_counter(void)
606 : {
607 444 : return mcounter;
608 : }
609 :
610 : /* LCOV_EXCL_START */
611 : static ssize_t malloc_print(int f, const char* str)
612 : {
613 : ssize_t len = 0;
614 : while (str[len])
615 : ++len;
616 : return write(f, str, len);
617 : }
618 : /* LCOV_EXCL_STOP */
619 :
620 : /* LCOV_EXCL_START */
621 : static ssize_t malloc_printn(int f, size_t value)
622 : {
623 : char buf[32];
624 : int i;
625 :
626 : if (!value)
627 : return write(f, "0", 1);
628 :
629 : i = sizeof(buf);
630 : while (value) {
631 : buf[--i] = (value % 10) + '0';
632 : value /= 10;
633 : }
634 :
635 : return write(f, buf + i, sizeof(buf) - i);
636 : }
637 : /* LCOV_EXCL_STOP */
638 :
639 : /* LCOV_EXCL_START */
640 : void malloc_fail(size_t size)
641 : {
642 : /* don't use printf to avoid any possible extra allocation */
643 : int f = 2; /* stderr */
644 :
645 : malloc_print(f, "Failed for Low Memory!\n");
646 : malloc_print(f, "Allocating ");
647 : malloc_printn(f, size);
648 : malloc_print(f, " bytes.\n");
649 : malloc_print(f, "Already allocated ");
650 : malloc_printn(f, malloc_counter());
651 : malloc_print(f, " bytes.\n");
652 : if (sizeof(void*) == 4) {
653 : malloc_print(f, "You are currently using a 32 bits executable.\n");
654 : malloc_print(f, "If you have more than 4GB of memory, please upgrade to a 64 bits one.\n");
655 : }
656 : }
657 : /* LCOV_EXCL_STOP */
658 :
659 13871380 : void* malloc_nofail(size_t size)
660 : {
661 13871380 : void* ptr = malloc(size);
662 :
663 13871380 : if (!ptr) {
664 : /* LCOV_EXCL_START */
665 : malloc_fail(size);
666 : exit(EXIT_FAILURE);
667 : /* LCOV_EXCL_STOP */
668 : }
669 :
670 : #ifndef CHECKER /* Don't preinitialize when running for valgrind */
671 : /* Here we preinitialize the memory to ensure that the OS is really allocating it */
672 : /* and not only reserving the addressable space. */
673 : /* Otherwise we are risking that the OOM (Out Of Memory) killer in Linux will kill the process. */
674 : /* Filling the memory doesn't ensure to disable OOM, but it increase a lot the chances to */
675 : /* get a real error from malloc() instead than a process killed. */
676 : /* Note that calloc() doesn't have the same effect. */
677 13871380 : memset(ptr, 0xA5, size);
678 : #endif
679 :
680 13871380 : mcounter += size;
681 :
682 13871380 : return ptr;
683 : }
684 :
685 9283 : void* calloc_nofail(size_t count, size_t size)
686 : {
687 : void* ptr;
688 :
689 9283 : size *= count;
690 :
691 : /* see the note in malloc_nofail() of why we don't use calloc() */
692 9283 : ptr = malloc(size);
693 :
694 9283 : if (!ptr) {
695 : /* LCOV_EXCL_START */
696 : malloc_fail(size);
697 : exit(EXIT_FAILURE);
698 : /* LCOV_EXCL_STOP */
699 : }
700 :
701 9283 : memset(ptr, 0, size);
702 :
703 9283 : mcounter += size;
704 :
705 9283 : return ptr;
706 : }
707 :
708 5662644 : char* strdup_nofail(const char* str)
709 : {
710 : size_t size;
711 : char* ptr;
712 :
713 5662644 : size = strlen(str) + 1;
714 :
715 5662644 : ptr = malloc(size);
716 :
717 5662644 : if (!ptr) {
718 : /* LCOV_EXCL_START */
719 : malloc_fail(size);
720 : exit(EXIT_FAILURE);
721 : /* LCOV_EXCL_STOP */
722 : }
723 :
724 5662644 : memcpy(ptr, str, size);
725 :
726 5662644 : mcounter += size;
727 :
728 5662644 : return ptr;
729 : }
730 :
731 : /****************************************************************************/
732 : /* smartctl */
733 :
734 : /**
735 : * Match a string with the specified pattern.
736 : * Like sscanf() a space match any sequence of spaces.
737 : * Return 0 if it matches.
738 : */
739 0 : static int smatch(const char* str, const char* pattern)
740 : {
741 0 : while (*pattern) {
742 0 : if (isspace(*pattern)) {
743 0 : ++pattern;
744 0 : while (isspace(*str))
745 0 : ++str;
746 0 : } else if (*pattern == *str) {
747 0 : ++pattern;
748 0 : ++str;
749 : } else
750 0 : return -1;
751 : }
752 :
753 0 : return 0;
754 : }
755 :
756 25 : int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* smart, char* serial)
757 : {
758 : unsigned i;
759 : int inside;
760 :
761 : /* preclear attribute */
762 25 : *serial = 0;
763 6525 : for (i = 0; i < SMART_COUNT; ++i)
764 6500 : smart[i] = SMART_UNASSIGNED;
765 :
766 : /* read the file */
767 25 : inside = 0;
768 : while (1) {
769 : char buf[256];
770 : char attr[64];
771 : unsigned id;
772 : uint64_t raw;
773 : char* s;
774 :
775 25 : s = fgets(buf, sizeof(buf), f);
776 25 : if (s == 0)
777 25 : break;
778 :
779 : /* remove extraneous chars */
780 0 : s = polish(buf);
781 :
782 0 : log_tag("smartctl:%s:%s:out: %s\n", file, name, s);
783 :
784 : /* skip initial spaces */
785 0 : while (isspace(*s))
786 0 : ++s;
787 :
788 0 : if (*s == 0) {
789 0 : inside = 0;
790 0 : } else if (smatch(s, "ID#") == 0) {
791 0 : inside = 1;
792 0 : } else if (smatch(s, "No Errors Logged") == 0) {
793 0 : smart[SMART_ERROR] = 0;
794 0 : } else if (sscanf(s, "ATA Error Count: %" SCNu64, &raw) == 1) {
795 0 : smart[SMART_ERROR] = raw;
796 0 : } else if (sscanf(s, "Serial Number: %63s", serial) == 1) {
797 0 : } else if (smatch(s, "Rotation Rate: Solid State") == 0) {
798 0 : smart[SMART_ROTATION_RATE] = 0;
799 0 : } else if (sscanf(s, "Rotation Rate: %" SCNu64, &smart[SMART_ROTATION_RATE]) == 1) {
800 0 : } else if (sscanf(s, "User Capacity: %63s", attr) == 1) {
801 0 : smart[SMART_SIZE] = 0;
802 0 : for (i = 0; attr[i]; ++i) {
803 0 : if (isdigit(attr[i])) {
804 0 : smart[SMART_SIZE] *= 10;
805 0 : smart[SMART_SIZE] += attr[i] - '0';
806 : }
807 : }
808 0 : } else if (inside) {
809 0 : if (sscanf(s, "%u %*s %*s %*s %*s %*s %*s %*s %*s %" SCNu64, &id, &raw) != 2) {
810 : /* LCOV_EXCL_START */
811 : log_fatal("Invalid smartctl line '%s'.\n", s);
812 : return -1;
813 : /* LCOV_EXCL_STOP */
814 : }
815 :
816 0 : if (id >= 256) {
817 : /* LCOV_EXCL_START */
818 : log_fatal("Invalid SMART id '%u'.\n", id);
819 : return -1;
820 : /* LCOV_EXCL_STOP */
821 : }
822 :
823 0 : smart[id] = raw;
824 : }
825 0 : }
826 :
827 25 : return 0;
828 : }
829 :
830 12 : int smartctl_flush(FILE* f, const char* file, const char* name)
831 : {
832 : /* read the file */
833 : while (1) {
834 : char buf[256];
835 : char* s;
836 :
837 12 : s = fgets(buf, sizeof(buf), f);
838 12 : if (s == 0)
839 12 : break;
840 :
841 : /* remove extraneous chars */
842 0 : s = polish(buf);
843 :
844 0 : log_tag("smartctl:%s:%s:out: %s\n", file, name, s);
845 0 : }
846 :
847 24 : return 0;
848 : }
849 :
|