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