1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <fcntl.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 
7 #include "alloc-util.h"
8 #include "fd-util.h"
9 #include "io-util.h"
10 #include "parse-util.h"
11 #include "show-status.h"
12 #include "string-table.h"
13 #include "string-util.h"
14 #include "terminal-util.h"
15 #include "util.h"
16 
17 static const char* const show_status_table[_SHOW_STATUS_MAX] = {
18         [SHOW_STATUS_NO]        = "no",
19         [SHOW_STATUS_ERROR]     = "error",
20         [SHOW_STATUS_AUTO]      = "auto",
21         [SHOW_STATUS_TEMPORARY] = "temporary",
22         [SHOW_STATUS_YES]       = "yes",
23 };
24 
25 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(show_status, ShowStatus, SHOW_STATUS_YES);
26 
parse_show_status(const char * v,ShowStatus * ret)27 int parse_show_status(const char *v, ShowStatus *ret) {
28         ShowStatus s;
29 
30         assert(ret);
31 
32         s = show_status_from_string(v);
33         if (s < 0 || s == SHOW_STATUS_TEMPORARY)
34                 return -EINVAL;
35 
36         *ret = s;
37         return 0;
38 }
39 
status_vprintf(const char * status,ShowStatusFlags flags,const char * format,va_list ap)40 int status_vprintf(const char *status, ShowStatusFlags flags, const char *format, va_list ap) {
41         static const char status_indent[] = "         "; /* "[" STATUS "] " */
42         _cleanup_free_ char *s = NULL;
43         _cleanup_close_ int fd = -1;
44         struct iovec iovec[7] = {};
45         int n = 0;
46         static bool prev_ephemeral;
47 
48         assert(format);
49 
50         /* This is independent of logging, as status messages are
51          * optional and go exclusively to the console. */
52 
53         if (vasprintf(&s, format, ap) < 0)
54                 return log_oom();
55 
56         /* Before you ask: yes, on purpose we open/close the console for each status line we write individually. This
57          * is a good strategy to avoid PID 1 getting killed by the kernel's SAK concept (it doesn't fix this entirely,
58          * but minimizes the time window the kernel might end up killing PID 1 due to SAK). It also makes things easier
59          * for us so that we don't have to recover from hangups and suchlike triggered on the console. */
60 
61         fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
62         if (fd < 0)
63                 return fd;
64 
65         if (FLAGS_SET(flags, SHOW_STATUS_ELLIPSIZE)) {
66                 char *e;
67                 size_t emax, sl;
68                 int c;
69 
70                 c = fd_columns(fd);
71                 if (c <= 0)
72                         c = 80;
73 
74                 sl = status ? sizeof(status_indent)-1 : 0;
75 
76                 emax = c - sl - 1;
77                 if (emax < 3)
78                         emax = 3;
79 
80                 e = ellipsize(s, emax, 50);
81                 if (e)
82                         free_and_replace(s, e);
83         }
84 
85         if (prev_ephemeral)
86                 iovec[n++] = IOVEC_MAKE_STRING(ANSI_REVERSE_LINEFEED "\r" ANSI_ERASE_TO_END_OF_LINE);
87 
88         if (status) {
89                 if (!isempty(status)) {
90                         iovec[n++] = IOVEC_MAKE_STRING("[");
91                         iovec[n++] = IOVEC_MAKE_STRING(status);
92                         iovec[n++] = IOVEC_MAKE_STRING("] ");
93                 } else
94                         iovec[n++] = IOVEC_MAKE_STRING(status_indent);
95         }
96 
97         iovec[n++] = IOVEC_MAKE_STRING(s);
98         iovec[n++] = IOVEC_MAKE_STRING("\n");
99 
100         if (prev_ephemeral && !FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL))
101                 iovec[n++] = IOVEC_MAKE_STRING(ANSI_ERASE_TO_END_OF_LINE);
102         prev_ephemeral = FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL) ;
103 
104         if (writev(fd, iovec, n) < 0)
105                 return -errno;
106 
107         return 0;
108 }
109 
status_printf(const char * status,ShowStatusFlags flags,const char * format,...)110 int status_printf(const char *status, ShowStatusFlags flags, const char *format, ...) {
111         va_list ap;
112         int r;
113 
114         assert(format);
115 
116         va_start(ap, format);
117         r = status_vprintf(status, flags, format, ap);
118         va_end(ap);
119 
120         return r;
121 }
122 
123 static const char* const status_unit_format_table[_STATUS_UNIT_FORMAT_MAX] = {
124         [STATUS_UNIT_FORMAT_NAME]        = "name",
125         [STATUS_UNIT_FORMAT_DESCRIPTION] = "description",
126         [STATUS_UNIT_FORMAT_COMBINED]    = "combined",
127 };
128 
129 DEFINE_STRING_TABLE_LOOKUP(status_unit_format, StatusUnitFormat);
130