fix prevent violation
[framework/system/dynamic-analysis-probe.git] / probe_third / libdaemon.c
1 /*
2  *  DA probe
3  *
4  * Copyright (c) 2000 - 2011 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 <stdlib.h>     // for malloc
32 #include <string.h>     // for memcpy
33 #include <errno.h>      // for errno
34
35 #include "daprobe.h"
36 #include "dahelper.h"
37
38 int daemon_close_allv(const int except_fds[])
39 {
40         static int (*daemon_close_allvp)(const int except_fds[]);
41         int i, saved_errno, ret;
42         int* fds;
43
44         GET_REAL_FUNCP(daemon_close_allv, LIBDAEMON, daemon_close_allvp);
45
46         probeBlockStart();
47         // get number of fds
48         for(i = 0; ; i++)
49         {
50                 if(except_fds[i] == -1)
51                         break;
52         }
53
54         // allocate memory for new except fds
55         fds = (int*)malloc((i + 2) * sizeof(int));
56
57         // copy fds
58         if(fds)
59         {
60                 if(i > 0)
61                         memcpy(fds, except_fds, i * sizeof(int));
62                 fds[i] = gTraceInfo.socket.daemonSock;
63                 fds[i + 1] = -1;
64         }
65         else
66         {
67                 // do nothing
68         }
69         probeBlockEnd();
70
71         // call original function
72         if(fds)
73         {
74                 ret = daemon_close_allvp(fds);
75         }
76         else
77         {
78                 ret = daemon_close_allvp(except_fds);
79         }
80
81         probeBlockStart();
82         saved_errno = errno;
83         free(fds);
84         errno = saved_errno;
85         probeBlockEnd();
86
87         return ret;
88 }
89