summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 2dd433e9d58ca53ebf3dc735e7e2990d06696352 (plain) (blame)
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
#include "userlist.h"
#include "warn.h"
#include "x11util.h"
#include "util.h"
#include "main.h"
#include "rpc.h"

#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <error.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <getopt.h>

#define CAP_SLEEP(newval) do { if ( (newval) < sleepTime ) sleepTime = (newval); } while (0)

static time_t lastActivity;

// Tracked user sessions
#define USERS (50)
static struct user *users;

// List of reboot and shutdown times
static const char *shutdownActions[SHUTDOWN_ENUM_END] = {
    [REBOOT] = "reboot",
    [POWEROFF] = "poweroff",
    [KEXEC] = "kexec",
    [SUSPEND] = "suspend",
};
#define TABMAX (20)
static struct {
    time_t deadline; // MONOTONIC; 0 = none, or last one was disarmed and time elapsed past deadline
    enum Shutdown action;
    bool disarmed;
    bool force;
} nextAction;

// Config via command line
static struct {
    char *shutdownCommand;
    struct time shutdown[TABMAX];
    int shutdowns;
    int poweroffTimeout;
    int logoutTimeout;
    int suspendTimeout;
    int saverTimeout;
    int dpmsTimeout;
    int gracePeriod;
    int minIdle;
} config;

static time_t combineTime( const time_t now, const struct time * time );

static void getXAuthority( struct user *user );

static bool parseCmdline( int argc, char **argv );

static void execShutdown( enum Shutdown action );

