cdown / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/2.24-agetty-etc-os-release.patch

05ad79
diff -up util-linux-2.23.2/include/pathnames.h.kzak util-linux-2.23.2/include/pathnames.h
05ad79
--- util-linux-2.23.2/include/pathnames.h.kzak	2013-07-30 10:39:26.201738190 +0200
05ad79
+++ util-linux-2.23.2/include/pathnames.h	2013-09-12 13:05:35.928359383 +0200
05ad79
@@ -63,6 +63,7 @@
05ad79
 
05ad79
 /* used in term-utils/agetty.c */
05ad79
 #define _PATH_ISSUE		"/etc/issue"
05ad79
+#define _PATH_OS_RELEASE	"/etc/os-release"
05ad79
 #define _PATH_NUMLOCK_ON	_PATH_LOCALSTATEDIR "/numlock-on"
05ad79
 
05ad79
 #define _PATH_LOGINDEFS		"/etc/login.defs"
05ad79
diff -up util-linux-2.23.2/term-utils/agetty.8.kzak util-linux-2.23.2/term-utils/agetty.8
05ad79
--- util-linux-2.23.2/term-utils/agetty.8.kzak	2013-07-30 10:58:20.889261333 +0200
05ad79
+++ util-linux-2.23.2/term-utils/agetty.8	2013-09-12 13:05:35.928359383 +0200
05ad79
@@ -310,6 +310,14 @@ Insert the current date.
05ad79
 .TP
05ad79
 s
