-
Notifications
You must be signed in to change notification settings - Fork 15
/
kfmon.h
333 lines (294 loc) · 16.7 KB
/
kfmon.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
KFMon: Kobo inotify-based launcher
Copyright (C) 2016-2024 NiLuJe <[email protected]>
SPDX-License-Identifier: GPL-3.0-or-later
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __KFMON_H
#define __KFMON_H
// For syscall, and the expected versions of strerror_r & basename
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include "FBInk/fbink.h"
#include "inih/ini.h"
#include "openssh/atomicio.h"
#include "str5/str5.h"
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <grp.h>
#include <limits.h>
#include <linux/limits.h>
#include <mntent.h>
#include <poll.h>
#include <pthread.h>
#include <pwd.h>
#include <signal.h>
#include <sqlite3.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
// NOTE: This is pulled from SQLite's *internal* API, here be dragons!
// c.f., sqlite/src/main.c
extern const char* sqlite3ErrName(int);
// Fallback version tag...
#ifndef KFMON_VERSION
# define KFMON_VERSION "v1.4.6"
#endif
// Fallback timestamp...
#ifndef KFMON_TIMESTAMP
# define KFMON_TIMESTAMP __TIMESTAMP__
#endif
// Do an ifdef check to allow overriding those at compile-time...
#ifndef KFMON_TARGET_MOUNTPOINT
# define KFMON_TARGET_MOUNTPOINT "/mnt/onboard"
#endif
// Use my debug paths on demand...
#ifndef NILUJE
# define KOBO_DB_PATH KFMON_TARGET_MOUNTPOINT "/.kobo/KoboReader.sqlite"
# define KFMON_LOGFILE "/usr/local/kfmon/kfmon.log"
# define KFMON_CONFIGPATH KFMON_TARGET_MOUNTPOINT "/.adds/kfmon/config"
#else
# define KOBO_DB_PATH "/home/niluje/Kindle/Staging/KoboReader.sqlite"
# define KFMON_LOGFILE "/home/niluje/Kindle/Staging/kfmon.log"
# define KFMON_CONFIGPATH "/home/niluje/Kindle/Staging/kfmon"
#endif
// Path to our pidfile
#define KFMON_PID_FILE "/var/run/kfmon.pid"
// Path to our IPC Unix socket
#define KFMON_IPC_SOCKET "/tmp/kfmon-ipc.ctl"
// MIN/MAX with no side-effects,
// c.f., https://gcc.gnu.org/onlinedocs/cpp/Duplication-of-Side-Effects.html#Duplication-of-Side-Effects
// & https://dustri.org/b/min-and-max-macro-considered-harmful.html
#define MIN(X, Y) \
({ \
__auto_type x_ = (X); \
__auto_type y_ = (Y); \
(x_ < y_) ? x_ : y_; \
})
#define MAX(X, Y) \
({ \
__auto_type x__ = (X); \
__auto_type y__ = (Y); \
(x__ > y__) ? x__ : y__; \
})
// NOTE: See https://kernelnewbies.org/FAQ/DoWhile0 for the reasoning behind the use of GCC's ({ … }) notation
// Log everything to stderr (which actually points to our logfile)
#define LOG(prio, fmt, ...) \
({ \
if (daemonConfig.use_syslog) { \
syslog(prio, fmt, ##__VA_ARGS__); \
} else { \
fprintf(stderr, \
"[KFMon] [%s] [%s] " fmt "\n", \
get_current_time(), \
get_log_prefix(prio), \
##__VA_ARGS__); \
} \
})
// Same, but with __PRETTY_FUNCTION__ right before fmt
#define PFLOG(prio, fmt, ...) ({ LOG(prio, "[%s] " fmt, __PRETTY_FUNCTION__, ##__VA_ARGS__); })
// Slight variation without date/time handling to ensure thread safety
#define MTLOG(prio, fmt, ...) \
({ \
if (daemonConfig.use_syslog) { \
syslog(prio, fmt, ##__VA_ARGS__); \
} else { \
fprintf(stderr, "[KFMon] " fmt "\n", ##__VA_ARGS__); \
} \
})
// Same, but with __PRETTY_FUNCTION__ right before fmt
#define PFMTLOG(prio, fmt, ...) ({ MTLOG(prio, "[%s] " fmt, __PRETTY_FUNCTION__, ##__VA_ARGS__); })
// Some extra verbose stuff is relegated to DEBUG builds... (c.f., https://stackoverflow.com/questions/1644868)
#ifdef DEBUG
# define DEBUG_LOG 1
#else
# define DEBUG_LOG 0
#endif
#define DBGLOG(fmt, ...) \
({ \
if (DEBUG_LOG) { \
LOG(LOG_DEBUG, fmt, ##__VA_ARGS__); \
} \
})
// Likely/Unlikely branch tagging
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
// Max length of a text metadata entry in the database (title, author, comment)
#define DB_SZ_MAX 128
// Max filepath length we bother to handle
// NOTE: PATH_MAX is usually set to 4096, which is fairly overkill here...
// On the other hand, _POSIX_PATH_MAX is always set to 256,
// and that happens to (roughly) match Windows's MAX_PATH, which, in turn,
// matches the FAT32 *filename* length limit.
// NOTE: Possibly *very* roughly, as the FAT32 limit is 255 *Unicode* characters, not bytes ;).
// c.f., https://docs.microsoft.com/de-de/windows/win32/fileio/filesystem-functionality-comparison#limits
// Since we operate on a FAT32 partition, and we mostly work one or two folder deep into our target mountpoint,
// a target mountpoint which itself has a relatively short path,
// we can relatively safely assume that (_POSIX_PATH_MAX * 2) will do the job just fine for our purpose.
// This is all in order to cadge a (very) tiny amount of stack space...
// NOTE: We mainly use this for snprintf usage with thumbnail paths.
#define KFMON_PATH_MAX (_POSIX_PATH_MAX * 2)
// NOTE: This is all well and good, but, in practice, we load our paths from an ini file via inih,
// whose default line buffer is 200 bytes. If we chop the NUL, the CR/LF, the key and the equal sign,
// that would actually leave us somewhere around 186 bytes.
// Just chop that down to 128 for symmetry, and we'll warn in case user input doesn't fit.
#define CFG_SZ_MAX 128
// For sscanf
#define CFG_SZ_MAX_STR "128"
// What the daemon config should look like
typedef struct
{
unsigned short int db_timeout;
bool use_syslog;
bool with_notifications;
} DaemonConfig;
// What a watch config should look like
typedef struct
{
time_t processing_ts;
int inotify_wd;
char filename[CFG_SZ_MAX];
char action[CFG_SZ_MAX];
char label[CFG_SZ_MAX];
char db_title[DB_SZ_MAX];
char db_author[DB_SZ_MAX];
char db_comment[DB_SZ_MAX];
bool hidden;
bool skip_db_checks;
bool do_db_update;
bool block_spawns;
bool wd_was_destroyed;
bool pending_processing;
bool is_active;
} WatchConfig;
// Hardcode the max amount of watches we handle
// NOTE: Cannot exceed INT8_MAX!
#define WATCH_MAX 16
// Used to keep track of our spawned processes, by storing their pids, and their watch idx.
// c.f., https://stackoverflow.com/a/35235950 & https://stackoverflow.com/a/8976461
// As well as issue #2 for details of past failures w/ a SIGCHLD handler
struct process_table
{
pid_t spawn_pids[WATCH_MAX];
// NOTE: Needs to be signed because we use -1 as a special value meaning 'available'.
int8_t spawn_watchids[WATCH_MAX];
} PT;
pthread_mutex_t ptlock = PTHREAD_MUTEX_INITIALIZER;
static void init_process_table(void);
static int8_t get_next_available_pt_entry(void);
static void add_process_to_table(uint8_t, pid_t, uint8_t);
static void remove_process_from_table(uint8_t);
static void init_fbink_config(void);
// SQLite macros inspired from http://www.lemoda.net/c/sqlite-insert/ :)
#define CALL_SQLITE(f) \
({ \
int i; \
i = sqlite3_##f; \
if (i != SQLITE_OK) { \
LOG(LOG_CRIT, "%s failed with status %d: %s", #f, i, sqlite3_errmsg(db)); \
return is_processed; \
} \
})
// Remember stdin/stdout/stderr to restore them in our children
int origStdin;
int origStdout;
int origStderr;
static int daemonize(void);
static struct tm* get_localtime(struct tm* restrict);
static char* format_localtime(struct tm* restrict, char* restrict, size_t);
static char* get_current_time(void);
static char* get_current_time_r(struct tm* restrict, char* restrict, size_t);
static const char* get_log_prefix(int) __attribute__((const));
static bool is_target_mounted(void);
static void wait_for_target_mountpoint(void);
static int strtoul_hu(const char*, unsigned short int* restrict);
static int strtobool(const char* restrict, bool* restrict);
static int daemon_handler(void*, const char* restrict, const char* restrict, const char* restrict);
static int watch_handler(void*, const char* restrict, const char* restrict, const char* restrict);
static bool validate_watch_config(void*);
static bool validate_and_merge_watch_config(void*, uint8_t, bool*);
static int8_t get_next_available_watch_entry(void);
static int fts_alphasort(const FTSENT**, const FTSENT**);
static int load_config(void);
static int update_watch_configs(void);
// Make our config global, because I'm terrible at C.
DaemonConfig daemonConfig = { 0 };
WatchConfig watchConfig[WATCH_MAX] = { 0 };
FBInkConfig fbinkConfig = { 0 };
FBInkState fbinkState = { 0 };
bool need_pen_mode = false;
// NOTE: Unless we're able to tell FBInk to follow the wb's rotation (i.e., with fbdamage's help),
// we want to bracket our refreshes in "pen" mode on older sunxi kernels (c.f., FBInk/#64 for more details),
// so handle the switcheroo in a macro to avoid code duplication...
#define FB_PRINT(msg) \
({ \
if (need_pen_mode) { \
int fbfd = fbink_open(); \
fbink_sunxi_toggle_ntx_pen_mode(fbfd, true); \
\
fbink_print(fbfd, msg, &fbinkConfig); \
\
fbink_sunxi_toggle_ntx_pen_mode(fbfd, false); \
\
fbink_close(fbfd); \
} else { \
fbink_print(FBFD_AUTO, msg, &fbinkConfig); \
} \
})
#define FB_PRINTF(fmt, ...) \
({ \
if (need_pen_mode) { \
int fbfd = fbink_open(); \
fbink_sunxi_toggle_ntx_pen_mode(fbfd, true); \
\
fbink_printf(fbfd, NULL, &fbinkConfig, NULL, fmt, ##__VA_ARGS__); \
\
fbink_sunxi_toggle_ntx_pen_mode(fbfd, false); \
\
fbink_close(fbfd); \
} else { \
fbink_printf(FBFD_AUTO, NULL, &fbinkConfig, NULL, fmt, ##__VA_ARGS__); \
} \
})
// Cute trick from https://stackoverflow.com/a/7618231
#define BOOL2STR(X) ({ ("false\0\0\0true" + 8 * !!(X)); })
static unsigned int qhash(const unsigned char* restrict, size_t);
static bool is_target_processed(uint8_t, bool);
static void* reaper_thread(void*);
static pid_t spawn(char* const*, uint8_t);
static bool is_watch_already_spawned(uint8_t);
static bool is_blocker_running(void);
static bool are_spawns_blocked(void);
static pid_t get_spawn_pid_for_watch(uint8_t);
static bool handle_events(int);
static void get_process_name(const pid_t, char*);
static void get_user_name(const uid_t, char*);
static void get_group_name(const gid_t, char*);
static void handle_connection(int);
static bool handle_ipc(int);
static void sql_errorlogcb(void* __attribute__((unused)), int, const char*);
#endif