genmetaindex.py: fix metaindex.xml generation
[enigma2.git] / 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 #if _FILE_OFFSET_BITS != 64
49 int open(const char *pathname, int flags, ...)
50 {
51         typedef int (*FUNC_PTR) (const char* pathname, int flags, ...);
52         static FUNC_PTR libc_open;
53         int fd=-1;
54         if (!libc_open)
55         {
56                 void *handle;
57                 char *error;
58                 handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
59                 if (!handle)
60                 {
61                         fputs(dlerror(), stderr);
62                         exit(1);
63                 }
64                 libc_open = (FUNC_PTR) dlsym(handle, "open");
65                 if ((error = dlerror()) != NULL) {
66                         fprintf(stderr, "%s\n", error);
67                         exit(1);
68                 }
69         }
70         fd = libc_open(pathname, flags);
71         if (fd >= 0)
72         {
73                 int fd_flags = fcntl(fd, F_GETFD, 0);
74                 if (fd_flags >= 0)
75                 {
76                         fd_flags |= FD_CLOEXEC;
77                         fcntl(fd, F_SETFD, fd_flags);
78                 }
79 #ifdef DEBUG
80                 fprintf(stdout, "open %s, flags %d returned fd %d\n", pathname, flags, fd);
81 #endif
82         }
83         return fd;
84 }
85 #endif
86
87 FILE *fopen64(const char *pathname, const char *mode)
88 {
89         typedef FILE *(*FUNC_PTR) (const char* pathname, const char *mode);
90         static FUNC_PTR libc_fopen64;
91         FILE *f=0;
92         if (!libc_fopen64)
93         {
94                 void *handle;
95                 char *error;
96                 handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
97                 if (!handle)
98                 {
99                         fputs(dlerror(), stderr);
100                         exit(1);
101                 }
102                 libc_fopen64 = (FUNC_PTR) dlsym(handle, "fopen64");
103                 if ((error = dlerror()) != NULL) {
104                         fprintf(stderr, "%s\n", error);
105                         exit(1);
106                 }
107         }
108         f = libc_fopen64(pathname, mode);
109         if (f)
110         {
111                 int fd = fileno(f);
112                 int fd_flags = fcntl(fd, F_GETFD, 0);
113                 if (fd_flags >= 0)
114                 {
115                         fd_flags |= FD_CLOEXEC;
116                         fcntl(fd, F_SETFD, fd_flags);
117                 }
118 #ifdef DEBUG
119                 fprintf(stdout, "fopen64 %s, mode %s returned FILE* %p fd %d\n", pathname, mode, f, fd);
120 #endif
121         }
122         return f;
123 }
124
125 #if _FILE_OFFSET_BITS != 64
126 FILE *fopen(const char *pathname, const char *mode)
127 {
128         typedef FILE *(*FUNC_PTR) (const char* pathname, const char *mode);
129         static FUNC_PTR libc_fopen;
130         FILE *f=0;
131         if (!libc_fopen)
132         {
133                 void *handle;
134                 char *error;
135                 handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
136                 if (!handle)
137                 {
138                         fputs(dlerror(), stderr);
139                         exit(1);
140                 }
141                 libc_fopen = (FUNC_PTR) dlsym(handle, "fopen");
142                 if ((error = dlerror()) != NULL) {
143                         fprintf(stderr, "%s\n", error);
144                         exit(1);
145                 }
146         }
147         f = libc_fopen(pathname, mode);
148         if (f)
149         {
150                 int fd = fileno(f);
151                 int fd_flags = fcntl(fd, F_GETFD, 0);
152                 if (fd_flags >= 0)
153                 {
154                         fd_flags |= FD_CLOEXEC;
155                         fcntl(fd, F_SETFD, fd_flags);
156                 }
157 #ifdef DEBUG
158                 fprintf(stdout, "fopen %s, mode %s returned FILE* %p fd %d\n", pathname, mode, f, fd);
159 #endif
160         }
161         return f;
162 }
163 #endif
164
165 int socket(int domain, int type, int protocol)
166 {
167         typedef int (*FUNC_PTR) (int domain, int type, int protocol);
168         static FUNC_PTR libc_socket;
169         int fd=-1;
170         if (!libc_socket)
171         {
172                 void *handle;
173                 char *error;
174                 handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
175                 if (!handle)
176                 {
177                         fputs(dlerror(), stderr);
178                         exit(1);
179                 }
180                 libc_socket = (FUNC_PTR) dlsym(handle, "socket");
181                 if ((error = dlerror()) != NULL) {
182                         fprintf(stderr, "%s\n", error);
183                         exit(1);
184                 }
185         }
186         fd = libc_socket(domain, type, protocol);
187         if (fd >= 0)
188         {
189                 int fd_flags = fcntl(fd, F_GETFD, 0);
190                 if (fd_flags >= 0)
191                 {
192                         fd_flags |= FD_CLOEXEC;
193                         fcntl(fd, F_SETFD, fd_flags);
194                 }
195 #ifdef DEBUG
196                 fprintf(stdout, "socket fd %d\n", fd);
197 #endif
198         }
199         return fd;
200 }
201
202 int socketpair(int d, int type, int protocol, int sv[2])
203 {
204         typedef int (*FUNC_PTR) (int d, int type, int protocol, int sv[2]);
205         static FUNC_PTR libc_socketpair;
206         int ret=-1;
207         if (!libc_socketpair)
208         {
209                 void *handle;
210                 char *error;
211                 handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
212                 if (!handle)
213                 {
214                         fputs(dlerror(), stderr);
215                         exit(1);
216                 }
217                 libc_socketpair = (FUNC_PTR) dlsym(handle, "socketpair");
218                 if ((error = dlerror()) != NULL) {
219                         fprintf(stderr, "%s\n", error);
220                         exit(1);
221                 }
222         }
223         ret = libc_socketpair(d, type, protocol, sv);
224         if (!ret)
225         {
226                 int fd_flags = fcntl(sv[0], F_GETFD, 0);
227                 if (fd_flags >= 0)
228                 {
229                         fd_flags |= FD_CLOEXEC;
230                         fcntl(sv[0], F_SETFD, fd_flags);
231                 }
232                 fd_flags = fcntl(sv[1], F_GETFD, 0);
233                 if (fd_flags >= 0)
234                 {
235                         fd_flags |= FD_CLOEXEC;
236                         fcntl(sv[1], F_SETFD, fd_flags);
237                 }
238 #ifdef DEBUG
239                 fprintf(stdout, "socketpair fd %d %d\n", sv[0], sv[1]);
240 #endif
241         }
242         return ret;
243 }
244
245 int pipe(int modus[2])
246 {
247         typedef int (*FUNC_PTR) (int modus[2]);
248         static FUNC_PTR libc_pipe;
249         int ret=-1;
250         if (!libc_pipe)
251         {
252                 void *handle;
253                 char *error;
254                 handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
255                 if (!handle)
256                 {
257                         fputs(dlerror(), stderr);
258                         exit(1);
259                 }
260                 libc_pipe = (FUNC_PTR) dlsym(handle, "pipe");
261                 if ((error = dlerror()) != NULL) {
262                         fprintf(stderr, "%s\n", error);
263                         exit(1);
264                 }
265         }
266         ret = libc_pipe(modus);
267         if (!ret)
268         {
269                 int fd_flags = fcntl(modus[0], F_GETFD, 0);
270                 if (fd_flags >= 0)
271                 {
272                         fd_flags |= FD_CLOEXEC;
273                         fcntl(modus[0], F_SETFD, fd_flags);
274                 }
275                 fd_flags = fcntl(modus[1], F_GETFD, 0);
276                 if (fd_flags >= 0)
277                 {
278                         fd_flags |= FD_CLOEXEC;
279                         fcntl(modus[1], F_SETFD, fd_flags);
280                 }
281 #ifdef DEBUG
282                 fprintf(stdout, "pipe fds[%d, %d]\n", modus[0], modus[1]);
283 #endif
284         }
285         return ret;
286 }
287