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

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

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