├── Client ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ └── MainMenu.xib ├── Info.plist └── main.m ├── LaunchDaemon.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata ├── xcshareddata │ └── xcschemes │ │ ├── Client.xcscheme │ │ ├── Install.xcscheme │ │ ├── LaunchDaemon.xcscheme │ │ └── Uninstall.xcscheme └── xcuserdata │ └── jeff.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist ├── LaunchDaemon ├── Daemon.h ├── Daemon.m ├── Protocols.h ├── daemon-Launchd.plist └── main.m ├── License ├── ReadMe └── Scripts ├── install.sh └── uninstall.sh /Client/AppDelegate.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Written by Jeff Spooner 4 | 5 | */ 6 | 7 | #import 8 | 9 | 10 | @interface AppDelegate : NSObject 11 | 12 | @end 13 | 14 | -------------------------------------------------------------------------------- /Client/AppDelegate.m: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Written by Jeff Spooner 4 | 5 | XPC allows clients to pass completion blocks as parameters to messages to the server, 6 | removing the need to have explicit methods which the server calls on the client. This 7 | is a really nice improvement over the old Distributed Objects API. 8 | 9 | */ 10 | 11 | #import "AppDelegate.h" 12 | #import "Protocols.h" 13 | 14 | 15 | @interface AppDelegate () 16 | 17 | @property (weak) IBOutlet NSWindow *window; 18 | 19 | @property (nonatomic, strong, readwrite) NSXPCConnection *connection; 20 | 21 | @end 22 | 23 | 24 | @implementation AppDelegate 25 | 26 | 27 | - (NSXPCConnection *) connection 28 | { 29 | // Create the XPC Connection on demand 30 | if (_connection == nil) { 31 | _connection = [[NSXPCConnection alloc] initWithMachServiceName:daemonLabel options:NSXPCConnectionPrivileged]; 32 | _connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(ExampleDaemonProtocol)]; 33 | _connection.invalidationHandler = 34 | ^{ 35 | _connection = nil; 36 | NSLog(@"connection has been invalidated"); 37 | }; 38 | 39 | // New connections always start in a suspended state 40 | [_connection resume]; 41 | } 42 | return _connection; 43 | } 44 | 45 | 46 | - (IBAction) incrementCount:(id)sender 47 | { 48 | [self.connection.remoteObjectProxy incrementCount]; 49 | } 50 | 51 | 52 | - (IBAction) getCount:(id)sender 53 | { 54 | [self.connection.remoteObjectProxy getCount: 55 | ^(int count) { 56 | NSLog(@"daemon's count is %d", count); 57 | }]; 58 | } 59 | 60 | 61 | #pragma mark - CFNotificationCenter 62 | 63 | static void notificationCenterCallBack(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) 64 | { 65 | NSLog(@"daemon has incremented it's count"); 66 | } 67 | 68 | 69 | #pragma mark - NSApplicationDelegate 70 | 71 | - (void) applicationDidFinishLaunching:(NSNotification *)notification 72 | { 73 | // Register to observe Darwin notifications 74 | CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)self, notificationCenterCallBack, CFSTR("com.example.daemonCounterDidChange"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately); 75 | } 76 | 77 | 78 | - (void) applicationWillTerminate:(NSNotification *)notification 79 | { 80 | // Unregister to observe Darwin notifications 81 | CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)self, CFSTR("com.example.daemonCounterDidChange"), NULL); 82 | } 83 | 84 | 85 | - (BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender 86 | { return YES; } 87 | 88 | 89 | @end 90 | -------------------------------------------------------------------------------- /Client/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "mac", 5 | "size" : "16x16", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "mac", 10 | "size" : "16x16", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "mac", 15 | "size" : "32x32", 16 | "scale" : "1x" 17 | }, 18 | { 19 | "idiom" : "mac", 20 | "size" : "32x32", 21 | "scale" : "2x" 22 | }, 23 | { 24 | "idiom" : "mac", 25 | "size" : "128x128", 26 | "scale" : "1x" 27 | }, 28 | { 29 | "idiom" : "mac", 30 | "size" : "128x128", 31 | "scale" : "2x" 32 | }, 33 | { 34 | "idiom" : "mac", 35 | "size" : "256x256", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "mac", 40 | "size" : "256x256", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "mac", 45 | "size" : "512x512", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "mac", 50 | "size" : "512x512", 51 | "scale" : "2x" 52 | } 53 | ], 54 | "info" : { 55 | "version" : 1, 56 | "author" : "xcode" 57 | } 58 | } -------------------------------------------------------------------------------- /Client/Base.lproj/MainMenu.xib: -------------------------------------------------------------------------------- 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 | Default 540 | 541 | 542 | 543 | 544 | 545 | 546 | Left to Right 547 | 548 | 549 | 550 | 551 | 552 | 553 | Right to Left 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | Default 565 | 566 | 567 | 568 | 569 | 570 | 571 | Left to Right 572 | 573 | 574 | 575 | 576 | 577 | 578 | Right to Left 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 | 689 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | -------------------------------------------------------------------------------- /Client/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | $(MACOSX_DEPLOYMENT_TARGET) 27 | NSHumanReadableCopyright 28 | Copyright © 2016 Example. All rights reserved. 29 | NSMainNibFile 30 | MainMenu 31 | NSPrincipalClass 32 | NSApplication 33 | 34 | 35 | -------------------------------------------------------------------------------- /Client/main.m: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | */ 4 | 5 | #import 6 | 7 | 8 | int main(int argc, const char * argv[]) 9 | { 10 | return NSApplicationMain(argc, argv); 11 | } 12 | -------------------------------------------------------------------------------- /LaunchDaemon.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXAggregateTarget section */ 10 | 1F8A80E11C4EDA4F00971E9D /* Install */ = { 11 | isa = PBXAggregateTarget; 12 | buildConfigurationList = 1F8A80E21C4EDA4F00971E9D /* Build configuration list for PBXAggregateTarget "Install" */; 13 | buildPhases = ( 14 | 1F8A80E71C4EDA6300971E9D /* ShellScript */, 15 | ); 16 | dependencies = ( 17 | 1F8A80E61C4EDA5D00971E9D /* PBXTargetDependency */, 18 | ); 19 | name = Install; 20 | productName = Install; 21 | }; 22 | 1F8A80EE1C4F083E00971E9D /* Uninstall */ = { 23 | isa = PBXAggregateTarget; 24 | buildConfigurationList = 1F8A80EF1C4F083E00971E9D /* Build configuration list for PBXAggregateTarget "Uninstall" */; 25 | buildPhases = ( 26 | 1F8A80F21C4F084200971E9D /* ShellScript */, 27 | ); 28 | dependencies = ( 29 | ); 30 | name = Uninstall; 31 | productName = Uninstall; 32 | }; 33 | /* End PBXAggregateTarget section */ 34 | 35 | /* Begin PBXBuildFile section */ 36 | 1F8A80B91C4EB0AF00971E9D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F8A80B81C4EB0AF00971E9D /* main.m */; }; 37 | 1F8A80C11C4EB0EB00971E9D /* Daemon.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F8A80C01C4EB0EB00971E9D /* Daemon.m */; }; 38 | 1F8A80CC1C4EB54300971E9D /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F8A80CB1C4EB54300971E9D /* AppDelegate.m */; }; 39 | 1F8A80CF1C4EB54300971E9D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F8A80CE1C4EB54300971E9D /* main.m */; }; 40 | 1F8A80D11C4EB54300971E9D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1F8A80D01C4EB54300971E9D /* Assets.xcassets */; }; 41 | 1F8A80D41C4EB54300971E9D /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1F8A80D21C4EB54300971E9D /* MainMenu.xib */; }; 42 | /* End PBXBuildFile section */ 43 | 44 | /* Begin PBXContainerItemProxy section */ 45 | 1F8A80E51C4EDA5D00971E9D /* PBXContainerItemProxy */ = { 46 | isa = PBXContainerItemProxy; 47 | containerPortal = 1F8A80AD1C4EB0AF00971E9D /* Project object */; 48 | proxyType = 1; 49 | remoteGlobalIDString = 1F8A80B41C4EB0AF00971E9D; 50 | remoteInfo = LaunchDaemon; 51 | }; 52 | /* End PBXContainerItemProxy section */ 53 | 54 | /* Begin PBXCopyFilesBuildPhase section */ 55 | 1F8A80B31C4EB0AF00971E9D /* CopyFiles */ = { 56 | isa = PBXCopyFilesBuildPhase; 57 | buildActionMask = 2147483647; 58 | dstPath = /usr/share/man/man1/; 59 | dstSubfolderSpec = 0; 60 | files = ( 61 | ); 62 | runOnlyForDeploymentPostprocessing = 1; 63 | }; 64 | /* End PBXCopyFilesBuildPhase section */ 65 | 66 | /* Begin PBXFileReference section */ 67 | 1F8A80B51C4EB0AF00971E9D /* LaunchDaemon */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = LaunchDaemon; sourceTree = BUILT_PRODUCTS_DIR; }; 68 | 1F8A80B81C4EB0AF00971E9D /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 69 | 1F8A80BF1C4EB0EB00971E9D /* Daemon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Daemon.h; sourceTree = ""; }; 70 | 1F8A80C01C4EB0EB00971E9D /* Daemon.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Daemon.m; sourceTree = ""; }; 71 | 1F8A80C21C4EB27900971E9D /* Protocols.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Protocols.h; sourceTree = ""; }; 72 | 1F8A80C31C4EB3BE00971E9D /* daemon-Launchd.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "daemon-Launchd.plist"; sourceTree = ""; }; 73 | 1F8A80C81C4EB54300971E9D /* Client.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Client.app; sourceTree = BUILT_PRODUCTS_DIR; }; 74 | 1F8A80CA1C4EB54300971E9D /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 75 | 1F8A80CB1C4EB54300971E9D /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 76 | 1F8A80CE1C4EB54300971E9D /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 77 | 1F8A80D01C4EB54300971E9D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 78 | 1F8A80D31C4EB54300971E9D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = ""; }; 79 | 1F8A80D51C4EB54300971E9D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 80 | 1F8A80EB1C4F004E00971E9D /* License */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = License; sourceTree = SOURCE_ROOT; }; 81 | 1F8A80EC1C4F005E00971E9D /* ReadMe */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = ReadMe; sourceTree = SOURCE_ROOT; }; 82 | 1F8A80ED1C4F022D00971E9D /* install.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; name = install.sh; path = Scripts/install.sh; sourceTree = SOURCE_ROOT; }; 83 | 1F8A80F31C4F087200971E9D /* uninstall.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; name = uninstall.sh; path = Scripts/uninstall.sh; sourceTree = SOURCE_ROOT; }; 84 | /* End PBXFileReference section */ 85 | 86 | /* Begin PBXFrameworksBuildPhase section */ 87 | 1F8A80B21C4EB0AF00971E9D /* Frameworks */ = { 88 | isa = PBXFrameworksBuildPhase; 89 | buildActionMask = 2147483647; 90 | files = ( 91 | ); 92 | runOnlyForDeploymentPostprocessing = 0; 93 | }; 94 | 1F8A80C51C4EB54300971E9D /* Frameworks */ = { 95 | isa = PBXFrameworksBuildPhase; 96 | buildActionMask = 2147483647; 97 | files = ( 98 | ); 99 | runOnlyForDeploymentPostprocessing = 0; 100 | }; 101 | /* End PBXFrameworksBuildPhase section */ 102 | 103 | /* Begin PBXGroup section */ 104 | 1F8A80AC1C4EB0AF00971E9D = { 105 | isa = PBXGroup; 106 | children = ( 107 | 1F8A80D91C4EC8A900971E9D /* Documentation */, 108 | 1F8A80B71C4EB0AF00971E9D /* LaunchDaemon */, 109 | 1F8A80C91C4EB54300971E9D /* Client */, 110 | 1F8A80E81C4EDEA700971E9D /* Scripts */, 111 | 1F8A80B61C4EB0AF00971E9D /* Products */, 112 | ); 113 | sourceTree = ""; 114 | }; 115 | 1F8A80B61C4EB0AF00971E9D /* Products */ = { 116 | isa = PBXGroup; 117 | children = ( 118 | 1F8A80B51C4EB0AF00971E9D /* LaunchDaemon */, 119 | 1F8A80C81C4EB54300971E9D /* Client.app */, 120 | ); 121 | name = Products; 122 | sourceTree = ""; 123 | }; 124 | 1F8A80B71C4EB0AF00971E9D /* LaunchDaemon */ = { 125 | isa = PBXGroup; 126 | children = ( 127 | 1F8A80B81C4EB0AF00971E9D /* main.m */, 128 | 1F8A80C31C4EB3BE00971E9D /* daemon-Launchd.plist */, 129 | 1F8A80BF1C4EB0EB00971E9D /* Daemon.h */, 130 | 1F8A80C01C4EB0EB00971E9D /* Daemon.m */, 131 | 1F8A80C21C4EB27900971E9D /* Protocols.h */, 132 | ); 133 | path = LaunchDaemon; 134 | sourceTree = ""; 135 | }; 136 | 1F8A80C91C4EB54300971E9D /* Client */ = { 137 | isa = PBXGroup; 138 | children = ( 139 | 1F8A80CA1C4EB54300971E9D /* AppDelegate.h */, 140 | 1F8A80CB1C4EB54300971E9D /* AppDelegate.m */, 141 | 1F8A80D01C4EB54300971E9D /* Assets.xcassets */, 142 | 1F8A80D21C4EB54300971E9D /* MainMenu.xib */, 143 | 1F8A80D51C4EB54300971E9D /* Info.plist */, 144 | 1F8A80CD1C4EB54300971E9D /* Supporting Files */, 145 | ); 146 | path = Client; 147 | sourceTree = ""; 148 | }; 149 | 1F8A80CD1C4EB54300971E9D /* Supporting Files */ = { 150 | isa = PBXGroup; 151 | children = ( 152 | 1F8A80CE1C4EB54300971E9D /* main.m */, 153 | ); 154 | name = "Supporting Files"; 155 | sourceTree = ""; 156 | }; 157 | 1F8A80D91C4EC8A900971E9D /* Documentation */ = { 158 | isa = PBXGroup; 159 | children = ( 160 | 1F8A80EB1C4F004E00971E9D /* License */, 161 | 1F8A80EC1C4F005E00971E9D /* ReadMe */, 162 | ); 163 | name = Documentation; 164 | path = LaunchDaemon; 165 | sourceTree = ""; 166 | }; 167 | 1F8A80E81C4EDEA700971E9D /* Scripts */ = { 168 | isa = PBXGroup; 169 | children = ( 170 | 1F8A80ED1C4F022D00971E9D /* install.sh */, 171 | 1F8A80F31C4F087200971E9D /* uninstall.sh */, 172 | ); 173 | name = Scripts; 174 | path = LaunchDaemon; 175 | sourceTree = ""; 176 | }; 177 | /* End PBXGroup section */ 178 | 179 | /* Begin PBXNativeTarget section */ 180 | 1F8A80B41C4EB0AF00971E9D /* LaunchDaemon */ = { 181 | isa = PBXNativeTarget; 182 | buildConfigurationList = 1F8A80BC1C4EB0AF00971E9D /* Build configuration list for PBXNativeTarget "LaunchDaemon" */; 183 | buildPhases = ( 184 | 1F8A80B11C4EB0AF00971E9D /* Sources */, 185 | 1F8A80B21C4EB0AF00971E9D /* Frameworks */, 186 | 1F8A80B31C4EB0AF00971E9D /* CopyFiles */, 187 | ); 188 | buildRules = ( 189 | ); 190 | dependencies = ( 191 | ); 192 | name = LaunchDaemon; 193 | productName = LaunchDaemon; 194 | productReference = 1F8A80B51C4EB0AF00971E9D /* LaunchDaemon */; 195 | productType = "com.apple.product-type.tool"; 196 | }; 197 | 1F8A80C71C4EB54300971E9D /* Client */ = { 198 | isa = PBXNativeTarget; 199 | buildConfigurationList = 1F8A80D61C4EB54300971E9D /* Build configuration list for PBXNativeTarget "Client" */; 200 | buildPhases = ( 201 | 1F8A80C41C4EB54300971E9D /* Sources */, 202 | 1F8A80C51C4EB54300971E9D /* Frameworks */, 203 | 1F8A80C61C4EB54300971E9D /* Resources */, 204 | ); 205 | buildRules = ( 206 | ); 207 | dependencies = ( 208 | ); 209 | name = Client; 210 | productName = Client; 211 | productReference = 1F8A80C81C4EB54300971E9D /* Client.app */; 212 | productType = "com.apple.product-type.application"; 213 | }; 214 | /* End PBXNativeTarget section */ 215 | 216 | /* Begin PBXProject section */ 217 | 1F8A80AD1C4EB0AF00971E9D /* Project object */ = { 218 | isa = PBXProject; 219 | attributes = { 220 | LastUpgradeCheck = 0720; 221 | ORGANIZATIONNAME = Example; 222 | TargetAttributes = { 223 | 1F8A80B41C4EB0AF00971E9D = { 224 | CreatedOnToolsVersion = 7.2; 225 | }; 226 | 1F8A80C71C4EB54300971E9D = { 227 | CreatedOnToolsVersion = 7.2; 228 | }; 229 | 1F8A80E11C4EDA4F00971E9D = { 230 | CreatedOnToolsVersion = 7.2; 231 | }; 232 | 1F8A80EE1C4F083E00971E9D = { 233 | CreatedOnToolsVersion = 7.2; 234 | }; 235 | }; 236 | }; 237 | buildConfigurationList = 1F8A80B01C4EB0AF00971E9D /* Build configuration list for PBXProject "LaunchDaemon" */; 238 | compatibilityVersion = "Xcode 3.2"; 239 | developmentRegion = English; 240 | hasScannedForEncodings = 0; 241 | knownRegions = ( 242 | en, 243 | Base, 244 | ); 245 | mainGroup = 1F8A80AC1C4EB0AF00971E9D; 246 | productRefGroup = 1F8A80B61C4EB0AF00971E9D /* Products */; 247 | projectDirPath = ""; 248 | projectRoot = ""; 249 | targets = ( 250 | 1F8A80B41C4EB0AF00971E9D /* LaunchDaemon */, 251 | 1F8A80C71C4EB54300971E9D /* Client */, 252 | 1F8A80E11C4EDA4F00971E9D /* Install */, 253 | 1F8A80EE1C4F083E00971E9D /* Uninstall */, 254 | ); 255 | }; 256 | /* End PBXProject section */ 257 | 258 | /* Begin PBXResourcesBuildPhase section */ 259 | 1F8A80C61C4EB54300971E9D /* Resources */ = { 260 | isa = PBXResourcesBuildPhase; 261 | buildActionMask = 2147483647; 262 | files = ( 263 | 1F8A80D11C4EB54300971E9D /* Assets.xcassets in Resources */, 264 | 1F8A80D41C4EB54300971E9D /* MainMenu.xib in Resources */, 265 | ); 266 | runOnlyForDeploymentPostprocessing = 0; 267 | }; 268 | /* End PBXResourcesBuildPhase section */ 269 | 270 | /* Begin PBXShellScriptBuildPhase section */ 271 | 1F8A80E71C4EDA6300971E9D /* ShellScript */ = { 272 | isa = PBXShellScriptBuildPhase; 273 | buildActionMask = 2147483647; 274 | files = ( 275 | ); 276 | inputPaths = ( 277 | ); 278 | outputPaths = ( 279 | ); 280 | runOnlyForDeploymentPostprocessing = 0; 281 | shellPath = /bin/sh; 282 | shellScript = "sudo -A ${PROJECT_DIR}/Scripts/install.sh ${PROJECT_DIR} ${BUILT_PRODUCTS_DIR}"; 283 | }; 284 | 1F8A80F21C4F084200971E9D /* ShellScript */ = { 285 | isa = PBXShellScriptBuildPhase; 286 | buildActionMask = 2147483647; 287 | files = ( 288 | ); 289 | inputPaths = ( 290 | ); 291 | outputPaths = ( 292 | ); 293 | runOnlyForDeploymentPostprocessing = 0; 294 | shellPath = /bin/sh; 295 | shellScript = "sudo -A ${PROJECT_DIR}/Scripts/uninstall.sh"; 296 | }; 297 | /* End PBXShellScriptBuildPhase section */ 298 | 299 | /* Begin PBXSourcesBuildPhase section */ 300 | 1F8A80B11C4EB0AF00971E9D /* Sources */ = { 301 | isa = PBXSourcesBuildPhase; 302 | buildActionMask = 2147483647; 303 | files = ( 304 | 1F8A80B91C4EB0AF00971E9D /* main.m in Sources */, 305 | 1F8A80C11C4EB0EB00971E9D /* Daemon.m in Sources */, 306 | ); 307 | runOnlyForDeploymentPostprocessing = 0; 308 | }; 309 | 1F8A80C41C4EB54300971E9D /* Sources */ = { 310 | isa = PBXSourcesBuildPhase; 311 | buildActionMask = 2147483647; 312 | files = ( 313 | 1F8A80CF1C4EB54300971E9D /* main.m in Sources */, 314 | 1F8A80CC1C4EB54300971E9D /* AppDelegate.m in Sources */, 315 | ); 316 | runOnlyForDeploymentPostprocessing = 0; 317 | }; 318 | /* End PBXSourcesBuildPhase section */ 319 | 320 | /* Begin PBXTargetDependency section */ 321 | 1F8A80E61C4EDA5D00971E9D /* PBXTargetDependency */ = { 322 | isa = PBXTargetDependency; 323 | target = 1F8A80B41C4EB0AF00971E9D /* LaunchDaemon */; 324 | targetProxy = 1F8A80E51C4EDA5D00971E9D /* PBXContainerItemProxy */; 325 | }; 326 | /* End PBXTargetDependency section */ 327 | 328 | /* Begin PBXVariantGroup section */ 329 | 1F8A80D21C4EB54300971E9D /* MainMenu.xib */ = { 330 | isa = PBXVariantGroup; 331 | children = ( 332 | 1F8A80D31C4EB54300971E9D /* Base */, 333 | ); 334 | name = MainMenu.xib; 335 | sourceTree = ""; 336 | }; 337 | /* End PBXVariantGroup section */ 338 | 339 | /* Begin XCBuildConfiguration section */ 340 | 1F8A80BA1C4EB0AF00971E9D /* Debug */ = { 341 | isa = XCBuildConfiguration; 342 | buildSettings = { 343 | ALWAYS_SEARCH_USER_PATHS = NO; 344 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 345 | CLANG_CXX_LIBRARY = "libc++"; 346 | CLANG_ENABLE_MODULES = YES; 347 | CLANG_ENABLE_OBJC_ARC = YES; 348 | CLANG_WARN_BOOL_CONVERSION = YES; 349 | CLANG_WARN_CONSTANT_CONVERSION = YES; 350 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 351 | CLANG_WARN_EMPTY_BODY = YES; 352 | CLANG_WARN_ENUM_CONVERSION = YES; 353 | CLANG_WARN_INT_CONVERSION = YES; 354 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 355 | CLANG_WARN_UNREACHABLE_CODE = YES; 356 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 357 | CODE_SIGN_IDENTITY = "-"; 358 | COPY_PHASE_STRIP = NO; 359 | DEBUG_INFORMATION_FORMAT = dwarf; 360 | ENABLE_STRICT_OBJC_MSGSEND = YES; 361 | ENABLE_TESTABILITY = YES; 362 | GCC_C_LANGUAGE_STANDARD = gnu99; 363 | GCC_DYNAMIC_NO_PIC = NO; 364 | GCC_NO_COMMON_BLOCKS = YES; 365 | GCC_OPTIMIZATION_LEVEL = 0; 366 | GCC_PREPROCESSOR_DEFINITIONS = ( 367 | "DEBUG=1", 368 | "$(inherited)", 369 | ); 370 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 371 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 372 | GCC_WARN_UNDECLARED_SELECTOR = YES; 373 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 374 | GCC_WARN_UNUSED_FUNCTION = YES; 375 | GCC_WARN_UNUSED_VARIABLE = YES; 376 | MACOSX_DEPLOYMENT_TARGET = 10.11; 377 | MTL_ENABLE_DEBUG_INFO = YES; 378 | ONLY_ACTIVE_ARCH = YES; 379 | SDKROOT = macosx; 380 | }; 381 | name = Debug; 382 | }; 383 | 1F8A80BB1C4EB0AF00971E9D /* Release */ = { 384 | isa = XCBuildConfiguration; 385 | buildSettings = { 386 | ALWAYS_SEARCH_USER_PATHS = NO; 387 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 388 | CLANG_CXX_LIBRARY = "libc++"; 389 | CLANG_ENABLE_MODULES = YES; 390 | CLANG_ENABLE_OBJC_ARC = YES; 391 | CLANG_WARN_BOOL_CONVERSION = YES; 392 | CLANG_WARN_CONSTANT_CONVERSION = YES; 393 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 394 | CLANG_WARN_EMPTY_BODY = YES; 395 | CLANG_WARN_ENUM_CONVERSION = YES; 396 | CLANG_WARN_INT_CONVERSION = YES; 397 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 398 | CLANG_WARN_UNREACHABLE_CODE = YES; 399 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 400 | CODE_SIGN_IDENTITY = "-"; 401 | COPY_PHASE_STRIP = NO; 402 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 403 | ENABLE_NS_ASSERTIONS = NO; 404 | ENABLE_STRICT_OBJC_MSGSEND = YES; 405 | GCC_C_LANGUAGE_STANDARD = gnu99; 406 | GCC_NO_COMMON_BLOCKS = YES; 407 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 408 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 409 | GCC_WARN_UNDECLARED_SELECTOR = YES; 410 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 411 | GCC_WARN_UNUSED_FUNCTION = YES; 412 | GCC_WARN_UNUSED_VARIABLE = YES; 413 | MACOSX_DEPLOYMENT_TARGET = 10.11; 414 | MTL_ENABLE_DEBUG_INFO = NO; 415 | SDKROOT = macosx; 416 | }; 417 | name = Release; 418 | }; 419 | 1F8A80BD1C4EB0AF00971E9D /* Debug */ = { 420 | isa = XCBuildConfiguration; 421 | buildSettings = { 422 | PRODUCT_NAME = "$(TARGET_NAME)"; 423 | }; 424 | name = Debug; 425 | }; 426 | 1F8A80BE1C4EB0AF00971E9D /* Release */ = { 427 | isa = XCBuildConfiguration; 428 | buildSettings = { 429 | PRODUCT_NAME = "$(TARGET_NAME)"; 430 | }; 431 | name = Release; 432 | }; 433 | 1F8A80D71C4EB54300971E9D /* Debug */ = { 434 | isa = XCBuildConfiguration; 435 | buildSettings = { 436 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 437 | COMBINE_HIDPI_IMAGES = YES; 438 | INFOPLIST_FILE = Client/Info.plist; 439 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; 440 | PRODUCT_BUNDLE_IDENTIFIER = com.example.Client; 441 | PRODUCT_NAME = "$(TARGET_NAME)"; 442 | }; 443 | name = Debug; 444 | }; 445 | 1F8A80D81C4EB54300971E9D /* Release */ = { 446 | isa = XCBuildConfiguration; 447 | buildSettings = { 448 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 449 | COMBINE_HIDPI_IMAGES = YES; 450 | INFOPLIST_FILE = Client/Info.plist; 451 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; 452 | PRODUCT_BUNDLE_IDENTIFIER = com.example.Client; 453 | PRODUCT_NAME = "$(TARGET_NAME)"; 454 | }; 455 | name = Release; 456 | }; 457 | 1F8A80E31C4EDA4F00971E9D /* Debug */ = { 458 | isa = XCBuildConfiguration; 459 | buildSettings = { 460 | PRODUCT_NAME = "$(TARGET_NAME)"; 461 | }; 462 | name = Debug; 463 | }; 464 | 1F8A80E41C4EDA4F00971E9D /* Release */ = { 465 | isa = XCBuildConfiguration; 466 | buildSettings = { 467 | PRODUCT_NAME = "$(TARGET_NAME)"; 468 | }; 469 | name = Release; 470 | }; 471 | 1F8A80F01C4F083E00971E9D /* Debug */ = { 472 | isa = XCBuildConfiguration; 473 | buildSettings = { 474 | PRODUCT_NAME = "$(TARGET_NAME)"; 475 | }; 476 | name = Debug; 477 | }; 478 | 1F8A80F11C4F083E00971E9D /* Release */ = { 479 | isa = XCBuildConfiguration; 480 | buildSettings = { 481 | PRODUCT_NAME = "$(TARGET_NAME)"; 482 | }; 483 | name = Release; 484 | }; 485 | /* End XCBuildConfiguration section */ 486 | 487 | /* Begin XCConfigurationList section */ 488 | 1F8A80B01C4EB0AF00971E9D /* Build configuration list for PBXProject "LaunchDaemon" */ = { 489 | isa = XCConfigurationList; 490 | buildConfigurations = ( 491 | 1F8A80BA1C4EB0AF00971E9D /* Debug */, 492 | 1F8A80BB1C4EB0AF00971E9D /* Release */, 493 | ); 494 | defaultConfigurationIsVisible = 0; 495 | defaultConfigurationName = Release; 496 | }; 497 | 1F8A80BC1C4EB0AF00971E9D /* Build configuration list for PBXNativeTarget "LaunchDaemon" */ = { 498 | isa = XCConfigurationList; 499 | buildConfigurations = ( 500 | 1F8A80BD1C4EB0AF00971E9D /* Debug */, 501 | 1F8A80BE1C4EB0AF00971E9D /* Release */, 502 | ); 503 | defaultConfigurationIsVisible = 0; 504 | }; 505 | 1F8A80D61C4EB54300971E9D /* Build configuration list for PBXNativeTarget "Client" */ = { 506 | isa = XCConfigurationList; 507 | buildConfigurations = ( 508 | 1F8A80D71C4EB54300971E9D /* Debug */, 509 | 1F8A80D81C4EB54300971E9D /* Release */, 510 | ); 511 | defaultConfigurationIsVisible = 0; 512 | }; 513 | 1F8A80E21C4EDA4F00971E9D /* Build configuration list for PBXAggregateTarget "Install" */ = { 514 | isa = XCConfigurationList; 515 | buildConfigurations = ( 516 | 1F8A80E31C4EDA4F00971E9D /* Debug */, 517 | 1F8A80E41C4EDA4F00971E9D /* Release */, 518 | ); 519 | defaultConfigurationIsVisible = 0; 520 | }; 521 | 1F8A80EF1C4F083E00971E9D /* Build configuration list for PBXAggregateTarget "Uninstall" */ = { 522 | isa = XCConfigurationList; 523 | buildConfigurations = ( 524 | 1F8A80F01C4F083E00971E9D /* Debug */, 525 | 1F8A80F11C4F083E00971E9D /* Release */, 526 | ); 527 | defaultConfigurationIsVisible = 0; 528 | }; 529 | /* End XCConfigurationList section */ 530 | }; 531 | rootObject = 1F8A80AD1C4EB0AF00971E9D /* Project object */; 532 | } 533 | -------------------------------------------------------------------------------- /LaunchDaemon.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LaunchDaemon.xcodeproj/xcshareddata/xcschemes/Client.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /LaunchDaemon.xcodeproj/xcshareddata/xcschemes/Install.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 34 | 35 | 45 | 46 | 52 | 53 | 54 | 55 | 59 | 60 | 61 | 62 | 63 | 64 | 70 | 71 | 77 | 78 | 79 | 80 | 82 | 83 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /LaunchDaemon.xcodeproj/xcshareddata/xcschemes/LaunchDaemon.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 55 | 57 | 63 | 64 | 65 | 66 | 67 | 68 | 74 | 76 | 82 | 83 | 84 | 85 | 87 | 88 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /LaunchDaemon.xcodeproj/xcshareddata/xcschemes/Uninstall.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 34 | 35 | 45 | 46 | 52 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 70 | 71 | 72 | 73 | 75 | 76 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /LaunchDaemon.xcodeproj/xcuserdata/jeff.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | LaunchDaemon.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 1F8A80B41C4EB0AF00971E9D 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /LaunchDaemon/Daemon.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Written by Jeff Spooner 4 | 5 | */ 6 | 7 | #import 8 | #import "Protocols.h" 9 | 10 | @interface Daemon : NSObject 11 | 12 | - (id) init; 13 | 14 | - (void) start; 15 | // Begin listening for incoming XPC connections 16 | 17 | - (void) stop; 18 | // Stop listening for incoming XPC connections 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /LaunchDaemon/Daemon.m: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Written by Jeff Spooner 4 | 5 | If you're writing a system which has a daemon and multiple clients, particularly 6 | if your clients need to sync some state with the daemon from time to time, you'll 7 | want a way for the daemon to initiate contact. Having each client maintain an XPC 8 | listener, and making the daemon send a message to each client when something changes 9 | is not a very good or viable solution, using CFNotificationCenter is much better. 10 | When something changes the daemon can post a notification, and upon receipt the 11 | clients can send the appropriate message to the daemon over XPC. 12 | 13 | */ 14 | 15 | #import "Daemon.h" 16 | #import "notify.h" 17 | 18 | @interface Daemon () 19 | 20 | @property (nonatomic, strong, readwrite) NSXPCListener *listener; 21 | @property (nonatomic, readwrite) BOOL started; 22 | 23 | @property (nonatomic, readwrite) int count; 24 | 25 | @end 26 | 27 | 28 | @implementation Daemon 29 | 30 | - (id) init 31 | { 32 | // Launch daemons must configure their listener with the machServiceName initializer 33 | _listener = [[NSXPCListener alloc] initWithMachServiceName:daemonLabel]; 34 | _listener.delegate = self; 35 | 36 | _started = NO; 37 | 38 | return self; 39 | } 40 | 41 | 42 | - (void) start 43 | { 44 | assert(_started == NO); 45 | 46 | // Begin listening for incoming XPC connections 47 | [_listener resume]; 48 | 49 | _started = YES; 50 | } 51 | 52 | 53 | - (void) stop 54 | { 55 | assert(_started == YES); 56 | 57 | // Stop listening for incoming XPC connections 58 | [_listener suspend]; 59 | 60 | _started = NO; 61 | } 62 | 63 | 64 | #pragma mark - ExampleDaemonProtocol 65 | 66 | - (void) incrementCount 67 | { 68 | // Incrememnt the counter 69 | _count++; 70 | 71 | // Post a notification 72 | notify_post("com.example.daemonCounterDidChange"); 73 | } 74 | 75 | 76 | - (void) getCount:(void (^)(int))completion 77 | { 78 | // Pass our count back in the completion block 79 | completion(_count); 80 | } 81 | 82 | 83 | #pragma mark - NSXPCListenerDelegate 84 | 85 | - (BOOL) listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection 86 | { 87 | // Sanity checks 88 | assert(listener == _listener); 89 | assert(newConnection != nil); 90 | 91 | // Configure the incoming connection 92 | newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(ExampleDaemonProtocol)]; 93 | newConnection.exportedObject = self; 94 | 95 | // New connections always start in a suspended state 96 | [newConnection resume]; 97 | 98 | return YES; 99 | } 100 | 101 | 102 | @end 103 | -------------------------------------------------------------------------------- /LaunchDaemon/Protocols.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Written by Jeff Spooner 4 | 5 | The NSXPCConnection API requires that the server object in an XPC communication conform to 6 | some protocol. However no such restriction applies for the client object, as the client 7 | can pass a completion block to the server in the original message. This is a marked improvement 8 | over the old Distributed Objects API. 9 | 10 | Methods intended to be called over XPC must return void 11 | 12 | The protocol is is declared in a dedicated file as both the client and daemon need to know about it. 13 | 14 | */ 15 | 16 | 17 | static NSString *daemonLabel = @"com.example.daemon"; 18 | 19 | 20 | @protocol ExampleDaemonProtocol 21 | 22 | - (void) incrementCount; 23 | 24 | - (void) getCount:(void(^)(int count))completion; 25 | 26 | @end -------------------------------------------------------------------------------- /LaunchDaemon/daemon-Launchd.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Label 6 | com.example.daemon 7 | KeepAlive 8 | 9 | WaitForDebugger 10 | 11 | MachServices 12 | 13 | com.example.daemon 14 | 15 | 16 | ProgramArguments 17 | 18 | /Library/PrivilegedHelperTools/LaunchDaemon 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /LaunchDaemon/main.m: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Written by Jeff Spooner 4 | 5 | */ 6 | 7 | #import "Daemon.h" 8 | 9 | 10 | int main(int argc, const char *argv[]) 11 | { 12 | // Create an instance of the daemon and start it 13 | Daemon *daemon = [[Daemon alloc] init]; 14 | [daemon start]; 15 | 16 | // Loop indefinitely 17 | [[NSRunLoop currentRunLoop] run]; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /License: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Jeff Spooner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /ReadMe: -------------------------------------------------------------------------------- 1 | Sample XPC Launch Daemon ReadMe 2 | 3 | This Xcode project demonstrates a basic launch daemon which: 4 | -maintains an XPC listener to handle incoming connections from it's client(s) 5 | -signals it's clients via Darwin notifications 6 | 7 | The project includes: 8 | -a Daemon intended to be run via launchd. It maintains a count, and responds to XPC requests for its count and to increment its count. Upon incrementing its count, it notifies all interested parties via Darwin notifications. 9 | -a Client, from which you can ask the Daemon for it's current count, and ask it to increment it's count. 10 | -an install script, which copies the daemon's executable to /Library/PrivilegedHelperTools/, the Daemon's launchd.plist to /Library/LaunchDaemons/, and then loads the daemon via launchctl 11 | -an uninstall script, which unloads the daemon via launchctl, and removes the daemon's executable and launchd.plist 12 | 13 | To run the project: 14 | -run the Install scheme 15 | -run the Client scheme 16 | -click the button and witness XPC communication and Darwin notifications in action! 17 | -run the Uninstall scheme 18 | 19 | NOTE: You will need to set SUDO_ASKPASS to run the installation and uninstallation targets. 20 | 21 | The daemon needs to be launched via launchd if you want the XPC stuff to actually work. 22 | However, if you're writing a more complicated daemon, you will probably want to be able to debug the daemon with Xcode. 23 | To do this, you'll need to do the follwing: 24 | -modify daemon-Launchd.plist so the WaitForDebugger flag is set to YES 25 | -ensure the Daemon's Xcode scheme: 26 | -runs as Root 27 | -waits for the executable to be launched 28 | -modify install.sh so it does NOT load the daemon 29 | -run the Install scheme 30 | -run the Daemon scheme, and then load the daemon via launchctl 31 | -you've got to do it in this order, or the Xcode debugger will never attach 32 | 33 | Remember to set the WaitForDebugger flag to NO again before releasing anything! -------------------------------------------------------------------------------- /Scripts/install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | PROJECT_DIR=$1 4 | BUILT_PRODUCTS_DIR=$2 5 | 6 | # Copy the daemon's launchd.plist into /Library/LaunchDaemons 7 | for plist in daemon-Launchd.plist 8 | do 9 | trg=/Library/LaunchDaemons/com.example.daemon.plist 10 | cp ${PROJECT_DIR}/LaunchDaemon/${plist} ${trg} 11 | chown root:wheel ${trg} 12 | chmod 644 ${trg} 13 | done 14 | 15 | # Copy the daemon's executable into /Library/PrivilegedHelperTools 16 | for executable in LaunchDaemon 17 | do 18 | trg=/Library/PrivilegedHelperTools/${executable} 19 | cp ${BUILT_PRODUCTS_DIR}/${executable} ${trg} 20 | chown -R root:admin ${trg} 21 | done 22 | 23 | # Load the new daemon 24 | launchctl load /Library/LaunchDaemons/com.example.daemon.plist 25 | -------------------------------------------------------------------------------- /Scripts/uninstall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Unload the daemon 4 | launchctl unload /Library/LaunchDaemons/com.example.daemon.plist 5 | 6 | # Remove the daemon's launchd.plist 7 | rm /Library/LaunchDaemons/com.example.daemon.plist 8 | 9 | # Remove the daemon's executable 10 | rm /Library/PrivilegedHelperTools/LaunchDaemon --------------------------------------------------------------------------------