19d69dc09SAxel Dörfler/*
29d69dc09SAxel Dörfler * Copyright 2015, Axel D��rfler, axeld@pinc-software.de.
39d69dc09SAxel Dörfler * Distributed under the terms of the MIT License.
49d69dc09SAxel Dörfler */
59d69dc09SAxel Dörfler
69d69dc09SAxel Dörfler
79d69dc09SAxel Dörfler#include <launch.h>
89d69dc09SAxel Dörfler
99d69dc09SAxel Dörfler#include <TokenSpace.h>
109d69dc09SAxel Dörfler#include <util/KMessage.h>
119d69dc09SAxel Dörfler
129d69dc09SAxel Dörfler
139d69dc09SAxel Dörflerstatic port_id sLaunchDaemonPort = -1;
149d69dc09SAxel Dörfler
159d69dc09SAxel Dörfler
169d69dc09SAxel Dörflerport_id
179d69dc09SAxel DörflerBPrivate::get_launch_daemon_port()
189d69dc09SAxel Dörfler{
19ef462ab5SAxel Dörfler	if (sLaunchDaemonPort < 0) {
209d69dc09SAxel Dörfler		sLaunchDaemonPort = find_port(B_LAUNCH_DAEMON_PORT_NAME);
219d69dc09SAxel Dörfler
22ef462ab5SAxel Dörfler		port_info info;
23ef462ab5SAxel Dörfler		if (get_port_info(sLaunchDaemonPort, &info) == B_OK
24ef462ab5SAxel Dörfler			&& info.team == find_thread(NULL)) {
25ef462ab5SAxel Dörfler			// Make sure that the launch_daemon doesn't wait on itself
26ef462ab5SAxel Dörfler			sLaunchDaemonPort = -1;
27ef462ab5SAxel Dörfler			return -1;
28ef462ab5SAxel Dörfler		}
29ef462ab5SAxel Dörfler	}
30ef462ab5SAxel Dörfler
319d69dc09SAxel Dörfler	return sLaunchDaemonPort;
329d69dc09SAxel Dörfler}
339d69dc09SAxel Dörfler
349d69dc09SAxel Dörfler
359d69dc09SAxel Dörflerstatus_t
369d69dc09SAxel DörflerBPrivate::send_request_to_launch_daemon(KMessage& request, KMessage& reply)
379d69dc09SAxel Dörfler{
389d69dc09SAxel Dörfler	status_t status = request.SendTo(get_launch_daemon_port(),
399d69dc09SAxel Dörfler		B_PREFERRED_TOKEN, &reply);
409d69dc09SAxel Dörfler	if (status != B_OK)
419d69dc09SAxel Dörfler		return status;
429d69dc09SAxel Dörfler
439d69dc09SAxel Dörfler	return (status_t)reply.What();
449d69dc09SAxel Dörfler}
459d69dc09SAxel Dörfler
469d69dc09SAxel Dörfler
479d69dc09SAxel Dörflerstatus_t
489d69dc09SAxel DörflerBPrivate::get_launch_data(const char* signature, KMessage& data)
499d69dc09SAxel Dörfler{
509d69dc09SAxel Dörfler	BPrivate::KMessage request(B_GET_LAUNCH_DATA);
519d69dc09SAxel Dörfler	request.AddString("name", signature);
529d69dc09SAxel Dörfler
539d69dc09SAxel Dörfler	return BPrivate::send_request_to_launch_daemon(request, data);
549d69dc09SAxel Dörfler}
55