[launchd-dev] Listening socket and fast user switching

Dave Zarzycki zarzycki at apple.com
Thu May 22 12:01:05 PDT 2008


Dave,

The Apple way to solve this problem is to use Bonjour with a dynamic  
port assignment. Just use port zero, and set Bonjour to true like so:

	<key>Sockets</key>
	<dict>
		<key>Listeners</key>
		<dict>
			<key>Bonjour</key>
			<true/>
			<key>SockServiceName</key>
			<string>0</string>
		</dict>
	</dict>

davez



On May 22, 2008, at 11:38 AM, Dave Dribin wrote:

> Hello,
>
> I'm writing a launchd agent that opens a TCP listening socket.
> Ideally, I'd like launchd to open and listen to the socket via
> SockServiceName so my agent only gets launched as-needed.
>
> The problem is I'd like to listen to the same port (e.g. 12345), no
> matter which user is logged in.  This is fine when there's only one
> user logged in, but as soon as fast user switching comes into play,
> the socket gets an "address already in use" when the second user logs
> in.
>
> This is to be expected, I suppose.  But is there any way to have
> launchd only listen to the socket when the user is the "active" user,
> and have it *not* listen when the user gets switched out, due to fast
> user switching?
>
> At first glance the LimitLoadToSessionType looked promising, but it
> doesn't look like any of those will do that.
>
> The solution I'm currently entertaining uses launchd to always launch
> my agent, and have my agent do the socket listening.  I can then use
> the user switch notification to close down the listening port when the
> session is deactivated and open it when activated.
>
> Thanks,
>
> -Dave
>
> _______________________________________________
> launchd-dev mailing list
> launchd-dev at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/launchd-dev



More information about the launchd-dev mailing list