4

I have a few questions regarding daemons. Indeed, even the macos developer center has limited information resources.

I want to develop an application daemon that runs after system boot without login.

a) a Daemon; Is it a simple console application combined with a plist? Because there are almost no tutorials on daemon development related to Xcode. If there is a code sample reference, can you share it here?

b) Can daemons be downloaded from the app store? Because there must be a software that I can offer to everyone through the App Store. Is the installation automatic like other app store apps? If anyone has experience and can share it, I would be very grateful.

c) I am working on an API related to mirroring the screen to Android phone. Do you think a daemon has full access to WiFi/BLE and screen capture APIs?

I would be very happy to hear your suggestions.

Alexander
  • 59,041
  • 12
  • 98
  • 151
Pınar
  • 81
  • 4

1 Answers1

3

I've made a launch daemon in the past, for the purpose of making a privileged helper tool with SMBless. I can share some of my experience.

A

a Daemon; Is it a simple console application combined with a plist? Because there are almost no tutorials on daemon development related to xcode. If there is a code sample reference, can you share it here?

Here are some resources that I found useful:

  1. Woody's Cocoa: implement a privileged Helper. This article covers the low-level, step by step process of making a launch daemon and launching it as a privileged helper tool. If you have no need for privileged execution, the steps would be much the same, but without the SMJobBless parts.
  2. SwiftAuthorizationSample which show cases SecureXPC (a framework for Swifty, Codable-based XPC communication) and Blessed (a framework for a Swifty, modern interface to SMJobBless and the AuthorizationServices framework). It handles a lot of the complexity from #1.
  3. Apple's Daemons and Services Programming Guide

B

Can daemons be downloaded from the app store? Because there must be a software that I can offer to everyone through the app store. Is the installation automatic like other app store apps? If anyone has experience and can share it, I would be very grateful.

No. You would distribute them as part of an app, and make your app install them when required.

I am working on an api related to mirroring the screen to android phone. Do you think a daemon has full access to WiFi/BLE and screen capture APIs?

WiFi certainly, but I'm not sure about the screen capture APIs. One of the differences between launch agents and daemons (IIRC), is that only launch agents can connect to the window server, which I assume is necessary for the screen capture APIs.

From Technical Note TN2083 – Daemons and Agents:

Daemons

A daemon is a program that runs in the background as part of the overall system (that is, it is not tied to a particular user). A daemon cannot display any GUI; more specifically, it is not allowed to connect to the window server. A web server is the perfect example of a daemon.

...

Agents

An agent is a process that runs in the background on behalf of a particular user. Agents are useful because they can do things that daemons can't, like reliably access the user's home directory or connect to the window server. A calendar monitoring program is a good example of an agent because:

Alexander
  • 59,041
  • 12
  • 98
  • 151
  • My guess is that the screen capture API seems to be independent of the window server. It's like the screen capture tool is a background service as well as a kernel service. But I can't be sure until I try it. Other than that, does app+daemon(console+plist) have to be served in a single app for the app store? Do we basically need to develop a console application to develop a daemon? (Console application that can be launched with plist) – Pınar Mar 20 '22 at 19:36
  • "does app+daemon(console+plist) have to be served in a single app for the app store?" not necessarily, but why woudln't you want them all in one? "Do we basically need to develop a console application to develop a daemon? (Console application that can be launched with plist)" Yep. You then copy that *command line tool* (that's the exact lingo from Xcode, not "console application") into your main App's bundle, and have your app install it at runtime. – Alexander Mar 20 '22 at 19:40
  • Hello again, I would like to get your opinion as I am inexperienced. On macOS, are the control icons (battery, time machine, sound, and so on) at the top right of the screen an XPS, a daemon, or part of an application connected to the window server? If we want to make such an icon to use as a shortcut to a windowless application, is it a non-window application or an xps ? – Pınar Mar 20 '22 at 19:50
  • You keep saying "xps", but I think you mean "XPC service". XPC services are headless processes (no UI) which are launched by an application to do some run. The only means of communication between an app and an XPC service is over the XPC inter-process communication APIs (which has a [`C` API](https://www.unix.com/man-page/osx/3/xpc/), a [first-party Objective C wrapper (NSXPCConnection)](https://developer.apple.com/documentation/foundation/nsxpcconnection) and a [third-party Swift wrapper (SecureXPC)](https://github.com/trilemma-dev/SecureXPC)). – Alexander Mar 20 '22 at 19:55
  • Those icons you're talking about are menu bar apps. They're still formally "apps", they just happen to have no dock icon, menu bar or regular windows. They use the [`NSStatusItem`](https://developer.apple.com/documentation/appkit/nsstatusitem) APIs to define their icons, and then custom popover-style windows to present their UI when clicked. – Alexander Mar 20 '22 at 19:57
  • First of all, thank you very much for your answers. Yes XPC :) Are the "apps" in the top right corner configured by the "plist" to start automatically on login, just like in a daemon/xpc? So in this case, is it also a plist declaration that automatically launches these applications on login? – Pınar Mar 20 '22 at 20:18
  • "Are the "apps" in the top right corner configured by the "plist" to start automatically on login, just like in a daemon/xpc?" I'm not sure, I suspect so. If I had to guess, they're probably just launch agents installed on the current user. – Alexander Mar 20 '22 at 20:33