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)
49 int result = MM_ERROR_NONE;
51 bool do_not_update_session_info = false;
54 debug_log("type : %d", sessiontype);
56 if (sessiontype < MM_SESSION_TYPE_MEDIA || sessiontype >= MM_SESSION_TYPE_NUM) {
57 debug_error("Invalid argument %d",sessiontype);
58 return MM_ERROR_INVALID_ARGUMENT;
61 result = _mm_session_util_read_type(-1, <ype);
62 if (MM_ERROR_INVALID_HANDLE != result) {
63 if ((ltype == MM_SESSION_TYPE_MEDIA_RECORD) && sessiontype == MM_SESSION_TYPE_MEDIA) {
64 /* already set by mm-camcorder, mm-sound(pcm in), keep going */
65 do_not_update_session_info = true;
67 debug_error("Session already initialized. Please finish current session first");
68 return MM_ERROR_POLICY_DUPLICATED;
72 g_session_type = sessiontype;
74 if (!do_not_update_session_info) {
75 result = _mm_session_util_write_type(-1, sessiontype);
76 if (MM_ERROR_NONE != result) {
77 debug_error("Write type failed");
89 int mm_session_update_option(session_update_type_t update_type, int options)
91 int result = MM_ERROR_NONE;
95 debug_log("update_type: %d(0:Add, 1:Remove), options: %x", update_type, options);
97 if (update_type < 0 || update_type >= MM_SESSION_UPDATE_TYPE_NUM) {
98 debug_error("Invalid update_type value(%d)", update_type);
99 return MM_ERROR_INVALID_ARGUMENT;
102 debug_error("Invalid options value(%x)", options);
103 return MM_ERROR_INVALID_ARGUMENT;
106 result = _mm_session_util_read_information(-1, <ype, &loption);
108 debug_error("failed to _mm_session_util_read_information(), ret(%x)", result);
111 debug_log("[current] session_type: %d, session_option: %x", ltype, loption);
113 if (update_type == MM_SESSION_UPDATE_TYPE_ADD) {
115 } else if (update_type == MM_SESSION_UPDATE_TYPE_REMOVE) {
119 result = _mm_session_util_write_information(-1, ltype, loption);
121 debug_error("failed to _mm_session_util_write_information(), ret(%x)", result);
125 debug_log("[updated] session_type: %d, session_option: %x", ltype, loption);
128 return MM_ERROR_NONE;
132 int mm_session_get_current_type(int *sessiontype)
134 int result = MM_ERROR_NONE;
139 if (sessiontype == NULL) {
140 debug_error("input argument is NULL\n");
141 return MM_ERROR_INVALID_ARGUMENT;
144 result = _mm_session_util_read_type(-1, <ype);
145 if (result == MM_ERROR_NONE) {
146 debug_log("Current process session type = [%d]\n", ltype);
147 *sessiontype = ltype;
149 debug_error("failed to get current process session type!!\n");
158 int mm_session_get_current_information(int *session_type, int *session_options)
160 int result = MM_ERROR_NONE;
166 if (session_type == NULL) {
167 debug_error("input argument is NULL\n");
168 return MM_ERROR_INVALID_ARGUMENT;
171 result = _mm_session_util_read_information(-1, <ype, &loption);
172 if (result == MM_ERROR_NONE) {
173 debug_log("Current process session type = [%d], options = [%x]\n", ltype, loption);
174 *session_type = ltype;
175 *session_options = loption;
177 debug_error("failed to get current process session type, option!!\n");
186 int mm_session_finish(void)
188 int result = MM_ERROR_NONE;
189 int sessiontype = MM_SESSION_TYPE_MEDIA;
193 result = _mm_session_util_read_type(-1, &sessiontype);
194 if (MM_ERROR_NONE != result) {
195 debug_error("Can not read current type");
199 /* Check monitor handle */
200 result = _mm_session_util_delete_information(-1);
201 if(result != MM_ERROR_NONE)
206 return MM_ERROR_NONE;
210 int _mm_session_util_delete_information(int app_pid)
213 char filename[MAX_FILE_LENGTH];
218 mypid = (pid_t)app_pid;
220 ////// DELETE SESSION TYPE /////////
221 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
222 if(-1 == unlink(filename))
223 return MM_ERROR_FILE_NOT_FOUND;
224 ////// DELETE SESSION TYPE /////////
226 return MM_ERROR_NONE;
230 int _mm_session_util_write_type(int app_pid, int sessiontype)
234 char filename[MAX_FILE_LENGTH];
236 if(sessiontype < MM_SESSION_TYPE_MEDIA || sessiontype >= MM_SESSION_TYPE_NUM) {
237 return MM_ERROR_INVALID_ARGUMENT;
243 mypid = (pid_t)app_pid;
245 ////// WRITE SESSION TYPE /////////
246 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
247 fd = open(filename, O_WRONLY | O_CREAT, 0644 );
249 debug_error("open() failed with %d",errno);
250 return MM_ERROR_FILE_WRITE;
252 sessiontype = sessiontype << 16;
253 write(fd, &sessiontype, sizeof(int));
254 if(0 > fchmod (fd, 00777)) {
255 debug_error("fchmod failed with %d", errno);
257 debug_warning("write sessiontype(%d) to /tmp/mm_session_%d", sessiontype >> 16, mypid);
260 ////// WRITE SESSION TYPE /////////
262 return MM_ERROR_NONE;
266 int _mm_session_util_read_type(int app_pid, int *sessiontype)
270 char filename[MAX_FILE_LENGTH];
274 if(sessiontype == NULL)
275 return MM_ERROR_INVALID_ARGUMENT;
280 mypid = (pid_t)app_pid;
282 ////// READ SESSION TYPE /////////
283 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
284 fd = open(filename, O_RDONLY);
286 return MM_ERROR_INVALID_HANDLE;
288 read(fd, sessiontype, sizeof(int));
289 *sessiontype = *sessiontype >> 16;
290 debug_warning("read sessiontype(%d) from /tmp/mm_session_%d", *sessiontype, mypid);
292 ////// READ SESSION TYPE /////////
296 return MM_ERROR_NONE;
300 int _mm_session_util_write_information(int app_pid, int session_type, int flags)
304 char filename[MAX_FILE_LENGTH];
307 if ((session_type != MM_SESSION_TYPE_REPLACED_BY_STREAM) &&
308 (session_type < MM_SESSION_TYPE_MEDIA || session_type >= MM_SESSION_TYPE_NUM)) {
309 return MM_ERROR_INVALID_ARGUMENT;
312 return MM_ERROR_INVALID_ARGUMENT;
318 mypid = (pid_t)app_pid;
321 ////// WRITE SESSION INFO /////////
322 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
323 fd = open(filename, O_WRONLY | O_CREAT, 0644 );
325 debug_error("open() failed with %d",errno);
326 return MM_ERROR_FILE_WRITE;
329 result_info = (flags) | (session_type << 16);
330 write(fd, &result_info, sizeof(int));
331 if (0 > fchmod (fd, 00777)) {
332 debug_error("fchmod failed with %d", errno);
334 debug_warning("write session information(%x) to /tmp/mm_session_%d", result_info, mypid);
337 ////// WRITE SESSION INFO /////////
339 return MM_ERROR_NONE;
343 int _mm_session_util_read_information(int app_pid, int *session_type, int *flags)
347 char filename[MAX_FILE_LENGTH];
352 if(session_type == NULL || flags == NULL) {
353 return MM_ERROR_INVALID_ARGUMENT;
359 mypid = (pid_t)app_pid;
362 ////// READ SESSION INFO /////////
363 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
364 fd = open(filename, O_RDONLY);
366 return MM_ERROR_INVALID_HANDLE;
368 read(fd, &result_info, sizeof(int));
369 *session_type = result_info >> 16;
370 *flags = result_info & 0x0000ffff;
372 debug_warning("read session_type(%d), session_option(%x) from /tmp/mm_session_%d", *session_type, *flags, mypid);
374 ////// READ SESSION INFO /////////
378 return MM_ERROR_NONE;
381 void __session_signal_handler(int signo)
383 char filename[MAX_FILE_LENGTH];
386 debug_warning("ENTER, sig.num(%d)", signo);
388 /* signal block -------------- */
389 sigset_t old_mask, all_mask;
390 sigfillset(&all_mask);
391 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
393 snprintf(filename, sizeof(filename) - 1, "/tmp/mm_session_%d", getpid());
394 if (!remove(filename)) {
395 debug_log(" remove %s success\n", filename);
397 strerror_r(errno, str_error, sizeof(str_error));
398 debug_error(" remove %s failed with %s\n", filename, str_error);
401 sigprocmask(SIG_SETMASK, &old_mask, NULL);
402 /* signal unblock ------------ */
406 sigaction(SIGINT, &session_int_old_action, NULL);
410 sigaction(SIGABRT, &session_abrt_old_action, NULL);
414 sigaction(SIGSEGV, &session_segv_old_action, NULL);
418 sigaction(SIGTERM, &session_term_old_action, NULL);
422 sigaction(SIGSYS, &session_sys_old_action, NULL);
426 sigaction(SIGXCPU, &session_xcpu_old_action, NULL);
433 debug_warning("LEAVE");
436 __attribute__ ((constructor))
437 void __mmsession_initialize(void)
439 struct sigaction session_action;
440 session_action.sa_handler = __session_signal_handler;
441 session_action.sa_flags = SA_NOCLDSTOP;
445 sigemptyset(&session_action.sa_mask);
447 sigaction(SIGINT, &session_action, &session_int_old_action);
448 sigaction(SIGABRT, &session_action, &session_abrt_old_action);
449 sigaction(SIGSEGV, &session_action, &session_segv_old_action);
450 sigaction(SIGTERM, &session_action, &session_term_old_action);
451 sigaction(SIGSYS, &session_action, &session_sys_old_action);
452 sigaction(SIGXCPU, &session_action, &session_xcpu_old_action);
457 __attribute__ ((destructor))
458 void __mmsession_finalize(void)
463 _mm_session_util_delete_information(-1);
465 sigaction(SIGINT, &session_int_old_action, NULL);
466 sigaction(SIGABRT, &session_abrt_old_action, NULL);
467 sigaction(SIGSEGV, &session_segv_old_action, NULL);
468 sigaction(SIGTERM, &session_term_old_action, NULL);
469 sigaction(SIGSYS, &session_sys_old_action, NULL);
470 sigaction(SIGXCPU, &session_xcpu_old_action, NULL);