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