3 #define DONT_INCLUDE_SYSCALL_NAMES 1
13 # include <spawn.h> // posix_spawn
17 #define assert(cnd) do{ \
19 fprintf(stderr, "%s:%i - assert failed - " #cnd"\n", __FILE__, __LINE__);\
31 FILE *gaSyscall_LocalFPs[MAX_FPS];
34 const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const char *ArgTypes, va_list *Args)
38 int direction = 0; // 0: Invalid, 1: Out, 2: In, 3: Out
43 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
44 if( *ArgTypes == '\0' ) return ArgTypes;
46 // DEBUG("ArgTypes = '%s'", ArgTypes);
51 default: // Defaults to output
52 case '>': direction = 1; break;
53 case '<': direction = 2; break;
54 case '?': direction = 3; break;
59 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
60 if( *ArgTypes == '\0' ) return ArgTypes;
68 if( direction != 1 ) {
69 Warning("ReadEntry: Recieving an integer is not defined");
73 val32 = va_arg(*Args, uint32_t);
75 Dest->Type = ARG_TYPE_INT32;
76 Dest->Length = sizeof(uint32_t);
80 *(uint32_t*)DataDest = val32;
85 if( direction != 1 ) {
86 fprintf(stderr, "ReadEntry: Recieving an integer is not defined\n");
90 val64 = va_arg(*Args, uint64_t);
92 Dest->Type = ARG_TYPE_INT64;
93 Dest->Length = sizeof(uint64_t);
96 *(uint64_t*)DataDest = val64;
100 // Input string makes no sense!
101 if( direction != 1 ) {
102 fprintf(stderr, "ReadEntry: Recieving a string is not defined\n");
106 str = va_arg(*Args, char*);
108 Dest->Type = ARG_TYPE_STRING;
109 Dest->Length = strlen(str) + 1;
114 memcpy(DataDest, str, Dest->Length);
117 // Data (special handling)
119 len = va_arg(*Args, size_t);
120 str = va_arg(*Args, char*);
122 // Save the pointer for later
123 if( PtrDest ) *PtrDest = str;
125 // Create parameter block
126 Dest->Type = ARG_TYPE_DATA;
127 Dest->Length = str ? len : 0;
130 Dest->Flags |= ARG_FLAG_RETURN;
135 if( DataDest && str )
136 memcpy(DataDest, str, len);
139 Dest->Flags |= ARG_FLAG_ZEROED;
153 * Whitespace is ignored
154 * >i: Input Integer (32-bits)
155 * >I: Input Long Integer (64-bits)
157 * >d: Input Buffer (Preceded by valid size)
158 * <I: Output long integer
159 * <d: Output Buffer (Preceded by valid size)
160 * ?d: Bi-directional buffer (Preceded by valid size), buffer contents
163 uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
166 int paramCount, dataLength;
167 int retCount = 2, retLength = sizeof(uint64_t) + sizeof(uint32_t);
168 void **retPtrs; // Pointers to return buffers
176 // printf("&tRequestHeader->Params = %i\n", offsetof(tRequestHeader, Params));
177 // printf("&tRequestValue->Flags = %i\n", offsetof(tRequestValue, Flags));
178 // printf("&tRequestValue->Length = %i\n", offsetof(tRequestValue, Length));
181 va_start(args, ArgTypes);
187 tRequestValue tmpVal;
189 str = ReadEntry(&tmpVal, NULL, NULL, str, &args);
191 fprintf(stderr, "syscalls.c: ReadEntry failed (SyscallID = %i)\n", SyscallID);
195 if( !(tmpVal.Flags & ARG_FLAG_ZEROED) )
196 dataLength += tmpVal.Length;
198 if( tmpVal.Flags & ARG_FLAG_RETURN ) {
199 retLength += tmpVal.Length;
205 dataLength += sizeof(tRequestHeader) + paramCount*sizeof(tRequestValue);
206 retLength += sizeof(tRequestHeader) + retCount*sizeof(tRequestValue);
209 retPtrs = malloc( sizeof(void*) * (retCount+1) );
210 if( dataLength > retLength)
211 req = malloc( dataLength );
213 req = malloc( retLength );
214 req->ClientID = 0; //< Filled later
215 req->CallID = SyscallID;
216 req->NParams = paramCount;
217 req->MessageLength = dataLength;
218 dataPtr = &req->Params[paramCount];
220 // Fill `output` and `input`
221 va_start(args, ArgTypes);
223 // - re-zero so they can be used as indicies
228 str = ReadEntry(&req->Params[paramCount], dataPtr, &retPtrs[retCount], str, &args);
231 if( !(req->Params[paramCount].Flags & ARG_FLAG_ZEROED) )
232 dataPtr += req->Params[paramCount].Length;
233 if( req->Params[paramCount].Flags & ARG_FLAG_RETURN )
240 // --- Send syscall request
241 if( SendRequest(req, dataLength, retLength) < 0 ) {
242 fprintf(stderr, "syscalls.c: SendRequest failed (SyscallID = %i)\n", SyscallID);
246 dataPtr = (void*)&req->Params[req->NParams];
247 assert(req->NParams >= 2);
249 assert(req->Params[0].Type == ARG_TYPE_INT64);
250 assert(req->Params[0].Length == sizeof(uint64_t));
251 retValue = *(uint64_t*)dataPtr;
252 dataPtr += sizeof(uint64_t);
254 assert(req->Params[1].Type == ARG_TYPE_INT32);
255 assert(req->Params[1].Length == sizeof(uint32_t));
256 acess__errno = *(uint32_t*)dataPtr;
257 dataPtr += sizeof(uint32_t);
259 // Write changes to buffers
260 if( req->NParams - 2 != retCount ) {
261 fprintf(stderr, "syscalls.c: Return count inbalance (%i - 1 != exp %i) [Call %i]\n",
262 req->NParams, retCount, SyscallID);
266 for( i = 2; i < req->NParams; i ++ )
270 printf("Return Data %i: (%i)", i, req->Params[i].Length);
271 for( j = 0; j < req->Params[i].Length; j ++ )
272 printf(" %02x", ((uint8_t*)dataPtr)[j]);
275 assert( req->Params[i].Type == ARG_TYPE_DATA );
276 memcpy( retPtrs[retCount++], dataPtr, req->Params[i].Length );
277 dataPtr += req->Params[i].Length;
283 SYSTRACE(": %i 0x%llx", SyscallID, retValue);
289 int native_open(const char *Path, int Flags)
292 for(ret = 0; ret < MAX_FPS && gaSyscall_LocalFPs[ret]; ret ++ ) ;
293 if(ret == MAX_FPS) return -1;
294 // TODO: Handle directories
295 gaSyscall_LocalFPs[ret] = fopen(&Path[4], "r+");
296 if(!gaSyscall_LocalFPs[ret]) return -1;
300 void native_close(int FD)
302 fclose( gaSyscall_LocalFPs[FD] );
303 gaSyscall_LocalFPs[FD] = NULL;
306 size_t native_read(int FD, void *Dest, size_t Bytes)
308 return fread( Dest, Bytes, 1, gaSyscall_LocalFPs[FD] );
311 size_t native_write(int FD, const void *Src, size_t Bytes)
313 return fwrite( Src, Bytes, 1, gaSyscall_LocalFPs[FD] );
316 int native_seek(int FD, int64_t Ofs, int Dir)
319 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_CUR );
321 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_SET );
323 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_END );
326 uint64_t native_tell(int FD)
328 return ftell( gaSyscall_LocalFPs[FD] );
331 int native_execve(const char *filename, const char *const argv[], const char *const envp[])
334 ret = execve(filename, (void*)argv, (void*)envp);
335 perror("native_execve");
339 int native_spawn(const char *filename, const char *const argv[], const char *const envp[])
344 rv = _spawnve(_P_NOWAIT, filename, argv, envp);
346 rv = posix_spawn(NULL, filename, NULL, NULL, (void*)argv, (void*)envp);