Annotation of OpenXM/src/kxx/oxd.c, Revision 1.1
1.1 ! takayama 1: /*
! 2: $OpenXM$
! 3: */
! 4:
! 5: #include <stdio.h>
! 6: #include <stdlib.h>
! 7: #include <fcntl.h>
! 8: #include <unistd.h>
! 9: #include <sys/types.h>
! 10: #include <sys/socket.h>
! 11: #include <sys/time.h>
! 12: #include <netinet/in.h>
! 13: #include <netdb.h>
! 14: #include <signal.h>
! 15: #include <setjmp.h>
! 16: /* -lnsl -lsocket /usr/ucblib/libucb.a */
! 17: #include "ox_kan.h"
! 18: #include "serversm.h"
! 19:
! 20: char *getTag(char *s);
! 21: char *getKeyValue(char *s,char *key);
! 22: char *getBody(char *s);
! 23:
! 24: int MyServerPid;
! 25: #define SERVERNAME_SIZE 4096
! 26: int Quiet = 0;
! 27: int Serial = 0;
! 28:
! 29: int LocalMode = 1;
! 30: static int findOxServer(char *server);
! 31: static void couldNotFind(char *s);
! 32: #if defined(__CYGWIN__)
! 33: int errno;
! 34: #endif
! 35:
! 36: main(int argc, char *argv[]) {
! 37: char sname[1024];
! 38: int i;
! 39: int fdControl = -1; int portControl = 8089;
! 40: extern int OpenedSocket;
! 41: extern int Serial;
! 42: int sleepingTime = 0;
! 43: int result;
! 44: int fd;
! 45:
! 46: strcpy(sname,"localhost");
! 47: i = 1;
! 48: while (i<argc) {
! 49: if (strcmp(argv[i],"--control")==0) {
! 50: i++;
! 51: if (i<argc) sscanf(argv[i],"%d",&portControl);
! 52: }else if (strcmp(argv[i],"--insecure") == 0) {
! 53: LocalMode = 0;
! 54: }else if (strcmp(argv[i],"--wait") == 0) {
! 55: i++;
! 56: if (i<argc) {
! 57: sscanf(argv[i],"%d",&sleepingTime);
! 58: }
! 59: }else {
! 60: fprintf(stderr,"Unknown option %s.\n",argv[i]);
! 61: oxdUsage(); exit(10);
! 62: }
! 63: i++;
! 64: }
! 65:
! 66: if (sleepingTime) {
! 67: fprintf(stderr,"Waiting to connect for %d seconds...\n",sleepingTime);
! 68: sleep(sleepingTime);
! 69: fprintf(stderr,"\nTrying to connect\n");
! 70: }
! 71:
! 72:
! 73: if (LocalMode) {
! 74: if (portControl != -1) {
! 75: fdControl = socketOpen(sname,portControl);
! 76: if (fdControl < 0) oxdError("Could not open a socket.");
! 77: portControl = OpenedSocket;
! 78: while (1) {
! 79: /* fdControl : fd for the wait queue */
! 80: /* fd : accepted fd */
! 81: fprintf(stderr,"Waiting a connection... Serial=%d\n",Serial);
! 82: fflush(NULL);
! 83: fd = socketAcceptLocal2(fdControl);
! 84: if (fd < 0) oxdError("Failed to accept.");
! 85: fprintf(stderr,"\nConnected.\n");
! 86: Serial++;
! 87: MyServerPid = fork();
! 88: if (MyServerPid > 0 ) {
! 89: /* parent */
! 90: close(fd);
! 91: } else {
! 92: result = childServerMain(fd);
! 93: exit(10);
! 94: }
! 95: }
! 96: }
! 97: }else{
! 98: oxdError("Non-localmode is not supported.");
! 99: }
! 100: }
! 101:
! 102:
! 103: oxdUsage() {
! 104: fprintf(stderr,"Usage: \n");
! 105: fprintf(stderr," oxd \n");
! 106: fprintf(stderr,"\n");
! 107: }
! 108:
! 109: void exitServer(int n) {
! 110: extern Serial;
! 111: fprintf(stderr,"\nSerial=%d, Timeout. Exiting.\n",Serial);
! 112: exit(0);
! 113: }
! 114: void exitServer2(FILE *fp,char *s) {
! 115: extern Serial;
! 116: fprintf(stderr,"\nSerial=%d: %s\n",Serial,s);
! 117: fprintf(fp,"Error: %s\n",s);
! 118: fprintf(fp,"Close connection.\n",s);
! 119: fflush(NULL);
! 120: exit(0);
! 121: }
! 122: childServerMain(int fd) {
! 123: FILE *fp;
! 124: #define SIZE 1024
! 125: char comm[SIZE+2];
! 126: char *status;
! 127: int id;
! 128: char *tag;
! 129: char *key;
! 130: char *home;
! 131: char *body;
! 132: char fname[SIZE*2];
! 133: char fnameBody[SIZE];
! 134: char ccc[SIZE*3];
! 135: /* Starting oxd session */
! 136: signal(SIGALRM,exitServer);
! 137: alarm(60);
! 138: fp = fdopen(fd,"w+");
! 139: if (fp == NULL) oxdError("failed fdopen\n");
! 140:
! 141: #define GET_COMMAND {\
! 142: fprintf(fp,"?"); fflush(fp); \
! 143: status = fgets(comm,SIZE,fp); \
! 144: if (status == NULL) { \
! 145: fprintf(stderr,"End of Input.\n"); \
! 146: exit(0); \
! 147: } \
! 148: /* fprintf(fp,"%s\n",comm); fflush(fp);*/ /* Just echo for debugging. */ \
! 149: fprintf(stderr,"Serial=%d: command=%s\n",Serial,comm); }
! 150:
! 151: /* Login */
! 152: GET_COMMAND ;
! 153: tag = getTag(comm);
! 154: if (tag == NULL) exitServer2(fp,"expecting <login method=\"file\"/>");
! 155: if (strcmp(tag,"login") != 0) exitServer2(fp,"expecting <login method=\"file\"/>");
! 156: key =getKeyValue(comm,"method");
! 157: if (key == NULL || strcmp(key,"file") != 0) exitServer2(fp,"expecting <login method=\"file\"/>");
! 158:
! 159: /* Out put a challenge. */
! 160: home = getenv("HOME");
! 161: if (home == NULL) oxdError("Set the HOME environmental variable.\n");
! 162: id = Serial*1000+getpid();
! 163: sprintf(fnameBody,".oxd%d",id);
! 164: sprintf(fname,"%s/%s",home,fnameBody);
! 165: fprintf(fp,"<challenge file=\"%s\"/>\n",fname); fflush(fp);
! 166:
! 167: /* Wait <done/> */
! 168: GET_COMMAND ;
! 169: tag = getTag(comm);
! 170: if ((tag == NULL) || (strcmp(tag,"done") != 0)) exitServer2(fp,"expecting <done>");
! 171: if (access(fname,R_OK) == 0) {
! 172: sprintf(ccc,"rm -f %s",fname);
! 173: system(ccc);
! 174: }else{
! 175: exitServer2(fp,"Challenge file does not exist.");
! 176: }
! 177:
! 178: /* Expect <launch> */
! 179: GET_COMMAND ;
! 180: tag = getTag(comm);
! 181: if ((tag == NULL) || (strcmp(tag,"launch")!=0))
! 182: exitServer2(fp,"expecting <launch> ox -ox ox_asir -reverse -data dport -control cport </launch>");
! 183:
! 184: body = getBody(comm);
! 185: if (strlen(body) > SIZE*2) exitServer2(fp,"too big body.");
! 186: sprintf(ccc,"/usr/local/bin/openxm %s",body);
! 187: fprintf(stderr,"Serial=%d : Executing command=%s\n",ccc);
! 188: fprintf(fp,"<bye/>\n"); fflush(NULL);
! 189: fclose(fp); /* close the connection */
! 190: system(ccc);
! 191: fprintf(stderr,"Serial=%d : The following command is finished : %s\n",ccc);
! 192: }
! 193:
! 194:
! 195: /* These are dummy. It is defined in stackmachine.c */
! 196: unlockCtrlCForOx() { ; }
! 197: restoreLockCtrlCForOx() { ; }
! 198:
! 199: oxdError(char *s) {
! 200: fprintf(stderr,"%s\n",s);
! 201: exit(10);
! 202: }
! 203:
! 204:
! 205:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>