LCOV - code coverage report
Current view: top level - cmdline - support.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 240 301 79.7 %
Date: 2016-01-16 Functions: 30 32 93.8 %

          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         830 : void log_fatal(const char* format, ...)
      58             : {
      59             :         va_list ap;
      60             : 
      61         830 :         pthread_mutex_lock(&msg_lock);
      62             : 
      63         830 :         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         830 :         va_start(ap, format);
      72         830 :         vfprintf(stderr, format, ap);
      73         830 :         fflush(stderr);
      74         830 :         va_end(ap);
      75             : 
      76         830 :         pthread_mutex_unlock(&msg_lock);
      77         830 : }
      78             : 
      79      614775 : void log_error(const char* format, ...)
      80             : {
      81             :         va_list ap;
      82             : 
      83      614775 :         pthread_mutex_lock(&msg_lock);
      84             : 
      85      614775 :         if (stdlog) {
      86      612895 :                 va_start(ap, format);
      87      612895 :                 fprintf(stdlog, "msg:error: ");
      88      612895 :                 vfprintf(stdlog, format, ap);
      89      612895 :                 fflush(stdlog);
      90      612895 :                 va_end(ap);
      91             :         } else {
      92        1880 :                 va_start(ap, format);
      93        1880 :                 vfprintf(stderr, format, ap);
      94        1880 :                 fflush(stderr);
      95        1880 :                 va_end(ap);
      96             :         }
      97             : 
      98      614775 :         pthread_mutex_unlock(&msg_lock);
      99      614775 : }
     100             : 
     101       11254 : void log_expected(const char* format, ...)
     102             : {
     103             :         va_list ap;
     104             : 
     105       11254 :         pthread_mutex_lock(&msg_lock);
     106             : 
     107       11254 :         if (stdlog) {
     108       11254 :                 va_start(ap, format);
     109       11254 :                 fprintf(stdlog, "msg:expected: ");
     110       11254 :                 vfprintf(stdlog, format, ap);
     111       11254 :                 fflush(stdlog);
     112       11254 :                 va_end(ap);
     113             :         }
     114             : 
     115       11254 :         pthread_mutex_unlock(&msg_lock);
     116       11254 : }
     117             : 
     118     4055558 : void log_tag(const char* format, ...)
     119             : {
     120             :         va_list ap;
     121             : 
     122     4055558 :         pthread_mutex_lock(&msg_lock);
     123             : 
     124     4055558 :         if (stdlog) {
     125     2975578 :                 va_start(ap, format);
     126     2975578 :                 vfprintf(stdlog, format, ap);
     127             :                 /* here we intentionally don't flush */
     128             :                 /* to make the output faster */
     129     2975578 :                 va_end(ap);
     130             :         }
     131             : 
     132     4055558 :         pthread_mutex_unlock(&msg_lock);
     133     4055558 : }
     134             : 
     135        1158 : void log_flush(void)
     136             : {
     137        1158 :         pthread_mutex_lock(&msg_lock);
     138             : 
     139        1158 :         if (stdlog)
     140         326 :                 fflush(stdlog);
     141        1158 :         fflush(stdout);
     142        1158 :         fflush(stderr);
     143             : 
     144        1158 :         pthread_mutex_unlock(&msg_lock);
     145        1158 : }
     146             : 
     147         579 : void msg_status(const char* format, ...)
     148             : {
     149             :         va_list ap;
     150             : 
     151         579 :         pthread_mutex_lock(&msg_lock);
     152             : 
     153         579 :         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         579 :         if (msg_level >= MSG_STATUS) {
     162         579 :                 va_start(ap, format);
     163         579 :                 vfprintf(stdout, format, ap);
     164         579 :                 fflush(stdout);
     165         579 :                 va_end(ap);
     166             :         }
     167             : 
     168         579 :         pthread_mutex_unlock(&msg_lock);
     169         579 : }
     170             : 
     171      295917 : void msg_info(const char* format, ...)
     172             : {
     173             :         va_list ap;
     174             : 
     175      295917 :         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      295917 :         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      295917 :         pthread_mutex_unlock(&msg_lock);
     188      295917 : }
     189             : 
     190        4207 : void msg_progress(const char* format, ...)
     191             : {
     192             :         va_list ap;
     193             : 
     194        4207 :         pthread_mutex_lock(&msg_lock);
     195             : 
     196        4207 :         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        4207 :         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        4207 :         pthread_mutex_unlock(&msg_lock);
     212        4207 : }
     213             : 
     214         994 : void msg_bar(const char* format, ...)
     215             : {
     216             :         va_list ap;
     217             : 
     218         994 :         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         994 :         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         994 :         pthread_mutex_unlock(&msg_lock);
     231         994 : }
     232             : 
     233      126828 : void msg_verbose(const char* format, ...)
     234             : {
     235             :         va_list ap;
     236             : 
     237      126828 :         pthread_mutex_lock(&msg_lock);
     238             : 
     239      126828 :         if (stdlog) {
     240      125018 :                 va_start(ap, format);
     241      125018 :                 fprintf(stdlog, "msg:verbose: ");
     242      125018 :                 vfprintf(stdlog, format, ap);
     243      125018 :                 fflush(stdlog);
     244      125018 :                 va_end(ap);
     245             :         }
     246             : 
     247      126828 :         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      126828 :         pthread_mutex_unlock(&msg_lock);
     255      126828 : }
     256             : 
     257         460 : void msg_flush(void)
     258             : {
     259         460 :         pthread_mutex_lock(&msg_lock);
     260             : 
     261         460 :         fflush(stdout);
     262         460 :         fflush(stderr);
     263             : 
     264         460 :         pthread_mutex_unlock(&msg_lock);
     265         460 : }
     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     3608964 : const char* esc(const char* str)
     293             : {
     294     3608964 :         char* begin = esc_table[esc_index];
     295     3608964 :         char* end = begin + PATH_MAX;
     296     3608964 :         char* p = begin;
     297             : 
     298             :         /* next call uses the next buffer */
     299     3608964 :         esc_index = (esc_index + 1) % ESC_MAX;
     300             : 
     301             :         /* copy string with escaping */
     302    53928048 :         while (*str) {
     303    46710120 :                 char c = *str;
     304             : 
     305    46710120 :                 switch (c) {
     306             : 
     307           0 :                 ESCAPE('\n', 'n');
     308           0 :                 ESCAPE('\r', 'r');
     309           0 :                 ESCAPE(':', 'd');
     310           0 :                 ESCAPE('\\', '\\');
     311             : 
     312             :                 default:
     313    46710120 :                         if (p == end)
     314           0 :                                 goto bail;
     315    46710120 :                         *p++ = c;
     316    46710120 :                         break;
     317             :                 }
     318             : 
     319    46710120 :                 ++str;
     320             :         }
     321             : 
     322             :         /* put final 0 */
     323     3608964 :         if (p == end)
     324           0 :                 goto bail;
     325     3608964 :         *p = 0;
     326             : 
     327     7217928 :         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     6957833 : void pathcpy(char* dst, size_t size, const char* src)
     353             : {
     354     6957833 :         size_t len = strlen(src);
     355             : 
     356     6957833 :         if (len + 1 > size) {
     357             :                 /* LCOV_EXCL_START */
     358             :                 log_fatal("Path too long '%s'\n", src);
     359             :                 os_abort();
     360             :                 /* LCOV_EXCL_STOP */
     361             :         }
     362             : 
     363     6957833 :         memcpy(dst, src, len + 1);
     364     6957833 : }
     365             : 
     366         258 : void pathcat(char* dst, size_t size, const char* src)
     367             : {
     368         258 :         size_t dst_len = strlen(dst);
     369         258 :         size_t src_len = strlen(src);
     370             : 
     371         258 :         if (dst_len + src_len + 1 > size) {
     372             :                 /* LCOV_EXCL_START */
     373             :                 log_fatal("Path too long '%s%s'\n", dst, src);
     374             :                 os_abort();
     375             :                 /* LCOV_EXCL_STOP */
     376             :         }
     377             : 
     378         258 :         memcpy(dst + dst_len, src, src_len + 1);
     379         258 : }
     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 '%s%c'\n", dst, c);
     388             :                 os_abort();
     389             :                 /* LCOV_EXCL_STOP */
     390             :         }
     391             : 
     392         788 :         dst[dst_len] = c;
     393         788 :         dst[dst_len + 1] = 0;
     394         788 : }
     395             : 
     396        9262 : void pathimport(char* dst, size_t size, const char* src)
     397             : {
     398        9262 :         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        9262 : }
     409             : 
     410       17589 : void pathexport(char* dst, size_t size, const char* src)
     411             : {
     412       17589 :         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       17589 : }
     423             : 
     424    50920352 : void pathprint(char* dst, size_t size, const char* format, ...)
     425             : {
     426             :         size_t len;
     427             :         va_list ap;
     428             : 
     429    50920352 :         va_start(ap, format);
     430    50920352 :         len = vsnprintf(dst, size, format, ap);
     431    50920352 :         va_end(ap);
     432             : 
     433    50920352 :         if (len >= size) {
     434             :                 /* LCOV_EXCL_START */
     435             :                 if (size > 0) {
     436             :                         dst[size - 1] = 0;
     437             :                         log_fatal("Path too long '%s...'\n", dst);
     438             :                 } else {
     439             :                         log_fatal("Path too long for empty size'\n");
     440             :                 }
     441             :                 os_abort();
     442             :                 /* LCOV_EXCL_STOP */
     443             :         }
     444    50920352 : }
     445             : 
     446        7481 : void pathslash(char* dst, size_t size)
     447             : {
     448        7481 :         size_t len = strlen(dst);
     449             : 
     450        7481 :         if (len > 0 && dst[len - 1] != '/') {
     451        5944 :                 if (len + 2 >= size) {
     452             :                         /* LCOV_EXCL_START */
     453             :                         log_fatal("Path too long '%s/'\n", dst);
     454             :                         os_abort();
     455             :                         /* LCOV_EXCL_STOP */
     456             :                 }
     457             : 
     458        5944 :                 dst[len] = '/';
     459        5944 :                 dst[len + 1] = 0;
     460             :         }
     461        7481 : }
     462             : 
     463          30 : void pathcut(char* dst)
     464             : {
     465          30 :         char* slash = strrchr(dst, '/');
     466          30 :         if (slash)
     467          30 :                 slash[1] = 0;
     468             :         else
     469           0 :                 dst[0] = 0;
     470          30 : }
     471             : 
     472    51339645 : int pathcmp(const char* a, const char* b)
     473             : {
     474             : #ifdef _WIN32
     475             :         char ai[PATH_MAX];
     476             :         char bi[PATH_MAX];
     477             : 
     478             :         /* import to convert \ to / */
     479             :         pathimport(ai, sizeof(ai), a);
     480             :         pathimport(bi, sizeof(bi), b);
     481             : 
     482             :         /* case insensitive compare in Windows */
     483             :         return stricmp(ai, bi);
     484             : #else
     485    51339645 :         return strcmp(a, b);
     486             : #endif
     487             : }
     488             : 
     489             : /****************************************************************************/
     490             : /* file-system */
     491             : 
     492      793150 : int mkancestor(const char* file)
     493             : {
     494             :         char dir[PATH_MAX];
     495             :         struct stat st;
     496             :         char* c;
     497             : 
     498      793150 :         pathcpy(dir, sizeof(dir), file);
     499             : 
     500      793150 :         c = strrchr(dir, '/');
     501      793150 :         if (!c) {
     502             :                 /* no ancestor */
     503           0 :                 return 0;
     504             :         }
     505             : 
     506             :         /* clear the file */
     507      793150 :         *c = 0;
     508             : 
     509             :         /* if it's the root dir */
     510      793150 :         if (*dir == 0) {
     511             :                 /* nothing more to do */
     512           0 :                 return 0;
     513             :         }
     514             : 
     515             : #ifdef _WIN32
     516             :         /* if it's a drive specificaion like "C:" */
     517             :         if (isalpha(dir[0]) && dir[1] == ':' && dir[2] == 0) {
     518             :                 /* nothing more to do */
     519             :                 return 0;
     520             :         }
     521             : #endif
     522             : 
     523             :         /*
     524             :          * Check if the dir already exists using lstat().
     525             :          *
     526             :          * Note that in Windows when dealing with read-only media
     527             :          * you cannot try to create the directory, and expecting
     528             :          * the EEXIST error because the call will fail with ERROR_WRITE_PROTECTED.
     529             :          *
     530             :          * Also in Windows it's better to use lstat() than stat() because it
     531             :          * doen't need to open the dir with CreateFile().
     532             :          */
     533      793150 :         if (lstat(dir, &st) == 0) {
     534             :                 /* it already exists */
     535      793070 :                 return 0;
     536             :         }
     537             : 
     538             :         /* recursively create them all */
     539          80 :         if (mkancestor(dir) != 0) {
     540             :                 /* LCOV_EXCL_START */
     541             :                 return -1;
     542             :                 /* LCOV_EXCL_STOP */
     543             :         }
     544             : 
     545             :         /* create it */
     546          80 :         if (mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
     547             :                 /* LCOV_EXCL_START */
     548             :                 log_fatal("Error creating directory '%s'. %s.\n", dir, strerror(errno));
     549             :                 return -1;
     550             :                 /* LCOV_EXCL_STOP */
     551             :         }
     552             : 
     553          80 :         return 0;
     554             : }
     555             : 
     556      119699 : int fmtime(int f, int64_t mtime_sec, int mtime_nsec)
     557             : {
     558             : #if HAVE_FUTIMENS
     559             :         struct timespec tv[2];
     560             : #else
     561             :         struct timeval tv[2];
     562             : #endif
     563             :         int ret;
     564             : 
     565             : #if HAVE_FUTIMENS /* futimens() is preferred because it gives nanosecond precision */
     566      119699 :         tv[0].tv_sec = mtime_sec;
     567      119699 :         if (mtime_nsec != STAT_NSEC_INVALID)
     568      119699 :                 tv[0].tv_nsec = mtime_nsec;
     569             :         else
     570           0 :                 tv[0].tv_nsec = 0;
     571      119699 :         tv[1].tv_sec = tv[0].tv_sec;
     572      119699 :         tv[1].tv_nsec = tv[0].tv_nsec;
     573             : 
     574      119699 :         ret = futimens(f, tv);
     575             : #elif HAVE_FUTIMES /* fallback to futimes() if nanosecond precision is not available */
     576             :         tv[0].tv_sec = mtime_sec;
     577             :         if (mtime_nsec != STAT_NSEC_INVALID)
     578             :                 tv[0].tv_usec = mtime_nsec / 1000;
     579             :         else
     580             :                 tv[0].tv_usec = 0;
     581             :         tv[1].tv_sec = tv[0].tv_sec;
     582             :         tv[1].tv_usec = tv[0].tv_usec;
     583             : 
     584             :         ret = futimes(f, tv);
     585             : #elif HAVE_FUTIMESAT /* fallback to futimesat() for Solaris, it only has futimesat() */
     586             :         tv[0].tv_sec = mtime_sec;
     587             :         if (mtime_nsec != STAT_NSEC_INVALID)
     588             :                 tv[0].tv_usec = mtime_nsec / 1000;
     589             :         else
     590             :                 tv[0].tv_usec = 0;
     591             :         tv[1].tv_sec = tv[0].tv_sec;
     592             :         tv[1].tv_usec = tv[0].tv_usec;
     593             : 
     594             :         ret = futimesat(f, 0, tv);
     595             : #else
     596             : #error No function available to set file timestamps with sub-second precision
     597             : #endif
     598             : 
     599      119699 :         return ret;
     600             : }
     601             : 
     602             : /****************************************************************************/
     603             : /* memory */
     604             : 
     605             : /**
     606             :  * Total amount of memory allocated.
     607             :  */
     608             : static size_t mcounter;
     609             : 
     610             : /**
     611             :  * Mutex protection for the global memory counter.
     612             :  */
     613             : #if HAVE_PTHREAD
     614             : pthread_mutex_t mcounter_mutex = PTHREAD_MUTEX_INITIALIZER;
     615             : #endif
     616             : 
     617         464 : size_t malloc_counter_get(void)
     618             : {
     619             :         size_t ret;
     620             : #if HAVE_PTHREAD
     621         464 :         pthread_mutex_lock(&mcounter_mutex);
     622             : #endif
     623         464 :         ret = mcounter;
     624             : #if HAVE_PTHREAD
     625         464 :         pthread_mutex_unlock(&mcounter_mutex);
     626             : #endif
     627         464 :         return ret;
     628             : }
     629             : 
     630    19797017 : void malloc_counter_inc(size_t inc)
     631             : {
     632             : #if HAVE_PTHREAD
     633    19797017 :         pthread_mutex_lock(&mcounter_mutex);
     634             : #endif
     635    19797017 :         mcounter += inc;
     636             : #if HAVE_PTHREAD
     637    19797017 :         pthread_mutex_unlock(&mcounter_mutex);
     638             : #endif
     639    19797017 : }
     640             : 
     641             : /* LCOV_EXCL_START */
     642             : static ssize_t malloc_print(int f, const char* str)
     643             : {
     644             :         ssize_t len = 0;
     645             :         while (str[len])
     646             :                 ++len;
     647             :         return write(f, str, len);
     648             : }
     649             : /* LCOV_EXCL_STOP */
     650             : 
     651             : /* LCOV_EXCL_START */
     652             : static ssize_t malloc_printn(int f, size_t value)
     653             : {
     654             :         char buf[32];
     655             :         int i;
     656             : 
     657             :         if (!value)
     658             :                 return write(f, "0", 1);
     659             : 
     660             :         i = sizeof(buf);
     661             :         while (value) {
     662             :                 buf[--i] = (value % 10) + '0';
     663             :                 value /= 10;
     664             :         }
     665             : 
     666             :         return write(f, buf + i, sizeof(buf) - i);
     667             : }
     668             : /* LCOV_EXCL_STOP */
     669             : 
     670             : /* LCOV_EXCL_START */
     671             : void malloc_fail(size_t size)
     672             : {
     673             :         /* don't use printf to avoid any possible extra allocation */
     674             :         int f = 2; /* stderr */
     675             : 
     676             :         malloc_print(f, "Failed for Low Memory!\n");
     677             :         malloc_print(f, "Allocating ");
     678             :         malloc_printn(f, size);
     679             :         malloc_print(f, " bytes.\n");
     680             :         malloc_print(f, "Already allocated ");
     681             :         malloc_printn(f, malloc_counter_get());
     682             :         malloc_print(f, " bytes.\n");
     683             :         if (sizeof(void*) == 4) {
     684             :                 malloc_print(f, "You are currently using a 32 bits executable.\n");
     685             :                 malloc_print(f, "If you have more than 4GB of memory, please upgrade to a 64 bits one.\n");
     686             :         }
     687             : }
     688             : /* LCOV_EXCL_STOP */
     689             : 
     690    14070101 : void* malloc_nofail(size_t size)
     691             : {
     692    14070101 :         void* ptr = malloc(size);
     693             : 
     694    14070101 :         if (!ptr) {
     695             :                 /* LCOV_EXCL_START */
     696             :                 malloc_fail(size);
     697             :                 exit(EXIT_FAILURE);
     698             :                 /* LCOV_EXCL_STOP */
     699             :         }
     700             : 
     701             : #ifndef CHECKER /* Don't preinitialize when running for valgrind */
     702             :         /* Here we preinitialize the memory to ensure that the OS is really allocating it */
     703             :         /* and not only reserving the addressable space. */
     704             :         /* Otherwise we are risking that the OOM (Out Of Memory) killer in Linux will kill the process. */
     705             :         /* Filling the memory doesn't ensure to disable OOM, but it increase a lot the chances to */
     706             :         /* get a real error from malloc() instead than a process killed. */
     707             :         /* Note that calloc() doesn't have the same effect. */
     708    14070101 :         memset(ptr, 0xA5, size);
     709             : #endif
     710             : 
     711    14070101 :         malloc_counter_inc(size);
     712             : 
     713    14070101 :         return ptr;
     714             : }
     715             : 
     716        9648 : void* calloc_nofail(size_t count, size_t size)
     717             : {
     718             :         void* ptr;
     719             : 
     720        9648 :         size *= count;
     721             : 
     722             :         /* see the note in malloc_nofail() of why we don't use calloc() */
     723        9648 :         ptr = malloc(size);
     724             : 
     725        9648 :         if (!ptr) {
     726             :                 /* LCOV_EXCL_START */
     727             :                 malloc_fail(size);
     728             :                 exit(EXIT_FAILURE);
     729             :                 /* LCOV_EXCL_STOP */
     730             :         }
     731             : 
     732        9648 :         memset(ptr, 0, size);
     733             : 
     734        9648 :         malloc_counter_inc(size);
     735             : 
     736        9648 :         return ptr;
     737             : }
     738             : 
     739     5717268 : char* strdup_nofail(const char* str)
     740             : {
     741             :         size_t size;
     742             :         char* ptr;
     743             : 
     744     5717268 :         size = strlen(str) + 1;
     745             : 
     746     5717268 :         ptr = malloc(size);
     747             : 
     748     5717268 :         if (!ptr) {
     749             :                 /* LCOV_EXCL_START */
     750             :                 malloc_fail(size);
     751             :                 exit(EXIT_FAILURE);
     752             :                 /* LCOV_EXCL_STOP */
     753             :         }
     754             : 
     755     5717268 :         memcpy(ptr, str, size);
     756             : 
     757     5717268 :         malloc_counter_inc(size);
     758             : 
     759     5717268 :         return ptr;
     760             : }
     761             : 
     762             : /****************************************************************************/
     763             : /* smartctl */
     764             : 
     765             : /**
     766             :  * Match a string with the specified pattern.
     767             :  * Like sscanf() a space match any sequence of spaces.
     768             :  * Return 0 if it matches.
     769             :  */
     770           0 : static int smatch(const char* str, const char* pattern)
     771             : {
     772           0 :         while (*pattern) {
     773           0 :                 if (isspace(*pattern)) {
     774           0 :                         ++pattern;
     775           0 :                         while (isspace(*str))
     776           0 :                                 ++str;
     777           0 :                 } else if (*pattern == *str) {
     778           0 :                         ++pattern;
     779           0 :                         ++str;
     780             :                 } else
     781           0 :                         return -1;
     782             :         }
     783             : 
     784           0 :         return 0;
     785             : }
     786             : 
     787          25 : int smartctl_attribute(FILE* f, const char* file, const char* name, uint64_t* smart, char* serial)
     788             : {
     789             :         unsigned i;
     790             :         int inside;
     791             : 
     792             :         /* preclear attribute */
     793          25 :         *serial = 0;
     794        6525 :         for (i = 0; i < SMART_COUNT; ++i)
     795        6500 :                 smart[i] = SMART_UNASSIGNED;
     796             : 
     797             :         /* read the file */
     798          25 :         inside = 0;
     799             :         while (1) {
     800             :                 char buf[256];
     801             :                 char attr[64];
     802             :                 unsigned id;
     803             :                 uint64_t raw;
     804             :                 char* s;
     805             : 
     806          25 :                 s = fgets(buf, sizeof(buf), f);
     807          25 :                 if (s == 0)
     808          25 :                         break;
     809             : 
     810             :                 /* remove extraneous chars */
     811           0 :                 s = polish(buf);
     812             : 
     813           0 :                 log_tag("smartctl:%s:%s:out: %s\n", file, name, s);
     814             : 
     815             :                 /* skip initial spaces */
     816           0 :                 while (isspace(*s))
     817           0 :                         ++s;
     818             : 
     819           0 :                 if (*s == 0) {
     820           0 :                         inside = 0;
     821           0 :                 } else if (smatch(s, "ID#") == 0) {
     822           0 :                         inside = 1;
     823           0 :                 } else if (smatch(s, "No Errors Logged") == 0) {
     824           0 :                         smart[SMART_ERROR] = 0;
     825           0 :                 } else if (sscanf(s, "ATA Error Count: %" SCNu64, &raw) == 1) {
     826           0 :                         smart[SMART_ERROR] = raw;
     827           0 :                 } else if (sscanf(s, "Serial Number: %63s", serial) == 1) {
     828           0 :                 } else if (smatch(s, "Rotation Rate: Solid State") == 0) {
     829           0 :                         smart[SMART_ROTATION_RATE] = 0;
     830           0 :                 } else if (sscanf(s, "Rotation Rate: %" SCNu64, &smart[SMART_ROTATION_RATE]) == 1) {
     831           0 :                 } else if (sscanf(s, "User Capacity: %63s", attr) == 1) {
     832           0 :                         smart[SMART_SIZE] = 0;
     833           0 :                         for (i = 0; attr[i]; ++i) {
     834           0 :                                 if (isdigit(attr[i])) {
     835           0 :                                         smart[SMART_SIZE] *= 10;
     836           0 :                                         smart[SMART_SIZE] += attr[i] - '0';
     837             :                                 }
     838             :                         }
     839           0 :                 } else if (inside) {
     840           0 :                         if (sscanf(s, "%u %*s %*s %*s %*s %*s %*s %*s %*s %" SCNu64, &id, &raw) != 2) {
     841             :                                 /* LCOV_EXCL_START */
     842             :                                 log_fatal("Invalid smartctl line '%s'.\n", s);
     843             :                                 return -1;
     844             :                                 /* LCOV_EXCL_STOP */
     845             :                         }
     846             : 
     847           0 :                         if (id >= 256) {
     848             :                                 /* LCOV_EXCL_START */
     849             :                                 log_fatal("Invalid SMART id '%u'.\n", id);
     850             :                                 return -1;
     851             :                                 /* LCOV_EXCL_STOP */
     852             :                         }
     853             : 
     854           0 :                         smart[id] = raw;
     855             :                 }
     856           0 :         }
     857             : 
     858          25 :         return 0;
     859             : }
     860             : 
     861          12 : int smartctl_flush(FILE* f, const char* file, const char* name)
     862             : {
     863             :         /* read the file */
     864             :         while (1) {
     865             :                 char buf[256];
     866             :                 char* s;
     867             : 
     868          12 :                 s = fgets(buf, sizeof(buf), f);
     869          12 :                 if (s == 0)
     870          12 :                         break;
     871             : 
     872             :                 /* remove extraneous chars */
     873           0 :                 s = polish(buf);
     874             : 
     875           0 :                 log_tag("smartctl:%s:%s:out: %s\n", file, name, s);
     876           0 :         }
     877             : 
     878          24 :         return 0;
     879             : }
     880             : 

Generated by: LCOV version 1.10