summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorDaniel Mack <daniel@zonque.org>2015-04-24 15:31:29 +0200
committerDaniel Mack <daniel@zonque.org>2015-04-24 17:48:12 +0200
commit99f710dde855f7ecb699ddac6ad77923c1f6bc85 (patch)
tree3fb5bb95bd9eeb0ad1839285b08ae0665cf76e50 /src/shared
parent8aaa023ae78f3cb28db3edd87f96b21486810b91 (diff)
shared/utmp-wtmp: add parameter for origin tty and callback userdata
Instead of looking up the tty from STDIN, let utmp_wall() take an argument to specify an origin tty for the wall message. Only if that argument is NULL do the STDIN lookup. Also add an void *userdata argument that is handed back to the callback function.
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/utmp-wtmp.c19
-rw-r--r--src/shared/utmp-wtmp.h15
2 files changed, 26 insertions, 8 deletions
diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c
index d6c4cc81b3..aaf249dd20 100644
--- a/src/shared/utmp-wtmp.c
+++ b/src/shared/utmp-wtmp.c
@@ -347,8 +347,14 @@ static int write_to_terminal(const char *tty, const char *message) {
return 0;
}
-int utmp_wall(const char *message, const char *username, bool (*match_tty)(const char *tty)) {
- _cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *tty = NULL;
+int utmp_wall(
+ const char *message,
+ const char *username,
+ const char *origin_tty,
+ bool (*match_tty)(const char *tty, void *userdata),
+ void *userdata) {
+
+ _cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *stdin_tty = NULL;
char date[FORMAT_TIMESTAMP_MAX];
struct utmpx *u;
int r;
@@ -362,14 +368,17 @@ int utmp_wall(const char *message, const char *username, bool (*match_tty)(const
return -ENOMEM;
}
- getttyname_harder(STDIN_FILENO, &tty);
+ if (!origin_tty) {
+ getttyname_harder(STDIN_FILENO, &stdin_tty);
+ origin_tty = stdin_tty;
+ }
if (asprintf(&text,
"\a\r\n"
"Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
"%s\r\n\r\n",
un ?: username, hn,
- tty ? " on " : "", strempty(tty),
+ origin_tty ? " on " : "", strempty(origin_tty),
format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
message) < 0)
return -ENOMEM;
@@ -396,7 +405,7 @@ int utmp_wall(const char *message, const char *username, bool (*match_tty)(const
path = buf;
}
- if (!match_tty || match_tty(path)) {
+ if (!match_tty || match_tty(path, userdata)) {
q = write_to_terminal(path, text);
if (q < 0)
r = q;
diff --git a/src/shared/utmp-wtmp.h b/src/shared/utmp-wtmp.h
index 87d004e615..6ac2c7b1c7 100644
--- a/src/shared/utmp-wtmp.h
+++ b/src/shared/utmp-wtmp.h
@@ -33,7 +33,12 @@ int utmp_put_runlevel(int runlevel, int previous);
int utmp_put_dead_process(const char *id, pid_t pid, int code, int status);
int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line);
-int utmp_wall(const char *message, const char *username, bool (*match_tty)(const char *tty));
+int utmp_wall(
+ const char *message,
+ const char *username,
+ const char *origin_tty,
+ bool (*match_tty)(const char *tty, void *userdata),
+ void *userdata);
#else /* HAVE_UTMP */
@@ -55,8 +60,12 @@ static inline int utmp_put_dead_process(const char *id, pid_t pid, int code, int
static inline int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line) {
return 0;
}
-static inline int utmp_wall(const char *message, const char *username,
- bool (*match_tty)(const char *tty)) {
+static inline int utmp_wall(
+ const char *message,
+ const char *username,
+ const char *origin_tty,
+ bool (*match_tty)(const char *tty, void *userdata),
+ void *userdata);
return 0;
}