[Mit-cadr-cvs] r201 - trunk/emulator/chaos

bparker at common-lisp.net bparker at common-lisp.net
Tue Sep 20 11:06:09 UTC 2011


Author: bparker
Date: Tue Sep 20 04:06:08 2011
New Revision: 201

Log:
rjs fixes from 8/22/2011

Added:
   trunk/emulator/chaos/MINI.c   (contents, props changed)
Modified:
   trunk/emulator/chaos/FILE.c
   trunk/emulator/chaos/FILE.h
   trunk/emulator/chaos/Makefile
   trunk/emulator/chaos/chaos.c
   trunk/emulator/chaos/glob.c
   trunk/emulator/chaos/server.c

Modified: trunk/emulator/chaos/FILE.c
==============================================================================
--- trunk/emulator/chaos/FILE.c	Mon Sep 19 18:36:54 2011	(r200)
+++ trunk/emulator/chaos/FILE.c	Tue Sep 20 04:06:08 2011	(r201)
@@ -79,6 +79,11 @@
 /* use utimes instead of "outmoded" utime */
 #endif
 
+#ifdef __NetBSD__
+#include <utime.h>
+#include <sys/statvfs.h>
+#endif
+
 #define LOG_INFO	0
 #define LOG_ERR		1
 #define LOG_NOTICE      2
