event.c 60.3 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2000-2010 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
4 5 6
 * All rights reserved.
 */

Ian Murdock's avatar
Ian Murdock committed
7 8 9 10 11
/*
 * event.c --
 *
 *      Testbed event library.  Currently uses the Elvin publish/
 *      subscribe system for routing event notifications.
12 13 14 15 16 17
 *
 * TODO:
 *	check all pubsub_* call sites to get return value sense correct.
 *	make sure handle->status (and error args in general) is correct.
 *	make sure _t types are passed as pointers-to
 *	deal with hmac_traverse
Ian Murdock's avatar
Ian Murdock committed
18 19 20 21 22 23 24
 */

#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
25
#include <netdb.h>
26
#include <limits.h>
27 28
#include <netinet/in.h>
#include <arpa/inet.h>
29
#include <sys/time.h>
30 31
#include <sys/param.h>
#include <time.h>
32
#include "event.h"
33

34 35 36 37
#ifdef ELVIN_COMPAT
#include <pubsub/elvin_hash.h>
#endif

Mike Hibler's avatar
Mike Hibler committed
38 39 40 41
#define ERROR(fmt,...) \
 { fputs(__FUNCTION__,stderr); fprintf(stderr,": " fmt, ## __VA_ARGS__); }
#define INFO(fmt,...) \
 { fputs(__FUNCTION__,stderr); fprintf(stderr,": " fmt, ## __VA_ARGS__); }
42
#ifdef  DEBUG
Mike Hibler's avatar
Mike Hibler committed
43 44
#define TRACE(fmt,...) \
 { fputs(__FUNCTION__,stderr); fprintf(stderr,": " fmt, ## __VA_ARGS__); }
45
#else
46
#define TRACE(fmt,...)
47
#endif
Ian Murdock's avatar
Ian Murdock committed
48

Kirk Webb's avatar
 
Kirk Webb committed
49 50
#define IPADDRFILE "/var/emulab/boot/myip"

51 52 53
static int event_notification_check_hmac(event_handle_t handle,
					  event_notification_t notification);

Ian Murdock's avatar
Ian Murdock committed
54
static char hostname[MAXHOSTNAMELEN];
55
static char ipaddr[32];
Ian Murdock's avatar
Ian Murdock committed
56

57 58 59 60 61 62
/*
 * Count of how many handles are in use, so that we can avoid cleaning up until
 * the last one is unregistered
 */
static int handles_in_use = 0;

63 64 65 66 67 68
/*
 * Register with the testbed event system.  NAME specifies the name of
 * the event server.  Returns a pointer to a handle that may be passed
 * to other event system routines if the operation is successful, NULL
 * otherwise.
 *
Ian Murdock's avatar
Ian Murdock committed
69 70 71 72 73 74 75 76 77
 * The THREADED parameter should be set to 1 if the registering
 * client is multi-threaded. If THREADED is 1, the event
 * library will call routines that are thread-safe, and event
 * notifications will be dispatched using background threads (i.e.,
 * the client will supply its own event loop). If THREADED is 0, event
 * notifications will be dispatched using an event system-provided
 * event loop, and the client must call event_main after connecting in
 * order to receive notifications.
 *
78 79 80 81 82 83 84 85 86 87
 * Elvin note: NAME is a URL of the form "elvin:/[protocol
 * stack]/[endpoint]", where a protocol stack names a transport
 * module, a security module, and a marshaling module as a comma
 * separated list (e.g., "http,none,xml"), and the endpoint format
 * is dependent on the transport module used.  If no protocol
 * stack is given, the default stack (tcp, none, xdr) is used.  For the
 * testbed's purposes, "elvin://HOSTNAME" should suffice.  If NAME
 * is NULL, then Elvin's server discovery protocol will be used to find
 * the Elvin server.
 */
Ian Murdock's avatar
Ian Murdock committed
88
event_handle_t
89
event_register(char *name, int threaded)
90 91 92 93 94
{
	return event_register_withkeydata(name, threaded, NULL, 0);
}

event_handle_t
Sachin Goyal's avatar
 
Sachin Goyal committed
95
event_register_withkeyfile(char *name, int threaded, char *keyfile) {
96 97
  return event_register_withkeyfile_withretry(name,
					      threaded, keyfile, INT_MAX);
Sachin Goyal's avatar
 
Sachin Goyal committed
98 99 100 101 102
}

event_handle_t
event_register_withkeyfile_withretry(char *name, int threaded, 
				     char *keyfile, int retrycount)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
{
    /* Grab the key data and stick it into the handle. */
    if (keyfile) {
	FILE		*fp;
	unsigned char   buf[2*BUFSIZ];
	int		cc;

	if ((fp = fopen(keyfile, "r")) == NULL) {
		ERROR("could not open keyfile: %s", keyfile);
		return 0;
	}
	if ((cc = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
		ERROR("could not read keyfile: %s", keyfile);
		fclose(fp);
		return 0;
	}
	if (cc == sizeof(buf)) {
		ERROR("keyfile is too big: %s", keyfile);
		fclose(fp);
		return 0;
	}
	fclose(fp);
Sachin Goyal's avatar
 
Sachin Goyal committed
125 126
	return event_register_withkeydata_withretry(name, threaded, 
					  buf, cc, retrycount);
127
    }
Sachin Goyal's avatar
 
Sachin Goyal committed
128 129
    return event_register_withkeydata_withretry(name, threaded, NULL, 
						0, retrycount);
130 131 132 133
}

event_handle_t
event_register_withkeydata(char *name, int threaded,
Sachin Goyal's avatar
 
Sachin Goyal committed
134 135
			   unsigned char *keydata, int keylen){
    return event_register_withkeydata_withretry(name, threaded, keydata,
136
						keylen, INT_MAX);
Sachin Goyal's avatar
 
Sachin Goyal committed
137 138 139 140 141 142 143

}

event_handle_t
event_register_withkeydata_withretry(char *name, int threaded,
			   unsigned char *keydata, int keylen,
			   int retrycount)
Ian Murdock's avatar
Ian Murdock committed
144
{
145 146
    extern int pubsub_is_threaded[] __attribute__ ((weak));
    
147
    event_handle_t	handle;
148
    pubsub_handle_t    *server;
149 150
    struct hostent     *he;
    struct in_addr	myip;
151 152
    char	       *sstr = 0, *pstr = 0, *cp;
    int			port = PUBSUB_SERVER_PORTNUM;
Ian Murdock's avatar
Ian Murdock committed
153 154 155 156 157 158

    if (gethostname(hostname, MAXHOSTNAMELEN) == -1) {
        ERROR("could not get hostname: %s\n", strerror(errno));
        return 0;
    }

159 160 161 162
    /*
     * Get our IP address. Thats how we name ourselves to the
     * Testbed Event System. 
     */
Kirk Webb's avatar
 
Kirk Webb committed
163 164 165 166
    if ((he = gethostbyname(hostname)) != NULL) {
        memcpy((char *)&myip, he->h_addr, he->h_length);
        strcpy(ipaddr, inet_ntoa(myip));
    } else {
167 168 169 170
	unsigned int        o1, o2, o3, o4;
	int                 scanres;
	FILE               *fp;

171 172
	ERROR("could not get IP address from hostname: %s, "
              "reading IP from %s.\n", hostname, IPADDRFILE);
Kirk Webb's avatar
 
Kirk Webb committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        /* Try getting the node's ID from BOOTDIR/myip before giving up. */
	fp = fopen(IPADDRFILE, "r");
	if (fp != NULL) {
            scanres = fscanf(fp, "%3u.%3u.%3u.%3u", &o1, &o2, &o3, &o4);
	    (void) fclose(fp);
            if (scanres != 4) {
                ERROR("IP address not found on first line of file!\n");
                return 0;
            }
            if (o1 > 255 || o2 > 255 || o3 > 255 || o4 > 255) {
                ERROR("IP address inside file is invalid!\n");
                return 0;
            }
            snprintf(ipaddr, sizeof(ipaddr), "%u.%u.%u.%u", o1, o2, o3, o4);
        } else {
            ERROR("could not get IP from local file %s either!", IPADDRFILE);
            return 0;
        }
191 192
    }

Ian Murdock's avatar
Ian Murdock committed
193
    TRACE("registering with event system (hostname=\"%s\")\n", hostname);
Ian Murdock's avatar
Ian Murdock committed
194

195 196
    /* Allocate a handle to be returned to the caller: */
    handle = xmalloc(sizeof(*handle));
197 198 199 200 201 202 203 204 205
    bzero(handle, sizeof(*handle));

    /* Grab the key data and stick it into the handle. */
    if (keydata) {
	handle->keydata = xmalloc(keylen + 1);
	handle->keylen  = keylen;
	memcpy(handle->keydata, keydata, keylen);
	handle->keydata[keylen] = (unsigned char)0;
    }
Ian Murdock's avatar
Ian Murdock committed
206

207 208 209
    /* Set up the interface pointers: */
    handle->connect = pubsub_connect;
    handle->disconnect = pubsub_disconnect;
210
#ifdef THREADED
211 212 213
    assert(threaded == 1);
    assert(pubsub_is_threaded != NULL);
    handle->mainloop = NULL; /* no mainloop for mt programs */
214
#else
215 216 217
    assert(threaded == 0);
    assert(pubsub_is_threaded == NULL);
    handle->mainloop = pubsub_mainloop;
218
#endif
219 220 221
    handle->notify = pubsub_notify;
    handle->subscribe = pubsub_add_subscription;
    handle->unsubscribe = pubsub_rem_subscription;
Ian Murdock's avatar
Ian Murdock committed
222

223 224 225 226
    /* XXX parse server and port from "elvin://host:port" */
    cp = strdup(name);
    if (cp) {
      sstr = strrchr(cp, '/');
Ian Murdock's avatar
Ian Murdock committed
227
    }
228 229 230
    if (!sstr) {
      ERROR("could not parse: %s", name);
      goto bad;
231
    }
232 233 234 235 236
    *sstr++ = '\0';
    pstr = strrchr(sstr, ':');
    if (pstr) {
	    *pstr++ = '\0';
	    port = atoi(pstr);
Sachin Goyal's avatar
 
Sachin Goyal committed
237 238
    }

239 240 241 242 243 244 245 246 247 248 249 250 251 252
    /* Preallocate a pubsub handle so we can set the retry count */
    if (pubsub_alloc_handle(&server) != 0) {
        ERROR("could not allocate event server handle\n");
	goto bad;
    }

    /* set connection retries */
    if (pubsub_set_connection_retries(server,
				      retrycount, &handle->status) != 0) {
	ERROR("pubsub_set_connection_retries failed\n");
	goto bad;
    }

    /* Connect to the event server */
253
    if (handle->connect(sstr, port, &server) != 0) {
254
        ERROR("could not connect to event server\n");
255
	goto bad;
Ian Murdock's avatar
Ian Murdock committed
256 257 258 259
    }

    handle->server = server;

260 261 262 263
    /*
     * Keep track of how many handles we have outstanding
     */
    handles_in_use++;
264
    free(cp);
Ian Murdock's avatar
Ian Murdock committed
265
    return handle;
266 267 268 269 270

 bad:
    if (handle->keydata)
        free(handle->keydata);
    free(handle);
271
    free(cp);
272
    return 0;
Ian Murdock's avatar
Ian Murdock committed
273 274
}

275 276

/*
Ian Murdock's avatar
Ian Murdock committed
277
 * Unregister with the testbed event system. Returns non-zero if the
278 279 280
 * operation is successful, 0 otherwise.
 */

Ian Murdock's avatar
Ian Murdock committed
281 282 283 284
int
event_unregister(event_handle_t handle)
{
    if (!handle) {
Ian Murdock's avatar
Ian Murdock committed
285
        ERROR("invalid parameter\n");
Ian Murdock's avatar
Ian Murdock committed
286 287 288
        return 0;
    }

Ian Murdock's avatar
Ian Murdock committed
289
    TRACE("unregistering with event system (hostname=\"%s\")\n", hostname);
Ian Murdock's avatar
Ian Murdock committed
290

291 292 293
    /* Disconnect from the server: */
    if (handle->disconnect(handle->server) != 0) {
        ERROR("could not disconnect from Pubsub server\n");
Ian Murdock's avatar
Ian Murdock committed
294 295 296
        return 0;
    }

297 298 299 300
    TRACE("disconnect completed\n");

    handles_in_use--;

301 302
    if (handle->keydata)
        free(handle->keydata);
Ian Murdock's avatar
Ian Murdock committed
303 304 305 306 307
    free(handle);

    return 1;
}

308 309 310 311 312 313 314 315 316 317 318 319 320 321
/*
 * Callback for event_poll timeout that just records that the timeout
 * happened.
 */
static int
timeout_callback(pubsub_handle_t *handle, pubsub_timeout_t *timeout,
		 void *data, pubsub_error_t *error)
{
	assert(data != 0);
	assert(*(int *)data == 0);
	*(int *)data = 1;

	return 0;
}
322

Mike Hibler's avatar
Mike Hibler committed
323
/*
324 325
 * An internal function to handle the two different event_poll calls, without
 * making the library user mess around with arguments they don't care about.
Mike Hibler's avatar
Mike Hibler committed
326 327
 */
int
328
internal_event_poll(event_handle_t handle, int blocking, unsigned int timeout)
Mike Hibler's avatar
Mike Hibler committed
329
{
330 331
	int rv, triggered = 0;
	pubsub_timeout_t *pubsub_timeout = NULL;
Mike Hibler's avatar
Mike Hibler committed
332 333 334 335 336 337

	if (!handle->mainloop) {
		ERROR("multithreaded programs cannot use event_poll\n");
		return 0;
	}

338 339 340 341 342 343
	/*
	 * If the user wants a timeout, set up an elvin timeout now. We just
	 * use a NULL callback, so that it simply causes a timeout, and doesn't
	 * actually do anything.
	 */
	if (timeout) {
344 345 346 347 348 349 350 351 352
		pubsub_timeout = pubsub_add_timeout(handle->server, NULL,
						    timeout,
						    timeout_callback,
						    (void *)&triggered,
						    &handle->status);
		if (!pubsub_timeout) {
			ERROR("Elvin pubsub_sync_add_timeout failed\n");
			pubsub_error_fprintf(stderr, &handle->status);
			return pubsub_error_get_code(&handle->status);
353 354
		}
	}
355 356 357 358
	rv = pubsub_dispatch(handle->server, blocking, &handle->status);
	if (rv != 0) {
		ERROR("Pubsub dispatcher failed\n");
		pubsub_error_fprintf(stderr, &handle->status);
Mike Hibler's avatar
Mike Hibler committed
359 360
	}

361 362
/*	rv = pubsub_error_get_code(&handle->status); */

363 364 365 366 367 368
	/*
	 * Try to remove the timeout - if it didn't go off, we don't want to
	 * hit it later. We don't check the return value, since, if it did go
	 * off (and we don't really have a good way of knowing that), it's not
	 * there any more, so it looks like an error.
	 */
369 370 371
	if (timeout && pubsub_timeout && !triggered)
		pubsub_remove_timeout(handle->server, pubsub_timeout,
				      &handle->status);
372

373
	return rv;
Mike Hibler's avatar
Mike Hibler committed
374 375
}

376
/*
377 378
 * A non-blocking poll of the event system.
 * XXX not an actual poll, rather a "dispatch at most once".
379 380 381 382 383 384 385 386 387
 */
int
event_poll(event_handle_t handle)
{
	return internal_event_poll(handle,0,0);
}

/*
 * A blocking poll of the event system, with an optional timeout
388
 * XXX not an actual poll either, rather a "dispatch for awhile".
389 390 391 392 393
 */
int event_poll_blocking(event_handle_t handle, unsigned int timeout)
{
	return internal_event_poll(handle,1,timeout);
}
Mike Hibler's avatar
Mike Hibler committed
394

395 396 397 398 399 400
/*
 * Enter the main loop of the event system, waiting to receive event
 * notifications. Returns non-zero if the operation is successful, 0
 * otherwise.
 */

Ian Murdock's avatar
Ian Murdock committed
401 402 403 404
int
event_main(event_handle_t handle)
{
    if (!handle) {
Ian Murdock's avatar
Ian Murdock committed
405
        ERROR("invalid parameter\n"); 
Ian Murdock's avatar
Ian Murdock committed
406 407 408
        return 0;
    }

409 410 411 412 413
    if (!handle->mainloop) {
        ERROR("multithreaded programs don't need to call event_main\n");
        return 0;
    }

Timothy Stack's avatar
 
Timothy Stack committed
414 415 416 417 418
    if (handle->do_loop) {
	ERROR("loop is already running\n");
	return 0;
    }

Ian Murdock's avatar
Ian Murdock committed
419 420
    TRACE("entering event loop...\n");

Timothy Stack's avatar
 
Timothy Stack committed
421
    handle->do_loop = 1;
422
    if (handle->mainloop(handle->server, &handle->do_loop, &handle->status)) {
423
        ERROR("Event mainloop failed: ");
424
        pubsub_error_fprintf(stderr, &handle->status);
Ian Murdock's avatar
Ian Murdock committed
425 426 427 428 429 430
        return 0;
    }

    return 1;
}

431

Timothy Stack's avatar
 
Timothy Stack committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
int event_stop_main(event_handle_t handle)
{
    if (!handle) {
	ERROR("invalid parameter\n");
	return 0;
    }

    if (!handle->mainloop) {
	ERROR("multithreaded programs do not have a mainloop\n");
	return 0;
    }

    if (!handle->do_loop) {
	ERROR("mainloop is not running\n");
	return 0;
    }

    handle->do_loop = 0;
    
    return 1;
}


455 456 457
/*
 * Send the event notification NOTIFICATION.  NOTIFICATION is
 * allocated by event_notification_alloc, and may optionally
458
 * have attributes added to it by event_notification_put_*.
459
 * Returns non-zero if the operation is successful, 0 otherwise.
Ian Murdock's avatar
Ian Murdock committed
460 461 462 463
 *
 * Note that NOTIFICATION is not deallocated by event_notify.  The
 * caller is responsible for deallocating the notification when it
 * is finished with it.
464 465
 */

Ian Murdock's avatar
Ian Murdock committed
466 467 468
int
event_notify(event_handle_t handle, event_notification_t notification)
{
Ian Murdock's avatar
Ian Murdock committed
469 470
    if (!handle || !notification) {
        ERROR("invalid parameter\n");
Ian Murdock's avatar
Ian Murdock committed
471 472
        return 0;
    }
473 474 475 476
    if (handle->keydata && !notification->has_hmac &&
	event_notification_insert_hmac(handle, notification)) {
        return 0;
    }
Ian Murdock's avatar
Ian Murdock committed
477

Ian Murdock's avatar
Ian Murdock committed
478
    TRACE("sending event notification %p\n", notification);
Ian Murdock's avatar
Ian Murdock committed
479 480

    /* Send notification to Elvin server for routing: */
481 482
    if (handle->notify(handle->server, notification->pubsub_notification,
		       &handle->status)) {
483
        ERROR("could not send event notification: ");
484
        pubsub_error_fprintf(stderr, &handle->status);
Ian Murdock's avatar
Ian Murdock committed
485 486 487 488 489 490
        return 0;
    }

    return 1;
}

491

492 493 494 495 496 497
/*
 * Schedule the event notification NOTIFICATION to be sent at time
 * TIME.  NOTIFICATION is allocated by event_notification_alloc,
 * and may optionally have attributes added to it by
 * event_notification_put_*.  Returns non-zero if the operation
 * is successful, 0 otherwise.
Ian Murdock's avatar
Ian Murdock committed
498 499 500 501
 *
 * This function essentially operates as a deferred event_notify.
 * event_notify sends notifications immediately,
 * whereas event_schedule sends notifications at some later time.
Ian Murdock's avatar
Ian Murdock committed
502 503 504 505
 *
 * Note that NOTIFICATION is not deallocated by event_schedule.
 * The caller is responsible for deallocating the notification
 * when it is finished with it.
506 507 508 509 510 511
 */

int
event_schedule(event_handle_t handle, event_notification_t notification,
               struct timeval *time)
{
Ian Murdock's avatar
Ian Murdock committed
512 513
    if (!handle || !notification || !time) {
        ERROR("invalid parameter\n");
514 515 516
        return 0;
    }

Ian Murdock's avatar
Ian Murdock committed
517 518
    TRACE("scheduling event notification %p to be sent at time (%ld, %ld)\n",
          notification, time->tv_sec, time->tv_usec);
519

520 521 522 523
    /*
     * Add an attribute that signifies its a scheduler operation.
     */
    if (! event_notification_remove(handle, notification, "SCHEDULER") ||
524 525
	! event_notification_put_int32(handle,
				       notification, "SCHEDULER", 1)) {
526
	ERROR("could not add scheduler attribute to notification %p\n",
Ian Murdock's avatar
Ian Murdock committed
527
              notification);
528 529 530 531 532 533 534 535 536 537
        return 0;
    }

    /* Add the time this event should be fired to the notification
       structure: */

    if (event_notification_put_int32(handle, notification, "time_sec",
                                     time->tv_sec)
        == 0)
    {
Ian Murdock's avatar
Ian Murdock committed
538 539
        ERROR("could not add time.tv_sec attribute to notification %p\n",
              notification);
540 541 542 543 544 545 546
        return 0;
    }
    if (event_notification_put_int32(handle, notification, "time_usec",
                                     time->tv_usec)
        == 0)

    {
Ian Murdock's avatar
Ian Murdock committed
547 548
        ERROR("could not add time.tv_usec attribute to notification %p\n",
              notification);
549 550 551 552 553 554 555
        return 0;
    }

    /* Send the event notification: */
    return event_notify(handle, notification);
}

556
/*
557 558 559
 * Allocate an event notification.  The address TUPLE defines who
 * should receive the notification. Returns a pointer to an event
 * notification structure if the operation is successful, 0 otherwise.
560 561
 */

Ian Murdock's avatar
Ian Murdock committed
562
event_notification_t
563
event_notification_alloc(event_handle_t handle, address_tuple_t tuple)
Ian Murdock's avatar
Ian Murdock committed
564
{
565
    event_notification_t notification;
566
    pubsub_notification_t *pubsub_notification;
Ian Murdock's avatar
Ian Murdock committed
567

568
    if (!handle) {
Ian Murdock's avatar
Ian Murdock committed
569
        ERROR("invalid paramater\n");
Ian Murdock's avatar
Ian Murdock committed
570 571 572
        return NULL;
    }

573
    TRACE("allocating notification (tuple=%p)\n", tuple);
Ian Murdock's avatar
Ian Murdock committed
574

Timothy Stack's avatar
 
Timothy Stack committed
575
    notification = xmalloc(sizeof(struct event_notification));
576 577 578 579 580
    pubsub_notification = pubsub_notification_alloc(handle->server,
						    &handle->status);
    if (pubsub_notification == NULL) {
        ERROR("pubsub_notification_alloc failed: ");
        pubsub_error_fprintf(stderr, &handle->status);
Ian Murdock's avatar
Ian Murdock committed
581 582
        return NULL;
    }
583
    notification->pubsub_notification = pubsub_notification;
584 585
    notification->has_hmac = 0;

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    /*
     * Event version number
     */
    if (!event_notification_set_version(handle, notification, "1.0")) {
        ERROR("pubsub_notification_alloc failed to set version number\n");
	event_notification_free(handle, notification);
        return NULL;
    }
#ifdef ELVIN_COMPAT
    if (!event_notification_set_elvincompat(handle, notification)) {
        ERROR("pubsub_notification_alloc failed to set elvin compat\n");
	event_notification_free(handle, notification);
        return NULL;
    }
#endif
#ifdef ELVIN_COMPAT0
    notification->hashtable = elvin_hashtable_alloc(0, &handle->status);
    if (notification->hashtable == NULL) {
        ERROR("pubsub_notification_alloc failed to allocate hashtable\n");
	event_notification_free(handle, notification);
        return NULL;
    }
#endif
609 610
    if (tuple == NULL)
	    return notification;
Ian Murdock's avatar
Ian Murdock committed
611

Ian Murdock's avatar
Ian Murdock committed
612
    TRACE("allocated notification %p\n", notification);
613 614 615 616 617 618
#define EVPUT(name, field) \
({ \
	char *foo = (tuple->field ? tuple->field : ADDRESSTUPLE_ALL); \
	\
	event_notification_put_string(handle, notification, name, foo); \
})
619

620 621 622 623 624 625 626 627
    /* Add the target address stuff to the notification */
    if (!EVPUT("SITE", site) ||
	!EVPUT("EXPT", expt) ||
	!EVPUT("GROUP", group) ||
	!EVPUT("HOST", host) ||
	!EVPUT("OBJTYPE", objtype) ||
	!EVPUT("OBJNAME", objname) ||
	!EVPUT("EVENTTYPE", eventtype) ||
Timothy Stack's avatar
 
Timothy Stack committed
628
	!EVPUT("TIMELINE", timeline) ||
629 630 631
	!event_notification_put_int32(handle,
				      notification, "SCHEDULER",
				      tuple->scheduler)) {
632
	ERROR("could not add attributes to notification %p\n", notification);
Ian Murdock's avatar
Ian Murdock committed
633 634
        return NULL;
    }
635 636 637
    /* Add our address */
    event_notification_set_sender(handle, notification, ipaddr);

Ian Murdock's avatar
Ian Murdock committed
638 639 640
    return notification;
}

641 642

/*
Ian Murdock's avatar
Ian Murdock committed
643
 * Free the event notification NOTIFICATION. Returns non-zero if the
644 645 646
 * operation is successful, 0 otherwise.
 */

Ian Murdock's avatar
Ian Murdock committed
647 648
int
event_notification_free(event_handle_t handle,
649
			event_notification_t notification)
Ian Murdock's avatar
Ian Murdock committed
650
{
Timothy Stack's avatar
 
Timothy Stack committed
651 652 653 654
    if (!notification) {
	return 1;
    }
    
655
    if (!handle || !notification->pubsub_notification) {
Ian Murdock's avatar
Ian Murdock committed
656
        ERROR("invalid parameter\n");
Ian Murdock's avatar
Ian Murdock committed
657 658 659
        return 0;
    }

Ian Murdock's avatar
Ian Murdock committed
660
    TRACE("freeing notification %p\n", notification);
Ian Murdock's avatar
Ian Murdock committed
661

662 663
    pubsub_notification_free(handle->server, notification->pubsub_notification,
			     &handle->status);
664 665 666 667 668
#ifdef ELVIN_COMPAT0
    if (notification->hashtable) {
        elvin_hashtable_free(notification->hashtable);
    }
#endif
669
    free(notification);
Ian Murdock's avatar
Ian Murdock committed
670 671 672 673

    return 1;
}

674 675 676 677 678 679 680 681 682 683
/*
 * Clones (copies) the event notificaion. Returns the copy if successful,
 * or NULL if it is not.
 */
event_notification_t
event_notification_clone(event_handle_t handle,
			 event_notification_t notification)
{
    event_notification_t clone;

684
    if (!handle || !notification || !notification->pubsub_notification) {
685 686 687 688 689 690
        ERROR("invalid parameter\n");
        return 0;
    }

    TRACE("cloning notification %p\n", notification);

Timothy Stack's avatar
 
Timothy Stack committed
691
    clone = xmalloc(sizeof(struct event_notification));
692 693 694 695 696 697
    if (! (clone->pubsub_notification =
	   pubsub_notification_clone(handle->server,
				     notification->pubsub_notification,
				     &handle->status))) {
        ERROR("pubsub_notification_clone failed: ");
        pubsub_error_fprintf(stderr, &handle->status);
698
	free(clone);
699 700
        return 0;
    }
701 702 703 704
#ifdef ELVIN_COMPAT0
    notification->hashtable = elvin_hashtable_clone(notification->hashtable,
						    &handle->status);
#endif
705
    clone->has_hmac = notification->has_hmac;
706 707 708 709

    return clone;
}

710 711

/*
712 713 714 715
 * Get the double attribute with name NAME from the event
 * notification NOTIFICATION.
 * Writes the value of the attribute to *VALUE and returns
 * non-zero if the named attribute is found, 0 otherwise.
716 717
 */

Ian Murdock's avatar
Ian Murdock committed
718
int
719 720 721
event_notification_get_double(event_handle_t handle,
                              event_notification_t notification,
                              char *name, double *value)
Ian Murdock's avatar
Ian Murdock committed
722
{
Ian Murdock's avatar
Ian Murdock committed
723 724
    if (!handle || !notification || !name || !value) {
        ERROR("invalid parameter\n");
725 726 727
        return 0;
    }

728 729
    if (pubsub_notification_get_real64(notification->pubsub_notification,
				       name, value, &handle->status) != 0) {
Ian Murdock's avatar
Ian Murdock committed
730
        ERROR("could not get double attribute \"%s\" from notification %p\n",
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
              name, notification);
        return 0;
    }

    return 1;
}


/*
 * Get the int32 attribute with name NAME from the event
 * notification NOTIFICATION.
 * Writes the value of the attribute to *VALUE and returns
 * non-zero if the named attribute is found, 0 otherwise.
 */

int
event_notification_get_int32(event_handle_t handle,
                             event_notification_t notification,
                             char *name, int32_t *value)
{
Ian Murdock's avatar
Ian Murdock committed
751 752
    if (!handle || !notification || !name || !value) {
        ERROR("invalid parameter\n");
753 754 755
        return 0;
    }

756 757
    if (pubsub_notification_get_int32(notification->pubsub_notification,
				      name, value, &handle->status) != 0) {
Ian Murdock's avatar
Ian Murdock committed
758
        ERROR("could not get int32 attribute \"%s\" from notification %p\n",
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
              name, notification);
        return 0;
    }

    return 1;
}


/*
 * Get the int64 attribute with name NAME from the event
 * notification NOTIFICATION.
 * Writes the value of the attribute to *VALUE and returns
 * non-zero if the named attribute is found, 0 otherwise.
 */

int
event_notification_get_int64(event_handle_t handle,
                             event_notification_t notification,
                             char *name, int64_t *value)
{
Ian Murdock's avatar
Ian Murdock committed
779 780
    if (!handle || !notification || !name || !value) {
        ERROR("invalid parameter\n");
781 782 783
        return 0;
    }

784 785
    if (pubsub_notification_get_int64(notification->pubsub_notification,
				      name, value, &handle->status) != 0) {
Ian Murdock's avatar
Ian Murdock committed
786
        ERROR("could not get int64 attribute \"%s\" from notification %p\n",
787 788 789 790 791 792 793 794
              name, notification);
        return 0;
    }

    return 1;
}


795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
/*
 * Return the length of a attribute with name NAME.
 * Used to dynamically size buffers for the event_notification_get_* calls.
 * Returns the length or -1 on error.
 *
 * Note that we only do this for opaques and strings as the other types
 * all have a "standard" size.
 */

int
event_notification_get_opaque_length(event_handle_t handle,
				     event_notification_t notification,
				     char *name)
{
    char *v;
    int len;

    if (!handle || !notification || !name) {
        ERROR("invalid parameter\n");
        return -1;
    }

    if (pubsub_notification_get_opaque(notification->pubsub_notification,
				       name, &v, &len, &handle->status) != 0) {
        ERROR("could not get opaque attribute \"%s\" from notification %p\n",
              name, notification);
        return -1;
    }

    return len;
}

int
event_notification_get_string_length(event_handle_t handle,
				     event_notification_t notification,
				     char *name)
{
    char *v;

    if (!handle || !notification || !name) {
        ERROR("invalid parameter\n");
        return -1;
    }

    if (pubsub_notification_get_string(notification->pubsub_notification,
				       name, &v, &handle->status) != 0) {
        ERROR("could not get string attribute \"%s\" from notification %p\n",
              name, notification);
        return -1;
    }

    return strlen(v);
}

849 850 851
/*
 * Get the opaque attribute with name NAME from the event
 * notification NOTIFICATION.
Ian Murdock's avatar
Ian Murdock committed
852 853
 * Writes LENGTH bytes into *BUFFER and returns non-zero if the named
 * attribute is found, 0 otherwise.
854 855 856 857 858 859 860
 */

int
event_notification_get_opaque(event_handle_t handle,
                              event_notification_t notification,
                              char *name, void *buffer, int length)
{
861 862
    char *v;
    int len;
863

Ian Murdock's avatar
Ian Murdock committed
864 865
    if (!handle || !notification || !name || !buffer || !length) {
        ERROR("invalid parameter\n");
866 867 868
        return 0;
    }

869 870
    if (pubsub_notification_get_opaque(notification->pubsub_notification,
				       name, &v, &len, &handle->status) != 0) {
Ian Murdock's avatar
Ian Murdock committed
871
        ERROR("could not get opaque attribute \"%s\" from notification %p\n",
872 873 874 875
              name, notification);
        return 0;
    }

876 877 878 879 880 881
    if (len < length) {
	memcpy(buffer, v, len);
	memset(buffer+len, 0, length-len);
    } else {
	memcpy(buffer, v, length);
    }
882 883 884 885 886 887 888 889

    return 1;
}


/*
 * Get the string attribute with name NAME from the event
 * notification NOTIFICATION.
Ian Murdock's avatar
Ian Murdock committed
890 891
 * Writes LENGTH bytes into *BUFFER and returns non-zero if the named
 * attribute is found, 0 otherwise.
892 893 894 895 896 897 898
 */

int
event_notification_get_string(event_handle_t handle,
                              event_notification_t notification,
                              char *name, char *buffer, int length)
{
899
    char *v;
900

Ian Murdock's avatar
Ian Murdock committed
901 902
    if (!handle || !notification || !name || !buffer || !length) {
        ERROR("invalid parameter\n");
903 904 905
        return 0;
    }

906 907
    if (pubsub_notification_get_string(notification->pubsub_notification,
				       name, &v, &handle->status) != 0) {
908
	buffer[0] = '\0';
909 910 911
        return 0;
    }

912
    strncpy(buffer, v, length);
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928

    return 1;
}


/*
 * Add a double attribute with name NAME and value VALUE to the
 * notification NOTIFICATION.
 * Returns non-zero if the operation is successful, 0 otherwise.
 */

int
event_notification_put_double(event_handle_t handle,
                              event_notification_t notification,
                              char *name, double value)
{
Ian Murdock's avatar
Ian Murdock committed
929 930
    if (!handle || !notification || !name) {
        ERROR("invalid parameter\n");
931 932 933
        return 0;
    }

Ian Murdock's avatar
Ian Murdock committed
934 935
    TRACE("adding attribute (name=\"%s\", value=%f) to notification %p\n",
          name, value, notification);
936

937 938
    if (pubsub_notification_add_real64(notification->pubsub_notification,
				       name, value, &handle->status) != 0)
939
    {
940 941
        ERROR("pubsub_notification_add_real64 failed: ");
        pubsub_error_fprintf(stderr, &handle->status);
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
        return 0;
    }
    return 1;
}


/*
 * Add an int32 attribute with name NAME and value VALUE to the
 * notification NOTIFICATION.
 * Returns non-zero if the operation is successful, 0 otherwise.
 */

int
event_notification_put_int32(event_handle_t handle,
                             event_notification_t notification,
957
                             char *name, int value)
958
{
Ian Murdock's avatar
Ian Murdock committed
959 960
    if (!handle || !notification || !name) {
        ERROR("invalid parameter\n");
961 962 963
        return 0;
    }

Ian Murdock's avatar
Ian Murdock committed
964 965
    TRACE("adding attribute (name=\"%s\", value=%d) to notification %p\n",
          name, value, notification);
966

967 968
    if (pubsub_notification_add_int32(notification->pubsub_notification,
				      name, value, &handle->status) != 0)
969
    {
970 971
        ERROR("pubsub_notification_add_int32 failed: ");
        pubsub_error_fprintf(stderr, &handle->status);
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
        return 0;
    }

    return 1;
}


/*
 * Add an int64 attribute with name NAME and value VALUE to the
 * notification NOTIFICATION.
 * Returns non-zero if the operation is successful, 0 otherwise.
 */

int
event_notification_put_int64(event_handle_t handle,
                             event_notification_t notification,
                             char *name, int64_t value)
{
Ian Murdock's avatar
Ian Murdock committed
990 991
    if (!handle || !notification || !name) {
        ERROR("invalid parameter\n");
992 993 994
        return 0;
    }

Ian Murdock's avatar
Ian Murdock committed
995 996
    TRACE("adding attribute (name=\"%s\", value=%lld) to notification %p\n",
          name, value, notification);
997

998 999
    if (pubsub_notification_add_int64(notification->pubsub_notification,
				      name, value, &handle->status) != 0)
1000
    {
1001 1002
        ERROR("pubsub_notification_add_int64 failed: ");
        pubsub_error_fprintf(stderr, &handle->status);
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
        return 0;
    }

    return 1;
}


/*
 * Add an opaque attribute with name NAME to the notification
 * NOTIFICATION. The attribute is stored in the buffer BUFFER
 * with length LENGTH.
 * Returns non-zero if the operation is successful, 0 otherwise.
 */

int
event_notification_put_opaque(event_handle_t handle,
                              event_notification_t notification,
                              char *name, void *buffer, int length)
{
Ian Murdock's avatar
Ian Murdock committed
1022 1023
    if (!handle || !notification || !buffer || !length) {
        ERROR("invalid parameter\n");
1024 1025 1026
        return 0;
    }

Ian Murdock's avatar
Ian Murdock committed
1027 1028
    TRACE("adding attribute (name=\"%s\", value=<opaque>) "
          "to notification %p\n", name, notification);
1029

1030 1031 1032
    if (pubsub_notification_add_opaque(notification->pubsub_notification,
				       name, buffer, length,
				       &handle->status) != 0)
1033
    {
1034 1035
        ERROR("pubsub_notification_add_opaque failed: ");
        pubsub_error_fprintf(stderr, &handle->status);
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
        return 0;
    }

    return 1;
}


/*
 * Add a string attribute with name NAME and value VALUE to the
 * notification NOTIFICATION.
 * Returns non-zero if the operation is successful, 0 otherwise.
 */

int
event_notification_put_string(event_handle_t handle,
                              event_notification_t notification,
                              char *name, char *value)
{
Ian Murdock's avatar
Ian Murdock committed
1054 1055
    if (!handle || !notification || !name || !value) {
        ERROR("invalid parameter\n");
1056 1057 1058
        return 0;
    }

Ian Murdock's avatar
Ian Murdock committed
1059 1060
    TRACE("adding attribute (name=\"%s\", value=\"%s\") to notification %p\n",
          name, value, notification);
1061

1062 1063
    if (pubsub_notification_add_string(notification->pubsub_notification,
				       name, value, &handle->status) != 0)
1064
    {
1065 1066
        ERROR("pubsub_notification_add_string failed: ");
        pubsub_error_fprintf(stderr, &handle->status);
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
        return 0;
    }

    return 1;
}


/*
 * Remove the attribute with name NAME and type TYPE from the event
 * notification NOTIFICATION.  Returns non-zero if the operation is
 * successful, 0 otherwise.
 */

int
event_notification_remove(event_handle_t handle,
                          event_notification_t notification, char *name)
{
Ian Murdock's avatar
Ian Murdock committed
1084 1085
    if (!handle || !notification || !name) {
        ERROR("invalid parameter\n");
1086 1087 1088
        return 0;
    }

Ian Murdock's avatar
Ian Murdock committed
1089 1090
    TRACE("removing attribute \"%s\" from notification %p\n",
          name, notification);
1091

1092 1093 1094 1095
    if (pubsub_notification_remove(notification->pubsub_notification,
				   name, &handle->status) != 0) {
        ERROR("pubsub_notification_remove of %s failed: ", name);
        pubsub_error_fprintf(stderr, &handle->status);
1096
        return 0;
Ian Murdock's avatar
Ian Murdock committed
1097 1098 1099 1100 1101
    }

    return 1;
}

1102

Ian Murdock's avatar
Ian Murdock committed
1103 1104 1105
struct notify_callback_arg {
    event_notify_callback_t callback;
    void *data;
1106
    event_handle_t handle;
Timothy Stack's avatar
 
Timothy Stack committed
1107
    int do_auth;
Ian Murdock's avatar
Ian Murdock committed
1108 1109
};

1110 1111 1112 1113
static void notify_callback(pubsub_handle_t *server,
                            pubsub_subscription_t *subscription,
                            pubsub_notification_t *notification,
			    void *rock);
Ian Murdock's avatar
Ian Murdock committed
1114