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,
1089
1071
/* "sufficient to read at least one event." - inotify(7) */
1090
1072
const size_t ievent_size = (sizeof(struct inotify_event)
1091
1073
+ NAME_MAX + 1);
1093
struct inotify_event event;
1094
char name_buffer[NAME_MAX + 1];
1096
struct inotify_event *const ievent = &ievent_buffer.event;
1074
char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
1075
struct inotify_event *ievent = ((struct inotify_event *)
1098
1078
const ssize_t read_length = read(fd, ievent, ievent_size);
1099
1079
if(read_length == 0){ /* EOF */
1899
1877
g_assert_true(queue->tasks[0].func == dummy_func);
1902
static void test_add_to_queue_overflow(__attribute__((unused))
1903
test_fixture *fixture,
1904
__attribute__((unused))
1905
gconstpointer user_data){
1906
__attribute__((cleanup(cleanup_queue)))
1907
task_queue *queue = create_queue();
1908
g_assert_nonnull(queue);
1909
g_assert_true(queue->length == 0);
1910
queue->length = SIZE_MAX / sizeof(task_context); /* fake max size */
1912
FILE *real_stderr = stderr;
1913
FILE *devnull = fopen("/dev/null", "we");
1914
g_assert_nonnull(devnull);
1916
const bool ret = add_to_queue(queue,
1917
(task_context){ .func=dummy_func });
1918
g_assert_true(errno == ENOMEM);
1919
g_assert_false(ret);
1920
stderr = real_stderr;
1921
g_assert_cmpint(fclose(devnull), ==, 0);
1922
queue->length = 0; /* Restore real size */
1925
1880
static void dummy_func(__attribute__((unused))
1926
1881
const task_context task,
1927
1882
__attribute__((unused))
3501
3450
g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3504
static void test_add_inotify_dir_watch_nondir(__attribute__((unused))
3505
test_fixture *fixture,
3506
__attribute__((unused))
3509
__attribute__((cleanup(cleanup_close)))
3510
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3511
g_assert_cmpint(epoll_fd, >=, 0);
3512
__attribute__((cleanup(cleanup_queue)))
3513
task_queue *queue = create_queue();
3514
g_assert_nonnull(queue);
3515
__attribute__((cleanup(string_set_clear)))
3516
string_set cancelled_filenames = {};
3517
const mono_microsecs current_time = 0;
3519
bool quit_now = false;
3520
buffer password = {};
3521
bool mandos_client_exited = false;
3522
bool password_is_read = false;
3524
const char not_a_directory[] = "/dev/tty";
3526
FILE *real_stderr = stderr;
3527
FILE *devnull = fopen("/dev/null", "we");
3528
g_assert_nonnull(devnull);
3530
g_assert_false(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
3531
&password, not_a_directory,
3532
&cancelled_filenames,
3534
&mandos_client_exited,
3535
&password_is_read));
3536
stderr = real_stderr;
3537
g_assert_cmpint(fclose(devnull), ==, 0);
3539
g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3542
3453
static void test_add_inotify_dir_watch_EAGAIN(__attribute__((unused))
3543
3454
test_fixture *fixture,
3544
3455
__attribute__((unused))
3762
void test_add_inotify_dir_watch_IN_MOVED_FROM(__attribute__((unused))
3763
test_fixture *fixture,
3764
__attribute__((unused))
3767
__attribute__((cleanup(cleanup_close)))
3768
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3769
g_assert_cmpint(epoll_fd, >=, 0);
3770
__attribute__((cleanup(cleanup_queue)))
3771
task_queue *queue = create_queue();
3772
g_assert_nonnull(queue);
3773
__attribute__((cleanup(string_set_clear)))
3774
string_set cancelled_filenames = {};
3775
const mono_microsecs current_time = 0;
3777
bool quit_now = false;
3778
buffer password = {};
3779
bool mandos_client_exited = false;
3780
bool password_is_read = false;
3782
__attribute__((cleanup(cleanup_string)))
3783
char *tempdir = make_temporary_directory();
3784
g_assert_nonnull(tempdir);
3786
__attribute__((cleanup(cleanup_string)))
3787
char *tempfilename = make_temporary_file_in_directory(tempdir);
3788
g_assert_nonnull(tempfilename);
3790
__attribute__((cleanup(cleanup_string)))
3791
char *targetdir = make_temporary_directory();
3792
g_assert_nonnull(targetdir);
3794
__attribute__((cleanup(cleanup_string)))
3795
char *targetfilename = NULL;
3796
g_assert_cmpint(asprintf(&targetfilename, "%s/%s", targetdir,
3797
basename(tempfilename)), >, 0);
3798
g_assert_nonnull(targetfilename);
3800
g_assert_true(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
3802
&cancelled_filenames,
3804
&mandos_client_exited,
3805
&password_is_read));
3807
g_assert_cmpint(rename(tempfilename, targetfilename), ==, 0);
3809
const task_context *const added_read_task
3810
= find_matching_task(queue,
3811
(task_context){ .func=read_inotify_event });
3812
g_assert_nonnull(added_read_task);
3814
/* "sufficient to read at least one event." - inotify(7) */
3815
const size_t ievent_size = (sizeof(struct inotify_event)
3817
struct inotify_event *ievent = malloc(ievent_size);
3818
g_assert_nonnull(ievent);
3820
ssize_t read_size = read(added_read_task->fd, ievent, ievent_size);
3822
g_assert_cmpint((int)read_size, >, 0);
3823
g_assert_true(ievent->mask & IN_MOVED_FROM);
3824
g_assert_cmpstr(ievent->name, ==, basename(tempfilename));
3828
g_assert_cmpint(unlink(targetfilename), ==, 0);
3829
g_assert_cmpint(rmdir(targetdir), ==, 0);
3830
g_assert_cmpint(rmdir(tempdir), ==, 0);
3834
3673
void test_add_inotify_dir_watch_IN_DELETE(__attribute__((unused))
3835
3674
test_fixture *fixture,
3836
3675
__attribute__((unused))
3894
3733
g_assert_cmpint(rmdir(tempdir), ==, 0);
3898
void test_add_inotify_dir_watch_IN_EXCL_UNLINK(__attribute__((unused))
3899
test_fixture *fixture,
3900
__attribute__((unused))
3903
__attribute__((cleanup(cleanup_close)))
3904
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3905
g_assert_cmpint(epoll_fd, >=, 0);
3906
__attribute__((cleanup(cleanup_queue)))
3907
task_queue *queue = create_queue();
3908
g_assert_nonnull(queue);
3909
__attribute__((cleanup(string_set_clear)))
3910
string_set cancelled_filenames = {};
3911
const mono_microsecs current_time = 0;
3913
bool quit_now = false;
3914
buffer password = {};
3915
bool mandos_client_exited = false;
3916
bool password_is_read = false;
3918
__attribute__((cleanup(cleanup_string)))
3919
char *tempdir = make_temporary_directory();
3920
g_assert_nonnull(tempdir);
3922
__attribute__((cleanup(cleanup_string)))
3923
char *tempfile = make_temporary_file_in_directory(tempdir);
3924
g_assert_nonnull(tempfile);
3925
int tempfile_fd = open(tempfile, O_WRONLY | O_CLOEXEC | O_NOCTTY
3927
g_assert_cmpint(tempfile_fd, >, 2);
3929
g_assert_true(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
3931
&cancelled_filenames,
3933
&mandos_client_exited,
3934
&password_is_read));
3935
g_assert_cmpint(unlink(tempfile), ==, 0);
3937
g_assert_cmpuint((unsigned int)queue->length, >, 0);
3939
const task_context *const added_read_task
3940
= find_matching_task(queue,
3941
(task_context){ .func=read_inotify_event });
3942
g_assert_nonnull(added_read_task);
3944
g_assert_cmpint(added_read_task->fd, >, 2);
3945
g_assert_true(fd_has_cloexec_and_nonblock(added_read_task->fd));
3947
/* "sufficient to read at least one event." - inotify(7) */
3948
const size_t ievent_size = (sizeof(struct inotify_event)
3950
struct inotify_event *ievent = malloc(ievent_size);
3951
g_assert_nonnull(ievent);
3953
ssize_t read_size = 0;
3954
read_size = read(added_read_task->fd, ievent, ievent_size);
3956
g_assert_cmpint((int)read_size, >, 0);
3957
g_assert_true(ievent->mask & IN_DELETE);
3958
g_assert_cmpstr(ievent->name, ==, basename(tempfile));
3960
g_assert_cmpint(close(tempfile_fd), ==, 0);
3962
/* IN_EXCL_UNLINK should make the closing of the previously unlinked
3963
file not appear as an ievent, so we should not see it now. */
3964
read_size = read(added_read_task->fd, ievent, ievent_size);
3965
g_assert_cmpint((int)read_size, ==, -1);
3966
g_assert_true(errno == EAGAIN);
3970
g_assert_cmpint(rmdir(tempdir), ==, 0);
3973
3736
static void test_read_inotify_event_readerror(__attribute__((unused))
3974
3737
test_fixture *fixture,
3975
3738
__attribute__((unused))
4167
3929
const size_t ievent_max_size = (sizeof(struct inotify_event)
4168
3930
+ NAME_MAX + 1);
4169
3931
g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4171
struct inotify_event event;
4172
char name_buffer[NAME_MAX + 1];
4174
struct inotify_event *const ievent = &ievent_buffer.event;
3932
char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
3933
struct inotify_event *ievent = ((struct inotify_event *)
4176
3936
const char dummy_file_name[] = "ask.dummy_file_name";
4177
3937
ievent->mask = IN_CLOSE_WRITE;
4179
3939
memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4180
3940
const size_t ievent_size = (sizeof(struct inotify_event)
4181
3941
+ sizeof(dummy_file_name));
4182
g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
3942
g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
4183
3943
==, ievent_size);
4184
3944
g_assert_cmpint(close(pipefds[1]), ==, 0);
4262
4022
const size_t ievent_max_size = (sizeof(struct inotify_event)
4263
4023
+ NAME_MAX + 1);
4264
4024
g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4266
struct inotify_event event;
4267
char name_buffer[NAME_MAX + 1];
4269
struct inotify_event *const ievent = &ievent_buffer.event;
4025
char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4026
struct inotify_event *ievent = ((struct inotify_event *)
4271
4029
const char dummy_file_name[] = "ask.dummy_file_name";
4272
4030
ievent->mask = IN_MOVED_TO;
4274
4032
memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4275
4033
const size_t ievent_size = (sizeof(struct inotify_event)
4276
4034
+ sizeof(dummy_file_name));
4277
g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4035
g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
4278
4036
==, ievent_size);
4279
4037
g_assert_cmpint(close(pipefds[1]), ==, 0);
4344
void test_read_inotify_event_IN_MOVED_FROM(__attribute__((unused))
4345
test_fixture *fixture,
4346
__attribute__((unused))
4347
gconstpointer user_data){
4348
__attribute__((cleanup(cleanup_close)))
4349
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
4350
g_assert_cmpint(epoll_fd, >=, 0);
4351
__attribute__((cleanup(string_set_clear)))
4352
string_set cancelled_filenames = {};
4353
const mono_microsecs current_time = 0;
4356
g_assert_cmpint(pipe2(pipefds, O_CLOEXEC | O_NONBLOCK), ==, 0);
4358
/* "sufficient to read at least one event." - inotify(7) */
4359
const size_t ievent_max_size = (sizeof(struct inotify_event)
4361
g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4363
struct inotify_event event;
4364
char name_buffer[NAME_MAX + 1];
4366
struct inotify_event *const ievent = &ievent_buffer.event;
4368
const char dummy_file_name[] = "ask.dummy_file_name";
4369
ievent->mask = IN_MOVED_FROM;
4370
ievent->len = sizeof(dummy_file_name);
4371
memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4372
const size_t ievent_size = (sizeof(struct inotify_event)
4373
+ sizeof(dummy_file_name));
4374
g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4376
g_assert_cmpint(close(pipefds[1]), ==, 0);
4378
bool quit_now = false;
4379
buffer password = {};
4380
bool mandos_client_exited = false;
4381
bool password_is_read = false;
4382
__attribute__((cleanup(cleanup_queue)))
4383
task_queue *queue = create_queue();
4384
g_assert_nonnull(queue);
4386
task_context task = {
4387
.func=read_inotify_event,
4390
.quit_now=&quit_now,
4391
.password=&password,
4392
.filename=strdup("/nonexistent"),
4393
.cancelled_filenames=&cancelled_filenames,
4394
.current_time=¤t_time,
4395
.mandos_client_exited=&mandos_client_exited,
4396
.password_is_read=&password_is_read,
4398
task.func(task, queue);
4399
g_assert_false(quit_now);
4400
g_assert_true(queue->next_run == 0);
4401
g_assert_cmpuint((unsigned int)queue->length, ==, 1);
4403
g_assert_nonnull(find_matching_task(queue, (task_context){
4404
.func=read_inotify_event,
4407
.quit_now=&quit_now,
4408
.password=&password,
4409
.filename=task.filename,
4410
.cancelled_filenames=&cancelled_filenames,
4411
.current_time=¤t_time,
4412
.mandos_client_exited=&mandos_client_exited,
4413
.password_is_read=&password_is_read,
4416
g_assert_true(epoll_set_contains(epoll_fd, pipefds[0],
4417
EPOLLIN | EPOLLRDHUP));
4419
__attribute__((cleanup(cleanup_string)))
4420
char *filename = NULL;
4421
g_assert_cmpint(asprintf(&filename, "%s/%s", task.filename,
4422
dummy_file_name), >, 0);
4423
g_assert_nonnull(filename);
4424
g_assert_true(string_set_contains(*task.cancelled_filenames,
4428
4101
static void test_read_inotify_event_IN_DELETE(__attribute__((unused))
4429
4102
test_fixture *fixture,
4430
4103
__attribute__((unused))
4444
4117
const size_t ievent_max_size = (sizeof(struct inotify_event)
4445
4118
+ NAME_MAX + 1);
4446
4119
g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4448
struct inotify_event event;
4449
char name_buffer[NAME_MAX + 1];
4451
struct inotify_event *const ievent = &ievent_buffer.event;
4120
char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4121
struct inotify_event *ievent = ((struct inotify_event *)
4453
4124
const char dummy_file_name[] = "ask.dummy_file_name";
4454
4125
ievent->mask = IN_DELETE;
4456
4127
memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4457
4128
const size_t ievent_size = (sizeof(struct inotify_event)
4458
4129
+ sizeof(dummy_file_name));
4459
g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4130
g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
4460
4131
==, ievent_size);
4461
4132
g_assert_cmpint(close(pipefds[1]), ==, 0);
4528
4199
const size_t ievent_max_size = (sizeof(struct inotify_event)
4529
4200
+ NAME_MAX + 1);
4530
4201
g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4532
struct inotify_event event;
4533
char name_buffer[NAME_MAX + 1];
4535
struct inotify_event *const ievent = &ievent_buffer.event;
4202
char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4203
struct inotify_event *ievent = ((struct inotify_event *)
4537
4206
const char dummy_file_name[] = "ignored.dummy_file_name";
4538
4207
ievent->mask = IN_CLOSE_WRITE;
4540
4209
memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4541
4210
const size_t ievent_size = (sizeof(struct inotify_event)
4542
4211
+ sizeof(dummy_file_name));
4543
g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4212
g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
4544
4213
==, ievent_size);
4545
4214
g_assert_cmpint(close(pipefds[1]), ==, 0);
4604
4273
const size_t ievent_max_size = (sizeof(struct inotify_event)
4605
4274
+ NAME_MAX + 1);
4606
4275
g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4608
struct inotify_event event;
4609
char name_buffer[NAME_MAX + 1];
4611
struct inotify_event *const ievent = &ievent_buffer.event;
4276
char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4277
struct inotify_event *ievent = ((struct inotify_event *)
4613
4280
const char dummy_file_name[] = "ignored.dummy_file_name";
4614
4281
ievent->mask = IN_MOVED_TO;
4616
4283
memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4617
4284
const size_t ievent_size = (sizeof(struct inotify_event)
4618
4285
+ sizeof(dummy_file_name));
4619
g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4286
g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
4620
4287
==, ievent_size);
4621
4288
g_assert_cmpint(close(pipefds[1]), ==, 0);
4663
4330
EPOLLIN | EPOLLRDHUP));
4667
test_read_inotify_event_IN_MOVED_FROM_badname(__attribute__((unused))
4668
test_fixture *fixture,
4669
__attribute__((unused))
4672
__attribute__((cleanup(cleanup_close)))
4673
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
4674
g_assert_cmpint(epoll_fd, >=, 0);
4675
__attribute__((cleanup(string_set_clear)))
4676
string_set cancelled_filenames = {};
4677
const mono_microsecs current_time = 0;
4680
g_assert_cmpint(pipe2(pipefds, O_CLOEXEC | O_NONBLOCK), ==, 0);
4682
/* "sufficient to read at least one event." - inotify(7) */
4683
const size_t ievent_max_size = (sizeof(struct inotify_event)
4685
g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4687
struct inotify_event event;
4688
char name_buffer[NAME_MAX + 1];
4690
struct inotify_event *const ievent = &ievent_buffer.event;
4692
const char dummy_file_name[] = "ignored.dummy_file_name";
4693
ievent->mask = IN_MOVED_FROM;
4694
ievent->len = sizeof(dummy_file_name);
4695
memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4696
const size_t ievent_size = (sizeof(struct inotify_event)
4697
+ sizeof(dummy_file_name));
4698
g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4700
g_assert_cmpint(close(pipefds[1]), ==, 0);
4702
bool quit_now = false;
4703
buffer password = {};
4704
bool mandos_client_exited = false;
4705
bool password_is_read = false;
4706
__attribute__((cleanup(cleanup_queue)))
4707
task_queue *queue = create_queue();
4708
g_assert_nonnull(queue);
4710
task_context task = {
4711
.func=read_inotify_event,
4714
.quit_now=&quit_now,
4715
.password=&password,
4716
.filename=strdup("/nonexistent"),
4717
.cancelled_filenames=&cancelled_filenames,
4718
.current_time=¤t_time,
4719
.mandos_client_exited=&mandos_client_exited,
4720
.password_is_read=&password_is_read,
4722
task.func(task, queue);
4723
g_assert_false(quit_now);
4724
g_assert_true(queue->next_run == 0);
4725
g_assert_cmpuint((unsigned int)queue->length, ==, 1);
4727
g_assert_nonnull(find_matching_task(queue, (task_context){
4728
.func=read_inotify_event,
4731
.quit_now=&quit_now,
4732
.password=&password,
4733
.filename=task.filename,
4734
.cancelled_filenames=&cancelled_filenames,
4735
.current_time=¤t_time,
4736
.mandos_client_exited=&mandos_client_exited,
4737
.password_is_read=&password_is_read,
4740
g_assert_true(epoll_set_contains(epoll_fd, pipefds[0],
4741
EPOLLIN | EPOLLRDHUP));
4743
__attribute__((cleanup(cleanup_string)))
4744
char *filename = NULL;
4745
g_assert_cmpint(asprintf(&filename, "%s/%s", task.filename,
4746
dummy_file_name), >, 0);
4747
g_assert_nonnull(filename);
4748
g_assert_false(string_set_contains(cancelled_filenames, filename));
4752
4334
void test_read_inotify_event_IN_DELETE_badname(__attribute__((unused))
4753
4335
test_fixture *fixture,
4768
4350
const size_t ievent_max_size = (sizeof(struct inotify_event)
4769
4351
+ NAME_MAX + 1);
4770
4352
g_assert_cmpint(ievent_max_size, <=, PIPE_BUF);
4772
struct inotify_event event;
4773
char name_buffer[NAME_MAX + 1];
4775
struct inotify_event *const ievent = &ievent_buffer.event;
4353
char ievent_buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
4354
struct inotify_event *ievent = ((struct inotify_event *)
4777
4357
const char dummy_file_name[] = "ignored.dummy_file_name";
4778
4358
ievent->mask = IN_DELETE;
4780
4360
memcpy(ievent->name, dummy_file_name, sizeof(dummy_file_name));
4781
4361
const size_t ievent_size = (sizeof(struct inotify_event)
4782
4362
+ sizeof(dummy_file_name));
4783
g_assert_cmpint(write(pipefds[1], (char *)ievent, ievent_size),
4363
g_assert_cmpint(write(pipefds[1], ievent_buffer, ievent_size),
4784
4364
==, ievent_size);
4785
4365
g_assert_cmpint(close(pipefds[1]), ==, 0);
5967
5546
char *const filename = strdup("/nonexistent/socket");
5968
5547
__attribute__((cleanup(string_set_clear)))
5969
5548
string_set cancelled_filenames = {};
5972
/* Find a message size which triggers EMSGSIZE */
5973
__attribute__((cleanup(cleanup_string)))
5974
char *message_buffer = NULL;
5975
size_t message_size = PIPE_BUF + 1;
5976
for(ssize_t ssret = 0; ssret >= 0; message_size += 1024){
5977
if(message_size >= 1024*1024*1024){ /* 1 GiB */
5978
g_test_skip("Skipping EMSGSIZE test: Will not try 1GiB");
5981
message_buffer = realloc(message_buffer, message_size);
5982
if(message_buffer == NULL){
5983
g_test_skip("Skipping EMSGSIZE test");
5984
g_test_message("Failed to malloc() %" PRIuMAX " bytes",
5985
(uintmax_t)message_size);
5988
/* Fill buffer with 'x' */
5989
memset(message_buffer, 'x', message_size);
5990
/* Create a new socketpair for each message size to avoid having
5991
to empty the pipe by reading the message to a separate buffer
5993
g_assert_cmpint(socketpair(PF_LOCAL, SOCK_DGRAM
5994
| SOCK_NONBLOCK | SOCK_CLOEXEC, 0,
5996
ssret = send(socketfds[1], message_buffer, message_size,
5998
error_t saved_errno = errno;
5999
g_assert_cmpint(close(socketfds[0]), ==, 0);
6000
g_assert_cmpint(close(socketfds[1]), ==, 0);
6003
if(saved_errno != EMSGSIZE) {
6004
g_test_skip("Skipping EMSGSIZE test");
6005
g_test_message("Error on send(): %s", strerror(saved_errno));
6009
} else if(ssret != (ssize_t)message_size){
6010
g_test_skip("Skipping EMSGSIZE test");
6011
g_test_message("Partial send(): %" PRIuMAX " of %" PRIdMAX
6012
" bytes", (uintmax_t)ssret,
6013
(intmax_t)message_size);
6017
g_test_message("EMSGSIZE triggered by %" PRIdMAX " bytes",
6018
(intmax_t)message_size);
6021
.data=message_buffer,
6022
.length=message_size - 2, /* Compensate for added '+' and NUL */
6023
.allocated=message_size,
5549
const size_t oversized = 1024*1024; /* Limit seems to be 212960 */
5550
__attribute__((cleanup(cleanup_buffer)))
5552
.data=malloc(oversized),
5554
.allocated=oversized,
5556
g_assert_nonnull(password.data);
6025
5557
if(mlock(password.data, password.allocated) != 0){
6026
5558
g_assert_true(errno == EPERM or errno == ENOMEM);
5560
/* Construct test password buffer */
5561
/* Start with + since that is what the real procotol uses */
5562
password.data[0] = '+';
5563
/* Set a special character at string end just to mark the end */
5564
password.data[oversized-3] = 'y';
5565
/* Set NUL at buffer end, as suggested by the protocol */
5566
password.data[oversized-2] = '\0';
5567
/* Fill rest of password with 'x' */
5568
memset(password.data+1, 'x', oversized-3);
6029
5570
__attribute__((cleanup(cleanup_queue)))
6030
5571
task_queue *queue = create_queue();
6031
5572
g_assert_nonnull(queue);
6032
5574
g_assert_cmpint(socketpair(PF_LOCAL, SOCK_DGRAM
6033
5575
| SOCK_NONBLOCK | SOCK_CLOEXEC, 0,
6034
5576
socketfds), ==, 0);
7941
7481
test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7942
7482
test_add("/queue/create", test_create_queue);
7943
7483
test_add("/queue/add", test_add_to_queue);
7944
test_add("/queue/add/overflow", test_add_to_queue_overflow);
7945
7484
test_add("/queue/has_question/empty",
7946
7485
test_queue_has_question_empty);
7947
7486
test_add("/queue/has_question/false",
8034
7573
test_add_inotify_dir_watch);
8035
7574
test_add_st("/task-creators/add_inotify_dir_watch/fail",
8036
7575
test_add_inotify_dir_watch_fail);
8037
test_add_st("/task-creators/add_inotify_dir_watch/not-a-directory",
8038
test_add_inotify_dir_watch_nondir);
8039
7576
test_add_st("/task-creators/add_inotify_dir_watch/EAGAIN",
8040
7577
test_add_inotify_dir_watch_EAGAIN);
8041
7578
test_add_st("/task-creators/add_inotify_dir_watch/IN_CLOSE_WRITE",
8042
7579
test_add_inotify_dir_watch_IN_CLOSE_WRITE);
8043
7580
test_add_st("/task-creators/add_inotify_dir_watch/IN_MOVED_TO",
8044
7581
test_add_inotify_dir_watch_IN_MOVED_TO);
8045
test_add_st("/task-creators/add_inotify_dir_watch/IN_MOVED_FROM",
8046
test_add_inotify_dir_watch_IN_MOVED_FROM);
8047
test_add_st("/task-creators/add_inotify_dir_watch/IN_EXCL_UNLINK",
8048
test_add_inotify_dir_watch_IN_EXCL_UNLINK);
8049
7582
test_add_st("/task-creators/add_inotify_dir_watch/IN_DELETE",
8050
7583
test_add_inotify_dir_watch_IN_DELETE);
8051
7584
test_add_st("/task/read_inotify_event/readerror",
8060
7593
test_read_inotify_event_IN_CLOSE_WRITE);
8061
7594
test_add_st("/task/read_inotify_event/IN_MOVED_TO",
8062
7595
test_read_inotify_event_IN_MOVED_TO);
8063
test_add_st("/task/read_inotify_event/IN_MOVED_FROM",
8064
test_read_inotify_event_IN_MOVED_FROM);
8065
7596
test_add_st("/task/read_inotify_event/IN_DELETE",
8066
7597
test_read_inotify_event_IN_DELETE);
8067
7598
test_add_st("/task/read_inotify_event/IN_CLOSE_WRITE/badname",
8068
7599
test_read_inotify_event_IN_CLOSE_WRITE_badname);
8069
7600
test_add_st("/task/read_inotify_event/IN_MOVED_TO/badname",
8070
7601
test_read_inotify_event_IN_MOVED_TO_badname);
8071
test_add_st("/task/read_inotify_event/IN_MOVED_FROM/badname",
8072
test_read_inotify_event_IN_MOVED_FROM_badname);
8073
7602
test_add_st("/task/read_inotify_event/IN_DELETE/badname",
8074
7603
test_read_inotify_event_IN_DELETE_badname);
8075
7604
test_add_st("/task/open_and_parse_question/ENOENT",
8172
7701
g_option_context_set_help_enabled(context, FALSE);
8173
7702
g_option_context_set_ignore_unknown_options(context, TRUE);
8175
gboolean should_run_tests = FALSE;
7704
gboolean run_tests = FALSE;
8176
7705
GOptionEntry entries[] = {
8177
7706
{ "test", 0, 0, G_OPTION_ARG_NONE,
8178
&should_run_tests, "Run tests", NULL },
7707
&run_tests, "Run tests", NULL },
8181
7710
g_option_context_add_main_entries(context, entries, NULL);