tizen 2.3 release
[framework/system/swap-probe.git] / helper / dahelper.c
1 /*
2  *  DA probe
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  *
12  * This library is free software; you can redistribute it and/or modify it under
13  * the terms of the GNU Lesser General Public License as published by the
14  * Free Software Foundation; either version 2.1 of the License, or (at your option)
15  * any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this library; if not, write to the Free Software Foundation, Inc., 51
24  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  *
26  * Contributors:
27  * - S-Core Co., Ltd
28  *
29  */
30
31 #include <wchar.h>                      // for wcslen
32 #include <unistd.h>                     // for unlink
33 #include <sys/types.h>
34 #include <dirent.h>                     // for opendir, readdir
35 #include <assert.h>
36 #include "dahelper.h"
37
38 int app_efl_main_flg = 0;
39
40 const char *lib_string[NUM_ORIGINAL_LIBRARY] = {
41         "libc.so.6",                            //0
42         "libpthread.so.0",                      //1
43         "libelementary.so.1",                   //2
44         "libecore_input_evas.so.1",             //3
45         "libdaemon.so.0",                       //4
46         "libcapi-appfw-application.so.0",       //5
47         "libGLESv2.so",                         //6
48         "libEGL.so",                            //7
49         SELF_LIB_NAME                           //8
50 };
51 void *lib_handle[NUM_ORIGINAL_LIBRARY];
52
53 /* trace info global variable */
54 __traceInfo gTraceInfo =
55 {
56         {
57                 1,                                                      // int eventIndex
58                 PTHREAD_MUTEX_INITIALIZER       // pthread_mutex_t eventMutex
59         },              // __indexInfo
60         {
61                 -1,                                                     // int daemonSock
62                 PTHREAD_MUTEX_INITIALIZER       // ptrhread_mutex_t sockMutex
63         },              // __socketInfo
64         {
65                 {0, },                                          // char appName[128]
66                 0                                                       // unsigned int startTime
67         },              // __appInfo
68         {
69                 0,                                                      // int state
70                 PTHREAD_MUTEX_INITIALIZER       // ptrhread_mutex_t ssMutex
71         },              // __screenshotInfo
72         {
73                 NULL,                                           // map_start
74                 NULL                                            // map_end
75         },              // __mapInfo
76         -1,                                                             // int stateTouch
77         0,                                                              // int init_complete
78         0,                                                              // int custom_chart_callback_count
79         0                                                               // unsigned long optionflag
80 };
81
82 void WcharToChar(char* pstrDest, const wchar_t* pwstrSrc)
83 {
84         int nLen=(int)wcslen(pwstrSrc);
85         wcstombs(pstrDest, pwstrSrc, nLen+1);
86 }
87
88 // return 0 if succeed
89 // return -1 if error occured
90 int remove_indir(const char *dirname)
91 {
92         DIR *dir;
93         struct dirent *entry;
94         char path[MAX_PATH_LENGTH];
95         static char dirent_buffer[ sizeof(struct dirent) + PATH_MAX + 1 ] = {0,};
96         static struct dirent *dirent_r = (struct dirent *)dirent_buffer;
97
98
99         dir = opendir(dirname);
100         if(dir == NULL)
101         {
102                 return -1;
103         }
104
105         while ((readdir_r(dir, dirent_r, &entry) == 0) && entry) {
106                 if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
107                 {
108                         snprintf(path, (size_t) MAX_PATH_LENGTH, "%s/%s", dirname, entry->d_name);
109                         if (entry->d_type != DT_DIR)    // file
110                         {
111                                 unlink(path);
112                         }
113                         else { }        // directory
114                 }
115         }
116         closedir(dir);
117
118         return 0;
119 }
120
121 static int absolute_filepath_p(const char *fname)
122 {
123         return fname[0] == '/';
124 }
125
126 char *absolutize_filepath(const char *fname, char *buffer, size_t bufsiz)
127 {
128         char cwd[PATH_MAX];
129
130         assert(fname && "Filename, passed to stdc function is NULL.");
131         if (absolute_filepath_p(fname) || getcwd(cwd, sizeof(cwd)) == NULL)
132                 snprintf(buffer, bufsiz, "%s", fname);
133         else
134                 snprintf(buffer, bufsiz, "%s/%s", cwd, fname);
135         return buffer;
136 }
137
138 char *real_abs_path(int fd, char *buffer, size_t bufsiz)
139 {
140         static const char *PROC_FD = "/proc/self/fd/%d";
141         char proc_path[sizeof(PROC_FD) + 16]; /* PATH_MAX not needed here */
142         ssize_t ret = 0;
143
144         if (fd < 0)
145                 return NULL;
146
147         snprintf(proc_path, sizeof(proc_path), PROC_FD, fd);
148
149         ret = readlink(proc_path, buffer, bufsiz);
150         if (ret < 0) /* some error occured */
151                 return NULL;
152         buffer[ret] = '\0';
153
154         return buffer;
155 }