who命令的编写——登录与注销__who, whoami, who am i
来源:互联网 发布:sql left join where 编辑:程序博客网 时间:2024/05/16 14:21
摘要:who是一个linux下重要的系统内建指令,编写who命令,需要了解相关的数据结构struct utmp。同时进一步熟悉结构化数据的读取和访问方法。另外总结了系统调用相关的知识。
本文来源:http://blog.csdn.net/trochiluses/article/details/10825061
1.who程序的编写
#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<fcntl.h>#include<utmp.h>#include<time.h>#define SHOWHOSTint show_info(struct utmp *utbufp);void showtime(long);int main(int arv, char *arg[]){ struct utmp current_record; int utmpfd; int reclen=sizeof(struct utmp); if((utmpfd = open(UTMP_FILE,O_RDONLY))==-1) { perror(UTMP_FILE); exit(1); } while(read(utmpfd,¤t_record,reclen)==reclen) { show_info(¤t_record); } close(utmpfd); return 0;}int show_info(struct utmp *utbufp){ if(utbufp->ut_type!=USER_PROCESS) return; printf("%-8.8s",utbufp->ut_name); printf(" "); printf("%-8.8s",utbufp->ut_line); printf(" "); showtime(utbufp->ut_time); printf(" ");#ifdef SHOWHOST printf("%-8.8s",utbufp->ut_host);#endif printf("\n");}void showtime(long timeeval){ char *p; p=ctime(&timeeval ); printf("%12.12s",p+4);}
1)从who命令的man文档可以查看到它与utmp这个文件有关,进而man utmp这个文件,可以找到这个头文件和utmp这个结构体的一些信息。
2)open,write,close,seek,creat是与文件读写有关的系统调用,我们在下一篇博客里总结它们。
3)与时间有关的函数ctime,我们将用一篇文章总结它们。
4)同样,知道了登录信息是存在UTMPFILE,那么我们可以进一步编写注销信息,实际上就是将改写该文件中的数据而已。下面是注销的代码:
2.注销程序的编写
#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<utmp.h>#include<fcntl.h>#include<sys/types.h>#include<string.h>#include<time.h>int main(int argc, char *argv[]){ char *termname=argv[1]; logout(termname); return 0;}int logout(const char *ttyname){ int fd; struct utmp utmpunit; int utmpunitlen=sizeof(struct utmp); if ((fd=open(UTMP_FILE,O_RDWR))==-1) { perror("can not open file:"); return -1; } while(read(fd,&utmpunit,utmpunitlen )==utmpunitlen) { printf("%s %s\n",ttyname,utmpunit.ut_line ); if(strstr(ttyname,utmpunit.ut_line)!=NULL) { printf("find the login account\n"); utmpunit.ut_type=DEAD_PROCESS; utmpunit.ut_time=time(NULL); if(lseek(fd,-utmpunitlen,SEEK_CUR)!=-1) { if(write(fd,&utmpunit,utmpunitlen)==utmpunitlen) ; } break; } } if(close(fd)==-1) { perror("can not close file:"); return -1; }注意,注销的是当前终端,当前终端的名称可以用tty获得,作为命令行参数
3.析who,whoami,who am i
4.结构化提取utmp数据:
定义函数:struct utmp *getutent(void);
函数说明:getutent()用来从utmp 文件(/var/run/utmp)中读取一项登录数据, 该数据以utmp 结构返回. 第一次调用时会取得第一位用户数据, 之后每调用一次就会返回下一项数据, 直到已无任何数据时返回NULL。
void getutmp(const struct utmpx *ux, struct utmp *u);
void getutmpx(const struct utmp *u, struct utmpx *ux);
DESCRIPTION
The getutmp() function copies the fields of the utmpx structure pointed
to by ux to the corresponding fields of the utmp structure pointed to
by u. The getutmpx() function performs the converse operation.
5.另外一个命令w:
显示当前用户和他们正在进行的操作
$ w
14:57:22 up 3 days, 18:35, 6 users, load average: 0.38, 0.25, 0.22
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
hyk tty7 Sun20 3days 1:18m 2.17s gnome-session -
hyk pts/1 :0.0 Mon18 17:33m 5.06s 4.77s vncviewer 10.10
hyk pts/2 :0.0 Mon10 16:57m 0.48s 0.01s ssh huangyukun@
hyk pts/3 :0.0 10:43 3:44m 0.83s 2:01 gnome-terminal
hyk pts/4 :0.0 10:45 2.00s 0.91s 0.01s w
6.断电处理
如果系统忽然掉电,此时utmp文件里面的内容还没有来得及更改,所以下次重启的时候,init进程将会清除utmp的所有记录为空(设定为DEADPROCESS),大致如下:
init——login——getty——utmp
- who命令的编写——登录与注销__who, whoami, who am i
- Linux命令who am i,who,whoami
- w、who、whoami、who am i命令
- who whoami who am i的区别
- linux 性能监控命令——w,who,whoami,whois ,who am i
- Linux命令 whoami 和 who am i 的区别
- who命令、whoami命令和who am i命令的区别
- who、whoami 和 who am i 的区别
- who whoami who am i的区别(Linux)
- "who am i" vs "whoami"
- linux下who am i和whoami的区别
- LINUX 当中 who am i 和 whoami 的区别
- linux系统下who&who am i与whoami的区别,以及与select * from dba_users的区别
- Linux基础1——控制台、Who命令与Whoami命令比较
- I am who I am
- who命令的编写
- Who am I?
- who am i
- LeetCode 17: Letter Combinations of a Phone Number
- arm-linux-gcc
- Windows和Linux下的Sleep()函数小结
- 黑马程序员————高新技术————类加载器
- POJ-3345 Bribing FIPA(Tree dp + 背包)
- who命令的编写——登录与注销__who, whoami, who am i
- hdu 3486 Interviewe 二分+RMQ
- ZOJ Problem Set - 1061
- 链表操作(从尾到头打印链表,使用栈结构实现)
- Android 可执行文件
- Demo-简单使用libcurl静态库访问网址
- 堆排序中--建堆的算法复杂度分析O(n)
- [安卓破解]听网页浏览器,无需注册即可语音朗读
- 第一个mapreduce