1/*
2 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include <launch.h>
8
9#include <TokenSpace.h>
10#include <util/KMessage.h>
11
12
13static port_id sLaunchDaemonPort = -1;
14
15
16port_id
17BPrivate::get_launch_daemon_port()
18{
19	if (sLaunchDaemonPort < 0) {
20		sLaunchDaemonPort = find_port(B_LAUNCH_DAEMON_PORT_NAME);
21
22		port_info info;
23		if (get_port_info(sLaunchDaemonPort, &info) == B_OK
24			&& info.team == find_thread(NULL)) {
25			// Make sure that the launch_daemon doesn't wait on itself
26			sLaunchDaemonPort = -1;
27			return -1;
28		}
29	}
30
31	return sLaunchDaemonPort;
32}
33
34
35status_t
36BPrivate::send_request_to_launch_daemon(KMessage& request, KMessage& reply)
37{
38	status_t status = request.SendTo(get_launch_daemon_port(),
39		B_PREFERRED_TOKEN, &reply);
40	if (status != B_OK)
41		return status;
42
43	return (status_t)reply.What();
44}
45
46
47status_t
48BPrivate::get_launch_data(const char* signature, KMessage& data)
49{
50	BPrivate::KMessage request(B_GET_LAUNCH_DATA);
51	request.AddString("name", signature);
52
53	return BPrivate::send_request_to_launch_daemon(request, data);
54}
55