1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
30 #include <sys/sockio.h>
34 #include "osl/diagnose.h"
37 /*****************************************************************************/
38 /* Static Module Functions */
39 /*****************************************************************************/
41 static int osl_getHWAddr(const char *ifname
, char* hard_addr
);
42 static int osl_checkAddr(const char* addr
);
45 /*****************************************************************************/
46 /* osl_getEthernetAddress */
47 /*****************************************************************************/
49 sal_Bool SAL_CALL
osl_getEthernetAddress( sal_uInt8
* pAddr
)
52 /** algorithm doesn't work on solaris */
69 * All we need is ... a network file descriptor.
70 * Normally, this is a very socket.
73 so
= socket(AF_INET
, SOCK_DGRAM
, 0);
77 * The first thing we have to do, get the interface configuration.
78 * It is a list of attached/configured interfaces
81 ifc
.ifc_len
= sizeof(buff
);
83 if ( ioctl(so
, SIOCGIFCONF
, &ifc
) < 0 )
85 OSL_TRACE( "SIOCGIFCONF: %s", strerror(errno
) );
93 * For each of the interfaces in the interface list,
94 * try to get the hardware address
98 for ( i
= ifc
.ifc_len
/ sizeof(struct ifreq
) ; --i
>= 0 ; ifr
++ )
101 nRet
= osl_getHWAddr(ifr
->ifr_name
,hard_addr
);
104 memcpy( pAddr
, hard_addr
, 6 );
114 /*****************************************************************************/
116 /*****************************************************************************/
118 static int osl_getHWAddr(const char *ifname
, char* hard_addr
)
122 int so
= socket(AF_INET
, SOCK_DGRAM
, 0);
124 if (strlen(ifname
) >= sizeof(ifr
.ifr_name
))
130 strcpy(ifr
.ifr_name
, ifname
);
133 * First, get the Interface-FLAGS
136 ret
=ioctl(so
, SIOCGIFFLAGS
, &ifr
) ;
140 OSL_TRACE( "SIOCGIFFLAGS: %s", strerror(errno
) );
147 * If it is the loopback device, do not consider it any further
150 if (ifr
.ifr_flags
& IFF_LOOPBACK
)
152 OSL_TRACE( "SIOCGIFFLAGS : is LOOPBACK : %s", strerror(errno
) );
159 * And now, the real thing: the get address
162 #if defined(SIOCGIFHWADDR) && !defined(SOLARIS)
163 ret
=ioctl(so
, SIOCGIFHWADDR
, &ifr
);
165 ret
=ioctl(so
, SIOCGIFADDR
, &ifr
);
169 OSL_TRACE( "SIOCGIFADDR: %s", strerror(errno
) );
170 memset(hard_addr
, 0, 32);
177 #if defined(SIOCGIFHWADDR) && !defined(SOLARIS)
178 memcpy(hard_addr
,ifr
.ifr_hwaddr
.sa_data
,8);
180 memcpy(hard_addr
,ifr
.ifr_ifru
.ifru_addr
.sa_data
,8);
185 * Check, if no real, i.e. 00:00:00:00:00:00, address was retrieved.
186 * The Linux dummy device has this kind of behaviour
189 ret
=osl_checkAddr(hard_addr
);
192 OSL_TRACE( "SIOCGIFADDR got '00:00:00:00:00:00'" );
200 /*****************************************************************************/
202 /*****************************************************************************/
204 static int osl_checkAddr(const char* addr
)
206 if (addr
[0]==0 && addr
[1]==0 &&
207 addr
[2]==0 && addr
[3]==0 &&
208 addr
[4]==0 && addr
[5]==0)
218 #if defined (SOLARIS) && !defined(__sparcv8plus) && !defined(__sparcv9)
219 #include <sys/types.h>
220 #include <sys/processor.h>
222 /*****************************************************************************/
223 /* osl_InitSparcV9 */
224 /*****************************************************************************/
226 void osl_InterlockedCountSetV9(sal_Bool bV9
);
229 * osl_InitSparcV9() should be executed as early as possible. We place it in the
230 * .init section of sal
232 #if defined ( __SUNPRO_C ) || defined ( __SUNPRO_CC )
233 void osl_InitSparcV9(void);
234 #pragma init (osl_InitSparcV9)
235 #elif defined ( __GNUC__ )
236 void osl_InitSparcV9(void) __attribute__((constructor
));
239 void osl_InitSparcV9(void)
241 /* processor_info() identifies SPARCV8 (ie sun4c machines) simply as "sparc"
242 * and SPARCV9 (ie ultra sparcs, sun4u) as "sparcv9". Since we know that we
243 * run at least on a SPARCV8 architecture or better, any processor type != "sparc"
244 * and != "i386" is considered to be SPARCV9 or better
246 * This way we are certain that this will still work if someone names SPARCV10
249 processor_info_t aInfo
;
252 rc
= processor_info(0, &aInfo
);
255 if ( !strcmp( "sparc", aInfo
.pi_processor_type
) /* SPARCV8 */
256 || !strcmp( "i386", aInfo
.pi_processor_type
) ) /* can't happen, but ... */
258 /* we are reasonably certain to be on sparcv9/sparcv8plus or better */
259 osl_InterlockedCountSetV9(sal_True
);
265 #if defined(NETBSD) && defined(__GNUC__) && !defined(__sparcv9) && !defined(__sparc_v9__)
267 #include <sys/param.h>
268 #include <sys/sysctl.h>
269 void osl_InitSparcV9(void) __attribute__((constructor
));
270 void osl_InterlockedCountSetV9(sal_Bool bV9
);
272 /* Determine which processor we are running on (sparc v8 or v9)
273 * The approach is very similar to Solaris.
276 void osl_InitSparcV9(void)
278 int mib
[2]={CTL_HW
,HW_MACHINE
};
279 char processorname
[256];
282 /* get the machine name */
283 sysctl(mib
, 2, processorname
, &len
, NULL
, 0);
284 if (!strncmp("sparc64",processorname
, len
)) {
285 osl_InterlockedCountSetV9(sal_True
);
293 #if defined ( LINUX ) && defined ( SPARC )
294 #include <sys/utsname.h>
295 void osl_InitSparcV9(void) __attribute__((constructor
));
296 void osl_InterlockedCountSetV9(sal_Bool bV9
);
297 /* Determine which processor we are running on (sparc v8 or v9)
298 * The approach is very similar to Solaris.
300 void osl_InitSparcV9(void)
306 if ( !strcmp( "sparc", name
.machine
))
308 osl_InterlockedCountSetV9(sal_True
);
313 #if ( defined(__GNUC__) && (defined(X86) || defined(X86_64)) )\
314 || ( defined(SOLARIS) && defined(__i386) )
317 int osl_isSingleCPU
= 0;
319 /* Determine if we are on a multiprocessor/multicore/HT x86/x64 system
321 * The lock prefix for atomic operations in osl_[inc|de]crementInterlockedCount()
322 * comes with a cost and is especially expensive on pre HT x86 single processor
323 * systems, where it isn't needed at all.
325 * This should be run as early as possible, thus it's placed in the init section
327 #if defined(_SC_NPROCESSORS_CONF) /* i.e. MACOSX for Intel doesn't have this */
328 #if defined(__GNUC__)
329 void osl_interlockedCountCheckForSingleCPU(void) __attribute__((constructor
));
330 #elif defined(__SUNPRO_C)
331 void osl_interlockedCountCheckForSingleCPU(void);
332 #pragma init (osl_interlockedCountCheckForSingleCPU)
335 void osl_interlockedCountCheckForSingleCPU(void)
337 /* In case sysconfig fails be on the safe side,
338 * consider it a multiprocessor/multicore/HT system */
339 if ( sysconf(_SC_NPROCESSORS_CONF
) == 1 ) {
343 #endif /* defined(_SC_NPROCESSORS_CONF) */
346 //might be useful on other platforms, but doesn't compiler under MACOSX anyway
347 #if defined(__GNUC__) && defined(LINUX)
348 //force the __data_start symbol to exist in any executables that link against
349 //libuno_sal so that dlopening of the libgcj provided libjvm.so on some
350 //platforms where it needs that symbol will succeed. e.g. Debian mips/lenny
351 //with gcc 4.3. With this in place the smoketest succeeds with libgcj provided
352 //java. Quite possibly also required/helpful for s390x/s390 and maybe some
353 //others. Without it the dlopen of libjvm.so will fail with __data_start
355 extern int __data_start
[] __attribute__((weak
));
356 extern int data_start
[] __attribute__((weak
));
357 extern int _end
[] __attribute__((weak
));
358 static void *dummy
[] __attribute__((used
)) = {__data_start
, data_start
, _end
};
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */