OSDN Git Service

* CreateFile: drd64_libbrownie_ldsohints.[ch] (for Read & Extract LD_LIBRARY_PATH...
authorKoine Yuusuke(koinec) <koinec@users.osdn.me>
Sun, 7 Feb 2016 02:54:39 +0000 (11:54 +0900)
committerKoine Yuusuke(koinec) <koinec@users.osdn.me>
Sun, 7 Feb 2016 02:54:39 +0000 (11:54 +0900)
include/libbrownie.h
libbrownie/Makefile
libbrownie/drd64_libbrownie.h
libbrownie/drd64_libbrownie_ldsohints.c [new file with mode: 0644]
libbrownie/drd64_libbrownie_ldsohints.h [new file with mode: 0644]
libbrownie/drd64_libbrownie_main.c
libbrownie/test_libbrownie.c
libbrownie/test_libbrownie.h
libbrownie/test_libbrownie_ldsohints.c [new file with mode: 0644]

index 358f561..eaf053b 100644 (file)
@@ -49,6 +49,12 @@ Comment:
        #define LIBBROWNIE_FUNC extern
 #endif
 
+
+#define        LIBBROWNIE_LDHINTS_MAX          2   
+#define        LIBBROWNIE_LDHINTS_ELF64        0x00
+#define        LIBBROWNIE_LDHINTS_ELF32        0x01
+
+
 /* drd64_libbrownie_main.c Functions =========================*/
 LIBBROWNIE_FUNC
        int LibBrownie_Init( void );
@@ -94,6 +100,17 @@ LIBBROWNIE_FUNC
                Drd64_LibBrownie_INetAddr_SetInAddr_LocalHost(
                        struct  in_addr *p_addr );
 
+/* drd64_libbrownie_ldsohints.c Functions ====================*/
+#ifdef DRD64_SRC_LIBBROWNIE_LDSOHINTS
+       #define LIBBROWNIE_LDSOHINTS_API
+#else
+       #define LIBBROWNIE_LDSOHINTS_API        extern
+#endif
+LIBBROWNIE_LDSOHINTS_API
+       char *LibBrownie_GetLoadLibraryPath( Byte b_ldbits );
+LIBBROWNIE_LDSOHINTS_API
+       int LibBrownie_FreeLoadLibraryPath( Byte b_ldbits );
+
 
 #endif /* DRD64_HEADER_XXX */
 
index 2c93d1c..2bdced0 100644 (file)
@@ -53,9 +53,11 @@ LIBBROWNIE_TARGET = libbrownie.a
 LIBBROWNIE_OBJS = drd64_libbrownie_main.o \
                                        drd64_libbrownie_error.o \
                                        drd64_libbrownie_inetaddr.o \
-                                       drd64_libbrownie_socket.o
+                                       drd64_libbrownie_socket.o \
+                                       drd64_libbrownie_ldsohints.o
 LIBBROWNIE_HEADER = drd64_libbrownie.h \
                                        drd64_libbrownie_error.h \
+                                       drd64_libbrownie_ldsohints.h \
                                        ../include/libbrownie.h \
                                        $(SUPER_HEADER)
 
@@ -64,11 +66,13 @@ TEST_FLAGS_LINKER = -L/usr/local/lib
 TEST_TARGET = test_libbrownie
 TEST_OBJS = \
                        test_libbrownie.o \
-                       test_libbrownie_error.o
+                       test_libbrownie_error.o \
+                       test_libbrownie_ldsohints.o
 TEST_HEADER = test_libbrownie.h
 
 
 all:   $(LIBBROWNIE_TARGET) test
+build: $(LIBBROWNIE_TARGET)
 test:  $(TEST_TARGET)
                ./$(TEST_TARGET)
 
@@ -89,11 +93,15 @@ drd64_libbrownie_error.o: drd64_libbrownie_error.c $(LIBBROWNIE_HEADER)
 drd64_libbrownie_inetaddr.o: drd64_libbrownie_inetaddr.c $(LIBBROWNIE_HEADER)
        $(CC) -c -o drd64_libbrownie_inetaddr.o $(FLAGS_DEBUG) \
                                drd64_libbrownie_inetaddr.c
-
 drd64_libbrownie_socket.o: drd64_libbrownie_socket.c $(LIBBROWNIE_HEADER)
        $(CC) -c -o drd64_libbrownie_socket.o $(FLAGS_DEBUG) \
                                drd64_libbrownie_socket.c
 
+drd64_libbrownie_ldsohints.o: drd64_libbrownie_ldsohints.c $(LIBBROWNIE_HEADER)
+       $(CC) -c -o drd64_libbrownie_ldsohints.o $(FLAGS_DEBUG) \
+                               drd64_libbrownie_ldsohints.c
+
+
 test_libbrownie.o: test_libbrownie.c $(LIBBROWNIE_HEADER) $(TEST_HEADER)
        $(CC) -c -o test_libbrownie.o \
                        $(FLAGS_DEBUG) $(FLAGS_COMPILE) $(TEST_FLAGS_COMPILE) \
@@ -102,6 +110,10 @@ test_libbrownie_error.o: test_libbrownie_error.c $(LIBBROWNIE_HEADER) $(TEST_HEA
        $(CC) -c -o test_libbrownie_error.o \
                        $(FLAGS_DEBUG) $(FLAGS_COMPILE) $(TEST_FLAGS_COMPILE) \
                        test_libbrownie_error.c
+test_libbrownie_ldsohints.o: test_libbrownie_ldsohints.c $(LIBBROWNIE_HEADER) $(TEST_HEADER)
+       $(CC) -c -o test_libbrownie_ldsohints.o \
+                       $(FLAGS_DEBUG) $(FLAGS_COMPILE) $(TEST_FLAGS_COMPILE) \
+                       test_libbrownie_ldsohints.c
 
 $(TEST_TARGET): $(TEST_OBJS) $(LIBBROWNIE_OBJS) $(LIBBROWNIE_TARGET)
        $(CC) -o $(TEST_TARGET) $(FLAGS_DEBUG) $(LIBBROWNIE_OBJS) $(TEST_OBJS) $(LIBBROWNIE_TARGET) \
index 17a4845..86d5a06 100644 (file)
@@ -42,6 +42,7 @@ Comment:
 #include<string.h>
 #include<netdb.h>
 #include<unistd.h>
+#include<fcntl.h>
 #include<assert.h>
 #include<sys/types.h>
 #include<sys/socket.h>
@@ -49,6 +50,8 @@ Comment:
 #include<netinet/in.h>
 #include<netinet/tcp.h>
 #include<arpa/inet.h>
+#include<elf-hints.h>
+#include<errno.h>
 #include"../include/drd64_types.h"
 #include"../include/drd64_defines.h"
 #include"../include/drd64_error.h"
@@ -56,6 +59,7 @@ Comment:
 #include"../include/drd64_packet.h"
 #include"../include/libbrownie.h"
 #include"drd64_libbrownie_error.h"
+#include"drd64_libbrownie_ldsohints.h"
 
 
 #endif /* DRD64_HEADER_XXX */
diff --git a/libbrownie/drd64_libbrownie_ldsohints.c b/libbrownie/drd64_libbrownie_ldsohints.c
new file mode 100644 (file)
index 0000000..80d0518
--- /dev/null
@@ -0,0 +1,158 @@
+/*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
+
+                         D r . D e a m o n  6 4
+                        for INTEL64(R), AMD64(R)
+       
+   Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+    this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+OF THE POSSIBILITY OF SUCH DAMAGE.
+
+DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
+
+/* File Info -----------------------------------------------------------
+File: drd64_.c
+Function: 
+Comment: 
+----------------------------------------------------------------------*/
+
+#define        DRD64_SRC_LIBBROWNIE_LDSOHINTS
+#include"drd64_libbrownie.h"
+
+static char    *gpstr_solibpath[LIBBROWNIE_LDHINTS_MAX];
+
+
+/***********************************************************************
+***********************************************************************/
+LIBBROWNIE_LDSOHINTS_API
+char *
+       LibBrownie_GetLoadLibraryPath(
+                       Byte    b_ldbits )
+{
+       int             i_fd;
+       ssize_t t_size;
+       char    **ppstr_solibpath;
+       char    *pstr_hintsfile;
+       struct  elfhints_hdr    t_elfhints_header;
+
+       if( b_ldbits == LIBBROWNIE_LDHINTS_ELF64 )
+               { pstr_hintsfile        = LIBBROWNIE_LDHINST_HINTSFILE64; }
+       else if( b_ldbits == LIBBROWNIE_LDHINTS_ELF32 )
+               { pstr_hintsfile        = LIBBROWNIE_LDHINST_HINTSFILE32; }
+       else    {
+               return NULL;
+       }
+       ppstr_solibpath = &(gpstr_solibpath[b_ldbits]);
+       if( NULL != *ppstr_solibpath )  { return *ppstr_solibpath; }
+
+       i_fd    = open( pstr_hintsfile, O_RDONLY );
+       if( -0x01 == i_fd )     {
+               return NULL;
+       }
+
+       t_size = read( i_fd, &t_elfhints_header, sizeof( struct elfhints_hdr ) );
+       if( sizeof( struct elfhints_hdr ) != t_size )   {
+               goto    goto_LibBrownie_GetLoadLibraryPath_FileClose;
+       }
+
+       if( ELFHINTS_MAGIC != t_elfhints_header.magic ) {
+               goto    goto_LibBrownie_GetLoadLibraryPath_FileClose;
+       }
+
+       *ppstr_solibpath        = (char *)malloc( t_elfhints_header.strsize + 1 );
+       if( NULL == *ppstr_solibpath )  {
+               goto    goto_LibBrownie_GetLoadLibraryPath_FileClose;
+       }
+       memset( *ppstr_solibpath, 0x00, (t_elfhints_header.strsize + 1) );
+
+       t_size  = read( i_fd, *ppstr_solibpath, t_elfhints_header.strsize );
+       if( t_elfhints_header.strsize != t_size )       {
+               goto    goto_LibBrownie_GetLoadLibraryPath_FileClose ;
+       }
+
+goto_LibBrownie_GetLoadLibraryPath_FileClose:
+       close( i_fd );
+
+       return *ppstr_solibpath;
+}
+
+
+/***********************************************************************
+***********************************************************************/
+LIBBROWNIE_LDSOHINTS_API
+int
+       LibBrownie_FreeLoadLibraryPath(
+                       Byte    b_ldbits )
+{
+       char    **ppstr_solibpath;
+
+       if(( b_ldbits != LIBBROWNIE_LDHINTS_ELF64 )
+                       && ( b_ldbits != LIBBROWNIE_LDHINTS_ELF32 ))    {
+               return -0x01;
+       }
+
+       ppstr_solibpath = &(gpstr_solibpath[b_ldbits]);
+
+       if( NULL != *ppstr_solibpath )  {
+               free( *ppstr_solibpath );
+               *ppstr_solibpath        = NULL;
+       }
+       
+       return 0x00;
+}
+
+               
+/*----------------------------------------------------------------------
+----------------------------------------------------------------------*/
+LIBBROWNIE_LDSOHINTS_EXTERN
+int
+       LibBrownie_LdSoHints_Init(
+                       void )
+{
+       gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF64 ]     = NULL;
+       gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF32 ]     = NULL;
+
+       return 0x00;
+}
+
+
+/*----------------------------------------------------------------------
+----------------------------------------------------------------------*/
+LIBBROWNIE_LDSOHINTS_EXTERN
+void
+       LibBrownie_LdSoHints_Term(
+                       void )
+{
+       if( NULL !=  gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF64 ] )      {
+               free( gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF64 ] );
+               gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF64 ]     = NULL;
+       }
+
+       if( NULL !=  gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF32 ] )      {
+               free( gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF32 ] );
+               gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF32 ]     = NULL;
+       }
+
+       return;
+}
+
+
+/* EOF of drd64_.c ----------------------------------- */
diff --git a/libbrownie/drd64_libbrownie_ldsohints.h b/libbrownie/drd64_libbrownie_ldsohints.h
new file mode 100644 (file)
index 0000000..ca4c41f
--- /dev/null
@@ -0,0 +1,58 @@
+/*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
+
+                         D r . D e a m o n  6 4
+                        for INTEL64(R), AMD64(R)
+       
+   Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+    this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+OF THE POSSIBILITY OF SUCH DAMAGE.
+
+DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
+
+/* File Info -----------------------------------------------------------
+File: drd64_.h
+Function: Header 
+Comment: 
+----------------------------------------------------------------------*/
+
+#ifndef DRD64_HEADER_LIBBROWNIE_LDSOHINTS
+#define DRD64_HEADER_LIBBROWNIE_LDSOHINTS
+
+#ifdef DRD64_SRC_LIBBROWNIE_LDSOHINTS
+       #define LIBBROWNIE_LDSOHINTS_EXTERN
+#else
+       #define LIBBROWNIE_LDSOHINTS_EXTERN             extern
+#endif
+
+#define        LIBBROWNIE_LDHINST_HINTSFILE64  _PATH_ELF_HINTS
+#define        LIBBROWNIE_LDHINST_HINTSFILE32  "/var/run/ld-elf32.so.hints"
+
+
+LIBBROWNIE_LDSOHINTS_EXTERN
+       int LibBrownie_LdSoHints_Init( void );
+LIBBROWNIE_LDSOHINTS_EXTERN
+       void LibBrownie_LdSoHints_Term( void );
+
+
+#endif /* DRD64_HEADER_XXX */
+
+/* EOF of drd64_.h ----------------------------------- */
index 39a56a7..a6e183e 100644 (file)
@@ -48,8 +48,16 @@ int
        int             i_result;
        
        i_result        = LibBrownie_Error_InitErrorSystem();
