sys_irix.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include <unistd.h>
  2. #include <signal.h>
  3. #include <stdlib.h>
  4. #include <limits.h>
  5. #include <sys/time.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <stdarg.h>
  10. #include <stdio.h>
  11. #include <sys/ipc.h>
  12. #include <sys/shm.h>
  13. #include <sys/stat.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <sys/wait.h>
  17. #include <sys/mman.h>
  18. #include <errno.h>
  19. #include <mntent.h>
  20. #include <dlfcn.h>
  21. #include "../qcommon/qcommon.h"
  22. #include "../linux/rw_linux.h"
  23. cvar_t *nostdout;
  24. unsigned sys_frame_time;
  25. uid_t saved_euid;
  26. qboolean stdin_active = true;
  27. // =======================================================================
  28. // General routines
  29. // =======================================================================
  30. void Sys_ConsoleOutput (char *string)
  31. {
  32. if (nostdout && nostdout->value)
  33. return;
  34. fputs(string, stdout);
  35. }
  36. void Sys_Printf (char *fmt, ...)
  37. {
  38. va_list argptr;
  39. char text[1024];
  40. unsigned char *p;
  41. va_start (argptr,fmt);
  42. vsprintf (text,fmt,argptr);
  43. va_end (argptr);
  44. if (strlen(text) > sizeof(text))
  45. Sys_Error("memory overwrite in Sys_Printf");
  46. if (nostdout && nostdout->value)
  47. return;
  48. for (p = (unsigned char *)text; *p; p++) {
  49. *p &= 0x7f;
  50. if ((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
  51. printf("[%02x]", *p);
  52. else
  53. putc(*p, stdout);
  54. }
  55. }
  56. void Sys_Quit (void)
  57. {
  58. CL_Shutdown ();
  59. Qcommon_Shutdown ();
  60. fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  61. _exit(0);
  62. }
  63. void Sys_Init(void)
  64. {
  65. #if id386
  66. // Sys_SetFPCW();
  67. #endif
  68. }
  69. void Sys_Error (char *error, ...)
  70. {
  71. va_list argptr;
  72. char string[1024];
  73. // change stdin to non blocking
  74. fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  75. va_start (argptr,error);
  76. vsprintf (string,error,argptr);
  77. va_end (argptr);
  78. fprintf(stderr, "Error: %s\n", string);
  79. CL_Shutdown ();
  80. Qcommon_Shutdown ();
  81. _exit (1);
  82. }
  83. void Sys_Warn (char *warning, ...)
  84. {
  85. va_list argptr;
  86. char string[1024];
  87. va_start (argptr,warning);
  88. vsprintf (string,warning,argptr);
  89. va_end (argptr);
  90. fprintf(stderr, "Warning: %s", string);
  91. }
  92. /*
  93. ============
  94. Sys_FileTime
  95. returns -1 if not present
  96. ============
  97. */
  98. int Sys_FileTime (char *path)
  99. {
  100. struct stat buf;
  101. if (stat (path,&buf) == -1)
  102. return -1;
  103. return buf.st_mtime;
  104. }
  105. void floating_point_exception_handler(int whatever)
  106. {
  107. // Sys_Warn("floating point exception\n");
  108. signal(SIGFPE, floating_point_exception_handler);
  109. }
  110. char *Sys_ConsoleInput(void)
  111. {
  112. static char text[256];
  113. int len;
  114. fd_set fdset;
  115. struct timeval timeout;
  116. if (!dedicated || !dedicated->value)
  117. return NULL;
  118. if (!stdin_active)
  119. return NULL;
  120. FD_ZERO(&fdset);
  121. FD_SET(0, &fdset); // stdin
  122. timeout.tv_sec = 0;
  123. timeout.tv_usec = 0;
  124. if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
  125. return NULL;
  126. len = read (0, text, sizeof(text));
  127. if (len == 0) { // eof!
  128. stdin_active = false;
  129. return NULL;
  130. }
  131. if (len < 1)
  132. return NULL;
  133. text[len-1] = 0; // rip off the /n and terminate
  134. return text;
  135. }
  136. /*****************************************************************************/
  137. static void *game_library;
  138. /*
  139. =================
  140. Sys_UnloadGame
  141. =================
  142. */
  143. void Sys_UnloadGame (void)
  144. {
  145. if (game_library)
  146. dlclose (game_library);
  147. game_library = NULL;
  148. }
  149. /*
  150. =================
  151. Sys_GetGameAPI
  152. Loads the game dll
  153. =================
  154. */
  155. void *Sys_GetGameAPI (void *parms)
  156. {
  157. #ifndef REF_HARD_LINKED
  158. void *(*GetGameAPI) (void *);
  159. char name[MAX_OSPATH];
  160. char curpath[MAX_OSPATH];
  161. char *path;
  162. #ifdef __sgi
  163. const char *gamename = "gamemips.so";
  164. #else
  165. #error Unknown arch
  166. #endif
  167. setreuid(getuid(), getuid());
  168. setegid(getgid());
  169. if (game_library)
  170. Com_Error (ERR_FATAL, "Sys_GetGameAPI without Sys_UnloadingGame");
  171. getcwd(curpath, sizeof(curpath));
  172. Com_Printf("------- Loading %s -------", gamename);
  173. // now run through the search paths
  174. path = NULL;
  175. while (1)
  176. {
  177. path = FS_NextPath (path);
  178. if (!path)
  179. return NULL; // couldn't find one anywhere
  180. sprintf (name, "%s/%s/%s", curpath, path, gamename);
  181. Com_Printf ("Trying to load library (%s)\n",name);
  182. game_library = dlopen (name, RTLD_NOW );
  183. if (game_library)
  184. {
  185. Com_DPrintf ("LoadLibrary (%s)\n",name);
  186. break;
  187. }
  188. }
  189. GetGameAPI = (void *)dlsym (game_library, "GetGameAPI");
  190. if (!GetGameAPI)
  191. {
  192. Sys_UnloadGame ();
  193. return NULL;
  194. }
  195. return GetGameAPI (parms);
  196. #else
  197. return (void *)GetGameAPI (parms);
  198. #endif
  199. }
  200. /*****************************************************************************/
  201. void Sys_AppActivate (void)
  202. {
  203. }
  204. void Sys_SendKeyEvents (void)
  205. {
  206. if (KBD_Update_fp)
  207. KBD_Update_fp();
  208. // grab frame time
  209. sys_frame_time = Sys_Milliseconds();
  210. }
  211. /*****************************************************************************/
  212. char *Sys_GetClipboardData(void)
  213. {
  214. return NULL;
  215. }
  216. int main (int argc, char **argv)
  217. {
  218. int time, oldtime, newtime;
  219. // go back to real user for config loads
  220. saved_euid = geteuid();
  221. seteuid(getuid());
  222. Qcommon_Init(argc, argv);
  223. /* fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY); */
  224. nostdout = Cvar_Get("nostdout", "0", 0);
  225. if (!nostdout->value) {
  226. /* fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY); */
  227. // printf ("Linux Quake -- Version %0.3f\n", LINUX_VERSION);
  228. }
  229. oldtime = Sys_Milliseconds ();
  230. while (1)
  231. {
  232. // find time spent rendering last frame
  233. do {
  234. newtime = Sys_Milliseconds ();
  235. time = newtime - oldtime;
  236. } while (time < 1);
  237. Qcommon_Frame (time);
  238. oldtime = newtime;
  239. }
  240. }
  241. void Sys_CopyProtect(void)
  242. {
  243. FILE *mnt;
  244. struct mntent *ent;
  245. char path[MAX_OSPATH];
  246. struct stat st;
  247. qboolean found_cd = false;
  248. static qboolean checked = false;
  249. if (checked)
  250. return;
  251. Com_Printf("XXX - Sys_CopyProtect disabled\n");
  252. checked = true;
  253. return;
  254. if ((mnt = setmntent("/etc/mtab", "r")) == NULL)
  255. Com_Error(ERR_FATAL, "Can't read mount table to determine mounted cd location.");
  256. while ((ent = getmntent(mnt)) != NULL) {
  257. if (strcmp(ent->mnt_type, "iso9660") == 0) {
  258. // found a cd file system
  259. found_cd = true;
  260. sprintf(path, "%s/%s", ent->mnt_dir, "install/data/quake2.exe");
  261. if (stat(path, &st) == 0) {
  262. // found it
  263. checked = true;
  264. endmntent(mnt);
  265. return;
  266. }
  267. sprintf(path, "%s/%s", ent->mnt_dir, "Install/Data/quake2.exe");
  268. if (stat(path, &st) == 0) {
  269. // found it
  270. checked = true;
  271. endmntent(mnt);
  272. return;
  273. }
  274. sprintf(path, "%s/%s", ent->mnt_dir, "quake2.exe");
  275. if (stat(path, &st) == 0) {
  276. // found it
  277. checked = true;
  278. endmntent(mnt);
  279. return;
  280. }
  281. }
  282. }
  283. endmntent(mnt);
  284. if (found_cd)
  285. Com_Error (ERR_FATAL, "Could not find a Quake2 CD in your CD drive.");
  286. Com_Error (ERR_FATAL, "Unable to find a mounted iso9660 file system.\n"
  287. "You must mount the Quake2 CD in a cdrom drive in order to play.");
  288. }
  289. #if 0
  290. /*
  291. ================
  292. Sys_MakeCodeWriteable
  293. ================
  294. */
  295. void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
  296. {
  297. int r;
  298. unsigned long addr;
  299. int psize = getpagesize();
  300. addr = (startaddr & ~(psize-1)) - psize;
  301. // fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
  302. // addr, startaddr+length, length);
  303. r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
  304. if (r < 0)
  305. Sys_Error("Protection change failed\n");
  306. }
  307. #endif