Fix hw/gt64xxx.c compilation with DEBUG defined
[qemu.git] / hw / virtio-9p-local.c
blob1afb7315486db770a4632d6fb044893294da5a2e
1 /*
2 * Virtio 9p Posix callback
4 * Copyright IBM, Corp. 2010
6 * Authors:
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.
13 #include "virtio.h"
14 #include "virtio-9p.h"
15 #include <arpa/inet.h>
16 #include <pwd.h>
17 #include <grp.h>
18 #include <sys/socket.h>
19 #include <sys/un.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);
26 return buffer;
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)
36 struct passwd *pw;
37 gid_t groups[33];
38 int ngroups;
39 static uid_t cur_uid = -1;
41 if (cur_uid == uid) {
42 return 0;
45 if (setreuid(0, 0)) {
46 return -1;
49 pw = getpwuid(uid);
50 if (pw == NULL) {
51 return -1;
54 ngroups = 33;
55 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1) {
56 return -1;
59 if (setgroups(ngroups, groups)) {
60 return -1;
63 if (setregid(-1, pw->pw_gid)) {
64 return -1;
67 if (setreuid(-1, uid)) {
68 return -1;
71 cur_uid = uid;
73 return 0;
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)
84 return close(fd);
87 static int local_closedir(FsContext *ctx, DIR *dir)
89 return closedir(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)
109 return telldir(dir);
112 static struct dirent *local_readdir(FsContext *ctx, DIR *dir)
114 return readdir(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,
123 int iovcnt)
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,
134 int iovcnt)
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;
152 int s;
154 addr.sun_family = AF_UNIX;
155 snprintf(addr.sun_path, 108, "%s", rpath(ctx2, path));
157 s = socket(PF_UNIX, SOCK_STREAM, 0);
158 if (s == -1) {
159 return -1;
162 if (bind(s, (struct sockaddr *)&addr, sizeof(addr))) {
163 close(s);
164 return -1;
167 close(s);
168 return 0;
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,
187 const char *newpath)
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));
195 int err, serrno = 0;
197 if (tmp == NULL) {
198 return -ENOMEM;
201 err = link(tmp, rpath(ctx, newpath));
202 if (err == -1) {
203 serrno = errno;
206 qemu_free(tmp);
208 if (err == -1) {
209 errno = serrno;
212 return err;
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,
221 const char *newpath)
223 char *tmp;
224 int err;
226 tmp = qemu_strdup(rpath(ctx, oldpath));
227 if (tmp == NULL) {
228 return -1;
231 err = rename(tmp, rpath(ctx, newpath));
232 if (err == -1) {
233 int serrno = errno;
234 qemu_free(tmp);
235 errno = serrno;
236 } else {
237 qemu_free(tmp);
240 return err;
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)
262 return fsync(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,
271 .open = local_open,
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,
287 .link = local_link,
288 .truncate = local_truncate,
289 .rename = local_rename,
290 .chown = local_chown,
291 .utime = local_utime,
292 .remove = local_remove,
293 .fsync = local_fsync,