+       if( 0x00 != i_result )  {
+               return -0x01;
+       }
+
+       i_result        = LibBrownie_LdSoHints_Init();
+       if( 0x00 != i_result )  {
+               return -0x02;
+       }
        
-       return i_result;
+       return 0x00;
 }
 
 
@@ -60,6 +68,8 @@ void
        LibBrownie_Term(
                void )
 {
+       LibBrownie_LdSoHints_Term();
+
        LibBrownie_Error_TermErrorSystem();
 
        return;
index 59ae30c..5d26e41 100644 (file)
@@ -49,6 +49,7 @@ int
        CU_initialize_registry();
 
        Test_LibBrownie_Error();
+       Test_LibBrownie_LdSoHints();
 
        CU_basic_run_tests();
        CU_cleanup_registry();
index c0de776..a801e00 100644 (file)
@@ -47,6 +47,13 @@ Comment:
 #endif
 DRD64_TEST_LIBBROWNIE_ERROR_EXTERN int Test_LibBrownie_Error( void );
 
+#ifdef DRD64_SRC_TEST_LIBBROWNIE_LDSOHINTS
+       #define DRD64_TEST_LIBBROWNIE_LDSOHINTS_EXTERN
+#else
+       #define DRD64_TEST_LIBBROWNIE_LDSOHINTS_EXTERN  extern
+#endif
+DRD64_TEST_LIBBROWNIE_LDSOHINTS_EXTERN int Test_LibBrownie_LdSoHints( void );
+
 
 
 #endif /* DRD64_HEADER_XXX */
diff --git a/libbrownie/test_libbrownie_ldsohints.c b/libbrownie/test_libbrownie_ldsohints.c
new file mode 100644 (file)
index 0000000..a10155b
--- /dev/null
@@ -0,0 +1,98 @@
+/*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
+
+                         D r . D e a m o n  6 4
+                        for INTEL64(R), AMD64(R)
+       
+   Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+    this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+OF THE POSSIBILITY OF SUCH DAMAGE.
+
+DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
+
+/* File Info -----------------------------------------------------------
+File: drd64_.c
+Function: 
+Comment: 
+----------------------------------------------------------------------*/
+
+#define        DRD64_SRC_TEST_LIBBROWNIE_LDSOHINTS
+#include"test_libbrownie.h"
+#include"drd64_libbrownie.h"
+
+/*----------------------------------------------------------------------
+----------------------------------------------------------------------*/
+void Test_LibBrownie_LdSoHints_test00_001(void)
+{
+       int             i_result;
+       char    *pstr_result;
+       
+       i_result        = LibBrownie_Init();
+       CU_ASSERT( i_result == 0x00 );
+
+       pstr_result     = LibBrownie_GetLoadLibraryPath( LIBBROWNIE_LDHINTS_ELF64 );
+       CU_ASSERT( NULL != pstr_result );
+
+       pstr_result     = LibBrownie_GetLoadLibraryPath( LIBBROWNIE_LDHINTS_ELF64 );
+       CU_ASSERT( NULL != pstr_result );
+
+       LibBrownie_FreeLoadLibraryPath( LIBBROWNIE_LDHINTS_ELF64 );
+
+       pstr_result     = LibBrownie_GetLoadLibraryPath( LIBBROWNIE_LDHINTS_ELF32 );
+       CU_ASSERT( NULL != pstr_result );
+
+       pstr_result     = LibBrownie_GetLoadLibraryPath( LIBBROWNIE_LDHINTS_ELF32 );
+       CU_ASSERT( NULL != pstr_result );
+
+       LibBrownie_FreeLoadLibraryPath( LIBBROWNIE_LDHINTS_ELF32 );
+
+       pstr_result     = LibBrownie_GetLoadLibraryPath( -1 );
+       CU_ASSERT( NULL == pstr_result );
+
+       pstr_result     = LibBrownie_GetLoadLibraryPath( 3 );
+       CU_ASSERT( NULL == pstr_result );
+
+       LibBrownie_Term();
+
+       return;
+}
+
+
+
+/*----------------------------------------------------------------------
+----------------------------------------------------------------------*/
+DRD64_TEST_LIBBROWNIE_LDSOHINTS_EXTERN
+int
+       Test_LibBrownie_LdSoHints(
+                       void )
+{
+       CU_pSuite       pt_brownie;
+       
+       pt_brownie      = CU_add_suite( "LibBrownie_LdSoHints", NULL, NULL );
+
+       CU_add_test( pt_brownie, "LibBrownie_LdSoHints_test00_001",
+                                                                       Test_LibBrownie_LdSoHints_test00_001 );
+
+       return 0x00;
+}
+
+
+/* EOF of drd64_.c ----------------------------------- */