I'm looking for a way to have my program started when user logins or when system boots up. The program needs to have root privilege and it needs to display some UI stuff on the top menu bar.
I have tried using launchd to start it as daemon and agents. The problem is daemons can't have UI. And agents are run as the user so it doesn't have root privilege.
I also tried having a daemon to call a script using setsid() to have my program running in a different process group but that didn't help either. It just can't show any UI stuff.
I am new to OSX and really need some help about this.
As you've correctly identified, a daemon can't display UI. What you need is a separate program that communicates with the daemon to provide the required UI.
There are various methods for the communication. If the design is suitable, the preferred method is to use XPC services, in which case, you'd create your main GUI app and make the daemon an XPC service of that application.
Alternatively, the GUI app can be a Launch Agent, so it is launched on login and you then need to provide the communication between the two processes. There are several methods to do this, such as using Distributed Notifications, TCP Sockets and Streams or local sockets
Whichever method you choose, in order for the daemon to provide UI, you must use a separate process.