-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremindd.c
313 lines (259 loc) · 8.32 KB
/
remindd.c
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
// remindme Daemon
#include "shared.h"
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/inotify.h>
#include <sys/timerfd.h>
#include <libnotify/notify.h>
#define EXIT_SUCCESS 0
#define EXIT_ERR_INIT_EPOLL 1
#define EXIT_ERR_INIT_INOTIFY 2
#define EXIT_ERR_ADD_WATCH 3
#define EXIT_FILE_DELETED 4
#define EXIT_ERR_GET_CURR_TIME 5
#define EXIT_INIT_TIMERFD 6
#define EXIT_ERR_EPOLL_WAIT 7
#define EXIT_ERR_SET_TIMERFD 8
#define EXIT_ERR_INIT_LIBNOTIFY 9
typedef struct {
int inotify_fd;
int ievent_status;
int timer_fd;
int epoll_fd;
struct epoll_event ev;
} SignalContext;
static SignalContext context;
static FILE *file;
void init_signal_context(int inotify_fd, int ievent_status, int timer_fd,
int epoll_fd, struct epoll_event ev) {
context.inotify_fd = inotify_fd;
context.ievent_status = ievent_status;
context.timer_fd = timer_fd;
context.epoll_fd = epoll_fd;
context.ev = ev;
}
void signal_handler(int signal) {
int inotify_close_status = -1;
printf("Signal received, cleaning up...\n");
if (context.ievent_status != -1) {
inotify_close_status =
inotify_rm_watch(context.inotify_fd, context.ievent_status);
if (inotify_close_status == -1) {
fprintf(stderr, "err: failed to remove inotify watch\n");
}
}
if (context.epoll_fd != -1) {
if (close(context.epoll_fd) == -1) {
fprintf(stderr, "err: failed to close epoll instance\n");
}
}
if (context.timer_fd != -1) {
if (close(context.timer_fd) == -1) {
fprintf(stderr, "err: failed to close timerfd\n");
}
}
if (context.inotify_fd != -1) {
close(context.inotify_fd);
}
if (file != NULL) {
fclose(file);
}
exit(EXIT_SUCCESS);
}
struct Reminder *get_next_reminder(struct Reminder *reminders) {
int reminder_count = get_reminder_count(file);
if (reminder_count == 0) {
return NULL;
}
struct Reminder *reminder = malloc(sizeof(struct Reminder));
// Initialize reminder with the first reminder
reminder->id = reminders[0].id;
reminder->message = reminders[0].message;
reminder->time = reminders[0].time;
char *buf = read_to_buf(file);
char *split = strtok(buf, "\n");
split = strtok(NULL, "\n");
while (split != NULL) {
unsigned short id;
char *message;
time_t time;
sscanf(split, "%hu === %m[^=] === %ld", &id, &message, &time);
struct Reminder curr_reminder = {id, message, time};
// Check if current reminder is earlier than reminder
if (difftime(curr_reminder.time, reminder->time) < 0) {
reminder->id = curr_reminder.id;
reminder->message = curr_reminder.message;
reminder->time = curr_reminder.time;
}
split = strtok(NULL, "\n");
}
free(buf);
return reminder;
}
void trigger_notification(char *title, char *message) {
NotifyNotification *notif_handle;
if (!notify_init("remindme")) {
fprintf(stderr, "err: failed to initialize libnotify\n");
exit(EXIT_ERR_INIT_LIBNOTIFY);
}
notif_handle = notify_notification_new(title, message, "dialog-information");
notify_notification_set_timeout(notif_handle, NOTIFY_EXPIRES_NEVER);
notify_notification_set_urgency(notif_handle, NOTIFY_URGENCY_CRITICAL);
notify_notification_set_category(notif_handle, "reminder");
notify_notification_show(notif_handle, NULL);
}
void load_reminders() {
fclose(file);
file = fopen(get_file_path(), "r");
struct Reminder *reminders = get_reminders(file);
struct Reminder *next_reminder = get_next_reminder(reminders);
if (get_reminder_count(file) != 0) {
printf("Next reminder: %s\n", next_reminder->message);
} else {
printf("No reminders found\n");
}
}
void update_timer(struct Reminder *next_reminder, int timer_fd, int epoll_fd,
struct epoll_event ev) {
struct itimerspec new_value;
time_t now = time(NULL);
time_t seconds = next_reminder->time - now;
new_value.it_value.tv_sec = seconds;
new_value.it_value.tv_nsec = 0;
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_nsec = 0;
if (seconds <= 0) {
fclose(file);
file = fopen(get_file_path(), "r");
trigger_notification("Overdue Reminder!", next_reminder->message);
delete_reminder(next_reminder->id, file);
load_reminders();
if (get_reminder_count(file) != 0) {
now = time(NULL);
seconds = next_reminder->time - now;
new_value.it_value.tv_sec = seconds;
}
}
if (timerfd_settime(timer_fd, 0, &new_value, NULL) == -1 && seconds >= 0) {
fprintf(stderr, "err: failed to set timerfd\n");
exit(EXIT_ERR_SET_TIMERFD);
}
ev.data.fd = timer_fd;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, timer_fd, &ev) == -1) {
if (errno == EEXIST) {
if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, timer_fd, &ev) == -1) {
fprintf(stderr, "err: failed to modify timer event\n");
perror("epoll_ctl");
exit(EXIT_ERR_INIT_EPOLL);
}
} else {
fprintf(stderr, "err: failed to add timer_fd to epoll\n");
perror("epoll_ctl");
exit(EXIT_ERR_INIT_EPOLL);
}
}
}
int main() {
int inotify_fd = -1;
int ievent_status = -1;
int epoll_fd = epoll_create1(0);
if (epoll_fd == -1) {
fprintf(stderr, "err: failed to create epoll instance\n");
exit(EXIT_ERR_INIT_EPOLL);
}
const uint32_t INOTIFY_MASK = IN_MODIFY | IN_CREATE | IN_DELETE;
inotify_fd = inotify_init1(IN_NONBLOCK);
if (inotify_fd == -1) {
fprintf(stderr, "err: failed to initialize inotify instance\n");
exit(EXIT_ERR_INIT_INOTIFY);
}
char *file_path = get_file_path();
file = fopen(file_path, "r");
if (file == NULL) {
file = fopen(file_path, "wb");
printf("remindd: Reminders file not found, created new file %s\n",
file_path);
if (file == NULL) {
fprintf(stderr, "err: failed to open %s\n", file_path);
exit(EXIT_FILE_DELETED);
}
}
ievent_status = inotify_add_watch(inotify_fd, file_path, INOTIFY_MASK);
if (ievent_status == -1) {
fprintf(stderr, "err: failed to add %s to inotify watch list\n", file_path);
exit(EXIT_ERR_ADD_WATCH);
}
printf("Watching %s for changes\n", file_path);
struct epoll_event ev;
ev.events = EPOLLIN;
// Initialize timer_fd
int timer_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
if (timer_fd == -1) {
fprintf(stderr, "err: failed to create timerfd\n");
exit(EXIT_INIT_TIMERFD);
}
load_reminders();
if (get_reminder_count(file) != 0) {
update_timer(get_next_reminder(get_reminders(file)), timer_fd, epoll_fd,
ev);
}
ev.data.fd = inotify_fd;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, inotify_fd, &ev) == -1) {
fprintf(stderr, "err: failed to add inotify_fd to epoll\n");
exit(EXIT_ERR_INIT_EPOLL);
}
init_signal_context(inotify_fd, ievent_status, timer_fd, epoll_fd, ev);
signal(SIGABRT, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
while (1) {
struct epoll_event events[2];
int n = epoll_wait(epoll_fd, events, 2, -1);
if (n == -1) {
fprintf(stderr, "err: failed to wait for epoll events\n");
exit(EXIT_ERR_EPOLL_WAIT);
}
bool reload = false;
for (int i = 0; i < n; i++) {
if (events[i].data.fd == inotify_fd) {
// Reload reminders
printf("Reloading reminders\n");
// Clear the inotify event
char buffer[1024];
read(inotify_fd, buffer, sizeof(buffer));
reload = true;
} else if (events[i].data.fd == timer_fd) {
trigger_notification("Reminder!",
get_next_reminder(get_reminders(file))->message);
uint64_t expirations;
ssize_t s = read(timer_fd, &expirations, sizeof(expirations));
if (s == -1) {
if (errno != EAGAIN) {
fprintf(stderr, "err: failed to read from timerfd\n");
exit(EXIT_ERR_SET_TIMERFD);
}
continue;
}
struct Reminder *next_reminder = get_next_reminder(get_reminders(file));
delete_reminder(next_reminder->id, file);
reload = true;
}
}
if (reload) {
load_reminders();
if (get_reminder_count(file) != 0) {
update_timer(get_next_reminder(get_reminders(file)), timer_fd, epoll_fd,
ev);
}
}
}
free(file_path);
return EXIT_SUCCESS;
}