svn commit: r201763 - user/ed/utmpx/lib/libc/gen
Ed Schouten
ed at FreeBSD.org
Thu Jan 7 23:28:59 UTC 2010
Author: ed
Date: Thu Jan 7 23:28:58 2010
New Revision: 201763
URL: http://svn.freebsd.org/changeset/base/201763
Log:
Put conversion between futx and utmpx in a separate file.
This allows me to get the return value of pututxline() working without
excessive code duplication.
Added:
user/ed/utmpx/lib/libc/gen/utxdb.c (contents, props changed)
Modified:
user/ed/utmpx/lib/libc/gen/Makefile.inc
user/ed/utmpx/lib/libc/gen/getutxent.c
user/ed/utmpx/lib/libc/gen/pututxline.c
user/ed/utmpx/lib/libc/gen/utxdb.h
Modified: user/ed/utmpx/lib/libc/gen/Makefile.inc
==============================================================================
--- user/ed/utmpx/lib/libc/gen/Makefile.inc Thu Jan 7 23:17:48 2010 (r201762)
+++ user/ed/utmpx/lib/libc/gen/Makefile.inc Thu Jan 7 23:28:58 2010 (r201763)
@@ -32,7 +32,7 @@ SRCS+= __getosreldate.c __xuname.c \
sysconf.c sysctl.c sysctlbyname.c sysctlnametomib.c \
syslog.c telldir.c termios.c time.c times.c timezone.c tls.c \
ttyname.c ttyslot.c ualarm.c ulimit.c uname.c unvis.c \
- usleep.c utime.c valloc.c vis.c wait.c wait3.c waitpid.c \
+ usleep.c utime.c utxdb.c valloc.c vis.c wait.c wait3.c waitpid.c \
wordexp.c
SYM_MAPS+=${.CURDIR}/gen/Symbol.map
Modified: user/ed/utmpx/lib/libc/gen/getutxent.c
==============================================================================
--- user/ed/utmpx/lib/libc/gen/getutxent.c Thu Jan 7 23:17:48 2010 (r201762)
+++ user/ed/utmpx/lib/libc/gen/getutxent.c Thu Jan 7 23:28:58 2010 (r201763)
@@ -28,6 +28,9 @@
__FBSDID("$FreeBSD$");
#include "namespace.h"
+#include <sys/endian.h>
+#include <sys/param.h>
+#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
@@ -42,6 +45,7 @@ static struct utmpx utx;
int
setutxdb(int db, const char *file)
{
+ struct stat sb;
switch (db) {
case UTXDB_ACTIVE:
@@ -66,6 +70,16 @@ setutxdb(int db, const char *file)
uf = fopen(file, "r");
if (uf == NULL)
return (-1);
+
+ /* Safety check: never use broken files. */
+ if (db != UTXDB_LOG && _fstat(fileno(uf), &sb) != -1 &&
+ sb.st_size % sizeof(struct futx) != 0) {
+ fclose(uf);
+ uf = NULL;
+ errno = EFTYPE;
+ return (-1);
+ }
+
udb = db;
return (0);
}
@@ -87,49 +101,6 @@ endutxent(void)
}
}
-static void
-futx_to_utx(const struct futx *fu, struct utmpx *ut)
-{
-
- memset(ut, 0, sizeof *ut);
-
- switch (fu->fu_type) {
- case BOOT_TIME:
- case OLD_TIME:
- case NEW_TIME:
- /* Extension: shutdown time. */
- case SHUTDOWN_TIME:
- break;
- case USER_PROCESS:
- FTOU_ID(fu, ut);
- FTOU_STRING(fu, ut, user);
- FTOU_STRING(fu, ut, line);
- /* Extension: host name. */
- FTOU_STRING(fu, ut, host);
- FTOU_PID(fu, ut);
- break;
- case INIT_PROCESS:
- FTOU_ID(fu, ut);
- FTOU_PID(fu, ut);
- break;
- case LOGIN_PROCESS:
- FTOU_ID(fu, ut);
- FTOU_STRING(fu, ut, user);
- FTOU_PID(fu, ut);
- break;
- case DEAD_PROCESS:
- FTOU_ID(fu, ut);
- FTOU_PID(fu, ut);
- break;
- default:
- ut->ut_type = EMPTY;
- return;
- }
-
- FTOU_TYPE(fu, ut);
- FTOU_TV(fu, ut);
-}
-
static struct futx *
getfutxent(void)
{
Modified: user/ed/utmpx/lib/libc/gen/pututxline.c
==============================================================================
--- user/ed/utmpx/lib/libc/gen/pututxline.c Thu Jan 7 23:17:48 2010 (r201762)
+++ user/ed/utmpx/lib/libc/gen/pututxline.c Thu Jan 7 23:28:58 2010 (r201763)
@@ -28,6 +28,7 @@
__FBSDID("$FreeBSD$");
#include "namespace.h"
+#include <sys/endian.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
@@ -55,50 +56,6 @@ futx_open(const char *file)
return (fd);
}
-static int
-utx_to_futx(const struct utmpx *ut, struct futx *fu)
-{
-
- memset(fu, 0, sizeof *fu);
-
- switch (ut->ut_type) {
- case BOOT_TIME:
- case OLD_TIME:
- case NEW_TIME:
- /* Extension: shutdown time. */
- case SHUTDOWN_TIME:
- break;
- case USER_PROCESS:
- UTOF_ID(ut, fu);
- UTOF_STRING(ut, fu, user);
- UTOF_STRING(ut, fu, line);
- /* Extension: host name. */
- UTOF_STRING(ut, fu, host);
- UTOF_PID(ut, fu);
- break;
- case INIT_PROCESS:
- UTOF_ID(ut, fu);
- UTOF_PID(ut, fu);
- break;
- case LOGIN_PROCESS:
- UTOF_ID(ut, fu);
- UTOF_STRING(ut, fu, user);
- UTOF_PID(ut, fu);
- break;
- case DEAD_PROCESS:
- UTOF_ID(ut, fu);
- UTOF_PID(ut, fu);
- break;
- default:
- return (-1);
- }
-
- UTOF_TYPE(ut, fu);
- UTOF_TV(ut, fu);
-
- return (0);
-}
-
static void
utx_active_add(const struct futx *fu)
{
@@ -254,9 +211,9 @@ struct utmpx *
pututxline(const struct utmpx *utmpx)
{
struct futx fu;
+ static struct utmpx ut;
- if (utx_to_futx(utmpx, &fu) != 0)
- return (NULL);
+ utx_to_futx(utmpx, &fu);
switch (fu.fu_type) {
case BOOT_TIME:
@@ -285,6 +242,6 @@ pututxline(const struct utmpx *utmpx)
}
utx_log_add(&fu);
- /* XXX: return an entry on success! */
- return (NULL);
+ futx_to_utx(&fu, &ut);
+ return (&ut);
}
Added: user/ed/utmpx/lib/libc/gen/utxdb.c
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ user/ed/utmpx/lib/libc/gen/utxdb.c Thu Jan 7 23:28:58 2010 (r201763)
@@ -0,0 +1,162 @@
+/*-
+ * Copyright (c) 2010 Ed Schouten <ed at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "namespace.h"
+#include <sys/endian.h>
+#include <sys/param.h>
+#include <string.h>
+#include <utmpx.h>
+#include "utxdb.h"
+#include "un-namespace.h"
+
+#define UTOF_STRING(ut, fu, field) do { \
+ strncpy((fu)->fu_ ## field, (ut)->ut_ ## field, \
+ MIN(sizeof (fu)->fu_ ## field, sizeof (ut)->ut_ ## field)); \
+} while (0)
+#define UTOF_ID(ut, fu) do { \
+ memcpy((fu)->fu_id, (ut)->ut_id, \
+ MIN(sizeof (fu)->fu_id, sizeof (ut)->ut_id)); \
+} while (0)
+#define UTOF_PID(ut, fu) do { \
+ (fu)->fu_pid = htobe32((ut)->ut_pid); \
+} while (0)
+#define UTOF_TYPE(ut, fu) do { \
+ (fu)->fu_type = (ut)->ut_type; \
+} while (0)
+#define UTOF_TV(ut, fu) do { \
+ (fu)->fu_tv = htobe64((uint64_t)(ut)->ut_tv.tv_sec * 1000000 + \
+ (uint64_t)(ut)->ut_tv.tv_usec); \
+} while (0)
+
+void
+utx_to_futx(const struct utmpx *ut, struct futx *fu)
+{
+
+ memset(fu, 0, sizeof *fu);
+
+ switch (ut->ut_type) {
+ case BOOT_TIME:
+ case OLD_TIME:
+ case NEW_TIME:
+ /* Extension: shutdown time. */
+ case SHUTDOWN_TIME:
+ break;
+ case USER_PROCESS:
+ UTOF_ID(ut, fu);
+ UTOF_STRING(ut, fu, user);
+ UTOF_STRING(ut, fu, line);
+ /* Extension: host name. */
+ UTOF_STRING(ut, fu, host);
+ UTOF_PID(ut, fu);
+ break;
+ case INIT_PROCESS:
+ UTOF_ID(ut, fu);
+ UTOF_PID(ut, fu);
+ break;
+ case LOGIN_PROCESS:
+ UTOF_ID(ut, fu);
+ UTOF_STRING(ut, fu, user);
+ UTOF_PID(ut, fu);
+ break;
+ case DEAD_PROCESS:
+ UTOF_ID(ut, fu);
+ UTOF_PID(ut, fu);
+ break;
+ default:
+ fu->fu_type = EMPTY;
+ return;
+ }
+
+ UTOF_TYPE(ut, fu);
+ UTOF_TV(ut, fu);
+}
+
+#define FTOU_STRING(fu, ut, field) do { \
+ strncpy((ut)->ut_ ## field, (fu)->fu_ ## field, \
+ MIN(sizeof (ut)->ut_ ## field - 1, sizeof (fu)->fu_ ## field)); \
+} while (0)
+#define FTOU_ID(fu, ut) do { \
+ memcpy((ut)->ut_id, (fu)->fu_id, \
+ MIN(sizeof (ut)->ut_id, sizeof (fu)->fu_id)); \
+} while (0)
+#define FTOU_PID(fu, ut) do { \
+ (ut)->ut_pid = be32toh((fu)->fu_pid); \
+} while (0)
+#define FTOU_TYPE(fu, ut) do { \
+ (ut)->ut_type = (fu)->fu_type; \
+} while (0)
+#define FTOU_TV(fu, ut) do { \
+ uint64_t t; \
+ t = be64toh((fu)->fu_tv); \
+ (ut)->ut_tv.tv_sec = t / 1000000; \
+ (ut)->ut_tv.tv_usec = t % 1000000; \
+} while (0)
+
+void
+futx_to_utx(const struct futx *fu, struct utmpx *ut)
+{
+
+ memset(ut, 0, sizeof *ut);
+
+ switch (fu->fu_type) {
+ case BOOT_TIME:
+ case OLD_TIME:
+ case NEW_TIME:
+ /* Extension: shutdown time. */
+ case SHUTDOWN_TIME:
+ break;
+ case USER_PROCESS:
+ FTOU_ID(fu, ut);
+ FTOU_STRING(fu, ut, user);
+ FTOU_STRING(fu, ut, line);
+ /* Extension: host name. */
+ FTOU_STRING(fu, ut, host);
+ FTOU_PID(fu, ut);
+ break;
+ case INIT_PROCESS:
+ FTOU_ID(fu, ut);
+ FTOU_PID(fu, ut);
+ break;
+ case LOGIN_PROCESS:
+ FTOU_ID(fu, ut);
+ FTOU_STRING(fu, ut, user);
+ FTOU_PID(fu, ut);
+ break;
+ case DEAD_PROCESS:
+ FTOU_ID(fu, ut);
+ FTOU_PID(fu, ut);
+ break;
+ default:
+ ut->ut_type = EMPTY;
+ return;
+ }
+
+ FTOU_TYPE(fu, ut);
+ FTOU_TV(fu, ut);
+}
Modified: user/ed/utmpx/lib/libc/gen/utxdb.h
==============================================================================
--- user/ed/utmpx/lib/libc/gen/utxdb.h Thu Jan 7 23:17:48 2010 (r201762)
+++ user/ed/utmpx/lib/libc/gen/utxdb.h Thu Jan 7 23:28:58 2010 (r201763)
@@ -29,9 +29,6 @@
#ifndef _UTXDB_H_
#define _UTXDB_H_
-#include <sys/cdefs.h>
-#include <sys/endian.h>
-#include <sys/param.h>
#include <stdint.h>
#define _PATH_UTX_ACTIVE "/var/run/utx.active"
@@ -46,6 +43,8 @@
* those at the front.
*/
+struct utmpx;
+
struct futx {
uint8_t fu_type;
uint64_t fu_tv;
@@ -56,44 +55,7 @@ struct futx {
char fu_host[128];
} __packed;
-#define FTOU_STRING(fu, ut, field) do { \
- strncpy((ut)->ut_ ## field, (fu)->fu_ ## field, \
- MIN(sizeof (ut)->ut_ ## field - 1, sizeof (fu)->fu_ ## field)); \
-} while (0)
-#define FTOU_ID(fu, ut) do { \
- memcpy((ut)->ut_id, (fu)->fu_id, \
- MIN(sizeof (ut)->ut_id, sizeof (fu)->fu_id)); \
-} while (0)
-#define FTOU_PID(fu, ut) do { \
- (ut)->ut_pid = be32toh((fu)->fu_pid); \
-} while (0)
-#define FTOU_TYPE(fu, ut) do { \
- (ut)->ut_type = (fu)->fu_type; \
-} while (0)
-#define FTOU_TV(fu, ut) do { \
- uint64_t t; \
- t = be64toh((fu)->fu_tv); \
- (ut)->ut_tv.tv_sec = t / 1000000; \
- (ut)->ut_tv.tv_usec = t % 1000000; \
-} while (0)
-
-#define UTOF_STRING(ut, fu, field) do { \
- strncpy((fu)->fu_ ## field, (ut)->ut_ ## field, \
- MIN(sizeof (fu)->fu_ ## field, sizeof (ut)->ut_ ## field)); \
-} while (0)
-#define UTOF_ID(ut, fu) do { \
- memcpy((fu)->fu_id, (ut)->ut_id, \
- MIN(sizeof (fu)->fu_id, sizeof (ut)->ut_id)); \
-} while (0)
-#define UTOF_PID(ut, fu) do { \
- (fu)->fu_pid = htobe32((ut)->ut_pid); \
-} while (0)
-#define UTOF_TYPE(ut, fu) do { \
- (fu)->fu_type = (ut)->ut_type; \
-} while (0)
-#define UTOF_TV(ut, fu) do { \
- (fu)->fu_tv = htobe64((uint64_t)(ut)->ut_tv.tv_sec * 1000000 + \
- (uint64_t)(ut)->ut_tv.tv_usec); \
-} while (0)
+void utx_to_futx(const struct utmpx *, struct futx *);
+void futx_to_utx(const struct futx *, struct utmpx *);
#endif /* !_UTXDB_H_ */
More information about the svn-src-user
mailing list