int main( int argc, char **argv )
{
    int idx;
    time_t startWall, startMono, endWall, endMono;
    init_time();
    if ( ! parseCmdline( argc, argv ) )
        return 2;
    // Always line buffered output
    setvbuf( stdout, NULL, _IOLBF, -BUFSIZ );
    setvbuf( stderr, NULL, _IOLBF, -BUFSIZ );
    if ( _testmode ) {
        fprintf( stderr, "Test mode only!\n" );
    }
    users = calloc( USERS, sizeof(*users) );
    nextAction.deadline = 0;
    startWall = time( NULL );
    startMono = now();
    // Calculate shortest possible sleep time without delaying any timeout related actions
    int defaultSleep = 900;
    if ( config.logoutTimeout > 0 && config.logoutTimeout < defaultSleep ) {
        defaultSleep = config.logoutTimeout;
    }
    if ( config.saverTimeout > 0 && config.saverTimeout < defaultSleep ) {
        defaultSleep = config.saverTimeout;
    }
    if ( config.dpmsTimeout > 0 && config.dpmsTimeout < defaultSleep ) {
        defaultSleep = config.dpmsTimeout;
    }
    int listenFd = rpcOpen();
    if ( listenFd == -1 )
        return 1;
    lastActivity = now();
    for ( ;; ) {
        int sleepTime = defaultSleep;
        const int count = getUserList( users, USERS );
        int minIdleTime = -1;
        for ( idx = 0; idx < count; ++idx ) {
            struct user * const usr = &users[idx];
            if ( !usr->mark ) {
                fprintf( stderr, "This will never happen \\o/\n" );
                continue;
            }
            const time_t NOW = time( NULL );
            const time_t monoNOW = now();
            if ( *usr->display ) {
                // User has an X11 session going, get actual idle time
                struct x11state screen;
                getXAuthority( usr );
                if ( !getX11IdleTimes( usr->xauth, usr->display, &screen ) ) {
                    // Failed, assume active to be safe
                    printf( "X11 query failed. Using NOW.\n ");
                    usr->lastActivity = NOW;
                    usr->lastActivityOffset = 0;
                } else {
                    // Fiddle and fumble
                    usr->isLocked = ( screen.saverState == SAVER_LOCKED );
                    if ( screen.lockTimestamp == 0 && ( NOW - usr->lockTime ) > 4 ) {
                        // Session is not locked, unconditionally update timestamp
                        if ( usr->lockTime != 0 ) {
                            printf( "%s #### Session got unlocked.\n", usr->display );
                        }
                        usr->lockTime = 0;
                        usr->lockTimeOffset = 0;
                        usr->lastActivity = NOW - screen.idleSeconds;
                        usr->lastActivityOffset = 0;
                    } else if ( screen.lockTimestamp - usr->lockTime > 10 ) { // Allow for slight offset in case we manually set this
                        // Session is locked
                        printf( "%s #### Session got externally locked.\n", usr->display );
                        usr->lockTime = screen.lockTimestamp;
                        usr->lockTimeOffset = 0;
                        if ( usr->lastActivity == 0 || usr->lockTime != 0 ) {
                            // We have no activity log yet, or quickly unlocked and relocked -- update idle time
                            usr->lastActivity = usr->lockTime;
                            usr->lastActivityOffset = 0;
                        }
                    }
                    const int idleTime = NOW - ( usr->lastActivity + usr->lastActivityOffset );
                    if ( config.dpmsTimeout > 0 && screen.screenStandby != SCREEN_UNKNOWN ) {
                        int want = SCREEN_UNKNOWN;
                        if ( config.logoutTimeout > 0 && idleTime + 300 > config.logoutTimeout ) {
                            want = SCREEN_ON;
                        } else if ( ! nextAction.disarmed && nextAction.deadline != 0 && monoNOW - nextAction.deadline < 300 ) {
                            want = SCREEN_ON;
                        } else if ( idleTime > config.dpmsTimeout && screen.idleSeconds >= 60 ) {
                            want = SCREEN_OFF;
                        }
                        if ( want != SCREEN_UNKNOWN && screen.screenStandby != want ) {
                            //printf( "#### Powering %s %s.\n", usr->display, want == SCREEN_ON ? "ON" : "OFF" );
                            setScreenDpms( usr->xauth, usr->display, want );
                        }
                    }
                    if ( config.saverTimeout > 0 ) {
                        int want = SAVER_OFF;
                        if ( config.gracePeriod >= 0 && idleTime > config.saverTimeout + config.gracePeriod
                                && ( screen.saverState == SAVER_OFF || screen.saverState == SAVER_ACTIVE ) ) {
                            want = SAVER_LOCKED;
                        } else if ( idleTime > config.saverTimeout && screen.saverState == SAVER_OFF ) {
                            want = SAVER_ACTIVE;
                        } else if ( screen.saverState == SAVER_OFF ) {
                            CAP_SLEEP( config.saverTimeout - idleTime );
                        }
                        if ( want != SAVER_OFF ) {
                            enableScreenSaver( usr->xauth, usr->display, want );
                            usr->lockTime = NOW;
                            usr->lockTimeOffset = 0;
                            if ( screen.screenStandby == SCREEN_OFF ) {
                                // Screen was off before, but our ugly vmware ungrab hack might
                                // have simulated input and woke up the screen - enable standby
                                // again right away
                                setScreenDpms( usr->xauth, usr->display, SCREEN_OFF );
                            }
                        }
                    }
                } // End X11 handling
            }
            //printf( "Have User %d: %s Locked %d (%+d) Idle %d (%+d) -- Leader: %d\n", idx, usr->user, (int)usr->lockTime, usr->lockTimeOffset, (int)usr->lastActivity, usr->lastActivityOffset, (int)usr->sessionLeader );
            const int idleTime = NOW - usr->lastActivity;
            // Min of all sessions
            if ( idleTime >= 0 && idleTime < minIdleTime ) {
                minIdleTime = idleTime;
            }
            // See if we need to shorten sleep time for a pending session kill
            // or warn the user about an imminent session kill
            // or whether the screen saver is supposed to activate
            if ( config.logoutTimeout > 0 ) {
                // Idle logout is enabled
                const int remaining = config.logoutTimeout - idleTime;
                if ( remaining <= 2 ) {
                    killSession( usr );
                } else if ( remaining <= 65 ) {
                    warnUser( usr, WARN_LOGOUT, remaining );
                    CAP_SLEEP( remaining );
                } else if ( remaining < 310 ) {
                    warnUser( usr, WARN_LOGOUT_LOW, remaining );
                    CAP_SLEEP( remaining - ( ( remaining - 30 ) / 60 * 60 ) );
                } else {
                    CAP_SLEEP( remaining - 300 );
                }
                usr->logoutTime = NOW + remaining;
            }
            const time_t laMono = monoNOW - idleTime;
            if ( laMono > lastActivity ) {
                lastActivity = laMono;
            }
        } // End loop over users
        const time_t monoNOW = now();
        // See if any reboot or poweroff is due (if none is currently pending or still 5min+ away)
        if ( ! nextAction.force && config.shutdowns > 0 && ( nextAction.deadline == 0 || nextAction.deadline - monoNOW > 300 ) ) {
            if ( nextAction.deadline != 0 && nextAction.deadline - monoNOW > 300 ) {
                nextAction.deadline = 0;
            }
            const time_t NOW = time( NULL );
            time_t deadline;
            int delta;
            for ( idx = 0; idx < config.shutdowns; ++idx ) {
                deadline = combineTime( NOW, &config.shutdown[idx] );
                if ( deadline == 0 )
                    continue;
                delta = deadline - NOW;
                if ( delta >= -6 && ( nextAction.deadline == 0 || ( nextAction.deadline - monoNOW - 3 ) > delta ) ) {
                    // Engage
                    nextAction.deadline = monoNOW + ( delta < 0 ? 0 : delta );
                    nextAction.disarmed = false;
                    nextAction.action = config.shutdown[idx].action;
                    if ( delta < 300 ) {
                        printf( "Scheduling reboot/poweroff in %d seconds\n", delta );
                    }
                    CAP_SLEEP( delta );
                }
            }
        }
        // Check unused idle time
        if ( count == 0 ) {
            if ( config.poweroffTimeout != 0 ) {
                int delta = config.poweroffTimeout - ( monoNOW - lastActivity );
                if ( delta <= 0 ) {
                    execShutdown( POWEROFF );
                } else {
                    CAP_SLEEP( delta );
                }
            }
            if ( config.suspendTimeout != 0 ) {
                int delta = config.suspendTimeout - ( monoNOW - lastActivity );
                if ( delta <= 0 ) {
                    execShutdown( SUSPEND );
                } else {
                    CAP_SLEEP( delta );
                }
            }
        }
        if ( nextAction.deadline != 0 && ( count == 0 || minIdleTime >= config.minIdle || nextAction.force ) ) {
            // Some action seems pending
            const int remaining = nextAction.deadline - monoNOW;
            if ( remaining < -60 ) {
                if ( ! nextAction.disarmed ) {
                    fprintf( stderr, "Missed planned action!? Late by %d seconds. Ignored.\n", remaining );
                }
                nextAction.deadline = 0;
            } else if ( ! nextAction.disarmed ) {
                if ( remaining <= 3 ) {
                    // Execute!
                    execShutdown( nextAction.action );
                    nextAction.disarmed = true;
                    nextAction.force = false;
                } else if ( remaining < 310 ) {
                    enum Warning w = WARN_REBOOT;
                    if ( nextAction.action == POWEROFF ) {
                        w = WARN_POWEROFF;
                    }
                    for ( idx = 0; idx < count; ++idx ) {
                        warnUser( &users[idx], w, remaining );
                    }
                    CAP_SLEEP( remaining - ( ( remaining - 30 ) / 60 * 60 ) );
                } else {
                    CAP_SLEEP( remaining );
                }
            }
        }
        // Handle requests
        rpcHandle( listenFd );
        // Might have set a new scheduled action
        if ( nextAction.deadline != 0 && ( count == 0 || minIdleTime >= config.minIdle || nextAction.force ) ) {
            int delta = nextAction.deadline - monoNOW;
            CAP_SLEEP( delta );
        }
        // Sleep until next run
        //printf( "Sleeping %d seconds\n ", sleepTime );
        rpcWait( listenFd, sleepTime > 5 ? sleepTime : 5 );
        // Detect time changes
        endWall = time( NULL );
        endMono = now();
        // Adjust for clock changes
        const int diff = ( endWall - startWall ) - ( endMono - startMono );
        if ( diff != 0 ) {
            for ( idx = 0; idx < count; ++idx ) {
                if ( users[idx].display[0] == '\0' )
                    continue;
                if ( users[idx].lockTime != 0 ) {
                    users[idx].lockTimeOffset += diff;
                }
                users[idx].lastActivityOffset += diff;
            }
        }
        startWall = endWall;
        startMono = endMono;
    }
    return 0;
}

