update dev300-m58
[ooovba.git] / sal / osl / unx / util.c
blob335773c4af9bc42fab6cc9e2e30d646c13b7ccbe
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: util.c,v $
10 * $Revision: 1.13 $
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 ************************************************************************/
32 #include <unistd.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <net/if.h>
40 #ifdef SOLARIS
41 #include <sys/sockio.h>
42 #endif
44 #include "osl/util.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 )
62 char buff[1024];
63 char hard_addr[64];
64 struct ifconf ifc;
65 struct ifreq *ifr;
66 int i;
67 int so;
69 #ifdef SOLARIS
70 /** algorithm doesn't work on solaris */
71 return sal_False;
72 #else
74 if ( pAddr == 0 )
76 return sal_False;
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);
94 ifc.ifc_buf = buff;
95 if ( ioctl(so, SIOCGIFCONF, &ifc) < 0 )
97 /* fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));*/
98 close(so);
99 return sal_False;
102 close(so);
105 * For each of the interfaces in the interface list,
106 * try to get the hardware address
109 ifr = ifc.ifc_req;
110 for ( i = ifc.ifc_len / sizeof(struct ifreq) ; --i >= 0 ; ifr++ )
112 int nRet=0;
113 nRet = osl_getHWAddr(ifr->ifr_name,hard_addr);
114 if ( nRet > 0 )
116 memcpy( pAddr , hard_addr, 6 );
117 return sal_True;
121 return sal_False;
122 #endif
126 /*****************************************************************************/
127 /* osl_getHWAddr */
128 /*****************************************************************************/
130 static int osl_getHWAddr(const char *ifname, char* hard_addr)
132 int ret=0;
133 struct ifreq ifr;
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) ;
144 if ( ret < 0 )
146 /* fprintf(stderr, "SIOCGIFFLAGS: %s\n", strerror(errno)); */
147 close(so);
148 return ret;
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));*/
159 close(so);
160 return 0;
165 * And now, the real thing: the get address
168 #ifdef LINUX
169 ret=ioctl(so, SIOCGIFHWADDR, &ifr);
170 #else
171 ret=ioctl(so, SIOCGIFADDR, &ifr);
172 #endif
174 if (ret < 0) {
175 /* fprintf(stderr, "SIOCGIFADDR: %s\n", strerror(errno));*/
176 memset(hard_addr, 0, 32);
177 close(so);
178 return ret;
181 close(so);
183 #ifdef LINUX
184 memcpy(hard_addr,ifr.ifr_hwaddr.sa_data,8);
185 #else
186 memcpy(hard_addr,ifr.ifr_ifru.ifru_addr.sa_data,8);
187 #endif
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);
197 if (ret < 0) {
198 /* fprintf(stderr, "SIOCGIFADDR got '00:00:00:00:00:00'\n"); */
199 return ret;
202 /* fprintf(stderr,"interface : %s -- ",ifname);*/
203 /* fprintf(stderr,"HWaddr : %s\n", print_ether(hard_addr));*/
205 return 1;
209 /*****************************************************************************/
210 /* osl_checkAddr */
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)
219 return -1;
221 return 0;
225 #if defined (SPARC)
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));
246 #endif
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
256 * "foobar"
258 processor_info_t aInfo;
259 int rc;
261 rc = processor_info(0, &aInfo);
263 if ( rc != -1 ) {
264 if ( !strcmp( "sparc", aInfo.pi_processor_type ) /* SPARCV8 */
265 || !strcmp( "i386", aInfo.pi_processor_type ) ) /* can't happen, but ... */
266 return;
267 /* we are reasonably certain to be on sparcv9/sparcv8plus or better */
268 osl_InterlockedCountSetV9(sal_True);
272 #endif /* SOLARIS */
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];
289 size_t len=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);
298 #endif /* NETBSD */
300 #endif /* SPARC */
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)
311 struct utsname name;
312 int rc;
313 rc = uname(&name);
314 if ( rc != -1 ) {
315 if ( !strcmp( "sparc", name.machine ))
316 return;
317 osl_InterlockedCountSetV9(sal_True);
320 #endif
322 #if ( defined(__GNUC__) && (defined(X86) || defined(X86_64)) )\
323 || ( defined(SOLARIS) && defined (__SUNPRO_C) && defined(__i386) )
325 /* Safe default */
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)
342 #endif
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 ) {
349 osl_isSingleCPU = 1;
352 #endif /* defined(_SC_NPROCESSORS_CONF) */
353 #endif