1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
--- ./newlib/libc/sys/psp/pipe.c.orig 2012-01-25 19:33:12.000000000 +0000
+++ ./newlib/libc/sys/psp/pipe.c 2012-01-25 19:33:12.000000000 +0000
@@ -0,0 +1,305 @@
+/*
+ * PSP Software Development Kit - http://www.pspdev.org
+ * -----------------------------------------------------------------------
+ * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
+ *
+ * pipe.c - Socket wrappers to provide similar functions to normal unix
+ *
+ * Copyright (c) 2006 Rafael Cabezas <rafpsp@gmail.com>
+ *
+ * - 20070630 Alper Akcan "anhanguera" <distchx@yahoo.com>
+ * [non]blocking read/write() fix
+ * illegal size fix for read/write()
+ *
+ */
+#include <stdio.h>
+#include <errno.h>
+#include <sys/syslimits.h>
+#include <sys/fd_set.h>
+
+#include <psptypes.h>
+#include <pspthreadman.h>
+#include <pspmodulemgr.h>
+#include <pspkerror.h>
+#include "fdman.h"
+
+/* Pipe functions */
+/* Returns how many bytes are in the pipe -- waiting to be read */
+size_t __psp_pipe_peekmsgsize(int fd)
+{
+ SceKernelMppInfo info;
+ info.size = sizeof(info);
+
+ if (!__PSP_IS_FD_OF_TYPE(fd, __PSP_DESCRIPTOR_TYPE_PIPE)) {
+ errno = EBADF;
+ return -1;
+ }
+
+ if (sceKernelReferMsgPipeStatus(__psp_descriptormap[fd]->sce_descriptor, &info) == 0) {
+ return (info.bufSize - info.freeSize);
+ }
+ else {
+ return -1;
+ }
+}
+
+int pipe(int fildes[2])
+{
+ static int iIndex = 0;
+ char name[32];
+ iIndex++;
+
+ sprintf(name, "__psp_pipe_%02d", iIndex);
+
+ SceUID uid = sceKernelCreateMsgPipe(name, PSP_MEMORY_PARTITION_USER, 0, (void *)PIPE_BUF, NULL);
+
+ if (uid >= 0) {
+ fildes[0] = __psp_fdman_get_new_descriptor();
+ if (fildes[0] != -1) {
+ __psp_descriptormap[fildes[0]]->sce_descriptor = uid;
+ __psp_descriptormap[fildes[0]]->type = __PSP_DESCRIPTOR_TYPE_PIPE;
+ }
+ else {
+ sceKernelDeleteMsgPipe(uid);
+ errno = EFAULT;
+ return -1;
+ }
+
+ fildes[1] = __psp_fdman_get_dup_descriptor(fildes[0]);
+ if (fildes[1] != -1) {
+ __psp_descriptormap[fildes[1]]->sce_descriptor = uid;
+ return 0;
+ }
+ else {
+ sceKernelDeleteMsgPipe(uid);
+ errno = EFAULT;
+ return -1;
+ }
+
+ }
+ else {
+ errno = EFAULT;
+ return -1;
+ }
+}
+
+int __psp_pipe_close(int fd)
+{
+ int ret = 0;
+
+ if (!__PSP_IS_FD_OF_TYPE(fd, __PSP_DESCRIPTOR_TYPE_PIPE)) {
+ errno = EBADF;
+ return -1;
+ }
+
+
+ if ( __psp_descriptormap[fd]->ref_count == 1 ) {
+ /**
+ * Delete a message pipe
+ *
+ * @param uid - The UID of the pipe
+ *
+ * @return 0 on success, < 0 on error
+ */
+ ret = sceKernelDeleteMsgPipe(__psp_descriptormap[fd]->sce_descriptor);
+ }
+
+ __psp_fdman_release_descriptor(fd);
+
+ if(ret < 0) {
+ return __psp_set_errno(ret);
+ }
+
+ return 0;
+}
+
+int __psp_pipe_nonblocking_read(int fd, void *buf, size_t len)
+{
+ int ret;
+ int sceuid;
+ int size;
+
+ if (!__PSP_IS_FD_OF_TYPE(fd, __PSP_DESCRIPTOR_TYPE_PIPE)) {
+ errno = EBADF;
+ return -1;
+ }
+
+ sceuid = __psp_descriptormap[fd]->sce_descriptor;
+
+ size = __psp_pipe_peekmsgsize(fd);
+ if (size > 0) {
+ if (size < len) {
+ len = size;
+ }
+ }
+ else if (size == 0) {
+ errno = EAGAIN;
+ return -1;
+ }
+ else {
+ errno = EBADF;
+ return -1;
+ }
+
+ /**
+ * Receive a message from a pipe
+ *
+ * @param uid - The UID of the pipe
+ * @param message - Pointer to the message
+ * @param size - Size of the message
+ * @param unk1 - Unknown
+ * @param unk2 - Unknown
+ * @param timeout - Timeout for receive
+ *
+ * @return 0 on success, < 0 on error
+ */
+ ret = sceKernelTryReceiveMsgPipe(sceuid, buf, len, 0, 0);
+
+ if (ret == 0) {/* Success - Data */
+ return len;
+ }
+ else if (ret == SCE_KERNEL_ERROR_MPP_EMPTY) {/* No data */
+ errno = EAGAIN;
+ return -1;
+ }
+ else {/* Error */
+ return __psp_set_errno(ret);
+ }
+}
+
+int __psp_pipe_read(int fd, void *buf, size_t len)
+{
+ int ret;
+ int sceuid;
+ int size;
+
+ if (!__PSP_IS_FD_OF_TYPE(fd, __PSP_DESCRIPTOR_TYPE_PIPE)) {
+ errno = EBADF;
+ return -1;
+ }
+
+ sceuid = __psp_descriptormap[fd]->sce_descriptor;
+
+#if 0
+ /* we should block until there is some data (or maybe for enough data),
+ * peeking the msg size should be only for nonblocking reads
+ */
+ size = __psp_pipe_peekmsgsize(fd);
+ if (size > 0) {
+ if (size < len) {
+ len = size;
+ }
+ }
+ else {
+ errno = EBADF;
+ return -1;
+ }
+#endif
+ /* if len is greater than PIPE_BUF then, MsgPipe functions returns
+ * SCE_KERNEL_ERROR_ILLEGAL_SIZE, but it should read at least
+ * PIPE_BUF bytes, and return the number of bytes read.
+ */
+ if (len > PIPE_BUF) {
+ len = PIPE_BUF;
+ }
+
+ /**
+ * Receive a message from a pipe
+ *
+ * @param uid - The UID of the pipe
+ * @param message - Pointer to the message
+ * @param size - Size of the message
+ * @param unk1 - Unknown
+ * @param unk2 - Unknown
+ * @param timeout - Timeout for receive
+ *
+ * @return 0 on success, < 0 on error
+ */
+ ret = sceKernelReceiveMsgPipe(sceuid, buf, len, 0, NULL, NULL);
+
+ if (ret == 0) {/* Success - Data */
+ return len;
+ }
+ else {/* Error */
+ return __psp_set_errno(ret);
+ }
+}
+
+int __psp_pipe_write(int fd, const void *buf, size_t len)
+{
+ int ret;
+ int sceuid;
+ char *cbuf;
+
+ if (!__PSP_IS_FD_OF_TYPE(fd, __PSP_DESCRIPTOR_TYPE_PIPE)) {
+ errno = EBADF;
+ return -1;
+ }
+
+ sceuid = __psp_descriptormap[fd]->sce_descriptor;
+
+ /* if len is greater than PIPE_BUF then, MsgPipe functions returns
+ * SCE_KERNEL_ERROR_ILLEGAL_SIZE, but it should write at least
+ * PIPE_BUF bytes, and return the number of bytes written.
+ */
+ if (len > PIPE_BUF) {
+ len = PIPE_BUF;
+ }
+
+ /**
+ * Send a message to a pipe
+ *
+ * @param uid - The UID of the pipe
+ * @param message - Pointer to the message
+ * @param size - Size of the message
+ * @param unk1 - Unknown
+ * @param unk2 - Unknown
+ * @param timeout - Timeout for send
+ *
+ * @return 0 on success, < 0 on error
+ */
+ cbuf = (char *)buf;
+ ret = sceKernelSendMsgPipe(sceuid, cbuf, len, 0, NULL, NULL);
+ if (ret == 0) {/* Success - Data */
+ return len;
+ }
+ else {/* Error */
+ return __psp_set_errno(ret);
+ }
+}
+
+int __psp_pipe_nonblocking_write(int fd, const void *buf, size_t len)
+{
+ int ret;
+ int sceuid;
+ char *cbuf;
+
+ if (!__PSP_IS_FD_OF_TYPE(fd, __PSP_DESCRIPTOR_TYPE_PIPE)) {
+ errno = EBADF;
+ return -1;
+ }
+
+ sceuid = __psp_descriptormap[fd]->sce_descriptor;
+
+ /* if len is greater than PIPE_BUF then, MsgPipe functions returns
+ * SCE_KERNEL_ERROR_ILLEGAL_SIZE, but it should write at least
+ * PIPE_BUF bytes, and return the number of bytes written.
+ */
+ if (len > PIPE_BUF) {
+ len = PIPE_BUF;
+ }
+
+ cbuf = (char *)buf;
+ ret = sceKernelTrySendMsgPipe(sceuid, cbuf, len, 0, 0);
+
+ if (ret == 0) {/* Success - Data */
+ return len;
+ }
+ else if (ret == SCE_KERNEL_ERROR_MPP_FULL) {
+ errno = EAGAIN;
+ return -1;
+ }
+ else {/* Error */
+ return __psp_set_errno(ret);
+ }
+}
|