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

ambarisha at FreeBSD.org ambarisha at FreeBSD.org
Thu Sep 26 12:30:28 UTC 2013


Author: ambarisha
Date: Thu Sep 26 12:30:28 2013
New Revision: 257719
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=257719

Log:
  Fixed the compilation warnings in dms and dmget
  

Added:
  soc2013/ambarisha/head/usr.bin/dmget/utils.h
  soc2013/ambarisha/head/usr.bin/dms/mirror.h
  soc2013/ambarisha/head/usr.bin/dms/utils.h
Modified:
  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/dms.c
  soc2013/ambarisha/head/usr.bin/dms/mirror.c
  soc2013/ambarisha/head/usr.bin/dms/utils.c
  soc2013/ambarisha/head/usr.bin/dms/worker.c

Modified: soc2013/ambarisha/head/usr.bin/dmget/dmget.c
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dmget/dmget.c	Thu Sep 26 10:33:15 2013	(r257718)
+++ soc2013/ambarisha/head/usr.bin/dmget/dmget.c	Thu Sep 26 12:30:28 2013	(r257719)
@@ -8,10 +8,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
-
+#include <signal.h>
 
 #include "dm.h"
 #include "dmget.h"
+#include "utils.h"
 
 dm_auth_t	 dmAuthMethod;
 stat_display_t	 dmStatDisplayMethod;
@@ -222,12 +223,12 @@
 }
 
 static int
-send_signal(int sock)
+send_signal(int sock, int sig)
 {
 	struct dmmsg msg;
 	msg.op = DMSIG;
-	msg.buf = &signal;
-	msg.len = sizeof(signal);
+	msg.buf = (char *)&sig;
+	msg.len = sizeof(sig);
 	return (send_dmmsg(sock, msg));
 }
 
@@ -409,8 +410,13 @@
 			goto failure;
 		}
 
-		if (sigint || siginfo) {
-			send_signal(sock);
+		if (sigint) {
+			send_signal(sock, SIGINT);
+			goto signal;
+		}
+
+		if (siginfo) {
+			send_signal(sock, SIGINFO);
 			goto signal;
 		}
 		

Modified: soc2013/ambarisha/head/usr.bin/dmget/dmget.h
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dmget/dmget.h	Thu Sep 26 10:33:15 2013	(r257718)
+++ soc2013/ambarisha/head/usr.bin/dmget/dmget.h	Thu Sep 26 12:30:28 2013	(r257719)
@@ -17,7 +17,7 @@
 	char 	*scheme;
 	char	*host;
 	char	 user[AUTH_USERLEN+1];
-	char	 pwd[AUTH_PWDLEN+1]
+	char	 pwd[AUTH_PWDLEN+1];
 };
 
 extern int		 dmLastErrCode;

Modified: soc2013/ambarisha/head/usr.bin/dmget/fetch.c
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dmget/fetch.c	Thu Sep 26 10:33:15 2013	(r257718)
+++ soc2013/ambarisha/head/usr.bin/dmget/fetch.c	Thu Sep 26 12:30:28 2013	(r257719)
@@ -261,7 +261,7 @@
 	dmreq.B_size = B_size;
 	dmreq.S_size = S_size;
 	dmreq.URL = URL;
-	dmreq.path = path;
+	dmreq.path = (char *)path;
 	dmreq.T_secs = T_secs;
 
 	if (i_flag) dmreq.i_filename = i_filename;

Modified: soc2013/ambarisha/head/usr.bin/dmget/utils.c
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dmget/utils.c	Thu Sep 26 10:33:15 2013	(r257718)
+++ soc2013/ambarisha/head/usr.bin/dmget/utils.c	Thu Sep 26 12:30:28 2013	(r257719)
@@ -1,13 +1,15 @@
-#include <sys/socket.h>
 #include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 
+#include <sys/socket.h>
+
 #include "dm.h"
 
 /* Utils for handling messages */
 
