diff options
author | Simon Rettberg | 2020-10-12 09:23:57 +0200 |
---|---|---|
committer | Simon Rettberg | 2020-10-12 09:23:57 +0200 |
commit | d712d3b76d6c2507f1f9b81349bc9b8b780f7a5c (patch) | |
tree | f4fbb4d9ddf739fe3f1bf929ec6c9544e72da1d2 /src | |
parent | Fix potential strcmp() overflow, better packed struct for pipe (diff) | |
download | idle-daemon-d712d3b76d6c2507f1f9b81349bc9b8b780f7a5c.tar.gz idle-daemon-d712d3b76d6c2507f1f9b81349bc9b8b780f7a5c.tar.xz idle-daemon-d712d3b76d6c2507f1f9b81349bc9b8b780f7a5c.zip |
Add usercount to RPC, simplify timing calculation
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 31 | ||||
-rw-r--r-- | src/main.h | 2 | ||||
-rw-r--r-- | src/rpc.c | 14 | ||||
-rw-r--r-- | src/rpc.h | 2 |
4 files changed, 27 insertions, 22 deletions
@@ -84,7 +84,7 @@ int main( int argc, char **argv ) startWall = time( NULL ); startMono = now(); // Calculate shortest possible sleep time without delaying any timeout related actions - int defaultSleep = 120; + int defaultSleep = 60; if ( config.logoutTimeout > 0 && config.logoutTimeout < defaultSleep ) { defaultSleep = config.logoutTimeout; } @@ -293,7 +293,7 @@ int main( int argc, char **argv ) nextAction.disarmed = true; nextAction.force = false; } else if ( remaining < 310 ) { - if ( remaining % 30 < 10 ) { + if ( remaining % 30 < 8 ) { enum Warning w = WARN_REBOOT; if ( nextAction.action == POWEROFF ) { w = WARN_POWEROFF; @@ -302,30 +302,30 @@ int main( int argc, char **argv ) warn_showDefaultWarning( &users[idx], w, remaining ); } } - CAP_SLEEP( remaining - ( ( remaining - 30 ) / 60 * 60 + 2 ) ); + CAP_SLEEP( ( remaining % 15 ) - 2 ); } else { CAP_SLEEP( remaining - 305 ); } } } do { - const time_t oldDeadline = nextAction.deadline; // Handle requests - rpc_handle( listenFd ); - // Might have set a new scheduled action - if ( nextAction.deadline != oldDeadline ) { - int delta = nextAction.deadline - monoNOW; - CAP_SLEEP( delta - 305 ); - } + const time_t oldDeadline = nextAction.deadline; // Sleep until next run - //printf( "Sleeping %d seconds\n ", sleepTime ); - rpc_wait( listenFd, sleepTime > 5 ? sleepTime : 5 ); + int sn = sleepTime - ( endMono - startMono ); + //printf( "Sleeping %d seconds\n ", sn ); + if ( rpc_wait( listenFd, sn < 1 ? 1 : sn ) ) { + rpc_handle( listenFd ); + // Might have set a new scheduled action, run main loop again + if ( nextAction.deadline != oldDeadline ) { + CAP_SLEEP( 1 ); + } + } // Detect time changes endWall = time( NULL ); endMono = now(); // Sloppy way of preventing we loop too fast -- handle rpc callbacks but don't run main logic - sleepTime -= ( endMono - startMono ); - } while ( endMono - startMono < 2 ); + } while ( sleepTime > ( endMono - startMono ) ); // Adjust for clock changes const int diff = ( endWall - startWall ) - ( endMono - startMono ); if ( diff != 0 ) { @@ -537,7 +537,7 @@ static void execShutdown( enum Shutdown action ) } } -void main_getStatus( const char **nextString, time_t *deadline ) +void main_getStatus( const char **nextString, time_t *deadline, int *numUsers ) { if ( nextAction.deadline == 0 || nextAction.disarmed ) { *deadline = 0; @@ -545,6 +545,7 @@ void main_getStatus( const char **nextString, time_t *deadline ) } *deadline = ( nextAction.deadline - now() ) + time( NULL ); *nextString = shutdownActions[nextAction.action]; + *numUsers = userCount; } struct user* main_getUser( const char *terminal ) @@ -17,7 +17,7 @@ struct time { enum Shutdown action; }; -void main_getStatus( const char **nextAction, time_t *deadline ); +void main_getStatus( const char **nextAction, time_t *deadline, int *numUsers ); struct user* main_getUser( const char *terminal ); @@ -68,15 +68,17 @@ int rpc_open( void ) return fd; } -void rpc_wait( int listenFd, int seconds ) +bool rpc_wait( int listenFd, int seconds ) { struct pollfd pd[2] = { { .fd = listenFd, .events = POLLIN | POLLHUP | POLLRDHUP }, { .fd = rpcPipe.read, .events = POLLIN | POLLHUP | POLLRDHUP }, }; - if ( poll( pd, 2, seconds * 1000 ) == -1 ) { + int ret = poll( pd, 2, seconds * 1000 ); + if ( ret == -1 ) { perror( "Error polling RPC sockets" ); } + return ret > 0; } void rpc_handle( int listenFd ) @@ -167,11 +169,13 @@ static void handleClient( int fd, struct ucred *creds ) // 1) Global state time_t deadline; const char *name = "none"; - main_getStatus( &name, &deadline ); + int userCount; + main_getStatus( &name, &deadline, &userCount ); fprintf( s, "[General]\n" "nextAction=%s\n" - "nextActionTime=%lld\n", - name, (long long)deadline ); + "nextActionTime=%lld\n" + "userCount=%d\n", + name, (long long)deadline, userCount ); // 2) Requested sessions char *tok = strtok( buffer + 4, " \t\n\r" ); while ( tok != NULL ) { @@ -7,7 +7,7 @@ int rpc_open( void ); void rpc_handle( int listenFd ); -void rpc_wait( int listenFd, int seconds ); +bool rpc_wait( int listenFd, int seconds ); bool rpc_send( const char *data ); |