summaryrefslogtreecommitdiff
path: root/server/thumb-server-internal.c
blob: 10b7162d17233d0be1850f5f9ce6712b11069a23 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
 * 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 <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <Ecore_Evas.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_BLOCK_SIZE 512
#define THUMB_COMM_SOCK_PATH tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcomm.socket")
#define THUMB_EMPTY_STR ""

GMainLoop *g_thumb_server_mainloop; // defined in thumb-server.c as extern

static gboolean __thumb_server_send_msg_to_agent(int msg_type);
static gboolean _thumb_server_send_deny_message(int sockfd);

gboolean _thumb_daemon_start_jobs(gpointer data)
{
	thumb_dbg("");
	/* Initialize ecore-evas to use evas library */
	ecore_evas_init();

	__thumb_server_send_msg_to_agent(MS_MSG_THUMB_SERVER_READY);

	return FALSE;
}

void _thumb_daemon_finish_jobs(void)
{
	sqlite3 *sqlite_db_handle = _media_thumb_db_get_handle();

	if (sqlite_db_handle != NULL) {
		_media_thumb_db_disconnect();
		thumb_dbg("sqlite3 handle is alive. So disconnect to sqlite3");
	}

	/* Shutdown ecore-evas */
	ecore_evas_shutdown();
	g_main_loop_quit(g_thumb_server_mainloop);

	return;
}

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_warn("_media_thumb_process_raw is failed: %d", err);
	} else {
		err = _media_thumb_process(req_msg, res_msg);
		if (err != MS_MEDIA_ERR_NONE)
			thumb_warn("_media_thumb_process is failed: %d", err);
	}

	return err;
}

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

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

	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(src);
	if (sock < 0) {
		thumb_err("sock fd is invalid!");
		return TRUE;
	}

	client_addr_len = sizeof(client_addr);

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

	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");
		close(client_sock);
		return FALSE;
	}

	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);
			close(client_sock);
			return TRUE;
		}
	}

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

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

	if (recv_msg.msg_type != THUMB_REQUEST_KILL_SERVER)
		_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));
	res_msg.origin_path_size = recv_msg.origin_path_size;
	if (res_msg.msg_type != THUMB_RESPONSE_RAW_DATA) {
		res_msg.dest_path_size = strlen(res_msg.dst_path)+1;
		res_msg.thumb_size = 0;
	} else {
		res_msg.dest_path_size = 1;
		res_msg.dst_path[0] = '\0';
	}

	int buf_size = 0;
	int sending_block = 0;
	int block_size = sizeof(res_msg) - MAX_FILEPATH_LEN*2 - sizeof(unsigned char *);
	unsigned char *buf = NULL;
	_media_thumb_set_buffer(&res_msg, &buf, &buf_size);

	while (buf_size > 0) {
		if (buf_size < THUMB_BLOCK_SIZE) {
			block_size = buf_size;
		}
		if (send(client_sock, buf+sending_block, block_size, 0) != block_size) {
			thumb_stderror("sendto failed");
		}
		sending_block += block_size;
		buf_size -= block_size;
		if (block_size < THUMB_BLOCK_SIZE) {
			block_size = THUMB_BLOCK_SIZE;
		}
	}

	thumb_dbg_slog("Sent data(%d) from %s", res_msg.thumb_size, res_msg.org_path);

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

	if (recv_msg.msg_type == THUMB_REQUEST_KILL_SERVER) {
		thumb_warn("Shutting down...");
		g_main_loop_quit(g_thumb_server_mainloop);
	}

	close(client_sock);

	return TRUE;
}

static gboolean __thumb_server_send_msg_to_agent(int msg_type)
{
	int sock;
	struct sockaddr_un serv_addr;
	ms_thumb_server_msg send_msg;

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

	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(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
		thumb_stderror("connect");
		close(sock);
		return MS_MEDIA_ERR_SOCKET_CONN;
	}

	send_msg.msg_type = msg_type;

	if (send(sock, &send_msg, sizeof(ms_thumb_server_msg), 0) != sizeof(ms_thumb_server_msg)) {
		thumb_stderror("sendto failed");
		close(sock);
		return FALSE;
	}

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

	return TRUE;
}

static gboolean _thumb_server_send_deny_message(int sockfd)
{
	thumbMsg msg = {0};
	int bytes_to_send = sizeof(msg) - sizeof(msg.org_path) - sizeof(msg.dst_path);

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

	if (send(sockfd, &msg, bytes_to_send, 0) != bytes_to_send) {
		thumb_stderror("send failed");
		return FALSE;
	}

	return TRUE;
}

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;
}

