48
48
#include <error.h> /* error() */
49
49
#include <sysexits.h> /* EX_USAGE, EX_OSERR, EX_OSFILE */
50
50
#include <errno.h> /* errno, error_t, EACCES,
51
ENAMETOOLONG, ENOENT, ENOTDIR,
52
EEXIST, ECHILD, EPERM, ENOMEM,
53
EAGAIN, EINTR, ENOBUFS, EADDRINUSE,
51
ENAMETOOLONG, ENOENT, EEXIST,
52
ECHILD, EPERM, ENOMEM, EAGAIN,
53
EINTR, ENOBUFS, EADDRINUSE,
54
54
ECONNREFUSED, ECONNRESET,
55
55
ETOOMANYREFS, EMSGSIZE, EBADF,
83
83
#include <sys/mman.h> /* munlock(), mlock() */
84
84
#include <fcntl.h> /* O_CLOEXEC, O_NONBLOCK, fcntl(),
85
85
F_GETFD, F_GETFL, FD_CLOEXEC,
86
open(), O_WRONLY, O_NOCTTY,
87
O_RDONLY, O_NOFOLLOW */
86
open(), O_WRONLY, O_RDONLY */
88
87
#include <sys/wait.h> /* waitpid(), WNOHANG, WIFEXITED(),
90
89
#include <limits.h> /* PIPE_BUF, NAME_MAX, INT_MAX */
91
90
#include <sys/inotify.h> /* inotify_init1(), IN_NONBLOCK,
92
91
IN_CLOEXEC, inotify_add_watch(),
93
92
IN_CLOSE_WRITE, IN_MOVED_TO,
94
IN_MOVED_FROM, IN_DELETE,
95
IN_EXCL_UNLINK, IN_ONLYDIR,
96
struct inotify_event */
93
IN_DELETE, struct inotify_event */
97
94
#include <fnmatch.h> /* fnmatch(), FNM_FILE_NAME */
98
95
#include <stdio.h> /* asprintf(), FILE, fopen(),
99
96
getline(), sscanf(), feof(),
2229
2224
__attribute__((cleanup(cleanup_close)))
2230
const int devnull_fd = open("/dev/null",
2231
O_WRONLY | O_CLOEXEC | O_NOCTTY);
2225
const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
2232
2226
g_assert_cmpint(devnull_fd, >=, 0);
2233
2227
__attribute__((cleanup(cleanup_close)))
2234
2228
const int real_stderr_fd = dup(STDERR_FILENO);
3459
3452
g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3462
static void test_add_inotify_dir_watch_nondir(__attribute__((unused))
3463
test_fixture *fixture,
3464
__attribute__((unused))
3467
__attribute__((cleanup(cleanup_close)))
3468
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3469
g_assert_cmpint(epoll_fd, >=, 0);
3470
__attribute__((cleanup(cleanup_queue)))
3471
task_queue *queue = create_queue();
3472
g_assert_nonnull(queue);
3473
__attribute__((cleanup(string_set_clear)))
3474
string_set cancelled_filenames = {};
3475
const mono_microsecs current_time = 0;
3477
bool quit_now = false;
3478
buffer password = {};
3479
bool mandos_client_exited = false;
3480
bool password_is_read = false;
3482
const char not_a_directory[] = "/dev/tty";
3484
FILE *real_stderr = stderr;
3485
FILE *devnull = fopen("/dev/null", "we");
3486
g_assert_nonnull(devnull);
3488
g_assert_false(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
3489
&password, not_a_directory,
3490
&cancelled_filenames,
3492
&mandos_client_exited,
3493
&password_is_read));
3494
stderr = real_stderr;
3495
g_assert_cmpint(fclose(devnull), ==, 0);
3497
g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3500
3455
static void test_add_inotify_dir_watch_EAGAIN(__attribute__((unused))
3501
3456
test_fixture *fixture,
3502
3457
__attribute__((unused))
3852
3807
g_assert_cmpint(rmdir(tempdir), ==, 0);
3856
void test_add_inotify_dir_watch_IN_EXCL_UNLINK(__attribute__((unused))
3857
test_fixture *fixture,
3858
__attribute__((unused))
3861
__attribute__((cleanup(cleanup_close)))
3862
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3863
g_assert_cmpint(epoll_fd, >=, 0);
3864
__attribute__((cleanup(cleanup_queue)))
3865
task_queue *queue = create_queue();
3866
g_assert_nonnull(queue);
3867
__attribute__((cleanup(string_set_clear)))
3868
string_set cancelled_filenames = {};
3869
const mono_microsecs current_time = 0;
3871
bool quit_now = false;
3872
buffer password = {};
3873
bool mandos_client_exited = false;
3874
bool password_is_read = false;
3876
__attribute__((cleanup(cleanup_string)))
3877
char *tempdir = make_temporary_directory();
3878
g_assert_nonnull(tempdir);
3880
__attribute__((cleanup(cleanup_string)))
3881
char *tempfile = make_temporary_file_in_directory(tempdir);
3882
g_assert_nonnull(tempfile);
3883
int tempfile_fd = open(tempfile, O_WRONLY | O_CLOEXEC | O_NOCTTY
3885
g_assert_cmpint(tempfile_fd, >, 2);
3887
g_assert_true(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
3889
&cancelled_filenames,
3891
&mandos_client_exited,
3892
&password_is_read));
3893
g_assert_cmpint(unlink(tempfile), ==, 0);
3895
g_assert_cmpuint((unsigned int)queue->length, >, 0);
3897
const task_context *const added_read_task
3898
= find_matching_task(queue,
3899
(task_context){ .func=read_inotify_event });
3900
g_assert_nonnull(added_read_task);
3902
g_assert_cmpint(added_read_task->fd, >, 2);
3903
g_assert_true(fd_has_cloexec_and_nonblock(added_read_task->fd));
3905
/* "sufficient to read at least one event." - inotify(7) */
3906
const size_t ievent_size = (sizeof(struct inotify_event)
3908
struct inotify_event *ievent = malloc(ievent_size);
3909
g_assert_nonnull(ievent);
3911
ssize_t read_size = 0;
3912
read_size = read(added_read_task->fd, ievent, ievent_size);
3914
g_assert_cmpint((int)read_size, >, 0);
3915
g_assert_true(ievent->mask & IN_DELETE);
3916
g_assert_cmpstr(ievent->name, ==, basename(tempfile));
3918
g_assert_cmpint(close(tempfile_fd), ==, 0);
3920
/* IN_EXCL_UNLINK should make the closing of the previously unlinked
3921
file not appear as an ievent, so we should not see it now. */
3922
read_size = read(added_read_task->fd, ievent, ievent_size);
3923
g_assert_cmpint((int)read_size, ==, -1);
3924
g_assert_true(errno == EAGAIN);
3928
g_assert_cmpint(rmdir(tempdir), ==, 0);
3931
3810
static void test_read_inotify_event_readerror(__attribute__((unused))
3932
3811
test_fixture *fixture,
3933
3812
__attribute__((unused))
5631
5509
__attribute__((unused))
5632
5510
gconstpointer user_data){
5633
5511
__attribute__((cleanup(cleanup_close)))
5634
const int epoll_fd = open("/dev/null",
5635
O_WRONLY | O_CLOEXEC | O_NOCTTY);
5512
const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
5636
5513
__attribute__((cleanup(cleanup_string)))
5637
5514
char *const question_filename = strdup("/nonexistent/question");
5638
5515
g_assert_nonnull(question_filename);
6042
5919
__attribute__((unused))
6043
5920
gconstpointer user_data){
6044
5921
__attribute__((cleanup(cleanup_close)))
6045
const int epoll_fd = open("/dev/null",
6046
O_WRONLY | O_CLOEXEC | O_NOCTTY);
5922
const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
6047
5923
__attribute__((cleanup(cleanup_string)))
6048
5924
char *const question_filename = strdup("/nonexistent/question");
6049
5925
g_assert_nonnull(question_filename);
6312
6188
const char *const
6314
6190
__attribute__((cleanup(cleanup_close)))
6315
const int devnull_fd = open("/dev/null",
6316
O_WRONLY | O_CLOEXEC | O_NOCTTY);
6191
const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
6317
6192
g_assert_cmpint(devnull_fd, >=, 0);
6318
6193
__attribute__((cleanup(cleanup_close)))
6319
6194
const int real_stderr_fd = dup(STDERR_FILENO);
7954
7829
test_add_inotify_dir_watch);
7955
7830
test_add_st("/task-creators/add_inotify_dir_watch/fail",
7956
7831
test_add_inotify_dir_watch_fail);
7957
test_add_st("/task-creators/add_inotify_dir_watch/not-a-directory",
7958
test_add_inotify_dir_watch_nondir);
7959
7832
test_add_st("/task-creators/add_inotify_dir_watch/EAGAIN",
7960
7833
test_add_inotify_dir_watch_EAGAIN);
7961
7834
test_add_st("/task-creators/add_inotify_dir_watch/IN_CLOSE_WRITE",
7964
7837
test_add_inotify_dir_watch_IN_MOVED_TO);
7965
7838
test_add_st("/task-creators/add_inotify_dir_watch/IN_MOVED_FROM",
7966
7839
test_add_inotify_dir_watch_IN_MOVED_FROM);
7967
test_add_st("/task-creators/add_inotify_dir_watch/IN_EXCL_UNLINK",
7968
test_add_inotify_dir_watch_IN_EXCL_UNLINK);
7969
7840
test_add_st("/task-creators/add_inotify_dir_watch/IN_DELETE",
7970
7841
test_add_inotify_dir_watch_IN_DELETE);
7971
7842
test_add_st("/task/read_inotify_event/readerror",