ÃÑ ÆäÀÌÁö ¼ö : 3224
![]()
|
Facebook Joinc ±×·ì
Joinc QA »çÀÌÆ®
![]()
Tweet
joinc´Â Firefox¿Í chrome¿¡¼ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù. getutent
utmpÆÄÀÏÀ» ÀÌ¿ëÇØ¼ ·Î±×ÀÎ
»ç¿ë¹ý#include <utmp.h> struct utmp *getutent(void); struct utmp *getutid(const struct utmp *id); struct utmp *getutline(const struct utmp *line); struct utmp *pututline(const struct utmp *utmp); void setutent(void); void endutent(void); int utmpname(const char *file); ¼³¸í
utmp´Â Ä¿³Î¿¡¼ °ü¸®ÇÏ´Â ·Î±×ÀÎÅ×ÀÌºí °ü¸® ½Ã½ºÅÛÀ¸·Î ÇöÀç ·Î±×ÀÎµÈ À¯Àú¿Í ¾ðÁ¦ ·Î±×ÀÎÇß´ÂÁö, ¾î¶² Å͹̳Î(tty)¸¦ »ç¿ëÇϰí ÀÖ´ÂÁö, Á¢¼ÓÇÑ È£½ºÆ®ÀÇ IPµîÀÇ Á¤º¸¸¦ ³²±ä´Ù. ·Î±×¾Æ¿ô Çß´Ù¸é ¾ðÁ¦ Çß´ÂÁö¿¡ ´ëÇÑ Á¤º¸µµ ÀúÀåÇÑ´Ù. #include <utmp.h> struct utmp *getutent(void);
getutent(), getutid(), getutline(), pututline()¸¦ ÀÌ¿ëÇϸé utmp ±¸Á¶Ã¼ÀÇ Æ÷ÀÎÅ͸¦ ¸®ÅÏÇÑ´Ù. utmp ±¸Á¶Ã¼´Â ´ÙÀ½°ú °°Àº ¸â¹öº¯¼ö¸¦ °¡Áø´Ù. struct utmp
{
short ut_type; /* type of login */
pid_t ut_pid; /* pid of login process */
char ut_line[UT_LINESIZE]; /* device name of tty - "/dev/" */
char ut_id[4]; /* init id or abbrev. ttyname */
char ut_user[UT_NAMESIZE]; /* user name */
char ut_host[UT_HOSTSIZE]; /* hostname for remote login */
struct exit_status ut_exit; /* The exit status of a
process marked as DEAD_PROCESS. *
long ut_session; /* session ID, used for
struct timeval ut_tv; /* time entry was made. */
int32_t ut_addr_v6[4]; /* IP address of remote host. */
char pad[20]; /* Reserved for future use. */
};
getutid()´Â utmpÀÇ ÇöÀç ut_id ¿Í id_ut_id °¡ ÀÏÄ¡ÇÏ´Â °ÍÀ» ÇöÀç Æ÷ÀÎÆ®ºÎÅÍ ¿£Æ®¸®¸¦ µ¹¸é¼ ã¾Æ³½´Ù. ¿£Æ®¸®ÀÇ ³¡±îÁö À̵¿ÇßÀ»¶§±îÁö ÀÏÄ¡ÇÏ´Â Á¤º¸°¡ ¾ø´Ù¸é NULLÀ» ¸®ÅÏÇÑ´Ù.
getutline()´Â ut_line ¿Í line->ut_line ¹®ÀÚ¿ÀÌ ÀÏÄ¡ÇÏ´Â °ÍÀ» ã¾Æ¼ ¸®ÅÏÈù´Ù. ÀÏÄ¡ÇÏ´Â ¿£Æ®¸®°¡ ¾ø´Ù¸é NULLÀ» ¸®ÅÏÇÑ´Ù. ut_type ÀÌ LOGIN_PROCESSÀÎ °Í¿¡ ´ëÇØ¼ °Ë»çÇÑ´Ù.
setutent()¸¦ »ç¿ëÇϸé utmp ÆÄÀÏÀÇ Ã³À½À¸·Î À̵¿ÇÑ´Ù.
endutent() ÃÖ±Ù ¿¸° utmp ÆÄÀÏÀ» ´Ý´Â´Ù. ¹Ýȯ°ª
utmp±¸Á¶Ã¼¿¡ ´ëÇÑ Æ÷ÀÎÅ͸¦ µ¹·ÁÁØ´Ù. ´õÀÌ»ó µ¥ÀÌÅͰ¡ ¾ø´Ù¸é NULLÀ» µÇµ¹·ÁÁØ´Ù. ¿¡·¯
¿¡·¯°¡ ¹ß»ýÇϸé (struct utmp *)0 À» µÇµ¹·ÁÁØ´Ù.
#include <unistd.h> #include <utmp.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <time.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> int get_current_pid(int login_pid); char *get_current_procname(int proc_num); int main(int argc, char **argv) { struct utmp *utmpfp; struct utmp *myutmp; char *tty_name = NULL; char *host_ip; char now_local_time[50]; time_t the_time; struct tm *tm_ptr; myutmp = malloc(sizeof(struct utmp)); //utmpfp = malloc(sizeof(struct utmp)); setutent(); while ((utmpfp = getutent()) != NULL) { the_time = utmpfp->ut_time; tm_ptr = localtime(&the_time); sprintf(now_local_time, "%d/%02d/%02d %02d:%02d", tm_ptr->tm_year+1900, tm_ptr->tm_mon+1, tm_ptr->tm_mday, tm_ptr->tm_hour, tm_ptr->tm_min); host_ip = utmpfp->ut_host; if (strlen(host_ip) < 1) host_ip = "-"; if (utmpfp->ut_type == USER_PROCESS) { printf("%-12s %-8s %-12s %s %s\n", utmpfp->ut_user, utmpfp->ut_line, host_ip, now_local_time, now_local_time, get_current_procname(get_current_pid(utmpfp->ut_pid))); } } setutent(); printf("\nmy utmp info is ---\n"); tty_name = ttyname(0); printf("%s\n",tty_name+5); strcpy(myutmp->ut_line, tty_name+5); utmpfp = getutline(myutmp); if(utmpfp == NULL) { printf("Not Fild utmp\n"); exit(0); } printf("%d\n", utmpfp->ut_time); the_time = utmpfp->ut_time; tm_ptr = localtime(&the_time); sprintf(now_local_time, "%d/%02d/%02d %02d:%02d", tm_ptr->tm_year+1900, tm_ptr->tm_mon+1, tm_ptr->tm_mday, tm_ptr->tm_hour, tm_ptr->tm_min); printf("%-12s %-8s %-12s %s %s\n", utmpfp->ut_user, utmpfp->ut_line, host_ip, now_local_time, get_current_procname(get_current_pid(utmpfp->ut_pid))); return 1; } int get_current_pid(int login_pid) { int fd; char buf[255]; char stat_file[25]; int field_num = 7; int i, j; int buf_index; char current_pid[11]; memset(buf, '\0', 255); sprintf(stat_file, "/proc/%d/stat", login_pid); if ((fd = open(stat_file, O_RDONLY)) == -1) { printf("error\n"); return -1; } read(fd, buf, 255); j = 0; for (i = 0, buf_index=0; i < 255; i++) { if (buf[i] == ' ') { j++; } else { if (j == field_num) { current_pid[buf_index] = buf[i]; buf_index++; } if (j > field_num) break; } } close(fd); return atoi(current_pid); } char *get_current_procname(int proc_num) { char *buf; char proc_file[256]; buf = (char *)malloc(256); memset(buf, '\0', 256); memset(proc_file, '\0', 256); sprintf(proc_file, "/proc/%d/exe", proc_num); readlink(proc_file, buf, 256); return buf; } |
|
|
EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù. |
|