merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / osl / unx / util.c
blob5ba04db56021c98e6a40e2d1b14b4824f2238cb0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
29 #include <unistd.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <net/if.h>
37 #ifdef SOLARIS
38 #include <sys/sockio.h>
39 #endif
41 #include "osl/util.h"
45 /*****************************************************************************/
46 /* Static Module Functions */
47 /*****************************************************************************/
49 static int osl_getHWAddr(const char *ifname, char* hard_addr);
50 static int osl_checkAddr(const char* addr);
53 /*****************************************************************************/
54 /* osl_getEthernetAddress */
55 /*****************************************************************************/
57 sal_Bool SAL_CALL osl_getEthernetAddress( sal_uInt8 * pAddr )
59 char buff[1024];
60 char hard_addr[64];
61 struct ifconf ifc;
62 struct ifreq *ifr;
63 int i;
64 int so;
66 #ifdef SOLARIS
67 /** algorithm doesn't work on solaris */
68 return sal_False;
69 #else
71 if ( pAddr == 0 )
73 return sal_False;
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);
91 ifc.ifc_buf = buff;
92 if ( ioctl(so, SIOCGIFCONF, &ifc) < 0 )
94 /* fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));*/
95 close(so);
96 return sal_False;
99 close(so);
102 * For each of the interfaces in the interface list,
103 * try to get the hardware address
106 ifr = ifc.ifc_req;
107 for ( i = ifc.ifc_len / sizeof(struct ifreq) ; --i >= 0 ; ifr++ )
109 int nRet=0;
110 nRet = osl_getHWAddr(ifr->ifr_name,hard_addr);
111 if ( nRet > 0 )
113 memcpy( pAddr , hard_addr, 6 );
114 return sal_True;
118 return sal_False;
119 #endif
123 /*****************************************************************************/
124 /* osl_getHWAddr */
125 /*****************************************************************************/
127 static int osl_getHWAddr(const char *ifname, char* hard_addr)
129 int ret=0;
130 struct ifreq ifr;
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) ;
141 if ( ret < 0 )
143 /* fprintf(stderr, "SIOCGIFFLAGS: %s\n", strerror(errno)); */
144 close(so);
145 return ret;
150 * If it is the loopback device, do not consider it any further
153 if (ifr.ifr_flags & IFF_LOOPBACK)
155 /* fprintf(stderr, "SIOCGIFFLAGS : is LOOPBACK : %s\n", strerror(errno));*/
156 close(so);
157 return 0;
162 * And now, the real thing: the get address
165 #ifdef SIOCGIFHWADDR
166 ret=ioctl(so, SIOCGIFHWADDR, &ifr);
167 #else
168 ret=ioctl(so, SIOCGIFADDR, &ifr);
169 #endif
171 if (ret < 0) {
172 /* fprintf(stderr, "SIOCGIFADDR: %s\n", strerror(errno));*/
173 memset(hard_addr, 0, 32);
174 close(so);
175 return ret;
178 close(so);
180 #ifdef SIOCGIFHWADDR
181 memcpy(hard_addr,ifr.ifr_hwaddr.sa_data,8);
182 #else
183 memcpy(hard_addr,ifr.ifr_ifru.ifru_addr.sa_data,8);
184 #endif
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);
194 if (ret < 0) {
195 /* fprintf(stderr, "SIOCGIFADDR got '00:00:00:00:00:00'\n"); */
196 return ret;
199 /* fprintf(stderr,"interface : %s -- ",ifname);*/
200 /* fprintf(stderr,"HWaddr : %s\n", print_ether(hard_addr));*/
202 return 1;
206 /*****************************************************************************/
207 /* osl_checkAddr */
208 /*****************************************************************************/
210 static int osl_checkAddr(const char* addr)
212 if (addr[0]==0 && addr[1]==0 &&
213 addr[2]==0 && addr[3]==0 &&
214 addr[4]==0 && addr[5]==0)
216 return -1;
218 return 0;
222 #if defined (SPARC)
224 #if defined (SOLARIS) && !defined(__sparcv8plus) && !defined(__sparcv9)
225 #include <sys/types.h>
226 #include <sys/processor.h>
228 /*****************************************************************************/
229 /* osl_InitSparcV9 */
230 /*****************************************************************************/
232 void osl_InterlockedCountSetV9(sal_Bool bV9);
235 * osl_InitSparcV9() should be executed as early as possible. We place it in the
236 * .init section of sal
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
245 void osl_InitSparcV9(void)
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
252 * This way we are certain that this will still work if someone names SPARCV10
253 * "foobar"
255 processor_info_t aInfo;
256 int rc;
258 rc = processor_info(0, &aInfo);
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);
269 #endif /* SOLARIS */
271 #if defined(NETBSD) && defined(GCC) && !defined(__sparcv9) && !defined(__sparc_v9__)
273 #include <sys/param.h>
274 #include <sys/sysctl.h>
275 void osl_InitSparcV9(void) __attribute__((constructor));
276 void osl_InterlockedCountSetV9(sal_Bool bV9);
278 /* Determine which processor we are running on (sparc v8 or v9)
279 * The approach is very similar to Solaris.
282 void osl_InitSparcV9(void)
284 int mib[2]={CTL_HW,HW_MACHINE};
285 char processorname[256];
286 size_t len=256;
288 /* get the machine name */
289 sysctl(mib, 2, processorname, &len, NULL, 0);
290 if (!strncmp("sparc64",processorname, len)) {
291 osl_InterlockedCountSetV9(sal_True);
295 #endif /* NETBSD */
297 #endif /* SPARC */
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.
306 void osl_InitSparcV9(void)
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);
317 #endif
319 #if ( defined(__GNUC__) && (defined(X86) || defined(X86_64)) )\
320 || ( defined(SOLARIS) && defined (__SUNPRO_C) && defined(__i386) )
322 /* Safe default */
323 int osl_isSingleCPU = 0;
325 /* Determine if we are on a multiprocessor/multicore/HT x86/x64 system
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.
331 * This should be run as early as possible, thus it's placed in the init section
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
341 void osl_interlockedCountCheckForSingleCPU(void)
343 /* In case sysconfig fails be on the safe side,
344 * consider it a multiprocessor/multicore/HT system */
345 if ( sysconf(_SC_NPROCESSORS_CONF) == 1 ) {
346 osl_isSingleCPU = 1;
349 #endif /* defined(_SC_NPROCESSORS_CONF) */
350 #endif