socsvn commit: r254850 - in soc2013/ambarisha/head/usr.bin: dmget dms

ambarisha at FreeBSD.org ambarisha at FreeBSD.org
Tue Jul 16 19:01:04 UTC 2013


Author: ambarisha
Date: Tue Jul 16 19:01:03 2013
New Revision: 254850
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=254850

Log:
  Improves signal handling in dms, dmget
  Adds status updates
  

Modified:
  soc2013/ambarisha/head/usr.bin/dmget/dm.h
  soc2013/ambarisha/head/usr.bin/dmget/dmget.c
  soc2013/ambarisha/head/usr.bin/dmget/dmget.h
  soc2013/ambarisha/head/usr.bin/dmget/fetch.c
  soc2013/ambarisha/head/usr.bin/dmget/utils.c
  soc2013/ambarisha/head/usr.bin/dms/dm.h
  soc2013/ambarisha/head/usr.bin/dms/dms.c
  soc2013/ambarisha/head/usr.bin/dms/dms.h
  soc2013/ambarisha/head/usr.bin/dms/utils.c
  soc2013/ambarisha/head/usr.bin/dms/worker.c

Modified: soc2013/ambarisha/head/usr.bin/dmget/dm.h
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dmget/dm.h	Tue Jul 16 18:02:44 2013	(r254849)
+++ soc2013/ambarisha/head/usr.bin/dmget/dm.h	Tue Jul 16 19:01:03 2013	(r254850)
@@ -11,6 +11,9 @@
 #define DMS_UDS_PATH	"/tmp/dms.uds"
 
 struct dmres {
+	int	 status;
+	int	 errcode;
+	char	*errstr;
 };
 
 struct dmreq {
@@ -36,24 +39,35 @@
 #define		p_FLAG		(1 << 10)
 #define		r_FLAG		(1 << 11)
 #define		s_FLAG		(1 << 12)
+#define		V_TTY		(1 << 13)
 
 	char	*i_filename;
 	char	*URL;
 	char	*path;
 };
 
-struct msg {
+struct dmmsg {
 	char 	 op;
 	int 	 len;
 	char 	*buf;
 };
 
+struct xferstat {
+	char		 name[64];
+	struct timeval	 start;		/* start of transfer */
+	struct timeval	 last;		/* time of last update */
+	struct timeval	 last2;		/* time of previous last update */
+	off_t		 size;		/* size of file per HTTP hdr */
+	off_t		 offset;	/* starting offset in file */
+	off_t		 rcvd;		/* bytes already received */
+	off_t		 lastrcvd;	/* bytes received since last update */
+};
+
 #define		DMREQ			1
 #define		DMRESP			2
-#define		DMSTATREQ		3
-#define		DMSTATRESP		4
-#define		DMAUTHREQ		5
-#define		DMAUTHRESP		6
-#define		DMSIG			7
+#define		DMAUTHREQ		3
+#define		DMAUTHRESP		4
+#define		DMSIG			5
+#define		DMSTAT			6
 
 #endif /* _DMCLIENT_H */

Modified: soc2013/ambarisha/head/usr.bin/dmget/dmget.c
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dmget/dmget.c	Tue Jul 16 18:02:44 2013	(r254849)
+++ soc2013/ambarisha/head/usr.bin/dmget/dmget.c	Tue Jul 16 19:01:03 2013	(r254850)
@@ -1,25 +1,31 @@
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <sys/un.h>
+#include <sys/stat.h>
 
 #include <unistd.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include <fcntl.h>
 
 
 #include "dm.h"
 #include "dmget.h"
 
 auth_t		 dmAuthMethod;
+stat_display_t	 dmStatDisplayMethod;
 int		 dmTimeout;
 int		 dmRestartCalls;
 int		 dmDebug;
 int		 dmLastErrCode;
 char		 dmLastErrString[MAXERRSTRING];
 
-static int dms;
-static int sigint;
+static int 	 sigint;
+static int 	 siginfo;
+
+static int 	 dmg_error;
+static char	*dmg_errstr;
 
 static void *
 Malloc(size_t size)
@@ -31,11 +37,70 @@
 	return ptr;
 }
 
