~sbalneav/ltsp/xexit

« back to all changes in this revision

Viewing changes to src/xexit.c

  • Committer: Scott Balneaves
  • Date: 2010-05-20 20:27:47 UTC
  • Revision ID: sbalneav@3jane-20100520202747-1cdne3jb8qs0ei85
Added pinger timeout.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#include <X11/Xatom.h>
19
19
#include <X11/Xresource.h>
20
20
#include <X11/Xlib.h>
 
21
#include <pthread.h>
21
22
 
22
23
#define XEXIT 1
23
24
#define XDIE 2
24
 
 
25
 
/* Script to exit at logout */
26
 
char *script;
27
 
 
28
 
int
29
 
handle_xerror(Display *d)
 
25
#define PING_INTERVAL 10
 
26
 
 
27
char *script;           /* Script to exit at logout */
 
28
char *d;                /* DISPLAY environment variable */
 
29
Display *dpy = NULL;    /* Display */
 
30
 
 
31
static int
 
32
handle_xerror(Display *display)
30
33
{
31
 
    XCloseDisplay(d);
32
34
    execlp(script, script, NULL);
33
35
}
34
36
 
 
37
void
 
38
run_script(void)
 
39
{
 
40
    if (dpy != NULL) {
 
41
        handle_xerror(dpy);
 
42
    }
 
43
}
 
44
 
 
45
/*
 
46
 * ping_timeout:
 
47
 *
 
48
 * This function will attempt to open up an X display ever PING_INTERVAL
 
49
 * seconds.  If the display open fails, we'll trigger the script.
 
50
 */
 
51
 
 
52
void
 
53
ping_timeout(void *nothing)
 
54
{
 
55
    struct timespec ping_interval;
 
56
    Display *pd;
 
57
 
 
58
    while (1) {
 
59
        ping_interval.tv_sec = PING_INTERVAL;
 
60
        ping_interval.tv_nsec = 0;
 
61
        nanosleep(&ping_interval, NULL);
 
62
        if (!(pd = XOpenDisplay(d))) {
 
63
            run_script();
 
64
        }
 
65
        XCloseDisplay(pd);
 
66
    }
 
67
}
 
68
 
35
69
int
36
70
handle_propchange(Display *d, Window w, Atom a)
37
71
{
72
106
int
73
107
main(int argc, char *argv[])
74
108
{
75
 
    Display *dpy;                   /* Display */
76
109
    Atom prog_atom;                 /* Our atom */
77
110
    Window root;                    /* root window */
78
111
    char *atomname;                 /* Our atom to watch for */
79
112
    XEvent ev;
80
 
    char *d = getenv("DISPLAY");
81
113
    int rv;
 
114
    pthread_t ping_thread;
82
115
 
83
116
    if (argc != 2) {
84
117
        fprintf(stderr, "usage: %s scriptname\n", argv[0]);
87
120
 
88
121
    script = argv[1];
89
122
 
 
123
    d = getenv("DISPLAY");
 
124
 
90
125
    /*
91
126
     * Daemonize.
92
127
     */
97
132
    }
98
133
 
99
134
    /*
 
135
     * Kick off our pinger thread.
 
136
     */
 
137
 
 
138
    if (pthread_create(&ping_thread, NULL, (void *) &ping_timeout,
 
139
                                           (void *) NULL) < 0) {
 
140
        exit(1);
 
141
    }
 
142
 
 
143
    pthread_detach(ping_thread);
 
144
 
 
145
    /*
100
146
     * Open our display
101
147
     */
102
148
 
151
197
        }
152
198
    }
153
199
 
154
 
    XCloseDisplay(dpy);
155
 
 
156
 
    if (rv == XDIE) {
157
 
        execlp(script, script, NULL);
 
200
    if (rv == XEXIT) {
 
201
        XCloseDisplay(dpy);
 
202
        dpy = NULL;
158
203
    }
159
204
 
160
 
    return 0;
 
205
    run_script();
161
206
}