static time_t combineTime( const time_t now, const struct time * time )
{
    struct tm dtime;
    if ( localtime_r( &now, &dtime ) == NULL )
        return 0;
    time_t result;
    dtime.tm_hour = time->hour;
    dtime.tm_min = time->minute;
    dtime.tm_sec = 0;
    result = mktime( &dtime );
    if ( result + 6 < now ) {
        // Try again tomorrow
        dtime.tm_mday++;
        result = mktime( &dtime );
    }
    if ( result == -1 ) {
        perror( "Cannot convert struct tm to time_t via mktime" );
        return 0;
    }
    if ( result < now ) {
        fprintf( stderr, "combineTime: Even +1 day seems in the past!?\n" );
        return 0;
    }
    return result;
}

/**
 * This doesn't really use any clever logic but rather assumes that
 * it will be $HOME/.Xauthority
 */
static void getXAuthority( struct user *user )
{
    if ( user->xauth[0] != '\0' )
        return;
    char buffer[1000];
    struct passwd p, *ok;
    if ( getpwnam_r( user->user, &p, buffer, sizeof(buffer), &ok ) != 0 || ok == NULL ) {
        user->xauth[0] = '-';
        user->xauth[1] = '\0';
        return;
    }
    // Got valid data
    snprintf( user->xauth, AUTHLEN, "%s/.Xauthority", p.pw_dir );
    struct stat foo;
    if ( stat( user->xauth, &foo ) == -1 ) {
        user->xauth[0] = '-';
        user->xauth[1] = '\0';
    }
}

