version0_gateway.c 6.87 KB
Newer Older
1 2
/*
 * Copyright (c) 2007-2010 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
 */

#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include <sys/time.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <time.h>
#include <assert.h>
#include <sys/types.h>
#include <setjmp.h>
#include <paths.h>
#include <pubsub/pubsub.h>
#include "log.h"

static int	debug   = 0;
static int	verbose = 1;
static int	stop    = 0;

/* Forward decls */
static void     pubsub_callback(pubsub_handle_t *handle,
				pubsub_subscription_t *subscription,
				pubsub_notification_t *notification,
				void *data);

static void     v0_callback(pubsub_handle_t *handle,
			    pubsub_subscription_t *subscription,
			    pubsub_notification_t *notification,
			    void *data);

/* A few things are easier if global */
static pubsub_handle_t		*pubsub_handle;
static pubsub_handle_t		*v0_handle;

char *usagestr = 
 "usage: pubsubv0_gateway [options]\n"
 " -h              Display this message\n"
 " -d              Turn on debugging\n"
 " -v              Turn on verbose logging\n"
 " -p portnum      Specify a pubsub port number to connect to\n"
 "\n";

void
usage()
{
	fprintf(stderr, "%s", usagestr);
	exit(1);
}

static void
setverbose(int sig)
{
	if (sig == SIGUSR1)
		verbose = 1;
	else
		verbose = 0;
}

static void
sigterm(int sig)
{
	stop = 1;
}

int
main(int argc, char **argv)
{
	int			ch;
	int			portnum = PUBSUB_SERVER_PORTNUM;
	char			buf[BUFSIZ];
	pubsub_error_t		pubsub_error;
	
	while ((ch = getopt(argc, argv, "hdvp:")) != -1) {
		switch(ch) {
		case 'd':
			debug++;
			break;
		case 'v':
			verbose++;
			break;
		case 'p':
			if (sscanf(optarg, "%d", &portnum) == 0) {
				fprintf(stderr,
					"Error: -p value is not a number: "
					"%s\n",
					optarg);
				usage();
			}
			else if ((portnum <= 0) || (portnum >= 65536)) {
				fprintf(stderr,
					"Error: -p value is not between "
					"0 and 65536: %d\n",
					portnum);
				usage();
			}
			break;
		case 'h':
			fprintf(stderr, "%s", usagestr);
			exit(0);
			break;
		default:
			usage();
		}
	}

	argv += optind;
	argc -= optind;

	if (argc) {
		error("Unrecognized command line arguments: %s ...\n",argv[0]);
		usage();
	}

	if (!debug) {
		loginit(1, "v0_gateway");
		/* See below for daemonization */
148
		pubsub_debug = 1; /* XXX for now */
149
	} else {
150
		pubsub_debug = debug;
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	}

	info("version0<->version1 gateway daemon starting\n");

	/*
	 * XXX Need to daemonize earlier or the threads go away.
	 */
	if (!debug)
		daemon(0, 0);
	
	signal(SIGUSR1, setverbose);
	signal(SIGUSR2, setverbose);
	signal(SIGTERM, sigterm);

	/*
	 * Stash the pid away.
	 */
	if (!geteuid()) {
		FILE	*fp;

		sprintf(buf, "%s/version0_gateway.pid", _PATH_VARRUN);
		fp = fopen(buf, "w");
		if (fp != NULL) {
			fprintf(fp, "%d\n", getpid());
			(void) fclose(fp);
		}
	}

	if (pubsub_connect("localhost",
			   PUBSUB_SERVER_PORTNUM, &pubsub_handle) < 0) {
		error("Could not connect to pubsub server on localhost!\n");
		exit(-1);
	}
	info("connected to pubsubd server\n");

	if (pubsub_connect("localhost", portnum, &v0_handle) < 0) {
		error("Could not connect to v0 pubsub on localhost!\n");
		exit(-1);
	}
	info("connected to pubsubd server\n");

	/* Subscribe to *all* events */
	if (!pubsub_add_subscription(pubsub_handle, "",
				     pubsub_callback, NULL,
				     &pubsub_error)) {
		error("subscription to pubsub server failed\n");
		exit(-1);
	}
	info("pubsub subscription added\n");

	/* Subscribe to *all* events */
	if (!pubsub_add_subscription(v0_handle, "",
				     v0_callback, NULL,
				     &pubsub_error)) {
		error("subscription to v0 server failed\n");
		exit(-1);
	}
	info("v0 subscription added\n");

	/*
	 * Threaded mode; stuff just happens. 
	 */
	while (!stop) {
		struct timeval  tv = { 5, 0 };

		select(0, NULL, NULL, NULL, &tv);
	}

	pubsub_disconnect(pubsub_handle);
	pubsub_disconnect(v0_handle);
	unlink(buf);
	return 0;
}

225
#if 0
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
static char	notify_debug_string[2*BUFSIZ];

static int
pubsub_notify_traverse_debug(void *arg, char *name,
			     pubsub_type_t type, pubsub_value_t value,
			     pubsub_error_t *error)
{
	char		**bp = (char **) arg;
	char		buf[BUFSIZ], *tp = buf;
	unsigned char   *up;
	int		i, cc;

	switch (type) {
	case STRING_TYPE:
		snprintf(*bp,
			 &notify_debug_string[sizeof(notify_debug_string)-1]-
			 *bp,
			 "%s=%s,", name, value.pv_string);
		break;
		
	case INT32_TYPE:
		snprintf(*bp,
			 &notify_debug_string[sizeof(notify_debug_string)-1]-
			 *bp,
			 "%s=%d,", name, value.pv_int32);
		break;
		
	case OPAQUE_TYPE:
		up = (unsigned char *) value.pv_opaque.data;
		for (i = 0; i < value.pv_opaque.length; i++, up++) {
			cc = snprintf(tp,
				      &buf[sizeof(buf)-1]-tp, "%02hhx", *up);
			tp += cc;
			if (tp >= &buf[sizeof(buf)-1])
				break;
		}		
		snprintf(*bp,
			 &notify_debug_string[sizeof(notify_debug_string)-1]-
			 *bp,
			 "%s=%s,", name, buf);
		break;
		
	default:
		snprintf(*bp,
			 &notify_debug_string[sizeof(notify_debug_string)-1]-
			 *bp,
			 "%s=...,", name);
		break;
	}
	*bp = *bp + strlen(*bp);
	
	return 1;
}
Mike Hibler's avatar
Mike Hibler committed
279
#endif
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

static void
pubsub_callback(pubsub_handle_t *handle,
		pubsub_subscription_t *subscription,
		pubsub_notification_t *notification,
		void *arg)
{
	pubsub_error_t		pubsub_error;

	/*
	 * These are events coming from the version 1 server. We need to
	 * remove the ___elvin_ordered___ ordered flag since the client
	 * will not understand it (no elvin compat on them).
	 */
	pubsub_notification_remove(notification,
				   "___elvin_ordered___", &pubsub_error);
	
	if (pubsub_notify(v0_handle, notification, &pubsub_error) < 0) {
		error("Failed to send pubsub notification to v0!\n");
	}
}

static void
v0_callback(pubsub_handle_t *handle,
	    pubsub_subscription_t *subscription,
	    pubsub_notification_t *notification,
	    void *arg)
{
	pubsub_error_t		pubsub_error;

	/*
	 * These are events coming from version 0 clients. We do not
	 * need to do anything except forward them.
	 */
	if (pubsub_notify(pubsub_handle, notification, &pubsub_error) < 0) {
		error("Failed to send v0 notification to pubsub!\n");
	}
}