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
|
/*
* ion/mod_sm/sm_session.c
*
* Copyright (c) Tuomo Valkonen 2004-2007.
*
* Based on the code of the 'sm' module for Ion1 by an unknown contributor.
*
* Ion is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <libtu/misc.h>
#include <X11/Xlib.h>
#include <X11/SM/SMlib.h>
#include <libextl/readconfig.h>
#include <libmainloop/select.h>
#include <libmainloop/exec.h>
#include <ioncore/exec.h>
#include <ioncore/global.h>
#include <ioncore/ioncore.h>
#include "sm_session.h"
static IceConn ice_sm_conn=NULL;
static SmcConn sm_conn=NULL;
static int sm_fd=-1;
static char *sm_client_id=NULL;
static char restart_hint=SmRestartImmediately;
static Bool sent_save_done=FALSE;
/* Function to be called when sm tells client save is complete */
static void (*save_complete_fn)();
void mod_sm_set_ion_id(const char *client_id)
{
if(sm_client_id)
free(sm_client_id);
if(client_id==NULL)
sm_client_id=NULL;
else
sm_client_id=scopy(client_id);
}
char *mod_sm_get_ion_id()
{
return sm_client_id;
}
/* Called when there's data to be read.
IcePcocessMessages determines message protocol,
unpacks the message and sends it to the client via
registered callbacks. */
static void sm_process_messages(int UNUSED(fd), void *UNUSED(data))
{
Bool ret;
if(IceProcessMessages(ice_sm_conn, NULL, &ret)==IceProcessMessagesIOError){
mod_sm_close();
}
}
/* Callback triggered when an Ice connection is
opened or closed. */
static void sm_ice_watch_fd(IceConn conn,
IcePointer UNUSED(client_data),
Bool opening,
IcePointer *UNUSED(watch_data))
{
if(opening){
if(sm_fd!=-1){ /* shouldn't happen */
warn(TR("Too many ICE connections."));
}
else{
sm_fd=IceConnectionNumber(conn);
cloexec_braindamage_fix(sm_fd);
mainloop_register_input_fd(sm_fd, NULL, &sm_process_messages);
}
}
else{
if (IceConnectionNumber(conn)==sm_fd){
mainloop_unregister_input_fd(sm_fd);
sm_fd=-1;
}
}
}
/* Store restart information and stuff in the session manager */
static void sm_set_some_properties()
{
SmPropValue program_val, userid_val;
SmProp program_prop, userid_prop, clone_prop;
SmProp *props[3];
props[0]=&program_prop;
props[1]=&userid_prop;
props[2]=&clone_prop;
program_val.value=ioncore_g.argv[0];
program_val.length=strlen(program_val.value);
program_prop.name=SmProgram;
program_prop.type=SmARRAY8;
program_prop.num_vals=1;
program_prop.vals=&program_val;
userid_val.value=getenv("USER");
userid_val.length=strlen(userid_val.value);
userid_prop.name=SmUserID;
userid_prop.type=SmARRAY8;
userid_prop.num_vals=1;
userid_prop.vals=&userid_val;
clone_prop.name=SmCloneCommand;
clone_prop.type=SmLISTofARRAY8;
clone_prop.num_vals=1;
clone_prop.vals=&program_val;
SmcSetProperties(sm_conn,
sizeof(props)/sizeof(props[0]),
(SmProp **)&props);
}
static void sm_set_other_properties()
{
char *restore="-session";
char *clientid="-smclientid";
int nvals=0, i;
const char *sdir=NULL, *cid=NULL;
SmPropValue restart_hint_val, *restart_val=NULL;
SmProp restart_hint_prop={ SmRestartStyleHint, SmCARD8, 1, NULL};
SmProp restart_prop={ SmRestartCommand, SmLISTofARRAY8, 0, NULL};
SmProp *props[2];
restart_hint_prop.vals=&restart_hint_val;
props[0]=&restart_prop;
props[1]=&restart_hint_prop;
sdir=extl_sessiondir();
cid=mod_sm_get_ion_id();
if(sdir==NULL || cid==NULL)
return;
restart_hint_val.value=&restart_hint;
restart_hint_val.length=1;
restart_val=(SmPropValue *)malloc((ioncore_g.argc+4)*sizeof(SmPropValue));
for(i=0; i<ioncore_g.argc; i++){
if(strcmp(ioncore_g.argv[i], restore)==0 ||
strcmp(ioncore_g.argv[i], clientid)==0){
i++;
}else{
restart_val[nvals].value=ioncore_g.argv[i];
restart_val[nvals++].length=strlen(ioncore_g.argv[i]);
}
}
restart_val[nvals].value=restore;
restart_val[nvals++].length=strlen(restore);
restart_val[nvals].value=(char*)sdir;
restart_val[nvals++].length=strlen(sdir);
restart_val[nvals].value=clientid;
restart_val[nvals++].length=strlen(clientid);
restart_val[nvals].value=(char*)cid;
restart_val[nvals++].length=strlen(cid);
restart_prop.num_vals=nvals;
restart_prop.vals=restart_val;
SmcSetProperties(sm_conn,
sizeof(props)/sizeof(props[0]),
(SmProp **)&props);
free(restart_val);
}
static void sm_set_properties()
{
static bool init=TRUE;
if(init){
sm_set_some_properties();
init=FALSE;
}
sm_set_other_properties();
}
/* Callback for the save yourself phase 2 message.
This message is sent by the sm when other clients in the session are finished
saving state. This is requested in the save yourself callback by clients
like this one that manages other clients. */
static void sm_save_yourself_phase2(SmcConn conn, SmPointer UNUSED(client_data))
{
Bool success;
if(!(success=ioncore_do_snapshot(TRUE)))
warn(TR("Failed to save session state"));
else
sm_set_properties();
SmcSaveYourselfDone(conn, success);
sent_save_done=TRUE;
}
/* Callback. Called when the client recieves a save yourself
message from the sm. */
static void sm_save_yourself(SmcConn UNUSED(conn),
SmPointer UNUSED(client_data),
int UNUSED(save_type),
Bool UNUSED(shutdown),
int UNUSED(interact_style),
Bool UNUSED(fast))
{
if(!SmcRequestSaveYourselfPhase2(sm_conn, sm_save_yourself_phase2, NULL)){
warn(TR("Failed to request save-yourself-phase2 from "
"session manager."));
SmcSaveYourselfDone(sm_conn, False);
sent_save_done=TRUE;
}else{
sent_save_done=FALSE;
}
}
/* Response to the shutdown cancelled message */
static void sm_shutdown_cancelled(SmcConn conn, SmPointer UNUSED(client_data))
{
save_complete_fn=NULL;
if(!sent_save_done){
SmcSaveYourselfDone(conn, False);
sent_save_done=True;
}
}
/* Callback */
static void sm_save_complete(SmcConn UNUSED(conn), SmPointer UNUSED(client_data))
{
if(save_complete_fn){
save_complete_fn();
save_complete_fn=NULL;
}
}
/* Callback */
static void sm_die(SmcConn conn, SmPointer UNUSED(client_data))
{
assert(conn==sm_conn);
ioncore_do_exit();
}
/* Connects to the sm and registers
callbacks for different messages */
bool mod_sm_init_session()
{
char error_str[256];
char *new_client_id=NULL;
SmcCallbacks smcall;
if(getenv("SESSION_MANAGER")==0){
warn(TR("SESSION_MANAGER environment variable not set."));
return FALSE;
}
if(IceAddConnectionWatch(&sm_ice_watch_fd, NULL) == 0){
warn(TR("Session Manager: IceAddConnectionWatch failed."));
return FALSE;
}
memset(&smcall, 0, sizeof(smcall));
smcall.save_yourself.callback=&sm_save_yourself;
smcall.save_yourself.client_data=NULL;
smcall.die.callback=&sm_die;
smcall.die.client_data=NULL;
smcall.save_complete.callback=&sm_save_complete;
smcall.save_complete.client_data=NULL;
smcall.shutdown_cancelled.callback=&sm_shutdown_cancelled;
smcall.shutdown_cancelled.client_data=NULL;
if((sm_conn=SmcOpenConnection(NULL, /* network ids */
NULL, /* context */
1, 0, /* protocol major, minor */
SmcSaveYourselfProcMask |
SmcSaveCompleteProcMask |
SmcShutdownCancelledProcMask |
SmcDieProcMask,
&smcall,
sm_client_id, &new_client_id,
sizeof(error_str), error_str)) == NULL)
{
warn(TR("Unable to connect to the session manager."));
return FALSE;
}
mod_sm_set_ion_id(new_client_id);
free(new_client_id);
ice_sm_conn=SmcGetIceConnection(sm_conn);
return TRUE;
}
void mod_sm_close()
{
if(sm_conn!=NULL){
SmcCloseConnection(sm_conn, 0, NULL);
sm_conn=NULL;
}
ice_sm_conn=NULL;
if(sm_fd>=0){
mainloop_unregister_input_fd(sm_fd);
close(sm_fd);
sm_fd=-1;
}
if(sm_client_id!=NULL){
free(sm_client_id);
sm_client_id=NULL;
}
}
static void sm_exit()
{
sm_die(sm_conn, NULL);
}
static void sm_restart()
{
ioncore_do_restart();
}
void mod_sm_smhook(int what)
{
save_complete_fn=NULL;
/* pending check? */
switch(what){
case IONCORE_SM_RESIGN:
restart_hint=SmRestartIfRunning;
sm_set_properties();
/*SmcRequestSaveYourself(sm_conn, SmSaveBoth, False,
SmInteractStyleAny, False, False);
save_complete_fn=&sm_exit;*/
ioncore_do_exit();
break;
case IONCORE_SM_SHUTDOWN:
restart_hint=SmRestartIfRunning;
SmcRequestSaveYourself(sm_conn, SmSaveBoth, True,
SmInteractStyleAny, False, True);
break;
case IONCORE_SM_RESTART:
restart_hint=SmRestartImmediately;
SmcRequestSaveYourself(sm_conn, SmSaveBoth, False,
SmInteractStyleAny, False, False);
save_complete_fn=&sm_exit;
break;
case IONCORE_SM_RESTART_OTHER:
restart_hint=SmRestartIfRunning;
SmcRequestSaveYourself(sm_conn, SmSaveBoth, False,
SmInteractStyleAny, False, False);
save_complete_fn=&sm_restart;
break;
case IONCORE_SM_SNAPSHOT:
restart_hint=SmRestartImmediately;
SmcRequestSaveYourself(sm_conn, SmSaveBoth, False,
SmInteractStyleAny, False, True);
break;
}
}
|