2 * Virtio 9p Posix callback
4 * Copyright IBM, Corp. 2010
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "virtio-9p.h"
15 #include <arpa/inet.h>
18 #include <sys/socket.h>
21 static const char *rpath(FsContext
*ctx
, const char *path
)
23 /* FIXME: so wrong... */
24 static char buffer
[4096];
25 snprintf(buffer
, sizeof(buffer
), "%s/%s", ctx
->fs_root
, path
);
29 static int local_lstat(FsContext
*ctx
, const char *path
, struct stat
*stbuf
)
31 return lstat(rpath(ctx
, path
), stbuf
);
34 static int local_setuid(FsContext
*ctx
, uid_t uid
)
39 static uid_t cur_uid
= -1;
55 if (getgrouplist(pw
->pw_name
, pw
->pw_gid
, groups
, &ngroups
) == -1) {
59 if (setgroups(ngroups
, groups
)) {
63 if (setregid(-1, pw
->pw_gid
)) {
67 if (setreuid(-1, uid
)) {
76 static ssize_t
local_readlink(FsContext
*ctx
, const char *path
,
77 char *buf
, size_t bufsz
)
79 return readlink(rpath(ctx
, path
), buf
, bufsz
);
82 static int local_close(FsContext
*ctx
, int fd
)
87 static int local_closedir(FsContext
*ctx
, DIR *dir
)
92 static int local_open(FsContext
*ctx
, const char *path
, int flags
)
94 return open(rpath(ctx
, path
), flags
);
97 static DIR *local_opendir(FsContext
*ctx
, const char *path
)
99 return opendir(rpath(ctx
, path
));
102 static void local_rewinddir(FsContext
*ctx
, DIR *dir
)
104 return rewinddir(dir
);
107 static off_t
local_telldir(FsContext
*ctx
, DIR *dir
)
112 static struct dirent
*local_readdir(FsContext
*ctx
, DIR *dir
)
117 static void local_seekdir(FsContext
*ctx
, DIR *dir
, off_t off
)
119 return seekdir(dir
, off
);
122 static ssize_t
local_readv(FsContext
*ctx
, int fd
, const struct iovec
*iov
,
125 return readv(fd
, iov
, iovcnt
);
128 static off_t
local_lseek(FsContext
*ctx
, int fd
, off_t offset
, int whence
)
130 return lseek(fd
, offset
, whence
);
133 static ssize_t
local_writev(FsContext
*ctx
, int fd
, const struct iovec
*iov
,
136 return writev(fd
, iov
, iovcnt
);
139 static int local_chmod(FsContext
*ctx
, const char *path
, mode_t mode
)
141 return chmod(rpath(ctx
, path
), mode
);
144 static int local_mknod(FsContext
*ctx
, const char *path
, mode_t mode
, dev_t dev
)
146 return mknod(rpath(ctx
, path
), mode
, dev
);
149 static int local_mksock(FsContext
*ctx2
, const char *path
)
151 struct sockaddr_un addr
;
154 addr
.sun_family
= AF_UNIX
;
155 snprintf(addr
.sun_path
, 108, "%s", rpath(ctx2
, path
));
157 s
= socket(PF_UNIX
, SOCK_STREAM
, 0);
162 if (bind(s
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
171 static int local_mkdir(FsContext
*ctx
, const char *path
, mode_t mode
)
173 return mkdir(rpath(ctx
, path
), mode
);
176 static int local_fstat(FsContext
*ctx
, int fd
, struct stat
*stbuf
)
178 return fstat(fd
, stbuf
);
181 static int local_open2(FsContext
*ctx
, const char *path
, int flags
, mode_t mode
)
183 return open(rpath(ctx
, path
), flags
, mode
);
186 static int local_symlink(FsContext
*ctx
, const char *oldpath
,
189 return symlink(oldpath
, rpath(ctx
, newpath
));
192 static int local_link(FsContext
*ctx
, const char *oldpath
, const char *newpath
)
194 char *tmp
= qemu_strdup(rpath(ctx
, oldpath
));
201 err
= link(tmp
, rpath(ctx
, newpath
));
215 static int local_truncate(FsContext
*ctx
, const char *path
, off_t size
)
217 return truncate(rpath(ctx
, path
), size
);
220 static int local_rename(FsContext
*ctx
, const char *oldpath
,
226 tmp
= qemu_strdup(rpath(ctx
, oldpath
));
231 err
= rename(tmp
, rpath(ctx
, newpath
));
244 static int local_chown(FsContext
*ctx
, const char *path
, uid_t uid
, gid_t gid
)
246 return chown(rpath(ctx
, path
), uid
, gid
);
249 static int local_utime(FsContext
*ctx
, const char *path
,
250 const struct utimbuf
*buf
)
252 return utime(rpath(ctx
, path
), buf
);
255 static int local_remove(FsContext
*ctx
, const char *path
)
257 return remove(rpath(ctx
, path
));
260 static int local_fsync(FsContext
*ctx
, int fd
)
265 FileOperations local_ops
= {
266 .lstat
= local_lstat
,
267 .setuid
= local_setuid
,
268 .readlink
= local_readlink
,
269 .close
= local_close
,
270 .closedir
= local_closedir
,
272 .opendir
= local_opendir
,
273 .rewinddir
= local_rewinddir
,
274 .telldir
= local_telldir
,
275 .readdir
= local_readdir
,
276 .seekdir
= local_seekdir
,
277 .readv
= local_readv
,
278 .lseek
= local_lseek
,
279 .writev
= local_writev
,
280 .chmod
= local_chmod
,
281 .mknod
= local_mknod
,
282 .mksock
= local_mksock
,
283 .mkdir
= local_mkdir
,
284 .fstat
= local_fstat
,
285 .open2
= local_open2
,
286 .symlink
= local_symlink
,
288 .truncate
= local_truncate
,
289 .rename
= local_rename
,
290 .chown
= local_chown
,
291 .utime
= local_utime
,
292 .remove
= local_remove
,
293 .fsync
= local_fsync
,