[BACK]Return to ox_launch.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / io

Annotation of OpenXM_contrib2/asir2000/io/ox_launch.c, Revision 1.1.1.1

1.1       noro        1: /* $OpenXM: OpenXM/src/asir99/io/ox_launch.c,v 1.1.1.1 1999/11/10 08:12:30 noro Exp $ */
                      2: #include "ca.h"
                      3: #include "com.h"
                      4: #include "ox.h"
                      5: #include <signal.h>
                      6: #include <setjmp.h>
                      7: #include <fcntl.h>
                      8: #if defined(VISUAL)
                      9: #include <windows.h>
                     10: #include <io.h>
                     11: #include <fcntl.h>
                     12: #include <process.h>
                     13: #else
                     14: #include <sys/file.h>
                     15: #include <sys/types.h>
                     16: #include <sys/stat.h>
                     17: #include <pwd.h>
                     18: #endif
                     19:
                     20: #if defined(SYSV)
                     21: #include <unistd.h>
                     22: #endif
                     23:
                     24: #if defined(VISUAL)
                     25: HANDLE hIntr,hReset,hKill;
                     26: #endif
                     27:
                     28: static void put_log(char *);
                     29: static int which_command(char *,char *);
                     30: static int search_command(char *);
                     31: static int ox_spawn(char *,int,char *);
                     32: static void launch_error(char *);
                     33: static int ox_io_init(int);
                     34: static void push_one(Obj);
                     35: static Obj pop_one();
                     36: static void do_cmd(int);
                     37:
                     38: static Obj *asir_OperandStack;
                     39: static int asir_OperandStackPtr;
                     40:
                     41: static char hostname[BUFSIZ];
                     42: static int sindex;
                     43: static struct sockaddr_in peer;
                     44: static int cpid;
                     45:
                     46: static void put_log(str)
                     47: char *str;
                     48: {
                     49:        static FILE *logfile;
                     50:
                     51:        if ( !logfile )
                     52:                logfile = fopen("/tmp/ox_log","w");
                     53:        fprintf(logfile,"%s\n",str);
                     54:        fflush(logfile);
                     55: }
                     56:
                     57: #if !defined(VISUAL)
                     58: static int which_command(com,file)
                     59: char *com,*file;
                     60: {
                     61:        char *c,*s;
                     62:        int len;
                     63:        char dir[BUFSIZ],path[BUFSIZ];
                     64:
                     65:        for ( s = (char *)getenv("PATH"); s; ) {
                     66:                c = (char *)index(s,':');
                     67:                if ( c ) {
                     68:                        len = c-s;
                     69:                        strncpy(dir,s,len); s = c+1; dir[len] = 0;
                     70:                } else {
                     71:                        strcpy(dir,s); s = 0;
                     72:                }
                     73:                sprintf(path,"%s/%s",dir,com);
                     74:                if ( search_command(path) ) {
                     75:                        strcpy(file,path); return 1;
                     76:                }
                     77:        }
                     78:        file[0] = 0; return 0;
                     79: }
                     80:
                     81: static int search_command(file)
                     82: char *file;
                     83: {
                     84:        struct stat buf;
                     85:
                     86:        if ( stat(file,&buf) || (buf.st_mode & S_IFDIR) )
                     87:                return 0;
                     88:        if ( access(file,X_OK) )
                     89:                return 0;
                     90:        else
                     91:                return 1;
                     92: }
                     93: #endif
                     94:
                     95: /*
                     96:        argv[1] : host to connect
                     97:        argv[2] : if 1, call try_bind_listen and try_accept
                     98:                        if 0, call try_connect
                     99:        argv[3] : control_port
                    100:        argv[4] : server_port
                    101:        argv[5] : server
                    102:        argv[6] : display or "0"
                    103: */
                    104:
                    105: void launch_main(argc,argv)
                    106: int argc;
                    107: char **argv;
                    108: {
                    109:        int id;
                    110:        Obj p,obj;
                    111:        char *name;
                    112:        char buf[BUFSIZ];
                    113:        int cs,ss;
                    114:        unsigned int cmd;
                    115:        int use_unix,accept_client;
                    116:        char *control_port_str,*server_port_str;
                    117:        char *rhost,*server,*dname;
                    118:
                    119:        GC_init(); nglob_init();
                    120:        gethostname(hostname,BUFSIZ);
                    121:        rhost = argv[1];
                    122:        use_unix = !strcmp(rhost,".") ? 1 : 0;
                    123:        accept_client = atoi(argv[2]) ? 1 : 0;
                    124:        control_port_str = argv[3];
                    125:        server_port_str = argv[4];
                    126:        server = argv[5];
                    127:        dname = argv[6];
                    128:
                    129: #if defined(VISUAL)
                    130:                init_socket();
                    131: #endif
                    132:
                    133:        signal(SIGINT,SIG_IGN);
                    134: #if defined(SIGUSR1)
                    135:        signal(SIGUSR1,SIG_IGN);
                    136: #endif
                    137:
                    138:        if ( accept_client ) {
                    139:                cs = try_bind_listen(use_unix,control_port_str);
                    140:                ss = try_bind_listen(use_unix,server_port_str);
                    141:                cs = try_accept(use_unix,cs);
                    142:                ss = try_accept(use_unix,ss);
                    143:        } else {
                    144:                cs = try_connect(use_unix,rhost,control_port_str);
                    145:                ss = try_connect(use_unix,rhost,server_port_str);
                    146:        }
                    147:        ox_io_init(cs);
                    148:        if ( cs < 0 || ss < 0 )
                    149:                launch_error("cannot connect to the client");
                    150:        cpid = ox_spawn(server,ss,dname);
                    151:
                    152:        while ( 1 ) {
                    153:                ox_recv(sindex,&id,&obj);
                    154:                switch ( id ) {
                    155:                        case OX_COMMAND:
                    156:                                cmd = ((USINT)obj)->body;
                    157:                                do_cmd(cmd);
                    158:                                break;
                    159:                        case OX_DATA:
                    160:                                push_one(obj);
                    161:                                break;
                    162:                        case OX_SYNC_BALL:
                    163:                                break;
                    164:                        default:
                    165:                                break;
                    166:                }
                    167:        }
                    168: }
                    169:
                    170: #if defined(VISUAL)
                    171: static void do_cmd(cmd)
                    172: int cmd;
                    173: {
                    174:        USINT t;
                    175:        int id,cindex;
                    176:        int bport,sport;
                    177:        int bs,bs0;
                    178:        STRING prog,dname;
                    179:
                    180:        switch ( cmd ) {
                    181:                case SM_shutdown:
                    182:                        SetEvent(hKill);
                    183:                        exit(0); break;
                    184:                case SM_control_intr:
                    185:                        SetEvent(hIntr);
                    186:                        break;
                    187:                case SM_control_kill:
                    188:                        SetEvent(hKill);
                    189:                        break;
                    190:                case SM_control_reset_connection:
                    191:                        MKUSINT(t,0);
                    192:                        ox_send_data(sindex,t);
                    193:                        SetEvent(hReset);
                    194:                        break;
                    195:                default:
                    196:                        break;
                    197:        }
                    198: }
                    199: #else
                    200: static void do_cmd(cmd)
                    201: int cmd;
                    202: {
                    203:        USINT t;
                    204:        int id,cindex;
                    205:        int bport,sport;
                    206:        int bs,bs0;
                    207:        int status;
                    208:        STRING prog,dname;
                    209:
                    210:        switch ( cmd ) {
                    211:                case SM_shutdown:
                    212:                        kill(cpid,SIGKILL);
                    213:                        exit(0); break;
                    214:                case SM_control_intr:
                    215:                        kill(cpid,SIGINT);
                    216:                        break;
                    217:                case SM_control_kill:
                    218:                        kill(cpid,SIGKILL);
                    219:                        break;
                    220:                case SM_control_reset_connection:
                    221:                        MKUSINT(t,0);
                    222:                        ox_send_data(sindex,t);
                    223:                        kill(cpid,SIGUSR1);
                    224:                        break;
                    225:                default:
                    226:                        break;
                    227:        }
                    228: }
                    229: #endif
                    230:
                    231: static int ox_spawn(prog,bs,dname)
                    232: char *prog;
                    233: int bs;
                    234: char *dname;
                    235: {
                    236: #if defined(VISUAL)
                    237:        char *av[BUFSIZ];
                    238:        char sock_id[BUFSIZ],ox_intr[BUFSIZ],ox_reset[BUFSIZ],ox_kill[BUFSIZ];
                    239:        char AsirExe[BUFSIZ];
                    240:        HANDLE hProc;
                    241:        STRING rootdir;
                    242:        int mypid;
                    243:
                    244:        mypid = GetCurrentProcessId();
                    245:        sprintf(ox_intr,"ox_intr_%d",mypid);
                    246:        sprintf(ox_reset,"ox_reset_%d",mypid);
                    247:        sprintf(ox_kill,"ox_kill_%d",mypid);
                    248:        hIntr = CreateEvent(NULL,TRUE,FALSE,ox_intr);
                    249:        hReset = CreateEvent(NULL,TRUE,FALSE,ox_reset);
                    250:        hKill = CreateEvent(NULL,TRUE,FALSE,ox_kill);
                    251:        sprintf(sock_id,"%d",bs);
                    252:        av[0] = prog;
                    253:        av[1] = sock_id;
                    254:        av[2] = ox_intr;
                    255:        av[3] = ox_reset;
                    256:        av[4] = ox_kill;
                    257:        av[5] = NULL;
                    258:        Pget_rootdir(&rootdir);
                    259:        sprintf(AsirExe,"%s\\bin\\engine.exe",BDY(rootdir));
                    260:        hProc = _spawnv(_P_NOWAIT,AsirExe,av);
                    261:        return (int)hProc;
                    262: #else /* VISUAL */
                    263:        int b,s,i;
                    264:        struct hostent *hp;
                    265:        int pid;
                    266:        char wname[BUFSIZ];
                    267:        char buf[BUFSIZ];
                    268:
                    269:        pid = fork();
                    270:        if ( pid )
                    271:                return pid;
                    272:        else {
                    273:                setpgid(0,getpid());
                    274:                if ( dup2(bs,3) != 3 )
                    275:                        exit(1);
                    276:                if ( dup2(bs,4) != 4 )
                    277:                        exit(1);
                    278:                {
                    279: #if defined(linux) || defined(__NeXT__) || defined(ultrix)
                    280: #include <sys/param.h>
                    281:                        close(0);
                    282:                        for ( i = 5; i < NOFILE; i++ )
                    283:                                close(i);
                    284: #else
                    285: #include <sys/resource.h>
                    286:                        struct rlimit rl;
                    287:
                    288:                        getrlimit(RLIMIT_NOFILE,&rl);
                    289:                        close(0);
                    290:                        for ( i = 5; i < rl.rlim_cur; i++ )
                    291:                                close(i);
                    292: #endif
                    293:                }
                    294:                if ( strcmp(dname,"0") )
                    295:                        execl(prog,prog,"-display",dname,0);
                    296:                else {
                    297:                        FILE *null;
                    298:
                    299:                        null = fopen("/dev/null","wb");
                    300:                        dup2(fileno(null),1);
                    301:                        dup2(fileno(null),2);
                    302:                        putenv("DISPLAY=");
                    303:                        execl(prog,prog,0);
                    304:                }
                    305:        }
                    306: #endif
                    307: }
                    308:
                    309: static void launch_error(s)
                    310: char *s;
                    311: {
                    312:        exit(0);
                    313: }
                    314:
                    315: static int ox_io_init(sock)
                    316: int sock;
                    317: {
                    318:        endian_init();
                    319:        /* server mode */
                    320:        sindex = get_iofp(sock,0,1);
                    321:        asir_OperandStack = (Obj *)CALLOC(BUFSIZ,sizeof(Obj));
                    322:        asir_OperandStackPtr = -1;
                    323: }
                    324:
                    325: static void push_one(obj)
                    326: Obj obj;
                    327: {
                    328:        if ( !obj || OID(obj) != O_VOID )
                    329:                asir_OperandStack[++asir_OperandStackPtr] = obj;
                    330: }
                    331:
                    332: static Obj pop_one() {
                    333:        if ( asir_OperandStackPtr >= 0 ) {
                    334:                return asir_OperandStack[asir_OperandStackPtr--];
                    335:        }
                    336: }
                    337:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>