Bump for 3.6-28
[LibreOffice.git] / sal / osl / unx / util.c
blob7411e7f2a81a867a7eda6ae789c3fbaa93b86c19
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 ************************************************************************/
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>
38 #ifdef SOLARIS
39 #include <sys/sockio.h>
40 #endif
42 #include "osl/util.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 )
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;
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 OSL_TRACE( "SIOCGIFCONF: %s", 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 OSL_TRACE( "SIOCGIFFLAGS: %s", 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 OSL_TRACE( "SIOCGIFFLAGS : is LOOPBACK : %s", strerror(errno) );
156 close(so);
157 return 0;
162 * And now, the real thing: the get address
165 #if defined(SIOCGIFHWADDR) && !defined(SOLARIS)
166 ret=ioctl(so, SIOCGIFHWADDR, &ifr);
167 #else
168 ret=ioctl(so, SIOCGIFADDR, &ifr);
169 #endif
171 if (ret < 0) {
172 OSL_TRACE( "SIOCGIFADDR: %s", strerror(errno) );
173 memset(hard_addr, 0, 32);
174 close(so);
175 return ret;
178 close(so);
180 #if defined(SIOCGIFHWADDR) && !defined(SOLARIS)
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 OSL_TRACE( "SIOCGIFADDR got '00:00:00:00:00:00'" );
196 return ret;
199 return 1;
203 /*****************************************************************************/
204 /* osl_checkAddr */
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)
213 return -1;
215 return 0;
219 #if defined (SPARC)
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));
240 #endif
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
250 * "foobar"
252 processor_info_t aInfo;
253 int rc;
255 rc = processor_info(0, &aInfo);
257 if ( rc != -1 ) {
258 if ( !strcmp( "sparc", aInfo.pi_processor_type ) /* SPARCV8 */
259 || !strcmp( "i386", aInfo.pi_processor_type ) ) /* can't happen, but ... */
260 return;
261 /* we are reasonably certain to be on sparcv9/sparcv8plus or better */
262 osl_InterlockedCountSetV9(sal_True);
266 #endif /* SOLARIS */
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];
283 size_t len=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);
292 #endif /* NETBSD */
294 #endif /* SPARC */
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)
305 struct utsname name;
306 int rc;
307 rc = uname(&name);
308 if ( rc != -1 ) {
309 if ( !strcmp( "sparc", name.machine ))
310 return;
311 osl_InterlockedCountSetV9(sal_True);
314 #endif
316 #if ( defined(__GNUC__) && (defined(X86) || defined(X86_64)) )\
317 || ( defined(SOLARIS) && defined (__SUNPRO_C) && defined(__i386) )
319 /* Safe default */
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)
336 #endif
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 ) {
343 osl_isSingleCPU = 1;
346 #endif /* defined(_SC_NPROCESSORS_CONF) */
347 #endif
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */