summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e3a7202)
raw | patch | inline | side by side (parent: e3a7202)
author | Florian Forster <octo@noris.net> | |
Mon, 14 Jul 2008 15:27:36 +0000 (17:27 +0200) | ||
committer | Florian Forster <octo@noris.net> | |
Mon, 14 Jul 2008 15:27:36 +0000 (17:27 +0200) |
`readdir' is not thread safe, so use the (more thread safe) `readdir_r'.
Also, don't use the non-standard `d_type' member of the `dirent'
structure - it's not portable.
I'm aware that this plugin is very Linux specific and Linux most likely
will never use another C library than the GNU libc, but using this
member prevents me from compiling with the most restrictive `strict'
settings..
Also, don't use the non-standard `d_type' member of the `dirent'
structure - it's not portable.
I'm aware that this plugin is very Linux specific and Linux most likely
will never use another C library than the GNU libc, but using this
member prevents me from compiling with the most restrictive `strict'
settings..
src/vserver.c | patch | blob | history |
diff --git a/src/vserver.c b/src/vserver.c
index 54843449b09acff812f3f2edc9f9c6dd8a9ad99e..7188b13e366b3b4b0f40bd4b8dbe5e8418b1bcd7 100644 (file)
--- a/src/vserver.c
+++ b/src/vserver.c
static int vserver_read (void)
{
+#if NAME_MAX < 1024
+# define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + 1024 + 1)
+#else
+# define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + NAME_MAX + 1)
+#endif
+
DIR *proc;
struct dirent *dent; /* 42 */
+ char dirent_buffer[DIRENT_BUFFER_SIZE];
errno = 0;
- if (NULL == (proc = opendir (PROCDIR)))
+ proc = opendir (PROCDIR);
+ if (proc == NULL)
{
char errbuf[1024];
ERROR ("vserver plugin: fopen (%s): %s", PROCDIR,
return (-1);
}
- while (NULL != (dent = readdir (proc)))
+ while (42)
{
- int len;
+ size_t len;
char file[BUFSIZE];
FILE *fh;
char buffer[BUFSIZE];
+ struct stat statbuf;
char *cols[4];
+ int status;
+
+ status = readdir_r (proc, (struct dirent *) dirent_buffer, &dent);
+ if (status != 0)
+ {
+ char errbuf[4096];
+ ERROR ("vserver plugin: readdir_r failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ closedir (proc);
+ return (-1);
+ }
+ else if (dent == NULL)
+ {
+ /* end of directory */
+ break;
+ }
+
if (dent->d_name[0] == '.')
continue;
- /* This is not a directory */
- if (dent->d_type != DT_DIR)
+ len = snprintf (file, sizeof (file), PROCDIR "/%s", dent->d_name);
+ if ((len < 0) || (len >= BUFSIZE))
+ continue;
+
+ status = stat (file, &statbuf);
+ if (status != 0)
+ {
+ char errbuf[4096];
+ WARNING ("vserver plugin: stat (%s) failed: %s",
+ file, sstrerror (errno, errbuf, sizeof (errbuf)));
+ continue;
+ }
+
+ if (!S_ISDIR (statbuf.st_mode))
continue;
/* socket message accounting */