use readdir_r instead of readdir for reentrancy

Signed-off-by: Harsh Prateek Bora <harsh@linux.vnet.ibm.com>
Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
This commit is contained in:
Harsh Prateek Bora 2011-05-18 17:23:00 +05:30 committed by Aneesh Kumar K.V
parent d208a0e005
commit 5f524c1ebc
5 changed files with 27 additions and 16 deletions

View File

@ -78,7 +78,7 @@ typedef struct FileOperations
int (*open2)(FsContext *, const char *, int, FsCred *); int (*open2)(FsContext *, const char *, int, FsCred *);
void (*rewinddir)(FsContext *, DIR *); void (*rewinddir)(FsContext *, DIR *);
off_t (*telldir)(FsContext *, DIR *); off_t (*telldir)(FsContext *, DIR *);
struct dirent *(*readdir)(FsContext *, DIR *); int (*readdir_r)(FsContext *, DIR *, struct dirent *, struct dirent **);
void (*seekdir)(FsContext *, DIR *, off_t); void (*seekdir)(FsContext *, DIR *, off_t);
ssize_t (*preadv)(FsContext *, int, const struct iovec *, int, off_t); ssize_t (*preadv)(FsContext *, int, const struct iovec *, int, off_t);
ssize_t (*pwritev)(FsContext *, int, const struct iovec *, int, off_t); ssize_t (*pwritev)(FsContext *, int, const struct iovec *, int, off_t);

View File

