who 命令显示 当前已经登录的用户

查看连接帮助: man who 

who(1) 表示who的小结编号。

NAME 包含命令的名字以及对这个命令的简短说明

SYNOPSYS 给出命令的用法说明,命令格式,参数列表等

DESCRIPTION 关于命令功能的详细阐述,描述命令的所有功能,命令的权威解释。

AUTHOR  命令的作者

REPORTING BUGS 命令的残留BUG

COPYRIGHT 版权

SEE ALSO 参阅,这个命令相关的其他主题。

man who :

DESCRIPTION

  If FILE is not specified, use /var/run/utmp. /var/log/wtmp as FILE is common. If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.

可以看到已登录信息是存放在 /var/run/utmp 文件中的

联机搜素帮助: man -k utmp

  endutent (3) - access utmp file entries
  endutxent (3) - access utmp file entries
  getutent (3) - access utmp file entries
  getutent_r (3) - access utmp file entries
  getutid (3) - access utmp file entries
  getutid_r (3) - access utmp file entries
  getutline (3) - access utmp file entries
  getutline_r (3) - access utmp file entries
  getutmp (3) - copy utmp structure to utmpx, and vice versa
  getutmpx (3) - copy utmp structure to utmpx, and vice versa
  getutxent (3) - access utmp file entries
  getutxid (3) - access utmp file entries
  getutxline (3) - access utmp file entries
  login (3) - write utmp and wtmp entries
  logout (3) - write utmp and wtmp entries
  pututline (3) - access utmp file entries
  pututxline (3) - access utmp file entries
  sessreg (1) - manage utmp/wtmp entries for non-init clients
  setutent (3) - access utmp file entries
  setutxent (3) - access utmp file entries
  utmp (5) - login records
  utmpname (3) - access utmp file entries
  utmpx (5) - login records
  utmpxname (3) - access utmp file entries

可以看到有关 用户登录的文件存放在 utmp (5) - login records 中。

联机搜索:man 5 utmp

NAME
  utmp, wtmp - login records

SYNOPSIS
  #include <utmp.h>

DESCRIPTION
  The utmp file allows one to discover information about who is currently using the system. There may be more users currently using the system,
  because not all programs use utmp logging.

可以看到命令 who 会去读 utmp 这个文件,utmp 中保存的是 utmp 这个数据结构,utmp 结构类型在 utmp.h 定义。

需要去 /usr/include/ 目录下面寻找相应的头文件 。

可以直接 more /usr/include/utmp.h  没有看到 utmp 结构的定义

man 5 utmp 可以得到:

struct utmp {
  short ut_type; /* Type of record */
  pid_t ut_pid; /* PID of login process */
  char ut_line[UT_LINESIZE]; /* Device name of tty - "/dev/" */
  char ut_id[4]; /* Terminal name suffix, or inittab(5) ID */
  char ut_user[UT_NAMESIZE]; /* Username */
  char ut_host[UT_HOSTSIZE]; /* Hostname for remote login, or kernel version for run-level messages */
  struct exit_status ut_exit; /* Exit status of a process marked as DEAD_PROCESS; not used by Linux init(8) */
  /* The ut_session and ut_tv fields must be the same size when
  compiled 32- and 64-bit. This allows data files and shared
  memory to be shared between 32- and 64-bit applications. */
#if __WORDSIZE == 64 && defined __WORDSIZE_COMPAT32
  int32_t ut_session; /* Session ID (getsid(2)),
  used for windowing */
  struct {
    int32_t tv_sec; /* Seconds */
    int32_t tv_usec; /* Microseconds */
  } ut_tv; /* Time entry was made */
#else
  long ut_session; /* Session ID */
  struct timeval ut_tv; /* Time entry was made */
#endif

  int32_t ut_addr_v6[4]; /* Internet address of remote
  host; IPv4 address uses
  just ut_addr_v6[0] */
  char __unused[20]; /* Reserved for future use */
};

/* Backwards compatibility hacks */
#define ut_name ut_user
#ifndef _NO_UT_TIME
#define ut_time ut_tv.tv_sec
#endif
#define ut_xtime ut_tv.tv_sec
#define ut_addr ut_addr_v6[0]