int _thumbnail_get_data(const char *origin_path,
						char *thumb_path,
						unsigned char **data,
						int *size,
						int *width,
						int *height,
						int *origin_width,
						int *origin_height,
						int *alpha,
						bool *is_saved)
{
	int err = MS_MEDIA_ERR_NONE;
	int thumb_width = -1;
	int thumb_height = -1;

	if (origin_path == NULL || size == NULL || width == NULL || height == NULL) {
		thumb_err("Invalid parameter");
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
		thumb_err("Original path (%s) does not exist", origin_path);
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	thumb_dbg("Origin path : %s", origin_path);

	int file_type = THUMB_NONE_TYPE;
	media_thumb_info thumb_info = {0,};
	file_type = _media_thumb_get_file_type(origin_path);
	thumb_width = *width;
	thumb_height = *height;
	if (thumb_width == 0 || thumb_height == 0) {
		thumb_width = THUMB_DEFAULT_WIDTH;
		thumb_height = THUMB_DEFAULT_HEIGHT;
	}

	thumb_info.is_raw = FALSE;

	if (file_type == THUMB_IMAGE_TYPE) {
		err = _media_thumb_image(origin_path, thumb_path, thumb_width, thumb_height, &thumb_info);
		if (err != MS_MEDIA_ERR_NONE) {
			thumb_err("_media_thumb_image failed");
			return err;
		}
	} else if (file_type == THUMB_VIDEO_TYPE) {
		err = _media_thumb_video(origin_path, thumb_width, thumb_height, &thumb_info);
		if (err != MS_MEDIA_ERR_NONE) {
			thumb_err("_media_thumb_image failed");
			return err;
		}
	} else {
		thumb_err("invalid file type");
		return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
	}

	if (size) *size = thumb_info.size;
	if (width) *width = thumb_info.width;
	if (height) *height = thumb_info.height;
	*data = thumb_info.data;
	if (origin_width) *origin_width = thumb_info.origin_width;
	if (origin_height) *origin_height = thumb_info.origin_height;
	if (alpha) *alpha = thumb_info.alpha;
	if (is_saved) *is_saved = thumb_info.is_saved;

	thumb_dbg("Thumb data is generated successfully (Size:%d, W:%d, H:%d) %p", *size, *width, *height, *data);

	return MS_MEDIA_ERR_NONE;
}

int _thumbnail_get_raw_data(const char *origin_path,
						int *width,
						int *height,
						unsigned char **data,
						int *size)
{
	int err = MS_MEDIA_ERR_NONE;
	int thumb_width = -1;
	int thumb_height = -1;
	const char * thumb_path = NULL;

	if (origin_path == NULL || *width <= 0 || *height <= 0) {
		thumb_err("Invalid parameter");
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
		thumb_err("Original path (%s) does not exist", origin_path);
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	int file_type = THUMB_NONE_TYPE;
	media_thumb_info thumb_info = {0,};
	file_type = _media_thumb_get_file_type(origin_path);
	thumb_width = *width;
	thumb_height = *height;
	thumb_info.is_raw = TRUE;

	if (file_type == THUMB_IMAGE_TYPE) {
		err = _media_thumb_image(origin_path, thumb_path, thumb_width, thumb_height, &thumb_info);
		if (err != MS_MEDIA_ERR_NONE) {
			thumb_err("_media_thumb_image failed");
			return err;
		}
	} else if (file_type == THUMB_VIDEO_TYPE) {
		err = _media_thumb_video(origin_path, thumb_width, thumb_height, &thumb_info);
		if (err != MS_MEDIA_ERR_NONE) {
			thumb_err("_media_thumb_image failed");
			return err;
		}
	} else {
		thumb_err("invalid file type");
		return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
	}

	if (size) *size = thumb_info.size;
	*data = thumb_info.data;
	*width = thumb_info.width;
	*height = thumb_info.height;

	return MS_MEDIA_ERR_NONE;
}

int _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg)
{
	int err = MS_MEDIA_ERR_NONE;
	unsigned char *data = NULL;
	int thumb_size = 0;
	int thumb_w = 0;
	int thumb_h = 0;
	int origin_w = 0;
	int origin_h = 0;
	int max_length = 0;
	char *thumb_path = NULL;
	int need_update_db = 0;
	int alpha = 0;
	bool is_saved = FALSE;

	if (req_msg == NULL || res_msg == NULL) {
		thumb_err("Invalid msg!");
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	int msg_type = req_msg->msg_type;
	const char *origin_path = req_msg->org_path;
	thumb_w = req_msg->thumb_width;
	thumb_h = req_msg->thumb_height;
	thumb_path = res_msg->dst_path;
	thumb_path[0] = '\0';
	max_length = sizeof(res_msg->dst_path) -1;
	res_msg->status = MS_MEDIA_ERR_NONE;

	if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | 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_db_connect(req_msg->uid);
	if (err != MS_MEDIA_ERR_NONE) {
		thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
		res_msg->status = MS_MEDIA_ERR_DB_CONNECT_FAIL;
		return err;
	}

	if (msg_type == THUMB_REQUEST_DB_INSERT) {
		err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
		if (err == MS_MEDIA_ERR_NONE) {
			res_msg->origin_width = origin_w;
			res_msg->origin_height = origin_h;
			_media_thumb_db_disconnect();
			return MS_MEDIA_ERR_NONE;
		} else {
			if (strlen(thumb_path) == 0) {
				err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length, req_msg->uid);
				if (err != MS_MEDIA_ERR_NONE) {
					thumb_err("_media_thumb_get_hash_name failed - %d", err);
					strncpy(thumb_path, THUMB_EMPTY_STR, max_length);
					_media_thumb_db_disconnect();
					res_msg->status = err;
					return err;
				}

				thumb_path[strlen(thumb_path)] = '\0';
			}
		}

	}

	thumb_dbg_slog("Thumb path : %s", thumb_path);

	if (g_file_test(thumb_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
		thumb_warn("thumb path already exists in file system.. remove the existed file");
		_media_thumb_remove_file(thumb_path);
	}

	err = _thumbnail_get_data(origin_path, thumb_path, &data, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha, &is_saved);
	if (err != MS_MEDIA_ERR_NONE) {
		thumb_err("_thumbnail_get_data failed - %d", err);
		SAFE_FREE(data);
		strncpy(thumb_path, THUMB_EMPTY_STR, max_length);
		res_msg->status = err;

		goto DB_UPDATE;
	}

	res_msg->msg_type = THUMB_RESPONSE;
	res_msg->thumb_size = thumb_size;
	res_msg->thumb_width = thumb_w;
	res_msg->thumb_height = thumb_h;
	res_msg->origin_width = origin_w;
	res_msg->origin_height = origin_h;

	/* If the image is transparent PNG format, make png file as thumbnail of this image */
	if (alpha) {
		char file_ext[10];
		err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
		if (strncasecmp(file_ext, "png", 3) == 0) {
			int len = strlen(thumb_path);
			thumb_path[len - 3] = 'p';
			thumb_path[len - 2] = 'n';
			thumb_path[len - 1] = 'g';
		}
		thumb_dbg_slog("Thumb path is changed : %s", thumb_path);
	}

	if (is_saved == FALSE && data != NULL) {
		err = _media_thumb_save_to_file_with_evas(data, thumb_w, thumb_h, alpha, thumb_path);
		if (err != MS_MEDIA_ERR_NONE) {
			thumb_err("save_to_file_with_evas failed - %d", err);
			SAFE_FREE(data);

			if (msg_type == THUMB_REQUEST_DB_INSERT) {
				strncpy(thumb_path, THUMB_EMPTY_STR, max_length);
			}
			_media_thumb_db_disconnect();
			res_msg->status = err;
			return err;
		} else {
			thumb_dbg("file save success");
		}
	} else {
		thumb_dbg("file is already saved");
	}

	/* fsync */
	int fd = 0;
	fd = open(thumb_path, O_WRONLY);
	if (fd < 0) {
		thumb_warn("open failed");
	} else {
		err = fsync(fd);
		if (err == -1) {
			thumb_warn("fsync failed");
			res_msg->status = MS_MEDIA_ERR_INTERNAL;
		}

		close(fd);
	}
	/* End of fsync */

	SAFE_FREE(data);
DB_UPDATE:
	/* DB update if needed */
	if (need_update_db == 1) {
		err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height, req_msg->uid);
		if (err != MS_MEDIA_ERR_NONE) {
			thumb_err("_media_thumb_update_db failed : %d", err);
			res_msg->status = err;
		}
	}

	_media_thumb_db_disconnect();

	return err;
}

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

	if (req_msg == NULL || res_msg == NULL) {
		thumb_err("Invalid msg!");
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	const char *origin_path = req_msg->org_path;
	thumb_w = req_msg->thumb_width;
	thumb_h = req_msg->thumb_height;
	res_msg->status = MS_MEDIA_ERR_NONE;
	res_msg->msg_type = THUMB_RESPONSE_RAW_DATA;

	err = _thumbnail_get_raw_data(origin_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;
		SAFE_FREE(data);

		return err;
	}

	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));
	memcpy(res_msg->thumb_data, data, thumb_size);

	SAFE_FREE(data);

	return err;
}