+void dm_sighandler(int signal)
+{
+	switch(signal) {
+	case SIGINT:
+		sigint = 1;
+		break;
+	case SIGINFO:
+		siginfo = 1;
+		break;
+	}
+}
+
+static int
+sigsafe_write(int sock, char *buf, int bufsize)
+{
+	int ret;
+	sigset_t sm;
+	sigemptyset(&sm);
+	sigaddset(&sm, SIGINT);
+	sigaddset(&sm, SIGINFO);
+
+	sigprocmask(SIG_BLOCK, &sm, NULL);
+	ret = Write(sock, buf, bufsize);
+	sigprocmask(SIG_UNBLOCK, &sm, NULL);
+
+	return ret;
+}
+
+static int
+sigsafe_read(int sock, char *buf, int bufsize)
+{
+	int ret, n = 0;
+
+	/* If the first read was an error return 
+	 * because that could be because of a signal
+	 * */
+	ret = Read(sock, buf, bufsize);
+	if (ret == -1 || ret == 0) 
+		return ret;
+		
+	/* But if we've already started reading, we keep reading */
+	while ((ret == -1 && errno == EINTR) || n > 0 && n < bufsize) {
+		ret = read(sock, buf + n, bufsize - n);	
+		if (ret == 0) {
+			/* Read ended prematurely
+			 * Set dmg_error appropriately and return
+			 */
+
+			break;
+		}
+		
+		if (ret != -1)
+			n += ret;
+	}
+
+	if (ret != -1)
+		return(n);
+	return(ret);
+}
+
 static int
 mk_reqbuf(struct dmreq dmreq, char **reqbuf, char op)
 {
-	int bufsize = 0;
-	printf("mk_reqbuf() : Starting\n");
+	int bufsize = 0, i = 0;
 
 	bufsize += sizeof(bufsize); 				// Buffer size
 	bufsize += 1; 						// Opcode
@@ -46,9 +111,7 @@
 
 	*reqbuf = (char *) Malloc(bufsize);
 	
-	int i = 0;
-	
-	memcpy(*reqbuf, &(bufsize), sizeof(bufsize));
+	memcpy(*reqbuf, &bufsize, sizeof(bufsize));
 	i += sizeof(bufsize);
 	
 	*(*reqbuf+i) = op;
@@ -87,99 +150,219 @@
 	strcpy(*reqbuf + i, dmreq.path);
 	i += strlen(dmreq.path) + 1;
 	
-	return bufsize;
+	return(i);
 }
 
-static int
-send_request(int sock, struct dmreq dmreq)
+static struct dmres *
+mk_dmres(char *buf, int buflen)
 {
-	char *reqbuf;
-	int bufsize = mk_reqbuf(dmreq, &reqbuf, DMREQ);
-	int err = Write(sock, reqbuf, bufsize);
+	int i = 0, len;
+	struct dmres *dmres;
 
-	free(reqbuf);
-	return (err);
+	dmres = (struct dmres*) Malloc(sizeof(struct dmres));
+
+	memcpy(&(dmres->status), buf + i, sizeof(dmres->status));
+	i += sizeof(dmres->status);
+
+	memcpy(&(dmres->errcode), buf + i, sizeof(dmres->errcode));
+	i += sizeof(dmres->errcode);
+
+	len = strlen(buf + i);
+	dmres->errstr = (char *) Malloc(len);
+	strcpy(dmres->errstr, buf + i);
+
+	return dmres;
+}
+
+static void
+rm_dmres(struct dmres **dmres)
+{
+	free((*dmres)->errstr);
+	free(*dmres);
+	*dmres = NULL;
 }
 
 static int
-keep_reading(int sock, char *buf, int size)
+send_signal(int sock)
 {
-	int err = read(sock, buf, size);
-	while (err == -1 && errno == EINTR && sigint == 0) {
-		err = read(sock, buf, size);
-	}
+	struct dmmsg msg;
+	msg.op = DMSIG;
+	msg.buf = &signal;
+	msg.len = sizeof(signal);
+	return (send_msg(sock, msg));
+}
 
-	if (err == -1)
-		perror("read():");
+static int
+write_fd(int sock, int fd)
+{
+	int ret;
+	char c;
+	struct msghdr msg;
+	struct iovec iov;
+
+	char control[CMSG_SPACE(sizeof(int))];
+	struct cmsghdr *cmptr;
+
+	c = 0;
+	iov.iov_base = &c;
+	iov.iov_len = sizeof(c);
+
+	msg.msg_control = (caddr_t) control;
+	msg.msg_controllen = CMSG_LEN(sizeof(int));
+	msg.msg_name = NULL;
+	msg.msg_namelen = 0;
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+
+	cmptr = CMSG_FIRSTHDR(&msg);
+	cmptr->cmsg_len = CMSG_LEN(sizeof(int));
+	cmptr->cmsg_level = SOL_SOCKET;
+	cmptr->cmsg_type = SCM_RIGHTS;
+	*((int *) CMSG_DATA(cmptr)) = fd;
+
+	ret = sendmsg(sock, &msg, 0);
+	if (ret == -1)
+		return (-1);
+	else
+		return (0);
+}
 
