-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracelinks.c
315 lines (277 loc) · 8.01 KB
/
tracelinks.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
/*
* Include <TargetConditionals.h> to address error:
* 'TARGET_OS_IPHONE' is not defined
* https://developer.apple.com/documentation/xcode/identifying-and-addressing-framework-module-issues
*/
#ifdef __APPLE__
#include <TargetConditionals.h>
#endif
#include <err.h>
#include <getopt.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef __linux__
#include <mntent.h>
#include <sys/statfs.h>
#endif
static int debug_flag = 0;
static int absolute_flag = 0;
static int print_fstype_flag = 0;
static int keep_going_flag = 0;
/* Maximum expected number of recursive calls (not symlinks). */
#define MAXITERATIONS 1024
static struct {
const char *root;
const char *path;
} seenTuples[MAXITERATIONS];
static int seenCount;
static void usage(const int rc) {
printf("Usage: tracelinks [OPTION] PATH [PATH]...\n");
printf("Report on symbolic links encountered in path traversals.\n\n");
printf(" -a, --absolute report paths as absolute paths\n");
printf(" -k, --keep-going keep reporting on other paths after an error\n");
printf(" -f, --fstype print filesystem type for each path reported\n");
printf(" -h, --help print this help message\n");
printf(" -d, --debug print extra debugging to STDERR\n");
printf(" -v, --version print version string\n");
exit(rc);
}
static void error(const char *format, ...) {
va_list argp;
va_start(argp, format);
vwarnx(format, argp);
va_end(argp);
usage(EXIT_FAILURE);
}
/*
* Loop check: verify that we haven't seen this (root,path) tuple before.
*/
static int loopcheck(const char *root, const char *path) {
if (debug_flag)
fprintf(stderr, "loopcheck('%s', '%s')\n", root, path);
if (seenCount < 0)
return (EXIT_SUCCESS);
if (seenCount >= MAXITERATIONS) {
warnx("encountered maximum path traversals (%d),"
" disabling loop detection",
MAXITERATIONS);
seenCount = -1;
return (EXIT_SUCCESS);
}
for (int i = 0; i < seenCount; i++)
if ((strcmp(root, seenTuples[i].root) == 0) &&
(strcmp(path, seenTuples[i].path) == 0)) {
fprintf(stderr, "ERROR: loop detected in path: ");
for (int j = i; j < seenCount; j++)
fprintf(stderr, "%s%s -> ", seenTuples[j].root, seenTuples[j].path);
fprintf(stderr, "%s%s\n", root, path);
return (EXIT_FAILURE);
}
seenTuples[seenCount].root = root;
seenTuples[seenCount++].path = path;
return (EXIT_SUCCESS);
}
void print_filesystem_type(const char *path) {
#ifdef __linux__
struct statfs fsinfo;
if (statfs(path, &fsinfo) == 0) {
FILE *mntFile = setmntent("/proc/mounts", "r");
if (mntFile) {
struct mntent *mnt;
while ((mnt = getmntent(mntFile)) != NULL) {
if (strcmp(mnt->mnt_dir, path) == 0 ||
strstr(path, mnt->mnt_dir) == path) {
printf(" (%s)", mnt->mnt_type);
endmntent(mntFile);
return;
}
}
endmntent(mntFile);
}
}
printf(" (unknown)");
#elif __APPLE__
struct statfs fsinfo;
if (statfs(path, &fsinfo) == 0) {
printf(" (%s)", fsinfo.f_fstypename);
} else {
perror("statfs");
printf(" (unknown)");
}
#else
printf(" (unknown)");
#endif
}
#define print_indent(x) printf("%*s", x * 2, "")
static int tracelinks(int indent, const char *root, const char *path) {
struct stat sb;
char pathbuf[PATH_MAX];
char *pathend = pathbuf;
memset(pathbuf, 0, PATH_MAX);
if (debug_flag)
fprintf(stderr, "tracelinks('%s', '%s')\n", root, path);
int rc = loopcheck(root, path);
if (rc)
return (rc);
if (strlen(root) == 0) {
if (*path == '/') {
/* absolute path */
return (tracelinks(indent, "/", (path + 1)));
} else {
/* relative path */
if (absolute_flag) {
/* Report paths as absolute */
if (getcwd(pathbuf, PATH_MAX) == NULL) {
perror("getcwd()");
return (EXIT_FAILURE);
}
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83404
strncat(pathbuf, "/", sizeof(pathbuf) - strlen(pathbuf) - 1);
return (tracelinks(indent, pathbuf, path));
} else {
/* Report paths relative to "." */
return (tracelinks(indent, "./", path));
}
}
}
/* Recursively step through path stat()ing as we go. */
char *d = strchr(path, '/');
if (d != NULL)
*d = '\0';
pathend = stpcpy(pathend, root);
pathend = stpcpy(pathend, path);
if (debug_flag)
fprintf(stderr, "lstat('%s')\n", pathbuf);
if (lstat(pathbuf, &sb) == -1) {
perror(pathbuf);
return (EXIT_FAILURE);
}
if (S_ISLNK(sb.st_mode)) {
char linkbuf[PATH_MAX];
memset(linkbuf, 0, PATH_MAX);
ssize_t nbytes = readlink(pathbuf, linkbuf, PATH_MAX);
if (nbytes == -1) {
perror(pathbuf);
return (EXIT_FAILURE);
}
print_indent(indent++);
printf("%s -> %.*s", pathbuf, (int)nbytes, linkbuf);
if (print_fstype_flag)
print_filesystem_type(pathbuf);
printf("\n");
if (d) {
*d = '/';
stpcpy(linkbuf + nbytes, d);
}
if (*linkbuf == '/')
/* Absolute link, reset root to "" and replace path */
return (tracelinks(indent, "", linkbuf));
else
/* Relative link, replace path only */
return (tracelinks(indent, root, linkbuf));
} else {
/*
* Recurse if this is a directory and there are further
* directories remaining, otherwise just report it as
* a directory.
*/
if (d && S_ISDIR(sb.st_mode)) {
stpcpy(pathend, "/");
return (tracelinks(indent, pathbuf, d + 1));
}
print_indent(indent++);
switch (sb.st_mode & S_IFMT) {
case S_IFBLK:
printf("%s: block device", pathbuf);
break;
case S_IFCHR:
printf("%s: character device", pathbuf);
break;
case S_IFDIR:
printf("%s: directory", pathbuf);
break;
case S_IFIFO:
printf("%s: FIFO/pipe", pathbuf);
break;
case S_IFREG:
printf("%s: regular file", pathbuf);
break;
case S_IFSOCK:
printf("%s: socket", pathbuf);
break;
default:
printf("%s: unknown?", pathbuf);
}
if (print_fstype_flag)
print_filesystem_type(pathbuf);
printf("\n");
if (d) {
warnx("extra trailing characters: %s", d + 1);
return (EXIT_FAILURE);
}
}
return (EXIT_SUCCESS);
}
int main(int argc, char **argv) {
int c;
int maxrc = 0;
while (1) {
static struct option long_options[] = {{"absolute", no_argument, 0, 'a'},
{"fstype", no_argument, 0, 'f'},
{"keep-going", no_argument, 0, 'k'},
{"version", no_argument, 0, 'v'},
{"debug", no_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long(argc, argv, "afkvdh", long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c) {
case 'a':
absolute_flag = 1;
break;
case 'f':
print_fstype_flag = 1;
break;
case 'k':
keep_going_flag = 1;
break;
case 'v':
puts(VERSION);
exit(EXIT_SUCCESS);
case 'd':
debug_flag = 1;
break;
case 'h':
usage(EXIT_SUCCESS);
case '?':
/* getopt_long already printed an error message. */
usage(EXIT_FAILURE);
default:
error("?? getopt returned character code 0%o ??", c);
}
}
if (optind == argc)
error("no paths provided\n");
while (optind < argc) {
seenCount = 0; /* reset with each new path traversal */
int rc = tracelinks(0, "", argv[optind++]);
if (rc > 0 && keep_going_flag == 0)
exit(rc);
if ((argc - optind) > 0)
printf("\n");
maxrc = MAX(rc, maxrc);
}
exit(maxrc);
}