-extern char *dm_errstr;
-
 int
 send_dmmsg(int socket, struct dmmsg msg)
 {
@@ -50,7 +52,7 @@
 	struct dmmsg *msg = (struct dmmsg *) malloc(sizeof(struct dmmsg));
 	if (msg == NULL) {
 		fprintf(stderr, "send_dmmsg: malloc: insufficient memory\n");
-		return -1;
+		return NULL;
 	}
 	
 	err = read(sock, &bufsize, sizeof(bufsize));
@@ -68,7 +70,7 @@
 
 	err = read(sock, &(msg->op), sizeof(msg->op));
 	if (err == 0) {
-		fprintf(stderr,stderr, "recv_dmmsg: remote end"
+		fprintf(stderr, "recv_dmmsg: remote end"
 					" closed connection\n");
 		goto error;
 	} else if (err == -1) {
@@ -89,7 +91,7 @@
 	err = read(sock, msg->buf, bufsize);
 	if (err == 0) {
 		msg->len = 0;
-		fprintf(stderr,stderr, "recv_dmmsg: remote end"
+		fprintf(stderr,"recv_dmmsg: remote end"
 					" closed connection\n");
 		free(msg->buf);
 		free(msg);

Added: soc2013/ambarisha/head/usr.bin/dmget/utils.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/ambarisha/head/usr.bin/dmget/utils.h	Thu Sep 26 12:30:28 2013	(r257719)
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include <sys/socket.h>
+
+#include "dm.h"
+
+/* Utils for handling messages */
+
+int
+send_dmmsg(int socket, struct dmmsg msg);
+
+struct dmmsg *
+recv_dmmsg(int sock);
+
+void
+free_dmmsg(struct dmmsg **msg);

Modified: soc2013/ambarisha/head/usr.bin/dms/dms.c
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dms/dms.c	Thu Sep 26 10:33:15 2013	(r257718)
+++ soc2013/ambarisha/head/usr.bin/dms/dms.c	Thu Sep 26 12:30:28 2013	(r257719)
@@ -10,22 +10,25 @@
 #include <err.h>
 #include <fetch.h>
 #include <pthread.h>
+#include <signal.h>
 
 #include "dm.h"
 #include "dms.h"
+#include "utils.h"
+#include "mirror.h"
 
 static int	dm_err;
 static char	dm_errstr[512];
 
 int	 	 	 stop;
-
 struct dmjob		*jobs;
 pthread_mutex_t	 	 job_queue_mutex;
 
 extern struct dmmirr		*mirrors;
 extern pthread_mutex_t		 mirror_list_mutex;
 
-void *run_worker(struct dmjob *job);
+extern void *run_worker(void *);
+extern int send_report(int, struct dmrep);
 
 static int
 read_fd(int sock)
@@ -349,97 +352,31 @@
 static void
 run_event_loop(int socket)
 {
-	int i, ret, maxfd = socket;
+	int ret, csock;
+	struct sockaddr_un cliaddr;
+	size_t cliaddrlen;
 	struct dmjob *cur;
 	void *retptr;
-	fd_set fdset;
 
 	jobs = NULL;
 	job_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 	mirrors = NULL;
 	mirror_list_mutex = PTHREAD_MUTEX_INITIALIZER;
 	load_mirrors();
 
 	signal(SIGINT, sigint_handler);
 	while (!stop) {
-
-		/* Prepare fdset and make select call */
-		FD_ZERO(&fdset);
-		maxfd = socket;
-		FD_SET(socket, &fdset);
-
-		/* Acquire job queue lock */
-		ret = pthread_mutex_lock(&job_queue_mutex);
-		if (ret == -1) {
-			fprintf(stderr, "handle_request: Attempt to acquire"
-					" job queue mutex failed\n");
-			return -1;
-		}
-
-		cur = jobs;
-		while (cur != NULL) {
-			FD_SET(cur->client, &fdset);
-			if (cur->client > maxfd)
-				maxfd = cur->client;
-			cur = cur->next;
-		}
-
-		ret = pthread_mutex_unlock(&job_queue_mutex);
-		if (ret == -1) {
-			fprintf(stderr, "handle_request: Couldn't release "
-					"job queue lock\n");
-
-			return -1;
-		}
-		/* Job queue lock released */
-
-		ret = select(maxfd + 1, &fdset, NULL, NULL, NULL);
-		if (ret ==  -1) {
+		cliaddrlen = sizeof(cliaddr);
+		csock = accept(socket, (struct sockaddr *) &cliaddr,
+				&cliaddrlen);
+		if (csock == -1) {
 			fprintf(stderr, "run_event_loop: "
-					"select: %s\n", strerror(errno));
+				"select: %s\n", strerror(errno));
 			goto wrap_up;
 		}
 
-		/* Acquire job queue lock */
-		ret = pthread_mutex_lock(&job_queue_mutex);
-		if (ret == -1) {
-			fprintf(stderr, "handle_request: Attempt to acquire"
-					" job queue mutex failed\n");
-			return -1;
-		}
-
-		cur = jobs;
-		while (cur != NULL) {
-			ret = service_job(cur, &fdset);
-			if (ret > 0) {
-				close(cur->client);
-				jobs = rm_job(jobs, cur);
-				rm_dmjob(&cur);
-			}
-			cur = cur->next;
-		}
-
-		ret = pthread_mutex_unlock(&job_queue_mutex);
-		if (ret == -1) {
-			fprintf(stderr, "handle_request: Couldn't release "
-					"job queue lock\n");
-
-			return -1;
-		}
-		/* Job queue lock released */
-
-		if (FD_ISSET(socket, &fdset)) {
-			struct sockaddr_un cliaddr;
-			size_t cliaddrlen = sizeof(cliaddr);
-			int csock = accept(socket, (struct sockaddr *) &cliaddr,
-					&cliaddrlen);
-			if (csock == -1) {
-				fprintf(stderr, "run_event_loop: "
-					"select: %s\n", strerror(errno));
-				goto wrap_up;
-			}
-			handle_request(csock);
-		}
+		handle_request(csock);
 	}
 
 wrap_up:
@@ -449,7 +386,7 @@
 	if (ret == -1) {
 		fprintf(stderr, "handle_request: Attempt to acquire"
 				" job queue mutex failed\n");
-		return -1;
+		return;
 	}
 
 	cur = jobs;
@@ -467,7 +404,7 @@
 		fprintf(stderr, "handle_request: Couldn't release "
 				"job queue lock\n");
 
-		return -1;
+		return;
 	}
 	/* Job queue lock released */
 

Modified: soc2013/ambarisha/head/usr.bin/dms/mirror.c
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dms/mirror.c	Thu Sep 26 10:33:15 2013	(r257718)
+++ soc2013/ambarisha/head/usr.bin/dms/mirror.c	Thu Sep 26 12:30:28 2013	(r257719)
@@ -1,15 +1,19 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+
 #include <errno.h>
 #include <sys/time.h>
+
 #include "dm.h"
 #include "dms.h"
+#include "mirror.h"
 
 #define 	MAX_SAMPLES	256
 #define		MAX_CONNS	5
 #define		MIRRORS_FILE	"mirrors.list"
 
-struct dmmirr		*mirrors;
-pthread_mutex_t	 	 mirror_list_mutex;
-
 static const char *MIRROR_LIST[] = {
 	"ftp.freebsd.org"
 };

Added: soc2013/ambarisha/head/usr.bin/dms/mirror.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/ambarisha/head/usr.bin/dms/mirror.h	Thu Sep 26 12:30:28 2013	(r257719)
@@ -0,0 +1,12 @@
+#ifndef _MIRROR_H_
+#define _MIRROR_H_
+
+struct dmmirr	*mirrors;
+pthread_mutex_t	 mirror_list_mutex;
+
+int load_mirrors(void);
+int save_mirrors(void);
+void update_mirror(struct dmmirr *, struct xferstat *);
+struct dmmirr *get_mirror(void);
+
+#endif

Modified: soc2013/ambarisha/head/usr.bin/dms/utils.c
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dms/utils.c	Thu Sep 26 10:33:15 2013	(r257718)
+++ soc2013/ambarisha/head/usr.bin/dms/utils.c	Thu Sep 26 12:30:28 2013	(r257719)
@@ -1,13 +1,15 @@
-#include <sys/socket.h>
 #include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 
+#include <sys/socket.h>
+
 #include "dm.h"
 
 /* Utils for handling messages */
 
-extern char *dm_errstr;
-
 int
 send_dmmsg(int socket, struct dmmsg msg)
 {
@@ -50,7 +52,7 @@
 	struct dmmsg *msg = (struct dmmsg *) malloc(sizeof(struct dmmsg));
 	if (msg == NULL) {
 		fprintf(stderr, "send_dmmsg: malloc: insufficient memory\n");
-		return -1;
+		return NULL;
 	}
 	
 	err = read(sock, &bufsize, sizeof(bufsize));
@@ -68,7 +70,7 @@
 
 	err = read(sock, &(msg->op), sizeof(msg->op));
 	if (err == 0) {
-		fprintf(stderr,stderr, "recv_dmmsg: remote end"
+		fprintf(stderr, "recv_dmmsg: remote end"
 					" closed connection\n");
 		goto error;
 	} else if (err == -1) {
@@ -89,7 +91,7 @@
 	err = read(sock, msg->buf, bufsize);
 	if (err == 0) {
 		msg->len = 0;
-		fprintf(stderr,stderr, "recv_dmmsg: remote end"
+		fprintf(stderr,"recv_dmmsg: remote end"
 					" closed connection\n");
 		free(msg->buf);
 		free(msg);

Added: soc2013/ambarisha/head/usr.bin/dms/utils.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2013/ambarisha/head/usr.bin/dms/utils.h	Thu Sep 26 12:30:28 2013	(r257719)
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include <sys/socket.h>
+
+#include "dm.h"
+
+/* Utils for handling messages */
+
+int
+send_dmmsg(int socket, struct dmmsg msg);
+
+struct dmmsg *
+recv_dmmsg(int sock);
+
+void
+free_dmmsg(struct dmmsg **msg);

Modified: soc2013/ambarisha/head/usr.bin/dms/worker.c
==============================================================================
--- soc2013/ambarisha/head/usr.bin/dms/worker.c	Thu Sep 26 10:33:15 2013	(r257718)
+++ soc2013/ambarisha/head/usr.bin/dms/worker.c	Thu Sep 26 12:30:28 2013	(r257719)
@@ -1,7 +1,10 @@
 #include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/select.h>
 
 #include <stdint.h>
+#include <stdlib.h>
+#include <pthread.h>
 #include <err.h>
 #include <string.h>
 #include <errno.h>
@@ -10,15 +13,16 @@
 #include <unistd.h>
 
 #include "dms.h"
+#include "utils.h"
 #include "dm.h"
+#include "mirror.h"
 
+#define	TMP_EXT		".tmp"
 
 static const char 	*prefixes = " kMGTP";
 extern struct dmjob 	*jobs;
 extern pthread_mutex_t	 job_queue_mutex;
 
-#define	TMP_EXT		".tmp"
-
 static int
 authenticate(struct url *url)
 {
@@ -316,6 +320,50 @@
 }
 
 static int
+check_signal(int signum, struct dmjob *dmjob)
+{
+	struct timeval tv;
+	struct dmmsg *msg;
+	int *sig, ret;		
+	fd_set fds;
+	
+	if (signum == SIGINT && dmjob->sigint != 0)
+		return 1;
+	if (signum == SIGINFO && dmjob->siginfo != 0)
+		return 1;
+	if (signum == SIGALRM && dmjob->sigalrm != 0)
+		return 1;
+
+	do {
+		FD_ZERO(&fds);
+		FD_SET(dmjob->client, &fds);
+
+		tv.tv_sec = 0;
+		tv.tv_usec = 0;
+
+		ret = select(dmjob->client + 1, &fds, NULL, NULL, &tv);
+		msg = recv_dmmsg(dmjob->client);
+		sig = (int *)msg->buf;	
+		if (*sig == SIGINT)
+			dmjob->sigint = 1;
+		else if (*sig == SIGINFO)
+			dmjob->siginfo = 1;
+		else if (*sig == SIGALRM)
+			dmjob->sigalrm = 1;
+	
+	} while (ret == 1);
+	
+	if (signum == SIGINT && dmjob->sigint != 0)
+		return dmjob->sigint;
+	if (signum == SIGINFO && dmjob->siginfo != 0)
+		return dmjob->siginfo;
+	if (signum == SIGALRM && dmjob->sigalrm != 0)
+		return dmjob->sigalrm;
+
+	return 0;
+}
+
+static int
 fetch(struct dmjob *dmjob, FILE *f, struct url_stat us)
 {
 	struct stat sb, nsb;
@@ -393,12 +441,9 @@
 	if (dmjob->timeout)
 		alarm(dmjob->timeout);
 
-	if (dmjob->sigalrm || dmjob->sigint)
+	if (check_signal(SIGALRM, dmjob) || check_signal(SIGINT, dmjob))
 		goto signal;
 	
-	if (dmjob->sigint)
-		goto signal;
-
 	/* check that size is as expected */
 	/*if (dmreq->S_size) {
 		if (us.size == -1) {
@@ -511,7 +556,7 @@
 			 * from scratch if we want the whole file
 			 */
 			dmjob->url->offset = 0;
-			if (dmjob->sigint)
+			if (check_signal(SIGINT, dmjob))
 				goto signal;
 		}
 
@@ -555,13 +600,13 @@
 
 	/* suck in the data */
 	dmjob->siginfo_en = 1;
-	while (!dmjob->sigint) {
+	while (!check_signal(SIGINT, dmjob)) {
 		if (us.size != -1 && us.size - count < dmreq->B_size &&
 		    us.size - count >= 0)
 			size = us.size - count;
 		else
 			size = dmreq->B_size;
-		if (dmjob->siginfo) {
+		if (check_signal(SIGINFO, dmjob)) {
 			stat_end(&xs, dmjob);
 			dmjob->siginfo = 0;
 		}
@@ -570,7 +615,7 @@
 			break;
 
 		if ((readcnt = fread(buf, 1, size, f)) < size) {
-			if (ferror(f) && errno == EINTR && !dmjob->sigint)
+			if (ferror(f) && errno == EINTR && !check_signal(SIGINT, dmjob))
 				clearerr(f);
 			else if (readcnt == 0) {
 				break;
@@ -581,7 +626,7 @@
 		stat_update(&xs, count += readcnt, dmjob);
 		for (ptr = buf; readcnt > 0; ptr += wr, readcnt -= wr)
 			if ((wr = fwrite(ptr, 1, readcnt, of)) < readcnt) {
-				if (ferror(of) && errno == EINTR && !dmjob->sigint)
+				if (ferror(of) && errno == EINTR && !check_signal(SIGINT, dmjob))
 					clearerr(of);
 				else
 					break;
@@ -590,7 +635,7 @@
 			break;
 	}
 
-	if (!dmjob->sigalrm)
+	if (!check_signal(SIGALRM, dmjob))
 		dmjob->sigalrm = ferror(f) && errno == ETIMEDOUT;
 	dmjob->siginfo_en = 0;
 
@@ -616,9 +661,9 @@
 	}
 
 	/* timed out or interrupted? */
-	if (dmjob->sigalrm)
+	if (check_signal(SIGALRM, dmjob))
 		warnx("transfer timed out");
-	if (dmjob->sigint) {
+	if (check_signal(SIGINT, dmjob)) {
 		warnx("transfer interrupted");
 		goto failure;
 	}
@@ -627,7 +672,7 @@
 	if (f == NULL)
 		goto failure;
 
-	if (!dmjob->sigalrm) {
+	if (!check_signal(SIGALRM, dmjob)) {
 		/* check the status of our files */
 		if (ferror(f))
 			warn("%s", dmreq->URL);
@@ -648,7 +693,7 @@
 	 * If the transfer timed out and we didn't know how much to
 	 * expect, assume the worst (i.e. we didn't get all of it)
 	 */
-	if (dmjob->sigalrm && us.size == -1) {
+	if (check_signal(SIGALRM, dmjob) && us.size == -1) {
 		warnx("%s may be truncated", dmreq->path);
 		goto failure_keep;
 	}
@@ -829,36 +874,6 @@
 	return fetch(dmjob, f, us);	
 }
 
-/* TODO: This handler isn't registered as SIGUSR1 interrupts the download
- * 	 Figure out a proper way to handle this
- * */
-void
-sig_handler(int sig)
-{
-	struct dmjob *tmp = jobs;
-	struct dmmsg *msg;
-	int *clisig;
-	pthread_t tid = pthread_self();
-	if (sig == SIGUSR1) {
-		/* TODO: Umm...Locking? */
-		while (tmp != NULL) {
-			if (pthread_equal(tid, tmp->worker) != 0)
-				break;
-			tmp = tmp->next;
-		}
-
-		msg = recv_dmmsg(tmp->client);
-		clisig = msg->buf;	
-		if (*clisig == SIGINT)
-			tmp->sigint = 1;
-		else if (*clisig == SIGINFO)
-			tmp->siginfo = 1;
-		else if (*clisig == SIGALRM) {
-			tmp->sigalrm = 1;
-		}
-	}
-}
-
 int 
 send_report(int sock, struct dmrep report)
 {
@@ -908,7 +923,7 @@
 	if (ret == -1) {
 		fprintf(stderr, "handle_request: Attempt to acquire"
 				" job queue mutex failed\n");
-		return -1;
+		return NULL;
 	}
 
 	tmp = jobs;
@@ -926,7 +941,7 @@
 		fprintf(stderr, "handle_request: Couldn't release "
 				"job queue lock\n");
 
-		return -1;
+		return NULL;
 	}
 	/* Job queue lock released */
 
@@ -942,7 +957,7 @@
 	if (ret == -1) {
 		fprintf(stderr, "handle_request: Attempt to acquire"
 				" job queue mutex failed\n");
-		return -1;
+		return NULL;
 	}
 
 	/* Serve any outstanding requests from the local tmp file */
@@ -972,8 +987,7 @@
 	if (ret == -1) {
 		fprintf(stderr, "handle_request: Couldn't release "
 				"job queue lock\n");
-
-		return -1;
+		return NULL;
 	}
 	/* Job queue lock released */
 


More information about the svn-soc-all mailing list