summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2020-10-12 09:23:57 +0200
committerSimon Rettberg2020-10-12 09:23:57 +0200
commitd712d3b76d6c2507f1f9b81349bc9b8b780f7a5c (patch)
treef4fbb4d9ddf739fe3f1bf929ec6c9544e72da1d2
parentFix potential strcmp() overflow, better packed struct for pipe (diff)
downloadidle-daemon-d712d3b76d6c2507f1f9b81349bc9b8b780f7a5c.tar.gz
idle-daemon-d712d3b76d6c2507f1f9b81349bc9b8b780f7a5c.tar.xz
idle-daemon-d712d3b76d6c2507f1f9b81349bc9b8b780f7a5c.zip
Add usercount to RPC, simplify timing calculation
-rw-r--r--src/main.c31
-rw-r--r--src/main.h2
-rw-r--r--src/rpc.c14
-rw-r--r--src/rpc.h2
4 files changed, 27 insertions, 22 deletions
diff --git a/src/main.c b/src/main.c
index 35d983e..a29fd35 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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 )
diff --git a/src/main.h b/src/main.h
index b1cd420..5d73e59 100644
--- a/src/main.h
+++ b/src/main.h
@@ -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 );
diff --git a/src/rpc.c b/src/rpc.c
index 28e4b2e..0689587 100644
--- a/src/rpc.c
+++ b/src/rpc.c
@@ -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 ) {
diff --git a/src/rpc.h b/src/rpc.h
index 9511cd0..7334cc9 100644
--- a/src/rpc.h
+++ b/src/rpc.h
@@ -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 );