fuser.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * tiny fuser implementation
  3. *
  4. * Copyright 2004 Tony J. White
  5. *
  6. * May be distributed under the conditions of the
  7. * GNU Library General Public License
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #include <limits.h>
  14. #include <dirent.h>
  15. #include <signal.h>
  16. #include <sys/types.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/stat.h>
  19. #include <sys/socket.h>
  20. #include <sys/sysmacros.h>
  21. #include "busybox.h"
  22. #define FUSER_PROC_DIR "/proc"
  23. #define FUSER_MAX_LINE 255
  24. #define FUSER_OPT_MOUNT 1
  25. #define FUSER_OPT_KILL 2
  26. #define FUSER_OPT_SILENT 4
  27. #define FUSER_OPT_IP6 8
  28. #define FUSER_OPT_IP4 16
  29. typedef struct inode_list {
  30. ino_t inode;
  31. dev_t dev;
  32. struct inode_list *next;
  33. } inode_list;
  34. typedef struct pid_list {
  35. pid_t pid;
  36. struct pid_list *next;
  37. } pid_list;
  38. static int fuser_option(char *option)
  39. {
  40. int opt = 0;
  41. if(!(strlen(option))) return 0;
  42. if(option[0] != '-') return 0;
  43. ++option;
  44. while(*option != '\0') {
  45. if(*option == 'm') opt |= FUSER_OPT_MOUNT;
  46. else if(*option == 'k') opt |= FUSER_OPT_KILL;
  47. else if(*option == 's') opt |= FUSER_OPT_SILENT;
  48. else if(*option == '6') opt |= FUSER_OPT_IP6;
  49. else if(*option == '4') opt |= FUSER_OPT_IP4;
  50. else {
  51. bb_error_msg_and_die(
  52. "Unsupported option '%c'", *option);
  53. }
  54. ++option;
  55. }
  56. return opt;
  57. }
  58. static int fuser_file_to_dev_inode(const char *filename,
  59. dev_t *dev, ino_t *inode)
  60. {
  61. struct stat f_stat;
  62. if((stat(filename, &f_stat)) < 0) return 0;
  63. *inode = f_stat.st_ino;
  64. *dev = f_stat.st_dev;
  65. return 1;
  66. }
  67. static int fuser_find_socket_dev(dev_t *dev)
  68. {
  69. int fd = socket(PF_INET, SOCK_DGRAM,0);
  70. struct stat buf;
  71. if (fd >= 0 && (fstat(fd, &buf)) == 0) {
  72. *dev = buf.st_dev;
  73. close(fd);
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. static int fuser_parse_net_arg(const char *filename,
  79. const char **proto, int *port)
  80. {
  81. char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
  82. if((sscanf(filename, "%d/%4s", port, tproto)) != 2) return 0;
  83. sprintf(path, "%s/net/%s", FUSER_PROC_DIR, tproto);
  84. if((access(path, R_OK)) != 0) return 0;
  85. *proto = bb_xstrdup(tproto);
  86. return 1;
  87. }
  88. static int fuser_add_pid(pid_list *plist, pid_t pid)
  89. {
  90. pid_list *curr = NULL, *last = NULL;
  91. if(plist->pid == 0) plist->pid = pid;
  92. curr = plist;
  93. while(curr != NULL) {
  94. if(curr->pid == pid) return 1;
  95. last = curr;
  96. curr = curr->next;
  97. }
  98. curr = xmalloc(sizeof(pid_list));
  99. last->next = curr;
  100. curr->pid = pid;
  101. curr->next = NULL;
  102. return 1;
  103. }
  104. static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
  105. {
  106. inode_list *curr = NULL, *last = NULL;
  107. if(!ilist->inode && !ilist->dev) {
  108. ilist->dev = dev;
  109. ilist->inode = inode;
  110. }
  111. curr = ilist;
  112. while(curr != NULL) {
  113. if(curr->inode == inode && curr->dev == dev) return 1;
  114. last = curr;
  115. curr = curr->next;
  116. }
  117. curr = xmalloc(sizeof(inode_list));
  118. last->next = curr;
  119. curr->dev = dev;
  120. curr->inode = inode;
  121. curr->next = NULL;
  122. return 1;
  123. }
  124. static int fuser_scan_proc_net(int opts, const char *proto,
  125. int port, inode_list *ilist)
  126. {
  127. char path[sizeof(FUSER_PROC_DIR)+12], line[FUSER_MAX_LINE+1];
  128. char addr[128];
  129. ino_t tmp_inode;
  130. dev_t tmp_dev;
  131. long long uint64_inode;
  132. int tmp_port;
  133. FILE *f;
  134. if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0;
  135. sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto);
  136. if (!(f = fopen(path, "r"))) return 0;
  137. while(fgets(line, FUSER_MAX_LINE, f)) {
  138. if(sscanf(line,
  139. "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
  140. "%*x:%*x %*x %*d %*d %llu",
  141. addr, &tmp_port, &uint64_inode) == 3) {
  142. if((strlen(addr) == 8) &&
  143. (opts & FUSER_OPT_IP6)) continue;
  144. else if((strlen(addr) > 8) &&
  145. (opts & FUSER_OPT_IP4)) continue;
  146. if(tmp_port == port) {
  147. tmp_inode = uint64_inode;
  148. fuser_add_inode(ilist, tmp_dev, tmp_inode);
  149. }
  150. }
  151. }
  152. fclose(f);
  153. return 1;
  154. }
  155. static int fuser_search_dev_inode(int opts, inode_list *ilist,
  156. dev_t dev, ino_t inode)
  157. {
  158. inode_list *curr;
  159. curr = ilist;
  160. while(curr) {
  161. if((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
  162. return 1;
  163. if(curr->inode == inode && curr->dev == dev)
  164. return 1;
  165. curr = curr->next;
  166. }
  167. return 0;
  168. }
  169. static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
  170. inode_list *ilist, pid_list *plist)
  171. {
  172. FILE *file;
  173. char line[FUSER_MAX_LINE + 1];
  174. int major, minor;
  175. ino_t inode;
  176. long long uint64_inode;
  177. dev_t dev;
  178. if (!(file = fopen(fname, "r"))) return 0;
  179. while (fgets(line, FUSER_MAX_LINE, file)) {
  180. if(sscanf(line, "%*s %*s %*s %x:%x %llu",
  181. &major, &minor, &uint64_inode) != 3) continue;
  182. inode = uint64_inode;
  183. if(major == 0 && minor == 0 && inode == 0) continue;
  184. dev = makedev(major, minor);
  185. if(fuser_search_dev_inode(opts, ilist, dev, inode)) {
  186. fuser_add_pid(plist, pid);
  187. }
  188. }
  189. fclose(file);
  190. return 1;
  191. }
  192. static int fuser_scan_link(int opts, const char *lname, pid_t pid,
  193. inode_list *ilist, pid_list *plist)
  194. {
  195. ino_t inode;
  196. dev_t dev;
  197. if(!fuser_file_to_dev_inode(lname, &dev, &inode)) return 0;
  198. if(fuser_search_dev_inode(opts, ilist, dev, inode))
  199. fuser_add_pid(plist, pid);
  200. return 1;
  201. }
  202. static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid,
  203. inode_list *ilist, pid_list *plist)
  204. {
  205. DIR *d;
  206. struct dirent *de;
  207. char *lname;
  208. if((d = opendir(dname))) {
  209. while((de = readdir(d)) != NULL) {
  210. lname = concat_subpath_file(dname, de->d_name);
  211. if(lname == NULL)
  212. continue;
  213. fuser_scan_link(opts, lname, pid, ilist, plist);
  214. free(lname);
  215. }
  216. closedir(d);
  217. }
  218. else return 0;
  219. return 1;
  220. }
  221. static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
  222. {
  223. DIR *d;
  224. struct dirent *de;
  225. pid_t pid;
  226. char *dname;
  227. if(!(d = opendir(FUSER_PROC_DIR))) return 0;
  228. while((de = readdir(d)) != NULL) {
  229. pid = (pid_t)atoi(de->d_name);
  230. if(!pid) continue;
  231. dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
  232. if(chdir(dname) < 0) {
  233. free(dname);
  234. continue;
  235. }
  236. free(dname);
  237. fuser_scan_link(opts, "cwd", pid, ilist, plist);
  238. fuser_scan_link(opts, "exe", pid, ilist, plist);
  239. fuser_scan_link(opts, "root", pid, ilist, plist);
  240. fuser_scan_dir_links(opts, "fd", pid, ilist, plist);
  241. fuser_scan_dir_links(opts, "lib", pid, ilist, plist);
  242. fuser_scan_dir_links(opts, "mmap", pid, ilist, plist);
  243. fuser_scan_pid_maps(opts, "maps", pid, ilist, plist);
  244. chdir("..");
  245. }
  246. closedir(d);
  247. return 1;
  248. }
  249. static int fuser_print_pid_list(pid_list *plist)
  250. {
  251. pid_list *curr = plist;
  252. if(plist == NULL) return 0;
  253. while(curr != NULL) {
  254. if(curr->pid > 0) printf("%d ", curr->pid);
  255. curr = curr->next;
  256. }
  257. printf("\n");
  258. return 1;
  259. }
  260. static int fuser_kill_pid_list(pid_list *plist, int sig)
  261. {
  262. pid_list *curr = plist;
  263. pid_t mypid = getpid();
  264. int success = 1;
  265. if(plist == NULL) return 0;
  266. while(curr != NULL) {
  267. if(curr->pid > 0 && curr->pid != mypid) {
  268. if (kill(curr->pid, sig) != 0) {
  269. bb_perror_msg(
  270. "Could not kill pid '%d'", curr->pid);
  271. success = 0;
  272. }
  273. }
  274. curr = curr->next;
  275. }
  276. return success;
  277. }
  278. int fuser_main(int argc, char **argv)
  279. {
  280. int port, i, optn;
  281. int* fni; /* file name indexes of argv */
  282. int fnic = 0; /* file name index count */
  283. const char *proto;
  284. static int opt = 0; /* FUSER_OPT_ */
  285. dev_t dev;
  286. ino_t inode;
  287. pid_list *pids;
  288. inode_list *inodes;
  289. int killsig = SIGTERM;
  290. int success = 1;
  291. fni = xmalloc(sizeof(int));
  292. for(i=1;i<argc;i++) {
  293. optn = fuser_option(argv[i]);
  294. if(optn) opt |= optn;
  295. else if(argv[i][0] == '-') {
  296. if(!(u_signal_names(argv[i]+1, &killsig, 0)))
  297. killsig = SIGTERM;
  298. }
  299. else {
  300. fni = xrealloc(fni, sizeof(int) * (fnic+2));
  301. fni[fnic++] = i;
  302. }
  303. }
  304. if(!fnic) return 1;
  305. pids = xmalloc(sizeof(pid_list));
  306. inodes = xmalloc(sizeof(inode_list));
  307. for(i=0;i<fnic;i++) {
  308. if(fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
  309. fuser_scan_proc_net(opt, proto, port, inodes);
  310. }
  311. else {
  312. if(!fuser_file_to_dev_inode(
  313. argv[fni[i]], &dev, &inode)) {
  314. free(pids);
  315. free(inodes);
  316. bb_perror_msg_and_die(
  317. "Could not open '%s'", argv[fni[i]]);
  318. }
  319. fuser_add_inode(inodes, dev, inode);
  320. }
  321. }
  322. success = fuser_scan_proc_pids(opt, inodes, pids);
  323. /* if the first pid in the list is 0, none have been found */
  324. if(pids->pid == 0) success = 0;
  325. if(success) {
  326. if(opt & FUSER_OPT_KILL) {
  327. success = fuser_kill_pid_list(pids, killsig);
  328. }
  329. else if(!(opt & FUSER_OPT_SILENT)) {
  330. success = fuser_print_pid_list(pids);
  331. }
  332. }
  333. free(pids);
  334. free(inodes);
  335. /* return 0 on (success == 1) 1 otherwise */
  336. return (success != 1);
  337. }