可以看到有 

ut_type 用户类型

ut_pid 用户PID

ut_line[UT_LINESIZE] 设备名 用户终端类型

ut_id[4] 用户ID

ut_user[UT_NAMESIZE] 登录名

ut_host[UT_HOSTSIZE] 终端名

ut_time 用户登录时间

who 的工作过程:

1.从数据库中读取数据结构

2.将结构中的信息以合适的形式读取显示出来。

如何从文件中读取数据结构

搜索联机帮助:man -k file | grep read

__freadable (3) - interfaces to stdio FILE structure
__freading (3) - interfaces to stdio FILE structure
_llseek (2) - reposition read/write file offset
eventfd_read (3) - create a file descriptor for event notification
fc-cat (1) - read font information cache files
fgetwc (3) - read a wide character from a FILE stream
fgetws (3) - read a wide-character string from a FILE stream
fts_read (3) - traverse a file hierarchy
getwc (3) - read a wide character from a FILE stream
llseek (2) - reposition read/write file offset
lseek (2) - reposition read/write file offset
lseek64 (3) - reposition 64-bit read/write file offset
pppdump (8) - convert PPP record file to readable format
pread (2) - read from or write to a file descriptor at a give...
pread64 (2) - read from or write to a file descriptor at a give...
pwrite (2) - read from or write to a file descriptor at a give...
pwrite64 (2) - read from or write to a file descriptor at a give...
read (2) - read from a file descriptor
readahead (2) - perform file readahead into page cache
readelf (1) - Displays information about ELF files.
readlinkat (2) - read value of a symbolic link relative to a direc...
readprofile (1) - a tool to read kernel profiling information
tee (1) - read from standard input and write to standard ou...
ureadahead (8) - Read files in advance during boot

可以看到最有可能的是 read (2) - read from a file descriptor

man 2 read

SYNOPSIS
  #include <unistd.h>

  ssize_t read(int fd, void *buf, size_t count);

DESCRIPTION
  read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.

  If count is zero, read() returns zero and has no other results. If count is greater than SSIZE_MAX, the result is unspecified.

可以看到这个 read 的系统调用命令从文件描述符 fd 指向的文件中将数据读入一个缓冲区*buf,指定读取的字节数 count 


如何得到这个文件描述符 fd 呢 在联机帮助中有

SEE ALSO
  close(2), fcntl(2), ioctl(2), lseek(2), open(2), pread(2), read‐
  dir(2), readlink(2), readv(2), select(2), write(2), fread(3)

man 2 open 

SYNOPSIS
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <fcntl.h>

  int open(const char *pathname, int flags);
  int open(const char *pathname, int flags, mode_t mode);

  int creat(const char *pathname, mode_t mode);

DESCRIPTION
  Given a pathname for a file, open() returns a file descriptor, a
  small, non-negative integer for use in subsequent system calls
  (read(2), write(2), lseek(2), fcntl(2), etc.). The file descrip‐
  tor returned by a successful call will be the lowest-numbered file
  descriptor not currently open for the process.

可以看到:Given a pathname for a file, open() returns a file descriptor 