-	return err;
+static int
+Write_fd(int sock, int fd)
+{
+	int ret = write_fd(sock, fd);
+	if (ret == -1) {
+		perror("Write_fd():");
+	} else {
+		printf("Write_fd(): Success\n");
+	}
 }
 
 static int
-recv_response(int sock, struct dmres *dmres)
+send_request(int sock, struct dmreq dmreq)
 {
-	int bufsize;
-	keep_reading(sock, &bufsize, sizeof(bufsize));
-	bufsize -= sizeof(bufsize);
+	char *reqbuf;
+	int bufsize, err;
 
-	char *buf = (char *) Malloc(bufsize);
-	keep_reading(sock, buf, bufsize);
+	bufsize = mk_reqbuf(dmreq, &reqbuf, DMREQ);
+	err = sigsafe_write(sock, reqbuf, bufsize);
 
-	/* TODO: Check the error code in the response and set the 
-		 dmLastErrCode & dmLastErrString values */
+	int fd = open(dmreq.path, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
+	Write_fd(sock, fd);
+	close(fd);	
+
+	free(reqbuf);
+	return(err);
 }
 
-int
-dm_request(struct dmreq dmreq)
+static void
+free_msg(struct dmmsg **msg)
 {
-	dms = socket(AF_UNIX, SOCK_STREAM, 0);
-	struct sockaddr_un dms_addr;
-	dms_addr.sun_family = AF_UNIX;
-	strncpy(dms_addr.sun_path, DMS_UDS_PATH, sizeof(dms_addr.sun_path));
-	int err = Connect(dms, (struct sockaddr *) &dms_addr, sizeof(dms_addr));
-	
-	send_request(dms, dmreq);
-
-	struct dmres dmres;
-	recv_response(dms, &dmres);	
+	free((*msg)->buf);
+	free(*msg);
+	*msg = NULL;
 }
 
-static int
-send_msg(int socket, struct msg msg)
+static struct dmmsg *
+recv_msg(int sock)
 {
-	int bufsize = sizeof(bufsize);	// Buffer size
-	bufsize += 1; 			// Op
-	bufsize += msg.len;		// Signal number
+	int err;
+	fd_set fds;
+	sigset_t sm;
+	struct dmmsg *msg;
 
-	char *sndbuf = (char *) Malloc(bufsize);
+	msg = (struct dmmsg *) Malloc(sizeof(struct dmmsg));
+
+	FD_ZERO(&fds);
+	FD_SET(sock, &fds);
 	
-	int i = 0;
-	memcpy(sndbuf + i, &bufsize, sizeof(bufsize));	
-	i += sizeof(bufsize);
+	err = Select(sock + 1, &fds, NULL, NULL, NULL);
+	if (err == -1)
+		return(-1);
 
-	*(sndbuf + i) = msg.op;
-	i++;
+	sigemptyset(&sm);
+	sigaddset(&sm, SIGINT);
+	sigaddset(&sm, SIGINFO);
+
+	sigprocmask(SIG_BLOCK, &sm, NULL);
+	err = Peel(sock, msg);
+	sigprocmask(SIG_UNBLOCK, &sm, NULL);
+
+	if (err == -1) {
+		/* Set dmg_err* */
+		free_msg(&msg);
+		return NULL;
+	}
+	
+	return msg;
+}
 
-	memcpy(sndbuf + i, msg.buf, msg.len);
-	i += msg.len;
+int
+dmget(struct dmreq dmreq)
+{
+	int sock, err, ret, force;
+	struct sockaddr_un dms_addr;
+	struct dmres *dmres;
+	struct xferstat xs;
 
-	int nbytes = Write(socket, sndbuf, bufsize);
-	free(sndbuf);
+	sock = Socket(AF_UNIX, SOCK_STREAM, 0);
 
-	return (nbytes);
-}
+	dms_addr.sun_family = AF_UNIX;
+	strncpy(dms_addr.sun_path, DMS_UDS_PATH, sizeof(dms_addr.sun_path));
+	err = Connect(sock, (struct sockaddr *) &dms_addr, sizeof(dms_addr));
 
-void
-dm_sighandler(int signal)
-{
-	struct msg msg;
-	msg.op = DMSIG;
-	msg.buf = &signal;
-	msg.len = sizeof(signal);
-	send_msg(dms, msg);
+	if (siginfo || sigint) 
+		goto signal;
 
-	if (signal == SIGINT) {
-		close(dms);
-		exit(2);
+	send_request(sock, dmreq);
+
+	while (!sigint) {
+		struct dmmsg *msg;
+		msg = recv_msg(sock);				
+		if (msg == NULL) {
+			goto failure;
+		}
+
+		if (sigint || siginfo) {
+			send_signal(sock);
+			goto signal;
+		}
+		
+		switch(msg->op) {
+		case DMRESP:
+			dmres = mk_dmres(msg->buf, msg->len);
+			free_msg(&msg);
+			if (dmres->status == 0){
+				/* set dmLastErr* */
+				rm_dmres(&dmres);
+				goto success;
+			} else {
+				rm_dmres(&dmres);
+				goto failure;
+			}
+		case DMSTAT:
+			force = *((int *)(msg->buf));
+			memcpy(&xs, (msg->buf) + sizeof(force), sizeof(xs));
+			free_msg(&msg);
+			dmStatDisplayMethod(&xs, force);
+			break;
+		case DMAUTHREQ:
+		default:
+			break;
+		}
 	}
+
+signal:
+	ret = -1;
+	goto done;
+failure:
+	ret = 1;
+	goto done;
+success:
+	ret = 0;
+	goto done;
+done:
+	/* Set dmLastErrCode dmLastErrString */
+	close(sock);
+	return (ret);
 }

Modified: soc2013/ambarisha/head/usr.bin/dmget/dmget.h
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dmget/dmget.h	Tue Jul 16 18:02:44 2013	(r254849)
+++ soc2013/ambarisha/head/usr.bin/dmget/dmget.h	Tue Jul 16 19:01:03 2013	(r254850)
@@ -15,6 +15,8 @@
 
 typedef int (*auth_t)(struct url *);
 extern auth_t		 dmAuthMethod;
+typedef void (*stat_display_t) (struct xferstat *, int);
+extern stat_display_t	 dmStatDisplayMethod;
 
 int dm_request(struct dmreq);
 void dm_sighandler(int sig);

Modified: soc2013/ambarisha/head/usr.bin/dmget/fetch.c
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dmget/fetch.c	Tue Jul 16 18:02:44 2013	(r254849)
+++ soc2013/ambarisha/head/usr.bin/dmget/fetch.c	Tue Jul 16 19:01:03 2013	(r254850)
@@ -88,28 +88,12 @@
 static long	 w_secs;	/*    -w: retry delay */
 static int	 family = PF_UNSPEC;	/* -[46]: address family to use */
 
-static int	 sigalrm;	/* SIGALRM received */
-static int	 siginfo;	/* SIGINFO received */
 static int	 sigint;	/* SIGINT received */
 
 static long	 ftp_timeout = TIMEOUT;	/* default timeout for FTP transfers */
 static long	 http_timeout = TIMEOUT;/* default timeout for HTTP transfers */
 static char	*buf;		/* transfer buffer */
 
-struct xferstat {
-	char		 name[64];
-	struct timeval	 start;		/* start of transfer */
-	struct timeval	 last;		/* time of last update */
-	struct timeval	 last2;		/* time of previous last update */
-	off_t		 size;		/* size of file per HTTP hdr */
-	off_t		 offset;	/* starting offset in file */
-	off_t		 rcvd;		/* bytes already received */
-	off_t		 lastrcvd;	/* bytes received since last update */
-};
-
-/*
- * Signal handler
- */
 /*
  * Compute and display ETA
  */
@@ -274,7 +258,6 @@
 	dmreq.B_size = B_size;
 	dmreq.S_size = S_size;
 	dmreq.URL = URL;
-	printf("URL = %s\n", dmreq.URL);
 	dmreq.path = path;
 	dmreq.T_secs = T_secs;
 
@@ -282,21 +265,27 @@
 	else dmreq.i_filename = "";
 
 	dmreq.flags = 0;
-	if (A_flag != 0) dmreq.flags |= A_FLAG;
-	if (F_flag != 0) dmreq.flags |= F_FLAG;
-	if (R_flag != 0) dmreq.flags |= R_FLAG;
-	if (U_flag != 0) dmreq.flags |= U_FLAG;
-	if (d_flag != 0) dmreq.flags |= d_FLAG;
-	if (i_flag != 0) dmreq.flags |= i_FLAG;
-	if (l_flag != 0) dmreq.flags |= l_FLAG;
-	if (m_flag != 0) dmreq.flags |= m_FLAG;
-	if (n_flag != 0) dmreq.flags |= n_FLAG;
-	if (p_flag != 0) dmreq.flags |= p_FLAG;
-	if (r_flag != 0) dmreq.flags |= r_FLAG;
-	if (s_flag != 0) dmreq.flags |= s_FLAG;
-	if (o_stdout != 0) dmreq.flags |= O_STDOUT;
+	if (A_flag) dmreq.flags |= A_FLAG;
+	if (F_flag) dmreq.flags |= F_FLAG;
+	if (R_flag) dmreq.flags |= R_FLAG;
+	if (U_flag) dmreq.flags |= U_FLAG;
+	if (d_flag) dmreq.flags |= d_FLAG;
+	if (i_flag) dmreq.flags |= i_FLAG;
+	if (l_flag) dmreq.flags |= l_FLAG;
+	if (m_flag) dmreq.flags |= m_FLAG;
+	if (n_flag) dmreq.flags |= n_FLAG;
+	if (p_flag) dmreq.flags |= p_FLAG;
+	if (r_flag) dmreq.flags |= r_FLAG;
+	if (s_flag) dmreq.flags |= s_FLAG;
+	if (o_stdout) dmreq.flags |= O_STDOUT;
+	if (v_tty) dmreq.flags |= V_TTY;
+	
+
+	if (dmreq.flags & V_TTY) printf("v_tty is set\n");
+	else printf("v_tty is not set\n");
 
-	return (dm_request(dmreq));
+	dmStatDisplayMethod = stat_display;
+	return (dmget(dmreq));
 }
 
 static void

Modified: soc2013/ambarisha/head/usr.bin/dmget/utils.c
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dmget/utils.c	Tue Jul 16 18:02:44 2013	(r254849)
+++ soc2013/ambarisha/head/usr.bin/dmget/utils.c	Tue Jul 16 19:01:03 2013	(r254850)
@@ -116,20 +116,31 @@
 }
 
 int
