3 #define DONT_INCLUDE_SYSCALL_NAMES 1
12 #include <spawn.h> // posix_spawn
16 #define DEBUG(str, x...) Debug(str, x)
18 #define DEBUG(...) do{}while(0)
28 FILE *gaSyscall_LocalFPs[MAX_FPS];
31 const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const char *ArgTypes, va_list *Args)
35 int direction = 0; // 0: Invalid, 1: Out, 2: In, 3: Out
40 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
41 if( *ArgTypes == '\0' ) return ArgTypes;
43 // DEBUG("ArgTypes = '%s'", ArgTypes);
48 default: // Defaults to output
49 case '>': direction = 1; break;
50 case '<': direction = 2; break;
51 case '?': direction = 3; break;
56 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
57 if( *ArgTypes == '\0' ) return ArgTypes;
65 if( direction != 1 ) {
66 Warning("ReadEntry: Recieving an integer is not defined");
70 val32 = va_arg(*Args, uint32_t);
72 Dest->Type = ARG_TYPE_INT32;
73 Dest->Length = sizeof(uint32_t);
77 *(uint32_t*)DataDest = val32;
82 if( direction != 1 ) {
83 fprintf(stderr, "ReadEntry: Recieving an integer is not defined\n");
87 val64 = va_arg(*Args, uint64_t);
89 Dest->Type = ARG_TYPE_INT64;
90 Dest->Length = sizeof(uint64_t);
93 *(uint64_t*)DataDest = val64;
97 // Input string makes no sense!
98 if( direction != 1 ) {
99 fprintf(stderr, "ReadEntry: Recieving a string is not defined\n");
103 str = va_arg(*Args, char*);
105 Dest->Type = ARG_TYPE_STRING;
106 Dest->Length = strlen(str) + 1;
111 memcpy(DataDest, str, Dest->Length);
114 // Data (special handling)
116 len = va_arg(*Args, size_t);
117 str = va_arg(*Args, char*);
119 // Save the pointer for later
120 if( PtrDest ) *PtrDest = str;
122 // Create parameter block
123 Dest->Type = ARG_TYPE_DATA;
124 Dest->Length = str ? len : 0;
127 Dest->Flags |= ARG_FLAG_RETURN;
132 if( DataDest && str )
133 memcpy(DataDest, str, len);
136 Dest->Flags |= ARG_FLAG_ZEROED;
150 * Whitespace is ignored
151 * >i: Input Integer (32-bits)
152 * >I: Input Long Integer (64-bits)
154 * >d: Input Buffer (Preceded by valid size)
155 * <I: Output long integer
156 * <d: Output Buffer (Preceded by valid size)
157 * ?d: Bi-directional buffer (Preceded by valid size), buffer contents
160 uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
163 int paramCount, dataLength;
164 int retCount = 1, retLength = sizeof(uint64_t);
165 void **retPtrs; // Pointers to return buffers
173 // printf("&tRequestHeader->Params = %i\n", offsetof(tRequestHeader, Params));
174 // printf("&tRequestValue->Flags = %i\n", offsetof(tRequestValue, Flags));
175 // printf("&tRequestValue->Length = %i\n", offsetof(tRequestValue, Length));
178 va_start(args, ArgTypes);
184 tRequestValue tmpVal;
186 str = ReadEntry(&tmpVal, NULL, NULL, str, &args);
188 fprintf(stderr, "syscalls.c: ReadEntry failed (SyscallID = %i)\n", SyscallID);
192 if( !(tmpVal.Flags & ARG_FLAG_ZEROED) )
193 dataLength += tmpVal.Length;
195 if( tmpVal.Flags & ARG_FLAG_RETURN ) {
196 retLength += tmpVal.Length;
202 dataLength += sizeof(tRequestHeader) + paramCount*sizeof(tRequestValue);
203 retLength += sizeof(tRequestHeader) + retCount*sizeof(tRequestValue);
206 retPtrs = malloc( sizeof(void*) * (retCount+1) );
207 if( dataLength > retLength)
208 req = malloc( dataLength );
210 req = malloc( retLength );
211 req->ClientID = 0; //< Filled later
212 req->CallID = SyscallID;
213 req->NParams = paramCount;
214 req->MessageLength = dataLength;
215 dataPtr = &req->Params[paramCount];
217 // Fill `output` and `input`
218 va_start(args, ArgTypes);
220 // - re-zero so they can be used as indicies
225 str = ReadEntry(&req->Params[paramCount], dataPtr, &retPtrs[retCount], str, &args);
228 if( !(req->Params[paramCount].Flags & ARG_FLAG_ZEROED) )
229 dataPtr += req->Params[paramCount].Length;
230 if( req->Params[paramCount].Flags & ARG_FLAG_RETURN )
237 // Send syscall request
238 if( SendRequest(req, dataLength, retLength) < 0 ) {
239 fprintf(stderr, "syscalls.c: SendRequest failed (SyscallID = %i)\n", SyscallID);
243 // Parse return value
244 dataPtr = &req->Params[req->NParams];
246 if( req->NParams >= 1 )
248 switch(req->Params[0].Type)
251 retValue = *(uint64_t*)dataPtr;
252 dataPtr += req->Params[0].Length;
255 retValue = *(uint32_t*)dataPtr;
256 dataPtr += req->Params[0].Length;
261 // Write changes to buffers
262 if( req->NParams - 1 != retCount ) {
263 fprintf(stderr, "syscalls.c: Return count inbalance (%i - 1 != exp %i) [Call %i]\n",
264 req->NParams, retCount, SyscallID);
268 for( i = 1; i < req->NParams; i ++ )
272 printf("Return Data %i: (%i)", i, req->Params[i].Length);
273 for( j = 0; j < req->Params[i].Length; j ++ )
274 printf(" %02x", ((uint8_t*)dataPtr)[j]);
277 memcpy( retPtrs[retCount++], dataPtr, req->Params[i].Length );
278 dataPtr += req->Params[i].Length;
284 DEBUG(": %i 0x%llx", SyscallID, retValue);
290 int native_open(const char *Path, int Flags)
293 for(ret = 0; ret < MAX_FPS && gaSyscall_LocalFPs[ret]; ret ++ ) ;
294 if(ret == MAX_FPS) return -1;
295 // TODO: Handle directories
296 gaSyscall_LocalFPs[ret] = fopen(&Path[4], "r+");
297 if(!gaSyscall_LocalFPs[ret]) return -1;
301 void native_close(int FD)
303 fclose( gaSyscall_LocalFPs[FD] );
304 gaSyscall_LocalFPs[FD] = NULL;
307 size_t native_read(int FD, void *Dest, size_t Bytes)
309 return fread( Dest, Bytes, 1, gaSyscall_LocalFPs[FD] );
312 size_t native_write(int FD, const void *Src, size_t Bytes)
314 return fwrite( Src, Bytes, 1, gaSyscall_LocalFPs[FD] );
317 int native_seek(int FD, int64_t Ofs, int Dir)
320 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_CUR );
322 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_SET );
324 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_END );
327 uint64_t native_tell(int FD)
329 return ftell( gaSyscall_LocalFPs[FD] );
332 int native_execve(const char *filename, const char *const argv[], const char *const envp[])
335 ret = execve(filename, (void*)argv, (void*)envp);
336 perror("native_execve");
340 int native_spawn(const char *filename, const char *const argv[], const char *const envp[])
344 rv = posix_spawn(NULL, filename, NULL, NULL, (void*)argv, (void*)envp);