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
|
/*****************************************************************************
*
* Authors: Michel Eyckmans (MCE) & Stefan De Troch (SDT)
*
* Content: This file is part of version 2.x of xautolock. It implements
* the program's IPC features.
*
* Please send bug reports etc. to mce@scarlet.be.
*
* --------------------------------------------------------------------------
*
* Copyright 1990, 1992-1999, 2001-2002, 2004, 2007 by Stefan De Troch and
* Michel Eyckmans.
*
* Versions 2.0 and above of xautolock are available under version 2 of the
* GNU GPL. Earlier versions are available under other conditions. For more
* information, see the License file.
*
*****************************************************************************/
#include "message.h"
#include "state.h"
#include "options.h"
#include "miscutil.h"
static Atom semaphore; /* semaphore property for locating
an already running xautolock */
static Atom messageAtom; /* message property for talking to
an already running xautolock */
#define SEM_PID "_SEMAPHORE_PID"
#define MESSAGE "_MESSAGE"
/*
* Message handlers.
*/
static void
disableByMessage (Display* d, Window root)
{
/*
* The order in which things are done is rather important here.
*/
if (!secure)
{
setLockTrigger (lockTime);
disableKillTrigger ();
disabled = True;
}
}
static void
enableByMessage (Display* d, Window root)
{
if (!secure)
{
resetTriggers ();
disabled = False;
}
}
static void
toggleByMessage (Display* d, Window root)
{
if (!secure)
{
if ((disabled = !disabled)) /* = intended */
{
setLockTrigger (lockTime);
disableKillTrigger ();
}
else
{
resetTriggers ();
}
}
}
static void
exitByMessage (Display* d, Window root)
{
if (!secure)
{
error0 ("Exiting. Bye bye...\n");
exit (0);
}
}
static void
lockNowByMessage (Display* d, Window root)
{
if (!secure && !disabled) lockNow = True;
}
static void
unlockNowByMessage (Display* d, Window root)
{
if (!secure && !disabled) unlockNow = True;
}
static void
restartByMessage (Display* d, Window root)
{
if (!secure)
{
XDeleteProperty (d, root, semaphore);
XFlush (d);
execv (argArray[0], argArray);
}
}
/*
* Function for looking for messages from another xautolock.
*/
void
lookForMessages (Display* d)
{
Window root; /* as it says */
Atom type; /* actual property type */
int format; /* dummy */
unsigned long nofItems; /* dummy */
unsigned long after; /* dummy */
int* contents; /* message property value */
static Bool first = True; /* is this the first call? */
/*
* It would be cleaner (more X-like, using less cpu time and
* network bandwith, ...) to implement this functionality by
* keeping an eye open for PropertyNotify events on the root
* window, or (even better) by using ClientMessage events.
* Maybe I'll rewrite it one day to do just that, but the
* current approach works just fine too.
*
* Note that we must clear the property before acting on it!
* Otherwise funny things can happen on receipt of msg_exit.
*/
root = RootWindowOfScreen (ScreenOfDisplay (d, 0));
(void) XGetWindowProperty (d, root, messageAtom, 0L, 2L,
False, AnyPropertyType, &type,
&format, &nofItems, &after,
(unsigned char**) &contents);
XDeleteProperty (d, root, messageAtom);
XFlush (d);
/*
* The very first time we get here, we ignore the message. Why?
* Well, suppose some weirdo sends a SIGSTOP to a running xautolock,
* then does an `xautolock -exit' and finally sends a SIGKILL to
* the stopped xautolock. This would leave an unread message sitting
* around. Unread that is, till the next xautolock is started.
*
* To solve this, we want any xautolock that enters the main event
* loop to consume and ignore any message that may have been around
* for a while and forgotten about.
*/
if (first)
{
first = False;
}
else if (type == XA_INTEGER)
{
switch (*contents)
{
case msg_disable:
disableByMessage (d, root);
break;
case msg_enable:
enableByMessage (d, root);
break;
case msg_toggle:
toggleByMessage (d, root);
break;
case msg_lockNow:
lockNowByMessage (d, root);
break;
case msg_unlockNow:
unlockNowByMessage (d, root);
break;
case msg_restart:
restartByMessage (d, root);
break;
case msg_exit:
exitByMessage (d, root);
break;
default:
/* unknown message, ignore silently */
break;
}
}
(void) XFree ((char*) contents);
}
/*
* Function for creating the communication atoms.
*/
void
getAtoms (Display* d)
{
char* sem; /* semaphore property name */
char* mes; /* message property name */
char* ptr; /* iterator */
sem = newArray (char, strlen (progName) + strlen (SEM_PID) + 1);
(void) sprintf (sem, "%s%s", progName, SEM_PID);
for (ptr = sem; *ptr; ++ptr) *ptr = (char) toupper (*ptr);
semaphore = XInternAtom (d, sem, False);
free (sem);
mes = newArray (char, strlen (progName) + strlen (MESSAGE) + 1);
(void) sprintf (mes, "%s%s", progName, MESSAGE);
for (ptr = mes; *ptr; ++ptr) *ptr = (char) toupper (*ptr);
messageAtom = XInternAtom (d, mes, False);
free (mes);
}
/*
* Function for finding out whether another xautolock is already
* running and for sending it a message if that's what the user
* wanted.
*/
void
checkConnectionAndSendMessage (Display* d)
{
pid_t pid; /* as it says */
Window root; /* as it says */
Atom type; /* actual property type */
int format; /* dummy */
unsigned long nofItems; /* dummy */
unsigned long after; /* dummy */
pid_t* contents; /* semaphore property value */
getAtoms (d);
root = RootWindowOfScreen (ScreenOfDisplay (d, 0));
(void) XGetWindowProperty (d, root, semaphore, 0L, 2L, False,
AnyPropertyType, &type, &format,
&nofItems, &after,
(unsigned char**) &contents);
if (type == XA_INTEGER)
{
/*
* This breaks if the other xautolock is not on the same machine.
*/
if (kill (*contents, 0))
{
if (messageToSend)
{
error1 ("No process with PID %d, or the process "
"is owned by another user.\n", *contents);
exit (EXIT_FAILURE);
}
}
else if (messageToSend)
{
(void) XChangeProperty (d, root, messageAtom, XA_INTEGER,
8, PropModeReplace,
(unsigned char*) &messageToSend,
(int) sizeof (messageToSend));
XFlush (d);
exit (EXIT_SUCCESS);
}
else
{
#ifdef VMS
error2 ("%s is already running (PID %x).\n", progName, *contents);
#else /* VMS */
error2 ("%s is already running (PID %d).\n", progName, *contents);
#endif /* VMS */
exit (EXIT_FAILURE);
}
}
else if (messageToSend)
{
error1 ("Could not locate a running %s.\n", progName);
exit (EXIT_FAILURE);
}
pid = getpid ();
(void) XChangeProperty (d, root, semaphore, XA_INTEGER, 8,
PropModeReplace, (unsigned char*) &pid,
(int) sizeof (pid));
(void) XFree ((char*) contents);
}
|