1
/* userspace library to interface with dazukofs
3
Copyright (C) 2008-2009 John Ogness
4
Author: John Ogness <dazukocode@ogness.net>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either
9
version 2.1 of the License, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU Lesser General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public
17
License along with this library; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
#include <sys/types.h>
32
struct dazukofs_handle
35
unsigned long event_id;
39
#define DAZUKOFS_ALLOWED_GROUPCHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
40
static int check_group_name(const char *gname)
42
size_t len = strlen(gname);
48
for (p = gname; *p; p++) {
49
if (strchr(DAZUKOFS_ALLOWED_GROUPCHARS, *p) == NULL)
56
dazukofs_handle_t dazukofs_open(const char *gname, int flags)
58
struct dazukofs_handle *hndl = NULL;
65
if (check_group_name(gname) != 0)
68
fd = open("/dev/dazukofs.ctrl", O_RDWR);
70
/* try to read at least
71
* (maybe the group already exists) */
72
fd = open("/dev/dazukofs.ctrl", O_RDONLY);
76
memset(buf, 0, sizeof(buf));
78
if (flags & DAZUKOFS_TRACK_GROUP)
79
snprintf(buf, sizeof(buf) - 1, "addtrack=%s", gname);
81
snprintf(buf, sizeof(buf) - 1, "add=%s", gname);
83
if (write(fd, buf, strlen(buf)) == -1)
86
lseek(fd, 0, SEEK_SET);
89
memset(buf, 0, sizeof(buf));
90
if (read(fd, buf, sizeof(buf)-1) == -1)
93
memset(key, 0, sizeof(key));
94
snprintf(key, sizeof(key) - 1, ":%s\n", gname);
102
if (gid < 0 || gid > 9)
103
goto error_out_close;
105
hndl = malloc(sizeof(struct dazukofs_handle));
107
goto error_out_close;
108
memset(hndl, 0, sizeof(struct dazukofs_handle));
110
hndl->group_name = strdup(gname);
111
if (!hndl->group_name)
114
memset(key, 0, sizeof(key));
115
snprintf(key, sizeof(key) - 1, "/dev/dazukofs.%d", gid);
117
hndl->dev_fd = open(key, O_RDWR);
118
if (hndl->dev_fd == -1)
126
if (hndl->group_name)
127
free(hndl->group_name);
136
int dazukofs_close(dazukofs_handle_t hndl, int flags)
142
if (flags & DAZUKOFS_REMOVE_GROUP) {
143
fd = open("/dev/dazukofs.ctrl", O_WRONLY);
147
memset(buf, 0, sizeof(buf));
148
snprintf(buf, sizeof(buf) - 1, "del=%s", hndl->group_name);
150
if (write(fd, buf, strlen(buf)) == -1) {
158
ret = close(hndl->dev_fd);
162
free(hndl->group_name);
171
int dazukofs_get_access(dazukofs_handle_t hndl, struct dazukofs_access *acc)
177
memset(buf, 0, sizeof(buf));
178
if (read(hndl->dev_fd, buf, sizeof(buf)-1) == -1)
181
p = strstr(buf, "id=");
185
hndl->event_id = strtoul(p, &p, 10);
187
p = strstr(p, "fd=");
191
acc->fd = (int)strtol(p, &p, 10);
193
p = strstr(p, "pid=");
197
acc->pid = strtoul(p, NULL, 10);
206
int dazukofs_return_access(dazukofs_handle_t hndl, struct dazukofs_access *acc)
211
if (close(acc->fd) != 0)
213
snprintf(buf, sizeof(buf)-1, "id=%lu r=%d", hndl->event_id,
215
buf[sizeof(buf)-1] = 0;
217
if (write(hndl->dev_fd, buf, strlen(buf)) == -1)
219
lseek(hndl->dev_fd, 0, SEEK_SET);
225
int dazukofs_get_filename(struct dazukofs_access *acc, char *buf, size_t bufsiz)
230
memset(proc, 0, sizeof(proc));
231
snprintf(proc, sizeof(proc) - 1, "/proc/self/fd/%d", acc->fd);
232
ret = readlink(proc, buf, bufsiz - 1);