summaryrefslogtreecommitdiff
path: root/server/thumb-server-internal.c
blob: 4f1e5f9fd49b0199389f5cee861f10c8c4e08212 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * media-thumbnail-server
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Hyunjun Ko <zzoon.ko@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 "thumb-server-internal.h"
#include "media-thumb-util.h"
#include "media-thumb-debug.h"
#include "media-thumbnail.h"

#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <tzplatform_config.h>

#ifdef LOG_TAG
#undef LOG_TAG
#endif

#define LOG_TAG "MEDIA_THUMBNAIL_SERVER"
#define THUMB_DEFAULT_WIDTH 320
#define THUMB_DEFAULT_HEIGHT 240
#define THUMB_COMM_SOCK_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcomm.socket")
#define THUMB_EMPTY_STR ""

static int __thumbnail_get_data(const char *origin_path, char *thumb_path)
{
	int err = MS_MEDIA_ERR_NONE;
	int file_type = THUMB_NONE_TYPE;

	thumb_retvm_if(!origin_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Original path is null");
	thumb_retvm_if(!g_file_test(origin_path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "Original path(%s) does not exist", origin_path);
	thumb_dbg_slog("Origin path : %s", origin_path);

	file_type = _media_thumb_get_file_type(origin_path);

	if (file_type == THUMB_IMAGE_TYPE) {
		err = create_image_thumbnail_to_file(origin_path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
	} else if (file_type == THUMB_VIDEO_TYPE) {
		err = create_video_thumbnail_to_file(origin_path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
	} else {
		thumb_err("invalid file type");
		return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
	}

	thumb_dbg("Thumb data is generated successfully");

	return err;
}

static int __thumbnail_get_raw_data(const char *origin_path, unsigned int *width, unsigned int *height, unsigned char **data, size_t *size)
{
	int err = MS_MEDIA_ERR_NONE;
	int file_type = THUMB_NONE_TYPE;

	thumb_retvm_if(!origin_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Original path is null");
	thumb_retvm_if(!g_file_test(origin_path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "Original path(%s) does not exist", origin_path);
	thumb_dbg_slog("Origin path : %s", origin_path);

	file_type = _media_thumb_get_file_type(origin_path);

	if (file_type == THUMB_IMAGE_TYPE) {
		err = create_image_thumbnail_to_buffer(origin_path, *width, *height, data, size, width, height);
	} else if (file_type == THUMB_VIDEO_TYPE) {
		err = create_video_thumbnail_to_buffer(origin_path, *width, *height, data, size, width, height, true, true);
	} else {
		thumb_err("invalid file type");
		return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
	}

	return err;
}

static int __media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg)
{
	int err = MS_MEDIA_ERR_NONE;

	thumb_retvm_if(!req_msg || !res_msg, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid msg");

	memset(res_msg->dst_path, 0, MAX_FILEPATH_LEN);
	res_msg->status = MS_MEDIA_ERR_NONE;

	if (!g_file_test(req_msg->org_path, G_FILE_TEST_IS_REGULAR)) {
		thumb_err("origin_path does not exist in file system.");
		res_msg->status = MS_MEDIA_ERR_FILE_NOT_EXIST;
		return MS_MEDIA_ERR_FILE_NOT_EXIST;
	}

	err = _media_thumb_get_thumb_from_db(req_msg->org_path, res_msg->dst_path, MAX_FILEPATH_LEN, req_msg->uid);
	thumb_retvm_if(err == MS_MEDIA_ERR_NONE, err, "Already exists");
	if (strlen(res_msg->dst_path) == 0) {
		err = _media_thumb_get_hash_name(req_msg->org_path, res_msg->dst_path, MAX_FILEPATH_LEN, req_msg->uid);
		if (err != MS_MEDIA_ERR_NONE) {
			thumb_err("_media_thumb_get_hash_name failed - %d", err);
			SAFE_STRLCPY(res_msg->dst_path, THUMB_EMPTY_STR, MAX_FILEPATH_LEN);
			res_msg->status = err;
			return err;
		}
	}

	thumb_dbg_slog("Thumb path : %s", res_msg->dst_path);

	err = __thumbnail_get_data(req_msg->org_path, res_msg->dst_path);
	if (err != MS_MEDIA_ERR_NONE) {
		thumb_err("_thumbnail_get_data failed - %d", err);
		SAFE_STRLCPY(res_msg->dst_path, THUMB_EMPTY_STR, MAX_FILEPATH_LEN);
		res_msg->status = err;

		goto DB_UPDATE;
	}

	res_msg->msg_type = THUMB_RESPONSE;

DB_UPDATE:
	err = _media_thumb_update_db(req_msg->org_path, res_msg->dst_path, req_msg->uid);
	if (err != MS_MEDIA_ERR_NONE) {
		thumb_err("_media_thumb_update_db failed : %d", err);
		res_msg->status = err;
	}

	return err;
}

static int __media_thumb_process_raw(thumbMsg *req_msg, thumbMsg *res_msg)
{
	int err = MS_MEDIA_ERR_NONE;
	unsigned char *data = NULL;
	size_t thumb_size = 0;
	unsigned int thumb_w = 0;
	unsigned int thumb_h = 0;

	thumb_retvm_if(!req_msg || !res_msg, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid msg");

	thumb_w = (req_msg->thumb_width > 0) ? req_msg->thumb_width : THUMB_DEFAULT_WIDTH;
	thumb_h = (req_msg->thumb_height > 0) ? req_msg->thumb_height : THUMB_DEFAULT_HEIGHT;

	res_msg->status = MS_MEDIA_ERR_NONE;
	res_msg->msg_type = THUMB_RESPONSE_RAW_DATA;

	err = __thumbnail_get_raw_data(req_msg->org_path, &thumb_w, &thumb_h, &data, &thumb_size);
	if (err != MS_MEDIA_ERR_NONE) {
		thumb_err("_thumbnail_get_data failed - %d", err);
		res_msg->status = err;
		res_msg->thumb_size = 0;
		goto ERROR;
	}

	res_msg->thumb_width = thumb_w;
	res_msg->thumb_height = thumb_h;
	res_msg->thumb_size = thumb_size;
	res_msg->thumb_data = malloc(thumb_size * sizeof(unsigned char));
	if (res_msg->thumb_data) {
		memcpy(res_msg->thumb_data, data, thumb_size);
	} else {
		thumb_err("Allocation failed");
		err = MS_MEDIA_ERR_OUT_OF_MEMORY;
	}

ERROR:
	SAFE_FREE(data);

	return err;
}

static int __thumb_daemon_process_job(thumbMsg *req_msg, thumbMsg *res_msg)
{
	int err = MS_MEDIA_ERR_NONE;

	if (req_msg->msg_type == THUMB_REQUEST_RAW_DATA) {
		err = __media_thumb_process_raw(req_msg, res_msg);
		if (err != MS_MEDIA_ERR_NONE)
			thumb_err("_media_thumb_process_raw is failed: %d", err);
	} else if (req_msg->msg_type == THUMB_REQUEST_DB_INSERT) {
		err = __media_thumb_process(req_msg, res_msg);
		if (err != MS_MEDIA_ERR_NONE)
			thumb_err("_media_thumb_process is failed: %d", err);
	}

	return err;
}

static void __thumb_server_send_deny_message(int sockfd)
{
	thumbMsg msg = {0};

	msg.msg_type = THUMB_RESPONSE;
	msg.status = MS_MEDIA_ERR_PERMISSION_DENIED;

	if (send(sockfd, &msg, sizeof(msg), 0) < 0)
		thumb_stderror("send failed");
}

gboolean _thumb_daemon_start_jobs(gpointer data)
{
	int client_sock;
	struct sockaddr_un serv_addr;
	ms_thumb_server_msg send_msg;

	if (ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &client_sock) < 0) {
		thumb_err("ms_ipc_create_server_socket failed");
		return G_SOURCE_REMOVE;
	}

	memset(&serv_addr, 0, sizeof(serv_addr));

	serv_addr.sun_family = AF_UNIX;
	SAFE_STRLCPY(serv_addr.sun_path, THUMB_COMM_SOCK_PATH, sizeof(serv_addr.sun_path));

	/* Connecting to the thumbnail server */
	if (connect(client_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
		thumb_stderror("connect");
		goto ERROR;
	}

	send_msg.msg_type = MS_MSG_THUMB_SERVER_READY;

	if (send(client_sock, &send_msg, sizeof(ms_thumb_server_msg), 0) < 0) {
		thumb_stderror("send failed");
		goto ERROR;
	}

	thumb_dbg("Sending msg to thumbnail agent[%d] is successful", send_msg.msg_type);
ERROR:
	close(client_sock);

	return G_SOURCE_REMOVE;
}

gboolean _thumb_server_read_socket(GIOChannel *channel, GIOCondition condition, gpointer data)
{
	struct sockaddr_un client_addr;
	unsigned int client_addr_len;
	thumbMsg recv_msg;
	thumbMsg res_msg;
	ms_peer_credentials credentials;
	GMainLoop *thumb_server = (GMainLoop *)data;

	int sock = -1;
	int client_sock = -1;

	int buf_size = 0;
	int send_len = 0;
	int send_pos = 0;
	unsigned char *buf = NULL;

	memset((void *)&recv_msg, 0, sizeof(recv_msg));
	memset((void *)&res_msg, 0, sizeof(res_msg));
	memset((void *)&credentials, 0, sizeof(credentials));

	sock = g_io_channel_unix_get_fd(channel);
	thumb_retvm_if(sock < 0, G_SOURCE_CONTINUE, "sock fd is invalid!");

	client_addr_len = sizeof(client_addr);

	if ((client_sock = accept(sock, (struct sockaddr*)&client_addr, &client_addr_len)) < 0) {
		thumb_stderror("accept failed");
		return G_SOURCE_CONTINUE;
	}

	if (ms_cynara_receive_untrusted_message_thumb(client_sock, &recv_msg, &credentials) != MS_MEDIA_ERR_NONE) {
		thumb_err("ms_cynara_receive_untrusted_message_thumb failed");
		goto ERROR;
	}

	if (recv_msg.msg_type != THUMB_REQUEST_KILL_SERVER) {
		if (ms_cynara_check(&credentials, MEDIA_STORAGE_PRIVILEGE) != MS_MEDIA_ERR_NONE) {
			thumb_err("Cynara denied access to process request");
			__thumb_server_send_deny_message(client_sock);
			goto ERROR;
		}
	} else {
		thumb_warn("Shutting down...");
		g_main_loop_quit(thumb_server);
		close(client_sock);

		return G_SOURCE_REMOVE;
	}

	SAFE_FREE(credentials.smack);
	SAFE_FREE(credentials.uid);

	thumb_warn_slog("Received [%d] %.*s(%zu) from PID(%d)", recv_msg.msg_type, MAX_FILEPATH_LEN, recv_msg.org_path, strlen(recv_msg.org_path), recv_msg.pid);

	__thumb_daemon_process_job(&recv_msg, &res_msg);

	if (res_msg.msg_type == 0)
		res_msg.msg_type = recv_msg.msg_type;
	res_msg.request_id = recv_msg.request_id;
	SAFE_STRLCPY(res_msg.org_path, recv_msg.org_path, sizeof(res_msg.org_path));
	if (res_msg.msg_type != THUMB_RESPONSE_RAW_DATA)
		res_msg.thumb_size = 0;
	else
		res_msg.dst_path[0] = '\0';

	_media_thumb_set_buffer(&res_msg, &buf, &buf_size);

	while (buf_size > 0) {
		if ((send_len = send(client_sock, buf + send_pos, buf_size, 0)) < 0) {
			thumb_stderror("send failed");
			break;
		}
		send_pos += send_len;
		buf_size -= send_len;
	}

	SAFE_FREE(buf);
	SAFE_FREE(res_msg.thumb_data);

ERROR:
	close(client_sock);

	return G_SOURCE_CONTINUE;
}

gboolean _thumb_server_prepare_socket(int *sock_fd)
{
	int sock;

	if (ms_ipc_create_server_socket(MS_THUMB_DAEMON_PORT, &sock) < 0) {
		thumb_err("ms_ipc_create_server_socket failed");
		return FALSE;
	}

	if (ms_cynara_enable_credentials_passing(sock) != MS_MEDIA_ERR_NONE) {
		thumb_err("ms_cynara_enable_credentials_passing failed");
		close(sock);
		return FALSE;
	}

	*sock_fd = sock;

	return TRUE;
}