who 命令的实现的更多相关文章

  1. Cmder--Windows下命令行利器

    cmder cmder是一个增强型命令行工具,不仅可以使用windows下的所有命令,更爽的是可以使用linux的命令,shell命令. 安装包 安装包链接 下载后,直接解压即用. 修改命令提示符λ为 ...

  2. 【每日一linux命令4】常用参数:

     下面所列的是常见的参数(选项)义: --help,-h                              显示帮助信息 --version,-V                        ...

  3. .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门

    2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布,社区里涌现了很多文章,我也计划写个系列文章,原因是.NET Core的入门门槛相当高, ...

  4. MVVM模式解析和在WPF中的实现(三)命令绑定

    MVVM模式解析和在WPF中的实现(三) 命令绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  5. SQLServer执行命令出现“目录无效的提示”

    异常处理汇总-数据库系列  http://www.cnblogs.com/dunitian/p/4522990.html 一般都是清理垃圾清理过头了,把不该删的目录删了 网上说法: 问题描述: 1.s ...

  6. SQLServer文件收缩-图形化+命令

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 收缩前 图形化演示: 不仅仅可以收缩日记文件,数据库文件也是可以收缩的,只不过日记收缩比 ...

  7. 让 windows 下的命令行程序 cmd.exe 用起来更顺手

    在 Windows 下使用 Larave 框架做开发,从 Composer 到 artisan 总是避免不了和 cmd.exe 打交道,系统默认的命令行界面却是不怎么好看,且每行显示的字符数是做了限制 ...

  8. [版本控制之道] Git 常用的命令总结(欢迎收藏备用)

    坚持每天学习,坚持每天复习,技术永远学不完,自己永远要前进 总结日常开发生产中常用的Git版本控制命令 ------------------------------main-------------- ...

  9. git 命令

    切换仓库地址: git remote set-url origin xxx.git切换分支:git checkout name撤销修改:git checkout -- file删除文件:git rm  ...

  10. svn 常用命令总结

    svn 命令篇 svn pget svn:ignore // 查看忽略项 svn commit -m "提交说明" // 提交修改 svn up(update) // 获取最新版本 ...

随机推荐

  1. [loj2339]通道

    类似于[loj2553] 对第一棵树边分治,对第二棵树建立虚树,并根据直径合并的性质来处理第三棵树(另外在第三棵树中计算距离需要使用dfs序+ST表做到$o(1)$优化) 总复杂度为$o(n\log^ ...

  2. [bzoj2789]Letters

    考虑A中第i次出现的j字符,最终位置一定是在B中第i次出现的j字符的位置,然后即求逆序对数量,cdq/线段树即可 1 #include<bits/stdc++.h> 2 using nam ...

  3. ICCV2021 | Swin Transformer: 使用移位窗口的分层视觉Transformer

    ​  前言  本文解读的论文是ICCV2021中的最佳论文,在短短几个月内,google scholar上有388引用次数,github上有6.1k star. 本文来自公众号CV技术指南的论文分享系 ...

  4. Mysql 预处理 PREPARE以及预处理的好处

    Mysql 预处理 PREPARE以及预处理的好处 Mysql手册 预处理记载: 预制语句的SQL语法在以下情况下使用:   · 在编代码前,您想要测试预制语句在您的应用程序中运行得如何.或者也许一个 ...

  5. xshell的快捷复制粘贴设置

    今天试着用xshell连接Linux,运行一些命令的时候想快点复制粘贴实现效率,却发现还要右键选择复制,再右键选择粘贴,很是麻烦. 看了一下xshell的设置,其实可以自己设置成快捷方式 以xshel ...

  6. 【Python小试】使用列表解析式简化代码

    列表解析式的好处: 代码简洁 可读性强 运行快 示例 来自<Python编程>中的一个例子:同时投掷两颗面数不同的骰子(如一个6面的D6和一个10面的D10)n次,统计两个骰子点数之和,并 ...

  7. R绘图布局包 customLayout

    今天介绍一个R画图布局的包,地址如下: https://github.com/zzawadz/customLayout https://www.customlayout.zstat.pl/index. ...

  8. Notepad++—显示代码对齐是使用了制表符还是空格

    使用Notepad++打开脚本,勾选"显示空格与制表符",此时你会看到代码对齐使用了制表符与空格 右箭头:TAB:空格:点: 参考:https://www.cnblogs.com/ ...

  9. Oracle-连接多个字段

    0.函数concat(A,B)作用:链接字符串 区别: Oracle中:CONCAT()只允许两个参数:(貌似可以内嵌) Mysql中:CONCAT()可以连接多个参数: 1.用||符号进行拼接 例子 ...

  10. 记一次 .NET 某化妆品 webapi 卡死分析

    一:背景 1. 讲故事 10月份星球里的一位老朋友找到我,说他们公司的程序在一个网红直播带货下给弄得无响应了,无响应期间有大量的 RabbitMQ 超时,寻求如何找到根源,聊天截图我就不发了. 既然无 ...