libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 2008-2011, 2013 Peter Miller 00004 * Written by Peter Miller <pmiller@opensource.org.au> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation; either version 3 of the License, or (at 00009 * your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include <libexplain/ac/limits.h> /* for PATH_MAX on Solaris */ 00021 #include <libexplain/ac/sys/param.h> /* for PATH_MAX except Solaris */ 00022 #include <libexplain/ac/sys/socket.h> 00023 #include <libexplain/ac/sys/stat.h> 00024 00025 #include <libexplain/buffer/fildes_to_pathname.h> 00026 #include <libexplain/buffer/sockaddr.h> 00027 #include <libexplain/fileinfo.h> 00028 #include <libexplain/string_buffer.h> 00029 00030 00031 static void 00032 explain_buffer_fildes_to_path(explain_string_buffer_t *sb, int fildes) 00033 { 00034 char path[PATH_MAX + 1]; 00035 00036 if (explain_fileinfo_self_fd_n(fildes, path, sizeof(path))) 00037 { 00038 explain_string_buffer_putc(sb, ' '); 00039 explain_string_buffer_puts_quoted(sb, path); 00040 } 00041 } 00042 00043 00044 static int 00045 explain_buffer_fildes_to_sockaddr(explain_string_buffer_t *sb, int fildes) 00046 { 00047 struct sockaddr_storage sa; 00048 socklen_t sas; 00049 00050 /* 00051 * Print the address of the local end of the connection. 00052 */ 00053 sas = sizeof(sa); 00054 if (getsockname(fildes, (struct sockaddr *)&sa, &sas) < 0) 00055 return -1; 00056 explain_string_buffer_putc(sb, ' '); 00057 explain_buffer_sockaddr(sb, (struct sockaddr *)&sa, sas); 00058 00059 /* 00060 * If available, also 00061 * print the address of the remote end of the connection. 00062 */ 00063 sas = sizeof(sa); 00064 if (getpeername(fildes, (struct sockaddr *)&sa, &sas) < 0) 00065 return 0; 00066 explain_string_buffer_puts(sb, " => "); 00067 explain_buffer_sockaddr(sb, (struct sockaddr *)&sa, sas); 00068 return 0; 00069 } 00070 00071 00072 void 00073 explain_buffer_fildes_to_pathname(explain_string_buffer_t *sb, int fildes) 00074 { 00075 struct stat st; 00076 00077 if (fstat(fildes, &st) < 0) 00078 return; 00079 switch (st.st_mode & S_IFMT) 00080 { 00081 case S_IFSOCK: 00082 if (explain_buffer_fildes_to_sockaddr(sb, fildes)) 00083 goto oops; 00084 break; 00085 00086 default: 00087 oops: 00088 explain_buffer_fildes_to_path(sb, fildes); 00089 break; 00090 } 00091 } 00092 00093 00094 /* vim: set ts=8 sw=4 et : */