12 #include "trinity.h" // __unused__
13 #include "arch.h" // page_size
19 #include "constants.h"
24 static int files_added
= 0;
25 const char **fileindex
;
26 unsigned int files_in_index
= 0;
29 struct list_head list
;
33 static struct namelist
*names
= NULL
;
38 static int ignore_files(const char *path
)
41 unsigned int pathlen
, offset
= 0;
43 /* These are exact matches. */
44 const char *ignored_paths
[] = {
47 /* dangerous/noisy/annoying stuff in /proc */
48 "/proc/sysrq-trigger", "/proc/kmem", "/proc/kcore",
50 /* dangerous/noisy/annoying stuff in /dev */
51 "/dev/log", "/dev/mem", "/dev/kmsg",
55 /* Partial matches. */ //FIXME: This whole function should just use globs to pattern match.
56 const char *ignored_patterns
[] = {
58 /* dangerous/noisy/annoying per-process stuff. */
59 "coredump_filter", "make-it-fail", "oom_adj", "oom_score_adj",
63 pathlen
= strlen(path
);
65 /* First do the exact matches */
66 for (i
= 0; ignored_paths
[i
]; i
++) {
67 if (strlen(ignored_paths
[i
]) != pathlen
) {
71 if (!strcmp(path
, ignored_paths
[i
])) {
72 debugf("Skipping %s\n", path
);
77 /* Now make sure none of the patterns match the end of the pathname */
78 for (j
= 0; j
< pathlen
; j
++) {
87 for (i
= 0; ignored_patterns
[i
]; i
++) {
88 if (!strcmp(path
+ offset
, ignored_patterns
[i
])) {
89 debugf("Skipping pattern %s\n", path
);
94 /* special case to match tty* until I do globbing */
95 if (!strncmp(path
+ offset
, "tty", 3)) {
96 debugf("Skipping %s\n", path
);
102 static void add_to_namelist(const char *name
)
104 struct namelist
*newnode
;
105 struct list_head
*list
= (struct list_head
*) names
;
107 newnode
= zmalloc(sizeof(struct namelist
));
108 newnode
->name
= strdup(name
);
109 list_add_tail(&newnode
->list
, list
);
112 static int check_stat_file(const struct stat
*sb
)
115 bool set_read
= FALSE
;
116 bool set_write
= FALSE
;
118 if (S_ISLNK(sb
->st_mode
))
121 if (sb
->st_uid
== my_uid
) {
122 if (sb
->st_mode
& S_IRUSR
)
124 if (sb
->st_mode
& S_IWUSR
)
128 if (sb
->st_gid
== my_gid
) {
129 if (sb
->st_mode
& S_IRGRP
)
131 if (sb
->st_mode
& S_IWGRP
)
135 if (sb
->st_mode
& S_IROTH
)
137 if (sb
->st_mode
& S_IWOTH
)
141 if ((set_read
| set_write
) == 0)
144 if (set_read
== TRUE
)
146 if (set_write
== TRUE
)
148 if ((set_read
== TRUE
) && (set_write
== TRUE
))
151 if (S_ISDIR(sb
->st_mode
))
157 static int file_tree_callback(const char *fpath
, const struct stat
*sb
, __unused__
int typeflag
, __unused__
struct FTW
*ftwbuf
)
160 if (ignore_files(fpath
)) {
161 return FTW_SKIP_SUBTREE
;
164 // Check we can read it.
165 if (check_stat_file(sb
) == -1)
168 if (shm
->exit_reason
!= STILL_RUNNING
)
171 add_to_namelist(fpath
);
178 static void open_fds(const char *dirpath
)
180 int before
= files_added
;
181 int flags
= FTW_DEPTH
| FTW_ACTIONRETVAL
| FTW_MOUNT
;
184 /* By default, don't follow symlinks so we only get each file once.
185 * But, if we do something like -V /lib, then follow it
187 * I'm not sure about this, might remove later.
189 if (victim_path
== NULL
)
192 ret
= nftw(dirpath
, file_tree_callback
, 32, flags
);
194 if (shm
->exit_reason
!= EXIT_SIGINT
)
195 output(0, "Something went wrong during nftw(%s). (%d:%s)\n",
196 dirpath
, ret
, strerror(errno
));
200 output(0, "Added %d filenames from %s\n", files_added
- before
, dirpath
);
203 void generate_filelist(void)
206 struct list_head
*node
;
212 names
= zmalloc(sizeof(struct namelist
));
213 INIT_LIST_HEAD(&names
->list
);
215 output(1, "Generating file descriptors\n");
217 if (victim_path
!= NULL
) {
218 open_fds(victim_path
);
225 if (shm
->exit_reason
!= STILL_RUNNING
)
228 if (files_added
== 0) {
229 output(1, "Didn't add any files!!\n");
233 /* Generate an index of pointers to the filenames */
235 fileindex
= malloc(sizeof(char *) * files_added
);
237 list_for_each(node
, &names
->list
) {
238 nl
= (struct namelist
*) node
;
239 fileindex
[i
++] = nl
->name
;
244 static int open_file(void)
248 const char *filename
;
254 filename
= get_filename();
255 ret
= lstat(filename
, &sb
);
259 flags
= check_stat_file(&sb
);
263 fd
= open(filename
, flags
| O_NONBLOCK
);
265 output(2, "Couldn't open %s : %s\n", filename
, strerror(errno
));
270 case O_RDONLY
: modestr
= "read-only"; break;
271 case O_WRONLY
: modestr
= "write-only"; break;
272 case O_RDWR
: modestr
= "read-write"; break;
273 default: modestr
= "unknown"; break;
275 output(2, "fd[%i] = %s (%s)\n", fd
, filename
, modestr
);
279 void open_files(void)
281 unsigned int i
, nr_to_open
;
284 if (files_in_index
< NR_FILE_FDS
)
285 nr_to_open
= files_in_index
;
287 nr_to_open
= NR_FILE_FDS
;
289 if (fileindex
== NULL
) /* this can happen if we ctrl-c'd */
292 for (i
= 0; i
< nr_to_open
; i
++) {
295 shm
->file_fds
[i
] = fd
;
300 void close_files(void)
306 shm
->fd_lifetime
= 0;
308 // FIXME: Does this need locking? At the least, check for NULL fd's
309 for (i
= 0; i
< nr_file_fds
; i
++) {
310 fd
= shm
->file_fds
[i
];
311 shm
->file_fds
[i
] = 0;
319 const char * get_filename(void)
321 if (files_in_index
== 0) /* This can happen if we run with -n. Should we do something else ? */
324 return fileindex
[rand() % files_in_index
];