// cmdline options. Only support long for now.
static struct option long_options[] = {
    { "reboot", required_argument, NULL, 'r' },
    { "poweroff", required_argument, NULL, 'p' },
    { "poweroff-timeout", required_argument, NULL, 'pot' },
    { "suspend-timeout", required_argument, NULL, 'sbt' },
    { "logout-timeout", required_argument, NULL, 'lot' },
    { "screensaver-timeout", required_argument, NULL, 'sst' },
    { "dpms-timeout", required_argument, NULL, 'dpms' },
    { "grace-period", required_argument, NULL, 'gp' },
    { "min-idle", required_argument, NULL, 'min' },
    { "cmd", required_argument, NULL, 'cmd' },
    { "send", required_argument, NULL, 'send' },
    { "test", no_argument, NULL, 't' },
    { NULL, 0, NULL, 0 }
};

static bool parseTime( const char *str, struct time *result )
{
    char *next;
    result->hour = (int)strtol( str, &next, 10 );
    if ( result->hour < 0 || result->hour > 24 )
        return false;
    result->hour %= 24; // Allow 24:00
    if ( *next != ':' )
        return false;
    result->minute = (int)strtol( next + 1, &next, 10 );
    if ( result->minute < 0 || result->minute > 59 )
        return false;
    if ( *next != '\0' ) {
        fprintf( stderr, "Ignoring trailing garbage after minute\n" );
    }
    return true;
}

/**
 * Parse command line and fill all the tables / vars etc.
 * Return false if command line is unparsable.
 */
