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


先看看这三个命令的输出信息:
 
[rocrocket@rocrocket ~]$ whoami
rocrocket
[rocrocket@rocrocket ~]$ who am i
rocrocket pts/3        2008-12-30 13:17 (:0.0)
[rocrocket@rocrocket ~]$ who
rocrocket :0           2008-12-30 09:54
rocrocket pts/0        2008-12-30 09:55 (:0.0)
rocrocket pts/1        2008-12-30 09:57 (:0.0)
rocrocket pts/3        2008-12-30 13:17 (:0.0)
当我用sudo su(或者sudo su -)更换到root用户之后,你再看看:
  www.2cto.com  
[rocrocket@rocrocket ~]$ sudo su
[root@rocrocket rocrocket]# whoami
root
[root@rocrocket rocrocket]# who am i
rocrocket pts/3        2008-12-30 13:17 (:0.0)
[root@rocrocket rocrocket]# who
rocrocket :0           2008-12-30 09:54
rocrocket pts/0        2008-12-30 09:55 (:0.0)
rocrocket pts/1        2008-12-30 09:57 (:0.0)
rocrocket pts/3        2008-12-30 13:17 (:0.0)
 
看出区别来了吧,whoami显示的是当前“操作用户”的用户名,而who am i显示的是“登录用户”的用户名。

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.


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



原创粉丝点击