Skip to content

Commit 94a4f29

Browse files
committed
Phase 2 of Fixing Worker To Handle Asynchronous Return of Pajara For Preflightling Safari Bookmarks Hash. This tests OK with my usual "change in bookmark in Chrome", so I think I have not broken anything yet.
• In the worker1() function, all of the two dozen or so goto have been removed, and it has been renamed to workerSetup(). • The old clean-up code after the run loop has been moved into new function workerEnd1() • The code between the first old goto label `end` and the second goto label `endNow` has been moved into new function workerEnd2() • The code after the second old goto label `endNow` has been moved into new function workerEnd3() All of the local variables in the outer scope of the old main() are now global variables, as are three new BOOLs shouldSkipEnd1, shouldSkipEnd2 and shouldSkipEnd3 which provide the skips formerly provided by the gotos. The run loop will exit immediately if workerSetup took any of its early returns. Next, I think I need to get the run loop running earlier.
1 parent fab7f1a commit 94a4f29

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

SSYUserInfo.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ __attribute__((visibility("default"))) @interface SSYUserInfo : NSObject {
4747
*/
4848
+ (NSString*)consoleUserNameAndUid_p:(uid_t*)uid_p
4949
gid_p:(gid_t*)gid_p ;
50-
5150
/*!
5251
@brief Returns a date object representing the time that the current user
5352
logged in to the macOS user account currently showing in the GUI
5453
55-
@result A NSDate, or upon failure, if assertions are not active, nil
54+
@param error_p Pointer which will, upon return, if an error occurred and
55+
said pointer is not NULL, point to an NSError describing said error.
56+
@result A NSDate, or upon failure, nil
5657
*/
57-
+ (NSDate*)whenThisUserLoggedIn;
58+
+ (NSDate*)whenThisUserLoggedInError_p:(NSError**)error_p ;
5859

5960
@end

SSYUserInfo.m

+36-9
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,42 @@ + (NSString*)consoleUserNameAndUid_p:(uid_t*)uid_p
4646
return [name autorelease] ;
4747
}
4848

49-
+ (NSDate*)whenThisUserLoggedIn {
49+
+ (NSDate*)whenThisUserLoggedInError_p:(NSError**)error_p {
5050
/* See:
5151
https://www.freebsd.org/cgi/man.cgi?query=getutxent&sektion=3
5252
*/
5353
NSString* targetUser = (NSString*)CFBridgingRelease(SCDynamicStoreCopyConsoleUser(NULL, NULL, NULL)) ;
54-
struct utmpx* record_p;
54+
struct utmpx* entry_p;
5555
/* getutxent gets the entire history of UTX entries. By passing 0 in the
5656
following line, we tell it to give us the most recent entries first. */
5757
setutxent_wtmp(0);
5858
NSDate* date = nil;
59-
while (!date && (record_p = getutxent_wtmp()) != NULL) {
60-
if (record_p->ut_type == USER_PROCESS) {
61-
/* In addition to macOS GUI logins, there are also UTX records for
59+
NSInteger countOfEntries = 0;
60+
NSInteger countOfUserEntries = 0;
61+
NSInteger countOfConsoleUserEntries = 0;
62+
NSInteger countOfThisConsoleUserEntries = 0;
63+
NSString* user = nil;
64+
NSMutableArray* lines = [NSMutableArray new];
65+
while (!date && (entry_p = getutxent_wtmp()) != NULL) {
66+
countOfEntries++;
67+
if (entry_p->ut_type == USER_PROCESS) {
68+
countOfUserEntries++;
69+
/* In addition to macOS GUI logins, there are also UTX entries for
6270
other logins such as Terminal.app tabs. These are
6371
differentiated as different "line" types. For the macOS GUI login
6472
which we want, the line type is "console". */
65-
NSString* line = [NSString stringWithUTF8String:record_p->ut_line];
73+
NSString* line = [NSString stringWithUTF8String:entry_p->ut_line];
74+
if (line) {
75+
[lines addObject:line];
76+
}
6677
if ([line isEqualToString:@"console"]) {
78+
countOfConsoleUserEntries++;
6779
/* Finally, of course, we are only interested in logins of the
6880
current macOS user. */
69-
NSString* user = [NSString stringWithUTF8String:record_p->ut_user];
81+
user = [NSString stringWithUTF8String:entry_p->ut_user];
7082
if ([user isEqualToString:targetUser]) {
71-
time_t unixTime = record_p->ut_tv.tv_sec;
83+
countOfThisConsoleUserEntries++;
84+
time_t unixTime = entry_p->ut_tv.tv_sec;
7285
date = [NSDate dateWithTimeIntervalSince1970:unixTime];
7386
break;
7487
}
@@ -77,7 +90,21 @@ + (NSDate*)whenThisUserLoggedIn {
7790
};
7891
endutxent_wtmp();
7992

80-
NSAssert(date, @"Could not decode UTX record");
93+
if (!date && error_p) {
94+
NSError* error = [NSError errorWithDomain:@"SSYUserInfoErrorDomain"
95+
code:948308
96+
userInfo:@{
97+
NSLocalizedDescriptionKey: @"SSYUserInfo could not find `%@` console UTX entry",
98+
@"entries count": [NSNumber numberWithInteger:countOfEntries],
99+
@"user entries count": [NSNumber numberWithInteger:countOfUserEntries ],
100+
@"console user entries count": [NSNumber numberWithInteger:countOfConsoleUserEntries ],
101+
@"this console user entries count": [NSNumber numberWithInteger:countOfThisConsoleUserEntries ],
102+
@"lines": lines
103+
}];
104+
*error_p = error;
105+
}
106+
107+
[lines release];
81108
return date;
82109
}
83110

0 commit comments

Comments
 (0)