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__);\
29 extern int gbSyscallDebugEnabled;
32 FILE *gaSyscall_LocalFPs[MAX_FPS];
35 const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const char *ArgTypes, va_list *Args)
39 int direction = 0; // 0: Invalid, 1: Out, 2: In, 3: Out
44 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
45 if( *ArgTypes == '\0' ) return ArgTypes;
47 // DEBUG("ArgTypes = '%s'", ArgTypes);
52 default: // Defaults to output
53 case '>': direction = 1; break;
54 case '<': direction = 2; break;
55 case '?': direction = 3; break;
60 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
61 if( *ArgTypes == '\0' ) return ArgTypes;
69 if( direction != 1 ) {
70 Warning("ReadEntry: Recieving an integer is not defined");
74 val32 = va_arg(*Args, uint32_t);
76 Dest->Type = ARG_TYPE_INT32;
77 Dest->Length = sizeof(uint32_t);
81 *(uint32_t*)DataDest = val32;
86 if( direction != 1 ) {
87 fprintf(stderr, "ReadEntry: Recieving an integer is not defined\n");
91 val64 = va_arg(*Args, uint64_t);
93 Dest->Type = ARG_TYPE_INT64;
94 Dest->Length = sizeof(uint64_t);
97 *(uint64_t*)DataDest = val64;
101 // Input string makes no sense!
102 if( direction != 1 ) {
103 fprintf(stderr, "ReadEntry: Recieving a string is not defined\n");
107 str = va_arg(*Args, char*);
109 Dest->Type = ARG_TYPE_STRING;
110 Dest->Length = strlen(str) + 1;
115 memcpy(DataDest, str, Dest->Length);
118 // Data (special handling)
120 len = va_arg(*Args, size_t);
121 str = va_arg(*Args, char*);
123 // Save the pointer for later
124 if( PtrDest ) *PtrDest = str;
126 // Create parameter block
127 Dest->Type = ARG_TYPE_DATA;
128 Dest->Length = str ? len : 0;
131 Dest->Flags |= ARG_FLAG_RETURN;
136 if( DataDest && str )
137 memcpy(DataDest, str, len);
140 Dest->Flags |= ARG_FLAG_ZEROED;
154 * Whitespace is ignored
155 * >i: Input Integer (32-bits)
156 * >I: Input Long Integer (64-bits)
158 * >d: Input Buffer (Preceded by valid size)
159 * <I: Output long integer
160 * <d: Output Buffer (Preceded by valid size)
161 * ?d: Bi-directional buffer (Preceded by valid size), buffer contents
164 uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
167 int paramCount, dataLength;
168 int retCount = 2, retLength = sizeof(uint64_t) + sizeof(uint32_t);
169 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 if( !(req->NParams >= 2) ) {
247 fprintf(stderr, "syscalls.c: Too few return params (%i)", req->NParams);
250 dataPtr = (void*)&req->Params[req->NParams];
252 assert(req->Params[0].Type == ARG_TYPE_INT64);
253 assert(req->Params[0].Length == sizeof(uint64_t));
254 retValue = *(uint64_t*)dataPtr;
255 dataPtr += sizeof(uint64_t);
257 assert(req->Params[1].Type == ARG_TYPE_INT32);
258 assert(req->Params[1].Length == sizeof(uint32_t));
259 acess__errno = *(uint32_t*)dataPtr;
260 dataPtr += sizeof(uint32_t);
262 // Write changes to buffers
263 if( req->NParams - 2 != retCount ) {
264 fprintf(stderr, "syscalls.c: Return count inbalance (%i - 1 != exp %i) [Call %i]\n",
265 req->NParams, retCount, SyscallID);
269 for( unsigned int i = 2; i < req->NParams; i ++ )
273 printf("Return Data %i: (%i)", i, req->Params[i].Length);
274 for( j = 0; j < req->Params[i].Length; j ++ )
275 printf(" %02x", ((uint8_t*)dataPtr)[j]);
278 assert( req->Params[i].Type == ARG_TYPE_DATA );
279 memcpy( retPtrs[retCount++], dataPtr, req->Params[i].Length );
280 dataPtr += req->Params[i].Length;
286 if( gbSyscallDebugEnabled ) {
287 SYSTRACE(": %i 0x%llx", SyscallID, retValue);
293 int native_int_getfd(void)
295 for(int ret = 0; ret < MAX_FPS; ret ++ )
296 if( gaSyscall_LocalFPs[ret] == NULL )
301 int native_open(const char *Path, int Flags)
303 int ret = native_int_getfd();
304 if(ret == -1) return -1;
305 // TODO: Handle directories
306 gaSyscall_LocalFPs[ret] = fopen(Path, "r+");
307 if(!gaSyscall_LocalFPs[ret]) return -1;
311 int native_shm(const char *Tag, int Flags)
313 // int ret = native_int_getfd();
314 //if(ret == -1) return -1;
315 //if( strcmp(Tag, "anon") == 0 )
316 // path = "/AcessNative/anon/RAND";
317 // FD = shm_open(path, O_RDWD);
320 // path = "/Acessnative/named/<TAG>";
321 // int FD = shm_open(path, O_RDWD);
323 //gaSyscall_LocalFPs[ret] =
327 void native_close(int FD)
329 fclose( gaSyscall_LocalFPs[FD] );
330 gaSyscall_LocalFPs[FD] = NULL;
333 size_t native_read(int FD, void *Dest, size_t Bytes)
335 return fread( Dest, Bytes, 1, gaSyscall_LocalFPs[FD] );
338 size_t native_write(int FD, const void *Src, size_t Bytes)
340 return fwrite( Src, Bytes, 1, gaSyscall_LocalFPs[FD] );
343 int native_seek(int FD, int64_t Ofs, int Dir)
346 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_CUR );
348 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_SET );
350 return fseek( gaSyscall_LocalFPs[FD], Ofs, SEEK_END );
353 uint64_t native_tell(int FD)
355 return ftell( gaSyscall_LocalFPs[FD] );
358 int native_execve(const char *filename, const char *const argv[], const char *const envp[])
361 ret = execve(filename, (void*)argv, (void*)envp);
362 perror("native_execve");
366 int native_spawn(const char *filename, const char *const argv[], const char *const envp[])
370 fprintf(stderr, "native_spawn('%s')\n", filename);
373 rv = _spawnve(_P_NOWAIT, filename, argv, envp);
375 rv = posix_spawn(NULL, filename, NULL, NULL, (void*)argv, (void*)envp);
379 perror("native_spawn");