1 /*
2
3 Copyright (c) 2017 Martin Sustrik
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom
10 the Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 IN THE SOFTWARE.
22
23 */
24
25 #include <assert.h>
26 #include <errno.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30
31 #include "../../libdillimpl.h"
32
33 static const int quux_type_placeholder = 0;
34 static const void *quux_type = &quux_type_placeholder;
35
36 struct quux {
37 struct hvfs hvfs;
38 int u;
39 };
40
41 static void *quux_hquery(struct hvfs *hvfs, const void *id);
42 static void quux_hclose(struct hvfs *hvfs);
43
quux_attach(int u)44 int quux_attach(int u) {
45 int err;
46 struct quux *self = malloc(sizeof(struct quux));
47 if(!self) {err = ENOMEM; goto error1;}
48 self->hvfs.query = quux_hquery;
49 self->hvfs.close = quux_hclose;
50 self->u = hown(u);
51 int h = hmake(&self->hvfs);
52 if(h < 0) {int err = errno; goto error2;}
53 return h;
54 error2:
55 free(self);
56 error1:
57 errno = err;
58 return -1;
59 }
60
quux_detach(int h)61 int quux_detach(int h) {
62 struct quux *self = hquery(h, quux_type);
63 if(!self) return -1;
64 int u = self->u;
65 free(self);
66 return u;
67 }
68
quux_hquery(struct hvfs * hvfs,const void * type)69 static void *quux_hquery(struct hvfs *hvfs, const void *type) {
70 struct quux *self = (struct quux*)hvfs;
71 if(type == quux_type) return self;
72 errno = ENOTSUP;
73 return NULL;
74 }
75
quux_hclose(struct hvfs * hvfs)76 static void quux_hclose(struct hvfs *hvfs) {
77 struct quux *self = (struct quux*)hvfs;
78 free(self);
79 }
80
client(int s)81 coroutine void client(int s) {
82 int q = quux_attach(s);
83 assert(q >= 0);
84 /* Do something useful here! */
85 s = quux_detach(q);
86 assert(s >= 0);
87 int rc = hclose(s);
88 assert(rc == 0);
89 }
90
main(void)91 int main(void) {
92 int ss[2];
93 int rc = ipc_pair(ss);
94 assert(rc == 0);
95 go(client(ss[0]));
96 int q = quux_attach(ss[1]);
97 assert(q >= 0);
98 /* Do something useful here! */
99 int s = quux_detach(q);
100 assert(s >= 0);
101 rc = hclose(s);
102 assert(rc == 0);
103 return 0;
104 }
105
106