Annotation of quake2/linux/sys_linux.c, revision 1.1.1.2

1.1.1.2 ! root        1: /*
        !             2: Copyright (C) 1997-2001 Id Software, Inc.
        !             3: 
        !             4: This program is free software; you can redistribute it and/or
        !             5: modify it under the terms of the GNU General Public License
        !             6: as published by the Free Software Foundation; either version 2
        !             7: of the License, or (at your option) any later version.
        !             8: 
        !             9: This program is distributed in the hope that it will be useful,
        !            10: but WITHOUT ANY WARRANTY; without even the implied warranty of
        !            11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
        !            12: 
        !            13: See the GNU General Public License for more details.
        !            14: 
        !            15: You should have received a copy of the GNU General Public License
        !            16: along with this program; if not, write to the Free Software
        !            17: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
        !            18: 
        !            19: */
1.1       root       20: #include <unistd.h>
                     21: #include <signal.h>
                     22: #include <stdlib.h>
                     23: #include <limits.h>
                     24: #include <sys/time.h>
                     25: #include <sys/types.h>
                     26: #include <unistd.h>
                     27: #include <fcntl.h>
                     28: #include <stdarg.h>
                     29: #include <stdio.h>
                     30: #include <sys/ipc.h>
                     31: #include <sys/shm.h>
                     32: #include <sys/stat.h>
                     33: #include <string.h>
                     34: #include <ctype.h>
                     35: #include <sys/wait.h>
                     36: #include <sys/mman.h>
                     37: #include <errno.h>
                     38: #include <mntent.h>
                     39: 
                     40: #include <dlfcn.h>
                     41: 
                     42: #include "../qcommon/qcommon.h"
                     43: 
                     44: #include "../linux/rw_linux.h"
                     45: 
                     46: cvar_t *nostdout;
                     47: 
                     48: unsigned       sys_frame_time;
                     49: 
                     50: uid_t saved_euid;
                     51: qboolean stdin_active = true;
                     52: 
                     53: // =======================================================================
                     54: // General routines
                     55: // =======================================================================
                     56: 
                     57: void Sys_ConsoleOutput (char *string)
                     58: {
                     59:        if (nostdout && nostdout->value)
                     60:                return;
                     61: 
                     62:        fputs(string, stdout);
                     63: }
                     64: 
                     65: void Sys_Printf (char *fmt, ...)
                     66: {
                     67:        va_list         argptr;
                     68:        char            text[1024];
                     69:        unsigned char           *p;
                     70: 
                     71:        va_start (argptr,fmt);
                     72:        vsprintf (text,fmt,argptr);
                     73:        va_end (argptr);
                     74: 
                     75:        if (strlen(text) > sizeof(text))
                     76:                Sys_Error("memory overwrite in Sys_Printf");
                     77: 
                     78:     if (nostdout && nostdout->value)
                     79:         return;
                     80: 
                     81:        for (p = (unsigned char *)text; *p; p++) {
                     82:                *p &= 0x7f;
                     83:                if ((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
                     84:                        printf("[%02x]", *p);
                     85:                else
                     86:                        putc(*p, stdout);
                     87:        }
                     88: }
                     89: 
                     90: void Sys_Quit (void)
                     91: {
                     92:        CL_Shutdown ();
                     93:        Qcommon_Shutdown ();
                     94:     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
                     95:        _exit(0);
                     96: }
                     97: 
                     98: void Sys_Init(void)
                     99: {
                    100: #if id386
                    101: //     Sys_SetFPCW();
                    102: #endif
                    103: }
                    104: 
                    105: void Sys_Error (char *error, ...)
                    106: { 
                    107:     va_list     argptr;
                    108:     char        string[1024];
                    109: 
                    110: // change stdin to non blocking
                    111:     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
                    112: 
                    113:        CL_Shutdown ();
                    114:        Qcommon_Shutdown ();
                    115:     
                    116:     va_start (argptr,error);
                    117:     vsprintf (string,error,argptr);
                    118:     va_end (argptr);
                    119:        fprintf(stderr, "Error: %s\n", string);
                    120: 
                    121:        _exit (1);
                    122: 
                    123: } 
                    124: 
                    125: void Sys_Warn (char *warning, ...)
                    126: { 
                    127:     va_list     argptr;
                    128:     char        string[1024];
                    129:     
                    130:     va_start (argptr,warning);
                    131:     vsprintf (string,warning,argptr);
                    132:     va_end (argptr);
                    133:        fprintf(stderr, "Warning: %s", string);
                    134: } 
                    135: 
                    136: /*
                    137: ============
                    138: Sys_FileTime
                    139: 
                    140: returns -1 if not present
                    141: ============
                    142: */
                    143: int    Sys_FileTime (char *path)
                    144: {
                    145:        struct  stat    buf;
                    146:        
                    147:        if (stat (path,&buf) == -1)
                    148:                return -1;
                    149:        
                    150:        return buf.st_mtime;
                    151: }
                    152: 
                    153: void floating_point_exception_handler(int whatever)
                    154: {
                    155: //     Sys_Warn("floating point exception\n");
                    156:        signal(SIGFPE, floating_point_exception_handler);
                    157: }
                    158: 
                    159: char *Sys_ConsoleInput(void)
                    160: {
                    161:     static char text[256];
                    162:     int     len;
                    163:        fd_set  fdset;
                    164:     struct timeval timeout;
                    165: 
                    166:        if (!dedicated || !dedicated->value)
                    167:                return NULL;
                    168: 
                    169:        if (!stdin_active)
                    170:                return NULL;
                    171: 
                    172:        FD_ZERO(&fdset);
                    173:        FD_SET(0, &fdset); // stdin
                    174:        timeout.tv_sec = 0;
                    175:        timeout.tv_usec = 0;
                    176:        if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
                    177:                return NULL;
                    178: 
                    179:        len = read (0, text, sizeof(text));
                    180:        if (len == 0) { // eof!
                    181:                stdin_active = false;
                    182:                return NULL;
                    183:        }
                    184: 
                    185:        if (len < 1)
                    186:                return NULL;
                    187:        text[len-1] = 0;    // rip off the /n and terminate
                    188: 
                    189:        return text;
                    190: }
                    191: 
                    192: /*****************************************************************************/
                    193: 
                    194: static void *game_library;
                    195: 
                    196: /*
                    197: =================
                    198: Sys_UnloadGame
                    199: =================
                    200: */
                    201: void Sys_UnloadGame (void)
                    202: {
                    203:        if (game_library) 
                    204:                dlclose (game_library);
                    205:        game_library = NULL;
                    206: }
                    207: 
                    208: /*
                    209: =================
                    210: Sys_GetGameAPI
                    211: 
                    212: Loads the game dll
                    213: =================
                    214: */
                    215: void *Sys_GetGameAPI (void *parms)
                    216: {
                    217:        void    *(*GetGameAPI) (void *);
                    218: 
                    219:        char    name[MAX_OSPATH];
                    220:        char    curpath[MAX_OSPATH];
                    221:        char    *path;
                    222: #ifdef __i386__
                    223:        const char *gamename = "gamei386.so";
                    224: #elif defined __alpha__
                    225:        const char *gamename = "gameaxp.so";
                    226: #else
                    227: #error Unknown arch
                    228: #endif
                    229: 
                    230:        setreuid(getuid(), getuid());
                    231:        setegid(getgid());
                    232: 
                    233:        if (game_library)
                    234:                Com_Error (ERR_FATAL, "Sys_GetGameAPI without Sys_UnloadingGame");
                    235: 
                    236:        getcwd(curpath, sizeof(curpath));
                    237: 
1.1.1.2 ! root      238:        Com_Printf("------- Loading %s -------\n", gamename);
1.1       root      239: 
                    240:        // now run through the search paths
                    241:        path = NULL;
                    242:        while (1)
                    243:        {
                    244:                path = FS_NextPath (path);
                    245:                if (!path)
                    246:                        return NULL;            // couldn't find one anywhere
                    247:                sprintf (name, "%s/%s/%s", curpath, path, gamename);
1.1.1.2 ! root      248:                game_library = dlopen (name, RTLD_LAZY );
1.1       root      249:                if (game_library)
                    250:                {
1.1.1.2 ! root      251:                        Com_Printf ("LoadLibrary (%s)\n",name);
1.1       root      252:                        break;
                    253:                }
                    254:        }
                    255: 
                    256:        GetGameAPI = (void *)dlsym (game_library, "GetGameAPI");
                    257:        if (!GetGameAPI)
                    258:        {
                    259:                Sys_UnloadGame ();              
                    260:                return NULL;
                    261:        }
                    262: 
                    263:        return GetGameAPI (parms);
                    264: }
                    265: 
                    266: /*****************************************************************************/
                    267: 
                    268: void Sys_AppActivate (void)
                    269: {
                    270: }
                    271: 
                    272: void Sys_SendKeyEvents (void)
                    273: {
                    274: #ifndef DEDICATED_ONLY
                    275:        if (KBD_Update_fp)
                    276:                KBD_Update_fp();
                    277: #endif
                    278: 
                    279:        // grab frame time 
                    280:        sys_frame_time = Sys_Milliseconds();
                    281: }
                    282: 
                    283: /*****************************************************************************/
                    284: 
                    285: char *Sys_GetClipboardData(void)
                    286: {
                    287:        return NULL;
                    288: }
                    289: 
                    290: int main (int argc, char **argv)
                    291: {
                    292:        int     time, oldtime, newtime;
                    293: 
                    294:        // go back to real user for config loads
                    295:        saved_euid = geteuid();
                    296:        seteuid(getuid());
                    297: 
                    298:        Qcommon_Init(argc, argv);
                    299: 
                    300:        fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
                    301: 
                    302:        nostdout = Cvar_Get("nostdout", "0", 0);
                    303:        if (!nostdout->value) {
                    304:                fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
                    305: //             printf ("Linux Quake -- Version %0.3f\n", LINUX_VERSION);
                    306:        }
                    307: 
                    308:     oldtime = Sys_Milliseconds ();
                    309:     while (1)
                    310:     {
                    311: // find time spent rendering last frame
                    312:                do {
                    313:                        newtime = Sys_Milliseconds ();
                    314:                        time = newtime - oldtime;
                    315:                } while (time < 1);
                    316:         Qcommon_Frame (time);
                    317:                oldtime = newtime;
                    318:     }
                    319: 
                    320: }
                    321: 
                    322: void Sys_CopyProtect(void)
                    323: {
                    324:        FILE *mnt;
                    325:        struct mntent *ent;
                    326:        char path[MAX_OSPATH];
                    327:        struct stat st;
                    328:        qboolean found_cd = false;
                    329: 
                    330:        static qboolean checked = false;
                    331: 
                    332:        if (checked)
                    333:                return;
                    334: 
                    335:        if ((mnt = setmntent("/etc/mtab", "r")) == NULL)
                    336:                Com_Error(ERR_FATAL, "Can't read mount table to determine mounted cd location.");
                    337: 
                    338:        while ((ent = getmntent(mnt)) != NULL) {
                    339:                if (strcmp(ent->mnt_type, "iso9660") == 0) {
                    340:                        // found a cd file system
                    341:                        found_cd = true;
                    342:                        sprintf(path, "%s/%s", ent->mnt_dir, "install/data/quake2.exe");
                    343:                        if (stat(path, &st) == 0) {
                    344:                                // found it
                    345:                                checked = true;
                    346:                                endmntent(mnt);
                    347:                                return;
                    348:                        }
                    349:                        sprintf(path, "%s/%s", ent->mnt_dir, "Install/Data/quake2.exe");
                    350:                        if (stat(path, &st) == 0) {
                    351:                                // found it
                    352:                                checked = true;
                    353:                                endmntent(mnt);
                    354:                                return;
                    355:                        }
                    356:                        sprintf(path, "%s/%s", ent->mnt_dir, "quake2.exe");
                    357:                        if (stat(path, &st) == 0) {
                    358:                                // found it
                    359:                                checked = true;
                    360:                                endmntent(mnt);
                    361:                                return;
                    362:                        }
                    363:                }
                    364:        }
                    365:        endmntent(mnt);
                    366: 
                    367:        if (found_cd)
                    368:                Com_Error (ERR_FATAL, "Could not find a Quake2 CD in your CD drive.");
                    369:        Com_Error (ERR_FATAL, "Unable to find a mounted iso9660 file system.\n"
                    370:                "You must mount the Quake2 CD in a cdrom drive in order to play.");
                    371: }
                    372: 
                    373: #if 0
                    374: /*
                    375: ================
                    376: Sys_MakeCodeWriteable
                    377: ================
                    378: */
                    379: void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
                    380: {
                    381: 
                    382:        int r;
                    383:        unsigned long addr;
                    384:        int psize = getpagesize();
                    385: 
                    386:        addr = (startaddr & ~(psize-1)) - psize;
                    387: 
                    388: //     fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
                    389: //                     addr, startaddr+length, length);
                    390: 
                    391:        r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
                    392: 
                    393:        if (r < 0)
                    394:                Sys_Error("Protection change failed\n");
                    395: 
                    396: }
                    397: 
                    398: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.