On Sun, Nov 14, 2010 at 3:06 PM, Gavin Atkinson <ga...@freebsd.org> wrote: > On Sun, 14 Nov 2010, Ed Schouten wrote: >> Author: ed >> Date: Sun Nov 14 18:42:39 2010 >> New Revision: 215310 >> URL: http://svn.freebsd.org/changeset/base/215310 >> >> Log: >> Always set errno to a sane value when pututxline(3) fails. >> >> For example, it will now return ESRCH when trying to replace a >> nonexistent entry with DEAD_PROCESS. >> >> Modified: >> head/lib/libc/gen/pututxline.c >> >> Modified: head/lib/libc/gen/pututxline.c >> ============================================================================== >> --- head/lib/libc/gen/pututxline.c Sun Nov 14 18:24:12 2010 >> (r215309) >> +++ head/lib/libc/gen/pututxline.c Sun Nov 14 18:42:39 2010 >> (r215310) >> @@ -31,6 +31,7 @@ __FBSDID("$FreeBSD$"); >> #include <sys/endian.h> >> #include <sys/stat.h> >> #include <sys/uio.h> >> +#include <errno.h> >> #include <fcntl.h> >> #include <stdio.h> >> #include <string.h> >> @@ -53,6 +54,7 @@ futx_open(const char *file) >> /* Safety check: never use broken files. */ >> if (_fstat(fd, &sb) != -1 && sb.st_size % sizeof(struct futx) != 0) { >> _close(fd); >> + errno = EINVAL; >> return (NULL); >> } > > setutxdb(3) returns EFTYPE here, for the same error. Should this be the > same? > >> >> @@ -142,6 +144,7 @@ utx_active_remove(struct futx *fu) >> } >> >> fclose(fp); >> + errno = ESRCH; >> return (1); >> } > > These possible errors should probably also now be documented in > pututxline(3).
I submitted another patch earlier to Ed that does this and a bit more (attached). He was going to take a look at the patch and get back to me (and I CCed Bruce Evans for additional comments), but please feel free to chime in :). Thanks, -Garrett
Index: lib/libc/gen/getutxent.3 =================================================================== --- lib/libc/gen/getutxent.3 (revision 215314) +++ lib/libc/gen/getutxent.3 (working copy) @@ -367,16 +367,15 @@ .Dv NULL when the provided .Vt utmpx -is invalid. -This may be because +is invalid, or .Fa ut_type -is invalid or -.Fa ut_type has a value of .Dv DEAD_PROCESS and an entry with an identifier with a value equal to the field .Fa ut_id -was not found. +was not found; the global variable +.Va errno +is set to indicate the error. .Pp The .Fn setutxdb @@ -387,8 +386,26 @@ is set to indicate the error. .Sh ERRORS In addition to the error conditions described in +.Xr fdopen 3 , .Xr fopen 3 , +.Xr fseek 3 , +.Xr open 3 , the +.Fn pututxline +function can generate the following errors: +.Bl -tag -width Er +.It Bq Er ESRCH +The value of +.Fa ut_type +is DEAD_PROCESS, and the process entry could not be found. +.It Bq Er EINVAL +The value of +.Fa ut_type +is not supported by this implementation. +.El +In addition to the error conditions described in +.Xr fopen 3 , +the .Fn setutxdb function can generate the following errors: .Bl -tag -width Er Index: lib/libc/gen/getutxent.c =================================================================== --- lib/libc/gen/getutxent.c (revision 215314) +++ lib/libc/gen/getutxent.c (working copy) @@ -112,22 +112,22 @@ if (udb == UTXDB_LOG) { uint16_t len; - if (fread(&len, sizeof len, 1, uf) != 1) + if (fread(&len, sizeof(len), 1, uf) != 1) return (-1); len = be16toh(len); if (len > sizeof *fu) { /* Forward compatibility. */ - if (fread(fu, sizeof *fu, 1, uf) != 1) + if (fread(fu, sizeof(*fu), 1, uf) != 1) return (-1); - fseek(uf, len - sizeof *fu, SEEK_CUR); + fseek(uf, len - sizeof(*fu), SEEK_CUR); } else { /* Partial record. */ - memset(fu, 0, sizeof *fu); + memset(fu, 0, sizeof(*fu)); if (fread(fu, len, 1, uf) != 1) return (-1); } } else { - if (fread(fu, sizeof *fu, 1, uf) != 1) + if (fread(fu, sizeof(*fu), 1, uf) != 1) return (-1); } return (0); @@ -163,7 +163,8 @@ case LOGIN_PROCESS: case DEAD_PROCESS: if (memcmp(fu.fu_id, id->ut_id, - MIN(sizeof fu.fu_id, sizeof id->ut_id)) == 0) + MIN(sizeof(fu.fu_id), sizeof(id->ut_id))) == + 0) goto found; } break; @@ -191,7 +192,8 @@ case USER_PROCESS: case LOGIN_PROCESS: if (strncmp(fu.fu_line, line->ut_line, - MIN(sizeof fu.fu_line, sizeof line->ut_line)) == 0) + MIN(sizeof(fu.fu_line), sizeof(line->ut_line))) == + 0) goto found; break; } @@ -212,7 +214,7 @@ switch (fu.fu_type) { case USER_PROCESS: - if (strncmp(fu.fu_user, user, sizeof fu.fu_user) == 0) + if (strncmp(fu.fu_user, user, sizeof(fu.fu_user)) == 0) goto found; break; } Index: lib/libc/gen/pututxline.c =================================================================== --- lib/libc/gen/pututxline.c (revision 215314) +++ lib/libc/gen/pututxline.c (working copy) @@ -43,9 +43,9 @@ static FILE * futx_open(const char *file) { + struct stat sb; + FILE *fp; int fd; - FILE *fp; - struct stat sb; fd = _open(file, O_CREAT|O_RDWR|O_EXLOCK, 0644); if (fd < 0) @@ -54,7 +54,7 @@ /* Safety check: never use broken files. */ if (_fstat(fd, &sb) != -1 && sb.st_size % sizeof(struct futx) != 0) { _close(fd); - errno = EINVAL; + errno = EFTYPE; return (NULL); } @@ -63,16 +63,16 @@ _close(fd); return (NULL); } - return (fp); } static int utx_active_add(const struct futx *fu) { + struct futx fe; FILE *fp; - struct futx fe; off_t partial = -1; + int error, ret; /* * Register user login sessions. Overwrite entries of sessions @@ -80,16 +80,17 @@ */ fp = futx_open(_PATH_UTX_ACTIVE); if (fp == NULL) - return (1); - while (fread(&fe, sizeof fe, 1, fp) == 1) { + return (-1); + while (fread(&fe, sizeof(fe), 1, fp) == 1) { switch (fe.fu_type) { case USER_PROCESS: case INIT_PROCESS: case LOGIN_PROCESS: case DEAD_PROCESS: /* Overwrite when ut_id matches. */ - if (memcmp(fu->fu_id, fe.fu_id, sizeof fe.fu_id) == 0) { - fseeko(fp, -(off_t)sizeof fe, SEEK_CUR); + if (memcmp(fu->fu_id, fe.fu_id, sizeof(fe.fu_id)) == + 0) { + ret = fseeko(fp, -(off_t)sizeof(fe), SEEK_CUR); goto exact; } if (fe.fu_type != DEAD_PROCESS) @@ -97,55 +98,73 @@ /* FALLTHROUGH */ default: /* Allow us to overwrite unused records. */ - if (partial == -1) - partial = ftello(fp) - (off_t)sizeof fe; + if (partial == -1) { + partial = ftello(fp); + /* + * Distinguish errors from valid values so we + * don't overwrite good data by accident. + */ + if (partial != -1) + partial -= (off_t)sizeof(fe); + } break; } } - + /* * No exact match found. Use the partial match. If no partial * match was found, just append a new record. */ if (partial != -1) - fseeko(fp, partial, SEEK_SET); + ret = fseeko(fp, partial, SEEK_SET); exact: - fwrite(fu, sizeof *fu, 1, fp); + if (ret == -1) + error = errno; + else if (fwrite(fu, sizeof(*fu), 1, fp) < 1) + error = errno; + else + error = 0; fclose(fp); - return (0); + errno = error; + return (error == 0 ? 0 : 1); } static int utx_active_remove(struct futx *fu) { + struct futx fe; FILE *fp; - struct futx fe; + int error, ret; /* * Remove user login sessions, having the same ut_id. */ fp = futx_open(_PATH_UTX_ACTIVE); if (fp == NULL) - return (1); - while (fread(&fe, sizeof fe, 1, fp) == 1) { + return (-1); + error = ESRCH; + ret = -1; + while (fread(&fe, sizeof(fe), 1, fp) == 1 && ret != 0) switch (fe.fu_type) { case USER_PROCESS: case INIT_PROCESS: case LOGIN_PROCESS: - if (memcmp(fu->fu_id, fe.fu_id, sizeof fe.fu_id) != 0) + if (memcmp(fu->fu_id, fe.fu_id, sizeof(fe.fu_id)) != 0) continue; /* Terminate session. */ - fseeko(fp, -(off_t)sizeof fe, SEEK_CUR); - fwrite(fu, sizeof *fu, 1, fp); - fclose(fp); - return (0); + if (fseeko(fp, -(off_t)sizeof(fe), SEEK_CUR) == -1) + error = errno; + else if (fwrite(fu, sizeof(*fu), 1, fp) < 1) + error = errno; + else + ret = 0; + } - } fclose(fp); - errno = ESRCH; - return (1); + errno = error; + return (ret); } static void @@ -158,9 +177,12 @@ static int utx_lastlogin_add(const struct futx *fu) { + struct futx fe; FILE *fp; - struct futx fe; + int error, ret; + ret = 0; + /* * Write an entry to lastlogin. Overwrite the entry if the * current user already has an entry. If not, append a new @@ -168,25 +190,31 @@ */ fp = futx_open(_PATH_UTX_LASTLOGIN); if (fp == NULL) - return (1); + return (-1); while (fread(&fe, sizeof fe, 1, fp) == 1) { if (strncmp(fu->fu_user, fe.fu_user, sizeof fe.fu_user) != 0) continue; - + /* Found a previous lastlogin entry for this user. */ - fseeko(fp, -(off_t)sizeof fe, SEEK_CUR); + ret = fseeko(fp, -(off_t)sizeof fe, SEEK_CUR); break; } - fwrite(fu, sizeof *fu, 1, fp); + if (ret == -1) + error = errno; + else if (fwrite(fu, sizeof *fu, 1, fp) < 1) { + error = errno; + ret = -1; + } fclose(fp); - return (0); + errno = error; + return (ret); } static void utx_lastlogin_upgrade(void) { + struct stat sb; int fd; - struct stat sb; fd = _open(_PATH_UTX_LASTLOGIN, O_RDWR, 0644); if (fd < 0) @@ -205,9 +233,9 @@ static int utx_log_add(const struct futx *fu) { - int fd; + struct iovec vec[2]; + int error, fd; uint16_t l; - struct iovec vec[2]; /* * Append an entry to the log file. We only need to append @@ -215,19 +243,23 @@ * zero-bytes. Prepend a length field, indicating the length of * the record, excluding the length field itself. */ - for (l = sizeof *fu; l > 0 && ((const char *)fu)[l - 1] == '\0'; l--); + for (l = sizeof(*fu); l > 0 && ((const char *)fu)[l - 1] == '\0'; l--) ; vec[0].iov_base = &l; - vec[0].iov_len = sizeof l; + vec[0].iov_len = sizeof(l); vec[1].iov_base = __DECONST(void *, fu); vec[1].iov_len = l; l = htobe16(l); fd = _open(_PATH_UTX_LOG, O_CREAT|O_WRONLY|O_APPEND, 0644); if (fd < 0) - return (1); - _writev(fd, vec, 2); + return (-1); + if (_writev(fd, vec, 2) == -1) + error = errno; + else + error = 0; _close(fd); - return (0); + errno = error; + return (error == 0 ? 0 : 1); } struct utmpx * @@ -237,7 +269,7 @@ int bad = 0; utx_to_futx(utmpx, &fu); - + switch (fu.fu_type) { case BOOT_TIME: case SHUTDOWN_TIME: @@ -267,6 +299,7 @@ return (NULL); break; default: + errno = EINVAL; return (NULL); } Index: lib/libc/gen/utxdb.c =================================================================== --- lib/libc/gen/utxdb.c (revision 215314) +++ lib/libc/gen/utxdb.c (working copy) @@ -132,9 +132,8 @@ ut = calloc(1, sizeof *ut); if (ut == NULL) return (NULL); - } else { + } else memset(ut, 0, sizeof *ut); - } switch (fu->fu_type) { case BOOT_TIME:
_______________________________________________ svn-src-head@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/svn-src-head To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"