4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin at samsung.com>, Sangchul Lee <sc11.lee at samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 #include <sys/types.h>
27 #include <mm_session_private.h>
34 #define EXPORT_API __attribute__((__visibility__("default")))
35 #define MAX_FILE_LENGTH 256
37 int g_session_type = -1;
39 struct sigaction session_int_old_action;
40 struct sigaction session_abrt_old_action;
41 struct sigaction session_segv_old_action;
42 struct sigaction session_term_old_action;
43 struct sigaction session_sys_old_action;
44 struct sigaction session_xcpu_old_action;
47 int mm_session_init(int sessiontype)
50 return mm_session_init_ex(sessiontype, NULL, NULL);
55 int mm_session_init_ex(int sessiontype, session_callback_fn callback, void* user_param)
57 int result = MM_ERROR_NONE;
59 bool do_not_update_session_info = false;
62 debug_log("type : %d", sessiontype);
64 if (sessiontype < MM_SESSION_TYPE_MEDIA || sessiontype >= MM_SESSION_TYPE_NUM) {
65 debug_error("Invalid argument %d",sessiontype);
66 return MM_ERROR_INVALID_ARGUMENT;
69 result = _mm_session_util_read_type(-1, <ype);
70 if (MM_ERROR_INVALID_HANDLE != result) {
71 if ((ltype == MM_SESSION_TYPE_MEDIA_RECORD) && sessiontype == MM_SESSION_TYPE_MEDIA) {
72 /* already set by mm-camcorder, mm-sound(pcm in), keep going */
73 do_not_update_session_info = true;
75 debug_error("Session already initialized. Please finish current session first");
76 return MM_ERROR_POLICY_DUPLICATED;
80 /* Monitor Callback */
81 if (NULL == callback) {
82 debug_warning("Null callback function");
84 debug_warning("It was deprecated, do not use monitor callback, callback(%p), user_param(%p)", callback, user_param);
87 g_session_type = sessiontype;
89 if (!do_not_update_session_info) {
90 result = _mm_session_util_write_type(-1, sessiontype);
91 if (MM_ERROR_NONE != result) {
92 debug_error("Write type failed");
100 return MM_ERROR_NONE;
104 int mm_session_update_option(session_update_type_t update_type, int options)
106 int result = MM_ERROR_NONE;
110 debug_log("update_type: %d(0:Add, 1:Remove), options: %x", update_type, options);
112 if (update_type < 0 || update_type >= MM_SESSION_UPDATE_TYPE_NUM) {
113 debug_error("Invalid update_type value(%d)", update_type);
114 return MM_ERROR_INVALID_ARGUMENT;
117 debug_error("Invalid options value(%x)", options);
118 return MM_ERROR_INVALID_ARGUMENT;
121 result = _mm_session_util_read_information(-1, <ype, &loption);
123 debug_error("failed to _mm_session_util_read_information(), ret(%x)", result);
126 debug_log("[current] session_type: %d, session_option: %x", ltype, loption);
128 if (update_type == MM_SESSION_UPDATE_TYPE_ADD) {
130 } else if (update_type == MM_SESSION_UPDATE_TYPE_REMOVE) {
134 result = _mm_session_util_write_information(-1, ltype, loption);
136 debug_error("failed to _mm_session_util_write_information(), ret(%x)", result);
140 debug_log("[updated] session_type: %d, session_option: %x", ltype, loption);
143 return MM_ERROR_NONE;
147 int mm_session_add_watch_callback(int watchevent, int watchstate, watch_callback_fn callback, void* user_param)
153 return MM_ERROR_NOT_SUPPORT_API;
157 int mm_session_get_current_type(int *sessiontype)
159 int result = MM_ERROR_NONE;
164 if (sessiontype == NULL) {
165 debug_error("input argument is NULL\n");
166 return MM_ERROR_INVALID_ARGUMENT;
169 result = _mm_session_util_read_type(-1, <ype);
170 if (result == MM_ERROR_NONE) {
171 debug_log("Current process session type = [%d]\n", ltype);
172 *sessiontype = ltype;
174 debug_error("failed to get current process session type!!\n");
183 int mm_session_get_current_information(int *session_type, int *session_options)
185 int result = MM_ERROR_NONE;
191 if (session_type == NULL) {
192 debug_error("input argument is NULL\n");
193 return MM_ERROR_INVALID_ARGUMENT;
196 result = _mm_session_util_read_information(-1, <ype, &loption);
197 if (result == MM_ERROR_NONE) {
198 debug_log("Current process session type = [%d], options = [%x]\n", ltype, loption);
199 *session_type = ltype;
200 *session_options = loption;
202 debug_error("failed to get current process session type, option!!\n");
211 int mm_session_finish(void)
213 int result = MM_ERROR_NONE;
214 int sessiontype = MM_SESSION_TYPE_MEDIA;
218 result = _mm_session_util_read_type(-1, &sessiontype);
219 if (MM_ERROR_NONE != result) {
220 debug_error("Can not read current type");
224 /* Check monitor handle */
225 result = _mm_session_util_delete_information(-1);
226 if(result != MM_ERROR_NONE)
231 return MM_ERROR_NONE;
235 int mm_session_remove_watch_callback(int watchevent, int watchstate)
241 return MM_ERROR_NOT_SUPPORT_API;
245 int mm_session_set_subsession(mm_subsession_t subsession, mm_subsession_option_t option)
251 return MM_ERROR_NOT_SUPPORT_API;
255 int mm_session_get_subsession(mm_subsession_t *subsession)
261 return MM_ERROR_NOT_SUPPORT_API;
265 int mm_session_set_subevent(mm_session_sub_t subevent)
271 return MM_ERROR_NOT_SUPPORT_API;
275 int mm_session_get_subevent(mm_session_sub_t *subevent)
281 return MM_ERROR_NOT_SUPPORT_API;
285 int mm_session_reset_resumption_info(void)
291 return MM_ERROR_NOT_SUPPORT_API;
295 int _mm_session_util_delete_information(int app_pid)
298 char filename[MAX_FILE_LENGTH];
303 mypid = (pid_t)app_pid;
305 ////// DELETE SESSION TYPE /////////
306 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
307 if(-1 == unlink(filename))
308 return MM_ERROR_FILE_NOT_FOUND;
309 ////// DELETE SESSION TYPE /////////
311 return MM_ERROR_NONE;
315 int _mm_session_util_write_type(int app_pid, int sessiontype)
319 char filename[MAX_FILE_LENGTH];
321 if(sessiontype < MM_SESSION_TYPE_MEDIA || sessiontype >= MM_SESSION_TYPE_NUM) {
322 return MM_ERROR_INVALID_ARGUMENT;
328 mypid = (pid_t)app_pid;
330 ////// WRITE SESSION TYPE /////////
331 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
332 fd = open(filename, O_WRONLY | O_CREAT, 0644 );
334 debug_error("open() failed with %d",errno);
335 return MM_ERROR_FILE_WRITE;
337 sessiontype = sessiontype << 16;
338 write(fd, &sessiontype, sizeof(int));
339 if(0 > fchmod (fd, 00777)) {
340 debug_error("fchmod failed with %d", errno);
342 debug_warning("write sessiontype(%d) to /tmp/mm_session_%d", sessiontype >> 16, mypid);
345 ////// WRITE SESSION TYPE /////////
347 return MM_ERROR_NONE;
351 int _mm_session_util_read_type(int app_pid, int *sessiontype)
355 char filename[MAX_FILE_LENGTH];
359 if(sessiontype == NULL)
360 return MM_ERROR_INVALID_ARGUMENT;
365 mypid = (pid_t)app_pid;
367 ////// READ SESSION TYPE /////////
368 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
369 fd = open(filename, O_RDONLY);
371 return MM_ERROR_INVALID_HANDLE;
373 read(fd, sessiontype, sizeof(int));
374 *sessiontype = *sessiontype >> 16;
375 debug_warning("read sessiontype(%d) from /tmp/mm_session_%d", *sessiontype, mypid);
377 ////// READ SESSION TYPE /////////
381 return MM_ERROR_NONE;
385 int _mm_session_util_write_information(int app_pid, int session_type, int flags)
389 char filename[MAX_FILE_LENGTH];
392 if ((session_type != MM_SESSION_TYPE_REPLACED_BY_STREAM) &&
393 (session_type < MM_SESSION_TYPE_MEDIA || session_type >= MM_SESSION_TYPE_NUM)) {
394 return MM_ERROR_INVALID_ARGUMENT;
397 return MM_ERROR_INVALID_ARGUMENT;
403 mypid = (pid_t)app_pid;
406 ////// WRITE SESSION INFO /////////
407 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
408 fd = open(filename, O_WRONLY | O_CREAT, 0644 );
410 debug_error("open() failed with %d",errno);
411 return MM_ERROR_FILE_WRITE;
414 result_info = (flags) | (session_type << 16);
415 write(fd, &result_info, sizeof(int));
416 if (0 > fchmod (fd, 00777)) {
417 debug_error("fchmod failed with %d", errno);
419 debug_warning("write session information(%x) to /tmp/mm_session_%d", result_info, mypid);
422 ////// WRITE SESSION INFO /////////
424 return MM_ERROR_NONE;
428 int _mm_session_util_read_information(int app_pid, int *session_type, int *flags)
432 char filename[MAX_FILE_LENGTH];
437 if(session_type == NULL || flags == NULL) {
438 return MM_ERROR_INVALID_ARGUMENT;
444 mypid = (pid_t)app_pid;
447 ////// READ SESSION INFO /////////
448 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
449 fd = open(filename, O_RDONLY);
451 return MM_ERROR_INVALID_HANDLE;
453 read(fd, &result_info, sizeof(int));
454 *session_type = result_info >> 16;
455 *flags = result_info & 0x0000ffff;
457 debug_warning("read session_type(%d), session_option(%x) from /tmp/mm_session_%d", *session_type, *flags, mypid);
459 ////// READ SESSION INFO /////////
463 return MM_ERROR_NONE;
466 void __session_signal_handler(int signo)
468 char filename[MAX_FILE_LENGTH];
471 debug_warning("ENTER, sig.num(%d)", signo);
473 /* signal block -------------- */
474 sigset_t old_mask, all_mask;
475 sigfillset(&all_mask);
476 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
478 snprintf(filename, sizeof(filename) - 1, "/tmp/mm_session_%d", getpid());
479 if (!remove(filename)) {
480 debug_log(" remove %s success\n", filename);
482 strerror_r(errno, str_error, sizeof(str_error));
483 debug_error(" remove %s failed with %s\n", filename, str_error);
486 sigprocmask(SIG_SETMASK, &old_mask, NULL);
487 /* signal unblock ------------ */
491 sigaction(SIGINT, &session_int_old_action, NULL);
495 sigaction(SIGABRT, &session_abrt_old_action, NULL);
499 sigaction(SIGSEGV, &session_segv_old_action, NULL);
503 sigaction(SIGTERM, &session_term_old_action, NULL);
507 sigaction(SIGSYS, &session_sys_old_action, NULL);
511 sigaction(SIGXCPU, &session_xcpu_old_action, NULL);
518 debug_warning("LEAVE");
521 __attribute__ ((constructor))
522 void __mmsession_initialize(void)
524 struct sigaction session_action;
525 session_action.sa_handler = __session_signal_handler;
526 session_action.sa_flags = SA_NOCLDSTOP;
530 sigemptyset(&session_action.sa_mask);
532 sigaction(SIGINT, &session_action, &session_int_old_action);
533 sigaction(SIGABRT, &session_action, &session_abrt_old_action);
534 sigaction(SIGSEGV, &session_action, &session_segv_old_action);
535 sigaction(SIGTERM, &session_action, &session_term_old_action);
536 sigaction(SIGSYS, &session_action, &session_sys_old_action);
537 sigaction(SIGXCPU, &session_action, &session_xcpu_old_action);
542 __attribute__ ((destructor))
543 void __mmsession_finalize(void)
548 _mm_session_util_delete_information(-1);
550 sigaction(SIGINT, &session_int_old_action, NULL);
551 sigaction(SIGABRT, &session_abrt_old_action, NULL);
552 sigaction(SIGSEGV, &session_segv_old_action, NULL);
553 sigaction(SIGTERM, &session_term_old_action, NULL);
554 sigaction(SIGSYS, &session_sys_old_action, NULL);
555 sigaction(SIGXCPU, &session_xcpu_old_action, NULL);