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