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_set_str(&log, time_str);
74 : 0 : varbuf_add_char(&log, ' ');
75 : 0 : varbuf_vprintf(&log, fmt, args);
76 : 0 : varbuf_add_char(&log, '\n');
77 : 0 : va_end(args);
78 : :
79 [ # # ]: 0 : if (fd_write(logfd, log.buf, log.used) < 0)
80 : 0 : notice(_("cannot write to log file '%s': %s"),
81 : 0 : log_file, strerror(errno));
82 : : }
83 : :
84 : : struct pipef {
85 : : struct pipef *next;
86 : : int fd;
87 : : };
88 : :
89 : : static struct pipef *status_pipes = NULL;
90 : :
91 : : void
92 : 0 : statusfd_add(int fd)
93 : : {
94 : : struct pipef *pipe_new;
95 : :
96 : 0 : setcloexec(fd, _("<package status and progress file descriptor>"));
97 : :
98 : 0 : pipe_new = nfmalloc(sizeof(*pipe_new));
99 : 0 : pipe_new->fd = fd;
100 : 0 : pipe_new->next = status_pipes;
101 : 0 : status_pipes = pipe_new;
102 : 0 : }
103 : :
104 : : void
105 : 0 : statusfd_send(const char *fmt, ...)
106 : : {
107 : : static struct varbuf vb;
108 : : struct pipef *pipef;
109 : : va_list args;
110 : :
111 [ # # ]: 0 : if (!status_pipes)
112 : 0 : return;
113 : :
114 : 0 : va_start(args, fmt);
115 : 0 : varbuf_reset(&vb);
116 : 0 : varbuf_vprintf(&vb, fmt, args);
117 : : /* Sanitize string to not include new lines, as front-ends should be
118 : : * doing their own word-wrapping. */
119 : 0 : varbuf_map_char(&vb, '\n', ' ');
120 : 0 : varbuf_add_char(&vb, '\n');
121 : 0 : va_end(args);
122 : :
123 [ # # ]: 0 : for (pipef = status_pipes; pipef; pipef = pipef->next) {
124 [ # # ]: 0 : if (fd_write(pipef->fd, vb.buf, vb.used) < 0)
125 : 0 : ohshite(_("unable to write to status fd %d"),
126 : : pipef->fd);
127 : : }
128 : : }
|