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, EEXIST,
52
ECHILD, EPERM, ENOMEM, EAGAIN,
53
EINTR, ENOBUFS, EADDRINUSE,
51
ENAMETOOLONG, ENOENT, ENOTDIR,
52
ENOMEM, EEXIST, ECHILD, EPERM,
53
EAGAIN, EINTR, ENOBUFS, EADDRINUSE,
54
54
ECONNREFUSED, ECONNRESET,
55
55
ETOOMANYREFS, EMSGSIZE, EBADF,
73
73
ARGP_ERR_UNKNOWN, ARGP_KEY_ARGS,
74
74
struct argp, argp_parse(),
76
#include <stdint.h> /* SIZE_MAX */
76
77
#include <unistd.h> /* uid_t, gid_t, close(), pipe2(),
77
78
fork(), _exit(), dup2(),
78
79
STDOUT_FILENO, setresgid(),
83
84
#include <sys/mman.h> /* munlock(), mlock() */
84
85
#include <fcntl.h> /* O_CLOEXEC, O_NONBLOCK, fcntl(),
85
86
F_GETFD, F_GETFL, FD_CLOEXEC,
86
open(), O_WRONLY, O_RDONLY */
87
open(), O_WRONLY, O_NOCTTY,
88
O_RDONLY, O_NOFOLLOW */
87
89
#include <sys/wait.h> /* waitpid(), WNOHANG, WIFEXITED(),
89
91
#include <limits.h> /* PIPE_BUF, NAME_MAX, INT_MAX */
90
92
#include <sys/inotify.h> /* inotify_init1(), IN_NONBLOCK,
91
93
IN_CLOEXEC, inotify_add_watch(),
92
94
IN_CLOSE_WRITE, IN_MOVED_TO,
93
IN_DELETE, struct inotify_event */
95
IN_MOVED_FROM, IN_DELETE,
96
IN_EXCL_UNLINK, IN_ONLYDIR,
97
struct inotify_event */
94
98
#include <fnmatch.h> /* fnmatch(), FNM_FILE_NAME */
95
#include <stdio.h> /* asprintf(), FILE, fopen(),
96
getline(), sscanf(), feof(),
97
ferror(), fclose(), stderr,
98
rename(), fdopen(), fprintf(),
99
#include <stdio.h> /* asprintf(), FILE, stderr, fopen(),
100
fclose(), getline(), sscanf(),
101
feof(), ferror(), rename(),
102
fdopen(), fprintf(), fscanf() */
100
103
#include <glib.h> /* GKeyFile, g_key_file_free(), g_key_file_new(),
101
104
GError, g_key_file_load_from_file(),
102
105
G_KEY_FILE_NONE, TRUE, G_FILE_ERROR_NOENT,
648
652
__attribute__((nonnull, warn_unused_result))
649
653
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));
650
661
const size_t needed_size = sizeof(task_context)*(queue->length + 1);
651
662
if(needed_size > (queue->allocated)){
652
663
task_context *const new_tasks = realloc(queue->tasks,
1879
1897
g_assert_true(queue->tasks[0].func == dummy_func);
1900
static void test_add_to_queue_overflow(__attribute__((unused))
1901
test_fixture *fixture,
1902
__attribute__((unused))
1903
gconstpointer user_data){
1904
__attribute__((cleanup(cleanup_queue)))
1905
task_queue *queue = create_queue();
1906
g_assert_nonnull(queue);
1907
g_assert_true(queue->length == 0);
1908
queue->length = SIZE_MAX / sizeof(task_context); /* fake max size */
1910
FILE *real_stderr = stderr;
1911
FILE *devnull = fopen("/dev/null", "we");
1912
g_assert_nonnull(devnull);
1914
const bool ret = add_to_queue(queue,
1915
(task_context){ .func=dummy_func });
1916
g_assert_true(errno == ENOMEM);
1917
g_assert_false(ret);
1918
stderr = real_stderr;
1919
g_assert_cmpint(fclose(devnull), ==, 0);
1920
queue->length = 0; /* Restore real size */
1882
1923
static void dummy_func(__attribute__((unused))
1883
1924
const task_context task,
1884
1925
__attribute__((unused))
2224
2269
__attribute__((cleanup(cleanup_close)))
2225
const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
2270
const int devnull_fd = open("/dev/null",
2271
O_WRONLY | O_CLOEXEC | O_NOCTTY);
2226
2272
g_assert_cmpint(devnull_fd, >=, 0);
2227
2273
__attribute__((cleanup(cleanup_close)))
2228
2274
const int real_stderr_fd = dup(STDERR_FILENO);
2253
2299
__attribute__((cleanup(cleanup_close)))
2254
2300
const int devnull_fd = open("/dev/null",
2255
O_WRONLY | O_CLOEXEC);
2301
O_WRONLY | O_CLOEXEC | O_NOCTTY);
2256
2302
g_assert_cmpint(devnull_fd, >=, 0);
2257
2303
__attribute__((cleanup(cleanup_close)))
2258
2304
const int real_stderr_fd = dup(STDERR_FILENO);
2904
2950
__attribute__((cleanup(cleanup_close)))
2905
2951
const int devnull_fd = open("/dev/null",
2906
O_WRONLY | O_CLOEXEC);
2952
O_WRONLY | O_CLOEXEC | O_NOCTTY);
2907
2953
g_assert_cmpint(devnull_fd, >=, 0);
2908
2954
__attribute__((cleanup(cleanup_close)))
2909
2955
const int real_stderr_fd = dup(STDERR_FILENO);
2975
3021
__attribute__((cleanup(cleanup_close)))
2976
3022
const int devnull_fd = open("/dev/null",
2977
O_WRONLY | O_CLOEXEC);
3023
O_WRONLY | O_CLOEXEC, O_NOCTTY);
2978
3024
g_assert_cmpint(devnull_fd, >=, 0);
2979
3025
__attribute__((cleanup(cleanup_close)))
2980
3026
const int real_stderr_fd = dup(STDERR_FILENO);
3452
3499
g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3502
static void test_add_inotify_dir_watch_nondir(__attribute__((unused))
3503
test_fixture *fixture,
3504
__attribute__((unused))
3507
__attribute__((cleanup(cleanup_close)))
3508
const int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3509
g_assert_cmpint(epoll_fd, >=, 0);
3510
__attribute__((cleanup(cleanup_queue)))
3511
task_queue *queue = create_queue();
3512
g_assert_nonnull(queue);
3513
__attribute__((cleanup(string_set_clear)))
3514
string_set cancelled_filenames = {};
3515
const mono_microsecs current_time = 0;
3517
bool quit_now = false;
3518
buffer password = {};
3519
bool mandos_client_exited = false;
3520
bool password_is_read = false;
3522
const char not_a_directory[] = "/dev/tty";
3524
FILE *real_stderr = stderr;
3525
FILE *devnull = fopen("/dev/null", "we");
3526
g_assert_nonnull(devnull);
3528
g_assert_false(add_inotify_dir_watch(queue, epoll_fd, &quit_now,
3529
&password, not_a_directory,
3530
&cancelled_filenames,
3532
&mandos_client_exited,
3533
&password_is_read));
3534
stderr = real_stderr;
3535
g_assert_cmpint(fclose(devnull), ==, 0);
3537
g_assert_cmpuint((unsigned int)queue->length, ==, 0);
3455
3540
static void test_add_inotify_dir_watch_EAGAIN(__attribute__((unused))
3456
3541
test_fixture *fixture,
3457
3542
__attribute__((unused))
3894
3979
const mono_microsecs current_time = 0;
3896
3981
/* Reading /proc/self/mem from offset 0 will always result in EIO */
3897
const int fd = open("/proc/self/mem", O_RDONLY | O_CLOEXEC);
3982
const int fd = open("/proc/self/mem",
3983
O_RDONLY | O_CLOEXEC | O_NOCTTY);
3899
3985
bool quit_now = false;
3900
3986
__attribute__((cleanup(cleanup_queue)))
5585
5671
__attribute__((unused))
5586
5672
gconstpointer user_data){
5587
5673
__attribute__((cleanup(cleanup_close)))
5588
const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
5674
const int epoll_fd = open("/dev/null",
5675
O_WRONLY | O_CLOEXEC | O_NOCTTY);
5589
5676
__attribute__((cleanup(cleanup_string)))
5590
5677
char *const question_filename = strdup("/nonexistent/question");
5591
5678
g_assert_nonnull(question_filename);
5995
6086
__attribute__((unused))
5996
6087
gconstpointer user_data){
5997
6088
__attribute__((cleanup(cleanup_close)))
5998
const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
6089
const int epoll_fd = open("/dev/null",
6090
O_WRONLY | O_CLOEXEC | O_NOCTTY);
5999
6091
__attribute__((cleanup(cleanup_string)))
6000
6092
char *const question_filename = strdup("/nonexistent/question");
6001
6093
g_assert_nonnull(question_filename);
6264
6356
const char *const
6266
6358
__attribute__((cleanup(cleanup_close)))
6267
const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
6359
const int devnull_fd = open("/dev/null",
6360
O_WRONLY | O_CLOEXEC | O_NOCTTY);
6268
6361
g_assert_cmpint(devnull_fd, >=, 0);
6269
6362
__attribute__((cleanup(cleanup_close)))
6270
6363
const int real_stderr_fd = dup(STDERR_FILENO);
7813
7906
test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7814
7907
test_add("/queue/create", test_create_queue);
7815
7908
test_add("/queue/add", test_add_to_queue);
7909
test_add("/queue/add/overflow", test_add_to_queue_overflow);
7816
7910
test_add("/queue/has_question/empty",
7817
7911
test_queue_has_question_empty);
7818
7912
test_add("/queue/has_question/false",
7905
7999
test_add_inotify_dir_watch);
7906
8000
test_add_st("/task-creators/add_inotify_dir_watch/fail",
7907
8001
test_add_inotify_dir_watch_fail);
8002
test_add_st("/task-creators/add_inotify_dir_watch/not-a-directory",
8003
test_add_inotify_dir_watch_nondir);
7908
8004
test_add_st("/task-creators/add_inotify_dir_watch/EAGAIN",
7909
8005
test_add_inotify_dir_watch_EAGAIN);
7910
8006
test_add_st("/task-creators/add_inotify_dir_watch/IN_CLOSE_WRITE",
8041
8137
g_option_context_set_help_enabled(context, FALSE);
8042
8138
g_option_context_set_ignore_unknown_options(context, TRUE);
8044
gboolean run_tests = FALSE;
8140
gboolean should_run_tests = FALSE;
8045
8141
GOptionEntry entries[] = {
8046
8142
{ "test", 0, 0, G_OPTION_ARG_NONE,
8047
&run_tests, "Run tests", NULL },
8143
&should_run_tests, "Run tests", NULL },
8050
8146
g_option_context_add_main_entries(context, entries, NULL);