diff options
author | ed <ed@FreeBSD.org> | 2010-10-21 15:10:35 +0000 |
---|---|---|
committer | ed <ed@FreeBSD.org> | 2010-10-21 15:10:35 +0000 |
commit | ca64e82eb9dac04adb0412c62cfb6c7dd82303cb (patch) | |
tree | d1bf345998a0aaeffaf49af01931a6f3b7f4cf8b /lib/libc | |
parent | 5a22d5e5873c05f015da11627eef679204444c63 (diff) | |
download | FreeBSD-src-ca64e82eb9dac04adb0412c62cfb6c7dd82303cb.zip FreeBSD-src-ca64e82eb9dac04adb0412c62cfb6c7dd82303cb.tar.gz |
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.
Diffstat (limited to 'lib/libc')
-rw-r--r-- | lib/libc/gen/pututxline.c | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/lib/libc/gen/pututxline.c b/lib/libc/gen/pututxline.c index ce4dc28..3b2c7a8 100644 --- a/lib/libc/gen/pututxline.c +++ b/lib/libc/gen/pututxline.c @@ -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)); } |