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 789 790 791 792 793 794 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 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
|
/* -*-c-*- */
/* Copyright (C) 2003 Olivier Chapuis
* some code taken from the xsm: Copyright 1993, 1998 The Open Group */
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see: <http://www.gnu.org/licenses/>
*/
/* A set of functions for implementing a dummy sm. The code is based on xsm */
/* ---------------------------- included header files ---------------------- */
#include "config.h"
#include <stdio.h>
#include <fcntl.h>
#ifdef HAVE_GETPWUID
#include <pwd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include "fvwmlib.h"
#include "System.h"
#include "flist.h"
#include "fsm.h"
/* #define FVWM_DEBUG_FSM */
/* ---------------------------- local definitions -------------------------- */
/* ---------------------------- local macros ------------------------------- */
/* ---------------------------- imports ------------------------------------ */
/* ---------------------------- included code files ------------------------ */
/* ---------------------------- local types -------------------------------- */
typedef struct
{
FIceConn ice_conn;
int fd;
} fsm_ice_conn_t;
/* ---------------------------- forward declarations ----------------------- */
/* ---------------------------- local variables ---------------------------- */
static FIceAuthDataEntry *sauthDataEntries = NULL;
static FIceIOErrorHandler prev_handler;
static FIceListenObj *slistenObjs;
static char *addAuthFile = NULL;
static char *remAuthFile = NULL;
static flist *fsm_ice_conn_list = NULL;
static flist *pending_ice_conn_list = NULL;
static int numTransports = 0;
static char *networkIds = NULL;
static int *ice_fd = NULL;
static char *module_name = NULL;
static flist *running_list = NULL;
static Bool fsm_init_succeed = False;
static Atom _XA_WM_CLIENT_LEADER = None;
static Atom _XA_SM_CLIENT_ID = None;
/* ---------------------------- exported variables (globals) --------------- */
/* ---------------------------- local functions ---------------------------- */
/*
* client list stuff
*/
static
void free_fsm_client(fsm_client_t *client)
{
if (!SessionSupport)
{
return;
}
if (client->clientId)
{
free(client->clientId);
}
free(client);
}
/*
* auth stuff
*/
static void
fprintfhex(register FILE *fp, unsigned int len, char *cp)
{
static const char hexchars[] = "0123456789abcdef";
for (; len > 0; len--, cp++) {
unsigned char s = *cp;
putc(hexchars[s >> 4], fp);
putc(hexchars[s & 0x0f], fp);
}
}
/*
* Host Based Authentication Callback. This callback is invoked if
* the connecting client can't offer any authentication methods that
* we can accept. We can accept/reject based on the hostname.
*/
static
Bool HostBasedAuthProc(char *hostname)
{
return (0); /* For now, we don't support host based authentication */
}
/*
* We use temporary files which contain commands to add/remove entries from
* the .ICEauthority file.
*/
static void
write_iceauth(FILE *addfp, FILE *removefp, FIceAuthDataEntry *entry)
{
if (!SessionSupport)
{
return;
}
fprintf(addfp,
"add %s \"\" %s %s ", entry->protocol_name, entry->network_id,
entry->auth_name);
fprintfhex(addfp, entry->auth_data_length, entry->auth_data);
fprintf(addfp, "\n");
fprintf(
removefp,
"remove protoname=%s protodata=\"\" netid=%s authname=%s\n",
entry->protocol_name, entry->network_id, entry->auth_name);
}
static char *
unique_filename(char *path, char *prefix, int *pFd)
{
char *tempFile;
xasprintf(&tempFile, "%s/%sXXXXXX", path, prefix);
*pFd = fvwm_mkstemp(tempFile);
if (*pFd == -1)
{
free(tempFile);
tempFile = NULL;
}
return tempFile;
}
/*
* Provide authentication data to clients that wish to connect
*/
#define MAGIC_COOKIE_LEN 16
static
Status SetAuthentication(
int count,FIceListenObj *listenObjs,FIceAuthDataEntry **authDataEntries)
{
FILE *addfp = NULL;
FILE *removefp = NULL;
char *path;
int original_umask;
char command[256];
int i;
int fd;
if (!SessionSupport)
{
return 0;
}
original_umask = umask (0077); /* disallow non-owner access */
path = (char *)getenv("SM_SAVE_DIR");
if (!path)
{
path = getenv("HOME");
}
#ifdef HAVE_GETPWUID
if (!path)
{
struct passwd *pwd;
pwd = getpwuid(getuid());
if (pwd)
{
path = pwd->pw_dir;
}
}
#endif
if (!path)
{
path = ".";
}
if ((addAuthFile = unique_filename (path, ".fsm-", &fd)) == NULL)
{
goto bad;
}
if (!(addfp = fdopen(fd, "wb")))
{
goto bad;
}
if ((remAuthFile = unique_filename (path, ".fsm-", &fd)) == NULL)
{
goto bad;
}
if (!(removefp = fdopen(fd, "wb")))
{
goto bad;
}
*authDataEntries = fxmalloc(count * 2 * sizeof (FIceAuthDataEntry));
for (i = 0; i < count * 2; i += 2)
{
(*authDataEntries)[i].network_id =
FIceGetListenConnectionString(listenObjs[i/2]);
(*authDataEntries)[i].protocol_name = "ICE";
(*authDataEntries)[i].auth_name = "MIT-MAGIC-COOKIE-1";
(*authDataEntries)[i].auth_data =
FIceGenerateMagicCookie (MAGIC_COOKIE_LEN);
(*authDataEntries)[i].auth_data_length = MAGIC_COOKIE_LEN;
(*authDataEntries)[i+1].network_id =
FIceGetListenConnectionString(listenObjs[i/2]);
(*authDataEntries)[i+1].protocol_name = "XSMP";
(*authDataEntries)[i+1].auth_name = "MIT-MAGIC-COOKIE-1";
(*authDataEntries)[i+1].auth_data =
FIceGenerateMagicCookie (MAGIC_COOKIE_LEN);
(*authDataEntries)[i+1].auth_data_length = MAGIC_COOKIE_LEN;
write_iceauth(addfp, removefp, &(*authDataEntries)[i]);
write_iceauth(addfp, removefp, &(*authDataEntries)[i+1]);
FIceSetPaAuthData(2, &(*authDataEntries)[i]);
FIceSetHostBasedAuthProc(listenObjs[i/2], HostBasedAuthProc);
}
fclose(addfp);
fclose(removefp);
umask (original_umask);
snprintf (command, sizeof(command), "iceauth source %s", addAuthFile);
{
int n;
n = system(command);
(void)n;
}
unlink (addAuthFile);
return 1;
bad:
if (addfp)
{
fclose (addfp);
}
if (removefp)
{
fclose (removefp);
}
if (addAuthFile)
{
unlink (addAuthFile);
free (addAuthFile);
}
if (remAuthFile)
{
unlink (remAuthFile);
free (remAuthFile);
}
return 0;
}
static
void FreeAuthenticationData(int count, FIceAuthDataEntry *authDataEntries)
{
/* Each transport has entries for ICE and XSMP */
char command[256];
int i;
if (!SessionSupport)
{
return;
}
for (i = 0; i < count * 2; i++)
{
free(authDataEntries[i].network_id);
free(authDataEntries[i].auth_data);
}
free ((char *) authDataEntries);
snprintf (command, sizeof(command), "iceauth source %s", remAuthFile);
{
int n;
n = system(command);
(void)n;
}
unlink (remAuthFile);
free (addAuthFile);
free (remAuthFile);
}
/*
* ice stuff
*/
static
void MyIoErrorHandler(FIceConn ice_conn)
{
if (!SessionSupport)
{
return;
}
if (prev_handler)
{
(*prev_handler) (ice_conn);
}
}
static
void InstallIOErrorHandler(void)
{
FIceIOErrorHandler default_handler;
if (!SessionSupport)
{
return;
}
prev_handler = FIceSetIOErrorHandler(NULL);
default_handler = FIceSetIOErrorHandler(MyIoErrorHandler);
if (prev_handler == default_handler)
{
prev_handler = NULL;
}
}
static void
CloseListeners(void)
{
if (!SessionSupport)
{
return;
}
FIceFreeListenObjs(numTransports, slistenObjs);
}
static
void ice_watch_fd(
FIceConn conn, FIcePointer client_data, Bool opening,
FIcePointer *watch_data)
{
fsm_ice_conn_t *fice_conn;
if (!SessionSupport)
{
return;
}
if (opening)
{
fice_conn = fxmalloc(sizeof(fsm_ice_conn_t));
fice_conn->ice_conn = conn;
fice_conn->fd = FIceConnectionNumber(conn);
*watch_data = (FIcePointer) fice_conn;
fsm_ice_conn_list =
flist_append_obj(fsm_ice_conn_list, fice_conn);
fcntl(fice_conn->fd, F_SETFD, FD_CLOEXEC);
}
else
{
fice_conn = (fsm_ice_conn_t *)*watch_data;
fsm_ice_conn_list =
flist_remove_obj(fsm_ice_conn_list, fice_conn);
free(fice_conn);
}
}
/*
* Session Manager callbacks
*/
static
Status RegisterClientProc(
FSmsConn smsConn, FSmPointer managerData, char *previousId)
{
fsm_client_t *client = (fsm_client_t *) managerData;
char *id;
int send_save = 0;
if (!SessionSupport)
{
return 0;
}
#ifdef FVWM_DEBUG_FSM
fvwm_debug(__func__,
"[%s][RegisterClientProc] On FIceConn fd = %d, received "
"REGISTER CLIENT [Previous Id = %s]\n", module_name,
FIceConnectionNumber (client->ice_conn),
previousId ? previousId : "NULL");
#endif
/* ignore previousID!! (we are dummy) */
id = FSmsGenerateClientID(smsConn);
if (!FSmsRegisterClientReply(smsConn, id))
{
/* cannot happen ? */
if (id)
{
free(id);
}
fvwm_debug(__func__,
"[%s][RegisterClientProc] ERR -- fail to register "
"client", module_name);
return 0;
}
#ifdef FVWM_DEBUG_FSM
fvwm_debug(__func__,
"[%s][RegisterClientProc] On FIceConn fd = %d, sent "
"REGISTER CLIENT REPLY [Client Id = %s]\n",
module_name, FIceConnectionNumber (client->ice_conn), id);
#endif
client->clientId = id;
/* client->clientHostname = FSmsClientHostName (smsConn); */
/* we are dummy ... do not do that */
if (send_save)
{
FSmsSaveYourself(
smsConn, FSmSaveLocal, False, FSmInteractStyleNone,
False);
}
return 1;
}
static void
InteractRequestProc(FSmsConn smsConn, FSmPointer managerData, int dialogType)
{
if (!SessionSupport)
{
return;
}
/* no intercation! */
#if 0
FSmsInteract (client->smsConn);
#endif
}
static void
InteractDoneProc(FSmsConn smsConn, FSmPointer managerData, Bool cancelShutdown)
{
/* no intercation! */
}
static void
SaveYourselfReqProc(FSmsConn smsConn, FSmPointer managerData, int saveType,
Bool shutdown, int interactStyle, Bool fast, Bool global)
{
/* no session to save */
}
static
void SaveYourselfPhase2ReqProc(FSmsConn smsConn, FSmPointer managerData)
{
fsm_client_t *client;
if (!SessionSupport)
{
return;
}
client = (fsm_client_t *)managerData;
SUPPRESS_UNUSED_VAR_WARNING(client);
FSmsSaveYourselfPhase2(client->smsConn);
}
static void
SaveYourselfDoneProc(FSmsConn smsConn, FSmPointer managerData, Bool success)
{
fsm_client_t *client;
if (!SessionSupport)
{
return;
}
client = (fsm_client_t *) managerData;
SUPPRESS_UNUSED_VAR_WARNING(client);
FSmsSaveComplete(client->smsConn);
}
static void
CloseDownClient(fsm_client_t *client)
{
if (!SessionSupport)
{
return;
}
#ifdef FVWM_DEBUG_FSM
fvwm_debug(__func__, "[%s][CloseDownClient] ICE Connection closed, "
"FIceConn fd = %d\n", module_name,
FIceConnectionNumber (client->ice_conn));
#endif
FSmsCleanUp(client->smsConn);
FIceSetShutdownNegotiation(client->ice_conn, False);
if ((!FIceCloseConnection(client->ice_conn)) != FIceClosedNow)
{
/* do not care */
}
client->ice_conn = NULL;
client->smsConn = NULL;
running_list = flist_remove_obj(running_list, client);
free_fsm_client(client);
}
static void
CloseConnectionProc(FSmsConn smsConn, FSmPointer managerData,
int count, char **reasonMsgs)
{
fsm_client_t *client = (fsm_client_t *) managerData;
if (!SessionSupport)
{
return;
}
FSmFreeReasons(count, reasonMsgs);
CloseDownClient(client);
}
static
void SetPropertiesProc(
FSmsConn smsConn, FSmPointer managerData, int numProps, FSmProp **props)
{
int i;
if (!SessionSupport)
{
return;
}
for (i = 0; i < numProps; i++)
{
FSmFreeProperty(props[i]);
}
free ((char *) props);
}
static
void DeletePropertiesProc(
FSmsConn smsConn, FSmPointer managerData, int numProps, char **propNames)
{
int i;
if (!SessionSupport)
{
return;
}
for (i = 0; i < numProps; i++)
{
free (propNames[i]);
}
free ((char *) propNames);
}
static
void GetPropertiesProc(FSmsConn smsConn, FSmPointer managerData)
{
}
static Status
NewClientProc(
FSmsConn smsConn, FSmPointer managerData, unsigned long *maskRet,
FSmsCallbacks *callbacksRet, char **failureReasonRet)
{
fsm_client_t *nc;
if (!SessionSupport)
{
return 0;
}
nc = fxmalloc(sizeof (fsm_client_t));
*maskRet = 0;
nc->smsConn = smsConn;
nc->ice_conn = FSmsGetIceConnection(smsConn);
nc->clientId = NULL;
running_list = flist_append_obj(running_list, nc);
#ifdef FVWM_DEBUG_FSM
fvwm_debug(__func__,
"[%s][NewClientProc] On FIceConn fd = %d, client "
"set up session mngmt protocol\n", module_name,
FIceConnectionNumber (nc->ice_conn));
#endif
/*
* Set up session manager callbacks.
*/
*maskRet |= FSmsRegisterClientProcMask;
callbacksRet->register_client.callback = RegisterClientProc;
callbacksRet->register_client.manager_data = (FSmPointer) nc;
*maskRet |= FSmsInteractRequestProcMask;
callbacksRet->interact_request.callback = InteractRequestProc;
callbacksRet->interact_request.manager_data = (FSmPointer) nc;
*maskRet |= FSmsInteractDoneProcMask;
callbacksRet->interact_done.callback = InteractDoneProc;
callbacksRet->interact_done.manager_data = (FSmPointer) nc;
*maskRet |= FSmsSaveYourselfRequestProcMask;
callbacksRet->save_yourself_request.callback = SaveYourselfReqProc;
callbacksRet->save_yourself_request.manager_data = (FSmPointer) nc;
*maskRet |= FSmsSaveYourselfP2RequestProcMask;
callbacksRet->save_yourself_phase2_request.callback =
SaveYourselfPhase2ReqProc;
callbacksRet->save_yourself_phase2_request.manager_data =
(FSmPointer) nc;
*maskRet |= FSmsSaveYourselfDoneProcMask;
callbacksRet->save_yourself_done.callback = SaveYourselfDoneProc;
callbacksRet->save_yourself_done.manager_data = (FSmPointer) nc;
*maskRet |= FSmsCloseConnectionProcMask;
callbacksRet->close_connection.callback = CloseConnectionProc;
callbacksRet->close_connection.manager_data = (FSmPointer) nc;
*maskRet |= FSmsSetPropertiesProcMask;
callbacksRet->set_properties.callback = SetPropertiesProc;
callbacksRet->set_properties.manager_data = (FSmPointer) nc;
*maskRet |= FSmsDeletePropertiesProcMask;
callbacksRet->delete_properties.callback = DeletePropertiesProc;
callbacksRet->delete_properties.manager_data = (FSmPointer) nc;
*maskRet |= FSmsGetPropertiesProcMask;
callbacksRet->get_properties.callback = GetPropertiesProc;
callbacksRet->get_properties.manager_data = (FSmPointer) nc;
return 1;
}
/*
*
*/
static
void CompletNewConnectionMsg(void)
{
flist *l = pending_ice_conn_list;
FIceConn ice_conn;
FIceAcceptStatus cstatus;
if (!SessionSupport)
{
return;
}
while(l != NULL)
{
ice_conn = (FIceConn)l->object;
cstatus = (FIceAcceptStatus)FIceConnectionStatus(ice_conn);
if (cstatus == (int)FIceConnectPending)
{
l = l->next;
}
else if (cstatus == (int)FIceConnectAccepted)
{
l = l->next;
pending_ice_conn_list =
flist_remove_obj(
pending_ice_conn_list,
ice_conn);
#ifdef FVWM_DEBUG_FSM
char *connstr;
connstr = FIceConnectionString (ice_conn);
fvwm_debug(__func__,
"[%s][NewConnection] ICE Connection "
"opened by client, FIceConn fd = %d, Accept at "
"networkId %s\n", module_name,
FIceConnectionNumber (ice_conn), connstr);
free (connstr);
#endif
}
else
{
if (FIceCloseConnection (ice_conn) != FIceClosedNow)
{
/* don't care */
}
pending_ice_conn_list =
flist_remove_obj(
pending_ice_conn_list,
ice_conn);
#ifdef FVWM_DEBUG_FSM
if (cstatus == FIceConnectIOError)
{
fvwm_debug(__func__,
"[%s][NewConnection] IO error "
"opening ICE Connection!\n",
module_name);
}
else
{
fvwm_debug(__func__,
"[%s][NewConnection] ICE "
"Connection rejected!\n",
module_name);
}
#endif
}
}
}
static
void NewConnectionMsg(int i)
{
FIceConn ice_conn;
FIceAcceptStatus status;
if (!SessionSupport)
{
return;
}
SUPPRESS_UNUSED_VAR_WARNING(status);
ice_conn = FIceAcceptConnection(slistenObjs[i], &status);
#ifdef FVWM_DEBUG_FSM
fvwm_debug(__func__, "[%s][NewConnection] %i\n", module_name, i);
#endif
if (!ice_conn)
{
#ifdef FVWM_DEBUG_FSM
fvwm_debug(__func__, "[%s][NewConnection] "
"FIceAcceptConnection failed\n", module_name);
#endif
}
else
{
pending_ice_conn_list =
flist_append_obj(pending_ice_conn_list, ice_conn);
CompletNewConnectionMsg();
}
}
static
void ProcessIceMsg(fsm_ice_conn_t *fic)
{
FIceProcessMessagesStatus status;
if (!SessionSupport)
{
return;
}
#ifdef FVWM_DEBUG_FSM
fvwm_debug(__func__, "[%s][ProcessIceMsg] %i\n", module_name,
(int)fic->fd);
#endif
status = FIceProcessMessages(fic->ice_conn, NULL, NULL);
if (status == FIceProcessMessagesIOError)
{
flist *cl;
int found = 0;
#ifdef FVWM_DEBUG_FSM
fvwm_debug(__func__,
"[%s][ProcessIceMsg] IO error on connection\n",
module_name);
#endif
for (cl = running_list; cl; cl = cl->next)
{
fsm_client_t *client = (fsm_client_t *) cl->object;
if (client->ice_conn == fic->ice_conn)
{
CloseDownClient (client);
found = 1;
break;
}
}
if (!found)
{
/*
* The client must have disconnected before it was added
* to the session manager's running list (i.e. before the
* NewClientProc callback was invoked).
*/
FIceSetShutdownNegotiation (fic->ice_conn, False);
if ((!FIceCloseConnection(fic->ice_conn)) != FIceClosedNow)
{
/* do not care */
}
}
}
}
/*
* proxy stuff
*/
static
char *GetClientID(Display *dpy, Window window)
{
char *client_id = NULL;
Window client_leader = None;
XTextProperty tp;
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop = NULL;
if (!SessionSupport)
{
return NULL;
}
if (!_XA_WM_CLIENT_LEADER)
{
_XA_WM_CLIENT_LEADER = XInternAtom(
dpy, "WM_CLIENT_LEADER", False);
}
if (!_XA_SM_CLIENT_ID)
{
_XA_SM_CLIENT_ID = XInternAtom(dpy, "SM_CLIENT_ID", False);
}
if (XGetWindowProperty(
dpy, window, _XA_WM_CLIENT_LEADER, 0L, 1L, False,
AnyPropertyType, &actual_type, &actual_format, &nitems,
&bytes_after, &prop) == Success)
{
if (actual_type == XA_WINDOW && actual_format == 32 &&
nitems == 1 && bytes_after == 0)
{
client_leader = (Window)(*(long *)prop);
}
}
if (!client_leader)
{
client_leader = window;
}
if (client_leader)
{
if (XGetTextProperty(dpy, client_leader, &tp, _XA_SM_CLIENT_ID))
{
if (tp.encoding == XA_STRING && tp.format == 8 &&
tp.nitems != 0)
{
client_id = (char *) tp.value;
}
}
}
if (prop)
{
XFree (prop);
}
return client_id;
}
static
void set_session_manager(Display *dpy, Window window, char *sm)
{
Window client_leader = None;
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop = NULL;
char *dummy_id;
static Atom _XA_SESSION_MANAGER = None;
if (!SessionSupport)
{
return;
}
dummy_id = "0";
if (!_XA_SESSION_MANAGER)
{
_XA_SESSION_MANAGER = XInternAtom (
dpy, "SESSION_MANAGER", False);
}
if (!_XA_WM_CLIENT_LEADER)
{
_XA_WM_CLIENT_LEADER = XInternAtom(
dpy, "WM_CLIENT_LEADER", False);
}
if (!_XA_SM_CLIENT_ID)
{
_XA_SM_CLIENT_ID = XInternAtom(dpy, "SM_CLIENT_ID", False);
}
if (XGetWindowProperty(
dpy, window, _XA_WM_CLIENT_LEADER, 0L, 1L, False,
AnyPropertyType, &actual_type, &actual_format, &nitems,
&bytes_after, &prop) == Success)
{
if (actual_type == XA_WINDOW && actual_format == 32 &&
nitems == 1 && bytes_after == 0)
{
client_leader = (Window)(*(long *)prop);
}
}
if (!client_leader)
{
client_leader = window;
}
#ifdef FVWM_DEBUG_FSM
fvwm_debug(__func__, "[%s][fsm_init] Proxy %s window 0x%lx\n",
module_name, (sm)? "On":"Off", client_leader);
#endif
/* set the client id for ksmserver */
if (sm)
{
XChangeProperty(
dpy, client_leader, _XA_SESSION_MANAGER, XA_STRING,
8, PropModeReplace, (unsigned char *)sm, strlen(sm));
XChangeProperty(
dpy, client_leader, _XA_SM_CLIENT_ID, XA_STRING,
8, PropModeReplace, (unsigned char *)dummy_id,
strlen(dummy_id));
}
else
{
XDeleteProperty(dpy, client_leader, _XA_SESSION_MANAGER);
XDeleteProperty(dpy, client_leader, _XA_SM_CLIENT_ID);
}
}
/* ---------------------------- interface functions ------------------------ */
int fsm_init(char *module)
{
char errormsg[256];
int i;
char *p;
if (!SessionSupport)
{
/* -Wall fix */
MyIoErrorHandler(NULL);
HostBasedAuthProc(NULL);
ice_watch_fd(0, NULL, 0, NULL);
NewClientProc(0, NULL, 0, NULL, NULL);
#ifdef FVWM_DEBUG_FSM
fvwm_debug(__func__, "[%s][fsm_init] No Session Support\n",
module_name);
#endif
return 0;
}
if (fsm_init_succeed)
{
fvwm_debug(__func__, "[%s][fsm_init] <<ERROR>> -- "
"Session already Initialized!\n", module_name);
return 1;
}
module_name = module;
InstallIOErrorHandler ();
#ifdef FVWM_DEBUG_FSM
fvwm_debug(__func__, "[%s][fsm_init] FSmsInitialize\n", module_name);
#endif
if (!FSmsInitialize(
module, "1.0", NewClientProc, NULL, HostBasedAuthProc, 256,
errormsg))
{
fvwm_debug(__func__, "[%s][fsm_init] <<ERROR>> -- "
"FSmsInitialize failed: %s\n",
module_name, errormsg);
return 0;
}
if (!FIceListenForConnections (
&numTransports, &slistenObjs, 256, errormsg))
{
fvwm_debug(__func__, "[%s][fsm_init] <<ERROR>> -- "
"FIceListenForConnections failed:\n"
"%s\n", module_name, errormsg);
return 0;
}
atexit(CloseListeners);
if (!SetAuthentication(numTransports, slistenObjs, &sauthDataEntries))
{
fvwm_debug(__func__, "[%s][fsm_init] <<ERROR>> -- "
"Could not set authorization\n", module_name);
return 0;
}
if (FIceAddConnectionWatch(&ice_watch_fd, NULL) == 0)
{
fvwm_debug(__func__,
"[%s][fsm_init] <<ERROR>> -- "
"FIceAddConnectionWatch failed\n", module_name);
return 0;
}
ice_fd = fxmalloc(sizeof(int) * numTransports + 1);
for (i = 0; i < numTransports; i++)
{
ice_fd[i] = FIceGetListenConnectionNumber(slistenObjs[i]);
}
networkIds = FIceComposeNetworkIdList(numTransports, slistenObjs);
xasprintf(&p, "SESSION_MANAGER=%s", networkIds);
putenv(p);
#ifdef FVWM_DEBUG_FSM
fvwm_debug(__func__, "[%s][fsm_init] OK: %i\n", module_name,
numTransports);
fvwm_debug(__func__, "\t%s\n", p);
#endif
fsm_init_succeed = True;
return 1;
}
void fsm_fdset(fd_set *in_fdset)
{
int i;
flist *l = fsm_ice_conn_list;
fsm_ice_conn_t *fic;
if (!SessionSupport || !fsm_init_succeed)
{
return;
}
for (i = 0; i < numTransports; i++)
{
FD_SET(ice_fd[i], in_fdset);
}
while(l != NULL)
{
fic = (fsm_ice_conn_t *)l->object;
FD_SET(fic->fd, in_fdset);
l = l->next;
}
}
Bool fsm_process(fd_set *in_fdset)
{
int i;
flist *l = fsm_ice_conn_list;
fsm_ice_conn_t *fic;
if (!SessionSupport || !fsm_init_succeed)
{
return False;
}
if (pending_ice_conn_list != NULL)
{
CompletNewConnectionMsg();
}
while(l != NULL)
{
fic = (fsm_ice_conn_t *)l->object;
if (FD_ISSET(fic->fd, in_fdset))
{
ProcessIceMsg(fic);
}
l = l->next;
}
for (i = 0; i < numTransports; i++)
{
if (FD_ISSET(ice_fd[i], in_fdset))
{
NewConnectionMsg(i);
}
}
if (pending_ice_conn_list != NULL)
{
return True;
}
return False;
}
/* this try to explain to ksmserver and various sm poxies that they should not
* connect our non XSMP award leader window to the top level sm */
void fsm_proxy(Display *dpy, Window win, char *sm)
{
char *client_id;
flist *l = running_list;
Bool found = False;
if (!SessionSupport || !fsm_init_succeed)
{
return;
}
client_id = GetClientID(dpy, win);
if (client_id != NULL && strcmp("0", client_id) != 0)
{
for (l = running_list; l; l = l->next)
{
fsm_client_t *client = (fsm_client_t *) l->object;
if (client->clientId &&
strcmp(client->clientId, client_id) == 0)
{
found = 1;
break;
}
}
}
if (found)
{
return;
}
set_session_manager(dpy, win, sm);
}
void fsm_close(void)
{
if (!SessionSupport || !fsm_init_succeed)
{
return;
}
FreeAuthenticationData(numTransports, sauthDataEntries);
}
|