LCOV - code coverage report
Current view: top level - cmdline - support.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 232 308 75.3 %
Date: 2015-05-12 Functions: 29 34 85.3 %

          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         784 : void log_fatal(const char* format, ...)
      58             : {
      59             :         va_list ap;
      60             : 
      61         784 :         pthread_mutex_lock(&msg_lock);
      62             : 
      63         784 :         if (stdlog) {
      64          92 :                 va_start(ap, format);
      65          92 :                 fprintf(stdlog, "msg:fatal: ");
      66          92 :                 vfprintf(stdlog, format, ap);
      67          92 :                 fflush(stdlog);
      68          92 :                 va_end(ap);
      69             :         }
      70             : 
      71         784 :         va_start(ap, format);
      72         784 :         vfprintf(stderr, format, ap);
      73         784 :         fflush(stderr);
      74         784 :         va_end(ap);
      75             : 
      76         784 :         pthread_mutex_unlock(&msg_lock);
      77         784 : }
      78             : 
      79      725185 : void log_error(const char* format, ...)
      80             : {
      81             :         va_list ap;
      82             : 
      83      725185 :         pthread_mutex_lock(&msg_lock);
      84             : 
      85      725185 :         if (stdlog) {
      86      723277 :                 va_start(ap, format);
      87      723277 :                 fprintf(stdlog, "msg:error: ");
      88      723277 :                 vfprintf(stdlog, format, ap);
      89      723277 :                 fflush(stdlog);
      90      723277 :                 va_end(ap);
      91             :         } else {
      92        1908 :                 va_start(ap, format);
      93        1908 :                 vfprintf(stderr, format, ap);
      94        1908 :                 fflush(stderr);
      95        1908 :                 va_end(ap);
      96             :         }
      97             : 
      98      725185 :         pthread_mutex_unlock(&msg_lock);
      99      725185 : }
     100             : 
     101       11738 : void log_expected(const char* format, ...)
     102             : {
     103             :         va_list ap;
     104             : 
     105       11738 :         pthread_mutex_lock(&msg_lock);
     106             : 
     107       11738 :         if (stdlog) {
     108       11738 :                 va_start(ap, format);
     109       11738 :                 fprintf(stdlog, "msg:expected: ");
     110       11738 :                 vfprintf(stdlog, format, ap);
     111       11738 :                 fflush(stdlog);
     112       11738 :                 va_end(ap);
     113             :         }
     114             : 
     115       11738 :         pthread_mutex_unlock(&msg_lock);
     116       11738 : }
     117             : 
     118     4036583 : void log_tag(const char* format, ...)
     119             : {
     120             :         va_list ap;
     121             : 
     122     4036583 :         pthread_mutex_lock(&msg_lock);
     123             : 
     124     4036583 :         if (stdlog) {
     125     2951533 :                 va_start(ap, format);
     126     2951533 :                 vfprintf(stdlog, format, ap);
     127             :                 /* here we intentionally don't flush */
     128             :                 /* to make the output faster */
     129     2951533 :                 va_end(ap);
     130             :         }
     131             : 
     132     4036583 :         pthread_mutex_unlock(&msg_lock);
     133     4036583 : }
     134             : 
     135        1055 : void log_flush(void)
     136             : {
     137        1055 :         pthread_mutex_lock(&msg_lock);
     138             : 
     139        1055 :         if (stdlog)
     140         316 :                 fflush(stdlog);
     141        1055 :         fflush(stdout);
     142        1055 :         fflush(stderr);
     143             : 
     144        1055 :         pthread_mutex_unlock(&msg_lock);
     145        1055 : }
     146             : 
     147         544 : void msg_status(const char* format, ...)
     148             : {
     149             :         va_list ap;
     150             : 
     151         544 :         pthread_mutex_lock(&msg_lock);
     152             : 
     153         544 :         if (stdlog) {
     154         270 :                 va_start(ap, format);
     155         270 :                 fprintf(stdlog, "msg:status: ");
     156         270 :                 vfprintf(stdlog, format, ap);
     157         270 :                 fflush(stdlog);
     158         270 :                 va_end(ap);
     159             :         }
     160             : 
     161         544 :         if (msg_level >= MSG_STATUS) {
     162         544 :                 va_start(ap, format);
     163         544 :                 vfprintf(stdout, format, ap);
     164         544 :                 fflush(stdout);
     165         544 :                 va_end(ap);
     166             :         }
     167             : 
     168         544 :         pthread_mutex_unlock(&msg_lock);
     169         544 : }
     170             : 
     171      297699 : void msg_info(const char* format, ...)
     172             : {
     173             :         va_list ap;
     174             : 
     175      297699 :         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      297699 :         if (msg_level >= MSG_INFO) {
     181       28823 :                 va_start(ap, format);
     182       28823 :                 vfprintf(stdout, format, ap);
     183       28823 :                 fflush(stdout);
     184       28823 :                 va_end(ap);
     185             :         }
     186             : 
     187      297699 :         pthread_mutex_unlock(&msg_lock);
     188      297699 : }
     189             : 
     190        2807 : void msg_progress(const char* format, ...)
     191             : {
     192             :         va_list ap;
     193             : 
     194        2807 :         pthread_mutex_lock(&msg_lock);
     195             : 
     196        2807 :         if (stdlog) {
     197         713 :                 va_start(ap, format);
     198         713 :                 fprintf(stdlog, "msg:progress: ");
     199         713 :                 vfprintf(stdlog, format, ap);
     200         713 :                 fflush(stdlog);
     201         713 :                 va_end(ap);
     202             :         }
     203             : 
     204        2807 :         if (msg_level >= MSG_PROGRESS) {
     205         139 :                 va_start(ap, format);
     206         139 :                 vfprintf(stdout, format, ap);
     207         139 :                 fflush(stdout);
     208         139 :                 va_end(ap);
     209             :         }
     210             : 
     211        2807 :         pthread_mutex_unlock(&msg_lock);
     212        2807 : }
     213             : 
     214         917 : void msg_bar(const char* format, ...)
     215             : {
     216             :         va_list ap;
     217             : 
     218         917 :         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         917 :         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         917 :         pthread_mutex_unlock(&msg_lock);
     231         917 : }
     232             : 
     233      126750 : void msg_verbose(const char* format, ...)
     234             : {
     235             :         va_list ap;
     236             : 
     237      126750 :         pthread_mutex_lock(&msg_lock);
     238             : 
     239      126750 :         if (stdlog) {
     240      125246 :                 va_start(ap, format);
     241      125246 :                 fprintf(stdlog, "msg:verbose: ");
     242      125246 :                 vfprintf(stdlog, format, ap);
     243      125246 :                 fflush(stdlog);
     244      125246 :                 va_end(ap);
     245             :         }
     246             : 
     247      126750 :         if (msg_level >= MSG_VERBOSE) {
     248          83 :                 va_start(ap, format);
     249          83 :                 vfprintf(stdout, format, ap);
     250          83 :                 fflush(stdout);
     251          83 :                 va_end(ap);
     252             :         }
     253             : 
     254      126750 :         pthread_mutex_unlock(&msg_lock);
     255      126750 : }
     256             : 
     257         423 : void msg_flush(void)
     258             : {
     259         423 :         pthread_mutex_lock(&msg_lock);
     260             : 
     261         423 :         fflush(stdout);
     262         423 :         fflush(stderr);
     263             : 
     264         423 :         pthread_mutex_unlock(&msg_lock);
     265         423 : }
     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     3589269 : const char* esc(const char* str)
     293             : {
     294     3589269 :         char* begin = esc_table[esc_index];
     295     3589269 :         char* end = begin + PATH_MAX;
     296     3589269 :         char* p = begin;
     297             : 
     298             :         /* next call uses the next buffer */
     299     3589269 :         esc_index = (esc_index + 1) % ESC_MAX;
     300             : 
     301             :         /* copy string with escaping */
     302    53647568 :         while (*str) {
     303    46469030 :                 char c = *str;
     304             : 
     305    46469030 :                 switch (c) {
     306             : 
     307           0 :                 ESCAPE('\n', 'n');
     308           0 :                 ESCAPE('\r', 'r');
     309           0 :                 ESCAPE(':', 'd');
     310           0 :                 ESCAPE('\\', '\\');
     311             : 
     312             :                 default:
     313    46469030 :                         if (p == end)
     314           0 :                                 goto bail;
     315    46469030 :                         *p++ = c;
     316    46469030 :                         break;
     317             :                 }
     318             : 
     319    46469030 :                 ++str;
     320             :         }
     321             : 
     322             :         /* put final 0 */
     323     3589269 :         if (p == end)
     324           0 :                 goto bail;
     325     3589269 :         *p = 0;
     326             : 
     327     7178538 :         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     6804242 : void pathcpy(char* dst, size_t size, const char* src)
     353             : {
     354     6804242 :         size_t len = strlen(src);
     355             : 
     356     6804242 :         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     6804242 :         memcpy(dst, src, len + 1);
     364     6804242 : }
     365             : 
     366         237 : void pathcat(char* dst, size_t size, const char* src)
     367             : {
     368         237 :         size_t dst_len = strlen(dst);
     369         237 :         size_t src_len = strlen(src);
     370             : 
     371         237 :         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         237 :         memcpy(dst + dst_len, src, src_len + 1);
     379         237 : }
     380             : 
     381         780 : void pathcatc(char* dst, size_t size, char c)
     382             : {
     383         780 :         size_t dst_len = strlen(dst);
     384             : 
     385         780 :         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         780 :         dst[dst_len] = c;
     393         780 :         dst[dst_len + 1] = 0;
     394         780 : }
     395             : 
     396        8384 : void pathimport(char* dst, size_t size, const char* src)
     397             : {
     398        8384 :         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        8384 : }
     409             : 
     410       17568 : void pathexport(char* dst, size_t size, const char* src)
     411             : {
     412       17568 :         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       17568 : }
     423             : 
     424    49788380 : void pathprint(char* dst, size_t size, const char* format, ...)
     425             : {
     426             :         size_t len;
     427             :         va_list ap;
     428             : 
     429    49788380 :         va_start(ap, format);
     430    49788380 :         len = vsnprintf(dst, size, format, ap);
     431    49788380 :         va_end(ap);
     432             : 
     433    49788380 :         if (len >= size) {
     434             :                 /* LCOV_EXCL_START */
     435             :                 log_fatal("Path too long\n");
     436             :                 exit(EXIT_FAILURE);
     437             :                 /* LCOV_EXCL_STOP */
     438             :         }
     439    49788380 : }
     440             : 
     441        7165 : void pathslash(char* dst, size_t size)
     442             : {
     443        7165 :         size_t len = strlen(dst);
     444             : 
     445        7165 :         if (len > 0 && dst[len - 1] != '/') {
     446        5754 :                 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        5754 :                 dst[len] = '/';
     454        5754 :                 dst[len + 1] = 0;
     455             :         }
     456        7165 : }
     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    49935952 : 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    49935952 :         return strcmp(a, b);
     481             : #endif
     482             : }
     483             : 
     484             : /****************************************************************************/
     485             : /* filesystem */
     486             : 
     487      794947 : int mkancestor(const char* file)
     488             : {
     489             :         char dir[PATH_MAX];
     490             :         struct stat st;
     491             :         char* c;
     492             : 
     493      794947 :         pathcpy(dir, sizeof(dir), file);
     494             : 
     495      794947 :         c = strrchr(dir, '/');
     496      794947 :         if (!c) {
     497             :                 /* no ancestor */
     498           0 :                 return 0;
     499             :         }
     500             : 
     501             :         /* clear the file */
     502      794947 :         *c = 0;
     503             : 
     504             :         /* if it's the root dir */
     505      794947 :         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      794947 :         if (lstat(dir, &st) == 0) {
     529             :                 /* it already exists */
     530      794867 :                 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      119773 : 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      119773 :         tv[0].tv_sec = mtime_sec;
     562      119773 :         if (mtime_nsec != STAT_NSEC_INVALID)
     563      119773 :                 tv[0].tv_nsec = mtime_nsec;
     564             :         else
     565           0 :                 tv[0].tv_nsec = 0;
     566      119773 :         tv[1].tv_sec = tv[0].tv_sec;
     567      119773 :         tv[1].tv_nsec = tv[0].tv_nsec;
     568             : 
     569      119773 :         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      119773 :         return ret;
     595             : }
     596             : 
     597             : /****************************************************************************/
     598             : /* memory */
     599             : 
     600             : static size_t mcounter;
     601             : 
     602         426 : size_t malloc_counter(void)
     603             : {
     604         426 :         return mcounter;
     605             : }
     606             : 
     607           0 : static ssize_t malloc_print(int f, const char* str)
     608             : {
     609           0 :         ssize_t len = 0;
     610           0 :         while (str[len])
     611           0 :                 ++len;
     612           0 :         return write(f, str, len);
     613             : }
     614             : 
     615           0 : static ssize_t malloc_printn(int f, size_t value)
     616             : {
     617             :         char buf[32];
     618             :         int i;
     619             : 
     620           0 :         if (!value)
     621           0 :                 return write(f, "0", 1);
     622             : 
     623           0 :         i = sizeof(buf);
     624           0 :         while (value) {
     625           0 :                 buf[--i] = (value % 10) + '0';
     626           0 :                 value /= 10;
     627             :         }
     628             : 
     629           0 :         return write(f, buf + i, sizeof(buf) - i);
     630             : }
     631             : 
     632           0 : void malloc_fail(size_t size)
     633             : {
     634             :         /* don't use printf to avoid any possible extra allocation */
     635             :         /* LCOV_EXCL_START */
     636             :         int f = 2; /* stderr */
     637             : 
     638             :         malloc_print(f, "Failed for Low Memory!\n");
     639             :         malloc_print(f, "Allocating ");
     640             :         malloc_printn(f, size);
     641             :         malloc_print(f, " bytes.\n");
     642             :         malloc_print(f, "Already allocated ");
     643             :         malloc_printn(f, malloc_counter());
     644             :         malloc_print(f, " bytes.\n");
     645             :         if (sizeof(void*) == 4) {
     646             :                 malloc_print(f, "You are currently using a 32 bits executable.\n");
     647             :                 malloc_print(f, "If you have more than 4GB of memory, please upgrade to a 64 bits one.\n");
     648             :         }
     649             :         /* LCOV_EXCL_STOP */
     650           0 : }
     651             : 
     652    10377145 : void* malloc_nofail(size_t size)
     653             : {
     654    10377145 :         void* ptr = malloc(size);
     655             : 
     656    10377145 :         if (!ptr) {
     657             :                 /* LCOV_EXCL_START */
     658             :                 malloc_fail(size);
     659             :                 exit(EXIT_FAILURE);
     660             :                 /* LCOV_EXCL_STOP */
     661             :         }
     662             : 
     663             : #ifndef CHECKER /* Don't preinitialize when running for valgrind */
     664             :         /* Here we preinitialize the memory to ensure that the OS is really allocating it */
     665             :         /* and not only reserving the addressable space. */
     666             :         /* Otherwise we are risking that the OOM (Out Of Memory) killer in Linux will kill the process. */
     667             :         /* Filling the memory doesn't ensure to disable OOM, but it increase a lot the chances to */
     668             :         /* get a real error from malloc() instead than a process killed. */
     669             :         /* Note that calloc() doesn't have the same effect. */
     670    10377145 :         memset(ptr, 0xA5, size);
     671             : #endif
     672             : 
     673    10377145 :         mcounter += size;
     674             : 
     675    10377145 :         return ptr;
     676             : }
     677             : 
     678       13324 : void* calloc_nofail(size_t count, size_t size)
     679             : {
     680             :         void* ptr;
     681             : 
     682       13324 :         size *= count;
     683             : 
     684             :         /* see the note in malloc_nofail() of why we don't use calloc() */
     685       13324 :         ptr = malloc(size);
     686             : 
     687       13324 :         if (!ptr) {
     688             :                 /* LCOV_EXCL_START */
     689             :                 malloc_fail(size);
     690             :                 exit(EXIT_FAILURE);
     691             :                 /* LCOV_EXCL_STOP */
     692             :         }
     693             : 
     694       13324 :         memset(ptr, 0, size);
     695             : 
     696       13324 :         mcounter += size;
     697             : 
     698       13324 :         return ptr;
     699             : }
     700             : 
     701     5657552 : char* strdup_nofail(const char* str)
     702             : {
     703             :         size_t size;
     704             :         char* ptr;
     705             : 
     706     5657552 :         size = strlen(str) + 1;
     707             : 
     708     5657552 :         ptr = malloc(size);
     709             : 
     710     5657552 :         if (!ptr) {
     711             :                 /* LCOV_EXCL_START */
     712             :                 malloc_fail(size);
     713             :                 exit(EXIT_FAILURE);
     714             :                 /* LCOV_EXCL_STOP */
     715             :         }
     716             : 
     717     5657552 :         memcpy(ptr, str, size);
     718             : 
     719     5657552 :         mcounter += size;
     720             : 
     721     5657552 :         return ptr;
     722             : }
     723             : 
     724             : /****************************************************************************/
     725             : /* smartctl */
     726             : 
     727             : /**
     728             :  * Matches a string with the specified pattern.
     729             :  * Like sscanf() a space match any sequence of spaces.
     730             :  * Returns 0 if it matches.
     731             :  */
     732           0 : static int smatch(const char* str, const char* pattern)
     733             : {
     734           0 :         while (*pattern) {
     735           0 :                 if (isspace(*pattern)) {
     736           0 :                         ++pattern;
     737           0 :                         while (isspace(*str))
     738           0 :                                 ++str;
     739           0 :                 } else if (*pattern == *str) {
     740           0 :                         ++pattern;
     741           0 :                         ++str;
     742             :                 } else
     743           0 :                         return -1;
     744             :         }
     745             : 
     746           0 :         return 0;
     747             : }
     748             : 
     749          25 : int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* smart, char* serial)
     750             : {
     751             :         unsigned i;
     752             :         int inside;
     753             : 
     754             :         /* preclear attribute */
     755          25 :         *serial = 0;
     756        6525 :         for (i = 0; i < SMART_COUNT; ++i)
     757        6500 :                 smart[i] = SMART_UNASSIGNED;
     758             : 
     759             :         /* read the file */
     760          25 :         inside = 0;
     761             :         while (1) {
     762             :                 char buf[256];
     763             :                 char attr[64];
     764             :                 unsigned id;
     765             :                 uint64_t raw;
     766             :                 char* s;
     767             : 
     768          25 :                 s = fgets(buf, sizeof(buf), f);
     769          25 :                 if (s == 0)
     770          25 :                         break;
     771             : 
     772             :                 /* remove extraneous chars */
     773           0 :                 s = polish(buf);
     774             : 
     775           0 :                 log_tag("smartctl:%s:%s:out: %s\n", file, name, s);
     776             : 
     777             :                 /* skip initial spaces */
     778           0 :                 while (isspace(*s))
     779           0 :                         ++s;
     780             : 
     781           0 :                 if (*s == 0) {
     782           0 :                         inside = 0;
     783           0 :                 } else if (smatch(s, "ID#") == 0) {
     784           0 :                         inside = 1;
     785           0 :                 } else if (smatch(s, "No Errors Logged") == 0) {
     786           0 :                         smart[SMART_ERROR] = 0;
     787           0 :                 } else if (sscanf(s, "ATA Error Count: %" SCNu64, &raw) == 1) {
     788           0 :                         smart[SMART_ERROR] = raw;
     789           0 :                 } else if (sscanf(s, "Serial Number: %63s", serial) == 1) {
     790           0 :                 } else if (smatch(s, "Rotation Rate: Solid State") == 0) {
     791           0 :                         smart[SMART_ROTATION_RATE] = 0;
     792           0 :                 } else if (sscanf(s, "Rotation Rate: %" SCNu64, &smart[SMART_ROTATION_RATE]) == 1) {
     793           0 :                 } else if (sscanf(s, "User Capacity: %63s", attr) == 1) {
     794           0 :                         smart[SMART_SIZE] = 0;
     795           0 :                         for (i = 0; attr[i]; ++i) {
     796           0 :                                 if (isdigit(attr[i])) {
     797           0 :                                         smart[SMART_SIZE] *= 10;
     798           0 :                                         smart[SMART_SIZE] += attr[i] - '0';
     799             :                                 }
     800             :                         }
     801           0 :                 } else if (inside) {
     802           0 :                         if (sscanf(s, "%u %*s %*s %*s %*s %*s %*s %*s %*s %" SCNu64, &id, &raw) != 2) {
     803             :                                 /* LCOV_EXCL_START */
     804             :                                 log_fatal("Invalid smartctl line '%s'.\n", s);
     805             :                                 return -1;
     806             :                                 /* LCOV_EXCL_STOP */
     807             :                         }
     808             : 
     809           0 :                         if (id >= 256) {
     810             :                                 /* LCOV_EXCL_START */
     811             :                                 log_fatal("Invalid SMART id '%u'.\n", id);
     812             :                                 return -1;
     813             :                                 /* LCOV_EXCL_STOP */
     814             :                         }
     815             : 
     816           0 :                         smart[id] = raw;
     817             :                 }
     818           0 :         }
     819             : 
     820          25 :         return 0;
     821             : }
     822             : 
     823          12 : int smartctl_flush(FILE* f, const char* file, const char* name)
     824             : {
     825             :         /* read the file */
     826             :         while (1) {
     827             :                 char buf[256];
     828             :                 char* s;
     829             : 
     830          12 :                 s = fgets(buf, sizeof(buf), f);
     831          12 :                 if (s == 0)
     832          12 :                         break;
     833             : 
     834             :                 /* remove extraneous chars */
     835           0 :                 s = polish(buf);
     836             : 
     837           0 :                 log_tag("smartctl:%s:%s:out: %s\n", file, name, s);
     838           0 :         }
     839             : 
     840          24 :         return 0;
     841             : }
     842             : 

Generated by: LCOV version 1.10