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
|
/*
Copyright 1993, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
*/
/*
* Author: Ralph Mor, X Consortium
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <X11/SM/SMlib.h>
#include "SMlibint.h"
int _SmcOpcode = 0;
int _SmsOpcode = 0;
#ifndef __UNIXOS2__
SmsNewClientProc _SmsNewClientProc;
SmPointer _SmsNewClientData;
#else
SmsNewClientProc _SmsNewClientProc = 0;
SmPointer _SmsNewClientData = 0;
#endif
SmcErrorHandler _SmcErrorHandler = _SmcDefaultErrorHandler;
SmsErrorHandler _SmsErrorHandler = _SmsDefaultErrorHandler;
static void
set_callbacks(SmcConn smcConn, unsigned long mask, SmcCallbacks *callbacks);
SmcConn
SmcOpenConnection(char *networkIdsList, SmPointer context,
int xsmpMajorRev, int xsmpMinorRev,
unsigned long mask, SmcCallbacks *callbacks,
char *previousId, char **clientIdRet,
int errorLength, char *errorStringRet)
{
SmcConn smcConn;
IceConn iceConn;
char *ids;
IceProtocolSetupStatus setupstat;
int majorVersion;
int minorVersion;
char *vendor = NULL;
char *release = NULL;
smRegisterClientMsg *pMsg;
char *pData;
int extra, len;
IceReplyWaitInfo replyWait;
_SmcRegisterClientReply reply;
Bool gotReply, ioErrorOccured;
const char *auth_names[] = {"MIT-MAGIC-COOKIE-1"};
IcePoAuthProc auth_procs[] = {_IcePoMagicCookie1Proc};
int auth_count = 1;
IcePoVersionRec versions[] = {
{SmProtoMajor, SmProtoMinor, _SmcProcessMessage}
};
int version_count = 1;
*clientIdRet = NULL;
if (errorStringRet && errorLength > 0)
*errorStringRet = '\0';
if (!_SmcOpcode)
{
/*
* For now, there is only one version of XSMP, so we don't
* have to check {xsmpMajorRev, xsmpMinorRev}. In the future,
* we will check against versions and generate the list
* of versions the application actually supports.
*/
if ((_SmcOpcode = IceRegisterForProtocolSetup ("XSMP",
SmVendorString, SmReleaseString, version_count, versions,
auth_count, auth_names, auth_procs, NULL)) < 0)
{
if (errorStringRet && errorLength > 0) {
strncpy (errorStringRet,
"Could not register XSMP protocol with ICE",
errorLength);
errorStringRet[errorLength - 1] = '\0';
}
return (NULL);
}
}
if (networkIdsList == NULL || *networkIdsList == '\0')
{
if ((ids = (char *) getenv ("SESSION_MANAGER")) == NULL)
{
if (errorStringRet && errorLength > 0) {
strncpy (errorStringRet,
"SESSION_MANAGER environment variable not defined",
errorLength);
errorStringRet[errorLength - 1] = '\0';
}
return (NULL);
}
}
else
{
ids = networkIdsList;
}
if ((iceConn = IceOpenConnection (
ids, context, 0, _SmcOpcode, errorLength, errorStringRet)) == NULL)
{
return (NULL);
}
if ((smcConn = (SmcConn) malloc (sizeof (struct _SmcConn))) == NULL)
{
if (errorStringRet && errorLength > 0) {
strncpy (errorStringRet, "Can't malloc", errorLength);
errorStringRet[errorLength - 1] = '\0';
}
IceCloseConnection (iceConn);
return (NULL);
}
setupstat = IceProtocolSetup (iceConn, _SmcOpcode,
(IcePointer) smcConn,
False /* mustAuthenticate */,
&majorVersion, &minorVersion,
&vendor, &release, errorLength, errorStringRet);
if (setupstat == IceProtocolSetupFailure ||
setupstat == IceProtocolSetupIOError)
{
IceCloseConnection (iceConn);
free ((char *) smcConn);
return (NULL);
}
else if (setupstat == IceProtocolAlreadyActive)
{
/*
* This case should never happen, because when we called
* IceOpenConnection, we required that the ICE connection
* may not already have XSMP active on it.
*/
free ((char *) smcConn);
if (errorStringRet && errorLength > 0) {
strncpy (errorStringRet, "Internal error in IceOpenConnection",
errorLength);
errorStringRet[errorLength - 1] = '\0';
}
return (NULL);
}
smcConn->iceConn = iceConn;
smcConn->proto_major_version = majorVersion;
smcConn->proto_minor_version = minorVersion;
smcConn->vendor = vendor;
smcConn->release = release;
smcConn->client_id = NULL;
bzero ((char *) &smcConn->callbacks, sizeof (SmcCallbacks));
set_callbacks (smcConn, mask, callbacks);
smcConn->interact_waits = NULL;
smcConn->phase2_wait = NULL;
smcConn->prop_reply_waits = NULL;
smcConn->save_yourself_in_progress = False;
smcConn->shutdown_in_progress = False;
/*
* Now register the client
*/
if (!previousId)
previousId = "";
len = strlen (previousId);
extra = ARRAY8_BYTES (len);
IceGetHeaderExtra (iceConn, _SmcOpcode, SM_RegisterClient,
SIZEOF (smRegisterClientMsg), WORD64COUNT (extra),
smRegisterClientMsg, pMsg, pData);
STORE_ARRAY8 (pData, len, previousId);
IceFlush (iceConn);
replyWait.sequence_of_request = IceLastSentSequenceNumber (iceConn);
replyWait.major_opcode_of_request = _SmcOpcode;
replyWait.minor_opcode_of_request = SM_RegisterClient;
replyWait.reply = (IcePointer) &reply;
gotReply = False;
ioErrorOccured = False;
while (!gotReply && !ioErrorOccured)
{
ioErrorOccured = (IceProcessMessages (
iceConn, &replyWait, &gotReply) == IceProcessMessagesIOError);
if (ioErrorOccured)
{
if (errorStringRet && errorLength > 0) {
strncpy (errorStringRet, "IO error occured opening connection",
errorLength);
errorStringRet[errorLength - 1] = '\0';
}
free (smcConn->vendor);
free (smcConn->release);
free ((char *) smcConn);
return (NULL);
}
else if (gotReply)
{
if (reply.status == 1)
{
/*
* The client successfully registered.
*/
*clientIdRet = reply.client_id;
smcConn->client_id = strdup (*clientIdRet);
}
else
{
/*
* Could not register the client because the previous ID
* was bad. So now we register the client with the
* previous ID set to NULL.
*/
extra = ARRAY8_BYTES (0);
IceGetHeaderExtra (iceConn, _SmcOpcode, SM_RegisterClient,
SIZEOF (smRegisterClientMsg), WORD64COUNT (extra),
smRegisterClientMsg, pMsg, pData);
STORE_ARRAY8 (pData, 0, "");
IceFlush (iceConn);
replyWait.sequence_of_request =
IceLastSentSequenceNumber (iceConn);
gotReply = False;
}
}
}
return (smcConn);
}
SmcCloseStatus
SmcCloseConnection(SmcConn smcConn, int count, char **reasonMsgs)
{
IceConn iceConn = smcConn->iceConn;
smCloseConnectionMsg *pMsg;
char *pData;
int extra, i;
IceCloseStatus closeStatus;
SmcCloseStatus statusRet;
extra = 8;
for (i = 0; i < count; i++)
extra += ARRAY8_BYTES (strlen (reasonMsgs[i]));
IceGetHeaderExtra (iceConn, _SmcOpcode, SM_CloseConnection,
SIZEOF (smCloseConnectionMsg), WORD64COUNT (extra),
smCloseConnectionMsg, pMsg, pData);
STORE_CARD32 (pData, count);
pData += 4;
for (i = 0; i < count; i++)
STORE_ARRAY8 (pData, strlen (reasonMsgs[i]), reasonMsgs[i]);
IceFlush (iceConn);
IceProtocolShutdown (iceConn, _SmcOpcode);
IceSetShutdownNegotiation (iceConn, False);
closeStatus = IceCloseConnection (iceConn);
if (smcConn->vendor)
free (smcConn->vendor);
if (smcConn->release)
free (smcConn->release);
if (smcConn->client_id)
free (smcConn->client_id);
if (smcConn->prop_reply_waits)
{
_SmcPropReplyWait *ptr = smcConn->prop_reply_waits;
_SmcPropReplyWait *next;
while (ptr)
{
next = ptr->next;
free ((char *) ptr);
ptr = next;
}
}
free ((char *) smcConn);
if (closeStatus == IceClosedNow)
statusRet = SmcClosedNow;
else if (closeStatus == IceClosedASAP)
statusRet = SmcClosedASAP;
else
statusRet = SmcConnectionInUse;
return (statusRet);
}
void
SmcModifyCallbacks(SmcConn smcConn, unsigned long mask, SmcCallbacks *callbacks)
{
set_callbacks (smcConn, mask, callbacks);
}
void
SmcSetProperties(SmcConn smcConn, int numProps, SmProp **props)
{
IceConn iceConn = smcConn->iceConn;
smSetPropertiesMsg *pMsg;
char *pBuf;
char *pStart;
int bytes;
IceGetHeader (iceConn, _SmcOpcode, SM_SetProperties,
SIZEOF (smSetPropertiesMsg), smSetPropertiesMsg, pMsg);
LISTOF_PROP_BYTES (numProps, props, bytes);
pMsg->length += WORD64COUNT (bytes);
pBuf = pStart = IceAllocScratch (iceConn, bytes);
memset(pStart, 0, bytes);
STORE_LISTOF_PROPERTY (pBuf, numProps, props);
IceWriteData (iceConn, bytes, pStart);
IceFlush (iceConn);
}
void
SmcDeleteProperties(SmcConn smcConn, int numProps, char **propNames)
{
IceConn iceConn = smcConn->iceConn;
smDeletePropertiesMsg *pMsg;
char *pData;
int extra, i;
extra = 8;
for (i = 0; i < numProps; i++)
extra += ARRAY8_BYTES (strlen (propNames[i]));
IceGetHeaderExtra (iceConn, _SmcOpcode, SM_DeleteProperties,
SIZEOF (smDeletePropertiesMsg), WORD64COUNT (extra),
smDeletePropertiesMsg, pMsg, pData);
STORE_CARD32 (pData, numProps);
pData += 4;
for (i = 0; i < numProps; i++)
STORE_ARRAY8 (pData, strlen (propNames[i]), propNames[i]);
IceFlush (iceConn);
}
Status
SmcGetProperties(SmcConn smcConn, SmcPropReplyProc propReplyProc,
SmPointer clientData)
{
IceConn iceConn = smcConn->iceConn;
_SmcPropReplyWait *wait, *ptr;
if ((wait = (_SmcPropReplyWait *) malloc (
sizeof (_SmcPropReplyWait))) == NULL)
{
return (0);
}
wait->prop_reply_proc = propReplyProc;
wait->client_data = clientData;
wait->next = NULL;
ptr = smcConn->prop_reply_waits;
while (ptr && ptr->next)
ptr = ptr->next;
if (ptr == NULL)
smcConn->prop_reply_waits = wait;
else
ptr->next = wait;
IceSimpleMessage (iceConn, _SmcOpcode, SM_GetProperties);
IceFlush (iceConn);
return (1);
}
Status
SmcInteractRequest(SmcConn smcConn, int dialogType,
SmcInteractProc interactProc, SmPointer clientData)
{
IceConn iceConn = smcConn->iceConn;
smInteractRequestMsg *pMsg;
_SmcInteractWait *wait, *ptr;
if ((wait = (_SmcInteractWait *) malloc (
sizeof (_SmcInteractWait))) == NULL)
{
return (0);
}
wait->interact_proc = interactProc;
wait->client_data = clientData;
wait->next = NULL;
ptr = smcConn->interact_waits;
while (ptr && ptr->next)
ptr = ptr->next;
if (ptr == NULL)
smcConn->interact_waits = wait;
else
ptr->next = wait;
IceGetHeader (iceConn, _SmcOpcode, SM_InteractRequest,
SIZEOF (smInteractRequestMsg), smInteractRequestMsg, pMsg);
pMsg->dialogType = dialogType;
IceFlush (iceConn);
return (1);
}
void
SmcInteractDone(SmcConn smcConn, Bool cancelShutdown)
{
IceConn iceConn = smcConn->iceConn;
smInteractDoneMsg *pMsg;
IceGetHeader (iceConn, _SmcOpcode, SM_InteractDone,
SIZEOF (smInteractDoneMsg), smInteractDoneMsg, pMsg);
pMsg->cancelShutdown = cancelShutdown;
IceFlush (iceConn);
}
void
SmcRequestSaveYourself(SmcConn smcConn, int saveType, Bool shutdown,
int interactStyle, Bool fast, Bool global)
{
IceConn iceConn = smcConn->iceConn;
smSaveYourselfRequestMsg *pMsg;
IceGetHeader (iceConn, _SmcOpcode, SM_SaveYourselfRequest,
SIZEOF (smSaveYourselfRequestMsg), smSaveYourselfRequestMsg, pMsg);
pMsg->saveType = saveType;
pMsg->shutdown = shutdown;
pMsg->interactStyle = interactStyle;
pMsg->fast = fast;
pMsg->global = global;
IceFlush (iceConn);
}
Status
SmcRequestSaveYourselfPhase2(SmcConn smcConn,
SmcSaveYourselfPhase2Proc saveYourselfPhase2Proc,
SmPointer clientData)
{
IceConn iceConn = smcConn->iceConn;
_SmcPhase2Wait *wait;
if (smcConn->phase2_wait)
wait = smcConn->phase2_wait;
else
{
if ((wait = (_SmcPhase2Wait *) malloc (
sizeof (_SmcPhase2Wait))) == NULL)
{
return (0);
}
}
wait->phase2_proc = saveYourselfPhase2Proc;
wait->client_data = clientData;
smcConn->phase2_wait = wait;
IceSimpleMessage (iceConn, _SmcOpcode, SM_SaveYourselfPhase2Request);
IceFlush (iceConn);
return (1);
}
void
SmcSaveYourselfDone(SmcConn smcConn, Bool success)
{
IceConn iceConn = smcConn->iceConn;
smSaveYourselfDoneMsg *pMsg;
IceGetHeader (iceConn, _SmcOpcode, SM_SaveYourselfDone,
SIZEOF (smSaveYourselfDoneMsg), smSaveYourselfDoneMsg, pMsg);
pMsg->success = success;
IceFlush (iceConn);
}
static void
set_callbacks(SmcConn smcConn, unsigned long mask, SmcCallbacks *callbacks)
{
if (mask & SmcSaveYourselfProcMask)
{
smcConn->callbacks.save_yourself.callback =
callbacks->save_yourself.callback;
smcConn->callbacks.save_yourself.client_data =
callbacks->save_yourself.client_data;
}
if (mask & SmcDieProcMask)
{
smcConn->callbacks.die.callback = callbacks->die.callback;
smcConn->callbacks.die.client_data = callbacks->die.client_data;
}
if (mask & SmcSaveCompleteProcMask)
{
smcConn->callbacks.save_complete.callback =
callbacks->save_complete.callback;
smcConn->callbacks.save_complete.client_data =
callbacks->save_complete.client_data;
}
if (mask & SmcShutdownCancelledProcMask)
{
smcConn->callbacks.shutdown_cancelled.callback =
callbacks->shutdown_cancelled.callback;
smcConn->callbacks.shutdown_cancelled.client_data =
callbacks->shutdown_cancelled.client_data;
}
}
|