#include <sys/stat.h>
#include <assert.h>
#include <atf-c.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
ATF_TC(seekdir_basic);
ATF_TC_HEAD(seekdir_basic, tc)
{
atf_tc_set_md_var(tc, "descr", "Check telldir(3) and seekdir(3) "
"for correct behavior (PR lib/24324)");
}
ATF_TC_BODY(seekdir_basic, tc)
{
DIR *dp;
char *wasname;
struct dirent *entry;
long here;
#define CREAT(x, m) do { \
int _creat_fd; \
ATF_REQUIRE_MSG((_creat_fd = creat((x), (m))) != -1, \
"creat(%s, %x) failed: %s", (x), (m), \
strerror(errno)); \
(void)close(_creat_fd); \
} while (0)
ATF_REQUIRE_MSG(mkdir("t", 0755) == 0,
"mkdir failed: %s", strerror(errno));
CREAT("t/a", 0600);
CREAT("t/b", 0600);
CREAT("t/c", 0600);
dp = opendir("t");
if ( dp == NULL)
atf_tc_fail("Could not open temp directory.");
entry = readdir(dp);
ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
".", strerror(errno));
entry = readdir(dp);
ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
"..", strerror(errno));
entry = readdir(dp);
ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
"first", strerror(errno));
here = telldir(dp);
ATF_REQUIRE_MSG(here != -1, "telldir failed: %s", strerror(errno));
entry = readdir(dp);
ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
"second", strerror(errno));
wasname = strdup(entry->d_name);
if (wasname == NULL)
atf_tc_fail("cannot allocate memory");
entry = readdir(dp);
ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
"third", strerror(errno));
seekdir(dp, here);
entry = readdir(dp);
ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
"first[1]", strerror(errno));
if (strcmp(entry->d_name, wasname) != 0)
atf_tc_fail("1st seekdir found wrong name");
seekdir(dp, here);
here = telldir(dp);
entry = readdir(dp);
ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
"second[1]", strerror(errno));
if (strcmp(entry->d_name, wasname) != 0)
atf_tc_fail("2nd seekdir found wrong name");
seekdir(dp, here);
entry = readdir(dp);
ATF_REQUIRE_MSG(entry != NULL, "readdir[%s] failed: %s",
"third[1]", strerror(errno));
if (strcmp(entry->d_name, wasname) != 0)
atf_tc_fail("3rd seekdir found wrong name");
closedir(dp);
free(wasname);
}
ATF_TC(telldir_leak);
ATF_TC_HEAD(telldir_leak, tc)
{
atf_tc_set_md_var(tc, "descr",
"Check telldir(3) for memory leakage (PR lib/24324)");
}
ATF_TC_BODY(telldir_leak, tc)
{
DIR *dp;
char *memused;
int i;
int oktouse = 4096;
dp = opendir(".");
if (dp == NULL)
atf_tc_fail("Could not open current directory");
(void)telldir(dp);
memused = sbrk(0);
closedir(dp);
for (i = 0; i < 1000; i++) {
dp = opendir(".");
if (dp == NULL)
atf_tc_fail("Could not open current directory");
(void)telldir(dp);
closedir(dp);
if ((char *)sbrk(0) - memused > oktouse) {
(void)printf("Used %td extra bytes for %d telldir "
"calls", ((char *)sbrk(0) - memused), i);
oktouse = (char *)sbrk(0) - memused;
}
}
if (oktouse > 4096) {
atf_tc_fail("Failure: leaked %d bytes", oktouse);
} else {
(void)printf("OK: used %td bytes\n", (char *)(sbrk(0))-memused);
}
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, seekdir_basic);
ATF_TP_ADD_TC(tp, telldir_leak);
return atf_no_error();
}