Browse Source

* Added cleanup of stale socket files (bug 17248)

git-svn-id: trunk@19632 -
michael 13 years ago
parent
commit
14edc4a496

+ 10 - 0
packages/fcl-process/src/simpleipc.pp

@@ -177,7 +177,11 @@ implementation
   as well as the communication class itself.
   as well as the communication class itself.
   
   
   This comes first, to allow the uses clause to be set.
   This comes first, to allow the uses clause to be set.
+  If the include file defines OSNEEDIPCINITDONE then the unit will
+  call IPCInit and IPCDone in the initialization/finalization code.
+  
   --------------------------------------------------------------------- }
   --------------------------------------------------------------------- }
+{$UNDEFINE OSNEEDIPCINITDONE}
 
 
 {$i simpleipc.inc}
 {$i simpleipc.inc}
 
 
@@ -482,5 +486,11 @@ begin
   SendStringMessage(MsgType, Format(Msg,Args));
   SendStringMessage(MsgType, Format(Msg,Args));
 end;
 end;
 
 
+{$IFDEF OSNEEDIPCINITDONE}
+initialization
+  IPCInit;
+finalization
+  IPCDone;
+{$ENDIF}  
 end.
 end.
 
 

+ 52 - 0
packages/fcl-process/src/unix/simpleipc.inc

@@ -24,7 +24,57 @@ uses sysutils, classes, simpleipc, baseunix;
 {$else}
 {$else}
 
 
 uses baseunix;
 uses baseunix;
+{$DEFINE OSNEEDIPCINITDONE}
 
 
+Var
+  SocketFiles : TStringList;
+
+Procedure IPCInit;
+
+begin
+end;
+
+Procedure IPCDone;
+
+Var
+  I : integer;
+  
+begin
+  if Assigned(SocketFiles) then
+    try
+      For I:=0 to SocketFiles.Count-1 do
+        DeleteFile(SocketFiles[i]);
+    finally  
+      FreeAndNil(SocketFiles);  
+    end;  
+end;
+
+
+Procedure RegisterSocketFile(Const AFileName : String);
+
+begin
+  If Not Assigned(SocketFiles) then
+    begin
+    SocketFiles:=TStringList.Create;
+    SocketFiles.Sorted:=True;
+    end;
+  SocketFiles.Add(AFileName);  
+end;
+
+Procedure UnRegisterSocketFile(Const AFileName : String);
+
+Var
+  I : Integer;
+begin
+  If Assigned(SocketFiles) then
+    begin
+    I:=SocketFiles.IndexOf(AFileName);  
+    If (I<>-1) then
+      SocketFiles.Delete(I);
+    If (SocketFiles.Count=0) then
+      FreeAndNil(SocketFiles);
+    end;
+end;
 {$endif}
 {$endif}
 
 
 
 
@@ -156,10 +206,12 @@ begin
     If (fpmkFifo(FFileName,438)<>0) then
     If (fpmkFifo(FFileName,438)<>0) then
       DoError(SErrFailedToCreatePipe,[FFileName]);
       DoError(SErrFailedToCreatePipe,[FFileName]);
   FStream:=TFileStream.Create(FFileName,fmOpenReadWrite+fmShareDenyNone,Rights[Owner.Global]);
   FStream:=TFileStream.Create(FFileName,fmOpenReadWrite+fmShareDenyNone,Rights[Owner.Global]);
+  RegisterSocketFile(FFileName);
 end;
 end;
 
 
 procedure TPipeServerComm.StopServer;
 procedure TPipeServerComm.StopServer;
 begin
 begin
+  UnregisterSocketFile(FFileName);
   FreeAndNil(FStream);
   FreeAndNil(FStream);
   if Not DeleteFile(FFileName) then
   if Not DeleteFile(FFileName) then
     DoError(SErrFailedtoRemovePipe,[FFileName]);
     DoError(SErrFailedtoRemovePipe,[FFileName]);