求如何编写强制umount磁盘挂载的程序?
求如何编写强制umount磁盘挂载的程序?我这里有个soleris下的程序,参考,能把它在linux下编译执行就可以了
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>//for sleep
#include <signal.h>
int VolumeUMount (const char *szDev, const char *szPoint);
int KillMount(const char *szPoint);
main(int argc,char *argv[])
{
if(argc!=3)
{
printf("Usage:UMOUNT devicename mountpoint\n");
exit(1);
}
if(access(argv[1],F_OK)!=0)
{
printf("%s does not exist!\n",argv[1]);
return 0;
}
if(access(argv[2],F_OK)!=0)
{
printf("%s does not exist!\n",argv[2]);
return 0;
}
printf("return:%d\n",VolumeUMount(argv[1],argv[2]));
}
int VolumeUMount (const char *szDev, const char *szPoint)
{
char buf[256];
sprintf (buf, "mount | grep %s >/dev/null 2>&1",szDev);
if(system(buf)!=0)
{
printf("%s is not mounted!\n",szDev);
return 0;
}
else
KillMount(szPoint);
sprintf (buf,"fuser -ck %s >/dev/null 2>&1",szPoint);
system(buf);
sleep(2);
sprintf(buf,"umount %s >/dev/null 2>&1",szDev);
if(system(buf)==0)
return 0;
return 1;
}
int KillMount(const char *szPoint)
{
char buf[256];
char szFile[64],*cp;
strcpy(szFile,"KillMount.tmp");
sprintf(buf, "ps -ef | /usr/bin/grep %s > %s 2>/dev/null",szPoint,szFile);
if(system(buf)!=0)
{
printf("ps -ef ERROR\n");
return -1;
}
FILE *fp=fopen(szFile,"r");
if(fp==NULL)
return -1;
int nPid=0;
int fKilled=0;
while(fgets(buf,sizeof(buf),fp)!=NULL)
{
if(strstr(buf,"grep")!=NULL)
continue;
strtok(buf," ");
cp=strtok(NULL," ");
nPid=atoi(cp);
if(nPid==getpid())
continue;
if(nPid>0)
{
kill(nPid,SIGKILL);
sleep(2);
fKilled=1;
}
}
fclose(fp);
unlink(szFile);
return fKilled;
}
放到编程孵化区吧
另外把上面的代码保存成*.c
然后cc *.c生成可执行文件
执行他就可以了 谢谢,真没想到就这么简单
页:
[1]