b3b5dce7a5a0ab25f660740f1c73291f0c2bafb6
[vuplus_dvbapp] / tools / libopen.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <sys/socket.h>
4 #include <fcntl.h>
5 #include <dlfcn.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #undef DEBUG
10
11 int open64(const char *pathname, int flags, ...)
12 {
13         typedef int (*FUNC_PTR) (const char* pathname, int flags, ...);
14         static FUNC_PTR libc_open64;
15         int fd=-1;
16         if (!libc_open64)
17         {
18                 void *handle;
19                 char *error;
20                 handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
21                 if (!handle)
22                 {
23                         fputs(dlerror(), stderr);
24                         exit(1);
25                 }
26                 libc_open64 = (FUNC_PTR) dlsym(handle, "open64");
27                 if ((error = dlerror()) != NULL) {
28                         fprintf(stderr, "%s\n", error);
29                         exit(1);
30                 }
31         }
32         fd = libc_open64(pathname, flags);
33         if (fd >= 0)
34         {
35                 int fd_flags = fcntl(fd, F_GETFD, 0);
36                 if (fd_flags >= 0)
37                 {
38                         fd_flags |= FD_CLOEXEC;
39                         fcntl(fd, F_SETFD, fd_flags);
40                 }
41 #ifdef DEBUG
42                 fprintf(stdout, "open64 %s, flags %d returned fd %d\n", pathname, flags, fd);
43 #endif
44         }
45         return fd;
46 }
47
48 int open(const char *pathname, int flags, ...)
49 {
50         typedef int (*FUNC_PTR) (const char* pathname, int flags, ...);
51         static FUNC_PTR libc_open;
52         int fd=-1;
53         if (!libc_open)
54         {
55                 void *handle;
56                 char *error;
57                 handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
58                 if (!handle)
59                 {
60                         fputs(dlerror(), stderr);
61                         exit(1);
62                 }
63                 libc_open = (FUNC_PTR) dlsym(handle, "open");
64                 if ((error = dlerror()) != NULL) {
65                         fprintf(stderr, "%s\n", error);
66                         exit(1);
67                 }
68         }
69         fd = libc_open(pathname, flags);
70         if (fd >= 0)
71         {
72                 int fd_flags = fcntl(fd, F_GETFD, 0);
73                 if (fd_flags >= 0)
74                 {
75                         fd_flags |= FD_CLOEXEC;
76                         fcntl(fd, F_SETFD, fd_flags);
77                 }
78 #ifdef DEBUG
79                 fprintf(stdout, "open %s, flags %d returned fd %d\n", pathname, flags, fd);
80 #endif
81         }
82         return fd;
83 }
84
85 FILE *fopen64(const char *pathname, const char *mode)
86 {
87         typedef FILE *(*FUNC_PTR) (const char* pathname, const char *mode);
88         static FUNC_PTR libc_fopen64;
89         FILE *f=0;
90         if (!libc_fopen64)
91         {
92                 void *handle;
93                 char *error;
94                 handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
95                 if (!handle)
96                 {
97                         fputs(dlerror(), stderr);
98                         exit(1);
99                 }
100                 libc_fopen64 = (FUNC_PTR) dlsym(handle, "fopen64");
101                 if ((error = dlerror()) != NULL) {
102                         fprintf(stderr, "%s\n", error);
103                         exit(1);
104                 }
105         }
106         f = libc_fopen64(pathname, mode);
107         if (f)
108         {
109                 int fd = fileno(f);
110                 int fd_flags = fcntl(fd, F_GETFD, 0);
111                 if (fd_flags >= 0)
112                 {
113                         fd_flags |= FD_CLOEXEC;
114                         fcntl(fd, F_SETFD, fd_flags);
115                 }
116 #ifdef DEBUG
117                 fprintf(stdout, "fopen64 %s, mode %s returned FILE* %p fd %d\n", pathname, mode, f, fd);
118 #endif
119         }
120         return f;
121 }
122
123 FILE *fopen(const char *pathname, const char *mode)
124 {
125         typedef FILE *(*FUNC_PTR) (const char* pathname, const char *mode);
126         static FUNC_PTR libc_fopen;
127         FILE *f=0;
128         if (!libc_fopen)
129         {
130                 void *handle;
131                 char *error;
132                 handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
133                 if (!handle)
134                 {
135                         fputs(dlerror(), stderr);
136                         exit(1);
137                 }
138                 libc_fopen = (FUNC_PTR) dlsym(handle, "fopen");
139                 if ((error = dlerror()) != NULL) {
140                         fprintf(stderr, "%s\n", error);
141                         exit(1);
142                 }
143         }
144         f = libc_fopen(pathname, mode);
145         if (f)
146         {
147                 int fd = fileno(f);
148                 int fd_flags = fcntl(fd, F_GETFD, 0);
149                 if (fd_flags >= 0)
150                 {
151                         fd_flags |= FD_CLOEXEC;
152                         fcntl(fd, F_SETFD, fd_flags);
153                 }
154 #ifdef DEBUG
155                 fprintf(stdout, "fopen %s, mode %s returned FILE* %p fd %d\n", pathname, mode, f, fd);
156 #endif
157         }
158         return f;
159 }
160
161 int socket(int domain, int type, int protocol)
162 {
163         typedef int (*FUNC_PTR) (int domain, int type, int protocol);
164         static FUNC_PTR libc_socket;
165         int fd=-1;
166         if (!libc_socket)
167         {
168                 void *handle;
169                 char *error;
170                 handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
171                 if (!handle)
172                 {
173                         fputs(dlerror(), stderr);
174                         exit(1);
175                 }
176                 libc_socket = (FUNC_PTR) dlsym(handle, "socket");
177                 if ((error = dlerror()) != NULL) {
178                         fprintf(stderr, "%s\n", error);
179                         exit(1);
180                 }
181         }
182         fd = libc_socket(domain, type, protocol);
183         if (fd >= 0)
184         {
185                 int fd_flags = fcntl(fd, F_GETFD, 0);
186                 if (fd_flags >= 0)
187                 {
188                         fd_flags |= FD_CLOEXEC;
189                         fcntl(fd, F_SETFD, fd_flags);
190                 }
191 #ifdef DEBUG
192                 fprintf(stdout, "socket fd %d\n", fd);
193 #endif
194         }
195         return fd;
196 }
197
198 int pipe(int modus[2])
199 {
200         typedef int (*FUNC_PTR) (int modus[2]);
201         static FUNC_PTR libc_pipe;
202         int ret=-1;
203         if (!libc_pipe)
204         {
205                 void *handle;
206                 char *error;
207                 handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
208                 if (!handle)
209                 {
210                         fputs(dlerror(), stderr);
211                         exit(1);
212                 }
213                 libc_pipe = (FUNC_PTR) dlsym(handle, "pipe");
214                 if ((error = dlerror()) != NULL) {
215                         fprintf(stderr, "%s\n", error);
216                         exit(1);
217                 }
218         }
219         ret = libc_pipe(modus);
220         if (!ret)
221         {
222                 int fd_flags = fcntl(modus[0], F_GETFD, 0);
223                 if (fd_flags >= 0)
224                 {
225                         fd_flags |= FD_CLOEXEC;
226                         fcntl(modus[0], F_SETFD, fd_flags);
227                 }
228                 fd_flags = fcntl(modus[1], F_GETFD, 0);
229                 if (fd_flags >= 0)
230                 {
231                         fd_flags |= FD_CLOEXEC;
232                         fcntl(modus[1], F_SETFD, fd_flags);
233                 }
234 #ifdef DEBUG
235                 fprintf(stdout, "pipe fds[%d, %d]\n", modus[0], modus[1]);
236 #endif
237         }
238         return ret;
239 }
240