svn commit: r202301 - in head: . lib/libc/gen usr.bin usr.bin/wtmpcvt

Ed Schouten ed at FreeBSD.org
Thu Jan 14 20:58:46 UTC 2010


Author: ed
Date: Thu Jan 14 20:58:45 2010
New Revision: 202301
URL: http://svn.freebsd.org/changeset/base/202301

Log:
  Add wtmpcvt(1).
  
  This utility allows users to convert their wtmp databases to the new
  format. It makes no sense for users to keep their wtmp log files if they
  are unable to view them.
  
  It basically copies ut_line into ut_id as well. This makes it possible
  for last(1) and ac(8) to match login records with their corresponding
  logout record.

Added:
  head/usr.bin/wtmpcvt/
  head/usr.bin/wtmpcvt/Makefile   (contents, props changed)
  head/usr.bin/wtmpcvt/wtmpcvt.1   (contents, props changed)
  head/usr.bin/wtmpcvt/wtmpcvt.c   (contents, props changed)
Modified:
  head/UPDATING
  head/lib/libc/gen/getutxent.3
  head/usr.bin/Makefile

Modified: head/UPDATING
==============================================================================
--- head/UPDATING	Thu Jan 14 20:58:19 2010	(r202300)
+++ head/UPDATING	Thu Jan 14 20:58:45 2010	(r202301)
@@ -37,7 +37,9 @@ NOTE TO PEOPLE WHO THINK THAT FreeBSD 9.
 
 	After the system has been upgraded, it is safe to remove the old
 	log files (/var/run/utmp, /var/log/lastlog and /var/log/wtmp*),
