1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
41 #include <sys/sockio.h>
48 /*****************************************************************************/
49 /* Static Module Functions */
50 /*****************************************************************************/
52 static int osl_getHWAddr(const char *ifname
, char* hard_addr
);
53 static int osl_checkAddr(const char* addr
);
56 /*****************************************************************************/
57 /* osl_getEthernetAddress */
58 /*****************************************************************************/
60 sal_Bool SAL_CALL
osl_getEthernetAddress( sal_uInt8
* pAddr
)
70 /** algorithm doesn't work on solaris */
81 * All we need is ... a network file descriptor.
82 * Normally, this is a very socket.
85 so
= socket(AF_INET
, SOCK_DGRAM
, 0);
89 * The first thing we have to do, get the interface configuration.
90 * It is a list of attached/configured interfaces
93 ifc
.ifc_len
= sizeof(buff
);
95 if ( ioctl(so
, SIOCGIFCONF
, &ifc
) < 0 )
97 /* fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));*/
105 * For each of the interfaces in the interface list,
106 * try to get the hardware address
110 for ( i
= ifc
.ifc_len
/ sizeof(struct ifreq
) ; --i
>= 0 ; ifr
++ )
113 nRet
= osl_getHWAddr(ifr
->ifr_name
,hard_addr
);
116 memcpy( pAddr
, hard_addr
, 6 );
126 /*****************************************************************************/
128 /*****************************************************************************/
130 static int osl_getHWAddr(const char *ifname
, char* hard_addr
)
134 int so
= socket(AF_INET
, SOCK_DGRAM
, 0);
136 strcpy(ifr
.ifr_name
, ifname
);
139 * First, get the Interface-FLAGS
142 ret
=ioctl(so
, SIOCGIFFLAGS
, &ifr
) ;
146 /* fprintf(stderr, "SIOCGIFFLAGS: %s\n", strerror(errno)); */
153 * If it is the loopback device, do not consider it any further
156 if (ifr
.ifr_flags
& IFF_LOOPBACK
)
158 /* fprintf(stderr, "SIOCGIFFLAGS : is LOOPBACK : %s\n", strerror(errno));*/
165 * And now, the real thing: the get address
169 ret
=ioctl(so
, SIOCGIFHWADDR
, &ifr
);
171 ret
=ioctl(so
, SIOCGIFADDR
, &ifr
);
175 /* fprintf(stderr, "SIOCGIFADDR: %s\n", strerror(errno));*/
176 memset(hard_addr
, 0, 32);
184 memcpy(hard_addr
,ifr
.ifr_hwaddr
.sa_data
,8);
186 memcpy(hard_addr
,ifr
.ifr_ifru
.ifru_addr
.sa_data
,8);
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
195 ret
=osl_checkAddr(hard_addr
);
198 /* fprintf(stderr, "SIOCGIFADDR got '00:00:00:00:00:00'\n"); */
202 /* fprintf(stderr,"interface : %s -- ",ifname);*/
203 /* fprintf(stderr,"HWaddr : %s\n", print_ether(hard_addr));*/
209 /*****************************************************************************/
211 /*****************************************************************************/
213 static int osl_checkAddr(const char* addr
)
215 if (addr
[0]==0 && addr
[1]==0 &&
216 addr
[2]==0 && addr
[3]==0 &&
217 addr
[4]==0 && addr
[5]==0)
227 #if defined (SOLARIS) && !defined(__sparcv8plus) && !defined(__sparcv9)
228 #include <sys/types.h>
229 #include <sys/processor.h>
231 /*****************************************************************************/
232 /* osl_InitSparcV9 */
233 /*****************************************************************************/
235 void osl_InterlockedCountSetV9(sal_Bool bV9
);
238 * osl_InitSparcV9() should be executed as early as possible. We place it in the
239 * .init section of sal
241 #if defined ( __SUNPRO_C ) || defined ( __SUNPRO_CC )
242 void osl_InitSparcV9(void);
243 #pragma init (osl_InitSparcV9)
244 #elif defined ( __GNUC__ )
245 void osl_InitSparcV9(void) __attribute__((constructor
));
248 void osl_InitSparcV9(void)
250 /* processor_info() identifies SPARCV8 (ie sun4c machines) simply as "sparc"
251 * and SPARCV9 (ie ultra sparcs, sun4u) as "sparcv9". Since we know that we
252 * run at least on a SPARCV8 architecture or better, any processor type != "sparc"
253 * and != "i386" is considered to be SPARCV9 or better
255 * This way we are certain that this will still work if someone names SPARCV10
258 processor_info_t aInfo
;
261 rc
= processor_info(0, &aInfo
);
264 if ( !strcmp( "sparc", aInfo
.pi_processor_type
) /* SPARCV8 */
265 || !strcmp( "i386", aInfo
.pi_processor_type
) ) /* can't happen, but ... */
267 /* we are reasonably certain to be on sparcv9/sparcv8plus or better */
268 osl_InterlockedCountSetV9(sal_True
);
274 #if defined(NETBSD) && defined(GCC) && !defined(__sparcv9) && !defined(__sparc_v9__)
276 #include <sys/param.h>
277 #include <sys/sysctl.h>
278 void osl_InitSparcV9(void) __attribute__((constructor
));
279 void osl_InterlockedCountSetV9(sal_Bool bV9
);
281 /* Determine which processor we are running on (sparc v8 or v9)
282 * The approach is very similar to Solaris.
285 void osl_InitSparcV9(void)
287 int mib
[2]={CTL_HW
,HW_MACHINE
};
288 char processorname
[256];
291 /* get the machine name */
292 sysctl(mib
, 2, processorname
, &len
, NULL
, 0);
293 if (!strncmp("sparc64",processorname
, len
)) {
294 osl_InterlockedCountSetV9(sal_True
);
302 #if defined ( LINUX ) && defined ( SPARC )
303 #include <sys/utsname.h>
304 void osl_InitSparcV9(void) __attribute__((constructor
));
305 void osl_InterlockedCountSetV9(sal_Bool bV9
);
306 /* Determine which processor we are running on (sparc v8 or v9)
307 * The approach is very similar to Solaris.
309 void osl_InitSparcV9(void)
315 if ( !strcmp( "sparc", name
.machine
))
317 osl_InterlockedCountSetV9(sal_True
);
322 #if ( defined(__GNUC__) && (defined(X86) || defined(X86_64)) )\
323 || ( defined(SOLARIS) && defined (__SUNPRO_C) && defined(__i386) )
326 int osl_isSingleCPU
= 0;
328 /* Determine if we are on a multiprocessor/multicore/HT x86/x64 system
330 * The lock prefix for atomic operations in osl_[inc|de]crementInterlockedCount()
331 * comes with a cost and is especially expensive on pre HT x86 single processor
332 * systems, where it isn't needed at all.
334 * This should be run as early as possible, thus it's placed in the init section
336 #if defined(_SC_NPROCESSORS_CONF) /* i.e. MACOSX for Intel doesn't have this */
337 #if defined(__GNUC__)
338 void osl_interlockedCountCheckForSingleCPU(void) __attribute__((constructor
));
339 #elif defined(__SUNPRO_C)
340 void osl_interlockedCountCheckForSingleCPU(void);
341 #pragma init (osl_interlockedCountCheckForSingleCPU)
344 void osl_interlockedCountCheckForSingleCPU(void)
346 /* In case sysconfig fails be on the safe side,
347 * consider it a multiprocessor/multicore/HT system */
348 if ( sysconf(_SC_NPROCESSORS_CONF
) == 1 ) {
352 #endif /* defined(_SC_NPROCESSORS_CONF) */