Skip to content

Commit

Permalink
Fix failing to detect running external EDB server on Monterey; improv…
Browse files Browse the repository at this point in the history
…e accuracy of detecting already-running postgres servers by incorporating username of process returned from ps command; aid debugging by writing detected postgres processes to logs in debug build
  • Loading branch information
mckenfra committed Feb 16, 2022
1 parent fc14742 commit fe95fa7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 33 deletions.
32 changes: 26 additions & 6 deletions PostgreSQL/Classes/PGSearchController.m
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,21 @@ - (NSArray *)startedServers
// Only add running servers that weren't started by launchd
NSMutableDictionary *pids = [NSMutableDictionary dictionaryWithCapacity:loadedServers.count];
for (PGServer *server in loadedServers) {
if (server.pid > 0) pids[@(server.pid)] = [NSNull null];
if (server.pid > 0) pids[@(server.pid)] = server;
}
for (PGServer *server in runningServers) {
if (server.pid <= 0) continue;
if (pids[@(server.pid)]) continue;
[result addObject:server];

PGServer *loadedServer = pids[@(server.pid)];
if (loadedServer) {
// Add missing properties to loaded server from runing server.
if (server.settings.username &&
!loadedServer.settings.username) {
loadedServer.settings.username = server.settings.username;
}
} else {
[result addObject:server];
}
}

return [NSArray arrayWithArray:result];
Expand All @@ -194,12 +203,23 @@ - (NSArray *)runningServers
NSArray *processes = [PGProcess runningProcessesWithNameLike:@".*postgre.*"];
if (processes.count == 0) return nil;

NSMutableArray *result = [NSMutableArray arrayWithCapacity:processes.count];
NSMutableArray *servers = [NSMutableArray arrayWithCapacity:processes.count];
NSUInteger numberOfServers = 0;

for (PGProcess *process in processes) {
PGServer *server = [self.serverController serverFromProcess:process];
if (server) [result addObject:server];
[servers addObject:(server ?: [NSNull null])];
if (server) { numberOfServers++; }
}
return result.count == 0 ? nil : [NSArray arrayWithArray:result];

#ifdef DEBUG
DLog(@"Postgres Processes:\n%@\n\nOther Processes:\n%@",
[processes objectsAtIndexes:[servers indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) { return obj != [NSNull null]; }]],
[processes objectsAtIndexes:[servers indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) { return obj == [NSNull null]; }]]
);
#endif

return numberOfServers == 0 ? nil : [servers filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id obj, NSDictionary *bindings) { return obj != [NSNull null]; }]];
}
- (NSArray *)loadedServers
{
Expand Down
80 changes: 54 additions & 26 deletions PostgreSQL/Classes/PGServerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -534,29 +534,7 @@ - (PGServer *)serverFromProcess:(PGProcess *)process
{
if (!process) return nil;

NSArray *args = [process.command componentsSeparatedByString:@" "];
// Need to fix the problem of spaces in paths
// E.g. ~/Library/Application Support/Postgres
// This is incorrectly interpreted as 2 args
NSMutableArray *programArgs = [NSMutableArray arrayWithCapacity:args.count];
NSMutableString *buffer = [NSMutableString string];
for (NSString *arg in args) {

// New arg type, flush previous one
if ([arg hasPrefix:@"-"] && arg.length == 2) {
if (buffer.length > 0) {
[programArgs addObject:[NSString stringWithString:buffer]];
[buffer deleteCharactersInRange:NSMakeRange(0,buffer.length)];
}
[programArgs addObject:arg];

// Append to previous string
} else {
[buffer appendString:@" "];
[buffer appendString:arg];
}
}
if (buffer.length > 0) [programArgs addObject:[NSString stringWithString:buffer]];
NSArray<NSString *> *programArgs = [self programArgsFromCommand:process.command];

PGServerSettings *settings = [[PGServerSettings alloc] init];
[self populateSettings:settings fromProgramArgs:programArgs];
Expand All @@ -582,7 +560,7 @@ - (PGServer *)serverFromDaemonFile:(NSString *)file

- (PGServer *)serverFromLoadedDaemon:(NSDictionary *)daemon forRootUser:(BOOL)root
{
if (IsLogging) { if (daemon.count > 0) { DLog(@"%@ launchd\n%@", (root ? @"Syatem" : @"User"), daemon); } }
if (IsLogging) { if (daemon.count > 0) { DLog(@"%@ launchd\n%@", (root ? @"System" : @"User"), daemon); } }

PGServer *result = [self serverFromDaemon:daemon];
result.daemonLoadedForAllUsers = root;
Expand Down Expand Up @@ -823,8 +801,7 @@ - (void)clean:(PGServer *)server
- (void)initServer:(PGServer *)server
{
// Internal vs External based on domain
server.external = NonBlank(server.domain) &&
![server.domain isEqualToString:PGPrefsAppID];
server.external = ![server.domain isEqualToString:PGPrefsAppID];

// Internal
if (!server.external) {
Expand Down Expand Up @@ -1023,6 +1000,57 @@ - (void)checkStatusForServer:(PGServer *)server
}
}

- (NSArray<NSString *> *)programArgsFromCommand:(NSString *)command
{
NSArray<NSString *> *args = [command componentsSeparatedByString:@" "];
if (args.count == 1) { return args; }

// Need to allow for spaces in paths
// E.g. ~/Library/Application Support/Postgres
// This is incorrectly split into 2 separate args
//
// If a component doesn't look like an option (starting with a hyphen),
// then assume it's a path and join it to the previous component.
NSMutableArray<NSString *> *programArgs = [NSMutableArray arrayWithCapacity:args.count];
NSMutableString *buffer = [NSMutableString string];
for (NSString *arg in args) {

BOOL isShortOption = [arg hasPrefix:@"-"] &&
arg.length == 2 &&
[[NSCharacterSet alphanumericCharacterSet] characterIsMember:[arg characterAtIndex:1]];
BOOL isLongOption = !isShortOption &&
[arg hasPrefix:@"--"] &&
arg.length > 2 &&
[[NSCharacterSet alphanumericCharacterSet] characterIsMember:[arg characterAtIndex:2]];

// New option, flush buffer containing previous arg
if (isShortOption || isLongOption) {
if (buffer.length > 0) {
[programArgs addObject:[NSString stringWithString:buffer]];
[buffer deleteCharactersInRange:NSMakeRange(0,buffer.length)];
}
}

// Short option, arg is complete
if (isShortOption) {
[programArgs addObject:arg];

// Anything else, arg may need to join to next component,
// so add to buffer
} else {
if (buffer.length > 0) { [buffer appendString:@" "]; }
[buffer appendString:arg];
}
}

// Final flush buffer
if (buffer.length > 0) {
[programArgs addObject:[NSString stringWithString:buffer]];
}

return [NSArray arrayWithArray:programArgs];
}

- (void)populateSettings:(PGServerSettings *)settings fromProgramArgs:(NSArray *)args
{
if (!settings || args.count == 0) return;
Expand Down
2 changes: 1 addition & 1 deletion PostgreSQL/Classes/Utils/PGProcess.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ - (id)initWithPid:(NSInteger)pid ppid:(NSInteger)ppid user:(PGUser *)user comman

- (NSString *)description
{
return [NSString stringWithFormat:@"%@ %@ %@", @(_pid), @(_ppid), _command];
return [NSString stringWithFormat:@"%@ %@ %@ %@", @(_pid), @(_ppid), _user, _command];
}

+ (PGProcess *)processFromPsCommandOutput:(NSString *)output
Expand Down

0 comments on commit fe95fa7

Please sign in to comment.