-Peel(int sock, struct msg *msg)
+Peel(int sock, struct dmmsg *msg)
 {
 	int bufsize = 0;
-	Read(sock, &bufsize, sizeof(bufsize));
+	int err;
+	err = Read(sock, &bufsize, sizeof(bufsize));
+	if (err == 0)
+		return (-1);
 	bufsize -= sizeof(bufsize);
 	
-	Read(sock, &(msg->op), sizeof(msg->op));
+	err = Read(sock, &(msg->op), sizeof(msg->op));
+	if (err == 0)
+		return (-1);
 	bufsize -= sizeof(msg->op);
 
 	msg->buf = (char *) Malloc(bufsize);
 	msg->len = bufsize;
 
 	Read(sock, msg->buf, bufsize);
-	return 0;
+	if (err == 0) {
+		free(msg->buf);
+		msg->len = 0;
+		return (-1);
+	}
+
+	return 1;
 }
 
 int
@@ -144,3 +155,29 @@
 	}
 	return err;
 }
+
+int
+send_msg(int socket, struct dmmsg msg)
+{
+	int bufsize = sizeof(bufsize);	// Buffer size
+	bufsize += 1; 			// Op
+	bufsize += msg.len;		// Signal number
+
+	char *sndbuf = (char *) Malloc(bufsize);
+	
+	int i = 0;
+	memcpy(sndbuf + i, &bufsize, sizeof(bufsize));	
+	i += sizeof(bufsize);
+
+	*(sndbuf + i) = msg.op;
+	i++;
+
+	memcpy(sndbuf + i, msg.buf, msg.len);
+	i += msg.len;
+
+	int nbytes = Write(socket, sndbuf, bufsize);
+	free(sndbuf);
+
+	return (nbytes);
+}
+

