[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.4

1.3       noro        1: /*
                      2:  * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
                      3:  * All rights reserved.
                      4:  *
                      5:  * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
                      6:  * non-exclusive and royalty-free license to use, copy, modify and
                      7:  * redistribute, solely for non-commercial and non-profit purposes, the
                      8:  * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
                      9:  * conditions of this Agreement. For the avoidance of doubt, you acquire
                     10:  * only a limited right to use the SOFTWARE hereunder, and FLL or any
                     11:  * third party developer retains all rights, including but not limited to
                     12:  * copyrights, in and to the SOFTWARE.
                     13:  *
                     14:  * (1) FLL does not grant you a license in any way for commercial
                     15:  * purposes. You may use the SOFTWARE only for non-commercial and
                     16:  * non-profit purposes only, such as academic, research and internal
                     17:  * business use.
                     18:  * (2) The SOFTWARE is protected by the Copyright Law of Japan and
                     19:  * international copyright treaties. If you make copies of the SOFTWARE,
                     20:  * with or without modification, as permitted hereunder, you shall affix
                     21:  * to all such copies of the SOFTWARE the above copyright notice.
                     22:  * (3) An explicit reference to this SOFTWARE and its copyright owner
                     23:  * shall be made on your publication or presentation in any form of the
                     24:  * results obtained by use of the SOFTWARE.
                     25:  * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.4     ! noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.3       noro       27:  * for such modification or the source code of the modified part of the
                     28:  * SOFTWARE.
                     29:  *
                     30:  * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
                     31:  * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
                     32:  * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
                     33:  * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
                     34:  * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
                     35:  * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
                     36:  * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
                     37:  * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
                     38:  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
                     39:  * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
                     40:  * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
                     41:  * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
                     42:  * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
                     43:  * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
                     44:  * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
                     45:  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
                     46:  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
1.4     ! noro       47:  * $OpenXM: OpenXM_contrib2/asir2000/io/ox_launch.c,v 1.3 2000/08/21 08:31:38 noro Exp $
1.3       noro       48: */
1.2       noro       49: #include <setjmp.h>
                     50: #include <signal.h>
                     51: #include <fcntl.h>
1.1       noro       52: #include "ca.h"
                     53: #include "com.h"
                     54: #include "ox.h"
                     55: #if defined(VISUAL)
                     56: #include <windows.h>
                     57: #include <io.h>
                     58: #include <fcntl.h>
                     59: #include <process.h>
                     60: #else
                     61: #include <sys/file.h>
                     62: #include <sys/types.h>
                     63: #include <sys/stat.h>
                     64: #include <pwd.h>
                     65: #endif
                     66:
                     67: #if defined(SYSV)
                     68: #include <unistd.h>
                     69: #endif
                     70:
                     71: #if defined(VISUAL)
                     72: HANDLE hIntr,hReset,hKill;
                     73: #endif
                     74:
                     75: static void put_log(char *);
                     76: static int which_command(char *,char *);
                     77: static int search_command(char *);
                     78: static int ox_spawn(char *,int,char *);
                     79: static void launch_error(char *);
                     80: static int ox_io_init(int);
                     81: static void push_one(Obj);
                     82: static Obj pop_one();
                     83: static void do_cmd(int);
                     84:
                     85: static Obj *asir_OperandStack;
                     86: static int asir_OperandStackPtr;
                     87:
                     88: static char hostname[BUFSIZ];
                     89: static int sindex;
                     90: static struct sockaddr_in peer;
                     91: static int cpid;
                     92:
                     93: static void put_log(str)
                     94: char *str;
                     95: {
                     96:        static FILE *logfile;
                     97:
                     98:        if ( !logfile )
                     99:                logfile = fopen("/tmp/ox_log","w");
                    100:        fprintf(logfile,"%s\n",str);
                    101:        fflush(logfile);
                    102: }
                    103:
                    104: #if !defined(VISUAL)
                    105: static int which_command(com,file)
                    106: char *com,*file;
                    107: {
                    108:        char *c,*s;
                    109:        int len;
                    110:        char dir[BUFSIZ],path[BUFSIZ];
                    111:
                    112:        for ( s = (char *)getenv("PATH"); s; ) {
                    113:                c = (char *)index(s,':');
                    114:                if ( c ) {
                    115:                        len = c-s;
                    116:                        strncpy(dir,s,len); s = c+1; dir[len] = 0;
                    117:                } else {
                    118:                        strcpy(dir,s); s = 0;
                    119:                }
                    120:                sprintf(path,"%s/%s",dir,com);
                    121:                if ( search_command(path) ) {
                    122:                        strcpy(file,path); return 1;
                    123:                }
                    124:        }
                    125:        file[0] = 0; return 0;
                    126: }
                    127:
                    128: static int search_command(file)
                    129: char *file;
                    130: {
                    131:        struct stat buf;
                    132:
                    133:        if ( stat(file,&buf) || (buf.st_mode & S_IFDIR) )
                    134:                return 0;
                    135:        if ( access(file,X_OK) )
                    136:                return 0;
                    137:        else
                    138:                return 1;
                    139: }
                    140: #endif
                    141:
                    142: /*
                    143:        argv[1] : host to connect
                    144:        argv[2] : if 1, call try_bind_listen and try_accept
                    145:                        if 0, call try_connect
                    146:        argv[3] : control_port
                    147:        argv[4] : server_port
                    148:        argv[5] : server
                    149:        argv[6] : display or "0"
                    150: */
                    151:
                    152: void launch_main(argc,argv)
                    153: int argc;
                    154: char **argv;
                    155: {
                    156:        int id;
                    157:        Obj p,obj;
                    158:        char *name;
                    159:        char buf[BUFSIZ];
                    160:        int cs,ss;
                    161:        unsigned int cmd;
                    162:        int use_unix,accept_client;
                    163:        char *control_port_str,*server_port_str;
                    164:        char *rhost,*server,*dname;
                    165:
                    166:        GC_init(); nglob_init();
                    167:        gethostname(hostname,BUFSIZ);
                    168:        rhost = argv[1];
                    169:        use_unix = !strcmp(rhost,".") ? 1 : 0;
                    170:        accept_client = atoi(argv[2]) ? 1 : 0;
                    171:        control_port_str = argv[3];
                    172:        server_port_str = argv[4];
                    173:        server = argv[5];
                    174:        dname = argv[6];
                    175:
                    176: #if defined(VISUAL)
                    177:                init_socket();
                    178: #endif
                    179:
                    180:        signal(SIGINT,SIG_IGN);
                    181: #if defined(SIGUSR1)
                    182:        signal(SIGUSR1,SIG_IGN);
                    183: #endif
                    184:
                    185:        if ( accept_client ) {
                    186:                cs = try_bind_listen(use_unix,control_port_str);
                    187:                ss = try_bind_listen(use_unix,server_port_str);
                    188:                cs = try_accept(use_unix,cs);
                    189:                ss = try_accept(use_unix,ss);
                    190:        } else {
                    191:                cs = try_connect(use_unix,rhost,control_port_str);
                    192:                ss = try_connect(use_unix,rhost,server_port_str);
                    193:        }
                    194:        ox_io_init(cs);
                    195:        if ( cs < 0 || ss < 0 )
                    196:                launch_error("cannot connect to the client");
                    197:        cpid = ox_spawn(server,ss,dname);
                    198:
                    199:        while ( 1 ) {
                    200:                ox_recv(sindex,&id,&obj);
                    201:                switch ( id ) {
                    202:                        case OX_COMMAND:
                    203:                                cmd = ((USINT)obj)->body;
                    204:                                do_cmd(cmd);
                    205:                                break;
                    206:                        case OX_DATA:
                    207:                                push_one(obj);
                    208:                                break;
                    209:                        case OX_SYNC_BALL:
                    210:                                break;
                    211:                        default:
                    212:                                break;
                    213:                }
                    214:        }
                    215: }
                    216:
                    217: #if defined(VISUAL)
                    218: static void do_cmd(cmd)
                    219: int cmd;
                    220: {
                    221:        USINT t;
                    222:        int id,cindex;
                    223:        int bport,sport;
                    224:        int bs,bs0;
                    225:        STRING prog,dname;
                    226:
                    227:        switch ( cmd ) {
                    228:                case SM_shutdown:
                    229:                        SetEvent(hKill);
                    230:                        exit(0); break;
                    231:                case SM_control_intr:
                    232:                        SetEvent(hIntr);
                    233:                        break;
                    234:                case SM_control_kill:
                    235:                        SetEvent(hKill);
                    236:                        break;
                    237:                case SM_control_reset_connection:
                    238:                        MKUSINT(t,0);
                    239:                        ox_send_data(sindex,t);
                    240:                        SetEvent(hReset);
                    241:                        break;
                    242:                default:
                    243:                        break;
                    244:        }
                    245: }
                    246: #else
                    247: static void do_cmd(cmd)
                    248: int cmd;
                    249: {
                    250:        USINT t;
                    251:        int id,cindex;
                    252:        int bport,sport;
                    253:        int bs,bs0;
                    254:        int status;
                    255:        STRING prog,dname;
                    256:
                    257:        switch ( cmd ) {
                    258:                case SM_shutdown:
                    259:                        kill(cpid,SIGKILL);
                    260:                        exit(0); break;
                    261:                case SM_control_intr:
                    262:                        kill(cpid,SIGINT);
                    263:                        break;
                    264:                case SM_control_kill:
                    265:                        kill(cpid,SIGKILL);
                    266:                        break;
                    267:                case SM_control_reset_connection:
                    268:                        MKUSINT(t,0);
                    269:                        ox_send_data(sindex,t);
                    270:                        kill(cpid,SIGUSR1);
                    271:                        break;
                    272:                default:
                    273:                        break;
                    274:        }
                    275: }
                    276: #endif
                    277:
                    278: static int ox_spawn(prog,bs,dname)
                    279: char *prog;
                    280: int bs;
                    281: char *dname;
                    282: {
                    283: #if defined(VISUAL)
                    284:        char *av[BUFSIZ];
                    285:        char sock_id[BUFSIZ],ox_intr[BUFSIZ],ox_reset[BUFSIZ],ox_kill[BUFSIZ];
                    286:        char AsirExe[BUFSIZ];
                    287:        HANDLE hProc;
                    288:        STRING rootdir;
                    289:        int mypid;
                    290:
                    291:        mypid = GetCurrentProcessId();
                    292:        sprintf(ox_intr,"ox_intr_%d",mypid);
                    293:        sprintf(ox_reset,"ox_reset_%d",mypid);
                    294:        sprintf(ox_kill,"ox_kill_%d",mypid);
                    295:        hIntr = CreateEvent(NULL,TRUE,FALSE,ox_intr);
                    296:        hReset = CreateEvent(NULL,TRUE,FALSE,ox_reset);
                    297:        hKill = CreateEvent(NULL,TRUE,FALSE,ox_kill);
                    298:        sprintf(sock_id,"%d",bs);
                    299:        av[0] = prog;
                    300:        av[1] = sock_id;
                    301:        av[2] = ox_intr;
                    302:        av[3] = ox_reset;
                    303:        av[4] = ox_kill;
                    304:        av[5] = NULL;
                    305:        Pget_rootdir(&rootdir);
                    306:        sprintf(AsirExe,"%s\\bin\\engine.exe",BDY(rootdir));
                    307:        hProc = _spawnv(_P_NOWAIT,AsirExe,av);
                    308:        return (int)hProc;
                    309: #else /* VISUAL */
                    310:        int b,s,i;
                    311:        struct hostent *hp;
                    312:        int pid;
                    313:        char wname[BUFSIZ];
                    314:        char buf[BUFSIZ];
                    315:
                    316:        pid = fork();
                    317:        if ( pid )
                    318:                return pid;
                    319:        else {
                    320:                setpgid(0,getpid());
                    321:                if ( dup2(bs,3) != 3 )
                    322:                        exit(1);
                    323:                if ( dup2(bs,4) != 4 )
                    324:                        exit(1);
                    325:                {
                    326: #if defined(linux) || defined(__NeXT__) || defined(ultrix)
                    327: #include <sys/param.h>
                    328:                        close(0);
                    329:                        for ( i = 5; i < NOFILE; i++ )
                    330:                                close(i);
                    331: #else
                    332: #include <sys/resource.h>
                    333:                        struct rlimit rl;
                    334:
                    335:                        getrlimit(RLIMIT_NOFILE,&rl);
                    336:                        close(0);
                    337:                        for ( i = 5; i < rl.rlim_cur; i++ )
                    338:                                close(i);
                    339: #endif
                    340:                }
                    341:                if ( strcmp(dname,"0") )
                    342:                        execl(prog,prog,"-display",dname,0);
                    343:                else {
                    344:                        FILE *null;
                    345:
                    346:                        null = fopen("/dev/null","wb");
                    347:                        dup2(fileno(null),1);
                    348:                        dup2(fileno(null),2);
                    349:                        putenv("DISPLAY=");
                    350:                        execl(prog,prog,0);
                    351:                }
                    352:        }
                    353: #endif
                    354: }
                    355:
                    356: static void launch_error(s)
                    357: char *s;
                    358: {
                    359:        exit(0);
                    360: }
                    361:
                    362: static int ox_io_init(sock)
                    363: int sock;
                    364: {
                    365:        endian_init();
                    366:        /* server mode */
                    367:        sindex = get_iofp(sock,0,1);
                    368:        asir_OperandStack = (Obj *)CALLOC(BUFSIZ,sizeof(Obj));
                    369:        asir_OperandStackPtr = -1;
                    370: }
                    371:
                    372: static void push_one(obj)
                    373: Obj obj;
                    374: {
                    375:        if ( !obj || OID(obj) != O_VOID )
                    376:                asir_OperandStack[++asir_OperandStackPtr] = obj;
                    377: }
                    378:
                    379: static Obj pop_one() {
                    380:        if ( asir_OperandStackPtr >= 0 ) {
                    381:                return asir_OperandStack[asir_OperandStackPtr--];
                    382:        }
                    383: }
                    384:

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