dcavalca / rpms / util-linux

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