@@ -702,7 +707,7 @@
 
 	if ((ufd = open(FILEUTMP, 1)) >= 0) {
 		(void)lseek(ufd,
-			    (long)(mylogin.cl_cnum * sizeof(struct chlogin)),
+			    (off_t)(mylogin.cl_cnum * sizeof(struct chlogin)),
 			    0);
 		(void)write(ufd, (char *)&mylogin, sizeof(mylogin));
 		(void)close(ufd);
@@ -1516,7 +1521,7 @@
 		strncpy(mylogin.cl_user, p->pw_name, sizeof(mylogin.cl_user));
 		if ((ufd = open(FILEUTMP, 1)) >= 0) {
 			(void)lseek(ufd,
-			  (long)(mylogin.cl_cnum * sizeof(struct chlogin)),
+			  (off_t)(mylogin.cl_cnum * sizeof(struct chlogin)),
 			    0);
 			(void)write(ufd, (char *)&mylogin, sizeof(mylogin));
 			(void)close(ufd);
@@ -2510,7 +2515,7 @@
 			
 			log(LOG_INFO, "xclose (3b)\n");
 			
-#ifdef OSX
+#if defined(OSX) || defined(BSD42)
 			struct timeval timep[2];
 
 			timep[0].tv_sec = (x->x_options&O_PRESERVE ||
@@ -2539,7 +2544,7 @@
 			 
 			log(LOG_INFO, "xclose (3c)\n");
 
-#ifdef OSX
+#if defined(OSX) || defined(BSD42)
 			if (utimes(x->x_realname, timep)) {
 			   log(LOG_INFO, "error from utimes: errno = %d %s\n", errno, strerror(errno));
 			}
@@ -2626,9 +2631,9 @@
 		errstring = "Incorrect transfer state for FILEPOS";
 		error(t, x->x_fh->f_name, BUG);
 	} else {
-		long new = t->t_args->a_numbers[0];
-		long old = tell(x->x_fd);
-		long size = lseek(x->x_fd, 0L, 2);
+		off_t new = t->t_args->a_numbers[0];
+		off_t old = tell(x->x_fd);
+		off_t size = lseek(x->x_fd, 0L, 2);
 
 		/* Adjust for bytesize */
 		new *= (x->x_bytesize == 16 ? 2 : 1);
@@ -3506,7 +3511,11 @@
 	long	used;
 	int	fd, len;
 	struct stat mstbuf;
+#ifdef __NetBSD__
+	struct statvfs sblock;
+#else
 	struct statfs sblock;
+#endif
 	struct mtab {
 		char path[32];
 		char spec[32];
@@ -3529,8 +3538,13 @@
 	if(len != sizeof(mtab))
 		return 0;
 
+#ifdef __NetBSD__
+	if (statvfs(mtab.path, &sblock) == -1)
+		return 0;
+#else
 	if (statfs(dev, &sblock))
 		return 0;
+#endif
 	total = sblock.f_bsize * (sblock.f_blocks - sblock.f_bfree);
 	free = sblock.f_bsize * sblock.f_bfree;
 	used = total - free;
@@ -4209,7 +4223,7 @@
 		}
 		while (x->x_room != 0) {
 			if (x->x_left == 0) {
-				long pos = tell(x->x_fd);
+				off_t pos = tell(x->x_fd);
 				register int n;
 
 				if (log_verbose) {

Modified: trunk/emulator/chaos/FILE.h
==============================================================================
--- trunk/emulator/chaos/FILE.h	Mon Sep 19 18:36:54 2011	(r200)
+++ trunk/emulator/chaos/FILE.h	Tue Sep 20 04:06:08 2011	(r201)
@@ -159,3 +159,7 @@
 #if defined(__APPLE__) && defined(__MACH__)
 #define OSX
 #endif
+
+#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
+#define BSD42
+#endif

Added: trunk/emulator/chaos/MINI.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/emulator/chaos/MINI.c	Tue Sep 20 04:06:08 2011	(r201)
@@ -0,0 +1,182 @@
+/*
+ * chaosnet MINI protocol server
+ *
+ * Reverse engineered from MIT Lispm code
+ *
+ * 8/2010 rjs at fdy2.demon.co.uk
+ */
+
+#include <sys/param.h>
+
+#include <stdio.h>
+#include <fcntl.h>
+
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+
+#define _XOPEN_SOURCE
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <errno.h>
+
+#include "chaos.h"
+
+struct chstatus chst;		/* Status buffer for connections */
+
+#define LOG_INFO	0
+#define LOG_ERR		1
+#define LOG_NOTICE      2
+
+int log_verbose = 0;
+int log_stderr_tofile = 1;
+
+void
+log(int level, char *fmt, ...)
+{
+	char string[512];
+	va_list ap;
+
+	if (log_stderr_tofile == 0) return;
+
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+
+	fflush(stderr);
+}
+
+/*
+ * This server gets called from the general RFC server, who gives us control
+ * with standard input and standard output set to the control connection
+ * and with the connection open (already accepted)
+ */
+main(argc, argv)
+int argc;
+char **argv;
+{
+	int length, fd, binary, ret, ret1;
+	unsigned char *cp;
+	unsigned char tbuf[20];
+	struct chpacket p, pout;
+	struct stat sbuf;
+	struct tm *ptm;
+	static char ebuf[BUFSIZ];
+
+	if (log_stderr_tofile) {
+		/* stderr */
+		char logfilename[256];
+		(void)close(2);
+#if 0
+		unlink("MINI.log");
+		strcpy(logfilename, "MINI.log");
+#else
+		sprintf(logfilename, "MINI.%d.log", getpid());
+#endif
+		(void)open(logfilename, O_WRONLY | O_CREAT, 0666);
+		(void)lseek(2, 0L, 2);
+		setbuf(stderr, ebuf);
+
+		fprintf(stderr,"MINI!\n"); fflush(stderr);
+	} else {
+		(void)close(2);
+		(void)open("/dev/null", O_WRONLY);
+	}
+
+	chsetmode(0, CHRECORD);
+	chstatus(0, &chst);
+
+	log(LOG_INFO, "MINI: %s\n", argv[1]);
+	binary = 0;
+
+	for (;;) {
+
+		length = read(0, (char *)&p, sizeof(p));
+		if (length <= 0) {
+			log(LOG_INFO, "MINI: Ctl connection broken(%d,%d)\n",
+				length, errno);
+			exit(0);
+		}
+
+		switch (p.cp_op) {
+		case 0200:
+		case 0201:
+			((char *)&p)[length] = '\0';
+			log(LOG_INFO, "MINI: op %o %s\n", p.cp_op, p.cp_data);
+			if ((fd = open(p.cp_data, O_RDONLY)) < 0) {
+				pout.cp_op = 0203;
+				log(LOG_ERR, "MINI: open failed %s\n",
+				    strerror(errno));
+			} else {
+				pout.cp_op = 0202;
+				fstat(fd, &sbuf);
+				ptm = localtime(&sbuf.st_mtime);
+				strftime(tbuf, sizeof(tbuf), "%D %T", ptm);
+				length = sprintf(pout.cp_data, "%s%c%s",
+						 p.cp_data, 0215, tbuf);
+				write(1, (char *)&pout, length + 1);
+				binary = p.cp_op & 1;
+			}
+
+			do {
+				pout.cp_op = (binary) ? DWDOP : DATOP;
+				length = read(fd, pout.cp_data, CHMAXDATA);
+				/*log(LOG_INFO, "MINI: read %d\n", length);*/
+				if (length == 0) break;
+				if (binary == 0)
+				  to_lispm(pout.cp_data, length);
+
+				while (write(1, (char *)&pout, length + 1) < 0)
+					usleep(10000);
+			} while (length > 0);
+
+			log(LOG_INFO, "MINI: before eof\n");
+			pout.cp_op = EOFOP;
+			while (write(1, (char *)&pout, 1) < 0)
+				usleep(10000);
+			close(fd);
+			break;
+		default:
+			log(LOG_INFO, "MINI: op %o\n", p.cp_op);
+			break;
+		}
+	}
+}
+
+
+/*
+ * Character set conversion routines.
+ */
+to_lispm(data, length)
+unsigned char *data;
+int length;
+{
+	register int c, i;
+
+	for (i = 0; i < length; i++) {
+		c = data[i] & 0377;
+		switch (c) {
+		case 0210:	/* Map SAIL symbols back */
+		case 0211:
+		case 0212:
+		case 0213:
+		case 0214:
+		case 0215:
+		case 0377:	/* Give back infinity */
+			c &= 0177;
+			break;
+		case '\n':	/* Map canonical newline to lispm */
+			c = CHNL;
+			break;
+		case 015:	/* Reverse linefeed map kludge */
+			c = 0212;
+		case 010:	/* Map the format effectors back */
+		case 011:
+		case 013:
+		case 014:
+		case 0177:
+			c |= 0200;
+		}
+		data[i] = c;
+	}
+}

Modified: trunk/emulator/chaos/Makefile
==============================================================================
--- trunk/emulator/chaos/Makefile	Mon Sep 19 18:36:54 2011	(r200)
+++ trunk/emulator/chaos/Makefile	Tue Sep 20 04:06:08 2011	(r201)
@@ -10,10 +10,17 @@
 
 ifeq ($(OS_NAME), Darwin)
 OS = OSX
+LIBS = 
 endif
 
 ifeq ($(OS_NAME), Linux)
 OS = LINUX
+LIBS =
+endif
+
+ifeq ($(OS_NAME), NetBSD)
+OS = BSD
+LIBS= -lcompat -lcrypt
 endif
 
 #----------- code ------------
@@ -22,7 +29,7 @@
 
 SERVER_OBJ = server.o chaos.o ncp.o rfc.o testpackets.o log.o signal.o
 
-CFLAGS = -g -fno-builtin-log
+CFLAGS = -O2 -fno-builtin-log
 
 # if 64 bit
 ifeq ($(MACH_NAME), x86_64)
@@ -30,7 +37,7 @@
 endif
 
 
-all: chaosd listen server client time FILE
+all: chaosd listen server client time FILE MINI
 
 chaosd: $(CHAOSD_OBJ)
 	$(CC) $(CFLAGS) -o chaosd $(CHAOSD_OBJ)
@@ -48,8 +55,11 @@
 	$(CC) $(CFLAGS) -o time time.c
 
 FILE: FILE.c FILE.h glob.c chlib.c
-	$(CC) $(CFLAGS) -o FILE FILE.c glob.c chlib.c
+	$(CC) $(CFLAGS) -o FILE FILE.c glob.c chlib.c $(LIBS)
+
+MINI: MINI.c chlib.c
+	$(CC) $(CFLAGS) -o MINI MINI.c chlib.c
 
 clean:
-	rm -f *.o chaosd listen server FILE time client
+	rm -f *.o chaosd listen server FILE MINI time client
 

Modified: trunk/emulator/chaos/chaos.c
==============================================================================
--- trunk/emulator/chaos/chaos.c	Mon Sep 19 18:36:54 2011	(r200)
+++ trunk/emulator/chaos/chaos.c	Tue Sep 20 04:06:08 2011	(r201)
@@ -12,6 +12,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <time.h>
 
 #include <sys/types.h>
 #include <sys/uio.h>
@@ -830,6 +831,17 @@
 		start_file(conn, pkt->pk_cdata, PH_LEN(pkt->pk_phead));
 		return;
 	}
+	if (concmp(pkt, "MINI", 4)) {
+		/* this is a hack to fake what chserver.c would do */
+		struct packet *p = ch_alloc_pkt(10);
+		strcpy(p->pk_cdata, "MINI");
+		conn = ch_listen(p, 0);
+		lsnmatch(pkt, conn);
+		ch_accept(conn);
+
+		start_mini(conn, pkt->pk_cdata, PH_LEN(pkt->pk_phead));
+		return;
+	}
 #endif
 	if (chaos_rfcrcv == 0) {
 		if (pkt->pk_op != BRDOP)

Modified: trunk/emulator/chaos/glob.c
==============================================================================
--- trunk/emulator/chaos/glob.c	Mon Sep 19 18:36:54 2011	(r200)
+++ trunk/emulator/chaos/glob.c	Tue Sep 20 04:06:08 2011	(r201)
@@ -208,7 +208,7 @@
 
 	dirp = opendir(gpath);
 	if (dirp != NULL)
-#if defined(linux) || defined(OSX)
+#if defined(linux) || defined(OSX) || defined(BSD42)
 		dirf = dirfd(dirp);
 #else
 		dirf = ((struct DIR *)dirp)->dd_fd;

Modified: trunk/emulator/chaos/server.c
==============================================================================
--- trunk/emulator/chaos/server.c	Mon Sep 19 18:36:54 2011	(r200)
+++ trunk/emulator/chaos/server.c	Tue Sep 20 04:06:08 2011	(r201)
@@ -291,15 +291,13 @@
 int child_conn_count;
 
 void
-fork_file(char *arg)
+fork_server(char *app_name, char *arg)
 {
     int ret, r, i;
     int svdo[2], svdi[2], svc[2], svs[2];
     int tmp[2];
 
-#define app_name "./FILE"
-
-    tracef(TRACE_MED, "fork_file('%s')\n", arg);
+    tracef(TRACE_MED, "fork_server('%s':'%s')\n", app_name, arg);
 
     ret = socketpair(AF_UNIX, SOCK_DGRAM, 0, tmp);
 
@@ -345,7 +343,7 @@
             child_fd_ctl = svc[0];
             child_fd_sctl = svs[0];
             debugf(DBG_LOW,
-                   "fork_file() pid %d, fd_o %d, fd_i %d, "
+                   "fork_server() pid %d, fd_o %d, fd_i %d, "
                    "fd_ctl %d, fd_sctl %d\n",
                    child_pid,
                    child_conn[0].fd_out, child_conn[0].fd_in,
@@ -658,9 +656,33 @@
         parg = &args[i+1];
     }
 
-    fork_file(parg);
+    fork_server("./FILE", parg);
 }
 
+void
+start_mini(void *conn, char *args, int arglen)
+{
+    int i;
+    char *parg;
+
+    child_conn[0].conn = conn;
+    child_conn[0].fd_out = -1;
+    child_conn[0].fd_in = -1;
+    child_conn_count = 1;
+
+    /* skip over RFC name */
+    for (i = 0; i < arglen; i++) {
+        if (args[i] == ' ')
+            break;
+    }
+    parg = 0;
+    if (args[i] == ' ') {
+        args[arglen] = 0;
+        parg = &args[i+1];
+    }
+
+    fork_server("./MINI", parg);
+}
 
 int
 read_chaos(void)




More information about the mit-cadr-cvs mailing list