-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdir_walker.c
185 lines (159 loc) · 3.7 KB
/
dir_walker.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
/*
*
* cgroup tests - dir_walker
*
* Copyright (C) 2014 BMW Car IT GmbH.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/* Iterate over cgroups and try to read all files. */
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <ftw.h>
#include <libgen.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <limits.h>
#define BUF_SIZE 4096
#define DBG(fmt, arg...) do { \
if (debug_enabled) \
printf("%s() " fmt "\n", __FUNCTION__ , ## arg); \
} while (0)
#define handle_err(fmt, arg...) \
do { \
log_error("ERROR: %s: " fmt "\n", strerror(errno), ## arg); \
exit(EXIT_FAILURE); \
} while (0)
static int debug_enabled;
static int verbose_level;
static void log_error(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
static int read_fn(const char *fpath, const struct stat *sb, int typeflag,
struct FTW *ftwbuf)
{
int fd;
char buf[BUF_SIZE];
char *p;
ssize_t n;
size_t l, t;
if (typeflag != FTW_F)
return 0;
if (!(sb->st_mode & (S_IRUSR | S_IRGRP | S_IROTH)))
return 0;
fd = open(fpath, 0);
if (fd < 0) {
DBG("failed to open %s", fpath);
return 0;
}
l = sizeof(buf);
p = buf;
t = 0;
for (;;) {
n = read(fd, p, l);
if (n > 0) {
l -= n;
if (l < 0)
break;
p += n;
t += n;
} else if (n == 0) {
break;
} else {
DBG("failed reading %s", fpath);
break;
}
}
buf[t - 1] = '\0';
if (verbose_level) {
printf("%s\n", fpath);
printf("%s\n\n", buf);
}
close(fd);
return 0;
}
static void usage(const char *progname)
{
printf("Usage: %s [lvdh] PATH\n", progname);
printf("\t-l --loop NUMBER\t- Loop NUMBER times (-1 == forever)\n");
printf("\t-v --verbose\t\t- Print file content\n");
printf("\t-d --debug\t\t- Enable debuggin output\n");
printf("\t-h --help\t\t- Print usage (d'oh)\n");
}
static struct option long_options[] = {
{ "loop", no_argument, 0, 'l' },
{ "verbose", no_argument, 0, 'v' },
{ "debug", no_argument, 0, 'd' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 },
};
int main(int argc, char *argv[])
{
int option_index, c;
int loop = 1;
for (;;) {
c = getopt_long(argc, argv, "l:dhv",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'l':
loop = strtoul(optarg, NULL, 10);
if ((errno == ERANGE && (loop == LONG_MAX ||
loop == LONG_MIN))
|| (errno != 0 && loop == 0)) {
fprintf(stderr,
"Invalid argument for <loop>\n");
}
break;
case 'v':
verbose_level = 1;
break;
case 'd':
debug_enabled = 1;
break;
case 'h':
usage(basename(argv[0]));
exit(EXIT_SUCCESS);
break;
default:
fprintf(stderr, "unknown agrument\n");
exit(EXIT_FAILURE);
}
}
if (optind >= argc) {
fprintf(stderr, "Expected argument PATH after options\n");
exit(EXIT_FAILURE);
}
while (loop) {
if (nftw(argv[optind], read_fn, 20, FTW_DEPTH) < 0)
handle_err("%s", argv[optind]);
if (loop < 0)
continue;
loop--;
}
return 0;
}