사용자 관련 정보 검색함수 getutxent()에 대해서
오늘 우연히 사용하게 되어 이에 대한 기록을 남겨놓고 감.
utmp => who 명령어에서 사용함.
wtmp => last 명령어에서 사용함
함수를 사용할때 추가해야할 해더파일
#include <utmpx.h>
getutxent함수와 관련된 다른 함수들
struct utmpx *getutxent(void);
void setutxent(void);
void endutxent(void);
int utmpxname(const char *file); => file - 교체할 파일이름이 매개변수로 넘어간다.
getutxent 함수는 /var/adm/utmpx 파일에서 로그인 정보를 순차적으로 읽어들임.
setutxent 함수는 /var/adm/utmpx 파일의 오프셋을 파일의 시작에 위치시킴.
endutxent 함수는 /var/adm/utmpx 위에서 열었던 파일을 닫는다.
utmpxname 함수는 로그인 정보 파일을 file로 지정한 다른 파일로 변경.
해당 정보가 있는 파일의 경로 (각 OS별)
|
로그파일 |
Solaris |
HP UX |
LINUX |
|
wtmp,utmp |
/var/adm/wtmpx /var/adm/utmpx |
/usr/adm/wtmp /etc/utmp |
/var/log/wtmp /var/run/utmp |
====================================
#include "sys/types.h" #include#include int main(void) { struct utmpx *utx; printf("-----------------\n"); printf(" Login User\n"); printf("-----------------\n"); while((utx=getutxent()) != NULL) { if(utx->ut_type != USER_PROCESS) continue; printf("%10s %5s\n", utx->ut_user, utx->ut_line); } return 0; }
결과화면
$ ./a.out
-----------------
Login User
-----------------
telcosys tty1
fresh pts/4
jeon pts/3
* who 명령의 결과화면
$ who
telcosys tty1 May 17 09:08
fresh pts/4 Aug 13 14:34 (10.10.101.55)
jeon pts/3 Aug 13 19:57 (10.10.104.55)
====================================
utmp파일에는 현재 접속중(Telnet 등..)인 사용자의 정보가 저장되는데,
로그아웃을 하면 이에 대한 정보가 삭제됨.
wtmp파일은 로그인시 utmp파일에 쓰이는 것과 같은 정보가 쓰여지고,
사용자가 로그아웃을 하면 그 정보도 쓰여지게 됨.(updwtmp).
또한 컴퓨터의 리부팅과, 몇몇정보들이 추가로 쓰여짐.
원본 source 파일을 첨부해서 살펴보면 아래와 같다.
출처 : http://www2.oldlinux.org/Linux.old/Linux-0.98/Yggdrasil-0.98.3/usr/src/sbin/getty_ps/getutent.c
==========================
/* getutxent.c */ #include#include struct utmpx * getutxent (void) { return (struct utmpx *) __getutent (); }
/* getutent.c */
#include "getty.h"
#if defined(RCSID) && !defined(lint)
static char *RcsId =
"@(#)$Id: getutent.c,v 2.0 90/09/19 20:00:51 paul Rel $";
#endif
typedef struct utmp UTMP;
static char *utmpfil = UTMP_FILE; /* default utmp file */
static FILE *ufp = (FILE *) NULL; /* file pointer to utmp file */
/* NULL = no utmp file open */
static UTMP ut; /* buffer for utmp record */
/*
** getutent() - get next valid utmp entry
**
** Returns (UTMP*)NULL if no vaild entry found.
*/
UTMP *
getutent()
{
if (ufp == (FILE *) NULL)
if ((ufp = fopen(utmpfil, "r+")) == (FILE *) NULL)
return((UTMP *) NULL);
do {
if (fread((char *)&ut, sizeof(ut), 1, ufp) != 1)
return((UTMP *) NULL);
} while (ut.ut_name[0] == '\0'); /* valid entry? */
return(&ut);
}
/*
** getutline() - get utmp entry that matches line.
**
** Returns (UTMP*)NULL if no match found.
*/
UTMP *
getutline(line)
register UTMP *line;
{
do {
if (strequal(ut.ut_line, line->ut_line))
return(&ut); /* match! */
} while (getutent() != NULL);
return((UTMP *) NULL);
}
/*
** setutent() - rewind utmp back to beginning
*/
void
setutent()
{
if (ufp != (FILE *) NULL)
rewind(ufp);
}
/*
** endutent() - close utmp file
*/
void
endutent()
{
if (ufp != (FILE *) NULL) {
(void) fclose(ufp);
ufp = (FILE *) NULL;
}
}
/*
** utmpname() - change utmp file name to "file"
*/
void
utmpname(file)
register char *file;
{
endutent();
utmpfil = strdup(file);
}
/* end of getutent.c */
==========================
'엔지니어' 카테고리의 다른 글
| strchr 함수 예제 (443) | 2012.08.14 |
|---|---|
| strdup 함수 예제 (477) | 2012.08.14 |
| strtok 함수 예제 (3378) | 2012.08.06 |
| 특수문자 파일명 디렉토리명 삭제 (inode이용) (446) | 2012.07.31 |
| writev 함수 예제 (473) | 2012.07.31 |