aboutsummaryrefslogtreecommitdiff
path: root/lib/libulog
diff options
context:
space:
mode:
authorEd Schouten <ed@FreeBSD.org>2009-12-26 22:36:05 +0000
committerEd Schouten <ed@FreeBSD.org>2009-12-26 22:36:05 +0000
commit444999a3147165b2f753184ec723a0c4e27b27bb (patch)
tree58bea5a34619877eb8346d2fea79c60e9cb0def0 /lib/libulog
parent76e42b3a0cdc3d51b2707fda606103ef897e9bc4 (diff)
downloadsrc-444999a3147165b2f753184ec723a0c4e27b27bb.tar.gz
src-444999a3147165b2f753184ec723a0c4e27b27bb.zip
Several refinements to libulog's API.
- Only set the fields in the ulog_utmpx structure that are valid for the command in question. This means that strings like "shutdown" or "~" are not visible to the user anymore. - Rename UTXF_* to UTXI_*, indicating the indexation, instead of using the `antique' filename. If we ever get rid of utmp, it makes little sense calling it by its old name.
Notes
Notes: svn path=/head/; revision=201033
Diffstat (limited to 'lib/libulog')
-rw-r--r--lib/libulog/ulog.h6
-rw-r--r--lib/libulog/ulog_getutxent.c65
-rw-r--r--lib/libulog/ulog_setutxfile.314
3 files changed, 50 insertions, 35 deletions
diff --git a/lib/libulog/ulog.h b/lib/libulog/ulog.h
index 1e45aed3569c..b57c0e0eb12f 100644
--- a/lib/libulog/ulog.h
+++ b/lib/libulog/ulog.h
@@ -85,9 +85,9 @@ void ulog_setutxent(void);
/* Extensions. */
struct ulog_utmpx *ulog_getutxuser(const char *);
int ulog_setutxfile(int, const char *);
-#define UTXF_UTMP 0
-#define UTXF_WTMP 1
-#define UTXF_LASTLOG 2
+#define UTXI_TTY 0
+#define UTXI_TIME 1
+#define UTXI_USER 2
/* Login/logout utility functions. */
void ulog_login(const char *, const char *, const char *);
diff --git a/lib/libulog/ulog_getutxent.c b/lib/libulog/ulog_getutxent.c
index 7ffc73913977..bcfab480cbed 100644
--- a/lib/libulog/ulog_getutxent.c
+++ b/lib/libulog/ulog_getutxent.c
@@ -38,7 +38,7 @@ __FBSDID("$FreeBSD$");
#include "ulog_internal.h"
static FILE *ufile;
-static int ufiletype = -1;
+static int ufileindex = -1;
void
ulog_endutxent(void)
@@ -61,11 +61,8 @@ ulog_futmp_to_utmpx(const struct futmp *ut, struct ulog_utmpx *utx)
strncpy(utx->ut_ ## field, ut->ut_ ## field, \
MIN(sizeof utx->ut_ ## field - 1, sizeof ut->ut_ ## field));\
} while (0)
- COPY_STRING(user);
- COPY_STRING(line);
- COPY_STRING(host);
-#undef COPY_STRING
-#define MATCH(field, value) (strcmp(utx->ut_ ## field, (value)) == 0)
+#define MATCH(field, value) (strncmp(ut->ut_ ## field, (value), \
+ sizeof(ut->ut_ ## field)) == 0)
if (MATCH(user, "reboot") && MATCH(line, "~"))
utx->ut_type = BOOT_TIME;
else if (MATCH(user, "date") && MATCH(line, "|"))
@@ -74,12 +71,23 @@ ulog_futmp_to_utmpx(const struct futmp *ut, struct ulog_utmpx *utx)
utx->ut_type = NEW_TIME;
else if (MATCH(user, "shutdown") && MATCH(line, "~"))
utx->ut_type = SHUTDOWN_TIME;
- else if (MATCH(user, "") && MATCH(host, ""))
+ else if (MATCH(user, "") && MATCH(host, "")) {
utx->ut_type = DEAD_PROCESS;
- else if (!MATCH(user, "") && !MATCH(line, "") && ut->ut_time != 0)
+ /* XXX: ut_id and ut_pid missing. ut_line not needed. */
+ COPY_STRING(line);
+ } else if (!MATCH(user, "") && !MATCH(line, "") && ut->ut_time != 0) {
utx->ut_type = USER_PROCESS;
- else
+ /* XXX: ut_id and ut_pid missing. */
+ COPY_STRING(user);
+ COPY_STRING(line);
+ COPY_STRING(host);
+ } else {
+ /* Only set ut_type for EMPTY. */
utx->ut_type = EMPTY;
+ return;
+ }
+#undef COPY_STRING
+#undef MATCH
utx->ut_tv.tv_sec = _time32_to_time(ut->ut_time);
utx->ut_tv.tv_usec = 0;
}
@@ -93,13 +101,20 @@ ulog_flastlog_to_utmpx(const struct flastlog *ll, struct ulog_utmpx *utx)
strncpy(utx->ut_ ## field, ll->ll_ ## field, \
MIN(sizeof utx->ut_ ## field - 1, sizeof ll->ll_ ## field));\
} while (0)
- COPY_STRING(line);
- COPY_STRING(host);
-#undef COPY_STRING
- if (!MATCH(line, "") && ll->ll_time != 0)
+#define MATCH(field, value) (strncmp(ll->ll_ ## field, (value), \
+ sizeof(ll->ll_ ## field)) == 0)
+ if (!MATCH(line, "") && ll->ll_time != 0) {
utx->ut_type = USER_PROCESS;
- else
+ /* XXX: ut_id and ut_pid missing. */
+ COPY_STRING(line);
+ COPY_STRING(host);
+ } else {
+ /* Only set ut_type for EMPTY. */
utx->ut_type = EMPTY;
+ return;
+ }
+#undef COPY_STRING
+#undef MATCH
utx->ut_tv.tv_sec = _time32_to_time(ll->ll_time);
utx->ut_tv.tv_usec = 0;
}
@@ -112,7 +127,7 @@ static inline off_t
ulog_tell(void)
{
- if (ufiletype == UTXF_LASTLOG)
+ if (ufileindex == UTXI_USER)
return (ftello(ufile) / sizeof(struct flastlog));
else
return (ftello(ufile) / sizeof(struct futmp));
@@ -132,7 +147,7 @@ ulog_read(off_t off, int whence, int resolve_user)
if (whence == SEEK_SET && ulog_tell() > off)
return (NULL);
- if (ufiletype == UTXF_LASTLOG) {
+ if (ufileindex == UTXI_USER) {
struct flastlog ll;
struct passwd *pw = NULL;
uid_t uid;
@@ -192,7 +207,7 @@ ulog_getutxline(const struct ulog_utmpx *line)
if (ufile == NULL)
return (NULL);
- if (ufiletype == UTXF_UTMP) {
+ if (ufileindex == UTXI_TTY) {
unsigned int slot;
slot = ulog_ttyslot(line->ut_line);
@@ -228,7 +243,7 @@ ulog_getutxuser(const char *user)
{
struct ulog_utmpx *utx;
- if (ufiletype == UTXF_LASTLOG) {
+ if (ufileindex == UTXI_USER) {
struct passwd *pw;
pw = getpwnam(user);
@@ -258,20 +273,20 @@ ulog_getutxuser(const char *user)
*/
int
-ulog_setutxfile(int type, const char *file)
+ulog_setutxfile(int uidx, const char *file)
{
/* Supply default files. */
- switch (type) {
- case UTXF_UTMP:
+ switch (uidx) {
+ case UTXI_TTY:
if (file == NULL)
file = _PATH_UTMP;
break;
- case UTXF_WTMP:
+ case UTXI_TIME:
if (file == NULL)
file = _PATH_WTMP;
break;
- case UTXF_LASTLOG:
+ case UTXI_USER:
if (file == NULL)
file = _PATH_LASTLOG;
break;
@@ -282,7 +297,7 @@ ulog_setutxfile(int type, const char *file)
if (ufile != NULL)
fclose(ufile);
ufile = fopen(file, "r");
- ufiletype = type;
+ ufileindex = uidx;
if (ufile == NULL)
return (-1);
return (0);
@@ -298,5 +313,5 @@ void
ulog_setutxent(void)
{
- ulog_setutxfile(UTXF_UTMP, NULL);
+ ulog_setutxfile(UTXI_TTY, NULL);
}
diff --git a/lib/libulog/ulog_setutxfile.3 b/lib/libulog/ulog_setutxfile.3
index 9760a168381c..e26380ca5fd0 100644
--- a/lib/libulog/ulog_setutxfile.3
+++ b/lib/libulog/ulog_setutxfile.3
@@ -36,7 +36,7 @@
.Sh SYNOPSIS
.In ulog.h
.Ft int
-.Fn ulog_setutxfile "int type" "const char *file"
+.Fn ulog_setutxfile "int index" "const char *file"
.Ft struct ulog_utmpx *
.Fn ulog_getutxuser "const char *user"
.Sh DESCRIPTION
@@ -54,17 +54,17 @@ function is similar to
.Fn ulog_setutxent ,
but is capable of returning an error code and also gives access to other
login record files by using one of the following values for
-.Fa type :
-.Bl -tag -width UTXF_LASTLOG
-.It Dv UTXF_UTMP
+.Fa index :
+.Bl -tag -width UTXI_TIME
+.It Dv UTXI_TTY
Open the default
.Nm utmp
file, which is indexed by TTY device.
-.It Dv UTXF_WTMP
+.It Dv UTXI_TIME
Open the
.Nm wtmp
file, which is indexed by time.
-.It Dv UTXF_LASTLOG
+.It Dv UTXI_USER
Open the
.Nm lastlog
file, which is indexed by user ID.
@@ -73,7 +73,7 @@ file, which is indexed by user ID.
The
.Fa file
argument determines the file to be opened.
-If left null, implementation-defined default file is opened.
+If left null, an implementation-defined default file is opened.
.Pp
The
.Fn ulog_getutxuser