who命令的编写——登录与注销__who, whoami, who am i
摘要:who是一个linux下重要的系统内建指令,编写who命令,需要了解相关的数据结构struct utmp。同时进一步熟悉结构化数据的读取和访问方法。另外总结了系统调用相关的知识。
本文来源:http://blog.csdn.net/trochiluses/article/details/10825061
#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获得,作为命令行参数
4.结构化提取utmp数据:
4.1.getutent函数头文件:#include <utmp.h>
定义函数:struct utmp *getutent(void);
函数说明:getutent()用来从utmp 文件(/var/run/utmp)中读取一项登录数据, 该数据以utmp 结构返回. 第一次调用时会取得第一位用户数据, 之后每调用一次就会返回下一项数据, 直到已无任何数据时返回NULL。
4.2.getutmp函数 #define _GNU_SOURCE /* See feature_test_macros(7) */ #include <utmpx.h>
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.
显示当前用户和他们正在进行的操作
$ 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
如果系统忽然掉电,此时utmp文件里面的内容还没有来得及更改,所以下次重启的时候,init进程将会清除utmp的所有记录为空(设定为DEADPROCESS),大致如下:
init——login——getty——utmp