LCOV - code coverage report
Current view: top level - lib/dpkg - log.c (source / functions) Hit Total Coverage
Test: dpkg 1.21.11 C code coverage Lines: 0 47 0.0 %
Date: 2022-12-03 00:40:01 Functions: 0 3 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 16 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * libdpkg - Debian packaging suite library routines
       3                 :            :  * log.c - logging related functions
       4                 :            :  *
       5                 :            :  * Copyright © 2005 Scott James Remnant <scott@netsplit.com>
       6                 :            :  *
       7                 :            :  * This is free software; you can redistribute it and/or modify
       8                 :            :  * it under the terms of the GNU General Public License as published by
       9                 :            :  * the Free Software Foundation; either version 2 of the License, or
      10                 :            :  * (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :            :  * GNU General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU General Public License
      18                 :            :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      19                 :            :  */
      20                 :            : 
      21                 :            : #include <config.h>
      22                 :            : #include <compat.h>
      23                 :            : 
      24                 :            : #include <sys/types.h>
      25                 :            : #include <sys/stat.h>
      26                 :            : 
      27                 :            : #include <errno.h>
      28                 :            : #include <time.h>
      29                 :            : #include <fcntl.h>
      30                 :            : #include <unistd.h>
      31                 :            : #include <stdarg.h>
      32                 :            : 
      33                 :            : #include <dpkg/i18n.h>
      34                 :            : #include <dpkg/dpkg.h>
      35                 :            : #include <dpkg/dpkg-db.h>
      36                 :            : #include <dpkg/fdio.h>
      37                 :            : 
      38                 :            : const char *log_file = NULL;
      39                 :            : 
      40                 :            : void
      41                 :          0 : log_message(const char *fmt, ...)
      42                 :            : {
      43                 :            :         static struct varbuf log;
      44                 :            :         static int logfd = -1;
      45                 :            :         char time_str[20];
      46                 :            :         time_t now;
      47                 :            :         struct tm tm;
      48                 :            :         va_list args;
      49                 :            : 
      50         [ #  # ]:          0 :         if (!log_file)
      51                 :          0 :                 return;
      52                 :            : 
      53         [ #  # ]:          0 :         if (logfd < 0) {
      54                 :          0 :                 logfd = open(log_file, O_WRONLY | O_CREAT | O_APPEND, 0644);
      55         [ #  # ]:          0 :                 if (logfd < 0) {
      56                 :          0 :                         notice(_("could not open log '%s': %s"),
      57                 :          0 :                                log_file, strerror(errno));
      58                 :          0 :                         log_file = NULL;
      59                 :          0 :                         return;
      60                 :            :                 }
      61                 :          0 :                 setcloexec(logfd, log_file);
      62                 :            :         }
      63                 :            : 
      64                 :          0 :         time(&now);
      65         [ #  # ]:          0 :         if (localtime_r(&now, &tm) == NULL) {
      66                 :          0 :                 notice(_("cannot get local time to log into '%s': %s"),
      67                 :          0 :                        log_file, strerror(errno));
      68                 :          0 :                 return;
      69                 :            :         }
      70                 :          0 :         strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", &tm);
      71                 :            : 
      72                 :          0 :         va_start(args, fmt);
      73                 :          0 :         varbuf_reset(&log);
      74                 :          0 :         varbuf_add_str(&log, time_str);
      75                 :          0 :         varbuf_add_char(&log, ' ');
      76                 :          0 :         varbuf_vprintf(&log, fmt, args);
      77                 :          0 :         varbuf_add_char(&log, '\n');
      78                 :          0 :         varbuf_end_str(&log);
      79                 :          0 :         va_end(args);
      80                 :            : 
      81         [ #  # ]:          0 :         if (fd_write(logfd, log.buf, log.used) < 0)
      82                 :          0 :                 notice(_("cannot write to log file '%s': %s"),
      83                 :          0 :                        log_file, strerror(errno));
      84                 :            : }
      85                 :            : 
      86                 :            : struct pipef {
      87                 :            :         struct pipef *next;
      88                 :            :         int fd;
      89                 :            : };
      90                 :            : 
      91                 :            : static struct pipef *status_pipes = NULL;
      92                 :            : 
      93                 :            : void
      94                 :          0 : statusfd_add(int fd)
      95                 :            : {
      96                 :            :         struct pipef *pipe_new;
      97                 :            : 
      98                 :          0 :         setcloexec(fd, _("<package status and progress file descriptor>"));
      99                 :            : 
     100                 :          0 :         pipe_new = nfmalloc(sizeof(*pipe_new));
     101                 :          0 :         pipe_new->fd = fd;
     102                 :          0 :         pipe_new->next = status_pipes;
     103                 :          0 :         status_pipes = pipe_new;
     104                 :          0 : }
     105                 :            : 
     106                 :            : void
     107                 :          0 : statusfd_send(const char *fmt, ...)
     108                 :            : {
     109                 :            :         static struct varbuf vb;
     110                 :            :         struct pipef *pipef;
     111                 :            :         va_list args;
     112                 :            : 
     113         [ #  # ]:          0 :         if (!status_pipes)
     114                 :          0 :                 return;
     115                 :            : 
     116                 :          0 :         va_start(args, fmt);
     117                 :          0 :         varbuf_reset(&vb);
     118                 :          0 :         varbuf_vprintf(&vb, fmt, args);
     119                 :            :         /* Sanitize string to not include new lines, as front-ends should be
     120                 :            :          * doing their own word-wrapping. */
     121                 :          0 :         varbuf_map_char(&vb, '\n', ' ');
     122                 :          0 :         varbuf_add_char(&vb, '\n');
     123                 :          0 :         va_end(args);
     124                 :            : 
     125         [ #  # ]:          0 :         for (pipef = status_pipes; pipef; pipef = pipef->next) {
     126         [ #  # ]:          0 :                 if (fd_write(pipef->fd, vb.buf, vb.used) < 0)
     127                 :          0 :                         ohshite(_("unable to write to status fd %d"),
     128                 :            :                                 pipef->fd);
     129                 :            :         }
     130                 :            : }

Generated by: LCOV version 1.16