who 命令的实现
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 命令的实现的更多相关文章
- Cmder--Windows下命令行利器
cmder cmder是一个增强型命令行工具,不仅可以使用windows下的所有命令,更爽的是可以使用linux的命令,shell命令. 安装包 安装包链接 下载后,直接解压即用. 修改命令提示符λ为 ...
- 【每日一linux命令4】常用参数:
下面所列的是常见的参数(选项)义: --help,-h 显示帮助信息 --version,-V ...
- .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门
2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布,社区里涌现了很多文章,我也计划写个系列文章,原因是.NET Core的入门门槛相当高, ...
- MVVM模式解析和在WPF中的实现(三)命令绑定
MVVM模式解析和在WPF中的实现(三) 命令绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- SQLServer执行命令出现“目录无效的提示”
异常处理汇总-数据库系列 http://www.cnblogs.com/dunitian/p/4522990.html 一般都是清理垃圾清理过头了,把不该删的目录删了 网上说法: 问题描述: 1.s ...
- SQLServer文件收缩-图形化+命令
汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 收缩前 图形化演示: 不仅仅可以收缩日记文件,数据库文件也是可以收缩的,只不过日记收缩比 ...
- 让 windows 下的命令行程序 cmd.exe 用起来更顺手
在 Windows 下使用 Larave 框架做开发,从 Composer 到 artisan 总是避免不了和 cmd.exe 打交道,系统默认的命令行界面却是不怎么好看,且每行显示的字符数是做了限制 ...
- [版本控制之道] Git 常用的命令总结(欢迎收藏备用)
坚持每天学习,坚持每天复习,技术永远学不完,自己永远要前进 总结日常开发生产中常用的Git版本控制命令 ------------------------------main-------------- ...
- git 命令
切换仓库地址: git remote set-url origin xxx.git切换分支:git checkout name撤销修改:git checkout -- file删除文件:git rm ...
- svn 常用命令总结
svn 命令篇 svn pget svn:ignore // 查看忽略项 svn commit -m "提交说明" // 提交修改 svn up(update) // 获取最新版本 ...
随机推荐
- [bzoj4971]记忆中的背包
为了使得方案的形式较为单一,不妨强制物品体积为1或$\ge \lceil\frac{w}{2}\rceil$,那么假设最终有$x$个1且$\ge \lceil\frac{w}{2}\rceil$的物品 ...
- scanner对象,顺序及选择结构
scanner对象 基本语法: Scanner s = new Scanner(System.in) 使用next()和nextLine()读取前,使用hasNext()和hasNextLine()来 ...
- Educational Codeforces Round 94 题解
我竟然比到了全场的 rk 14,incredible! A 大水题,直接输出 \(n\) 遍 \(s_n\) 即可. B 分类讨论题,放在 B 题可能难度有点大了. 直接暴力枚举你拿了多少个宝剑,然后 ...
- 【R】write.table输出数据带有行名?
目录 问题 解决一 解决二 问题 这个问题应该很常见吧.R中输出数据框时,想要把行名和列名都输出.如果直接输出的话,输出的结果列名会往前移动一位,这显然不是我们想要的. 直接上例子: > a = ...
- 巩固javawbe第二天
巩固内容: <!DOCTYPE> 声明 <!DOCTYPE>声明有助于浏览器中正确显示网页. 网络上有很多不同的文件,如果能够正确声明HTML的版本,浏览器就能正确显示网页内容 ...
- Elasticsearch【基础入门】
目录 一.操作index 1.查看index 2.增加index 3.删除index 二.操作index 1.新增document 2.查询type 全部数据 3.查找指定 id 的 document ...
- Spark(十二)【SparkSql中数据读取和保存】
一. 读取和保存说明 SparkSQL提供了通用的保存数据和数据加载的方式,还提供了专用的方式 读取:通用和专用 保存 保存有四种模式: 默认: error : 输出目录存在就报错 append: 向 ...
- HDFS【Java API操作】
通过java的api对hdfs的资源进行操作 代码:上传.下载.删除.移动/修改.文件详情.判断目录or文件.IO流操作上传/下载 package com.atguigu.hdfsdemo; impo ...
- Angular 组件通信的三种方式
我们可以通过以下三种方式来实现: 传递一个组件的引用给另一个组件 通过子组件发送EventEmitter和父组件通信 通过serive通信 1. 传递一个组件的引用给另一个组件 Demo1 模板引用变 ...
- Shell脚本实现自动修改IP地址
作为一名Linux SA,日常运维中很多地方都会用到脚本,而服务器的ip一般采用静态ip或者MAC绑定,当然后者比较操作起来相对繁琐,而前者我们可以设置主机名.ip信息.网关等配置.修改成特定的主机名 ...