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
|
/*
* Copyright 2017 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Authors: Jérôme Glisse <jglisse@redhat.com>
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include "compote.h"
#include "compote-uapi.h"
int compote_context_new(compote_context_t **ctxp)
{
compote_context_t *ctx;
int fd;
fd = open("/dev/compote", O_RDWR, 0);
if (fd < 0) {
fprintf(stderr, "could not open compote device\n");
return -ENODEV;
}
ctx = calloc(1, sizeof(*ctx));
if (ctx == NULL) {
close(fd);
return -ENOMEM;
}
ctx->fd = fd;
*ctxp = ctx;
return 0;
}
void compote_context_del(compote_context_t **ctxp)
{
compote_context_t *ctx = *ctxp;
*ctxp = NULL;
close(ctx->fd);
free(ctx);
}
int compote_context_ioctl(compote_context_t *ctx, int command, void *arg)
{
do {
int ret;
ret = ioctl(ctx->fd, command, arg);
if (ret && errno != EINTR) {
printf("ret %d errno %d\n", ret, errno);
return ret;
}
} while (errno == EINTR);
return 0;
}
int compote_mo_new(compote_context_t *ctx, compote_mo_t **mop, uint64_t nbytes)
{
struct compote_ioctl_mem_alloc arg;
compote_mo_t *mo;
int ret;
*mop = NULL;
mo = calloc(1, sizeof(*mo));
if (mo == NULL) {
return -ENOMEM;
}
arg.nbytes = nbytes;
ret = compote_context_ioctl(ctx, COMPOTE_IOCTL_MEM_ALLOC, &arg);
if (ret) {
free(mo);
return ret;
}
mo->foffset = arg.foffset;
mo->nbytes = nbytes;
*mop = mo;
return 0;
}
void compote_mo_del(compote_context_t *ctx, compote_mo_t **mop)
{
struct compote_ioctl_mem_free arg;
compote_mo_t *mo = *mop;
*mop = NULL;
if (mo == NULL) {
return;
}
arg.foffset = mo->foffset;
compote_context_ioctl(ctx, COMPOTE_IOCTL_MEM_FREE, &arg);
free(mo);
}
int main(int argc, char *argv[])
{
compote_context_t *ctx;
compote_mo_t *mo;
int ret;
ret = compote_context_new(&ctx);
if (ret) {
return ret;
}
ret = compote_mo_new(ctx, &mo, 64 << 10);
if (ret) {
return ret;
}
printf("La compote c'est bon !\n");
compote_mo_del(ctx, &mo);
compote_context_del(&ctx);
return 0;
}
|