diff options
author | Mystery Man <unknown@mandriva.org> | 2003-08-20 07:37:27 +0000 |
---|---|---|
committer | Mystery Man <unknown@mandriva.org> | 2003-08-20 07:37:27 +0000 |
commit | 7f2ac73888b8ef372ea597049981b27e7d810ac2 (patch) | |
tree | 354132889b63bbe5f446cb82869b3c3367135a80 /mdk-stage1/dietlibc/libugly/utent.c | |
parent | 327bd24f8e4291bd1882de1990dd7339f781a9cb (diff) | |
download | drakx-7f2ac73888b8ef372ea597049981b27e7d810ac2.tar drakx-7f2ac73888b8ef372ea597049981b27e7d810ac2.tar.gz drakx-7f2ac73888b8ef372ea597049981b27e7d810ac2.tar.bz2 drakx-7f2ac73888b8ef372ea597049981b27e7d810ac2.tar.xz drakx-7f2ac73888b8ef372ea597049981b27e7d810ac2.zip |
This commit was manufactured by cvs2svn to create branch 'MDKC_1_0'.topic/MDKC_1_0
Diffstat (limited to 'mdk-stage1/dietlibc/libugly/utent.c')
-rw-r--r-- | mdk-stage1/dietlibc/libugly/utent.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/mdk-stage1/dietlibc/libugly/utent.c b/mdk-stage1/dietlibc/libugly/utent.c new file mode 100644 index 000000000..d8d5d1ba7 --- /dev/null +++ b/mdk-stage1/dietlibc/libugly/utent.c @@ -0,0 +1,103 @@ +#include <unistd.h> +#include <fcntl.h> +#include <string.h> +#include <utmp.h> + +static const char *utmp_file_name = _PATH_UTMP; +static int fd = -1; + +static int lock_record(int type) { + struct flock fl; + fl.l_whence = SEEK_CUR; + fl.l_start = 0; + fl.l_len = sizeof(struct utmp); + fl.l_pid = 0; + fl.l_type = type; + return fcntl(fd, F_SETLKW, &fl); +} + +static int unlock_record() { + struct flock fl; + fl.l_whence = SEEK_CUR; + fl.l_start = -sizeof(struct utmp); + fl.l_len = sizeof(struct utmp); + fl.l_pid = 0; + fl.l_type = F_UNLCK; + return fcntl(fd, F_SETLK, &fl); +} + +void utmpname(const char *file) { + if (file) + utmp_file_name = file; + else + utmp_file_name = _PATH_UTMP; +} + +void setutent() { + if (fd<0) fd = open(utmp_file_name,O_RDWR); + if (fd<0) fd = open(utmp_file_name,O_RDONLY); + fcntl (fd, F_SETFD, FD_CLOEXEC); + lseek(fd,0,SEEK_SET); +} + +void endutent() { + if (fd<0) return; + close(fd); fd=-1; +} + +struct utmp *getutent(void) { + static struct utmp getutent_tmp; + int ret; + + if (fd<0) { + setutent(); + if (fd<0) return 0; + } + if (lock_record(F_RDLCK)) return 0; + ret=read(fd, &getutent_tmp, sizeof(struct utmp)); + unlock_record(); + if (ret<1) return 0; + return &getutent_tmp; +} + +struct utmp *getutid(struct utmp *ut) { + struct utmp *tmp; + + while ((tmp = getutent())) { + if (ut->ut_type && (ut->ut_type <= OLD_TIME)) { + if (ut->ut_type == tmp->ut_type) break; + } + if ((ut->ut_type >= INIT_PROCESS) && (ut->ut_type <= DEAD_PROCESS)) { + if (!strncmp(ut->ut_id,tmp->ut_id,4)) break; + } + } + return tmp; +} + +struct utmp *getutline(struct utmp *ut) { + struct utmp *tmp; + + while ((tmp = getutent())) { + if ((tmp->ut_type == USER_PROCESS) || (tmp->ut_type == LOGIN_PROCESS)) { + if (!strncmp(ut->ut_line,tmp->ut_line,UT_LINESIZE)) break; + } + } + return tmp; +} + +void pututline(struct utmp *ut) { + struct utmp *tmp; + + if ((tmp = getutid(ut))) { + lseek(fd, - (off_t)sizeof(struct utmp), SEEK_CUR); + if (lock_record(F_WRLCK)) return; + write(fd, ut, sizeof(struct utmp)); + } + else { + lseek(fd, 0, SEEK_END); + if (lock_record(F_WRLCK)) return; + write(fd, ut, (off_t)sizeof(struct utmp)); + } + unlock_record(); +} + |