From: John Hodge Date: Sat, 14 Jul 2012 05:09:41 +0000 (+0800) Subject: DiskTool - Adding cleanup X-Git-Tag: rel0.15~611^2~41^2 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=93afaf7e85ef0eed813cd05b31f516a50f4b8f99;p=tpg%2Facess2.git DiskTool - Adding cleanup --- diff --git a/Tools/DiskTool/src/actions.c b/Tools/DiskTool/src/actions.c index 43748d8f..377608de 100644 --- a/Tools/DiskTool/src/actions.c +++ b/Tools/DiskTool/src/actions.c @@ -12,18 +12,22 @@ // === IMPORTS === extern int NativeFS_Install(char **Arguments); +extern int LVM_Cleanup(void); // === PROTOTYPES === void DiskTool_Initialise(void) __attribute__((constructor(101))); +void DiskTool_Cleanup(void); int DiskTool_int_TranslateOpen(const char *File, int Mode); - int DiskTook_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest); - int DiskTook_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest); + int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest); + int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest); +void DiskTool_LVM_Cleanup(void *Handle); // === GLOBALS === tLVM_VolType gDiskTool_VolumeType = { .Name = "DiskTool", - .Read = DiskTook_LVM_Read, - .Write = DiskTook_LVM_Write + .Read = DiskTool_LVM_Read, + .Write = DiskTool_LVM_Write, + .Cleanup = DiskTool_LVM_Cleanup }; // === CODE === @@ -35,6 +39,29 @@ void DiskTool_Initialise(void) VFS_Mount("/", "/Native", "nativefs", ""); } +void DiskTool_Cleanup(void) +{ + int vfs_rv, lvm_rv; + int nNochangeLoop = 0; + // Unmount all + do { + lvm_rv = LVM_Cleanup(); + vfs_rv = VFS_UnmountAll(); + Log_Debug("DiskTool", "Unmounted %i volumes", vfs_rv); + if( vfs_rv == 0 && lvm_rv == 0 ) { + nNochangeLoop ++; + if(nNochangeLoop == 2) { + Log_Error("DiskTool", "Possible handle leak"); + break; + } + } + else { + nNochangeLoop = 0; + } + } + while( vfs_rv >= 0 || lvm_rv != 0 ); +} + int DiskTool_RegisterLVM(const char *Identifier, const char *Path) { int fd = DiskTool_int_TranslateOpen(Path, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE); @@ -60,6 +87,7 @@ int DiskTool_MountImage(const char *Identifier, const char *Path) DiskTool_int_TranslatePath(tpath, Path); // Call mount + VFS_MkDir(mountpoint); // TODO: Detect filesystem? return VFS_Mount(tpath, mountpoint, "fat", ""); } @@ -115,16 +143,20 @@ int DiskTool_ListDirectory(const char *Directory) return 0; } -int DiskTook_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest) +int DiskTool_LVM_Read(void *Handle, Uint64 Block, size_t BlockCount, void *Dest) { VFS_ReadAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest); return 0; } -int DiskTook_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest) +int DiskTool_LVM_Write(void *Handle, Uint64 Block, size_t BlockCount, const void *Dest) { VFS_WriteAt( (int)(tVAddr)Handle, Block*512, BlockCount*512, Dest); return 0; } +void DiskTool_LVM_Cleanup(void *Handle) +{ + VFS_Close( (int)(tVAddr)Handle ); +} // --- Internal helpers --- int DiskTool_int_TranslateOpen(const char *File, int Flags) diff --git a/Tools/DiskTool/src/include/disktool_common.h b/Tools/DiskTool/src/include/disktool_common.h index d98d4201..bd30b01d 100644 --- a/Tools/DiskTool/src/include/disktool_common.h +++ b/Tools/DiskTool/src/include/disktool_common.h @@ -8,6 +8,8 @@ #ifndef _INCLUDE__DISKTOOL_COMMON_H_ #define _INCLUDE__DISKTOOL_COMMON_H_ +extern void DiskTool_Cleanup(void); + extern int DiskTool_RegisterLVM(const char *Identifier, const char *Path); extern int DiskTool_MountImage(const char *Identifier, const char *Path); extern int DiskTool_Copy(const char *Source, const char *Destination); diff --git a/Tools/DiskTool/src/include/rwlock.h b/Tools/DiskTool/src/include/rwlock.h new file mode 100644 index 00000000..b9ff514a --- /dev/null +++ b/Tools/DiskTool/src/include/rwlock.h @@ -0,0 +1,22 @@ +/* + * Acess2 Disk Tool + */ +#ifndef _RWLOCK_H +#define _RWLOCK_H + +typedef char tRWLock; + +static inline int RWLock_AcquireRead(tRWLock *m) { + if(*m) Log_KernelPanic("---", "Double mutex lock"); + *m = 1; + return 0; +} +static inline int RWLock_AcquireWrite(tRWLock *m) { + if(*m) Log_KernelPanic("---", "Double mutex lock"); + *m = 1; + return 0; +} +static inline void RWLock_Release(tRWLock *m) { *m = 0; } + +#endif + diff --git a/Tools/DiskTool/src/main.c b/Tools/DiskTool/src/main.c index 58ce6d2e..823bb2b1 100644 --- a/Tools/DiskTool/src/main.c +++ b/Tools/DiskTool/src/main.c @@ -13,7 +13,7 @@ int main(int argc, char *argv[]) // Parse arguments for( int i = 1; i < argc; i ++ ) { - if( strcmp("--image", argv[i]) == 0 || strcmp("-i", argv[i]) == 0 ) { + if( strcmp("mount", argv[i]) == 0 || strcmp("-i", argv[i]) == 0 ) { // Mount an image if( argc - i < 3 ) { fprintf(stderr, "--image/-i takes 2 arguments (ident and path)\n"); @@ -22,7 +22,7 @@ int main(int argc, char *argv[]) if( DiskTool_MountImage(argv[i+1], argv[i+2]) ) { fprintf(stderr, "Unable to mount '%s' as '%s'\n", argv[i+2], argv[i+1]); - exit(-1); + break; } i += 2; @@ -38,7 +38,7 @@ int main(int argc, char *argv[]) if( DiskTool_RegisterLVM(argv[i+1], argv[i+2]) ) { fprintf(stderr, "Unable to register '%s' as LVM '%s'\n", argv[i+2], argv[i+1]); - exit(-1); + break; } i += 2; @@ -48,7 +48,7 @@ int main(int argc, char *argv[]) if( strcmp("ls", argv[i]) == 0 ) { if( argc - i < 2 ) { fprintf(stderr, "ls 1 argument (path)\n"); - exit(-1); + break; } DiskTool_ListDirectory(argv[i+1]); @@ -60,7 +60,7 @@ int main(int argc, char *argv[]) if( argc - i < 3 ) { fprintf(stderr, "cp takes 2 arguments (source and destination)\n"); - exit(-1); + break; } DiskTool_Copy(argv[i+1], argv[i+2]); @@ -70,8 +70,7 @@ int main(int argc, char *argv[]) } } - // Unmount all - // Clear LVM + DiskTool_Cleanup(); return 0; }