add changelog for 1.13
[beanstalkd.git] / serv.c
blob1dd9e299c8e85557ab0f17d4b87dda77eaf9d090
1 #include "dat.h"
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <sys/socket.h>
6 struct Server srv = {
7 .port = Portdef,
8 .wal = {
9 .filesize = Filesizedef,
10 .wantsync = 1,
11 .syncrate = DEFAULT_FSYNC_MS * 1000000,
15 // srv_acquire_wal tries to lock the wal dir specified by s->wal and
16 // replay entries from it to initialize the s state with jobs.
17 // On errors it exits from the program.
18 void srv_acquire_wal(Server *s) {
19 if (s->wal.use) {
20 // We want to make sure that only one beanstalkd tries
21 // to use the wal directory at a time. So acquire a lock
22 // now and never release it.
23 if (!waldirlock(&s->wal)) {
24 twarnx("failed to lock wal dir %s", s->wal.dir);
25 exit(10);
28 Job list = {.prev=NULL, .next=NULL};
29 list.prev = list.next = &list;
30 walinit(&s->wal, &list);
31 int ok = prot_replay(s, &list);
32 if (!ok) {
33 twarnx("failed to replay log");
34 exit(1);
39 void
40 srvserve(Server *s)
42 Socket *sock;
44 if (sockinit() == -1) {
45 twarnx("sockinit");
46 exit(1);
49 s->sock.x = s;
50 s->sock.f = (Handle)srvaccept;
51 s->conns.less = conn_less;
52 s->conns.setpos = conn_setpos;
54 if (sockwant(&s->sock, 'r') == -1) {
55 twarn("sockwant");
56 exit(2);
60 for (;;) {
61 int64 period = prottick(s);
63 int rw = socknext(&sock, period);
64 if (rw == -1) {
65 twarnx("socknext");
66 exit(1);
69 if (rw) {
70 sock->f(sock->x, rw);
76 void
77 srvaccept(Server *s, int ev)
79 h_accept(s->sock.fd, ev, s);