Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 21 Oct 2010 15:10:35 +0000 (UTC)
From:      Ed Schouten <ed@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r214134 - head/lib/libc/gen
Message-ID:  <201010211510.o9LFAZ5Y094083@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ed
Date: Thu Oct 21 15:10:35 2010
New Revision: 214134
URL: http://svn.freebsd.org/changeset/base/214134

Log:
  Fix error handling logic of pututxline(3).
  
  Instead of only returning NULL when the entry is invalid and can't be
  matched against the current database, also return it when it cannot open
  the log files properly.

Modified:
  head/lib/libc/gen/pututxline.c

Modified: head/lib/libc/gen/pututxline.c
==============================================================================
--- head/lib/libc/gen/pututxline.c	Thu Oct 21 12:58:26 2010	(r214133)
+++ head/lib/libc/gen/pututxline.c	Thu Oct 21 15:10:35 2010	(r214134)
@@ -65,7 +65,7 @@ futx_open(const char *file)
 	return (fp);
 }
 
-static void
+static int
 utx_active_add(const struct futx *fu)
 {
 	FILE *fp;
@@ -78,7 +78,7 @@ utx_active_add(const struct futx *fu)
 	 */
 	fp = futx_open(_PATH_UTX_ACTIVE);
 	if (fp == NULL)
-		return;
+		return (1);
 	while (fread(&fe, sizeof fe, 1, fp) == 1) {
 		switch (fe.fu_type) {
 		case USER_PROCESS:
@@ -110,6 +110,7 @@ utx_active_add(const struct futx *fu)
 exact:
 	fwrite(fu, sizeof *fu, 1, fp);
 	fclose(fp);
+	return (0);
 }
 
 static int
@@ -123,7 +124,7 @@ utx_active_remove(struct futx *fu)
 	 */
 	fp = futx_open(_PATH_UTX_ACTIVE);
 	if (fp == NULL)
-		return (0);
+		return (1);
 	while (fread(&fe, sizeof fe, 1, fp) == 1) {
 		switch (fe.fu_type) {
 		case USER_PROCESS:
@@ -151,7 +152,7 @@ utx_active_purge(void)
 	truncate(_PATH_UTX_ACTIVE, 0);
 }
 
-static void
+static int
 utx_lastlogin_add(const struct futx *fu)
 {
 	FILE *fp;
@@ -164,7 +165,7 @@ utx_lastlogin_add(const struct futx *fu)
 	 */
 	fp = futx_open(_PATH_UTX_LASTLOGIN);
 	if (fp == NULL)
-		return;
+		return (1);
 	while (fread(&fe, sizeof fe, 1, fp) == 1) {
 		if (strncmp(fu->fu_user, fe.fu_user, sizeof fe.fu_user) != 0)
 			continue;
@@ -175,6 +176,7 @@ utx_lastlogin_add(const struct futx *fu)
 	}
 	fwrite(fu, sizeof *fu, 1, fp);
 	fclose(fp);
+	return (0);
 }
 
 static void
@@ -197,7 +199,7 @@ utx_lastlogin_upgrade(void)
 	_close(fd);
 }
 
-static void
+static int
 utx_log_add(const struct futx *fu)
 {
 	int fd;
@@ -219,15 +221,17 @@ utx_log_add(const struct futx *fu)
 
 	fd = _open(_PATH_UTX_LOG, O_CREAT|O_WRONLY|O_APPEND, 0644);
 	if (fd < 0)
-		return;
+		return (1);
 	_writev(fd, vec, 2);
 	_close(fd);
+	return (0);
 }
 
 struct utmpx *
 pututxline(const struct utmpx *utmpx)
 {
 	struct futx fu;
+	int bad = 0;
 
 	utx_to_futx(utmpx, &fu);
 	
@@ -241,16 +245,21 @@ pututxline(const struct utmpx *utmpx)
 	case NEW_TIME:
 		break;
 	case USER_PROCESS:
-		utx_active_add(&fu);
-		utx_lastlogin_add(&fu);
+		bad |= utx_active_add(&fu);
+		bad |= utx_lastlogin_add(&fu);
 		break;
 #if 0 /* XXX: Are these records of any use to us? */
 	case INIT_PROCESS:
 	case LOGIN_PROCESS:
-		utx_active_add(&fu);
+		bad |= utx_active_add(&fu);
 		break;
 #endif
 	case DEAD_PROCESS:
+		/*
+		 * In case writing a logout entry fails, never attempt
+		 * to write it to utx.log.  The logout entry's ut_id
+		 * might be invalid.
+		 */
 		if (utx_active_remove(&fu) != 0)
 			return (NULL);
 		break;
@@ -258,6 +267,6 @@ pututxline(const struct utmpx *utmpx)
 		return (NULL);
 	}
 
-	utx_log_add(&fu);
-	return (futx_to_utx(&fu));
+	bad |= utx_log_add(&fu);
+	return (bad ? NULL : futx_to_utx(&fu));
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201010211510.o9LFAZ5Y094083>