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

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.11    ! noro       47:  * $OpenXM: OpenXM_contrib2/asir2000/io/ox_launch.c,v 1.10 2000/11/08 06:21:17 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)
1.7       noro       56: #include <stdlib.h>
1.1       noro       57: #include <windows.h>
                     58: #include <io.h>
                     59: #include <fcntl.h>
                     60: #include <process.h>
                     61: #else
                     62: #include <sys/file.h>
                     63: #include <sys/types.h>
                     64: #include <sys/stat.h>
                     65: #include <pwd.h>
                     66: #endif
                     67:
                     68: #if defined(SYSV)
                     69: #include <unistd.h>
                     70: #endif
                     71:
                     72: #if defined(VISUAL)
                     73: HANDLE hIntr,hReset,hKill;
                     74: #endif
                     75:
                     76: static void put_log(char *);
                     77: static int which_command(char *,char *);
                     78: static int search_command(char *);
1.10      noro       79: static int ox_spawn(char *,int,char *,int);
1.1       noro       80: static void launch_error(char *);
                     81: static int ox_io_init(int);
                     82: static void push_one(Obj);
                     83: static Obj pop_one();
                     84: static void do_cmd(int);
                     85:
                     86: static Obj *asir_OperandStack;
                     87: static int asir_OperandStackPtr;
                     88:
                     89: static char hostname[BUFSIZ];
                     90: static int sindex;
                     91: static struct sockaddr_in peer;
                     92: static int cpid;
                     93:
                     94: static void put_log(str)
                     95: char *str;
                     96: {
                     97:        static FILE *logfile;
                     98:
                     99:        if ( !logfile )
                    100:                logfile = fopen("/tmp/ox_log","w");
                    101:        fprintf(logfile,"%s\n",str);
                    102:        fflush(logfile);
                    103: }
                    104:
                    105: #if !defined(VISUAL)
                    106: static int which_command(com,file)
                    107: char *com,*file;
                    108: {
                    109:        char *c,*s;
                    110:        int len;
                    111:        char dir[BUFSIZ],path[BUFSIZ];
                    112:
                    113:        for ( s = (char *)getenv("PATH"); s; ) {
                    114:                c = (char *)index(s,':');
                    115:                if ( c ) {
                    116:                        len = c-s;
                    117:                        strncpy(dir,s,len); s = c+1; dir[len] = 0;
                    118:                } else {
                    119:                        strcpy(dir,s); s = 0;
                    120:                }
                    121:                sprintf(path,"%s/%s",dir,com);
                    122:                if ( search_command(path) ) {
                    123:                        strcpy(file,path); return 1;
                    124:                }
                    125:        }
                    126:        file[0] = 0; return 0;
                    127: }
                    128:
                    129: static int search_command(file)
                    130: char *file;
                    131: {
                    132:        struct stat buf;
                    133:
                    134:        if ( stat(file,&buf) || (buf.st_mode & S_IFDIR) )
                    135:                return 0;
                    136:        if ( access(file,X_OK) )
                    137:                return 0;
                    138:        else
                    139:                return 1;
                    140: }
                    141: #endif
                    142:
                    143: /*
                    144:        argv[1] : host to connect
                    145:        argv[2] : if 1, call try_bind_listen and try_accept
                    146:                        if 0, call try_connect
                    147:        argv[3] : control_port
                    148:        argv[4] : server_port
                    149:        argv[5] : server
                    150:        argv[6] : display or "0"
1.10      noro      151:        argv[7] : if exists, it should be "-nolog"
1.1       noro      152: */
                    153:
                    154: void launch_main(argc,argv)
                    155: int argc;
                    156: char **argv;
                    157: {
                    158:        int id;
                    159:        Obj p,obj;
                    160:        char *name;
                    161:        char buf[BUFSIZ];
                    162:        int cs,ss;
                    163:        unsigned int cmd;
1.10      noro      164:        int use_unix,accept_client,nolog;
1.1       noro      165:        char *control_port_str,*server_port_str;
                    166:        char *rhost,*server,*dname;
                    167:
                    168:        GC_init(); nglob_init();
                    169:        gethostname(hostname,BUFSIZ);
                    170:        rhost = argv[1];
                    171:        use_unix = !strcmp(rhost,".") ? 1 : 0;
                    172:        accept_client = atoi(argv[2]) ? 1 : 0;
                    173:        control_port_str = argv[3];
                    174:        server_port_str = argv[4];
                    175:        server = argv[5];
                    176:        dname = argv[6];
1.10      noro      177:        nolog = argc > 7 ? 1 : 0;
1.1       noro      178:
                    179: #if defined(VISUAL)
                    180:                init_socket();
                    181: #endif
                    182:
                    183:        signal(SIGINT,SIG_IGN);
                    184: #if defined(SIGUSR1)
                    185:        signal(SIGUSR1,SIG_IGN);
                    186: #endif
                    187:
                    188:        if ( accept_client ) {
                    189:                cs = try_bind_listen(use_unix,control_port_str);
                    190:                ss = try_bind_listen(use_unix,server_port_str);
                    191:                cs = try_accept(use_unix,cs);
                    192:                ss = try_accept(use_unix,ss);
                    193:        } else {
                    194:                cs = try_connect(use_unix,rhost,control_port_str);
                    195:                ss = try_connect(use_unix,rhost,server_port_str);
                    196:        }
                    197:        ox_io_init(cs);
                    198:        if ( cs < 0 || ss < 0 )
                    199:                launch_error("cannot connect to the client");
1.10      noro      200:        cpid = ox_spawn(server,ss,dname,nolog);
1.1       noro      201:
                    202:        while ( 1 ) {
                    203:                ox_recv(sindex,&id,&obj);
                    204:                switch ( id ) {
                    205:                        case OX_COMMAND:
                    206:                                cmd = ((USINT)obj)->body;
                    207:                                do_cmd(cmd);
                    208:                                break;
                    209:                        case OX_DATA:
                    210:                                push_one(obj);
                    211:                                break;
                    212:                        case OX_SYNC_BALL:
                    213:                                break;
                    214:                        default:
                    215:                                break;
                    216:                }
                    217:        }
                    218: }
                    219:
                    220: #if defined(VISUAL)
                    221: static void do_cmd(cmd)
                    222: int cmd;
                    223: {
                    224:        USINT t;
                    225:        int id,cindex;
                    226:        int bport,sport;
                    227:        int bs,bs0;
                    228:        STRING prog,dname;
                    229:
                    230:        switch ( cmd ) {
                    231:                case SM_shutdown:
                    232:                        SetEvent(hKill);
1.8       noro      233:                        ExitProcess(0);
                    234:                        break;
1.1       noro      235:                case SM_control_intr:
                    236:                        SetEvent(hIntr);
                    237:                        break;
                    238:                case SM_control_kill:
                    239:                        SetEvent(hKill);
                    240:                        break;
                    241:                case SM_control_reset_connection:
                    242:                        MKUSINT(t,0);
                    243:                        ox_send_data(sindex,t);
                    244:                        SetEvent(hReset);
                    245:                        break;
                    246:                default:
                    247:                        break;
                    248:        }
                    249: }
                    250: #else
                    251: static void do_cmd(cmd)
                    252: int cmd;
                    253: {
                    254:        USINT t;
                    255:        int id,cindex;
                    256:        int bport,sport;
                    257:        int bs,bs0;
                    258:        int status;
                    259:        STRING prog,dname;
                    260:
                    261:        switch ( cmd ) {
                    262:                case SM_shutdown:
                    263:                        kill(cpid,SIGKILL);
                    264:                        exit(0); break;
                    265:                case SM_control_intr:
                    266:                        kill(cpid,SIGINT);
                    267:                        break;
                    268:                case SM_control_kill:
                    269:                        kill(cpid,SIGKILL);
                    270:                        break;
                    271:                case SM_control_reset_connection:
                    272:                        MKUSINT(t,0);
                    273:                        ox_send_data(sindex,t);
                    274:                        kill(cpid,SIGUSR1);
                    275:                        break;
                    276:                default:
                    277:                        break;
                    278:        }
                    279: }
                    280: #endif
                    281:
1.10      noro      282: static int ox_spawn(prog,bs,dname,nolog)
1.1       noro      283: char *prog;
                    284: int bs;
                    285: char *dname;
1.10      noro      286: int nolog;
1.1       noro      287: {
                    288: #if defined(VISUAL)
                    289:        char *av[BUFSIZ];
                    290:        char sock_id[BUFSIZ],ox_intr[BUFSIZ],ox_reset[BUFSIZ],ox_kill[BUFSIZ];
                    291:        char AsirExe[BUFSIZ];
                    292:        HANDLE hProc;
                    293:        STRING rootdir;
                    294:        int mypid;
1.7       noro      295:        int newbs;
1.1       noro      296:
1.7       noro      297:        DuplicateHandle(GetCurrentProcess(),(HANDLE)bs,
                    298:                                        GetCurrentProcess(),(HANDLE *)&newbs,
                    299:                                        0,TRUE,DUPLICATE_SAME_ACCESS);
                    300:        close(bs);
                    301:        bs = newbs;
                    302:
1.1       noro      303:        mypid = GetCurrentProcessId();
                    304:        sprintf(ox_intr,"ox_intr_%d",mypid);
                    305:        sprintf(ox_reset,"ox_reset_%d",mypid);
                    306:        sprintf(ox_kill,"ox_kill_%d",mypid);
                    307:        hIntr = CreateEvent(NULL,TRUE,FALSE,ox_intr);
                    308:        hReset = CreateEvent(NULL,TRUE,FALSE,ox_reset);
                    309:        hKill = CreateEvent(NULL,TRUE,FALSE,ox_kill);
                    310:        sprintf(sock_id,"%d",bs);
                    311:        av[0] = prog;
                    312:        av[1] = sock_id;
1.7       noro      313:        av[2] = dname; /* if dname == "1" then a message window is opened */
                    314:        av[3] = ox_intr;
                    315:        av[4] = ox_reset;
                    316:        av[5] = ox_kill;
                    317:        av[6] = NULL;
1.1       noro      318:        Pget_rootdir(&rootdir);
1.9       noro      319:        sprintf(AsirExe,"%s\\bin\\engine.exe",BDY(rootdir));
1.7       noro      320:        _fileinfo = 1;
1.1       noro      321:        hProc = _spawnv(_P_NOWAIT,AsirExe,av);
                    322:        return (int)hProc;
                    323: #else /* VISUAL */
                    324:        int b,s,i;
                    325:        struct hostent *hp;
                    326:        int pid;
                    327:        char wname[BUFSIZ];
                    328:        char buf[BUFSIZ];
1.5       noro      329:        char errcode;
1.1       noro      330:
                    331:        pid = fork();
                    332:        if ( pid )
                    333:                return pid;
                    334:        else {
                    335:                setpgid(0,getpid());
                    336:                if ( dup2(bs,3) != 3 )
                    337:                        exit(1);
                    338:                if ( dup2(bs,4) != 4 )
                    339:                        exit(1);
                    340:                {
                    341: #if defined(linux) || defined(__NeXT__) || defined(ultrix)
                    342: #include <sys/param.h>
                    343:                        close(0);
                    344:                        for ( i = 5; i < NOFILE; i++ )
                    345:                                close(i);
                    346: #else
                    347: #include <sys/resource.h>
                    348:                        struct rlimit rl;
                    349:
                    350:                        getrlimit(RLIMIT_NOFILE,&rl);
                    351:                        close(0);
                    352:                        for ( i = 5; i < rl.rlim_cur; i++ )
                    353:                                close(i);
                    354: #endif
                    355:                }
1.6       noro      356:                if ( !strcmp(dname,"1" ) ) /* XXX: for ssh */
                    357:                        execl(prog,prog,0);
1.10      noro      358:                else if ( nolog || !strcmp(dname,"0") ) {
1.1       noro      359:                        FILE *null;
                    360:
                    361:                        null = fopen("/dev/null","wb");
                    362:                        dup2(fileno(null),1);
                    363:                        dup2(fileno(null),2);
1.11    ! noro      364:                        if ( strcmp(dname,"0") )
        !           365:                                execl(prog,prog,"-display",dname,0);
        !           366:                        else {
1.10      noro      367:                                putenv("DISPLAY=");
                    368:                                execl(prog,prog,0);
1.11    ! noro      369:                        }
1.6       noro      370:                } else
                    371:                        execl(prog,prog,"-display",dname,0);
1.5       noro      372:                /* On failure */
                    373:                errcode = 2;
                    374:                write(4,&errcode,1);
                    375:                exit(1);
1.1       noro      376:        }
                    377: #endif
                    378: }
                    379:
                    380: static void launch_error(s)
                    381: char *s;
                    382: {
                    383:        exit(0);
                    384: }
                    385:
                    386: static int ox_io_init(sock)
                    387: int sock;
                    388: {
                    389:        endian_init();
                    390:        /* server mode */
                    391:        sindex = get_iofp(sock,0,1);
                    392:        asir_OperandStack = (Obj *)CALLOC(BUFSIZ,sizeof(Obj));
                    393:        asir_OperandStackPtr = -1;
                    394: }
                    395:
                    396: static void push_one(obj)
                    397: Obj obj;
                    398: {
                    399:        if ( !obj || OID(obj) != O_VOID )
                    400:                asir_OperandStack[++asir_OperandStackPtr] = obj;
                    401: }
                    402:
                    403: static Obj pop_one() {
                    404:        if ( asir_OperandStackPtr >= 0 ) {
                    405:                return asir_OperandStack[asir_OperandStackPtr--];
                    406:        }
                    407: }
                    408:

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