Modified: soc2013/ambarisha/head/usr.bin/dms/dm.h
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dms/dm.h	Tue Jul 16 18:02:44 2013	(r254849)
+++ soc2013/ambarisha/head/usr.bin/dms/dm.h	Tue Jul 16 19:01:03 2013	(r254850)
@@ -1,5 +1,5 @@
-#ifndef _DMCLIENT_H
-#define _DMCLIENT_H
+#ifndef _DM_H
+#define _DM_H
 
 #include <sys/param.h>
 #include <sys/time.h>
@@ -11,6 +11,9 @@
 #define DMS_UDS_PATH	"/tmp/dms.uds"
 
 struct dmres {
+	int	 status;
+	int	 errcode;
+	char	*errstr;
 };
 
 struct dmreq {
@@ -36,24 +39,35 @@
 #define		p_FLAG		(1 << 10)
 #define		r_FLAG		(1 << 11)
 #define		s_FLAG		(1 << 12)
+#define		V_TTY		(1 << 13)
 
 	char	*i_filename;
 	char	*URL;
 	char	*path;
 };
 
-struct msg {
+struct dmmsg {
 	char 	 op;
 	int 	 len;
 	char 	*buf;
 };
 
+struct xferstat {
+	char		 name[64];
+	struct timeval	 start;		/* start of transfer */
+	struct timeval	 last;		/* time of last update */
+	struct timeval	 last2;		/* time of previous last update */
+	off_t		 size;		/* size of file per HTTP hdr */
+	off_t		 offset;	/* starting offset in file */
+	off_t		 rcvd;		/* bytes already received */
+	off_t		 lastrcvd;	/* bytes received since last update */
+};
+
 #define		DMREQ			1
 #define		DMRESP			2
-#define		DMSTATREQ		3
-#define		DMSTATRESP		4
-#define		DMAUTHREQ		5
-#define		DMAUTHRESP		6
-#define		DMSIG			7
+#define		DMAUTHREQ		3
+#define		DMAUTHRESP		4
+#define		DMSIG			5
+#define		DMSTAT			6
 
 #endif /* _DMCLIENT_H */

Modified: soc2013/ambarisha/head/usr.bin/dms/dms.c
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dms/dms.c	Tue Jul 16 18:02:44 2013	(r254849)
+++ soc2013/ambarisha/head/usr.bin/dms/dms.c	Tue Jul 16 19:01:03 2013	(r254850)
@@ -14,71 +14,168 @@
 #include "list.h"
 #include "dms.h"
 
-#define	MAX_ALIVE_CONNECTIONS	256
+int	 stop;
 
-static int	sigint;
+static int
+read_fd(int sock)
+{
+	struct msghdr msg;
+	struct iovec iov[1];
+	ssize_t n;
+	char c;
+	int newfd;
+
+	union {
+		struct cmsghdr cm;
+		char control[CMSG_SPACE(sizeof(int))];
+	} control_un;
+	struct cmsghdr *cmptr;
+
+	msg.msg_control = control_un.control;
+	msg.msg_controllen = sizeof(control_un.control);
+
+	msg.msg_name = NULL;
+	msg.msg_namelen = 0;
+
+	iov[0].iov_base = &c;
+	iov[0].iov_len = sizeof(c);
+	msg.msg_iov = iov;
+	msg.msg_iovlen = 1;
+
+	if ( (n = recvmsg(sock, &msg, 0)) <= 0)
+		return (n);
+
+	if ( (cmptr = CMSG_FIRSTHDR(&msg)) != NULL &&
+		cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
+		if (cmptr->cmsg_level != SOL_SOCKET)
+			/* ERROR : control level != SOL_SOCKET */;
+
+		if (cmptr->cmsg_type != SCM_RIGHTS)
+			 /* ERROR : control type != SCM_RIGHTS */;
+
+		newfd = *((int *) CMSG_DATA(cmptr));
+	} else {
+		newfd = -1;
+	}
+
+	return newfd;
+}
 
 static int
-mk_dmjob(int sock, struct dmreq dmreq, struct dmjob *dmjob)
+Read_fd(int sock)
 {
+	int ret = read_fd(sock);
+	if (ret == -1) {
+		perror("Read_fd():");
+	} else {
+		printf("Read_fd(): Success\n");
+	}
+	return(ret);
+}
+
+static struct dmjob *
+mk_dmjob(int sock, struct dmreq dmreq)
+{
+	struct dmjob *dmjob = (struct dmjob *) Malloc(sizeof(struct dmjob));
+
 	/* Right now dmjob and dmreq are same */
 	dmjob->v_level = dmreq.v_level;
 	dmjob->family = dmreq.family;
 	dmjob->ftp_timeout = dmreq.ftp_timeout;
 	dmjob->http_timeout = dmreq.http_timeout;
 	dmjob->B_size = dmreq.B_size;
+
 	dmjob->S_size = dmreq.S_size;
 	dmjob->T_secs = dmreq.T_secs;
 	dmjob->flags = dmreq.flags;
-	dmjob->i_filename = dmreq.i_filename;
-	dmjob->URL = dmreq.URL;
-	dmjob->path = dmreq.path;
-	return 0;
+
+	if (dmjob->flags & V_TTY)
+		printf("v_tty is set :)\n");
+	else 	
+		printf("v_tty is already gone\n");
+	printf("HELLO???\n");
+
+	dmjob->i_filename = (char *) Malloc(strlen(dmreq.i_filename) + 1);
+	strcpy(dmjob->i_filename, dmreq.i_filename);
+
+	dmjob->URL = (char *) Malloc(strlen(dmreq.URL) + 1);
+	strcpy(dmjob->URL, dmreq.URL);
+
+	dmjob->path = (char *) Malloc(strlen(dmreq.path) + 1);
+	strcpy(dmjob->path, dmreq.path);
+
+	dmjob->fd = Read_fd(sock);
+	dmjob->csock = sock;
+
+	return dmjob;
 }
 
-static int
-Mk_dmjob(int sock, struct dmreq dmreq, struct dmjob *dmjob)
+static struct dmjob *
+Mk_dmjob(int sock, struct dmreq dmreq)
 {
-	int err = mk_dmjob(sock, dmreq, dmjob);
-	if (err == -1) {
+	struct dmjob *dmjob = mk_dmjob(sock, dmreq);
+	if (dmjob == NULL) {
 		perror("mk_dmjob():");
 #if DEBUG
 	} else {
 		printf("mk_dmjob(): Success\n");
 #endif
 	}
-	return err;
+	return dmjob;
+}
+
+static void
+rm_dmjob(struct dmjob **dmjob)
+{
+	free((*dmjob)->i_filename);
+	free((*dmjob)->path);
+	free((*dmjob)->URL);
+	free(*dmjob);
+	*dmjob = NULL;
 }
 
 static int
-parse_request(char *rcvbuf, int bufsize, struct dmreq *dmreq)
+parse_request(char *rcvbuf, int bufsize)
 {
 	int i = 0;
 
+	struct dmreq *dmreq = (struct dmreq *) Malloc(sizeof(struct dmreq));
+
 	memcpy(&(dmreq->v_level), rcvbuf + i, sizeof(dmreq->v_level));
 	i += sizeof(dmreq->v_level);
 
 	memcpy(&(dmreq->family), rcvbuf + i, sizeof(dmreq->family));
 	i += sizeof(dmreq->family);
 
-	memcpy(&(dmreq->ftp_timeout), rcvbuf, sizeof(dmreq->ftp_timeout));
+	memcpy(&(dmreq->ftp_timeout), rcvbuf + i, sizeof(dmreq->ftp_timeout));
 	i += sizeof(dmreq->ftp_timeout);
 	
-	memcpy(&(dmreq->http_timeout), rcvbuf, sizeof(dmreq->http_timeout));
+	memcpy(&(dmreq->http_timeout), rcvbuf + i, sizeof(dmreq->http_timeout));
 	i += sizeof(dmreq->http_timeout);
 	
-	memcpy(&(dmreq->B_size), rcvbuf, sizeof(dmreq->B_size));
+	memcpy(&(dmreq->B_size), rcvbuf + i, sizeof(dmreq->B_size));
 	i += sizeof(dmreq->B_size);
 	
-	memcpy(&(dmreq->S_size), rcvbuf, sizeof(dmreq->S_size));
+	memcpy(&(dmreq->S_size), rcvbuf + i, sizeof(dmreq->S_size));
 	i += sizeof(dmreq->S_size);
 	
-	memcpy(&(dmreq->T_secs), rcvbuf, sizeof(dmreq->T_secs));
+	memcpy(&(dmreq->T_secs), rcvbuf + i, sizeof(dmreq->T_secs));
 	i += sizeof(dmreq->T_secs);
 	
-	memcpy(&(dmreq->flags), rcvbuf, sizeof(dmreq->flags));
+	memcpy(&(dmreq->flags), rcvbuf + i, sizeof(dmreq->flags));
+
+	printf("flags = %d\n", *(int *)(rcvbuf + i));
 	i += sizeof(dmreq->flags);
-	
+	printf("i after flags == %d\n honey", i);
+
+	if (dmreq->flags & V_TTY)
+		printf("v_tty is STTIIIIIILLL set :)\n");
+	else 	
+		printf("v_tty is already gone\n");
+	printf("RARRRR\n");
+
+	write(1, rcvbuf, bufsize);
+
 	int sz = strlen(rcvbuf+i);
 	dmreq->i_filename = (char *) Malloc(sz);
 	strcpy(dmreq->i_filename, rcvbuf+i);
@@ -94,29 +191,69 @@
 	strcpy(dmreq->path, rcvbuf+i);
 	i += sz + 1;
 	
-	return (0);
+	return dmreq;
 }
 
 static int
-Parse_request(char *rcvbuf, int bufsize, struct dmreq *dmreq)
+Parse_request(char *rcvbuf, int bufsize)
 {
-	int err = parse_request(rcvbuf, bufsize, dmreq);
-	if (err == -1) {
+	struct dmreq *dmreq = parse_request(rcvbuf, bufsize);
+	if (dmreq == NULL) {
 		perror("Parse_request():");
 #if DEBUG
 	} else {
 		printf("Parse_reqeust(): Success\n");
 #endif
 	}
-	return err;
+	return dmreq;
+}
+
+static void
+Free_request(struct dmreq **dmreq)
+{
+	free((*dmreq)->i_filename);
+	free((*dmreq)->URL);
+	free((*dmreq)->path);
+	free(*dmreq);
+	*dmreq = NULL;
+}
+
+static void
+send_report(int sock, struct dmrep report, char op)
+{
+	char *buf;
+	int bufsize = sizeof(report) - sizeof(report.errstr);
+	int errlen = strlen(report.errstr);
+	bufsize +=  errlen;	
+
+	buf = (char *) Malloc(bufsize);
+	int i = 0;
+	
+	memcpy(buf + i, &(report.status), sizeof(report.status));
+	i += sizeof(report.status);
+
+	memcpy(buf + i, &(report.errcode), sizeof(report.errcode));
+	i += sizeof(report.errcode);
+
+	strcpy(buf + i, report.errstr);
+	i += errlen;
+	
+	struct dmmsg msg;
+	msg.op = op;
+	msg.buf = buf;
+	msg.len = bufsize;
+	send_msg(sock, msg);
+	
+	free(buf);
 }
 
 static int
 handle_request(int csock, struct conn **conns)
 {
-	struct dmjob dmjob;
-	struct dmreq dmreq;
-	struct msg msg;
+	struct dmjob *dmjob;
+	struct dmreq *dmreq;
+	struct dmmsg msg;
+	struct dmrep report;
 	int err;
 	pid_t pid;
 
@@ -124,38 +261,11 @@
 	
 	switch (msg.op) {
 	case DMREQ:
- 		err = Parse_request(msg.buf, msg.len, &dmreq);
-		err = Mk_dmjob(csock, dmreq, &dmjob);
-
-		int sockets[2];
-		Socketpair(AF_LOCAL, SOCK_STREAM, 0, sockets);
-
-		pid = fork();
-		if (pid == 0) {
-			/* Close all unwanted fds */
-			struct conn *cur = *conns;
-			while (cur != NULL) {
-				close(cur->client);
-				close(cur->worker);
-				*conns = rm_conn(*conns, cur);
-				cur = *conns;
-			}
-
-			close(csock);
-			close(sockets[0]);
-
-			/* Enter sandbox mode */
-			// if(cap_enter() < 0) 
-			//	errx(2, "Worker: Couldn't enter sandbox mode");
-			/* Can't do this till libfetch is modified */
-
-			run_worker(dmjob, sockets[1]);
-			exit(0);
-		} else {
-			//close(sockets[1]);
-			*conns = add_conn(*conns, csock, sockets[0]);
-		}
-
+ 		dmreq = Parse_request(msg.buf, msg.len);
+		dmjob = Mk_dmjob(csock, *dmreq);
+		Free_request(&dmreq);
+		do_job(*dmjob, &report);
+		send_report(csock, report, DMRESP);
 		default:
 			/* Unknown opcode recieved */
 			return -1;
@@ -168,14 +278,14 @@
 void
 sigint_handler(int sig)
 {
-	sigint = 1;
+	stop = 1;
 	exit(1); // Temporary

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***


More information about the svn-soc-all mailing list