summaryrefslogtreecommitdiff
path: root/src/dbus-util.c
blob: ba47cbf3fc3238dfc5be68aa8da17325fbac598f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <E_DBus.h>
#include <dbus/dbus-glib.h>

#include "starter.h"
#include "starter-util.h"
#include "util.h"

#define DBUS_HOME_BUS_NAME "org.tizen.coreapps.home"
#define DBUS_HOME_RAISE_PATH "/Org/Tizen/Coreapps/home/raise"
#define DBUS_HOME_RAISE_INTERFACE DBUS_HOME_BUS_NAME".raise"
#define DBUS_HOME_RAISE_MEMBER "homeraise"

#define DEVICED_BUS_NAME       "org.tizen.system.deviced"
#define DEVICED_OBJECT_PATH    "/Org/Tizen/System/DeviceD"
#define DEVICED_INTERFACE_NAME DEVICED_BUS_NAME

#define DEVICED_PATH		DEVICED_OBJECT_PATH"/Process"
#define DEVICED_INTERFACE	DEVICED_INTERFACE_NAME".Process"

#define DEVICED_SET_METHOD	"oomadj_set"

#define DBUS_REPLY_TIMEOUT (120 * 1000)

static struct _info {
	DBusConnection *connection;
} s_info = {
	.connection = NULL,
};

static DBusConnection *_dbus_connection_get(void) {
	if (s_info.connection == NULL) {
		_W("no connection for dbus. get dbus connection");
		DBusError derror;
		DBusConnection *connection = NULL;

		dbus_error_init(&derror);
		connection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &derror);
		if (connection == NULL) {
			_E("Failed to get dbus connection:%s", derror.message);
			dbus_error_free(&derror);
			return NULL;
		}
		dbus_connection_setup_with_g_main(connection, NULL);
		dbus_error_free(&derror);

		s_info.connection = connection;
	}

	return s_info.connection;
}

static int _append_variant(DBusMessageIter *iter, const char *sig, char *param[])
{
	char *ch;
	int i;
	int int_type;
	uint64_t int64_type;

	if (!sig || !param)
		return 0;

	for (ch = (char*)sig, i = 0; *ch != '\0'; ++i, ++ch) {
		switch (*ch) {
		case 'i':
			int_type = atoi(param[i]);
			dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &int_type);
			break;
		case 'u':
			int_type = atoi(param[i]);
			dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &int_type);
			break;
		case 't':
			int64_type = atoi(param[i]);
			dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT64, &int64_type);
			break;
		case 's':
			dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &param[i]);
			break;
		default:
			return -EINVAL;
		}
	}

	return 0;
}

static int _invoke_dbus_method_sync(const char *dest, const char *path,
		const char *interface, const char *method,
		const char *sig, char *param[])
{
	DBusMessage *msg;
	DBusMessageIter iter;
	DBusMessage *reply;
	DBusError err;
	int r, ret;

	DBusConnection *conn = NULL;

	conn = (DBusConnection *)_dbus_connection_get();
	if (!conn) {
		_E("dbus_bus_get error");
		return -1;
	}

	msg = dbus_message_new_method_call(dest, path, interface, method);
	if (!msg) {
		_E("dbus_message_new_method_call(%s:%s-%s)", path, interface, method);
		return -EBADMSG;
	}

	dbus_message_iter_init_append(msg, &iter);
	r = _append_variant(&iter, sig, param);
	if (r < 0) {
		_E("append_variant error(%d)", r);
		dbus_message_unref(msg);
		return -EBADMSG;
	}

	dbus_error_init(&err);

	reply = dbus_connection_send_with_reply_and_block(conn, msg, DBUS_REPLY_TIMEOUT, &err);
	dbus_message_unref(msg);
	if (!reply) {
		_E("dbus_connection_send error(%s:%s)", err.name, err.message);
		dbus_error_free(&err);
		return -EBADMSG;
	}

	return reply;
}

static int _dbus_message_send(const char *path, const char *interface, const char *member)
{
	int ret = 0;
	DBusMessage *msg = NULL;
	DBusConnection *conn = NULL;

	conn = (DBusConnection *)_dbus_connection_get();
	if (!conn) {
		_E("dbus_bus_get error");
		return -1;
	}

	msg = dbus_message_new_signal(path, interface, member);
	if (!msg) {
		_E("dbus_message_new_signal(%s:%s-%s)", path, interface, member);
		return -1;
	}

	ret = dbus_connection_send(conn, msg, NULL); //async call
	dbus_message_unref(msg);
	if (ret != TRUE) {
		_E("dbus_connection_send error(%s:%s-%s)", path, interface, member);
		return -ECOMM;
	}
	_D("dbus_connection_send, ret=%d", ret);
	return 0;
}

void starter_dbus_home_raise_signal_send(void)
{
	int ret = 0;

	ret = _dbus_message_send(
			DBUS_HOME_RAISE_PATH,
			DBUS_HOME_RAISE_INTERFACE,
			DBUS_HOME_RAISE_MEMBER);
	_E("Sending HOME RAISE signal, result:%d", ret);
}

int starter_dbus_set_oomadj(int pid, int oom_adj_value)
{
	if(pid <= 0){
		_E("Pid is invalid");
		return -1;
	}
	DBusError err;
	DBusMessage *msg;
	char *pa[4];
	char buf1[16];
	char buf2[16];
	int ret, val;

	snprintf(buf1, sizeof(buf1), "%d", pid);
	snprintf(buf2, sizeof(buf2), "%d", oom_adj_value);

	pa[0] = DEVICED_SET_METHOD;
	pa[1] = "2";
	pa[2] = buf1;
	pa[3] = buf2;

	msg = _invoke_dbus_method_sync(DEVICED_BUS_NAME, DEVICED_PATH, DEVICED_INTERFACE, DEVICED_SET_METHOD, "siss", pa);
	if (!msg)
		return -EBADMSG;

	dbus_error_init(&err);

	ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
	if (!ret) {
		_E("no message : [%s:%s]", err.name, err.message);
		val = -EBADMSG;
	}

	dbus_message_unref(msg);
	dbus_error_free(&err);

	_D("%s-%s : %d", DEVICED_INTERFACE, pa[0], val);
	return val;
}