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
EEXIST, ECHILD, EPERM, ENOMEM,
53
EAGAIN, 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_RDONLY */
86
open(), O_WRONLY, O_NOCTTY,
87
O_RDONLY, O_NOFOLLOW */
87
88
#include <sys/wait.h> /* waitpid(), WNOHANG, WIFEXITED(),
89
90
#include <limits.h> /* PIPE_BUF, NAME_MAX, INT_MAX */
90
91
#include <sys/inotify.h> /* inotify_init1(), IN_NONBLOCK,
91
92
IN_CLOEXEC, inotify_add_watch(),
92
93
IN_CLOSE_WRITE, IN_MOVED_TO,
93
IN_DELETE, struct inotify_event */
94
IN_MOVED_FROM, IN_DELETE,
95
IN_EXCL_UNLINK, IN_ONLYDIR,
96
struct inotify_event */
94
97
#include <fnmatch.h> /* fnmatch(), FNM_FILE_NAME */
95
98
#include <stdio.h> /* asprintf(), FILE, fopen(),
96
99
getline(), sscanf(), feof(),
1020
1024
if(inotify_add_watch(fd, dir, IN_CLOSE_WRITE | IN_MOVED_TO
1021
| IN_MOVED_FROM| IN_DELETE | IN_EXCL_UNLINK)
1025
| IN_MOVED_FROM| IN_DELETE | IN_EXCL_UNLINK
1023
1028
error(0, errno, "Failed to create inotify watch on %s", dir);
2224
2229
__attribute__((cleanup(cleanup_close)))
2225
const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
2230
const int devnull_fd = open("/dev/null",
2231
O_WRONLY | O_CLOEXEC | O_NOCTTY);
2226
2232
g_assert_cmpint(devnull_fd, >=, 0);
2227
2233
__attribute__((cleanup(cleanup_close)))
2228
2234
const int real_stderr_fd = dup(STDERR_FILENO);
2253
2259
__attribute__((cleanup(cleanup_close)))
2254
2260
const int devnull_fd = open("/dev/null",
2255
O_WRONLY | O_CLOEXEC);
2261
O_WRONLY | O_CLOEXEC | O_NOCTTY);
2256
2262
g_assert_cmpint(devnull_fd, >=, 0);
2257
2263
__attribute__((cleanup(cleanup_close)))
2258
2264
const int real_stderr_fd = dup(STDERR_FILENO);
2904
2910
__attribute__((cleanup(cleanup_close)))
2905
2911
const int devnull_fd = open("/dev/null",
2906
O_WRONLY | O_CLOEXEC);
2912
O_WRONLY | O_CLOEXEC | O_NOCTTY);
2907
2913
g_assert_cmpint(devnull_fd, >=, 0);
2908
2914
__attribute__((cleanup(cleanup_close)))
2909
2915
const int real_stderr_fd = dup(STDERR_FILENO);
2975
2981
__attribute__((cleanup(cleanup_close)))
2976
2982
const int devnull_fd = open("/dev/null",
2977
O_WRONLY | O_CLOEXEC);
2983
O_WRONLY | O_CLOEXEC, O_NOCTTY);
2978
2984
g_assert_cmpint(devnull_fd, >=, 0);
2979
2985
__attribute__((cleanup(cleanup_close)))
2980
2986
const int real_stderr_fd = dup(STDERR_FILENO);
3018
3024
buffer password = {};
3020
3026
/* Reading /proc/self/mem from offset 0 will always give EIO */
3021
const int fd = open("/proc/self/mem", O_RDONLY | O_CLOEXEC);
3027
const int fd = open("/proc/self/mem",
3028
O_RDONLY | O_CLOEXEC | O_NOCTTY);
3023
3030
bool password_is_read = false;
3024
3031
bool quit_now = false;
3452
3459
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);
3455
3500
static void test_add_inotify_dir_watch_EAGAIN(__attribute__((unused))
3456
3501
test_fixture *fixture,
3457
3502
__attribute__((unused))
3894
3939
const mono_microsecs current_time = 0;
3896
3941
/* Reading /proc/self/mem from offset 0 will always result in EIO */
3897
const int fd = open("/proc/self/mem", O_RDONLY | O_CLOEXEC);
3942
const int fd = open("/proc/self/mem",
3943
O_RDONLY | O_CLOEXEC | O_NOCTTY);
3899
3945
bool quit_now = false;
3900
3946
__attribute__((cleanup(cleanup_queue)))
5585
5631
__attribute__((unused))
5586
5632
gconstpointer user_data){
5587
5633
__attribute__((cleanup(cleanup_close)))
5588
const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
5634
const int epoll_fd = open("/dev/null",
5635
O_WRONLY | O_CLOEXEC | O_NOCTTY);
5589
5636
__attribute__((cleanup(cleanup_string)))
5590
5637
char *const question_filename = strdup("/nonexistent/question");
5591
5638
g_assert_nonnull(question_filename);
5995
6042
__attribute__((unused))
5996
6043
gconstpointer user_data){
5997
6044
__attribute__((cleanup(cleanup_close)))
5998
const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
6045
const int epoll_fd = open("/dev/null",
6046
O_WRONLY | O_CLOEXEC | O_NOCTTY);
5999
6047
__attribute__((cleanup(cleanup_string)))
6000
6048
char *const question_filename = strdup("/nonexistent/question");
6001
6049
g_assert_nonnull(question_filename);
6264
6312
const char *const
6266
6314
__attribute__((cleanup(cleanup_close)))
6267
const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
6315
const int devnull_fd = open("/dev/null",
6316
O_WRONLY | O_CLOEXEC | O_NOCTTY);
6268
6317
g_assert_cmpint(devnull_fd, >=, 0);
6269
6318
__attribute__((cleanup(cleanup_close)))
6270
6319
const int real_stderr_fd = dup(STDERR_FILENO);
7905
7954
test_add_inotify_dir_watch);
7906
7955
test_add_st("/task-creators/add_inotify_dir_watch/fail",
7907
7956
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);
7908
7959
test_add_st("/task-creators/add_inotify_dir_watch/EAGAIN",
7909
7960
test_add_inotify_dir_watch_EAGAIN);
7910
7961
test_add_st("/task-creators/add_inotify_dir_watch/IN_CLOSE_WRITE",