AcessNative - Added message length to simplfy message reception
[tpg/acess2.git] / AcessNative / ld-acess_src / main.c
1 /*
2  */
3 #include "common.h"
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <stdarg.h>
7 #include <string.h>
8
9 // === IMPORTS ===
10 extern int      gSocket;
11 extern int      giSyscall_ClientID;
12 extern void     acess__exit(int Status);
13 extern void     Request_Preinit(void);
14
15 // === PROTOTYPES ===
16 void    CallUser(void *Entry, int argc, char *argv[], char **envp) __attribute__((noreturn));
17
18 // === GLOBALS ===
19 char    **gEnvP;
20
21 // === CODE ===
22 int main(int argc, char *argv[], char **envp)
23 {
24          int    i;
25          int    appArgc;
26         char    **appArgv;
27         char    *appPath = NULL;
28          int    (*appMain)(int, char *[], char **);
29         void    *base;
30          int    rv;
31
32         gEnvP = envp;
33         
34         Request_Preinit();
35
36 //       int    syscall_handle = -1;
37         
38         for( i = 1; i < argc; i ++ )
39         {
40                 if(strcmp(argv[i], "--key") == 0) {
41                         giSyscall_ClientID = atoi(argv[++i]);
42                         continue ;
43                 }
44
45                 if(strcmp(argv[i], "--socket") == 0) {
46                         gSocket = atoi(argv[++i]);
47                         continue ;
48                 }
49                 
50                 if(strcmp(argv[i], "--binary") == 0) {
51                         appPath = argv[++i];
52                         continue ;
53                 }
54                 
55                 if(strcmp(argv[i], "--open") == 0) {
56                         if( acess_open(argv[++i], 6) == -1 ) {  // Read/Write
57                                 fprintf(stderr, "Unable to open '%s'\n", argv[i]);
58                                 exit(1);
59                         }
60                         continue ;
61                 }
62                 
63                 if( argv[i][0] != '-' ) break;
64         }
65
66         if( i >= argc ) {
67                 fprintf(stderr,
68                         "Usage: ld-acess <executable> [arguments ...]\n"
69                         "\n"
70                         "--key\t(internal) used to pass the system call handle when run with execve\n"
71                         "--binary\tLoad a local binary directly\n"
72                         "--open\tOpen a file before executing\n"
73                         );
74                 return 1;
75         }
76         
77         if( !appPath )
78                 appPath = argv[i];
79
80         appArgc = argc - i;
81         appArgv = &argv[i];
82
83 //      printf("Exectutable Path: '%s'\n", appPath);
84 //      printf("Executable argc = %i\n", appArgc);
85
86         base = Binary_Load(appPath, (uintptr_t*)&appMain);
87         printf("[DEBUG %i] base = %p\n", giSyscall_ClientID, base);
88         if( !base )     return 127;
89         
90         printf("==============================\n");
91         printf("[DEBUG %i] %i ", giSyscall_ClientID, appArgc);
92         for(i = 0; i < appArgc; i ++)
93                 printf("\"%s\" ", appArgv[i]);
94         printf("\n");
95         printf("[DEBUG %i] appMain = %p\n", giSyscall_ClientID, appMain);
96 //      CallUser(appMain, appArgc, appArgv, envp);
97         rv = appMain(appArgc, appArgv, envp);
98         acess__exit(rv);
99         return rv;
100 }
101
102 void CallUser(void *Entry, int argc, char *argv[], char **envp)
103 {
104         #if 0
105         __asm__ __volatile__ (
106                 "push %3;\n\t"
107                 "push %2;\n\t"
108                 "push %1;\n\t"
109                 "call *%0"
110                 : : "r" (Entry), "r" (argc), "r" (argv), "r" (envp)
111                 );
112         #else
113         #endif
114         for(;;);
115 }
116
117 void Warning(const char *Format, ...)
118 {
119         va_list args;
120         printf("[WARN   %i] ", giSyscall_ClientID);
121         va_start(args, Format);
122         vprintf(Format, args);
123         va_end(args);
124         printf("\n");
125 }
126
127 void Notice(const char *Format, ...)
128 {
129         va_list args;
130         printf("[NOTICE %i] ", giSyscall_ClientID);
131         va_start(args, Format);
132         vprintf(Format, args);
133         va_end(args);
134         printf("\n");
135 }
136
137 void Debug(const char *Format, ...)
138 {
139         va_list args;
140         printf("[DEBUG ");
141         printf("%2i] ", giSyscall_ClientID);
142         va_start(args, Format);
143         vprintf(Format, args);
144         va_end(args);
145         printf("\n");
146 }
147

UCC git Repository :: git.ucc.asn.au