-
Notifications
You must be signed in to change notification settings - Fork 10
/
inode.c
180 lines (129 loc) · 5.34 KB
/
inode.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
/*
runfs: a self-cleaning filesystem for runtime state.
Copyright (C) 2015 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU General
Public License, 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.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#include "inode.h"
// set up a pidfile inode
// return 0 on success
// return -ENOMEM on OOM
// return negative on failure to stat the process identified
int runfs_inode_init( struct runfs_inode* inode, pid_t pid, int verify_discipline ) {
int rc = 0;
int flags = 0;
memset( inode, 0, sizeof(struct runfs_inode) );
inode->ps = pstat_new();
if( inode->ps == NULL ) {
return -ENOMEM;
}
rc = pstat( pid, inode->ps, flags );
if( rc != 0 ) {
runfs_safe_free( inode->ps );
return rc;
}
inode->verify_discipline = verify_discipline;
return 0;
}
// verify that a given process created the given file
// return 0 if not equal
// return 1 if equal
// return negative on error
int runfs_inode_is_created_by_proc( struct runfs_inode* inode, struct pstat* proc_stat, int verify_discipline ) {
struct stat sb;
struct stat inode_sb;
char bin_path[PATH_MAX+1];
char inode_path[PATH_MAX+1];
pstat_get_stat( proc_stat, &sb );
pstat_get_stat( inode->ps, &inode_sb );
pstat_get_path( proc_stat, bin_path );
pstat_get_path( inode->ps, inode_path );
if( !pstat_is_running( proc_stat ) ) {
runfs_debug("PID %d is not running\n", pstat_get_pid( proc_stat ) );
return 0;
}
if( pstat_get_pid( proc_stat ) != pstat_get_pid( inode->ps ) ) {
runfs_debug("PID mismatch: %d != %d\n", pstat_get_pid( inode->ps ), pstat_get_pid( proc_stat ) );
return 0;
}
if( verify_discipline & RUNFS_VERIFY_INODE ) {
if( pstat_is_deleted( proc_stat ) || inode_sb.st_ino != sb.st_ino ) {
runfs_debug("%d: Inode mismatch: %ld != %ld\n", pstat_get_pid( inode->ps ), inode_sb.st_ino, sb.st_ino );
return 0;
}
}
if( verify_discipline & RUNFS_VERIFY_SIZE ) {
if( pstat_is_deleted( proc_stat ) || inode_sb.st_size != sb.st_size ) {
runfs_debug("%d: Size mismatch: %jd != %jd\n", pstat_get_pid( inode->ps ), inode_sb.st_size, sb.st_size );
return 0;
}
}
if( verify_discipline & RUNFS_VERIFY_MTIME ) {
if( pstat_is_deleted( proc_stat )|| inode_sb.st_mtim.tv_sec != sb.st_mtim.tv_sec || inode_sb.st_mtim.tv_nsec != sb.st_mtim.tv_nsec ) {
runfs_debug("%d: Modtime mismatch: %ld.%ld != %ld.%ld\n", pstat_get_pid( inode->ps ), inode_sb.st_mtim.tv_sec, inode_sb.st_mtim.tv_nsec, sb.st_mtim.tv_sec, sb.st_mtim.tv_nsec );
return 0;
}
}
if( verify_discipline & RUNFS_VERIFY_PATH ) {
if( pstat_is_deleted( proc_stat ) || strcmp(bin_path, inode_path) != 0 ) {
runfs_debug("%d: Path mismatch: %s != %s\n", pstat_get_pid( inode->ps ), inode_path, bin_path );
return 0;
}
}
if( verify_discipline & RUNFS_VERIFY_STARTTIME ) {
if( pstat_get_starttime( proc_stat ) != pstat_get_starttime( inode->ps ) ) {
runfs_debug("%d: Start time mismatch: %" PRIu64 " != %" PRIu64 "\n", pstat_get_pid( inode->ps ), pstat_get_starttime( proc_stat ), pstat_get_starttime( inode->ps ) );
return 0;
}
}
return 1;
}
// verify that an inode is still valid.
// that is, there's a process with the given PID running, and it's an instance of the same program that created it.
// to speed this up, only check the hash of the process binary if the modtime has changed
// return 1 if valid
// return 0 if not valid
// return negative on error
int runfs_inode_is_valid( struct runfs_inode* inode ) {
int rc = 0;
struct pstat* ps = pstat_new();
if( ps == NULL ) {
return -ENOMEM;
}
pid_t pid = pstat_get_pid( inode->ps );
rc = pstat( pid, ps, 0 );
if( rc < 0 ) {
runfs_safe_free( ps );
runfs_error("pstat(%d) rc = %d\n", pid, rc );
return rc;
}
rc = runfs_inode_is_created_by_proc( inode, ps, inode->verify_discipline );
runfs_safe_free( ps );
if( rc < 0 ) {
runfs_error("runfs_inode_is_created_by_proc(%d) rc = %d\n", pid, rc );
return rc;
}
return rc;
}
// free a pid inode
int runfs_inode_free( struct runfs_inode* inode ) {
if( inode->contents != NULL ) {
runfs_safe_free( inode->contents );
}
if( inode->ps != NULL ) {
runfs_safe_free( inode->ps );
}
memset( inode, 0, sizeof(struct runfs_inode) );
return 0;
}