summaryrefslogtreecommitdiff
path: root/legacy/escape/src
diff options
context:
space:
mode:
authorYouness Alaoui <kakaroto@kakaroto.homelinux.net>2011-09-07 07:18:07 +0000
committerYouness Alaoui <kakaroto@kakaroto.homelinux.net>2011-09-07 07:18:07 +0000
commitf26611c3de57b4a90232aed44535e80d3c855ea7 (patch)
tree9ed07ed608ba4a47b17d6b62af791ef6cb1033ef /legacy/escape/src
parentdf91f38ffbbcbaa375e4f62d984d7ef1e3000d91 (diff)
Adding Escape: A compatibility library for jailbroken PS3s (PSL1GHT)
SVN revision: 63260
Diffstat (limited to 'legacy/escape/src')
-rw-r--r--legacy/escape/src/Makefile.am3
-rw-r--r--legacy/escape/src/lib/Escape.h53
-rw-r--r--legacy/escape/src/lib/Makefile.am31
-rw-r--r--legacy/escape/src/lib/escape_libgen.c89
-rw-r--r--legacy/escape/src/lib/escape_libgen.h35
-rw-r--r--legacy/escape/src/lib/escape_mman.c67
-rw-r--r--legacy/escape/src/lib/escape_unistd.c181
-rw-r--r--legacy/escape/src/lib/escape_unistd.h106
-rw-r--r--legacy/escape/src/lib/sys/mman.h183
9 files changed, 748 insertions, 0 deletions
diff --git a/legacy/escape/src/Makefile.am b/legacy/escape/src/Makefile.am
new file mode 100644
index 0000000000..bba4a0c648
--- /dev/null
+++ b/legacy/escape/src/Makefile.am
@@ -0,0 +1,3 @@
1SUBDIRS = lib
2
3MAINTAINERCLEANFILES = Makefile.in
diff --git a/legacy/escape/src/lib/Escape.h b/legacy/escape/src/lib/Escape.h
new file mode 100644
index 0000000000..28b5fca7fe
--- /dev/null
+++ b/legacy/escape/src/lib/Escape.h
@@ -0,0 +1,53 @@
1#ifndef __ESCAPE_H__
2#define __ESCAPE_H__
3
4#ifndef EAPI
5#define EAPI
6#endif /* EAPI */
7#define __UNUSED__ __attribute__((unused))
8
9#include <unistd.h>
10
11#ifdef CLOCK_REALTIME
12#undef CLOCK_REALTIME
13#endif
14#ifdef CLOCK_PROF
15#undef CLOCK_PROF
16#endif
17#ifdef CLOCK_PROCESS_CPUTIME_ID
18#undef CLOCK_PROCESS_CPUTIME_ID
19#endif
20
21
22/**
23 * @mainpage Escape
24 * @image html e_big.png
25 * @author Youness Alaoui
26 * @date 2011
27 *
28 * @section intro_sec Introduction
29 *
30 * The Escape library is a library that acts implements some of the missing
31 * function from the PS3 GameOS system that are needed for the proper
32 * functioning of the EFL.
33 *
34 * @section escape_sec Escape API Documentation
35 *
36 * Take a look at the documentation of @ref mmap/munmap.
37 *
38 */
39
40#include "escape_libgen.h"
41#include "escape_unistd.h"
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47 /* Nothing to see here */
48
49#ifdef __cplusplus
50}
51#endif
52
53#endif /* __ESCAPE_H__ */
diff --git a/legacy/escape/src/lib/Makefile.am b/legacy/escape/src/lib/Makefile.am
new file mode 100644
index 0000000000..99949b190d
--- /dev/null
+++ b/legacy/escape/src/lib/Makefile.am
@@ -0,0 +1,31 @@
1MAINTAINERCLEANFILES = Makefile.in
2
3AM_CPPFLAGS = \
4-I$(top_builddir)/src \
5-DPACKAGE_BIN_DIR=\"$(bindir)\" \
6-DPACKAGE_LIB_DIR=\"$(libdir)\" \
7-DPACKAGE_DATA_DIR=\"$(datadir)/$(PACKAGE)\" \
8@ESCAPE_CPPFLAGS@
9
10base_sources = \
11escape_mman.c \
12escape_libgen.c \
13escape_unistd.c
14
15EXTRA_DIST = \
16sys/mman.h \
17Escape.h
18
19nobase_stdheadersdir = $(includedir)/escape-@VMAJ@
20dist_nobase_stdheaders_DATA = Escape.h escape_libgen.h escape_unistd.h
21stdheadersdir = $(includedir)/escape-@VMAJ@/sys
22dist_stdheaders_DATA = sys/mman.h
23
24
25lib_LTLIBRARIES = libescape.la
26
27libescape_la_SOURCES = $(base_sources)
28
29libescape_la_LIBADD = @iconv_libs@ @rt_libs@ @ESCAPE_LIBS@
30libescape_la_LDFLAGS = -no-undefined @lt_enable_auto_import@ -version-info @version_info@ @release_info@
31libescape_la_CFLAGS = @ESCAPE_CFLAGS@
diff --git a/legacy/escape/src/lib/escape_libgen.c b/legacy/escape/src/lib/escape_libgen.c
new file mode 100644
index 0000000000..1e63a363b6
--- /dev/null
+++ b/legacy/escape/src/lib/escape_libgen.c
@@ -0,0 +1,89 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif /* HAVE_CONFIG_H */
4
5#include <string.h>
6#include <malloc.h>
7
8#include "Escape.h"
9
10static char _escape_basename_buf[PATH_MAX];
11static char _escape_dirname_buf[PATH_MAX];
12
13char *
14escape_basename(char *path)
15{
16 char *p1;
17 char *p2;
18 size_t length;
19
20 /* path must begin by "?:\" or "?:/" */
21 if (!path || strlen(path) <= 1)
22 {
23 memcpy(_escape_basename_buf, path, PATH_MAX);
24 return _escape_basename_buf;
25 }
26
27 length = strlen(path);
28 p1 = strdup(path);
29 if (!p1)
30 {
31 memcpy(_escape_basename_buf, path, PATH_MAX);
32 return _escape_basename_buf;
33 }
34
35 /* remove trailing backslashes */
36 p2 = p1 + (length - 1);
37 if (*p2 == '/')
38 {
39 while (*p2 == '/')
40 p2--;
41 }
42 *(p2 + 1) = '\0';
43
44 p2 = strrchr(p1, '/');
45 memcpy(_escape_basename_buf, p2 + 1, (p1 + length + 1) - p2);
46
47 free(p1);
48
49 return _escape_basename_buf;
50}
51
52char *
53escape_dirname(char *path)
54{
55 char *p1;
56 char *p2;
57 size_t length;
58
59 /* path must begin by "?:\" or "?:/" */
60 if (!path || strlen(path) <= 1)
61 {
62 memcpy(_escape_dirname_buf, path, PATH_MAX);
63 return _escape_dirname_buf;
64 }
65
66 length = strlen(path);
67 p1 = strdup(path);
68 if (!p1)
69 {
70 memcpy(_escape_dirname_buf, path, PATH_MAX);
71 return _escape_dirname_buf;
72 }
73 /* remove trailing backslashes */
74 p2 = p1 + (length - 1);
75 if (*p2 == '/')
76 {
77 while (*p2 == '/')
78 p2--;
79 }
80 *(p2 + 1) = '\0';
81
82 p2 = strrchr(p1, '/');
83 *p2 = '\0';
84 memcpy(_escape_dirname_buf, p1, strlen(p1) + 1);
85
86 free(p1);
87
88 return _escape_dirname_buf;
89}
diff --git a/legacy/escape/src/lib/escape_libgen.h b/legacy/escape/src/lib/escape_libgen.h
new file mode 100644
index 0000000000..8bdde7c7e9
--- /dev/null
+++ b/legacy/escape/src/lib/escape_libgen.h
@@ -0,0 +1,35 @@
1#ifndef __ESCAPE_LIBGEN_H__
2#define __ESCAPE_LIBGEN_H__
3
4
5/**
6 * @file escape_libgen.h
7 * @brief The file that provides functions ported from Unix in libgen.h.
8 * @defgroup Escape_Libgen_Group Libgen.h functions.
9 *
10 * This header provides functions ported from Unix in libgen.h.
11 *
12 * @{
13 */
14
15EAPI char *escape_basename(char *path);
16
17#ifdef basename
18#undef basename
19#endif
20#define basename(p) escape_basename(p)
21
22EAPI char *escape_dirname(char *path);
23
24#ifdef dirname
25#undef dirname
26#endif
27#define dirname(p) escape_dirname(p)
28
29
30/**
31 * @}
32 */
33
34
35#endif /* __ESCAPE_LIBGEN_H__ */
diff --git a/legacy/escape/src/lib/escape_mman.c b/legacy/escape/src/lib/escape_mman.c
new file mode 100644
index 0000000000..7b52804328
--- /dev/null
+++ b/legacy/escape/src/lib/escape_mman.c
@@ -0,0 +1,67 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif /* HAVE_CONFIG_H */
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <sys/types.h>
8#include <unistd.h>
9
10#include "sys/mman.h"
11
12/***** API *****/
13
14
15void *
16mmap(void *addr __UNUSED__,
17 size_t len,
18 int prot,
19 int flags,
20 int fd,
21 off_t offset)
22{
23 void *data;
24 size_t size;
25
26 data = malloc(len);
27 if (!data)
28 {
29 fprintf (stderr, "[Escape] [mmap] malloc failed\n");
30 return MAP_FAILED;
31 }
32
33 size = read(fd, data, len);
34 if (size != len)
35 {
36 fprintf (stderr, "[Escape] [mmap] read failed\n");
37 free(data);
38 return MAP_FAILED;
39 }
40
41 if (lseek(fd, -len, SEEK_CUR) == -1)
42 {
43 fprintf (stderr, "[Escape] [mmap] lseek failed\n");
44 free(data);
45 return MAP_FAILED;
46 }
47
48 return data;
49}
50
51int
52munmap(void *addr,
53 size_t len __UNUSED__)
54{
55 if (addr && (addr != MAP_FAILED))
56 free(addr);
57
58 return 0;
59}
60
61
62int madvise(void *addr __UNUSED__,
63 size_t length __UNUSED__,
64 int advice __UNUSED__)
65{
66 return 0;
67}
diff --git a/legacy/escape/src/lib/escape_unistd.c b/legacy/escape/src/lib/escape_unistd.c
new file mode 100644
index 0000000000..d7106eceef
--- /dev/null
+++ b/legacy/escape/src/lib/escape_unistd.c
@@ -0,0 +1,181 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif /* HAVE_CONFIG_H */
4
5#ifdef HAVE_ERRNO_H
6#include <errno.h>
7#endif /* HAVE_ERRNO_H */
8
9#include <sys/types.h> /* See NOTES */
10#include <sys/socket.h>
11#include <net/socket.h>
12#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <net/net.h>
15#include <string.h>
16#include <libiberty.h>
17#include <sys/stat.h>
18
19
20#include "Escape.h"
21
22char *escape_realpath(const char *path, char *resolved_path)
23{
24 char *real = lrealpath (path);
25
26 if (real)
27 {
28 if (resolved_path)
29 {
30 memcpy (resolved_path, real, PATH_MAX);
31 free (real);
32 return resolved_path;
33 }
34 else
35 {
36 return real;
37 }
38 }
39
40 return NULL;
41}
42
43
44
45int escape_access(const char *pathname, int mode)
46{
47 struct stat stat_buf;
48
49 if (stat(pathname, &stat_buf) != 0)
50 return -1;
51
52 if (mode == F_OK)
53 return 0;
54 if (mode == R_OK) {
55 if (stat_buf.st_mode & S_IRUSR)
56 return 0;
57 errno = EACCES;
58 return -1;
59 }
60 if (mode == W_OK) {
61 if (stat_buf.st_mode & S_IWUSR)
62 return 0;
63 errno = EROFS;
64 return -1;
65 }
66 if (mode == X_OK) {
67 if (stat_buf.st_mode & S_IXUSR)
68 return 0;
69 errno = EACCES;
70 return -1;
71 }
72
73 return 0;
74}
75
76EAPI ssize_t escape_readlink(const char *path,
77 char * buf,
78 size_t bufsize)
79{
80 errno = EINVAL;
81 return -1;
82}
83
84EAPI int escape_symlink(const char *path1, const char *path2)
85{
86 errno = EINVAL;
87 return -1;
88}
89
90/*
91 * The code of the following functions has been kindly offered
92 * by Tor Lillqvist.
93 */
94int
95escape_pipe(int *fds)
96{
97 struct sockaddr_in saddr;
98 int temp;
99 int socket1 = -1;
100 int socket2 = -1;
101 fd_set read_set;
102 fd_set write_set;
103 int len;
104
105 temp = socket (AF_INET, SOCK_STREAM, 0);
106
107 if (temp == -1)
108 goto out0;
109
110 memset (&saddr, 0, sizeof (saddr));
111 saddr.sin_family = AF_INET;
112 saddr.sin_port = 0;
113 saddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
114
115 if (bind (temp, (struct sockaddr *)&saddr, sizeof (saddr)))
116 goto out0;
117
118 if (listen (temp, 1) == -1)
119 goto out0;
120
121 len = sizeof (saddr);
122 if (getsockname (temp, (struct sockaddr *)&saddr, &len))
123 goto out0;
124
125 socket1 = socket (AF_INET, SOCK_STREAM, 0);
126
127 if (socket1 == -1)
128 goto out0;
129
130
131 if ((connect (socket1, (struct sockaddr *)&saddr, len) == -1) &&
132 (errno != EAGAIN))
133 goto out1;
134
135 FD_ZERO (&read_set);
136 FD_SET (temp, &read_set);
137
138 if (select (0, &read_set, NULL, NULL, NULL) == -1)
139 goto out1;
140
141 if (!FD_ISSET (temp, &read_set))
142 goto out1;
143
144 socket2 = accept (temp, (struct sockaddr *) &saddr, &len);
145 if (socket2 == -1)
146 goto out1;
147
148 FD_ZERO (&write_set);
149 FD_SET (socket1, &write_set);
150
151 if (select (0, NULL, &write_set, NULL, NULL) == -1)
152 goto out2;
153
154 if (!FD_ISSET (socket1, &write_set))
155 goto out2;
156
157 fds[0] = socket1;
158 fds[1] = socket2;
159
160 closesocket (temp);
161
162 return 0;
163
164 out2:
165 closesocket (socket2);
166 out1:
167 closesocket (socket1);
168 out0:
169 closesocket (temp);
170
171 fds[0] = -1;
172 fds[1] = -1;
173
174 return -1;
175}
176
177#undef access
178int access(const char *pathname, int mode)
179{
180 return escape_access (pathname, mode);
181}
diff --git a/legacy/escape/src/lib/escape_unistd.h b/legacy/escape/src/lib/escape_unistd.h
new file mode 100644
index 0000000000..3fc9385e88
--- /dev/null
+++ b/legacy/escape/src/lib/escape_unistd.h
@@ -0,0 +1,106 @@
1#ifndef __ESCAPE_UNISTD_H__
2#define __ESCAPE_UNISTD_H__
3
4
5/**
6 * @file escape_unistd.h
7 * @brief The file that provides functions ported from Unix in unistd.h.
8 * @defgroup Escape_Unistd_Group Unistd.h functions
9 *
10 * This header provides functions ported from Unix in unistd.h.
11 *
12 * @{
13 */
14
15/* Path function */
16/**
17 * @brief return the canonicalized absolute pathname
18 *
19 * @param path A path to canonicalize
20 * @param resolved_path A pointer of size PATH_MAX where to store the result
21 *
22 * realpath() expands all symbolic links and resolves references to /./, /../
23 * and extra '/' characters in the null-terminated string named by path to
24 * produce a canonicalized absolute pathname. The resulting pathname is
25 * stored as a null-terminated string, up to a maximum of PATH_MAX bytes,
26 * in the buffer pointed to by resolved_path. The resulting path will have
27 * no symbolic link, /./ or /../ components.
28 *
29 * If resolved_path is specified as NULL, then realpath() uses malloc(3)
30 * to allocate a buffer of up to PATH_MAX bytes to hold the resolved pathname,
31 * and returns a pointer to this buffer.
32 * The caller should deallocate this buffer using free(3).
33 *
34 */
35EAPI char *escape_realpath(const char *path, char *resolved_path);
36#ifdef realpath
37#undef realpath
38#endif
39#define realpath escape_realpath
40
41EAPI ssize_t escape_readlink(const char * path,
42 char * buf,
43 size_t bufsize);
44#ifdef readlink
45#undef readlink
46#endif
47#define readlink escape_readlink
48
49EAPI int escape_symlink(const char *path1, const char *path2);
50#ifdef symlink
51#undef symlink
52#endif
53#define symlink escape_symlink
54
55
56/**
57 * @brief check real user's permissions for a file
58 *
59 * @param pathname The path to check
60 * @param mode the permission to check
61 *
62 * access() checks whether the calling process can access the file pathname.
63 * If pathname is a symbolic link, it is dereferenced.
64 * The mode specifies the accessibility check(s) to be performed, and is either
65 * the value F_OK, or a mask consisting of the bitwise OR of one or more
66 * of R_OK, W_OK, and X_OK. F_OK tests for the existence of the file.
67 * R_OK, W_OK, and X_OK test whether the file exists and grants read, write,
68 * and execute permissions, respectively.
69 *
70 */
71EAPI int escape_access(const char *pathname, int mode);
72#ifdef access
73#undef access
74#endif
75#define access escape_access
76
77/**
78 * @brief Create a pair of sockets.
79 *
80 * @param fds A pointer that contains two sockets.
81 *
82 * Create a pair of sockets that can be use with select().
83 * Contrary to Unix, that functions does not
84 * create a pair of file descriptors.
85 *
86 * Conformity: Not applicable.
87 */
88EAPI int escape_pipe(int *fds);
89
90/**
91 * @def pipe(fds)
92 *
93 * Wrapper around escape_pipe().
94 */
95#define pipe(fds) escape_pipe(fds)
96
97
98//#define sync()
99
100
101/**
102 * @}
103 */
104
105
106#endif /* __ESCAPE_UNISTD_H__ */
diff --git a/legacy/escape/src/lib/sys/mman.h b/legacy/escape/src/lib/sys/mman.h
new file mode 100644
index 0000000000..e60bb45d4b
--- /dev/null
+++ b/legacy/escape/src/lib/sys/mman.h
@@ -0,0 +1,183 @@
1#ifndef __ESCAPE_SYS_MMAN_H__
2#define __ESCAPE_SYS_MMAN_H__
3
4#include <Escape.h>
5
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11
12/**
13 * @def PROT_NONE
14 * Data can not be accessed.
15 */
16
17/**
18 * @def PROT_READ
19 * Data can be read.
20 */
21
22/**
23 * @def PROT_WRITE
24 * Data can be written.
25 */
26
27/**
28 * @def PROT_EXEC
29 * Data can be executed.
30 */
31
32#define PROT_NONE 0x00
33#define PROT_READ 0x01
34#define PROT_WRITE 0x02
35#define PROT_EXEC 0x04
36
37/**
38 * @def MAP_SHARED
39 * Changes are shared.
40 */
41
42/**
43 * @def MAP_PRIVATE
44 * Changes are private.
45 */
46
47/**
48 * @def MAP_FIXED
49 * Interpret the address (addr) exactly.
50 */
51
52/**
53 * @def MAP_FAILED
54 * Error return from mmap().
55 */
56
57#define MAP_SHARED 0x0001
58#define MAP_PRIVATE 0x0002
59#define MAP_FIXED 0x0010
60
61#define MAP_FAILED ((void *)-1)
62
63
64/**
65 * @file mman.h
66 * @brief The file that provides the memory map functions
67 * @defgroup Mman Functions that manage memory mappping.
68 *
69 * This header provides the meomry map functions mmap and munmap.
70 *
71 */
72
73/**
74 * Creates or opens a named or unnamed file mapping object for a
75 * specified file and maps a view of a file mapping into the
76 * address space of a calling process.
77 *
78 * @param addr Unused
79 * @param len Number of bytes to be mapped.
80 * @param prot Protections.
81 * @param flags Type of the mapped object.
82 * @param fd File descriptor that describes the object to map.
83 * @param offset Number of bytes from which to start the mapping.
84 * @return The starting address of the mapped view on success, -1 otherwise.
85 *
86 * Create or open an unnamed file mapping object for a specified
87 * file described by the file descriptor @p fd. The number of
88 * bytes that are mapped is given by @p len and start after
89 * @p offset bytes. The parameter @p addr is unused.
90 *
91 * The only type of the mapped object that is supported is
92 * @c MAP_SHARED. If another value if given, -1 is returned.
93 *
94 * @p prot specifies the protection of the mapped region. If
95 * PROT_EXEC is used, it set the execute access. If PROT_READ
96 * is used, it sets the read access. If PROT_WRITE is used, it
97 * sets the write access.
98 *
99 * If the map view of file can not be created, -1 is returned.
100 * If the mappping can not be done, -1 is returned.
101 *
102 * If no error occured, the starting address of the mapped view
103 * is returned.
104 *
105 * Conformity: None.
106 *
107 * @ingroup Mman
108 */
109EAPI void *mmap(void *addr,
110 size_t len,
111 int prot,
112 int flags,
113 int fd,
114 off_t offset);
115
116/**
117 * Unmaps a mapped view of a file from the calling process's
118 * address space.
119 *
120 * @param addr Pointer to the base address.
121 * @param len Unused.
122 * @return 0 on success, -1 otherwise.
123 *
124 * Unmaps a mapped view of a file from the calling process's
125 * address space. @p addr is the pointer to the base address.
126 * This value must be identical to the value returned by a
127 * previous call to mmap(). The parameter @p len is unsed.
128 *
129 * Conformity: None.
130 *
131 * @ingroup Mman
132 */
133EAPI int munmap(void *addr,
134 size_t len);
135
136
137# define MADV_NORMAL 0 /* No further special treatment. */
138# define MADV_RANDOM 1 /* Expect random page references. */
139# define MADV_SEQUENTIAL 2 /* Expect sequential page references. */
140# define MADV_WILLNEED 3 /* Will need these pages. */
141# define MADV_DONTNEED 4 /* Don't need these pages. */
142# define MADV_REMOVE 9 /* Remove these pages and resources. */
143# define MADV_DONTFORK 10 /* Do not inherit across fork. */
144# define MADV_DOFORK 11 /* Do inherit across fork. */
145# define MADV_MERGEABLE 12 /* KSM may merge identical pages. */
146# define MADV_UNMERGEABLE 13 /* KSM may not merge identical pages. */
147# define MADV_HUGEPAGE 14 /* Worth backing with hugepages. */
148# define MADV_NOHUGEPAGE 15 /* Not worth backing with hugepages. */
149# define MADV_HWPOISON 100 /* Poison a page for testing. */
150
151/**
152 * give advice about use of memory
153 *
154 * @param addr Unused.
155 * @param length Unused.
156 * @param advice Unused.
157 * @return 0 on success, -1 otherwise.
158 *
159 * The madvise() system call advises the kernel about how to handle
160 * paging input/output in the address range beginning at address addr and
161 * with size length bytes. It allows an application to tell the kernel
162 * how it expects to use some mapped or shared memory areas, so that
163 * the kernel can choose appropriate read-ahead and caching techniques.
164 * This call does not influence the semantics of the application (except
165 * in the case of MADV_DONTNEED), but may influence its performance.
166 * The kernel is free to ignore the advice.
167
168 * Conformity: None.
169 *
170 * @ingroup Mman
171 */
172EAPI int madvise(void *addr,
173 size_t length,
174 int advice);
175
176
177#ifdef __cplusplus
178}
179#endif
180
181
182#endif /* __ESCAPE_SYS_MMAN_H__ */
183