-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputSourceSwitch.m
805 lines (655 loc) · 22.1 KB
/
InputSourceSwitch.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
#import "ISSUtils.h"
#import <AppKit/AppKit.h>
#import <Carbon/Carbon.h>
#import <sys/stat.h>
#import <dlfcn.h>
#import "InputSourceSwitch.h"
#ifndef ISS_MONITOR_EXECUTABLE
#define ISS_MONITOR_EXECUTABLE "KeyboardMonitor"
#endif
#define ISS_LOCKFILE_ENCODING NSISOLatin1StringEncoding
#define ISS_QUIT_EVENT_SUBTYPE 0x5155
#define ISS_ERROR_EXIT_CODE 127
CFMessagePortRef CFMessagePortCreatePerProcessRemote (CFAllocatorRef allocator, CFStringRef name, CFIndex pid);
BOOL _CreateFlattenedInputSource (TISInputSourceRef inputSource, CFDictionaryRef *flattendProperties);
@interface LockFile : NSObject
@property (readonly) NSURL *url;
@property (readonly) BOOL isLocked;
- (instancetype) initWithAppName: (NSString *) appName;
- (BOOL) lock;
@end
@implementation LockFile {
NSFileHandle *handle;
}
- (instancetype) init {
NSString *name = NSBundle.mainBundle.infoDictionary[(__bridge NSString *) kCFBundleExecutableKey];
if (!name)
name = NSProcessInfo.processInfo.processName;
return [self initWithAppName: name];
}
- (instancetype) initWithAppName: (NSString *) appName {
if (self = [super init]) {
int fd = [self open: appName];
if (fd < 0)
return nil;
handle = [[NSFileHandle alloc] initWithFileDescriptor: fd closeOnDealloc: YES];
if (!handle) {
close (fd);
return nil;
}
}
return self;
}
- (int) open: (NSString *) appName {
NSFileManager *fileManager = NSFileManager.defaultManager;
NSURL *url = [fileManager
URLForDirectory: NSApplicationSupportDirectory
inDomain: NSUserDomainMask
appropriateForURL: nil
create: NO
error: nil
];
if (!url)
return -1;
url = [url URLByAppendingPathComponent: appName isDirectory: YES];
if (!url)
return -1;
_url = [url URLByAppendingPathComponent: @".lockfile" isDirectory: NO];
if (!_url)
return -1;
if (![fileManager createDirectoryAtURL: url withIntermediateDirectories: YES attributes: nil error: nil])
return -1;
return open (_url.fileSystemRepresentation, O_RDWR|O_CREAT|O_CLOEXEC, 0644);
}
- (BOOL) lock {
if (_isLocked)
return YES;
if (flock (handle.fileDescriptor, LOCK_EX|LOCK_NB)) {
int pid = [self readPID];
NSLog (@"Another instance%@ is already running.",
(pid > 0) ? [NSString stringWithFormat: @" (PID %d)", pid] : @""
);
return NO;
}
if (![self isOwned]) {
NSLog (@"Lockfile ownership verification failed.");
return NO;
}
[self writePID];
return (_isLocked = YES);
}
- (BOOL) isOwned {
struct stat fsstat, fdstat;
if (stat (_url.fileSystemRepresentation, &fsstat) || fstat (handle.fileDescriptor, &fdstat))
return NO;
return (fsstat.st_dev == fdstat.st_dev && fsstat.st_ino == fdstat.st_ino);
}
- (int) readPID {
[handle seekToFileOffset: 0];
return [[NSString alloc]
initWithData: [handle readDataOfLength: 1024]
encoding: ISS_LOCKFILE_ENCODING
].intValue;
}
- (void) writePID {
[handle truncateFileAtOffset: 0];
[handle
writeData: [[NSString stringWithFormat: @"%d", getpid ()]
dataUsingEncoding: ISS_LOCKFILE_ENCODING
]
];
[handle synchronizeFile];
}
- (void) dealloc {
if (_isLocked && [self isOwned])
[NSFileManager.defaultManager removeItemAtURL: _url error: nil];
}
@end
@interface DynamicBSM : NSObject
- (pid_t) pidFromAuditToken: (audit_token_t *) auditToken;
@end
@implementation DynamicBSM {
void *_handle;
pid_t (*_audit_token_to_pid) (audit_token_t token);
}
- (instancetype) init {
if (self = [super init]) {
if (!(_handle = dlopen ("libbsm.dylib", RTLD_GLOBAL)))
return nil;
if (!(_audit_token_to_pid = dlsym (_handle, "audit_token_to_pid")))
return nil;
}
return self;
}
- (pid_t) pidFromAuditToken: (audit_token_t *) auditToken {
return _audit_token_to_pid (*auditToken);
}
- (void) dealloc {
if (_handle)
dlclose (_handle);
}
@end
@interface InputSourceSwitchApplication : NSApplication
- (int) runWithServerPortHolder: (ISSUMachPortHolder *) serverPortHolder andMonitorPID: (pid_t) monitorPID;
- (void) quitWithReturnValue: (int) returnValue;
@end
@implementation InputSourceSwitchApplication {
pid_t _monitorPID;
DynamicBSM *_bsm;
ISSUMachPort *_listenPort;
ISSUMachPort *_sendPort;
BOOL _subscribed;
int _returnValue;
}
- (void) receiveDectivationNote: (NSNotification *) note {
if (_subscribed) {
NSLog (@"Received dectivation notfication: %@", [note name]);
[self sendMonitorCommand: ISS_CMD_DEACTIVATE_MONITOR];
}
}
- (void) receiveActivationNote: (NSNotification *) note {
if (_subscribed) {
NSLog (@"Received activation notfication: %@", [note name]);
[self sendMonitorCommand: ISS_CMD_ACTIVATE_MONITOR];
}
}
- (void) subscribeToWorkspaceNotification: (NSString *) notificationName withSelector: (SEL) notificationSelector {
[NSWorkspace.sharedWorkspace.notificationCenter
addObserver: self
selector: notificationSelector
name: notificationName
object: nil
];
}
- (void) unsubscribeFromWorkspaceNotification: (NSString *) notificationName {
[NSWorkspace.sharedWorkspace.notificationCenter
removeObserver: self
name: notificationName
object: nil
];
}
- (void) subscribeToNotifications {
#if 0
[self
subscribeToWorkspaceNotification: NSWorkspaceWillSleepNotification
withSelector: @selector (receiveDectivationNote:)
];
[self
subscribeToWorkspaceNotification: NSWorkspaceDidWakeNotification
withSelector: @selector (receiveActivationNote:)
];
#endif
[self
subscribeToWorkspaceNotification: NSWorkspaceSessionDidResignActiveNotification
withSelector: @selector (receiveDectivationNote:)
];
[self
subscribeToWorkspaceNotification: NSWorkspaceSessionDidBecomeActiveNotification
withSelector: @selector (receiveActivationNote:)
];
_subscribed = YES;
}
- (void) unsubscribeFromNotifications {
_subscribed = NO;
#if 0
[self unsubscribeFromWorkspaceNotification: NSWorkspaceWillSleepNotification];
[self unsubscribeFromWorkspaceNotification: NSWorkspaceDidWakeNotification];
#endif
[self unsubscribeFromWorkspaceNotification: NSWorkspaceSessionDidResignActiveNotification];
[self unsubscribeFromWorkspaceNotification: NSWorkspaceSessionDidBecomeActiveNotification];
}
- (void) runLoop {
[self finishLaunching];
_running = YES;
do {
@autoreleasepool {
NSEvent *event = [self
nextEventMatchingMask: NSEventMaskAny
untilDate: NSDate.distantFuture
inMode: NSDefaultRunLoopMode
dequeue: YES
];
if (event.type == NSEventTypeApplicationDefined && (short) event.subtype == ISS_QUIT_EVENT_SUBTYPE) {
_returnValue = event.data1;
_running = NO;
} else
[self sendEvent: event];
}
} while (_running);
}
- (void) run {
_returnValue = 0;
[self subscribeToNotifications];
@try {
[self runLoop];
} @finally {
[self unsubscribeFromNotifications];
}
}
- (int) runWithServerPortHolder: (ISSUMachPortHolder *) serverPortHolder andMonitorPID: (pid_t) monitorPID {
@try {
_monitorPID = monitorPID;
if (_monitorPID > 0 && !(_bsm = [DynamicBSM new])) {
NSLog (@"Process identity verification required but libbsm could not be loaded.");
return (_returnValue = 2);
}
_listenPort = [serverPortHolder get];
[_listenPort setMessageCallBack: &portExchangeHandler andInfo: (__bridge void *) self];
if (![_listenPort scheduleInRunLoop: CFRunLoopGetMain () forMode: kCFRunLoopDefaultMode]) {
NSLog (@"Failed to schedule server mach port.");
_returnValue = 2;
} else
[self run];
return _returnValue;
} @finally {
_monitorPID = 0;
_bsm = nil;
_listenPort = nil;
_sendPort = nil;
}
}
- (void) sendMonitorCommand: (int) command {
if (_sendPort)
if (!ISSUCommandSend (_sendPort, command)) {
NSLog (@"Failed to send monitor command.");
[self quitWithReturnValue: 3];
}
}
- (void) quitWithReturnValue: (int) returnValue {
[self
postEvent: [NSEvent
otherEventWithType: NSEventTypeApplicationDefined
location: NSMakePoint (0, 0)
modifierFlags: 0
timestamp: NSProcessInfo.processInfo.systemUptime
windowNumber: 0
context: nil
subtype: ISS_QUIT_EVENT_SUBTYPE
data1: returnValue
data2: 0
]
atStart: YES
];
}
// override to avoid hard exit
- (void) terminate: (id) sender {
[self quitWithReturnValue: 0];
}
- (void) dealloc {
// the super class doesn't seem to expect to ever be deallocated,
// so here we perform some obvious cleanup
[NSDistributedNotificationCenter.defaultCenter removeObserver: self];
[NSWorkspace.sharedWorkspace.notificationCenter removeObserver: self];
[NSNotificationCenter.defaultCenter removeObserver: self];
}
static BOOL isSessionActive (void) {
NSDictionary *sessionInfo = (__bridge_transfer NSDictionary *) CGSessionCopyCurrentDictionary ();
if (!sessionInfo)
return NO;
CFTypeRef isActiveValueRef = (__bridge CFTypeRef) sessionInfo[(__bridge NSString *) kCGSessionOnConsoleKey];
return isBooleanTrue (isActiveValueRef);
}
static void portExchangeHandler (ISSUMachPort *port, mach_msg_header_t *msg, void *info) {
InputSourceSwitchApplication *instance = (__bridge InputSourceSwitchApplication *) info;
ISSUMachPort *listenPort, *sendPort;
mach_port_t machPort[instance->_monitorPID > 0 ? 1 : 2];
BOOL ignore;
if (ISSUArrayLength (machPort) < 2) {
audit_token_t *auditToken;
if (ISSUGetAuditToken (msg, &auditToken)) {
pid_t pid = [instance->_bsm pidFromAuditToken: auditToken];
if ((ignore = (pid != instance->_monitorPID)))
NSLog (@"Ignoring message from unauthorized process PID: %d", pid);
} else {
ignore = YES;
NSLog (@"Ignoring message without audit token.");
}
} else {
if ((machPort[1] = ISSUGetBootstrapPort ()) == MACH_PORT_NULL) {
NSLog (@"Failed to retrieve bootstrap port.");
goto error_exit;
}
ignore = NO;
}
if (!ISSUPortRightsReceive (msg, machPort, 1)) {
if (ignore)
return;
NSLog (@"Failed to receive mach port rights.");
goto error_exit;
}
if (!(sendPort = [[ISSUMachPort alloc] initWithMachPort: machPort[0]])) {
if (ignore)
return;
NSLog (@"Failed to create send mach port.");
goto error_exit;
}
if (ignore) {
ISSUCommandSend (sendPort, -1);
return;
}
instance->_sendPort = sendPort;
// now replace the listen port which is registered with the bootstrap
// server (the "server port") with an anonymous one (the "listen port")
if (!(listenPort = [ISSUMachPort new])) {
NSLog (@"Failed to create listen mach port.");
goto error_exit;
}
[listenPort setMessageCallBack: &monitorEventHandler andInfo: info];
if (![listenPort scheduleInRunLoop: CFRunLoopGetMain () forMode: kCFRunLoopDefaultMode]) {
NSLog (@"Failed to schedule listen mach port.");
goto error_exit;
}
machPort[0] = listenPort.machPort;
// send the new listen port to the monitor process
if (!ISSUPortRightsSend (instance->_sendPort, machPort, ISSUArrayLength (machPort))) {
NSLog (@"Failed to send mach port rights.");
goto error_exit;
}
if (isSessionActive ())
[instance sendMonitorCommand: ISS_CMD_ACTIVATE_MONITOR];
// N.B. since the "server port" was used as the requestor port when creating
// the bootstrap subset passed as the bootstrap port to the monitor process
// its deallocation below will cause the bootstrap subset to be destroyed
// and in effect the monitor process's bootstrap port to become a dead port
// unless/until the monitor process resets its bootstrap port
instance->_listenPort = listenPort;
instance->_bsm = nil; // this is not needed beyond port exchange
return;
error_exit:
[instance quitWithReturnValue: 3];
}
static void monitorEventHandler (ISSUMachPort *port, mach_msg_header_t *msg, void *info) {
InputSourceSwitchApplication *instance = (__bridge InputSourceSwitchApplication *) info;
int command;
if (!ISSUCommandReceive (msg, &command)) {
NSLog (@"Invalid message received.");
goto error_exit;
}
switch (command) {
case ISS_CMD_PERFORM_SWITCH:
switchInputSource ();
break;
default:
NSLog (@"Invalid command received: %d", command);
goto error_exit;
}
return;
error_exit:
[instance quitWithReturnValue: 3];
}
static void switchInputSource (void) {
NSArray *inputSources = (__bridge_transfer NSArray *) TISCreateInputSourceList (
(__bridge CFDictionaryRef) @{
(__bridge NSString *) kTISPropertyInputSourceCategory:
(__bridge NSString *) kTISCategoryKeyboardInputSource,
(__bridge NSString *) kTISPropertyInputSourceIsEnabled: @YES,
(__bridge NSString *) kTISPropertyInputSourceIsSelectCapable: @YES
},
NO
);
NSUInteger count;
if ((count = [inputSources count]) < 2)
return; // no point to switch if less than two sources are available
[inputSources enumerateObjectsUsingBlock: ^ (id element, NSUInteger idx, BOOL *stop) {
TISInputSourceRef inputSource = (__bridge TISInputSourceRef) element;
CFTypeRef isSelectedValueRef = (CFTypeRef) TISGetInputSourceProperty (
inputSource,
kTISPropertyInputSourceIsSelected
);
if (isBooleanTrue (isSelectedValueRef)) {
inputSource = (__bridge TISInputSourceRef) inputSources[(idx + 1) % count];
selectInputSource (inputSource);
*stop = YES;
}
}];
}
static void selectInputSource (TISInputSourceRef inputSource) {
// send the input source select TSM message first
if (!sendSelectInputSourceTSMMessage (inputSource)) {
// try the traditional way if the input source select TSM message didn't work
TISSelectInputSource (inputSource);
}
}
static BOOL sendSelectInputSourceTSMMessage (TISInputSourceRef inputSource) {
NSDictionary *ownerProperties = getCurrentInputSourceOwnerProperties ();
if (!ownerProperties)
return NO;
pid_t ownerPid;
if (!getInputSourceOwnerPid (ownerProperties, &ownerPid))
return NO;
{
NSData *messageData = getInputSourceSelectTSMMessageData (inputSource, ownerProperties);
if (!messageData)
return NO;
CFMessagePortRef ownerTSMPort = CFMessagePortCreatePerProcessRemote (kCFAllocatorDefault, CFSTR ("com.apple.tsm.portname"), ownerPid);
if (!ownerTSMPort) {
NSLog (@"Failed to open TSM port to input source onwer PID %d.", ownerPid);
return NO;
}
NSData *responseData;
SInt32 result = CFMessagePortSendRequest (
ownerTSMPort, 7, (__bridge CFDataRef) messageData, 1.0, 1.0, CFSTR ("TSM Message"), (void *) &responseData
);
CFMessagePortInvalidate (ownerTSMPort);
CFRelease (ownerTSMPort);
if (result != 0) {
NSLog (@"Failed to send input source select TSM message to the input source owner PID %d.", ownerPid);
return NO;
}
if (!responseData) {
NSLog (@"No response to select input source TSM message from PID %d.", ownerPid);
return NO;
}
#if 0
NSLog (@"Input source select TSM message response from PID %d:\n%.*s", ownerPid, (int) responseData.length, responseData.bytes);
#endif
}
{
id selectedInputSource = (__bridge_transfer id) TISCopyCurrentKeyboardInputSource ();
if (CFEqual ((__bridge TISInputSourceRef) selectedInputSource, inputSource))
return YES;
}
NSLog (@"The input source select TSM message didn't work for PID %d.", ownerPid);
return NO;
}
static NSDictionary *getCurrentInputSourceOwnerProperties (void) {
NSDictionary *properties;
CFMessagePortRef serverTSMPort = CFMessagePortCreateRemote (kCFAllocatorDefault, CFSTR ("com.apple.tsm.uiserver"));
if (!serverTSMPort) {
NSLog (@"Failed to open TIM Core TSM port.");
return nil;
}
NSData *responseData;
SInt32 result = CFMessagePortSendRequest (serverTSMPort, 20, NULL, 1.0, 1.0, CFSTR ("TIM Core Request"), (void *) &responseData);
CFMessagePortInvalidate (serverTSMPort);
CFRelease (serverTSMPort);
if (result != 0) {
NSLog (@"Failed to send current input source owner TIM Core TSM request.");
return nil;
}
properties = (__bridge_transfer NSDictionary *) CFPropertyListCreateWithData (
kCFAllocatorDefault, (__bridge CFDataRef) responseData, kCFPropertyListImmutable, NULL, NULL
);
if (!properties) {
NSLog (@"Failed to parse current input source owner TIM Core TSM response.");
return nil;
}
return properties;
}
static BOOL getInputSourceOwnerPid (NSDictionary *ownerProperties, pid_t *pidPointer) {
NSArray *psnData = ownerProperties[@"tsmMessagePSNKey"];
if (!psnData) {
NSLog (@"Input source owner properties do not include process serial number:\n%@", ownerProperties.descriptionInStringsFileFormat);
return NO;
}
ProcessSerialNumber psn;
psn.highLongOfPSN = [psnData[0] unsignedLongValue];
psn.lowLongOfPSN = [psnData[1] unsignedLongValue];
if (
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
GetProcessPID
#pragma clang diagnostic pop
(
&psn,
pidPointer
) != 0
) {
NSLog (@"Failed to obtain PID of the input source owner PSN %u:%u.", psn.highLongOfPSN, psn.lowLongOfPSN);
return NO;
}
return YES;
}
static NSData *getInputSourceSelectTSMMessageData (TISInputSourceRef inputSource, NSDictionary *ownerProperties) {
NSMutableDictionary *messageDataDictionary = [NSMutableDictionary dictionaryWithCapacity: 2];
{
NSDictionary *flattenedProperties;
if (!_CreateFlattenedInputSource (inputSource, (void *) &flattenedProperties)) {
NSLog (@"Failed to obtain input source flattened properties.");
return nil;
}
messageDataDictionary[@"tsmInputSourceSelectedInpSrcKey"] = flattenedProperties;
}
{
id selectedTargetTSMDocKey = ownerProperties[@"tsmTargetTSMDocumentKeyKey"];
if (selectedTargetTSMDocKey) {
messageDataDictionary[@"tsmInputSourceSelectedTSMDocKey"] = selectedTargetTSMDocKey;
}
}
NSData *messageData = (__bridge_transfer NSData *) CFPropertyListCreateData (
kCFAllocatorDefault, (__bridge CFDictionaryRef) messageDataDictionary, kCFPropertyListXMLFormat_v1_0, 0, NULL
);
if (!messageData) {
NSLog (@"Failed to serialize input source select message data.");
return nil;
}
return messageData;
}
static BOOL isBooleanTrue (CFTypeRef valueRef) {
return
valueRef
&&
CFGetTypeID (valueRef) == CFBooleanGetTypeID ()
&&
CFBooleanGetValue (valueRef);
}
@end
static void handleSignalAsQuit (void *info) {
[NSApp quitWithReturnValue: 0];
}
static void handleChildDeath (void *info) {
int status;
// reap the child
if (waitpid (-1, &status, WNOHANG) > 0) {
if (WIFEXITED (status)) {
int exitStatus = WEXITSTATUS (status);
if (exitStatus)
NSLog (@"Monitor process exited unexpectedly with exit status: %d", exitStatus);
else
NSLog (@"Monitor process exited unexpectedly.");
} else if (WIFSIGNALED (status)) {
int signum = WTERMSIG (status);
NSLog (@"Monitor process was terminated unexpectedly by signal: %d (SIG%@)", signum, [@(sys_signame[signum]) uppercaseString]);
}
} else
NSLog (@"Failed to get the monitor process exit status.");
[NSApp quitWithReturnValue: 3];
}
static ISSUSignalHandlerTableEntry signalHandlerTable[] = {
{SIGTERM, &handleSignalAsQuit},
{SIGINT, &handleSignalAsQuit},
{SIGCHLD, &handleChildDeath}
};
static NSURL *getMonitorExecutableURL (char *argv0) {
NSURL *url = ISSUGetAbsoluteFileURL (argv0);
if (!url)
return nil;
url = [url URLByDeletingLastPathComponent];
if (!url)
return nil;
return [NSURL
fileURLWithFileSystemRepresentation: ISS_MONITOR_EXECUTABLE
isDirectory: NO
relativeToURL: url
];
}
static ISSUMachPortHolder *createServerPortHolder (ISSUMachPort *port) {
NSString *name = ISSUGetPerProcessName (ISS_SERVER_PORT_NAME, getpid ());
if (!name)
return nil;
if (![port registerName: name])
return nil;
return [ISSUMachPortHolder holderWithPort: port];
}
int autoreleasedMain (int argc, char * const argv[]) {
LockFile *lockFile;
NSArray *signalHandlerManagers;
ISSUMachPortHolder *serverPortHolder;
pid_t pid;
if (!(signalHandlerManagers = ISSUSetupSignalHandlers (signalHandlerTable, ISSUArrayLength (signalHandlerTable)))) {
NSLog (@"Failed to setup signal handlers.");
goto error_exit;
}
if (!(lockFile = [LockFile new])) {
NSLog (@"Failed to create lockfile.");
goto error_exit;
}
if (![lockFile lock])
goto error_exit;
{
NSURL *monitorExecutableURL = getMonitorExecutableURL (argv[0]);
if (!monitorExecutableURL) {
NSLog (@"Failed to build monitor executable path.");
goto error_exit;
}
if (access (monitorExecutableURL.fileSystemRepresentation, X_OK) != 0) {
NSLog (@"Monitor executable not present/executable: %s", monitorExecutableURL.fileSystemRepresentation);
goto error_exit;
}
ISSUMachPort *port = [ISSUMachPort new];
if (!port) {
NSLog (@"Failed to create server mach port.");
goto error_exit;
}
// try to create a bootstrap subset / private bootstrap namespace
mach_port_t bootstrapPort = ISSUCreateBootstrapSubset (port.machPort);
if (!(serverPortHolder = createServerPortHolder (port))) {
NSLog (@"Failed to register server mach port.");
goto error_exit;
}
switch (pid = fork ()) {
case -1: { // error
NSLog (@"Failed to fork: %s", strerror (errno));
goto error_exit;
}
case 0: { // child / monitor
char *monitorCmd[] = {(char *) monitorExecutableURL.fileSystemRepresentation, NULL};
// start the monitor process
execvp (*monitorCmd, monitorCmd);
_exit (ISS_ERROR_EXIT_CODE);
}
}
// parent / switch application
// we negate the PID if the server port is registered in a private bootstrap
// namespace
// the negative PID causes the monitor process identity verification to be
// skipped by the InputSourceSwitchApplication, which is ok, as no foreign
// processes can get access to the ports registered in the private bootstrap
// namespace
if (bootstrapPort != MACH_PORT_NULL) {
if (!ISSUResetBootstrapPort (bootstrapPort)) {
NSLog (@"Failed to restore bootstrap port.");
goto error_exit;
}
pid = -pid;
}
}
[InputSourceSwitchApplication sharedApplication];
if (!NSApp)
goto error_exit;
int rv = [NSApp runWithServerPortHolder: serverPortHolder andMonitorPID: pid];
NSApp = nil; // release the global application instance
return rv;
error_exit:
return 2;
}