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)
118 result = _mm_session_util_write_information(-1, ltype, loption);
120 debug_error("failed to _mm_session_util_write_information(), ret(%x)", result);
124 debug_log("[updated] session_type: %d, session_option: %x", ltype, loption);
127 return MM_ERROR_NONE;
131 int mm_session_get_current_type(int *sessiontype)
133 int result = MM_ERROR_NONE;
138 if (sessiontype == NULL) {
139 debug_error("input argument is NULL\n");
140 return MM_ERROR_INVALID_ARGUMENT;
143 result = _mm_session_util_read_type(-1, <ype);
144 if (result == MM_ERROR_NONE) {
145 debug_log("Current process session type = [%d]\n", ltype);
146 *sessiontype = ltype;
148 debug_error("failed to get current process session type!!\n");
157 int mm_session_get_current_information(int *session_type, int *session_options)
159 int result = MM_ERROR_NONE;
165 if (session_type == NULL) {
166 debug_error("input argument is NULL\n");
167 return MM_ERROR_INVALID_ARGUMENT;
170 result = _mm_session_util_read_information(-1, <ype, &loption);
171 if (result == MM_ERROR_NONE) {
172 debug_log("Current process session type = [%d], options = [%x]\n", ltype, loption);
173 *session_type = ltype;
174 *session_options = loption;
176 debug_error("failed to get current process session type, option!!\n");
185 int mm_session_finish(void)
187 int result = MM_ERROR_NONE;
188 int sessiontype = MM_SESSION_TYPE_MEDIA;
192 result = _mm_session_util_read_type(-1, &sessiontype);
193 if (MM_ERROR_NONE != result) {
194 debug_error("Can not read current type");
198 /* Check monitor handle */
199 result = _mm_session_util_delete_information(-1);
200 if (result != MM_ERROR_NONE)
205 return MM_ERROR_NONE;
209 int _mm_session_util_delete_information(int app_pid)
212 char filename[MAX_FILE_LENGTH];
217 mypid = (pid_t)app_pid;
219 ////// DELETE SESSION TYPE /////////
220 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
221 if (-1 == unlink(filename))
222 return MM_ERROR_FILE_NOT_FOUND;
223 ////// DELETE SESSION TYPE /////////
225 return MM_ERROR_NONE;
229 int _mm_session_util_write_type(int app_pid, int sessiontype)
233 char filename[MAX_FILE_LENGTH];
235 if (sessiontype < MM_SESSION_TYPE_MEDIA || sessiontype >= MM_SESSION_TYPE_NUM)
236 return MM_ERROR_INVALID_ARGUMENT;
241 mypid = (pid_t)app_pid;
243 ////// WRITE SESSION TYPE /////////
244 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
245 fd = open(filename, O_WRONLY | O_CREAT, 0644);
247 debug_error("open() failed with %d", errno);
248 return MM_ERROR_FILE_WRITE;
250 sessiontype = sessiontype << 16;
251 write(fd, &sessiontype, sizeof(int));
252 if (0 > fchmod(fd, 00777))
253 debug_error("fchmod failed with %d", errno);
255 debug_warning("write sessiontype(%d) to /tmp/mm_session_%d", sessiontype >> 16, mypid);
258 ////// WRITE SESSION TYPE /////////
260 return MM_ERROR_NONE;
264 int _mm_session_util_read_type(int app_pid, int *sessiontype)
268 char filename[MAX_FILE_LENGTH];
272 if (sessiontype == NULL)
273 return MM_ERROR_INVALID_ARGUMENT;
278 mypid = (pid_t)app_pid;
280 ////// READ SESSION TYPE /////////
281 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
282 fd = open(filename, O_RDONLY);
284 return MM_ERROR_INVALID_HANDLE;
286 read(fd, sessiontype, sizeof(int));
287 *sessiontype = *sessiontype >> 16;
288 debug_warning("read sessiontype(%d) from /tmp/mm_session_%d", *sessiontype, mypid);
290 ////// READ SESSION TYPE /////////
294 return MM_ERROR_NONE;
298 int _mm_session_util_write_information(int app_pid, int session_type, int flags)
302 char filename[MAX_FILE_LENGTH];
305 if ((session_type != MM_SESSION_TYPE_REPLACED_BY_STREAM) &&
306 (session_type < MM_SESSION_TYPE_MEDIA || session_type >= MM_SESSION_TYPE_NUM)) {
307 return MM_ERROR_INVALID_ARGUMENT;
310 return MM_ERROR_INVALID_ARGUMENT;
315 mypid = (pid_t)app_pid;
317 ////// WRITE SESSION INFO /////////
318 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
319 fd = open(filename, O_WRONLY | O_CREAT, 0644);
321 debug_error("open() failed with %d", errno);
322 return MM_ERROR_FILE_WRITE;
325 result_info = (flags) | (session_type << 16);
326 write(fd, &result_info, sizeof(int));
327 if (0 > fchmod(fd, 00777))
328 debug_error("fchmod failed with %d", errno);
330 debug_warning("write session information(%x) to /tmp/mm_session_%d", result_info, mypid);
332 ////// WRITE SESSION INFO /////////
334 return MM_ERROR_NONE;
338 int _mm_session_util_read_information(int app_pid, int *session_type, int *flags)
342 char filename[MAX_FILE_LENGTH];
347 if (session_type == NULL || flags == NULL)
348 return MM_ERROR_INVALID_ARGUMENT;
353 mypid = (pid_t)app_pid;
355 ////// READ SESSION INFO /////////
356 snprintf(filename, sizeof(filename)-1, "/tmp/mm_session_%d", mypid);
357 fd = open(filename, O_RDONLY);
359 return MM_ERROR_INVALID_HANDLE;
360 read(fd, &result_info, sizeof(int));
361 *session_type = result_info >> 16;
362 *flags = result_info & 0x0000ffff;
364 debug_warning("read session_type(%d), session_option(%x) from /tmp/mm_session_%d", *session_type, *flags, mypid);
366 ////// READ SESSION INFO /////////
370 return MM_ERROR_NONE;
373 void __session_signal_handler(int signo, siginfo_t *siginfo, void *context)
375 char filename[MAX_FILE_LENGTH];
378 debug_warning("ENTER, sig.num(%d)", signo);
380 /* signal block -------------- */
381 sigset_t old_mask, all_mask;
382 sigfillset(&all_mask);
383 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
385 snprintf(filename, sizeof(filename) - 1, "/tmp/mm_session_%d", getpid());
386 if (!remove(filename)) {
387 debug_log(" remove %s success\n", filename);
389 strerror_r(errno, str_error, sizeof(str_error));
390 debug_error(" remove %s failed with %s\n", filename, str_error);
393 sigprocmask(SIG_SETMASK, &old_mask, NULL);
394 /* signal unblock ------------ */
398 if (session_int_old_action.sa_sigaction)
399 session_int_old_action.sa_sigaction(signo, siginfo, context);
401 sigaction(signo, &session_int_old_action, NULL);
404 if (session_abrt_old_action.sa_sigaction)
405 session_abrt_old_action.sa_sigaction(signo, siginfo, context);
407 sigaction(signo, &session_abrt_old_action, NULL);
410 if (session_segv_old_action.sa_sigaction)
411 session_segv_old_action.sa_sigaction(signo, siginfo, context);
413 sigaction(signo, &session_segv_old_action, NULL);
416 if (session_term_old_action.sa_sigaction)
417 session_term_old_action.sa_sigaction(signo, siginfo, context);
419 sigaction(signo, &session_term_old_action, NULL);
422 if (session_sys_old_action.sa_sigaction)
423 session_sys_old_action.sa_sigaction(signo, siginfo, context);
425 sigaction(signo, &session_sys_old_action, NULL);
428 if (session_xcpu_old_action.sa_sigaction)
429 session_xcpu_old_action.sa_sigaction(signo, siginfo, context);
431 sigaction(signo, &session_xcpu_old_action, NULL);
437 debug_warning("LEAVE");
440 __attribute__ ((constructor))
441 void __mmsession_initialize(void)
443 struct sigaction session_action;
444 session_action.sa_sigaction = __session_signal_handler;
445 session_action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
449 sigemptyset(&session_action.sa_mask);
451 sigaction(SIGINT, &session_action, &session_int_old_action);
452 sigaction(SIGABRT, &session_action, &session_abrt_old_action);
453 sigaction(SIGSEGV, &session_action, &session_segv_old_action);
454 sigaction(SIGTERM, &session_action, &session_term_old_action);
455 sigaction(SIGSYS, &session_action, &session_sys_old_action);
456 sigaction(SIGXCPU, &session_action, &session_xcpu_old_action);
461 __attribute__ ((destructor))
462 void __mmsession_finalize(void)
467 _mm_session_util_delete_information(-1);
469 sigaction(SIGINT, &session_int_old_action, NULL);
470 sigaction(SIGABRT, &session_abrt_old_action, NULL);
471 sigaction(SIGSEGV, &session_segv_old_action, NULL);
472 sigaction(SIGTERM, &session_term_old_action, NULL);
473 sigaction(SIGSYS, &session_sys_old_action, NULL);
474 sigaction(SIGXCPU, &session_xcpu_old_action, NULL);