summaryrefslogtreecommitdiff
path: root/client/src/muse_client.c
blob: 1be5e7463829c89a33ecb5d7926307e573804e23 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * muse-client
 *
 * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: YoungHun Kim <yh8004.kim@samsung.com>
 *
 * 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 "muse_client.h"
#include "muse_core_internal.h"

#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "MUSED_CLIENT"

GMutex g_muse_client_mutex;
GHashTable *g_muse_client_table;
int g_muse_client_table_id;

const char *UDS_files[MUSE_CHANNEL_MAX] = {MUSE_SOCK_FILE0, MUSE_SOCK_FILE1};

#ifdef MUSE_USE_CLIENT_SIGHANDLER
/* signal handler */
struct sigaction muse_client_int_old_action;
struct sigaction muse_client_abrt_old_action;
struct sigaction muse_client_segv_old_action;
struct sigaction muse_client_term_old_action;
struct sigaction muse_client_sys_old_action;

static gboolean _muse_client_table_remove_func(gpointer key, gpointer value, gpointer user_data);
static void _muse_client_sigaction(int signo, siginfo_t *siginfo, void *context);
static void _muse_client_constructor(void) __attribute__((constructor));
#endif

static int _muse_client_new(muse_channel_e channel);
static void _muse_client_table_new(void);
static gpointer _muse_client_get_fd_ptr(int sock_fd);

#ifdef MUSE_USE_CLIENT_SIGHANDLER
static gboolean _muse_client_table_remove_func(gpointer key, gpointer value, gpointer user_data)
{
	int sock_fd = GPOINTER_TO_INT(key);

	LOGW("close muse connection fd %d", sock_fd);

	muse_core_connection_close(sock_fd);

	return TRUE;
}

static void _muse_client_sigaction(int signo, siginfo_t *siginfo, void *context)
{
	struct sigaction *old_action = NULL;
	pid_t my_pid = getpid();

	LOGE("Enter - signo [%d], pid [%d]", signo, my_pid);

	/* close all opened fds */
	g_mutex_lock(&g_muse_client_mutex);

	g_hash_table_foreach_remove(g_muse_client_table, _muse_client_table_remove_func, NULL);

	g_mutex_unlock(&g_muse_client_mutex);

	/* call old signal handler */
	switch (signo) {
	case SIGINT:
		old_action = &muse_client_int_old_action;
		break;
	case SIGABRT:
		old_action = &muse_client_abrt_old_action;
		break;
	case SIGSEGV:
		old_action = &muse_client_segv_old_action;
		break;
	case SIGTERM:
		old_action = &muse_client_term_old_action;
		break;
	case SIGSYS:
		old_action = &muse_client_sys_old_action;
		break;
	default:
		LOGE("unhandled signal %d", signo);
		return;
	}

	if (old_action->sa_sigaction)
		old_action->sa_sigaction(signo, siginfo, context);
	else
		sigaction(signo, old_action, NULL);

	LOGE("Leave");

	return;
}

static void _muse_client_constructor(void)
{
	struct sigaction muse_client_action;
	muse_client_action.sa_sigaction = _muse_client_sigaction;
	muse_client_action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;

	LOGI("Enter");

	sigemptyset(&muse_client_action.sa_mask);

	sigaction(SIGINT, &muse_client_action, &muse_client_int_old_action);
	sigaction(SIGABRT, &muse_client_action, &muse_client_abrt_old_action);
	sigaction(SIGSEGV, &muse_client_action, &muse_client_segv_old_action);
	sigaction(SIGTERM, &muse_client_action, &muse_client_term_old_action);
	sigaction(SIGSYS, &muse_client_action, &muse_client_sys_old_action);

	LOGI("Leave");

	return;
}
#endif

