cdown / rpms / util-linux

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