1 | |
---|
2 | // Environment_UNIX.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/Environment_UNIX.cpp#7 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Core |
---|
8 | // Module: Environment |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include <Poco/Environment_UNIX.h> |
---|
38 | #include <Poco/Exception.h> |
---|
39 | #include <cstring> |
---|
40 | #include <unistd.h> |
---|
41 | #include <stdlib.h> |
---|
42 | #include <sys/utsname.h> |
---|
43 | #include <sys/param.h> |
---|
44 | #include <cstring> |
---|
45 | #if defined(POCO_OS_FAMILY_BSD) |
---|
46 | #include <sys/sysctl.h> |
---|
47 | #elif POCO_OS == POCO_OS_HPUX |
---|
48 | #include <pthread.h> |
---|
49 | #endif |
---|
50 | |
---|
51 | |
---|
52 | namespace Poco { |
---|
53 | |
---|
54 | |
---|
55 | EnvironmentImpl::StringMap EnvironmentImpl::_map; |
---|
56 | FastMutex EnvironmentImpl::_mutex; |
---|
57 | |
---|
58 | |
---|
59 | std::string EnvironmentImpl::getImpl(const std::string& name) |
---|
60 | { |
---|
61 | FastMutex::ScopedLock lock(_mutex); |
---|
62 | |
---|
63 | const char* val = getenv(name.c_str()); |
---|
64 | if (val) |
---|
65 | return std::string(val); |
---|
66 | else |
---|
67 | throw NotFoundException(name); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | bool EnvironmentImpl::hasImpl(const std::string& name) |
---|
72 | { |
---|
73 | FastMutex::ScopedLock lock(_mutex); |
---|
74 | |
---|
75 | return getenv(name.c_str()) != 0; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | void EnvironmentImpl::setImpl(const std::string& name, const std::string& value) |
---|
80 | { |
---|
81 | FastMutex::ScopedLock lock(_mutex); |
---|
82 | |
---|
83 | std::string var = name; |
---|
84 | var.append("="); |
---|
85 | var.append(value); |
---|
86 | _map[name] = var; |
---|
87 | if (putenv((char*) _map[name].c_str())) |
---|
88 | { |
---|
89 | std::string msg = "cannot set environment variable: "; |
---|
90 | msg.append(name); |
---|
91 | throw SystemException(msg); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | std::string EnvironmentImpl::osNameImpl() |
---|
97 | { |
---|
98 | struct utsname uts; |
---|
99 | uname(&uts); |
---|
100 | return uts.sysname; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | std::string EnvironmentImpl::osVersionImpl() |
---|
105 | { |
---|
106 | struct utsname uts; |
---|
107 | uname(&uts); |
---|
108 | return uts.release; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | std::string EnvironmentImpl::osArchitectureImpl() |
---|
113 | { |
---|
114 | struct utsname uts; |
---|
115 | uname(&uts); |
---|
116 | return uts.machine; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | std::string EnvironmentImpl::nodeNameImpl() |
---|
121 | { |
---|
122 | struct utsname uts; |
---|
123 | uname(&uts); |
---|
124 | return uts.nodename; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | unsigned EnvironmentImpl::processorCountImpl() |
---|
129 | { |
---|
130 | #if defined(POCO_OS_FAMILY_BSD) |
---|
131 | unsigned count; |
---|
132 | std::size_t size = sizeof(count); |
---|
133 | if (sysctlbyname("hw.ncpu", &count, &size, 0, 0)) |
---|
134 | return 1; |
---|
135 | else |
---|
136 | return count; |
---|
137 | #elif POCO_OS == POCO_OS_HPUX |
---|
138 | return pthread_num_processors_np(); |
---|
139 | #elif defined(_SC_NPROCESSORS_ONLN) |
---|
140 | int count = sysconf(_SC_NPROCESSORS_ONLN); |
---|
141 | if (count <= 0) count = 1; |
---|
142 | return static_cast<int>(count); |
---|
143 | #else |
---|
144 | return 1; |
---|
145 | #endif |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | } // namespace Poco |
---|
150 | |
---|
151 | |
---|
152 | // |
---|
153 | // nodeIdImpl |
---|
154 | // |
---|
155 | #if defined(POCO_OS_FAMILY_BSD) || POCO_OS == POCO_OS_QNX |
---|
156 | // |
---|
157 | // BSD variants |
---|
158 | // |
---|
159 | #include <sys/types.h> |
---|
160 | #include <sys/socket.h> |
---|
161 | #include <ifaddrs.h> |
---|
162 | #include <net/if_dl.h> |
---|
163 | |
---|
164 | |
---|
165 | namespace Poco { |
---|
166 | |
---|
167 | |
---|
168 | void EnvironmentImpl::nodeIdImpl(NodeId& id) |
---|
169 | { |
---|
170 | struct ifaddrs* ifaphead; |
---|
171 | int rc = getifaddrs(&ifaphead); |
---|
172 | if (rc) throw SystemException("cannot get network adapter list"); |
---|
173 | |
---|
174 | bool foundAdapter = false; |
---|
175 | for (struct ifaddrs* ifap = ifaphead; ifap; ifap = ifap->ifa_next) |
---|
176 | { |
---|
177 | if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_LINK) |
---|
178 | { |
---|
179 | struct sockaddr_dl* sdl = reinterpret_cast<struct sockaddr_dl*>(ifap->ifa_addr); |
---|
180 | caddr_t ap = (caddr_t) (sdl->sdl_data + sdl->sdl_nlen); |
---|
181 | int alen = sdl->sdl_alen; |
---|
182 | if (ap && alen > 0) |
---|
183 | { |
---|
184 | std::memcpy(&id, ap, sizeof(id)); |
---|
185 | foundAdapter = true; |
---|
186 | break; |
---|
187 | } |
---|
188 | } |
---|
189 | } |
---|
190 | freeifaddrs(ifaphead); |
---|
191 | if (!foundAdapter) throw SystemException("cannot determine MAC address (no suitable network adapter found)"); |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | } // namespace Poco |
---|
196 | |
---|
197 | |
---|
198 | #elif defined(__CYGWIN__) || POCO_OS == POCO_OS_LINUX |
---|
199 | // |
---|
200 | // Linux, Cygwin |
---|
201 | // |
---|
202 | #include <sys/ioctl.h> |
---|
203 | #include <sys/socket.h> |
---|
204 | #include <netinet/in.h> |
---|
205 | #include <net/if.h> |
---|
206 | #include <arpa/inet.h> |
---|
207 | #include <unistd.h> |
---|
208 | |
---|
209 | |
---|
210 | namespace Poco { |
---|
211 | |
---|
212 | |
---|
213 | void EnvironmentImpl::nodeIdImpl(NodeId& id) |
---|
214 | { |
---|
215 | struct ifreq ifr; |
---|
216 | |
---|
217 | int s = socket(PF_INET, SOCK_DGRAM, 0); |
---|
218 | if (s == -1) throw SystemException("cannot open socket"); |
---|
219 | |
---|
220 | std::strcpy(ifr.ifr_name, "eth0"); |
---|
221 | int rc = ioctl(s, SIOCGIFHWADDR, &ifr); |
---|
222 | close(s); |
---|
223 | if (rc < 0) throw SystemException("cannot get MAC address"); |
---|
224 | struct sockaddr* sa = reinterpret_cast<struct sockaddr*>(&ifr.ifr_addr); |
---|
225 | std::memcpy(&id, sa->sa_data, sizeof(id)); |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | } // namespace Poco |
---|
230 | |
---|
231 | |
---|
232 | #elif defined(POCO_OS_FAMILY_UNIX) |
---|
233 | // |
---|
234 | // General Unix |
---|
235 | // |
---|
236 | #include <sys/ioctl.h> |
---|
237 | #if defined(sun) || defined(__sun) |
---|
238 | #include <sys/sockio.h> |
---|
239 | #endif |
---|
240 | #include <sys/socket.h> |
---|
241 | #include <sys/types.h> |
---|
242 | #include <netinet/in.h> |
---|
243 | #include <net/if.h> |
---|
244 | #include <arpa/inet.h> |
---|
245 | #include <netdb.h> |
---|
246 | #include <net/if.h> |
---|
247 | #include <net/if_arp.h> |
---|
248 | #include <unistd.h> |
---|
249 | |
---|
250 | |
---|
251 | namespace Poco { |
---|
252 | |
---|
253 | |
---|
254 | void EnvironmentImpl::nodeIdImpl(NodeId& id) |
---|
255 | { |
---|
256 | char name[MAXHOSTNAMELEN]; |
---|
257 | if (gethostname(name, sizeof(name))) |
---|
258 | throw SystemException("cannot get host name"); |
---|
259 | |
---|
260 | struct hostent* pHost = gethostbyname(name); |
---|
261 | if (!pHost) throw SystemException("cannot get host IP address"); |
---|
262 | |
---|
263 | int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
---|
264 | if (s == -1) throw SystemException("cannot open socket"); |
---|
265 | |
---|
266 | struct arpreq ar; |
---|
267 | std::memset(&ar, 0, sizeof(ar)); |
---|
268 | struct sockaddr_in* pAddr = reinterpret_cast<struct sockaddr_in*>(&ar.arp_pa); |
---|
269 | pAddr->sin_family = AF_INET; |
---|
270 | std::memcpy(&pAddr->sin_addr, *pHost->h_addr_list, sizeof(struct in_addr)); |
---|
271 | int rc = ioctl(s, SIOCGARP, &ar); |
---|
272 | close(s); |
---|
273 | if (rc < 0) throw SystemException("cannot get MAC address"); |
---|
274 | std::memcpy(&id, ar.arp_ha.sa_data, sizeof(id)); |
---|
275 | } |
---|
276 | |
---|
277 | |
---|
278 | } // namespace Poco |
---|
279 | |
---|
280 | |
---|
281 | #endif |
---|