static int _muse_client_new(muse_channel_e channel)
{
	struct sockaddr_un address;
	int len, ret = MUSE_ERR;
	int sockfd;
	char err_msg[MUSE_MAX_MSG_LEN] = {'\0',};

	LOGI("Enter");
	g_return_val_if_fail(channel < MUSE_CHANNEL_MAX, MM_ERROR_INVALID_ARGUMENT);

	/*Create socket*/
	if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
		strerror_r(errno, err_msg, MUSE_MAX_MSG_LEN);
		LOGE("[socket failure] sock: %s", err_msg);
		return ret;
	}
	LOGI("sockfd: %d", sockfd);

	if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) < 0) {
		strerror_r(errno, err_msg, MUSE_MAX_MSG_LEN);
		LOGE("unable to set on ctrls socket fd %d: %s", sockfd, err_msg);
		close(sockfd);
		return ret;
	}
	LOGD("fcntl");

	memset(&address, 0, sizeof(address));
	address.sun_family = AF_UNIX;
	strncpy(address.sun_path, UDS_files[channel], sizeof(address.sun_path));
	len = sizeof(address);

	if (muse_core_set_nonblocking(sockfd, false) != MM_ERROR_NONE)
		LOGE("Error - fd (%d) set blocking", sockfd);

	if ((ret = connect(sockfd, (struct sockaddr *)&address, len)) < 0) {
		strerror_r(errno, err_msg, MUSE_MAX_MSG_LEN);
		LOGE("[Critical Error] connect failure : %s", err_msg);
		close(sockfd);
		return ret;
	}

	g_mutex_lock(&g_muse_client_mutex);

	g_hash_table_insert(g_muse_client_table, GINT_TO_POINTER(sockfd), GINT_TO_POINTER(g_muse_client_table_id++));

	g_mutex_unlock(&g_muse_client_mutex);

	LOGI("Leave");
	return sockfd;
}

static void _muse_client_table_new(void)
{
	g_mutex_lock(&g_muse_client_mutex);

	if (!g_muse_client_table) {
		g_muse_client_table_id = 1;
		g_muse_client_table = g_hash_table_new(g_direct_hash, g_direct_equal);
		LOGD("client table : %p", g_muse_client_table);
	}

	g_mutex_unlock(&g_muse_client_mutex);
}

static gpointer _muse_client_get_fd_ptr(int sock_fd)
{
	g_return_val_if_fail(g_muse_client_table, NULL);

	return g_hash_table_lookup(g_muse_client_table, GINT_TO_POINTER(sock_fd));
}

int muse_client_new(void)
{
	_muse_client_table_new();
	return _muse_client_new(MUSE_CHANNEL_MSG);
}

int muse_client_new_data_ch(void)
{
	return _muse_client_new(MUSE_CHANNEL_DATA);
}

int muse_client_close(int sock_fd)
{
	int ret = MM_ERROR_NONE;

	ret = muse_core_connection_close(sock_fd);
	if (ret != MM_ERROR_NONE) {
		LOGE("close connection failed 0x%x", ret);
		return ret;
	}

	/* remove fd from table */
	g_mutex_lock(&g_muse_client_mutex);

	if (g_hash_table_remove(g_muse_client_table, GINT_TO_POINTER(sock_fd)))
		LOGD("success : remove fd %d from table", sock_fd);
	else
		LOGW("fail : remove fd %d from table[%p]", sock_fd, g_muse_client_table);

	g_mutex_unlock(&g_muse_client_mutex);

	return MM_ERROR_NONE;
}

int muse_client_ipc_push_data(int sock_fd, const char *data, int size, uint64_t data_id)
{
	char err_msg[MUSE_MAX_MSG_LEN] = {'\0',};
	muse_recv_data_head_t header;
	int sended_len = 0;

	g_return_val_if_fail(data, MM_ERROR_INVALID_ARGUMENT);

	header.marker = MUSE_DATA_HEAD;
	header.size = size;
	header.id = data_id;

	LOGD("[%d] header.marker : %x header.size : %d header.id : %"G_GUINT64_FORMAT"",
					sock_fd, header.marker, header.size, header.id);

	if ((sended_len = send(sock_fd, &header, sizeof(muse_recv_data_head_t), 0)) < 0) {
		strerror_r(errno, err_msg, MUSE_MAX_MSG_LEN);
		LOGE("[%d] fail to send msg (%s) %d", sock_fd, err_msg, errno);
	}

	if ((sended_len += send(sock_fd, data, size, 0)) < 0) {
		strerror_r(errno, err_msg, MUSE_MAX_MSG_LEN);
		LOGE("[%d] fail to send msg (%s) %d", sock_fd, err_msg, errno);
	}

	return sended_len;
}

int muse_client_get_fd_id_value(int sock_fd)
{
	gpointer sock_id_ptr = _muse_client_get_fd_ptr(sock_fd);

	g_return_val_if_fail(sock_id_ptr, MUSE_ERR);

	return GPOINTER_TO_INT(sock_id_ptr);
}

bool muse_client_check_fd_id_value(int sock_fd, int sock_id)
{
	gpointer sock_id_ptr = _muse_client_get_fd_ptr(sock_fd);

	g_return_val_if_fail(sock_id_ptr, false);

	if (sock_id == GPOINTER_TO_INT(sock_id_ptr))
		return true;
	else
		return false;
}