LCOV - code coverage report
Current view: top level - sal/osl/unx - util.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 3 52 5.8 %
Date: 2012-08-25 Functions: 1 4 25.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 32 3.1 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : 
      30                 :            : #include <unistd.h>
      31                 :            : #include <errno.h>
      32                 :            : #include <stdio.h>
      33                 :            : #include <string.h>
      34                 :            : #include <sys/ioctl.h>
      35                 :            : #include <sys/socket.h>
      36                 :            : #include <net/if.h>
      37                 :            : 
      38                 :            : #ifdef SOLARIS
      39                 :            : #include <sys/sockio.h>
      40                 :            : #endif
      41                 :            : 
      42                 :            : #include "osl/util.h"
      43                 :            : #include "osl/diagnose.h"
      44                 :            : 
      45                 :            : 
      46                 :            : /*****************************************************************************/
      47                 :            : /* Static Module Functions */
      48                 :            : /*****************************************************************************/
      49                 :            : 
      50                 :            : static int   osl_getHWAddr(const char *ifname, char* hard_addr);
      51                 :            : static int   osl_checkAddr(const char* addr);
      52                 :            : 
      53                 :            : 
      54                 :            : /*****************************************************************************/
      55                 :            : /* osl_getEthernetAddress */
      56                 :            : /*****************************************************************************/
      57                 :            : 
      58                 :          0 : sal_Bool SAL_CALL osl_getEthernetAddress( sal_uInt8 * pAddr )
      59                 :            : {
      60                 :            : #ifdef SOLARIS
      61                 :            :     /** algorithm doesn't work on solaris */
      62                 :            :     return sal_False;
      63                 :            : #else
      64                 :            :     char buff[1024];
      65                 :            :     char hard_addr[64];
      66                 :            :     struct ifconf ifc;
      67                 :            :     struct ifreq *ifr;
      68                 :            :     int i;
      69                 :            :     int so;
      70                 :            : 
      71         [ #  # ]:          0 :     if ( pAddr == 0 )
      72                 :            :     {
      73                 :          0 :         return sal_False;
      74                 :            :     }
      75                 :            : 
      76                 :            : 
      77                 :            :     /*
      78                 :            :      * All we need is ... a network file descriptor.
      79                 :            :      * Normally, this is a very socket.
      80                 :            :      */
      81                 :            : 
      82                 :          0 :     so = socket(AF_INET, SOCK_DGRAM, 0);
      83                 :            : 
      84                 :            : 
      85                 :            :     /*
      86                 :            :      * The first thing we have to do, get the interface configuration.
      87                 :            :      * It is a list of attached/configured interfaces
      88                 :            :      */
      89                 :            : 
      90                 :          0 :     ifc.ifc_len = sizeof(buff);
      91                 :          0 :     ifc.ifc_buf = buff;
      92         [ #  # ]:          0 :     if ( ioctl(so, SIOCGIFCONF, &ifc) < 0 )
      93                 :            :     {
      94                 :            :         OSL_TRACE( "SIOCGIFCONF: %s", strerror(errno) );
      95                 :          0 :         close(so);
      96                 :          0 :         return sal_False;
      97                 :            :     }
      98                 :            : 
      99                 :          0 :     close(so);
     100                 :            : 
     101                 :            :     /*
     102                 :            :      *  For each of the interfaces in the interface list,
     103                 :            :      *  try to get the hardware address
     104                 :            :      */
     105                 :            : 
     106                 :          0 :     ifr = ifc.ifc_req;
     107         [ #  # ]:          0 :     for ( i = ifc.ifc_len / sizeof(struct ifreq) ; --i >= 0 ; ifr++ )
     108                 :            :     {
     109                 :          0 :         int nRet=0;
     110                 :          0 :         nRet = osl_getHWAddr(ifr->ifr_name,hard_addr);
     111         [ #  # ]:          0 :         if ( nRet  > 0 )
     112                 :            :         {
     113                 :          0 :             memcpy( pAddr , hard_addr, 6 );
     114                 :          0 :             return sal_True;
     115                 :            :         }
     116                 :            :     }
     117                 :            : 
     118                 :          0 :     return sal_False;
     119                 :            : #endif
     120                 :            : }
     121                 :            : 
     122                 :            : 
     123                 :            : /*****************************************************************************/
     124                 :            : /* osl_getHWAddr */
     125                 :            : /*****************************************************************************/
     126                 :            : 
     127                 :          0 : static int osl_getHWAddr(const char *ifname, char* hard_addr)
     128                 :            : {
     129                 :          0 :     int ret=0;
     130                 :            :     struct ifreq ifr;
     131                 :          0 :     int so = socket(AF_INET, SOCK_DGRAM, 0);
     132                 :            : 
     133         [ #  # ]:          0 :     if (strlen(ifname) >= sizeof(ifr.ifr_name))
     134                 :          0 :         return 0;
     135                 :            : 
     136                 :          0 :     strcpy(ifr.ifr_name, ifname);
     137                 :            : 
     138                 :            :     /*
     139                 :            :      *   First, get the Interface-FLAGS
     140                 :            :      */
     141                 :            : 
     142                 :          0 :     ret=ioctl(so, SIOCGIFFLAGS, &ifr) ;
     143                 :            : 
     144         [ #  # ]:          0 :     if ( ret < 0 )
     145                 :            :     {
     146                 :            :         OSL_TRACE( "SIOCGIFFLAGS: %s", strerror(errno) );
     147                 :          0 :         close(so);
     148                 :          0 :         return ret;
     149                 :            :     }
     150                 :            : 
     151                 :            : 
     152                 :            :     /*
     153                 :            :      *  If it is the loopback device, do not consider it any further
     154                 :            :      */
     155                 :            : 
     156         [ #  # ]:          0 :     if (ifr.ifr_flags & IFF_LOOPBACK)
     157                 :            :     {
     158                 :            :         OSL_TRACE( "SIOCGIFFLAGS : is LOOPBACK : %s", strerror(errno) );
     159                 :          0 :         close(so);
     160                 :          0 :         return 0;
     161                 :            :     }
     162                 :            : 
     163                 :            : 
     164                 :            :     /*
     165                 :            :      *  And now, the real thing: the get address
     166                 :            :      */
     167                 :            : 
     168                 :            : #if defined(SIOCGIFHWADDR) && !defined(SOLARIS)
     169                 :          0 :     ret=ioctl(so, SIOCGIFHWADDR, &ifr);
     170                 :            : #else
     171                 :            :     ret=ioctl(so, SIOCGIFADDR, &ifr);
     172                 :            : #endif
     173                 :            : 
     174         [ #  # ]:          0 :     if (ret < 0) {
     175                 :            :         OSL_TRACE( "SIOCGIFADDR: %s", strerror(errno) );
     176                 :          0 :         memset(hard_addr, 0, 32);
     177                 :          0 :         close(so);
     178                 :          0 :         return ret;
     179                 :            :     }
     180                 :            : 
     181                 :          0 :     close(so);
     182                 :            : 
     183                 :            : #if defined(SIOCGIFHWADDR) && !defined(SOLARIS)
     184                 :          0 :     memcpy(hard_addr,ifr.ifr_hwaddr.sa_data,8);
     185                 :            : #else
     186                 :            :     memcpy(hard_addr,ifr.ifr_ifru.ifru_addr.sa_data,8);
     187                 :            : #endif
     188                 :            : 
     189                 :            : 
     190                 :            :     /*
     191                 :            :      *  Check, if no real, i.e. 00:00:00:00:00:00, address was retrieved.
     192                 :            :      *  The Linux dummy device has this kind of behaviour
     193                 :            :      */
     194                 :            : 
     195                 :          0 :     ret=osl_checkAddr(hard_addr);
     196                 :            : 
     197         [ #  # ]:          0 :     if (ret < 0) {
     198                 :            :         OSL_TRACE( "SIOCGIFADDR got '00:00:00:00:00:00'" );
     199                 :          0 :         return ret;
     200                 :            :     }
     201                 :            : 
     202                 :          0 :     return 1;
     203                 :            : }
     204                 :            : 
     205                 :            : 
     206                 :            : /*****************************************************************************/
     207                 :            : /* osl_checkAddr */
     208                 :            : /*****************************************************************************/
     209                 :            : 
     210                 :          0 : static int osl_checkAddr(const char* addr)
     211                 :            : {
     212 [ #  # ][ #  # ]:          0 :     if (addr[0]==0 && addr[1]==0 &&
                 [ #  # ]
     213 [ #  # ][ #  # ]:          0 :         addr[2]==0 && addr[3]==0 &&
     214         [ #  # ]:          0 :         addr[4]==0 && addr[5]==0)
     215                 :            :     {
     216                 :          0 :         return -1;
     217                 :            :     }
     218                 :          0 :     return 0;
     219                 :            : }
     220                 :            : 
     221                 :            : 
     222                 :            : #if defined (SPARC)
     223                 :            : 
     224                 :            : #if defined (SOLARIS) && !defined(__sparcv8plus) && !defined(__sparcv9)
     225                 :            : #include <sys/types.h>
     226                 :            : #include <sys/processor.h>
     227                 :            : 
     228                 :            : /*****************************************************************************/
     229                 :            : /* osl_InitSparcV9 */
     230                 :            : /*****************************************************************************/
     231                 :            : 
     232                 :            : void osl_InterlockedCountSetV9(sal_Bool bV9);
     233                 :            : 
     234                 :            : /*
     235                 :            :  * osl_InitSparcV9() should be executed as early as possible. We place it in the
     236                 :            :  * .init section of sal
     237                 :            :  */
     238                 :            : #if defined ( __SUNPRO_C ) || defined ( __SUNPRO_CC )
     239                 :            : void osl_InitSparcV9(void);
     240                 :            : #pragma init (osl_InitSparcV9)
     241                 :            : #elif defined ( __GNUC__ )
     242                 :            : void osl_InitSparcV9(void)  __attribute__((constructor));
     243                 :            : #endif
     244                 :            : 
     245                 :            : void osl_InitSparcV9(void)
     246                 :            : {
     247                 :            :     /* processor_info() identifies SPARCV8 (ie sun4c machines) simply as "sparc"
     248                 :            :      * and SPARCV9 (ie ultra sparcs, sun4u) as "sparcv9". Since we know that we
     249                 :            :      * run at least on a SPARCV8 architecture or better, any processor type != "sparc"
     250                 :            :      * and != "i386" is considered to be SPARCV9 or better
     251                 :            :      *
     252                 :            :      * This way we are certain that this will still work if someone names SPARCV10
     253                 :            :      * "foobar"
     254                 :            :      */
     255                 :            :     processor_info_t aInfo;
     256                 :            :     int rc;
     257                 :            : 
     258                 :            :     rc = processor_info(0, &aInfo);
     259                 :            : 
     260                 :            :     if ( rc != -1 ) {
     261                 :            :         if ( !strcmp( "sparc", aInfo.pi_processor_type )    /* SPARCV8 */
     262                 :            :             || !strcmp( "i386", aInfo.pi_processor_type ) ) /* can't happen, but ... */
     263                 :            :             return;
     264                 :            :         /* we are reasonably certain to be on sparcv9/sparcv8plus or better */
     265                 :            :         osl_InterlockedCountSetV9(sal_True);
     266                 :            :     }
     267                 :            : }
     268                 :            : 
     269                 :            : #endif /* SOLARIS */
     270                 :            : 
     271                 :            : #if defined(NETBSD) && defined(GCC) && !defined(__sparcv9) && !defined(__sparc_v9__)
     272                 :            : 
     273                 :            : #include <sys/param.h>
     274                 :            : #include <sys/sysctl.h>
     275                 :            : void osl_InitSparcV9(void)  __attribute__((constructor));
     276                 :            : void osl_InterlockedCountSetV9(sal_Bool bV9);
     277                 :            : 
     278                 :            : /* Determine which processor we are running on (sparc v8 or v9)
     279                 :            :  * The approach is very similar to Solaris.
     280                 :            :  */
     281                 :            : 
     282                 :            : void osl_InitSparcV9(void)
     283                 :            : {
     284                 :            :     int mib[2]={CTL_HW,HW_MACHINE};
     285                 :            :     char processorname[256];
     286                 :            :     size_t len=256;
     287                 :            : 
     288                 :            :     /* get the machine name */
     289                 :            :     sysctl(mib, 2, processorname, &len, NULL, 0);
     290                 :            :     if (!strncmp("sparc64",processorname, len)) {
     291                 :            :         osl_InterlockedCountSetV9(sal_True);
     292                 :            :     }
     293                 :            : }
     294                 :            : 
     295                 :            : #endif /* NETBSD */
     296                 :            : 
     297                 :            : #endif /* SPARC */
     298                 :            : 
     299                 :            : #if defined ( LINUX ) && defined ( SPARC )
     300                 :            : #include <sys/utsname.h>
     301                 :            : void osl_InitSparcV9(void)  __attribute__((constructor));
     302                 :            : void osl_InterlockedCountSetV9(sal_Bool bV9);
     303                 :            : /* Determine which processor we are running on (sparc v8 or v9)
     304                 :            :  * The approach is very similar to Solaris.
     305                 :            :  */
     306                 :            : void osl_InitSparcV9(void)
     307                 :            : {
     308                 :            :     struct utsname name;
     309                 :            :     int rc;
     310                 :            :     rc = uname(&name);
     311                 :            :     if ( rc != -1 ) {
     312                 :            :         if ( !strcmp( "sparc", name.machine ))
     313                 :            :         return;
     314                 :            :     osl_InterlockedCountSetV9(sal_True);
     315                 :            :     }
     316                 :            : }
     317                 :            : #endif
     318                 :            : 
     319                 :            : #if    ( defined(__GNUC__) && (defined(X86) || defined(X86_64)) )\
     320                 :            :     || ( defined(SOLARIS) && defined(__i386) )
     321                 :            : 
     322                 :            : /* Safe default */
     323                 :            : int osl_isSingleCPU = 0;
     324                 :            : 
     325                 :            : /* Determine if we are on a multiprocessor/multicore/HT x86/x64 system
     326                 :            :  *
     327                 :            :  * The lock prefix for atomic operations in osl_[inc|de]crementInterlockedCount()
     328                 :            :  * comes with a cost and is especially expensive on pre HT x86 single processor
     329                 :            :  * systems, where it isn't needed at all.
     330                 :            :  *
     331                 :            :  * This should be run as early as possible, thus it's placed in the init section
     332                 :            :  */
     333                 :            : #if defined(_SC_NPROCESSORS_CONF) /* i.e. MACOSX for Intel doesn't have this */
     334                 :            : #if defined(__GNUC__)
     335                 :            : void osl_interlockedCountCheckForSingleCPU(void)  __attribute__((constructor));
     336                 :            : #elif defined(__SUNPRO_C)
     337                 :            : void osl_interlockedCountCheckForSingleCPU(void);
     338                 :            : #pragma init (osl_interlockedCountCheckForSingleCPU)
     339                 :            : #endif
     340                 :            : 
     341                 :       3013 : void osl_interlockedCountCheckForSingleCPU(void)
     342                 :            : {
     343                 :            :     /* In case sysconfig fails be on the safe side,
     344                 :            :      * consider it a multiprocessor/multicore/HT system */
     345         [ -  + ]:       3013 :     if ( sysconf(_SC_NPROCESSORS_CONF) == 1 ) {
     346                 :          0 :         osl_isSingleCPU = 1;
     347                 :            :     }
     348                 :       3013 : }
     349                 :            : #endif /* defined(_SC_NPROCESSORS_CONF) */
     350                 :            : #endif
     351                 :            : 
     352                 :            : //might be useful on other platforms, but doesn't compiler under MACOSX anyway
     353                 :            : #if defined(__GNUC__) && defined(LINUX)
     354                 :            : //force the __data_start symbol to exist in any executables that link against
     355                 :            : //libuno_sal so that dlopening of the libgcj provided libjvm.so on some
     356                 :            : //platforms where it needs that symbol will succeed. e.g. Debian mips/lenny
     357                 :            : //with gcc 4.3. With this in place the smoketest succeeds with libgcj provided
     358                 :            : //java. Quite possibly also required/helpful for s390x/s390 and maybe some
     359                 :            : //others. Without it the dlopen of libjvm.so will fail with __data_start
     360                 :            : //not found
     361                 :            : #pragma weak __data_start
     362                 :            : extern int __data_start[];
     363                 :            : #pragma weak data_start
     364                 :            : extern int data_start[];
     365                 :            : #pragma weak _end
     366                 :            : extern int _end[];
     367                 :            : static void *dummy[] __attribute__((used)) = {__data_start, data_start, _end};
     368                 :            : #endif
     369                 :            : 
     370                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10