1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
|
/*
* Author: Colin King <colin.king@canonical.com>
*
* Copyright (C) 2012 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/inotify.h>
#define TEST_PASSED (0)
#define TEST_FAILED (1)
#define TEST_ERROR (2)
#define DIR_FLAGS (S_IRWXU | S_IRWXG)
#define FILE_FLAGS (S_IRUSR | S_IWUSR)
#define TIME_OUT (15) /* Duration in secs for inotify to report something */
#define BUF_SIZE (4096)
#define MOVE_PATH "/tmp/test_file"
typedef int (*test_helper)(char *path, void *private);
typedef int (*test_func)(char *path);
#define flags_check(buf, flags, flag) \
if (flags & flag) \
flags_add(buf, #flag)
void flags_add(char *buff, char *flagstr)
{
if (*buff == '\0') {
strcpy(buff, flagstr);
} else {
strcat(buff, " ");
strcat(buff, flagstr);
}
}
/*
* flags_to_str()
* convert inotify flags to human readable form
*/
char *flags_to_str(int flags)
{
static char buf[4096];
*buf = '\0';
flags_check(buf, flags, IN_ACCESS);
flags_check(buf, flags, IN_MODIFY);
flags_check(buf, flags, IN_ATTRIB);
flags_check(buf, flags, IN_CLOSE_WRITE);
flags_check(buf, flags, IN_CLOSE_NOWRITE);
flags_check(buf, flags, IN_OPEN);
flags_check(buf, flags, IN_MOVED_FROM);
flags_check(buf, flags, IN_MOVED_TO);
flags_check(buf, flags, IN_CREATE);
flags_check(buf, flags, IN_DELETE);
flags_check(buf, flags, IN_DELETE_SELF);
flags_check(buf, flags, IN_MOVE_SELF);
flags_check(buf, flags, IN_UNMOUNT);
return buf;
}
/*
* test_inotify()
* run a given test helper function 'func' and see if this triggers the
* required inotify event flags 'flags'. Return TEST_FAILED if inotify()
* fails to work or match the require flags, TEST_ERROR if something went
* horribly wrong, or TEST_PASSED if it worked as expected
*/
int test_inotify(char *filename,/* Filename in test */
char *watchname, /* File or directory to watch using inotify */
char *matchname, /* Filename we expect inotify event to report */
test_helper func, /* Helper func */
int flags, /* IN_* flags to watch for */
void *private) /* Helper func private data */
{
int len;
int fd;
int wd;
int ret = 0;
char buffer[1024];
int check_flags = flags;
int ignored = 0;
if ((fd = inotify_init()) < 0) {
fprintf(stderr, "inotify_init failed: %s", strerror(errno));
return TEST_FAILED;
}
if ((wd = inotify_add_watch(fd, watchname, flags)) < 0) {
close(fd);
fprintf(stderr, "inotify_add_watch failed: %s", strerror(errno));
return TEST_FAILED;
}
if (func(filename, private) < 0) {
close(fd);
return TEST_ERROR;
}
while (check_flags) {
int i = 0;
struct timeval tv;
fd_set rfds;
int err;
/* We give inotify TIME_OUT seconds to report back */
tv.tv_sec = TIME_OUT;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
/* Wait for an inotify event ... */
err = select(fd + 1, &rfds, NULL, NULL, &tv);
if (err == -1) {
fprintf(stderr, "Select error: %s\n", strerror(errno));
ret = TEST_FAILED;
break;
} else if (err == 0) {
fprintf(stderr, "Timeout waiting for event flags 0x%x (%s)\n", flags, flags_to_str(flags));
ret = TEST_FAILED;
break;
}
if ((len = read(fd, buffer, sizeof(buffer))) < 0) {
fprintf(stderr, "Error reading inotify fd: %s\n", strerror(errno));
ret = TEST_FAILED;
break;
}
/* Scan through inotify events */
while (i < len) {
int f;
struct inotify_event *event = (struct inotify_event *)&buffer[i];
/*
* IN_IGNORED indicates the watch file/dir was removed and
* a side effect is that the kernel removes the watch so
* we shouldn't use inotify_rm_watch to reap this later
*/
if (event->mask & IN_IGNORED)
ignored = 1;
f = event->mask & (IN_DELETE_SELF | IN_MOVE_SELF |
IN_MOVED_TO | IN_MOVED_FROM | IN_ATTRIB);
if (event->len &&
strcmp(event->name, matchname) == 0 &&
flags & event->mask)
check_flags &= ~(flags & event->mask);
else if (flags & f)
check_flags &= ~(flags & event->mask);
i += sizeof(struct inotify_event) + event->len;
}
}
/* Note: EINVAL happens if the watched file itself is deleted */
if (!ignored)
inotify_rm_watch(fd, wd);
close(fd);
return ret;
}
/*
* mk_filename()
* simple helper to create a filename
*/
inline void mk_filename(char *filename, size_t len, const char *path, const char *name)
{
snprintf(filename, len, "%s/%s", path, name);
}
/*
* mk_file()
* create file of length sz bytes
*/
int mk_file(char *filename, size_t sz)
{
int fd;
char buffer[BUF_SIZE];
(void)unlink(filename);
if ((fd = open(filename, O_CREAT | O_RDWR, FILE_FLAGS)) < 0) {
fprintf(stderr, "Cannot create %s: %s\n", filename, strerror(errno));
return -1;
}
memset(buffer, 'x', BUF_SIZE);
while (sz > 0) {
size_t n = (sz > BUF_SIZE) ? BUF_SIZE : sz;
int ret;
if ((ret = write(fd, buffer, n)) < 0) {
fprintf(stderr, "Error writing to file %s: %s\n",
filename, strerror(errno));
close(fd);
return -1;
}
sz -= ret;
}
if (close(fd) < 0) {
fprintf(stderr, "Cannot close %s: %s\n", filename, strerror(errno));
return -1;
}
return 0;
}
int test_attrib_helper(char *path, void *dummy)
{
if (chmod(path, S_IRUSR | S_IWUSR) < 0) {
fprintf(stderr, "Cannot chmod %s: %s\n", path, strerror(errno));
return -1;
}
return 0;
}
int test_attrib_file(char *path)
{
char filepath[PATH_MAX];
int ret;
mk_filename(filepath, PATH_MAX, path, "test_file");
if (mk_file(filepath, 4096) < 0)
return TEST_ERROR;
ret = test_inotify(filepath, path, "test_file", test_attrib_helper, IN_ATTRIB, NULL);
unlink(filepath);
return ret;
}
int test_access_helper(char *path, void *dummy)
{
int fd;
char buffer[1];
int rc = 0;
if ((fd = open(path, O_RDONLY)) < 0) {
fprintf(stderr, "Cannot open %s: %s\n", path, strerror(errno));
return -1;
}
/* Just want to force an access */
if (read(fd, buffer, 1) < 0) {
fprintf(stderr, "Cannot read %s: %s\n", path, strerror(errno));
rc = -1;
}
close(fd);
return rc;
}
int test_access_file(char *path)
{
char filepath[PATH_MAX];
int ret;
mk_filename(filepath, PATH_MAX, path, "test_file");
if (mk_file(filepath, 4096) < 0)
return TEST_ERROR;
ret = test_inotify(filepath, path, "test_file", test_access_helper, IN_ACCESS, NULL);
(void)unlink(filepath);
return ret;
}
int test_modify_helper(char *path, void *dummy)
{
int fd;
char buffer[1] = { 0 };
int rc = 0;
if (mk_file(path, 4096) < 0)
return -1;
if ((fd = open(path, O_RDWR)) < 0) {
fprintf(stderr, "Cannot open %s: %s\n", path, strerror(errno));
rc = -1;
goto remove;
}
if (write(fd, buffer, 1) < 0) {
fprintf(stderr, "Cannot write %s: %s\n", path, strerror(errno));
rc = -1;
}
close(fd);
remove:
(void)unlink(path);
return rc;
}
int test_modify_file(char *path)
{
char filepath[PATH_MAX];
mk_filename(filepath, PATH_MAX, path, "test_file");
return test_inotify(filepath, path, "test_file", test_modify_helper, IN_MODIFY, NULL);
}
int test_creat_helper(char *path, void *dummy)
{
if (creat(path, FILE_FLAGS) < 0) {
fprintf(stderr, "Cannot creat %s: %s\n", path, strerror(errno));
return -1;
}
return 0;
}
int test_creat_file(char *path)
{
char filepath[PATH_MAX];
int ret;
mk_filename(filepath, PATH_MAX, path, "test_file");
ret = test_inotify(filepath, path, "test_file", test_creat_helper, IN_CREATE, NULL);
unlink(filepath);
return ret;
}
int test_open_helper(char *path, void *dummy)
{
int fd;
if ((fd = open(path, O_RDONLY)) < 0) {
fprintf(stderr, "Cannot open %s: %s\n", path, strerror(errno));
return -1;
}
close(fd);
return 0;
}
int test_open_file(char *path)
{
char filepath[PATH_MAX];
int ret;
mk_filename(filepath, PATH_MAX, path, "test_file");
if (mk_file(filepath, 4096) < 0)
return TEST_ERROR;
ret = test_inotify(filepath, path, "test_file", test_open_helper, IN_OPEN, NULL);
(void)unlink(filepath);
return ret;
}
int test_delete_helper(char *path, void *dummy)
{
if (unlink(path) < 0) {
fprintf(stderr, "Cannot unlink %s: %s\n", path, strerror(errno));
return -1;
}
return 0;
}
int test_delete_file(char *path)
{
char filepath[PATH_MAX];
int ret;
mk_filename(filepath, PATH_MAX, path, "test_file");
if (mk_file(filepath, 4096) < 0)
return TEST_ERROR;
ret = test_inotify(filepath, path, "test_file", test_delete_helper, IN_DELETE, NULL);
/* We remove (again) it just in case the test failed */
(void)unlink(filepath);
return ret;
}
int test_delete_self_helper(char *path, void *dummy)
{
if (rmdir(path) < 0) {
fprintf(stderr, "Cannot rmdir %s: %s\n", path, strerror(errno));
return -1;
}
return 0;
}
int test_delete_self(char *path)
{
char filepath[PATH_MAX];
int ret;
mk_filename(filepath, PATH_MAX, path, "test_dir");
if (mkdir(filepath, DIR_FLAGS) < 0)
return TEST_ERROR;
ret = test_inotify(filepath, filepath, "test_dir", test_delete_self_helper, IN_DELETE_SELF, NULL);
/* We remove (again) in case the test failed */
(void)rmdir(filepath);
return ret;
}
int test_move_self_helper(char *oldpath, void *private)
{
char *newpath = (char*)private;
if (rename(oldpath, newpath) < 0) {
fprintf(stderr, "Cannot rename %s to %s: %s\n",
oldpath, newpath, strerror(errno));
return -1;
}
return 0;
}
int test_move_self(char *path)
{
char filepath[PATH_MAX];
char newpath[PATH_MAX];
int ret;
mk_filename(filepath, PATH_MAX, path, "test_dir");
if (mkdir(filepath, DIR_FLAGS) < 0) {
fprintf(stderr, "Cannot mkdir %s: %s\n", filepath, strerror(errno));
return TEST_ERROR;
}
mk_filename(newpath, PATH_MAX, path, "renamed_dir");
ret = test_inotify(filepath, filepath, "test_dir", test_move_self_helper, IN_MOVE_SELF, newpath);
(void)rmdir(newpath);
return ret;
}
int test_moved_to_helper(char *newpath, void *private)
{
char *oldpath = (char*)private;
if (rename(oldpath, newpath) < 0) {
fprintf(stderr, "Cannot rename %s to %s: %s\n",
oldpath, newpath, strerror(errno));
return -1;
}
return 0;
}
int test_moved_to(char *path)
{
char olddir[PATH_MAX];
char oldfile[PATH_MAX];
char newfile[PATH_MAX];
int ret;
mk_filename(olddir, PATH_MAX, path, "new_dir");
(void)rmdir(olddir);
if (mkdir(olddir, DIR_FLAGS) < 0) {
fprintf(stderr, "Cannot create directory %s: %s\n", olddir, strerror(errno));
return TEST_ERROR;
}
mk_filename(oldfile, PATH_MAX, olddir, "test_file");
if (mk_file(oldfile, 4096) < 0)
return TEST_ERROR;
mk_filename(newfile, PATH_MAX, path, "test_file");
ret = test_inotify(newfile, path, "test_dir", test_moved_to_helper, IN_MOVED_TO, oldfile);
(void)rmdir(olddir);
(void)unlink(newfile);
return ret;
}
int test_moved_from_helper(char *oldpath, void *private)
{
char *newpath = (char*)private;
if (rename(oldpath, newpath) < 0) {
fprintf(stderr, "Cannot rename %s to %s: %s\n",
oldpath, MOVE_PATH, strerror(errno));
return -1;
}
return 0;
}
int test_moved_from(char *path)
{
char oldfile[PATH_MAX];
char newdir[PATH_MAX];
char newfile[PATH_MAX];
int ret;
mk_filename(oldfile, PATH_MAX, path, "test_file");
if (mk_file(oldfile, 4096) < 0)
return TEST_ERROR;
mk_filename(newdir, PATH_MAX, path, "new_dir");
(void)rmdir(newdir);
if (mkdir(newdir, DIR_FLAGS) < 0) {
fprintf(stderr, "Cannot create directory %s: %s\n", newdir, strerror(errno));
return TEST_ERROR;
}
mk_filename(newfile, PATH_MAX, newdir, "test_file");
ret = test_inotify(oldfile, path, "test_dir", test_moved_from_helper, IN_MOVED_FROM, newfile);
unlink(newfile);
(void)rmdir(newdir);
return ret;
}
int test_close_write_helper(char *path, void *fdptr)
{
int fd = *(int*)fdptr;
(void)close(fd);
return 0;
}
int test_close_write_file(char *path)
{
char filepath[PATH_MAX];
int fd;
int ret;
mk_filename(filepath, PATH_MAX, path, "test_file");
if (mk_file(filepath, 4096) < 0)
return -1;
if ((fd = open(filepath, O_RDWR)) < 0) {
fprintf(stderr, "Cannot re-open %s: %s\n", filepath, strerror(errno));
return -1;
}
ret = test_inotify(filepath, path, "test_file", test_close_write_helper, IN_CLOSE_WRITE, (void*)&fd);
(void)unlink(filepath);
return ret;
}
int test_close_nowrite_helper(char *path, void *fdptr)
{
int fd = *(int*)fdptr;
(void)close(fd);
return 0;
}
int test_close_nowrite_file(char *path)
{
char filepath[PATH_MAX];
int fd;
int ret;
mk_filename(filepath, PATH_MAX, path, "test_file");
if (mk_file(filepath, 4096) < 0)
return TEST_ERROR;
if ((fd = open(filepath, O_RDONLY)) < 0) {
fprintf(stderr, "Cannot re-open %s: %s\n", filepath, strerror(errno));
(void)unlink(filepath);
return TEST_ERROR;
}
ret = test_inotify(filepath, path, "test_file", test_close_nowrite_helper, IN_CLOSE_NOWRITE, (void*)&fd);
(void)unlink(filepath);
return ret;
}
struct {
test_func func;
char* description;
} inotify_test[] = {
{ test_access_file, "IN_ACCESS" },
{ test_modify_file, "IN_MODIFY" },
{ test_attrib_file, "IN_ATTRIB" },
{ test_close_write_file, "IN_CLOSE_WRITE" },
{ test_close_nowrite_file, "IN_CLOSE_NOWRITE" },
{ test_open_file, "IN_OPEN" },
{ test_moved_from, "IN_MOVED_FROM" },
{ test_moved_to, "IN_MOVED_TO" },
{ test_creat_file, "IN_CREATE" },
{ test_delete_file, "IN_DELETE" },
{ test_delete_self, "IN_DELETE_SELF" },
{ test_move_self, "IN_MOVE_SELF" },
{ NULL, NULL }
};
int main(int argc, char **argv)
{
int i;
int test_failed = 0;
if (argc < 2) {
fprintf(stderr, "Usage: %s path\n", argv[0]);
fprintf(stderr, "\twhere path is the path to run the inotify file tests in\n");
exit(TEST_ERROR);
}
if (mkdir(argv[1], DIR_FLAGS) < 0) {
if (errno != EEXIST) {
fprintf(stderr, "Cannot create directory %s\n", argv[1]);
exit(TEST_ERROR);
}
}
for (i=0; inotify_test[i].func; i++) {
int ret = inotify_test[i].func(argv[1]);
/* Something went horribly wrong, bail out early */
if (ret == TEST_ERROR) {
printf("%s test encounted an error\n", inotify_test[i].description);
exit(TEST_ERROR);
}
if (ret == TEST_FAILED) {
test_failed++;
}
}
(void)rmdir(argv[1]);
exit(test_failed ? TEST_FAILED : TEST_PASSED);
}
|