1 /* ISC license. */
2
3 #include <sys/uio.h>
4 #include <stdint.h>
5 #include <errno.h>
6
7 #include <skalibs/posixishard.h>
8 #include <skalibs/error.h>
9 #include <skalibs/uint16.h>
10 #include <skalibs/genalloc.h>
11 #include <skalibs/gensetdyn.h>
12 #include <skalibs/textclient.h>
13
14 #include <s6/s6lock.h>
15
msghandler(struct iovec const * v,void * context)16 static int msghandler (struct iovec const *v, void *context)
17 {
18 s6lock_t *a = (s6lock_t *)context ;
19 char const *s = v->iov_base ;
20 char *p ;
21 uint16_t id ;
22 if (v->iov_len != 3) return (errno = EPROTO, 0) ;
23 uint16_unpack_big(s, &id) ;
24 p = GENSETDYN_P(char, &a->data, id) ;
25 if (*p == EBUSY) *p = s[2] ;
26 else if (error_isagain(*p)) *p = s[2] ? s[2] : EBUSY ;
27 else return (errno = EPROTO, 0) ;
28 if (!genalloc_append(uint16_t, &a->list, &id)) return 0 ;
29 return 1 ;
30 }
31
s6lock_update(s6lock_t * a)32 int s6lock_update (s6lock_t *a)
33 {
34 genalloc_setlen(uint16_t, &a->list, 0) ;
35 return textclient_update(&a->connection, &msghandler, a) ;
36 }
37