util: define union dirent_storage and make use of it everywhere

Make sure to allocate enough space for readdir_r().

https://bugzilla.redhat.com/show_bug.cgi?id=858754
This commit is contained in:
Lennart Poettering 2012-09-19 22:21:09 +02:00
parent 57f3067825
commit 7d5e9c0f60
11 changed files with 52 additions and 30 deletions

View File

@ -176,11 +176,12 @@ static int enumerate_dir(Hashmap *top, Hashmap *bottom, const char *path) {
} }
for (;;) { for (;;) {
struct dirent *de, buf; struct dirent *de;
union dirent_storage buf;
int k; int k;
char *p; char *p;
k = readdir_r(d, &buf, &de); k = readdir_r(d, &buf.de, &de);
if (k != 0) { if (k != 0) {
r = -k; r = -k;
goto finish; goto finish;

View File

@ -86,7 +86,8 @@ int journal_directory_vacuum(const char *directory, uint64_t max_use, uint64_t m
for (;;) { for (;;) {
int k; int k;
struct dirent buf, *de; struct dirent *de;
union dirent_storage buf;
size_t q; size_t q;
struct stat st; struct stat st;
char *p; char *p;
@ -94,7 +95,7 @@ int journal_directory_vacuum(const char *directory, uint64_t max_use, uint64_t m
sd_id128_t seqnum_id; sd_id128_t seqnum_id;
bool have_seqnum; bool have_seqnum;
k = readdir_r(d, &buf, &de); k = readdir_r(d, &buf.de, &de);
if (k != 0) { if (k != 0) {
r = -k; r = -k;
goto finish; goto finish;

View File

@ -141,9 +141,10 @@ static uint64_t available_space(Server *s) {
for (;;) { for (;;) {
struct stat st; struct stat st;
struct dirent buf, *de; struct dirent *de;
union dirent_storage buf;
r = readdir_r(d, &buf, &de); r = readdir_r(d, &buf.de, &de);
if (r != 0) if (r != 0)
break; break;

View File

@ -1256,9 +1256,10 @@ static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
} }
for (;;) { for (;;) {
struct dirent buf, *de; struct dirent *de;
union dirent_storage buf;
r = readdir_r(d, &buf, &de); r = readdir_r(d, &buf.de, &de);
if (r != 0 || !de) if (r != 0 || !de)
break; break;
@ -1334,10 +1335,11 @@ static int add_root_directory(sd_journal *j, const char *p) {
} }
for (;;) { for (;;) {
struct dirent buf, *de; struct dirent *de;
union dirent_storage buf;
sd_id128_t id; sd_id128_t id;
r = readdir_r(d, &buf, &de); r = readdir_r(d, &buf.de, &de);
if (r != 0 || !de) if (r != 0 || !de)
break; break;

View File

@ -651,11 +651,12 @@ _public_ int sd_get_uids(uid_t **users) {
return -errno; return -errno;
for (;;) { for (;;) {
struct dirent buffer, *de; struct dirent *de;
union dirent_storage buf;
int k; int k;
uid_t uid; uid_t uid;
k = readdir_r(d, &buffer, &de); k = readdir_r(d, &buf.de, &de);
if (k != 0) { if (k != 0) {
r = -k; r = -k;
goto finish; goto finish;

View File

@ -39,7 +39,6 @@
static int files_add(Hashmap *h, const char *path, const char *suffix) { static int files_add(Hashmap *h, const char *path, const char *suffix) {
DIR *dir; DIR *dir;
struct dirent buffer, *de;
int r = 0; int r = 0;
dir = opendir(path); dir = opendir(path);
@ -50,10 +49,12 @@ static int files_add(Hashmap *h, const char *path, const char *suffix) {
} }
for (;;) { for (;;) {
struct dirent *de;
union dirent_storage buf;
int k; int k;
char *p; char *p;
k = readdir_r(dir, &buffer, &de); k = readdir_r(dir, &buf.de, &de);
if (k != 0) { if (k != 0) {
r = -k; r = -k;
goto finish; goto finish;

View File

@ -61,10 +61,11 @@ static int rtc_open(int flags) {
for (;;) { for (;;) {
char *p, *v; char *p, *v;
struct dirent buf, *de; struct dirent *de;
union dirent_storage buf;
int r; int r;
r = readdir_r(d, &buf, &de); r = readdir_r(d, &buf.de, &de);
if (r != 0) if (r != 0)
goto fallback; goto fallback;

View File

@ -201,7 +201,6 @@ static int remove_marked_symlinks_fd(
int r = 0; int r = 0;
DIR *d; DIR *d;
struct dirent buffer, *de;
assert(remove_symlinks_to); assert(remove_symlinks_to);
assert(fd >= 0); assert(fd >= 0);
@ -218,9 +217,11 @@ static int remove_marked_symlinks_fd(
rewinddir(d); rewinddir(d);
for (;;) { for (;;) {
struct dirent *de;
union dirent_storage buf;
int k; int k;
k = readdir_r(d, &buffer, &de); k = readdir_r(d, &buf.de, &de);
if (k != 0) { if (k != 0) {
r = -errno; r = -errno;
break; break;
@ -375,7 +376,6 @@ static int find_symlinks_fd(
int r = 0; int r = 0;
DIR *d; DIR *d;
struct dirent buffer, *de;
assert(name); assert(name);
assert(fd >= 0); assert(fd >= 0);
@ -391,8 +391,10 @@ static int find_symlinks_fd(
for (;;) { for (;;) {
int k; int k;
struct dirent *de;
union dirent_storage buf;
k = readdir_r(d, &buffer, &de); k = readdir_r(d, &buf.de, &de);
if (k != 0) { if (k != 0) {
r = -errno; r = -errno;
break; break;
@ -1906,7 +1908,6 @@ int unit_file_get_list(
return r; return r;
STRV_FOREACH(i, paths.unit_path) { STRV_FOREACH(i, paths.unit_path) {
struct dirent buffer, *de;
const char *units_dir; const char *units_dir;
free(buf); free(buf);
@ -1934,9 +1935,11 @@ int unit_file_get_list(
} }
for (;;) { for (;;) {
struct dirent *de;
union dirent_storage buffer;
UnitFileList *f; UnitFileList *f;
r = readdir_r(d, &buffer, &de); r = readdir_r(d, &buffer.de, &de);
if (r != 0) { if (r != 0) {
r = -r; r = -r;
goto finish; goto finish;

View File

@ -2956,9 +2956,10 @@ int dir_is_empty(const char *path) {
return -errno; return -errno;
for (;;) { for (;;) {
struct dirent buf, *de; struct dirent *de;
union dirent_storage buf;
r = readdir_r(d, &buf, &de); r = readdir_r(d, &buf.de, &de);
if (r > 0) if (r > 0)
return -r; return -r;
@ -3260,12 +3261,13 @@ int rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct
} }
for (;;) { for (;;) {
struct dirent buf, *de; struct dirent *de;
union dirent_storage buf;
bool is_dir, keep_around; bool is_dir, keep_around;
struct stat st; struct stat st;
int r; int r;
r = readdir_r(d, &buf, &de); r = readdir_r(d, &buf.de, &de);
if (r != 0 && ret == 0) { if (r != 0 && ret == 0) {
ret = -r; ret = -r;
break; break;
@ -4942,10 +4944,11 @@ int get_files_in_directory(const char *path, char ***list) {
return -errno; return -errno;
for (;;) { for (;;) {
struct dirent buffer, *de; struct dirent *de;
union dirent_storage buf;
int k; int k;
k = readdir_r(d, &buffer, &de); k = readdir_r(d, &buf.de, &de);
if (k != 0) { if (k != 0) {
r = -k; r = -k;
goto finish; goto finish;

View File

@ -35,6 +35,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <dirent.h> #include <dirent.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <stddef.h>
#include "macro.h" #include "macro.h"
@ -46,6 +47,12 @@ typedef struct dual_timestamp {
usec_t monotonic; usec_t monotonic;
} dual_timestamp; } dual_timestamp;
union dirent_storage {
struct dirent de;
uint8_t storage[offsetof(struct dirent, d_name) +
((NAME_MAX + 1 + sizeof(long)) & ~(sizeof(long) - 1))];
};
#define MSEC_PER_SEC 1000ULL #define MSEC_PER_SEC 1000ULL
#define USEC_PER_SEC 1000000ULL #define USEC_PER_SEC 1000000ULL
#define USEC_PER_MSEC 1000ULL #define USEC_PER_MSEC 1000ULL

View File

@ -551,12 +551,13 @@ static int recursive_relabel_children(Item *i, const char *path) {
return errno == ENOENT ? 0 : -errno; return errno == ENOENT ? 0 : -errno;
for (;;) { for (;;) {
struct dirent buf, *de; struct dirent *de;
union dirent_storage buf;
bool is_dir; bool is_dir;
int r; int r;
char *entry_path; char *entry_path;
r = readdir_r(d, &buf, &de); r = readdir_r(d, &buf.de, &de);
if (r != 0) { if (r != 0) {
if (ret == 0) if (ret == 0)
ret = -r; ret = -r;