diff options
author | "Vladimir N. Oleynik" <dzo@simtreas.ru> | 2005-10-28 16:08:47 +0000 |
---|---|---|
committer | "Vladimir N. Oleynik" <dzo@simtreas.ru> | 2005-10-28 16:08:47 +0000 |
commit | 7eb8e453484c2dc75fbbdb76348c55134041a909 (patch) | |
tree | 9f1b2405b7f27c818022a612545c603fd4e9bac9 /procps | |
parent | ea97282af1e95b9cd7ea4939bcfebd8da589f979 (diff) | |
download | busybox-w32-7eb8e453484c2dc75fbbdb76348c55134041a909.tar.gz busybox-w32-7eb8e453484c2dc75fbbdb76348c55134041a909.tar.bz2 busybox-w32-7eb8e453484c2dc75fbbdb76348c55134041a909.zip |
Please don't use if(asprintf(&t)<0) free(t); Use concat_subpath_file() for scan dir. Added losed chdir(".."). Removed warnings for 64-bit inode size. Why use memcpy and do not let? Very strange code for me
Diffstat (limited to 'procps')
-rw-r--r-- | procps/fuser.c | 55 |
1 files changed, 21 insertions, 34 deletions
diff --git a/procps/fuser.c b/procps/fuser.c index 2f53685a2..66e0988ae 100644 --- a/procps/fuser.c +++ b/procps/fuser.c | |||
@@ -7,11 +7,6 @@ | |||
7 | * GNU Library General Public License | 7 | * GNU Library General Public License |
8 | */ | 8 | */ |
9 | 9 | ||
10 | /* this define is needed for asprintf() */ | ||
11 | #ifndef _GNU_SOURCE | ||
12 | #define _GNU_SOURCE | ||
13 | #endif | ||
14 | |||
15 | #include <stdio.h> | 10 | #include <stdio.h> |
16 | #include <stdlib.h> | 11 | #include <stdlib.h> |
17 | #include <unistd.h> | 12 | #include <unistd.h> |
@@ -72,8 +67,8 @@ static int fuser_file_to_dev_inode(const char *filename, | |||
72 | { | 67 | { |
73 | struct stat f_stat; | 68 | struct stat f_stat; |
74 | if((stat(filename, &f_stat)) < 0) return 0; | 69 | if((stat(filename, &f_stat)) < 0) return 0; |
75 | memcpy(inode, &f_stat.st_ino, sizeof(ino_t)); | 70 | *inode = f_stat.st_ino; |
76 | memcpy(dev, &f_stat.st_dev, sizeof(dev_t)); | 71 | *dev = f_stat.st_dev; |
77 | return 1; | 72 | return 1; |
78 | } | 73 | } |
79 | 74 | ||
@@ -82,7 +77,7 @@ static int fuser_find_socket_dev(dev_t *dev) { | |||
82 | struct stat buf; | 77 | struct stat buf; |
83 | 78 | ||
84 | if (fd >= 0 && (fstat(fd, &buf)) == 0) { | 79 | if (fd >= 0 && (fstat(fd, &buf)) == 0) { |
85 | memcpy(dev, &buf.st_dev, sizeof(dev_t)); | 80 | *dev = buf.st_dev; |
86 | close(fd); | 81 | close(fd); |
87 | return 1; | 82 | return 1; |
88 | } | 83 | } |
@@ -92,16 +87,12 @@ static int fuser_find_socket_dev(dev_t *dev) { | |||
92 | static int fuser_parse_net_arg(const char *filename, | 87 | static int fuser_parse_net_arg(const char *filename, |
93 | const char **proto, int *port) | 88 | const char **proto, int *port) |
94 | { | 89 | { |
95 | int tport; | 90 | char path[sizeof(FUSER_PROC_DIR)+12], tproto[5]; |
96 | char path[PATH_MAX+1], tproto[5]; | ||
97 | 91 | ||
98 | if((sscanf(filename, "%d/%4s", &tport, &tproto[0])) != 2) return 0; | 92 | if((sscanf(filename, "%d/%4s", port, tproto)) != 2) return 0; |
99 | strncpy(path, FUSER_PROC_DIR, sizeof(FUSER_PROC_DIR)); | 93 | sprintf(path, "%s/net/%s", FUSER_PROC_DIR, tproto); |
100 | strncat(path, "/net/", 5); | ||
101 | strncat(path, tproto, strlen(tproto)); | ||
102 | if((access(path, R_OK)) != 0) return 0; | 94 | if((access(path, R_OK)) != 0) return 0; |
103 | *proto = bb_xstrndup(tproto, strlen(tproto)); | 95 | *proto = bb_xstrdup(tproto); |
104 | memcpy(port, &tport, sizeof(int)); | ||
105 | return 1; | 96 | return 1; |
106 | } | 97 | } |
107 | 98 | ||
@@ -148,30 +139,29 @@ static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode) | |||
148 | static int fuser_scan_proc_net(int opts, const char *proto, | 139 | static int fuser_scan_proc_net(int opts, const char *proto, |
149 | int port, inode_list *ilist) | 140 | int port, inode_list *ilist) |
150 | { | 141 | { |
151 | char path[PATH_MAX+1], line[FUSER_MAX_LINE+1]; | 142 | char path[sizeof(FUSER_PROC_DIR)+12], line[FUSER_MAX_LINE+1]; |
152 | char addr[128]; | 143 | char addr[128]; |
153 | ino_t tmp_inode; | 144 | ino_t tmp_inode; |
154 | dev_t tmp_dev; | 145 | dev_t tmp_dev; |
146 | uint64_t uint64_inode; | ||
155 | int tmp_port; | 147 | int tmp_port; |
156 | FILE *f; | 148 | FILE *f; |
157 | 149 | ||
158 | if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0; | 150 | if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0; |
159 | 151 | sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto); | |
160 | strncpy(path, FUSER_PROC_DIR, sizeof(FUSER_PROC_DIR)); | ||
161 | strncat(path, "/net/", 5); | ||
162 | strncat(path, proto, strlen(proto)); | ||
163 | 152 | ||
164 | if (!(f = fopen(path, "r"))) return 0; | 153 | if (!(f = fopen(path, "r"))) return 0; |
165 | while(fgets(line, FUSER_MAX_LINE, f)) { | 154 | while(fgets(line, FUSER_MAX_LINE, f)) { |
166 | if(sscanf(line, | 155 | if(sscanf(line, |
167 | "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x " | 156 | "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x " |
168 | "%*x:%*x %*x %*d %*d %lu", | 157 | "%*x:%*x %*x %*d %*d %llu", |
169 | &addr[0], &tmp_port, &tmp_inode) == 3) { | 158 | addr, &tmp_port, &uint64_inode) == 3) { |
170 | if((strlen(addr) == 8) && | 159 | if((strlen(addr) == 8) && |
171 | (opts & FUSER_OPT_IP6)) continue; | 160 | (opts & FUSER_OPT_IP6)) continue; |
172 | else if((strlen(addr) > 8) && | 161 | else if((strlen(addr) > 8) && |
173 | (opts & FUSER_OPT_IP4)) continue; | 162 | (opts & FUSER_OPT_IP4)) continue; |
174 | if(tmp_port == port) { | 163 | if(tmp_port == port) { |
164 | tmp_inode = uint64_inode; | ||
175 | fuser_add_inode(ilist, tmp_dev, tmp_inode); | 165 | fuser_add_inode(ilist, tmp_dev, tmp_inode); |
176 | } | 166 | } |
177 | } | 167 | } |
@@ -204,12 +194,14 @@ static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid, | |||
204 | char line[FUSER_MAX_LINE + 1]; | 194 | char line[FUSER_MAX_LINE + 1]; |
205 | int major, minor; | 195 | int major, minor; |
206 | ino_t inode; | 196 | ino_t inode; |
197 | uint64_t uint64_inode; | ||
207 | dev_t dev; | 198 | dev_t dev; |
208 | 199 | ||
209 | if (!(file = fopen(fname, "r"))) return 0; | 200 | if (!(file = fopen(fname, "r"))) return 0; |
210 | while (fgets(line, FUSER_MAX_LINE, file)) { | 201 | while (fgets(line, FUSER_MAX_LINE, file)) { |
211 | if(sscanf(line, "%*s %*s %*s %x:%x %ld", | 202 | if(sscanf(line, "%*s %*s %*s %x:%x %llu", |
212 | &major, &minor, &inode) != 3) continue; | 203 | &major, &minor, &uint64_inode) != 3) continue; |
204 | inode = uint64_inode; | ||
213 | if(major == 0 && minor == 0 && inode == 0) continue; | 205 | if(major == 0 && minor == 0 && inode == 0) continue; |
214 | dev = makedev(major, minor); | 206 | dev = makedev(major, minor); |
215 | if(fuser_search_dev_inode(opts, ilist, dev, inode)) { | 207 | if(fuser_search_dev_inode(opts, ilist, dev, inode)) { |
@@ -242,12 +234,9 @@ static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid, | |||
242 | 234 | ||
243 | if((d = opendir(dname))) { | 235 | if((d = opendir(dname))) { |
244 | while((de = readdir(d)) != NULL) { | 236 | while((de = readdir(d)) != NULL) { |
245 | if(!(strcmp(de->d_name, "."))) continue; | 237 | lname = concat_subpath_file(dname, de->d_name); |
246 | if(!(strcmp(de->d_name, ".."))) continue; | 238 | if(lname == NULL) |
247 | if(asprintf(&lname, "%s/%s", dname, de->d_name) < 0) { | ||
248 | free(lname); | ||
249 | continue; | 239 | continue; |
250 | } | ||
251 | fuser_scan_link(opts, lname, pid, ilist, plist); | 240 | fuser_scan_link(opts, lname, pid, ilist, plist); |
252 | free(lname); | 241 | free(lname); |
253 | } | 242 | } |
@@ -269,10 +258,7 @@ static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist) | |||
269 | while((de = readdir(d)) != NULL) { | 258 | while((de = readdir(d)) != NULL) { |
270 | pid = (pid_t)atoi(de->d_name); | 259 | pid = (pid_t)atoi(de->d_name); |
271 | if(!pid) continue; | 260 | if(!pid) continue; |
272 | if(asprintf(&dname, "%s/%d/", FUSER_PROC_DIR, pid) < 0) { | 261 | dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name); |
273 | free(dname); | ||
274 | continue; | ||
275 | } | ||
276 | if(chdir(dname) < 0) { | 262 | if(chdir(dname) < 0) { |
277 | free(dname); | 263 | free(dname); |
278 | continue; | 264 | continue; |
@@ -285,6 +271,7 @@ static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist) | |||
285 | fuser_scan_dir_links(opts, "lib", pid, ilist, plist); | 271 | fuser_scan_dir_links(opts, "lib", pid, ilist, plist); |
286 | fuser_scan_dir_links(opts, "mmap", pid, ilist, plist); | 272 | fuser_scan_dir_links(opts, "mmap", pid, ilist, plist); |
287 | fuser_scan_pid_maps(opts, "maps", pid, ilist, plist); | 273 | fuser_scan_pid_maps(opts, "maps", pid, ilist, plist); |
274 | chdir(".."); | ||
288 | } | 275 | } |
289 | closedir(d); | 276 | closedir(d); |
290 | return 1; | 277 | return 1; |