summaryrefslogtreecommitdiffstats
path: root/src/rpc.c
blob: e49547e6f801e9a09f76405cd1eee97208a8870c (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
#include "rpc.h"
#include "util.h"
#include "main.h"
#include "userlist.h"

#include <poll.h>
#include <sys/types.h>  
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#define SOCKPATH "/run/idle-daemon"

static void handleClient( int fd, struct ucred *user );

int rpcOpen()
{
	int fd = socket( AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0 );
	if ( fd == -1 ) {
		perror( "Cannot create local RPC socket" );
		return -1;
	}
	struct sockaddr_un address = {
		.sun_family = AF_UNIX,
		.sun_path = SOCKPATH,
	};
	unlink( SOCKPATH );
	if ( bind( fd, (struct sockaddr *)&address, sizeof(address) ) == -1 ) {
		perror( "Could not bind RPC socket" );
		close( fd );
		return -1;
	}
	chmod( SOCKPATH, 0777 );
	if ( listen( fd, 10 ) == -1 ) {
		perror( "Could not listen() on RPC socket" );
		close( fd );
		return -1;
	}
	return fd;
}

void rpcWait( int listenFd, int seconds )
{
	waitRead( listenFd, seconds * 1000 );
}

void rpcHandle( int listenFd )
{
	int fd;
	while ( ( fd = accept( listenFd, NULL, NULL ) ) != -1 ) {
		// Determine who connected
		socklen_t len;
		struct ucred ucred;
		len = sizeof(struct ucred);
		if ( getsockopt( fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len ) == -1 ) {
			perror( "Could not get credentials of connection" );
			close( fd ); // TODO: Allow more for root (eg. cancel reboot/poweroff)
			continue;
		}
		if ( ! doublefork() )
			continue; // Parent continues
		handleClient( fd, &ucred );
		exit( 0 );
	}
	if ( errno != EAGAIN && errno != EWOULDBLOCK ) {
		perror( "accept() on RPC socket failed" );
	}
}

static void handleClient( int fd, struct ucred *user )
{
	//printf( "Credentials from SO_PEERCRED: pid=%ld, euid=%ld, egid=%ld\n",
	//	(long) ucred.pid, (long) ucred.uid, (long) ucred.gid );
	// Make socket blocking (should be default on linux after accept() but...)
	(void)user;
	int flags = fcntl( fd, F_GETFL, 0 );
	if ( flags != -1 ) {
		fcntl( fd, F_SETFL, flags & ~O_NONBLOCK );
	}
	// But set timeouts
	struct timeval tv = {
		.tv_sec = 2,
	};
	setsockopt( fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv) );
	setsockopt( fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tv, sizeof(tv) );
	// Now read request
	char buffer[1000];
	ssize_t len = read( fd, buffer, sizeof(buffer) - 1 );
	if ( len <= 0 )
		return;
	buffer[len] = '\0';
	if ( strncmp( buffer, "get", 3 ) != 0 ) {
		write( fd, "error", 5 );
	} else {
		// Get request
		FILE *s = fdopen( fd, "w" );
		if ( s == NULL ) {
			perror( "Cannot wrap socket in stream" );
			return;
		}
		// 1) Global state
		time_t deadline;
		const char *name = "none";
		main_getStatus( &name, &deadline );
		fprintf( s, "[General]\n"
				"nextAction=%s\n"
				"nextActionTime=%lld\n",
				name, (long long)deadline );
		// 2) Requested sessions
		char *tok = strtok( buffer + 4, " \t\n\r" );
		while ( tok != NULL ) {
			struct user *user = main_getUser( tok );
			if ( user != NULL ) {
				fprintf( s, "[%s]\n"
						"logoutTime=%lld\n"
						"locked=%d\n",
						tok, (long long)user->logoutTime, (int)user->isLocked );
			}
			tok = strtok( NULL, " \t\n\r" );
		}
		fflush( s );
	}
	shutdown( fd, SHUT_WR );
	read( fd, buffer, sizeof(buffer) );
	close( fd );
}