static bool parseCmdline( int argc, char **argv )
{
    int ch;
    config.shutdowns = 0;
    config.logoutTimeout = 0;
    config.poweroffTimeout = 0;
    config.suspendTimeout = 0;
    config.saverTimeout = 0;
    config.dpmsTimeout = 0;
    config.gracePeriod = -1;
    config.shutdownCommand = NULL;
    config.minIdle = 0;
    while ( ( ch = getopt_long( argc, argv, "", long_options, NULL ) ) != -1 ) {
        switch ( ch ) {
        case 'pot':
            config.poweroffTimeout = atoi( optarg );
            break;
        case 'sbt':
            config.suspendTimeout = atoi( optarg );
            break;
        case 'lot':
            config.logoutTimeout = atoi( optarg );
            break;
        case 'sst':
            config.saverTimeout = atoi( optarg );
            break;
        case 'dpms':
            config.dpmsTimeout = atoi( optarg );
            break;
        case 'gp':
            config.gracePeriod = atoi( optarg );
            break;
        case 'min':
            config.minIdle = atoi( optarg );
            break;
        case 'r':
        case 'p':
            if ( config.shutdowns < TABMAX ) {
                if ( parseTime( optarg, &config.shutdown[config.shutdowns] ) ) {
                    config.shutdown[config.shutdowns].action = ( ch == 'r' ? REBOOT : POWEROFF );
                    config.shutdowns++;
                } else {
                    fprintf( stderr, "Could not parse shutdown time %s\n", optarg );
                }
            } else {
                fprintf( stderr, "Ignoring shutdown time %s: Table full\n", optarg );
            }
            break;
        case 't':
            _testmode = 1;
            break;
        case 'cmd':
            config.shutdownCommand = strdup( optarg );
            break;
        case 'send':
            exit( !rpc_send( optarg ) );
        default:
            fprintf( stderr, "Unhandled command line option %d, aborting\n", ch );
            return false;
        }
    }
    return true;
}

static void execShutdown( enum Shutdown action )
{
    if ( action < 0 && action >= SHUTDOWN_ENUM_END ) {
        fprintf( stderr, "Invalid shutdown action %d\n", (int)action );
        return;
    }
    if ( _testmode ) {
        printf( "[dryrun] Not execution shutdown(%d)\n", (int)action );
        return;
    }
    if ( config.shutdownCommand != NULL ) {
        printf( "Executing shutdown via %s %s\n", config.shutdownCommand, shutdownActions[action] );
        run( true, config.shutdownCommand, shutdownActions[action], (char*)NULL );
        return;
    }
    // Builtin
    printf( "Executing shutdown via builtin handler for %s\n", shutdownActions[action] );
    switch ( action ) {
    case REBOOT:
    case POWEROFF:
    case SUSPEND:
        run( true, "systemctl", shutdownActions[action], (char*)NULL );
        break;
    case KEXEC:
        run( true, "systemctl", "isolate", "kexec.target", (char*)NULL );
        break;
    default:
        fprintf( stderr, "BUG! Unhandled shutdown action %d\n", (int)action );
        break;
    }
}

void main_getStatus( const char **nextString, time_t *deadline )
{
    if ( nextAction.deadline == 0 || nextAction.disarmed ) {
        *deadline = 0;
        return;
    }
    *deadline = ( nextAction.deadline - now() ) + time( NULL );
    *nextString = shutdownActions[nextAction.action];
}

struct user* main_getUser( const char *terminal )
{
    for ( int i = 0; i < USERS && users[i].mark; ++i ) {
        if ( strcmp( users[i].device, terminal ) == 0
                || strcmp( users[i].display, terminal ) == 0 ) {
            return &users[i];
        }
    }
    return NULL;
}

void main_queueAction( enum Shutdown action, int delta )
{
    if ( delta < 0 || action < 0 || action >= SHUTDOWN_ENUM_END )
        return;
    time_t monoNOW = now();
    if ( nextAction.deadline == 0 || ( nextAction.deadline - monoNOW - 3 ) > delta ) {
        // Engage
        nextAction.deadline = monoNOW + delta;
        nextAction.disarmed = false;
        nextAction.force = true;
        nextAction.action = action;
        printf( "RPC: Scheduling reboot/poweroff in %d seconds\n", delta );
    }
}