You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
7.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* source: fdname.c */
  2. /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
  3. /* Published under the GNU General Public License V.2, see file COPYING */
  4. /* the subroutine sockname prints the basic info about the address of a socket
  5. NOTE: it works on UNIX (kernel) file descriptors, not on libc files! */
  6. #include "config.h"
  7. #include "xioconfig.h" /* what features are enabled */
  8. #include "sysincludes.h"
  9. #include "mytypes.h"
  10. #include "compat.h"
  11. #include "error.h"
  12. #include "sycls.h"
  13. #include "sysutils.h"
  14. #include "filan.h"
  15. struct sockopt {
  16. int so;
  17. char *name;
  18. };
  19. int statname(const char *file, int fd, int filetype, FILE *outfile);
  20. int cdevname(int fd, FILE *outfile);
  21. int sockname(int fd, FILE *outfile);
  22. int unixame(int fd, FILE *outfile);
  23. int tcpname(int fd, FILE *outfile);
  24. int fdname(const char *file, int fd, FILE *outfile, const char *numform) {
  25. struct stat buf = {0};
  26. int filetype;
  27. Debug1("checking file descriptor %u", fd);
  28. if (fd >= 0) {
  29. if (Fstat(fd, &buf) < 0) {
  30. if (errno == EBADF) {
  31. Debug2("fstat(%d): %s", fd, strerror(errno));
  32. return -1;
  33. } else {
  34. Error2("fstat(%d): %s", fd, strerror(errno));
  35. }
  36. }
  37. filetype = (buf.st_mode&S_IFMT)>>12;
  38. if (numform != NULL) {
  39. fprintf(outfile, numform, fd);
  40. }
  41. return statname(file, fd, filetype, outfile);
  42. } else {
  43. if (Stat(file, &buf) < 0) {
  44. Error2("stat(\"%s\"): %s", file, strerror(errno));
  45. }
  46. filetype = (buf.st_mode&S_IFMT)>>12;
  47. return statname(file, -1, filetype, outfile);
  48. }
  49. }
  50. #if HAVE_PROC_DIR_FD
  51. static int procgetfdname(int fd, char *filepath, size_t pathsize) {
  52. static pid_t pid = -1;
  53. char procpath[PATH_MAX];
  54. int len;
  55. /* even if configure has shown that we have /proc, we must check if it
  56. exists at runtime, because we might be in a chroot environment */
  57. #if HAVE_STAT64
  58. {
  59. struct stat64 buf;
  60. if (Stat64("/proc", &buf) < 0) {
  61. return -1;
  62. }
  63. if (!S_ISDIR(buf.st_mode)) {
  64. return -1;
  65. }
  66. }
  67. #else /* !HAVE_STAT64 */
  68. {
  69. struct stat buf;
  70. if (Stat("/proc", &buf) < 0) {
  71. return -1;
  72. }
  73. if (!S_ISDIR(buf.st_mode)) {
  74. return -1;
  75. }
  76. }
  77. #endif /* !HAVE_STAT64 */
  78. if (pid < 0) pid = Getpid();
  79. snprintf(procpath, sizeof(procpath), "/proc/"F_pid"/fd/%d", pid, fd);
  80. if ((len = Readlink(procpath, filepath, pathsize-1)) < 0) {
  81. Error4("readlink(\"%s\", %p, "F_Zu"): %s",
  82. procpath, filepath, pathsize, strerror(errno));
  83. return -1;
  84. }
  85. filepath[len] = '\0';
  86. return 0;
  87. }
  88. #endif /* HAVE_PROC_DIR_FD */
  89. int statname(const char *file, int fd, int filetype, FILE *outfile) {
  90. char filepath[PATH_MAX];
  91. filepath[0] = '\0';
  92. #if HAVE_PROC_DIR_FD
  93. if (fd >= 0) {
  94. procgetfdname(fd, filepath, sizeof(filepath));
  95. if (filepath[0] == '/') {
  96. file = filepath;
  97. }
  98. }
  99. #endif /* HAVE_PROC_DIR_FD */
  100. /* now see for type specific infos */
  101. switch (filetype) {
  102. case (S_IFIFO>>12): /* 1, FIFO */
  103. fputs("pipe", outfile);
  104. if (file) fprintf(outfile, " %s", file);
  105. break;
  106. case (S_IFCHR>>12): /* 2, character device */
  107. if (cdevname(fd, outfile) == 0) {
  108. if (file) fprintf(outfile, " %s", file);
  109. }
  110. break;
  111. case (S_IFDIR>>12): /* 4, directory */
  112. fputs("dir", outfile);
  113. if (file) fprintf(outfile, " %s", file);
  114. break;
  115. case (S_IFBLK>>12): /* 6, block device */
  116. fputs("blkdev", outfile);
  117. if (file) fprintf(outfile, " %s", file);
  118. break;
  119. case (S_IFREG>>12): /* 8, regular file */
  120. fputs("file", outfile);
  121. if (file) fprintf(outfile, " %s", file);
  122. break;
  123. case (S_IFLNK>>12): /* 10, symbolic link */
  124. fputs("link", outfile);
  125. if (file) fprintf(outfile, " %s", file);
  126. break;
  127. case (S_IFSOCK>>12): /* 12, socket */
  128. #if _WITH_SOCKET
  129. if (fd >= 0) {
  130. sockname(fd, outfile);
  131. } else if (file) {
  132. fprintf(outfile, "socket %s", file);
  133. } else {
  134. fputs("socket", outfile);
  135. }
  136. #else
  137. Error("SOCKET support not compiled in");
  138. return -1;
  139. #endif /* !_WITH_SOCKET */
  140. break;
  141. }
  142. /* ioctl() */
  143. fputc('\n', outfile);
  144. return 0;
  145. }
  146. /* character device analysis */
  147. /* return -1 on error, 0 if no name was found, or 1 if it printed ttyname */
  148. int cdevname(int fd, FILE *outfile) {
  149. int ret;
  150. if ((ret = Isatty(fd)) < 0) {
  151. Error2("isatty(%d): %s", fd, strerror(errno));
  152. return -1;
  153. }
  154. if (ret > 0) {
  155. char *name;
  156. fputs("tty", outfile);
  157. if ((name = Ttyname(fd)) != NULL) {
  158. fputc(' ', outfile);
  159. fputs(name, outfile);
  160. return 1;
  161. }
  162. } else {
  163. fputs("chrdev", outfile);
  164. }
  165. return 0;
  166. }
  167. #if _WITH_SOCKET
  168. int sockname(int fd, FILE *outfile) {
  169. #define FDNAME_OPTLEN 256
  170. #define FDNAME_NAMELEN 256
  171. socklen_t optlen;
  172. int opttype;
  173. #ifdef SO_ACCEPTCONN
  174. int optacceptconn;
  175. #endif
  176. int result /*0, i*/;
  177. char namebuff[FDNAME_NAMELEN];
  178. char peerbuff[FDNAME_NAMELEN];
  179. /* in Linux these optcodes are 'enum', but on AIX they are bits! */
  180. union sockaddr_union sockname, peername; /* the longest I know of */
  181. socklen_t namelen;
  182. #if 0 && defined(SIOCGIFNAME)
  183. /*Linux struct ifreq ifc = {{{ 0 }}};*/
  184. struct ifreq ifc = {{ 0 }};
  185. #endif
  186. optlen = FDNAME_OPTLEN;
  187. Getsockopt(fd, SOL_SOCKET, SO_TYPE, &opttype, &optlen);
  188. #ifdef SO_ACCEPTCONN
  189. Getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &optacceptconn, &optlen);
  190. #endif
  191. namelen = sizeof(sockname);
  192. result = Getsockname(fd, &sockname.soa, &namelen);
  193. if (result < 0) {
  194. Error2("getsockname(%d): %s", fd, strerror(errno));
  195. return -1;
  196. }
  197. namelen = sizeof(peername);
  198. result = Getpeername(fd, (struct sockaddr *)&peername, &namelen);
  199. if (result < 0) {
  200. Error2("getpeername(%d): %s", fd, strerror(errno));
  201. }
  202. switch (sockname.soa.sa_family) {
  203. #if WITH_UNIX
  204. case AF_UNIX:
  205. fprintf(outfile, "unix%s%s %s",
  206. opttype==SOCK_DGRAM?"datagram":"",
  207. #ifdef SO_ACCEPTCONN
  208. optacceptconn?"(listening)":
  209. #endif
  210. "",
  211. sockaddr_unix_info(&sockname.un, namelen,
  212. namebuff, sizeof(namebuff)));
  213. break;
  214. #endif
  215. #if WITH_IP4
  216. case AF_INET:
  217. switch (opttype) {
  218. #if WITH_TCP
  219. case SOCK_STREAM:
  220. fprintf(outfile, "tcp%s %s %s",
  221. #ifdef SO_ACCEPTCONN
  222. optacceptconn?"(listening)":
  223. #endif
  224. "",
  225. sockaddr_inet4_info(&sockname.ip4,
  226. namebuff, sizeof(namebuff)),
  227. sockaddr_inet4_info(&peername.ip4,
  228. peerbuff, sizeof(peerbuff)));
  229. break;
  230. #endif
  231. #if WITH_UDP
  232. case SOCK_DGRAM:
  233. fprintf(outfile, "udp%s %s %s",
  234. #ifdef SO_ACCEPTCONN
  235. optacceptconn?"(listening)":
  236. #endif
  237. "",
  238. sockaddr_inet4_info(&sockname.ip4,
  239. namebuff, sizeof(namebuff)),
  240. sockaddr_inet4_info(&peername.ip4,
  241. peerbuff, sizeof(peerbuff)));
  242. break;
  243. #endif
  244. default:
  245. fprintf(outfile, "ip %s",
  246. sockaddr_inet4_info(&sockname.ip4,
  247. namebuff, sizeof(namebuff)));
  248. break;
  249. }
  250. break;
  251. #endif /* WITH_IP4 */
  252. #if WITH_IP6
  253. case AF_INET6:
  254. switch (opttype) {
  255. #if WITH_TCP
  256. case SOCK_STREAM:
  257. fprintf(outfile, "tcp6%s %s %s",
  258. #ifdef SO_ACCEPTCONN
  259. optacceptconn?"(listening)":
  260. #endif
  261. "",
  262. sockaddr_inet6_info(&sockname.ip6,
  263. namebuff, sizeof(namebuff)),
  264. sockaddr_inet6_info(&peername.ip6,
  265. peerbuff, sizeof(peerbuff)));
  266. break;
  267. #endif
  268. #if WITH_UDP
  269. case SOCK_DGRAM:
  270. fprintf(outfile, "udp6%s %s %s",
  271. #ifdef SO_ACCEPTCONN
  272. optacceptconn?"(listening)":
  273. #endif
  274. "",
  275. sockaddr_inet6_info(&sockname.ip6,
  276. namebuff, sizeof(namebuff)),
  277. sockaddr_inet6_info(&peername.ip6,
  278. peerbuff, sizeof(peerbuff)));
  279. break;
  280. #endif
  281. default:
  282. fprintf(outfile, "ip6 %s",
  283. sockaddr_inet6_info(&sockname.ip6,
  284. namebuff, sizeof(namebuff)));
  285. break;
  286. }
  287. #endif /* WITH_IP6 */
  288. default:
  289. fputs("socket", outfile);
  290. }
  291. return result;
  292. #undef FDNAME_OPTLEN
  293. #undef FDNAME_NAMELEN
  294. }
  295. #endif /* _WITH_SOCKET */