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>
35 #define EXPORT_API __attribute__((__visibility__("default")))
36 #define MAX_FILE_LENGTH 256
38 int g_session_type = -1;
40 struct sigaction session_int_old_action;
41 struct sigaction session_abrt_old_action;
42 struct sigaction session_segv_old_action;
43 struct sigaction session_term_old_action;
44 struct sigaction session_sys_old_action;
45 struct sigaction session_xcpu_old_action;
48 int mm_session_init(int sessiontype)
51 return mm_session_init_ex(sessiontype, NULL, NULL);
56 int mm_session_init_ex(int sessiontype, session_callback_fn callback, void* user_param)
58 int result = MM_ERROR_NONE;
60 bool do_not_update_session_info = false;
63 debug_log("type : %d", sessiontype);
65 if (sessiontype < MM_SESSION_TYPE_MEDIA || sessiontype >= MM_SESSION_TYPE_NUM) {
66 debug_error("Invalid argument %d",sessiontype);
67 return MM_ERROR_INVALID_ARGUMENT;
70 result = _mm_session_util_read_type(-1, <ype);
71 if (MM_ERROR_INVALID_HANDLE != result) {
72 if ((ltype == MM_SESSION_TYPE_MEDIA_RECORD) && sessiontype == MM_SESSION_TYPE_MEDIA) {
73 /* already set by mm-camcorder, mm-sound(pcm in), keep going */
74 do_not_update_session_info = true;
76 debug_error("Session already initialized. Please finish current session first");
77 return MM_ERROR_POLICY_DUPLICATED;
81 /* Monitor Callback */
82 if (NULL == callback) {
83 debug_warning("Null callback function");
85 debug_warning("It was deprecated, do not use monitor callback, callback(%p), user_param(%p)", callback, user_param);
88 g_session_type = sessiontype;
90 if (!do_not_update_session_info) {
91 result = _mm_session_util_write_type(-1, sessiontype);
92 if (MM_ERROR_NONE != result) {
93 debug_error("Write type failed");
101 return MM_ERROR_NONE;
105 int mm_session_update_option(session_update_type_t update_type, int options)
107 int result = MM_ERROR_NONE;
111 debug_log("update_type: %d(0:Add, 1:Remove), options: %x", update_type, options);
113 if (update_type < 0 || update_type >= MM_SESSION_UPDATE_TYPE_NUM) {
114 debug_error("Invalid update_type value(%d)", update_type);
115 return MM_ERROR_INVALID_ARGUMENT;
118 debug_error("Invalid options value(%x)", options);
119 return MM_ERROR_INVALID_ARGUMENT;
122 result = _mm_session_util_read_information(-1, <ype, &loption);
124 debug_error("failed to _mm_session_util_read_information(), ret(%x)", result);
127 debug_log("[current] session_type: %d, session_option: %x", ltype, loption);
129 if (update_type == MM_SESSION_UPDATE_TYPE_ADD) {
131 } else if (update_type == MM_SESSION_UPDATE_TYPE_REMOVE) {
135 result = _mm_session_util_write_information(-1, ltype, loption);
137 debug_error("failed to _mm_session_util_write_information(), ret(%x)", result);
141 debug_log("[updated] session_type: %d, session_option: %x", ltype, loption);
144 return MM_ERROR_NONE;
148 int mm_session_add_watch_callback(int watchevent, int watchstate, watch_callback_fn callback, void* user_param)
154 return MM_ERROR_NOT_SUPPORT_API;
158 int mm_session_get_current_type(int *sessiontype)
160 int result = MM_ERROR_NONE;
165 if (sessiontype == NULL) {
166 debug_error("input argument is NULL\n");
167 return MM_ERROR_INVALID_ARGUMENT;
170 result = _mm_session_util_read_type(-1, <ype);
171 if (result == MM_ERROR_NONE) {
172 debug_log("Current process session type = [%d]\n", ltype);
173 *sessiontype = ltype;
175 debug_error("failed to get current process session type!!\n");
184 int mm_session_get_current_information(int *session_type, int *session_options)
186 int result = MM_ERROR_NONE;
192 if (session_type == NULL) {
193 debug_error("input argument is NULL\n");
194 return MM_ERROR_INVALID_ARGUMENT;
197 result = _mm_session_util_read_information(-1, <ype, &loption);
198 if (result == MM_ERROR_NONE) {
199 debug_log("Current process session type = [%d], options = [%x]\n", ltype, loption);
200 *session_type = ltype;
201 *session_options = loption;
203 debug_error("failed to get current process session type, option!!\n");
212 int mm_session_finish(void)
214 int result = MM_ERROR_NONE;
215 int sessiontype = MM_SESSION_TYPE_MEDIA;
219 result = _mm_session_util_read_type(-1, &sessiontype);
220 if (MM_ERROR_NONE != result) {
221 debug_error("Can not read current type");
225 /* Check monitor handle */
226 result = _mm_session_util_delete_information(-1);
227 if(result != MM_ERROR_NONE)
232 return MM_ERROR_NONE;
236 int mm_session_remove_watch_callback(int watchevent, int watchstate)
242 return MM_ERROR_NOT_SUPPORT_API;
246 int mm_session_set_subsession(mm_subsession_t subsession, mm_subsession_option_t option)
252 return MM_ERROR_NOT_SUPPORT_API;
256 int mm_session_get_subsession(mm_subsession_t *subsession)
262 return MM_ERROR_NOT_SUPPORT_API;
266 int mm_session_set_subevent(mm_session_sub_t subevent)
272 return MM_ERROR_NOT_SUPPORT_API;
276 int mm_session_get_subevent(mm_session_sub_t *subevent)
282 return MM_ERROR_NOT_SUPPORT_API;
286 int mm_session_reset_resumption_info(void)
292 return MM_ERROR_NOT_SUPPORT_API;
296 int _mm_session_util_delete_information(int app_pid)
299 char filename[MAX_FILE_LENGTH];
304 mypid = (pid_t)app_pid;
306 ////// DELETE SESSION TYPE /////////
307 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
308 if(-1 == unlink(filename))
309 return MM_ERROR_FILE_NOT_FOUND;
310 ////// DELETE SESSION TYPE /////////
312 return MM_ERROR_NONE;
316 int _mm_session_util_write_type(int app_pid, int sessiontype)
320 char filename[MAX_FILE_LENGTH];
322 if(sessiontype < MM_SESSION_TYPE_MEDIA || sessiontype >= MM_SESSION_TYPE_NUM) {
323 return MM_ERROR_INVALID_ARGUMENT;
329 mypid = (pid_t)app_pid;
331 ////// WRITE SESSION TYPE /////////
332 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
333 fd = open(filename, O_WRONLY | O_CREAT, 0644 );
335 debug_error("open() failed with %d",errno);
336 return MM_ERROR_FILE_WRITE;
338 sessiontype = sessiontype << 16;
339 write(fd, &sessiontype, sizeof(int));
340 if(0 > fchmod (fd, 00777)) {
341 debug_error("fchmod failed with %d", errno);
343 debug_warning("write sessiontype(%d) to /tmp/mm_session_%d", sessiontype >> 16, mypid);
346 ////// WRITE SESSION TYPE /////////
348 return MM_ERROR_NONE;
352 int _mm_session_util_read_type(int app_pid, int *sessiontype)
356 char filename[MAX_FILE_LENGTH];
360 if(sessiontype == NULL)
361 return MM_ERROR_INVALID_ARGUMENT;
366 mypid = (pid_t)app_pid;
368 ////// READ SESSION TYPE /////////
369 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
370 fd = open(filename, O_RDONLY);
372 return MM_ERROR_INVALID_HANDLE;
374 read(fd, sessiontype, sizeof(int));
375 *sessiontype = *sessiontype >> 16;
376 debug_warning("read sessiontype(%d) from /tmp/mm_session_%d", *sessiontype, mypid);
378 ////// READ SESSION TYPE /////////
382 return MM_ERROR_NONE;
386 int _mm_session_util_write_information(int app_pid, int session_type, int flags)
390 char filename[MAX_FILE_LENGTH];
393 if(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 = NULL;
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 filename = g_strdup_printf("/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);
488 sigprocmask(SIG_SETMASK, &old_mask, NULL);
489 /* signal unblock ------------ */
493 sigaction(SIGINT, &session_int_old_action, NULL);
497 sigaction(SIGABRT, &session_abrt_old_action, NULL);
501 sigaction(SIGSEGV, &session_segv_old_action, NULL);
505 sigaction(SIGTERM, &session_term_old_action, NULL);
509 sigaction(SIGSYS, &session_sys_old_action, NULL);
513 sigaction(SIGXCPU, &session_xcpu_old_action, NULL);
520 debug_warning("LEAVE");
523 __attribute__ ((constructor))
524 void __mmsession_initialize(void)
526 struct sigaction session_action;
527 session_action.sa_handler = __session_signal_handler;
528 session_action.sa_flags = SA_NOCLDSTOP;
532 sigemptyset(&session_action.sa_mask);
534 sigaction(SIGINT, &session_action, &session_int_old_action);
535 sigaction(SIGABRT, &session_action, &session_abrt_old_action);
536 sigaction(SIGSEGV, &session_action, &session_segv_old_action);
537 sigaction(SIGTERM, &session_action, &session_term_old_action);
538 sigaction(SIGSYS, &session_action, &session_sys_old_action);
539 sigaction(SIGXCPU, &session_action, &session_xcpu_old_action);
544 __attribute__ ((destructor))
545 void __mmsession_finalize(void)
550 _mm_session_util_delete_information(-1);
552 sigaction(SIGINT, &session_int_old_action, NULL);
553 sigaction(SIGABRT, &session_abrt_old_action, NULL);
554 sigaction(SIGSEGV, &session_segv_old_action, NULL);
555 sigaction(SIGTERM, &session_term_old_action, NULL);
556 sigaction(SIGSYS, &session_sys_old_action, NULL);
557 sigaction(SIGXCPU, &session_xcpu_old_action, NULL);