From: Karel Zak Date: Tue, 12 May 2020 13:21:21 +0000 (+0200) Subject: login: add MOTD_FIRSTONLY= X-Git-Tag: v2.36-rc1~104 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9789d21a96be47fdd9b2c330b640e23254a22194;p=thirdparty%2Futil-linux.git login: add MOTD_FIRSTONLY= This login.defs option allow to configure login to be more compatible with pam_motd. Addresses: https://github.com/karelzak/util-linux/issues/1034 Signed-off-by: Karel Zak --- diff --git a/login-utils/login.1 b/login-utils/login.1 index 1e2d3cb289..24cd7d2bc1 100644 --- a/login-utils/login.1 +++ b/login-utils/login.1 @@ -168,6 +168,25 @@ PAM module. The directories in the .B MOTD_FILE are supported since version 2.36. +.PP +Note that +.B login +does not implement any filenames overriding behavior like pam_motd +(see also MOTD_FIRSTONLY), but all content from all files is displayed. It is +recommended to keep extra logic in content generators and use /run/motd.d rather +than rely on overriding behavior hardcoded in system tools. +.RE +.PP +.B MOTD_FIRSTONLY (boolean) +.RS 4 +Forces +.B login +to stop display content specified by +.B MOTD_FILE +after first accessible item in the list. Note that a directory is one item in this case. +This option allows to configure +.B login +semantic to be more compatible with pam_motd. .RE .PP .B LOGIN_PLAIN_PROMPT diff --git a/login-utils/login.c b/login-utils/login.c index 0ea2fbb3fa..4be8bcfaf3 100644 --- a/login-utils/login.c +++ b/login-utils/login.c @@ -267,14 +267,14 @@ static int motddir_filter(const struct dirent *d) return 1; /* accept */ } -static void motddir(const char *dirname) +static int motddir(const char *dirname) { - int dd, nfiles, i; + int dd, nfiles, i, done = 0; struct dirent **namelist = NULL; dd = open(dirname, O_RDONLY|O_CLOEXEC|O_DIRECTORY); if (dd < 0) - return; + return 0; nfiles = scandirat(dd, ".", &namelist, motddir_filter, versionsort); if (nfiles <= 0) @@ -290,6 +290,7 @@ static void motddir(const char *dirname) if (fstat(fd, &st) == 0 && st.st_size > 0) sendfile(fileno(stdout), fd, NULL, st.st_size); close(fd); + done++; } } @@ -298,6 +299,7 @@ static void motddir(const char *dirname) free(namelist); done: close(dd); + return done; } #endif /* MOTDDIR_SUPPORT */ @@ -313,6 +315,9 @@ static void motd(void) { const char *mb; char *file, *list; + int firstonly, done = 0; + + firstonly = getlogindefs_bool("MOTD_FIRSTONLY", 0); mb = getlogindefs_str("MOTD_FILE", _PATH_MOTDFILE); if (!mb || !*mb) @@ -327,15 +332,17 @@ static void motd(void) continue; #ifdef MOTDDIR_SUPPORT if (S_ISDIR(st.st_mode)) - motddir(file); - else + done += motddir(file); #endif if (S_ISREG(st.st_mode) && st.st_size > 0) { int fd = open(file, O_RDONLY, 0); if (fd >= 0) sendfile(fileno(stdout), fd, NULL, st.st_size); close(fd); + done++; } + if (firstonly && done) + break; } free(list); }