[FIX] prevent issue
[platform/core/system/swap-manager.git] / daemon / da_debug.c
1 /*
2  *  DA manager
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Jaewon Lim <jaewon81.lim@samsung.com>
9  * Woojin Jung <woojin2.jung@samsung.com>
10  * Juyoung Kim <j0.kim@samsung.com>
11  * Cherepanov Vitaliy <v.cherepanov@samsung.com>
12  * Nikita Kalyazin    <n.kalyazin@samsung.com>
13  *
14  * Licensed under the Apache License, Version 2.0 (the "License");
15  * you may not use this file except in compliance with the License.
16  * You may obtain a copy of the License at
17  *
18  * http://www.apache.org/licenses/LICENSE-2.0
19  *
20  * Unless required by applicable law or agreed to in writing, software
21  * distributed under the License is distributed on an "AS IS" BASIS,
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23  * See the License for the specific language governing permissions and
24  * limitations under the License.
25  *
26  * Contributors:
27  * - S-Core Co., Ltd
28  * - Samsung RnD Institute Russia
29  *
30  */
31 #define _GNU_SOURCE
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37
38 #include "daemon.h"
39 #include "debug.h"
40 #include <errno.h>
41
42 #define DEBUG_LOGFILE           "/tmp/daemonlog.da"
43
44 #if DEBUG
45 static inline int close_on_exec_dup(int old, int new)
46 {
47         int ret = -1;
48
49         if (dup2(old, new) != -1) {
50                 unsigned long flags = fcntl(new, F_GETFD);
51                 if (flags == -1) {
52                         LOGE("can not get flags fd #%d errno <%d>\n", new,
53                              errno);
54                         goto err_ret;
55                 }
56
57                 if (fcntl(new, F_SETFD, flags | FD_CLOEXEC) == -1) {
58                         LOGE("can not get flags fd #%d errno <%d>\n", new,
59                              errno);
60                         goto err_ret;
61                 }
62         } else {
63                 LOGE("dup2 fail\n");
64                 goto err_ret;
65         }
66
67         /* success */
68         ret = 0;
69
70 err_ret:
71         return ret;
72 }
73
74 int initialize_log(void)
75 {
76         /* TODO fix problem with ecore and redirect stderr to DEBUG_LOGFILE back
77          *
78          * error sample
79          * *** IN FUNCTION: _ecore_main_fdh_epoll_mark_active()
80          * ERR<2328>:ecore ecore.c:572 _ecore_magic_fail()   Input handle has already been freed!
81          * ERR<2328>:ecore ecore.c:581 _ecore_magic_fail() *** NAUGHTY PROGRAMMER!!!
82          * *** SPANK SPANK SPANK!!!
83          *
84          */
85         int ret = 0;
86         int fd = -1;
87         int fd_null = -1;
88
89         if (remove(DEBUG_LOGFILE))
90                 LOGE("remove(%s), return error, errno=%d\n",
91                      DEBUG_LOGFILE, errno);
92
93         fd = open(DEBUG_LOGFILE, O_WRONLY | O_CREAT | O_TRUNC, 0777);
94         fd_null = open("/dev/null", O_WRONLY | O_CREAT | O_TRUNC, 0777);
95
96         if (fd != -1 && fd_null != -1) {
97                 if (close_on_exec_dup(fd_null, 1) != 0 ||
98                     close_on_exec_dup(fd, 2) != 0) {
99                         LOGE("duplicate fd fail\n");
100                         ret = -1;
101                 }
102         } else {
103                 close(1);
104                 close(2);
105         }
106
107         if (fd_null != -1)
108                 close(fd_null);
109
110         if (fd != -1)
111                 close(fd);
112
113         close(0);
114         return ret;
115 }
116
117 #else
118 int initialize_log(void)
119 {
120         return 0;
121 }
122 #endif