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

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.9.2.1 ! maekawa    47:  * $OpenXM: OpenXM_contrib2/asir2000/io/ox_launch.c,v 1.9 2000/11/07 09:31:36 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 *);
                     79: static int ox_spawn(char *,int,char *);
                     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"
                    151: */
                    152:
                    153: void launch_main(argc,argv)
                    154: int argc;
                    155: char **argv;
                    156: {
                    157:        int id;
                    158:        Obj p,obj;
                    159:        char *name;
                    160:        char buf[BUFSIZ];
                    161:        int cs,ss;
                    162:        unsigned int cmd;
                    163:        int use_unix,accept_client;
                    164:        char *control_port_str,*server_port_str;
                    165:        char *rhost,*server,*dname;
                    166:
                    167:        GC_init(); nglob_init();
                    168:        gethostname(hostname,BUFSIZ);
                    169:        rhost = argv[1];
                    170:        use_unix = !strcmp(rhost,".") ? 1 : 0;
                    171:        accept_client = atoi(argv[2]) ? 1 : 0;
                    172:        control_port_str = argv[3];
                    173:        server_port_str = argv[4];
                    174:        server = argv[5];
                    175:        dname = argv[6];
                    176:
                    177: #if defined(VISUAL)
                    178:                init_socket();
                    179: #endif
                    180:
                    181:        signal(SIGINT,SIG_IGN);
                    182: #if defined(SIGUSR1)
                    183:        signal(SIGUSR1,SIG_IGN);
                    184: #endif
                    185:
                    186:        if ( accept_client ) {
                    187:                cs = try_bind_listen(use_unix,control_port_str);
                    188:                ss = try_bind_listen(use_unix,server_port_str);
1.9.2.1 ! maekawa   189:                cs = try_accept(cs);
        !           190:                ss = try_accept(ss);
1.1       noro      191:        } else {
                    192:                cs = try_connect(use_unix,rhost,control_port_str);
                    193:                ss = try_connect(use_unix,rhost,server_port_str);
                    194:        }
                    195:        ox_io_init(cs);
                    196:        if ( cs < 0 || ss < 0 )
                    197:                launch_error("cannot connect to the client");
                    198:        cpid = ox_spawn(server,ss,dname);
                    199:
                    200:        while ( 1 ) {
                    201:                ox_recv(sindex,&id,&obj);
                    202:                switch ( id ) {
                    203:                        case OX_COMMAND:
                    204:                                cmd = ((USINT)obj)->body;
                    205:                                do_cmd(cmd);
                    206:                                break;
                    207:                        case OX_DATA:
                    208:                                push_one(obj);
                    209:                                break;
                    210:                        case OX_SYNC_BALL:
                    211:                                break;
                    212:                        default:
                    213:                                break;
                    214:                }
                    215:        }
                    216: }
                    217:
                    218: #if defined(VISUAL)
                    219: static void do_cmd(cmd)
                    220: int cmd;
                    221: {
                    222:        USINT t;
                    223:        int id,cindex;
                    224:        int bport,sport;
                    225:        int bs,bs0;
                    226:        STRING prog,dname;
                    227:
                    228:        switch ( cmd ) {
                    229:                case SM_shutdown:
                    230:                        SetEvent(hKill);
1.8       noro      231:                        ExitProcess(0);
                    232:                        break;
1.1       noro      233:                case SM_control_intr:
                    234:                        SetEvent(hIntr);
                    235:                        break;
                    236:                case SM_control_kill:
                    237:                        SetEvent(hKill);
                    238:                        break;
                    239:                case SM_control_reset_connection:
                    240:                        MKUSINT(t,0);
                    241:                        ox_send_data(sindex,t);
                    242:                        SetEvent(hReset);
                    243:                        break;
                    244:                default:
                    245:                        break;
                    246:        }
                    247: }
                    248: #else
                    249: static void do_cmd(cmd)
                    250: int cmd;
                    251: {
                    252:        USINT t;
                    253:        int id,cindex;
                    254:        int bport,sport;
                    255:        int bs,bs0;
                    256:        int status;
                    257:        STRING prog,dname;
                    258:
                    259:        switch ( cmd ) {
                    260:                case SM_shutdown:
                    261:                        kill(cpid,SIGKILL);
                    262:                        exit(0); break;
                    263:                case SM_control_intr:
                    264:                        kill(cpid,SIGINT);
                    265:                        break;
                    266:                case SM_control_kill:
                    267:                        kill(cpid,SIGKILL);
                    268:                        break;
                    269:                case SM_control_reset_connection:
                    270:                        MKUSINT(t,0);
                    271:                        ox_send_data(sindex,t);
                    272:                        kill(cpid,SIGUSR1);
                    273:                        break;
                    274:                default:
                    275:                        break;
                    276:        }
                    277: }
                    278: #endif
                    279:
                    280: static int ox_spawn(prog,bs,dname)
                    281: char *prog;
                    282: int bs;
                    283: char *dname;
                    284: {
                    285: #if defined(VISUAL)
                    286:        char *av[BUFSIZ];
                    287:        char sock_id[BUFSIZ],ox_intr[BUFSIZ],ox_reset[BUFSIZ],ox_kill[BUFSIZ];
                    288:        char AsirExe[BUFSIZ];
                    289:        HANDLE hProc;
                    290:        STRING rootdir;
                    291:        int mypid;
1.7       noro      292:        int newbs;
1.1       noro      293:
1.7       noro      294:        DuplicateHandle(GetCurrentProcess(),(HANDLE)bs,
                    295:                                        GetCurrentProcess(),(HANDLE *)&newbs,
                    296:                                        0,TRUE,DUPLICATE_SAME_ACCESS);
                    297:        close(bs);
                    298:        bs = newbs;
                    299:
1.1       noro      300:        mypid = GetCurrentProcessId();
                    301:        sprintf(ox_intr,"ox_intr_%d",mypid);
                    302:        sprintf(ox_reset,"ox_reset_%d",mypid);
                    303:        sprintf(ox_kill,"ox_kill_%d",mypid);
                    304:        hIntr = CreateEvent(NULL,TRUE,FALSE,ox_intr);
                    305:        hReset = CreateEvent(NULL,TRUE,FALSE,ox_reset);
                    306:        hKill = CreateEvent(NULL,TRUE,FALSE,ox_kill);
                    307:        sprintf(sock_id,"%d",bs);
                    308:        av[0] = prog;
                    309:        av[1] = sock_id;
1.7       noro      310:        av[2] = dname; /* if dname == "1" then a message window is opened */
                    311:        av[3] = ox_intr;
                    312:        av[4] = ox_reset;
                    313:        av[5] = ox_kill;
                    314:        av[6] = NULL;
1.1       noro      315:        Pget_rootdir(&rootdir);
1.9       noro      316:        sprintf(AsirExe,"%s\\bin\\engine.exe",BDY(rootdir));
1.7       noro      317:        _fileinfo = 1;
1.1       noro      318:        hProc = _spawnv(_P_NOWAIT,AsirExe,av);
                    319:        return (int)hProc;
                    320: #else /* VISUAL */
                    321:        int b,s,i;
                    322:        struct hostent *hp;
                    323:        int pid;
                    324:        char wname[BUFSIZ];
                    325:        char buf[BUFSIZ];
1.5       noro      326:        char errcode;
1.1       noro      327:
                    328:        pid = fork();
                    329:        if ( pid )
                    330:                return pid;
                    331:        else {
                    332:                setpgid(0,getpid());
                    333:                if ( dup2(bs,3) != 3 )
                    334:                        exit(1);
                    335:                if ( dup2(bs,4) != 4 )
                    336:                        exit(1);
                    337:                {
                    338: #if defined(linux) || defined(__NeXT__) || defined(ultrix)
                    339: #include <sys/param.h>
                    340:                        close(0);
                    341:                        for ( i = 5; i < NOFILE; i++ )
                    342:                                close(i);
                    343: #else
                    344: #include <sys/resource.h>
                    345:                        struct rlimit rl;
                    346:
                    347:                        getrlimit(RLIMIT_NOFILE,&rl);
                    348:                        close(0);
                    349:                        for ( i = 5; i < rl.rlim_cur; i++ )
                    350:                                close(i);
                    351: #endif
                    352:                }
1.6       noro      353:                if ( !strcmp(dname,"1" ) ) /* XXX: for ssh */
                    354:                        execl(prog,prog,0);
                    355:                else if ( !strcmp(dname,"0") ) {
1.1       noro      356:                        FILE *null;
                    357:
                    358:                        null = fopen("/dev/null","wb");
                    359:                        dup2(fileno(null),1);
                    360:                        dup2(fileno(null),2);
                    361:                        putenv("DISPLAY=");
                    362:                        execl(prog,prog,0);
1.6       noro      363:                } else
                    364:                        execl(prog,prog,"-display",dname,0);
1.5       noro      365:                /* On failure */
                    366:                errcode = 2;
                    367:                write(4,&errcode,1);
                    368:                exit(1);
1.1       noro      369:        }
                    370: #endif
                    371: }
                    372:
                    373: static void launch_error(s)
                    374: char *s;
                    375: {
                    376:        exit(0);
                    377: }
                    378:
                    379: static int ox_io_init(sock)
                    380: int sock;
                    381: {
                    382:        endian_init();
                    383:        /* server mode */
                    384:        sindex = get_iofp(sock,0,1);
                    385:        asir_OperandStack = (Obj *)CALLOC(BUFSIZ,sizeof(Obj));
                    386:        asir_OperandStackPtr = -1;
                    387: }
                    388:
                    389: static void push_one(obj)
                    390: Obj obj;
                    391: {
                    392:        if ( !obj || OID(obj) != O_VOID )
                    393:                asir_OperandStack[++asir_OperandStackPtr] = obj;
                    394: }
                    395:
                    396: static Obj pop_one() {
                    397:        if ( asir_OperandStackPtr >= 0 ) {
                    398:                return asir_OperandStack[asir_OperandStackPtr--];
                    399:        }
                    400: }
                    401:

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