-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathznowflaked.c
293 lines (251 loc) · 9.02 KB
/
znowflaked.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <fcntl.h>
#include <unistd.h>
#include <libconfig.h>
#include "czmq.h"
#define TIME_BITLEN 39
#define MACHINE_BITLEN 15
#define SEQ_BITLEN 10
#define MACHINE_BITSHIFT SEQ_BITLEN
#define TIME_BITSHIFT SEQ_BITLEN + MACHINE_BITLEN
#define MACHINE_MAX (1ULL << MACHINE_BITLEN) - 1
#define SEQ_MAX (1ULL << SEQ_BITLEN) - 1
#define DEFAULT_PORT 23138
#define EPOCH 1337000000ULL
// Signal handling
static int s_interrupted = 0;
static void
s_signal_handler (int signal_value)
{
s_interrupted = 1;
}
static void
s_catch_signals (void)
{
struct sigaction action;
action.sa_handler = s_signal_handler;
action.sa_flags = 0;
sigemptyset (&action.sa_mask);
sigaction (SIGINT, &action, NULL);
sigaction (SIGTERM, &action, NULL);
}
// Help
static void
print_help (void)
{
printf ("Example: `znowflaked -d -p 5555 -m 1234` ");
printf ("starts daemon for machine 1234 listening on port 5555\n\n");
}
// Building IDs
static inline uint64_t
get_ts (void)
{
struct timeval t;
gettimeofday (&t, NULL);
return (((t.tv_sec - EPOCH) * 1000) + (t.tv_usec / 1000));
}
static inline uint64_t
build_id (uint64_t ts, uint64_t machine, uint64_t seq)
{
return (ts << TIME_BITSHIFT) | (machine << MACHINE_BITSHIFT) | seq;
}
// Main
int
main (int argc, char **argv)
{
// Do some initial sanity checking
assert (TIME_BITLEN + MACHINE_BITLEN + SEQ_BITLEN == 64);
// Parse command-line arguments
int opt;
int has_port_opt = 0;
int has_machine_opt = 0;
int has_config_file_opt = 0;
int has_daemonize_opt = 0;
int machine_specified = 0;
const char *config_file_path;
int port = DEFAULT_PORT;
uint64_t machine;
while ((opt = getopt (argc, argv, "hp:m:f:d")) != -1) {
switch (opt) {
case 'h':
print_help ();
exit (EXIT_SUCCESS);
case 'p':
has_port_opt = 1;
port = atoi (optarg);
break;
case 'm':
has_machine_opt = 1;
machine = atoll (optarg);
machine_specified = 1;
break;
case 'f':
has_config_file_opt = 1;
config_file_path = optarg;
break;
case 'd':
has_daemonize_opt = 1;
break;
}
}
// Read the config file
if (has_config_file_opt) {
config_t cfg;
config_init (&cfg);
if (!config_read_file (&cfg, config_file_path)) {
config_destroy (&cfg);
fprintf (stderr, "Invalid config file\n");
exit (EXIT_FAILURE);
}
long unsigned int machine_from_file;
if (config_lookup_int (&cfg, "machine", &machine_from_file) && !has_machine_opt) {
machine_specified = 1;
machine = (uint64_t) machine_from_file;
}
long unsigned int port_from_file;
if (config_lookup_int (&cfg, "port", &port_from_file) && !has_port_opt)
port = (int) port_from_file;
}
// Sanity check the machine number
if (!machine_specified) {
fprintf (stderr, "No machine number specified.\n");
exit (EXIT_FAILURE);
}
else if (machine > MACHINE_MAX) {
fprintf (stderr, "Machine number too large. Cannot be greater than %llu\n", MACHINE_MAX);
exit (EXIT_FAILURE);
}
// Daemonize
static char *pid_file_path = "/var/run/znowflaked.pid";
int pid_file;
if (has_daemonize_opt) {
pid_t pid, sid;
pid = fork ();
if (pid < 0) {
exit (EXIT_FAILURE);
}
if (pid > 0) {
exit (EXIT_SUCCESS);
}
umask (0);
sid = setsid ();
if (sid < 0) {
exit (EXIT_FAILURE);
}
if ((chdir ("/")) < 0) {
exit (EXIT_FAILURE);
}
// Create and lock the pid file
pid_file = open (pid_file_path, O_CREAT | O_RDWR, 0666);
if (pid_file > 0) {
int rc = lockf (pid_file, F_TLOCK, 0);
if (rc) {
switch (errno) {
case EACCES:
case EAGAIN:
fprintf (stderr, "PID file already locked\n");
break;
case EBADF:
fprintf (stderr, "Bad pid file\n");
break;
default:
fprintf (stderr, "Could not lock pid file\n");
}
exit (EXIT_FAILURE);
}
char *pid_string = NULL;
int pid_string_len = asprintf (&pid_string, "%ld", (long) getpid ());
write (pid_file, pid_string, pid_string_len);
}
close (STDIN_FILENO);
close (STDOUT_FILENO);
close (STDERR_FILENO);
}
// Sleep for 1ms to prevent collisions
struct timespec ms;
ms.tv_sec = 0;
ms.tv_nsec = 1000000;
nanosleep (&ms, NULL);
// Initialize ZeroMQ
zctx_t *context = zctx_new ();
assert (context);
void *socket = zsocket_new (context, ZMQ_REP);
assert (socket);
assert (streq (zsocket_type_str (socket), "REP"));
int rc = zsocket_bind (socket, "tcp://*:%d", port);
if (rc != port) {
printf ("E: bind failed: %s\n", strerror (errno));
exit (EXIT_FAILURE);
}
// Start remembering the last timer tick
uint64_t ts = 0;
uint64_t last_ts = 0;
uint64_t seq = 0;
// Main loop
s_catch_signals ();
while (1) {
// Wait for the next request
zmsg_t *request_msg = zmsg_new ();
assert (request_msg);
request_msg = zmsg_recv (socket);
assert (request_msg);
zmsg_destroy (&request_msg);
// Grab a time click
last_ts = ts;
ts = get_ts ();
// Make sure the system clock wasn't reversed on us
if (ts < last_ts) {
// Wait until it catches up
while (ts <= last_ts) {
nanosleep (&ms, NULL);
ts = get_ts ();
}
}
// Increment the sequence number
if (ts != last_ts) {
// We're in a new time click, so reset the sequence
seq = 0;
}
else if (seq == SEQ_MAX) {
// Wrapped sequence, so wait for the next time tick
seq = 0;
nanosleep (&ms, NULL);
}
else {
// Still in the same time click
seq++;
}
// Build the ID and put it in network byte order
uint64_t id = build_id (ts, machine, seq);
uint64_t id_be64 = htobe64 (id);
// Reply
zmsg_t *reply_msg = zmsg_new ();
assert (reply_msg);
zframe_t *frame = zframe_new (&id_be64, 8);
assert (frame);
zmsg_push (reply_msg, frame);
assert (zmsg_size (reply_msg) == 1);
assert (zmsg_content_size (reply_msg) == 8);
zmsg_send (&reply_msg, socket);
assert (reply_msg == NULL);
// Exit program
if (s_interrupted) {
printf ("interrupt received, killing server…\n");
break;
}
}
zctx_destroy (&context);
if (has_daemonize_opt) {
if (pid_file > 0) {
unlink (pid_file_path);
close (pid_file);
}
}
exit (EXIT_SUCCESS);
}