Debian Codegraph @

src/shared/wall.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "sd-login.h"
6
7 #include "alloc-util.h"
8 #include "errno-util.h"
9 #include "fd-util.h"
10 #include "hostname-setup.h"
11 #include "io-util.h"
12 #include "path-util.h"
13 #include "string-util.h"
14 #include "strv.h"
15 #include "terminal-util.h"
16 #include "time-util.h"
17 #include "user-util.h"
18 #include "utmp-wtmp.h"
19 #include "wall.h"
20
21 #if ENABLE_UTMP || ENABLE_LOGIND
22
23 #define TIMEOUT_USEC (50 * USEC_PER_MSEC)
24
25 static int write_to_terminal(const char *tty, const char *message) {
26 _cleanup_close_ int fd = -EBADF;
27
28 assert(tty);
29 assert(message);
30
31 fd = open(tty, O_WRONLY|O_NONBLOCK|O_NOCTTY|O_CLOEXEC);
32 if (fd < 0)
33 return -errno;
34
35 if (!isatty_safe(fd))
36 return -ENOTTY;
37
38 return loop_write_full(fd, message, SIZE_MAX, TIMEOUT_USEC);
39 }
40
41 static int wall_utmp(
42 const char *message,
43 bool (*match_tty)(const char *tty, bool is_local, void *userdata),
44 void *userdata) {
45
46 #if ENABLE_UTMP
47 _unused_ _cleanup_(utxent_cleanup) bool utmpx = false;
48 struct utmpx *u;
49 int r = 0;
50
51 assert(message);
52
53 /* libc's setutxent() unfortunately doesn't inform us about success, i.e. whether /var/run/utmp
54 * exists. Hence we have to check manually first. */
55 if (access(UTMPX_FILE, F_OK) < 0) {
56 if (errno == ENOENT)
57 return -ENOPROTOOPT;
58
59 return -errno;
60 }
61
62 utmpx = utxent_start();
63
64 while ((u = getutxent())) {
65 _cleanup_free_ char *p = NULL;
66 const char *tty_path;
67 bool is_local;
68
69 if (u->ut_type != USER_PROCESS || isempty(u->ut_user))
70 continue;
71
72 /* This access is fine, because strlen("/dev/") < 32 (UT_LINESIZE) */
73 if (path_startswith(u->ut_line, "/dev/"))
74 tty_path = u->ut_line;
75 else {
76 if (asprintf(&p, "/dev/%.*s", (int) sizeof(u->ut_line), u->ut_line) < 0)
77 return -ENOMEM;
78
79 tty_path = p;
80 }
81
82 /* It seems that the address field is always set for remote logins. For local logins and
83 * other local entries, we get [0,0,0,0]. */
84 is_local = eqzero(u->ut_addr_v6);
85
86 if (!match_tty || match_tty(tty_path, is_local, userdata))
87 RET_GATHER(r, write_to_terminal(tty_path, message));
88 }
89
90 return r;
91
92 #else
93 return -ENOPROTOOPT;
94 #endif
95 }
96
97 static int wall_logind(
98 const char *message,
99 bool (*match_tty)(const char *tty, bool is_local, void *userdata),
100 void *userdata) {
101
102 #if ENABLE_LOGIND
103 _cleanup_strv_free_ char **sessions = NULL;
104 int r;
105
106 assert(message);
107
108 r = sd_get_sessions(&sessions);
109 if (r <= 0)
110 return r;
111
112 r = 0;
113
114 STRV_FOREACH(s, sessions) {
115 _cleanup_free_ char *tty_path = NULL, *tty = NULL, *rhost = NULL;
116 bool is_local;
117 int q;
118
119 q = sd_session_get_tty(*s, &tty);
120 if (IN_SET(q, -ENXIO, -ENODATA))
121 continue;
122 if (q < 0)
123 return RET_GATHER(r, q);
124
125 tty_path = strjoin("/dev/", tty);
126 if (!tty_path)
127 return -ENOMEM;
128
129 (void) sd_session_get_remote_host(*s, &rhost);
130 is_local = !rhost;
131
132 if (!match_tty || match_tty(tty_path, is_local, userdata))
133 RET_GATHER(r, write_to_terminal(tty_path, message));
134 }
135
136 return r;
137
138 #else
139 return -ENOPROTOOPT;
140 #endif
141 }
142
143 int wall(
144 const char *message,
145 const char *username,
146 const char *origin_tty,
147 bool (*match_tty)(const char *tty, bool is_local, void *userdata),
148 void *userdata) {
149
150 _cleanup_free_ char *text = NULL, *hostname = NULL, *username_alloc = NULL, *stdin_tty = NULL;
151 int r;
152
153 assert(message);
154
155 hostname = gethostname_malloc();
156 if (!hostname)
157 return -ENOMEM;
158
159 if (!username) {
160 username_alloc = getlogname_malloc();
161 if (!username_alloc)
162 return -ENOMEM;
163
164 username = username_alloc;
165 }
166
167 if (!origin_tty) {
168 (void) getttyname_harder(STDIN_FILENO, &stdin_tty);
169 origin_tty = stdin_tty;
170 }
171
172 if (asprintf(&text,
173 "\r\n"
174 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
175 "%s\r\n\r\n",
176 username, hostname,
177 origin_tty ? " on " : "", strempty(origin_tty),
178 FORMAT_TIMESTAMP(now(CLOCK_REALTIME)),
179 message) < 0)
180 return -ENOMEM;
181
182 r = wall_utmp(text, match_tty, userdata);
183 if (r == -ENOPROTOOPT)
184 r = wall_logind(text, match_tty, userdata);
185
186 return r == -ENOPROTOOPT ? 0 : r;
187 }
188
189 #endif