Changeset 84
- Timestamp:
- 12/27/05 17:19:06 (3 years ago)
- Files:
-
- trunk/Libs/SSHAgent.m (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Libs/SSHAgent.m
r83 r84 169 169 int i; 170 170 171 /* If the PID is > 0, the agent should (in theory) be running. */ 172 if([self PID] > 0) 171 if ([self isRunning]) 173 172 { 174 173 NSLog(@"Agent is already started"); … … 176 175 } 177 176 178 /* Clear the agentSocketPath object. */ 179 if([self agentSocketPath]) 180 { 181 [agentSocketPathLock lock]; 182 [agentSocketPath release]; 183 agentSocketPath = nil; 184 [agentSocketPathLock unlock]; 185 } 186 187 188 if([self socketPath] == nil) 177 [self setAgentSocketPath:nil]; 178 179 if (![self socketPath]) 189 180 { 190 181 NSLog(@"DEBUG: start: socketPath not set"); … … 215 206 continue; 216 207 208 NSString *key = [columns objectAtIndex:1]; 217 209 /* If 2nd column matches "SSH_AUTH_SOCK", then 3rd column is the socket path. */ 218 if ([@"SSH_AUTH_SOCK" isEqualToString:[columns objectAtIndex:1]]) 219 { 220 [agentSocketPathLock lock]; 221 agentSocketPath = [[NSString stringWithString:[columns objectAtIndex:2]] retain]; 222 [agentSocketPathLock unlock]; 223 } 210 if ([key isEqualToString:@"SSH_AUTH_SOCK"]) 211 [self setAgentSocketPath:[columns objectAtIndex:2]]; 224 212 225 213 /* If 2nd column matches "SSH_AGENT_PID", then 3rd column is the PID. */ 226 if ([@"SSH_AGENT_PID" isEqualToString:[columns objectAtIndex:1]]) 227 { 228 [thePIDLock lock]; 229 thePID = [[columns objectAtIndex:2] intValue]; 230 [thePIDLock unlock]; 231 } 232 } 233 234 /* If the PID is > 0 but the socket path isn't filled, stop the agent and return -1. */ 235 if(([self PID] > 0) && (([self agentSocketPath] == nil) || ([agentSocketPath length] < 1))) 214 else if ([key isEqualToString:@"SSH_AGENT_PID"]) 215 [self setPID:[[columns objectAtIndex:2] intValue]]; 216 } 217 218 /* If the agent is not running, or the socket path is empty then stop the agent and fail */ 219 if (![self isRunning] || ![[self agentSocketPath] length]) 236 220 { 237 221 NSLog(@"SSHAgent start: ssh-agent didn't give the output we expected"); … … 240 224 } 241 225 242 /* If there's no PID just return -1. */243 if([self PID] < 1)244 {245 NSLog(@"SSHAgent start: ssh-agent didn't give the output we expected");246 return NO;247 }248 249 226 /* Handle connections in a seperate thread. */ 250 227 [NSThread detachNewThreadSelector:@selector(handleAgentConnections) toTarget:self withObject:nil]; … … 261 238 - (BOOL)stop 262 239 { 263 /* We can't stop something if we haven't got the pid. */ 264 if([self PID] > 0) 265 { 266 /* We don't need to check if this fails. We clean up the variables either way. */ 267 kill([self PID], SIGTERM); 268 269 [agentSocketPathLock lock]; 270 [agentSocketPath release]; 271 agentSocketPath = nil; 272 [agentSocketPathLock unlock]; 273 274 [self closeSockets]; 275 276 [thePIDLock lock]; 277 thePID = 0; 278 [thePIDLock unlock]; 279 280 [keysOnAgentLock lock]; 281 [keysOnAgent release]; 282 keysOnAgent = nil; 283 [keysOnAgentLock unlock]; 284 285 [[NSNotificationCenter defaultCenter] postNotificationName:@"AgentStopped" object:nil]; 286 } 240 /* We can't stop something if we it's not running */ 241 if (![self isRunning]) 242 return YES; 243 244 /* We don't need to check if this fails. We clean up the variables either way. */ 245 kill([self PID], SIGTERM); 246 247 [self setAgentSocketPath:nil]; 248 [self closeSockets]; 249 [self setPID:0]; 250 [self setKeysOnAgent:nil]; 251 252 [[NSNotificationCenter defaultCenter] postNotificationName:@"AgentStopped" object:nil]; 287 253 288 254 return YES; … … 504 470 write(fds[i+1], buf, r); 505 471 506 [keysOnAgentLock lock]; 507 keysOnAgent = [[[SSHAgent currentAgent] currentKeysOnAgent] retain]; 508 [keysOnAgentLock unlock]; 472 [self setKeysOnAgent:[[SSHAgent currentAgent] currentKeysOnAgent]]; 509 473 [[NSNotificationCenter defaultCenter] postNotificationName:@"KeysOnAgentUnknown" object:nil]; 510 474 } … … 515 479 write(fds[i+1], buf, r); 516 480 517 [keysOnAgentLock lock]; 518 keysOnAgent = [[[SSHAgent currentAgent] currentKeysOnAgent] retain]; 519 [keysOnAgentLock unlock]; 520 481 [self setKeysOnAgent:[[SSHAgent currentAgent] currentKeysOnAgent]]; 521 482 [[NSNotificationCenter defaultCenter] postNotificationName:@"KeysOnAgentUnknown" object:nil]; 522 483 } … … 708 669 NSArray *columns, *key, *lines; 709 670 710 /* If the PID is < 1, we assume the agent isn't running. */ 711 if([self PID] < 1) 712 { 671 if (![self isRunning]) 713 672 return nil; 714 }715 673 716 674 /* Initialize a ssh-add SSHTool, set the arguments to -l for a list of keys. */ … … 775 733 - (void)keysOnAgentStatusChange:(NSNotification *)notification 776 734 { 777 if([[notification name] isEqualToString:@"AgentEmptied"]) 778 { 779 [keysOnAgentLock lock]; 780 if(keysOnAgent) 781 { 782 [keysOnAgent release]; 783 } 784 785 keysOnAgent = nil; 786 787 [keysOnAgentLock unlock]; 788 } 789 790 else if([[notification name] isEqualToString:@"AgentFilled"]) 791 { 792 [keysOnAgentLock lock]; 793 keysOnAgent = [[[SSHAgent currentAgent] currentKeysOnAgent] retain]; 794 [keysOnAgentLock unlock]; 795 } 796 735 if ([[notification name] isEqualToString:@"AgentEmptied"]) 736 [self setKeysOnAgent:nil]; 737 738 else if ([[notification name] isEqualToString:@"AgentFilled"]) 739 [self setKeysOnAgent:[[SSHAgent currentAgent] currentKeysOnAgent]]; 797 740 } 798 741
