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