-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipc.c
396 lines (333 loc) · 10.7 KB
/
ipc.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* This file is part of clipsim.
* Copyright (C) 2024 Lucas Mior
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "clipsim.h"
static File command_fifo = { .file = NULL, .fd = -1,
.name = "/tmp/clipsim/command.fifo" };
static File passid_fifo = { .file = NULL, .fd = -1,
.name = "/tmp/clipsim/passid.fifo" };
static File content_fifo = { .file = NULL, .fd = -1,
.name = "/tmp/clipsim/content.fifo" };
static void ipc_daemon_history_save(void);
static void ipc_client_check_save(void);
static void ipc_daemon_pipe_entries(void);
static void ipc_daemon_pipe_id(int32);
static void ipc_client_print_entries(void);
static int32 ipc_daemon_get_id(void);
static void ipc_client_ask_id(int32);
static void ipc_make_fifos(void);
static void ipc_clean_fifo(const char *);
static void ipc_create_fifo(const char *);
static void sig_abrt_handler(int32) __attribute__((noreturn));
void
sig_abrt_handler(int32 unused) {
(void) unused;
error("Received SIGABRT signal, something is wrong with history file.\n");
error("Creating backup for history file...\n");
history_backup();
error("Restarting clipsim --daemon with empty history...\n");
execlp("clipsim", "clipsim", "--daemon", NULL);
error("Error while trying to exec clipsim --daemon: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
int32
ipc_daemon_listen_fifo(void *unused) {
DEBUG_PRINT("void");
(void) unused;
char command;
struct timespec pause;
pause.tv_sec = 0;
pause.tv_nsec = PAUSE10MS;
ipc_make_fifos();
signal(SIGABRT, sig_abrt_handler);
while (true) {
isize r;
nanosleep(&pause, NULL);
if (util_open(&command_fifo, O_RDONLY) < 0)
continue;
mtx_lock(&lock);
r = read(command_fifo.fd, &command, sizeof(*(&command)));
if (r < (isize) sizeof(*(&command))) {
error("Error reading command from %s: %s\n",
command_fifo.name, strerror(errno));
mtx_unlock(&lock);
continue;
}
util_close(&command_fifo);
switch (command) {
case COMMAND_PRINT:
ipc_daemon_pipe_entries();
break;
case COMMAND_SAVE:
ipc_daemon_history_save();
break;
case COMMAND_COPY:
history_recover(ipc_daemon_get_id());
break;
case COMMAND_REMOVE:
history_remove(ipc_daemon_get_id());
break;
case COMMAND_INFO:
ipc_daemon_pipe_id(ipc_daemon_get_id());
break;
default:
error("Invalid command received: '%c'\n", command);
}
mtx_unlock(&lock);
}
}
void
ipc_client_speak_fifo(int32 command, int32 id) {
DEBUG_PRINT("%u, %d", command, id);
isize w;
if (util_open(&command_fifo, O_WRONLY | O_NONBLOCK) < 0) {
error("Could not open Fifo for sending command to daemon. "
"Is `%s daemon` running?\n", "clipsim");
exit(EXIT_FAILURE);
}
w = write(command_fifo.fd, &command, sizeof(*(&command)));
util_close(&command_fifo);
if (w < (isize) sizeof(*(&command))) {
error("Error writing command to %s: %s\n",
command_fifo.name, strerror(errno));
exit(EXIT_FAILURE);
}
switch (command) {
case COMMAND_PRINT:
ipc_client_print_entries();
break;
case COMMAND_SAVE:
ipc_client_check_save();
break;
case COMMAND_COPY:
case COMMAND_REMOVE:
ipc_client_ask_id(id);
break;
case COMMAND_INFO:
ipc_client_ask_id(id);
ipc_client_print_entries();
break;
default:
error("Invalid command: %u\n", command);
exit(EXIT_FAILURE);
}
return;
}
void
ipc_daemon_history_save(void) {
DEBUG_PRINT("void");
char saved;
isize saved_size = sizeof(*(&saved));
error("Trying to save history...\n");
if (util_open(&content_fifo, O_WRONLY) < 0)
return;
saved = history_save();
if (write(content_fifo.fd, &saved, (usize) saved_size) < saved_size)
error("Error sending save result to client.\n");
util_close(&content_fifo);
return;
}
void
ipc_client_check_save(void) {
DEBUG_PRINT("void");
isize r;
char saved = 0;
error("Trying to save history...\n");
if (util_open(&content_fifo, O_RDONLY) < 0)
exit(EXIT_FAILURE);
if ((r = read(content_fifo.fd, &saved, sizeof(*(&saved)))) > 0) {
if (saved)
error("History saved to disk.\n");
else
error("Error saving history to disk.\n");
}
util_close(&content_fifo);
if (!saved)
exit(EXIT_FAILURE);
return;
}
void
ipc_daemon_pipe_entries(void) {
DEBUG_PRINT("void");
static char buffer[BUFSIZ];
int32 history_length;
content_fifo.file = fopen(content_fifo.name, "w");
setvbuf(content_fifo.file, buffer, _IOFBF, BUFSIZ);
history_length = history_length_get();
if (history_length <= 0) {
error("Clipboard history empty. Start copying text.\n");
goto close;
}
for (int32 i = history_length - 1; i >= 0; i -= 1) {
Entry *e = &entries[i];
usize size = (usize) e->trimmed_length + 1;
char *trimmed = &e->content[e->trimmed];
fprintf(content_fifo.file, "%.*d ", PRINT_DIGITS, i);
if (fwrite(trimmed, 1, size, content_fifo.file) < size) {
error("Error writing to client fifo.\n");
break;
}
}
fflush(content_fifo.file);
close:
util_close(&content_fifo);
return;
}
void
ipc_daemon_pipe_id(int32 id) {
DEBUG_PRINT("%d", id);
Entry *e;
int32 history_length;
usize tag_size = sizeof(*(&IMAGE_TAG));
if (util_open(&content_fifo, O_WRONLY) < 0)
return;
history_length = history_length_get();
if (history_length <= -1) {
error("Clipboard history empty. Start copying text.\n");
dprintf(content_fifo.fd,
"000 Clipboard history empty. Start copying text.\n");
goto close;
}
if (id < 0)
id = history_length + id;
if ((id >= history_length) || (id < 0)) {
error("Invalid index: %d\n", id);
goto close;
}
e = &entries[id];
if (is_image[id]) {
if (write(content_fifo.fd, &IMAGE_TAG, tag_size) < (isize)tag_size) {
dprintf(content_fifo.fd, "Error printing image tag.\n");
goto close;
}
} else {
dprintf(content_fifo.fd,
"Lenght: \033[31;1m%d\n\033[0;m", e->content_length);
}
dprintf(content_fifo.fd, "%s", e->content);
close:
util_close(&content_fifo);
return;
}
void
ipc_client_print_entries(void) {
DEBUG_PRINT("void");
static char buffer[BUFSIZ];
isize r;
if (util_open(&content_fifo, O_RDONLY) < 0)
return;
r = read(content_fifo.fd, buffer, sizeof(buffer));
if (r <= 0) {
error("Error reading data from %s", content_fifo.name);
if (r < 0)
error(": %s", strerror(errno));
error(".\n");
util_close(&content_fifo);
exit(EXIT_FAILURE);
}
if (buffer[0] != IMAGE_TAG) {
do {
fwrite(buffer, 1, (usize) r, stdout);
} while ((r = read(content_fifo.fd, buffer, sizeof(buffer))) > 0);
} else {
int32 test;
char *CLIPSIM_IMAGE_PREVIEW;
if (r == 1) {
r = read(content_fifo.fd, buffer + 1, sizeof(buffer) - 1);
if (r <= 0) {
util_die_notify("Error reading image name from %s.\n",
content_fifo.name);
}
}
util_close(&content_fifo);
if ((test = open(buffer + 1, O_RDONLY)) >= 0) {
close(test);
} else {
error("Error opening %s: %s\n", buffer + 1, strerror(errno));
return;
}
CLIPSIM_IMAGE_PREVIEW = getenv("CLIPSIM_IMAGE_PREVIEW");
if (CLIPSIM_IMAGE_PREVIEW == NULL)
CLIPSIM_IMAGE_PREVIEW = "chafa";
if (!strcmp(CLIPSIM_IMAGE_PREVIEW, "stiv_draw"))
execlp("stiv_draw", "stiv_draw", buffer + 1, "30", "15", NULL);
else
execlp("chafa", "chafa", buffer + 1, "-s", "40x", NULL);
}
util_close(&content_fifo);
return;
}
int32
ipc_daemon_get_id(void) {
DEBUG_PRINT("void");
int32 id;
if ((passid_fifo.file = fopen(passid_fifo.name, "r")) == NULL) {
error("Error opening fifo for reading id: %s\n", strerror(errno));
return HISTORY_INVALID_ID;
}
if (fread(&id, sizeof(*(&id)), 1, passid_fifo.file) != 1) {
error("Error reading id from pipe: %s\n", strerror(errno));
util_close(&passid_fifo);
return HISTORY_INVALID_ID;
}
util_close(&passid_fifo);
return id;
}
void
ipc_client_ask_id(int32 id) {
DEBUG_PRINT("%d", id);
if ((passid_fifo.file = fopen(passid_fifo.name, "w")) == NULL) {
util_die_notify("Error opening fifo for sending id to daemon: "
"%s\n", strerror(errno));
}
if (fwrite(&id, sizeof(*(&id)), 1, passid_fifo.file) != 1)
error("Error sending id to daemon: %s\n", strerror(errno));
util_close(&passid_fifo);
return;
}
void
ipc_make_fifos(void) {
DEBUG_PRINT("void");
char *tmp = "/tmp/clipsim";
if (mkdir(tmp, 0770) < 0) {
if (errno != EEXIST)
util_die_notify("Error creating %s: %s\n", tmp, strerror(errno));
}
ipc_clean_fifo(command_fifo.name);
ipc_clean_fifo(passid_fifo.name);
ipc_clean_fifo(content_fifo.name);
ipc_create_fifo(command_fifo.name);
ipc_create_fifo(passid_fifo.name);
ipc_create_fifo(content_fifo.name);
return;
}
void
ipc_clean_fifo(const char *name) {
DEBUG_PRINT("%s", name);
if (unlink(name) < 0) {
if (errno != ENOENT)
util_die_notify("Error deleting %s: %s\n", name, strerror(errno));
}
return;
}
void
ipc_create_fifo(const char *name) {
DEBUG_PRINT("%s", name);
if (mkfifo(name, 0600) < 0) {
if (errno != EEXIST) {
util_die_notify("Error creating fifo %s: %s\n",
name, strerror(errno));
}
}
return;
}