利用/proc/mounts检查已经被系统挂载的设备

介绍/proc/mounts

如何利用/proc/mounts知道已经挂载上的设备呢,我们先来看看/proc/mounts都有啥东西



解释一下,第一列是设备路径,比如说/dev/sda1  第二列是挂载点(即设备挂载到的目录)  第三列是以什么文件系统挂载


2 编代码读取前3列


#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int read_proc_mounts()
{
	FILE *fp = NULL;
	int i = 0;
	int nSscanfNum = 0;
	char chDevicePath[255] = {0};
	char chMountPath[255] = {0};
	char chFsTypeName[255]= {0};
	char chBuffer[1024] = {0};
	
	fp = fopen("/proc/mounts","r");

	if (NULL == fp)
	{
		printf("\n read  /proc/mounts  error! \n");
		return -1;
	}
	while(1)
	{
		memset(chBuffer,0,sizeof(chBuffer));
		if(NULL == fgets(chBuffer,sizeof(chBuffer),fp))
		{
			break;
		}
		memset(chDevicePath,0,sizeof(chDevicePath));
		memset(chMountPath,0,sizeof(chMountPath));
		memset(chFsTypeName,0,sizeof(chFsTypeName));
		
		nSscanfNum = sscanf(chBuffer ,"%[^' '] %[^' '] %[^' ']",chDevicePath,chMountPath,chFsTypeName );
		if(3 != nSscanfNum) 
		{
			continue;
		}
	
		printf("\nchDevicePath[%s],chMountPath[%s],chFstypeName[%s]\n",chDevicePath,chMountPath,chFsTypeName);

	}
	
	fclose(fp);
	
	return 0;
}


int main()
{
     read_proc_mounts();
     return 0;
}




3.运行结果

-bash-3.2$ 
-bash-3.2$ gcc test_proc_mounts.c 
-bash-3.2$ 
-bash-3.2$ 
-bash-3.2$ ./a.out 


chDevicePath[rootfs],chMountPath[/],chFstypeName[rootfs]


chDevicePath[/dev/root],chMountPath[/],chFstypeName[ext3]


chDevicePath[/dev],chMountPath[/dev],chFstypeName[tmpfs]


chDevicePath[/proc],chMountPath[/proc],chFstypeName[proc]


chDevicePath[/sys],chMountPath[/sys],chFstypeName[sysfs]


chDevicePath[/proc/bus/usb],chMountPath[/proc/bus/usb],chFstypeName[usbfs]


chDevicePath[devpts],chMountPath[/dev/pts],chFstypeName[devpts]


chDevicePath[/dev/sda6],chMountPath[/home],chFstypeName[ext3]


chDevicePath[/dev/sda5],chMountPath[/var],chFstypeName[ext3]


chDevicePath[/dev/sda1],chMountPath[/boot],chFstypeName[ext3]


chDevicePath[tmpfs],chMountPath[/dev/shm],chFstypeName[tmpfs]


chDevicePath[/dev/sdb1],chMountPath[/homesec],chFstypeName[ext4]


chDevicePath[/dev/sda6],chMountPath[/home_mount],chFstypeName[ext3]


chDevicePath[/dev/sdb1],chMountPath[/homesec_mount],chFstypeName[ext4]


chDevicePath[none],chMountPath[/proc/sys/fs/binfmt_misc],chFstypeName[binfmt_misc]


chDevicePath[sunrpc],chMountPath[/var/lib/nfs/rpc_pipefs],chFstypeName[rpc_pipefs]


chDevicePath[nfsd],chMountPath[/proc/fs/nfsd],chFstypeName[nfsd]


chDevicePath[/etc/auto.misc],chMountPath[/misc],chFstypeName[autofs]


chDevicePath[-hosts],chMountPath[/net],chFstypeName[autofs]


chDevicePath[/root],chMountPath[/root],chFstypeName[esn_cfs]


chDevicePath[/home],chMountPath[/home],chFstypeName[esn_cfs]


chDevicePath[/homesec],chMountPath[/homesec],chFstypeName[esn_cfs]
-bash-3.2$ 

   





评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值