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
ENOMEM, EEXIST, ECHILD, EPERM,
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,
84
83
#include <sys/mman.h> /* munlock(), mlock() */
85
84
#include <fcntl.h> /* O_CLOEXEC, O_NONBLOCK, fcntl(),
86
85
F_GETFD, F_GETFL, FD_CLOEXEC,
87
open(), O_WRONLY, O_NOCTTY,
88
O_RDONLY, O_NOFOLLOW */
86
open(), O_WRONLY, O_RDONLY */
89
87
#include <sys/wait.h> /* waitpid(), WNOHANG, WIFEXITED(),
91
89
#include <limits.h> /* PIPE_BUF, NAME_MAX, INT_MAX */
92
90
#include <sys/inotify.h> /* inotify_init1(), IN_NONBLOCK,
93
91
IN_CLOEXEC, inotify_add_watch(),
94
92
IN_CLOSE_WRITE, IN_MOVED_TO,
95
IN_MOVED_FROM, IN_DELETE,
96
IN_EXCL_UNLINK, IN_ONLYDIR,
97
struct inotify_event */
93
IN_DELETE, struct inotify_event */
98
94
#include <fnmatch.h> /* fnmatch(), FNM_FILE_NAME */
99
#include <stdio.h> /* asprintf(), FILE, stderr, fopen(),
100
fclose(), getline(), sscanf(),
101
feof(), ferror(), rename(),
102
fdopen(), fprintf(), fscanf() */
95
#include <stdio.h> /* asprintf(), FILE, fopen(),
96
getline(), sscanf(), feof(),
97
ferror(), fclose(), stderr,
98
rename(), fdopen(), fprintf(),
103
100
#include <glib.h> /* GKeyFile, g_key_file_free(), g_key_file_new(),
104
101
GError, g_key_file_load_from_file(),
105
102
G_KEY_FILE_NONE, TRUE, G_FILE_ERROR_NOENT,
652
648
__attribute__((nonnull, warn_unused_result))
653
649
bool add_to_queue(task_queue *const queue, const task_context task){
654
if((queue->length + 1) > (SIZE_MAX / sizeof(task_context))){
656
error(0, ENOMEM, "Failed to allocate %" PRIuMAX
657
" tasks for queue->tasks", (uintmax_t)(queue->length + 1));
661
650
const size_t needed_size = sizeof(task_context)*(queue->length + 1);
662
651
if(needed_size > (queue->allocated)){
663
652
task_context *const new_tasks = realloc(queue->tasks,
1891
1879
g_assert_true(queue->tasks[0].func == dummy_func);
1894
static void test_add_to_queue_overflow(__attribute__((unused))
1895
test_fixture *fixture,
1896
__attribute__((unused))
1897
gconstpointer user_data){
1898
__attribute__((cleanup(cleanup_queue)))
1899
task_queue *queue = create_queue();
1900
g_assert_nonnull(queue);
1901
g_assert_true(queue->length == 0);
1902
queue->length = SIZE_MAX / sizeof(task_context); /* fake max size */
1904
FILE *real_stderr = stderr;
1905
FILE *devnull = fopen("/dev/null", "we");
1906
g_assert_nonnull(devnull);
1908
const bool ret = add_to_queue(queue,
1909
(task_context){ .func=dummy_func });
1910
g_assert_true(errno == ENOMEM);
1911
g_assert_false(ret);
1912
stderr = real_stderr;
1913
g_assert_cmpint(fclose(devnull), ==, 0);
1914
queue->length = 0; /* Restore real size */
1917
1882
static void dummy_func(__attribute__((unused))
1918
1883
const task_context task,
1919
1884
__attribute__((unused))
3489
3452
g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3492
static void test_add_inotify_dir_watch_nondir(__attribute__((unused))
3493
test_fixture *fixture,
3494
__attribute__((unused))
3497
__attribute__((cleanup(cleanup_close)))
3498
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3499
g_assert_cmpint(epoll_fd, >=, 0);
3500
__attribute__((cleanup(cleanup_queue)))
3501
task_queue *queue = create_queue();
3502
g_assert_nonnull(queue);
3503
__attribute__((cleanup(string_set_clear)))
3504
string_set cancelled_filenames = {};
3505
const mono_microsecs current_time = 0;
3507
bool quit_now = false;
3508
buffer password = {};
3509
bool mandos_client_exited = false;
3510
bool password_is_read = false;
3512
const char not_a_directory[] = "/dev/tty";
3514
FILE *real_stderr = stderr;
3515
FILE *devnull = fopen("/dev/null", "we");
3516
g_assert_nonnull(devnull);
3518
g_assert_false(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
3519
&password, not_a_directory,
3520
&cancelled_filenames,
3522
&mandos_client_exited,
3523
&password_is_read));
3524
stderr = real_stderr;
3525
g_assert_cmpint(fclose(devnull), ==, 0);
3527
g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3530
3455
static void test_add_inotify_dir_watch_EAGAIN(__attribute__((unused))
3531
3456
test_fixture *fixture,
3532
3457
__attribute__((unused))
3750
void test_add_inotify_dir_watch_IN_MOVED_FROM(__attribute__((unused))
3751
test_fixture *fixture,
3752
__attribute__((unused))
3755
__attribute__((cleanup(cleanup_close)))
3756
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3757
g_assert_cmpint(epoll_fd, >=, 0);
3758
__attribute__((cleanup(cleanup_queue)))
3759
task_queue *queue = create_queue();
3760
g_assert_nonnull(queue);
3761
__attribute__((cleanup(string_set_clear)))
3762
string_set cancelled_filenames = {};
3763
const mono_microsecs current_time = 0;
3765
bool quit_now = false;
3766
buffer password = {};
3767
bool mandos_client_exited = false;
3768
bool password_is_read = false;
3770
__attribute__((cleanup(cleanup_string)))
3771
char *tempdir = make_temporary_directory();
3772
g_assert_nonnull(tempdir);
3774
__attribute__((cleanup(cleanup_string)))
3775
char *tempfilename = make_temporary_file_in_directory(tempdir);
3776
g_assert_nonnull(tempfilename);
3778
__attribute__((cleanup(cleanup_string)))
3779
char *targetdir = make_temporary_directory();
3780
g_assert_nonnull(targetdir);
3782
__attribute__((cleanup(cleanup_string)))
3783
char *targetfilename = NULL;
3784
g_assert_cmpint(asprintf(&targetfilename, "%s/%s", targetdir,
3785
basename(tempfilename)), >, 0);
3786
g_assert_nonnull(targetfilename);
3788
g_assert_true(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
3790
&cancelled_filenames,
3792
&mandos_client_exited,
3793
&password_is_read));
3795
g_assert_cmpint(rename(tempfilename, targetfilename), ==, 0);
3797
const task_context *const added_read_task
3798
= find_matching_task(queue,
3799
(task_context){ .func=read_inotify_event });
3800
g_assert_nonnull(added_read_task);
3802
/* "sufficient to read at least one event." - inotify(7) */
3803
const size_t ievent_size = (sizeof(struct inotify_event)
3805
struct inotify_event *ievent = malloc(ievent_size);
3806
g_assert_nonnull(ievent);
3808
ssize_t read_size = read(added_read_task->fd, ievent, ievent_size);
3810
g_assert_cmpint((int)read_size, >, 0);
3811
g_assert_true(ievent->mask & IN_MOVED_FROM);
3812
g_assert_cmpstr(ievent->name, ==, basename(tempfilename));
3816
g_assert_cmpint(unlink(targetfilename), ==, 0);
3817
g_assert_cmpint(rmdir(targetdir), ==, 0);
3818
g_assert_cmpint(rmdir(tempdir), ==, 0);
3822
3675
void test_add_inotify_dir_watch_IN_DELETE(__attribute__((unused))
3823
3676
test_fixture *fixture,
3824
3677
__attribute__((unused))
3882
3735
g_assert_cmpint(rmdir(tempdir), ==, 0);
3886
void test_add_inotify_dir_watch_IN_EXCL_UNLINK(__attribute__((unused))
3887
test_fixture *fixture,
3888
__attribute__((unused))
3891
__attribute__((cleanup(cleanup_close)))
3892
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3893
g_assert_cmpint(epoll_fd, >=, 0);
3894
__attribute__((cleanup(cleanup_queue)))
3895
task_queue *queue = create_queue();
3896
g_assert_nonnull(queue);
3897
__attribute__((cleanup(string_set_clear)))
3898
string_set cancelled_filenames = {};
3899
const mono_microsecs current_time = 0;
3901
bool quit_now = false;
3902
buffer password = {};
3903
bool mandos_client_exited = false;
3904
bool password_is_read = false;
3906
__attribute__((cleanup(cleanup_string)))
3907
char *tempdir = make_temporary_directory();
3908
g_assert_nonnull(tempdir);
3910
__attribute__((cleanup(cleanup_string)))
3911
char *tempfile = make_temporary_file_in_directory(tempdir);
3912
g_assert_nonnull(tempfile);
3913
int tempfile_fd = open(tempfile, O_WRONLY | O_CLOEXEC | O_NOCTTY
3915
g_assert_cmpint(tempfile_fd, >, 2);
3917
g_assert_true(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
3919
&cancelled_filenames,
3921
&mandos_client_exited,
3922
&password_is_read));
3923
g_assert_cmpint(unlink(tempfile), ==, 0);
3925
g_assert_cmpuint((unsigned int)queue->length, >, 0);
3927
const task_context *const added_read_task
3928
= find_matching_task(queue,
3929
(task_context){ .func=read_inotify_event });
3930
g_assert_nonnull(added_read_task);
3932
g_assert_cmpint(added_read_task->fd, >, 2);
3933
g_assert_true(fd_has_cloexec_and_nonblock(added_read_task->fd));
3935
/* "sufficient to read at least one event." - inotify(7) */
3936
const size_t ievent_size = (sizeof(struct inotify_event)
3938
struct inotify_event *ievent = malloc(ievent_size);
3939
g_assert_nonnull(ievent);
3941
ssize_t read_size = 0;
3942
read_size = read(added_read_task->fd, ievent, ievent_size);
3944
g_assert_cmpint((int)read_size, >, 0);
3945
g_assert_true(ievent->mask & IN_DELETE);
3946
g_assert_cmpstr(ievent->name, ==, basename(tempfile));
3948
g_assert_cmpint(close(tempfile_fd), ==, 0);
3950
/* IN_EXCL_UNLINK should make the closing of the previously unlinked
3951
file not appear as an ievent, so we should not see it now. */
3952
read_size = read(added_read_task->fd, ievent, ievent_size);
3953
g_assert_cmpint((int)read_size, ==, -1);
3954
g_assert_true(errno == EAGAIN);
3958
g_assert_cmpint(rmdir(tempdir), ==, 0);
3961
3738
static void test_read_inotify_event_readerror(__attribute__((unused))
3962
3739
test_fixture *fixture,
3963
3740
__attribute__((unused))
4332
void test_read_inotify_event_IN_MOVED_FROM(__attribute__((unused))
4333
test_fixture *fixture,
4334
__attribute__((unused))
4335
gconstpointer user_data){
4336
__attribute__((cleanup(cleanup_close)))
4337
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
4338
g_assert_cmpint(epoll_fd, >=, 0);
4339
__attribute__((cleanup(string_set_clear)))
4340
string_set cancelled_filenames = {};
4341
const mono_microsecs current_time = 0;
4344
g_assert_cmpint(pipe2(pipefds, O_CLOEXEC | O_NONBLOCK), ==, 0);
4346
/* "sufficient to read at least one event." - inotify(7) */
4347
const size_t ievent_max_size = (sizeof(struct inotify_event)
4349
g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4351
struct inotify_event event;
4352
char name_buffer[NAME_MAX + 1];
4354
struct inotify_event *const ievent = &ievent_buffer.event;
4356
const char dummy_file_name[] = "ask.dummy_file_name";
4357
ievent->mask = IN_MOVED_FROM;
4358
ievent->len = sizeof(dummy_file_name);
4359
memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4360
const size_t ievent_size = (sizeof(struct inotify_event)
4361
+ sizeof(dummy_file_name));
4362
g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4364
g_assert_cmpint(close(pipefds[1]), ==, 0);
4366
bool quit_now = false;
4367
buffer password = {};
4368
bool mandos_client_exited = false;
4369
bool password_is_read = false;
4370
__attribute__((cleanup(cleanup_queue)))
4371
task_queue *queue = create_queue();
4372
g_assert_nonnull(queue);
4374
task_context task = {
4375
.func=read_inotify_event,
4378
.quit_now=&quit_now,
4379
.password=&password,
4380
.filename=strdup("/nonexistent"),
4381
.cancelled_filenames=&cancelled_filenames,
4382
.current_time=¤t_time,
4383
.mandos_client_exited=&mandos_client_exited,
4384
.password_is_read=&password_is_read,
4386
task.func(task, queue);
4387
g_assert_false(quit_now);
4388
g_assert_true(queue->next_run == 0);
4389
g_assert_cmpuint((unsigned int)queue->length, ==, 1);
4391
g_assert_nonnull(find_matching_task(queue, (task_context){
4392
.func=read_inotify_event,
4395
.quit_now=&quit_now,
4396
.password=&password,
4397
.filename=task.filename,
4398
.cancelled_filenames=&cancelled_filenames,
4399
.current_time=¤t_time,
4400
.mandos_client_exited=&mandos_client_exited,
4401
.password_is_read=&password_is_read,
4404
g_assert_true(epoll_set_contains(epoll_fd, pipefds[0],
4405
EPOLLIN | EPOLLRDHUP));
4407
__attribute__((cleanup(cleanup_string)))
4408
char *filename = NULL;
4409
g_assert_cmpint(asprintf(&filename, "%s/%s", task.filename,
4410
dummy_file_name), >, 0);
4411
g_assert_nonnull(filename);
4412
g_assert_true(string_set_contains(*task.cancelled_filenames,
4416
4107
static void test_read_inotify_event_IN_DELETE(__attribute__((unused))
4417
4108
test_fixture *fixture,
4418
4109
__attribute__((unused))
4651
4342
EPOLLIN | EPOLLRDHUP));
4655
test_read_inotify_event_IN_MOVED_FROM_badname(__attribute__((unused))
4656
test_fixture *fixture,
4657
__attribute__((unused))
4660
__attribute__((cleanup(cleanup_close)))
4661
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
4662
g_assert_cmpint(epoll_fd, >=, 0);
4663
__attribute__((cleanup(string_set_clear)))
4664
string_set cancelled_filenames = {};
4665
const mono_microsecs current_time = 0;
4668
g_assert_cmpint(pipe2(pipefds, O_CLOEXEC | O_NONBLOCK), ==, 0);
4670
/* "sufficient to read at least one event." - inotify(7) */
4671
const size_t ievent_max_size = (sizeof(struct inotify_event)
4673
g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4675
struct inotify_event event;
4676
char name_buffer[NAME_MAX + 1];
4678
struct inotify_event *const ievent = &ievent_buffer.event;
4680
const char dummy_file_name[] = "ignored.dummy_file_name";
4681
ievent->mask = IN_MOVED_FROM;
4682
ievent->len = sizeof(dummy_file_name);
4683
memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4684
const size_t ievent_size = (sizeof(struct inotify_event)
4685
+ sizeof(dummy_file_name));
4686
g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4688
g_assert_cmpint(close(pipefds[1]), ==, 0);
4690
bool quit_now = false;
4691
buffer password = {};
4692
bool mandos_client_exited = false;
4693
bool password_is_read = false;
4694
__attribute__((cleanup(cleanup_queue)))
4695
task_queue *queue = create_queue();
4696
g_assert_nonnull(queue);
4698
task_context task = {
4699
.func=read_inotify_event,
4702
.quit_now=&quit_now,
4703
.password=&password,
4704
.filename=strdup("/nonexistent"),
4705
.cancelled_filenames=&cancelled_filenames,
4706
.current_time=¤t_time,
4707
.mandos_client_exited=&mandos_client_exited,
4708
.password_is_read=&password_is_read,
4710
task.func(task, queue);
4711
g_assert_false(quit_now);
4712
g_assert_true(queue->next_run == 0);
4713
g_assert_cmpuint((unsigned int)queue->length, ==, 1);
4715
g_assert_nonnull(find_matching_task(queue, (task_context){
4716
.func=read_inotify_event,
4719
.quit_now=&quit_now,
4720
.password=&password,
4721
.filename=task.filename,
4722
.cancelled_filenames=&cancelled_filenames,
4723
.current_time=¤t_time,
4724
.mandos_client_exited=&mandos_client_exited,
4725
.password_is_read=&password_is_read,
4728
g_assert_true(epoll_set_contains(epoll_fd, pipefds[0],
4729
EPOLLIN | EPOLLRDHUP));
4731
__attribute__((cleanup(cleanup_string)))
4732
char *filename = NULL;
4733
g_assert_cmpint(asprintf(&filename, "%s/%s", task.filename,
4734
dummy_file_name), >, 0);
4735
g_assert_nonnull(filename);
4736
g_assert_false(string_set_contains(cancelled_filenames, filename));
4740
4346
void test_read_inotify_event_IN_DELETE_badname(__attribute__((unused))
4741
4347
test_fixture *fixture,
7892
7495
test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7893
7496
test_add("/queue/create", test_create_queue);
7894
7497
test_add("/queue/add", test_add_to_queue);
7895
test_add("/queue/add/overflow", test_add_to_queue_overflow);
7896
7498
test_add("/queue/has_question/empty",
7897
7499
test_queue_has_question_empty);
7898
7500
test_add("/queue/has_question/false",
7985
7587
test_add_inotify_dir_watch);
7986
7588
test_add_st("/task-creators/add_inotify_dir_watch/fail",
7987
7589
test_add_inotify_dir_watch_fail);
7988
test_add_st("/task-creators/add_inotify_dir_watch/not-a-directory",
7989
test_add_inotify_dir_watch_nondir);
7990
7590
test_add_st("/task-creators/add_inotify_dir_watch/EAGAIN",
7991
7591
test_add_inotify_dir_watch_EAGAIN);
7992
7592
test_add_st("/task-creators/add_inotify_dir_watch/IN_CLOSE_WRITE",
7993
7593
test_add_inotify_dir_watch_IN_CLOSE_WRITE);
7994
7594
test_add_st("/task-creators/add_inotify_dir_watch/IN_MOVED_TO",
7995
7595
test_add_inotify_dir_watch_IN_MOVED_TO);
7996
test_add_st("/task-creators/add_inotify_dir_watch/IN_MOVED_FROM",
7997
test_add_inotify_dir_watch_IN_MOVED_FROM);
7998
test_add_st("/task-creators/add_inotify_dir_watch/IN_EXCL_UNLINK",
7999
test_add_inotify_dir_watch_IN_EXCL_UNLINK);
8000
7596
test_add_st("/task-creators/add_inotify_dir_watch/IN_DELETE",
8001
7597
test_add_inotify_dir_watch_IN_DELETE);
8002
7598
test_add_st("/task/read_inotify_event/readerror",
8011
7607
test_read_inotify_event_IN_CLOSE_WRITE);
8012
7608
test_add_st("/task/read_inotify_event/IN_MOVED_TO",
8013
7609
test_read_inotify_event_IN_MOVED_TO);
8014
test_add_st("/task/read_inotify_event/IN_MOVED_FROM",
8015
test_read_inotify_event_IN_MOVED_FROM);
8016
7610
test_add_st("/task/read_inotify_event/IN_DELETE",
8017
7611
test_read_inotify_event_IN_DELETE);
8018
7612
test_add_st("/task/read_inotify_event/IN_CLOSE_WRITE/badname",
8019
7613
test_read_inotify_event_IN_CLOSE_WRITE_badname);
8020
7614
test_add_st("/task/read_inotify_event/IN_MOVED_TO/badname",
8021
7615
test_read_inotify_event_IN_MOVED_TO_badname);
8022
test_add_st("/task/read_inotify_event/IN_MOVED_FROM/badname",
8023
test_read_inotify_event_IN_MOVED_FROM_badname);
8024
7616
test_add_st("/task/read_inotify_event/IN_DELETE/badname",
8025
7617
test_read_inotify_event_IN_DELETE_badname);
8026
7618
test_add_st("/task/open_and_parse_question/ENOENT",
8123
7715
g_option_context_set_help_enabled(context, FALSE);
8124
7716
g_option_context_set_ignore_unknown_options(context, TRUE);
8126
gboolean should_run_tests = FALSE;
7718
gboolean run_tests = FALSE;
8127
7719
GOptionEntry entries[] = {
8128
7720
{ "test", 0, 0, G_OPTION_ARG_NONE,
8129
&should_run_tests, "Run tests", NULL },
7721
&run_tests, "Run tests", NULL },
8132
7724
g_option_context_add_main_entries(context, entries, NULL);