This repository was archived by the owner on Oct 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipc-unix.c
More file actions
75 lines (61 loc) · 1.55 KB
/
ipc-unix.c
File metadata and controls
75 lines (61 loc) · 1.55 KB
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
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <unistd.h>
#include "database.h"
#include "error.h"
#include "ipc.h"
#ifndef NDEBUG
#define CLEAR_CACHED_MASTER_PWD_INTERVAL 30
#else
#define CLEAR_CACHED_MASTER_PWD_INTERVAL 180
#endif
master_pwd_cache *get_shared_memory() {
char db_path[FS_MAX_PATH_LENGTH];
bool path_ok = get_db_path(db_path);
if (!path_ok) {
return NULL;
}
key_t key = ftok(db_path, 0);
if (key < 0) {
last_error = ERR_SHARED_MEM;
return NULL;
}
int shared_block_id = shmget(key, sizeof(master_pwd_cache), 0644 | IPC_CREAT);
if (shared_block_id < 0) {
last_error = ERR_SHARED_MEM;
return NULL;
}
master_pwd_cache *result = shmat(shared_block_id, NULL, 0);
if (result < 0) {
last_error = ERR_SHARED_MEM;
return NULL;
}
return result;
}
/*+
* The master password daemon runs as a standalone process and caches the
* correctly entered master password for up three minutes, before clearing it.
*/
void run_master_password_daemon(master_pwd_cache *cache) {
// parent
pid_t pid = fork();
if (pid > 0) {
return;
}
// child from here on
pid_t sid = setsid();
if (sid < 0) {
detach_shared_memory(cache);
exit(EXIT_FAILURE);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
sleep(CLEAR_CACHED_MASTER_PWD_INTERVAL);
memset(cache->master_password, 0, PASSWD_MAX_LENGTH);
cache->password_available = false;
detach_shared_memory(cache);
exit(EXIT_SUCCESS);
}
void detach_shared_memory(master_pwd_cache *cache) { shmdt(cache); }