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