-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightserver.c
217 lines (180 loc) · 3.83 KB
/
lightserver.c
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
#include <systemd/sd-daemon.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/fcntl.h>
#include <time.h>
#include <sys/timerfd.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
static int socket_from_systemd()
{
/* Return a fd > 0 if given a socket, 0 if not, and -1 on error. */
int nr = sd_listen_fds(1);
if (nr == 1)
return SD_LISTEN_FDS_START + 0;
else if (nr == 0)
return 0;
else {
fprintf(stderr, "Too many descriptors passed by systemd.\n");
return -1;
}
}
static int create_own_socket()
{
/* Return a fd or -1 on error. */
struct sockaddr_un address;
char *ADDRESS = "/run/lightserver.socket";
int rc;
rc = unlink(ADDRESS);
if (rc == -1 && errno != ENOENT) {
perror("unlink()");
return -1;
}
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) {
perror("socket()");
return -1;
}
memset(&address, 0, sizeof address);
address.sun_family = AF_UNIX;
strncpy(address.sun_path, ADDRESS, sizeof address.sun_path - 1);
rc = bind(fd, (struct sockaddr *) &address, sizeof address);
if (rc == -1) {
perror("bind()");
return -1;
}
rc = listen(fd, 1);
if (rc == -1) {
perror("listen()");
return -1;
}
return fd;
}
static int timer_start(long long period_ns)
{
int timer, rc;
struct itimerspec period;
timer = timerfd_create(CLOCK_MONOTONIC, 0);
if (timer == -1) {
perror("timerfd_create()");
return -1;
}
memset(&period, 0, sizeof period);
period.it_value.tv_nsec = period_ns;
period.it_interval.tv_nsec = period_ns;
rc = timerfd_settime(timer, 0, &period, 0);
if (rc == -1) {
perror("timerfd_settime()");
return -1;
}
return timer;
}
static void timer_stop(int timer)
{
close(timer);
}
static int timer_wait(int timer)
{
uint64_t expirations;
int rc = read(timer, &expirations, sizeof expirations);
if (rc == -1) {
perror("timer read()");
return -1;
}
return 0;
}
static int read_lux(char *s, int capacity)
{
const char *SYSFS_ATTRIBUTE = "/dev/sine";
int fd;
int rc;
fd = open(SYSFS_ATTRIBUTE, O_RDONLY, O_NONBLOCK);
if (fd == -1) {
perror("sysfs open()");
return -1;
}
rc = read(fd, s, capacity - 1);
if (rc == -1) {
if (errno == EBUSY) {
// device in suspend
sprintf(s, "0\n");
rc = 0;
} else
perror("sysfs read()");
}
close(fd);
return rc == -1 ? -1 : 0;
}
static int serve(int fd)
{
const long long DATA_RATE_NS = 100LL * 1000LL * 1000LL; /* 100 ms */
int timer;
int rc;
rc = fcntl(fd, F_SETFL, O_NONBLOCK);
if (rc == -1) {
perror("fcntl()");
return -1;
}
timer = timer_start(DATA_RATE_NS); /* Stop before returning. */
if (timer == -1)
return -1;
for (;;) {
const int CAPACITY = 80;
char message[CAPACITY];
char *cursor;
struct timeval now;
long long ms;
memset(message, 0, CAPACITY);
rc = timer_wait(timer);
if (rc == -1)
break;
rc = gettimeofday(&now, 0);
if (rc == -1)
break;
rc = read_lux(message, CAPACITY);
if (rc == -1)
break;
ms = now.tv_sec * 1000LL + now.tv_usec / 1000LL;
cursor = strchr(message, '\n');
snprintf(cursor, CAPACITY - (cursor - message), " %lli\n", ms);
rc = send(fd, message, strnlen(message, CAPACITY), MSG_NOSIGNAL);
if (rc == -1) {
if (errno == EAGAIN) {
fprintf(stderr, "write() blocked, closing");
rc = 0;
} else if (errno == EPIPE) {
rc = 0;
} else
perror("send()");
break;
}
}
timer_stop(timer);
return rc;
}
int main(int argc, char** argv)
{
int server_fd;
server_fd = socket_from_systemd();
if (server_fd == 0)
server_fd = create_own_socket();
if (server_fd < 0)
exit(EXIT_FAILURE);
for (;;) {
int client_fd, rc;
client_fd = accept(server_fd, 0, 0);
if (client_fd == -1) {
perror("accept()");
exit(EXIT_FAILURE);
}
rc = serve(client_fd);
close(client_fd);
if (rc == -1)
exit(EXIT_FAILURE);
}
}