-	assuming their contents is of no importance anymore.
+	assuming their contents is of no importance anymore.  Old wtmp
+	databases can only be used by last(1) and ac(8) after they have
+	been converted to the new format using wtmpcvt(1).
 
 20100108:
 	Introduce the kernel thread "deadlock resolver" (which can be enabled

Modified: head/lib/libc/gen/getutxent.3
==============================================================================
--- head/lib/libc/gen/getutxent.3	Thu Jan 14 20:58:19 2010	(r202300)
+++ head/lib/libc/gen/getutxent.3	Thu Jan 14 20:58:45 2010	(r202301)
@@ -401,6 +401,7 @@ The file format is invalid.
 .Sh SEE ALSO
 .Xr last 1 ,
 .Xr write 1 ,
+.Xr wtmpcvt 1 ,
 .Xr getpid 2 ,
 .Xr gettimeofday 2 ,
 .Xr tty 4 ,

Modified: head/usr.bin/Makefile
==============================================================================
--- head/usr.bin/Makefile	Thu Jan 14 20:58:19 2010	(r202300)
+++ head/usr.bin/Makefile	Thu Jan 14 20:58:45 2010	(r202301)
@@ -225,6 +225,7 @@ SUBDIR=	alias \
 	who \
 	whois \
 	write \
+	wtmpcvt \
 	xargs \
 	xinstall \
 	${_xlint} \

Added: head/usr.bin/wtmpcvt/Makefile
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.bin/wtmpcvt/Makefile	Thu Jan 14 20:58:45 2010	(r202301)
@@ -0,0 +1,5 @@
+# $FreeBSD$
+
+PROG=	wtmpcvt
+
+.include <bsd.prog.mk>

Added: head/usr.bin/wtmpcvt/wtmpcvt.1
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.bin/wtmpcvt/wtmpcvt.1	Thu Jan 14 20:58:45 2010	(r202301)
@@ -0,0 +1,66 @@
+.\" 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.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd January 14, 2010
+.Os
+.Dt WTMPCVT 1
+.Sh NAME
+.Nm wtmpcvt
+.Nd convert wtmp files to the utmpx format
+.Sh SYNOPSIS
+.Nm
+.Ar input
+.Ar output
+.Sh DESCRIPTION
+The
+.Nm
+utility converts traditional
+.Pa wtmp
+user accounting database files to the same format that is used by
+.Pa /var/log/utx.log .
+This makes it possible to view their contents using existing accouting
+utilities, such as
+.Xr last 1
+and
+.Xr ac 8 .
+.Sh SEE ALSO
+.Xr last 1 ,
+.Xr getutxent 3 ,
+.Xr ac 8
+.Sh HISTORY
+The
+.Nm
+utility appeared in
+.Fx 9.0 .
+.Sh BUGS
+The application assumes the
+.Ar input
+file has the same byte order as the host system.
+The
+.Ar output
+file can be used on any architecture.
+.Sh AUTHORS
+.An Ed Schouten Aq ed at FreeBSD.org

Added: head/usr.bin/wtmpcvt/wtmpcvt.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.bin/wtmpcvt/wtmpcvt.c	Thu Jan 14 20:58:45 2010	(r202301)
@@ -0,0 +1,138 @@
+/*-
+ * 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 <sys/endian.h>
+#include <sys/param.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <utmpx.h>
+
+#include "../../lib/libc/gen/utxdb.h"
+
+struct outmp {
+	char	ut_line[8];
+	char	ut_user[16];
+	char	ut_host[16];
+	int32_t	ut_time;
+};
+
+static void
+usage(void)
+{
+
+	fprintf(stderr, "usage: wtmpcvt input output\n");
+	exit(1);
+}
+
+static void
+outmp_to_futx(const struct outmp *ui, struct futx *uo)
+{
+
+	memset(uo, 0, sizeof *uo);
+#define	COPY_STRING(field) do {						\
+	strncpy(uo->fu_ ## field, ui->ut_ ## field,			\
+	    MIN(sizeof uo->fu_ ## field, sizeof ui->ut_ ## field));	\
+} while (0)
+#define	COPY_LINE_TO_ID() do {						\
+	strncpy(uo->fu_id, ui->ut_line,					\
+	    MIN(sizeof uo->fu_id, sizeof ui->ut_line));			\
+} while (0)
+#define	MATCH(field, value)	(strncmp(ui->ut_ ## field, (value),	\
+					sizeof(ui->ut_ ## field)) == 0)
+	if (MATCH(user, "reboot") && MATCH(line, "~"))
+		uo->fu_type = BOOT_TIME;
+	else if (MATCH(user, "date") && MATCH(line, "|"))
+		uo->fu_type = OLD_TIME;
+	else if (MATCH(user, "date") && MATCH(line, "{"))
+		uo->fu_type = NEW_TIME;
+	else if (MATCH(user, "shutdown") && MATCH(line, "~"))
+		uo->fu_type = SHUTDOWN_TIME;
+	else if (MATCH(user, "") && MATCH(host, "") && !MATCH(line, "")) {
+		uo->fu_type = DEAD_PROCESS;
+		COPY_LINE_TO_ID();
+	} else if (!MATCH(user, "") && !MATCH(line, "") && ui->ut_time != 0) {
+		uo->fu_type = USER_PROCESS;
+		COPY_STRING(user);
+		COPY_STRING(line);
+		COPY_STRING(host);
+		COPY_LINE_TO_ID();
+	} else {
+		uo->fu_type = EMPTY;
+		return;
+	}
+#undef COPY_STRING
+#undef COPY_LINE_TO_ID
+#undef MATCH
+
+	/* Timestamp conversion. XXX: Assumes host byte order! */
+	uo->fu_tv = htobe64((uint64_t)ui->ut_time * 1000000);
+}
+
+int
+main(int argc, char *argv[])
+{
+	FILE *in, *out;
+	struct outmp ui;
+	struct futx uo;
+	size_t l;
+	uint16_t lo;
+
+	if (argc != 3)
+		usage();
+
+	/* Open files. */
+	in = fopen(argv[1], "r");
+	if (in == NULL)
+		err(1, argv[1]);
+	out = fopen(argv[2], "w");
+	if (out == NULL)
+		err(1, argv[2]);
+
+	/* Process entries. */
+	while (fread(&ui, sizeof ui, 1, in) == 1) {
+		outmp_to_futx(&ui, &uo);
+		if (uo.fu_type == EMPTY)
+			continue;
+
+		/* Write new entry to output file. */
+		for (l = sizeof uo; l > 0 &&
+		    ((const char *)&uo)[l - 1] == '\0'; l--);
+		lo = htobe16(l);
+		fwrite(&lo, sizeof lo, 1, out);
+		fwrite(&uo, l, 1, out);
+	}
+
+	fclose(in);
+	fclose(out);
+	return (0);
+}


More information about the svn-src-all mailing list