@ -17,16 +17,16 @@
#include "qemu-coroutine.h" #include "qemu-coroutine.h"
#include "virtio-9p-coth.h" #include "virtio-9p-coth.h"
int v9fs_co_readdir(V9fsState *s, V9fsFidState *fidp, struct dirent **dent) int v9fs_co_readdir_r(V9fsState *s, V9fsFidState *fidp, struct dirent *dent,
struct dirent **result)
{ {
int err; int err;
v9fs_co_run_in_worker( v9fs_co_run_in_worker(
{ {
errno = 0; errno = 0;
/*FIXME!! need to switch to readdir_r */ err = s->ops->readdir_r(&s->ctx, fidp->fs.dir, dent, result);
*dent = s->ops->readdir(&s->ctx, fidp->fs.dir); if (!*result && errno) {
if (!*dent && errno) {
err = -errno; err = -errno;
} else { } else {
err = 0; err = 0;

View File

@ -57,8 +57,8 @@ typedef struct V9fsThPool {
extern void co_run_in_worker_bh(void *); extern void co_run_in_worker_bh(void *);
extern int v9fs_init_worker_threads(void); extern int v9fs_init_worker_threads(void);
extern int v9fs_co_readlink(V9fsState *, V9fsString *, V9fsString *); extern int v9fs_co_readlink(V9fsState *, V9fsString *, V9fsString *);
extern int v9fs_co_readdir(V9fsState *, V9fsFidState *, extern int v9fs_co_readdir_r(V9fsState *, V9fsFidState *,
struct dirent **); struct dirent *, struct dirent **result);
extern off_t v9fs_co_telldir(V9fsState *, V9fsFidState *); extern off_t v9fs_co_telldir(V9fsState *, V9fsFidState *);
extern void v9fs_co_seekdir(V9fsState *, V9fsFidState *, off_t); extern void v9fs_co_seekdir(V9fsState *, V9fsFidState *, off_t);
extern void v9fs_co_rewinddir(V9fsState *, V9fsFidState *); extern void v9fs_co_rewinddir(V9fsState *, V9fsFidState *);

View File

@ -165,9 +165,10 @@ static off_t local_telldir(FsContext *ctx, DIR *dir)
return telldir(dir); return telldir(dir);
} }
static struct dirent *local_readdir(FsContext *ctx, DIR *dir) static int local_readdir_r(FsContext *ctx, DIR *dir, struct dirent *entry,
struct dirent **result)
{ {
return readdir(dir); return readdir_r(dir, entry, result);
} }
static void local_seekdir(FsContext *ctx, DIR *dir, off_t off) static void local_seekdir(FsContext *ctx, DIR *dir, off_t off)
@ -532,7 +533,7 @@ FileOperations local_ops = {
.opendir = local_opendir, .opendir = local_opendir,
.rewinddir = local_rewinddir, .rewinddir = local_rewinddir,
.telldir = local_telldir, .telldir = local_telldir,
.readdir = local_readdir, .readdir_r = local_readdir_r,
.seekdir = local_seekdir, .seekdir = local_seekdir,
.preadv = local_preadv, .preadv = local_preadv,
.pwritev = local_pwritev, .pwritev = local_pwritev,

View File

@ -1495,17 +1495,20 @@ static int v9fs_do_readdir_with_stat(V9fsState *s, V9fsPDU *pdu,
int32_t count = 0; int32_t count = 0;
struct stat stbuf; struct stat stbuf;
off_t saved_dir_pos; off_t saved_dir_pos;
struct dirent *dent; struct dirent *dent, *result;
/* save the directory position */ /* save the directory position */
saved_dir_pos = v9fs_co_telldir(s, fidp); saved_dir_pos = v9fs_co_telldir(s, fidp);
if (saved_dir_pos < 0) { if (saved_dir_pos < 0) {
return saved_dir_pos; return saved_dir_pos;
} }
dent = g_malloc(sizeof(struct dirent));
while (1) { while (1) {
v9fs_string_init(&name); v9fs_string_init(&name);
err = v9fs_co_readdir(s, fidp, &dent); err = v9fs_co_readdir_r(s, fidp, dent, &result);
if (err || !dent) { if (err || !result) {
break; break;
} }
v9fs_string_sprintf(&name, "%s/%s", fidp->path.data, dent->d_name); v9fs_string_sprintf(&name, "%s/%s", fidp->path.data, dent->d_name);
@ -1524,6 +1527,7 @@ static int v9fs_do_readdir_with_stat(V9fsState *s, V9fsPDU *pdu,
v9fs_co_seekdir(s, fidp, saved_dir_pos); v9fs_co_seekdir(s, fidp, saved_dir_pos);
v9fs_stat_free(&v9stat); v9fs_stat_free(&v9stat);
v9fs_string_free(&name); v9fs_string_free(&name);
g_free(dent);
return count; return count;
} }
count += len; count += len;
@ -1532,6 +1536,7 @@ static int v9fs_do_readdir_with_stat(V9fsState *s, V9fsPDU *pdu,
saved_dir_pos = dent->d_off; saved_dir_pos = dent->d_off;
} }
out: out:
g_free(dent);
v9fs_string_free(&name); v9fs_string_free(&name);
if (err < 0) { if (err < 0) {
return err; return err;
@ -1628,16 +1633,19 @@ static int v9fs_do_readdir(V9fsState *s, V9fsPDU *pdu,
int len, err = 0; int len, err = 0;
int32_t count = 0; int32_t count = 0;
off_t saved_dir_pos; off_t saved_dir_pos;
struct dirent *dent; struct dirent *dent, *result;
/* save the directory position */ /* save the directory position */
saved_dir_pos = v9fs_co_telldir(s, fidp); saved_dir_pos = v9fs_co_telldir(s, fidp);
if (saved_dir_pos < 0) { if (saved_dir_pos < 0) {
return saved_dir_pos; return saved_dir_pos;
} }
dent = g_malloc(sizeof(struct dirent));
while (1) { while (1) {
err = v9fs_co_readdir(s, fidp, &dent); err = v9fs_co_readdir_r(s, fidp, dent, &result);
if (err || !dent) { if (err || !result) {
break; break;
} }
v9fs_string_init(&name); v9fs_string_init(&name);
@ -1646,6 +1654,7 @@ static int v9fs_do_readdir(V9fsState *s, V9fsPDU *pdu,
/* Ran out of buffer. Set dir back to old position and return */ /* Ran out of buffer. Set dir back to old position and return */
v9fs_co_seekdir(s, fidp, saved_dir_pos); v9fs_co_seekdir(s, fidp, saved_dir_pos);
v9fs_string_free(&name); v9fs_string_free(&name);
g_free(dent);
return count; return count;
} }
/* /*
@ -1667,6 +1676,7 @@ static int v9fs_do_readdir(V9fsState *s, V9fsPDU *pdu,
v9fs_string_free(&name); v9fs_string_free(&name);
saved_dir_pos = dent->d_off; saved_dir_pos = dent->d_off;
} }
g_free(dent);
if (err < 0) { if (err < 0) {
return err; return err;
} }