[20171101]linux peekfd的简单使用.txt
--//今天再解决一个问题时需要使用fuser命令,看看某个文件是那些进程打开,在执行时发现普通用户无法执行,我记忆里普通用户可以执
--//行的,我看了一台CentOS release 6.2 (Final)的机器,可以正常执行,我还发现在这台机器的/etc/peofile路径中包含如下脚本:
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
pathmunge /sbin after
fi
--//我使用另外一台Oracle Linux Server release 5.9机器:
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
fi
--//而fuser路径实际上是
# which fuser
/sbin/fuser
--//而无意中我发现centos 6.2的机器中还包含一个peekfd命令.
# rpm -qilf `which fuser`
Name : psmisc Relocations: (not relocatable)
Version : 22.6 Vendor: CentOS
Release : 15.el6_0.1 Build Date: Sat 25 Jun 2011 02:02:12 PM CST
Install Date: Tue 03 Jul 2012 12:11:22 PM CST Build Host: c6b5.bsys.dev.centos.org
Group : Applications/System Source RPM: psmisc-22.6-15.el6_0.1.src.rpm
Size : 222302 License: GPLv2+
Signature : RSA/8, Wed 06 Jul 2011 09:45:24 AM CST, Key ID 0946fca2c105b9de
Packager : CentOS BuildSystem <http://bugs.centos.org>
URL : http://sourceforge.net/projects/psmisc
Summary : Utilities for managing processes on your system
Description :
The psmisc package contains utilities for managing processes on your
system: pstree, killall and fuser. The pstree command displays a tree
structure of all of the running processes on your system. The killall
command sends a specified signal (SIGTERM if nothing is specified) to
processes identified by name. The fuser command identifies the PIDs
of processes that are using specified files or filesystems.
/sbin/fuser
/usr/bin/killall
/usr/bin/peekfd
/usr/bin/pstree
/usr/bin/pstree.x11
/usr/share/doc/psmisc-22.6
/usr/share/doc/psmisc-22.6/AUTHORS
/usr/share/doc/psmisc-22.6/COPYING
/usr/share/doc/psmisc-22.6/INSTALL
/usr/share/doc/psmisc-22.6/README
...
--//而Oracle Linux Server release 5.9的机器没有:
# rpm -qilf $(which fuser )
Name : psmisc Relocations: (not relocatable)
Version : 22.2 Vendor: Oracle America
Release : 11 Build Date: Wed 09 Jan 2013 03:29:45 PM CST
Install Date: Fri 29 Aug 2014 09:28:53 PM CST Build Host: ca-build56.us.oracle.com
Group : Applications/System Source RPM: psmisc-22.2-11.src.rpm
Size : 162941 License: BSD/GPL
Signature : DSA/SHA1, Fri 11 Jan 2013 05:08:40 AM CST, Key ID 66ced3de1e5e0159
URL : http://psmisc.sourceforge.net
Summary : Utilities for managing processes on your system.
Description :
The psmisc package contains utilities for managing processes on your
system: pstree, killall and fuser. The pstree command displays a tree
structure of all of the running processes on your system. The killall
command sends a specified signal (SIGTERM if nothing is specified) to
processes identified by name. The fuser command identifies the PIDs
of processes that are using specified files or filesystems.
/sbin/fuser
/usr/bin/killall
/usr/bin/pstree
/usr/bin/pstree.x11
/usr/share/locale/bg/LC_MESSAGES/psmisc.mo
--//好奇心决定学习一下peekfd命令. peek 偷看, 窥视的意思,fd 应该表示file descriptors.
# man peekfd
PEEKFD(1) User Commands PEEKFD(1)
NAME
peekfd - peek at file descriptors of running processes
SYNOPSIS
peekfd [-8,--eight-bit-clean] [-n,--no-headers] [-f,--follow] [-d,--duplicates-removed] [-V,--version] [-h,--help] pid [fd] [fd] ...
DESCRIPTION
peekfd attaches to a running process and intercepts all reads and writes to file descriptors. You can specify
the desired file descriptor numbers or dump all of them.
OPTIONS
-8 Do no post-processing on the bytes being read or written.
-n Do not display headers indicating the source of the bytes dumped.
-c Also dump the requested file descriptor activity in any new child processes that are created.
-d Remove duplicate read/writes from the output. If you're looking at a tty with echo, you might want this.
-v Display a version string.
-h Display a help message.
--//通过这个命令简单了解打开文件句柄的读写情况.简单测试一下:
--//我不想在生产系统做这些测试,我直接把/usr/bin/peekfd拷贝到我自己的测试环境.我觉得应该没有问题.
1.环境:
SCOTT@book> @ &r/ver1
PORT_STRING VERSION BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx 11.2.0.4.0 Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
SCOTT@book> @ &r/spid
SID SERIAL# SPID PID P_SERIAL# C50
---------- ---------- ------ ------- ---------- --------------------------------------------------
274 11 5019 21 6 alter system kill session '274,11' immediate;
2.测试:
--//session 1:
# peekfd 5019
--//session 2:
SCOTT@book> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
--//session 1:
# peekfd 5019
writing fd 10:
[02] [10] [06] [10] [17] [0e] 絆 [12] [9a] 瞅狈 [97] [94] 0鹃 [8e] 謝u [0b] [01] [10] [10] 1 [03] Q [01] [02] [02] [16] [06] [06] [06] DEPTNO [01] [01] [80] [0e] T [03] [01] [0e] [01] [05] [05] [05] DNAME [01] [01] [01] [80]
T [03] [01]
[01] [03] [03] [03] LOC [02] [07] [07] xu [0b] [01] [10] [10] [01] ?[1f]
[06] [01] [1a] [03] [01] [07] [1a] , [01] [03] [02] ?[0b]
ACCOUNTING [08] NEW YORK [08] [06]
V^ [17] [03] [05] [04] [01] [1c] [01] [01] [05] [0e] [03] [1e] [01] 6 [01] @?[0e] [0c] [01] & [06] [06] [01] [1a] [03] ?[07] [16] , [01] [03] [02] ?[15] [08] RESEARCH [06] DALLAS [07] [14] , [01] [03] [02] ?[1f] [05] SALES [07] CHICAGO [07] [18] , [01] [03] [02] ?
OPERATIONS [06] BOSTON [04] [01] [1d] [01] [04] { [05] [05] [03] [1f] [01] 6 [01] @?[0e] [0c] [19] ORA-01403: no data found
--//有点乱,很明显显示的以上输出的信息.
# ls -l /proc/5019/fd/10
l-wx------ 1 oracle oinstall 64 2017-11-01 15:17:37 /proc/5019/fd/10 -> pipe:[35968225]
--//继续测试:
SCOTT@book> alter system flush BUFFER_CACHE;
System altered.
SCOTT@book> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
writing fd 8:
<msg time="2017-11-01T15:19:39.091+08:00" org_id="oracle" comp_id="rdbms" client_id='' type='UNKNOWN' level='16'
host_id='gxqyydg4' host_addr='192.168.100.78' module='SQL*Plus'
pid='5134'>
ALTER SYSTEM: Flushing buffer cache
Wed Nov 01 15:19:39 2017
ALTER SYSTEM: Flushing buffer cache
writing fd 10:
?[06] [08] [06] [10] W^ [17] [03] [02] [04] [01] [1c] [01] [02] 1 [1e] [01] 6 [01] @?[0e] [0c] [02] [10] [06] [10] [17] [0e] 絆 [12] [9a] 瞅狈 [97] [94] 0??[8e] 謝u [0b] [01] [10] [10] 1 [03] Q [01] [02] [02] [16] [06] [06] [06] DEPTNO [01] [01] [80] [0e] T [03] [01] [0e] [01] [05] [05] [05] DNAME [01] [01] [01] [80]
T [03] [01]
[01] [03] [03] [03] LOC [02] [07] [07] xu [0b] [01] [10] [15] [06] [01] ?[1f]
[06] [01] [1a] [03] [01] [07] [1a] , [01] [03] [02] ?[0b]
ACCOUNTING [08] NEW YORK [08] [06] -W^ [17] [03] [03] [04] [01] [1e] [01] [01] [03] [03] [01] 6 [01] @?[0e] [0c] [01] & [06] [06] [01] [1a] [10] [03] ?[02] [01] [07] [16] , [01] [03] [02] ?[15] [08] RESEARCH [06] DALLAS [07] [14] , [01] [03] [02] ?[1f] [05] SALES [07] CHICAGO [07] [18] , [01] [03] [02] ?
OPERATIONS [06] BOSTON [04] [01] [1f] [01] [04] { [05] [03] [03] ! [01] 6 [01] @?[0e] [0c] [19] ORA-01403: no data found
--//没有看到对表空间数据文件user01.dbf的读写,因为我使用内存盘,不存在对该文件的读写.而且操作系统也缓存了该文件.
# cachestats /mnt/ramdisk/book/users01.dbf
/mnt/ramdisk/book/users01.dbf pages in cache: 32770/32770 (100.0%) [filesize=131080.0K, pagesize=4K]
# cachedel /mnt/ramdisk/book/users01.dbf
# cachestats /mnt/ramdisk/book/users01.dbf
/mnt/ramdisk/book/users01.dbf pages in cache: 32770/32770 (100.0%) [filesize=131080.0K, pagesize=4K]
--//可以发现即使使用cacheldel 清除该/mnt/ramdisk/book/users01.dbf文件环境,os依旧缓存该文件.
3.重新测试:
--//关闭数据库将改文件移动到本地磁盘,然后做一个链接欺骗oracle还在原来位置.
$ mv /mnt/ramdisk/book/users01.dbf /home/oracle/
$ ln -s /home/oracle/users01.dbf /mnt/ramdisk/book/
$ ls -l /mnt/ramdisk/book/users01.dbf
lrwxrwxrwx 1 oracle oinstall 24 2017-11-01 15:34:03 /mnt/ramdisk/book/users01.dbf -> /home/oracle/users01.dbf
# cachedel /mnt/ramdisk/book/users01.dbf
# cachestats /mnt/ramdisk/book/users01.dbf
/mnt/ramdisk/book/users01.dbf pages in cache: 0/32770 (0.0%) [filesize=131080.0K, pagesize=4K]
--//清除os缓存.
--//重复前面的测试:
SYS@book> startup
ORACLE instance started.
Total System Global Area 634732544 bytes
Fixed Size 2255792 bytes
Variable Size 197133392 bytes
Database Buffers 427819008 bytes
Redo Buffers 7524352 bytes
Database mounted.
Database opened.
SYS@book> @ &r/spid
SID SERIAL# SPID PID P_SERIAL# C50
---------- ---------- ------ ------- ---------- --------------------------------------------------
274 3 5371 21 2 alter system kill session '274,3' immediate;
SYS@book> select * from scott.dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
--//很奇怪看到的还是writing fd 10,换一个方式:
# ps -ef | grep ora_ck
oracle 5353 1 0 15:34 ? 00:00:00 ora_ckpt_book
root 5510 5031 0 15:47 pts/9 00:00:00 grep ora_ck
SYS@book> alter system checkpoint ;
System altered.
# peekfd 5353
reading fd 14:
rootfs / rootfs rw 0 0
/dev/root / ext3 rw,relatime,errors=continue,user_xattr,acl,barrier=0,data=ordered 0 0
/dev /dev tmpfs rw,relatime,mode=755 0 0
/proc /proc proc rw,relatime 0 0
/sys /sys sysfs rw,relatime 0 0
/proc/bus/usb /proc/bus/usb usbfs rw,relatime 0 0
devpts /dev/pts devpts rw,relatime,gid=5,mode=620,ptmxmode=000 0 0
/dev/cciss/c0d0p6 /u01 ext3 rw,relatime,errors=continue,user_xattr,acl,barrier=0,data=ordered 0 0
/dev/cciss/c0d0p1 /boot ext3 rw,relatime,errors=continue,user_xattr,acl,barrier=0,data=ordered 0 0
tmpfs /dev/shm tmpfs rw,relatime 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
/etc/auto.misc /misc autofs rw,relatime,fd=7,pgrp=4371,timeout=300,minproto=5,maxproto=5,indirect 0 0
-hosts /net autofs rw,relatime,fd=13,pgrp=4371,timeout=300,minproto=5,maxproto=5,indirect 0 0
tmpfs /mnt/ramdisk tmpfs rw,relatime,size=8388608k 0 0
none /sys/kernel/debug debugfs rw,relatime 0 0
nfsd /proc/fs/nfsd nfsd rw,relatime 0 0
/de
writing fd 14:
<msg time="2017-11-01T15:47:57.232+08:00" org_id="oracle" comp_id="rdbms" client_id='' type='UNKNOWN' level='16'
host_id='gxqyydg4' host_addr='192.168.100.78' module=''
pid='5353'>
Beginning global checkpoint up to RBA [0x2bc.649e.10], SCN: 13276962656
Wed Nov 01 15:47:57 2017
Beginning global checkpoint up to RBA [0x2bc.649e.10], SCN: 13276962656
<msg time="2017-11-01T15:47:57.241+08:00" org_id="oracle" comp_id="rdbms" client_id='' type='UNKNOWN' level='16'
host_id='gxqyydg4' host_addr='192.168.100.78' module=''
pid='5353'>
Completed checkpoint up to RBA [0x2bc.649e.10], SCN: 13276962656
Completed checkpoint up to RBA [0x2bc.649e.10], SCN: 13276962656
# ls -l /proc/5353/fd/14
ls: /proc/5353/fd/14: No such file or directory
--//很奇怪看不到对数据文件的读写.放弃.
--//至少目前可以看用户执行命令时的输出.放弃!!顺便看看汉字的输出问题.
SYS@book> select '文化123456' c20 from dual ;
C20
--------------------
文化123456
[01] y [06] [10] [17] ? [99] 睨 [87] / [15] X?[1c] R; [8d] B [03] xu [0b] [01] [11] [01] &
[01] Q [01] ` [80]
T [03] [01] [08] [01] [03] [03] [03] C20 [07] [07] xu [0b] [01] [11] [01] 7?[1f] [02] [02] [06] [01] " [01] [01] 鹻完 [07]
文化123456 [08] [06] 雊^ [17] [03] [19] [04] [01] [17] [01] [01] [19] [1d] [03] [1a] [01] 6 [01] @?[0e] [0c] ?[06] [04] [01] [18] [01] [01] { [05] [19] [03] [1b] [01] 6 [01] @?[0e] [0c] [19] ORA-01403: no data found
--//汉字显示正常.