1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 148 149 150 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 225 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 279 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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
|
#include "server_config.h"
#include <string.h> /* for memmove */
#include <stdlib.h> /* for exit() */
#include <time.h>
#include "gam_connection.h"
#include "gam_subscription.h"
#include "gam_listener.h"
#include "gam_server.h"
#include "gam_event.h"
#include "gam_protocol.h"
#include "gam_channel.h"
#include "gam_error.h"
#include "gam_pidname.h"
#include "gam_eq.h"
#ifdef GAMIN_DEBUG_API
#include "gam_debugging.h"
#endif
#ifdef ENABLE_INOTIFY
#include "gam_inotify.h"
#endif
#include "fam.h"
/************************************************************************
* *
* Connection data handling *
* *
************************************************************************/
static GList *gamConnList;
struct GamConnData {
GamConnState state; /* the state for the connection */
int fd; /* the file descriptor */
int pid; /* the PID of the remote process */
gchar *pidname; /* The name of the process */
GMainLoop *loop; /* the Glib loop used */
GIOChannel *source; /* the Glib I/O Channel used */
int request_len; /* how many bytes of request are valid */
GAMPacket request; /* the next request being read */
GamListener *listener; /* the listener associated with the connection */
gam_eq_t *eq; /* the event queue */
guint eq_source; /* the event queue GSource id */
};
static void gam_cancel_server_timeout (void);
static const char *
gam_reqtype_to_string (GAMReqType type)
{
switch (type)
{
case GAM_REQ_FILE:
return "MONFILE";
case GAM_REQ_DIR:
return "MONDIR";
case GAM_REQ_CANCEL:
return "CANCEL";
case GAM_REQ_DEBUG:
return "4";
}
return "";
}
/**
* gam_connections_init:
*
* Initialize the connections data layer
*
* Returns 0 on success; -1 on failure
*/
int
gam_connections_init(void)
{
return (0);
}
/**
* gam_connection_exists:
* @conn: the connection
*
* Routine to chech whether a connection still exists
*
* Returns 1 if still registered, 0 otherwise
*/
int
gam_connection_exists(GamConnDataPtr conn)
{
g_assert(conn);
return g_list_find(gamConnList, (gconstpointer) conn) != NULL;
}
/**
* gam_connection_close:
* @conn: the connection
*
* Routine to close a connection and discard the associated data
*
* Returns 0 on success; -1 on error
*/
int
gam_connection_close(GamConnDataPtr conn)
{
g_assert (conn);
/* A valid connection is on gamConnList. */
g_assert(g_list_find(gamConnList, (gconstpointer) conn));
g_assert(conn->source);
/* Kill the queue event source */
if (conn->eq_source != 0)
g_source_remove (conn->eq_source);
/* Flush the event queue */
gam_eq_flush (conn->eq, conn);
/* Kill the event queue */
gam_eq_free (conn->eq);
if (conn->listener != NULL) {
gam_listener_free(conn->listener);
}
#ifdef GAMIN_DEBUG_API
gam_debug_release(conn);
#endif
GAM_DEBUG(DEBUG_INFO, "Closing connection %d\n", conn->fd);
g_io_channel_unref(conn->source);
gamConnList = g_list_remove(gamConnList, conn);
g_assert (!g_list_find(gamConnList, conn));
g_free(conn->pidname);
g_free(conn);
if (gamConnList == NULL && gam_server_use_timeout ())
gam_schedule_server_timeout ();
return (0);
}
/**
* gam_connections_close:
*
* Close all the registered connections
*
* Returns 0 on success; -1 if at least one connection failed to close
*/
int
gam_connections_close(void)
{
int ret = 0;
GList *cur;
while ((cur = g_list_first(gamConnList)) != NULL) {
if (gam_connection_close((GamConnDataPtr) cur->data) < 0)
ret = -1;
}
return (ret);
}
/**
* gam_connection_eq_flush:
*
* Flushes the connections event queue
*
* returns TRUE
*/
static gboolean
gam_connection_eq_flush (gpointer data)
{
gboolean work;
GamConnDataPtr conn = (GamConnDataPtr)data;
if (!conn)
return FALSE;
work = gam_eq_flush (conn->eq, conn);
if (!work)
conn->eq_source = 0;
return work;
}
/**
* gam_connection_new:
* @loop: the Glib loop
* @source: the Glib I/O Channel
*
* Create a new connection data structure.
*
* Returns a new connection structure on success; NULL on error.
*/
GamConnDataPtr
gam_connection_new(GMainLoop *loop, GIOChannel *source)
{
GamConnDataPtr ret;
g_assert(loop);
g_assert(source);
ret = g_malloc0(sizeof(GamConnData));
if (ret == NULL)
return (NULL);
ret->state = GAM_STATE_AUTH;
ret->fd = g_io_channel_unix_get_fd(source);
ret->loop = loop;
ret->source = source;
ret->eq = gam_eq_new ();
ret->eq_source = g_timeout_add (100 /* 100 milisecond */, gam_connection_eq_flush, ret);
gamConnList = g_list_prepend(gamConnList, ret);
gam_cancel_server_timeout ();
GAM_DEBUG(DEBUG_INFO, "Created connection %d\n", ret->fd);
return (ret);
}
/**
* gam_connection_get_fd:
* @conn: a connection data structure.
*
* Get the file descriptor associated with a connection
*
* Returns the file descriptor or -1 in case of error.
*/
int
gam_connection_get_fd(GamConnDataPtr conn)
{
g_assert(conn);
return (conn->fd);
}
/**
* gam_connection_get_pid:
* @conn: a connection data structure.
*
* accessor for the pid associated to the connection
*
* Returns the process identifier or -1 in case of error.
*/
int
gam_connection_get_pid(GamConnDataPtr conn)
{
g_assert(conn);
return (conn->pid);
}
gchar *
gam_connection_get_pidname(GamConnDataPtr conn)
{
g_assert (conn);
return conn->pidname;
}
/**
* gam_connection_set_pid:
* @conn: a connection data structure.
* @pid: the client process id
*
* Set the client process id, this also indicate that authentication was done.
*
* Returns 0 in case of success or -1 in case of error.
*/
int
gam_connection_set_pid(GamConnDataPtr conn, int pid)
{
g_assert(conn);
if (conn->state != GAM_STATE_AUTH) {
GAM_DEBUG(DEBUG_INFO, "Connection in unexpected state: "
"not waiting for authentication\n");
conn->state = GAM_STATE_ERROR;
return (-1);
}
conn->state = GAM_STATE_OKAY;
conn->pid = pid;
conn->pidname = gam_get_pidname (pid);
conn->listener = gam_listener_new(conn, pid);
if (conn->listener == NULL) {
GAM_DEBUG(DEBUG_INFO, "Failed to create listener\n");
conn->state = GAM_STATE_ERROR;
return (-1);
}
return (0);
}
/**
* gam_connection_get_state:
* @conn: a connection
*
* Accessor for the connection state
*
* Returns the connection's connection state
*/
GamConnState
gam_connection_get_state(GamConnDataPtr conn)
{
g_assert(conn);
return (conn->state);
}
/**
* gam_connection_get_data:
* @conn: a connection
* @data: address to store pointer to data
* @size: amount of data available
*
* Get the address and length of the data store for the connection
*
* Returns 0 on success; -1 on failure
*/
int
gam_connection_get_data(GamConnDataPtr conn, char **data, int *size)
{
g_assert(conn);
g_assert(data);
g_assert(size);
*data = (char *) &conn->request + conn->request_len;
*size = sizeof(GAMPacket) - conn->request_len;
return (0);
}
/**
* gam_connection_request:
*
* @conn: connection data structure.
* @req: the request
*
* Process a complete request.
*
* Returns 0 on success; -1 on error
*/
static int
gam_connection_request(GamConnDataPtr conn, GAMPacketPtr req)
{
GamSubscription *sub;
int events;
gboolean is_dir = TRUE;
char byte_save;
int type;
int options;
g_assert(conn);
g_assert(req);
g_assert(conn->state == GAM_STATE_OKAY);
g_assert(conn->fd >= 0);
g_assert(conn->listener);
type = req->type & 0xF;
options = req->type & 0xFFF0;
GAM_DEBUG(DEBUG_INFO, "%s request: from %s, seq %d, type %x options %x\n",
gam_reqtype_to_string (type), conn->pidname, req->seq, type, options);
if (req->pathlen >= MAXPATHLEN)
return (-1);
/*
* zero-terminate the string in the buffer, but keep the byte as
* it may be the first one of the next request.
*/
byte_save = req->path[req->pathlen];
req->path[req->pathlen] = 0;
switch (type) {
case GAM_REQ_FILE:
case GAM_REQ_DIR:
events = GAMIN_EVENT_CHANGED | GAMIN_EVENT_CREATED |
GAMIN_EVENT_DELETED | GAMIN_EVENT_MOVED |
GAMIN_EVENT_EXISTS;
is_dir = (type == GAM_REQ_DIR);
sub = gam_subscription_new(req->path, events, req->seq,
is_dir, options);
gam_subscription_set_listener(sub, conn->listener);
gam_add_subscription(sub);
break;
case GAM_REQ_CANCEL: {
char *path;
int pathlen;
sub = gam_listener_get_subscription_by_reqno(conn->listener,
req->seq);
if (sub == NULL) {
GAM_DEBUG(DEBUG_INFO,
"Cancel: no subscription with reqno %d found\n",
req->seq);
goto error;
}
GAM_DEBUG(DEBUG_INFO, "Cancelling subscription with reqno %d\n",
req->seq);
/* We need to make a copy of sub's path as gam_send_ack
needs it but gam_listener_remove_subscription frees
it. */
path = g_strdup(gam_subscription_get_path(sub));
pathlen = gam_subscription_pathlen(sub);
gam_listener_remove_subscription(conn->listener, sub);
gam_remove_subscription(sub);
#ifdef ENABLE_INOTIFY
if ((gam_inotify_is_running()) && (!gam_exclude_check(path))) {
gam_fs_mon_type type;
type = gam_fs_get_mon_type (path);
if (type != GFS_MT_POLL)
gam_subscription_free(sub);
}
#endif
if (gam_send_ack(conn, req->seq, path, pathlen) < 0) {
GAM_DEBUG(DEBUG_INFO, "Failed to send cancel ack to PID %d\n",
gam_connection_get_pid(conn));
}
g_free(path);
break;
}
case GAM_REQ_DEBUG:
#ifdef GAMIN_DEBUG_API
gam_debug_add(conn, req->path, options);
#else
GAM_DEBUG(DEBUG_INFO, "Unhandled debug request for %s\n",
req->path);
#endif
break;
default:
GAM_DEBUG(DEBUG_INFO, "Unknown request type %d for %s\n",
type, req->path);
goto error;
}
req->path[req->pathlen] = byte_save;
return (0);
error:
req->path[req->pathlen] = byte_save;
return (-1);
}
/**
* gam_connection_data:
* @conn: the connection data structure
* @len: the amount of data added to the request buffer
*
* When receiving data, it should be read into an internal buffer
* retrieved using gam_connection_get_data. After receiving some
* incoming data, call this to process the data.
*
* Returns 0 in case of success, -1 in case of error
*/
int
gam_connection_data(GamConnDataPtr conn, int len)
{
GAMPacketPtr req;
g_assert(conn);
g_assert(len >= 0);
g_assert(conn->request_len >= 0);
g_assert(len + conn->request_len <= (int) sizeof(GAMPacket));
conn->request_len += len;
req = &conn->request;
/*
* loop processing all complete requests available in conn->request
*/
while (1) {
if (conn->request_len < (int) GAM_PACKET_HEADER_LEN) {
/*
* we don't have enough data to check the current request
* keep it as a pending incomplete request and wait for more.
*/
break;
}
/* check the packet total length */
if (req->len > sizeof(GAMPacket)) {
GAM_DEBUG(DEBUG_INFO, "malformed request: invalid length %d\n",
req->len);
return (-1);
}
/* check the version */
if (req->version != GAM_PROTO_VERSION) {
GAM_DEBUG(DEBUG_INFO, "unsupported version %d\n", req->version);
return (-1);
}
if (GAM_REQ_CANCEL != req->type) {
/* double check pathlen and total length */
if ((req->pathlen <= 0) || (req->pathlen > MAXPATHLEN)) {
GAM_DEBUG(DEBUG_INFO,
"malformed request: invalid path length %d\n",
req->pathlen);
return (-1);
}
}
if (req->pathlen + GAM_PACKET_HEADER_LEN != req->len) {
GAM_DEBUG(DEBUG_INFO,
"malformed request: invalid packet sizes: %d %d\n",
req->len, req->pathlen);
return (-1);
}
/* Check the type of the request: TODO !!! */
if (conn->request_len < req->len) {
/*
* the current request is incomplete, wait for the rest.
*/
break;
}
if (gam_connection_request(conn, req) < 0) {
GAM_DEBUG(DEBUG_INFO, "gam_connection_request() failed\n");
return (-1);
}
/*
* process any remaining request piggy-back'ed on the same packet
*/
conn->request_len -= req->len;
if (conn->request_len == 0)
break;
#if defined(__i386__) || defined(__x86_64__)
req = (void *) req + req->len;
#else
memmove(&conn->request, (void *)req + req->len, conn->request_len);
#endif
}
if ((conn->request_len > 0) && (req != &conn->request))
memmove(&conn->request, req, conn->request_len);
return (0);
}
/**
* gam_send_event:
* @conn: the connection
* @event: the event type
* @path: the path
*
* Send an event over a connection
*
* Returns 0 on success; -1 on failure
*/
int
gam_send_event(GamConnDataPtr conn, int reqno, int event,
const char *path, int len)
{
GAMPacket req;
size_t tlen;
int ret;
int type;
g_assert(conn);
g_assert(conn->fd >= 0);
g_assert(path);
g_assert(path[len] == '\0');
if (len >= MAXPATHLEN) {
GAM_DEBUG(DEBUG_INFO, "File path too long %s\n", path);
return (-1);
}
/*
* Convert between Gamin/Marmot internal values and FAM ones.
*/
switch (event) {
case GAMIN_EVENT_CHANGED:
type = FAMChanged;
break;
case GAMIN_EVENT_CREATED:
type = FAMCreated;
break;
case GAMIN_EVENT_DELETED:
type = FAMDeleted;
break;
case GAMIN_EVENT_MOVED:
type = FAMMoved;
break;
case GAMIN_EVENT_EXISTS:
type = FAMExists;
break;
case GAMIN_EVENT_ENDEXISTS:
type = FAMEndExist;
break;
#ifdef GAMIN_DEBUG_API
case 50:
type = 50 + reqno;
break;
#endif
default:
GAM_DEBUG(DEBUG_INFO, "Unknown event type %d\n", event);
return (-1);
}
GAM_DEBUG(DEBUG_INFO, "Event to %s : %d, %d, %s %s\n", conn->pidname,
reqno, type, path, gam_event_to_string(event));
/*
* prepare the packet
*/
tlen = GAM_PACKET_HEADER_LEN + len;
/* We use only local socket so no need for network byte order conversion */
req.len = (unsigned short) tlen;
req.version = GAM_PROTO_VERSION;
req.seq = reqno;
req.type = (unsigned short) type;
req.pathlen = len;
memcpy(req.path, path, len);
ret = gam_client_conn_write(conn->source, conn->fd, (gpointer) &req, tlen);
if (!ret) {
GAM_DEBUG(DEBUG_INFO, "Failed to send event to %s\n", conn->pidname);
return (-1);
}
return (0);
}
/**
* gam_queue_event:
* @conn: the connection
* @event: the event type
* @path: the path
*
* Queue an event to be sent over a connection within the next second.
* If an identical event is found at the tail of the event queue
* no event will be queued.
*/
void
gam_queue_event(GamConnDataPtr conn, int reqno, int event,
const char *path, int len)
{
g_assert (conn);
g_assert (conn->eq);
gam_eq_queue (conn->eq, reqno, event, path, len);
if (!conn->eq_source)
conn->eq_source = g_timeout_add (100 /* 100 milisecond */, gam_connection_eq_flush, conn);
}
/**
* gam_send_ack:
* @conn: the connection data
* @path: the file/directory path
*
* Emit an acknowledge event on the connection
*
* Returns 0 on success; -1 on failure
*/
int
gam_send_ack(GamConnDataPtr conn, int reqno,
const char *path, int len)
{
GAMPacket req;
size_t tlen;
int ret;
g_assert(conn);
g_assert(conn->fd >= 0);
g_assert(path);
g_assert(len > 0);
g_assert(path[len] == '\0');
if (len >= MAXPATHLEN) {
GAM_DEBUG(DEBUG_INFO,
"path (%s)'s length (%d) exceeds MAXPATHLEN (%d)\n",
path, len, MAXPATHLEN);
return (-1);
}
GAM_DEBUG(DEBUG_INFO, "Event to %s: %d, %d, %s\n", conn->pidname,
reqno, FAMAcknowledge, path);
/*
* prepare the packet
*/
tlen = GAM_PACKET_HEADER_LEN + len;
/* We only use local sockets so no need for network byte order
conversion */
req.len = (unsigned short) tlen;
req.version = GAM_PROTO_VERSION;
req.seq = reqno;
req.type = FAMAcknowledge;
req.pathlen = len;
memcpy(req.path, path, len);
ret = gam_client_conn_write(conn->source, conn->fd, (gpointer) &req, tlen);
if (!ret) {
GAM_DEBUG(DEBUG_INFO, "Failed to send event to %s\n", conn->pidname);
return (-1);
}
return (0);
}
/************************************************************************
* *
* Automatic exit handling *
* *
************************************************************************/
#define MAX_IDLE_TIMEOUT_MSEC (30*1000) /* 30 seconds */
static guint server_timeout_id = 0;
/**
* gam_connections_check:
*
* This function can be called periodically by e.g. g_timeout_add and
* shuts the server down if there have been no outstanding connections
* for a while.
*/
static gboolean
gam_connections_check(void)
{
server_timeout_id = 0;
if (gamConnList == NULL) {
GAM_DEBUG(DEBUG_INFO, "Exiting on timeout\n");
gam_shutdown();
exit(0);
}
return (FALSE);
}
static void
gam_cancel_server_timeout (void)
{
if (server_timeout_id)
g_source_remove (server_timeout_id);
server_timeout_id = 0;
}
void
gam_schedule_server_timeout (void)
{
gam_cancel_server_timeout ();
server_timeout_id =
g_timeout_add(MAX_IDLE_TIMEOUT_MSEC, (GSourceFunc) gam_connections_check, NULL);
}
/**
* gam_connections_debug:
*
* Calling this function generate debugging informations about the set
* of existing connections.
*/
void
gam_connections_debug(void)
{
#ifdef GAM_DEBUG_ENABLED
GamConnDataPtr conn;
GList *cur;
if (!gam_debug_active)
return;
if (gamConnList == NULL) {
GAM_DEBUG(DEBUG_INFO, "No active connections\n");
return;
}
for (cur = gamConnList; cur; cur = g_list_next(cur)) {
conn = (GamConnDataPtr) cur->data;
if (conn == NULL) {
GAM_DEBUG(DEBUG_INFO, "Error: connection with no data\n");
} else {
const char *state = "unknown";
switch (conn->state) {
case GAM_STATE_ERROR:
state = "error";
break;
case GAM_STATE_AUTH:
state = "need auth";
break;
case GAM_STATE_OKAY:
state = "okay";
break;
case GAM_STATE_CLOSED:
state = "closed";
break;
}
GAM_DEBUG(DEBUG_INFO,
"Connection fd %d to %s: state %s, %d read\n",
conn->fd, conn->pidname, state, conn->request_len);
gam_listener_debug(conn->listener);
}
}
#endif
}
|