Post

Replies

Boosts

Views

Activity

Issues with LaunchAgent and LaunchDaemon
I have this application that is divided in 3 parts. A server that handles all the networking code A agent that handles all System related code A manager (NSApplication) to interact with the other two processes. Goals All three process should be kept alive if they crash All three processes must not restart if the user quits them though the NSApplication They need to run during the login window. My current set up using LaunchD is as follows. My Server process plist (relevant part) saved in System/LaunchDaemons key>MachServices</key> <dict> <key>com.myCompany.Agent.xpc</key> <true/> <key>com.myCompany.Manager.xpc</key> <true/> </dict> <key>ProgramArguments</key> <array> <string>PathToExecutable</string> <string>-service</string> </array> <key>RunAtLoad</key> <false/> My agent plist (saved in System/LaunchAgent) <key>QueueDirectories</key> <array> <string>PathToDirectory</string> </array> <key>RunAtLoad</key> <false/> <key>ProgramArguments</key> <array> <string>PathToExecutable</string> <string>service</string> </array> my Manager app plist (saved in System/LaunchAgent) <key>LimitLoadToSessionType</key> <string>Aqua</string> <key>RunAtLoad</key> <false/> <key>ProgramArguments</key> <array> <string>PathToExecutable</string> </array> <key>MachServices</key> <dict> <key>com.myCompany.Agent.manager.xpc</key> <true/> </dict> Currently I have another app that saves a file to the path of the QueueDirectories which triggers the launch of my Agent which then triggers the Server and Manager by starting a XPC Connection. QueueDirectories keeps the Agent alive (and hence all other processes) til file is removed and processes are quited through the manager. XPC Connections Server listens for a connection from agent and manager Manager listens for a connection from agent and starts a connection with server Agent starts a connection with Manager and Server Agent and Manager are owned by the user and server by root The problems When I start Agent by saving a file in the QueueDirectories path and connect to the Server over xpc I end up with two Agents, one owned by the user (the one I expect) and one owned by root. But if I manually start the Agent I do not have that problem. As I mentioned before, the server listens for a connection from the Agent. How do I stop getting two instances? or what is a better way to approach this?
3
0
499
Jul ’24
Weird animation in NSCollectionViewCompositionalLayout
I'm implementing the drag and drop in a collection view, for the most part it works as expected but I have this weird bug that when I click on a item and start dragging it the item appears outside outside the view and it off from my mouse cursor. I have no idea what is causing this. This is how I accept the drop and call the func moveItem from the collection view. indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool { guard !indexPathsOfItemsBeingDragged.isEmpty else { return false } var acceptDrop = false for fromIndexPath in indexPathsOfItemsBeingDragged { var modelUpdated = false if fromIndexPath.section == indexPath.section { //moving items in same section modelUpdated = dataSource.moveItemIn(section: fromIndexPath.section, fromIndex: fromIndexPath.item, toIndex: indexPath.item) } else if fromIndexPath.section != indexPath.section{ //move items between sections modelUpdated = dataSource.moveItem(fromSection: fromIndexPath.section, fromIndex: fromIndexPath.item, toSection: indexPath.section, toIndex: indexPath.item) } if modelUpdated { //notify collection view if model was updated NSAnimationContext.current.duration = 0.25 collectionView.animator().moveItem(at: fromIndexPath, to: indexPath) acceptDrop = true } } return acceptDrop } This is how I set the constraints for my collection view. `NSLayoutConstraint.activate([moviesCollectionView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor), moviesCollectionView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor), moviesCollectionView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor), moviesCollectionView.bottomAnchor.constraint(equalTo: bottomAnchor)])` And while I'm at it if somebody could elaborate a bit more on this delegate function. pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? { let pasteBoardItem = NSPasteboardItem() pasteBoardItem.setString("\(indexPath.item)", forType: .string) return pasteBoardItem } The drag and drop is only locally but nothing works if I don't return a pasteboard item? and if needed, how do you go about posting a custom object? If somebody has an idea I would greatly appreciate it. Happy to post other code that would help with the debugging
0
0
439
Apr ’23
Unable to remove app from InputMonitoring pane
To remove my application from the Accessibility pane I do this in terminal: sudo tccutil reset ScreenCapture appsidentifier Which successfully removes the app from the Accessibility pane but when I try the same for InputMonitoring it does not works. sudo tccutil reset ScreenCapture appsidentifier I get this: Failed to reset InputMonitoring approval status for appIdentifier After a lot of research I’m unable to find anything on it. Thank you in advance.
2
0
705
Mar ’23
Catching SIGTERM in daemon
I have a process that I start and keep alive like this. ServerMain.shared.startFSM() CFRunLoopRun() Now I’m trying to react accordingly to when the computer is going to sleep, or shutting down so I’m trying to catch the SIGTERM signal as follows. private func setSIGTERMSignalHandler() { let signalSource = DispatchSource.makeSignalSource(signal: SIGTERM, queue: .main) signalSource.setEventHandler { self.signOut() } signalSource.resume() signTermSource = signalSource } However the event handler is not getting called in any circumstance. Is this the right track to catch them since it is a LaunchDaemon?
4
0
1k
Feb ’23
CGStreamDisplay and CGEvents for keyboard not working during LoginWindow in intel Mac
I have a remote application I'm working on. I have a LaunchAgent triggered by a plist stored in /Library/LaunchAgents that is in charge of injecting the mouse and keyboard events and in charge of capturing the monitor. When user is logged in everything works great but when I log out is when I run into issues. I'm certain my agent is running because I can receive and inject the mouse events but: I'm unable to capture the monitor, the CGDisplayStream init returns nil. I'm unable to post any keyboard events. The interesting part is that it has worked as expected sometimes but that is probably one out of 10 attempts maybe. Another interesting part is that I see this issue only in a intel Mac but it does not happen at all on my m1 Mac. I'm also certain that all security permissions have been granted to the Agent (Input Monitoring, Screen Capture, Accessibility) otherwise it would work when user is logged either. I'm not posting any code since that is not likely to be the issue since it "works" in some Macs and when user is logged in but happy to do so if we need to. Thank you in advance.
1
0
1k
Feb ’23
Stop LaunchAgent from restarting.
I have a Mac OS remote application that is made out of three parts. 1.- A LaunchDaemon that handles all of my network code and connects to my client 2.- A LaunchAgent to provides access to the window server to capture monitors, events, etc. 3.- A NSApplication to allow the user to control the LaunchDaemon I'm trying to get the LaunchAgent and LaunchDaemon to run only while my NSApp is running. LaunchDaemon - it uses machServices so I "control" it by starting an xpcConnection from my NSApplication. LaunchAgent - it uses QueueDirectories and I start it by having my NSApplication save a file to the QueueDirectories path. My NSApp and LaunchAgent both communicate with the LaunchDaemon via XPCConnection. Everything works as expected until I try quit my NSApp, LaunchDaemon and LaunchAgent. This is how I do it. User quits NSApp -> NSApp removes QueueDirectory file, sends a quit message to Daemon and it gets terminated -> Daemon sends a message to Agent then it terminates with exit(0) -> Agent receives the message and it terminates with exit(0) as well. Agent gets terminated and immediately launches again creating demand for my Daemon and starting it again. When I run launchctl blame on my Agent I get xpc event. which means that the xpc connection with my daemon is still creating demand somehow even after calling .invalidate() and deallocating the connection object.? Is there a specific way to terminated an XPCConnection? this is how I create my XPCConnectionListener: NSXPCListener(machServiceName: "myMachServiceName") and my connections let connection = NSXPCConnection("myMachServiceName", options: [.privileged]) connection.remoteObjectInterface = NSXPCInterface(with: myProtocol.self) connection.exportedInterface = NSXPCInterface(with: myProtocol.self) connection.exportedObject = exportedObject connection.interruptionHandler = interruptionHandler connection.invalidationHandler = invalidationHandler return connection So it seems that the issue is that my connections aren't fully terminated when I quit the processes? any pointers would be greatly appreciated.
2
0
1.2k
Jan ’23
capturing LoginScreen with LaunchDaemon
I'm working in a remote control application for Mac OS, and currently I have a LaunchDaemon working, I'm able to capture monitors, inject mouse and keyboard events during the user context but as soon as I log out my monitor capturing stops working and it is just stuck in this frame. But my connection to my client is still active and I'm able to login remotely without any video. My set up is LaunchDaemon that runs all the time and that is in charge of sending and receiving events as well as the monitor capturing / streaming. And then a LaunchAgent that provides the ui to interact with my daemon.
2
0
749
Nov ’22
AVCaptureSession and GrabThreadHandler error
I have an application where I use AVCaptureSession along with AVCaptureScreenInput to capture my monitors. Everything works as expected most of the time but sometimes there is a delay on getting the frames, it varies how long it takes but when that happens I see this error in the console. CMIO_Unit_Input_ASC.mm:3442:GrabThreadHandler Error: signalling end of data because the CG grab failed for 10 seconds, which usually means the display went to sleep I looked around and I wasn't able to find anything on that particular error, not I have been able to reproduce it or notice anything specific that could be causing it. NOTE: I'm currently implementing it using ScreenCaptureKit but in the mean time I need to try and fix this.
0
0
512
Sep ’22
AvCaptureScreenInput ignoring capturesCurso set false
I’m having an issue where AVCaptureScreenInput.capturesCursor set to false works as expected if pointer size is set to normal but If i change it to anything else in Accessibility > Display > Pointer size then the cursor shows in my screen captured even with capturesCursor still set to false. Anybody seen anything similar? i found this thread that there was a similar bug a while ago so it might be something related to this? [https://github.com/wulkano/kap/issues/477]
1
1
583
Sep ’22
Keep app "alive" when going from Login to user context.
I have a remote control application working in user context but I'm trying to implement "remote login". During the LoginWindow I'm able to "launch" the app, establish connection and login but as son as I click log in the connection is lost until the app gets relaunched in the user context. I'm reading about LaunchAgents, daemons and xpc services but it is all very confusing so I'm trying to get some pointers. Right now I save my plist in /Library/LaunchAgents with the key LimitLoadSessionType set to LoginWindow "pointing" to my apps executable. This gets my app running during the LoginWindow and I can remote login. Once I hit enter to login my app gets shutdown with no mercy until it gets launched in the user context by another LaunchAgent. I know that there is a way to keep my app alive ( I say alive but I'm not sure that's how it actually works) while it goes from Login to user context. Does anyone know the work flow for a process like this? how does a LaunchAgent daemon connects to a xpc service?
12
0
2.7k
Aug ’22
getting system wide cursor events possible?
I'm wondering if it is possible to get system wide NSEvent.cursorUpdate (or cursor events in any other form?) NSEvent.addGlobalMonitorForEvents(matching: .cursorUpdate, handler: cursorEventReceived(_:)) That isn't working and all I documentation and examples I can find are local and involving a the apps view but my app does not have any views and still need to get them if possible.
4
0
1.3k
Feb ’22
Keeping NSApplication during LoginWindow
I'm writing a Agent Application that records the Screen and I'm trying to keep the recording going even when the user logs out. I've been reading about LaunchAgents and LaunchDaemons. From what I understand a Prelogin LaunchAgent is my best bet since I need NSApplication to keep my process going and to access the screens. I'm only able to relaunch the app after login or to reopen the app when the os closes it. But the recording process is interrupted. Here is what I have as far as my LaunchAgent. My prelogin LaunchAgent (to open the app during the LoginWindow context) &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt; &lt;plist version="1.0"&gt; &lt;dict&gt; &lt;key&gt;Label&lt;/key&gt; &lt;string&gt;com.myApp.prelogin&lt;/string&gt; &lt;key&gt;LimitLoadToSessionType&lt;/key&gt; &lt;string&gt;LoginWindow&lt;/string&gt; &lt;key&gt;RunAtLoad&lt;/key&gt; &lt;/true&gt; &lt;key&gt;ProgramArguments&lt;/key&gt; &lt;array&gt; &lt;string&gt;/Library/myApp/myAgent0&lt;/string&gt; &lt;string&gt;service&lt;/key&gt; &lt;array&gt; &lt;key&gt;QueueDirectories&lt;/key&gt; &lt;array&gt; &lt;string&gt;/etc/myApp/service&lt;/string&gt; &lt;/array&gt; &lt;/dict&gt; &lt;/plist&gt; Per user launch agent (to keep the application open while a file exists in the path of QueDirectories) &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt; &lt;plist version="1.0"&gt; &lt;dict&gt; &lt;key&gt;Label&lt;/key&gt; &lt;string&gt;com.myApp.peruser&lt;/string&gt; &lt;key&gt;LimitLoadToSessionType&lt;/key&gt; &lt;string&gt;Aqua&lt;/string&gt; &lt;key&gt;RunAtLoad&lt;/key&gt; &lt;true/&gt; &lt;key&gt;ProgramArguments&lt;/key&gt; &lt;array&gt; &lt;string&gt;/Library/myApp/vmyAgent0&lt;/string&gt; &lt;string&gt;service&lt;/string&gt; &lt;/array&gt; &lt;key&gt;QueueDirectories&lt;/key&gt; &lt;array&gt; &lt;string&gt;/etc/myApp/service&lt;/string&gt; &lt;/array&gt; &lt;/dict&gt; &lt;/plist&gt; And then a daemon that I was suggested to add. &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt; &lt;plist version="1.0"&gt; &lt;dict&gt; &lt;key&gt;Label&lt;/key&gt; &lt;string&gt;com.myApp.server&lt;/string&gt; &lt;key&gt;ProgramArguments&lt;/key&gt; &lt;array&gt; &lt;string&gt;/Library/myApp/myAgent0&lt;/string&gt; &lt;string&gt;-service&lt;/string&gt; &lt;/array&gt; &lt;key&gt;QueueDirectories&lt;/key&gt; &lt;array&gt; &lt;string&gt;/etc/myApp/service&lt;/string&gt; &lt;/array&gt; &lt;/dict&gt; &lt;/plist&gt; Currently the application get closed down when the user logs out and gets reopened when the user logs back in or every time I close it (until I remove the file from QueueDirectories). I'm not sure if it gets open during LoginWindow but I don't see any recordings of that so I don't think it does. I know it is possible since VNC viewer does it (you can remotely log into your Mac). I'm not even sure I'm on the right track and I found this other question which tells me I'm on the wrong side using NSApplication for something like this? I'm in need of confirmations lol. Thank you in advance.
11
0
2k
Oct ’21
Keeping command line tool alive and still get mouse and keyboard events
I have swift dylib that I call from a simple cSharp program through mono. I need the dylib to stay alive and listen to keyboard, mouse, application events as well as receiving commands to move mouse, etc. Looking online I found some suggestion to keep the process running and while they do keep the process alive I'm unable to receive the events I add monitors for. NSEvent.addGlobalMonitorForEvents(matching: mEvent.eventMask, handler: globalEventCallBack" I have tried Runloop.main.run DispatchGroup, Semaphore and while I understand why some don't work (they block the main thread and that's where the events are delivered) I don't understand others. So here I'm in search of suggestions of how to keep my process alive and still get the events.
10
0
3.0k
May ’21