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,
73
73
ARGP_ERR_UNKNOWN, ARGP_KEY_ARGS,
74
74
struct argp, argp_parse(),
76
#include <stdint.h> /* SIZE_MAX */
77
76
#include <unistd.h> /* uid_t, gid_t, close(), pipe2(),
78
77
fork(), _exit(), dup2(),
79
78
STDOUT_FILENO, setresgid(),
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
649
__attribute__((nonnull, warn_unused_result))
653
650
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
651
const size_t needed_size = sizeof(task_context)*(queue->length + 1);
662
652
if(needed_size > (queue->allocated)){
663
653
task_context *const new_tasks = realloc(queue->tasks,
1897
1881
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 */
1923
1884
static void dummy_func(__attribute__((unused))
1924
1885
const task_context task,
1925
1886
__attribute__((unused))
2269
2226
__attribute__((cleanup(cleanup_close)))
2270
const int devnull_fd = open("/dev/null",
2271
O_WRONLY | O_CLOEXEC | O_NOCTTY);
2227
const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
2272
2228
g_assert_cmpint(devnull_fd, >=, 0);
2273
2229
__attribute__((cleanup(cleanup_close)))
2274
2230
const int real_stderr_fd = dup(STDERR_FILENO);
2299
2255
__attribute__((cleanup(cleanup_close)))
2300
2256
const int devnull_fd = open("/dev/null",
2301
O_WRONLY | O_CLOEXEC | O_NOCTTY);
2257
O_WRONLY | O_CLOEXEC);
2302
2258
g_assert_cmpint(devnull_fd, >=, 0);
2303
2259
__attribute__((cleanup(cleanup_close)))
2304
2260
const int real_stderr_fd = dup(STDERR_FILENO);
2950
2906
__attribute__((cleanup(cleanup_close)))
2951
2907
const int devnull_fd = open("/dev/null",
2952
O_WRONLY | O_CLOEXEC | O_NOCTTY);
2908
O_WRONLY | O_CLOEXEC);
2953
2909
g_assert_cmpint(devnull_fd, >=, 0);
2954
2910
__attribute__((cleanup(cleanup_close)))
2955
2911
const int real_stderr_fd = dup(STDERR_FILENO);
3021
2977
__attribute__((cleanup(cleanup_close)))
3022
2978
const int devnull_fd = open("/dev/null",
3023
O_WRONLY | O_CLOEXEC, O_NOCTTY);
2979
O_WRONLY | O_CLOEXEC);
3024
2980
g_assert_cmpint(devnull_fd, >=, 0);
3025
2981
__attribute__((cleanup(cleanup_close)))
3026
2982
const int real_stderr_fd = dup(STDERR_FILENO);
3979
3934
const mono_microsecs current_time = 0;
3981
3936
/* Reading /proc/self/mem from offset 0 will always result in EIO */
3982
const int fd = open("/proc/self/mem",
3983
O_RDONLY | O_CLOEXEC | O_NOCTTY);
3937
const int fd = open("/proc/self/mem", O_RDONLY | O_CLOEXEC);
3985
3939
bool quit_now = false;
3986
3940
__attribute__((cleanup(cleanup_queue)))
5671
5625
__attribute__((unused))
5672
5626
gconstpointer user_data){
5673
5627
__attribute__((cleanup(cleanup_close)))
5674
const int epoll_fd = open("/dev/null",
5675
O_WRONLY | O_CLOEXEC | O_NOCTTY);
5628
const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
5676
5629
__attribute__((cleanup(cleanup_string)))
5677
5630
char *const question_filename = strdup("/nonexistent/question");
5678
5631
g_assert_nonnull(question_filename);
6086
6035
__attribute__((unused))
6087
6036
gconstpointer user_data){
6088
6037
__attribute__((cleanup(cleanup_close)))
6089
const int epoll_fd = open("/dev/null",
6090
O_WRONLY | O_CLOEXEC | O_NOCTTY);
6038
const int epoll_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
6091
6039
__attribute__((cleanup(cleanup_string)))
6092
6040
char *const question_filename = strdup("/nonexistent/question");
6093
6041
g_assert_nonnull(question_filename);
6356
6304
const char *const
6358
6306
__attribute__((cleanup(cleanup_close)))
6359
const int devnull_fd = open("/dev/null",
6360
O_WRONLY | O_CLOEXEC | O_NOCTTY);
6307
const int devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
6361
6308
g_assert_cmpint(devnull_fd, >=, 0);
6362
6309
__attribute__((cleanup(cleanup_close)))
6363
6310
const int real_stderr_fd = dup(STDERR_FILENO);
7906
7853
test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7907
7854
test_add("/queue/create", test_create_queue);
7908
7855
test_add("/queue/add", test_add_to_queue);
7909
test_add("/queue/add/overflow", test_add_to_queue_overflow);
7910
7856
test_add("/queue/has_question/empty",
7911
7857
test_queue_has_question_empty);
7912
7858
test_add("/queue/has_question/false",
8137
8083
g_option_context_set_help_enabled(context, FALSE);
8138
8084
g_option_context_set_ignore_unknown_options(context, TRUE);
8140
gboolean should_run_tests = FALSE;
8086
gboolean run_tests = FALSE;
8141
8087
GOptionEntry entries[] = {
8142
8088
{ "test", 0, 0, G_OPTION_ARG_NONE,
8143
&should_run_tests, "Run tests", NULL },
8089
&run_tests, "Run tests", NULL },
8146
8092
g_option_context_add_main_entries(context, entries, NULL);