1 #include <fcntl.h> 2 #include <string.h> 3 #include <assert.h> 4 #include <pthread.h> 5 #include <unistd.h> 6 #include <stdlib.h> 7 #include <sys/stat.h> 8 #include <sys/time.h> 9 #include <errno.h> 10 #include <stdio.h> 11 #include <linux/reboot.h> 12 #include <dirent.h> 13 #include <sys/mount.h> 14 15 //def for bool 16 #ifndef bool //8bit 17 #define bool unsigned char 18 #endif 19 #ifndef NULL 20 #define NULL ((void*)0) 21 #endif 22 #ifndef false 23 #define false 0 24 #endif 25 #ifndef true 26 #define true 1 27 #endif 28 //end of def for bool 29 30 /* 31 * check file is exist!! 32 */ 33 static bool file_exist(const char* path) 34 { 35 36 if(!path) return false; 37 if(access(path,F_OK) == 0) 38 { 39 return true;//file exist 40 } 41 printf("[Info] file:%s is not exist!\n",path); 42 return false ; //file is not exist 43 44 } 45 static int umount_disk_partition(const char* disk) 46 { 47 if(!disk) 48 return 0; 49 50 #define MOUNTS_FILE_SIZE 2048 51 const char* mounts = "/proc/self/mounts"; 52 const char* delims = " "; 53 54 bool bug = true; 55 char buf[MOUNTS_FILE_SIZE] = {0}; 56 int fd,ret = 0; 57 int row = 0; 58 char *start = NULL,*end = NULL,*lineend = NULL,*cp = NULL,*result = NULL; 59 60 if((fd = open(mounts,O_RDONLY)) < 0) 61 { 62 perror("open error:"); 63 return -1; 64 } 65 if(lseek(fd,0,SEEK_SET) == (off_t)-1) 66 { 67 printf("lseek: %s (%d)\n", strerror(errno), errno); 68 close(fd); 69 return -1; 70 } 71 if((ret = read(fd,buf,sizeof(buf)-1)) < 0 ) 72 { 73 perror("read error:"); 74 close(fd); 75 return -1; 76 } 77 close(fd); 78 end = buf + ret; 79 *end = 0; 80 start = buf; 81 while(start < end) 82 { 83 lineend = strchr(start,'\0'); 84 if(!lineend) 85 break; 86 *lineend = 0; 87 88 cp = strstr(start,disk); 89 if(!cp) 90 { 91 start = lineend + 1; 92 continue; 93 } 94 95 result = strtok(cp,delims); 96 row = 0; 97 while(result) 98 { 99 if(row == 1) 100 { 101 bug && printf("mount points:%s \n",result); 102 umount2(result,MNT_FORCE); 103 break; 104 } 105 row++; 106 result = strtok(NULL,delims); 107 } 108 start = cp + 1; 109 } 110 111 112 return 0; 113 } 114 main() 115 { 116 umount_disk_partition("/dev/mtdblock"); 117 }