~sbalneav/ltsp/xexit

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
/*
 * xexit.c: - Wait for an X session to die, then execute a script.
 *
 * (c) 2010 Scott Balneaves <sbalneav@ltsp.org>
 *
 * This software is distributed under the terms and conditions of the
 * GNU General Public License. See file COPYING for the full text of
 * the license.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
#include <X11/Xresource.h>
#include <X11/Xlib.h>
#include <pthread.h>

#define XEXIT 1
#define PING_INTERVAL 10

char *script;                   /* Script to exit at logout */
char *d;                        /* DISPLAY environment variable */
Display *dpy = NULL;            /* Display */

static int
handle_xerror (Display * display)
{
  execlp (script, script, NULL);
}

/*
 * ping_timeout:
 *
 * This function will attempt to open up an X display ever PING_INTERVAL
 * seconds.  If the display open fails, we'll trigger the script.
 */

void
ping_timeout (void *nothing)
{
  Display *pd;

  for (;;)
    {
      sleep (PING_INTERVAL);
      if (!(pd = XOpenDisplay (d)))
        {
          handle_xerror (NULL);
        }
      XCloseDisplay (pd);
    }
}

int
handle_propchange (Display * d, Window w, Atom a)
{
  char *result;
  Atom type;
  int format, res;
  unsigned long nitems, nleft;
  int return_val = 0;


  res = XGetWindowProperty (d, w, a, 0, 512, 0, XA_STRING,
                            &type, &format, &nitems, &nleft,
                            (unsigned char **) &result);

  if (res != Success)
    {
      if (type == XA_STRING)
        {
          if (*result == 'E' || *result == 'e')
            {                   /* EXIT */
              return_val = XEXIT;
            }
        }
      XFree (result);
    }

  return return_val;
}

int
main (int argc, char *argv[])
{
  Window root;                  /* root window */
  Atom prog_atom;               /* Our atom */
  XEvent ev;
  pthread_t ping_thread;
  char *atomname;               /* Our atom to watch for */
  int rv;

  if (setenv ("XEXITSENTINEL", "True", 1) != 0)
    {
      fprintf (stderr, "Couldn't set XEXITSENTINEL variable");
      exit (1);
    }

  if (argc != 2)
    {
      fprintf (stderr, "usage: %s scriptname\n", argv[0]);
      exit (1);
    }

  script = argv[1];

  d = getenv ("DISPLAY");

  /*
   * Daemonize.
   */

  if (daemon (0, 0) < 0)
    {
      fprintf (stderr, "Couldn't background!\n");
      exit (1);
    }

  /*
   * Kick off our pinger thread.
   */

  if (pthread_create (&ping_thread, NULL, (void *) &ping_timeout,
                      (void *) NULL) < 0)
    {
      exit (1);
    }

  pthread_detach (ping_thread);

  /*
   * Open our display
   */

  if (!(dpy = XOpenDisplay (d)))
    {
      exit (2);
    }

  /*
   * Atom to control the program.
   */

  atomname = "LTSP_XEXIT";

  XSetIOErrorHandler (handle_xerror);

  root = RootWindow (dpy, DefaultScreen (dpy));

  /*
   * We've got the root window now.  First, register our needed atom.
   */

  prog_atom = XInternAtom (dpy, atomname, False);

  if (prog_atom == None)
    {
      exit (3);
    }

  /*
   * Declare our interest in obtaining PropertyChange events.
   */

  XSelectInput (dpy, root, PropertyChangeMask);

  /*
   * Finally, sit in our event loop, and search for the property changes.
   */

  for (;;)
    {
      if (XPending (dpy))
        {
          XNextEvent (dpy, &ev);
          if (ev.type == PropertyNotify)
            {
              if (ev.xproperty.atom == prog_atom)
                {
                  rv = handle_propchange (dpy, root, prog_atom);
                  if (rv == XEXIT)
                    {
                      XCloseDisplay (dpy);
                      exit (0);
                    }
                }
            }
        }
      else
        {
          usleep (100000);
        }
    }

  handle_xerror (NULL);
}