1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
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.
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).
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.
27 ************************************************************************/
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
39 #include <sys/sockio.h>
43 #include "osl/diagnose.h"
46 /*****************************************************************************/
47 /* Static Module Functions */
48 /*****************************************************************************/
50 static int osl_getHWAddr(const char *ifname
, char* hard_addr
);
51 static int osl_checkAddr(const char* addr
);
54 /*****************************************************************************/
55 /* osl_getEthernetAddress */
56 /*****************************************************************************/
58 sal_Bool SAL_CALL
osl_getEthernetAddress( sal_uInt8
* pAddr
)
61 /** algorithm doesn't work on solaris */
78 * All we need is ... a network file descriptor.
79 * Normally, this is a very socket.
82 so
= socket(AF_INET
, SOCK_DGRAM
, 0);
86 * The first thing we have to do, get the interface configuration.
87 * It is a list of attached/configured interfaces
90 ifc
.ifc_len
= sizeof(buff
);
92 if ( ioctl(so
, SIOCGIFCONF
, &ifc
) < 0 )
94 OSL_TRACE( "SIOCGIFCONF: %s", strerror(errno
) );
102 * For each of the interfaces in the interface list,
103 * try to get the hardware address
107 for ( i
= ifc
.ifc_len
/ sizeof(struct ifreq
) ; --i
>= 0 ; ifr
++ )
110 nRet
= osl_getHWAddr(ifr
->ifr_name
,hard_addr
);
113 memcpy( pAddr
, hard_addr
, 6 );
123 /*****************************************************************************/
125 /*****************************************************************************/
127 static int osl_getHWAddr(const char *ifname
, char* hard_addr
)
131 int so
= socket(AF_INET
, SOCK_DGRAM
, 0);
133 strcpy(ifr
.ifr_name
, ifname
);
136 * First, get the Interface-FLAGS
139 ret
=ioctl(so
, SIOCGIFFLAGS
, &ifr
) ;
143 OSL_TRACE( "SIOCGIFFLAGS: %s", strerror(errno
) );
150 * If it is the loopback device, do not consider it any further
153 if (ifr
.ifr_flags
& IFF_LOOPBACK
)
155 OSL_TRACE( "SIOCGIFFLAGS : is LOOPBACK : %s", strerror(errno
) );
162 * And now, the real thing: the get address
165 #if defined(SIOCGIFHWADDR) && !defined(SOLARIS)
166 ret
=ioctl(so
, SIOCGIFHWADDR
, &ifr
);
168 ret
=ioctl(so
, SIOCGIFADDR
, &ifr
);
172 OSL_TRACE( "SIOCGIFADDR: %s", strerror(errno
) );
173 memset(hard_addr
, 0, 32);
180 #if defined(SIOCGIFHWADDR) && !defined(SOLARIS)
181 memcpy(hard_addr
,ifr
.ifr_hwaddr
.sa_data
,8);
183 memcpy(hard_addr
,ifr
.ifr_ifru
.ifru_addr
.sa_data
,8);
188 * Check, if no real, i.e. 00:00:00:00:00:00, address was retrieved.
189 * The Linux dummy device has this kind of behaviour
192 ret
=osl_checkAddr(hard_addr
);
195 OSL_TRACE( "SIOCGIFADDR got '00:00:00:00:00:00'" );
203 /*****************************************************************************/
205 /*****************************************************************************/
207 static int osl_checkAddr(const char* addr
)
209 if (addr
[0]==0 && addr
[1]==0 &&
210 addr
[2]==0 && addr
[3]==0 &&
211 addr
[4]==0 && addr
[5]==0)
221 #if defined (SOLARIS) && !defined(__sparcv8plus) && !defined(__sparcv9)
222 #include <sys/types.h>
223 #include <sys/processor.h>
225 /*****************************************************************************/
226 /* osl_InitSparcV9 */
227 /*****************************************************************************/
229 void osl_InterlockedCountSetV9(sal_Bool bV9
);
232 * osl_InitSparcV9() should be executed as early as possible. We place it in the
233 * .init section of sal
235 #if defined ( __SUNPRO_C ) || defined ( __SUNPRO_CC )
236 void osl_InitSparcV9(void);
237 #pragma init (osl_InitSparcV9)
238 #elif defined ( __GNUC__ )
239 void osl_InitSparcV9(void) __attribute__((constructor
));
242 void osl_InitSparcV9(void)
244 /* processor_info() identifies SPARCV8 (ie sun4c machines) simply as "sparc"
245 * and SPARCV9 (ie ultra sparcs, sun4u) as "sparcv9". Since we know that we
246 * run at least on a SPARCV8 architecture or better, any processor type != "sparc"
247 * and != "i386" is considered to be SPARCV9 or better
249 * This way we are certain that this will still work if someone names SPARCV10
252 processor_info_t aInfo
;
255 rc
= processor_info(0, &aInfo
);
258 if ( !strcmp( "sparc", aInfo
.pi_processor_type
) /* SPARCV8 */
259 || !strcmp( "i386", aInfo
.pi_processor_type
) ) /* can't happen, but ... */
261 /* we are reasonably certain to be on sparcv9/sparcv8plus or better */
262 osl_InterlockedCountSetV9(sal_True
);
268 #if defined(NETBSD) && defined(GCC) && !defined(__sparcv9) && !defined(__sparc_v9__)
270 #include <sys/param.h>
271 #include <sys/sysctl.h>
272 void osl_InitSparcV9(void) __attribute__((constructor
));
273 void osl_InterlockedCountSetV9(sal_Bool bV9
);
275 /* Determine which processor we are running on (sparc v8 or v9)
276 * The approach is very similar to Solaris.
279 void osl_InitSparcV9(void)
281 int mib
[2]={CTL_HW
,HW_MACHINE
};
282 char processorname
[256];
285 /* get the machine name */
286 sysctl(mib
, 2, processorname
, &len
, NULL
, 0);
287 if (!strncmp("sparc64",processorname
, len
)) {
288 osl_InterlockedCountSetV9(sal_True
);
296 #if defined ( LINUX ) && defined ( SPARC )
297 #include <sys/utsname.h>
298 void osl_InitSparcV9(void) __attribute__((constructor
));
299 void osl_InterlockedCountSetV9(sal_Bool bV9
);
300 /* Determine which processor we are running on (sparc v8 or v9)
301 * The approach is very similar to Solaris.
303 void osl_InitSparcV9(void)
309 if ( !strcmp( "sparc", name
.machine
))
311 osl_InterlockedCountSetV9(sal_True
);
316 #if ( defined(__GNUC__) && (defined(X86) || defined(X86_64)) )\
317 || ( defined(SOLARIS) && defined (__SUNPRO_C) && defined(__i386) )
320 int osl_isSingleCPU
= 0;
322 /* Determine if we are on a multiprocessor/multicore/HT x86/x64 system
324 * The lock prefix for atomic operations in osl_[inc|de]crementInterlockedCount()
325 * comes with a cost and is especially expensive on pre HT x86 single processor
326 * systems, where it isn't needed at all.
328 * This should be run as early as possible, thus it's placed in the init section
330 #if defined(_SC_NPROCESSORS_CONF) /* i.e. MACOSX for Intel doesn't have this */
331 #if defined(__GNUC__)
332 void osl_interlockedCountCheckForSingleCPU(void) __attribute__((constructor
));
333 #elif defined(__SUNPRO_C)
334 void osl_interlockedCountCheckForSingleCPU(void);
335 #pragma init (osl_interlockedCountCheckForSingleCPU)
338 void osl_interlockedCountCheckForSingleCPU(void)
340 /* In case sysconfig fails be on the safe side,
341 * consider it a multiprocessor/multicore/HT system */
342 if ( sysconf(_SC_NPROCESSORS_CONF
) == 1 ) {
346 #endif /* defined(_SC_NPROCESSORS_CONF) */
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */