| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <time.h> |
| 6 |
#include <xpap.h> |
| 7 |
|
| 8 |
#define XPAMB_CLASS "XPAMB" |
| 9 |
#define XPAMB_NAME "xpamb" |
| 10 |
#define XPAMB_MODE NULL |
| 11 |
#define XPAMB_HELP "xpa message bus:\n" |
| 12 |
|
| 13 |
|
| 14 |
typedef struct mbrec{ |
| 15 |
struct mbrec *next; |
| 16 |
char *name; |
| 17 |
char *info; |
| 18 |
char *buf; |
| 19 |
size_t len; |
| 20 |
time_t t; |
| 21 |
} *MB, MBRec; |
| 22 |
|
| 23 |
|
| 24 |
static MB mbhead=NULL; |
| 25 |
|
| 26 |
|
| 27 |
static char errbuf[SZ_LINE]; |
| 28 |
static int maxhosts=XPA_MAXHOSTS; |
| 29 |
static XPA xpa=NULL; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#ifdef ANSI_FUNC |
| 43 |
static int |
| 44 |
XPAReceiveExit (void *client_data, void *call_data, char *paramlist, |
| 45 |
char *buf, size_t len) |
| 46 |
#else |
| 47 |
static int XPAReceiveExit(client_data, call_data, paramlist, buf, len) |
| 48 |
void *client_data; |
| 49 |
void *call_data; |
| 50 |
char *paramlist; |
| 51 |
char *buf; |
| 52 |
size_t len; |
| 53 |
#endif |
| 54 |
{ |
| 55 |
|
| 56 |
XPAFree(xpa); |
| 57 |
return(0); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
#ifdef ANSI_FUNC |
| 72 |
static int |
| 73 |
XPASendExit (void *client_data, void *call_data, char *paramlist, |
| 74 |
char **buf, size_t *len) |
| 75 |
#else |
| 76 |
static int XPASendExit(client_data, call_data, paramlist, buf, len) |
| 77 |
void *client_data; |
| 78 |
void *call_data; |
| 79 |
char *paramlist; |
| 80 |
char **buf; |
| 81 |
size_t *len; |
| 82 |
#endif |
| 83 |
{ |
| 84 |
|
| 85 |
XPAFree(xpa); |
| 86 |
return(0); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
#ifdef ANSI_FUNC |
| 101 |
static MB |
| 102 |
MBLookup (char *name) |
| 103 |
#else |
| 104 |
static MB MBLookup(name) |
| 105 |
char *name; |
| 106 |
#endif |
| 107 |
{ |
| 108 |
MB cur; |
| 109 |
|
| 110 |
|
| 111 |
if( (name == NULL) || (*name == '\0') ) |
| 112 |
return(NULL); |
| 113 |
|
| 114 |
|
| 115 |
for(cur=mbhead; cur!=NULL; cur=cur->next){ |
| 116 |
if( !strcmp(name, cur->name) ){ |
| 117 |
return(cur); |
| 118 |
} |
| 119 |
} |
| 120 |
return(NULL); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
#ifdef ANSI_FUNC |
| 135 |
static int |
| 136 |
MBDel (MB mb) |
| 137 |
#else |
| 138 |
static int MBDel(mb) |
| 139 |
MB mb; |
| 140 |
#endif |
| 141 |
{ |
| 142 |
MB cur; |
| 143 |
|
| 144 |
if( mb == NULL ){ |
| 145 |
snprintf(errbuf, SZ_LINE, "can't delete invalid xpamb entry"); |
| 146 |
return(-1); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
if( mbhead ){ |
| 151 |
if( mbhead == mb ){ |
| 152 |
mbhead = mbhead->next; |
| 153 |
} |
| 154 |
else{ |
| 155 |
for(cur=mbhead; cur!=NULL; cur=cur->next){ |
| 156 |
if( cur->next == mb ){ |
| 157 |
cur->next = (cur->next)->next; |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
if( mb->name ) xfree(mb->name); |
| 166 |
if( mb->info ) xfree(mb->info); |
| 167 |
if( mb->buf ) xfree(mb->buf); |
| 168 |
|
| 169 |
|
| 170 |
xfree((char *)mb); |
| 171 |
return(0); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
#ifdef ANSI_FUNC |
| 186 |
static int |
| 187 |
MBRemove (char *name) |
| 188 |
#else |
| 189 |
static int MBRemove(name) |
| 190 |
char *name; |
| 191 |
#endif |
| 192 |
{ |
| 193 |
MB mb; |
| 194 |
|
| 195 |
if( (mb=MBLookup(name)) != NULL ) |
| 196 |
return(MBDel(mb)); |
| 197 |
else{ |
| 198 |
snprintf(errbuf, SZ_LINE, "can't delete unknown xpamb entry: %s", name); |
| 199 |
return(-1); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
#ifdef ANSI_FUNC |
| 215 |
static int |
| 216 |
MBAdd (char *name, char *info, char *buf, size_t len, int replace) |
| 217 |
#else |
| 218 |
static int MBAdd(name, info, buf, len, replace) |
| 219 |
char *name; |
| 220 |
char *info; |
| 221 |
char *buf; |
| 222 |
size_t len; |
| 223 |
int replace; |
| 224 |
#endif |
| 225 |
{ |
| 226 |
MB xnew=NULL; |
| 227 |
MB cur; |
| 228 |
|
| 229 |
|
| 230 |
if( (cur = MBLookup(name)) != NULL ){ |
| 231 |
if( !replace ){ |
| 232 |
snprintf(errbuf, SZ_LINE, "xpamb entry already exists: %s", name); |
| 233 |
goto error; |
| 234 |
} |
| 235 |
else |
| 236 |
MBDel(cur); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
if( (xnew = (MB)xcalloc(1, sizeof(MBRec))) == NULL ){ |
| 241 |
snprintf(errbuf, SZ_LINE, "can't allocate memory for xpamb entry: %s", |
| 242 |
name); |
| 243 |
goto error; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
xnew->name = xstrdup(name); |
| 248 |
xnew->info = xstrdup(info); |
| 249 |
if( !(xnew->buf = (char *)xmalloc(len)) ){ |
| 250 |
snprintf(errbuf, SZ_LINE, "can't allocate memory for xpamb buffer: %s", |
| 251 |
name); |
| 252 |
goto error; |
| 253 |
} |
| 254 |
memcpy(xnew->buf, buf, len); |
| 255 |
xnew->len = len; |
| 256 |
xnew->t = time(NULL); |
| 257 |
|
| 258 |
|
| 259 |
if( mbhead == NULL ){ |
| 260 |
mbhead = xnew; |
| 261 |
} |
| 262 |
else{ |
| 263 |
for(cur=mbhead; cur->next!=NULL; cur=cur->next) |
| 264 |
; |
| 265 |
cur->next = xnew; |
| 266 |
} |
| 267 |
return(0); |
| 268 |
|
| 269 |
error: |
| 270 |
if( xnew ) |
| 271 |
xfree(xnew); |
| 272 |
return(-1); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
#ifdef ANSI_FUNC |
| 287 |
static int |
| 288 |
MBInfo (int fd, MB mb) |
| 289 |
#else |
| 290 |
static int MBInfo(fd, mb) |
| 291 |
int fd; |
| 292 |
MB mb; |
| 293 |
#endif |
| 294 |
{ |
| 295 |
MB cur; |
| 296 |
char *tbuf; |
| 297 |
|
| 298 |
if( mb != NULL ){ |
| 299 |
tbuf = (char *)xmalloc(strlen(mb->name) + SZ_LINE); |
| 300 |
snprintf(tbuf, SZ_LINE, "%s\tsize:\t\t%d\n\tcreated:\t%s", |
| 301 |
mb->name, (int)mb->len, ctime(&(mb->t))); |
| 302 |
send(fd, tbuf, strlen(tbuf), 0); |
| 303 |
xfree(tbuf); |
| 304 |
if( mb->info && *(mb->info) ){ |
| 305 |
tbuf = (char *)xmalloc(strlen(mb->info) + SZ_LINE); |
| 306 |
snprintf(tbuf, SZ_LINE, "\tinfo:\t\t%s\n", mb->info); |
| 307 |
send(fd, tbuf, strlen(tbuf), 0); |
| 308 |
xfree(tbuf); |
| 309 |
} |
| 310 |
} |
| 311 |
else{ |
| 312 |
|
| 313 |
for(cur=mbhead; cur!=NULL; cur=cur->next){ |
| 314 |
MBInfo(fd, cur); |
| 315 |
} |
| 316 |
} |
| 317 |
return(0); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
#ifdef ANSI_FUNC |
| 332 |
static int |
| 333 |
MBData (int fd, MB mb) |
| 334 |
#else |
| 335 |
static int MBData(fd, mb) |
| 336 |
int fd; |
| 337 |
MB mb; |
| 338 |
#endif |
| 339 |
{ |
| 340 |
if( mb != NULL ){ |
| 341 |
if( mb->buf && mb->len ){ |
| 342 |
if( send(fd, mb->buf, mb->len, 0) == mb->len ){ |
| 343 |
return(0); |
| 344 |
} |
| 345 |
else{ |
| 346 |
snprintf(errbuf, SZ_LINE, "writing data for xpamb entry: %s", |
| 347 |
mb->name); |
| 348 |
return(-1); |
| 349 |
} |
| 350 |
} |
| 351 |
else{ |
| 352 |
return(0); |
| 353 |
} |
| 354 |
} |
| 355 |
return(0); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
#ifdef ANSI_FUNC |
| 370 |
static int |
| 371 |
MBSendCB (void *client_data, void *call_data, char *paramlist, |
| 372 |
char **buf, size_t *len) |
| 373 |
#else |
| 374 |
static int MBSendCB(client_data, call_data, paramlist, buf, len) |
| 375 |
void *client_data; |
| 376 |
void *call_data; |
| 377 |
char *paramlist; |
| 378 |
char **buf; |
| 379 |
size_t *len; |
| 380 |
#endif |
| 381 |
{ |
| 382 |
XPA xpa = (XPA)call_data; |
| 383 |
MB mb; |
| 384 |
int i; |
| 385 |
int got=0; |
| 386 |
int tp=0; |
| 387 |
int doinfo=0; |
| 388 |
int dodata=0; |
| 389 |
int status=0; |
| 390 |
int fds[1]; |
| 391 |
char tbuf[SZ_LINE]; |
| 392 |
char *errs[XPA_MAXHOSTS]; |
| 393 |
char *names[XPA_MAXHOSTS]; |
| 394 |
|
| 395 |
|
| 396 |
*errbuf = '\0'; |
| 397 |
|
| 398 |
|
| 399 |
if( !paramlist || !*paramlist ){ |
| 400 |
return(MBInfo(xpa_datafd(xpa), NULL)); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
while( word(paramlist, tbuf, &tp) ){ |
| 405 |
if( *tbuf != '-' ) |
| 406 |
break; |
| 407 |
else if( !strcmp(tbuf, "-data") ){ |
| 408 |
dodata++; |
| 409 |
} |
| 410 |
else if( !strcmp(tbuf, "-info") ){ |
| 411 |
doinfo++; |
| 412 |
} |
| 413 |
else{ |
| 414 |
break; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
if( doinfo || dodata ){ |
| 419 |
if( !*tbuf ){ |
| 420 |
snprintf(errbuf, SZ_LINE, "missing xpamb entry name"); |
| 421 |
status = -1; |
| 422 |
} |
| 423 |
if( (mb=MBLookup(tbuf)) != NULL ){ |
| 424 |
if( doinfo ){ |
| 425 |
status = MBInfo(xpa_datafd(xpa), mb); |
| 426 |
} |
| 427 |
if( dodata ){ |
| 428 |
status = MBData(xpa_datafd(xpa), mb); |
| 429 |
} |
| 430 |
} |
| 431 |
else{ |
| 432 |
snprintf(errbuf, SZ_LINE, "unknown xpamb entry: %s", tbuf); |
| 433 |
status = -1; |
| 434 |
} |
| 435 |
} |
| 436 |
else{ |
| 437 |
fds[0] = xpa_datafd(xpa); |
| 438 |
got = XPAGetFd(NULL, tbuf, ¶mlist[tp], NULL, fds, names, errs, |
| 439 |
-maxhosts); |
| 440 |
for(i=0; i<got; i++){ |
| 441 |
if( errs[i] ){ |
| 442 |
if( !*errbuf ) |
| 443 |
strcpy(errbuf, errs[i]); |
| 444 |
status = -1; |
| 445 |
xfree(errs[i]); |
| 446 |
} |
| 447 |
if( names[i] ) xfree(names[i]); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
if( *errbuf ) |
| 453 |
XPAError(xpa, errbuf); |
| 454 |
return(status); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
#ifdef ANSI_FUNC |
| 469 |
static int |
| 470 |
MBRecCB (void *client_data, void *call_data, char *paramlist, |
| 471 |
char *buf, size_t len) |
| 472 |
#else |
| 473 |
static int MBRecCB(client_data, call_data, paramlist, buf, len) |
| 474 |
void *client_data; |
| 475 |
void *call_data; |
| 476 |
char *paramlist; |
| 477 |
char *buf; |
| 478 |
size_t len; |
| 479 |
#endif |
| 480 |
{ |
| 481 |
XPA xpa = (XPA)call_data; |
| 482 |
MB mb; |
| 483 |
int i; |
| 484 |
int got=0; |
| 485 |
int tp=0; |
| 486 |
int rflag=0; |
| 487 |
int status=0; |
| 488 |
char tbuf[SZ_LINE]; |
| 489 |
char xsend[SZ_LINE]; |
| 490 |
char xadd[SZ_LINE]; |
| 491 |
char xinfo[SZ_LINE]; |
| 492 |
char xremove[SZ_LINE]; |
| 493 |
char xtemplate[SZ_LINE]; |
| 494 |
char *errs[XPA_MAXHOSTS]; |
| 495 |
char *names[XPA_MAXHOSTS]; |
| 496 |
|
| 497 |
|
| 498 |
*xsend = '\0'; |
| 499 |
*xadd = '\0'; |
| 500 |
*xinfo = '\0'; |
| 501 |
*xremove = '\0'; |
| 502 |
|
| 503 |
*errbuf = '\0'; |
| 504 |
|
| 505 |
|
| 506 |
while( word(paramlist, tbuf, &tp) ){ |
| 507 |
if( *tbuf != '-' ) |
| 508 |
break; |
| 509 |
else if( !strcmp(tbuf, "-data") ){ |
| 510 |
word(paramlist, xadd, &tp); |
| 511 |
} |
| 512 |
else if( !strcmp(tbuf, "-add") ){ |
| 513 |
word(paramlist, xadd, &tp); |
| 514 |
} |
| 515 |
else if( !strcmp(tbuf, "-replace") ){ |
| 516 |
word(paramlist, xadd, &tp); |
| 517 |
rflag = 1; |
| 518 |
} |
| 519 |
else if( !strcmp(tbuf, "-info") ){ |
| 520 |
word(paramlist, xinfo, &tp); |
| 521 |
} |
| 522 |
else if( !strncmp(tbuf, "-del", 4) ){ |
| 523 |
word(paramlist, xremove, &tp); |
| 524 |
} |
| 525 |
else if( !strcmp(tbuf, "-send") ){ |
| 526 |
word(paramlist, xsend, &tp); |
| 527 |
} |
| 528 |
else |
| 529 |
break; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
if( *tbuf ){ |
| 535 |
strcpy(xtemplate, tbuf); |
| 536 |
|
| 537 |
paramlist = &(paramlist[tp]); |
| 538 |
|
| 539 |
if( *xsend != '\0' ){ |
| 540 |
if( (mb=MBLookup(xsend)) != NULL ){ |
| 541 |
got = XPASet(NULL, xtemplate, paramlist, NULL, mb->buf, mb->len, |
| 542 |
names, errs, maxhosts); |
| 543 |
} |
| 544 |
if( (buf != NULL) && (len>0) ){ |
| 545 |
got = XPASet(NULL, xtemplate, paramlist, NULL, buf, len, |
| 546 |
names, errs, maxhosts); |
| 547 |
} |
| 548 |
} |
| 549 |
else{ |
| 550 |
got = XPASet(NULL, xtemplate, paramlist, NULL, buf, len, |
| 551 |
names, errs, maxhosts); |
| 552 |
} |
| 553 |
for(i=0; i<got; i++){ |
| 554 |
if( errs[i] ){ |
| 555 |
if( !*errbuf ) |
| 556 |
strcpy(errbuf, errs[i]); |
| 557 |
status = -1; |
| 558 |
xfree(errs[i]); |
| 559 |
} |
| 560 |
if( names[i] ) xfree(names[i]); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
if( *xadd !='\0' ){ |
| 566 |
status = MBAdd(xadd, xinfo, buf, len, rflag); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
if( *xremove !='\0' ){ |
| 571 |
status = MBRemove(xremove); |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
if( *errbuf ) |
| 576 |
XPAError(xpa, errbuf); |
| 577 |
return(status); |
| 578 |
} |
| 579 |
|
| 580 |
#ifdef ANSI_FUNC |
| 581 |
int |
| 582 |
main (int argc, char **argv) |
| 583 |
#else |
| 584 |
int main(argc, argv) |
| 585 |
int argc; |
| 586 |
char **argv; |
| 587 |
#endif |
| 588 |
{ |
| 589 |
char *s; |
| 590 |
|
| 591 |
|
| 592 |
if( (argc == 2) && !strcmp(argv[1], "--version") ){ |
| 593 |
fprintf(stderr, "%s\n", XPA_VERSION); |
| 594 |
exit(0); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
maxhosts=XPA_MAXHOSTS; |
| 599 |
if( (s=(char *)getenv("XPA_MAXHOSTS")) != NULL ) |
| 600 |
maxhosts = atoi(s); |
| 601 |
|
| 602 |
|
| 603 |
if( !(xpa=XPANew(XPAMB_CLASS, XPAMB_NAME, XPAMB_HELP, |
| 604 |
MBSendCB, NULL, XPAMB_MODE, MBRecCB, NULL, XPAMB_MODE)) ){ |
| 605 |
fprintf(stderr, "XPA$ERROR: could not create xpamb access point\n"); |
| 606 |
exit(1); |
| 607 |
} |
| 608 |
|
| 609 |
XPACmdAdd(XPAGetReserved (), "-exit", "\texit program", |
| 610 |
XPASendExit, NULL, NULL, XPAReceiveExit, NULL, "fillbuf=false"); |
| 611 |
|
| 612 |
|
| 613 |
XPAMainLoop(); |
| 614 |
return(0); |
| 615 |
} |