05ad79
 Insert the system name, the name of the operating system. Same as `uname \-s'.
05ad79
+See also \\S escape code.
05ad79
+.TP
05ad79
+S or S{VARIABLE}
05ad79
+Insert the VARIABLE data from \fI/etc/os-release\fP.  If the VARIABLE argument
05ad79
+is not specified then use PRETTY_NAME from the file or the system name (see \\s).
05ad79
+This escape code allows to keep \fI/etc/issue\fP distribution and release
05ad79
+independent.  Note that \\S{ANSI_COLOR} is converted to the real terminal
05ad79
+escape sequence.
05ad79
 .TP
05ad79
 l
05ad79
 Insert the name of the current tty line.
05ad79
@@ -368,11 +376,14 @@ the system status file.
05ad79
 .B /etc/issue
05ad79
 printed before the login prompt.
05ad79
 .TP
05ad79
+.B /etc/os-release
05ad79
+operating system identification data.
05ad79
+.TP
05ad79
 .B /dev/console
05ad79
 problem reports (if syslog(3) is not used).
05ad79
 .TP
05ad79
 .B /etc/inittab
05ad79
-\fIinit\fP(8) configuration file.
05ad79
+\fIinit\fP(8) configuration file for SysV-style init daemon.
05ad79
 .SH BUGS
05ad79
 .ad
05ad79
 .fi
05ad79
diff -up util-linux-2.23.2/term-utils/agetty.c.kzak util-linux-2.23.2/term-utils/agetty.c
05ad79
--- util-linux-2.23.2/term-utils/agetty.c.kzak	2013-09-12 13:05:35.927359379 +0200
05ad79
+++ util-linux-2.23.2/term-utils/agetty.c	2013-09-12 13:05:35.929359388 +0200
05ad79
@@ -129,6 +129,7 @@ struct options {
05ad79
 	char *issue;			/* alternative issue file */
05ad79
 	char *erasechars;		/* string with erase chars */
05ad79
 	char *killchars;		/* string with kill chars */
05ad79
+	char *osrelease;		/* /etc/os-release data */
05ad79
 	int delay;			/* Sleep seconds before prompt */
05ad79
 	int nice;			/* Run login with this priority */
05ad79
 	int numspeed;			/* number of baud rates to try */
05ad79
@@ -431,7 +432,8 @@ int main(int argc, char **argv)
05ad79
 			log_warn(_("%s: can't change process priority: %m"),
05ad79
 				options.tty);
05ad79
 	}
05ad79
-
05ad79
+	if (options.osrelease)
05ad79
+		free(options.osrelease);
05ad79
 #ifdef DEBUGGING
05ad79
 	fprintf(dbf, "read %c\n", ch);
05ad79
 	if (close_stream(dbf) != 0)
05ad79
@@ -1279,6 +1281,84 @@ static char *xgetdomainname(void)
05ad79
 	return NULL;
05ad79
 }
05ad79
 
05ad79
+static char *read_os_release(struct options *op, const char *varname)
05ad79
+{
05ad79
+	int fd = -1;
05ad79
+	struct stat st;
05ad79
+	size_t varsz = strlen(varname);
05ad79
+	char *p, *buf = NULL, *ret = NULL;
05ad79
+
05ad79
+	/* read the file only once */
05ad79
+	if (!op->osrelease) {
05ad79
+		fd = open(_PATH_OS_RELEASE, O_RDONLY);
05ad79
+		if (fd == -1) {
05ad79
+			log_warn(_("cannot open: %s: %m"), _PATH_OS_RELEASE);
05ad79
+			return NULL;
05ad79
+		}
05ad79
+
05ad79
+		if (fstat(fd, &st) < 0 || st.st_size > 4 * 1024 * 1024)
05ad79
+			goto done;
05ad79
+
05ad79
+		op->osrelease = malloc(st.st_size + 1);
05ad79
+		if (!op->osrelease)
05ad79
+			log_err(_("failed to allocate memory: %m"));
05ad79
+		if (read_all(fd, op->osrelease, st.st_size) != (ssize_t) st.st_size) {
05ad79
+			free(op->osrelease);
05ad79
+			op->osrelease = NULL;
05ad79
+			goto done;
05ad79
+		}
05ad79
+		op->osrelease[st.st_size] = 0;
05ad79
+	}
05ad79
+	buf = strdup(op->osrelease);
05ad79
+	if (!buf)
05ad79
+		log_err(_("failed to allocate memory: %m"));
05ad79
+	p = buf;
05ad79
+
05ad79
+	for (;;) {
05ad79
+		char *eol, *eon;
05ad79
+
05ad79
+		p += strspn(p, "\n\r");
05ad79
+		p += strspn(p, " \t\n\r");
05ad79
+		if (!*p)
05ad79
+			break;
05ad79
+		if (strspn(p, "#;\n") != 0) {
05ad79
+			p += strcspn(p, "\n\r");
05ad79
+			continue;
05ad79
+		}
05ad79
+		if (strncmp(p, varname, varsz) != 0) {
05ad79
+			p += strcspn(p, "\n\r");
05ad79
+			continue;
05ad79
+		}
05ad79
+		p += varsz;
05ad79
+		p += strspn(p, " \t\n\r=\"");
05ad79
+		eol = p + strcspn(p, "\n\r");
05ad79
+		*eol = '\0';
05ad79
+		eon = eol-1;
05ad79
+		while (eon > p) {
05ad79
+			if (*eon == '\t' || *eon == ' ') {
05ad79
+				eon--;
05ad79
+				continue;
05ad79
+			}
05ad79
+			if (*eon == '"') {
05ad79
+				*eon = '\0';
05ad79
+				break;
05ad79
+			}
05ad79
+			break;
05ad79
+		}
05ad79
+		if (ret)
05ad79
+			free(ret);
05ad79
+		ret = strdup(p);
05ad79
+		if (!ret)
05ad79
+			log_err(_("failed to allocate memory: %m"));
05ad79
+		p = eol + 1;
05ad79
+	}
05ad79
+done:
05ad79
+	free(buf);
05ad79
+	if (fd >= 0)
05ad79
+		close(fd);
05ad79
+	return ret;
05ad79
+}
05ad79
+
05ad79
 /* Show login prompt, optionally preceded by /etc/issue contents. */
05ad79
 static void do_prompt(struct options *op, struct termios *tp)
05ad79
 {
05ad79
@@ -1968,6 +2048,24 @@ static void output_special_char(unsigned
05ad79
 		}
05ad79
 		break;
05ad79
 	}
05ad79
+	case 'S':
05ad79
+	{
05ad79
+		char *var = NULL, varname[64];
05ad79
+
05ad79
+		if (get_escape_argument(fp, varname, sizeof(varname)))
05ad79
+			var = read_os_release(op, varname);
05ad79
+		else if (!(var = read_os_release(op, "PRETTY_NAME")))
05ad79
+			var = uts.sysname;
05ad79
+		if (var) {
05ad79
+			if (strcmp(varname, "ANSI_COLOR") == 0)
05ad79
+				printf("\033[%sm", var);
05ad79
+			else
05ad79
+				printf("%s", var);
05ad79
+			if (var != uts.sysname)
05ad79
+				free(var);
05ad79
+		}
05ad79
+		break;
05ad79
+	}
05ad79
 	case 'u':
05ad79
 	case 'U':
05ad79
 	{